Changed a few core int10 routines to use the BIOS data segment cursor
[wine.git] / tools / winebuild / relay.c
blobfdab80cc28d92baa619ab7b65e5ebf0a5adad078
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? "Register" : short_ret? "Word" : "Long";
92 /* Function header */
93 fprintf( outfile, "\n\t.align 4\n" );
94 #ifdef USE_STABS
95 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
96 name, name);
97 #endif
98 fprintf( outfile, "\t.type " PREFIX "CallFrom16%s,@function\n", name );
99 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
100 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
102 /* Create STACK16FRAME (except STACK32FRAME link) */
103 fprintf( outfile, "\tpushw %%gs\n" );
104 fprintf( outfile, "\tpushw %%fs\n" );
105 fprintf( outfile, "\tpushw %%es\n" );
106 fprintf( outfile, "\tpushw %%ds\n" );
107 fprintf( outfile, "\tpushl %%ebp\n" );
108 fprintf( outfile, "\tpushl %%ecx\n" );
109 fprintf( outfile, "\tpushl %%edx\n" );
111 /* Save original EFlags register */
112 fprintf( outfile, "\tpushfl\n" );
114 if ( UsePIC )
116 /* Get Global Offset Table into %ecx */
117 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
118 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
119 fprintf( outfile, "\tpopl %%ecx\n" );
120 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
123 /* Load 32-bit segment registers */
124 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", data_selector );
125 #ifdef __svr4__
126 fprintf( outfile, "\tdata16\n");
127 #endif
128 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
129 #ifdef __svr4__
130 fprintf( outfile, "\tdata16\n");
131 #endif
132 fprintf( outfile, "\tmovw %%dx, %%es\n" );
134 if ( UsePIC )
136 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
137 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
139 else
140 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
142 /* Get address of ldt_copy array into %ecx */
143 if ( UsePIC )
144 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
145 else
146 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
148 /* Translate STACK16FRAME base to flat offset in %edx */
149 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
150 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
151 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
152 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
153 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
155 /* Get saved flags into %ecx */
156 fprintf( outfile, "\tpopl %%ecx\n" );
158 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
159 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
160 fprintf( outfile, "\tpushl %%ebp\n" );
162 /* Switch stacks */
163 #ifdef __svr4__
164 fprintf( outfile,"\tdata16\n");
165 #endif
166 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
167 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
168 fprintf( outfile, "\tpushl %%ds\n" );
169 fprintf( outfile, "\tpopl %%ss\n" );
170 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
171 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
174 /* At this point:
175 STACK16FRAME is completely set up
176 DS, ES, SS: flat data segment
177 FS: current TEB
178 ESP: points to last STACK32FRAME
179 EBP: points to ebp member of last STACK32FRAME
180 EDX: points to current STACK16FRAME
181 ECX: contains saved flags
182 all other registers: unchanged */
184 /* Special case: C16ThkSL stub */
185 if ( thunk )
187 /* Set up registers as expected and call thunk */
188 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
189 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
191 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
193 /* Switch stack back */
194 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
195 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
196 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
198 /* Restore registers and return directly to caller */
199 fprintf( outfile, "\taddl $8, %%esp\n" );
200 fprintf( outfile, "\tpopl %%ebp\n" );
201 fprintf( outfile, "\tpopw %%ds\n" );
202 fprintf( outfile, "\tpopw %%es\n" );
203 fprintf( outfile, "\tpopw %%fs\n" );
204 fprintf( outfile, "\tpopw %%gs\n" );
205 fprintf( outfile, "\taddl $20, %%esp\n" );
207 fprintf( outfile, "\txorb %%ch, %%ch\n" );
208 fprintf( outfile, "\tpopl %%ebx\n" );
209 fprintf( outfile, "\taddw %%cx, %%sp\n" );
210 fprintf( outfile, "\tpush %%ebx\n" );
212 fprintf( outfile, "\t.byte 0x66\n" );
213 fprintf( outfile, "\tlret\n" );
215 return;
219 /* Build register CONTEXT */
220 if ( reg_func )
222 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
224 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
226 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
227 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
228 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
229 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
231 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
232 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
233 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
234 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
235 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
236 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
238 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
239 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
240 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
241 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
242 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
243 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
244 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
245 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
247 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
248 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
249 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
250 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
252 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
253 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
254 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
255 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
256 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
257 #if 0
258 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
259 #endif
261 /* Push address of CONTEXT86 structure -- popped by the relay routine */
262 fprintf( outfile, "\tpushl %%esp\n" );
266 /* Print debug info before call */
267 if ( debugging )
269 if ( UsePIC )
271 fprintf( outfile, "\tpushl %%ebx\n" );
273 /* Get Global Offset Table into %ebx (for PLT call) */
274 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
275 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
276 fprintf( outfile, "\tpopl %%ebx\n" );
277 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
280 fprintf( outfile, "\tpushl %%edx\n" );
281 if ( reg_func )
282 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
283 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
284 else
285 fprintf( outfile, "\tpushl $0\n" );
287 if ( UsePIC )
288 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
289 else
290 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
292 fprintf( outfile, "\tpopl %%edx\n" );
293 fprintf( outfile, "\tpopl %%edx\n" );
295 if ( UsePIC )
296 fprintf( outfile, "\tpopl %%ebx\n" );
299 /* Call relay routine (which will call the API entry point) */
300 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
301 fprintf( outfile, "\tpushl %%eax\n" );
302 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
303 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
305 /* Print debug info after call */
306 if ( debugging )
308 if ( UsePIC )
310 fprintf( outfile, "\tpushl %%ebx\n" );
312 /* Get Global Offset Table into %ebx (for PLT call) */
313 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
314 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
315 fprintf( outfile, "\tpopl %%ebx\n" );
316 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
319 fprintf( outfile, "\tpushl %%eax\n" );
320 if ( reg_func )
321 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
322 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
323 else
324 fprintf( outfile, "\tpushl $0\n" );
326 if ( UsePIC )
327 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
328 else
329 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
331 fprintf( outfile, "\tpopl %%eax\n" );
332 fprintf( outfile, "\tpopl %%eax\n" );
334 if ( UsePIC )
335 fprintf( outfile, "\tpopl %%ebx\n" );
339 if ( reg_func )
341 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
343 /* Switch stack back */
344 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
345 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
346 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
348 /* Get return address to CallFrom16 stub */
349 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
350 fprintf( outfile, "\tpopl %%eax\n" );
351 fprintf( outfile, "\tpopl %%edx\n" );
353 /* Restore all registers from CONTEXT */
354 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
355 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
356 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
358 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
359 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
360 fprintf( outfile, "\tpushl %%edx\n" );
361 fprintf( outfile, "\tpushl %%eax\n" );
362 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
363 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
365 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
366 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
367 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
369 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
370 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
371 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
372 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
373 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
374 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
375 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
377 fprintf( outfile, "\tpopl %%ds\n" );
378 fprintf( outfile, "\tpopfl\n" );
379 fprintf( outfile, "\tlret\n" );
381 else
383 /* Switch stack back */
384 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
385 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
386 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
388 /* Restore registers */
389 fprintf( outfile, "\tpopl %%edx\n" );
390 fprintf( outfile, "\tpopl %%ecx\n" );
391 fprintf( outfile, "\tpopl %%ebp\n" );
392 fprintf( outfile, "\tpopw %%ds\n" );
393 fprintf( outfile, "\tpopw %%es\n" );
394 fprintf( outfile, "\tpopw %%fs\n" );
395 fprintf( outfile, "\tpopw %%gs\n" );
397 /* Prepare return value and set flags accordingly */
398 if ( !short_ret )
399 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
400 fprintf( outfile, "\torl %%eax, %%eax\n" );
402 /* Return to return stub which will return to caller */
403 fprintf( outfile, "\tlret $12\n" );
408 /*******************************************************************
409 * BuildCallTo16Core
411 * This routine builds the core routines used in 32->16 thunks:
413 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
414 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
415 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
416 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
418 * These routines can be called directly from 32-bit code.
420 * All routines expect that the 16-bit stack contents (arguments) were
421 * already set up by the caller; nb_args must contain the number of bytes
422 * to be conserved. The 16-bit SS:SP will be set accordinly.
424 * All other registers are either taken from the CONTEXT86 structure
425 * or else set to default values. The target routine address is either
426 * given directly or taken from the CONTEXT86.
428 * If you want to call a 16-bit routine taking only standard argument types
429 * (WORD and LONG), you can also have an appropriate argument conversion
430 * stub automatically generated (see BuildCallTo16); you'd then call this
431 * stub, which in turn would prepare the 16-bit stack and call the appropiate
432 * core routine.
436 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
438 char *name = reg_func == 2 ? "RegisterLong" :
439 reg_func == 1 ? "RegisterShort" :
440 short_ret? "Word" : "Long";
442 /* Function header */
443 fprintf( outfile, "\n\t.align 4\n" );
444 #ifdef USE_STABS
445 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
446 name, name);
447 #endif
448 fprintf( outfile, "\t.type " PREFIX "CallTo16%s,@function\n", name );
449 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
450 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
452 /* Function entry sequence */
453 fprintf( outfile, "\tpushl %%ebp\n" );
454 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
456 /* Save the 32-bit registers */
457 fprintf( outfile, "\tpushl %%ebx\n" );
458 fprintf( outfile, "\tpushl %%ecx\n" );
459 fprintf( outfile, "\tpushl %%edx\n" );
460 fprintf( outfile, "\tpushl %%esi\n" );
461 fprintf( outfile, "\tpushl %%edi\n" );
463 if ( UsePIC )
465 /* Get Global Offset Table into %ebx */
466 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
467 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
468 fprintf( outfile, "\tpopl %%ebx\n" );
469 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
472 /* Enter Win16 Mutex */
473 if ( UsePIC )
474 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
475 else
476 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
478 /* Print debugging info */
479 if (debugging)
481 /* Push flags, number of arguments, and target */
482 fprintf( outfile, "\tpushl $%d\n", reg_func );
483 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
484 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
486 if ( UsePIC )
487 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
488 else
489 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
491 fprintf( outfile, "\taddl $12, %%esp\n" );
494 /* Get return address */
495 if ( UsePIC )
497 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
498 fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" );
500 else
501 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
503 /* Call the actual CallTo16 routine (simulate a lcall) */
504 fprintf( outfile, "\tpushl %%cs\n" );
505 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
507 /* Convert and push return value */
508 if ( short_ret )
510 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
511 fprintf( outfile, "\tpushl %%eax\n" );
513 else if ( reg_func != 2 )
515 fprintf( outfile, "\tshll $16,%%edx\n" );
516 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
517 fprintf( outfile, "\tpushl %%edx\n" );
519 else
520 fprintf( outfile, "\tpushl %%eax\n" );
522 if ( UsePIC )
524 /* Get Global Offset Table into %ebx (might have been overwritten) */
525 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
526 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
527 fprintf( outfile, "\tpopl %%ebx\n" );
528 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
531 /* Print debugging info */
532 if (debugging)
534 if ( UsePIC )
535 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
536 else
537 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
540 /* Leave Win16 Mutex */
541 if ( UsePIC )
542 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
543 else
544 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
546 /* Get return value */
547 fprintf( outfile, "\tpopl %%eax\n" );
549 /* Restore the 32-bit registers */
550 fprintf( outfile, "\tpopl %%edi\n" );
551 fprintf( outfile, "\tpopl %%esi\n" );
552 fprintf( outfile, "\tpopl %%edx\n" );
553 fprintf( outfile, "\tpopl %%ecx\n" );
554 fprintf( outfile, "\tpopl %%ebx\n" );
556 /* Function exit sequence */
557 fprintf( outfile, "\tpopl %%ebp\n" );
558 fprintf( outfile, "\tret $8\n" );
561 /* Start of the actual CallTo16 routine */
563 fprintf( outfile, ".LCallTo16%s:\n", name );
565 /* Complete STACK32FRAME */
566 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
567 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
569 /* Switch to the 16-bit stack */
570 #ifdef __svr4__
571 fprintf( outfile,"\tdata16\n");
572 #endif
573 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
574 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
575 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
577 /* Make %bp point to the previous stackframe (built by CallFrom16) */
578 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
579 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
581 /* Add the specified offset to the new sp */
582 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
584 /* Push the return address
585 * With sreg suffix, we push 16:16 address (normal lret)
586 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
588 if (reg_func != 2)
589 fprintf( outfile, "\tpushl %%ecx\n" );
590 else
592 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
593 fprintf( outfile, "\tpushw $0\n" );
594 fprintf( outfile, "\tpushw %%ax\n" );
595 fprintf( outfile, "\tpushw $0\n" );
596 fprintf( outfile, "\tpushw %%cx\n" );
599 if (reg_func)
601 /* Push the called routine address */
602 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
603 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
604 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
606 /* Get the registers */
607 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
608 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
609 fprintf( outfile, "\tmovw %%ax,%%es\n" );
610 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
611 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
612 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
613 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
614 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
615 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
616 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
617 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
618 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
620 /* Get the 16-bit ds */
621 fprintf( outfile, "\tpopw %%ds\n" );
623 else /* not a register function */
625 /* Push the called routine address */
626 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
628 /* Set %fs to the value saved by the last CallFrom16 */
629 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
630 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
632 /* Set %ds and %es (and %ax just in case) equal to %ss */
633 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
634 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
635 fprintf( outfile, "\tmovw %%ax,%%es\n" );
638 /* Jump to the called routine */
639 fprintf( outfile, "\t.byte 0x66\n" );
640 fprintf( outfile, "\tlret\n" );
644 /*******************************************************************
645 * BuildRet16Func
647 * Build the return code for 16-bit callbacks
649 static void BuildRet16Func( FILE *outfile )
652 * Note: This must reside in the .data section to allow
653 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
656 fprintf( outfile, "\n\t.type " PREFIX "CallTo16_Ret,@function\n" );
657 fprintf( outfile, "\t.globl " PREFIX "CallTo16_Ret\n" );
658 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
660 /* Restore 32-bit segment registers */
662 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", data_selector );
663 #ifdef __svr4__
664 fprintf( outfile, "\tdata16\n");
665 #endif
666 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
667 #ifdef __svr4__
668 fprintf( outfile, "\tdata16\n");
669 #endif
670 fprintf( outfile, "\tmovw %%bx,%%es\n" );
672 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
674 /* Restore the 32-bit stack */
676 #ifdef __svr4__
677 fprintf( outfile, "\tdata16\n");
678 #endif
679 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
680 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
681 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
683 /* Return to caller */
685 fprintf( outfile, "\tlret\n" );
687 /* Declare the return address variable */
689 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
690 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
694 /*******************************************************************
695 * BuildCallTo32CBClient
697 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
699 * Since the relay stub is itself 32-bit, this should not be a problem;
700 * unfortunately, the relay stubs are expected to switch back to a
701 * 16-bit stack (and 16-bit code) after completion :-(
703 * This would conflict with our 16- vs. 32-bit stack handling, so
704 * we simply switch *back* to our 32-bit stack before returning to
705 * the caller ...
707 * The CBClient relay stub expects to be called with the following
708 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
709 * stack at the designated places:
711 * ...
712 * (ebp+14) original arguments to the callback routine
713 * (ebp+10) far return address to original caller
714 * (ebp+6) Thunklet target address
715 * (ebp+2) Thunklet relay ID code
716 * (ebp) BP (saved by CBClientGlueSL)
717 * (ebp-2) SI (saved by CBClientGlueSL)
718 * (ebp-4) DI (saved by CBClientGlueSL)
719 * (ebp-6) DS (saved by CBClientGlueSL)
721 * ... buffer space used by the 16-bit side glue for temp copies
723 * (ebx+4) far return address to 16-bit side glue code
724 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
726 * The 32-bit side glue code accesses both the original arguments (via ebp)
727 * and the temporary copies prepared by the 16-bit side glue (via ebx).
728 * After completion, the stub will load ss:sp from the buffer at ebx
729 * and perform a far return to 16-bit code.
731 * To trick the relay stub into returning to us, we replace the 16-bit
732 * return address to the glue code by a cs:ip pair pointing to our
733 * return entry point (the original return address is saved first).
734 * Our return stub thus called will then reload the 32-bit ss:esp and
735 * return to 32-bit code (by using and ss:esp value that we have also
736 * pushed onto the 16-bit stack before and a cs:eip values found at
737 * that position on the 32-bit stack). The ss:esp to be restored is
738 * found relative to the 16-bit stack pointer at:
740 * (ebx-4) ss (flat)
741 * (ebx-8) sp (32-bit stack pointer)
743 * The second variant of this routine, CALL32_CBClientEx, which is used
744 * to implement KERNEL.621, has to cope with yet another problem: Here,
745 * the 32-bit side directly returns to the caller of the CBClient thunklet,
746 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
747 * As we have to return to our 32-bit code first, we have to adapt the
748 * layout of our temporary area so as to include values for the registers
749 * that are to be restored, and later (in the implementation of KERNEL.621)
750 * we *really* restore them. The return stub restores DS, DI, SI, and BP
751 * from the stack, skips the next 8 bytes (CBClient relay code / target),
752 * and then performs a lret NN, where NN is the number of arguments to be
753 * removed. Thus, we prepare our temporary area as follows:
755 * (ebx+22) 16-bit cs (this segment)
756 * (ebx+20) 16-bit ip ('16-bit' return entry point)
757 * (ebx+16) 32-bit ss (flat)
758 * (ebx+12) 32-bit sp (32-bit stack pointer)
759 * (ebx+10) 16-bit bp (points to ebx+24)
760 * (ebx+8) 16-bit si (ignored)
761 * (ebx+6) 16-bit di (ignored)
762 * (ebx+4) 16-bit ds (we actually use the flat DS here)
763 * (ebx+2) 16-bit ss (16-bit stack segment)
764 * (ebx+0) 16-bit sp (points to ebx+4)
766 * Note that we ensure that DS is not changed and remains the flat segment,
767 * and the 32-bit stack pointer our own return stub needs fits just
768 * perfectly into the 8 bytes that are skipped by the Windows stub.
769 * One problem is that we have to determine the number of removed arguments,
770 * as these have to be really removed in KERNEL.621. Thus, the BP value
771 * that we place in the temporary area to be restored, contains the value
772 * that SP would have if no arguments were removed. By comparing the actual
773 * value of SP with this value in our return stub we can compute the number
774 * of removed arguments. This is then returned to KERNEL.621.
776 * The stack layout of this function:
777 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
778 * (ebp+16) esi pointer to caller's esi value
779 * (ebp+12) arg ebp value to be set for relay stub
780 * (ebp+8) func CBClient relay stub address
781 * (ebp+4) ret addr
782 * (ebp) ebp
784 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
786 char *name = isEx? "CBClientEx" : "CBClient";
787 int size = isEx? 24 : 12;
789 /* Function header */
791 fprintf( outfile, "\n\t.align 4\n" );
792 #ifdef USE_STABS
793 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
794 name, name );
795 #endif
796 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
797 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
799 /* Entry code */
801 fprintf( outfile, "\tpushl %%ebp\n" );
802 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
803 fprintf( outfile, "\tpushl %%edi\n" );
804 fprintf( outfile, "\tpushl %%esi\n" );
805 fprintf( outfile, "\tpushl %%ebx\n" );
807 /* Get the 16-bit stack */
809 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
811 /* Convert it to a flat address */
813 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
814 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
815 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
816 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
817 fprintf( outfile, "\taddl %%eax,%%esi\n" );
819 /* Allocate temporary area (simulate STACK16_PUSH) */
821 fprintf( outfile, "\tpushf\n" );
822 fprintf( outfile, "\tcld\n" );
823 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
824 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
825 fprintf( outfile, "\trep\n\tmovsb\n" );
826 fprintf( outfile, "\tpopf\n" );
828 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
830 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
832 /* Set up temporary area */
834 if ( !isEx )
836 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
838 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
839 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
841 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
842 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
843 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
845 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
846 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
848 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
849 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
851 else
853 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
854 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
856 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
857 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
859 fprintf( outfile, "\taddl $20, %%ebx\n" );
860 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
862 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
863 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
865 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
866 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
867 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
869 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
870 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
873 /* Set up registers and call CBClient relay stub (simulating a far call) */
875 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
876 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
878 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
879 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
880 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
882 fprintf( outfile, "\tpushl %%cs\n" );
883 fprintf( outfile, "\tcall *%%eax\n" );
885 /* Return new esi value to caller */
887 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
888 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
890 /* Cleanup temporary area (simulate STACK16_POP) */
892 fprintf( outfile, "\tpop %%esi\n" );
894 fprintf( outfile, "\tpushf\n" );
895 fprintf( outfile, "\tstd\n" );
896 fprintf( outfile, "\tdec %%esi\n" );
897 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
898 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
899 fprintf( outfile, "\trep\n\tmovsb\n" );
900 fprintf( outfile, "\tpopf\n" );
902 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
904 /* Return argument size to caller */
905 if ( isEx )
907 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
908 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
911 /* Restore registers and return */
913 fprintf( outfile, "\tpopl %%ebx\n" );
914 fprintf( outfile, "\tpopl %%esi\n" );
915 fprintf( outfile, "\tpopl %%edi\n" );
916 fprintf( outfile, "\tpopl %%ebp\n" );
917 fprintf( outfile, "\tret\n" );
920 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
922 char *name = isEx? "CBClientEx" : "CBClient";
924 /* '16-bit' return stub */
926 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
927 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
929 if ( !isEx )
931 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
932 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
934 else
936 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
937 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
938 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
939 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
941 fprintf( outfile, "\tlret\n" );
943 /* Declare the return address variable */
945 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
946 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
950 /*******************************************************************
951 * BuildCallFrom32Regs
953 * Build a 32-bit-to-Wine call-back function for a 'register' function.
954 * 'args' is the number of dword arguments.
956 * Stack layout:
957 * ...
958 * (ebp+12) first arg
959 * (ebp+8) ret addr to user code
960 * (ebp+4) ret addr to relay code
961 * (ebp+0) saved ebp
962 * (ebp-128) buffer area to allow stack frame manipulation
963 * (ebp-332) CONTEXT86 struct
964 * (ebp-336) CONTEXT86 *argument
965 * .... other arguments copied from (ebp+12)
967 * The entry point routine is called with a CONTEXT* extra argument,
968 * following the normal args. In this context structure, EIP_reg
969 * contains the return address to user code, and ESP_reg the stack
970 * pointer on return (with the return address and arguments already
971 * removed).
973 static void BuildCallFrom32Regs( FILE *outfile )
975 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
977 /* Function header */
979 fprintf( outfile, "\n\t.align 4\n" );
980 #ifdef USE_STABS
981 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
982 #endif
983 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
984 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
986 /* Allocate some buffer space on the stack */
988 fprintf( outfile, "\tpushl %%ebp\n" );
989 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
990 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
992 /* Build the context structure */
994 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
995 fprintf( outfile, "\tpushfl\n" );
996 fprintf( outfile, "\tpopl %%eax\n" );
997 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
998 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
999 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1000 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1001 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1002 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1003 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1004 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1006 fprintf( outfile, "\txorl %%eax,%%eax\n" );
1007 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1008 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1009 fprintf( outfile, "\tmovw %%es,%%ax\n" );
1010 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1011 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1012 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1013 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1014 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1015 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1016 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1017 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1018 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1019 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
1021 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1022 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1024 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1025 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1027 /* Transfer the arguments */
1029 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
1030 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
1031 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1032 fprintf( outfile, "\tjecxz 1f\n" );
1033 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1034 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
1035 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1036 fprintf( outfile, "\tshrl $2,%%ecx\n" );
1037 fprintf( outfile, "\tcld\n" );
1038 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
1040 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1041 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1042 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1044 /* Call the entry point */
1046 fprintf( outfile, "\tcall *0(%%ebx)\n" );
1048 /* Store %eip and %ebp onto the new stack */
1050 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1051 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1052 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1053 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1054 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1056 /* Restore the context structure */
1058 /* Note: we don't bother to restore %cs, %ds and %ss
1059 * changing them in 32-bit code is a recipe for disaster anyway
1061 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1062 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1063 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1064 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1065 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1066 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1068 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1069 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1070 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1071 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1072 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1074 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1075 fprintf( outfile, "\tpushl %%eax\n" );
1076 fprintf( outfile, "\tpopfl\n" );
1077 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1079 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1080 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1081 fprintf( outfile, "\tpopl %%ebp\n" );
1082 fprintf( outfile, "\tret\n" );
1086 /*******************************************************************
1087 * BuildRelays
1089 * Build all the relay callbacks
1091 void BuildRelays( FILE *outfile )
1093 /* File header */
1095 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1096 fprintf( outfile, "\t.text\n" );
1098 #ifdef __i386__
1100 #ifdef USE_STABS
1101 if (output_file_name)
1103 char buffer[1024];
1104 getcwd(buffer, sizeof(buffer));
1105 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1108 * The stabs help the internal debugger as they are an indication that it
1109 * is sensible to step into a thunk/trampoline.
1111 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1112 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1113 fprintf( outfile, "Code_Start:\n\n" );
1115 #endif
1116 fprintf( outfile, PREFIX"Call16_Start:\n" );
1117 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1118 fprintf( outfile, "\t.byte 0\n\n" );
1120 /* Standard CallFrom16 routine (WORD return) */
1121 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1123 /* Standard CallFrom16 routine (DWORD return) */
1124 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1126 /* Register CallFrom16 routine */
1127 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1129 /* C16ThkSL CallFrom16 routine */
1130 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1132 /* Standard CallTo16 routine (WORD return) */
1133 BuildCallTo16Core( outfile, TRUE, FALSE );
1135 /* Standard CallTo16 routine (DWORD return) */
1136 BuildCallTo16Core( outfile, FALSE, FALSE );
1138 /* Register CallTo16 routine (16:16 retf) */
1139 BuildCallTo16Core( outfile, FALSE, 1 );
1141 /* Register CallTo16 routine (16:32 retf) */
1142 BuildCallTo16Core( outfile, FALSE, 2 );
1144 /* CBClientThunkSL routine */
1145 BuildCallTo32CBClient( outfile, FALSE );
1147 /* CBClientThunkSLEx routine */
1148 BuildCallTo32CBClient( outfile, TRUE );
1150 /* 32-bit register entry point */
1151 BuildCallFrom32Regs( outfile );
1153 fprintf( outfile, PREFIX"Call16_End:\n" );
1154 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1156 #ifdef USE_STABS
1157 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1158 fprintf( outfile, ".Letext:\n");
1159 #endif
1161 /* The whole Call16_Ret segment must lie within the .data section */
1162 fprintf( outfile, "\n\t.data\n" );
1163 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1164 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1166 /* Standard CallTo16 return stub */
1167 BuildRet16Func( outfile );
1169 /* CBClientThunkSL return stub */
1170 BuildCallTo32CBClientRet( outfile, FALSE );
1172 /* CBClientThunkSLEx return stub */
1173 BuildCallTo32CBClientRet( outfile, TRUE );
1175 /* End of Call16_Ret segment */
1176 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1177 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1179 #else /* __i386__ */
1181 fprintf( outfile, PREFIX"Call16_Start:\n" );
1182 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1183 fprintf( outfile, "\t.byte 0\n\n" );
1184 fprintf( outfile, PREFIX"Call16_End:\n" );
1185 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1187 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1188 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1189 fprintf( outfile, "\t.byte 0\n\n" );
1190 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1191 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1193 #endif /* __i386__ */