strmbase: do not lock in BaseOutputPinImpl_GetDeliveryBuffer the MemInputPin will...
[wine] / tools / winebuild / spec32.c
1 /*
2  * 32-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 #include <stdarg.h>
31 #include <string.h>
32
33 #include "build.h"
34
35 #define IMAGE_FILE_MACHINE_UNKNOWN 0
36 #define IMAGE_FILE_MACHINE_I386    0x014c
37 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
38 #define IMAGE_FILE_MACHINE_AMD64   0x8664
39 #define IMAGE_FILE_MACHINE_ARMV7   0x01C4
40 /* Wine extension */
41 #define IMAGE_FILE_MACHINE_SPARC   0x2000
42
43 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
44 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45
46 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
47 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
48 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC  0x107
49
50 /* check if entry point needs a relay thunk */
51 static inline int needs_relay( const ORDDEF *odp )
52 {
53     /* skip nonexistent entry points */
54     if (!odp) return 0;
55     /* skip non-functions */
56     switch (odp->type)
57     {
58     case TYPE_STDCALL:
59     case TYPE_CDECL:
60     case TYPE_THISCALL:
61         break;
62     case TYPE_STUB:
63         if (odp->u.func.nb_args != -1) break;
64         /* fall through */
65     default:
66         return 0;
67     }
68     /* skip norelay and forward entry points */
69     if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
70     /* skip register entry points on x86_64 */
71     if (target_cpu == CPU_x86_64 && (odp->flags & FLAG_REGISTER)) return 0;
72     return 1;
73 }
74
75 static int is_float_arg( const ORDDEF *odp, int arg )
76 {
77     if (arg >= odp->u.func.nb_args) return 0;
78     return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
79 }
80
81 /* check if dll will output relay thunks */
82 int has_relays( DLLSPEC *spec )
83 {
84     int i;
85
86     if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
87
88     for (i = spec->base; i <= spec->limit; i++)
89     {
90         ORDDEF *odp = spec->ordinals[i];
91         if (needs_relay( odp )) return 1;
92     }
93     return 0;
94 }
95
96 /*******************************************************************
97  *         output_relay_debug
98  *
99  * Output entry points for relay debugging
100  */
101 static void output_relay_debug( DLLSPEC *spec )
102 {
103     int i, j;
104     unsigned int pos, args, flags;
105
106     /* first the table of entry point offsets */
107
108     output( "\t%s\n", get_asm_rodata_section() );
109     output( "\t.align %d\n", get_alignment(4) );
110     output( ".L__wine_spec_relay_entry_point_offsets:\n" );
111
112     for (i = spec->base; i <= spec->limit; i++)
113     {
114         ORDDEF *odp = spec->ordinals[i];
115
116         if (needs_relay( odp ))
117             output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
118         else
119             output( "\t.long 0\n" );
120     }
121
122     /* then the table of argument types */
123
124     output( "\t.align %d\n", get_alignment(4) );
125     output( ".L__wine_spec_relay_arg_types:\n" );
126
127     for (i = spec->base; i <= spec->limit; i++)
128     {
129         ORDDEF *odp = spec->ordinals[i];
130         unsigned int mask = 0;
131
132         if (needs_relay( odp ))
133         {
134             for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
135             {
136                 switch (odp->u.func.args[j])
137                 {
138                 case ARG_STR:    mask |= 1 << (2 * pos++); break;
139                 case ARG_WSTR:   mask |= 2 << (2 * pos++); break;
140                 case ARG_INT64:
141                 case ARG_DOUBLE: pos += 8 / get_ptr_size(); break;
142                 case ARG_INT128: pos += (target_cpu == CPU_x86) ? 4 : 1; break;
143                 default:         pos++; break;
144                 }
145             }
146         }
147         output( "\t.long 0x%08x\n", mask );
148     }
149
150     /* then the relay thunks */
151
152     output( "\t.text\n" );
153     output( "__wine_spec_relay_entry_points:\n" );
154     output( "\tnop\n" );  /* to avoid 0 offset */
155
156     for (i = spec->base; i <= spec->limit; i++)
157     {
158         ORDDEF *odp = spec->ordinals[i];
159
160         if (!needs_relay( odp )) continue;
161
162         output( "\t.align %d\n", get_alignment(4) );
163         output( ".L__wine_spec_relay_entry_point_%d:\n", i );
164         output_cfi( ".cfi_startproc" );
165
166         args = get_args_size(odp) / get_ptr_size();
167         flags = 0;
168
169         switch (target_cpu)
170         {
171         case CPU_x86:
172             if (odp->type == TYPE_THISCALL)  /* add the this pointer */
173             {
174                 output( "\tpopl %%eax\n" );
175                 output( "\tpushl %%ecx\n" );
176                 output( "\tpushl %%eax\n" );
177                 flags |= 2;
178             }
179             if (odp->flags & FLAG_REGISTER)
180                 output( "\tpushl %%eax\n" );
181             else
182                 output( "\tpushl %%esp\n" );
183             output_cfi( ".cfi_adjust_cfa_offset 4" );
184
185             if (odp->flags & FLAG_RET64) flags |= 1;
186             output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
187             output_cfi( ".cfi_adjust_cfa_offset 4" );
188
189             if (UsePIC)
190             {
191                 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
192                 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
193             }
194             else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
195             output( "\tpushl %%eax\n" );
196             output_cfi( ".cfi_adjust_cfa_offset 4" );
197
198             if (odp->flags & FLAG_REGISTER)
199             {
200                 output( "\tcall *8(%%eax)\n" );
201             }
202             else
203             {
204                 output( "\tcall *4(%%eax)\n" );
205                 output_cfi( ".cfi_adjust_cfa_offset -12" );
206                 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
207                     output( "\tret $%u\n", args * get_ptr_size() );
208                 else
209                     output( "\tret\n" );
210             }
211             break;
212
213         case CPU_x86_64:
214             output( "\tsubq $40,%%rsp\n" );
215             output_cfi( ".cfi_adjust_cfa_offset 40" );
216             switch (args)
217             {
218             default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
219             /* fall through */
220             case 3:  output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
221             /* fall through */
222             case 2:  output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
223             /* fall through */
224             case 1:  output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
225             /* fall through */
226             case 0:  break;
227             }
228             output( "\tleaq 40(%%rsp),%%r8\n" );
229             output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
230             output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
231             output( "\tcallq *8(%%rcx)\n" );
232             output( "\taddq $40,%%rsp\n" );
233             output_cfi( ".cfi_adjust_cfa_offset -40" );
234             output( "\tret\n" );
235             break;
236
237         default:
238             assert(0);
239         }
240         output_cfi( ".cfi_endproc" );
241     }
242 }
243
244 /*******************************************************************
245  *         output_exports
246  *
247  * Output the export table for a Win32 module.
248  */
249 void output_exports( DLLSPEC *spec )
250 {
251     int i, fwd_size = 0;
252     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
253
254     if (!nr_exports) return;
255
256     output( "\n/* export table */\n\n" );
257     output( "\t.data\n" );
258     output( "\t.align %d\n", get_alignment(4) );
259     output( ".L__wine_spec_exports:\n" );
260
261     /* export directory header */
262
263     output( "\t.long 0\n" );                       /* Characteristics */
264     output( "\t.long 0\n" );                       /* TimeDateStamp */
265     output( "\t.long 0\n" );                       /* MajorVersion/MinorVersion */
266     output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
267     output( "\t.long %u\n", spec->base );          /* Base */
268     output( "\t.long %u\n", nr_exports );          /* NumberOfFunctions */
269     output( "\t.long %u\n", spec->nb_names );      /* NumberOfNames */
270     output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
271     if (spec->nb_names)
272     {
273         output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
274         output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" );  /* AddressOfNameOrdinals */
275     }
276     else
277     {
278         output( "\t.long 0\n" );  /* AddressOfNames */
279         output( "\t.long 0\n" );  /* AddressOfNameOrdinals */
280     }
281
282     /* output the function pointers */
283
284     output( "\n.L__wine_spec_exports_funcs:\n" );
285     for (i = spec->base; i <= spec->limit; i++)
286     {
287         ORDDEF *odp = spec->ordinals[i];
288         if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
289         else switch(odp->type)
290         {
291         case TYPE_EXTERN:
292         case TYPE_STDCALL:
293         case TYPE_VARARGS:
294         case TYPE_CDECL:
295         case TYPE_THISCALL:
296             if (odp->flags & FLAG_FORWARD)
297             {
298                 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
299                 fwd_size += strlen(odp->link_name) + 1;
300             }
301             else if (odp->flags & FLAG_EXT_LINK)
302             {
303                 output( "\t%s %s_%s\n",
304                          get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
305             }
306             else
307             {
308                 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
309             }
310             break;
311         case TYPE_STUB:
312             output( "\t%s %s\n", get_asm_ptr_keyword(),
313                      asm_name( get_stub_name( odp, spec )) );
314             break;
315         default:
316             assert(0);
317         }
318     }
319
320     if (spec->nb_names)
321     {
322         /* output the function name pointers */
323
324         int namepos = strlen(spec->file_name) + 1;
325
326         output( "\n.L__wine_spec_exp_name_ptrs:\n" );
327         for (i = 0; i < spec->nb_names; i++)
328         {
329             output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
330             namepos += strlen(spec->names[i]->name) + 1;
331         }
332
333         /* output the function ordinals */
334
335         output( "\n.L__wine_spec_exp_ordinals:\n" );
336         for (i = 0; i < spec->nb_names; i++)
337         {
338             output( "\t%s %d\n",
339                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
340         }
341         if (spec->nb_names % 2)
342         {
343             output( "\t%s 0\n", get_asm_short_keyword() );
344         }
345     }
346
347     /* output the export name strings */
348
349     output( "\n.L__wine_spec_exp_names:\n" );
350     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
351     for (i = 0; i < spec->nb_names; i++)
352         output( "\t%s \"%s\"\n",
353                  get_asm_string_keyword(), spec->names[i]->name );
354
355     /* output forward strings */
356
357     if (fwd_size)
358     {
359         output( "\n.L__wine_spec_forwards:\n" );
360         for (i = spec->base; i <= spec->limit; i++)
361         {
362             ORDDEF *odp = spec->ordinals[i];
363             if (odp && (odp->flags & FLAG_FORWARD))
364                 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
365         }
366     }
367     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
368     output( ".L__wine_spec_exports_end:\n" );
369
370     /* output relays */
371
372     if (!has_relays( spec ))
373     {
374         output( "\t%s 0\n", get_asm_ptr_keyword() );
375         return;
376     }
377
378     output( ".L__wine_spec_relay_descr:\n" );
379     output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() );  /* magic */
380     output( "\t%s 0,0\n", get_asm_ptr_keyword() );         /* relay funcs */
381     output( "\t%s 0\n", get_asm_ptr_keyword() );           /* private data */
382     output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
383     output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
384     output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
385
386     output_relay_debug( spec );
387 }
388
389
390 /*******************************************************************
391  *         output_asm_constructor
392  *
393  * Output code for calling a dll constructor.
394  */
395 static void output_asm_constructor( const char *constructor )
396 {
397     if (target_platform == PLATFORM_APPLE)
398     {
399         /* Mach-O doesn't have an init section */
400         output( "\n\t.mod_init_func\n" );
401         output( "\t.align %d\n", get_alignment(4) );
402         output( "\t.long %s\n", asm_name(constructor) );
403     }
404     else
405     {
406         output( "\n\t.section \".init\",\"ax\"\n" );
407         switch(target_cpu)
408         {
409         case CPU_x86:
410         case CPU_x86_64:
411             output( "\tcall %s\n", asm_name(constructor) );
412             break;
413         case CPU_SPARC:
414             output( "\tcall %s\n", asm_name(constructor) );
415             output( "\tnop\n" );
416             break;
417         case CPU_ARM:
418             output( "\tblx %s\n", asm_name(constructor) );
419             output( "\t.arm\n" );
420             break;
421         case CPU_POWERPC:
422             output( "\tbl %s\n", asm_name(constructor) );
423             break;
424         }
425     }
426 }
427
428
429 /*******************************************************************
430  *         output_module
431  *
432  * Output the module data.
433  */
434 void output_module( DLLSPEC *spec )
435 {
436     int machine = 0;
437     unsigned int page_size = get_page_size();
438
439     /* Reserve some space for the PE header */
440
441     switch (target_platform)
442     {
443     case PLATFORM_APPLE:
444         output( "\t.text\n" );
445         output( "\t.align %d\n", get_alignment(page_size) );
446         output( "__wine_spec_pe_header:\n" );
447         output( "\t.space 65536\n" );
448         break;
449     case PLATFORM_SOLARIS:
450         output( "\n\t.section \".text\",\"ax\"\n" );
451         output( "__wine_spec_pe_header:\n" );
452         output( "\t.skip %u\n", 65536 + page_size );
453         break;
454     default:
455         output( "\n\t.section \".init\",\"ax\"\n" );
456         switch(target_cpu)
457         {
458         case CPU_x86:
459         case CPU_x86_64:
460         case CPU_SPARC:
461             output( "\tjmp 1f\n" );
462             break;
463         case CPU_ARM:
464             output( "\n\t.syntax unified\n" );
465             output( "\n\t.thumb\n" );
466             output( "\tb.w 1f\n" );
467             break;
468         case CPU_POWERPC:
469             output( "\tb 1f\n" );
470             break;
471         }
472         output( "__wine_spec_pe_header:\n" );
473         output( "\t.skip %u\n", 65536 + page_size );
474         output( "1:\n" );
475         break;
476     }
477
478     /* Output the NT header */
479
480     output( "\n\t.data\n" );
481     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
482     output( "%s\n", asm_globl("__wine_spec_nt_header") );
483     output( ".L__wine_spec_rva_base:\n" );
484
485     output( "\t.long 0x4550\n" );         /* Signature */
486     switch(target_cpu)
487     {
488     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
489     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
490     case CPU_ARM:     machine = IMAGE_FILE_MACHINE_ARMV7; break;
491     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
492     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_SPARC; break;
493     }
494     output( "\t%s 0x%04x\n",              /* Machine */
495              get_asm_short_keyword(), machine );
496     output( "\t%s 0\n",                   /* NumberOfSections */
497              get_asm_short_keyword() );
498     output( "\t.long 0\n" );              /* TimeDateStamp */
499     output( "\t.long 0\n" );              /* PointerToSymbolTable */
500     output( "\t.long 0\n" );              /* NumberOfSymbols */
501     output( "\t%s %d\n",                  /* SizeOfOptionalHeader */
502              get_asm_short_keyword(),
503              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
504     output( "\t%s 0x%04x\n",              /* Characteristics */
505              get_asm_short_keyword(), spec->characteristics );
506     output( "\t%s 0x%04x\n",              /* Magic */
507              get_asm_short_keyword(),
508              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
509     output( "\t.byte 0\n" );              /* MajorLinkerVersion */
510     output( "\t.byte 0\n" );              /* MinorLinkerVersion */
511     output( "\t.long 0\n" );              /* SizeOfCode */
512     output( "\t.long 0\n" );              /* SizeOfInitializedData */
513     output( "\t.long 0\n" );              /* SizeOfUninitializedData */
514     /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
515     output( "\t%s %s\n",                  /* AddressOfEntryPoint */
516             get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
517     if (get_ptr_size() == 4)
518     {
519         output( "\t.long 0\n" );          /* BaseOfCode */
520         output( "\t.long 0\n" );          /* BaseOfData */
521     }
522     output( "\t%s __wine_spec_pe_header\n",         /* ImageBase */
523              get_asm_ptr_keyword() );
524     output( "\t.long %u\n", page_size );  /* SectionAlignment */
525     output( "\t.long %u\n", page_size );  /* FileAlignment */
526     output( "\t%s 1,0\n",                 /* Major/MinorOperatingSystemVersion */
527              get_asm_short_keyword() );
528     output( "\t%s 0,0\n",                 /* Major/MinorImageVersion */
529              get_asm_short_keyword() );
530     output( "\t%s %u,%u\n",               /* Major/MinorSubsystemVersion */
531              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
532     output( "\t.long 0\n" );                          /* Win32VersionValue */
533     output( "\t.long %s-.L__wine_spec_rva_base\n",    /* SizeOfImage */
534              asm_name("_end") );
535     output( "\t.long %u\n", page_size );  /* SizeOfHeaders */
536     output( "\t.long 0\n" );              /* CheckSum */
537     output( "\t%s 0x%04x\n",              /* Subsystem */
538              get_asm_short_keyword(), spec->subsystem );
539     output( "\t%s 0x%04x\n",              /* DllCharacteristics */
540             get_asm_short_keyword(), spec->dll_characteristics );
541     output( "\t%s %u,%u\n",               /* SizeOfStackReserve/Commit */
542              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
543     output( "\t%s %u,%u\n",               /* SizeOfHeapReserve/Commit */
544              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
545     output( "\t.long 0\n" );              /* LoaderFlags */
546     output( "\t.long 16\n" );             /* NumberOfRvaAndSizes */
547
548     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
549         output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
550                  ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
551     else
552         output( "\t.long 0,0\n" );
553
554     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
555         output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
556                  ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
557     else
558         output( "\t.long 0,0\n" );
559
560     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
561         output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
562                  ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
563     else
564         output( "\t.long 0,0\n" );
565
566     output( "\t.long 0,0\n" );  /* DataDirectory[3] */
567     output( "\t.long 0,0\n" );  /* DataDirectory[4] */
568     output( "\t.long 0,0\n" );  /* DataDirectory[5] */
569     output( "\t.long 0,0\n" );  /* DataDirectory[6] */
570     output( "\t.long 0,0\n" );  /* DataDirectory[7] */
571     output( "\t.long 0,0\n" );  /* DataDirectory[8] */
572     output( "\t.long 0,0\n" );  /* DataDirectory[9] */
573     output( "\t.long 0,0\n" );  /* DataDirectory[10] */
574     output( "\t.long 0,0\n" );  /* DataDirectory[11] */
575     output( "\t.long 0,0\n" );  /* DataDirectory[12] */
576     output( "\t.long 0,0\n" );  /* DataDirectory[13] */
577     output( "\t.long 0,0\n" );  /* DataDirectory[14] */
578     output( "\t.long 0,0\n" );  /* DataDirectory[15] */
579
580     output( "\n\t%s\n", get_asm_string_section() );
581     output( "%s\n", asm_globl("__wine_spec_file_name") );
582     output( ".L__wine_spec_file_name:\n" );
583     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
584     if (target_platform == PLATFORM_APPLE)
585         output( "\t.lcomm %s,4\n", asm_name("_end") );
586
587     output_asm_constructor( "__wine_spec_init_ctor" );
588 }
589
590
591 /*******************************************************************
592  *         BuildSpec32File
593  *
594  * Build a Win32 C file from a spec file.
595  */
596 void BuildSpec32File( DLLSPEC *spec )
597 {
598     resolve_imports( spec );
599     output_standard_file_header();
600     output_module( spec );
601     output_stubs( spec );
602     output_exports( spec );
603     output_imports( spec );
604     if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
605     output_resources( spec );
606     output_gnu_stack_note();
607 }
608
609
610 /*******************************************************************
611  *         output_fake_module
612  *
613  * Build a fake binary module from a spec file.
614  */
615 void output_fake_module( DLLSPEC *spec )
616 {
617     static const unsigned char dll_code_section[] = { 0x31, 0xc0,          /* xor %eax,%eax */
618                                                       0xc2, 0x0c, 0x00 };  /* ret $12 */
619
620     static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00,  /* movl $1,%eax */
621                                                       0xc2, 0x04, 0x00 };            /* ret $4 */
622
623     static const char fakedll_signature[] = "Wine placeholder DLL";
624     const unsigned int page_size = get_page_size();
625     const unsigned int section_align = page_size;
626     const unsigned int file_align = 0x200;
627     const unsigned int reloc_size = 8;
628     const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
629     const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
630     const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
631                                     sizeof(dll_code_section) : sizeof(exe_code_section);
632     unsigned char *resources;
633     unsigned int resources_size;
634     unsigned int image_size = 3 * section_align;
635
636     resolve_imports( spec );
637     output_bin_resources( spec, 3 * section_align );
638     resources = output_buffer;
639     resources_size = output_buffer_pos;
640     if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
641
642     init_output_buffer();
643
644     put_word( 0x5a4d );       /* e_magic */
645     put_word( 0x40 );         /* e_cblp */
646     put_word( 0x01 );         /* e_cp */
647     put_word( 0 );            /* e_crlc */
648     put_word( lfanew / 16 );  /* e_cparhdr */
649     put_word( 0x0000 );       /* e_minalloc */
650     put_word( 0xffff );       /* e_maxalloc */
651     put_word( 0x0000 );       /* e_ss */
652     put_word( 0x00b8 );       /* e_sp */
653     put_word( 0 );            /* e_csum */
654     put_word( 0 );            /* e_ip */
655     put_word( 0 );            /* e_cs */
656     put_word( lfanew );       /* e_lfarlc */
657     put_word( 0 );            /* e_ovno */
658     put_dword( 0 );           /* e_res */
659     put_dword( 0 );
660     put_word( 0 );            /* e_oemid */
661     put_word( 0 );            /* e_oeminfo */
662     put_dword( 0 );           /* e_res2 */
663     put_dword( 0 );
664     put_dword( 0 );
665     put_dword( 0 );
666     put_dword( 0 );
667     put_dword( lfanew );
668
669     put_data( fakedll_signature, sizeof(fakedll_signature) );
670     align_output( 16 );
671
672     put_dword( 0x4550 );                             /* Signature */
673     switch(target_cpu)
674     {
675     case CPU_x86:     put_word( IMAGE_FILE_MACHINE_I386 ); break;
676     case CPU_x86_64:  put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
677     case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
678     case CPU_SPARC:   put_word( IMAGE_FILE_MACHINE_SPARC ); break;
679     case CPU_ARM:     put_word( IMAGE_FILE_MACHINE_ARMV7 ); break;
680     }
681     put_word( nb_sections );                         /* NumberOfSections */
682     put_dword( 0 );                                  /* TimeDateStamp */
683     put_dword( 0 );                                  /* PointerToSymbolTable */
684     put_dword( 0 );                                  /* NumberOfSymbols */
685     put_word( get_ptr_size() == 8 ?
686               IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
687               IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );   /* SizeOfOptionalHeader */
688     put_word( spec->characteristics );               /* Characteristics */
689     put_word( get_ptr_size() == 8 ?
690               IMAGE_NT_OPTIONAL_HDR64_MAGIC :
691               IMAGE_NT_OPTIONAL_HDR32_MAGIC );       /* Magic */
692     put_byte(  0 );                                  /* MajorLinkerVersion */
693     put_byte(  0 );                                  /* MinorLinkerVersion */
694     put_dword( text_size );                          /* SizeOfCode */
695     put_dword( 0 );                                  /* SizeOfInitializedData */
696     put_dword( 0 );                                  /* SizeOfUninitializedData */
697     put_dword( section_align );                      /* AddressOfEntryPoint */
698     put_dword( section_align );                      /* BaseOfCode */
699     if (get_ptr_size() == 4) put_dword( 0 );         /* BaseOfData */
700     put_pword( 0x10000000 );                         /* ImageBase */
701     put_dword( section_align );                      /* SectionAlignment */
702     put_dword( file_align );                         /* FileAlignment */
703     put_word( 1 );                                   /* MajorOperatingSystemVersion */
704     put_word( 0 );                                   /* MinorOperatingSystemVersion */
705     put_word( 0 );                                   /* MajorImageVersion */
706     put_word( 0 );                                   /* MinorImageVersion */
707     put_word( spec->subsystem_major );               /* MajorSubsystemVersion */
708     put_word( spec->subsystem_minor );               /* MinorSubsystemVersion */
709     put_dword( 0 );                                  /* Win32VersionValue */
710     put_dword( image_size );                         /* SizeOfImage */
711     put_dword( file_align );                         /* SizeOfHeaders */
712     put_dword( 0 );                                  /* CheckSum */
713     put_word( spec->subsystem );                     /* Subsystem */
714     put_word( spec->dll_characteristics );           /* DllCharacteristics */
715     put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
716     put_pword( page_size );                          /* SizeOfStackCommit */
717     put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 );   /* SizeOfHeapReserve */
718     put_pword( page_size );                          /* SizeOfHeapCommit */
719     put_dword( 0 );                                  /* LoaderFlags */
720     put_dword( 16 );                                 /* NumberOfRvaAndSizes */
721
722     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
723     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
724     if (resources_size)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
725     {
726         put_dword( 3 * section_align );
727         put_dword( resources_size );
728     }
729     else
730     {
731         put_dword( 0 );
732         put_dword( 0 );
733     }
734
735     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
736     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
737     put_dword( 2 * section_align );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
738     put_dword( reloc_size );
739     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
740     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
741     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
742     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
743     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
744     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
745     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
746     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
747     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
748     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[15] */
749
750     /* .text section */
751     put_data( ".text\0\0", 8 );    /* Name */
752     put_dword( section_align );    /* VirtualSize */
753     put_dword( section_align );    /* VirtualAddress */
754     put_dword( text_size );        /* SizeOfRawData */
755     put_dword( file_align );       /* PointerToRawData */
756     put_dword( 0 );                /* PointerToRelocations */
757     put_dword( 0 );                /* PointerToLinenumbers */
758     put_word( 0 );                 /* NumberOfRelocations */
759     put_word( 0 );                 /* NumberOfLinenumbers */
760     put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics  */
761
762     /* .reloc section */
763     put_data( ".reloc\0", 8 );     /* Name */
764     put_dword( section_align );    /* VirtualSize */
765     put_dword( 2 * section_align );/* VirtualAddress */
766     put_dword( reloc_size );       /* SizeOfRawData */
767     put_dword( 2 * file_align );   /* PointerToRawData */
768     put_dword( 0 );                /* PointerToRelocations */
769     put_dword( 0 );                /* PointerToLinenumbers */
770     put_word( 0 );                 /* NumberOfRelocations */
771     put_word( 0 );                 /* NumberOfLinenumbers */
772     put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
773
774     /* .rsrc section */
775     if (resources_size)
776     {
777         put_data( ".rsrc\0\0", 8 );    /* Name */
778         put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
779         put_dword( 3 * section_align );/* VirtualAddress */
780         put_dword( resources_size );   /* SizeOfRawData */
781         put_dword( 3 * file_align );   /* PointerToRawData */
782         put_dword( 0 );                /* PointerToRelocations */
783         put_dword( 0 );                /* PointerToLinenumbers */
784         put_word( 0 );                 /* NumberOfRelocations */
785         put_word( 0 );                 /* NumberOfLinenumbers */
786         put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
787     }
788
789     /* .text contents */
790     align_output( file_align );
791     if (spec->characteristics & IMAGE_FILE_DLL)
792         put_data( dll_code_section, sizeof(dll_code_section) );
793     else
794         put_data( exe_code_section, sizeof(exe_code_section) );
795
796     /* .reloc contents */
797     align_output( file_align );
798     put_dword( 0 );   /* VirtualAddress */
799     put_dword( 0 );   /* SizeOfBlock */
800
801     /* .rsrc contents */
802     if (resources_size)
803     {
804         align_output( file_align );
805         put_data( resources, resources_size );
806     }
807     flush_output_buffer();
808 }
809
810
811 /*******************************************************************
812  *         output_def_file
813  *
814  * Build a Win32 def file from a spec file.
815  */
816 void output_def_file( DLLSPEC *spec, int include_private )
817 {
818     DLLSPEC *spec32 = NULL;
819     const char *name;
820     int i, total;
821
822     if (spec->type == SPEC_WIN16)
823     {
824         spec32 = alloc_dll_spec();
825         add_16bit_exports( spec32, spec );
826         spec = spec32;
827     }
828
829     if (spec_file_name)
830         output( "; File generated automatically from %s; do not edit!\n\n",
831                  spec_file_name );
832     else
833         output( "; File generated automatically; do not edit!\n\n" );
834
835     output( "LIBRARY %s\n\n", spec->file_name);
836     output( "EXPORTS\n");
837
838     /* Output the exports and relay entry points */
839
840     for (i = total = 0; i < spec->nb_entry_points; i++)
841     {
842         const ORDDEF *odp = &spec->entry_points[i];
843         int is_data = 0;
844
845         if (!odp) continue;
846
847         if (odp->name) name = odp->name;
848         else if (odp->export_name) name = odp->export_name;
849         else continue;
850
851         if (!(odp->flags & FLAG_PRIVATE)) total++;
852         else if (!include_private) continue;
853
854         if (odp->type == TYPE_STUB) continue;
855
856         output( "  %s", name );
857
858         switch(odp->type)
859         {
860         case TYPE_EXTERN:
861             is_data = 1;
862             /* fall through */
863         case TYPE_VARARGS:
864         case TYPE_CDECL:
865         case TYPE_THISCALL:
866             /* try to reduce output */
867             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
868                 output( "=%s", odp->link_name );
869             break;
870         case TYPE_STDCALL:
871         {
872             int at_param = get_args_size( odp );
873             if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
874             if  (odp->flags & FLAG_FORWARD)
875             {
876                 output( "=%s", odp->link_name );
877             }
878             else if (strcmp(name, odp->link_name)) /* try to reduce output */
879             {
880                 output( "=%s", odp->link_name );
881                 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
882             }
883             break;
884         }
885         default:
886             assert(0);
887         }
888         output( " @%d", odp->ordinal );
889         if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
890         if (is_data) output( " DATA" );
891         if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
892         output( "\n" );
893     }
894     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
895     if (spec32) free_dll_spec( spec32 );
896 }