4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
11 #include "builtin16.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(thunk
);
19 /* List of the 16-bit callback functions. This list is used */
20 /* by the build program to generate the file if1632/callto16.S */
22 /* ### start build ### */
23 extern WORD CALLBACK
THUNK_CallTo16_word_lllw (FARPROC16
,LONG
,LONG
,LONG
,WORD
);
24 extern WORD CALLBACK
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
25 /* ### stop build ### */
27 static THUNK
*firstThunk
= NULL
;
29 CALLOUT_TABLE Callout
= {
30 /* UserSignalProc */ NULL
,
31 /* DestroyIcon32 */ NULL
34 /***********************************************************************
37 FARPROC
THUNK_Alloc( FARPROC16 func
, RELAY relay
)
43 /* NULL maps to NULL */
44 if ( !func
) return NULL
;
47 * If we got an 16-bit built-in API entry point, retrieve the Wine
48 * 32-bit handler for that API routine.
50 * NOTE: For efficiency reasons, we only check whether the selector
51 * of 'func' points to the code segment of a built-in module.
52 * It might be theoretically possible that the offset is such
53 * that 'func' does not point, in fact, to an API entry point.
54 * In this case, however, the pointer is corrupt anyway.
56 hSeg
= GlobalHandle16( SELECTOROF( func
) );
57 pModule
= NE_GetPtr( FarGetOwner16( hSeg
) );
59 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
)
60 && NE_SEG_TABLE(pModule
)[0].hSeg
== hSeg
)
62 FARPROC proc
= (FARPROC
)((ENTRYPOINT16
*)MapSL( (SEGPTR
)func
))->target
;
64 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
65 SELECTOROF( func
), OFFSETOF( func
), relay
, proc
);
69 /* Otherwise, we need to alloc a thunk */
70 thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
73 thunk
->popl_eax
= 0x58;
74 thunk
->pushl_func
= 0x68;
76 thunk
->pushl_eax
= 0x50;
78 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
79 thunk
->magic
= CALLTO16_THUNK_MAGIC
;
80 thunk
->next
= firstThunk
;
84 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
85 SELECTOROF( func
), OFFSETOF( func
), relay
, thunk
);
86 return (FARPROC
)thunk
;
89 /***********************************************************************
92 void THUNK_Free( FARPROC thunk
)
94 THUNK
*t
= (THUNK
*)thunk
;
95 if ( !t
|| IsBadReadPtr( t
, sizeof(*t
) )
96 || t
->magic
!= CALLTO16_THUNK_MAGIC
)
99 if (HeapValidate( GetProcessHeap(), 0, t
))
101 THUNK
**prev
= &firstThunk
;
102 while (*prev
&& (*prev
!= t
)) prev
= &(*prev
)->next
;
106 HeapFree( GetProcessHeap(), 0, t
);
110 ERR("invalid thunk addr %p\n", thunk
);
115 /***********************************************************************
116 * THUNK_GetCalloutThunk
118 * Retrieve API entry point with given name from given module.
119 * If module is builtin, return the 32-bit entry point, otherwise
120 * create a 32->16 thunk to the 16-bit entry point, using the
124 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
126 FARPROC16 proc
= GetProcAddress16( pModule
->self
, name
);
127 if ( !proc
) return 0;
129 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
130 return (FARPROC
)((ENTRYPOINT16
*)MapSL( (SEGPTR
)proc
))->target
;
132 return (FARPROC
)THUNK_Alloc( proc
, relay
);
135 /***********************************************************************
138 void THUNK_InitCallout(void)
140 NE_MODULE
*pModule
= NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
143 #define GETADDR( var, name, thk ) \
144 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
145 (RELAY)THUNK_CallTo16_##thk )
147 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
148 GETADDR( UserSignalProc
, "SignalProc32", word_lllw
);
151 else WARN("no 16-bit USER\n");