winebuild: Disallow register functions in 32-bit modules.
[wine.git] / tools / winebuild / spec32.c
blob6536cd8a8b6fa6a22660a70a54448c10053b9081
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_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 */
55 if (!odp) return 0;
56 /* skip non-functions */
57 switch (odp->type)
59 case TYPE_STDCALL:
60 case TYPE_CDECL:
61 case TYPE_THISCALL:
62 break;
63 case TYPE_STUB:
64 if (odp->u.func.nb_args != -1) break;
65 /* fall through */
66 default:
67 return 0;
69 /* skip norelay and forward entry points */
70 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
71 return 1;
74 static int is_float_arg( const ORDDEF *odp, int arg )
76 if (arg >= odp->u.func.nb_args) return 0;
77 return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
80 /* check if dll will output relay thunks */
81 static int has_relays( DLLSPEC *spec )
83 int i;
85 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64 &&
86 target_cpu != CPU_ARM && target_cpu != CPU_ARM64)
87 return 0;
89 for (i = spec->base; i <= spec->limit; i++)
91 ORDDEF *odp = spec->ordinals[i];
92 if (needs_relay( odp )) return 1;
94 return 0;
97 /*******************************************************************
98 * output_relay_debug
100 * Output entry points for relay debugging
102 static void output_relay_debug( DLLSPEC *spec )
104 int i, j;
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 );
119 else
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;
141 case ARG_INT64:
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();
168 flags = 0;
170 switch (target_cpu)
172 case CPU_x86:
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" );
178 flags |= 2;
180 output( "\tpushl %%esp\n" );
181 output_cfi( ".cfi_adjust_cfa_offset 4" );
183 if (odp->flags & FLAG_RET64) flags |= 1;
184 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
185 output_cfi( ".cfi_adjust_cfa_offset 4" );
187 if (UsePIC)
189 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
190 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
191 needs_get_pc_thunk = 1;
193 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
194 output( "\tpushl %%eax\n" );
195 output_cfi( ".cfi_adjust_cfa_offset 4" );
197 output( "\tcall *4(%%eax)\n" );
198 output_cfi( ".cfi_adjust_cfa_offset -12" );
199 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
200 output( "\tret $%u\n", args * get_ptr_size() );
201 else
202 output( "\tret\n" );
203 break;
205 case CPU_ARM:
207 unsigned int mask, val, count = 0;
208 unsigned int stack_size = min( 16, (args * 4 + 7) & ~7 );
210 if (odp->flags & FLAG_RET64) flags |= 1;
211 val = (flags << 24) | (args << 16) | (i - spec->base);
212 switch (stack_size)
214 case 16: output( "\tpush {r0-r3}\n" ); break;
215 case 8: output( "\tpush {r0-r1}\n" ); break;
216 case 0: break;
218 output( "\tpush {LR}\n" );
219 output( "\tmov r2, SP\n");
220 output( "\tsub SP, #4\n");
221 for (mask = 0xff; mask; mask <<= 8)
222 if (val & mask) output( "\t%s r1,#%u\n", count++ ? "add" : "mov", val & mask );
223 if (!count) output( "\tmov r1,#0\n" );
224 output( "\tldr r0, 2f\n");
225 output( "\tadd r0, PC\n");
226 output( "\tldr IP, [r0, #4]\n");
227 output( "1:\tblx IP\n");
228 output( "\tldr IP, [SP, #4]\n" );
229 output( "\tadd SP, #%u\n", stack_size + 8 );
230 output( "\tbx IP\n");
231 output( "2:\t.long .L__wine_spec_relay_descr-1b\n" );
232 break;
235 case CPU_ARM64:
236 switch (args)
238 default:
239 case 8:
240 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
241 /* fall through */
242 case 6:
243 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
244 /* fall through */
245 case 4:
246 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
247 /* fall through */
248 case 2:
249 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
250 /* fall through */
251 case 0: break;
253 output( "\tstp x29, x30, [SP,#-16]!\n" );
254 output( "\tstp x8, x9, [SP,#-16]!\n" );
255 output( "\tmov x2, SP\n");
256 if (odp->flags & FLAG_RET64) flags |= 1;
257 output( "\tmov w1, #%u\n", (flags << 24) );
258 if (args) output( "\tadd w1, w1, #%u\n", (args << 16) );
259 if (i - spec->base) output( "\tadd w1, w1, #%u\n", i - spec->base );
260 output( "\tldr x0, 1f\n");
261 output( "\tldr x3, [x0, #8]\n");
262 output( "\tblr x3\n");
263 output( "\tadd SP, SP, #16\n" );
264 output( "\tldp x29, x30, [SP], #16\n" );
265 if (args) output( "\tadd SP, SP, #%u\n", 8 * ((min(args, 8) + 1) & 0xe) );
266 output( "\tret\n");
267 output( "\t1: .quad .L__wine_spec_relay_descr\n");
268 break;
270 case CPU_x86_64:
271 output( "\tsubq $40,%%rsp\n" );
272 output_cfi( ".cfi_adjust_cfa_offset 40" );
273 switch (args)
275 default: output( "\tmovq %%%s,72(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
276 /* fall through */
277 case 3: output( "\tmovq %%%s,64(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
278 /* fall through */
279 case 2: output( "\tmovq %%%s,56(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
280 /* fall through */
281 case 1: output( "\tmovq %%%s,48(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
282 /* fall through */
283 case 0: break;
285 output( "\tleaq 40(%%rsp),%%r8\n" );
286 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
287 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
288 output( "\tcallq *8(%%rcx)\n" );
289 output( "\taddq $40,%%rsp\n" );
290 output_cfi( ".cfi_adjust_cfa_offset -40" );
291 output( "\tret\n" );
292 break;
294 default:
295 assert(0);
297 output_cfi( ".cfi_endproc" );
301 /*******************************************************************
302 * output_exports
304 * Output the export table for a Win32 module.
306 void output_exports( DLLSPEC *spec )
308 int i, fwd_size = 0;
309 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
311 if (!nr_exports) return;
313 output( "\n/* export table */\n\n" );
314 output( "\t.data\n" );
315 output( "\t.align %d\n", get_alignment(4) );
316 output( ".L__wine_spec_exports:\n" );
318 /* export directory header */
320 output( "\t.long 0\n" ); /* Characteristics */
321 output( "\t.long 0\n" ); /* TimeDateStamp */
322 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
323 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
324 output( "\t.long %u\n", spec->base ); /* Base */
325 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
326 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
327 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
328 if (spec->nb_names)
330 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
331 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
333 else
335 output( "\t.long 0\n" ); /* AddressOfNames */
336 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
339 /* output the function pointers */
341 output( "\n.L__wine_spec_exports_funcs:\n" );
342 for (i = spec->base; i <= spec->limit; i++)
344 ORDDEF *odp = spec->ordinals[i];
345 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
346 else switch(odp->type)
348 case TYPE_EXTERN:
349 case TYPE_STDCALL:
350 case TYPE_VARARGS:
351 case TYPE_CDECL:
352 case TYPE_THISCALL:
353 if (odp->flags & FLAG_FORWARD)
355 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
356 fwd_size += strlen(odp->link_name) + 1;
358 else if (odp->flags & FLAG_EXT_LINK)
360 output( "\t%s %s_%s\n",
361 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
363 else
365 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
367 break;
368 case TYPE_STUB:
369 output( "\t%s %s\n", get_asm_ptr_keyword(),
370 asm_name( get_stub_name( odp, spec )) );
371 break;
372 default:
373 assert(0);
377 if (spec->nb_names)
379 /* output the function name pointers */
381 int namepos = strlen(spec->file_name) + 1;
383 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
384 for (i = 0; i < spec->nb_names; i++)
386 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
387 namepos += strlen(spec->names[i]->name) + 1;
390 /* output the function ordinals */
392 output( "\n.L__wine_spec_exp_ordinals:\n" );
393 for (i = 0; i < spec->nb_names; i++)
395 output( "\t.short %d\n", spec->names[i]->ordinal - spec->base );
397 if (spec->nb_names % 2)
399 output( "\t.short 0\n" );
403 /* output the export name strings */
405 output( "\n.L__wine_spec_exp_names:\n" );
406 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
407 for (i = 0; i < spec->nb_names; i++)
408 output( "\t%s \"%s\"\n",
409 get_asm_string_keyword(), spec->names[i]->name );
411 /* output forward strings */
413 if (fwd_size)
415 output( "\n.L__wine_spec_forwards:\n" );
416 for (i = spec->base; i <= spec->limit; i++)
418 ORDDEF *odp = spec->ordinals[i];
419 if (odp && (odp->flags & FLAG_FORWARD))
420 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
423 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
424 output( ".L__wine_spec_exports_end:\n" );
426 /* output relays */
428 if (!has_relays( spec ))
430 output( "\t%s 0\n", get_asm_ptr_keyword() );
431 return;
434 output( ".L__wine_spec_relay_descr:\n" );
435 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
436 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
437 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
438 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
439 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
440 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
442 output_relay_debug( spec );
446 /*******************************************************************
447 * output_asm_constructor
449 * Output code for calling a dll constructor.
451 static void output_asm_constructor( const char *constructor )
453 if (target_platform == PLATFORM_APPLE)
455 /* Mach-O doesn't have an init section */
456 output( "\n\t.mod_init_func\n" );
457 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
458 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(constructor) );
460 else
462 switch(target_cpu)
464 case CPU_x86:
465 case CPU_x86_64:
466 output( "\n\t.section \".init\",\"ax\"\n" );
467 output( "\tcall %s\n", asm_name(constructor) );
468 break;
469 case CPU_ARM:
470 output( "\n\t.section \".text\",\"ax\"\n" );
471 output( "\tblx %s\n", asm_name(constructor) );
472 break;
473 case CPU_ARM64:
474 case CPU_POWERPC:
475 output( "\n\t.section \".init\",\"ax\"\n" );
476 output( "\tbl %s\n", asm_name(constructor) );
477 break;
483 /*******************************************************************
484 * output_module
486 * Output the module data.
488 void output_module( DLLSPEC *spec )
490 int machine = 0;
491 unsigned int page_size = get_page_size();
493 /* Reserve some space for the PE header */
495 switch (target_platform)
497 case PLATFORM_APPLE:
498 output( "\t.text\n" );
499 output( "\t.align %d\n", get_alignment(page_size) );
500 output( "__wine_spec_pe_header:\n" );
501 output( "\t.space 65536\n" );
502 break;
503 case PLATFORM_SOLARIS:
504 output( "\n\t.section \".text\",\"ax\"\n" );
505 output( "__wine_spec_pe_header:\n" );
506 output( "\t.skip %u\n", 65536 + page_size );
507 break;
508 default:
509 switch(target_cpu)
511 case CPU_x86:
512 case CPU_x86_64:
513 output( "\n\t.section \".init\",\"ax\"\n" );
514 output( "\tjmp 1f\n" );
515 break;
516 case CPU_ARM:
517 output( "\n\t.section \".text\",\"ax\"\n" );
518 output( "\tb 1f\n" );
519 break;
520 case CPU_ARM64:
521 case CPU_POWERPC:
522 output( "\n\t.section \".init\",\"ax\"\n" );
523 output( "\tb 1f\n" );
524 break;
526 output( "__wine_spec_pe_header:\n" );
527 output( "\t.skip %u\n", 65536 + page_size );
528 output( "1:\n" );
529 break;
532 /* Output the NT header */
534 output( "\n\t.data\n" );
535 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
536 output( "%s\n", asm_globl("__wine_spec_nt_header") );
537 output( ".L__wine_spec_rva_base:\n" );
539 output( "\t.long 0x4550\n" ); /* Signature */
540 switch(target_cpu)
542 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
543 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
544 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
545 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
546 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
548 output( "\t.short 0x%04x\n", /* Machine */
549 machine );
550 output( "\t.short 0\n" ); /* NumberOfSections */
551 output( "\t.long 0\n" ); /* TimeDateStamp */
552 output( "\t.long 0\n" ); /* PointerToSymbolTable */
553 output( "\t.long 0\n" ); /* NumberOfSymbols */
554 output( "\t.short %d\n", /* SizeOfOptionalHeader */
555 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
556 output( "\t.short 0x%04x\n", /* Characteristics */
557 spec->characteristics );
558 output( "\t.short 0x%04x\n", /* Magic */
559 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
560 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
561 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
562 output( "\t.long 0\n" ); /* SizeOfCode */
563 output( "\t.long 0\n" ); /* SizeOfInitializedData */
564 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
565 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
566 output( "\t%s %s\n", /* AddressOfEntryPoint */
567 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
568 if (get_ptr_size() == 4)
570 output( "\t.long 0\n" ); /* BaseOfCode */
571 output( "\t.long 0\n" ); /* BaseOfData */
573 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
574 get_asm_ptr_keyword() );
575 output( "\t.long %u\n", page_size ); /* SectionAlignment */
576 output( "\t.long %u\n", page_size ); /* FileAlignment */
577 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
578 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
579 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
580 spec->subsystem_major, spec->subsystem_minor );
581 output( "\t.long 0\n" ); /* Win32VersionValue */
582 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
583 asm_name("_end") );
584 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
585 output( "\t.long 0\n" ); /* CheckSum */
586 output( "\t.short 0x%04x\n", /* Subsystem */
587 spec->subsystem );
588 output( "\t.short 0x%04x\n", /* DllCharacteristics */
589 spec->dll_characteristics );
590 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
591 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
592 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
593 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
594 output( "\t.long 0\n" ); /* LoaderFlags */
595 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
597 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
598 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
599 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
600 else
601 output( "\t.long 0,0\n" );
603 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
604 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
605 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
606 else
607 output( "\t.long 0,0\n" );
609 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
610 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
611 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
612 else
613 output( "\t.long 0,0\n" );
615 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
616 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
617 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
618 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
619 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
620 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
621 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
622 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
623 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
624 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
625 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
626 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
627 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
629 output( "\n\t%s\n", get_asm_string_section() );
630 output( "%s\n", asm_globl("__wine_spec_file_name") );
631 output( ".L__wine_spec_file_name:\n" );
632 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
633 if (target_platform == PLATFORM_APPLE)
634 output( "\t.lcomm %s,4\n", asm_name("_end") );
636 output_asm_constructor( "__wine_spec_init_ctor" );
640 /*******************************************************************
641 * BuildSpec32File
643 * Build a Win32 C file from a spec file.
645 void BuildSpec32File( DLLSPEC *spec )
647 needs_get_pc_thunk = 0;
648 resolve_imports( spec );
649 output_standard_file_header();
650 output_module( spec );
651 output_stubs( spec );
652 output_exports( spec );
653 output_imports( spec );
654 if (is_undefined( "__wine_call_from_regs" )) output_asm_relays();
655 if (needs_get_pc_thunk) output_get_pc_thunk();
656 output_resources( spec );
657 output_gnu_stack_note();
661 /*******************************************************************
662 * output_fake_module
664 * Build a fake binary module from a spec file.
666 void output_fake_module( DLLSPEC *spec )
668 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
669 0xc2, 0x0c, 0x00 }; /* ret $12 */
671 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
672 0xc2, 0x04, 0x00 }; /* ret $4 */
674 static const char fakedll_signature[] = "Wine placeholder DLL";
675 const unsigned int page_size = get_page_size();
676 const unsigned int section_align = page_size;
677 const unsigned int file_align = 0x200;
678 const unsigned int reloc_size = 8;
679 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
680 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
681 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
682 sizeof(dll_code_section) : sizeof(exe_code_section);
683 unsigned char *resources;
684 unsigned int resources_size;
685 unsigned int image_size = 3 * section_align;
687 resolve_imports( spec );
688 output_bin_resources( spec, 3 * section_align );
689 resources = output_buffer;
690 resources_size = output_buffer_pos;
691 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
693 init_output_buffer();
695 put_word( 0x5a4d ); /* e_magic */
696 put_word( 0x40 ); /* e_cblp */
697 put_word( 0x01 ); /* e_cp */
698 put_word( 0 ); /* e_crlc */
699 put_word( lfanew / 16 ); /* e_cparhdr */
700 put_word( 0x0000 ); /* e_minalloc */
701 put_word( 0xffff ); /* e_maxalloc */
702 put_word( 0x0000 ); /* e_ss */
703 put_word( 0x00b8 ); /* e_sp */
704 put_word( 0 ); /* e_csum */
705 put_word( 0 ); /* e_ip */
706 put_word( 0 ); /* e_cs */
707 put_word( lfanew ); /* e_lfarlc */
708 put_word( 0 ); /* e_ovno */
709 put_dword( 0 ); /* e_res */
710 put_dword( 0 );
711 put_word( 0 ); /* e_oemid */
712 put_word( 0 ); /* e_oeminfo */
713 put_dword( 0 ); /* e_res2 */
714 put_dword( 0 );
715 put_dword( 0 );
716 put_dword( 0 );
717 put_dword( 0 );
718 put_dword( lfanew );
720 put_data( fakedll_signature, sizeof(fakedll_signature) );
721 align_output( 16 );
723 put_dword( 0x4550 ); /* Signature */
724 switch(target_cpu)
726 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
727 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
728 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
729 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
730 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
732 put_word( nb_sections ); /* NumberOfSections */
733 put_dword( 0 ); /* TimeDateStamp */
734 put_dword( 0 ); /* PointerToSymbolTable */
735 put_dword( 0 ); /* NumberOfSymbols */
736 put_word( get_ptr_size() == 8 ?
737 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
738 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
739 put_word( spec->characteristics ); /* Characteristics */
740 put_word( get_ptr_size() == 8 ?
741 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
742 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
743 put_byte( 7 ); /* MajorLinkerVersion */
744 put_byte( 10 ); /* MinorLinkerVersion */
745 put_dword( text_size ); /* SizeOfCode */
746 put_dword( 0 ); /* SizeOfInitializedData */
747 put_dword( 0 ); /* SizeOfUninitializedData */
748 put_dword( section_align ); /* AddressOfEntryPoint */
749 put_dword( section_align ); /* BaseOfCode */
750 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
751 put_pword( 0x10000000 ); /* ImageBase */
752 put_dword( section_align ); /* SectionAlignment */
753 put_dword( file_align ); /* FileAlignment */
754 put_word( 1 ); /* MajorOperatingSystemVersion */
755 put_word( 0 ); /* MinorOperatingSystemVersion */
756 put_word( 0 ); /* MajorImageVersion */
757 put_word( 0 ); /* MinorImageVersion */
758 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
759 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
760 put_dword( 0 ); /* Win32VersionValue */
761 put_dword( image_size ); /* SizeOfImage */
762 put_dword( file_align ); /* SizeOfHeaders */
763 put_dword( 0 ); /* CheckSum */
764 put_word( spec->subsystem ); /* Subsystem */
765 put_word( spec->dll_characteristics ); /* DllCharacteristics */
766 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
767 put_pword( page_size ); /* SizeOfStackCommit */
768 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
769 put_pword( page_size ); /* SizeOfHeapCommit */
770 put_dword( 0 ); /* LoaderFlags */
771 put_dword( 16 ); /* NumberOfRvaAndSizes */
773 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
774 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
775 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
777 put_dword( 3 * section_align );
778 put_dword( resources_size );
780 else
782 put_dword( 0 );
783 put_dword( 0 );
786 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
787 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
788 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
789 put_dword( reloc_size );
790 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
791 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
792 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
793 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
794 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
795 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
796 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
797 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
798 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
799 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
801 /* .text section */
802 put_data( ".text\0\0", 8 ); /* Name */
803 put_dword( section_align ); /* VirtualSize */
804 put_dword( section_align ); /* VirtualAddress */
805 put_dword( text_size ); /* SizeOfRawData */
806 put_dword( file_align ); /* PointerToRawData */
807 put_dword( 0 ); /* PointerToRelocations */
808 put_dword( 0 ); /* PointerToLinenumbers */
809 put_word( 0 ); /* NumberOfRelocations */
810 put_word( 0 ); /* NumberOfLinenumbers */
811 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
813 /* .reloc section */
814 put_data( ".reloc\0", 8 ); /* Name */
815 put_dword( section_align ); /* VirtualSize */
816 put_dword( 2 * section_align );/* VirtualAddress */
817 put_dword( reloc_size ); /* SizeOfRawData */
818 put_dword( 2 * file_align ); /* PointerToRawData */
819 put_dword( 0 ); /* PointerToRelocations */
820 put_dword( 0 ); /* PointerToLinenumbers */
821 put_word( 0 ); /* NumberOfRelocations */
822 put_word( 0 ); /* NumberOfLinenumbers */
823 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
825 /* .rsrc section */
826 if (resources_size)
828 put_data( ".rsrc\0\0", 8 ); /* Name */
829 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
830 put_dword( 3 * section_align );/* VirtualAddress */
831 put_dword( resources_size ); /* SizeOfRawData */
832 put_dword( 3 * file_align ); /* PointerToRawData */
833 put_dword( 0 ); /* PointerToRelocations */
834 put_dword( 0 ); /* PointerToLinenumbers */
835 put_word( 0 ); /* NumberOfRelocations */
836 put_word( 0 ); /* NumberOfLinenumbers */
837 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
840 /* .text contents */
841 align_output( file_align );
842 if (spec->characteristics & IMAGE_FILE_DLL)
843 put_data( dll_code_section, sizeof(dll_code_section) );
844 else
845 put_data( exe_code_section, sizeof(exe_code_section) );
847 /* .reloc contents */
848 align_output( file_align );
849 put_dword( 0 ); /* VirtualAddress */
850 put_dword( 0 ); /* SizeOfBlock */
852 /* .rsrc contents */
853 if (resources_size)
855 align_output( file_align );
856 put_data( resources, resources_size );
858 flush_output_buffer();
862 /*******************************************************************
863 * output_def_file
865 * Build a Win32 def file from a spec file.
867 void output_def_file( DLLSPEC *spec, int include_private )
869 DLLSPEC *spec32 = NULL;
870 const char *name;
871 int i, total;
873 if (spec->type == SPEC_WIN16)
875 spec32 = alloc_dll_spec();
876 add_16bit_exports( spec32, spec );
877 spec = spec32;
880 if (spec_file_name)
881 output( "; File generated automatically from %s; do not edit!\n\n",
882 spec_file_name );
883 else
884 output( "; File generated automatically; do not edit!\n\n" );
886 output( "LIBRARY %s\n\n", spec->file_name);
887 output( "EXPORTS\n");
889 /* Output the exports and relay entry points */
891 for (i = total = 0; i < spec->nb_entry_points; i++)
893 const ORDDEF *odp = &spec->entry_points[i];
894 int is_data = 0;
896 if (odp->name) name = odp->name;
897 else if (odp->export_name) name = odp->export_name;
898 else continue;
900 if (!(odp->flags & FLAG_PRIVATE)) total++;
901 else if (!include_private) continue;
903 if (odp->type == TYPE_STUB) continue;
905 output( " %s", name );
907 switch(odp->type)
909 case TYPE_EXTERN:
910 is_data = 1;
911 /* fall through */
912 case TYPE_VARARGS:
913 case TYPE_CDECL:
914 case TYPE_THISCALL:
915 /* try to reduce output */
916 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
917 output( "=%s", odp->link_name );
918 break;
919 case TYPE_STDCALL:
921 int at_param = get_args_size( odp );
922 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
923 if (odp->flags & FLAG_FORWARD)
925 output( "=%s", odp->link_name );
927 else if (strcmp(name, odp->link_name)) /* try to reduce output */
929 output( "=%s", odp->link_name );
930 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
932 break;
934 default:
935 assert(0);
937 output( " @%d", odp->ordinal );
938 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
939 if (is_data) output( " DATA" );
940 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
941 output( "\n" );
943 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
944 if (spec32) free_dll_spec( spec32 );