makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[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     return 1;
59 }
60
61 static int is_float_arg( const ORDDEF *odp, unsigned int arg )
62 {
63     if (arg >= odp->u.func.nb_args) return 0;
64     return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
65 }
66
67 /* check if dll will output relay thunks */
68 int has_relays( DLLSPEC *spec )
69 {
70     int i;
71
72     if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
73
74     for (i = spec->base; i <= spec->limit; i++)
75     {
76         ORDDEF *odp = spec->ordinals[i];
77         if (needs_relay( odp )) return 1;
78     }
79     return 0;
80 }
81
82 /*******************************************************************
83  *         output_relay_debug
84  *
85  * Output entry points for relay debugging
86  */
87 static void output_relay_debug( DLLSPEC *spec )
88 {
89     int i;
90     unsigned int j, pos, args, flags;
91
92     /* first the table of entry point offsets */
93
94     output( "\t%s\n", get_asm_rodata_section() );
95     output( "\t.align %d\n", get_alignment(4) );
96     output( ".L__wine_spec_relay_entry_point_offsets:\n" );
97
98     for (i = spec->base; i <= spec->limit; i++)
99     {
100         ORDDEF *odp = spec->ordinals[i];
101
102         if (needs_relay( odp ))
103             output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
104         else
105             output( "\t.long 0\n" );
106     }
107
108     /* then the table of argument types */
109
110     output( "\t.align %d\n", get_alignment(4) );
111     output( ".L__wine_spec_relay_arg_types:\n" );
112
113     for (i = spec->base; i <= spec->limit; i++)
114     {
115         ORDDEF *odp = spec->ordinals[i];
116         unsigned int mask = 0;
117
118         if (needs_relay( odp ))
119         {
120             for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
121             {
122                 switch (odp->u.func.args[j])
123                 {
124                 case ARG_STR:    mask |= 1 << (2 * pos++); break;
125                 case ARG_WSTR:   mask |= 2 << (2 * pos++); break;
126                 case ARG_INT64:
127                 case ARG_DOUBLE: pos += 8 / get_ptr_size(); break;
128                 case ARG_INT128: pos += (target_cpu == CPU_x86) ? 4 : 1; break;
129                 default:         pos++; break;
130                 }
131             }
132         }
133         output( "\t.long 0x%08x\n", mask );
134     }
135
136     /* then the relay thunks */
137
138     output( "\t.text\n" );
139     output( "__wine_spec_relay_entry_points:\n" );
140     output( "\tnop\n" );  /* to avoid 0 offset */
141
142     for (i = spec->base; i <= spec->limit; i++)
143     {
144         ORDDEF *odp = spec->ordinals[i];
145
146         if (!needs_relay( odp )) continue;
147
148         output( "\t.align %d\n", get_alignment(4) );
149         output( ".L__wine_spec_relay_entry_point_%d:\n", i );
150
151         args = get_args_size(odp) / get_ptr_size();
152         flags = 0;
153
154         switch (target_cpu)
155         {
156         case CPU_x86:
157             if (odp->type == TYPE_THISCALL)  /* add the this pointer */
158             {
159                 output( "\tpopl %%eax\n" );
160                 output( "\tpushl %%ecx\n" );
161                 output( "\tpushl %%eax\n" );
162                 flags |= 2;
163             }
164             if (odp->flags & FLAG_REGISTER)
165                 output( "\tpushl %%eax\n" );
166             else
167                 output( "\tpushl %%esp\n" );
168
169             if (odp->flags & FLAG_RET64) flags |= 1;
170             output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
171
172             if (UsePIC)
173             {
174                 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
175                 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
176             }
177             else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
178             output( "\tpushl %%eax\n" );
179
180             if (odp->flags & FLAG_REGISTER)
181             {
182                 output( "\tcall *8(%%eax)\n" );
183             }
184             else
185             {
186                 output( "\tcall *4(%%eax)\n" );
187                 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
188                     output( "\tret $%u\n", args * get_ptr_size() );
189                 else
190                     output( "\tret\n" );
191             }
192             break;
193
194         case CPU_x86_64:
195             output( "\t.cfi_startproc\n" );
196             output( "\tsubq $40,%%rsp\n" );
197             output( "\t.cfi_adjust_cfa_offset 40\n" );
198             switch (args)
199             {
200             default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
201             case 3:  output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
202             case 2:  output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
203             case 1:  output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
204             case 0:  break;
205             }
206             output( "\tleaq 40(%%rsp),%%r8\n" );
207             output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
208             output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
209             output( "\tcallq *%u(%%rcx)\n", (odp->flags & FLAG_REGISTER) ? 16 : 8 );
210             output( "\taddq $40,%%rsp\n" );
211             output( "\t.cfi_adjust_cfa_offset -40\n" );
212             output( "\tret\n" );
213             output( "\t.cfi_endproc\n" );
214             break;
215
216         default:
217             assert(0);
218         }
219     }
220 }
221
222 /*******************************************************************
223  *         output_exports
224  *
225  * Output the export table for a Win32 module.
226  */
227 void output_exports( DLLSPEC *spec )
228 {
229     int i, fwd_size = 0;
230     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
231
232     if (!nr_exports) return;
233
234     output( "\n/* export table */\n\n" );
235     output( "\t.data\n" );
236     output( "\t.align %d\n", get_alignment(4) );
237     output( ".L__wine_spec_exports:\n" );
238
239     /* export directory header */
240
241     output( "\t.long 0\n" );                       /* Characteristics */
242     output( "\t.long 0\n" );                       /* TimeDateStamp */
243     output( "\t.long 0\n" );                       /* MajorVersion/MinorVersion */
244     output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
245     output( "\t.long %u\n", spec->base );          /* Base */
246     output( "\t.long %u\n", nr_exports );          /* NumberOfFunctions */
247     output( "\t.long %u\n", spec->nb_names );      /* NumberOfNames */
248     output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
249     if (spec->nb_names)
250     {
251         output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
252         output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" );  /* AddressOfNameOrdinals */
253     }
254     else
255     {
256         output( "\t.long 0\n" );  /* AddressOfNames */
257         output( "\t.long 0\n" );  /* AddressOfNameOrdinals */
258     }
259
260     /* output the function pointers */
261
262     output( "\n.L__wine_spec_exports_funcs:\n" );
263     for (i = spec->base; i <= spec->limit; i++)
264     {
265         ORDDEF *odp = spec->ordinals[i];
266         if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
267         else switch(odp->type)
268         {
269         case TYPE_EXTERN:
270         case TYPE_STDCALL:
271         case TYPE_VARARGS:
272         case TYPE_CDECL:
273         case TYPE_THISCALL:
274             if (odp->flags & FLAG_FORWARD)
275             {
276                 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
277                 fwd_size += strlen(odp->link_name) + 1;
278             }
279             else if (odp->flags & FLAG_EXT_LINK)
280             {
281                 output( "\t%s %s_%s\n",
282                          get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
283             }
284             else
285             {
286                 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
287             }
288             break;
289         case TYPE_STUB:
290             output( "\t%s %s\n", get_asm_ptr_keyword(),
291                      asm_name( get_stub_name( odp, spec )) );
292             break;
293         default:
294             assert(0);
295         }
296     }
297
298     if (spec->nb_names)
299     {
300         /* output the function name pointers */
301
302         int namepos = strlen(spec->file_name) + 1;
303
304         output( "\n.L__wine_spec_exp_name_ptrs:\n" );
305         for (i = 0; i < spec->nb_names; i++)
306         {
307             output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
308             namepos += strlen(spec->names[i]->name) + 1;
309         }
310
311         /* output the function ordinals */
312
313         output( "\n.L__wine_spec_exp_ordinals:\n" );
314         for (i = 0; i < spec->nb_names; i++)
315         {
316             output( "\t%s %d\n",
317                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
318         }
319         if (spec->nb_names % 2)
320         {
321             output( "\t%s 0\n", get_asm_short_keyword() );
322         }
323     }
324
325     /* output the export name strings */
326
327     output( "\n.L__wine_spec_exp_names:\n" );
328     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
329     for (i = 0; i < spec->nb_names; i++)
330         output( "\t%s \"%s\"\n",
331                  get_asm_string_keyword(), spec->names[i]->name );
332
333     /* output forward strings */
334
335     if (fwd_size)
336     {
337         output( "\n.L__wine_spec_forwards:\n" );
338         for (i = spec->base; i <= spec->limit; i++)
339         {
340             ORDDEF *odp = spec->ordinals[i];
341             if (odp && (odp->flags & FLAG_FORWARD))
342                 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
343         }
344     }
345     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
346     output( ".L__wine_spec_exports_end:\n" );
347
348     /* output relays */
349
350     if (!has_relays( spec ))
351     {
352         output( "\t%s 0\n", get_asm_ptr_keyword() );
353         return;
354     }
355
356     output( ".L__wine_spec_relay_descr:\n" );
357     output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() );  /* magic */
358     output( "\t%s 0,0\n", get_asm_ptr_keyword() );         /* relay funcs */
359     output( "\t%s 0\n", get_asm_ptr_keyword() );           /* private data */
360     output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
361     output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
362     output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
363
364     output_relay_debug( spec );
365 }
366
367
368 /*******************************************************************
369  *         output_asm_constructor
370  *
371  * Output code for calling a dll constructor.
372  */
373 static void output_asm_constructor( const char *constructor )
374 {
375     if (target_platform == PLATFORM_APPLE)
376     {
377         /* Mach-O doesn't have an init section */
378         output( "\n\t.mod_init_func\n" );
379         output( "\t.align %d\n", get_alignment(4) );
380         output( "\t.long %s\n", asm_name(constructor) );
381     }
382     else
383     {
384         output( "\n\t.section \".init\",\"ax\"\n" );
385         switch(target_cpu)
386         {
387         case CPU_x86:
388         case CPU_x86_64:
389             output( "\tcall %s\n", asm_name(constructor) );
390             break;
391         case CPU_SPARC:
392             output( "\tcall %s\n", asm_name(constructor) );
393             output( "\tnop\n" );
394             break;
395         case CPU_ALPHA:
396             output( "\tjsr $26,%s\n", asm_name(constructor) );
397             break;
398         case CPU_ARM:
399             output( "\tblx %s\n", asm_name(constructor) );
400             break;
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 }