Added LDFLAGS to linking of wine-related binaries.
[wine] / tools / winebuild / relay.c
1 /*
2  * Relay calls helper routines
3  *
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  */
10
11 #include <ctype.h>
12 #include <unistd.h>
13
14 #include "winnt.h"
15 #include "thread.h"
16 #include "stackframe.h"
17
18 #include "build.h"
19
20
21 /*******************************************************************
22  *         BuildCallFrom16Core
23  *
24  * This routine builds the core routines used in 16->32 thunks:
25  * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
26  *
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).  
31  *
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.)
38  * 
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.
45  *
46  * The 16-bit call stub now returns to the caller, popping the 16-bit
47  * arguments if necessary (pascal calling convention).
48  *
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.
55  *
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.
65  *
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
77  *
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
87  */
88 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
89 {
90     char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
91
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 );
101
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" );
110
111     /* Save original EFlags register */
112     fprintf( outfile, "\tpushfl\n" );
113
114     if ( UsePIC )
115     {
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 );
121     }
122
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" );
133
134     if ( UsePIC )
135     {
136         fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
137         fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
138     }
139     else
140         fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
141
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" );
147
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" );  
154
155     /* Get saved flags into %ecx */
156     fprintf( outfile, "\tpopl %%ecx\n" );
157
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" );
161
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) );
172
173
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 */
183
184     /* Special case: C16ThkSL stub */
185     if ( thunk )
186     {
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" );
190
191         fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
192
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 );
197
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" );
206
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" );
211
212         fprintf( outfile, "\t.byte 0x66\n" );
213         fprintf( outfile, "\tlret\n" );
214
215         return;
216     }
217
218
219     /* Build register CONTEXT */
220     if ( reg_func )
221     {
222         fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
223
224         fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );  
225
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) );
230
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) );
237
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) );
246
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) );
251
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
260
261         /* Push address of CONTEXT86 structure -- popped by the relay routine */
262         fprintf( outfile, "\tpushl %%esp\n" );
263     }
264
265
266     /* Print debug info before call */
267     if ( debugging )
268     {
269         if ( UsePIC )
270         {
271             fprintf( outfile, "\tpushl %%ebx\n" );
272
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 );
278         }
279
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" );
286
287         if ( UsePIC )
288             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
289         else
290             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
291
292         fprintf( outfile, "\tpopl %%edx\n" );
293         fprintf( outfile, "\tpopl %%edx\n" );
294
295         if ( UsePIC )
296             fprintf( outfile, "\tpopl %%ebx\n" );
297     }
298
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) );
304
305     /* Print debug info after call */
306     if ( debugging )
307     {
308         if ( UsePIC )
309         {
310             fprintf( outfile, "\tpushl %%ebx\n" );
311
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 );
317         }
318
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" );
325
326         if ( UsePIC )
327             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
328         else
329             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
330
331         fprintf( outfile, "\tpopl %%eax\n" );
332         fprintf( outfile, "\tpopl %%eax\n" );
333
334         if ( UsePIC )
335             fprintf( outfile, "\tpopl %%ebx\n" );
336     }
337
338
339     if ( reg_func )
340     {
341         fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
342
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 );
347
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" );
352
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 */
357
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) );
364
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) );
368
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) );
376   
377         fprintf( outfile, "\tpopl %%ds\n" );
378         fprintf( outfile, "\tpopfl\n" );
379         fprintf( outfile, "\tlret\n" );
380     }
381     else
382     {
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 );
387
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" );
396
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" );
401
402         /* Return to return stub which will return to caller */
403         fprintf( outfile, "\tlret $12\n" );
404     }
405 }
406   
407
408 /*******************************************************************
409  *         BuildCallTo16Core
410  *
411  * This routine builds the core routines used in 32->16 thunks:
412  *
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 );
417  *
418  * These routines can be called directly from 32-bit code. 
419  *
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.
423  *
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.
427  *
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.
433  *
434  */
435
436 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
437 {
438     char *name = reg_func == 2 ? "RegisterLong" : 
439                  reg_func == 1 ? "RegisterShort" :
440                  short_ret? "Word" : "Long";
441
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 );
451
452     /* Function entry sequence */
453     fprintf( outfile, "\tpushl %%ebp\n" );
454     fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
455
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" );
462
463     if ( UsePIC )
464     {
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 );
470     }
471
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" );
477
478     /* Print debugging info */
479     if (debugging)
480     {
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" );
485
486         if ( UsePIC )
487             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
488         else
489             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
490
491         fprintf( outfile, "\taddl $12, %%esp\n" );
492     }
493
494     /* Get return address */
495     if ( UsePIC )
496     {
497         fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
498         fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" );
499     }
500     else
501         fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
502
503     /* Call the actual CallTo16 routine (simulate a lcall) */
504     fprintf( outfile, "\tpushl %%cs\n" );
505     fprintf( outfile, "\tcall .LCallTo16%s\n", name );
506
507     if ( !reg_func )
508     {
509         /* Convert and push return value */
510         if ( short_ret )
511         {
512             fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
513             fprintf( outfile, "\tpushl %%eax\n" );
514         }
515         else
516         {
517             fprintf( outfile, "\tshll $16,%%edx\n" );
518             fprintf( outfile, "\tmovw %%ax,%%dx\n" );
519             fprintf( outfile, "\tpushl %%edx\n" );
520         }
521     }
522     else
523     {
524         /* 
525          * Modify CONTEXT86 structure to contain new values
526          *
527          * NOTE:  We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
528          *        The segment registers as well as ESI and EDI should
529          *        not be modified by a well-behaved 16-bit routine in
530          *        any case.  [If necessary, we could restore them as well,
531          *        at the cost of a somewhat less efficient return path.]
532          */
533         
534         fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target)-12 );
535         fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
536         fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
537         fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
538         fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
539         fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
540         fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
541                  /* The return glue code saved %esp into %esi */
542
543         fprintf( outfile, "\tpushl %%edi\n" );
544     }
545
546     if ( UsePIC )
547     {
548         /* Get Global Offset Table into %ebx (might have been overwritten) */
549         fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
550         fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
551         fprintf( outfile, "\tpopl %%ebx\n" );
552         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
553     }
554
555     /* Print debugging info */
556     if (debugging)
557     {
558         fprintf( outfile, "\tpushl $%d\n", reg_func );
559
560         if ( UsePIC )
561             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
562         else
563             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
564
565         fprintf( outfile, "\taddl $4, %%esp\n" );
566     }
567
568     /* Leave Win16 Mutex */
569     if ( UsePIC )
570         fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
571     else
572         fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
573
574     /* Get return value */
575     fprintf( outfile, "\tpopl %%eax\n" );
576
577     /* Restore the 32-bit registers */
578     fprintf( outfile, "\tpopl %%edi\n" );
579     fprintf( outfile, "\tpopl %%esi\n" );
580     fprintf( outfile, "\tpopl %%edx\n" );
581     fprintf( outfile, "\tpopl %%ecx\n" );
582     fprintf( outfile, "\tpopl %%ebx\n" );
583
584     /* Function exit sequence */
585     fprintf( outfile, "\tpopl %%ebp\n" );
586     fprintf( outfile, "\tret $8\n" );
587
588
589     /* Start of the actual CallTo16 routine */
590
591     fprintf( outfile, ".LCallTo16%s:\n", name );
592
593     /* Complete STACK32FRAME */
594     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
595     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
596
597     /* Switch to the 16-bit stack */
598 #ifdef __svr4__
599     fprintf( outfile,"\tdata16\n");
600 #endif
601     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
602     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
603     fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
604
605     /* Make %bp point to the previous stackframe (built by CallFrom16) */
606     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
607     fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
608
609     /* Add the specified offset to the new sp */
610     fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
611
612     /* Push the return address 
613      * With sreg suffix, we push 16:16 address (normal lret)
614      * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
615      */
616     if (reg_func != 2)
617         fprintf( outfile, "\tpushl %%ecx\n" );
618     else 
619     {
620         fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
621         fprintf( outfile, "\tpushw $0\n" );
622         fprintf( outfile, "\tpushw %%ax\n" );
623         fprintf( outfile, "\tpushw $0\n" );
624         fprintf( outfile, "\tpushw %%cx\n" );
625     }
626
627     if (reg_func)
628     {
629         /* Push the called routine address */
630         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
631         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
632         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
633
634         /* Get the registers */
635         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
636         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
637         fprintf( outfile, "\tmovw %%ax,%%es\n" );
638         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
639         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
640         fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
641         fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
642         fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
643         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
644         fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
645         fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
646         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
647
648         /* Get the 16-bit ds */
649         fprintf( outfile, "\tpopw %%ds\n" );
650     }
651     else  /* not a register function */
652     {
653         /* Push the called routine address */
654         fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
655
656         /* Set %fs to the value saved by the last CallFrom16 */
657         fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
658         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
659
660         /* Set %ds and %es (and %ax just in case) equal to %ss */
661         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
662         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
663         fprintf( outfile, "\tmovw %%ax,%%es\n" );
664     }
665
666     /* Jump to the called routine */
667     fprintf( outfile, "\t.byte 0x66\n" );
668     fprintf( outfile, "\tlret\n" );
669 }
670
671
672 /*******************************************************************
673  *         BuildRet16Func
674  *
675  * Build the return code for 16-bit callbacks
676  */
677 static void BuildRet16Func( FILE *outfile )
678 {
679     /* 
680      *  Note: This must reside in the .data section to allow
681      *        run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
682      */
683
684     fprintf( outfile, "\n\t.type " PREFIX "CallTo16_Ret,@function\n" );
685     fprintf( outfile, "\t.globl " PREFIX "CallTo16_Ret\n" );
686     fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
687
688     /* Save %esp into %esi */
689     fprintf( outfile, "\tmovl %%esp,%%esi\n" );
690
691     /* Restore 32-bit segment registers */
692
693     fprintf( outfile, "\tmovw $0x%04x,%%di\n", data_selector );
694 #ifdef __svr4__
695     fprintf( outfile, "\tdata16\n");
696 #endif
697     fprintf( outfile, "\tmovw %%di,%%ds\n" );
698 #ifdef __svr4__
699     fprintf( outfile, "\tdata16\n");
700 #endif
701     fprintf( outfile, "\tmovw %%di,%%es\n" );
702
703     fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
704
705     /* Restore the 32-bit stack */
706
707 #ifdef __svr4__
708     fprintf( outfile, "\tdata16\n");
709 #endif
710     fprintf( outfile, "\tmovw %%di,%%ss\n" );
711     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
712     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
713
714     /* Return to caller */
715
716     fprintf( outfile, "\tlret\n" );
717
718     /* Declare the return address variable */
719
720     fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
721     fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
722 }
723
724
725 /*******************************************************************
726  *         BuildCallTo32CBClient
727  *
728  * Call a CBClient relay stub from 32-bit code (KERNEL.620).
729  *
730  * Since the relay stub is itself 32-bit, this should not be a problem;
731  * unfortunately, the relay stubs are expected to switch back to a 
732  * 16-bit stack (and 16-bit code) after completion :-(
733  *
734  * This would conflict with our 16- vs. 32-bit stack handling, so
735  * we simply switch *back* to our 32-bit stack before returning to
736  * the caller ...
737  *
738  * The CBClient relay stub expects to be called with the following
739  * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
740  * stack at the designated places:
741  *
742  *    ...
743  *  (ebp+14) original arguments to the callback routine
744  *  (ebp+10) far return address to original caller
745  *  (ebp+6)  Thunklet target address
746  *  (ebp+2)  Thunklet relay ID code
747  *  (ebp)    BP (saved by CBClientGlueSL)
748  *  (ebp-2)  SI (saved by CBClientGlueSL)
749  *  (ebp-4)  DI (saved by CBClientGlueSL)
750  *  (ebp-6)  DS (saved by CBClientGlueSL)
751  *
752  *   ...     buffer space used by the 16-bit side glue for temp copies
753  *
754  *  (ebx+4)  far return address to 16-bit side glue code
755  *  (ebx)    saved 16-bit ss:sp (pointing to ebx+4)
756  *
757  * The 32-bit side glue code accesses both the original arguments (via ebp)
758  * and the temporary copies prepared by the 16-bit side glue (via ebx).
759  * After completion, the stub will load ss:sp from the buffer at ebx
760  * and perform a far return to 16-bit code.  
761  *
762  * To trick the relay stub into returning to us, we replace the 16-bit
763  * return address to the glue code by a cs:ip pair pointing to our
764  * return entry point (the original return address is saved first).
765  * Our return stub thus called will then reload the 32-bit ss:esp and
766  * return to 32-bit code (by using and ss:esp value that we have also
767  * pushed onto the 16-bit stack before and a cs:eip values found at
768  * that position on the 32-bit stack).  The ss:esp to be restored is
769  * found relative to the 16-bit stack pointer at:
770  *
771  *  (ebx-4)   ss  (flat)
772  *  (ebx-8)   sp  (32-bit stack pointer)
773  *
774  * The second variant of this routine, CALL32_CBClientEx, which is used
775  * to implement KERNEL.621, has to cope with yet another problem: Here,
776  * the 32-bit side directly returns to the caller of the CBClient thunklet,
777  * restoring registers saved by CBClientGlueSL and cleaning up the stack.
778  * As we have to return to our 32-bit code first, we have to adapt the
779  * layout of our temporary area so as to include values for the registers
780  * that are to be restored, and later (in the implementation of KERNEL.621)
781  * we *really* restore them. The return stub restores DS, DI, SI, and BP
782  * from the stack, skips the next 8 bytes (CBClient relay code / target),
783  * and then performs a lret NN, where NN is the number of arguments to be
784  * removed. Thus, we prepare our temporary area as follows:
785  *
786  *     (ebx+22) 16-bit cs  (this segment)
787  *     (ebx+20) 16-bit ip  ('16-bit' return entry point)
788  *     (ebx+16) 32-bit ss  (flat)
789  *     (ebx+12) 32-bit sp  (32-bit stack pointer)
790  *     (ebx+10) 16-bit bp  (points to ebx+24)
791  *     (ebx+8)  16-bit si  (ignored)
792  *     (ebx+6)  16-bit di  (ignored)
793  *     (ebx+4)  16-bit ds  (we actually use the flat DS here)
794  *     (ebx+2)  16-bit ss  (16-bit stack segment)
795  *     (ebx+0)  16-bit sp  (points to ebx+4)
796  *
797  * Note that we ensure that DS is not changed and remains the flat segment,
798  * and the 32-bit stack pointer our own return stub needs fits just 
799  * perfectly into the 8 bytes that are skipped by the Windows stub.
800  * One problem is that we have to determine the number of removed arguments,
801  * as these have to be really removed in KERNEL.621. Thus, the BP value 
802  * that we place in the temporary area to be restored, contains the value 
803  * that SP would have if no arguments were removed. By comparing the actual
804  * value of SP with this value in our return stub we can compute the number
805  * of removed arguments. This is then returned to KERNEL.621.
806  *
807  * The stack layout of this function:
808  * (ebp+20)  nArgs     pointer to variable receiving nr. of args (Ex only)
809  * (ebp+16)  esi       pointer to caller's esi value
810  * (ebp+12)  arg       ebp value to be set for relay stub
811  * (ebp+8)   func      CBClient relay stub address
812  * (ebp+4)   ret addr
813  * (ebp)     ebp
814  */
815 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
816 {
817     char *name = isEx? "CBClientEx" : "CBClient";
818     int size = isEx? 24 : 12;
819
820     /* Function header */
821
822     fprintf( outfile, "\n\t.align 4\n" );
823 #ifdef USE_STABS
824     fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
825                       name, name );
826 #endif
827     fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
828     fprintf( outfile, PREFIX "CALL32_%s:\n", name );
829
830     /* Entry code */
831
832     fprintf( outfile, "\tpushl %%ebp\n" );
833     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
834     fprintf( outfile, "\tpushl %%edi\n" );
835     fprintf( outfile, "\tpushl %%esi\n" );
836     fprintf( outfile, "\tpushl %%ebx\n" );
837
838     /* Get the 16-bit stack */
839
840     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
841     
842     /* Convert it to a flat address */
843
844     fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
845     fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
846     fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
847     fprintf( outfile, "\tmovw %%bx,%%ax\n" );
848     fprintf( outfile, "\taddl %%eax,%%esi\n" );
849
850     /* Allocate temporary area (simulate STACK16_PUSH) */
851
852     fprintf( outfile, "\tpushf\n" );
853     fprintf( outfile, "\tcld\n" );
854     fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
855     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
856     fprintf( outfile, "\trep\n\tmovsb\n" );
857     fprintf( outfile, "\tpopf\n" );
858
859     fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
860
861     fprintf( outfile, "\tpushl %%edi\n" );  /* remember address */
862
863     /* Set up temporary area */
864
865     if ( !isEx )
866     {
867         fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
868
869         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
870         fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" );    /* 32-bit sp */
871
872         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
873         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
874         fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" );    /* 32-bit ss */
875
876         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
877         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );    /* 16-bit ss:sp */
878
879         fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
880         fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );   /* overwrite return address */
881     }
882     else
883     {
884         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
885         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
886
887         fprintf( outfile, "\tmovw %%ds, %%ax\n" );
888         fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
889
890         fprintf( outfile, "\taddl $20, %%ebx\n" );
891         fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
892
893         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
894         fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
895
896         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
897         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
898         fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
899
900         fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
901         fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
902     }
903
904     /* Set up registers and call CBClient relay stub (simulating a far call) */
905
906     fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
907     fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
908
909     fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
910     fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
911     fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
912
913     fprintf( outfile, "\tpushl %%cs\n" );
914     fprintf( outfile, "\tcall *%%eax\n" );
915
916     /* Return new esi value to caller */
917
918     fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
919     fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
920
921     /* Cleanup temporary area (simulate STACK16_POP) */
922
923     fprintf( outfile, "\tpop %%esi\n" );
924
925     fprintf( outfile, "\tpushf\n" );
926     fprintf( outfile, "\tstd\n" );
927     fprintf( outfile, "\tdec %%esi\n" );
928     fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
929     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
930     fprintf( outfile, "\trep\n\tmovsb\n" );
931     fprintf( outfile, "\tpopf\n" );
932
933     fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
934
935     /* Return argument size to caller */
936     if ( isEx )
937     {
938         fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
939         fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
940     }
941
942     /* Restore registers and return */
943
944     fprintf( outfile, "\tpopl %%ebx\n" );
945     fprintf( outfile, "\tpopl %%esi\n" );
946     fprintf( outfile, "\tpopl %%edi\n" );
947     fprintf( outfile, "\tpopl %%ebp\n" );
948     fprintf( outfile, "\tret\n" );
949 }
950
951 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
952 {
953     char *name = isEx? "CBClientEx" : "CBClient";
954
955     /* '16-bit' return stub */
956
957     fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
958     fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
959
960     if ( !isEx )
961     {
962         fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
963         fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
964     }
965     else
966     {
967         fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
968         fprintf( outfile, "\tsubw %%bp, %%sp\n" );
969         fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
970         fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
971     }
972     fprintf( outfile, "\tlret\n" );
973
974     /* Declare the return address variable */
975
976     fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
977     fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
978 }
979
980
981 /*******************************************************************
982  *         BuildCallFrom32Regs
983  *
984  * Build a 32-bit-to-Wine call-back function for a 'register' function.
985  * 'args' is the number of dword arguments.
986  *
987  * Stack layout:
988  *   ...
989  * (ebp+12)  first arg
990  * (ebp+8)   ret addr to user code
991  * (ebp+4)   ret addr to relay code
992  * (ebp+0)   saved ebp
993  * (ebp-128) buffer area to allow stack frame manipulation
994  * (ebp-332) CONTEXT86 struct
995  * (ebp-336) CONTEXT86 *argument
996  *  ....     other arguments copied from (ebp+12)
997  *
998  * The entry point routine is called with a CONTEXT* extra argument,
999  * following the normal args. In this context structure, EIP_reg
1000  * contains the return address to user code, and ESP_reg the stack
1001  * pointer on return (with the return address and arguments already
1002  * removed).
1003  */
1004 static void BuildCallFrom32Regs( FILE *outfile )
1005 {
1006     static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
1007
1008     /* Function header */
1009
1010     fprintf( outfile, "\n\t.align 4\n" );
1011 #ifdef USE_STABS
1012     fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
1013 #endif
1014     fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
1015     fprintf( outfile, PREFIX "CALL32_Regs:\n" );
1016
1017     /* Allocate some buffer space on the stack */
1018
1019     fprintf( outfile, "\tpushl %%ebp\n" );
1020     fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
1021     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
1022     
1023     /* Build the context structure */
1024
1025     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1026     fprintf( outfile, "\tpushfl\n" );
1027     fprintf( outfile, "\tpopl %%eax\n" );
1028     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1029     fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
1030     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1031     fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1032     fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1033     fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1034     fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1035     fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1036
1037     fprintf( outfile, "\txorl %%eax,%%eax\n" );
1038     fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1039     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1040     fprintf( outfile, "\tmovw %%es,%%ax\n" );
1041     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1042     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1043     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1044     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1045     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1046     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1047     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1048     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1049     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1050     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
1051
1052     fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1053     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1054
1055     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1056     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1057
1058     /* Transfer the arguments */
1059
1060     fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" );   /* get relay code addr */
1061     fprintf( outfile, "\tpushl %%esp\n" );           /* push ptr to context struct */
1062     fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1063     fprintf( outfile, "\tjecxz 1f\n" );
1064     fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1065     fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" );  /* get %esp at time of call */
1066     fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1067     fprintf( outfile, "\tshrl $2,%%ecx\n" );
1068     fprintf( outfile, "\tcld\n" );
1069     fprintf( outfile, "\trep\n\tmovsl\n" );  /* copy args */
1070
1071     fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1072     fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1073     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1074
1075     /* Call the entry point */
1076
1077     fprintf( outfile, "\tcall *0(%%ebx)\n" );
1078
1079     /* Store %eip and %ebp onto the new stack */
1080
1081     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1082     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1083     fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1084     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1085     fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1086
1087     /* Restore the context structure */
1088
1089     /* Note: we don't bother to restore %cs, %ds and %ss
1090      *       changing them in 32-bit code is a recipe for disaster anyway
1091      */
1092     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1093     fprintf( outfile, "\tmovw %%ax,%%es\n" );
1094     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1095     fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1096     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1097     fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1098
1099     fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1100     fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1101     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1102     fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1103     fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1104
1105     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1106     fprintf( outfile, "\tpushl %%eax\n" );
1107     fprintf( outfile, "\tpopfl\n" );
1108     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1109
1110     fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1111     fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1112     fprintf( outfile, "\tpopl %%ebp\n" );
1113     fprintf( outfile, "\tret\n" );
1114 }
1115
1116
1117 /*******************************************************************
1118  *         BuildRelays
1119  *
1120  * Build all the relay callbacks
1121  */
1122 void BuildRelays( FILE *outfile )
1123 {
1124     /* File header */
1125
1126     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1127     fprintf( outfile, "\t.text\n" );
1128
1129 #ifdef __i386__
1130
1131 #ifdef USE_STABS
1132     if (output_file_name)
1133     {
1134         char buffer[1024];
1135         getcwd(buffer, sizeof(buffer));
1136         fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1137
1138         /*
1139          * The stabs help the internal debugger as they are an indication that it
1140          * is sensible to step into a thunk/trampoline.
1141          */
1142         fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1143         fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1144         fprintf( outfile, "Code_Start:\n\n" );
1145     }
1146 #endif
1147     fprintf( outfile, PREFIX"Call16_Start:\n" );
1148     fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1149     fprintf( outfile, "\t.byte 0\n\n" );
1150
1151     /* Standard CallFrom16 routine (WORD return) */
1152     BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1153
1154     /* Standard CallFrom16 routine (DWORD return) */
1155     BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1156
1157     /* Register CallFrom16 routine */
1158     BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1159
1160     /* C16ThkSL CallFrom16 routine */
1161     BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1162
1163     /* Standard CallTo16 routine (WORD return) */
1164     BuildCallTo16Core( outfile, TRUE, FALSE );
1165
1166     /* Standard CallTo16 routine (DWORD return) */
1167     BuildCallTo16Core( outfile, FALSE, FALSE );
1168
1169     /* Register CallTo16 routine (16:16 retf) */
1170     BuildCallTo16Core( outfile, FALSE, 1 );
1171
1172     /* Register CallTo16 routine (16:32 retf) */
1173     BuildCallTo16Core( outfile, FALSE, 2 );
1174
1175     /* CBClientThunkSL routine */
1176     BuildCallTo32CBClient( outfile, FALSE );
1177
1178     /* CBClientThunkSLEx routine */
1179     BuildCallTo32CBClient( outfile, TRUE  );
1180
1181     /* 32-bit register entry point */
1182     BuildCallFrom32Regs( outfile );
1183
1184     fprintf( outfile, PREFIX"Call16_End:\n" );
1185     fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1186
1187 #ifdef USE_STABS
1188     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1189     fprintf( outfile, ".Letext:\n");
1190 #endif
1191
1192     /* The whole Call16_Ret segment must lie within the .data section */
1193     fprintf( outfile, "\n\t.data\n" );
1194     fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1195     fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1196
1197     /* Standard CallTo16 return stub */
1198     BuildRet16Func( outfile );
1199
1200     /* CBClientThunkSL return stub */
1201     BuildCallTo32CBClientRet( outfile, FALSE );
1202
1203     /* CBClientThunkSLEx return stub */
1204     BuildCallTo32CBClientRet( outfile, TRUE  );
1205
1206     /* End of Call16_Ret segment */
1207     fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1208     fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1209
1210 #else  /* __i386__ */
1211
1212     fprintf( outfile, PREFIX"Call16_Start:\n" );
1213     fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1214     fprintf( outfile, "\t.byte 0\n\n" );
1215     fprintf( outfile, PREFIX"Call16_End:\n" );
1216     fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1217
1218     fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1219     fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1220     fprintf( outfile, "\t.byte 0\n\n" );
1221     fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1222     fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1223
1224 #endif  /* __i386__ */
1225 }