Moved all references to file descriptors out of the generic object
[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  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <ctype.h>
29
30 #include "thread.h"
31 #include "stackframe.h"
32
33 #include "build.h"
34
35 #if defined(__GNUC__) && !defined(__svr4__)
36 static const int use_stabs = 1;
37 #else
38 static const int use_stabs = 0;
39 #endif
40
41 #ifdef __i386__
42
43 static void function_header( FILE *outfile, const char *name )
44 {
45     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
46     if (use_stabs)
47         fprintf( outfile, "\t.stabs \"%s:F1\",36,0,0," __ASM_NAME("%s") "\n", name, name);
48     fprintf( outfile, "\t" __ASM_FUNC("%s") "\n", name );
49     fprintf( outfile, "\t.globl " __ASM_NAME("%s") "\n", name );
50     fprintf( outfile, __ASM_NAME("%s") ":\n", name );
51 }
52
53
54 /*******************************************************************
55  *         BuildCallFrom16Core
56  *
57  * This routine builds the core routines used in 16->32 thunks:
58  * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
59  *
60  * These routines are intended to be called via a far call (with 32-bit
61  * operand size) from 16-bit code.  The 16-bit code stub must push %bp,
62  * the 32-bit entry point to be called, and the argument conversion
63  * routine to be used (see stack layout below).
64  *
65  * The core routine completes the STACK16FRAME on the 16-bit stack and
66  * switches to the 32-bit stack.  Then, the argument conversion routine
67  * is called; it gets passed the 32-bit entry point and a pointer to the
68  * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
69  * use conversion routines automatically generated by BuildCallFrom16,
70  * or write your own for special purposes.)
71  *
72  * The conversion routine must call the 32-bit entry point, passing it
73  * the converted arguments, and return its return value to the core.
74  * After the conversion routine has returned, the core switches back
75  * to the 16-bit stack, converts the return value to the DX:AX format
76  * (CallFrom16Long), and returns to the 16-bit call stub.  All parameters,
77  * including %bp, are popped off the stack.
78  *
79  * The 16-bit call stub now returns to the caller, popping the 16-bit
80  * arguments if necessary (pascal calling convention).
81  *
82  * In the case of a 'register' function, CallFrom16Register fills a
83  * CONTEXT86 structure with the values all registers had at the point
84  * the first instruction of the 16-bit call stub was about to be
85  * executed.  A pointer to this CONTEXT86 is passed as third parameter
86  * to the argument conversion routine, which typically passes it on
87  * to the called 32-bit entry point.
88  *
89  * CallFrom16Thunk is a special variant used by the implementation of
90  * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
91  * implemented as follows:
92  * On entry, the EBX register is set up to contain a flat pointer to the
93  * 16-bit stack such that EBX+22 points to the first argument.
94  * Then, the entry point is called, while EBP is set up to point
95  * to the return address (on the 32-bit stack).
96  * The called function returns with CX set to the number of bytes
97  * to be popped of the caller's stack.
98  *
99  * Stack layout upon entry to the core routine (STACK16FRAME):
100  *  ...           ...
101  * (sp+24) word   first 16-bit arg
102  * (sp+22) word   cs
103  * (sp+20) word   ip
104  * (sp+18) word   bp
105  * (sp+14) long   32-bit entry point (reused for Win16 mutex recursion count)
106  * (sp+12) word   ip of actual entry point (necessary for relay debugging)
107  * (sp+8)  long   relay (argument conversion) function entry point
108  * (sp+4)  long   cs of 16-bit entry point
109  * (sp)    long   ip of 16-bit entry point
110  *
111  * Added on the stack:
112  * (sp-2)  word   saved gs
113  * (sp-4)  word   saved fs
114  * (sp-6)  word   saved es
115  * (sp-8)  word   saved ds
116  * (sp-12) long   saved ebp
117  * (sp-16) long   saved ecx
118  * (sp-20) long   saved edx
119  * (sp-24) long   saved previous stack
120  */
121 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
122 {
123     char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
124
125     /* Function header */
126     if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
127     else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
128     else if (short_ret) function_header( outfile, "__wine_call_from_16_word" );
129     else function_header( outfile, "__wine_call_from_16_long" );
130
131     /* Create STACK16FRAME (except STACK32FRAME link) */
132     fprintf( outfile, "\tpushw %%gs\n" );
133     fprintf( outfile, "\tpushw %%fs\n" );
134     fprintf( outfile, "\tpushw %%es\n" );
135     fprintf( outfile, "\tpushw %%ds\n" );
136     fprintf( outfile, "\tpushl %%ebp\n" );
137     fprintf( outfile, "\tpushl %%ecx\n" );
138     fprintf( outfile, "\tpushl %%edx\n" );
139
140     /* Save original EFlags register */
141     fprintf( outfile, "\tpushfl\n" );
142
143     if ( UsePIC )
144     {
145         /* Get Global Offset Table into %ecx */
146         fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name );
147         fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name );
148         fprintf( outfile, "\tpopl %%ecx\n" );
149         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name );
150     }
151
152     if (UsePIC)
153     {
154         fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector@GOT") "(%%ecx), %%edx\n" );
155         fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
156     }
157     else
158         fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector") ",%%edx\n" );
159
160     /* Load 32-bit segment registers */
161 #ifdef __svr4__
162     fprintf( outfile, "\tdata16\n");
163 #endif
164     fprintf( outfile, "\tmovw %%dx, %%ds\n" );
165 #ifdef __svr4__
166     fprintf( outfile, "\tdata16\n");
167 #endif
168     fprintf( outfile, "\tmovw %%dx, %%es\n" );
169
170     if ( UsePIC )
171     {
172         fprintf( outfile, "\tmovl " __ASM_NAME("SYSLEVEL_Win16CurrentTeb@GOT") "(%%ecx), %%edx\n" );
173         fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
174     }
175     else
176         fprintf( outfile, "\tmovw " __ASM_NAME("SYSLEVEL_Win16CurrentTeb") ", %%fs\n" );
177
178     /* Get address of wine_ldt_copy array into %ecx */
179     if ( UsePIC )
180         fprintf( outfile, "\tmovl " __ASM_NAME("wine_ldt_copy@GOT") "(%%ecx), %%ecx\n" );
181     else
182         fprintf( outfile, "\tmovl $" __ASM_NAME("wine_ldt_copy") ", %%ecx\n" );
183
184     /* Translate STACK16FRAME base to flat offset in %edx */
185     fprintf( outfile, "\tmovw %%ss, %%dx\n" );
186     fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
187     fprintf( outfile, "\tshrl $1, %%edx\n" );
188     fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
189     fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
190     fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
191
192     /* Get saved flags into %ecx */
193     fprintf( outfile, "\tpopl %%ecx\n" );
194
195     /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
196     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
197     fprintf( outfile, "\tpushl %%ebp\n" );
198
199     /* Switch stacks */
200 #ifdef __svr4__
201     fprintf( outfile,"\tdata16\n");
202 #endif
203     fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
204     fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
205     fprintf( outfile, "\tpushl %%ds\n" );
206     fprintf( outfile, "\tpopl %%ss\n" );
207     fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
208     fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
209
210
211     /* At this point:
212        STACK16FRAME is completely set up
213        DS, ES, SS: flat data segment
214        FS: current TEB
215        ESP: points to last STACK32FRAME
216        EBP: points to ebp member of last STACK32FRAME
217        EDX: points to current STACK16FRAME
218        ECX: contains saved flags
219        all other registers: unchanged */
220
221     /* Special case: C16ThkSL stub */
222     if ( thunk )
223     {
224         /* Set up registers as expected and call thunk */
225         fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
226         fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
227
228         fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
229
230         /* Switch stack back */
231         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
232         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
233         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
234
235         /* Restore registers and return directly to caller */
236         fprintf( outfile, "\taddl $8, %%esp\n" );
237         fprintf( outfile, "\tpopl %%ebp\n" );
238         fprintf( outfile, "\tpopw %%ds\n" );
239         fprintf( outfile, "\tpopw %%es\n" );
240         fprintf( outfile, "\tpopw %%fs\n" );
241         fprintf( outfile, "\tpopw %%gs\n" );
242         fprintf( outfile, "\taddl $20, %%esp\n" );
243
244         fprintf( outfile, "\txorb %%ch, %%ch\n" );
245         fprintf( outfile, "\tpopl %%ebx\n" );
246         fprintf( outfile, "\taddw %%cx, %%sp\n" );
247         fprintf( outfile, "\tpush %%ebx\n" );
248
249         fprintf( outfile, "\t.byte 0x66\n" );
250         fprintf( outfile, "\tlret\n" );
251
252         return;
253     }
254
255
256     /* Build register CONTEXT */
257     if ( reg_func )
258     {
259         fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
260
261         fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
262
263         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
264         fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
265         fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
266         fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
267
268         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
269         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
270         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
271         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
272         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
273         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
274
275         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
276         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
277         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
278         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
279         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
280         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
281         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
282         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
283
284         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
285         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
286         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
287         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
288
289         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
290         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
291         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
292         fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
293         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
294 #if 0
295         fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
296 #endif
297
298         /* Push address of CONTEXT86 structure -- popped by the relay routine */
299         fprintf( outfile, "\tpushl %%esp\n" );
300     }
301
302
303     /* Print debug info before call */
304     if ( debugging )
305     {
306         if ( UsePIC )
307         {
308             fprintf( outfile, "\tpushl %%ebx\n" );
309
310             /* Get Global Offset Table into %ebx (for PLT call) */
311             fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name );
312             fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name );
313             fprintf( outfile, "\tpopl %%ebx\n" );
314             fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name );
315         }
316
317         fprintf( outfile, "\tpushl %%edx\n" );
318         if ( reg_func )
319             fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
320                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
321         else
322             fprintf( outfile, "\tpushl $0\n" );
323
324         if ( UsePIC )
325             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16@PLT") "\n ");
326         else
327             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16") "\n ");
328
329         fprintf( outfile, "\tpopl %%edx\n" );
330         fprintf( outfile, "\tpopl %%edx\n" );
331
332         if ( UsePIC )
333             fprintf( outfile, "\tpopl %%ebx\n" );
334     }
335
336     /* Call relay routine (which will call the API entry point) */
337     fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
338     fprintf( outfile, "\tpushl %%eax\n" );
339     fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
340     fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
341
342     /* Print debug info after call */
343     if ( debugging )
344     {
345         if ( UsePIC )
346         {
347             fprintf( outfile, "\tpushl %%ebx\n" );
348
349             /* Get Global Offset Table into %ebx (for PLT call) */
350             fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name );
351             fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name );
352             fprintf( outfile, "\tpopl %%ebx\n" );
353             fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name );
354         }
355
356         fprintf( outfile, "\tpushl %%eax\n" );
357         if ( reg_func )
358             fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
359                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
360         else
361             fprintf( outfile, "\tpushl $0\n" );
362
363         if ( UsePIC )
364             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16Ret@PLT") "\n ");
365         else
366             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16Ret") "\n ");
367
368         fprintf( outfile, "\tpopl %%eax\n" );
369         fprintf( outfile, "\tpopl %%eax\n" );
370
371         if ( UsePIC )
372             fprintf( outfile, "\tpopl %%ebx\n" );
373     }
374
375
376     if ( reg_func )
377     {
378         fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
379
380         /* Switch stack back */
381         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
382         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
383         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
384
385         /* Get return address to CallFrom16 stub */
386         fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
387         fprintf( outfile, "\tpopl %%eax\n" );
388         fprintf( outfile, "\tpopl %%edx\n" );
389
390         /* Restore all registers from CONTEXT */
391         fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
392         fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
393         fprintf( outfile, "\taddl $4, %%esp\n" );  /* room for final return address */
394
395         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
396         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
397         fprintf( outfile, "\tpushl %%edx\n" );
398         fprintf( outfile, "\tpushl %%eax\n" );
399         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
400         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
401
402         fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
403         fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
404         fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
405
406         fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
407         fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
408         fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
409         fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
410         fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
411         fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
412         fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
413
414         fprintf( outfile, "\tpopl %%ds\n" );
415         fprintf( outfile, "\tpopfl\n" );
416         fprintf( outfile, "\tlret\n" );
417     }
418     else
419     {
420         /* Switch stack back */
421         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
422         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
423         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
424
425         /* Restore registers */
426         fprintf( outfile, "\tpopl %%edx\n" );
427         fprintf( outfile, "\tpopl %%ecx\n" );
428         fprintf( outfile, "\tpopl %%ebp\n" );
429         fprintf( outfile, "\tpopw %%ds\n" );
430         fprintf( outfile, "\tpopw %%es\n" );
431         fprintf( outfile, "\tpopw %%fs\n" );
432         fprintf( outfile, "\tpopw %%gs\n" );
433
434         /* Prepare return value and set flags accordingly */
435         if ( !short_ret )
436             fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
437         fprintf( outfile, "\torl %%eax, %%eax\n" );
438
439         /* Return to return stub which will return to caller */
440         fprintf( outfile, "\tlret $12\n" );
441     }
442 }
443
444
445 /*******************************************************************
446  *         BuildCallTo16Core
447  *
448  * This routine builds the core routines used in 32->16 thunks:
449  *
450  *   extern LONG WINAPI wine_call_to_16( SEGPTR target, int nb_args );
451  *   extern void WINAPI wine_call_to_16_regs_short( const CONTEXT86 *context, int nb_args );
452  *   extern void WINAPI wine_call_to_16_regs_long ( const CONTEXT86 *context, int nb_args );
453  *
454  * These routines can be called directly from 32-bit code.
455  *
456  * All routines expect that the 16-bit stack contents (arguments) were
457  * already set up by the caller; nb_args must contain the number of bytes
458  * to be conserved.  The 16-bit SS:SP will be set accordinly.
459  *
460  * All other registers are either taken from the CONTEXT86 structure
461  * or else set to default values.  The target routine address is either
462  * given directly or taken from the CONTEXT86.
463  *
464  * If you want to call a 16-bit routine taking only standard argument types
465  * (WORD and LONG), you can also have an appropriate argument conversion
466  * stub automatically generated (see BuildCallTo16); you'd then call this
467  * stub, which in turn would prepare the 16-bit stack and call the appropiate
468  * core routine.
469  *
470  */
471 static void BuildCallTo16Core( FILE *outfile, int reg_func )
472 {
473     const char *name = reg_func == 2 ? "wine_call_to_16_regs_long" :
474                        reg_func == 1 ? "wine_call_to_16_regs_short" :
475                        "wine_call_to_16";
476
477     /* Function header */
478     function_header( outfile, name );
479
480     /* Function entry sequence */
481     fprintf( outfile, "\tpushl %%ebp\n" );
482     fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
483
484     /* Save the 32-bit registers */
485     fprintf( outfile, "\tpushl %%ebx\n" );
486     fprintf( outfile, "\tpushl %%ecx\n" );
487     fprintf( outfile, "\tpushl %%edx\n" );
488     fprintf( outfile, "\tpushl %%esi\n" );
489     fprintf( outfile, "\tpushl %%edi\n" );
490
491     if ( UsePIC )
492     {
493         /* Get Global Offset Table into %ebx */
494         fprintf( outfile, "\tcall .L%s.getgot1\n", name );
495         fprintf( outfile, ".L%s.getgot1:\n", name );
496         fprintf( outfile, "\tpopl %%ebx\n" );
497         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L%s.getgot1], %%ebx\n", name );
498     }
499
500     /* Print debugging info */
501     if (debugging)
502     {
503         /* Push flags, number of arguments, and target */
504         fprintf( outfile, "\tpushl $%d\n", reg_func );
505         fprintf( outfile, "\tpushl 12(%%ebp)\n" );
506         fprintf( outfile, "\tpushl  8(%%ebp)\n" );
507
508         if ( UsePIC )
509             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallTo16@PLT") "\n" );
510         else
511             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallTo16") "\n" );
512
513         fprintf( outfile, "\taddl $12, %%esp\n" );
514     }
515
516     /* Enter Win16 Mutex */
517     if ( UsePIC )
518         fprintf( outfile, "\tcall " __ASM_NAME("_EnterWin16Lock@PLT") "\n" );
519     else
520         fprintf( outfile, "\tcall " __ASM_NAME("_EnterWin16Lock") "\n" );
521
522     /* Setup exception frame */
523     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
524     if (UsePIC)
525         fprintf( outfile, "\tpushl " __ASM_NAME("__wine_callto16_handler@GOT") "(%%ebx)\n" );
526     else
527         fprintf( outfile, "\tpushl $" __ASM_NAME("__wine_callto16_handler") "\n" );
528     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STRUCTOFFSET(TEB,except) );
529     fprintf( outfile, "\t.byte 0x64\n\tmovl %%esp,(%d)\n", STRUCTOFFSET(TEB,except) );
530
531     /* Get return address */
532     if ( UsePIC )
533     {
534         fprintf( outfile, "\tmovl " __ASM_NAME("CallTo16_RetAddr@GOT") "(%%ebx), %%ecx\n" );
535         fprintf( outfile, "\tmovl (%%ecx), %%ecx\n" );
536     }
537     else
538         fprintf( outfile, "\tmovl " __ASM_NAME("CallTo16_RetAddr") ", %%ecx\n" );
539
540     /* Call the actual CallTo16 routine (simulate a lcall) */
541     fprintf( outfile, "\tpushl %%cs\n" );
542     fprintf( outfile, "\tcall .L%s\n", name );
543
544     /* Remove exception frame */
545     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STRUCTOFFSET(TEB,except) );
546     fprintf( outfile, "\taddl $4, %%esp\n" );
547     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
548
549     if ( !reg_func )
550     {
551         /* Convert and push return value */
552         fprintf( outfile, "\tshll $16,%%edx\n" );
553         fprintf( outfile, "\tmovw %%ax,%%dx\n" );
554         fprintf( outfile, "\tpushl %%edx\n" );
555     }
556     else
557     {
558         /*
559          * Modify CONTEXT86 structure to contain new values
560          *
561          * NOTE:  We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
562          *        The segment registers as well as ESI and EDI should
563          *        not be modified by a well-behaved 16-bit routine in
564          *        any case.  [If necessary, we could restore them as well,
565          *        at the cost of a somewhat less efficient return path.]
566          */
567
568         fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target) - STACK32OFFSET(edi));
569                 /* everything above edi has been popped already */
570
571         fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
572         fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
573         fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
574         fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
575         fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
576         fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
577                  /* The return glue code saved %esp into %esi */
578
579         fprintf( outfile, "\tpushl %%edi\n" );
580     }
581
582     if ( UsePIC )
583     {
584         /* Get Global Offset Table into %ebx (might have been overwritten) */
585         fprintf( outfile, "\tcall .L%s.getgot2\n", name );
586         fprintf( outfile, ".L%s.getgot2:\n", name );
587         fprintf( outfile, "\tpopl %%ebx\n" );
588         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L%s.getgot2], %%ebx\n", name );
589     }
590
591     /* Leave Win16 Mutex */
592     if ( UsePIC )
593         fprintf( outfile, "\tcall " __ASM_NAME("_LeaveWin16Lock@PLT") "\n" );
594     else
595         fprintf( outfile, "\tcall " __ASM_NAME("_LeaveWin16Lock") "\n" );
596
597     /* Print debugging info */
598     if (debugging)
599     {
600         fprintf( outfile, "\tpushl $%d\n", reg_func );
601
602         if ( UsePIC )
603             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallTo16Ret@PLT") "\n" );
604         else
605             fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallTo16Ret") "\n" );
606
607         fprintf( outfile, "\taddl $4, %%esp\n" );
608     }
609
610     /* Get return value */
611     fprintf( outfile, "\tpopl %%eax\n" );
612
613     /* Restore the 32-bit registers */
614     fprintf( outfile, "\tpopl %%edi\n" );
615     fprintf( outfile, "\tpopl %%esi\n" );
616     fprintf( outfile, "\tpopl %%edx\n" );
617     fprintf( outfile, "\tpopl %%ecx\n" );
618     fprintf( outfile, "\tpopl %%ebx\n" );
619
620     /* Function exit sequence */
621     fprintf( outfile, "\tpopl %%ebp\n" );
622     fprintf( outfile, "\tret $8\n" );
623
624
625     /* Start of the actual CallTo16 routine */
626
627     fprintf( outfile, ".L%s:\n", name );
628
629     /* Switch to the 16-bit stack */
630     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
631 #ifdef __svr4__
632     fprintf( outfile,"\tdata16\n");
633 #endif
634     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
635     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
636     fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
637
638     /* Make %bp point to the previous stackframe (built by CallFrom16) */
639     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
640     fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
641
642     /* Add the specified offset to the new sp */
643     fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
644
645     /* Push the return address
646      * With sreg suffix, we push 16:16 address (normal lret)
647      * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
648      */
649     if (reg_func != 2)
650         fprintf( outfile, "\tpushl %%ecx\n" );
651     else
652     {
653         fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
654         fprintf( outfile, "\tpushw $0\n" );
655         fprintf( outfile, "\tpushw %%ax\n" );
656         fprintf( outfile, "\tpushw $0\n" );
657         fprintf( outfile, "\tpushw %%cx\n" );
658     }
659
660     if (reg_func)
661     {
662         /* Push the called routine address */
663         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
664         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
665         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
666
667         /* Get the registers */
668         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
669         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
670         fprintf( outfile, "\tmovw %%ax,%%es\n" );
671         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
672         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
673         fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
674         fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
675         fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
676         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
677         fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
678         fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
679         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
680
681         /* Get the 16-bit ds */
682         fprintf( outfile, "\tpopw %%ds\n" );
683     }
684     else  /* not a register function */
685     {
686         /* Push the called routine address */
687         fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
688
689         /* Set %fs to the value saved by the last CallFrom16 */
690         fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
691         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
692
693         /* Set %ds and %es (and %ax just in case) equal to %ss */
694         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
695         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
696         fprintf( outfile, "\tmovw %%ax,%%es\n" );
697     }
698
699     /* Jump to the called routine */
700     fprintf( outfile, "\t.byte 0x66\n" );
701     fprintf( outfile, "\tlret\n" );
702 }
703
704
705 /*******************************************************************
706  *         BuildRet16Func
707  *
708  * Build the return code for 16-bit callbacks
709  */
710 static void BuildRet16Func( FILE *outfile )
711 {
712     /*
713      *  Note: This must reside in the .data section to allow
714      *        run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
715      */
716
717     function_header( outfile, "CallTo16_Ret" );
718
719     /* Save %esp into %esi */
720     fprintf( outfile, "\tmovl %%esp,%%esi\n" );
721
722     /* Restore 32-bit segment registers */
723
724     fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector") "-" __ASM_NAME("Call16_Ret_Start") ",%%edi\n" );
725 #ifdef __svr4__
726     fprintf( outfile, "\tdata16\n");
727 #endif
728     fprintf( outfile, "\tmovw %%di,%%ds\n" );
729 #ifdef __svr4__
730     fprintf( outfile, "\tdata16\n");
731 #endif
732     fprintf( outfile, "\tmovw %%di,%%es\n" );
733
734     fprintf( outfile, "\tmovw " __ASM_NAME("SYSLEVEL_Win16CurrentTeb") ",%%fs\n" );
735
736     /* Restore the 32-bit stack */
737
738 #ifdef __svr4__
739     fprintf( outfile, "\tdata16\n");
740 #endif
741     fprintf( outfile, "\tmovw %%di,%%ss\n" );
742     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
743
744     /* Return to caller */
745
746     fprintf( outfile, "\tlret\n" );
747
748     /* Declare the return address and data selector variables */
749
750     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
751     fprintf( outfile, "\t.globl " __ASM_NAME("CallTo16_DataSelector") "\n" );
752     fprintf( outfile, __ASM_NAME("CallTo16_DataSelector") ":\t.long 0\n" );
753     fprintf( outfile, "\t.globl " __ASM_NAME("CallTo16_RetAddr") "\n" );
754     fprintf( outfile, __ASM_NAME("CallTo16_RetAddr") ":\t.long 0\n" );
755 }
756
757
758 /*******************************************************************
759  *         BuildCallTo32CBClient
760  *
761  * Call a CBClient relay stub from 32-bit code (KERNEL.620).
762  *
763  * Since the relay stub is itself 32-bit, this should not be a problem;
764  * unfortunately, the relay stubs are expected to switch back to a
765  * 16-bit stack (and 16-bit code) after completion :-(
766  *
767  * This would conflict with our 16- vs. 32-bit stack handling, so
768  * we simply switch *back* to our 32-bit stack before returning to
769  * the caller ...
770  *
771  * The CBClient relay stub expects to be called with the following
772  * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
773  * stack at the designated places:
774  *
775  *    ...
776  *  (ebp+14) original arguments to the callback routine
777  *  (ebp+10) far return address to original caller
778  *  (ebp+6)  Thunklet target address
779  *  (ebp+2)  Thunklet relay ID code
780  *  (ebp)    BP (saved by CBClientGlueSL)
781  *  (ebp-2)  SI (saved by CBClientGlueSL)
782  *  (ebp-4)  DI (saved by CBClientGlueSL)
783  *  (ebp-6)  DS (saved by CBClientGlueSL)
784  *
785  *   ...     buffer space used by the 16-bit side glue for temp copies
786  *
787  *  (ebx+4)  far return address to 16-bit side glue code
788  *  (ebx)    saved 16-bit ss:sp (pointing to ebx+4)
789  *
790  * The 32-bit side glue code accesses both the original arguments (via ebp)
791  * and the temporary copies prepared by the 16-bit side glue (via ebx).
792  * After completion, the stub will load ss:sp from the buffer at ebx
793  * and perform a far return to 16-bit code.
794  *
795  * To trick the relay stub into returning to us, we replace the 16-bit
796  * return address to the glue code by a cs:ip pair pointing to our
797  * return entry point (the original return address is saved first).
798  * Our return stub thus called will then reload the 32-bit ss:esp and
799  * return to 32-bit code (by using and ss:esp value that we have also
800  * pushed onto the 16-bit stack before and a cs:eip values found at
801  * that position on the 32-bit stack).  The ss:esp to be restored is
802  * found relative to the 16-bit stack pointer at:
803  *
804  *  (ebx-4)   ss  (flat)
805  *  (ebx-8)   sp  (32-bit stack pointer)
806  *
807  * The second variant of this routine, CALL32_CBClientEx, which is used
808  * to implement KERNEL.621, has to cope with yet another problem: Here,
809  * the 32-bit side directly returns to the caller of the CBClient thunklet,
810  * restoring registers saved by CBClientGlueSL and cleaning up the stack.
811  * As we have to return to our 32-bit code first, we have to adapt the
812  * layout of our temporary area so as to include values for the registers
813  * that are to be restored, and later (in the implementation of KERNEL.621)
814  * we *really* restore them. The return stub restores DS, DI, SI, and BP
815  * from the stack, skips the next 8 bytes (CBClient relay code / target),
816  * and then performs a lret NN, where NN is the number of arguments to be
817  * removed. Thus, we prepare our temporary area as follows:
818  *
819  *     (ebx+22) 16-bit cs  (this segment)
820  *     (ebx+20) 16-bit ip  ('16-bit' return entry point)
821  *     (ebx+16) 32-bit ss  (flat)
822  *     (ebx+12) 32-bit sp  (32-bit stack pointer)
823  *     (ebx+10) 16-bit bp  (points to ebx+24)
824  *     (ebx+8)  16-bit si  (ignored)
825  *     (ebx+6)  16-bit di  (ignored)
826  *     (ebx+4)  16-bit ds  (we actually use the flat DS here)
827  *     (ebx+2)  16-bit ss  (16-bit stack segment)
828  *     (ebx+0)  16-bit sp  (points to ebx+4)
829  *
830  * Note that we ensure that DS is not changed and remains the flat segment,
831  * and the 32-bit stack pointer our own return stub needs fits just
832  * perfectly into the 8 bytes that are skipped by the Windows stub.
833  * One problem is that we have to determine the number of removed arguments,
834  * as these have to be really removed in KERNEL.621. Thus, the BP value
835  * that we place in the temporary area to be restored, contains the value
836  * that SP would have if no arguments were removed. By comparing the actual
837  * value of SP with this value in our return stub we can compute the number
838  * of removed arguments. This is then returned to KERNEL.621.
839  *
840  * The stack layout of this function:
841  * (ebp+20)  nArgs     pointer to variable receiving nr. of args (Ex only)
842  * (ebp+16)  esi       pointer to caller's esi value
843  * (ebp+12)  arg       ebp value to be set for relay stub
844  * (ebp+8)   func      CBClient relay stub address
845  * (ebp+4)   ret addr
846  * (ebp)     ebp
847  */
848 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
849 {
850     char *name = isEx? "CBClientEx" : "CBClient";
851     int size = isEx? 24 : 12;
852
853     /* Function header */
854
855     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
856     if (use_stabs)
857         fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," __ASM_NAME("CALL32_%s") "\n",
858                  name, name );
859     fprintf( outfile, "\t.globl " __ASM_NAME("CALL32_%s") "\n", name );
860     fprintf( outfile, __ASM_NAME("CALL32_%s") ":\n", name );
861
862     /* Entry code */
863
864     fprintf( outfile, "\tpushl %%ebp\n" );
865     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
866     fprintf( outfile, "\tpushl %%edi\n" );
867     fprintf( outfile, "\tpushl %%esi\n" );
868     fprintf( outfile, "\tpushl %%ebx\n" );
869
870     /* Get the 16-bit stack */
871
872     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
873
874     /* Convert it to a flat address */
875
876     fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
877     fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
878     fprintf( outfile, "\tshrl $1,%%eax\n" );
879     fprintf( outfile, "\tmovl " __ASM_NAME("wine_ldt_copy") "(%%eax),%%esi\n" );
880     fprintf( outfile, "\tmovw %%bx,%%ax\n" );
881     fprintf( outfile, "\taddl %%eax,%%esi\n" );
882
883     /* Allocate temporary area (simulate STACK16_PUSH) */
884
885     fprintf( outfile, "\tpushf\n" );
886     fprintf( outfile, "\tcld\n" );
887     fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
888     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
889     fprintf( outfile, "\trep\n\tmovsb\n" );
890     fprintf( outfile, "\tpopf\n" );
891
892     fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
893
894     fprintf( outfile, "\tpushl %%edi\n" );  /* remember address */
895
896     /* Set up temporary area */
897
898     if ( !isEx )
899     {
900         fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
901
902         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
903         fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" );    /* 32-bit sp */
904
905         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
906         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
907         fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" );    /* 32-bit ss */
908
909         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
910         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );    /* 16-bit ss:sp */
911
912         fprintf( outfile, "\tmovl " __ASM_NAME("CALL32_%s_RetAddr") ", %%eax\n", name );
913         fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );   /* overwrite return address */
914     }
915     else
916     {
917         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
918         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
919
920         fprintf( outfile, "\tmovw %%ds, %%ax\n" );
921         fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
922
923         fprintf( outfile, "\taddl $20, %%ebx\n" );
924         fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
925
926         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
927         fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
928
929         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
930         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
931         fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
932
933         fprintf( outfile, "\tmovl " __ASM_NAME("CALL32_%s_RetAddr") ", %%eax\n", name );
934         fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
935     }
936
937     /* Set up registers and call CBClient relay stub (simulating a far call) */
938
939     fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
940     fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
941
942     fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
943     fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
944     fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
945
946     fprintf( outfile, "\tpushl %%cs\n" );
947     fprintf( outfile, "\tcall *%%eax\n" );
948
949     /* Return new esi value to caller */
950
951     fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
952     fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
953
954     /* Cleanup temporary area (simulate STACK16_POP) */
955
956     fprintf( outfile, "\tpop %%esi\n" );
957
958     fprintf( outfile, "\tpushf\n" );
959     fprintf( outfile, "\tstd\n" );
960     fprintf( outfile, "\tdec %%esi\n" );
961     fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
962     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
963     fprintf( outfile, "\trep\n\tmovsb\n" );
964     fprintf( outfile, "\tpopf\n" );
965
966     fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
967
968     /* Return argument size to caller */
969     if ( isEx )
970     {
971         fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
972         fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
973     }
974
975     /* Restore registers and return */
976
977     fprintf( outfile, "\tpopl %%ebx\n" );
978     fprintf( outfile, "\tpopl %%esi\n" );
979     fprintf( outfile, "\tpopl %%edi\n" );
980     fprintf( outfile, "\tpopl %%ebp\n" );
981     fprintf( outfile, "\tret\n" );
982 }
983
984 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
985 {
986     char *name = isEx? "CBClientEx" : "CBClient";
987
988     /* '16-bit' return stub */
989
990     fprintf( outfile, "\n\t.globl " __ASM_NAME("CALL32_%s_Ret") "\n", name );
991     fprintf( outfile, __ASM_NAME("CALL32_%s_Ret") ":\n", name );
992
993     if ( !isEx )
994     {
995         fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
996         fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
997     }
998     else
999     {
1000         fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
1001         fprintf( outfile, "\tsubw %%bp, %%sp\n" );
1002         fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1003         fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
1004     }
1005     fprintf( outfile, "\tlret\n" );
1006
1007     /* Declare the return address variable */
1008
1009     fprintf( outfile, "\n\t.globl " __ASM_NAME("CALL32_%s_RetAddr") "\n", name );
1010     fprintf( outfile, __ASM_NAME("CALL32_%s_RetAddr") ":\t.long 0\n", name );
1011 }
1012
1013
1014 /*******************************************************************
1015  *         BuildCallFrom32Regs
1016  *
1017  * Build a 32-bit-to-Wine call-back function for a 'register' function.
1018  * 'args' is the number of dword arguments.
1019  *
1020  * Stack layout:
1021  *   ...
1022  * (ebp+12)  first arg
1023  * (ebp+8)   ret addr to user code
1024  * (ebp+4)   ret addr to relay code
1025  * (ebp+0)   saved ebp
1026  * (ebp-128) buffer area to allow stack frame manipulation
1027  * (ebp-332) CONTEXT86 struct
1028  * (ebp-336) CONTEXT86 *argument
1029  *  ....     other arguments copied from (ebp+12)
1030  *
1031  * The entry point routine is called with a CONTEXT* extra argument,
1032  * following the normal args. In this context structure, EIP_reg
1033  * contains the return address to user code, and ESP_reg the stack
1034  * pointer on return (with the return address and arguments already
1035  * removed).
1036  */
1037 static void BuildCallFrom32Regs( FILE *outfile )
1038 {
1039     static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
1040
1041     /* Function header */
1042
1043     function_header( outfile, "__wine_call_from_32_regs" );
1044
1045     /* Allocate some buffer space on the stack */
1046
1047     fprintf( outfile, "\tpushl %%ebp\n" );
1048     fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
1049     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
1050
1051     /* Build the context structure */
1052
1053     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1054     fprintf( outfile, "\tpushfl\n" );
1055     fprintf( outfile, "\tpopl %%eax\n" );
1056     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1057     fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
1058     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1059     fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1060     fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1061     fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1062     fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1063     fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1064
1065     fprintf( outfile, "\txorl %%eax,%%eax\n" );
1066     fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1067     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1068     fprintf( outfile, "\tmovw %%es,%%ax\n" );
1069     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1070     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1071     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1072     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1073     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1074     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1075     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1076     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1077     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1078     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
1079
1080     fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1081     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1082
1083     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1084     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1085
1086     /* Transfer the arguments */
1087
1088     fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" );   /* get relay code addr */
1089     fprintf( outfile, "\tpushl %%esp\n" );           /* push ptr to context struct */
1090     fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1091     fprintf( outfile, "\tjecxz 1f\n" );
1092     fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1093     fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" );  /* get %esp at time of call */
1094     fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1095     fprintf( outfile, "\tshrl $2,%%ecx\n" );
1096     fprintf( outfile, "\tcld\n" );
1097     fprintf( outfile, "\trep\n\tmovsl\n" );  /* copy args */
1098
1099     fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1100     fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1101     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1102
1103     /* Call the entry point */
1104
1105     fprintf( outfile, "\tcall *0(%%ebx)\n" );
1106
1107     /* Store %eip and %ebp onto the new stack */
1108
1109     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1110     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1111     fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1112     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1113     fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1114
1115     /* Restore the context structure */
1116
1117     /* Note: we don't bother to restore %cs, %ds and %ss
1118      *       changing them in 32-bit code is a recipe for disaster anyway
1119      */
1120     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1121     fprintf( outfile, "\tmovw %%ax,%%es\n" );
1122     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1123     fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1124     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1125     fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1126
1127     fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1128     fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1129     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1130     fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1131     fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1132
1133     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1134     fprintf( outfile, "\tpushl %%eax\n" );
1135     fprintf( outfile, "\tpopfl\n" );
1136     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1137
1138     fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1139     fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1140     fprintf( outfile, "\tpopl %%ebp\n" );
1141     fprintf( outfile, "\tret\n" );
1142 }
1143
1144
1145 /*******************************************************************
1146  *         BuildRelays16
1147  *
1148  * Build all the 16-bit relay callbacks
1149  */
1150 void BuildRelays16( FILE *outfile )
1151 {
1152     char buffer[1024];
1153
1154     /* File header */
1155
1156     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1157     fprintf( outfile, "\t.text\n" );
1158
1159     if (output_file_name && use_stabs)
1160     {
1161         getcwd(buffer, sizeof(buffer));
1162         fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1163
1164         /*
1165          * The stabs help the internal debugger as they are an indication that it
1166          * is sensible to step into a thunk/trampoline.
1167          */
1168         fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1169         fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1170         fprintf( outfile, "Code_Start:\n\n" );
1171     }
1172
1173     fprintf( outfile, __ASM_NAME("Call16_Start") ":\n" );
1174     fprintf( outfile, "\t.globl " __ASM_NAME("Call16_Start") "\n" );
1175     fprintf( outfile, "\t.byte 0\n\n" );
1176
1177     /* Standard CallFrom16 routine (WORD return) */
1178     BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1179
1180     /* Standard CallFrom16 routine (DWORD return) */
1181     BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1182
1183     /* Register CallFrom16 routine */
1184     BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1185
1186     /* C16ThkSL CallFrom16 routine */
1187     BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1188
1189     /* Standard CallTo16 routine */
1190     BuildCallTo16Core( outfile, 0 );
1191
1192     /* Register CallTo16 routine (16:16 retf) */
1193     BuildCallTo16Core( outfile, 1 );
1194
1195     /* Register CallTo16 routine (16:32 retf) */
1196     BuildCallTo16Core( outfile, 2 );
1197
1198     /* CBClientThunkSL routine */
1199     BuildCallTo32CBClient( outfile, FALSE );
1200
1201     /* CBClientThunkSLEx routine */
1202     BuildCallTo32CBClient( outfile, TRUE  );
1203
1204     fprintf( outfile, __ASM_NAME("Call16_End") ":\n" );
1205     fprintf( outfile, "\t.globl " __ASM_NAME("Call16_End") "\n" );
1206
1207     if (use_stabs)
1208     {
1209         fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1210         fprintf( outfile, ".Letext:\n");
1211
1212         /* Some versions of gdb need to have the filename data for
1213            each section, so output it again for the data section. */
1214         if (output_file_name)
1215         {
1216             fprintf( outfile, ".stabs \"%s/\",100,0,0,Data_Start\n", buffer);
1217             fprintf( outfile, ".stabs \"%s\",100,0,0,Data_Start\n", output_file_name );
1218             fprintf( outfile, "Data_Start:\n\n" );
1219         }
1220     }
1221
1222     /* The whole Call16_Ret segment must lie within the .data section */
1223     fprintf( outfile, "\n\t.data\n" );
1224     fprintf( outfile, "\t.globl " __ASM_NAME("Call16_Ret_Start") "\n" );
1225     fprintf( outfile, __ASM_NAME("Call16_Ret_Start") ":\n" );
1226
1227     /* Standard CallTo16 return stub */
1228     BuildRet16Func( outfile );
1229
1230     /* CBClientThunkSL return stub */
1231     BuildCallTo32CBClientRet( outfile, FALSE );
1232
1233     /* CBClientThunkSLEx return stub */
1234     BuildCallTo32CBClientRet( outfile, TRUE  );
1235
1236     /* End of Call16_Ret segment */
1237     fprintf( outfile, "\n\t.globl " __ASM_NAME("Call16_Ret_End") "\n" );
1238     fprintf( outfile, __ASM_NAME("Call16_Ret_End") ":\n" );
1239 }
1240
1241 /*******************************************************************
1242  *         BuildRelays32
1243  *
1244  * Build all the 32-bit relay callbacks
1245  */
1246 void BuildRelays32( FILE *outfile )
1247 {
1248     /* File header */
1249
1250     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1251     fprintf( outfile, "\t.text\n" );
1252
1253     if (output_file_name && use_stabs)
1254     {
1255         char buffer[1024];
1256         getcwd(buffer, sizeof(buffer));
1257         fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1258
1259         /*
1260          * The stabs help the internal debugger as they are an indication that it
1261          * is sensible to step into a thunk/trampoline.
1262          */
1263         fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1264         fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1265         fprintf( outfile, "Code_Start:\n\n" );
1266     }
1267
1268     /* 32-bit register entry point */
1269     BuildCallFrom32Regs( outfile );
1270
1271     if (use_stabs)
1272     {
1273         fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1274         fprintf( outfile, ".Letext:\n");
1275     }
1276 }
1277
1278 #else /* __i386__ */
1279
1280 void BuildRelays16( FILE *outfile )
1281 {
1282     fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1283 }
1284
1285 void BuildRelays32( FILE *outfile )
1286 {
1287     fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1288 }
1289
1290 #endif  /* __i386__ */