Fix GetCurrentDirectoryA and W to return the needed space for the CWD,
[wine/multimedia.git] / relay32 / relay386.c
blobb58c3b5b89cf9093420d4cb5cc86eef72270d532
1 /*
2 * 386-specific Win32 relay functions
4 * Copyright 1997 Alexandre Julliard
5 */
8 #include <assert.h>
9 #include <string.h>
10 #include "winnt.h"
11 #include "builtin32.h"
12 #include "selectors.h"
13 #include "stackframe.h"
14 #include "syslevel.h"
15 #include "debugstr.h"
16 #include "main.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 && !lstrncmpiA(*listitem, func, len)) ||
52 (itemlen == len2 && !lstrncmpiA(*listitem, func, len2)) ||
53 !lstrcmpiA(*listitem, term)) {
54 show = !show;
55 break;
58 return show;
60 return 1;
64 #ifdef __i386__
66 /***********************************************************************
67 * RELAY_PrintArgs
69 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
71 while (nb_args--)
73 if ((typemask & 3) && HIWORD(*args))
75 if (typemask & 2)
76 DPRINTF( "%08x L%s", *args, debugstr_w((LPWSTR)*args) );
77 else
78 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
80 else DPRINTF( "%08x", *args );
81 if (nb_args) DPRINTF( "," );
82 args++;
83 typemask >>= 2;
88 /***********************************************************************
89 * RELAY_CallFrom32
91 * Stack layout on entry to this function:
92 * ... ...
93 * (esp+12) arg2
94 * (esp+8) arg1
95 * (esp+4) ret_addr
96 * (esp) return addr to relay code
98 int RELAY_CallFrom32( int ret_addr, ... )
100 int ret;
101 char buffer[80];
102 unsigned int typemask;
103 FARPROC func;
104 WORD fs;
106 int *args = &ret_addr + 1;
107 /* Relay addr is the return address for this function */
108 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
109 WORD nb_args = *(WORD *)(relay_addr + 1) / sizeof(int);
111 assert(TRACE_ON(relay));
112 func = (FARPROC)BUILTIN32_GetEntryPoint( buffer, relay_addr - 5, &typemask );
113 DPRINTF( "Call %s(", buffer );
114 RELAY_PrintArgs( args, nb_args, typemask );
115 GET_FS( fs );
116 DPRINTF( ") ret=%08x fs=%04x\n", ret_addr, fs );
118 SYSLEVEL_CheckNotLevel( 2 );
120 if (*relay_addr == 0xc3) /* cdecl */
122 LRESULT (*cfunc)() = (LRESULT(*)())func;
123 switch(nb_args)
125 case 0: ret = cfunc(); break;
126 case 1: ret = cfunc(args[0]); break;
127 case 2: ret = cfunc(args[0],args[1]); break;
128 case 3: ret = cfunc(args[0],args[1],args[2]); break;
129 case 4: ret = cfunc(args[0],args[1],args[2],args[3]); break;
130 case 5: ret = cfunc(args[0],args[1],args[2],args[3],args[4]); break;
131 case 6: ret = cfunc(args[0],args[1],args[2],args[3],args[4],
132 args[5]); break;
133 case 7: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
134 args[6]); break;
135 case 8: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
136 args[6],args[7]); break;
137 case 9: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
138 args[6],args[7],args[8]); break;
139 case 10: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
140 args[6],args[7],args[8],args[9]); break;
141 case 11: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
142 args[6],args[7],args[8],args[9],args[10]); break;
143 case 12: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
144 args[6],args[7],args[8],args[9],args[10],
145 args[11]); break;
146 case 13: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
147 args[6],args[7],args[8],args[9],args[10],args[11],
148 args[12]); break;
149 case 14: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
150 args[6],args[7],args[8],args[9],args[10],args[11],
151 args[12],args[13]); break;
152 case 15: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
153 args[6],args[7],args[8],args[9],args[10],args[11],
154 args[12],args[13],args[14]); break;
155 case 16: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
156 args[6],args[7],args[8],args[9],args[10],args[11],
157 args[12],args[13],args[14],args[15]); break;
158 default:
159 ERR( "Unsupported nb of args %d\n", nb_args );
160 assert(FALSE);
163 else /* stdcall */
165 switch(nb_args)
167 case 0: ret = func(); break;
168 case 1: ret = func(args[0]); break;
169 case 2: ret = func(args[0],args[1]); break;
170 case 3: ret = func(args[0],args[1],args[2]); break;
171 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
172 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
173 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
174 args[5]); break;
175 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
176 args[6]); break;
177 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
178 args[6],args[7]); break;
179 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
180 args[6],args[7],args[8]); break;
181 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
182 args[6],args[7],args[8],args[9]); break;
183 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
184 args[6],args[7],args[8],args[9],args[10]); break;
185 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
186 args[6],args[7],args[8],args[9],args[10],
187 args[11]); break;
188 case 13: 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],args[11],
190 args[12]); break;
191 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
192 args[6],args[7],args[8],args[9],args[10],args[11],
193 args[12],args[13]); break;
194 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
195 args[6],args[7],args[8],args[9],args[10],args[11],
196 args[12],args[13],args[14]); break;
197 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
198 args[6],args[7],args[8],args[9],args[10],args[11],
199 args[12],args[13],args[14],args[15]); break;
200 default:
201 ERR( "Unsupported nb of args %d\n", nb_args );
202 assert(FALSE);
205 DPRINTF( "Ret %s() retval=%08x ret=%08x fs=%04x\n",
206 buffer, ret, ret_addr, fs );
208 SYSLEVEL_CheckNotLevel( 2 );
210 return ret;
214 /***********************************************************************
215 * RELAY_CallFrom32Regs
217 * Stack layout (esp is ESP_reg(context), not the current %esp):
219 * ...
220 * (esp+4) first arg
221 * (esp) return addr to caller
222 * (esp-4) return addr to DEBUG_ENTRY_POINT
223 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
224 * ... >128 bytes space free to be modified (ensured by the assembly glue)
227 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context );
228 DEFINE_REGS_ENTRYPOINT_0( RELAY_CallFrom32Regs, RELAY_DoCallFrom32Regs )
229 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
231 unsigned int typemask;
232 char buffer[80];
233 int* args;
234 FARPROC func;
235 BYTE *entry_point;
237 BYTE *relay_addr = *((BYTE **)ESP_reg(context) - 1);
238 WORD nb_args = *(WORD *)(relay_addr + 1) / sizeof(int);
240 /* remove extra stuff from the stack */
241 EIP_reg(context) = stack32_pop(context);
242 args = (int *)ESP_reg(context);
243 ESP_reg(context) += 4 * nb_args;
245 assert(TRACE_ON(relay));
247 entry_point = (BYTE *)BUILTIN32_GetEntryPoint( buffer, relay_addr - 5, &typemask );
248 assert( *entry_point == 0xe8 /* lcall */ );
249 func = *(FARPROC *)(entry_point + 5);
251 DPRINTF( "Call %s(", buffer );
252 RELAY_PrintArgs( args, nb_args, typemask );
253 DPRINTF( ") ret=%08lx fs=%04lx\n", EIP_reg(context), FS_reg(context) );
255 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
256 EAX_reg(context), EBX_reg(context), ECX_reg(context),
257 EDX_reg(context), ESI_reg(context), EDI_reg(context) );
258 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
259 EBP_reg(context), ESP_reg(context), DS_reg(context),
260 ES_reg(context), GS_reg(context), EFL_reg(context) );
262 SYSLEVEL_CheckNotLevel( 2 );
264 /* Now call the real function */
265 switch(nb_args)
267 case 0: func(context); break;
268 case 1: func(args[0],context); break;
269 case 2: func(args[0],args[1],context); break;
270 case 3: func(args[0],args[1],args[2],context); break;
271 case 4: func(args[0],args[1],args[2],args[3],context); break;
272 case 5: func(args[0],args[1],args[2],args[3],args[4],context); break;
273 case 6: func(args[0],args[1],args[2],args[3],args[4],args[5],context); break;
274 case 7: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],context); break;
275 case 8: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],context); break;
276 case 9: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
277 context); break;
278 case 10: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
279 args[9],context); break;
280 case 11: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
281 args[9],args[10],context); break;
282 case 12: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
283 args[9],args[10],args[11],context); break;
284 case 13: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
285 args[9],args[10],args[11],args[12],context); break;
286 case 14: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
287 args[9],args[10],args[11],args[12],args[13],context); break;
288 case 15: func(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],
289 args[9],args[10],args[11],args[12],args[13],args[14],context); break;
290 case 16: func(args[0],args[1],args[2],args[3],args[4],args[5], args[6],args[7],args[8],
291 args[9],args[10],args[11],args[12],args[13],args[14],args[15],context); break;
292 default:
293 ERR( "Unsupported nb of args %d\n", nb_args );
294 assert(FALSE);
297 DPRINTF( "Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
298 buffer, EAX_reg(context), EIP_reg(context), FS_reg(context) );
299 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
300 EAX_reg(context), EBX_reg(context), ECX_reg(context),
301 EDX_reg(context), ESI_reg(context), EDI_reg(context) );
302 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
303 EBP_reg(context), ESP_reg(context), DS_reg(context),
304 ES_reg(context), GS_reg(context), EFL_reg(context) );
306 SYSLEVEL_CheckNotLevel( 2 );
308 #endif /* __i386__ */