winebuild: Copy the strmake function from winegcc to simplify string formatting.
[wine] / tools / winebuild / spec16.c
1 /*
2  * 16-bit spec files
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30
31 #include "build.h"
32
33 #define NE_FFLAGS_SINGLEDATA 0x0001
34 #define NE_FFLAGS_LIBMODULE  0x8000
35
36 /* argument type flags for relay debugging */
37 enum arg_types
38 {
39     ARG_NONE = 0, /* indicates end of arg list */
40     ARG_WORD,     /* unsigned word */
41     ARG_SWORD,    /* signed word */
42     ARG_LONG,     /* long or segmented pointer */
43     ARG_PTR,      /* linear pointer */
44     ARG_STR,      /* linear pointer to null-terminated string */
45     ARG_SEGSTR,   /* segmented pointer to null-terminated string */
46     ARG_VARARG    /* start of varargs */
47 };
48
49 /* sequences of nops to fill a certain number of words */
50 static const char * const nop_sequence[4] =
51 {
52     ".byte 0x89,0xf6",  /* mov %esi,%esi */
53     ".byte 0x8d,0x74,0x26,0x00",  /* lea 0x00(%esi),%esi */
54     ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00",  /* lea 0x00000000(%esi),%esi */
55     ".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
56 };
57
58 static inline int is_function( const ORDDEF *odp )
59 {
60     if (odp->flags & FLAG_EXPORT32) return 0;
61     return (odp->type == TYPE_CDECL ||
62             odp->type == TYPE_PASCAL ||
63             odp->type == TYPE_VARARGS ||
64             odp->type == TYPE_STUB);
65 }
66
67 /*******************************************************************
68  *         output_entries
69  *
70  * Output entries for individual symbols in the entry table.
71  */
72 static void output_entries( DLLSPEC *spec, int first, int count )
73 {
74     int i;
75
76     for (i = 0; i < count; i++)
77     {
78         ORDDEF *odp = spec->ordinals[first + i];
79         output( "\t.byte 0x03\n" );  /* flags: exported & public data */
80         switch (odp->type)
81         {
82         case TYPE_CDECL:
83         case TYPE_PASCAL:
84         case TYPE_VARARGS:
85         case TYPE_STUB:
86             output( "\t%s .L__wine_%s_%u-.L__wine_spec_code_segment\n",
87                      get_asm_short_keyword(),
88                      make_c_identifier(spec->dll_name), first + i );
89             break;
90         case TYPE_VARIABLE:
91             output( "\t%s .L__wine_%s_%u-.L__wine_spec_data_segment\n",
92                      get_asm_short_keyword(),
93                      make_c_identifier(spec->dll_name), first + i );
94             break;
95         case TYPE_ABS:
96             output( "\t%s 0x%04x  /* %s */\n",
97                      get_asm_short_keyword(), odp->u.abs.value, odp->name );
98             break;
99         default:
100             assert(0);
101         }
102     }
103 }
104
105
106 /*******************************************************************
107  *         output_entry_table
108  */
109 static void output_entry_table( DLLSPEC *spec )
110 {
111     int i, prev = 0, prev_sel = -1, bundle_count = 0;
112
113     for (i = 1; i <= spec->limit; i++)
114     {
115         int selector = 0;
116         ORDDEF *odp = spec->ordinals[i];
117         if (!odp) continue;
118         if (odp->flags & FLAG_EXPORT32) continue;
119
120         switch (odp->type)
121         {
122         case TYPE_CDECL:
123         case TYPE_PASCAL:
124         case TYPE_VARARGS:
125         case TYPE_STUB:
126             selector = 1;  /* Code selector */
127             break;
128         case TYPE_VARIABLE:
129             selector = 2;  /* Data selector */
130             break;
131         case TYPE_ABS:
132             selector = 0xfe;  /* Constant selector */
133             break;
134         default:
135             continue;
136         }
137
138         if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
139         {
140             /* need to start a new bundle */
141
142             /* flush previous bundle */
143             if (bundle_count)
144             {
145                 output( "\t/* %s.%d - %s.%d */\n",
146                          spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
147                 output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
148                 output_entries( spec, prev - bundle_count + 1, bundle_count );
149             }
150
151             if (prev + 1 != i)
152             {
153                 int skip = i - (prev + 1);
154                 while (skip > 255)
155                 {
156                     output( "\t.byte 0xff,0x00\n" );
157                     skip -= 255;
158                 }
159                 output( "\t.byte 0x%02x,0x00\n", skip );
160             }
161
162             bundle_count = 0;
163             prev_sel = selector;
164         }
165         bundle_count++;
166         prev = i;
167     }
168
169     /* flush last bundle */
170     if (bundle_count)
171     {
172         output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
173         output_entries( spec, prev - bundle_count + 1, bundle_count );
174     }
175     output( "\t.byte 0x00\n" );
176 }
177
178
179 /*******************************************************************
180  *         output_resident_name
181  */
182 static void output_resident_name( const char *string, int ordinal )
183 {
184     unsigned int i, len = strlen(string);
185
186     output( "\t.byte 0x%02x", len );
187     for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)toupper(string[i]) );
188     output( " /* %s */\n", string );
189     output( "\t%s %u\n", get_asm_short_keyword(), ordinal );
190 }
191
192
193 /*******************************************************************
194  *         get_callfrom16_name
195  */
196 static const char *get_callfrom16_name( const ORDDEF *odp )
197 {
198     static char *buffer;
199
200     free( buffer );
201     buffer = strmake( "%s_%s_%s",
202                       (odp->type == TYPE_PASCAL) ? "p" :
203                       (odp->type == TYPE_VARARGS) ? "v" : "c",
204                       (odp->flags & FLAG_REGISTER) ? "regs" :
205                       (odp->flags & FLAG_RET16) ? "word" : "long",
206                       odp->u.func.arg_types );
207     return buffer;
208 }
209
210
211 /*******************************************************************
212  *         get_relay_name
213  */
214 static const char *get_relay_name( const ORDDEF *odp )
215 {
216     static char buffer[80];
217     char *p;
218
219     switch(odp->type)
220     {
221     case TYPE_PASCAL:
222         strcpy( buffer, "p_" );
223         break;
224     case TYPE_VARARGS:
225         strcpy( buffer, "v_" );
226         break;
227     case TYPE_CDECL:
228     case TYPE_STUB:
229         strcpy( buffer, "c_" );
230         break;
231     default:
232         assert(0);
233     }
234     strcat( buffer, odp->u.func.arg_types );
235     for (p = buffer + 2; *p; p++)
236     {
237         /* map string types to the corresponding plain pointer type */
238         if (*p == 't') *p = 'p';
239         else if (*p == 'T') *p = 'l';
240     }
241     if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
242     return buffer;
243 }
244
245
246 /*******************************************************************
247  *         get_function_argsize
248  */
249 static int get_function_argsize( const ORDDEF *odp )
250 {
251     const char *args;
252     int argsize = 0;
253
254     for (args = odp->u.func.arg_types; *args; args++)
255     {
256         switch (*args)
257         {
258         case 'w':  /* word */
259         case 's':  /* s_word */
260             argsize += 2;
261             break;
262         case 'l':  /* long or segmented pointer */
263         case 'T':  /* segmented pointer to null-terminated string */
264         case 'p':  /* linear pointer */
265         case 't':  /* linear pointer to null-terminated string */
266             argsize += 4;
267             break;
268         default:
269             assert(0);
270         }
271     }
272     return argsize;
273 }
274
275
276 /*******************************************************************
277  *         output_call16_function
278  *
279  * Build a 16-bit-to-Wine callback glue function.
280  *
281  * The generated routines are intended to be used as argument conversion
282  * routines to be called by the CallFrom16... core. Thus, the prototypes of
283  * the generated routines are (see also CallFrom16):
284  *
285  *  extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
286  *  extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
287  *  extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
288  *
289  * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
290  * and each 'x' is an argument  ('w'=word, 's'=signed word, 'l'=long,
291  * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
292  * 'T'=segmented pointer to null-terminated string).
293  *
294  * The generated routines fetch the arguments from the 16-bit stack (pointed
295  * to by 'args'); the offsets of the single argument values are computed
296  * according to the calling convention and the argument types.  Then, the
297  * 32-bit entry point is called with these arguments.
298  *
299  * For register functions, the arguments (if present) are converted just
300  * the same as for normal functions, but in addition the CONTEXT86 pointer
301  * filled with the current register values is passed to the 32-bit routine.
302  */
303 static void output_call16_function( ORDDEF *odp )
304 {
305     char *name;
306     int i, pos, stack_words;
307     const char *args = odp->u.func.arg_types;
308     int argsize = get_function_argsize( odp );
309     int needs_ldt = strchr( args, 'p' ) || strchr( args, 't' );
310
311     name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
312
313     output( "\t.align %d\n", get_alignment(4) );
314     output( "\t%s\n", func_declaration(name) );
315     output( "%s:\n", name );
316     output_cfi( ".cfi_startproc" );
317     output( "\tpushl %%ebp\n" );
318     output_cfi( ".cfi_adjust_cfa_offset 4" );
319     output_cfi( ".cfi_rel_offset %%ebp,0" );
320     output( "\tmovl %%esp,%%ebp\n" );
321     output_cfi( ".cfi_def_cfa_register %%ebp" );
322     stack_words = 2;
323     if (needs_ldt)
324     {
325         output( "\tpushl %%esi\n" );
326         output_cfi( ".cfi_rel_offset %%esi,-4" );
327         stack_words++;
328         if (UsePIC)
329         {
330             output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
331             output( "1:\tmovl wine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
332         }
333         else
334             output( "\tmovl $%s,%%esi\n", asm_name("wine_ldt_copy") );
335     }
336
337     /* preserve 16-byte stack alignment */
338     stack_words += strlen(args);
339     if ((odp->flags & FLAG_REGISTER) || (odp->type == TYPE_VARARGS)) stack_words++;
340     if (stack_words % 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words % 4) );
341
342     if (args[0] || odp->type == TYPE_VARARGS)
343         output( "\tmovl 12(%%ebp),%%ecx\n" );  /* args */
344
345     if (odp->flags & FLAG_REGISTER)
346     {
347         output( "\tpushl 16(%%ebp)\n" );  /* context */
348     }
349     else if (odp->type == TYPE_VARARGS)
350     {
351         output( "\tleal %d(%%ecx),%%eax\n", argsize );
352         output( "\tpushl %%eax\n" );  /* va_list16 */
353     }
354
355     pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
356     for (i = strlen(args) - 1; i >= 0; i--)
357     {
358         switch (args[i])
359         {
360         case 'w':  /* word */
361             if (odp->type != TYPE_PASCAL) pos -= 2;
362             output( "\tmovzwl %d(%%ecx),%%eax\n", pos );
363             output( "\tpushl %%eax\n" );
364             if (odp->type == TYPE_PASCAL) pos += 2;
365             break;
366
367         case 's':  /* s_word */
368             if (odp->type != TYPE_PASCAL) pos -= 2;
369             output( "\tmovswl %d(%%ecx),%%eax\n", pos );
370             output( "\tpushl %%eax\n" );
371             if (odp->type == TYPE_PASCAL) pos += 2;
372             break;
373
374         case 'l':  /* long or segmented pointer */
375         case 'T':  /* segmented pointer to null-terminated string */
376             if (odp->type != TYPE_PASCAL) pos -= 4;
377             output( "\tpushl %d(%%ecx)\n", pos );
378             if (odp->type == TYPE_PASCAL) pos += 4;
379             break;
380
381         case 'p':  /* linear pointer */
382         case 't':  /* linear pointer to null-terminated string */
383             if (odp->type != TYPE_PASCAL) pos -= 4;
384             output( "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
385             output( "\tshr $3,%%edx\n" );
386             output( "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
387             output( "\taddl (%%esi,%%edx,4),%%eax\n" );
388             output( "\tpushl %%eax\n" );
389             if (odp->type == TYPE_PASCAL) pos += 4;
390             break;
391
392         default:
393             assert(0);
394         }
395     }
396
397     output( "\tcall *8(%%ebp)\n" );
398
399     if (needs_ldt)
400     {
401         output( "\tmovl -4(%%ebp),%%esi\n" );
402         output_cfi( ".cfi_same_value %%esi" );
403     }
404     output( "\tleave\n" );
405     output_cfi( ".cfi_def_cfa %%esp,4" );
406     output_cfi( ".cfi_same_value %%ebp" );
407     output( "\tret\n" );
408     output_cfi( ".cfi_endproc" );
409     output_function_size( name );
410     free( name );
411 }
412
413
414 /*******************************************************************
415  *         callfrom16_type_compare
416  *
417  * Compare two callfrom16 sequences.
418  */
419 static int callfrom16_type_compare( const void *e1, const void *e2 )
420 {
421     const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
422     const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
423     int retval;
424     int type1 = odp1->type;
425     int type2 = odp2->type;
426
427     if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
428     if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
429
430     if ((retval = type1 - type2) != 0) return retval;
431
432     type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
433     type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
434
435     if ((retval = type1 - type2) != 0) return retval;
436
437     return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
438 }
439
440
441 /*******************************************************************
442  *         relay_type_compare
443  *
444  * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
445  */
446 static int relay_type_compare( const void *e1, const void *e2 )
447 {
448     const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
449     const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
450     char name1[80];
451
452     strcpy( name1, get_relay_name(odp1) );
453     return strcmp( name1, get_relay_name(odp2) );
454 }
455
456
457 /*******************************************************************
458  *         sort_func_list
459  *
460  * Sort a list of functions, removing duplicates.
461  */
462 static int sort_func_list( ORDDEF **list, int count,
463                            int (*compare)(const void *, const void *) )
464 {
465     int i, j;
466
467     if (!count) return 0;
468     qsort( list, count, sizeof(*list), compare );
469
470     for (i = j = 0; i < count; i++)
471     {
472         if (compare( &list[j], &list[i] )) list[++j] = list[i];
473     }
474     return j + 1;
475 }
476
477
478 /*******************************************************************
479  *         output_module16
480  *
481  * Output code for a 16-bit module.
482  */
483 static void output_module16( DLLSPEC *spec )
484 {
485     ORDDEF **typelist;
486     ORDDEF *entry_point = NULL;
487     int i, j, nb_funcs;
488
489     /* store the main entry point as ordinal 0 */
490
491     if (!spec->ordinals)
492     {
493         assert(spec->limit == 0);
494         spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) );
495         spec->ordinals[0] = NULL;
496     }
497     if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
498     {
499         entry_point = xmalloc( sizeof(*entry_point) );
500         entry_point->type = TYPE_PASCAL;
501         entry_point->ordinal = 0;
502         entry_point->lineno = 0;
503         entry_point->flags = FLAG_REGISTER;
504         entry_point->name = NULL;
505         entry_point->link_name = xstrdup( spec->init_func );
506         entry_point->export_name = NULL;
507         entry_point->u.func.arg_types[0] = 0;
508         assert( !spec->ordinals[0] );
509         spec->ordinals[0] = entry_point;
510     }
511
512     /* Build sorted list of all argument types, without duplicates */
513
514     typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
515
516     for (i = nb_funcs = 0; i <= spec->limit; i++)
517     {
518         ORDDEF *odp = spec->ordinals[i];
519         if (!odp) continue;
520         if (is_function( odp )) typelist[nb_funcs++] = odp;
521     }
522
523     nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
524
525     /* Output the module structure */
526
527     output( "\n/* module data */\n\n" );
528     output( "\t.data\n" );
529     output( "\t.align %d\n", get_alignment(4) );
530     output( ".L__wine_spec_dos_header:\n" );
531     output( "\t%s 0x5a4d\n", get_asm_short_keyword() );                    /* e_magic */
532     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_cblp */
533     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_cp */
534     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_crlc */
535     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_cparhdr */
536     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_minalloc */
537     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_maxalloc */
538     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_ss */
539     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_sp */
540     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_csum */
541     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_ip */
542     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_cs */
543     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_lfarlc */
544     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_ovno */
545     output( "\t%s 0,0,0,0\n", get_asm_short_keyword() );                   /* e_res */
546     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_oemid */
547     output( "\t%s 0\n", get_asm_short_keyword() );                         /* e_oeminfo */
548     output( "\t%s 0,0,0,0,0,0,0,0,0,0\n", get_asm_short_keyword() );       /* e_res2 */
549     output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
550
551     output( ".L__wine_spec_ne_header:\n" );
552     output( "\t%s 0x454e\n", get_asm_short_keyword() );                    /* ne_magic */
553     output( "\t.byte 0\n" );                                               /* ne_ver */
554     output( "\t.byte 0\n" );                                               /* ne_rev */
555     output( "\t%s .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n",      /* ne_enttab */
556              get_asm_short_keyword() );
557     output( "\t%s .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n",  /* ne_cbenttab */
558              get_asm_short_keyword() );
559     output( "\t.long 0\n" );                                               /* ne_crc */
560     output( "\t%s 0x%04x\n", get_asm_short_keyword(),                      /* ne_flags */
561              NE_FFLAGS_SINGLEDATA |
562              ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
563     output( "\t%s 2\n", get_asm_short_keyword() );                         /* ne_autodata */
564     output( "\t%s %u\n", get_asm_short_keyword(), spec->heap_size );       /* ne_heap */
565     output( "\t%s 0\n", get_asm_short_keyword() );                         /* ne_stack */
566     if (!entry_point) output( "\t.long 0\n" );                             /* ne_csip */
567     else output( "\t%s .L__wine_%s_0-.L__wine_spec_code_segment,1\n",
568                  get_asm_short_keyword(), make_c_identifier(spec->dll_name) );
569     output( "\t%s 0,2\n", get_asm_short_keyword() );                       /* ne_sssp */
570     output( "\t%s 2\n", get_asm_short_keyword() );                         /* ne_cseg */
571     output( "\t%s 0\n", get_asm_short_keyword() );                         /* ne_cmod */
572     output( "\t%s 0\n", get_asm_short_keyword() );                         /* ne_cbnrestab */
573     output( "\t%s .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n",      /* ne_segtab */
574              get_asm_short_keyword() );
575     output( "\t%s .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n",     /* ne_rsrctab */
576              get_asm_short_keyword() );
577     output( "\t%s .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n",      /* ne_restab */
578              get_asm_short_keyword() );
579     output( "\t%s .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n",      /* ne_modtab */
580              get_asm_short_keyword() );
581     output( "\t%s .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n",      /* ne_imptab */
582              get_asm_short_keyword() );
583     output( "\t.long 0\n" );                                   /* ne_nrestab */
584     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_cmovent */
585     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_align */
586     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_cres */
587     output( "\t.byte 0x02\n" );                                /* ne_exetyp = NE_OSFLAGS_WINDOWS */
588     output( "\t.byte 0x08\n" );                                /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
589     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_pretthunks */
590     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_psegrefbytes */
591     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_swaparea */
592     output( "\t%s 0\n", get_asm_short_keyword() );             /* ne_expver */
593
594     /* segment table */
595
596     output( "\n.L__wine_spec_ne_segtab:\n" );
597
598     /* code segment entry */
599
600     output( "\t%s .L__wine_spec_code_segment-.L__wine_spec_dos_header\n",  /* filepos */
601              get_asm_short_keyword() );
602     output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* size */
603              get_asm_short_keyword() );
604     output( "\t%s 0x2000\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_32BIT */
605     output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* minsize */
606              get_asm_short_keyword() );
607
608     /* data segment entry */
609
610     output( "\t%s .L__wine_spec_data_segment-.L__wine_spec_dos_header\n",  /* filepos */
611              get_asm_short_keyword() );
612     output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* size */
613              get_asm_short_keyword() );
614     output( "\t%s 0x0001\n", get_asm_short_keyword() ); /* flags = NE_SEGFLAGS_DATA */
615     output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* minsize */
616              get_asm_short_keyword() );
617
618     /* resource directory */
619
620     output_res16_directory( spec );
621
622     /* resident names table */
623
624     output( "\n\t.align %d\n", get_alignment(2) );
625     output( ".L__wine_spec_ne_restab:\n" );
626     output_resident_name( spec->dll_name, 0 );
627     for (i = 1; i <= spec->limit; i++)
628     {
629         ORDDEF *odp = spec->ordinals[i];
630         if (!odp || !odp->name[0]) continue;
631         if (odp->flags & FLAG_EXPORT32) continue;
632         output_resident_name( odp->name, i );
633     }
634     output( "\t.byte 0\n" );
635
636     /* imported names table */
637
638     output( "\n\t.align %d\n", get_alignment(2) );
639     output( ".L__wine_spec_ne_modtab:\n" );
640     output( ".L__wine_spec_ne_imptab:\n" );
641     output( "\t.byte 0,0\n" );
642
643     /* entry table */
644
645     output( "\n.L__wine_spec_ne_enttab:\n" );
646     output_entry_table( spec );
647     output( ".L__wine_spec_ne_enttab_end:\n" );
648
649     /* code segment */
650
651     output( "\n\t.align %d\n", get_alignment(2) );
652     output( ".L__wine_spec_code_segment:\n" );
653
654     for ( i = 0; i < nb_funcs; i++ )
655     {
656         unsigned int arg_types[2];
657         int nop_words, argsize = 0;
658
659         if ( typelist[i]->type == TYPE_PASCAL )
660             argsize = get_function_argsize( typelist[i] );
661
662         /* build the arg types bit fields */
663         arg_types[0] = arg_types[1] = 0;
664         for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
665         {
666             int type = 0;
667             switch(typelist[i]->u.func.arg_types[j])
668             {
669             case 'w': type = ARG_WORD; break;
670             case 's': type = ARG_SWORD; break;
671             case 'l': type = ARG_LONG; break;
672             case 'p': type = ARG_PTR; break;
673             case 't': type = ARG_STR; break;
674             case 'T': type = ARG_SEGSTR; break;
675             }
676             arg_types[j / 10] |= type << (3 * (j % 10));
677         }
678         if (typelist[i]->type == TYPE_VARARGS) arg_types[j / 10] |= ARG_VARARG << (3 * (j % 10));
679
680         output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
681         output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
682         output( "\tlcall $0,$0\n" );
683
684         if (typelist[i]->flags & FLAG_REGISTER)
685         {
686             nop_words = 4;
687         }
688         else if (typelist[i]->flags & FLAG_RET16)
689         {
690             output( "\torw %%ax,%%ax\n" );
691             output( "\tnop\n" );  /* so that the lretw is aligned */
692             nop_words = 2;
693         }
694         else
695         {
696             output( "\tshld $16,%%eax,%%edx\n" );
697             output( "\torl %%eax,%%eax\n" );
698             nop_words = 1;
699         }
700
701         if (argsize)
702         {
703             output( "\tlretw $%u\n", argsize );
704             nop_words--;
705         }
706         else output( "\tlretw\n" );
707
708         if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
709
710         /* the movl is here so that the code contains only valid instructions, */
711         /* it's never actually executed, we only care about the arg_types[] values */
712         output( "\t%s 0x86c7\n", get_asm_short_keyword() );
713         output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
714     }
715
716     for (i = 0; i <= spec->limit; i++)
717     {
718         ORDDEF *odp = spec->ordinals[i];
719         if (!odp || !is_function( odp )) continue;
720         output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
721         output( "\tpushw %%bp\n" );
722         output( "\tpushl $%s\n",
723                  asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : odp->link_name ));
724         output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
725     }
726     output( ".L__wine_spec_code_segment_end:\n" );
727
728     /* data segment */
729
730     output( "\n.L__wine_spec_data_segment:\n" );
731     output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" );  /* instance data */
732     for (i = 0; i <= spec->limit; i++)
733     {
734         ORDDEF *odp = spec->ordinals[i];
735         if (!odp || odp->type != TYPE_VARIABLE) continue;
736         output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
737         output( "\t.long " );
738         for (j = 0; j < odp->u.var.n_values-1; j++)
739             output( "0x%08x,", odp->u.var.values[j] );
740         output( "0x%08x\n", odp->u.var.values[j] );
741     }
742     output( ".L__wine_spec_data_segment_end:\n" );
743
744     /* resource data */
745
746     if (spec->nb_resources)
747     {
748         output( "\n.L__wine_spec_resource_data:\n" );
749         output_res16_data( spec );
750     }
751
752     output( "\t.byte 0\n" );  /* make sure the last symbol points to something */
753
754     /* relay functions */
755
756     nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
757     if (nb_funcs)
758     {
759         output( "\n/* relay functions */\n\n" );
760         output( "\t.text\n" );
761         for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
762         output( "\t.data\n" );
763         output( "wine_ldt_copy_ptr:\n" );
764         output( "\t.long %s\n", asm_name("wine_ldt_copy") );
765     }
766
767     free( typelist );
768 }
769
770
771 /*******************************************************************
772  *         output_spec16_file
773  *
774  * Output the complete data for a spec 16-bit file.
775  */
776 void output_spec16_file( DLLSPEC *spec16 )
777 {
778     DLLSPEC *spec32 = alloc_dll_spec();
779
780     resolve_imports( spec16 );
781     add_16bit_exports( spec32, spec16 );
782
783     output_standard_file_header();
784     output_module( spec32 );
785     output_module16( spec16 );
786     output_stubs( spec16 );
787     output_exports( spec32 );
788     output_imports( spec16 );
789     if (is_undefined( "__wine_call_from_16" )) output_asm_relays16();
790     if (spec16->main_module)
791     {
792         output( "\n\t%s\n", get_asm_string_section() );
793         output( ".L__wine_spec_main_module:\n" );
794         output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
795     }
796     output_gnu_stack_note();
797     free_dll_spec( spec32 );
798 }
799
800 /*******************************************************************
801  *         output_fake_module16
802  *
803  * Create a fake 16-bit binary module.
804  */
805 void output_fake_module16( DLLSPEC *spec )
806 {
807     static const unsigned char code_segment[] = { 0x90, 0xc3 };
808     static const unsigned char data_segment[16] = { 0 };
809     static const char fakedll_signature[] = "Wine placeholder DLL";
810     const unsigned int cseg = 2;
811     const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
812     const unsigned int segtab = lfanew + 0x40;
813
814     unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata;
815
816     init_output_buffer();
817
818     rsrctab = lfanew;
819     restab = segtab + 8 * cseg;
820     if (spec->nb_resources)
821     {
822         output_bin_res16_directory( spec, 0 );
823         align_output( 2 );
824         rsrctab = restab;
825         restab += output_buffer_pos;
826         free( output_buffer );
827         init_output_buffer();
828     }
829
830     namelen  = strlen( spec->dll_name );
831     modtab   = restab + ((namelen + 3) & ~1);
832     imptab   = modtab;
833     enttab   = modtab + 2;
834     cbenttab = 1;
835     codeseg  = (enttab + cbenttab + 1) & ~1;
836     dataseg  = codeseg + sizeof(code_segment);
837     rsrcdata = dataseg + sizeof(data_segment);
838
839     init_output_buffer();
840
841     put_word( 0x5a4d );       /* e_magic */
842     put_word( 0x40 );         /* e_cblp */
843     put_word( 0x01 );         /* e_cp */
844     put_word( 0 );            /* e_crlc */
845     put_word( lfanew / 16 );  /* e_cparhdr */
846     put_word( 0x0000 );       /* e_minalloc */
847     put_word( 0xffff );       /* e_maxalloc */
848     put_word( 0x0000 );       /* e_ss */
849     put_word( 0x00b8 );       /* e_sp */
850     put_word( 0 );            /* e_csum */
851     put_word( 0 );            /* e_ip */
852     put_word( 0 );            /* e_cs */
853     put_word( lfanew );       /* e_lfarlc */
854     put_word( 0 );            /* e_ovno */
855     put_dword( 0 );           /* e_res */
856     put_dword( 0 );
857     put_word( 0 );            /* e_oemid */
858     put_word( 0 );            /* e_oeminfo */
859     put_dword( 0 );           /* e_res2 */
860     put_dword( 0 );
861     put_dword( 0 );
862     put_dword( 0 );
863     put_dword( 0 );
864     put_dword( lfanew );
865
866     put_data( fakedll_signature, sizeof(fakedll_signature) );
867     align_output( 16 );
868
869     put_word( 0x454e );                    /* ne_magic */
870     put_byte( 0 );                         /* ne_ver */
871     put_byte( 0 );                         /* ne_rev */
872     put_word( enttab - lfanew );           /* ne_enttab */
873     put_word( cbenttab );                  /* ne_cbenttab */
874     put_dword( 0 );                        /* ne_crc */
875     put_word( NE_FFLAGS_SINGLEDATA |       /* ne_flags */
876               ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
877     put_word( 2 );                         /* ne_autodata */
878     put_word( spec->heap_size );           /* ne_heap */
879     put_word( 0 );                         /* ne_stack */
880     put_word( 0 ); put_word( 0 );          /* ne_csip */
881     put_word( 0 ); put_word( 2 );          /* ne_sssp */
882     put_word( cseg );                      /* ne_cseg */
883     put_word( 0 );                         /* ne_cmod */
884     put_word( 0 );                         /* ne_cbnrestab */
885     put_word( segtab - lfanew );           /* ne_segtab */
886     put_word( rsrctab - lfanew );          /* ne_rsrctab */
887     put_word( restab - lfanew );           /* ne_restab */
888     put_word( modtab - lfanew );           /* ne_modtab */
889     put_word( imptab - lfanew );           /* ne_imptab */
890     put_dword( 0 );                        /* ne_nrestab */
891     put_word( 0 );                         /* ne_cmovent */
892     put_word( 0 );                         /* ne_align */
893     put_word( 0 );                         /* ne_cres */
894     put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ );  /* ne_exetyp */
895     put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ );  /* ne_flagsothers */
896     put_word( 0 );                         /* ne_pretthunks */
897     put_word( 0 );                         /* ne_psegrefbytes */
898     put_word( 0 );                         /* ne_swaparea */
899     put_word( 0 );                         /* ne_expver */
900
901     /* segment table */
902     put_word( codeseg );
903     put_word( sizeof(code_segment) );
904     put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
905     put_word( sizeof(code_segment) );
906     put_word( dataseg );
907     put_word( sizeof(data_segment) );
908     put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
909     put_word( sizeof(data_segment) );
910
911     /* resource directory */
912     if (spec->nb_resources)
913     {
914         output_bin_res16_directory( spec, rsrcdata );
915         align_output( 2 );
916     }
917
918     /* resident names table */
919     put_byte( namelen );
920     for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
921     put_byte( 0 );
922     align_output( 2 );
923
924     /* imported names table */
925     put_word( 0 );
926
927     /* entry table */
928     put_byte( 0 );
929     align_output( 2 );
930
931     /* code segment */
932     put_data( code_segment, sizeof(code_segment) );
933
934     /* data segment */
935     put_data( data_segment, sizeof(data_segment) );
936
937     /* resource data */
938     output_bin_res16_data( spec );
939
940     flush_output_buffer();
941 }