Make wineinstall work in the new-autoconf-version world.
[wine/testsucceed.git] / tools / winebuild / relay.c
blobb944c90d91037e68bc62fb643f9a0e5d9a1871fd
1 /*
2 * Relay calls helper routines
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
9 */
11 #include "config.h"
13 #include <ctype.h>
14 #include <unistd.h>
16 #include "winnt.h"
17 #include "thread.h"
18 #include "stackframe.h"
20 #include "build.h"
22 #ifdef __i386__
24 static void function_header( FILE *outfile, const char *name )
26 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
27 #ifdef USE_STABS
28 fprintf( outfile, "\t.stabs \"%s:F1\",36,0,0," PREFIX "%s\n", name, name);
29 #endif
30 #ifdef NEED_TYPE_IN_DEF
31 fprintf( outfile, "\t.def " PREFIX "%s; .scl 2; .type 32; .endef\n", name );
32 #else
33 fprintf( outfile, "\t.type " PREFIX "%s,@function\n", name );
34 #endif
35 fprintf( outfile, "\t.globl " PREFIX "%s\n", name );
36 fprintf( outfile, PREFIX "%s:\n", name );
40 /*******************************************************************
41 * BuildCallFrom16Core
43 * This routine builds the core routines used in 16->32 thunks:
44 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
46 * These routines are intended to be called via a far call (with 32-bit
47 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
48 * the 32-bit entry point to be called, and the argument conversion
49 * routine to be used (see stack layout below).
51 * The core routine completes the STACK16FRAME on the 16-bit stack and
52 * switches to the 32-bit stack. Then, the argument conversion routine
53 * is called; it gets passed the 32-bit entry point and a pointer to the
54 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
55 * use conversion routines automatically generated by BuildCallFrom16,
56 * or write your own for special purposes.)
58 * The conversion routine must call the 32-bit entry point, passing it
59 * the converted arguments, and return its return value to the core.
60 * After the conversion routine has returned, the core switches back
61 * to the 16-bit stack, converts the return value to the DX:AX format
62 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
63 * including %bp, are popped off the stack.
65 * The 16-bit call stub now returns to the caller, popping the 16-bit
66 * arguments if necessary (pascal calling convention).
68 * In the case of a 'register' function, CallFrom16Register fills a
69 * CONTEXT86 structure with the values all registers had at the point
70 * the first instruction of the 16-bit call stub was about to be
71 * executed. A pointer to this CONTEXT86 is passed as third parameter
72 * to the argument conversion routine, which typically passes it on
73 * to the called 32-bit entry point.
75 * CallFrom16Thunk is a special variant used by the implementation of
76 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
77 * implemented as follows:
78 * On entry, the EBX register is set up to contain a flat pointer to the
79 * 16-bit stack such that EBX+22 points to the first argument.
80 * Then, the entry point is called, while EBP is set up to point
81 * to the return address (on the 32-bit stack).
82 * The called function returns with CX set to the number of bytes
83 * to be popped of the caller's stack.
85 * Stack layout upon entry to the core routine (STACK16FRAME):
86 * ... ...
87 * (sp+24) word first 16-bit arg
88 * (sp+22) word cs
89 * (sp+20) word ip
90 * (sp+18) word bp
91 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
92 * (sp+12) word ip of actual entry point (necessary for relay debugging)
93 * (sp+8) long relay (argument conversion) function entry point
94 * (sp+4) long cs of 16-bit entry point
95 * (sp) long ip of 16-bit entry point
97 * Added on the stack:
98 * (sp-2) word saved gs
99 * (sp-4) word saved fs
100 * (sp-6) word saved es
101 * (sp-8) word saved ds
102 * (sp-12) long saved ebp
103 * (sp-16) long saved ecx
104 * (sp-20) long saved edx
105 * (sp-24) long saved previous stack
107 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
109 char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
111 /* Function header */
112 if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
113 else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
114 else if (short_ret) function_header( outfile, "__wine_call_from_16_word" );
115 else function_header( outfile, "__wine_call_from_16_long" );
117 /* Create STACK16FRAME (except STACK32FRAME link) */
118 fprintf( outfile, "\tpushw %%gs\n" );
119 fprintf( outfile, "\tpushw %%fs\n" );
120 fprintf( outfile, "\tpushw %%es\n" );
121 fprintf( outfile, "\tpushw %%ds\n" );
122 fprintf( outfile, "\tpushl %%ebp\n" );
123 fprintf( outfile, "\tpushl %%ecx\n" );
124 fprintf( outfile, "\tpushl %%edx\n" );
126 /* Save original EFlags register */
127 fprintf( outfile, "\tpushfl\n" );
129 if ( UsePIC )
131 /* Get Global Offset Table into %ecx */
132 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name );
133 fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name );
134 fprintf( outfile, "\tpopl %%ecx\n" );
135 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name );
138 if (UsePIC)
140 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector@GOT(%%ecx), %%edx\n" );
141 fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
143 else
144 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector,%%edx\n" );
146 /* Load 32-bit segment registers */
147 #ifdef __svr4__
148 fprintf( outfile, "\tdata16\n");
149 #endif
150 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
151 #ifdef __svr4__
152 fprintf( outfile, "\tdata16\n");
153 #endif
154 fprintf( outfile, "\tmovw %%dx, %%es\n" );
156 if ( UsePIC )
158 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
159 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
161 else
162 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
164 /* Get address of wine_ldt_copy array into %ecx */
165 if ( UsePIC )
166 fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy@GOT(%%ecx), %%ecx\n" );
167 else
168 fprintf( outfile, "\tmovl $" PREFIX "wine_ldt_copy, %%ecx\n" );
170 /* Translate STACK16FRAME base to flat offset in %edx */
171 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
172 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
173 fprintf( outfile, "\tshrl $1, %%edx\n" );
174 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
175 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
176 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
178 /* Get saved flags into %ecx */
179 fprintf( outfile, "\tpopl %%ecx\n" );
181 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
182 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
183 fprintf( outfile, "\tpushl %%ebp\n" );
185 /* Switch stacks */
186 #ifdef __svr4__
187 fprintf( outfile,"\tdata16\n");
188 #endif
189 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
190 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
191 fprintf( outfile, "\tpushl %%ds\n" );
192 fprintf( outfile, "\tpopl %%ss\n" );
193 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
194 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
197 /* At this point:
198 STACK16FRAME is completely set up
199 DS, ES, SS: flat data segment
200 FS: current TEB
201 ESP: points to last STACK32FRAME
202 EBP: points to ebp member of last STACK32FRAME
203 EDX: points to current STACK16FRAME
204 ECX: contains saved flags
205 all other registers: unchanged */
207 /* Special case: C16ThkSL stub */
208 if ( thunk )
210 /* Set up registers as expected and call thunk */
211 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
212 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
214 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
216 /* Switch stack back */
217 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
218 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
219 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
221 /* Restore registers and return directly to caller */
222 fprintf( outfile, "\taddl $8, %%esp\n" );
223 fprintf( outfile, "\tpopl %%ebp\n" );
224 fprintf( outfile, "\tpopw %%ds\n" );
225 fprintf( outfile, "\tpopw %%es\n" );
226 fprintf( outfile, "\tpopw %%fs\n" );
227 fprintf( outfile, "\tpopw %%gs\n" );
228 fprintf( outfile, "\taddl $20, %%esp\n" );
230 fprintf( outfile, "\txorb %%ch, %%ch\n" );
231 fprintf( outfile, "\tpopl %%ebx\n" );
232 fprintf( outfile, "\taddw %%cx, %%sp\n" );
233 fprintf( outfile, "\tpush %%ebx\n" );
235 fprintf( outfile, "\t.byte 0x66\n" );
236 fprintf( outfile, "\tlret\n" );
238 return;
242 /* Build register CONTEXT */
243 if ( reg_func )
245 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
247 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
249 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
250 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
251 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
252 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
254 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
255 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
256 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
257 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
258 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
259 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
261 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
262 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
263 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
264 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
265 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
266 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
267 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
268 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
270 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
271 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
272 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
273 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
275 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
276 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
277 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
278 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
279 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
280 #if 0
281 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
282 #endif
284 /* Push address of CONTEXT86 structure -- popped by the relay routine */
285 fprintf( outfile, "\tpushl %%esp\n" );
289 /* Print debug info before call */
290 if ( debugging )
292 if ( UsePIC )
294 fprintf( outfile, "\tpushl %%ebx\n" );
296 /* Get Global Offset Table into %ebx (for PLT call) */
297 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name );
298 fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name );
299 fprintf( outfile, "\tpopl %%ebx\n" );
300 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name );
303 fprintf( outfile, "\tpushl %%edx\n" );
304 if ( reg_func )
305 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
306 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
307 else
308 fprintf( outfile, "\tpushl $0\n" );
310 if ( UsePIC )
311 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
312 else
313 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
315 fprintf( outfile, "\tpopl %%edx\n" );
316 fprintf( outfile, "\tpopl %%edx\n" );
318 if ( UsePIC )
319 fprintf( outfile, "\tpopl %%ebx\n" );
322 /* Call relay routine (which will call the API entry point) */
323 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
324 fprintf( outfile, "\tpushl %%eax\n" );
325 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
326 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
328 /* Print debug info after call */
329 if ( debugging )
331 if ( UsePIC )
333 fprintf( outfile, "\tpushl %%ebx\n" );
335 /* Get Global Offset Table into %ebx (for PLT call) */
336 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name );
337 fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name );
338 fprintf( outfile, "\tpopl %%ebx\n" );
339 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name );
342 fprintf( outfile, "\tpushl %%eax\n" );
343 if ( reg_func )
344 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
345 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
346 else
347 fprintf( outfile, "\tpushl $0\n" );
349 if ( UsePIC )
350 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
351 else
352 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
354 fprintf( outfile, "\tpopl %%eax\n" );
355 fprintf( outfile, "\tpopl %%eax\n" );
357 if ( UsePIC )
358 fprintf( outfile, "\tpopl %%ebx\n" );
362 if ( reg_func )
364 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
366 /* Switch stack back */
367 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
368 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
369 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
371 /* Get return address to CallFrom16 stub */
372 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
373 fprintf( outfile, "\tpopl %%eax\n" );
374 fprintf( outfile, "\tpopl %%edx\n" );
376 /* Restore all registers from CONTEXT */
377 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
378 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
379 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
381 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
382 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
383 fprintf( outfile, "\tpushl %%edx\n" );
384 fprintf( outfile, "\tpushl %%eax\n" );
385 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
386 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
388 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
389 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
390 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
392 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
393 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
394 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
395 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
396 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
397 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
398 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
400 fprintf( outfile, "\tpopl %%ds\n" );
401 fprintf( outfile, "\tpopfl\n" );
402 fprintf( outfile, "\tlret\n" );
404 else
406 /* Switch stack back */
407 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
408 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
409 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
411 /* Restore registers */
412 fprintf( outfile, "\tpopl %%edx\n" );
413 fprintf( outfile, "\tpopl %%ecx\n" );
414 fprintf( outfile, "\tpopl %%ebp\n" );
415 fprintf( outfile, "\tpopw %%ds\n" );
416 fprintf( outfile, "\tpopw %%es\n" );
417 fprintf( outfile, "\tpopw %%fs\n" );
418 fprintf( outfile, "\tpopw %%gs\n" );
420 /* Prepare return value and set flags accordingly */
421 if ( !short_ret )
422 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
423 fprintf( outfile, "\torl %%eax, %%eax\n" );
425 /* Return to return stub which will return to caller */
426 fprintf( outfile, "\tlret $12\n" );
431 /*******************************************************************
432 * BuildCallTo16Core
434 * This routine builds the core routines used in 32->16 thunks:
436 * extern void WINAPI wine_call_to_16_word( SEGPTR target, int nb_args );
437 * extern void WINAPI wine_call_to_16_long( SEGPTR target, int nb_args );
438 * extern void WINAPI wine_call_to_16_regs_short( const CONTEXT86 *context, int nb_args );
439 * extern void WINAPI wine_call_to_16_regs_long ( const CONTEXT86 *context, int nb_args );
441 * These routines can be called directly from 32-bit code.
443 * All routines expect that the 16-bit stack contents (arguments) were
444 * already set up by the caller; nb_args must contain the number of bytes
445 * to be conserved. The 16-bit SS:SP will be set accordinly.
447 * All other registers are either taken from the CONTEXT86 structure
448 * or else set to default values. The target routine address is either
449 * given directly or taken from the CONTEXT86.
451 * If you want to call a 16-bit routine taking only standard argument types
452 * (WORD and LONG), you can also have an appropriate argument conversion
453 * stub automatically generated (see BuildCallTo16); you'd then call this
454 * stub, which in turn would prepare the 16-bit stack and call the appropiate
455 * core routine.
458 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
460 char *name = reg_func == 2 ? "regs_long" :
461 reg_func == 1 ? "regs_short" :
462 short_ret? "word" : "long";
464 /* Function header */
465 if (reg_func == 2) function_header( outfile, "wine_call_to_16_regs_long" );
466 else if (reg_func == 1) function_header( outfile, "wine_call_to_16_regs_short" );
467 else if (short_ret) function_header( outfile, "wine_call_to_16_word" );
468 else function_header( outfile, "wine_call_to_16_long" );
470 /* Function entry sequence */
471 fprintf( outfile, "\tpushl %%ebp\n" );
472 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
474 /* Save the 32-bit registers */
475 fprintf( outfile, "\tpushl %%ebx\n" );
476 fprintf( outfile, "\tpushl %%ecx\n" );
477 fprintf( outfile, "\tpushl %%edx\n" );
478 fprintf( outfile, "\tpushl %%esi\n" );
479 fprintf( outfile, "\tpushl %%edi\n" );
481 if ( UsePIC )
483 /* Get Global Offset Table into %ebx */
484 fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot1\n", name );
485 fprintf( outfile, ".Lwine_call_to_16_%s.getgot1:\n", name );
486 fprintf( outfile, "\tpopl %%ebx\n" );
487 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot1], %%ebx\n", name );
490 /* Enter Win16 Mutex */
491 if ( UsePIC )
492 fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock@PLT\n" );
493 else
494 fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock\n" );
496 /* Print debugging info */
497 if (debugging)
499 /* Push flags, number of arguments, and target */
500 fprintf( outfile, "\tpushl $%d\n", reg_func );
501 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
502 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
504 if ( UsePIC )
505 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
506 else
507 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
509 fprintf( outfile, "\taddl $12, %%esp\n" );
512 /* Get return address */
513 if ( UsePIC )
515 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
516 fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" );
518 else
519 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
521 /* Call the actual CallTo16 routine (simulate a lcall) */
522 fprintf( outfile, "\tpushl %%cs\n" );
523 fprintf( outfile, "\tcall .Lwine_call_to_16_%s\n", name );
525 if ( !reg_func )
527 /* Convert and push return value */
528 if ( short_ret )
530 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
531 fprintf( outfile, "\tpushl %%eax\n" );
533 else
535 fprintf( outfile, "\tshll $16,%%edx\n" );
536 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
537 fprintf( outfile, "\tpushl %%edx\n" );
540 else
543 * Modify CONTEXT86 structure to contain new values
545 * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
546 * The segment registers as well as ESI and EDI should
547 * not be modified by a well-behaved 16-bit routine in
548 * any case. [If necessary, we could restore them as well,
549 * at the cost of a somewhat less efficient return path.]
552 fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target)-12 );
553 fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
554 fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
555 fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
556 fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
557 fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
558 fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
559 /* The return glue code saved %esp into %esi */
561 fprintf( outfile, "\tpushl %%edi\n" );
564 if ( UsePIC )
566 /* Get Global Offset Table into %ebx (might have been overwritten) */
567 fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot2\n", name );
568 fprintf( outfile, ".Lwine_call_to_16_%s.getgot2:\n", name );
569 fprintf( outfile, "\tpopl %%ebx\n" );
570 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot2], %%ebx\n", name );
573 /* Print debugging info */
574 if (debugging)
576 fprintf( outfile, "\tpushl $%d\n", reg_func );
578 if ( UsePIC )
579 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
580 else
581 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
583 fprintf( outfile, "\taddl $4, %%esp\n" );
586 /* Leave Win16 Mutex */
587 if ( UsePIC )
588 fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock@PLT\n" );
589 else
590 fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock\n" );
592 /* Get return value */
593 fprintf( outfile, "\tpopl %%eax\n" );
595 /* Restore the 32-bit registers */
596 fprintf( outfile, "\tpopl %%edi\n" );
597 fprintf( outfile, "\tpopl %%esi\n" );
598 fprintf( outfile, "\tpopl %%edx\n" );
599 fprintf( outfile, "\tpopl %%ecx\n" );
600 fprintf( outfile, "\tpopl %%ebx\n" );
602 /* Function exit sequence */
603 fprintf( outfile, "\tpopl %%ebp\n" );
604 fprintf( outfile, "\tret $8\n" );
607 /* Start of the actual CallTo16 routine */
609 fprintf( outfile, ".Lwine_call_to_16_%s:\n", name );
611 /* Complete STACK32FRAME */
612 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
613 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
615 /* Switch to the 16-bit stack */
616 #ifdef __svr4__
617 fprintf( outfile,"\tdata16\n");
618 #endif
619 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
620 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
621 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
623 /* Make %bp point to the previous stackframe (built by CallFrom16) */
624 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
625 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
627 /* Add the specified offset to the new sp */
628 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
630 /* Push the return address
631 * With sreg suffix, we push 16:16 address (normal lret)
632 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
634 if (reg_func != 2)
635 fprintf( outfile, "\tpushl %%ecx\n" );
636 else
638 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
639 fprintf( outfile, "\tpushw $0\n" );
640 fprintf( outfile, "\tpushw %%ax\n" );
641 fprintf( outfile, "\tpushw $0\n" );
642 fprintf( outfile, "\tpushw %%cx\n" );
645 if (reg_func)
647 /* Push the called routine address */
648 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
649 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
650 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
652 /* Get the registers */
653 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
654 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
655 fprintf( outfile, "\tmovw %%ax,%%es\n" );
656 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
657 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
658 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
659 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
660 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
661 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
662 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
663 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
664 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
666 /* Get the 16-bit ds */
667 fprintf( outfile, "\tpopw %%ds\n" );
669 else /* not a register function */
671 /* Push the called routine address */
672 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
674 /* Set %fs to the value saved by the last CallFrom16 */
675 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
676 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
678 /* Set %ds and %es (and %ax just in case) equal to %ss */
679 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
680 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
681 fprintf( outfile, "\tmovw %%ax,%%es\n" );
684 /* Jump to the called routine */
685 fprintf( outfile, "\t.byte 0x66\n" );
686 fprintf( outfile, "\tlret\n" );
690 /*******************************************************************
691 * BuildRet16Func
693 * Build the return code for 16-bit callbacks
695 static void BuildRet16Func( FILE *outfile )
698 * Note: This must reside in the .data section to allow
699 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
702 function_header( outfile, "CallTo16_Ret" );
704 /* Save %esp into %esi */
705 fprintf( outfile, "\tmovl %%esp,%%esi\n" );
707 /* Restore 32-bit segment registers */
709 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector-" PREFIX "Call16_Ret_Start,%%edi\n" );
710 #ifdef __svr4__
711 fprintf( outfile, "\tdata16\n");
712 #endif
713 fprintf( outfile, "\tmovw %%di,%%ds\n" );
714 #ifdef __svr4__
715 fprintf( outfile, "\tdata16\n");
716 #endif
717 fprintf( outfile, "\tmovw %%di,%%es\n" );
719 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
721 /* Restore the 32-bit stack */
723 #ifdef __svr4__
724 fprintf( outfile, "\tdata16\n");
725 #endif
726 fprintf( outfile, "\tmovw %%di,%%ss\n" );
727 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
728 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
730 /* Return to caller */
732 fprintf( outfile, "\tlret\n" );
734 /* Declare the return address and data selector variables */
736 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
737 fprintf( outfile, "\t.globl " PREFIX "CallTo16_DataSelector\n" );
738 fprintf( outfile, PREFIX "CallTo16_DataSelector:\t.long 0\n" );
739 fprintf( outfile, "\t.globl " PREFIX "CallTo16_RetAddr\n" );
740 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
744 /*******************************************************************
745 * BuildCallTo32CBClient
747 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
749 * Since the relay stub is itself 32-bit, this should not be a problem;
750 * unfortunately, the relay stubs are expected to switch back to a
751 * 16-bit stack (and 16-bit code) after completion :-(
753 * This would conflict with our 16- vs. 32-bit stack handling, so
754 * we simply switch *back* to our 32-bit stack before returning to
755 * the caller ...
757 * The CBClient relay stub expects to be called with the following
758 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
759 * stack at the designated places:
761 * ...
762 * (ebp+14) original arguments to the callback routine
763 * (ebp+10) far return address to original caller
764 * (ebp+6) Thunklet target address
765 * (ebp+2) Thunklet relay ID code
766 * (ebp) BP (saved by CBClientGlueSL)
767 * (ebp-2) SI (saved by CBClientGlueSL)
768 * (ebp-4) DI (saved by CBClientGlueSL)
769 * (ebp-6) DS (saved by CBClientGlueSL)
771 * ... buffer space used by the 16-bit side glue for temp copies
773 * (ebx+4) far return address to 16-bit side glue code
774 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
776 * The 32-bit side glue code accesses both the original arguments (via ebp)
777 * and the temporary copies prepared by the 16-bit side glue (via ebx).
778 * After completion, the stub will load ss:sp from the buffer at ebx
779 * and perform a far return to 16-bit code.
781 * To trick the relay stub into returning to us, we replace the 16-bit
782 * return address to the glue code by a cs:ip pair pointing to our
783 * return entry point (the original return address is saved first).
784 * Our return stub thus called will then reload the 32-bit ss:esp and
785 * return to 32-bit code (by using and ss:esp value that we have also
786 * pushed onto the 16-bit stack before and a cs:eip values found at
787 * that position on the 32-bit stack). The ss:esp to be restored is
788 * found relative to the 16-bit stack pointer at:
790 * (ebx-4) ss (flat)
791 * (ebx-8) sp (32-bit stack pointer)
793 * The second variant of this routine, CALL32_CBClientEx, which is used
794 * to implement KERNEL.621, has to cope with yet another problem: Here,
795 * the 32-bit side directly returns to the caller of the CBClient thunklet,
796 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
797 * As we have to return to our 32-bit code first, we have to adapt the
798 * layout of our temporary area so as to include values for the registers
799 * that are to be restored, and later (in the implementation of KERNEL.621)
800 * we *really* restore them. The return stub restores DS, DI, SI, and BP
801 * from the stack, skips the next 8 bytes (CBClient relay code / target),
802 * and then performs a lret NN, where NN is the number of arguments to be
803 * removed. Thus, we prepare our temporary area as follows:
805 * (ebx+22) 16-bit cs (this segment)
806 * (ebx+20) 16-bit ip ('16-bit' return entry point)
807 * (ebx+16) 32-bit ss (flat)
808 * (ebx+12) 32-bit sp (32-bit stack pointer)
809 * (ebx+10) 16-bit bp (points to ebx+24)
810 * (ebx+8) 16-bit si (ignored)
811 * (ebx+6) 16-bit di (ignored)
812 * (ebx+4) 16-bit ds (we actually use the flat DS here)
813 * (ebx+2) 16-bit ss (16-bit stack segment)
814 * (ebx+0) 16-bit sp (points to ebx+4)
816 * Note that we ensure that DS is not changed and remains the flat segment,
817 * and the 32-bit stack pointer our own return stub needs fits just
818 * perfectly into the 8 bytes that are skipped by the Windows stub.
819 * One problem is that we have to determine the number of removed arguments,
820 * as these have to be really removed in KERNEL.621. Thus, the BP value
821 * that we place in the temporary area to be restored, contains the value
822 * that SP would have if no arguments were removed. By comparing the actual
823 * value of SP with this value in our return stub we can compute the number
824 * of removed arguments. This is then returned to KERNEL.621.
826 * The stack layout of this function:
827 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
828 * (ebp+16) esi pointer to caller's esi value
829 * (ebp+12) arg ebp value to be set for relay stub
830 * (ebp+8) func CBClient relay stub address
831 * (ebp+4) ret addr
832 * (ebp) ebp
834 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
836 char *name = isEx? "CBClientEx" : "CBClient";
837 int size = isEx? 24 : 12;
839 /* Function header */
841 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
842 #ifdef USE_STABS
843 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
844 name, name );
845 #endif
846 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
847 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
849 /* Entry code */
851 fprintf( outfile, "\tpushl %%ebp\n" );
852 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
853 fprintf( outfile, "\tpushl %%edi\n" );
854 fprintf( outfile, "\tpushl %%esi\n" );
855 fprintf( outfile, "\tpushl %%ebx\n" );
857 /* Get the 16-bit stack */
859 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
861 /* Convert it to a flat address */
863 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
864 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
865 fprintf( outfile, "\tshrl $1,%%eax\n" );
866 fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy(%%eax),%%esi\n" );
867 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
868 fprintf( outfile, "\taddl %%eax,%%esi\n" );
870 /* Allocate temporary area (simulate STACK16_PUSH) */
872 fprintf( outfile, "\tpushf\n" );
873 fprintf( outfile, "\tcld\n" );
874 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
875 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
876 fprintf( outfile, "\trep\n\tmovsb\n" );
877 fprintf( outfile, "\tpopf\n" );
879 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
881 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
883 /* Set up temporary area */
885 if ( !isEx )
887 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
889 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
890 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
892 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
893 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
894 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
896 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
897 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
899 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
900 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
902 else
904 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
905 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
907 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
908 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
910 fprintf( outfile, "\taddl $20, %%ebx\n" );
911 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
913 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
914 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
916 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
917 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
918 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
920 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
921 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
924 /* Set up registers and call CBClient relay stub (simulating a far call) */
926 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
927 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
929 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
930 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
931 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
933 fprintf( outfile, "\tpushl %%cs\n" );
934 fprintf( outfile, "\tcall *%%eax\n" );
936 /* Return new esi value to caller */
938 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
939 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
941 /* Cleanup temporary area (simulate STACK16_POP) */
943 fprintf( outfile, "\tpop %%esi\n" );
945 fprintf( outfile, "\tpushf\n" );
946 fprintf( outfile, "\tstd\n" );
947 fprintf( outfile, "\tdec %%esi\n" );
948 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
949 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
950 fprintf( outfile, "\trep\n\tmovsb\n" );
951 fprintf( outfile, "\tpopf\n" );
953 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
955 /* Return argument size to caller */
956 if ( isEx )
958 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
959 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
962 /* Restore registers and return */
964 fprintf( outfile, "\tpopl %%ebx\n" );
965 fprintf( outfile, "\tpopl %%esi\n" );
966 fprintf( outfile, "\tpopl %%edi\n" );
967 fprintf( outfile, "\tpopl %%ebp\n" );
968 fprintf( outfile, "\tret\n" );
971 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
973 char *name = isEx? "CBClientEx" : "CBClient";
975 /* '16-bit' return stub */
977 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
978 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
980 if ( !isEx )
982 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
983 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
985 else
987 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
988 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
989 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
990 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
992 fprintf( outfile, "\tlret\n" );
994 /* Declare the return address variable */
996 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
997 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
1001 /*******************************************************************
1002 * BuildCallFrom32Regs
1004 * Build a 32-bit-to-Wine call-back function for a 'register' function.
1005 * 'args' is the number of dword arguments.
1007 * Stack layout:
1008 * ...
1009 * (ebp+12) first arg
1010 * (ebp+8) ret addr to user code
1011 * (ebp+4) ret addr to relay code
1012 * (ebp+0) saved ebp
1013 * (ebp-128) buffer area to allow stack frame manipulation
1014 * (ebp-332) CONTEXT86 struct
1015 * (ebp-336) CONTEXT86 *argument
1016 * .... other arguments copied from (ebp+12)
1018 * The entry point routine is called with a CONTEXT* extra argument,
1019 * following the normal args. In this context structure, EIP_reg
1020 * contains the return address to user code, and ESP_reg the stack
1021 * pointer on return (with the return address and arguments already
1022 * removed).
1024 static void BuildCallFrom32Regs( FILE *outfile )
1026 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
1028 /* Function header */
1030 function_header( outfile, "__wine_call_from_32_regs" );
1032 /* Allocate some buffer space on the stack */
1034 fprintf( outfile, "\tpushl %%ebp\n" );
1035 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
1036 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
1038 /* Build the context structure */
1040 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1041 fprintf( outfile, "\tpushfl\n" );
1042 fprintf( outfile, "\tpopl %%eax\n" );
1043 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1044 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
1045 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1046 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1047 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1048 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1049 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1050 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1052 fprintf( outfile, "\txorl %%eax,%%eax\n" );
1053 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1054 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1055 fprintf( outfile, "\tmovw %%es,%%ax\n" );
1056 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1057 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1058 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1059 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1060 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1061 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1062 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1063 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1064 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1065 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
1067 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1068 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1070 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1071 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1073 /* Transfer the arguments */
1075 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
1076 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
1077 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1078 fprintf( outfile, "\tjecxz 1f\n" );
1079 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1080 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
1081 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1082 fprintf( outfile, "\tshrl $2,%%ecx\n" );
1083 fprintf( outfile, "\tcld\n" );
1084 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
1086 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1087 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1088 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1090 /* Call the entry point */
1092 fprintf( outfile, "\tcall *0(%%ebx)\n" );
1094 /* Store %eip and %ebp onto the new stack */
1096 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1097 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1098 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1099 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1100 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1102 /* Restore the context structure */
1104 /* Note: we don't bother to restore %cs, %ds and %ss
1105 * changing them in 32-bit code is a recipe for disaster anyway
1107 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1108 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1109 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1110 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1111 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1112 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1114 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1115 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1116 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1117 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1118 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1120 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1121 fprintf( outfile, "\tpushl %%eax\n" );
1122 fprintf( outfile, "\tpopfl\n" );
1123 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1125 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1126 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1127 fprintf( outfile, "\tpopl %%ebp\n" );
1128 fprintf( outfile, "\tret\n" );
1132 /*******************************************************************
1133 * BuildRelays16
1135 * Build all the 16-bit relay callbacks
1137 void BuildRelays16( FILE *outfile )
1139 char buffer[1024];
1141 /* File header */
1143 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1144 fprintf( outfile, "\t.text\n" );
1146 #ifdef USE_STABS
1147 if (output_file_name)
1149 getcwd(buffer, sizeof(buffer));
1150 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1153 * The stabs help the internal debugger as they are an indication that it
1154 * is sensible to step into a thunk/trampoline.
1156 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1157 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1158 fprintf( outfile, "Code_Start:\n\n" );
1160 #endif
1161 fprintf( outfile, PREFIX"Call16_Start:\n" );
1162 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1163 fprintf( outfile, "\t.byte 0\n\n" );
1165 /* Standard CallFrom16 routine (WORD return) */
1166 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1168 /* Standard CallFrom16 routine (DWORD return) */
1169 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1171 /* Register CallFrom16 routine */
1172 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1174 /* C16ThkSL CallFrom16 routine */
1175 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1177 /* Standard CallTo16 routine (WORD return) */
1178 BuildCallTo16Core( outfile, TRUE, FALSE );
1180 /* Standard CallTo16 routine (DWORD return) */
1181 BuildCallTo16Core( outfile, FALSE, FALSE );
1183 /* Register CallTo16 routine (16:16 retf) */
1184 BuildCallTo16Core( outfile, FALSE, 1 );
1186 /* Register CallTo16 routine (16:32 retf) */
1187 BuildCallTo16Core( outfile, FALSE, 2 );
1189 /* CBClientThunkSL routine */
1190 BuildCallTo32CBClient( outfile, FALSE );
1192 /* CBClientThunkSLEx routine */
1193 BuildCallTo32CBClient( outfile, TRUE );
1195 fprintf( outfile, PREFIX"Call16_End:\n" );
1196 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1198 #ifdef USE_STABS
1199 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1200 fprintf( outfile, ".Letext:\n");
1202 /* Some versions of gdb need to have the filename data for
1203 each section, so output it again for the data section. */
1204 if (output_file_name)
1206 fprintf( outfile, ".stabs \"%s/\",100,0,0,Data_Start\n", buffer);
1207 fprintf( outfile, ".stabs \"%s\",100,0,0,Data_Start\n", output_file_name );
1208 fprintf( outfile, "Data_Start:\n\n" );
1210 #endif
1212 /* The whole Call16_Ret segment must lie within the .data section */
1213 fprintf( outfile, "\n\t.data\n" );
1214 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1215 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1217 /* Standard CallTo16 return stub */
1218 BuildRet16Func( outfile );
1220 /* CBClientThunkSL return stub */
1221 BuildCallTo32CBClientRet( outfile, FALSE );
1223 /* CBClientThunkSLEx return stub */
1224 BuildCallTo32CBClientRet( outfile, TRUE );
1226 /* End of Call16_Ret segment */
1227 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1228 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1231 /*******************************************************************
1232 * BuildRelays32
1234 * Build all the 32-bit relay callbacks
1236 void BuildRelays32( FILE *outfile )
1238 /* File header */
1240 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1241 fprintf( outfile, "\t.text\n" );
1243 #ifdef USE_STABS
1244 if (output_file_name)
1246 char buffer[1024];
1247 getcwd(buffer, sizeof(buffer));
1248 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1251 * The stabs help the internal debugger as they are an indication that it
1252 * is sensible to step into a thunk/trampoline.
1254 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1255 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1256 fprintf( outfile, "Code_Start:\n\n" );
1258 #endif
1259 /* 32-bit register entry point */
1260 BuildCallFrom32Regs( outfile );
1262 #ifdef USE_STABS
1263 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1264 fprintf( outfile, ".Letext:\n");
1265 #endif
1268 #else /* __i386__ */
1270 void BuildRelays16( FILE *outfile )
1272 fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1275 void BuildRelays32( FILE *outfile )
1277 fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1280 #endif /* __i386__ */