4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
11 #include "builtin16.h"
15 #include "stackframe.h"
16 #include "selectors.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(thunk
);
22 /* List of the 16-bit callback functions. This list is used */
23 /* by the build program to generate the file if1632/callto16.S */
25 /* ### start build ### */
26 extern WORD CALLBACK
THUNK_CallTo16_word_ (FARPROC16
);
27 extern WORD CALLBACK
THUNK_CallTo16_word_l (FARPROC16
,LONG
);
28 extern LONG CALLBACK
THUNK_CallTo16_long_l (FARPROC16
,LONG
);
29 extern WORD CALLBACK
THUNK_CallTo16_word_lllw (FARPROC16
,LONG
,LONG
,LONG
,WORD
);
30 extern WORD CALLBACK
THUNK_CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
31 extern LONG CALLBACK
THUNK_CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
32 extern WORD CALLBACK
THUNK_CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
33 extern WORD CALLBACK
THUNK_CallTo16_word_w (FARPROC16
,WORD
);
34 extern WORD CALLBACK
THUNK_CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
35 extern WORD CALLBACK
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
36 extern WORD CALLBACK
THUNK_CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
37 /* ### stop build ### */
39 static THUNK
*firstThunk
= NULL
;
41 CALLOUT_TABLE Callout
= {
42 /* PeekMessageA */ NULL
,
43 /* GetMessageA */ NULL
,
44 /* SendMessageA */ NULL
,
45 /* PostMessageA */ NULL
,
46 /* TranslateMessage */ NULL
,
47 /* DispatchMessageA */ NULL
,
48 /* RedrawWindow */ NULL
,
49 /* UserSignalProc */ NULL
,
50 /* FinalUserInit16 */ NULL
,
51 /* InitThreadInput16 */ NULL
,
52 /* UserYield16) */ NULL
,
53 /* DestroyIcon32 */ NULL
,
54 /* WaitForInputIdle */ NULL
,
55 /* MsgWaitForMultipleObjects */ NULL
,
56 /* WindowFromDC */ NULL
,
57 /* MessageBoxA */ NULL
,
58 /* MessageBoxW */ NULL
61 /***********************************************************************
64 FARPROC
THUNK_Alloc( FARPROC16 func
, RELAY relay
)
70 /* NULL maps to NULL */
71 if ( !func
) return NULL
;
74 * If we got an 16-bit built-in API entry point, retrieve the Wine
75 * 32-bit handler for that API routine.
77 * NOTE: For efficiency reasons, we only check whether the selector
78 * of 'func' points to the code segment of a built-in module.
79 * It might be theoretically possible that the offset is such
80 * that 'func' does not point, in fact, to an API entry point.
81 * In this case, however, the pointer is corrupt anyway.
83 hSeg
= GlobalHandle16( SELECTOROF( func
) );
84 pModule
= NE_GetPtr( FarGetOwner16( hSeg
) );
86 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
)
87 && NE_SEG_TABLE(pModule
)[0].hSeg
== hSeg
)
89 FARPROC proc
= (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( func
))->target
;
91 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
92 SELECTOROF( func
), OFFSETOF( func
), relay
, proc
);
96 /* Otherwise, we need to alloc a thunk */
97 thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
100 thunk
->popl_eax
= 0x58;
101 thunk
->pushl_func
= 0x68;
103 thunk
->pushl_eax
= 0x50;
105 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
106 thunk
->magic
= CALLTO16_THUNK_MAGIC
;
107 thunk
->next
= firstThunk
;
111 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
112 SELECTOROF( func
), OFFSETOF( func
), relay
, thunk
);
113 return (FARPROC
)thunk
;
116 /***********************************************************************
119 void THUNK_Free( FARPROC thunk
)
121 THUNK
*t
= (THUNK
*)thunk
;
122 if ( !t
|| IsBadReadPtr( t
, sizeof(*t
) )
123 || t
->magic
!= CALLTO16_THUNK_MAGIC
)
126 if (HeapValidate( GetProcessHeap(), 0, t
))
128 THUNK
**prev
= &firstThunk
;
129 while (*prev
&& (*prev
!= t
)) prev
= &(*prev
)->next
;
133 HeapFree( GetProcessHeap(), 0, t
);
137 ERR("invalid thunk addr %p\n", thunk
);
142 /***********************************************************************
143 * THUNK_GetCalloutThunk
145 * Retrieve API entry point with given name from given module.
146 * If module is builtin, return the 32-bit entry point, otherwise
147 * create a 32->16 thunk to the 16-bit entry point, using the
151 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
153 FARPROC16 proc
= GetProcAddress16( pModule
->self
, name
);
154 if ( !proc
) return 0;
156 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
157 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
159 return (FARPROC
)THUNK_Alloc( proc
, relay
);
162 /***********************************************************************
165 void THUNK_InitCallout(void)
170 hModule
= GetModuleHandleA( "user32.dll" );
173 #define GETADDR( name ) \
174 *(FARPROC *)&Callout.name = GetProcAddress( hModule, #name )
176 GETADDR( PeekMessageA
);
177 GETADDR( GetMessageA
);
178 GETADDR( SendMessageA
);
179 GETADDR( PostMessageA
);
180 GETADDR( TranslateMessage
);
181 GETADDR( DispatchMessageA
);
182 GETADDR( RedrawWindow
);
183 GETADDR( WaitForInputIdle
);
184 GETADDR( MsgWaitForMultipleObjects
);
185 GETADDR( WindowFromDC
);
186 GETADDR( MessageBoxA
);
187 GETADDR( MessageBoxW
);
190 else WARN("no 32-bit USER\n");
192 pModule
= NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
195 #define GETADDR( var, name, thk ) \
196 *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
197 (RELAY)THUNK_CallTo16_##thk )
199 GETADDR( FinalUserInit16
, "FinalUserInit", word_
);
200 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
201 GETADDR( UserYield16
, "UserYield", word_
);
202 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
203 GETADDR( UserSignalProc
, "SignalProc32", word_lllw
);
206 else WARN("no 16-bit USER\n");