mshtml: Share nsanchor reference with nsnode.
[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         switch(target_cpu)
407         {
408         case CPU_x86:
409         case CPU_x86_64:
410             output( "\n\t.section \".init\",\"ax\"\n" );
411             output( "\tcall %s\n", asm_name(constructor) );
412             break;
413         case CPU_SPARC:
414             output( "\n\t.section \".init\",\"ax\"\n" );
415             output( "\tcall %s\n", asm_name(constructor) );
416             output( "\tnop\n" );
417             break;
418         case CPU_ARM:
419             output( "\n\t.section \".text\",\"ax\"\n" );
420             output( "\tblx %s\n", asm_name(constructor) );
421             break;
422         case CPU_POWERPC:
423             output( "\n\t.section \".init\",\"ax\"\n" );
424             output( "\tbl %s\n", asm_name(constructor) );
425             break;
426         }
427     }
428 }
429
430
431 /*******************************************************************
432  *         output_module
433  *
434  * Output the module data.
435  */
436 void output_module( DLLSPEC *spec )
437 {
438     int machine = 0;
439     unsigned int page_size = get_page_size();
440
441     /* Reserve some space for the PE header */
442
443     switch (target_platform)
444     {
445     case PLATFORM_APPLE:
446         output( "\t.text\n" );
447         output( "\t.align %d\n", get_alignment(page_size) );
448         output( "__wine_spec_pe_header:\n" );
449         output( "\t.space 65536\n" );
450         break;
451     case PLATFORM_SOLARIS:
452         output( "\n\t.section \".text\",\"ax\"\n" );
453         output( "__wine_spec_pe_header:\n" );
454         output( "\t.skip %u\n", 65536 + page_size );
455         break;
456     default:
457         switch(target_cpu)
458         {
459         case CPU_x86:
460         case CPU_x86_64:
461         case CPU_SPARC:
462             output( "\n\t.section \".init\",\"ax\"\n" );
463             output( "\tjmp 1f\n" );
464             break;
465         case CPU_ARM:
466             output( "\n\t.section \".text\",\"ax\"\n" );
467             output( "\tb 1f\n" );
468             break;
469         case CPU_POWERPC:
470             output( "\n\t.section \".init\",\"ax\"\n" );
471             output( "\tb 1f\n" );
472             break;
473         }
474         output( "__wine_spec_pe_header:\n" );
475         output( "\t.skip %u\n", 65536 + page_size );
476         output( "1:\n" );
477         break;
478     }
479
480     /* Output the NT header */
481
482     output( "\n\t.data\n" );
483     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
484     output( "%s\n", asm_globl("__wine_spec_nt_header") );
485     output( ".L__wine_spec_rva_base:\n" );
486
487     output( "\t.long 0x4550\n" );         /* Signature */
488     switch(target_cpu)
489     {
490     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
491     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
492     case CPU_ARM:     machine = IMAGE_FILE_MACHINE_ARMV7; break;
493     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
494     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_SPARC; break;
495     }
496     output( "\t%s 0x%04x\n",              /* Machine */
497              get_asm_short_keyword(), machine );
498     output( "\t%s 0\n",                   /* NumberOfSections */
499              get_asm_short_keyword() );
500     output( "\t.long 0\n" );              /* TimeDateStamp */
501     output( "\t.long 0\n" );              /* PointerToSymbolTable */
502     output( "\t.long 0\n" );              /* NumberOfSymbols */
503     output( "\t%s %d\n",                  /* SizeOfOptionalHeader */
504              get_asm_short_keyword(),
505              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
506     output( "\t%s 0x%04x\n",              /* Characteristics */
507              get_asm_short_keyword(), spec->characteristics );
508     output( "\t%s 0x%04x\n",              /* Magic */
509              get_asm_short_keyword(),
510              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
511     output( "\t.byte 0\n" );              /* MajorLinkerVersion */
512     output( "\t.byte 0\n" );              /* MinorLinkerVersion */
513     output( "\t.long 0\n" );              /* SizeOfCode */
514     output( "\t.long 0\n" );              /* SizeOfInitializedData */
515     output( "\t.long 0\n" );              /* SizeOfUninitializedData */
516     /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
517     output( "\t%s %s\n",                  /* AddressOfEntryPoint */
518             get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
519     if (get_ptr_size() == 4)
520     {
521         output( "\t.long 0\n" );          /* BaseOfCode */
522         output( "\t.long 0\n" );          /* BaseOfData */
523     }
524     output( "\t%s __wine_spec_pe_header\n",         /* ImageBase */
525              get_asm_ptr_keyword() );
526     output( "\t.long %u\n", page_size );  /* SectionAlignment */
527     output( "\t.long %u\n", page_size );  /* FileAlignment */
528     output( "\t%s 1,0\n",                 /* Major/MinorOperatingSystemVersion */
529              get_asm_short_keyword() );
530     output( "\t%s 0,0\n",                 /* Major/MinorImageVersion */
531              get_asm_short_keyword() );
532     output( "\t%s %u,%u\n",               /* Major/MinorSubsystemVersion */
533              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
534     output( "\t.long 0\n" );                          /* Win32VersionValue */
535     output( "\t.long %s-.L__wine_spec_rva_base\n",    /* SizeOfImage */
536              asm_name("_end") );
537     output( "\t.long %u\n", page_size );  /* SizeOfHeaders */
538     output( "\t.long 0\n" );              /* CheckSum */
539     output( "\t%s 0x%04x\n",              /* Subsystem */
540              get_asm_short_keyword(), spec->subsystem );
541     output( "\t%s 0x%04x\n",              /* DllCharacteristics */
542             get_asm_short_keyword(), spec->dll_characteristics );
543     output( "\t%s %u,%u\n",               /* SizeOfStackReserve/Commit */
544              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
545     output( "\t%s %u,%u\n",               /* SizeOfHeapReserve/Commit */
546              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
547     output( "\t.long 0\n" );              /* LoaderFlags */
548     output( "\t.long 16\n" );             /* NumberOfRvaAndSizes */
549
550     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
551         output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
552                  ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
553     else
554         output( "\t.long 0,0\n" );
555
556     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
557         output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
558                  ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
559     else
560         output( "\t.long 0,0\n" );
561
562     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
563         output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
564                  ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
565     else
566         output( "\t.long 0,0\n" );
567
568     output( "\t.long 0,0\n" );  /* DataDirectory[3] */
569     output( "\t.long 0,0\n" );  /* DataDirectory[4] */
570     output( "\t.long 0,0\n" );  /* DataDirectory[5] */
571     output( "\t.long 0,0\n" );  /* DataDirectory[6] */
572     output( "\t.long 0,0\n" );  /* DataDirectory[7] */
573     output( "\t.long 0,0\n" );  /* DataDirectory[8] */
574     output( "\t.long 0,0\n" );  /* DataDirectory[9] */
575     output( "\t.long 0,0\n" );  /* DataDirectory[10] */
576     output( "\t.long 0,0\n" );  /* DataDirectory[11] */
577     output( "\t.long 0,0\n" );  /* DataDirectory[12] */
578     output( "\t.long 0,0\n" );  /* DataDirectory[13] */
579     output( "\t.long 0,0\n" );  /* DataDirectory[14] */
580     output( "\t.long 0,0\n" );  /* DataDirectory[15] */
581
582     output( "\n\t%s\n", get_asm_string_section() );
583     output( "%s\n", asm_globl("__wine_spec_file_name") );
584     output( ".L__wine_spec_file_name:\n" );
585     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
586     if (target_platform == PLATFORM_APPLE)
587         output( "\t.lcomm %s,4\n", asm_name("_end") );
588
589     output_asm_constructor( "__wine_spec_init_ctor" );
590 }
591
592
593 /*******************************************************************
594  *         BuildSpec32File
595  *
596  * Build a Win32 C file from a spec file.
597  */
598 void BuildSpec32File( DLLSPEC *spec )
599 {
600     resolve_imports( spec );
601     output_standard_file_header();
602     output_module( spec );
603     output_stubs( spec );
604     output_exports( spec );
605     output_imports( spec );
606     if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
607     output_resources( spec );
608     output_gnu_stack_note();
609 }
610
611
612 /*******************************************************************
613  *         output_fake_module
614  *
615  * Build a fake binary module from a spec file.
616  */
617 void output_fake_module( DLLSPEC *spec )
618 {
619     static const unsigned char dll_code_section[] = { 0x31, 0xc0,          /* xor %eax,%eax */
620                                                       0xc2, 0x0c, 0x00 };  /* ret $12 */
621
622     static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00,  /* movl $1,%eax */
623                                                       0xc2, 0x04, 0x00 };            /* ret $4 */
624
625     static const char fakedll_signature[] = "Wine placeholder DLL";
626     const unsigned int page_size = get_page_size();
627     const unsigned int section_align = page_size;
628     const unsigned int file_align = 0x200;
629     const unsigned int reloc_size = 8;
630     const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
631     const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
632     const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
633                                     sizeof(dll_code_section) : sizeof(exe_code_section);
634     unsigned char *resources;
635     unsigned int resources_size;
636     unsigned int image_size = 3 * section_align;
637
638     resolve_imports( spec );
639     output_bin_resources( spec, 3 * section_align );
640     resources = output_buffer;
641     resources_size = output_buffer_pos;
642     if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
643
644     init_output_buffer();
645
646     put_word( 0x5a4d );       /* e_magic */
647     put_word( 0x40 );         /* e_cblp */
648     put_word( 0x01 );         /* e_cp */
649     put_word( 0 );            /* e_crlc */
650     put_word( lfanew / 16 );  /* e_cparhdr */
651     put_word( 0x0000 );       /* e_minalloc */
652     put_word( 0xffff );       /* e_maxalloc */
653     put_word( 0x0000 );       /* e_ss */
654     put_word( 0x00b8 );       /* e_sp */
655     put_word( 0 );            /* e_csum */
656     put_word( 0 );            /* e_ip */
657     put_word( 0 );            /* e_cs */
658     put_word( lfanew );       /* e_lfarlc */
659     put_word( 0 );            /* e_ovno */
660     put_dword( 0 );           /* e_res */
661     put_dword( 0 );
662     put_word( 0 );            /* e_oemid */
663     put_word( 0 );            /* e_oeminfo */
664     put_dword( 0 );           /* e_res2 */
665     put_dword( 0 );
666     put_dword( 0 );
667     put_dword( 0 );
668     put_dword( 0 );
669     put_dword( lfanew );
670
671     put_data( fakedll_signature, sizeof(fakedll_signature) );
672     align_output( 16 );
673
674     put_dword( 0x4550 );                             /* Signature */
675     switch(target_cpu)
676     {
677     case CPU_x86:     put_word( IMAGE_FILE_MACHINE_I386 ); break;
678     case CPU_x86_64:  put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
679     case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
680     case CPU_SPARC:   put_word( IMAGE_FILE_MACHINE_SPARC ); break;
681     case CPU_ARM:     put_word( IMAGE_FILE_MACHINE_ARMV7 ); break;
682     }
683     put_word( nb_sections );                         /* NumberOfSections */
684     put_dword( 0 );                                  /* TimeDateStamp */
685     put_dword( 0 );                                  /* PointerToSymbolTable */
686     put_dword( 0 );                                  /* NumberOfSymbols */
687     put_word( get_ptr_size() == 8 ?
688               IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
689               IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );   /* SizeOfOptionalHeader */
690     put_word( spec->characteristics );               /* Characteristics */
691     put_word( get_ptr_size() == 8 ?
692               IMAGE_NT_OPTIONAL_HDR64_MAGIC :
693               IMAGE_NT_OPTIONAL_HDR32_MAGIC );       /* Magic */
694     put_byte(  0 );                                  /* MajorLinkerVersion */
695     put_byte(  0 );                                  /* MinorLinkerVersion */
696     put_dword( text_size );                          /* SizeOfCode */
697     put_dword( 0 );                                  /* SizeOfInitializedData */
698     put_dword( 0 );                                  /* SizeOfUninitializedData */
699     put_dword( section_align );                      /* AddressOfEntryPoint */
700     put_dword( section_align );                      /* BaseOfCode */
701     if (get_ptr_size() == 4) put_dword( 0 );         /* BaseOfData */
702     put_pword( 0x10000000 );                         /* ImageBase */
703     put_dword( section_align );                      /* SectionAlignment */
704     put_dword( file_align );                         /* FileAlignment */
705     put_word( 1 );                                   /* MajorOperatingSystemVersion */
706     put_word( 0 );                                   /* MinorOperatingSystemVersion */
707     put_word( 0 );                                   /* MajorImageVersion */
708     put_word( 0 );                                   /* MinorImageVersion */
709     put_word( spec->subsystem_major );               /* MajorSubsystemVersion */
710     put_word( spec->subsystem_minor );               /* MinorSubsystemVersion */
711     put_dword( 0 );                                  /* Win32VersionValue */
712     put_dword( image_size );                         /* SizeOfImage */
713     put_dword( file_align );                         /* SizeOfHeaders */
714     put_dword( 0 );                                  /* CheckSum */
715     put_word( spec->subsystem );                     /* Subsystem */
716     put_word( spec->dll_characteristics );           /* DllCharacteristics */
717     put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
718     put_pword( page_size );                          /* SizeOfStackCommit */
719     put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 );   /* SizeOfHeapReserve */
720     put_pword( page_size );                          /* SizeOfHeapCommit */
721     put_dword( 0 );                                  /* LoaderFlags */
722     put_dword( 16 );                                 /* NumberOfRvaAndSizes */
723
724     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
725     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
726     if (resources_size)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
727     {
728         put_dword( 3 * section_align );
729         put_dword( resources_size );
730     }
731     else
732     {
733         put_dword( 0 );
734         put_dword( 0 );
735     }
736
737     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
738     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
739     put_dword( 2 * section_align );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
740     put_dword( reloc_size );
741     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
742     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
743     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
744     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
745     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
746     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
747     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
748     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
749     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
750     put_dword( 0 ); put_dword( 0 );   /* DataDirectory[15] */
751
752     /* .text section */
753     put_data( ".text\0\0", 8 );    /* Name */
754     put_dword( section_align );    /* VirtualSize */
755     put_dword( section_align );    /* VirtualAddress */
756     put_dword( text_size );        /* SizeOfRawData */
757     put_dword( file_align );       /* PointerToRawData */
758     put_dword( 0 );                /* PointerToRelocations */
759     put_dword( 0 );                /* PointerToLinenumbers */
760     put_word( 0 );                 /* NumberOfRelocations */
761     put_word( 0 );                 /* NumberOfLinenumbers */
762     put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics  */
763
764     /* .reloc section */
765     put_data( ".reloc\0", 8 );     /* Name */
766     put_dword( section_align );    /* VirtualSize */
767     put_dword( 2 * section_align );/* VirtualAddress */
768     put_dword( reloc_size );       /* SizeOfRawData */
769     put_dword( 2 * file_align );   /* PointerToRawData */
770     put_dword( 0 );                /* PointerToRelocations */
771     put_dword( 0 );                /* PointerToLinenumbers */
772     put_word( 0 );                 /* NumberOfRelocations */
773     put_word( 0 );                 /* NumberOfLinenumbers */
774     put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
775
776     /* .rsrc section */
777     if (resources_size)
778     {
779         put_data( ".rsrc\0\0", 8 );    /* Name */
780         put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
781         put_dword( 3 * section_align );/* VirtualAddress */
782         put_dword( resources_size );   /* SizeOfRawData */
783         put_dword( 3 * file_align );   /* PointerToRawData */
784         put_dword( 0 );                /* PointerToRelocations */
785         put_dword( 0 );                /* PointerToLinenumbers */
786         put_word( 0 );                 /* NumberOfRelocations */
787         put_word( 0 );                 /* NumberOfLinenumbers */
788         put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
789     }
790
791     /* .text contents */
792     align_output( file_align );
793     if (spec->characteristics & IMAGE_FILE_DLL)
794         put_data( dll_code_section, sizeof(dll_code_section) );
795     else
796         put_data( exe_code_section, sizeof(exe_code_section) );
797
798     /* .reloc contents */
799     align_output( file_align );
800     put_dword( 0 );   /* VirtualAddress */
801     put_dword( 0 );   /* SizeOfBlock */
802
803     /* .rsrc contents */
804     if (resources_size)
805     {
806         align_output( file_align );
807         put_data( resources, resources_size );
808     }
809     flush_output_buffer();
810 }
811
812
813 /*******************************************************************
814  *         output_def_file
815  *
816  * Build a Win32 def file from a spec file.
817  */
818 void output_def_file( DLLSPEC *spec, int include_private )
819 {
820     DLLSPEC *spec32 = NULL;
821     const char *name;
822     int i, total;
823
824     if (spec->type == SPEC_WIN16)
825     {
826         spec32 = alloc_dll_spec();
827         add_16bit_exports( spec32, spec );
828         spec = spec32;
829     }
830
831     if (spec_file_name)
832         output( "; File generated automatically from %s; do not edit!\n\n",
833                  spec_file_name );
834     else
835         output( "; File generated automatically; do not edit!\n\n" );
836
837     output( "LIBRARY %s\n\n", spec->file_name);
838     output( "EXPORTS\n");
839
840     /* Output the exports and relay entry points */
841
842     for (i = total = 0; i < spec->nb_entry_points; i++)
843     {
844         const ORDDEF *odp = &spec->entry_points[i];
845         int is_data = 0;
846
847         if (!odp) continue;
848
849         if (odp->name) name = odp->name;
850         else if (odp->export_name) name = odp->export_name;
851         else continue;
852
853         if (!(odp->flags & FLAG_PRIVATE)) total++;
854         else if (!include_private) continue;
855
856         if (odp->type == TYPE_STUB) continue;
857
858         output( "  %s", name );
859
860         switch(odp->type)
861         {
862         case TYPE_EXTERN:
863             is_data = 1;
864             /* fall through */
865         case TYPE_VARARGS:
866         case TYPE_CDECL:
867         case TYPE_THISCALL:
868             /* try to reduce output */
869             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
870                 output( "=%s", odp->link_name );
871             break;
872         case TYPE_STDCALL:
873         {
874             int at_param = get_args_size( odp );
875             if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
876             if  (odp->flags & FLAG_FORWARD)
877             {
878                 output( "=%s", odp->link_name );
879             }
880             else if (strcmp(name, odp->link_name)) /* try to reduce output */
881             {
882                 output( "=%s", odp->link_name );
883                 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
884             }
885             break;
886         }
887         default:
888             assert(0);
889         }
890         output( " @%d", odp->ordinal );
891         if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
892         if (is_data) output( " DATA" );
893         if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
894         output( "\n" );
895     }
896     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
897     if (spec32) free_dll_spec( spec32 );
898 }