2 * 386-specific Win32 relay functions
4 * Copyright 1997 Alexandre Julliard
14 #include "stackframe.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
, ':');
34 int len
, len2
, itemlen
, show
;
36 if(debug_relay_excludelist
) {
38 listitem
= debug_relay_excludelist
;
41 listitem
= debug_relay_includelist
;
44 assert(strlen(term
) > 2);
46 len2
= strchr(func
, '.') - func
;
47 assert(len2
&& len2
> 0 && len2
< 64);
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
)) {
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 */
77 /***********************************************************************
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
)
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
];
97 /***********************************************************************
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
;
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
;
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
);
134 sprintf( buffer
, "%s.%ld", base
+ exp
->Name
, ordinal
+ exp
->Base
);
138 /***********************************************************************
141 static inline void RELAY_PrintArgs( int *args
, int nb_args
, unsigned int typemask
)
145 if ((typemask
& 3) && HIWORD(*args
))
148 DPRINTF( "%08x %s", *args
, debugstr_w((LPWSTR
)*args
) );
150 DPRINTF( "%08x %s", *args
, debugstr_a((LPCSTR
)*args
) );
152 else DPRINTF( "%08x", *args
);
153 if (nb_args
) DPRINTF( "," );
160 typedef LONGLONG
WINAPI (*LONGLONG_FARPROC
)();
162 /***********************************************************************
165 * Stack layout on entry to this function:
170 * (esp) return addr to relay code
172 static LONGLONG
RELAY_CallFrom32( int ret_addr
, ... )
178 int *args
= &ret_addr
+ 1;
179 /* Relay addr is the return address for this function */
180 BYTE
*relay_addr
= (BYTE
*)__builtin_return_address(0);
181 DEBUG_ENTRY_POINT
*relay
= (DEBUG_ENTRY_POINT
*)(relay_addr
- 5);
182 WORD nb_args
= relay
->args
/ sizeof(int);
184 get_entry_point( buffer
, relay
);
186 DPRINTF( "Call %s(", buffer
);
187 RELAY_PrintArgs( args
, nb_args
, relay
->argtypes
);
188 DPRINTF( ") ret=%08x tid=%08lx\n", ret_addr
, GetCurrentThreadId() );
189 ret64
= (relay
->argtypes
& 0x80000000) && (nb_args
< 16);
191 /* the user driver functions may be called with the window lock held */
192 if (memcmp( buffer
, "x11drv.", 7 ) && memcmp( buffer
, "ttydrv.", 7 ))
193 SYSLEVEL_CheckNotLevel( 2 );
195 if (relay
->ret
== 0xc3) /* cdecl */
197 LONGLONG (*cfunc
)() = relay
->orig
;
200 case 0: ret
= cfunc(); break;
201 case 1: ret
= cfunc(args
[0]); break;
202 case 2: ret
= cfunc(args
[0],args
[1]); break;
203 case 3: ret
= cfunc(args
[0],args
[1],args
[2]); break;
204 case 4: ret
= cfunc(args
[0],args
[1],args
[2],args
[3]); break;
205 case 5: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4]); break;
206 case 6: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],
208 case 7: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
210 case 8: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
211 args
[6],args
[7]); break;
212 case 9: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
213 args
[6],args
[7],args
[8]); break;
214 case 10: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
215 args
[6],args
[7],args
[8],args
[9]); break;
216 case 11: 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]); break;
218 case 12: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
219 args
[6],args
[7],args
[8],args
[9],args
[10],
221 case 13: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
222 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
224 case 14: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
225 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
226 args
[12],args
[13]); break;
227 case 15: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
228 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
229 args
[12],args
[13],args
[14]); break;
230 case 16: ret
= cfunc(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
231 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
232 args
[12],args
[13],args
[14],args
[15]); break;
234 ERR( "Unsupported nb of args %d\n", nb_args
);
240 LONGLONG_FARPROC func
= relay
->orig
;
243 case 0: ret
= func(); break;
244 case 1: ret
= func(args
[0]); break;
245 case 2: ret
= func(args
[0],args
[1]); break;
246 case 3: ret
= func(args
[0],args
[1],args
[2]); break;
247 case 4: ret
= func(args
[0],args
[1],args
[2],args
[3]); break;
248 case 5: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4]); break;
249 case 6: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],
251 case 7: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
253 case 8: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
254 args
[6],args
[7]); break;
255 case 9: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
256 args
[6],args
[7],args
[8]); break;
257 case 10: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
258 args
[6],args
[7],args
[8],args
[9]); break;
259 case 11: 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]); break;
261 case 12: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
262 args
[6],args
[7],args
[8],args
[9],args
[10],
264 case 13: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
265 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
267 case 14: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
268 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
269 args
[12],args
[13]); break;
270 case 15: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
271 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
272 args
[12],args
[13],args
[14]); break;
273 case 16: ret
= func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],
274 args
[6],args
[7],args
[8],args
[9],args
[10],args
[11],
275 args
[12],args
[13],args
[14],args
[15]); break;
277 ERR( "Unsupported nb of args %d\n", nb_args
);
282 DPRINTF( "Ret %s() retval=%08x%08x ret=%08x tid=%08lx\n",
283 buffer
, (UINT
)(ret
>> 32), (UINT
)ret
, ret_addr
, GetCurrentThreadId() );
285 DPRINTF( "Ret %s() retval=%08x ret=%08x tid=%08lx\n",
286 buffer
, (UINT
)ret
, ret_addr
, GetCurrentThreadId() );
288 if (memcmp( buffer
, "x11drv.", 7 ) && memcmp( buffer
, "ttydrv.", 7 ))
289 SYSLEVEL_CheckNotLevel( 2 );
295 /***********************************************************************
296 * RELAY_CallFrom32Regs
298 * Stack layout (esp is context->Esp, not the current %esp):
302 * (esp) return addr to caller
303 * (esp-4) return addr to DEBUG_ENTRY_POINT
304 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
305 * ... >128 bytes space free to be modified (ensured by the assembly glue)
307 void WINAPI
RELAY_DoCallFrom32Regs( CONTEXT86
*context
)
314 BYTE
*relay_addr
= *((BYTE
**)context
->Esp
- 1);
315 DEBUG_ENTRY_POINT
*relay
= (DEBUG_ENTRY_POINT
*)(relay_addr
- 5);
316 WORD nb_args
= (relay
->args
& ~0x8000) / sizeof(int);
318 /* remove extra stuff from the stack */
319 context
->Eip
= stack32_pop(context
);
320 args
= (int *)context
->Esp
;
321 context
->Esp
+= 4 * nb_args
;
323 assert(TRACE_ON(relay
));
325 entry_point
= (BYTE
*)relay
->orig
;
326 assert( *entry_point
== 0xe8 /* lcall */ );
327 func
= *(FARPROC
*)(entry_point
+ 5);
329 get_entry_point( buffer
, relay
);
331 DPRINTF( "Call %s(", buffer
);
332 RELAY_PrintArgs( args
, nb_args
, relay
->argtypes
);
333 DPRINTF( ") ret=%08lx fs=%04lx\n", context
->Eip
, context
->SegFs
);
335 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
336 context
->Eax
, context
->Ebx
, context
->Ecx
,
337 context
->Edx
, context
->Esi
, context
->Edi
);
338 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
339 context
->Ebp
, context
->Esp
, context
->SegDs
,
340 context
->SegEs
, context
->SegGs
, context
->EFlags
);
342 SYSLEVEL_CheckNotLevel( 2 );
344 /* Now call the real function */
347 case 0: func(context
); break;
348 case 1: func(args
[0],context
); break;
349 case 2: func(args
[0],args
[1],context
); break;
350 case 3: func(args
[0],args
[1],args
[2],context
); break;
351 case 4: func(args
[0],args
[1],args
[2],args
[3],context
); break;
352 case 5: func(args
[0],args
[1],args
[2],args
[3],args
[4],context
); break;
353 case 6: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],context
); break;
354 case 7: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],context
); break;
355 case 8: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],context
); break;
356 case 9: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
358 case 10: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
359 args
[9],context
); break;
360 case 11: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
361 args
[9],args
[10],context
); break;
362 case 12: 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],context
); break;
364 case 13: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
365 args
[9],args
[10],args
[11],args
[12],context
); break;
366 case 14: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
367 args
[9],args
[10],args
[11],args
[12],args
[13],context
); break;
368 case 15: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],
369 args
[9],args
[10],args
[11],args
[12],args
[13],args
[14],context
); break;
370 case 16: func(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5], args
[6],args
[7],args
[8],
371 args
[9],args
[10],args
[11],args
[12],args
[13],args
[14],args
[15],context
); break;
373 ERR( "Unsupported nb of args %d\n", nb_args
);
377 DPRINTF( "Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
378 buffer
, context
->Eax
, context
->Eip
, context
->SegFs
);
380 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
381 context
->Eax
, context
->Ebx
, context
->Ecx
,
382 context
->Edx
, context
->Esi
, context
->Edi
);
383 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
384 context
->Ebp
, context
->Esp
, context
->SegDs
,
385 context
->SegEs
, context
->SegGs
, context
->EFlags
);
387 SYSLEVEL_CheckNotLevel( 2 );
390 void WINAPI
RELAY_CallFrom32Regs(void);
391 __ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs
,
392 "call " __ASM_NAME("CALL32_Regs") "\n\t"
393 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
395 /***********************************************************************
398 * Setup relay debugging for a built-in dll.
400 void RELAY_SetupDLL( const char *module
)
402 IMAGE_DATA_DIRECTORY
*dir
;
403 IMAGE_EXPORT_DIRECTORY
*exports
;
404 DEBUG_ENTRY_POINT
*debug
;
407 const char *name
, *dllname
;
409 dir
= &PE_HEADER(module
)->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
410 if (!dir
->Size
) return;
411 exports
= (IMAGE_EXPORT_DIRECTORY
*)(module
+ dir
->VirtualAddress
);
412 debug
= (DEBUG_ENTRY_POINT
*)((char *)exports
+ dir
->Size
);
413 funcs
= (DWORD
*)(module
+ exports
->AddressOfFunctions
);
414 dllname
= module
+ exports
->Name
;
416 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++, debug
++)
420 if (!debug
->call
) continue; /* not a normal function */
421 if (debug
->call
!= 0xe8 && debug
->call
!= 0xe9) break; /* not a debug thunk at all */
423 if ((name
= find_exported_name( module
, exports
, i
+ exports
->Base
)))
426 sprintf( buffer
, "%s.%d: %s", dllname
, i
, name
);
427 on
= RELAY_ShowDebugmsgRelay(buffer
);
432 debug
->call
= 0xe8; /* call relative */
433 if (debug
->args
& 0x8000) /* register func */
434 debug
->callfrom32
= (char *)RELAY_CallFrom32Regs
- (char *)&debug
->ret
;
436 debug
->callfrom32
= (char *)RELAY_CallFrom32
- (char *)&debug
->ret
;
440 debug
->call
= 0xe9; /* jmp relative */
441 debug
->callfrom32
= (char *)debug
->orig
- (char *)&debug
->ret
;
444 debug
->orig
= (FARPROC
)(module
+ (DWORD
)*funcs
);
445 *funcs
= (char *)debug
- module
;
451 void RELAY_SetupDLL( const char *module
)
455 #endif /* __i386__ */