Reserve space for the PE header at the start of the .so file.
[wine.git] / relay32 / relay386.c
blob09a22f1713f99b210b29501c01ff964c9750198a
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>
11 #include "winnt.h"
12 #include "builtin32.h"
13 #include "selectors.h"
14 #include "stackframe.h"
15 #include "syslevel.h"
16 #include "main.h"
17 #include "module.h"
18 #include "process.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(relay);
23 char **debug_relay_excludelist = NULL, **debug_relay_includelist = NULL;
25 /***********************************************************************
26 * RELAY_ShowDebugmsgRelay
28 * Simple function to decide if a particular debugging message is
29 * wanted. Called from RELAY_CallFrom32 and from in if1632/relay.c
31 int RELAY_ShowDebugmsgRelay(const char *func) {
33 if(debug_relay_excludelist || debug_relay_includelist) {
34 const char *term = strchr(func, ':');
35 char **listitem;
36 int len, len2, itemlen, show;
38 if(debug_relay_excludelist) {
39 show = 1;
40 listitem = debug_relay_excludelist;
41 } else {
42 show = 0;
43 listitem = debug_relay_includelist;
45 assert(term);
46 assert(strlen(term) > 2);
47 len = term - func;
48 len2 = strchr(func, '.') - func;
49 assert(len2 && len2 > 0 && len2 < 64);
50 term += 2;
51 for(; *listitem; listitem++) {
52 itemlen = strlen(*listitem);
53 if((itemlen == len && !lstrncmpiA(*listitem, func, len)) ||
54 (itemlen == len2 && !lstrncmpiA(*listitem, func, len2)) ||
55 !lstrcmpiA(*listitem, term)) {
56 show = !show;
57 break;
60 return show;
62 return 1;
66 #ifdef __i386__
68 typedef struct
70 BYTE call; /* 0xe8 call callfrom32 (relative) */
71 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
72 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
73 WORD args; /* nb of args to remove from the stack */
74 FARPROC orig; /* original entry point */
75 DWORD argtypes; /* argument types */
76 } DEBUG_ENTRY_POINT;
79 /***********************************************************************
80 * find_exported_name
82 * Find the name of an exported function.
84 static const char *find_exported_name( const char *module,
85 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
87 int i;
88 const char *ret = NULL;
90 WORD *ordptr = (WORD *)(module + exp->AddressOfNameOrdinals);
91 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
92 if (*ordptr + exp->Base == ordinal) break;
93 if (i < exp->NumberOfNames)
94 ret = module + ((DWORD*)(module + exp->AddressOfNames))[i];
95 return ret;
99 /***********************************************************************
100 * get_entry_point
102 * Get the name of the DLL entry point corresponding to a relay address.
104 static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
106 IMAGE_DATA_DIRECTORY *dir;
107 IMAGE_EXPORT_DIRECTORY *exp = NULL;
108 DEBUG_ENTRY_POINT *debug;
109 char *base = NULL;
110 const char *name;
111 int ordinal = 0;
112 WINE_MODREF *wm;
114 /* First find the module */
116 for (wm = PROCESS_Current()->modref_list; wm; wm = wm->next)
118 if (wm->type != MODULE32_PE) continue;
119 if (!(wm->flags & WINE_MODREF_INTERNAL)) continue;
120 base = (char *)wm->module;
121 dir = &PE_HEADER(base)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
122 if (!dir->Size) continue;
123 exp = (IMAGE_EXPORT_DIRECTORY *)(base + dir->VirtualAddress);
124 debug = (DEBUG_ENTRY_POINT *)((char *)exp + dir->Size);
125 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
127 ordinal = relay - debug;
128 break;
132 /* Now find the function */
134 name = find_exported_name( base, exp, ordinal + exp->Base );
135 sprintf( buffer, "%s.%ld: %s", base + exp->Name, ordinal + exp->Base, name ? name : "@" );
139 /***********************************************************************
140 * RELAY_PrintArgs
142 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
144 while (nb_args--)
146 if ((typemask & 3) && HIWORD(*args))
148 if (typemask & 2)
149 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
150 else
151 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
153 else DPRINTF( "%08x", *args );
154 if (nb_args) DPRINTF( "," );
155 args++;
156 typemask >>= 2;
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 int RELAY_CallFrom32( int ret_addr, ... )
173 int ret;
174 char buffer[80];
176 int *args = &ret_addr + 1;
177 /* Relay addr is the return address for this function */
178 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
179 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
180 WORD nb_args = relay->args / sizeof(int);
182 get_entry_point( buffer, relay );
184 DPRINTF( "Call %s(", buffer );
185 RELAY_PrintArgs( args, nb_args, relay->argtypes );
186 DPRINTF( ") ret=%08x fs=%04x\n", ret_addr, __get_fs() );
188 SYSLEVEL_CheckNotLevel( 2 );
190 if (relay->ret == 0xc3) /* cdecl */
192 LRESULT (*cfunc)() = (LRESULT(*)())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 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 DPRINTF( "Ret %s() retval=%08x ret=%08x fs=%04x\n",
277 buffer, ret, ret_addr, __get_fs() );
279 SYSLEVEL_CheckNotLevel( 2 );
281 return ret;
285 /***********************************************************************
286 * RELAY_CallFrom32Regs
288 * Stack layout (esp is ESP_reg(context), not the current %esp):
290 * ...
291 * (esp+4) first arg
292 * (esp) return addr to caller
293 * (esp-4) return addr to DEBUG_ENTRY_POINT
294 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
295 * ... >128 bytes space free to be modified (ensured by the assembly glue)
298 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context );
299 DEFINE_REGS_ENTRYPOINT_0( RELAY_CallFrom32Regs, RELAY_DoCallFrom32Regs )
300 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
302 char buffer[80];
303 int* args;
304 FARPROC func;
305 BYTE *entry_point;
307 BYTE *relay_addr = *((BYTE **)ESP_reg(context) - 1);
308 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
309 WORD nb_args = (relay->args & ~0x8000) / sizeof(int);
311 /* remove extra stuff from the stack */
312 EIP_reg(context) = stack32_pop(context);
313 args = (int *)ESP_reg(context);
314 ESP_reg(context) += 4 * nb_args;
316 assert(TRACE_ON(relay));
318 entry_point = (BYTE *)relay->orig;
319 assert( *entry_point == 0xe8 /* lcall */ );
320 func = *(FARPROC *)(entry_point + 5);
322 get_entry_point( buffer, relay );
324 DPRINTF( "Call %s(", buffer );
325 RELAY_PrintArgs( args, nb_args, relay->argtypes );
326 DPRINTF( ") ret=%08lx fs=%04lx\n", EIP_reg(context), FS_reg(context) );
328 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
329 EAX_reg(context), EBX_reg(context), ECX_reg(context),
330 EDX_reg(context), ESI_reg(context), EDI_reg(context) );
331 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
332 EBP_reg(context), ESP_reg(context), DS_reg(context),
333 ES_reg(context), GS_reg(context), EFL_reg(context) );
335 SYSLEVEL_CheckNotLevel( 2 );
337 /* Now call the real function */
338 switch(nb_args)
340 case 0: func(context); break;
341 case 1: func(args[0],context); break;
342 case 2: func(args[0],args[1],context); break;
343 case 3: func(args[0],args[1],args[2],context); break;
344 case 4: func(args[0],args[1],args[2],args[3],context); break;
345 case 5: func(args[0],args[1],args[2],args[3],args[4],context); break;
346 case 6: func(args[0],args[1],args[2],args[3],args[4],args[5],context); break;
347 case 7: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],context); break;
348 case 8: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],context); break;
349 case 9: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
350 context); break;
351 case 10: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
352 args[9],context); break;
353 case 11: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
354 args[9],args[10],context); break;
355 case 12: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
356 args[9],args[10],args[11],context); break;
357 case 13: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
358 args[9],args[10],args[11],args[12],context); break;
359 case 14: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
360 args[9],args[10],args[11],args[12],args[13],context); break;
361 case 15: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
362 args[9],args[10],args[11],args[12],args[13],args[14],context); break;
363 case 16: func(args[0],args[1],args[2],args[3],args[4],args[5], args[6],args[7],args[8],
364 args[9],args[10],args[11],args[12],args[13],args[14],args[15],context); break;
365 default:
366 ERR( "Unsupported nb of args %d\n", nb_args );
367 assert(FALSE);
370 DPRINTF( "Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
371 buffer, EAX_reg(context), EIP_reg(context), FS_reg(context) );
372 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
373 EAX_reg(context), EBX_reg(context), ECX_reg(context),
374 EDX_reg(context), ESI_reg(context), EDI_reg(context) );
375 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
376 EBP_reg(context), ESP_reg(context), DS_reg(context),
377 ES_reg(context), GS_reg(context), EFL_reg(context) );
379 SYSLEVEL_CheckNotLevel( 2 );
383 /***********************************************************************
384 * RELAY_SetupDLL
386 * Setup relay debugging for a built-in dll.
388 void RELAY_SetupDLL( const char *module )
390 IMAGE_DATA_DIRECTORY *dir;
391 IMAGE_EXPORT_DIRECTORY *exports;
392 DEBUG_ENTRY_POINT *debug;
393 DWORD *funcs;
394 int i;
395 const char *name, *dllname;
397 dir = &PE_HEADER(module)->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
398 if (!dir->Size) return;
399 exports = (IMAGE_EXPORT_DIRECTORY *)(module + dir->VirtualAddress);
400 debug = (DEBUG_ENTRY_POINT *)((char *)exports + dir->Size);
401 funcs = (DWORD *)(module + exports->AddressOfFunctions);
402 dllname = module + exports->Name;
404 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
406 int on = 1;
408 if (!debug->call) continue; /* not a normal function */
410 if ((name = find_exported_name( module, exports, i + exports->Base )))
412 char buffer[200];
413 sprintf( buffer, "%s.%d: %s", dllname, i, name );
414 on = RELAY_ShowDebugmsgRelay(buffer);
417 if (on)
419 debug->call = 0xe8; /* call relative */
420 if (debug->args & 0x8000) /* register func */
421 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
422 else
423 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
425 else
427 debug->call = 0xe9; /* jmp relative */
428 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
431 debug->orig = (FARPROC)(module + (DWORD)*funcs);
432 *funcs = (char *)debug - module;
436 #else /* __i386__ */
438 void RELAY_SetupDLL( const char *module )
442 #endif /* __i386__ */