blob: 92d4b5647f0c3b5efcdcb92d5aa212a906ae608e [file] [log] [blame]
Alexandre Julliard44ed71f1997-12-21 19:17:50 +00001/*
2 * 386-specific Win32 relay functions
3 *
4 * Copyright 1997 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000019 */
20
Francois Gougete5ddd262001-10-14 16:18:52 +000021#include "config.h"
Alexandre Julliard894b1882002-04-25 21:40:56 +000022#include "wine/port.h"
Francois Gougete5ddd262001-10-14 16:18:52 +000023
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000024#include <assert.h>
Alexandre Julliardf90efa91998-06-14 15:24:15 +000025#include <string.h>
Alexandre Julliard246c3602000-05-10 03:48:00 +000026#include <stdio.h>
Dimitrie O. Paun0b7a7bb2000-11-25 01:31:17 +000027
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000028#include "winnt.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000029#include "winternl.h"
Alexandre Julliard06b97891999-05-13 16:13:17 +000030#include "stackframe.h"
Alexandre Julliard246c3602000-05-10 03:48:00 +000031#include "module.h"
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000032#include "wine/unicode.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000033#include "wine/debug.h"
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000034
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000035WINE_DEFAULT_DEBUG_CHANNEL(relay);
Alexandre Julliarda061b842002-06-04 17:48:41 +000036WINE_DECLARE_DEBUG_CHANNEL(snoop);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000037
Alexandre Julliarda061b842002-06-04 17:48:41 +000038const char **debug_relay_excludelist = NULL;
39const char **debug_relay_includelist = NULL;
40const char **debug_snoop_excludelist = NULL;
41const char **debug_snoop_includelist = NULL;
Alexandre Julliardf90efa91998-06-14 15:24:15 +000042
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000043/***********************************************************************
Alexandre Julliarda061b842002-06-04 17:48:41 +000044 * build_list
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000045 *
Alexandre Julliarda061b842002-06-04 17:48:41 +000046 * Build a function list from a ';'-separated string.
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000047 */
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000048static const char **build_list( const WCHAR *bufferW )
Alexandre Julliarda061b842002-06-04 17:48:41 +000049{
50 int count = 1;
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000051 char buffer[1024];
Alexandre Julliarda061b842002-06-04 17:48:41 +000052 const char *p = buffer;
53 const char **ret;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000054
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000055 RtlUnicodeToMultiByteN( buffer, sizeof(buffer), NULL,
56 bufferW, (strlenW(bufferW)+1) * sizeof(WCHAR) );
57
Alexandre Julliarda061b842002-06-04 17:48:41 +000058 while ((p = strchr( p, ';' )))
59 {
60 count++;
61 p++;
62 }
63 /* allocate count+1 pointers, plus the space for a copy of the string */
64 if ((ret = HeapAlloc( GetProcessHeap(), 0, (count+1) * sizeof(char*) + strlen(buffer) + 1 )))
65 {
66 char *str = (char *)(ret + count + 1);
67 char *p = str;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000068
Alexandre Julliarda061b842002-06-04 17:48:41 +000069 strcpy( str, buffer );
70 count = 0;
71 for (;;)
72 {
73 ret[count++] = p;
74 if (!(p = strchr( p, ';' ))) break;
75 *p++ = 0;
76 }
77 ret[count++] = NULL;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +000078 }
Alexandre Julliarda061b842002-06-04 17:48:41 +000079 return ret;
80}
81
82
83/***********************************************************************
84 * RELAY_InitDebugLists
85 *
86 * Build the relay include/exclude function lists.
87 */
88void RELAY_InitDebugLists(void)
89{
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000090 OBJECT_ATTRIBUTES attr;
91 UNICODE_STRING name;
Alexandre Julliarda061b842002-06-04 17:48:41 +000092 char buffer[1024];
93 HKEY hkey;
Alexandre Julliard81bdcf12002-09-13 17:47:44 +000094 DWORD count;
95 WCHAR *str;
96 static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
97 'S','o','f','t','w','a','r','e','\\',
98 'W','i','n','e','\\',
99 'W','i','n','e','\\',
100 'C','o','n','f','i','g','\\',
101 'D','e','b','u','g',0};
102 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
103 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
104 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
105 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
Alexandre Julliarda061b842002-06-04 17:48:41 +0000106
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000107 attr.Length = sizeof(attr);
108 attr.RootDirectory = 0;
109 attr.ObjectName = &name;
110 attr.Attributes = 0;
111 attr.SecurityDescriptor = NULL;
112 attr.SecurityQualityOfService = NULL;
113 RtlInitUnicodeString( &name, configW );
Alexandre Julliarda061b842002-06-04 17:48:41 +0000114
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000115 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return;
116
117 str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
118 RtlInitUnicodeString( &name, RelayIncludeW );
119 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
Alexandre Julliarda061b842002-06-04 17:48:41 +0000120 {
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000121 TRACE("RelayInclude = %s\n", debugstr_w(str) );
122 debug_relay_includelist = build_list( str );
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +0000123 }
Alexandre Julliarda061b842002-06-04 17:48:41 +0000124
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000125 RtlInitUnicodeString( &name, RelayExcludeW );
126 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
Alexandre Julliarda061b842002-06-04 17:48:41 +0000127 {
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000128 TRACE( "RelayExclude = %s\n", debugstr_w(str) );
129 debug_relay_excludelist = build_list( str );
Alexandre Julliarda061b842002-06-04 17:48:41 +0000130 }
131
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000132 RtlInitUnicodeString( &name, SnoopIncludeW );
133 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
Alexandre Julliarda061b842002-06-04 17:48:41 +0000134 {
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000135 TRACE_(snoop)( "SnoopInclude = %s\n", debugstr_w(str) );
136 debug_snoop_includelist = build_list( str );
Alexandre Julliarda061b842002-06-04 17:48:41 +0000137 }
138
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000139 RtlInitUnicodeString( &name, SnoopExcludeW );
140 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
Alexandre Julliarda061b842002-06-04 17:48:41 +0000141 {
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000142 TRACE_(snoop)( "SnoopExclude = %s\n", debugstr_w(str) );
143 debug_snoop_excludelist = build_list( str );
Alexandre Julliarda061b842002-06-04 17:48:41 +0000144 }
145
Alexandre Julliard81bdcf12002-09-13 17:47:44 +0000146 NtClose( hkey );
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +0000147}
148
Alexandre Julliard06b97891999-05-13 16:13:17 +0000149
Ulrich Weigand6ca85a51999-11-14 21:28:57 +0000150#ifdef __i386__
151
Alexandre Julliard246c3602000-05-10 03:48:00 +0000152typedef struct
153{
154 BYTE call; /* 0xe8 call callfrom32 (relative) */
155 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
156 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
157 WORD args; /* nb of args to remove from the stack */
Alexandre Julliarda8378492000-10-01 01:33:50 +0000158 void *orig; /* original entry point */
Alexandre Julliard246c3602000-05-10 03:48:00 +0000159 DWORD argtypes; /* argument types */
160} DEBUG_ENTRY_POINT;
161
162
163/***********************************************************************
Alexandre Julliarda061b842002-06-04 17:48:41 +0000164 * check_relay_include
165 *
166 * Check if a given function must be included in the relay output.
167 */
168static BOOL check_relay_include( const char *module, const char *func )
169{
170 const char **listitem;
171 BOOL show;
172
173 if (!debug_relay_excludelist && !debug_relay_includelist) return TRUE;
174 if (debug_relay_excludelist)
175 {
176 show = TRUE;
177 listitem = debug_relay_excludelist;
178 }
179 else
180 {
181 show = FALSE;
182 listitem = debug_relay_includelist;
183 }
184 for(; *listitem; listitem++)
185 {
Alexandre Julliard6da43662002-07-08 20:46:58 +0000186 char *p = strrchr( *listitem, '.' );
Alexandre Julliarda061b842002-06-04 17:48:41 +0000187 if (p && p > *listitem) /* check module and function */
188 {
189 int len = p - *listitem;
190 if (strncasecmp( *listitem, module, len-1 ) || module[len]) continue;
Alexandre Julliard6da43662002-07-08 20:46:58 +0000191 if (!strcmp( p + 1, func ) || !strcmp( p + 1, "*" )) return !show;
Alexandre Julliarda061b842002-06-04 17:48:41 +0000192 }
193 else /* function only */
194 {
195 if (!strcmp( *listitem, func )) return !show;
196 }
197 }
198 return show;
199}
200
201
202/***********************************************************************
Alexandre Julliard246c3602000-05-10 03:48:00 +0000203 * find_exported_name
204 *
205 * Find the name of an exported function.
206 */
207static const char *find_exported_name( const char *module,
208 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
209{
210 int i;
211 const char *ret = NULL;
212
213 WORD *ordptr = (WORD *)(module + exp->AddressOfNameOrdinals);
214 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
215 if (*ordptr + exp->Base == ordinal) break;
216 if (i < exp->NumberOfNames)
217 ret = module + ((DWORD*)(module + exp->AddressOfNames))[i];
218 return ret;
219}
220
221
222/***********************************************************************
223 * get_entry_point
224 *
225 * Get the name of the DLL entry point corresponding to a relay address.
226 */
227static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
228{
Alexandre Julliard246c3602000-05-10 03:48:00 +0000229 IMAGE_EXPORT_DIRECTORY *exp = NULL;
230 DEBUG_ENTRY_POINT *debug;
Alexandre Julliard6da43662002-07-08 20:46:58 +0000231 char *p, *base = NULL;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000232 const char *name;
233 int ordinal = 0;
234 WINE_MODREF *wm;
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000235 DWORD size;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000236
237 /* First find the module */
238
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000239 for (wm = MODULE_modref_list; wm; wm = wm->next)
Alexandre Julliard246c3602000-05-10 03:48:00 +0000240 {
Alexandre Julliard246c3602000-05-10 03:48:00 +0000241 if (!(wm->flags & WINE_MODREF_INTERNAL)) continue;
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000242 exp = RtlImageDirectoryEntryToData( wm->module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
243 if (!exp) continue;
244 debug = (DEBUG_ENTRY_POINT *)((char *)exp + size);
Alexandre Julliard246c3602000-05-10 03:48:00 +0000245 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
246 {
247 ordinal = relay - debug;
248 break;
249 }
250 }
251
252 /* Now find the function */
253
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000254 base = (char *)wm->module;
Alexandre Julliard6da43662002-07-08 20:46:58 +0000255 strcpy( buffer, base + exp->Name );
256 p = buffer + strlen(buffer);
257 if (p > buffer + 4 && !strcasecmp( p - 4, ".dll" )) p -= 4;
258
Dmitry Timoshkov4ea3c262001-04-09 18:47:10 +0000259 if ((name = find_exported_name( base, exp, ordinal + exp->Base )))
Alexandre Julliard6da43662002-07-08 20:46:58 +0000260 sprintf( p, ".%s", name );
Dmitry Timoshkov4ea3c262001-04-09 18:47:10 +0000261 else
Alexandre Julliard6da43662002-07-08 20:46:58 +0000262 sprintf( p, ".%ld", ordinal + exp->Base );
Alexandre Julliard246c3602000-05-10 03:48:00 +0000263}
264
265
Alexandre Julliard06b97891999-05-13 16:13:17 +0000266/***********************************************************************
267 * RELAY_PrintArgs
268 */
269static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
270{
271 while (nb_args--)
272 {
273 if ((typemask & 3) && HIWORD(*args))
274 {
275 if (typemask & 2)
Francois Gougete73b8b81999-12-26 00:40:37 +0000276 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
Alexandre Julliard06b97891999-05-13 16:13:17 +0000277 else
278 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
279 }
280 else DPRINTF( "%08x", *args );
281 if (nb_args) DPRINTF( "," );
282 args++;
283 typemask >>= 2;
284 }
285}
286
287
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000288typedef LONGLONG (*LONGLONG_CPROC)();
Patrik Stridvall57e57842002-02-02 18:42:11 +0000289typedef LONGLONG (WINAPI *LONGLONG_FARPROC)();
Alexandre Julliard3f2b2d52000-10-12 20:55:26 +0000290
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000291
292/***********************************************************************
293 * call_cdecl_function
294 */
295static LONGLONG call_cdecl_function( LONGLONG_CPROC func, int nb_args, const int *args )
296{
297 LONGLONG ret;
298 switch(nb_args)
299 {
300 case 0: ret = func(); break;
301 case 1: ret = func(args[0]); break;
302 case 2: ret = func(args[0],args[1]); break;
303 case 3: ret = func(args[0],args[1],args[2]); break;
304 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
305 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
306 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
307 args[5]); break;
308 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
309 args[6]); break;
310 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
311 args[6],args[7]); break;
312 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
313 args[6],args[7],args[8]); break;
314 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
315 args[6],args[7],args[8],args[9]); break;
316 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
317 args[6],args[7],args[8],args[9],args[10]); break;
318 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
319 args[6],args[7],args[8],args[9],args[10],
320 args[11]); break;
321 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
322 args[6],args[7],args[8],args[9],args[10],args[11],
323 args[12]); break;
324 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
325 args[6],args[7],args[8],args[9],args[10],args[11],
326 args[12],args[13]); break;
327 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
328 args[6],args[7],args[8],args[9],args[10],args[11],
329 args[12],args[13],args[14]); break;
330 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
331 args[6],args[7],args[8],args[9],args[10],args[11],
332 args[12],args[13],args[14],args[15]); break;
333 default:
334 ERR( "Unsupported nb of args %d\n", nb_args );
335 assert(FALSE);
336 }
337 return ret;
338}
339
340
341/***********************************************************************
342 * call_stdcall_function
343 */
344static LONGLONG call_stdcall_function( LONGLONG_FARPROC func, int nb_args, const int *args )
345{
346 LONGLONG ret;
347 switch(nb_args)
348 {
349 case 0: ret = func(); break;
350 case 1: ret = func(args[0]); break;
351 case 2: ret = func(args[0],args[1]); break;
352 case 3: ret = func(args[0],args[1],args[2]); break;
353 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
354 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
355 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
356 args[5]); break;
357 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
358 args[6]); break;
359 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
360 args[6],args[7]); break;
361 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
362 args[6],args[7],args[8]); break;
363 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
364 args[6],args[7],args[8],args[9]); break;
365 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
366 args[6],args[7],args[8],args[9],args[10]); break;
367 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
368 args[6],args[7],args[8],args[9],args[10],
369 args[11]); break;
370 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
371 args[6],args[7],args[8],args[9],args[10],args[11],
372 args[12]); break;
373 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
374 args[6],args[7],args[8],args[9],args[10],args[11],
375 args[12],args[13]); break;
376 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
377 args[6],args[7],args[8],args[9],args[10],args[11],
378 args[12],args[13],args[14]); break;
379 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
380 args[6],args[7],args[8],args[9],args[10],args[11],
381 args[12],args[13],args[14],args[15]); break;
382 default:
383 ERR( "Unsupported nb of args %d\n", nb_args );
384 assert(FALSE);
385 }
386 return ret;
387}
388
389
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +0000390/***********************************************************************
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000391 * RELAY_CallFrom32
392 *
393 * Stack layout on entry to this function:
394 * ... ...
395 * (esp+12) arg2
396 * (esp+8) arg1
397 * (esp+4) ret_addr
398 * (esp) return addr to relay code
399 */
Alexandre Julliarda8378492000-10-01 01:33:50 +0000400static LONGLONG RELAY_CallFrom32( int ret_addr, ... )
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000401{
Alexandre Julliarda8378492000-10-01 01:33:50 +0000402 LONGLONG ret;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000403 char buffer[80];
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000404
Alexandre Julliard06b97891999-05-13 16:13:17 +0000405 int *args = &ret_addr + 1;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000406 /* Relay addr is the return address for this function */
Alexandre Julliard06b97891999-05-13 16:13:17 +0000407 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
Alexandre Julliard246c3602000-05-10 03:48:00 +0000408 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
409 WORD nb_args = relay->args / sizeof(int);
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000410
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000411 if (TRACE_ON(relay))
412 {
413 get_entry_point( buffer, relay );
Alexandre Julliard246c3602000-05-10 03:48:00 +0000414
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000415 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
416 RELAY_PrintArgs( args, nb_args, relay->argtypes );
417 DPRINTF( ") ret=%08x\n", ret_addr );
418 }
Marcus Meissner38980e41998-11-22 14:12:36 +0000419
Alexandre Julliard246c3602000-05-10 03:48:00 +0000420 if (relay->ret == 0xc3) /* cdecl */
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000421 {
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000422 ret = call_cdecl_function( (LONGLONG_CPROC)relay->orig, nb_args, args );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000423 }
424 else /* stdcall */
425 {
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000426 ret = call_stdcall_function( (LONGLONG_FARPROC)relay->orig, nb_args, args );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000427 }
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000428
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000429 if (TRACE_ON(relay))
430 {
431 BOOL ret64 = (relay->argtypes & 0x80000000) && (nb_args < 16);
432 if (ret64)
433 DPRINTF( "%08lx:Ret %s() retval=%08x%08x ret=%08x\n",
434 GetCurrentThreadId(),
435 buffer, (UINT)(ret >> 32), (UINT)ret, ret_addr );
436 else
437 DPRINTF( "%08lx:Ret %s() retval=%08x ret=%08x\n",
438 GetCurrentThreadId(),
439 buffer, (UINT)ret, ret_addr );
440 }
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000441 return ret;
442}
443
444
445/***********************************************************************
446 * RELAY_CallFrom32Regs
447 *
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000448 * Stack layout (esp is context->Esp, not the current %esp):
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000449 *
Alexandre Julliard06b97891999-05-13 16:13:17 +0000450 * ...
451 * (esp+4) first arg
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000452 * (esp) return addr to caller
Alexandre Julliard06b97891999-05-13 16:13:17 +0000453 * (esp-4) return addr to DEBUG_ENTRY_POINT
454 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000455 * ... >128 bytes space free to be modified (ensured by the assembly glue)
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000456 */
Ulrich Weigand6ca85a51999-11-14 21:28:57 +0000457void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000458{
Alexandre Julliard06b97891999-05-13 16:13:17 +0000459 char buffer[80];
460 int* args;
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000461 int args_copy[17];
Alexandre Julliard06b97891999-05-13 16:13:17 +0000462 BYTE *entry_point;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000463
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000464 BYTE *relay_addr = *((BYTE **)context->Esp - 1);
Alexandre Julliard246c3602000-05-10 03:48:00 +0000465 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
466 WORD nb_args = (relay->args & ~0x8000) / sizeof(int);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000467
Alexandre Julliard06b97891999-05-13 16:13:17 +0000468 /* remove extra stuff from the stack */
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000469 context->Eip = stack32_pop(context);
470 args = (int *)context->Esp;
Alexandre Julliard6a9325d2001-12-19 19:17:09 +0000471 if (relay->ret == 0xc2) /* stdcall */
472 context->Esp += nb_args * sizeof(int);
Alexandre Julliard06b97891999-05-13 16:13:17 +0000473
Alexandre Julliard246c3602000-05-10 03:48:00 +0000474 entry_point = (BYTE *)relay->orig;
Alexandre Julliard06b97891999-05-13 16:13:17 +0000475 assert( *entry_point == 0xe8 /* lcall */ );
Alexandre Julliard06b97891999-05-13 16:13:17 +0000476
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000477 if (TRACE_ON(relay))
478 {
479 get_entry_point( buffer, relay );
Alexandre Julliard246c3602000-05-10 03:48:00 +0000480
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000481 DPRINTF( "%08lx:Call %s(", GetCurrentThreadId(), buffer );
482 RELAY_PrintArgs( args, nb_args, relay->argtypes );
483 DPRINTF( ") ret=%08lx fs=%04lx\n", context->Eip, context->SegFs );
Alexandre Julliard06b97891999-05-13 16:13:17 +0000484
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000485 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
486 context->Eax, context->Ebx, context->Ecx,
487 context->Edx, context->Esi, context->Edi );
488 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
489 context->Ebp, context->Esp, context->SegDs,
490 context->SegEs, context->SegGs, context->EFlags );
491 }
Alexandre Julliard06b97891999-05-13 16:13:17 +0000492
493 /* Now call the real function */
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000494
495 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
496 args_copy[nb_args] = (int)context; /* append context argument */
497 if (relay->ret == 0xc3) /* cdecl */
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000498 {
Alexandre Julliard7662ea12001-12-14 23:14:22 +0000499 call_cdecl_function( *(LONGLONG_CPROC *)(entry_point + 5), nb_args+1, args_copy );
500 }
501 else /* stdcall */
502 {
503 call_stdcall_function( *(LONGLONG_FARPROC *)(entry_point + 5), nb_args+1, args_copy );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000504 }
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000505
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000506 if (TRACE_ON(relay))
507 {
508 DPRINTF( "%08lx:Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
509 GetCurrentThreadId(),
510 buffer, context->Eax, context->Eip, context->SegFs );
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000511
Alexandre Julliard1cf02612002-06-04 21:25:34 +0000512 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
513 context->Eax, context->Ebx, context->Ecx,
514 context->Edx, context->Esi, context->Edi );
515 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
516 context->Ebp, context->Esp, context->SegDs,
517 context->SegEs, context->SegGs, context->EFlags );
518 }
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000519}
Alexandre Julliard06b97891999-05-13 16:13:17 +0000520
Alexandre Julliard85d666a2000-12-12 00:49:45 +0000521void WINAPI RELAY_CallFrom32Regs(void);
522__ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs,
Alexandre Julliardeb9a8632001-12-11 00:50:33 +0000523 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
Alexandre Julliard85d666a2000-12-12 00:49:45 +0000524 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
Alexandre Julliard246c3602000-05-10 03:48:00 +0000525
526/***********************************************************************
527 * RELAY_SetupDLL
528 *
529 * Setup relay debugging for a built-in dll.
530 */
531void RELAY_SetupDLL( const char *module )
532{
Alexandre Julliard246c3602000-05-10 03:48:00 +0000533 IMAGE_EXPORT_DIRECTORY *exports;
534 DEBUG_ENTRY_POINT *debug;
535 DWORD *funcs;
536 int i;
Alexandre Julliard6da43662002-07-08 20:46:58 +0000537 const char *name;
538 char *p, dllname[80];
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000539 DWORD size;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000540
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000541 exports = RtlImageDirectoryEntryToData( (HMODULE)module, TRUE,
542 IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
543 if (!exports) return;
544 debug = (DEBUG_ENTRY_POINT *)((char *)exports + size);
Alexandre Julliard246c3602000-05-10 03:48:00 +0000545 funcs = (DWORD *)(module + exports->AddressOfFunctions);
Alexandre Julliard6da43662002-07-08 20:46:58 +0000546 strcpy( dllname, module + exports->Name );
547 p = dllname + strlen(dllname) - 4;
548 if (p > dllname && !strcasecmp( p, ".dll" )) *p = 0;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000549
550 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
551 {
552 int on = 1;
553
554 if (!debug->call) continue; /* not a normal function */
Alexandre Julliard1a5e22f2000-11-27 23:48:08 +0000555 if (debug->call != 0xe8 && debug->call != 0xe9) break; /* not a debug thunk at all */
Alexandre Julliard246c3602000-05-10 03:48:00 +0000556
557 if ((name = find_exported_name( module, exports, i + exports->Base )))
Alexandre Julliarda061b842002-06-04 17:48:41 +0000558 on = check_relay_include( dllname, name );
Alexandre Julliard246c3602000-05-10 03:48:00 +0000559
560 if (on)
561 {
562 debug->call = 0xe8; /* call relative */
563 if (debug->args & 0x8000) /* register func */
564 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
565 else
566 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
567 }
568 else
569 {
570 debug->call = 0xe9; /* jmp relative */
571 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
572 }
573
574 debug->orig = (FARPROC)(module + (DWORD)*funcs);
575 *funcs = (char *)debug - module;
576 }
577}
578
579#else /* __i386__ */
580
581void RELAY_SetupDLL( const char *module )
582{
583}
584
585#endif /* __i386__ */