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_ALPHA 0x0184
38 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
39 #define IMAGE_FILE_MACHINE_AMD64 0x8664
40 #define IMAGE_FILE_MACHINE_ARM 0x01C0
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 /* check if entry point needs a relay thunk */
50 static inline int needs_relay( const ORDDEF
*odp
)
52 /* skip nonexistent entry points */
54 /* skip non-functions */
55 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) return 0;
56 /* skip norelay and forward entry points */
57 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) return 0;
61 /* check if dll will output relay thunks */
62 int has_relays( DLLSPEC
*spec
)
66 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
) return 0;
68 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
70 ORDDEF
*odp
= spec
->ordinals
[i
];
71 if (needs_relay( odp
)) return 1;
76 /*******************************************************************
79 * Generate an internal name for an entry point. Used for stubs etc.
81 static const char *make_internal_name( const ORDDEF
*odp
, DLLSPEC
*spec
, const char *prefix
)
83 static char buffer
[256];
84 if (odp
->name
|| odp
->export_name
)
87 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, spec
->file_name
,
88 odp
->name
? odp
->name
: odp
->export_name
);
89 /* make sure name is a legal C identifier */
90 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
91 if (!*p
) return buffer
;
93 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, make_c_identifier(spec
->file_name
), odp
->ordinal
);
98 /*******************************************************************
101 * Output entry points for relay debugging
103 static void output_relay_debug( DLLSPEC
*spec
)
106 unsigned int j
, args
, flags
;
108 /* first the table of entry point offsets */
110 output( "\t%s\n", get_asm_rodata_section() );
111 output( "\t.align %d\n", get_alignment(4) );
112 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
114 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
116 ORDDEF
*odp
= spec
->ordinals
[i
];
118 if (needs_relay( odp
))
119 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i
);
121 output( "\t.long 0\n" );
124 /* then the table of argument types */
126 output( "\t.align %d\n", get_alignment(4) );
127 output( ".L__wine_spec_relay_arg_types:\n" );
129 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
131 ORDDEF
*odp
= spec
->ordinals
[i
];
132 unsigned int mask
= 0;
134 if (needs_relay( odp
))
136 for (j
= 0; j
< 16 && odp
->u
.func
.arg_types
[j
]; j
++)
138 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
139 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
142 output( "\t.long 0x%08x\n", mask
);
145 /* then the relay thunks */
147 output( "\t.text\n" );
148 output( "__wine_spec_relay_entry_points:\n" );
149 output( "\tnop\n" ); /* to avoid 0 offset */
151 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
153 ORDDEF
*odp
= spec
->ordinals
[i
];
155 if (!needs_relay( odp
)) continue;
157 output( "\t.align %d\n", get_alignment(4) );
158 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
160 args
= strlen(odp
->u
.func
.arg_types
);
166 if (odp
->flags
& FLAG_REGISTER
)
167 output( "\tpushl %%eax\n" );
169 output( "\tpushl %%esp\n" );
171 if (odp
->flags
& FLAG_RET64
) flags
|= 1;
172 output( "\tpushl $%u\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
176 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
177 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
179 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
180 output( "\tpushl %%eax\n" );
182 if (odp
->flags
& FLAG_REGISTER
)
184 output( "\tcall *8(%%eax)\n" );
188 output( "\tcall *4(%%eax)\n" );
189 if (odp
->type
== TYPE_STDCALL
)
190 output( "\tret $%u\n", args
* get_ptr_size() );
197 output( "\t.cfi_startproc\n" );
198 output( "\tsubq $40,%%rsp\n" );
199 output( "\t.cfi_adjust_cfa_offset 40\n" );
200 output( "\tmovq %%rcx,48(%%rsp)\n" );
201 output( "\tmovq %%rdx,56(%%rsp)\n" );
202 output( "\tmovq %%r8,64(%%rsp)\n" );
203 output( "\tmovq %%r9,72(%%rsp)\n" );
204 output( "\tleaq 40(%%rsp),%%r8\n" );
205 output( "\tmovq $%u,%%rdx\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
206 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
207 output( "\tcallq *%u(%%rcx)\n", (odp
->flags
& FLAG_REGISTER
) ? 16 : 8 );
208 output( "\taddq $40,%%rsp\n" );
209 output( "\t.cfi_adjust_cfa_offset -40\n" );
211 output( "\t.cfi_endproc\n" );
220 /*******************************************************************
223 * Output the export table for a Win32 module.
225 void output_exports( DLLSPEC
*spec
)
228 int nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
230 if (!nr_exports
) return;
232 output( "\n/* export table */\n\n" );
233 output( "\t.data\n" );
234 output( "\t.align %d\n", get_alignment(4) );
235 output( ".L__wine_spec_exports:\n" );
237 /* export directory header */
239 output( "\t.long 0\n" ); /* Characteristics */
240 output( "\t.long 0\n" ); /* TimeDateStamp */
241 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
242 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
243 output( "\t.long %u\n", spec
->base
); /* Base */
244 output( "\t.long %u\n", nr_exports
); /* NumberOfFunctions */
245 output( "\t.long %u\n", spec
->nb_names
); /* NumberOfNames */
246 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
249 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
250 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
254 output( "\t.long 0\n" ); /* AddressOfNames */
255 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
258 /* output the function pointers */
260 output( "\n.L__wine_spec_exports_funcs:\n" );
261 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
263 ORDDEF
*odp
= spec
->ordinals
[i
];
264 if (!odp
) output( "\t%s 0\n", get_asm_ptr_keyword() );
265 else switch(odp
->type
)
271 if (odp
->flags
& FLAG_FORWARD
)
273 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size
);
274 fwd_size
+= strlen(odp
->link_name
) + 1;
276 else if (odp
->flags
& FLAG_EXT_LINK
)
278 output( "\t%s %s_%s\n",
279 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp
->link_name
);
283 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp
->link_name
) );
287 output( "\t%s %s\n", get_asm_ptr_keyword(),
288 asm_name( get_stub_name( odp
, spec
)) );
297 /* output the function name pointers */
299 int namepos
= strlen(spec
->file_name
) + 1;
301 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
302 for (i
= 0; i
< spec
->nb_names
; i
++)
304 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos
);
305 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
308 /* output the function ordinals */
310 output( "\n.L__wine_spec_exp_ordinals:\n" );
311 for (i
= 0; i
< spec
->nb_names
; i
++)
314 get_asm_short_keyword(), spec
->names
[i
]->ordinal
- spec
->base
);
316 if (spec
->nb_names
% 2)
318 output( "\t%s 0\n", get_asm_short_keyword() );
322 /* output the export name strings */
324 output( "\n.L__wine_spec_exp_names:\n" );
325 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
326 for (i
= 0; i
< spec
->nb_names
; i
++)
327 output( "\t%s \"%s\"\n",
328 get_asm_string_keyword(), spec
->names
[i
]->name
);
330 /* output forward strings */
334 output( "\n.L__wine_spec_forwards:\n" );
335 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
337 ORDDEF
*odp
= spec
->ordinals
[i
];
338 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
339 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->link_name
);
342 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
343 output( ".L__wine_spec_exports_end:\n" );
347 if (!has_relays( spec
))
349 output( "\t%s 0\n", get_asm_ptr_keyword() );
353 output( ".L__wine_spec_relay_descr:\n" );
354 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
355 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
356 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
357 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
358 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
359 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
361 output_relay_debug( spec
);
365 /*******************************************************************
368 * Output the functions for stub entry points
370 static void output_stub_funcs( DLLSPEC
*spec
)
375 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
377 ORDDEF
*odp
= &spec
->entry_points
[i
];
378 if (odp
->type
!= TYPE_STUB
) continue;
379 fprintf( outfile
, "#ifdef __GNUC__\n" );
380 fprintf( outfile
, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func ) __attribute__((noreturn));\n" );
381 fprintf( outfile
, "#else\n" );
382 fprintf( outfile
, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func );\n" );
383 fprintf( outfile
, "#endif\n\n" );
388 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
390 const ORDDEF
*odp
= &spec
->entry_points
[i
];
391 if (odp
->type
!= TYPE_STUB
) continue;
392 output( "void %s(void) ", make_internal_name( odp
, spec
, "stub" ) );
394 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp
->name
);
395 else if (odp
->export_name
)
396 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp
->export_name
);
398 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%d\"); }\n", odp
->ordinal
);
403 /*******************************************************************
404 * output_asm_constructor
406 * Output code for calling a dll constructor.
408 static void output_asm_constructor( const char *constructor
)
410 if (target_platform
== PLATFORM_APPLE
)
412 /* Mach-O doesn't have an init section */
413 output( "\n\t.mod_init_func\n" );
414 output( "\t.align %d\n", get_alignment(4) );
415 output( "\t.long %s\n", asm_name(constructor
) );
419 output( "\n\t.section \".init\",\"ax\"\n" );
424 output( "\tcall %s\n", asm_name(constructor
) );
427 output( "\tcall %s\n", asm_name(constructor
) );
431 output( "\tjsr $26,%s\n", asm_name(constructor
) );
434 output( "\tblx %s\n", asm_name(constructor
) );
437 output( "\tbl %s\n", asm_name(constructor
) );
444 /*******************************************************************
447 * Output the module data.
449 void output_module( DLLSPEC
*spec
)
452 unsigned int page_size
= get_page_size();
454 /* Reserve some space for the PE header */
456 switch (target_platform
)
459 output( "\t.text\n" );
460 output( "\t.align %d\n", get_alignment(page_size
) );
461 output( "__wine_spec_pe_header:\n" );
462 output( "\t.space 65536\n" );
464 case PLATFORM_SOLARIS
:
465 output( "\n\t.section \".text\",\"ax\"\n" );
466 output( "__wine_spec_pe_header:\n" );
467 output( "\t.skip %u\n", 65536 + page_size
);
470 output( "\n\t.section \".init\",\"ax\"\n" );
477 output( "\tjmp 1f\n" );
481 output( "\tb 1f\n" );
484 output( "__wine_spec_pe_header:\n" );
485 output( "\t.skip %u\n", 65536 + page_size
);
490 /* Output the NT header */
492 output( "\n\t.data\n" );
493 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
494 output( "%s\n", asm_globl("__wine_spec_nt_header") );
495 output( ".L__wine_spec_rva_base:\n" );
497 output( "\t.long 0x4550\n" ); /* Signature */
500 case CPU_x86
: machine
= IMAGE_FILE_MACHINE_I386
; break;
501 case CPU_x86_64
: machine
= IMAGE_FILE_MACHINE_AMD64
; break;
502 case CPU_ARM
: machine
= IMAGE_FILE_MACHINE_ARM
; break;
503 case CPU_POWERPC
: machine
= IMAGE_FILE_MACHINE_POWERPC
; break;
504 case CPU_ALPHA
: machine
= IMAGE_FILE_MACHINE_ALPHA
; break;
505 case CPU_SPARC
: machine
= IMAGE_FILE_MACHINE_UNKNOWN
; break;
507 output( "\t%s 0x%04x\n", /* Machine */
508 get_asm_short_keyword(), machine
);
509 output( "\t%s 0\n", /* NumberOfSections */
510 get_asm_short_keyword() );
511 output( "\t.long 0\n" ); /* TimeDateStamp */
512 output( "\t.long 0\n" ); /* PointerToSymbolTable */
513 output( "\t.long 0\n" ); /* NumberOfSymbols */
514 output( "\t%s %d\n", /* SizeOfOptionalHeader */
515 get_asm_short_keyword(),
516 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
: IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
);
517 output( "\t%s 0x%04x\n", /* Characteristics */
518 get_asm_short_keyword(), spec
->characteristics
);
519 output( "\t%s 0x%04x\n", /* Magic */
520 get_asm_short_keyword(),
521 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
: IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
522 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
523 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
524 output( "\t.long 0\n" ); /* SizeOfCode */
525 output( "\t.long 0\n" ); /* SizeOfInitializedData */
526 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
527 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
528 output( "\t%s %s\n", /* AddressOfEntryPoint */
529 get_asm_ptr_keyword(), spec
->init_func
? asm_name(spec
->init_func
) : "0" );
530 if (get_ptr_size() == 4)
532 output( "\t.long 0\n" ); /* BaseOfCode */
533 output( "\t.long 0\n" ); /* BaseOfData */
535 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
536 get_asm_ptr_keyword() );
537 output( "\t.long %u\n", page_size
); /* SectionAlignment */
538 output( "\t.long %u\n", page_size
); /* FileAlignment */
539 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
540 get_asm_short_keyword() );
541 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
542 get_asm_short_keyword() );
543 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
544 get_asm_short_keyword(), spec
->subsystem_major
, spec
->subsystem_minor
);
545 output( "\t.long 0\n" ); /* Win32VersionValue */
546 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
548 output( "\t.long %u\n", page_size
); /* SizeOfHeaders */
549 output( "\t.long 0\n" ); /* CheckSum */
550 output( "\t%s 0x%04x\n", /* Subsystem */
551 get_asm_short_keyword(), spec
->subsystem
);
552 output( "\t%s 0x%04x\n", /* DllCharacteristics */
553 get_asm_short_keyword(), spec
->dll_characteristics
);
554 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
555 get_asm_ptr_keyword(), (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
556 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
557 get_asm_ptr_keyword(), (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
558 output( "\t.long 0\n" ); /* LoaderFlags */
559 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
561 if (spec
->base
<= spec
->limit
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
562 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
563 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
565 output( "\t.long 0,0\n" );
567 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
568 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
569 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
571 output( "\t.long 0,0\n" );
573 if (spec
->nb_resources
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
574 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
575 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
577 output( "\t.long 0,0\n" );
579 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
580 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
581 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
582 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
583 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
584 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
585 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
586 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
587 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
588 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
589 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
590 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
591 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
593 output( "\n\t%s\n", get_asm_string_section() );
594 output( "%s\n", asm_globl("__wine_spec_file_name") );
595 output( ".L__wine_spec_file_name:\n" );
596 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
597 if (target_platform
== PLATFORM_APPLE
)
598 output( "\t.lcomm %s,4\n", asm_name("_end") );
600 output_asm_constructor( "__wine_spec_init_ctor" );
604 /*******************************************************************
607 * Build a Win32 C file from a spec file.
609 void BuildSpec32File( DLLSPEC
*spec
)
611 resolve_imports( spec
);
612 output_standard_file_header();
613 output_module( spec
);
614 output_stubs( spec
);
615 output_exports( spec
);
616 output_imports( spec
);
617 output_resources( spec
);
618 output_gnu_stack_note();
622 /*******************************************************************
625 * Build a fake binary module from a spec file.
627 void output_fake_module( DLLSPEC
*spec
)
629 static const unsigned char dll_code_section
[] = { 0x31, 0xc0, /* xor %eax,%eax */
630 0xc2, 0x0c, 0x00 }; /* ret $12 */
632 static const unsigned char exe_code_section
[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
633 0xc2, 0x04, 0x00 }; /* ret $4 */
635 static const char fakedll_signature
[] = "Wine placeholder DLL";
636 const unsigned int page_size
= get_page_size();
637 const unsigned int section_align
= page_size
;
638 const unsigned int file_align
= 0x200;
639 const unsigned int reloc_size
= 8;
640 const unsigned int lfanew
= (0x40 + sizeof(fakedll_signature
) + 15) & ~15;
641 const unsigned int nb_sections
= 2 + (spec
->nb_resources
!= 0);
642 const unsigned int text_size
= (spec
->characteristics
& IMAGE_FILE_DLL
) ?
643 sizeof(dll_code_section
) : sizeof(exe_code_section
);
644 unsigned char *resources
;
645 unsigned int resources_size
;
646 unsigned int image_size
= 3 * section_align
;
648 resolve_imports( spec
);
649 output_bin_resources( spec
, 3 * section_align
);
650 resources
= output_buffer
;
651 resources_size
= output_buffer_pos
;
652 if (resources_size
) image_size
+= (resources_size
+ section_align
- 1) & ~(section_align
- 1);
654 init_output_buffer();
656 put_word( 0x5a4d ); /* e_magic */
657 put_word( 0x40 ); /* e_cblp */
658 put_word( 0x01 ); /* e_cp */
659 put_word( 0 ); /* e_crlc */
660 put_word( lfanew
/ 16 ); /* e_cparhdr */
661 put_word( 0x0000 ); /* e_minalloc */
662 put_word( 0xffff ); /* e_maxalloc */
663 put_word( 0x0000 ); /* e_ss */
664 put_word( 0x00b8 ); /* e_sp */
665 put_word( 0 ); /* e_csum */
666 put_word( 0 ); /* e_ip */
667 put_word( 0 ); /* e_cs */
668 put_word( lfanew
); /* e_lfarlc */
669 put_word( 0 ); /* e_ovno */
670 put_dword( 0 ); /* e_res */
672 put_word( 0 ); /* e_oemid */
673 put_word( 0 ); /* e_oeminfo */
674 put_dword( 0 ); /* e_res2 */
681 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
684 put_dword( 0x4550 ); /* Signature */
687 case CPU_x86
: put_word( IMAGE_FILE_MACHINE_I386
); break;
688 case CPU_x86_64
: put_word( IMAGE_FILE_MACHINE_AMD64
); break;
689 case CPU_POWERPC
: put_word( IMAGE_FILE_MACHINE_POWERPC
); break;
690 case CPU_ALPHA
: put_word( IMAGE_FILE_MACHINE_ALPHA
); break;
691 case CPU_SPARC
: put_word( IMAGE_FILE_MACHINE_UNKNOWN
); break;
692 case CPU_ARM
: put_word( IMAGE_FILE_MACHINE_ARM
); break;
694 put_word( nb_sections
); /* NumberOfSections */
695 put_dword( 0 ); /* TimeDateStamp */
696 put_dword( 0 ); /* PointerToSymbolTable */
697 put_dword( 0 ); /* NumberOfSymbols */
698 put_word( get_ptr_size() == 8 ?
699 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
:
700 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
); /* SizeOfOptionalHeader */
701 put_word( spec
->characteristics
); /* Characteristics */
702 put_word( get_ptr_size() == 8 ?
703 IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
704 IMAGE_NT_OPTIONAL_HDR32_MAGIC
); /* Magic */
705 put_byte( 0 ); /* MajorLinkerVersion */
706 put_byte( 0 ); /* MinorLinkerVersion */
707 put_dword( text_size
); /* SizeOfCode */
708 put_dword( 0 ); /* SizeOfInitializedData */
709 put_dword( 0 ); /* SizeOfUninitializedData */
710 put_dword( section_align
); /* AddressOfEntryPoint */
711 put_dword( section_align
); /* BaseOfCode */
712 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
713 put_pword( 0x10000000 ); /* ImageBase */
714 put_dword( section_align
); /* SectionAlignment */
715 put_dword( file_align
); /* FileAlignment */
716 put_word( 1 ); /* MajorOperatingSystemVersion */
717 put_word( 0 ); /* MinorOperatingSystemVersion */
718 put_word( 0 ); /* MajorImageVersion */
719 put_word( 0 ); /* MinorImageVersion */
720 put_word( spec
->subsystem_major
); /* MajorSubsystemVersion */
721 put_word( spec
->subsystem_minor
); /* MinorSubsystemVersion */
722 put_dword( 0 ); /* Win32VersionValue */
723 put_dword( image_size
); /* SizeOfImage */
724 put_dword( file_align
); /* SizeOfHeaders */
725 put_dword( 0 ); /* CheckSum */
726 put_word( spec
->subsystem
); /* Subsystem */
727 put_word( spec
->dll_characteristics
); /* DllCharacteristics */
728 put_pword( (spec
->stack_size
? spec
->stack_size
: 1024) * 1024 ); /* SizeOfStackReserve */
729 put_pword( page_size
); /* SizeOfStackCommit */
730 put_pword( (spec
->heap_size
? spec
->heap_size
: 1024) * 1024 ); /* SizeOfHeapReserve */
731 put_pword( page_size
); /* SizeOfHeapCommit */
732 put_dword( 0 ); /* LoaderFlags */
733 put_dword( 16 ); /* NumberOfRvaAndSizes */
735 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
736 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
737 if (resources_size
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
739 put_dword( 3 * section_align
);
740 put_dword( resources_size
);
748 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
749 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
750 put_dword( 2 * section_align
); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
751 put_dword( reloc_size
);
752 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
753 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
754 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
755 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
756 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
757 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
758 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
759 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
760 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
761 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
764 put_data( ".text\0\0", 8 ); /* Name */
765 put_dword( section_align
); /* VirtualSize */
766 put_dword( section_align
); /* VirtualAddress */
767 put_dword( text_size
); /* SizeOfRawData */
768 put_dword( file_align
); /* PointerToRawData */
769 put_dword( 0 ); /* PointerToRelocations */
770 put_dword( 0 ); /* PointerToLinenumbers */
771 put_word( 0 ); /* NumberOfRelocations */
772 put_word( 0 ); /* NumberOfLinenumbers */
773 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
776 put_data( ".reloc\0", 8 ); /* Name */
777 put_dword( section_align
); /* VirtualSize */
778 put_dword( 2 * section_align
);/* VirtualAddress */
779 put_dword( reloc_size
); /* SizeOfRawData */
780 put_dword( 2 * file_align
); /* PointerToRawData */
781 put_dword( 0 ); /* PointerToRelocations */
782 put_dword( 0 ); /* PointerToLinenumbers */
783 put_word( 0 ); /* NumberOfRelocations */
784 put_word( 0 ); /* NumberOfLinenumbers */
785 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
790 put_data( ".rsrc\0\0", 8 ); /* Name */
791 put_dword( (resources_size
+ section_align
- 1) & ~(section_align
- 1) ); /* VirtualSize */
792 put_dword( 3 * section_align
);/* VirtualAddress */
793 put_dword( resources_size
); /* SizeOfRawData */
794 put_dword( 3 * file_align
); /* PointerToRawData */
795 put_dword( 0 ); /* PointerToRelocations */
796 put_dword( 0 ); /* PointerToLinenumbers */
797 put_word( 0 ); /* NumberOfRelocations */
798 put_word( 0 ); /* NumberOfLinenumbers */
799 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
803 align_output( file_align
);
804 if (spec
->characteristics
& IMAGE_FILE_DLL
)
805 put_data( dll_code_section
, sizeof(dll_code_section
) );
807 put_data( exe_code_section
, sizeof(exe_code_section
) );
809 /* .reloc contents */
810 align_output( file_align
);
811 put_dword( 0 ); /* VirtualAddress */
812 put_dword( 0 ); /* SizeOfBlock */
817 align_output( file_align
);
818 put_data( resources
, resources_size
);
820 flush_output_buffer();
824 /*******************************************************************
827 * Build a Win32 def file from a spec file.
829 void BuildDef32File( DLLSPEC
*spec
)
835 output( "; File generated automatically from %s; do not edit!\n\n",
838 output( "; File generated automatically; do not edit!\n\n" );
840 output( "LIBRARY %s\n\n", spec
->file_name
);
841 output( "EXPORTS\n");
843 /* Output the exports and relay entry points */
845 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
847 const ORDDEF
*odp
= &spec
->entry_points
[i
];
852 if (odp
->name
) name
= odp
->name
;
853 else if (odp
->export_name
) name
= odp
->export_name
;
856 if (!(odp
->flags
& FLAG_PRIVATE
)) total
++;
865 /* try to reduce output */
866 output( " %s", name
);
867 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
868 output( "=%s", odp
->link_name
);
872 int at_param
= strlen(odp
->u
.func
.arg_types
) * get_ptr_size();
873 output( " %s", name
);
874 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
875 if (odp
->flags
& FLAG_FORWARD
)
877 output( "=%s", odp
->link_name
);
879 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
881 output( "=%s", odp
->link_name
);
882 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
888 int at_param
= strlen(odp
->u
.func
.arg_types
) * get_ptr_size();
890 if (!kill_at
) output( "@" );
891 output( "%s", name
);
892 if (!kill_at
) output( "@%d", at_param
);
893 if (odp
->flags
& FLAG_FORWARD
)
896 if (!kill_at
) output( "@" );
897 output( "%s", odp
->link_name
);
899 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
902 if (!kill_at
) output( "@" );
903 output( "%s", odp
->link_name
);
904 if (!kill_at
) output( "@%d", at_param
);
910 output( " %s", name
);
913 const char *check
= name
+ strlen(name
);
914 while (name
!= check
&&
915 '0' <= check
[-1] && check
[-1] <= '9')
919 if (name
!= check
&& check
!= name
+ strlen(name
) &&
922 output("%s", check
- 1);
925 if (odp
->name
|| odp
->export_name
)
927 output("=%s", make_internal_name( odp
, spec
, "stub" ));
934 output( " @%d", odp
->ordinal
);
935 if (!odp
->name
|| (odp
->flags
& FLAG_ORDINAL
)) output( " NONAME" );
936 if (is_data
) output( " DATA" );
937 if (odp
->flags
& FLAG_PRIVATE
) output( " PRIVATE" );
940 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);
944 /*******************************************************************
947 * Build a PE DLL C file from a spec file.
949 void BuildPedllFile( DLLSPEC
*spec
)
951 int i
, has_stubs
= 0;
953 output_standard_file_header();
955 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
957 const ORDDEF
*odp
= &spec
->entry_points
[i
];
958 if (odp
->type
== TYPE_STUB
)
967 output( "/* This file is intentionally left blank */\n");
971 output( "#include <stdarg.h>\n");
972 output( "#include \"windef.h\"\n");
973 output( "#include \"winbase.h\"\n");
974 output( "#include \"wine/config.h\"\n");
975 output( "#include \"wine/exception.h\"\n\n");
977 output( "void __wine_spec_unimplemented_stub( const char *module, const char *function )\n");
979 output( " ULONG_PTR args[2];\n");
981 output( " args[0] = (ULONG_PTR)module;\n");
982 output( " args[1] = (ULONG_PTR)function;\n");
983 output( " RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );\n");
986 output( "static const char __wine_spec_file_name[] = \"%s\";\n\n", spec
->file_name
);
988 /* Output the stub functions */
989 output_stub_funcs( spec
);