4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
11 #include "builtin16.h"
14 #include "stackframe.h"
15 #include "selectors.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(thunk
);
21 /* List of the 16-bit callback functions. This list is used */
22 /* by the build program to generate the file if1632/callto16.S */
24 /* ### start build ### */
25 extern WORD CALLBACK
THUNK_CallTo16_word_ (FARPROC16
);
26 extern WORD CALLBACK
THUNK_CallTo16_word_l (FARPROC16
,LONG
);
27 extern LONG CALLBACK
THUNK_CallTo16_long_l (FARPROC16
,LONG
);
28 extern WORD CALLBACK
THUNK_CallTo16_word_lllw (FARPROC16
,LONG
,LONG
,LONG
,WORD
);
29 extern WORD CALLBACK
THUNK_CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
30 extern LONG CALLBACK
THUNK_CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
31 extern WORD CALLBACK
THUNK_CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
32 extern WORD CALLBACK
THUNK_CallTo16_word_w (FARPROC16
,WORD
);
33 extern WORD CALLBACK
THUNK_CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
34 extern WORD CALLBACK
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
35 extern WORD CALLBACK
THUNK_CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
36 /* ### stop build ### */
38 static THUNK
*firstThunk
= NULL
;
40 CALLOUT_TABLE Callout
= {
41 /* PeekMessageA */ NULL
,
42 /* GetMessageA */ NULL
,
43 /* SendMessageA */ NULL
,
44 /* PostMessageA */ NULL
,
45 /* TranslateMessage */ NULL
,
46 /* DispatchMessageA */ NULL
,
47 /* RedrawWindow */ NULL
,
48 /* UserSignalProc */ NULL
,
49 /* FinalUserInit16 */ NULL
,
50 /* InitThreadInput16 */ NULL
,
51 /* UserYield16) */ NULL
,
52 /* DestroyIcon32 */ NULL
,
53 /* WaitForInputIdle */ NULL
,
54 /* MsgWaitForMultipleObjects */ NULL
,
55 /* WindowFromDC */ NULL
,
56 /* MessageBoxA */ NULL
,
57 /* MessageBoxW */ NULL
60 /***********************************************************************
63 FARPROC
THUNK_Alloc( FARPROC16 func
, RELAY relay
)
69 /* NULL maps to NULL */
70 if ( !func
) return NULL
;
73 * If we got an 16-bit built-in API entry point, retrieve the Wine
74 * 32-bit handler for that API routine.
76 * NOTE: For efficiency reasons, we only check whether the selector
77 * of 'func' points to the code segment of a built-in module.
78 * It might be theoretically possible that the offset is such
79 * that 'func' does not point, in fact, to an API entry point.
80 * In this case, however, the pointer is corrupt anyway.
82 hSeg
= GlobalHandle16( SELECTOROF( func
) );
83 pModule
= NE_GetPtr( FarGetOwner16( hSeg
) );
85 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
)
86 && NE_SEG_TABLE(pModule
)[0].hSeg
== hSeg
)
88 FARPROC proc
= (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( func
))->target
;
90 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
91 SELECTOROF( func
), OFFSETOF( func
), relay
, proc
);
95 /* Otherwise, we need to alloc a thunk */
96 thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
99 thunk
->popl_eax
= 0x58;
100 thunk
->pushl_func
= 0x68;
102 thunk
->pushl_eax
= 0x50;
104 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
105 thunk
->magic
= CALLTO16_THUNK_MAGIC
;
106 thunk
->next
= firstThunk
;
110 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
111 SELECTOROF( func
), OFFSETOF( func
), relay
, thunk
);
112 return (FARPROC
)thunk
;
115 /***********************************************************************
118 void THUNK_Free( FARPROC thunk
)
120 THUNK
*t
= (THUNK
*)thunk
;
121 if ( !t
|| IsBadReadPtr( t
, sizeof(*t
) )
122 || t
->magic
!= CALLTO16_THUNK_MAGIC
)
125 if (HeapValidate( GetProcessHeap(), 0, t
))
127 THUNK
**prev
= &firstThunk
;
128 while (*prev
&& (*prev
!= t
)) prev
= &(*prev
)->next
;
132 HeapFree( GetProcessHeap(), 0, t
);
136 ERR("invalid thunk addr %p\n", thunk
);
141 /***********************************************************************
142 * THUNK_GetCalloutThunk
144 * Retrieve API entry point with given name from given module.
145 * If module is builtin, return the 32-bit entry point, otherwise
146 * create a 32->16 thunk to the 16-bit entry point, using the
150 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
152 FARPROC16 proc
= GetProcAddress16( pModule
->self
, name
);
153 if ( !proc
) return 0;
155 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
156 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
158 return (FARPROC
)THUNK_Alloc( proc
, relay
);
161 /***********************************************************************
164 void THUNK_InitCallout(void)
169 hModule
= GetModuleHandleA( "user32.dll" );
172 #define GETADDR( name ) \
173 *(FARPROC *)&Callout.name = GetProcAddress( hModule, #name )
175 GETADDR( PeekMessageA
);
176 GETADDR( GetMessageA
);
177 GETADDR( SendMessageA
);
178 GETADDR( PostMessageA
);
179 GETADDR( TranslateMessage
);
180 GETADDR( DispatchMessageA
);
181 GETADDR( RedrawWindow
);
182 GETADDR( WaitForInputIdle
);
183 GETADDR( MsgWaitForMultipleObjects
);
184 GETADDR( WindowFromDC
);
185 GETADDR( MessageBoxA
);
186 GETADDR( MessageBoxW
);
189 else WARN("no 32-bit USER\n");
191 pModule
= NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
194 #define GETADDR( var, name, thk ) \
195 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
196 (RELAY)THUNK_CallTo16_##thk )
198 GETADDR( FinalUserInit16
, "FinalUserInit", word_
);
199 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
200 GETADDR( UserYield16
, "UserYield", word_
);
201 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
202 GETADDR( UserSignalProc
, "SignalProc32", word_lllw
);
205 else WARN("no 16-bit USER\n");