Export the CallFrom16xxx functions from kernel32. Renamed them
[wine/wine-kai.git] / tools / winebuild / relay.c
bloba58c0a793bf090eccc255ae246765d77582d8b0e
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 <ctype.h>
12 #include <unistd.h>
14 #include "winnt.h"
15 #include "thread.h"
16 #include "stackframe.h"
18 #include "build.h"
21 /*******************************************************************
22 * BuildCallFrom16Core
24 * This routine builds the core routines used in 16->32 thunks:
25 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
27 * These routines are intended to be called via a far call (with 32-bit
28 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
29 * the 32-bit entry point to be called, and the argument conversion
30 * routine to be used (see stack layout below).
32 * The core routine completes the STACK16FRAME on the 16-bit stack and
33 * switches to the 32-bit stack. Then, the argument conversion routine
34 * is called; it gets passed the 32-bit entry point and a pointer to the
35 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
36 * use conversion routines automatically generated by BuildCallFrom16,
37 * or write your own for special purposes.)
39 * The conversion routine must call the 32-bit entry point, passing it
40 * the converted arguments, and return its return value to the core.
41 * After the conversion routine has returned, the core switches back
42 * to the 16-bit stack, converts the return value to the DX:AX format
43 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
44 * including %bp, are popped off the stack.
46 * The 16-bit call stub now returns to the caller, popping the 16-bit
47 * arguments if necessary (pascal calling convention).
49 * In the case of a 'register' function, CallFrom16Register fills a
50 * CONTEXT86 structure with the values all registers had at the point
51 * the first instruction of the 16-bit call stub was about to be
52 * executed. A pointer to this CONTEXT86 is passed as third parameter
53 * to the argument conversion routine, which typically passes it on
54 * to the called 32-bit entry point.
56 * CallFrom16Thunk is a special variant used by the implementation of
57 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
58 * implemented as follows:
59 * On entry, the EBX register is set up to contain a flat pointer to the
60 * 16-bit stack such that EBX+22 points to the first argument.
61 * Then, the entry point is called, while EBP is set up to point
62 * to the return address (on the 32-bit stack).
63 * The called function returns with CX set to the number of bytes
64 * to be popped of the caller's stack.
66 * Stack layout upon entry to the core routine (STACK16FRAME):
67 * ... ...
68 * (sp+24) word first 16-bit arg
69 * (sp+22) word cs
70 * (sp+20) word ip
71 * (sp+18) word bp
72 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
73 * (sp+12) word ip of actual entry point (necessary for relay debugging)
74 * (sp+8) long relay (argument conversion) function entry point
75 * (sp+4) long cs of 16-bit entry point
76 * (sp) long ip of 16-bit entry point
78 * Added on the stack:
79 * (sp-2) word saved gs
80 * (sp-4) word saved fs
81 * (sp-6) word saved es
82 * (sp-8) word saved ds
83 * (sp-12) long saved ebp
84 * (sp-16) long saved ecx
85 * (sp-20) long saved edx
86 * (sp-24) long saved previous stack
88 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
90 char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
92 /* Function header */
93 fprintf( outfile, "\n\t.align 4\n" );
94 #ifdef USE_STABS
95 fprintf( outfile, ".stabs \"__wine_call_from_16_%s:F1\",36,0,0," PREFIX "__wine_call_from_16_%s\n", name, name);
96 #endif
97 fprintf( outfile, "\t.type " PREFIX "__wine_call_from_16_%s,@function\n", name );
98 fprintf( outfile, "\t.globl " PREFIX "__wine_call_from_16_%s\n", name );
99 fprintf( outfile, PREFIX "__wine_call_from_16_%s:\n", name );
101 /* Create STACK16FRAME (except STACK32FRAME link) */
102 fprintf( outfile, "\tpushw %%gs\n" );
103 fprintf( outfile, "\tpushw %%fs\n" );
104 fprintf( outfile, "\tpushw %%es\n" );
105 fprintf( outfile, "\tpushw %%ds\n" );
106 fprintf( outfile, "\tpushl %%ebp\n" );
107 fprintf( outfile, "\tpushl %%ecx\n" );
108 fprintf( outfile, "\tpushl %%edx\n" );
110 /* Save original EFlags register */
111 fprintf( outfile, "\tpushfl\n" );
113 if ( UsePIC )
115 /* Get Global Offset Table into %ecx */
116 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name );
117 fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name );
118 fprintf( outfile, "\tpopl %%ecx\n" );
119 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name );
122 if (UsePIC)
124 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector@GOT(%%ecx), %%edx\n" );
125 fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
127 else
128 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector,%%edx\n" );
130 /* Load 32-bit segment registers */
131 #ifdef __svr4__
132 fprintf( outfile, "\tdata16\n");
133 #endif
134 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
135 #ifdef __svr4__
136 fprintf( outfile, "\tdata16\n");
137 #endif
138 fprintf( outfile, "\tmovw %%dx, %%es\n" );
140 if ( UsePIC )
142 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
143 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
145 else
146 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
148 /* Get address of ldt_copy array into %ecx */
149 if ( UsePIC )
150 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
151 else
152 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
154 /* Translate STACK16FRAME base to flat offset in %edx */
155 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
156 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
157 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
158 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
159 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
161 /* Get saved flags into %ecx */
162 fprintf( outfile, "\tpopl %%ecx\n" );
164 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
165 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
166 fprintf( outfile, "\tpushl %%ebp\n" );
168 /* Switch stacks */
169 #ifdef __svr4__
170 fprintf( outfile,"\tdata16\n");
171 #endif
172 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
173 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
174 fprintf( outfile, "\tpushl %%ds\n" );
175 fprintf( outfile, "\tpopl %%ss\n" );
176 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
177 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
180 /* At this point:
181 STACK16FRAME is completely set up
182 DS, ES, SS: flat data segment
183 FS: current TEB
184 ESP: points to last STACK32FRAME
185 EBP: points to ebp member of last STACK32FRAME
186 EDX: points to current STACK16FRAME
187 ECX: contains saved flags
188 all other registers: unchanged */
190 /* Special case: C16ThkSL stub */
191 if ( thunk )
193 /* Set up registers as expected and call thunk */
194 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
195 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
197 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
199 /* Switch stack back */
200 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
201 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
202 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
204 /* Restore registers and return directly to caller */
205 fprintf( outfile, "\taddl $8, %%esp\n" );
206 fprintf( outfile, "\tpopl %%ebp\n" );
207 fprintf( outfile, "\tpopw %%ds\n" );
208 fprintf( outfile, "\tpopw %%es\n" );
209 fprintf( outfile, "\tpopw %%fs\n" );
210 fprintf( outfile, "\tpopw %%gs\n" );
211 fprintf( outfile, "\taddl $20, %%esp\n" );
213 fprintf( outfile, "\txorb %%ch, %%ch\n" );
214 fprintf( outfile, "\tpopl %%ebx\n" );
215 fprintf( outfile, "\taddw %%cx, %%sp\n" );
216 fprintf( outfile, "\tpush %%ebx\n" );
218 fprintf( outfile, "\t.byte 0x66\n" );
219 fprintf( outfile, "\tlret\n" );
221 return;
225 /* Build register CONTEXT */
226 if ( reg_func )
228 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
230 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
232 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
233 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
234 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
235 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
237 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
238 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
239 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
240 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
241 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
242 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
244 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
245 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
246 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
247 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
248 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
249 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
250 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
251 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
253 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
254 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
255 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
256 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
258 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
259 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
260 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
261 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
262 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
263 #if 0
264 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
265 #endif
267 /* Push address of CONTEXT86 structure -- popped by the relay routine */
268 fprintf( outfile, "\tpushl %%esp\n" );
272 /* Print debug info before call */
273 if ( debugging )
275 if ( UsePIC )
277 fprintf( outfile, "\tpushl %%ebx\n" );
279 /* Get Global Offset Table into %ebx (for PLT call) */
280 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name );
281 fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name );
282 fprintf( outfile, "\tpopl %%ebx\n" );
283 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name );
286 fprintf( outfile, "\tpushl %%edx\n" );
287 if ( reg_func )
288 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
289 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
290 else
291 fprintf( outfile, "\tpushl $0\n" );
293 if ( UsePIC )
294 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
295 else
296 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
298 fprintf( outfile, "\tpopl %%edx\n" );
299 fprintf( outfile, "\tpopl %%edx\n" );
301 if ( UsePIC )
302 fprintf( outfile, "\tpopl %%ebx\n" );
305 /* Call relay routine (which will call the API entry point) */
306 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
307 fprintf( outfile, "\tpushl %%eax\n" );
308 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
309 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
311 /* Print debug info after call */
312 if ( debugging )
314 if ( UsePIC )
316 fprintf( outfile, "\tpushl %%ebx\n" );
318 /* Get Global Offset Table into %ebx (for PLT call) */
319 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name );
320 fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name );
321 fprintf( outfile, "\tpopl %%ebx\n" );
322 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name );
325 fprintf( outfile, "\tpushl %%eax\n" );
326 if ( reg_func )
327 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
328 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
329 else
330 fprintf( outfile, "\tpushl $0\n" );
332 if ( UsePIC )
333 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
334 else
335 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
337 fprintf( outfile, "\tpopl %%eax\n" );
338 fprintf( outfile, "\tpopl %%eax\n" );
340 if ( UsePIC )
341 fprintf( outfile, "\tpopl %%ebx\n" );
345 if ( reg_func )
347 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
349 /* Switch stack back */
350 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
351 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
352 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
354 /* Get return address to CallFrom16 stub */
355 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
356 fprintf( outfile, "\tpopl %%eax\n" );
357 fprintf( outfile, "\tpopl %%edx\n" );
359 /* Restore all registers from CONTEXT */
360 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
361 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
362 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
364 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
365 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
366 fprintf( outfile, "\tpushl %%edx\n" );
367 fprintf( outfile, "\tpushl %%eax\n" );
368 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
369 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
371 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
372 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
373 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
375 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
376 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
377 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
378 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
379 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
380 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
381 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
383 fprintf( outfile, "\tpopl %%ds\n" );
384 fprintf( outfile, "\tpopfl\n" );
385 fprintf( outfile, "\tlret\n" );
387 else
389 /* Switch stack back */
390 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
391 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
392 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
394 /* Restore registers */
395 fprintf( outfile, "\tpopl %%edx\n" );
396 fprintf( outfile, "\tpopl %%ecx\n" );
397 fprintf( outfile, "\tpopl %%ebp\n" );
398 fprintf( outfile, "\tpopw %%ds\n" );
399 fprintf( outfile, "\tpopw %%es\n" );
400 fprintf( outfile, "\tpopw %%fs\n" );
401 fprintf( outfile, "\tpopw %%gs\n" );
403 /* Prepare return value and set flags accordingly */
404 if ( !short_ret )
405 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
406 fprintf( outfile, "\torl %%eax, %%eax\n" );
408 /* Return to return stub which will return to caller */
409 fprintf( outfile, "\tlret $12\n" );
414 /*******************************************************************
415 * BuildCallTo16Core
417 * This routine builds the core routines used in 32->16 thunks:
419 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
420 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
421 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
422 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
424 * These routines can be called directly from 32-bit code.
426 * All routines expect that the 16-bit stack contents (arguments) were
427 * already set up by the caller; nb_args must contain the number of bytes
428 * to be conserved. The 16-bit SS:SP will be set accordinly.
430 * All other registers are either taken from the CONTEXT86 structure
431 * or else set to default values. The target routine address is either
432 * given directly or taken from the CONTEXT86.
434 * If you want to call a 16-bit routine taking only standard argument types
435 * (WORD and LONG), you can also have an appropriate argument conversion
436 * stub automatically generated (see BuildCallTo16); you'd then call this
437 * stub, which in turn would prepare the 16-bit stack and call the appropiate
438 * core routine.
442 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
444 char *name = reg_func == 2 ? "RegisterLong" :
445 reg_func == 1 ? "RegisterShort" :
446 short_ret? "Word" : "Long";
448 /* Function header */
449 fprintf( outfile, "\n\t.align 4\n" );
450 #ifdef USE_STABS
451 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
452 name, name);
453 #endif
454 fprintf( outfile, "\t.type " PREFIX "CallTo16%s,@function\n", name );
455 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
456 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
458 /* Function entry sequence */
459 fprintf( outfile, "\tpushl %%ebp\n" );
460 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
462 /* Save the 32-bit registers */
463 fprintf( outfile, "\tpushl %%ebx\n" );
464 fprintf( outfile, "\tpushl %%ecx\n" );
465 fprintf( outfile, "\tpushl %%edx\n" );
466 fprintf( outfile, "\tpushl %%esi\n" );
467 fprintf( outfile, "\tpushl %%edi\n" );
469 if ( UsePIC )
471 /* Get Global Offset Table into %ebx */
472 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
473 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
474 fprintf( outfile, "\tpopl %%ebx\n" );
475 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
478 /* Enter Win16 Mutex */
479 if ( UsePIC )
480 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
481 else
482 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
484 /* Print debugging info */
485 if (debugging)
487 /* Push flags, number of arguments, and target */
488 fprintf( outfile, "\tpushl $%d\n", reg_func );
489 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
490 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
492 if ( UsePIC )
493 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
494 else
495 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
497 fprintf( outfile, "\taddl $12, %%esp\n" );
500 /* Get return address */
501 if ( UsePIC )
503 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
504 fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" );
506 else
507 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
509 /* Call the actual CallTo16 routine (simulate a lcall) */
510 fprintf( outfile, "\tpushl %%cs\n" );
511 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
513 if ( !reg_func )
515 /* Convert and push return value */
516 if ( short_ret )
518 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
519 fprintf( outfile, "\tpushl %%eax\n" );
521 else
523 fprintf( outfile, "\tshll $16,%%edx\n" );
524 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
525 fprintf( outfile, "\tpushl %%edx\n" );
528 else
531 * Modify CONTEXT86 structure to contain new values
533 * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
534 * The segment registers as well as ESI and EDI should
535 * not be modified by a well-behaved 16-bit routine in
536 * any case. [If necessary, we could restore them as well,
537 * at the cost of a somewhat less efficient return path.]
540 fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target)-12 );
541 fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
542 fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
543 fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
544 fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
545 fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
546 fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
547 /* The return glue code saved %esp into %esi */
549 fprintf( outfile, "\tpushl %%edi\n" );
552 if ( UsePIC )
554 /* Get Global Offset Table into %ebx (might have been overwritten) */
555 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
556 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
557 fprintf( outfile, "\tpopl %%ebx\n" );
558 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
561 /* Print debugging info */
562 if (debugging)
564 fprintf( outfile, "\tpushl $%d\n", reg_func );
566 if ( UsePIC )
567 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
568 else
569 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
571 fprintf( outfile, "\taddl $4, %%esp\n" );
574 /* Leave Win16 Mutex */
575 if ( UsePIC )
576 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
577 else
578 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
580 /* Get return value */
581 fprintf( outfile, "\tpopl %%eax\n" );
583 /* Restore the 32-bit registers */
584 fprintf( outfile, "\tpopl %%edi\n" );
585 fprintf( outfile, "\tpopl %%esi\n" );
586 fprintf( outfile, "\tpopl %%edx\n" );
587 fprintf( outfile, "\tpopl %%ecx\n" );
588 fprintf( outfile, "\tpopl %%ebx\n" );
590 /* Function exit sequence */
591 fprintf( outfile, "\tpopl %%ebp\n" );
592 fprintf( outfile, "\tret $8\n" );
595 /* Start of the actual CallTo16 routine */
597 fprintf( outfile, ".LCallTo16%s:\n", name );
599 /* Complete STACK32FRAME */
600 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
601 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
603 /* Switch to the 16-bit stack */
604 #ifdef __svr4__
605 fprintf( outfile,"\tdata16\n");
606 #endif
607 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
608 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
609 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
611 /* Make %bp point to the previous stackframe (built by CallFrom16) */
612 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
613 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
615 /* Add the specified offset to the new sp */
616 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
618 /* Push the return address
619 * With sreg suffix, we push 16:16 address (normal lret)
620 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
622 if (reg_func != 2)
623 fprintf( outfile, "\tpushl %%ecx\n" );
624 else
626 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
627 fprintf( outfile, "\tpushw $0\n" );
628 fprintf( outfile, "\tpushw %%ax\n" );
629 fprintf( outfile, "\tpushw $0\n" );
630 fprintf( outfile, "\tpushw %%cx\n" );
633 if (reg_func)
635 /* Push the called routine address */
636 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
637 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
638 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
640 /* Get the registers */
641 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
642 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
643 fprintf( outfile, "\tmovw %%ax,%%es\n" );
644 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
645 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
646 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
647 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
648 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
649 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
650 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
651 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
652 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
654 /* Get the 16-bit ds */
655 fprintf( outfile, "\tpopw %%ds\n" );
657 else /* not a register function */
659 /* Push the called routine address */
660 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
662 /* Set %fs to the value saved by the last CallFrom16 */
663 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
664 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
666 /* Set %ds and %es (and %ax just in case) equal to %ss */
667 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
668 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
669 fprintf( outfile, "\tmovw %%ax,%%es\n" );
672 /* Jump to the called routine */
673 fprintf( outfile, "\t.byte 0x66\n" );
674 fprintf( outfile, "\tlret\n" );
678 /*******************************************************************
679 * BuildRet16Func
681 * Build the return code for 16-bit callbacks
683 static void BuildRet16Func( FILE *outfile )
686 * Note: This must reside in the .data section to allow
687 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
690 fprintf( outfile, "\n\t.type " PREFIX "CallTo16_Ret,@function\n" );
691 fprintf( outfile, "\t.globl " PREFIX "CallTo16_Ret\n" );
692 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
694 /* Save %esp into %esi */
695 fprintf( outfile, "\tmovl %%esp,%%esi\n" );
697 /* Restore 32-bit segment registers */
699 fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector-" PREFIX "Call16_Ret_Start,%%edi\n" );
700 #ifdef __svr4__
701 fprintf( outfile, "\tdata16\n");
702 #endif
703 fprintf( outfile, "\tmovw %%di,%%ds\n" );
704 #ifdef __svr4__
705 fprintf( outfile, "\tdata16\n");
706 #endif
707 fprintf( outfile, "\tmovw %%di,%%es\n" );
709 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
711 /* Restore the 32-bit stack */
713 #ifdef __svr4__
714 fprintf( outfile, "\tdata16\n");
715 #endif
716 fprintf( outfile, "\tmovw %%di,%%ss\n" );
717 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
718 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
720 /* Return to caller */
722 fprintf( outfile, "\tlret\n" );
724 /* Declare the return address and data selector variables */
726 fprintf( outfile, "\n\t.align 4\n" );
727 fprintf( outfile, "\t.globl " PREFIX "CallTo16_DataSelector\n" );
728 fprintf( outfile, PREFIX "CallTo16_DataSelector:\t.long 0\n" );
729 fprintf( outfile, "\t.globl " PREFIX "CallTo16_RetAddr\n" );
730 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
734 /*******************************************************************
735 * BuildCallTo32CBClient
737 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
739 * Since the relay stub is itself 32-bit, this should not be a problem;
740 * unfortunately, the relay stubs are expected to switch back to a
741 * 16-bit stack (and 16-bit code) after completion :-(
743 * This would conflict with our 16- vs. 32-bit stack handling, so
744 * we simply switch *back* to our 32-bit stack before returning to
745 * the caller ...
747 * The CBClient relay stub expects to be called with the following
748 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
749 * stack at the designated places:
751 * ...
752 * (ebp+14) original arguments to the callback routine
753 * (ebp+10) far return address to original caller
754 * (ebp+6) Thunklet target address
755 * (ebp+2) Thunklet relay ID code
756 * (ebp) BP (saved by CBClientGlueSL)
757 * (ebp-2) SI (saved by CBClientGlueSL)
758 * (ebp-4) DI (saved by CBClientGlueSL)
759 * (ebp-6) DS (saved by CBClientGlueSL)
761 * ... buffer space used by the 16-bit side glue for temp copies
763 * (ebx+4) far return address to 16-bit side glue code
764 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
766 * The 32-bit side glue code accesses both the original arguments (via ebp)
767 * and the temporary copies prepared by the 16-bit side glue (via ebx).
768 * After completion, the stub will load ss:sp from the buffer at ebx
769 * and perform a far return to 16-bit code.
771 * To trick the relay stub into returning to us, we replace the 16-bit
772 * return address to the glue code by a cs:ip pair pointing to our
773 * return entry point (the original return address is saved first).
774 * Our return stub thus called will then reload the 32-bit ss:esp and
775 * return to 32-bit code (by using and ss:esp value that we have also
776 * pushed onto the 16-bit stack before and a cs:eip values found at
777 * that position on the 32-bit stack). The ss:esp to be restored is
778 * found relative to the 16-bit stack pointer at:
780 * (ebx-4) ss (flat)
781 * (ebx-8) sp (32-bit stack pointer)
783 * The second variant of this routine, CALL32_CBClientEx, which is used
784 * to implement KERNEL.621, has to cope with yet another problem: Here,
785 * the 32-bit side directly returns to the caller of the CBClient thunklet,
786 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
787 * As we have to return to our 32-bit code first, we have to adapt the
788 * layout of our temporary area so as to include values for the registers
789 * that are to be restored, and later (in the implementation of KERNEL.621)
790 * we *really* restore them. The return stub restores DS, DI, SI, and BP
791 * from the stack, skips the next 8 bytes (CBClient relay code / target),
792 * and then performs a lret NN, where NN is the number of arguments to be
793 * removed. Thus, we prepare our temporary area as follows:
795 * (ebx+22) 16-bit cs (this segment)
796 * (ebx+20) 16-bit ip ('16-bit' return entry point)
797 * (ebx+16) 32-bit ss (flat)
798 * (ebx+12) 32-bit sp (32-bit stack pointer)
799 * (ebx+10) 16-bit bp (points to ebx+24)
800 * (ebx+8) 16-bit si (ignored)
801 * (ebx+6) 16-bit di (ignored)
802 * (ebx+4) 16-bit ds (we actually use the flat DS here)
803 * (ebx+2) 16-bit ss (16-bit stack segment)
804 * (ebx+0) 16-bit sp (points to ebx+4)
806 * Note that we ensure that DS is not changed and remains the flat segment,
807 * and the 32-bit stack pointer our own return stub needs fits just
808 * perfectly into the 8 bytes that are skipped by the Windows stub.
809 * One problem is that we have to determine the number of removed arguments,
810 * as these have to be really removed in KERNEL.621. Thus, the BP value
811 * that we place in the temporary area to be restored, contains the value
812 * that SP would have if no arguments were removed. By comparing the actual
813 * value of SP with this value in our return stub we can compute the number
814 * of removed arguments. This is then returned to KERNEL.621.
816 * The stack layout of this function:
817 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
818 * (ebp+16) esi pointer to caller's esi value
819 * (ebp+12) arg ebp value to be set for relay stub
820 * (ebp+8) func CBClient relay stub address
821 * (ebp+4) ret addr
822 * (ebp) ebp
824 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
826 char *name = isEx? "CBClientEx" : "CBClient";
827 int size = isEx? 24 : 12;
829 /* Function header */
831 fprintf( outfile, "\n\t.align 4\n" );
832 #ifdef USE_STABS
833 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
834 name, name );
835 #endif
836 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
837 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
839 /* Entry code */
841 fprintf( outfile, "\tpushl %%ebp\n" );
842 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
843 fprintf( outfile, "\tpushl %%edi\n" );
844 fprintf( outfile, "\tpushl %%esi\n" );
845 fprintf( outfile, "\tpushl %%ebx\n" );
847 /* Get the 16-bit stack */
849 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
851 /* Convert it to a flat address */
853 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
854 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
855 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
856 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
857 fprintf( outfile, "\taddl %%eax,%%esi\n" );
859 /* Allocate temporary area (simulate STACK16_PUSH) */
861 fprintf( outfile, "\tpushf\n" );
862 fprintf( outfile, "\tcld\n" );
863 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
864 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
865 fprintf( outfile, "\trep\n\tmovsb\n" );
866 fprintf( outfile, "\tpopf\n" );
868 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
870 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
872 /* Set up temporary area */
874 if ( !isEx )
876 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
878 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
879 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
881 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
882 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
883 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
885 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
886 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
888 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
889 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
891 else
893 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
894 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
896 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
897 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
899 fprintf( outfile, "\taddl $20, %%ebx\n" );
900 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
902 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
903 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
905 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
906 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
907 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
909 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
910 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
913 /* Set up registers and call CBClient relay stub (simulating a far call) */
915 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
916 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
918 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
919 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
920 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
922 fprintf( outfile, "\tpushl %%cs\n" );
923 fprintf( outfile, "\tcall *%%eax\n" );
925 /* Return new esi value to caller */
927 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
928 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
930 /* Cleanup temporary area (simulate STACK16_POP) */
932 fprintf( outfile, "\tpop %%esi\n" );
934 fprintf( outfile, "\tpushf\n" );
935 fprintf( outfile, "\tstd\n" );
936 fprintf( outfile, "\tdec %%esi\n" );
937 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
938 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
939 fprintf( outfile, "\trep\n\tmovsb\n" );
940 fprintf( outfile, "\tpopf\n" );
942 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
944 /* Return argument size to caller */
945 if ( isEx )
947 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
948 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
951 /* Restore registers and return */
953 fprintf( outfile, "\tpopl %%ebx\n" );
954 fprintf( outfile, "\tpopl %%esi\n" );
955 fprintf( outfile, "\tpopl %%edi\n" );
956 fprintf( outfile, "\tpopl %%ebp\n" );
957 fprintf( outfile, "\tret\n" );
960 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
962 char *name = isEx? "CBClientEx" : "CBClient";
964 /* '16-bit' return stub */
966 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
967 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
969 if ( !isEx )
971 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
972 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
974 else
976 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
977 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
978 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
979 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
981 fprintf( outfile, "\tlret\n" );
983 /* Declare the return address variable */
985 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
986 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
990 /*******************************************************************
991 * BuildCallFrom32Regs
993 * Build a 32-bit-to-Wine call-back function for a 'register' function.
994 * 'args' is the number of dword arguments.
996 * Stack layout:
997 * ...
998 * (ebp+12) first arg
999 * (ebp+8) ret addr to user code
1000 * (ebp+4) ret addr to relay code
1001 * (ebp+0) saved ebp
1002 * (ebp-128) buffer area to allow stack frame manipulation
1003 * (ebp-332) CONTEXT86 struct
1004 * (ebp-336) CONTEXT86 *argument
1005 * .... other arguments copied from (ebp+12)
1007 * The entry point routine is called with a CONTEXT* extra argument,
1008 * following the normal args. In this context structure, EIP_reg
1009 * contains the return address to user code, and ESP_reg the stack
1010 * pointer on return (with the return address and arguments already
1011 * removed).
1013 static void BuildCallFrom32Regs( FILE *outfile )
1015 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
1017 /* Function header */
1019 fprintf( outfile, "\n\t.align 4\n" );
1020 #ifdef USE_STABS
1021 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
1022 #endif
1023 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
1024 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
1026 /* Allocate some buffer space on the stack */
1028 fprintf( outfile, "\tpushl %%ebp\n" );
1029 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
1030 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
1032 /* Build the context structure */
1034 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1035 fprintf( outfile, "\tpushfl\n" );
1036 fprintf( outfile, "\tpopl %%eax\n" );
1037 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1038 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
1039 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1040 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1041 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1042 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1043 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1044 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1046 fprintf( outfile, "\txorl %%eax,%%eax\n" );
1047 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1048 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1049 fprintf( outfile, "\tmovw %%es,%%ax\n" );
1050 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1051 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1052 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1053 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1054 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1055 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1056 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1057 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1058 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1059 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
1061 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1062 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1064 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1065 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1067 /* Transfer the arguments */
1069 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
1070 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
1071 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1072 fprintf( outfile, "\tjecxz 1f\n" );
1073 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1074 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
1075 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1076 fprintf( outfile, "\tshrl $2,%%ecx\n" );
1077 fprintf( outfile, "\tcld\n" );
1078 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
1080 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1081 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1082 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1084 /* Call the entry point */
1086 fprintf( outfile, "\tcall *0(%%ebx)\n" );
1088 /* Store %eip and %ebp onto the new stack */
1090 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1091 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1092 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1093 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1094 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1096 /* Restore the context structure */
1098 /* Note: we don't bother to restore %cs, %ds and %ss
1099 * changing them in 32-bit code is a recipe for disaster anyway
1101 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1102 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1103 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1104 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1105 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1106 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1108 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1109 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1110 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1111 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1112 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1114 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1115 fprintf( outfile, "\tpushl %%eax\n" );
1116 fprintf( outfile, "\tpopfl\n" );
1117 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1119 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1120 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1121 fprintf( outfile, "\tpopl %%ebp\n" );
1122 fprintf( outfile, "\tret\n" );
1126 /*******************************************************************
1127 * BuildRelays
1129 * Build all the relay callbacks
1131 void BuildRelays( FILE *outfile )
1133 /* File header */
1135 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1136 fprintf( outfile, "\t.text\n" );
1138 #ifdef __i386__
1140 #ifdef USE_STABS
1141 if (output_file_name)
1143 char buffer[1024];
1144 getcwd(buffer, sizeof(buffer));
1145 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1148 * The stabs help the internal debugger as they are an indication that it
1149 * is sensible to step into a thunk/trampoline.
1151 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1152 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1153 fprintf( outfile, "Code_Start:\n\n" );
1155 #endif
1156 fprintf( outfile, PREFIX"Call16_Start:\n" );
1157 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1158 fprintf( outfile, "\t.byte 0\n\n" );
1160 /* Standard CallFrom16 routine (WORD return) */
1161 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1163 /* Standard CallFrom16 routine (DWORD return) */
1164 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1166 /* Register CallFrom16 routine */
1167 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1169 /* C16ThkSL CallFrom16 routine */
1170 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1172 /* Standard CallTo16 routine (WORD return) */
1173 BuildCallTo16Core( outfile, TRUE, FALSE );
1175 /* Standard CallTo16 routine (DWORD return) */
1176 BuildCallTo16Core( outfile, FALSE, FALSE );
1178 /* Register CallTo16 routine (16:16 retf) */
1179 BuildCallTo16Core( outfile, FALSE, 1 );
1181 /* Register CallTo16 routine (16:32 retf) */
1182 BuildCallTo16Core( outfile, FALSE, 2 );
1184 /* CBClientThunkSL routine */
1185 BuildCallTo32CBClient( outfile, FALSE );
1187 /* CBClientThunkSLEx routine */
1188 BuildCallTo32CBClient( outfile, TRUE );
1190 /* 32-bit register entry point */
1191 BuildCallFrom32Regs( outfile );
1193 fprintf( outfile, PREFIX"Call16_End:\n" );
1194 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1196 #ifdef USE_STABS
1197 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1198 fprintf( outfile, ".Letext:\n");
1199 #endif
1201 /* The whole Call16_Ret segment must lie within the .data section */
1202 fprintf( outfile, "\n\t.data\n" );
1203 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1204 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1206 /* Standard CallTo16 return stub */
1207 BuildRet16Func( outfile );
1209 /* CBClientThunkSL return stub */
1210 BuildCallTo32CBClientRet( outfile, FALSE );
1212 /* CBClientThunkSLEx return stub */
1213 BuildCallTo32CBClientRet( outfile, TRUE );
1215 /* End of Call16_Ret segment */
1216 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1217 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1219 #else /* __i386__ */
1221 fprintf( outfile, PREFIX"Call16_Start:\n" );
1222 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1223 fprintf( outfile, "\t.byte 0\n\n" );
1224 fprintf( outfile, PREFIX"Call16_End:\n" );
1225 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1227 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1228 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1229 fprintf( outfile, "\t.byte 0\n\n" );
1230 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1231 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1233 #endif /* __i386__ */