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