Various cosmetic changes.
[wine/wine64.git] / relay32 / relay386.c
blobd524879344bc088b3636db00b40a14a9faf8c23c
1 /*
2 * 386-specific Win32 relay functions
4 * Copyright 1997 Alexandre Julliard
5 */
8 #include "config.h"
10 #include <assert.h>
11 #include <string.h>
12 #include <stdio.h>
14 #include "winnt.h"
15 #include "stackframe.h"
16 #include "module.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(relay);
21 char **debug_relay_excludelist = NULL, **debug_relay_includelist = NULL;
23 /***********************************************************************
24 * RELAY_ShowDebugmsgRelay
26 * Simple function to decide if a particular debugging message is
27 * wanted. Called from RELAY_CallFrom32 and from in if1632/relay.c
29 int RELAY_ShowDebugmsgRelay(const char *func) {
31 if(debug_relay_excludelist || debug_relay_includelist) {
32 const char *term = strchr(func, ':');
33 char **listitem;
34 int len, len2, itemlen, show;
36 if(debug_relay_excludelist) {
37 show = 1;
38 listitem = debug_relay_excludelist;
39 } else {
40 show = 0;
41 listitem = debug_relay_includelist;
43 assert(term);
44 assert(strlen(term) > 2);
45 len = term - func;
46 len2 = strchr(func, '.') - func;
47 assert(len2 && len2 > 0 && len2 < 64);
48 term += 2;
49 for(; *listitem; listitem++) {
50 itemlen = strlen(*listitem);
51 if((itemlen == len && !strncasecmp(*listitem, func, len)) ||
52 (itemlen == len2 && !strncasecmp(*listitem, func, len2)) ||
53 !strcasecmp(*listitem, term)) {
54 show = !show;
55 break;
58 return show;
60 return 1;
64 #ifdef __i386__
66 typedef struct
68 BYTE call; /* 0xe8 call callfrom32 (relative) */
69 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
70 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
71 WORD args; /* nb of args to remove from the stack */
72 void *orig; /* original entry point */
73 DWORD argtypes; /* argument types */
74 } DEBUG_ENTRY_POINT;
77 /***********************************************************************
78 * find_exported_name
80 * Find the name of an exported function.
82 static const char *find_exported_name( const char *module,
83 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
85 int i;
86 const char *ret = NULL;
88 WORD *ordptr = (WORD *)(module + exp->AddressOfNameOrdinals);
89 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
90 if (*ordptr + exp->Base == ordinal) break;
91 if (i < exp->NumberOfNames)
92 ret = module + ((DWORD*)(module + exp->AddressOfNames))[i];
93 return ret;
97 /***********************************************************************
98 * get_entry_point
100 * Get the name of the DLL entry point corresponding to a relay address.
102 static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
104 IMAGE_DATA_DIRECTORY *dir;
105 IMAGE_EXPORT_DIRECTORY *exp = NULL;
106 DEBUG_ENTRY_POINT *debug;
107 char *base = NULL;
108 const char *name;
109 int ordinal = 0;
110 WINE_MODREF *wm;
112 /* First find the module */
114 for (wm = MODULE_modref_list; wm; wm = wm->next)
116 if (!(wm->flags & WINE_MODREF_INTERNAL)) continue;
117 base = (char *)wm->module;
118 dir = &PE_HEADER(base)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
119 if (!dir->Size) continue;
120 exp = (IMAGE_EXPORT_DIRECTORY *)(base + dir->VirtualAddress);
121 debug = (DEBUG_ENTRY_POINT *)((char *)exp + dir->Size);
122 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
124 ordinal = relay - debug;
125 break;
129 /* Now find the function */
131 if ((name = find_exported_name( base, exp, ordinal + exp->Base )))
132 sprintf( buffer, "%s.%s", base + exp->Name, name );
133 else
134 sprintf( buffer, "%s.%ld", base + exp->Name, ordinal + exp->Base );
138 /***********************************************************************
139 * RELAY_PrintArgs
141 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
143 while (nb_args--)
145 if ((typemask & 3) && HIWORD(*args))
147 if (typemask & 2)
148 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
149 else
150 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
152 else DPRINTF( "%08x", *args );
153 if (nb_args) DPRINTF( "," );
154 args++;
155 typemask >>= 2;
160 typedef LONGLONG (*LONGLONG_CPROC)();
161 typedef LONGLONG (WINAPI *LONGLONG_FARPROC)();
164 /***********************************************************************
165 * call_cdecl_function
167 static LONGLONG call_cdecl_function( LONGLONG_CPROC func, int nb_args, const int *args )
169 LONGLONG ret;
170 switch(nb_args)
172 case 0: ret = func(); break;
173 case 1: ret = func(args[0]); break;
174 case 2: ret = func(args[0],args[1]); break;
175 case 3: ret = func(args[0],args[1],args[2]); break;
176 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
177 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
178 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
179 args[5]); break;
180 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
181 args[6]); break;
182 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
183 args[6],args[7]); break;
184 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
185 args[6],args[7],args[8]); break;
186 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
187 args[6],args[7],args[8],args[9]); break;
188 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
189 args[6],args[7],args[8],args[9],args[10]); break;
190 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
191 args[6],args[7],args[8],args[9],args[10],
192 args[11]); break;
193 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
194 args[6],args[7],args[8],args[9],args[10],args[11],
195 args[12]); break;
196 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
197 args[6],args[7],args[8],args[9],args[10],args[11],
198 args[12],args[13]); break;
199 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
200 args[6],args[7],args[8],args[9],args[10],args[11],
201 args[12],args[13],args[14]); break;
202 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
203 args[6],args[7],args[8],args[9],args[10],args[11],
204 args[12],args[13],args[14],args[15]); break;
205 default:
206 ERR( "Unsupported nb of args %d\n", nb_args );
207 assert(FALSE);
209 return ret;
213 /***********************************************************************
214 * call_stdcall_function
216 static LONGLONG call_stdcall_function( LONGLONG_FARPROC func, int nb_args, const int *args )
218 LONGLONG ret;
219 switch(nb_args)
221 case 0: ret = func(); break;
222 case 1: ret = func(args[0]); break;
223 case 2: ret = func(args[0],args[1]); break;
224 case 3: ret = func(args[0],args[1],args[2]); break;
225 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
226 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
227 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
228 args[5]); break;
229 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
230 args[6]); break;
231 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
232 args[6],args[7]); break;
233 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
234 args[6],args[7],args[8]); break;
235 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
236 args[6],args[7],args[8],args[9]); break;
237 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
238 args[6],args[7],args[8],args[9],args[10]); break;
239 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
240 args[6],args[7],args[8],args[9],args[10],
241 args[11]); break;
242 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
243 args[6],args[7],args[8],args[9],args[10],args[11],
244 args[12]); break;
245 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
246 args[6],args[7],args[8],args[9],args[10],args[11],
247 args[12],args[13]); break;
248 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
249 args[6],args[7],args[8],args[9],args[10],args[11],
250 args[12],args[13],args[14]); break;
251 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
252 args[6],args[7],args[8],args[9],args[10],args[11],
253 args[12],args[13],args[14],args[15]); break;
254 default:
255 ERR( "Unsupported nb of args %d\n", nb_args );
256 assert(FALSE);
258 return ret;
262 /***********************************************************************
263 * RELAY_CallFrom32
265 * Stack layout on entry to this function:
266 * ... ...
267 * (esp+12) arg2
268 * (esp+8) arg1
269 * (esp+4) ret_addr
270 * (esp) return addr to relay code
272 static LONGLONG RELAY_CallFrom32( int ret_addr, ... )
274 LONGLONG ret;
275 char buffer[80];
276 BOOL ret64;
278 int *args = &ret_addr + 1;
279 /* Relay addr is the return address for this function */
280 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
281 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
282 WORD nb_args = relay->args / sizeof(int);
284 get_entry_point( buffer, relay );
286 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
287 RELAY_PrintArgs( args, nb_args, relay->argtypes );
288 DPRINTF( ") ret=%08x\n", ret_addr );
289 ret64 = (relay->argtypes & 0x80000000) && (nb_args < 16);
291 if (relay->ret == 0xc3) /* cdecl */
293 ret = call_cdecl_function( (LONGLONG_CPROC)relay->orig, nb_args, args );
295 else /* stdcall */
297 ret = call_stdcall_function( (LONGLONG_FARPROC)relay->orig, nb_args, args );
300 if (ret64)
301 DPRINTF( "%08lx:Ret %s() retval=%08x%08x ret=%08x\n",
302 GetCurrentThreadId(),
303 buffer, (UINT)(ret >> 32), (UINT)ret, ret_addr );
304 else
305 DPRINTF( "%08lx:Ret %s() retval=%08x ret=%08x\n",
306 GetCurrentThreadId(),
307 buffer, (UINT)ret, ret_addr );
309 return ret;
313 /***********************************************************************
314 * RELAY_CallFrom32Regs
316 * Stack layout (esp is context->Esp, not the current %esp):
318 * ...
319 * (esp+4) first arg
320 * (esp) return addr to caller
321 * (esp-4) return addr to DEBUG_ENTRY_POINT
322 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
323 * ... >128 bytes space free to be modified (ensured by the assembly glue)
325 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
327 char buffer[80];
328 int* args;
329 int args_copy[17];
330 BYTE *entry_point;
332 BYTE *relay_addr = *((BYTE **)context->Esp - 1);
333 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
334 WORD nb_args = (relay->args & ~0x8000) / sizeof(int);
336 /* remove extra stuff from the stack */
337 context->Eip = stack32_pop(context);
338 args = (int *)context->Esp;
339 if (relay->ret == 0xc2) /* stdcall */
340 context->Esp += nb_args * sizeof(int);
342 assert(TRACE_ON(relay));
344 entry_point = (BYTE *)relay->orig;
345 assert( *entry_point == 0xe8 /* lcall */ );
347 get_entry_point( buffer, relay );
349 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
350 RELAY_PrintArgs( args, nb_args, relay->argtypes );
351 DPRINTF( ") ret=%08lx fs=%04lx\n", context->Eip, context->SegFs );
353 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
354 context->Eax, context->Ebx, context->Ecx,
355 context->Edx, context->Esi, context->Edi );
356 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
357 context->Ebp, context->Esp, context->SegDs,
358 context->SegEs, context->SegGs, context->EFlags );
360 /* Now call the real function */
362 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
363 args_copy[nb_args] = (int)context; /* append context argument */
364 if (relay->ret == 0xc3) /* cdecl */
366 call_cdecl_function( *(LONGLONG_CPROC *)(entry_point + 5), nb_args+1, args_copy );
368 else /* stdcall */
370 call_stdcall_function( *(LONGLONG_FARPROC *)(entry_point + 5), nb_args+1, args_copy );
373 DPRINTF( "%08lx:Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
374 GetCurrentThreadId(),
375 buffer, context->Eax, context->Eip, context->SegFs );
377 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
378 context->Eax, context->Ebx, context->Ecx,
379 context->Edx, context->Esi, context->Edi );
380 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
381 context->Ebp, context->Esp, context->SegDs,
382 context->SegEs, context->SegGs, context->EFlags );
385 void WINAPI RELAY_CallFrom32Regs(void);
386 __ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs,
387 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
388 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
390 /***********************************************************************
391 * RELAY_SetupDLL
393 * Setup relay debugging for a built-in dll.
395 void RELAY_SetupDLL( const char *module )
397 IMAGE_DATA_DIRECTORY *dir;
398 IMAGE_EXPORT_DIRECTORY *exports;
399 DEBUG_ENTRY_POINT *debug;
400 DWORD *funcs;
401 int i;
402 const char *name, *dllname;
404 dir = &PE_HEADER(module)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
405 if (!dir->Size) return;
406 exports = (IMAGE_EXPORT_DIRECTORY *)(module + dir->VirtualAddress);
407 debug = (DEBUG_ENTRY_POINT *)((char *)exports + dir->Size);
408 funcs = (DWORD *)(module + exports->AddressOfFunctions);
409 dllname = module + exports->Name;
411 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
413 int on = 1;
415 if (!debug->call) continue; /* not a normal function */
416 if (debug->call != 0xe8 && debug->call != 0xe9) break; /* not a debug thunk at all */
418 if ((name = find_exported_name( module, exports, i + exports->Base )))
420 char buffer[200];
421 sprintf( buffer, "%s.%d: %s", dllname, i, name );
422 on = RELAY_ShowDebugmsgRelay(buffer);
425 if (on)
427 debug->call = 0xe8; /* call relative */
428 if (debug->args & 0x8000) /* register func */
429 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
430 else
431 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
433 else
435 debug->call = 0xe9; /* jmp relative */
436 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
439 debug->orig = (FARPROC)(module + (DWORD)*funcs);
440 *funcs = (char *)debug - module;
444 #else /* __i386__ */
446 void RELAY_SetupDLL( const char *module )
450 #endif /* __i386__ */