Added basic support for SPI_GETSCREENREADER.
[wine/multimedia.git] / relay32 / relay386.c
blob28173723cdda0d2b88810468d0e7399f5cd13d18
1 /*
2 * 386-specific Win32 relay functions
4 * Copyright 1997 Alexandre Julliard
5 */
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
12 #include "config.h"
13 #include "winnt.h"
14 #include "stackframe.h"
15 #include "module.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(relay);
20 char **debug_relay_excludelist = NULL, **debug_relay_includelist = NULL;
22 /***********************************************************************
23 * RELAY_ShowDebugmsgRelay
25 * Simple function to decide if a particular debugging message is
26 * wanted. Called from RELAY_CallFrom32 and from in if1632/relay.c
28 int RELAY_ShowDebugmsgRelay(const char *func) {
30 if(debug_relay_excludelist || debug_relay_includelist) {
31 const char *term = strchr(func, ':');
32 char **listitem;
33 int len, len2, itemlen, show;
35 if(debug_relay_excludelist) {
36 show = 1;
37 listitem = debug_relay_excludelist;
38 } else {
39 show = 0;
40 listitem = debug_relay_includelist;
42 assert(term);
43 assert(strlen(term) > 2);
44 len = term - func;
45 len2 = strchr(func, '.') - func;
46 assert(len2 && len2 > 0 && len2 < 64);
47 term += 2;
48 for(; *listitem; listitem++) {
49 itemlen = strlen(*listitem);
50 if((itemlen == len && !strncasecmp(*listitem, func, len)) ||
51 (itemlen == len2 && !strncasecmp(*listitem, func, len2)) ||
52 !strcasecmp(*listitem, term)) {
53 show = !show;
54 break;
57 return show;
59 return 1;
63 #ifdef __i386__
65 typedef struct
67 BYTE call; /* 0xe8 call callfrom32 (relative) */
68 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
69 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
70 WORD args; /* nb of args to remove from the stack */
71 void *orig; /* original entry point */
72 DWORD argtypes; /* argument types */
73 } DEBUG_ENTRY_POINT;
76 /***********************************************************************
77 * find_exported_name
79 * Find the name of an exported function.
81 static const char *find_exported_name( const char *module,
82 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
84 int i;
85 const char *ret = NULL;
87 WORD *ordptr = (WORD *)(module + exp->AddressOfNameOrdinals);
88 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
89 if (*ordptr + exp->Base == ordinal) break;
90 if (i < exp->NumberOfNames)
91 ret = module + ((DWORD*)(module + exp->AddressOfNames))[i];
92 return ret;
96 /***********************************************************************
97 * get_entry_point
99 * Get the name of the DLL entry point corresponding to a relay address.
101 static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
103 IMAGE_DATA_DIRECTORY *dir;
104 IMAGE_EXPORT_DIRECTORY *exp = NULL;
105 DEBUG_ENTRY_POINT *debug;
106 char *base = NULL;
107 const char *name;
108 int ordinal = 0;
109 WINE_MODREF *wm;
111 /* First find the module */
113 for (wm = MODULE_modref_list; wm; wm = wm->next)
115 if (!(wm->flags & WINE_MODREF_INTERNAL)) continue;
116 base = (char *)wm->module;
117 dir = &PE_HEADER(base)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
118 if (!dir->Size) continue;
119 exp = (IMAGE_EXPORT_DIRECTORY *)(base + dir->VirtualAddress);
120 debug = (DEBUG_ENTRY_POINT *)((char *)exp + dir->Size);
121 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
123 ordinal = relay - debug;
124 break;
128 /* Now find the function */
130 if ((name = find_exported_name( base, exp, ordinal + exp->Base )))
131 sprintf( buffer, "%s.%s", base + exp->Name, name );
132 else
133 sprintf( buffer, "%s.%ld", base + exp->Name, ordinal + exp->Base );
137 /***********************************************************************
138 * RELAY_PrintArgs
140 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
142 while (nb_args--)
144 if ((typemask & 3) && HIWORD(*args))
146 if (typemask & 2)
147 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
148 else
149 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
151 else DPRINTF( "%08x", *args );
152 if (nb_args) DPRINTF( "," );
153 args++;
154 typemask >>= 2;
159 typedef LONGLONG WINAPI (*LONGLONG_FARPROC)();
161 /***********************************************************************
162 * RELAY_CallFrom32
164 * Stack layout on entry to this function:
165 * ... ...
166 * (esp+12) arg2
167 * (esp+8) arg1
168 * (esp+4) ret_addr
169 * (esp) return addr to relay code
171 static LONGLONG RELAY_CallFrom32( int ret_addr, ... )
173 LONGLONG ret;
174 char buffer[80];
175 BOOL ret64;
177 int *args = &ret_addr + 1;
178 /* Relay addr is the return address for this function */
179 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
180 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
181 WORD nb_args = relay->args / sizeof(int);
183 get_entry_point( buffer, relay );
185 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
186 RELAY_PrintArgs( args, nb_args, relay->argtypes );
187 DPRINTF( ") ret=%08x\n", ret_addr );
188 ret64 = (relay->argtypes & 0x80000000) && (nb_args < 16);
190 if (relay->ret == 0xc3) /* cdecl */
192 LONGLONG (*cfunc)() = relay->orig;
193 switch(nb_args)
195 case 0: ret = cfunc(); break;
196 case 1: ret = cfunc(args[0]); break;
197 case 2: ret = cfunc(args[0],args[1]); break;
198 case 3: ret = cfunc(args[0],args[1],args[2]); break;
199 case 4: ret = cfunc(args[0],args[1],args[2],args[3]); break;
200 case 5: ret = cfunc(args[0],args[1],args[2],args[3],args[4]); break;
201 case 6: ret = cfunc(args[0],args[1],args[2],args[3],args[4],
202 args[5]); break;
203 case 7: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
204 args[6]); break;
205 case 8: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
206 args[6],args[7]); break;
207 case 9: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
208 args[6],args[7],args[8]); break;
209 case 10: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
210 args[6],args[7],args[8],args[9]); break;
211 case 11: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
212 args[6],args[7],args[8],args[9],args[10]); break;
213 case 12: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
214 args[6],args[7],args[8],args[9],args[10],
215 args[11]); break;
216 case 13: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
217 args[6],args[7],args[8],args[9],args[10],args[11],
218 args[12]); break;
219 case 14: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
220 args[6],args[7],args[8],args[9],args[10],args[11],
221 args[12],args[13]); break;
222 case 15: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
223 args[6],args[7],args[8],args[9],args[10],args[11],
224 args[12],args[13],args[14]); break;
225 case 16: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
226 args[6],args[7],args[8],args[9],args[10],args[11],
227 args[12],args[13],args[14],args[15]); break;
228 default:
229 ERR( "Unsupported nb of args %d\n", nb_args );
230 assert(FALSE);
233 else /* stdcall */
235 LONGLONG_FARPROC func = relay->orig;
236 switch(nb_args)
238 case 0: ret = func(); break;
239 case 1: ret = func(args[0]); break;
240 case 2: ret = func(args[0],args[1]); break;
241 case 3: ret = func(args[0],args[1],args[2]); break;
242 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
243 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
244 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
245 args[5]); break;
246 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
247 args[6]); break;
248 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
249 args[6],args[7]); break;
250 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
251 args[6],args[7],args[8]); break;
252 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
253 args[6],args[7],args[8],args[9]); break;
254 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
255 args[6],args[7],args[8],args[9],args[10]); break;
256 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
257 args[6],args[7],args[8],args[9],args[10],
258 args[11]); break;
259 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
260 args[6],args[7],args[8],args[9],args[10],args[11],
261 args[12]); break;
262 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
263 args[6],args[7],args[8],args[9],args[10],args[11],
264 args[12],args[13]); break;
265 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
266 args[6],args[7],args[8],args[9],args[10],args[11],
267 args[12],args[13],args[14]); break;
268 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
269 args[6],args[7],args[8],args[9],args[10],args[11],
270 args[12],args[13],args[14],args[15]); break;
271 default:
272 ERR( "Unsupported nb of args %d\n", nb_args );
273 assert(FALSE);
276 if (ret64)
277 DPRINTF( "%08lx:Ret %s() retval=%08x%08x ret=%08x\n",
278 GetCurrentThreadId(),
279 buffer, (UINT)(ret >> 32), (UINT)ret, ret_addr );
280 else
281 DPRINTF( "%08lx:Ret %s() retval=%08x ret=%08x\n",
282 GetCurrentThreadId(),
283 buffer, (UINT)ret, ret_addr );
285 return ret;
289 /***********************************************************************
290 * RELAY_CallFrom32Regs
292 * Stack layout (esp is context->Esp, not the current %esp):
294 * ...
295 * (esp+4) first arg
296 * (esp) return addr to caller
297 * (esp-4) return addr to DEBUG_ENTRY_POINT
298 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
299 * ... >128 bytes space free to be modified (ensured by the assembly glue)
301 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
303 char buffer[80];
304 int* args;
305 FARPROC func;
306 BYTE *entry_point;
308 BYTE *relay_addr = *((BYTE **)context->Esp - 1);
309 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
310 WORD nb_args = (relay->args & ~0x8000) / sizeof(int);
312 /* remove extra stuff from the stack */
313 context->Eip = stack32_pop(context);
314 args = (int *)context->Esp;
315 context->Esp += 4 * nb_args;
317 assert(TRACE_ON(relay));
319 entry_point = (BYTE *)relay->orig;
320 assert( *entry_point == 0xe8 /* lcall */ );
321 func = *(FARPROC *)(entry_point + 5);
323 get_entry_point( buffer, relay );
325 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
326 RELAY_PrintArgs( args, nb_args, relay->argtypes );
327 DPRINTF( ") ret=%08lx fs=%04lx\n", context->Eip, context->SegFs );
329 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
330 context->Eax, context->Ebx, context->Ecx,
331 context->Edx, context->Esi, context->Edi );
332 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
333 context->Ebp, context->Esp, context->SegDs,
334 context->SegEs, context->SegGs, context->EFlags );
336 /* Now call the real function */
337 switch(nb_args)
339 case 0: func(context); break;
340 case 1: func(args[0],context); break;
341 case 2: func(args[0],args[1],context); break;
342 case 3: func(args[0],args[1],args[2],context); break;
343 case 4: func(args[0],args[1],args[2],args[3],context); break;
344 case 5: func(args[0],args[1],args[2],args[3],args[4],context); break;
345 case 6: func(args[0],args[1],args[2],args[3],args[4],args[5],context); break;
346 case 7: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],context); break;
347 case 8: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],context); break;
348 case 9: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
349 context); break;
350 case 10: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
351 args[9],context); break;
352 case 11: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
353 args[9],args[10],context); break;
354 case 12: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
355 args[9],args[10],args[11],context); break;
356 case 13: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
357 args[9],args[10],args[11],args[12],context); break;
358 case 14: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
359 args[9],args[10],args[11],args[12],args[13],context); break;
360 case 15: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
361 args[9],args[10],args[11],args[12],args[13],args[14],context); break;
362 case 16: func(args[0],args[1],args[2],args[3],args[4],args[5], args[6],args[7],args[8],
363 args[9],args[10],args[11],args[12],args[13],args[14],args[15],context); break;
364 default:
365 ERR( "Unsupported nb of args %d\n", nb_args );
366 assert(FALSE);
369 DPRINTF( "%08lx:Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
370 GetCurrentThreadId(),
371 buffer, context->Eax, context->Eip, context->SegFs );
373 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
374 context->Eax, context->Ebx, context->Ecx,
375 context->Edx, context->Esi, context->Edi );
376 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
377 context->Ebp, context->Esp, context->SegDs,
378 context->SegEs, context->SegGs, context->EFlags );
381 void WINAPI RELAY_CallFrom32Regs(void);
382 __ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs,
383 "call " __ASM_NAME("CALL32_Regs") "\n\t"
384 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
386 /***********************************************************************
387 * RELAY_SetupDLL
389 * Setup relay debugging for a built-in dll.
391 void RELAY_SetupDLL( const char *module )
393 IMAGE_DATA_DIRECTORY *dir;
394 IMAGE_EXPORT_DIRECTORY *exports;
395 DEBUG_ENTRY_POINT *debug;
396 DWORD *funcs;
397 int i;
398 const char *name, *dllname;
400 dir = &PE_HEADER(module)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
401 if (!dir->Size) return;
402 exports = (IMAGE_EXPORT_DIRECTORY *)(module + dir->VirtualAddress);
403 debug = (DEBUG_ENTRY_POINT *)((char *)exports + dir->Size);
404 funcs = (DWORD *)(module + exports->AddressOfFunctions);
405 dllname = module + exports->Name;
407 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
409 int on = 1;
411 if (!debug->call) continue; /* not a normal function */
412 if (debug->call != 0xe8 && debug->call != 0xe9) break; /* not a debug thunk at all */
414 if ((name = find_exported_name( module, exports, i + exports->Base )))
416 char buffer[200];
417 sprintf( buffer, "%s.%d: %s", dllname, i, name );
418 on = RELAY_ShowDebugmsgRelay(buffer);
421 if (on)
423 debug->call = 0xe8; /* call relative */
424 if (debug->args & 0x8000) /* register func */
425 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
426 else
427 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
429 else
431 debug->call = 0xe9; /* jmp relative */
432 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
435 debug->orig = (FARPROC)(module + (DWORD)*funcs);
436 *funcs = (char *)debug - module;
440 #else /* __i386__ */
442 void RELAY_SetupDLL( const char *module )
446 #endif /* __i386__ */