mapi32: Check folder for NULL (Coverity).
[wine/hacks.git] / tools / winebuild / spec32.c
blob09e69f575a122285a43bff8b6232e08439402a97
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_ALPHA 0x0184
38 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
39 #define IMAGE_FILE_MACHINE_AMD64 0x8664
41 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
42 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
44 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
45 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
46 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
48 /* check if entry point needs a relay thunk */
49 static inline int needs_relay( const ORDDEF *odp )
51 /* skip nonexistent entry points */
52 if (!odp) return 0;
53 /* skip non-functions */
54 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) return 0;
55 /* skip norelay and forward entry points */
56 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
57 return 1;
60 /* check if dll will output relay thunks */
61 int has_relays( DLLSPEC *spec )
63 int i;
65 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
67 for (i = spec->base; i <= spec->limit; i++)
69 ORDDEF *odp = spec->ordinals[i];
70 if (needs_relay( odp )) return 1;
72 return 0;
75 /*******************************************************************
76 * output_relay_debug
78 * Output entry points for relay debugging
80 static void output_relay_debug( DLLSPEC *spec )
82 int i;
83 unsigned int j, args, flags;
85 /* first the table of entry point offsets */
87 output( "\t%s\n", get_asm_rodata_section() );
88 output( "\t.align %d\n", get_alignment(4) );
89 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
91 for (i = spec->base; i <= spec->limit; i++)
93 ORDDEF *odp = spec->ordinals[i];
95 if (needs_relay( odp ))
96 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
97 else
98 output( "\t.long 0\n" );
101 /* then the table of argument types */
103 output( "\t.align %d\n", get_alignment(4) );
104 output( ".L__wine_spec_relay_arg_types:\n" );
106 for (i = spec->base; i <= spec->limit; i++)
108 ORDDEF *odp = spec->ordinals[i];
109 unsigned int mask = 0;
111 if (needs_relay( odp ))
113 for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
115 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
116 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
119 output( "\t.long 0x%08x\n", mask );
122 /* then the relay thunks */
124 output( "\t.text\n" );
125 output( "__wine_spec_relay_entry_points:\n" );
126 output( "\tnop\n" ); /* to avoid 0 offset */
128 for (i = spec->base; i <= spec->limit; i++)
130 ORDDEF *odp = spec->ordinals[i];
132 if (!needs_relay( odp )) continue;
134 output( "\t.align %d\n", get_alignment(4) );
135 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
137 args = strlen(odp->u.func.arg_types);
138 flags = 0;
140 switch (target_cpu)
142 case CPU_x86:
143 if (odp->flags & FLAG_REGISTER)
144 output( "\tpushl %%eax\n" );
145 else
146 output( "\tpushl %%esp\n" );
148 if (odp->flags & FLAG_RET64) flags |= 1;
149 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
151 if (UsePIC)
153 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
154 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
156 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
157 output( "\tpushl %%eax\n" );
159 if (odp->flags & FLAG_REGISTER)
161 output( "\tcall *8(%%eax)\n" );
163 else
165 output( "\tcall *4(%%eax)\n" );
166 if (odp->type == TYPE_STDCALL)
167 output( "\tret $%u\n", args * get_ptr_size() );
168 else
169 output( "\tret\n" );
171 break;
173 case CPU_x86_64:
174 output( "\t.cfi_startproc\n" );
175 output( "\tsubq $40,%%rsp\n" );
176 output( "\t.cfi_adjust_cfa_offset 40\n" );
177 output( "\tmovq %%rcx,48(%%rsp)\n" );
178 output( "\tmovq %%rdx,56(%%rsp)\n" );
179 output( "\tmovq %%r8,64(%%rsp)\n" );
180 output( "\tmovq %%r9,72(%%rsp)\n" );
181 output( "\tleaq 40(%%rsp),%%r8\n" );
182 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
183 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
184 output( "\tcallq *%u(%%rcx)\n", (odp->flags & FLAG_REGISTER) ? 16 : 8 );
185 output( "\taddq $40,%%rsp\n" );
186 output( "\t.cfi_adjust_cfa_offset -40\n" );
187 output( "\tret\n" );
188 output( "\t.cfi_endproc\n" );
189 break;
191 default:
192 assert(0);
197 /*******************************************************************
198 * output_exports
200 * Output the export table for a Win32 module.
202 void output_exports( DLLSPEC *spec )
204 int i, fwd_size = 0;
205 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
207 if (!nr_exports) return;
209 output( "\n/* export table */\n\n" );
210 output( "\t.data\n" );
211 output( "\t.align %d\n", get_alignment(4) );
212 output( ".L__wine_spec_exports:\n" );
214 /* export directory header */
216 output( "\t.long 0\n" ); /* Characteristics */
217 output( "\t.long 0\n" ); /* TimeDateStamp */
218 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
219 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
220 output( "\t.long %u\n", spec->base ); /* Base */
221 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
222 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
223 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
224 if (spec->nb_names)
226 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
227 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
229 else
231 output( "\t.long 0\n" ); /* AddressOfNames */
232 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
235 /* output the function pointers */
237 output( "\n.L__wine_spec_exports_funcs:\n" );
238 for (i = spec->base; i <= spec->limit; i++)
240 ORDDEF *odp = spec->ordinals[i];
241 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
242 else switch(odp->type)
244 case TYPE_EXTERN:
245 case TYPE_STDCALL:
246 case TYPE_VARARGS:
247 case TYPE_CDECL:
248 if (odp->flags & FLAG_FORWARD)
250 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
251 fwd_size += strlen(odp->link_name) + 1;
253 else if (odp->flags & FLAG_EXT_LINK)
255 output( "\t%s %s_%s\n",
256 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
258 else
260 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
262 break;
263 case TYPE_STUB:
264 output( "\t%s %s\n", get_asm_ptr_keyword(),
265 asm_name( get_stub_name( odp, spec )) );
266 break;
267 default:
268 assert(0);
272 if (spec->nb_names)
274 /* output the function name pointers */
276 int namepos = strlen(spec->file_name) + 1;
278 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
279 for (i = 0; i < spec->nb_names; i++)
281 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
282 namepos += strlen(spec->names[i]->name) + 1;
285 /* output the function ordinals */
287 output( "\n.L__wine_spec_exp_ordinals:\n" );
288 for (i = 0; i < spec->nb_names; i++)
290 output( "\t%s %d\n",
291 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
293 if (spec->nb_names % 2)
295 output( "\t%s 0\n", get_asm_short_keyword() );
299 /* output the export name strings */
301 output( "\n.L__wine_spec_exp_names:\n" );
302 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
303 for (i = 0; i < spec->nb_names; i++)
304 output( "\t%s \"%s\"\n",
305 get_asm_string_keyword(), spec->names[i]->name );
307 /* output forward strings */
309 if (fwd_size)
311 output( "\n.L__wine_spec_forwards:\n" );
312 for (i = spec->base; i <= spec->limit; i++)
314 ORDDEF *odp = spec->ordinals[i];
315 if (odp && (odp->flags & FLAG_FORWARD))
316 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
319 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
320 output( ".L__wine_spec_exports_end:\n" );
322 /* output relays */
324 if (!has_relays( spec ))
326 output( "\t%s 0\n", get_asm_ptr_keyword() );
327 return;
330 output( ".L__wine_spec_relay_descr:\n" );
331 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
332 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
333 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
334 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
335 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
336 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
338 output_relay_debug( spec );
342 /*******************************************************************
343 * output_asm_constructor
345 * Output code for calling a dll constructor.
347 static void output_asm_constructor( const char *constructor )
349 if (target_platform == PLATFORM_APPLE)
351 /* Mach-O doesn't have an init section */
352 output( "\n\t.mod_init_func\n" );
353 output( "\t.align %d\n", get_alignment(4) );
354 output( "\t.long %s\n", asm_name(constructor) );
356 else
358 output( "\n\t.section \".init\",\"ax\"\n" );
359 switch(target_cpu)
361 case CPU_x86:
362 case CPU_x86_64:
363 output( "\tcall %s\n", asm_name(constructor) );
364 break;
365 case CPU_SPARC:
366 output( "\tcall %s\n", asm_name(constructor) );
367 output( "\tnop\n" );
368 break;
369 case CPU_ALPHA:
370 output( "\tjsr $26,%s\n", asm_name(constructor) );
371 break;
372 case CPU_POWERPC:
373 output( "\tbl %s\n", asm_name(constructor) );
374 break;
380 /*******************************************************************
381 * output_module
383 * Output the module data.
385 void output_module( DLLSPEC *spec )
387 int machine = 0;
388 unsigned int page_size = get_page_size();
390 /* Reserve some space for the PE header */
392 switch (target_platform)
394 case PLATFORM_APPLE:
395 output( "\t.text\n" );
396 output( "\t.align %d\n", get_alignment(page_size) );
397 output( "__wine_spec_pe_header:\n" );
398 output( "\t.space 65536\n" );
399 break;
400 case PLATFORM_SOLARIS:
401 output( "\n\t.section \".text\",\"ax\"\n" );
402 output( "__wine_spec_pe_header:\n" );
403 output( "\t.skip %u\n", 65536 + page_size );
404 break;
405 default:
406 output( "\n\t.section \".init\",\"ax\"\n" );
407 switch(target_cpu)
409 case CPU_x86:
410 case CPU_x86_64:
411 case CPU_ALPHA:
412 case CPU_SPARC:
413 output( "\tjmp 1f\n" );
414 break;
415 case CPU_POWERPC:
416 output( "\tb 1f\n" );
417 break;
419 output( "__wine_spec_pe_header:\n" );
420 output( "\t.skip %u\n", 65536 + page_size );
421 output( "1:\n" );
422 break;
425 /* Output the NT header */
427 output( "\n\t.data\n" );
428 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
429 output( "%s\n", asm_globl("__wine_spec_nt_header") );
430 output( ".L__wine_spec_rva_base:\n" );
432 output( "\t.long 0x4550\n" ); /* Signature */
433 switch(target_cpu)
435 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
436 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
437 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
438 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
439 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
441 output( "\t%s 0x%04x\n", /* Machine */
442 get_asm_short_keyword(), machine );
443 output( "\t%s 0\n", /* NumberOfSections */
444 get_asm_short_keyword() );
445 output( "\t.long 0\n" ); /* TimeDateStamp */
446 output( "\t.long 0\n" ); /* PointerToSymbolTable */
447 output( "\t.long 0\n" ); /* NumberOfSymbols */
448 output( "\t%s %d\n", /* SizeOfOptionalHeader */
449 get_asm_short_keyword(),
450 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
451 output( "\t%s 0x%04x\n", /* Characteristics */
452 get_asm_short_keyword(), spec->characteristics );
453 output( "\t%s 0x%04x\n", /* Magic */
454 get_asm_short_keyword(),
455 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
456 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
457 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
458 output( "\t.long 0\n" ); /* SizeOfCode */
459 output( "\t.long 0\n" ); /* SizeOfInitializedData */
460 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
461 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
462 output( "\t%s %s\n", /* AddressOfEntryPoint */
463 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
464 if (get_ptr_size() == 4)
466 output( "\t.long 0\n" ); /* BaseOfCode */
467 output( "\t.long 0\n" ); /* BaseOfData */
469 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
470 get_asm_ptr_keyword() );
471 output( "\t.long %u\n", page_size ); /* SectionAlignment */
472 output( "\t.long %u\n", page_size ); /* FileAlignment */
473 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
474 get_asm_short_keyword() );
475 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
476 get_asm_short_keyword() );
477 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
478 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
479 output( "\t.long 0\n" ); /* Win32VersionValue */
480 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
481 asm_name("_end") );
482 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
483 output( "\t.long 0\n" ); /* CheckSum */
484 output( "\t%s 0x%04x\n", /* Subsystem */
485 get_asm_short_keyword(), spec->subsystem );
486 output( "\t%s 0x%04x\n", /* DllCharacteristics */
487 get_asm_short_keyword(), spec->dll_characteristics );
488 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
489 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
490 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
491 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
492 output( "\t.long 0\n" ); /* LoaderFlags */
493 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
495 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
496 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
497 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
498 else
499 output( "\t.long 0,0\n" );
501 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
502 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
503 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
504 else
505 output( "\t.long 0,0\n" );
507 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
508 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
509 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
510 else
511 output( "\t.long 0,0\n" );
513 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
514 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
515 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
516 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
517 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
518 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
519 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
520 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
521 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
522 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
523 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
524 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
525 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
527 output( "\n\t%s\n", get_asm_string_section() );
528 output( "%s\n", asm_globl("__wine_spec_file_name") );
529 output( ".L__wine_spec_file_name:\n" );
530 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
531 if (target_platform == PLATFORM_APPLE)
532 output( "\t.lcomm %s,4\n", asm_name("_end") );
534 output_asm_constructor( "__wine_spec_init_ctor" );
538 /*******************************************************************
539 * BuildSpec32File
541 * Build a Win32 C file from a spec file.
543 void BuildSpec32File( DLLSPEC *spec )
545 resolve_imports( spec );
546 output_standard_file_header();
547 output_module( spec );
548 output_stubs( spec );
549 output_exports( spec );
550 output_imports( spec );
551 output_resources( spec );
552 output_gnu_stack_note();
556 /*******************************************************************
557 * output_fake_module
559 * Build a fake binary module from a spec file.
561 void output_fake_module( DLLSPEC *spec )
563 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
564 0xc2, 0x0c, 0x00 }; /* ret $12 */
566 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
567 0xc2, 0x04, 0x00 }; /* ret $4 */
569 static const char fakedll_signature[] = "Wine placeholder DLL";
570 const unsigned int page_size = get_page_size();
571 const unsigned int section_align = page_size;
572 const unsigned int file_align = 0x200;
573 const unsigned int reloc_size = 8;
574 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
575 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
576 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
577 sizeof(dll_code_section) : sizeof(exe_code_section);
578 unsigned char *resources;
579 unsigned int resources_size;
580 unsigned int image_size = 3 * section_align;
582 resolve_imports( spec );
583 output_bin_resources( spec, 3 * section_align );
584 resources = output_buffer;
585 resources_size = output_buffer_pos;
586 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
588 init_output_buffer();
590 put_word( 0x5a4d ); /* e_magic */
591 put_word( 0x40 ); /* e_cblp */
592 put_word( 0x01 ); /* e_cp */
593 put_word( 0 ); /* e_crlc */
594 put_word( lfanew / 16 ); /* e_cparhdr */
595 put_word( 0x0000 ); /* e_minalloc */
596 put_word( 0xffff ); /* e_maxalloc */
597 put_word( 0x0000 ); /* e_ss */
598 put_word( 0x00b8 ); /* e_sp */
599 put_word( 0 ); /* e_csum */
600 put_word( 0 ); /* e_ip */
601 put_word( 0 ); /* e_cs */
602 put_word( lfanew ); /* e_lfarlc */
603 put_word( 0 ); /* e_ovno */
604 put_dword( 0 ); /* e_res */
605 put_dword( 0 );
606 put_word( 0 ); /* e_oemid */
607 put_word( 0 ); /* e_oeminfo */
608 put_dword( 0 ); /* e_res2 */
609 put_dword( 0 );
610 put_dword( 0 );
611 put_dword( 0 );
612 put_dword( 0 );
613 put_dword( lfanew );
615 put_data( fakedll_signature, sizeof(fakedll_signature) );
616 align_output( 16 );
618 put_dword( 0x4550 ); /* Signature */
619 switch(target_cpu)
621 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
622 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
623 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
624 case CPU_ALPHA: put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
625 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
627 put_word( nb_sections ); /* NumberOfSections */
628 put_dword( 0 ); /* TimeDateStamp */
629 put_dword( 0 ); /* PointerToSymbolTable */
630 put_dword( 0 ); /* NumberOfSymbols */
631 put_word( get_ptr_size() == 8 ?
632 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
633 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
634 put_word( spec->characteristics ); /* Characteristics */
635 put_word( get_ptr_size() == 8 ?
636 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
637 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
638 put_byte( 0 ); /* MajorLinkerVersion */
639 put_byte( 0 ); /* MinorLinkerVersion */
640 put_dword( text_size ); /* SizeOfCode */
641 put_dword( 0 ); /* SizeOfInitializedData */
642 put_dword( 0 ); /* SizeOfUninitializedData */
643 put_dword( section_align ); /* AddressOfEntryPoint */
644 put_dword( section_align ); /* BaseOfCode */
645 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
646 put_pword( 0x10000000 ); /* ImageBase */
647 put_dword( section_align ); /* SectionAlignment */
648 put_dword( file_align ); /* FileAlignment */
649 put_word( 1 ); /* MajorOperatingSystemVersion */
650 put_word( 0 ); /* MinorOperatingSystemVersion */
651 put_word( 0 ); /* MajorImageVersion */
652 put_word( 0 ); /* MinorImageVersion */
653 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
654 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
655 put_dword( 0 ); /* Win32VersionValue */
656 put_dword( image_size ); /* SizeOfImage */
657 put_dword( file_align ); /* SizeOfHeaders */
658 put_dword( 0 ); /* CheckSum */
659 put_word( spec->subsystem ); /* Subsystem */
660 put_word( spec->dll_characteristics ); /* DllCharacteristics */
661 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
662 put_pword( page_size ); /* SizeOfStackCommit */
663 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
664 put_pword( page_size ); /* SizeOfHeapCommit */
665 put_dword( 0 ); /* LoaderFlags */
666 put_dword( 16 ); /* NumberOfRvaAndSizes */
668 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
669 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
670 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
672 put_dword( 3 * section_align );
673 put_dword( resources_size );
675 else
677 put_dword( 0 );
678 put_dword( 0 );
681 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
682 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
683 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
684 put_dword( reloc_size );
685 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
686 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
687 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
688 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
689 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
690 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
691 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
692 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
693 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
694 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
696 /* .text section */
697 put_data( ".text\0\0", 8 ); /* Name */
698 put_dword( section_align ); /* VirtualSize */
699 put_dword( section_align ); /* VirtualAddress */
700 put_dword( text_size ); /* SizeOfRawData */
701 put_dword( file_align ); /* PointerToRawData */
702 put_dword( 0 ); /* PointerToRelocations */
703 put_dword( 0 ); /* PointerToLinenumbers */
704 put_word( 0 ); /* NumberOfRelocations */
705 put_word( 0 ); /* NumberOfLinenumbers */
706 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
708 /* .reloc section */
709 put_data( ".reloc\0", 8 ); /* Name */
710 put_dword( section_align ); /* VirtualSize */
711 put_dword( 2 * section_align );/* VirtualAddress */
712 put_dword( reloc_size ); /* SizeOfRawData */
713 put_dword( 2 * file_align ); /* PointerToRawData */
714 put_dword( 0 ); /* PointerToRelocations */
715 put_dword( 0 ); /* PointerToLinenumbers */
716 put_word( 0 ); /* NumberOfRelocations */
717 put_word( 0 ); /* NumberOfLinenumbers */
718 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
720 /* .rsrc section */
721 if (resources_size)
723 put_data( ".rsrc\0\0", 8 ); /* Name */
724 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
725 put_dword( 3 * section_align );/* VirtualAddress */
726 put_dword( resources_size ); /* SizeOfRawData */
727 put_dword( 3 * file_align ); /* PointerToRawData */
728 put_dword( 0 ); /* PointerToRelocations */
729 put_dword( 0 ); /* PointerToLinenumbers */
730 put_word( 0 ); /* NumberOfRelocations */
731 put_word( 0 ); /* NumberOfLinenumbers */
732 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
735 /* .text contents */
736 align_output( file_align );
737 if (spec->characteristics & IMAGE_FILE_DLL)
738 put_data( dll_code_section, sizeof(dll_code_section) );
739 else
740 put_data( exe_code_section, sizeof(exe_code_section) );
742 /* .reloc contents */
743 align_output( file_align );
744 put_dword( 0 ); /* VirtualAddress */
745 put_dword( 0 ); /* SizeOfBlock */
747 /* .rsrc contents */
748 if (resources_size)
750 align_output( file_align );
751 put_data( resources, resources_size );
753 flush_output_buffer();
757 /*******************************************************************
758 * BuildDef32File
760 * Build a Win32 def file from a spec file.
762 void BuildDef32File( DLLSPEC *spec )
764 const char *name;
765 int i, total;
767 if (spec_file_name)
768 output( "; File generated automatically from %s; do not edit!\n\n",
769 spec_file_name );
770 else
771 output( "; File generated automatically; do not edit!\n\n" );
773 output( "LIBRARY %s\n\n", spec->file_name);
774 output( "EXPORTS\n");
776 /* Output the exports and relay entry points */
778 for (i = total = 0; i < spec->nb_entry_points; i++)
780 const ORDDEF *odp = &spec->entry_points[i];
781 int is_data = 0;
783 if (!odp) continue;
785 if (odp->name) name = odp->name;
786 else if (odp->export_name) name = odp->export_name;
787 else continue;
789 if (!(odp->flags & FLAG_PRIVATE)) total++;
791 if (odp->type == TYPE_STUB) continue;
793 output( " %s", name );
795 switch(odp->type)
797 case TYPE_EXTERN:
798 is_data = 1;
799 /* fall through */
800 case TYPE_VARARGS:
801 case TYPE_CDECL:
802 /* try to reduce output */
803 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
804 output( "=%s", odp->link_name );
805 break;
806 case TYPE_STDCALL:
808 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
809 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
810 if (odp->flags & FLAG_FORWARD)
812 output( "=%s", odp->link_name );
814 else if (strcmp(name, odp->link_name)) /* try to reduce output */
816 output( "=%s", odp->link_name );
817 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
819 break;
821 default:
822 assert(0);
824 output( " @%d", odp->ordinal );
825 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
826 if (is_data) output( " DATA" );
827 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
828 output( "\n" );
830 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );