Don't include wine internal functions in the relay debugging table.
[wine/multimedia.git] / if1632 / thunk.c
blob3a1dbcd885de7de0118a3db7dd3f51ad1c4ad38e
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "callback.h"
11 #include "builtin16.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "neexe.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 /***********************************************************************
62 * THUNK_Alloc
64 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
66 HANDLE16 hSeg;
67 NE_MODULE *pModule;
68 THUNK *thunk;
70 /* NULL maps to NULL */
71 if ( !func ) return NULL;
73 /*
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 );
93 return proc;
96 /* Otherwise, we need to alloc a thunk */
97 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
98 if (thunk)
100 thunk->popl_eax = 0x58;
101 thunk->pushl_func = 0x68;
102 thunk->proc = func;
103 thunk->pushl_eax = 0x50;
104 thunk->jmp = 0xe9;
105 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
106 thunk->magic = CALLTO16_THUNK_MAGIC;
107 thunk->next = firstThunk;
108 firstThunk = thunk;
111 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
112 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
113 return (FARPROC)thunk;
116 /***********************************************************************
117 * THUNK_Free
119 void THUNK_Free( FARPROC thunk )
121 THUNK *t = (THUNK*)thunk;
122 if ( !t || IsBadReadPtr( t, sizeof(*t) )
123 || t->magic != CALLTO16_THUNK_MAGIC )
124 return;
126 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t ))
128 THUNK **prev = &firstThunk;
129 while (*prev && (*prev != t)) prev = &(*prev)->next;
130 if (*prev)
132 *prev = t->next;
133 HeapFree( GetProcessHeap(), 0, t );
134 return;
137 ERR("invalid thunk addr %p\n", thunk );
138 return;
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
148 * given relay code.
151 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
153 FARPROC16 proc = WIN32_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;
158 else
159 return (FARPROC)THUNK_Alloc( proc, relay );
162 /***********************************************************************
163 * THUNK_InitCallout
165 void THUNK_InitCallout(void)
167 HMODULE hModule;
168 NE_MODULE *pModule;
170 hModule = GetModuleHandleA( "user32.dll" );
171 if ( hModule )
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 );
188 #undef GETADDR
190 else WARN("no 32-bit USER\n");
192 pModule = NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
193 if ( pModule )
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 );
204 #undef GETADDR
206 else WARN("no 16-bit USER\n");