po: Remove English strings from the Turkish translation.
[wine/multimedia.git] / tools / winebuild / spec32.c
blob2cfeb99add82b84672565c0910ea8f6c462b2c89
1 /*
2 * 32-bit spec files
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
33 #include "build.h"
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_ARM 0x01C0
40 /* Wine extension */
41 #define IMAGE_FILE_MACHINE_SPARC 0x2000
43 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
44 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
46 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
47 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
48 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
50 /* check if entry point needs a relay thunk */
51 static inline int needs_relay( const ORDDEF *odp )
53 /* skip nonexistent entry points */
54 if (!odp) return 0;
55 /* skip non-functions */
56 switch (odp->type)
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;
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;
75 static int is_float_arg( const ORDDEF *odp, int arg )
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);
81 /* check if dll will output relay thunks */
82 int has_relays( DLLSPEC *spec )
84 int i;
86 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
88 for (i = spec->base; i <= spec->limit; i++)
90 ORDDEF *odp = spec->ordinals[i];
91 if (needs_relay( odp )) return 1;
93 return 0;
96 /*******************************************************************
97 * output_relay_debug
99 * Output entry points for relay debugging
101 static void output_relay_debug( DLLSPEC *spec )
103 int i, j;
104 unsigned int pos, args, flags;
106 /* first the table of entry point offsets */
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" );
112 for (i = spec->base; i <= spec->limit; i++)
114 ORDDEF *odp = spec->ordinals[i];
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" );
122 /* then the table of argument types */
124 output( "\t.align %d\n", get_alignment(4) );
125 output( ".L__wine_spec_relay_arg_types:\n" );
127 for (i = spec->base; i <= spec->limit; i++)
129 ORDDEF *odp = spec->ordinals[i];
130 unsigned int mask = 0;
132 if (needs_relay( odp ))
134 for (j = pos = 0; pos < 16 && j < odp->u.func.nb_args; j++)
136 switch (odp->u.func.args[j])
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;
147 output( "\t.long 0x%08x\n", mask );
150 /* then the relay thunks */
152 output( "\t.text\n" );
153 output( "__wine_spec_relay_entry_points:\n" );
154 output( "\tnop\n" ); /* to avoid 0 offset */
156 for (i = spec->base; i <= spec->limit; i++)
158 ORDDEF *odp = spec->ordinals[i];
160 if (!needs_relay( odp )) continue;
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" );
166 args = get_args_size(odp) / get_ptr_size();
167 flags = 0;
169 switch (target_cpu)
171 case CPU_x86:
172 if (odp->type == TYPE_THISCALL) /* add the this pointer */
174 output( "\tpopl %%eax\n" );
175 output( "\tpushl %%ecx\n" );
176 output( "\tpushl %%eax\n" );
177 flags |= 2;
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" );
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" );
189 if (UsePIC)
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" );
194 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
195 output( "\tpushl %%eax\n" );
196 output_cfi( ".cfi_adjust_cfa_offset 4" );
198 if (odp->flags & FLAG_REGISTER)
200 output( "\tcall *8(%%eax)\n" );
202 else
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" );
211 break;
213 case CPU_x86_64:
214 output( "\tsubq $40,%%rsp\n" );
215 output_cfi( ".cfi_adjust_cfa_offset 40" );
216 switch (args)
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;
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;
237 default:
238 assert(0);
240 output_cfi( ".cfi_endproc" );
244 /*******************************************************************
245 * output_exports
247 * Output the export table for a Win32 module.
249 void output_exports( DLLSPEC *spec )
251 int i, fwd_size = 0;
252 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
254 if (!nr_exports) return;
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" );
261 /* export directory header */
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)
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 */
276 else
278 output( "\t.long 0\n" ); /* AddressOfNames */
279 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
282 /* output the function pointers */
284 output( "\n.L__wine_spec_exports_funcs:\n" );
285 for (i = spec->base; i <= spec->limit; i++)
287 ORDDEF *odp = spec->ordinals[i];
288 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
289 else switch(odp->type)
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)
298 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
299 fwd_size += strlen(odp->link_name) + 1;
301 else if (odp->flags & FLAG_EXT_LINK)
303 output( "\t%s %s_%s\n",
304 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
306 else
308 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
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);
320 if (spec->nb_names)
322 /* output the function name pointers */
324 int namepos = strlen(spec->file_name) + 1;
326 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
327 for (i = 0; i < spec->nb_names; i++)
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;
333 /* output the function ordinals */
335 output( "\n.L__wine_spec_exp_ordinals:\n" );
336 for (i = 0; i < spec->nb_names; i++)
338 output( "\t%s %d\n",
339 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
341 if (spec->nb_names % 2)
343 output( "\t%s 0\n", get_asm_short_keyword() );
347 /* output the export name strings */
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 );
355 /* output forward strings */
357 if (fwd_size)
359 output( "\n.L__wine_spec_forwards:\n" );
360 for (i = spec->base; i <= spec->limit; i++)
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 );
367 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
368 output( ".L__wine_spec_exports_end:\n" );
370 /* output relays */
372 if (!has_relays( spec ))
374 output( "\t%s 0\n", get_asm_ptr_keyword() );
375 return;
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() );
386 output_relay_debug( spec );
390 /*******************************************************************
391 * output_asm_constructor
393 * Output code for calling a dll constructor.
395 static void output_asm_constructor( const char *constructor )
397 if (target_platform == PLATFORM_APPLE)
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) );
404 else
406 output( "\n\t.section \".init\",\"ax\"\n" );
407 switch(target_cpu)
409 case CPU_x86:
410 case CPU_x86_64:
411 output( "\tcall %s\n", asm_name(constructor) );
412 break;
413 case CPU_SPARC:
414 output( "\tcall %s\n", asm_name(constructor) );
415 output( "\tnop\n" );
416 break;
417 case CPU_ARM:
418 case CPU_POWERPC:
419 output( "\tbl %s\n", asm_name(constructor) );
420 break;
426 /*******************************************************************
427 * output_module
429 * Output the module data.
431 void output_module( DLLSPEC *spec )
433 int machine = 0;
434 unsigned int page_size = get_page_size();
436 /* Reserve some space for the PE header */
438 switch (target_platform)
440 case PLATFORM_APPLE:
441 output( "\t.text\n" );
442 output( "\t.align %d\n", get_alignment(page_size) );
443 output( "__wine_spec_pe_header:\n" );
444 output( "\t.space 65536\n" );
445 break;
446 case PLATFORM_SOLARIS:
447 output( "\n\t.section \".text\",\"ax\"\n" );
448 output( "__wine_spec_pe_header:\n" );
449 output( "\t.skip %u\n", 65536 + page_size );
450 break;
451 default:
452 output( "\n\t.section \".init\",\"ax\"\n" );
453 switch(target_cpu)
455 case CPU_x86:
456 case CPU_x86_64:
457 case CPU_SPARC:
458 output( "\tjmp 1f\n" );
459 break;
460 case CPU_ARM:
461 case CPU_POWERPC:
462 output( "\tb 1f\n" );
463 break;
465 output( "__wine_spec_pe_header:\n" );
466 output( "\t.skip %u\n", 65536 + page_size );
467 output( "1:\n" );
468 break;
471 /* Output the NT header */
473 output( "\n\t.data\n" );
474 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
475 output( "%s\n", asm_globl("__wine_spec_nt_header") );
476 output( ".L__wine_spec_rva_base:\n" );
478 output( "\t.long 0x4550\n" ); /* Signature */
479 switch(target_cpu)
481 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
482 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
483 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARM; break;
484 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
485 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_SPARC; break;
487 output( "\t%s 0x%04x\n", /* Machine */
488 get_asm_short_keyword(), machine );
489 output( "\t%s 0\n", /* NumberOfSections */
490 get_asm_short_keyword() );
491 output( "\t.long 0\n" ); /* TimeDateStamp */
492 output( "\t.long 0\n" ); /* PointerToSymbolTable */
493 output( "\t.long 0\n" ); /* NumberOfSymbols */
494 output( "\t%s %d\n", /* SizeOfOptionalHeader */
495 get_asm_short_keyword(),
496 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
497 output( "\t%s 0x%04x\n", /* Characteristics */
498 get_asm_short_keyword(), spec->characteristics );
499 output( "\t%s 0x%04x\n", /* Magic */
500 get_asm_short_keyword(),
501 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
502 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
503 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
504 output( "\t.long 0\n" ); /* SizeOfCode */
505 output( "\t.long 0\n" ); /* SizeOfInitializedData */
506 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
507 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
508 output( "\t%s %s\n", /* AddressOfEntryPoint */
509 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
510 if (get_ptr_size() == 4)
512 output( "\t.long 0\n" ); /* BaseOfCode */
513 output( "\t.long 0\n" ); /* BaseOfData */
515 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
516 get_asm_ptr_keyword() );
517 output( "\t.long %u\n", page_size ); /* SectionAlignment */
518 output( "\t.long %u\n", page_size ); /* FileAlignment */
519 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
520 get_asm_short_keyword() );
521 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
522 get_asm_short_keyword() );
523 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
524 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
525 output( "\t.long 0\n" ); /* Win32VersionValue */
526 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
527 asm_name("_end") );
528 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
529 output( "\t.long 0\n" ); /* CheckSum */
530 output( "\t%s 0x%04x\n", /* Subsystem */
531 get_asm_short_keyword(), spec->subsystem );
532 output( "\t%s 0x%04x\n", /* DllCharacteristics */
533 get_asm_short_keyword(), spec->dll_characteristics );
534 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
535 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
536 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
537 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
538 output( "\t.long 0\n" ); /* LoaderFlags */
539 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
541 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
542 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
543 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
544 else
545 output( "\t.long 0,0\n" );
547 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
548 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
549 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
550 else
551 output( "\t.long 0,0\n" );
553 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
554 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
555 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
556 else
557 output( "\t.long 0,0\n" );
559 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
560 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
561 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
562 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
563 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
564 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
565 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
566 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
567 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
568 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
569 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
570 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
571 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
573 output( "\n\t%s\n", get_asm_string_section() );
574 output( "%s\n", asm_globl("__wine_spec_file_name") );
575 output( ".L__wine_spec_file_name:\n" );
576 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
577 if (target_platform == PLATFORM_APPLE)
578 output( "\t.lcomm %s,4\n", asm_name("_end") );
580 output_asm_constructor( "__wine_spec_init_ctor" );
584 /*******************************************************************
585 * BuildSpec32File
587 * Build a Win32 C file from a spec file.
589 void BuildSpec32File( DLLSPEC *spec )
591 resolve_imports( spec );
592 output_standard_file_header();
593 output_module( spec );
594 output_stubs( spec );
595 output_exports( spec );
596 output_imports( spec );
597 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
598 output_resources( spec );
599 output_gnu_stack_note();
603 /*******************************************************************
604 * output_fake_module
606 * Build a fake binary module from a spec file.
608 void output_fake_module( DLLSPEC *spec )
610 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
611 0xc2, 0x0c, 0x00 }; /* ret $12 */
613 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
614 0xc2, 0x04, 0x00 }; /* ret $4 */
616 static const char fakedll_signature[] = "Wine placeholder DLL";
617 const unsigned int page_size = get_page_size();
618 const unsigned int section_align = page_size;
619 const unsigned int file_align = 0x200;
620 const unsigned int reloc_size = 8;
621 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
622 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
623 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
624 sizeof(dll_code_section) : sizeof(exe_code_section);
625 unsigned char *resources;
626 unsigned int resources_size;
627 unsigned int image_size = 3 * section_align;
629 resolve_imports( spec );
630 output_bin_resources( spec, 3 * section_align );
631 resources = output_buffer;
632 resources_size = output_buffer_pos;
633 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
635 init_output_buffer();
637 put_word( 0x5a4d ); /* e_magic */
638 put_word( 0x40 ); /* e_cblp */
639 put_word( 0x01 ); /* e_cp */
640 put_word( 0 ); /* e_crlc */
641 put_word( lfanew / 16 ); /* e_cparhdr */
642 put_word( 0x0000 ); /* e_minalloc */
643 put_word( 0xffff ); /* e_maxalloc */
644 put_word( 0x0000 ); /* e_ss */
645 put_word( 0x00b8 ); /* e_sp */
646 put_word( 0 ); /* e_csum */
647 put_word( 0 ); /* e_ip */
648 put_word( 0 ); /* e_cs */
649 put_word( lfanew ); /* e_lfarlc */
650 put_word( 0 ); /* e_ovno */
651 put_dword( 0 ); /* e_res */
652 put_dword( 0 );
653 put_word( 0 ); /* e_oemid */
654 put_word( 0 ); /* e_oeminfo */
655 put_dword( 0 ); /* e_res2 */
656 put_dword( 0 );
657 put_dword( 0 );
658 put_dword( 0 );
659 put_dword( 0 );
660 put_dword( lfanew );
662 put_data( fakedll_signature, sizeof(fakedll_signature) );
663 align_output( 16 );
665 put_dword( 0x4550 ); /* Signature */
666 switch(target_cpu)
668 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
669 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
670 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
671 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_SPARC ); break;
672 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARM ); break;
674 put_word( nb_sections ); /* NumberOfSections */
675 put_dword( 0 ); /* TimeDateStamp */
676 put_dword( 0 ); /* PointerToSymbolTable */
677 put_dword( 0 ); /* NumberOfSymbols */
678 put_word( get_ptr_size() == 8 ?
679 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
680 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
681 put_word( spec->characteristics ); /* Characteristics */
682 put_word( get_ptr_size() == 8 ?
683 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
684 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
685 put_byte( 0 ); /* MajorLinkerVersion */
686 put_byte( 0 ); /* MinorLinkerVersion */
687 put_dword( text_size ); /* SizeOfCode */
688 put_dword( 0 ); /* SizeOfInitializedData */
689 put_dword( 0 ); /* SizeOfUninitializedData */
690 put_dword( section_align ); /* AddressOfEntryPoint */
691 put_dword( section_align ); /* BaseOfCode */
692 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
693 put_pword( 0x10000000 ); /* ImageBase */
694 put_dword( section_align ); /* SectionAlignment */
695 put_dword( file_align ); /* FileAlignment */
696 put_word( 1 ); /* MajorOperatingSystemVersion */
697 put_word( 0 ); /* MinorOperatingSystemVersion */
698 put_word( 0 ); /* MajorImageVersion */
699 put_word( 0 ); /* MinorImageVersion */
700 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
701 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
702 put_dword( 0 ); /* Win32VersionValue */
703 put_dword( image_size ); /* SizeOfImage */
704 put_dword( file_align ); /* SizeOfHeaders */
705 put_dword( 0 ); /* CheckSum */
706 put_word( spec->subsystem ); /* Subsystem */
707 put_word( spec->dll_characteristics ); /* DllCharacteristics */
708 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
709 put_pword( page_size ); /* SizeOfStackCommit */
710 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
711 put_pword( page_size ); /* SizeOfHeapCommit */
712 put_dword( 0 ); /* LoaderFlags */
713 put_dword( 16 ); /* NumberOfRvaAndSizes */
715 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
716 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
717 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
719 put_dword( 3 * section_align );
720 put_dword( resources_size );
722 else
724 put_dword( 0 );
725 put_dword( 0 );
728 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
729 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
730 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
731 put_dword( reloc_size );
732 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
733 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
734 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
735 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
736 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
737 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
738 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
739 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
740 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
741 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
743 /* .text section */
744 put_data( ".text\0\0", 8 ); /* Name */
745 put_dword( section_align ); /* VirtualSize */
746 put_dword( section_align ); /* VirtualAddress */
747 put_dword( text_size ); /* SizeOfRawData */
748 put_dword( file_align ); /* PointerToRawData */
749 put_dword( 0 ); /* PointerToRelocations */
750 put_dword( 0 ); /* PointerToLinenumbers */
751 put_word( 0 ); /* NumberOfRelocations */
752 put_word( 0 ); /* NumberOfLinenumbers */
753 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
755 /* .reloc section */
756 put_data( ".reloc\0", 8 ); /* Name */
757 put_dword( section_align ); /* VirtualSize */
758 put_dword( 2 * section_align );/* VirtualAddress */
759 put_dword( reloc_size ); /* SizeOfRawData */
760 put_dword( 2 * 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( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
767 /* .rsrc section */
768 if (resources_size)
770 put_data( ".rsrc\0\0", 8 ); /* Name */
771 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
772 put_dword( 3 * section_align );/* VirtualAddress */
773 put_dword( resources_size ); /* SizeOfRawData */
774 put_dword( 3 * file_align ); /* PointerToRawData */
775 put_dword( 0 ); /* PointerToRelocations */
776 put_dword( 0 ); /* PointerToLinenumbers */
777 put_word( 0 ); /* NumberOfRelocations */
778 put_word( 0 ); /* NumberOfLinenumbers */
779 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
782 /* .text contents */
783 align_output( file_align );
784 if (spec->characteristics & IMAGE_FILE_DLL)
785 put_data( dll_code_section, sizeof(dll_code_section) );
786 else
787 put_data( exe_code_section, sizeof(exe_code_section) );
789 /* .reloc contents */
790 align_output( file_align );
791 put_dword( 0 ); /* VirtualAddress */
792 put_dword( 0 ); /* SizeOfBlock */
794 /* .rsrc contents */
795 if (resources_size)
797 align_output( file_align );
798 put_data( resources, resources_size );
800 flush_output_buffer();
804 /*******************************************************************
805 * output_def_file
807 * Build a Win32 def file from a spec file.
809 void output_def_file( DLLSPEC *spec, int include_private )
811 DLLSPEC *spec32 = NULL;
812 const char *name;
813 int i, total;
815 if (spec->type == SPEC_WIN16)
817 spec32 = alloc_dll_spec();
818 add_16bit_exports( spec32, spec );
819 spec = spec32;
822 if (spec_file_name)
823 output( "; File generated automatically from %s; do not edit!\n\n",
824 spec_file_name );
825 else
826 output( "; File generated automatically; do not edit!\n\n" );
828 output( "LIBRARY %s\n\n", spec->file_name);
829 output( "EXPORTS\n");
831 /* Output the exports and relay entry points */
833 for (i = total = 0; i < spec->nb_entry_points; i++)
835 const ORDDEF *odp = &spec->entry_points[i];
836 int is_data = 0;
838 if (!odp) continue;
840 if (odp->name) name = odp->name;
841 else if (odp->export_name) name = odp->export_name;
842 else continue;
844 if (!(odp->flags & FLAG_PRIVATE)) total++;
845 else if (!include_private) continue;
847 if (odp->type == TYPE_STUB) continue;
849 output( " %s", name );
851 switch(odp->type)
853 case TYPE_EXTERN:
854 is_data = 1;
855 /* fall through */
856 case TYPE_VARARGS:
857 case TYPE_CDECL:
858 case TYPE_THISCALL:
859 /* try to reduce output */
860 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
861 output( "=%s", odp->link_name );
862 break;
863 case TYPE_STDCALL:
865 int at_param = get_args_size( odp );
866 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
867 if (odp->flags & FLAG_FORWARD)
869 output( "=%s", odp->link_name );
871 else if (strcmp(name, odp->link_name)) /* try to reduce output */
873 output( "=%s", odp->link_name );
874 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
876 break;
878 default:
879 assert(0);
881 output( " @%d", odp->ordinal );
882 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
883 if (is_data) output( " DATA" );
884 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
885 output( "\n" );
887 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
888 if (spec32) free_dll_spec( spec32 );