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
26 #include "wine/port.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_ARMNT 0x01C4
40 #define IMAGE_FILE_MACHINE_ARM64 0xaa64
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
49 int needs_get_pc_thunk
= 0;
51 /* check if entry point needs a relay thunk */
52 static inline int needs_relay( const ORDDEF
*odp
)
54 /* skip nonexistent entry points */
56 /* skip non-functions */
64 if (odp
->u
.func
.nb_args
!= -1) break;
69 /* skip norelay and forward entry points */
70 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) return 0;
71 /* skip register entry points on x86_64 */
72 if (target_cpu
== CPU_x86_64
&& (odp
->flags
& FLAG_REGISTER
)) return 0;
76 static int is_float_arg( const ORDDEF
*odp
, int arg
)
78 if (arg
>= odp
->u
.func
.nb_args
) return 0;
79 return (odp
->u
.func
.args
[arg
] == ARG_FLOAT
|| odp
->u
.func
.args
[arg
] == ARG_DOUBLE
);
82 /* check if dll will output relay thunks */
83 static int has_relays( DLLSPEC
*spec
)
87 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
&& target_cpu
!= CPU_ARM
) return 0;
89 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
91 ORDDEF
*odp
= spec
->ordinals
[i
];
92 if (needs_relay( odp
)) return 1;
97 /*******************************************************************
100 * Output entry points for relay debugging
102 static void output_relay_debug( DLLSPEC
*spec
)
105 unsigned int pos
, args
, flags
;
107 /* first the table of entry point offsets */
109 output( "\t%s\n", get_asm_rodata_section() );
110 output( "\t.align %d\n", get_alignment(4) );
111 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
113 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
115 ORDDEF
*odp
= spec
->ordinals
[i
];
117 if (needs_relay( odp
))
118 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i
);
120 output( "\t.long 0\n" );
123 /* then the table of argument types */
125 output( "\t.align %d\n", get_alignment(4) );
126 output( ".L__wine_spec_relay_arg_types:\n" );
128 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
130 ORDDEF
*odp
= spec
->ordinals
[i
];
131 unsigned int mask
= 0;
133 if (needs_relay( odp
))
135 for (j
= pos
= 0; pos
< 16 && j
< odp
->u
.func
.nb_args
; j
++)
137 switch (odp
->u
.func
.args
[j
])
139 case ARG_STR
: mask
|= 1 << (2 * pos
++); break;
140 case ARG_WSTR
: mask
|= 2 << (2 * pos
++); break;
142 case ARG_DOUBLE
: pos
+= 8 / get_ptr_size(); break;
143 case ARG_INT128
: pos
+= (target_cpu
== CPU_x86
) ? 4 : 1; break;
144 default: pos
++; break;
148 output( "\t.long 0x%08x\n", mask
);
151 /* then the relay thunks */
153 output( "\t.text\n" );
154 output( "__wine_spec_relay_entry_points:\n" );
155 output( "\tnop\n" ); /* to avoid 0 offset */
157 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
159 ORDDEF
*odp
= spec
->ordinals
[i
];
161 if (!needs_relay( odp
)) continue;
163 output( "\t.align %d\n", get_alignment(4) );
164 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
165 output_cfi( ".cfi_startproc" );
167 args
= get_args_size(odp
) / get_ptr_size();
173 if (odp
->type
== TYPE_THISCALL
) /* add the this pointer */
175 output( "\tpopl %%eax\n" );
176 output( "\tpushl %%ecx\n" );
177 output( "\tpushl %%eax\n" );
180 if (odp
->flags
& FLAG_REGISTER
)
181 output( "\tpushl %%eax\n" );
183 output( "\tpushl %%esp\n" );
184 output_cfi( ".cfi_adjust_cfa_offset 4" );
186 if (odp
->flags
& FLAG_RET64
) flags
|= 1;
187 output( "\tpushl $%u\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
188 output_cfi( ".cfi_adjust_cfa_offset 4" );
192 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
193 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
194 needs_get_pc_thunk
= 1;
196 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
197 output( "\tpushl %%eax\n" );
198 output_cfi( ".cfi_adjust_cfa_offset 4" );
200 if (odp
->flags
& FLAG_REGISTER
)
202 output( "\tcall *8(%%eax)\n" );
206 output( "\tcall *4(%%eax)\n" );
207 output_cfi( ".cfi_adjust_cfa_offset -12" );
208 if (odp
->type
== TYPE_STDCALL
|| odp
->type
== TYPE_THISCALL
)
209 output( "\tret $%u\n", args
* get_ptr_size() );
217 unsigned int mask
, val
, count
= 0;
218 unsigned int stack_size
= min( 16, (args
* 4 + 7) & ~7 );
220 if (odp
->flags
& FLAG_RET64
) flags
|= 1;
221 val
= (flags
<< 24) | (args
<< 16) | (i
- spec
->base
);
224 case 16: output( "\tpush {r0-r3}\n" ); break;
225 case 8: output( "\tpush {r0-r1}\n" ); break;
228 output( "\tpush {LR}\n" );
229 output( "\tmov r2, SP\n");
230 output( "\tsub SP, #4\n");
231 for (mask
= 0xff; mask
; mask
<<= 8)
232 if (val
& mask
) output( "\t%s r1,#%u\n", count
++ ? "add" : "mov", val
& mask
);
233 if (!count
) output( "\tmov r1,#0\n" );
234 output( "\tldr r0, 2f\n");
235 output( "\tadd r0, PC\n");
236 output( "\tldr IP, [r0, #4]\n");
237 output( "1:\tblx IP\n");
238 output( "\tldr IP, [SP, #4]\n" );
239 output( "\tadd SP, #%u\n", stack_size
+ 8 );
240 output( "\tbx IP\n");
241 output( "2:\t.long .L__wine_spec_relay_descr-1b\n" );
246 output( "\tsubq $40,%%rsp\n" );
247 output_cfi( ".cfi_adjust_cfa_offset 40" );
250 default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp
, 3 ) ? "xmm3" : "r9" );
252 case 3: output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp
, 2 ) ? "xmm2" : "r8" );
254 case 2: output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp
, 1 ) ? "xmm1" : "rdx" );
256 case 1: output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp
, 0 ) ? "xmm0" : "rcx" );
260 output( "\tleaq 40(%%rsp),%%r8\n" );
261 output( "\tmovq $%u,%%rdx\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
262 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
263 output( "\tcallq *8(%%rcx)\n" );
264 output( "\taddq $40,%%rsp\n" );
265 output_cfi( ".cfi_adjust_cfa_offset -40" );
272 output_cfi( ".cfi_endproc" );
276 /*******************************************************************
279 * Output the export table for a Win32 module.
281 void output_exports( DLLSPEC
*spec
)
284 int nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
286 if (!nr_exports
) return;
288 output( "\n/* export table */\n\n" );
289 output( "\t.data\n" );
290 output( "\t.align %d\n", get_alignment(4) );
291 output( ".L__wine_spec_exports:\n" );
293 /* export directory header */
295 output( "\t.long 0\n" ); /* Characteristics */
296 output( "\t.long 0\n" ); /* TimeDateStamp */
297 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
298 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
299 output( "\t.long %u\n", spec
->base
); /* Base */
300 output( "\t.long %u\n", nr_exports
); /* NumberOfFunctions */
301 output( "\t.long %u\n", spec
->nb_names
); /* NumberOfNames */
302 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
305 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
306 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
310 output( "\t.long 0\n" ); /* AddressOfNames */
311 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
314 /* output the function pointers */
316 output( "\n.L__wine_spec_exports_funcs:\n" );
317 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
319 ORDDEF
*odp
= spec
->ordinals
[i
];
320 if (!odp
) output( "\t%s 0\n", get_asm_ptr_keyword() );
321 else switch(odp
->type
)
328 if (odp
->flags
& FLAG_FORWARD
)
330 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size
);
331 fwd_size
+= strlen(odp
->link_name
) + 1;
333 else if (odp
->flags
& FLAG_EXT_LINK
)
335 output( "\t%s %s_%s\n",
336 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp
->link_name
);
340 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp
->link_name
) );
344 output( "\t%s %s\n", get_asm_ptr_keyword(),
345 asm_name( get_stub_name( odp
, spec
)) );
354 /* output the function name pointers */
356 int namepos
= strlen(spec
->file_name
) + 1;
358 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
359 for (i
= 0; i
< spec
->nb_names
; i
++)
361 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos
);
362 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
365 /* output the function ordinals */
367 output( "\n.L__wine_spec_exp_ordinals:\n" );
368 for (i
= 0; i
< spec
->nb_names
; i
++)
370 output( "\t.short %d\n", spec
->names
[i
]->ordinal
- spec
->base
);
372 if (spec
->nb_names
% 2)
374 output( "\t.short 0\n" );
378 /* output the export name strings */
380 output( "\n.L__wine_spec_exp_names:\n" );
381 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
382 for (i
= 0; i
< spec
->nb_names
; i
++)
383 output( "\t%s \"%s\"\n",
384 get_asm_string_keyword(), spec
->names
[i
]->name
);
386 /* output forward strings */
390 output( "\n.L__wine_spec_forwards:\n" );
391 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
393 ORDDEF
*odp
= spec
->ordinals
[i
];
394 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
395 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->link_name
);
398 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
399 output( ".L__wine_spec_exports_end:\n" );
403 if (!has_relays( spec
))
405 output( "\t%s 0\n", get_asm_ptr_keyword() );
409 output( ".L__wine_spec_relay_descr:\n" );
410 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
411 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
412 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
413 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
414 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
415 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
417 output_relay_debug( spec
);
421 /*******************************************************************
422 * output_asm_constructor
424 * Output code for calling a dll constructor.
426 static void output_asm_constructor( const char *constructor
)
428 if (target_platform
== PLATFORM_APPLE
)
430 /* Mach-O doesn't have an init section */
431 output( "\n\t.mod_init_func\n" );
432 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
433 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(constructor
) );
441 output( "\n\t.section \".init\",\"ax\"\n" );
442 output( "\tcall %s\n", asm_name(constructor
) );
445 output( "\n\t.section \".text\",\"ax\"\n" );
446 output( "\tblx %s\n", asm_name(constructor
) );
450 output( "\n\t.section \".init\",\"ax\"\n" );
451 output( "\tbl %s\n", asm_name(constructor
) );
458 /*******************************************************************
461 * Output the module data.
463 void output_module( DLLSPEC
*spec
)
466 unsigned int page_size
= get_page_size();
468 /* Reserve some space for the PE header */
470 switch (target_platform
)
473 output( "\t.text\n" );
474 output( "\t.align %d\n", get_alignment(page_size
) );
475 output( "__wine_spec_pe_header:\n" );
476 output( "\t.space 65536\n" );
478 case PLATFORM_SOLARIS
:
479 output( "\n\t.section \".text\",\"ax\"\n" );
480 output( "__wine_spec_pe_header:\n" );
481 output( "\t.skip %u\n", 65536 + page_size
);
488 output( "\n\t.section \".init\",\"ax\"\n" );
489 output( "\tjmp 1f\n" );
492 output( "\n\t.section \".text\",\"ax\"\n" );
493 output( "\tb 1f\n" );
497 output( "\n\t.section \".init\",\"ax\"\n" );
498 output( "\tb 1f\n" );
501 output( "__wine_spec_pe_header:\n" );
502 output( "\t.skip %u\n", 65536 + page_size
);
507 /* Output the NT header */
509 output( "\n\t.data\n" );
510 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
511 output( "%s\n", asm_globl("__wine_spec_nt_header") );
512 output( ".L__wine_spec_rva_base:\n" );
514 output( "\t.long 0x4550\n" ); /* Signature */
517 case CPU_x86
: machine
= IMAGE_FILE_MACHINE_I386
; break;
518 case CPU_x86_64
: machine
= IMAGE_FILE_MACHINE_AMD64
; break;
519 case CPU_POWERPC
: machine
= IMAGE_FILE_MACHINE_POWERPC
; break;
520 case CPU_ARM
: machine
= IMAGE_FILE_MACHINE_ARMNT
; break;
521 case CPU_ARM64
: machine
= IMAGE_FILE_MACHINE_ARM64
; break;
523 output( "\t.short 0x%04x\n", /* Machine */
525 output( "\t.short 0\n" ); /* NumberOfSections */
526 output( "\t.long 0\n" ); /* TimeDateStamp */
527 output( "\t.long 0\n" ); /* PointerToSymbolTable */
528 output( "\t.long 0\n" ); /* NumberOfSymbols */
529 output( "\t.short %d\n", /* SizeOfOptionalHeader */
530 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
: IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
);
531 output( "\t.short 0x%04x\n", /* Characteristics */
532 spec
->characteristics
);
533 output( "\t.short 0x%04x\n", /* Magic */
534 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
: IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
535 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
536 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
537 output( "\t.long 0\n" ); /* SizeOfCode */
538 output( "\t.long 0\n" ); /* SizeOfInitializedData */
539 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
540 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
541 output( "\t%s %s\n", /* AddressOfEntryPoint */
542 get_asm_ptr_keyword(), spec
->init_func
? asm_name(spec
->init_func
) : "0" );
543 if (get_ptr_size() == 4)
545 output( "\t.long 0\n" ); /* BaseOfCode */
546 output( "\t.long 0\n" ); /* BaseOfData */
548 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
549 get_asm_ptr_keyword() );
550 output( "\t.long %u\n", page_size
); /* SectionAlignment */
551 output( "\t.long %u\n", page_size
); /* FileAlignment */
552 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
553 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
554 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
555 spec
->subsystem_major
, spec
->subsystem_minor
);
556 output( "\t.long 0\n" ); /* Win32VersionValue */
557 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
559 output( "\t.long %u\n", page_size
); /* SizeOfHeaders */
560 output( "\t.long 0\n" ); /* CheckSum */
561 output( "\t.short 0x%04x\n", /* Subsystem */
563 output( "\t.short 0x%04x\n", /* DllCharacteristics */
564 spec
->dll_characteristics
);
565 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
566 get_asm_ptr_keyword(), (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
567 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
568 get_asm_ptr_keyword(), (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
569 output( "\t.long 0\n" ); /* LoaderFlags */
570 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
572 if (spec
->base
<= spec
->limit
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
573 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
574 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
576 output( "\t.long 0,0\n" );
578 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
579 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
580 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
582 output( "\t.long 0,0\n" );
584 if (spec
->nb_resources
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
585 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
586 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
588 output( "\t.long 0,0\n" );
590 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
591 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
592 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
593 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
594 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
595 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
596 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
597 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
598 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
599 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
600 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
601 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
602 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
604 output( "\n\t%s\n", get_asm_string_section() );
605 output( "%s\n", asm_globl("__wine_spec_file_name") );
606 output( ".L__wine_spec_file_name:\n" );
607 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
608 if (target_platform
== PLATFORM_APPLE
)
609 output( "\t.lcomm %s,4\n", asm_name("_end") );
611 output_asm_constructor( "__wine_spec_init_ctor" );
615 /*******************************************************************
618 * Build a Win32 C file from a spec file.
620 void BuildSpec32File( DLLSPEC
*spec
)
622 needs_get_pc_thunk
= 0;
623 resolve_imports( spec
);
624 output_standard_file_header();
625 output_module( spec
);
626 output_stubs( spec
);
627 output_exports( spec
);
628 output_imports( spec
);
629 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
630 if (needs_get_pc_thunk
) output_get_pc_thunk();
631 output_resources( spec
);
632 output_gnu_stack_note();
636 /*******************************************************************
639 * Build a fake binary module from a spec file.
641 void output_fake_module( DLLSPEC
*spec
)
643 static const unsigned char dll_code_section
[] = { 0x31, 0xc0, /* xor %eax,%eax */
644 0xc2, 0x0c, 0x00 }; /* ret $12 */
646 static const unsigned char exe_code_section
[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
647 0xc2, 0x04, 0x00 }; /* ret $4 */
649 static const char fakedll_signature
[] = "Wine placeholder DLL";
650 const unsigned int page_size
= get_page_size();
651 const unsigned int section_align
= page_size
;
652 const unsigned int file_align
= 0x200;
653 const unsigned int reloc_size
= 8;
654 const unsigned int lfanew
= (0x40 + sizeof(fakedll_signature
) + 15) & ~15;
655 const unsigned int nb_sections
= 2 + (spec
->nb_resources
!= 0);
656 const unsigned int text_size
= (spec
->characteristics
& IMAGE_FILE_DLL
) ?
657 sizeof(dll_code_section
) : sizeof(exe_code_section
);
658 unsigned char *resources
;
659 unsigned int resources_size
;
660 unsigned int image_size
= 3 * section_align
;
662 resolve_imports( spec
);
663 output_bin_resources( spec
, 3 * section_align
);
664 resources
= output_buffer
;
665 resources_size
= output_buffer_pos
;
666 if (resources_size
) image_size
+= (resources_size
+ section_align
- 1) & ~(section_align
- 1);
668 init_output_buffer();
670 put_word( 0x5a4d ); /* e_magic */
671 put_word( 0x40 ); /* e_cblp */
672 put_word( 0x01 ); /* e_cp */
673 put_word( 0 ); /* e_crlc */
674 put_word( lfanew
/ 16 ); /* e_cparhdr */
675 put_word( 0x0000 ); /* e_minalloc */
676 put_word( 0xffff ); /* e_maxalloc */
677 put_word( 0x0000 ); /* e_ss */
678 put_word( 0x00b8 ); /* e_sp */
679 put_word( 0 ); /* e_csum */
680 put_word( 0 ); /* e_ip */
681 put_word( 0 ); /* e_cs */
682 put_word( lfanew
); /* e_lfarlc */
683 put_word( 0 ); /* e_ovno */
684 put_dword( 0 ); /* e_res */
686 put_word( 0 ); /* e_oemid */
687 put_word( 0 ); /* e_oeminfo */
688 put_dword( 0 ); /* e_res2 */
695 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
698 put_dword( 0x4550 ); /* Signature */
701 case CPU_x86
: put_word( IMAGE_FILE_MACHINE_I386
); break;
702 case CPU_x86_64
: put_word( IMAGE_FILE_MACHINE_AMD64
); break;
703 case CPU_POWERPC
: put_word( IMAGE_FILE_MACHINE_POWERPC
); break;
704 case CPU_ARM
: put_word( IMAGE_FILE_MACHINE_ARMNT
); break;
705 case CPU_ARM64
: put_word( IMAGE_FILE_MACHINE_ARM64
); break;
707 put_word( nb_sections
); /* NumberOfSections */
708 put_dword( 0 ); /* TimeDateStamp */
709 put_dword( 0 ); /* PointerToSymbolTable */
710 put_dword( 0 ); /* NumberOfSymbols */
711 put_word( get_ptr_size() == 8 ?
712 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
:
713 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
); /* SizeOfOptionalHeader */
714 put_word( spec
->characteristics
); /* Characteristics */
715 put_word( get_ptr_size() == 8 ?
716 IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
717 IMAGE_NT_OPTIONAL_HDR32_MAGIC
); /* Magic */
718 put_byte( 7 ); /* MajorLinkerVersion */
719 put_byte( 10 ); /* MinorLinkerVersion */
720 put_dword( text_size
); /* SizeOfCode */
721 put_dword( 0 ); /* SizeOfInitializedData */
722 put_dword( 0 ); /* SizeOfUninitializedData */
723 put_dword( section_align
); /* AddressOfEntryPoint */
724 put_dword( section_align
); /* BaseOfCode */
725 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
726 put_pword( 0x10000000 ); /* ImageBase */
727 put_dword( section_align
); /* SectionAlignment */
728 put_dword( file_align
); /* FileAlignment */
729 put_word( 1 ); /* MajorOperatingSystemVersion */
730 put_word( 0 ); /* MinorOperatingSystemVersion */
731 put_word( 0 ); /* MajorImageVersion */
732 put_word( 0 ); /* MinorImageVersion */
733 put_word( spec
->subsystem_major
); /* MajorSubsystemVersion */
734 put_word( spec
->subsystem_minor
); /* MinorSubsystemVersion */
735 put_dword( 0 ); /* Win32VersionValue */
736 put_dword( image_size
); /* SizeOfImage */
737 put_dword( file_align
); /* SizeOfHeaders */
738 put_dword( 0 ); /* CheckSum */
739 put_word( spec
->subsystem
); /* Subsystem */
740 put_word( spec
->dll_characteristics
); /* DllCharacteristics */
741 put_pword( (spec
->stack_size
? spec
->stack_size
: 1024) * 1024 ); /* SizeOfStackReserve */
742 put_pword( page_size
); /* SizeOfStackCommit */
743 put_pword( (spec
->heap_size
? spec
->heap_size
: 1024) * 1024 ); /* SizeOfHeapReserve */
744 put_pword( page_size
); /* SizeOfHeapCommit */
745 put_dword( 0 ); /* LoaderFlags */
746 put_dword( 16 ); /* NumberOfRvaAndSizes */
748 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
749 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
750 if (resources_size
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
752 put_dword( 3 * section_align
);
753 put_dword( resources_size
);
761 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
762 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
763 put_dword( 2 * section_align
); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
764 put_dword( reloc_size
);
765 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
766 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
767 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
768 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
769 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
770 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
771 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
772 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
773 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
774 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
777 put_data( ".text\0\0", 8 ); /* Name */
778 put_dword( section_align
); /* VirtualSize */
779 put_dword( section_align
); /* VirtualAddress */
780 put_dword( text_size
); /* SizeOfRawData */
781 put_dword( file_align
); /* PointerToRawData */
782 put_dword( 0 ); /* PointerToRelocations */
783 put_dword( 0 ); /* PointerToLinenumbers */
784 put_word( 0 ); /* NumberOfRelocations */
785 put_word( 0 ); /* NumberOfLinenumbers */
786 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
789 put_data( ".reloc\0", 8 ); /* Name */
790 put_dword( section_align
); /* VirtualSize */
791 put_dword( 2 * section_align
);/* VirtualAddress */
792 put_dword( reloc_size
); /* SizeOfRawData */
793 put_dword( 2 * file_align
); /* PointerToRawData */
794 put_dword( 0 ); /* PointerToRelocations */
795 put_dword( 0 ); /* PointerToLinenumbers */
796 put_word( 0 ); /* NumberOfRelocations */
797 put_word( 0 ); /* NumberOfLinenumbers */
798 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
803 put_data( ".rsrc\0\0", 8 ); /* Name */
804 put_dword( (resources_size
+ section_align
- 1) & ~(section_align
- 1) ); /* VirtualSize */
805 put_dword( 3 * section_align
);/* VirtualAddress */
806 put_dword( resources_size
); /* SizeOfRawData */
807 put_dword( 3 * file_align
); /* PointerToRawData */
808 put_dword( 0 ); /* PointerToRelocations */
809 put_dword( 0 ); /* PointerToLinenumbers */
810 put_word( 0 ); /* NumberOfRelocations */
811 put_word( 0 ); /* NumberOfLinenumbers */
812 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
816 align_output( file_align
);
817 if (spec
->characteristics
& IMAGE_FILE_DLL
)
818 put_data( dll_code_section
, sizeof(dll_code_section
) );
820 put_data( exe_code_section
, sizeof(exe_code_section
) );
822 /* .reloc contents */
823 align_output( file_align
);
824 put_dword( 0 ); /* VirtualAddress */
825 put_dword( 0 ); /* SizeOfBlock */
830 align_output( file_align
);
831 put_data( resources
, resources_size
);
833 flush_output_buffer();
837 /*******************************************************************
840 * Build a Win32 def file from a spec file.
842 void output_def_file( DLLSPEC
*spec
, int include_private
)
844 DLLSPEC
*spec32
= NULL
;
848 if (spec
->type
== SPEC_WIN16
)
850 spec32
= alloc_dll_spec();
851 add_16bit_exports( spec32
, spec
);
856 output( "; File generated automatically from %s; do not edit!\n\n",
859 output( "; File generated automatically; do not edit!\n\n" );
861 output( "LIBRARY %s\n\n", spec
->file_name
);
862 output( "EXPORTS\n");
864 /* Output the exports and relay entry points */
866 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
868 const ORDDEF
*odp
= &spec
->entry_points
[i
];
871 if (odp
->name
) name
= odp
->name
;
872 else if (odp
->export_name
) name
= odp
->export_name
;
875 if (!(odp
->flags
& FLAG_PRIVATE
)) total
++;
876 else if (!include_private
) continue;
878 if (odp
->type
== TYPE_STUB
) continue;
880 output( " %s", name
);
890 /* try to reduce output */
891 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
892 output( "=%s", odp
->link_name
);
896 int at_param
= get_args_size( odp
);
897 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
898 if (odp
->flags
& FLAG_FORWARD
)
900 output( "=%s", odp
->link_name
);
902 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
904 output( "=%s", odp
->link_name
);
905 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
912 output( " @%d", odp
->ordinal
);
913 if (!odp
->name
|| (odp
->flags
& FLAG_ORDINAL
)) output( " NONAME" );
914 if (is_data
) output( " DATA" );
915 if (odp
->flags
& FLAG_PRIVATE
) output( " PRIVATE" );
918 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);
919 if (spec32
) free_dll_spec( spec32
);