ntdll: Display floating point arguments in relay debugging on i386 and x86-64.
[wine.git] / tools / winebuild / spec32.c
blobd028726bc16fc8a892739c75b86b99047050e21b
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 static int cmp_func_args( const void *p1, const void *p2 )
99 const ORDDEF *odp1 = *(const ORDDEF **)p1;
100 const ORDDEF *odp2 = *(const ORDDEF **)p2;
102 return odp2->u.func.nb_args - odp1->u.func.nb_args;
105 static void get_arg_string( ORDDEF *odp, char str[MAX_ARGUMENTS + 1] )
107 int i;
109 for (i = 0; i < odp->u.func.nb_args; i++)
111 switch (odp->u.func.args[i])
113 case ARG_STR: str[i] = 's'; break;
114 case ARG_WSTR: str[i] = 'w'; break;
115 case ARG_FLOAT: str[i] = 'f'; break;
116 case ARG_DOUBLE: str[i] = 'd'; break;
117 case ARG_INT64:
118 case ARG_INT128:
119 if (get_ptr_size() == 4)
121 str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
122 break;
124 /* fall through */
125 case ARG_LONG:
126 case ARG_PTR:
127 default:
128 str[i] = 'i';
129 break;
132 if (target_cpu == CPU_x86 && odp->type == TYPE_THISCALL) str[0] = 't';
134 /* append return value */
135 if (get_ptr_size() == 4 && (odp->flags & FLAG_RET64))
136 strcpy( str + i, "J" );
137 else
138 strcpy( str + i, "I" );
141 /*******************************************************************
142 * build_args_string
144 static char *build_args_string( DLLSPEC *spec )
146 int i, count = 0, len = 1;
147 char *p, *buffer;
148 char str[MAX_ARGUMENTS + 2];
149 ORDDEF **funcs;
151 funcs = xmalloc( (spec->limit + 1 - spec->base) * sizeof(*funcs) );
152 for (i = spec->base; i <= spec->limit; i++)
154 ORDDEF *odp = spec->ordinals[i];
156 if (!needs_relay( odp )) continue;
157 funcs[count++] = odp;
158 len += odp->u.func.nb_args + 1;
160 /* sort functions by decreasing number of arguments */
161 qsort( funcs, count, sizeof(*funcs), cmp_func_args );
162 buffer = xmalloc( len );
163 buffer[0] = 0;
164 /* build the arguments string, reusing substrings where possible */
165 for (i = 0; i < count; i++)
167 get_arg_string( funcs[i], str );
168 if (!(p = strstr( buffer, str )))
170 p = buffer + strlen( buffer );
171 strcpy( p, str );
173 funcs[i]->u.func.args_str_offset = p - buffer;
175 free( funcs );
176 return buffer;
179 /*******************************************************************
180 * output_relay_debug
182 * Output entry points for relay debugging
184 static void output_relay_debug( DLLSPEC *spec )
186 int i;
188 /* first the table of entry point offsets */
190 output( "\t%s\n", get_asm_rodata_section() );
191 output( "\t.align %d\n", get_alignment(4) );
192 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
194 for (i = spec->base; i <= spec->limit; i++)
196 ORDDEF *odp = spec->ordinals[i];
198 if (needs_relay( odp ))
199 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
200 else
201 output( "\t.long 0\n" );
204 /* then the strings of argument types */
206 output( ".L__wine_spec_relay_args_string:\n" );
207 output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( spec ));
209 /* then the relay thunks */
211 output( "\t.text\n" );
212 output( "__wine_spec_relay_entry_points:\n" );
213 output( "\tnop\n" ); /* to avoid 0 offset */
215 for (i = spec->base; i <= spec->limit; i++)
217 ORDDEF *odp = spec->ordinals[i];
219 if (!needs_relay( odp )) continue;
221 output( "\t.align %d\n", get_alignment(4) );
222 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
223 output_cfi( ".cfi_startproc" );
225 switch (target_cpu)
227 case CPU_x86:
228 if (odp->type == TYPE_THISCALL) /* add the this pointer */
230 output( "\tpopl %%eax\n" );
231 output( "\tpushl %%ecx\n" );
232 output( "\tpushl %%eax\n" );
234 output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
235 output_cfi( ".cfi_adjust_cfa_offset 4" );
237 if (UsePIC)
239 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
240 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
241 needs_get_pc_thunk = 1;
243 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
244 output( "\tpushl %%eax\n" );
245 output_cfi( ".cfi_adjust_cfa_offset 4" );
247 output( "\tcall *4(%%eax)\n" );
248 output_cfi( ".cfi_adjust_cfa_offset -8" );
249 if (odp->type == TYPE_STDCALL || odp->type == TYPE_THISCALL)
250 output( "\tret $%u\n", get_args_size( odp ));
251 else
252 output( "\tret\n" );
253 break;
255 case CPU_ARM:
257 unsigned int mask, val, count = 0;
258 unsigned int stack_size = min( 16, (get_args_size( odp ) + 7) & ~7 );
260 val = (odp->u.func.args_str_offset << 16) | (i - spec->base);
261 switch (stack_size)
263 case 16: output( "\tpush {r0-r3}\n" ); break;
264 case 8: output( "\tpush {r0-r1}\n" ); break;
265 case 0: break;
267 output( "\tmov r2, SP\n");
268 output( "\tpush {LR}\n" );
269 output( "\tsub SP, #4\n");
270 for (mask = 0xff; mask; mask <<= 8)
271 if (val & mask) output( "\t%s r1,#%u\n", count++ ? "add" : "mov", val & mask );
272 if (!count) output( "\tmov r1,#0\n" );
273 output( "\tldr r0, 2f\n");
274 output( "\tadd r0, PC\n");
275 output( "\tldr IP, [r0, #4]\n");
276 output( "1:\tblx IP\n");
277 output( "\tldr IP, [SP, #4]\n" );
278 output( "\tadd SP, #%u\n", stack_size + 8 );
279 output( "\tbx IP\n");
280 output( "2:\t.long .L__wine_spec_relay_descr-1b\n" );
281 break;
284 case CPU_ARM64:
285 switch (odp->u.func.nb_args)
287 default:
288 case 8:
289 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
290 /* fall through */
291 case 6:
292 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
293 /* fall through */
294 case 4:
295 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
296 /* fall through */
297 case 2:
298 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
299 /* fall through */
300 case 0: break;
302 output( "\tmov x2, SP\n");
303 output( "\tstp x29, x30, [SP,#-16]!\n" );
304 output( "\tstp x8, x9, [SP,#-16]!\n" );
305 output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
306 if (i - spec->base) output( "\tadd w1, w1, #%u\n", i - spec->base );
307 output( "\tadrp x0, .L__wine_spec_relay_descr\n");
308 output( "\tadd x0, x0, #:lo12:.L__wine_spec_relay_descr\n");
309 output( "\tldr x3, [x0, #8]\n");
310 output( "\tblr x3\n");
311 output( "\tadd SP, SP, #16\n" );
312 output( "\tldp x29, x30, [SP], #16\n" );
313 if (odp->u.func.nb_args)
314 output( "\tadd SP, SP, #%u\n", 8 * ((min(odp->u.func.nb_args, 8) + 1) & ~1) );
315 output( "\tret\n");
316 break;
318 case CPU_x86_64:
319 switch (odp->u.func.nb_args)
321 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
322 /* fall through */
323 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
324 /* fall through */
325 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
326 /* fall through */
327 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
328 /* fall through */
329 case 0: break;
331 output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
332 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
333 output( "\tcallq *8(%%rcx)\n" );
334 output( "\tret\n" );
335 break;
337 default:
338 assert(0);
340 output_cfi( ".cfi_endproc" );
344 /*******************************************************************
345 * output_exports
347 * Output the export table for a Win32 module.
349 void output_exports( DLLSPEC *spec )
351 int i, fwd_size = 0;
352 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
354 if (!nr_exports) return;
356 output( "\n/* export table */\n\n" );
357 output( "\t.data\n" );
358 output( "\t.align %d\n", get_alignment(4) );
359 output( ".L__wine_spec_exports:\n" );
361 /* export directory header */
363 output( "\t.long 0\n" ); /* Characteristics */
364 output( "\t.long 0\n" ); /* TimeDateStamp */
365 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
366 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
367 output( "\t.long %u\n", spec->base ); /* Base */
368 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
369 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
370 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
371 if (spec->nb_names)
373 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
374 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
376 else
378 output( "\t.long 0\n" ); /* AddressOfNames */
379 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
382 /* output the function pointers */
384 output( "\n.L__wine_spec_exports_funcs:\n" );
385 for (i = spec->base; i <= spec->limit; i++)
387 ORDDEF *odp = spec->ordinals[i];
388 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
389 else switch(odp->type)
391 case TYPE_EXTERN:
392 case TYPE_STDCALL:
393 case TYPE_VARARGS:
394 case TYPE_CDECL:
395 case TYPE_THISCALL:
396 if (odp->flags & FLAG_FORWARD)
398 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
399 fwd_size += strlen(odp->link_name) + 1;
401 else if (odp->flags & FLAG_EXT_LINK)
403 output( "\t%s %s_%s\n",
404 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
406 else
408 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
410 break;
411 case TYPE_STUB:
412 output( "\t%s %s\n", get_asm_ptr_keyword(),
413 asm_name( get_stub_name( odp, spec )) );
414 break;
415 default:
416 assert(0);
420 if (spec->nb_names)
422 /* output the function name pointers */
424 int namepos = strlen(spec->file_name) + 1;
426 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
427 for (i = 0; i < spec->nb_names; i++)
429 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
430 namepos += strlen(spec->names[i]->name) + 1;
433 /* output the function ordinals */
435 output( "\n.L__wine_spec_exp_ordinals:\n" );
436 for (i = 0; i < spec->nb_names; i++)
438 output( "\t.short %d\n", spec->names[i]->ordinal - spec->base );
440 if (spec->nb_names % 2)
442 output( "\t.short 0\n" );
446 /* output the export name strings */
448 output( "\n.L__wine_spec_exp_names:\n" );
449 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
450 for (i = 0; i < spec->nb_names; i++)
451 output( "\t%s \"%s\"\n",
452 get_asm_string_keyword(), spec->names[i]->name );
454 /* output forward strings */
456 if (fwd_size)
458 output( "\n.L__wine_spec_forwards:\n" );
459 for (i = spec->base; i <= spec->limit; i++)
461 ORDDEF *odp = spec->ordinals[i];
462 if (odp && (odp->flags & FLAG_FORWARD))
463 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
466 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
467 output( ".L__wine_spec_exports_end:\n" );
469 /* output relays */
471 if (!has_relays( spec ))
473 output( "\t%s 0\n", get_asm_ptr_keyword() );
474 return;
477 output( ".L__wine_spec_relay_descr:\n" );
478 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
479 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
480 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
481 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
482 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
483 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
485 output_relay_debug( spec );
489 /*******************************************************************
490 * output_asm_constructor
492 * Output code for calling a dll constructor.
494 static void output_asm_constructor( const char *constructor )
496 if (target_platform == PLATFORM_APPLE)
498 /* Mach-O doesn't have an init section */
499 output( "\n\t.mod_init_func\n" );
500 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
501 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(constructor) );
503 else
505 switch(target_cpu)
507 case CPU_x86:
508 case CPU_x86_64:
509 output( "\n\t.section \".init\",\"ax\"\n" );
510 output( "\tcall %s\n", asm_name(constructor) );
511 break;
512 case CPU_ARM:
513 output( "\n\t.section \".text\",\"ax\"\n" );
514 output( "\tblx %s\n", asm_name(constructor) );
515 break;
516 case CPU_ARM64:
517 case CPU_POWERPC:
518 output( "\n\t.section \".init\",\"ax\"\n" );
519 output( "\tbl %s\n", asm_name(constructor) );
520 break;
526 /*******************************************************************
527 * output_module
529 * Output the module data.
531 void output_module( DLLSPEC *spec )
533 int machine = 0;
534 unsigned int page_size = get_page_size();
536 /* Reserve some space for the PE header */
538 switch (target_platform)
540 case PLATFORM_APPLE:
541 output( "\t.text\n" );
542 output( "\t.align %d\n", get_alignment(page_size) );
543 output( "__wine_spec_pe_header:\n" );
544 output( "\t.space 65536\n" );
545 break;
546 case PLATFORM_SOLARIS:
547 output( "\n\t.section \".text\",\"ax\"\n" );
548 output( "__wine_spec_pe_header:\n" );
549 output( "\t.skip %u\n", 65536 + page_size );
550 break;
551 default:
552 switch(target_cpu)
554 case CPU_x86:
555 case CPU_x86_64:
556 output( "\n\t.section \".init\",\"ax\"\n" );
557 output( "\tjmp 1f\n" );
558 break;
559 case CPU_ARM:
560 output( "\n\t.section \".text\",\"ax\"\n" );
561 output( "\tb 1f\n" );
562 break;
563 case CPU_ARM64:
564 case CPU_POWERPC:
565 output( "\n\t.section \".init\",\"ax\"\n" );
566 output( "\tb 1f\n" );
567 break;
569 output( "__wine_spec_pe_header:\n" );
570 output( "\t.skip %u\n", 65536 + page_size );
571 output( "1:\n" );
572 break;
575 /* Output the NT header */
577 output( "\n\t.data\n" );
578 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
579 output( "%s\n", asm_globl("__wine_spec_nt_header") );
580 output( ".L__wine_spec_rva_base:\n" );
582 output( "\t.long 0x4550\n" ); /* Signature */
583 switch(target_cpu)
585 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
586 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
587 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
588 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
589 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
591 output( "\t.short 0x%04x\n", /* Machine */
592 machine );
593 output( "\t.short 0\n" ); /* NumberOfSections */
594 output( "\t.long 0\n" ); /* TimeDateStamp */
595 output( "\t.long 0\n" ); /* PointerToSymbolTable */
596 output( "\t.long 0\n" ); /* NumberOfSymbols */
597 output( "\t.short %d\n", /* SizeOfOptionalHeader */
598 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
599 output( "\t.short 0x%04x\n", /* Characteristics */
600 spec->characteristics );
601 output( "\t.short 0x%04x\n", /* Magic */
602 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
603 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
604 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
605 output( "\t.long 0\n" ); /* SizeOfCode */
606 output( "\t.long 0\n" ); /* SizeOfInitializedData */
607 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
608 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
609 output( "\t%s %s\n", /* AddressOfEntryPoint */
610 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
611 if (get_ptr_size() == 4)
613 output( "\t.long 0\n" ); /* BaseOfCode */
614 output( "\t.long 0\n" ); /* BaseOfData */
616 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
617 get_asm_ptr_keyword() );
618 output( "\t.long %u\n", page_size ); /* SectionAlignment */
619 output( "\t.long %u\n", page_size ); /* FileAlignment */
620 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
621 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
622 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
623 spec->subsystem_major, spec->subsystem_minor );
624 output( "\t.long 0\n" ); /* Win32VersionValue */
625 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
626 asm_name("_end") );
627 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
628 output( "\t.long 0\n" ); /* CheckSum */
629 output( "\t.short 0x%04x\n", /* Subsystem */
630 spec->subsystem );
631 output( "\t.short 0x%04x\n", /* DllCharacteristics */
632 spec->dll_characteristics );
633 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
634 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
635 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
636 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
637 output( "\t.long 0\n" ); /* LoaderFlags */
638 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
640 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
641 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
642 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
643 else
644 output( "\t.long 0,0\n" );
646 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
647 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
648 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
649 else
650 output( "\t.long 0,0\n" );
652 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
653 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
654 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
655 else
656 output( "\t.long 0,0\n" );
658 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
659 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
660 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
661 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
662 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
663 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
664 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
665 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
666 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
667 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
668 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
669 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
670 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
672 output( "\n\t%s\n", get_asm_string_section() );
673 output( "%s\n", asm_globl("__wine_spec_file_name") );
674 output( ".L__wine_spec_file_name:\n" );
675 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
676 if (target_platform == PLATFORM_APPLE)
677 output( "\t.lcomm %s,4\n", asm_name("_end") );
679 output_asm_constructor( "__wine_spec_init_ctor" );
683 /*******************************************************************
684 * BuildSpec32File
686 * Build a Win32 C file from a spec file.
688 void BuildSpec32File( DLLSPEC *spec )
690 needs_get_pc_thunk = 0;
691 resolve_imports( spec );
692 output_standard_file_header();
693 output_module( spec );
694 output_stubs( spec );
695 output_exports( spec );
696 output_imports( spec );
697 if (needs_get_pc_thunk) output_get_pc_thunk();
698 output_resources( spec );
699 output_gnu_stack_note();
703 /*******************************************************************
704 * output_fake_module
706 * Build a fake binary module from a spec file.
708 void output_fake_module( DLLSPEC *spec )
710 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
711 0xc2, 0x0c, 0x00 }; /* ret $12 */
713 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
714 0xc2, 0x04, 0x00 }; /* ret $4 */
716 static const char fakedll_signature[] = "Wine placeholder DLL";
717 const unsigned int page_size = get_page_size();
718 const unsigned int section_align = page_size;
719 const unsigned int file_align = 0x200;
720 const unsigned int reloc_size = 8;
721 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
722 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
723 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
724 sizeof(dll_code_section) : sizeof(exe_code_section);
725 unsigned char *resources;
726 unsigned int resources_size;
727 unsigned int image_size = 3 * section_align;
729 resolve_imports( spec );
730 output_bin_resources( spec, 3 * section_align );
731 resources = output_buffer;
732 resources_size = output_buffer_pos;
733 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
735 init_output_buffer();
737 put_word( 0x5a4d ); /* e_magic */
738 put_word( 0x40 ); /* e_cblp */
739 put_word( 0x01 ); /* e_cp */
740 put_word( 0 ); /* e_crlc */
741 put_word( lfanew / 16 ); /* e_cparhdr */
742 put_word( 0x0000 ); /* e_minalloc */
743 put_word( 0xffff ); /* e_maxalloc */
744 put_word( 0x0000 ); /* e_ss */
745 put_word( 0x00b8 ); /* e_sp */
746 put_word( 0 ); /* e_csum */
747 put_word( 0 ); /* e_ip */
748 put_word( 0 ); /* e_cs */
749 put_word( lfanew ); /* e_lfarlc */
750 put_word( 0 ); /* e_ovno */
751 put_dword( 0 ); /* e_res */
752 put_dword( 0 );
753 put_word( 0 ); /* e_oemid */
754 put_word( 0 ); /* e_oeminfo */
755 put_dword( 0 ); /* e_res2 */
756 put_dword( 0 );
757 put_dword( 0 );
758 put_dword( 0 );
759 put_dword( 0 );
760 put_dword( lfanew );
762 put_data( fakedll_signature, sizeof(fakedll_signature) );
763 align_output( 16 );
765 put_dword( 0x4550 ); /* Signature */
766 switch(target_cpu)
768 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
769 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
770 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
771 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
772 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
774 put_word( nb_sections ); /* NumberOfSections */
775 put_dword( 0 ); /* TimeDateStamp */
776 put_dword( 0 ); /* PointerToSymbolTable */
777 put_dword( 0 ); /* NumberOfSymbols */
778 put_word( get_ptr_size() == 8 ?
779 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
780 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
781 put_word( spec->characteristics ); /* Characteristics */
782 put_word( get_ptr_size() == 8 ?
783 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
784 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
785 put_byte( 7 ); /* MajorLinkerVersion */
786 put_byte( 10 ); /* MinorLinkerVersion */
787 put_dword( text_size ); /* SizeOfCode */
788 put_dword( 0 ); /* SizeOfInitializedData */
789 put_dword( 0 ); /* SizeOfUninitializedData */
790 put_dword( section_align ); /* AddressOfEntryPoint */
791 put_dword( section_align ); /* BaseOfCode */
792 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
793 put_pword( 0x10000000 ); /* ImageBase */
794 put_dword( section_align ); /* SectionAlignment */
795 put_dword( file_align ); /* FileAlignment */
796 put_word( 1 ); /* MajorOperatingSystemVersion */
797 put_word( 0 ); /* MinorOperatingSystemVersion */
798 put_word( 0 ); /* MajorImageVersion */
799 put_word( 0 ); /* MinorImageVersion */
800 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
801 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
802 put_dword( 0 ); /* Win32VersionValue */
803 put_dword( image_size ); /* SizeOfImage */
804 put_dword( file_align ); /* SizeOfHeaders */
805 put_dword( 0 ); /* CheckSum */
806 put_word( spec->subsystem ); /* Subsystem */
807 put_word( spec->dll_characteristics ); /* DllCharacteristics */
808 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
809 put_pword( page_size ); /* SizeOfStackCommit */
810 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
811 put_pword( page_size ); /* SizeOfHeapCommit */
812 put_dword( 0 ); /* LoaderFlags */
813 put_dword( 16 ); /* NumberOfRvaAndSizes */
815 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
816 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
817 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
819 put_dword( 3 * section_align );
820 put_dword( resources_size );
822 else
824 put_dword( 0 );
825 put_dword( 0 );
828 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
829 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
830 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
831 put_dword( reloc_size );
832 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
833 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
834 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
835 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
836 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
837 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
838 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
839 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
840 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
841 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
843 /* .text section */
844 put_data( ".text\0\0", 8 ); /* Name */
845 put_dword( section_align ); /* VirtualSize */
846 put_dword( section_align ); /* VirtualAddress */
847 put_dword( text_size ); /* SizeOfRawData */
848 put_dword( file_align ); /* PointerToRawData */
849 put_dword( 0 ); /* PointerToRelocations */
850 put_dword( 0 ); /* PointerToLinenumbers */
851 put_word( 0 ); /* NumberOfRelocations */
852 put_word( 0 ); /* NumberOfLinenumbers */
853 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
855 /* .reloc section */
856 put_data( ".reloc\0", 8 ); /* Name */
857 put_dword( section_align ); /* VirtualSize */
858 put_dword( 2 * section_align );/* VirtualAddress */
859 put_dword( reloc_size ); /* SizeOfRawData */
860 put_dword( 2 * file_align ); /* PointerToRawData */
861 put_dword( 0 ); /* PointerToRelocations */
862 put_dword( 0 ); /* PointerToLinenumbers */
863 put_word( 0 ); /* NumberOfRelocations */
864 put_word( 0 ); /* NumberOfLinenumbers */
865 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
867 /* .rsrc section */
868 if (resources_size)
870 put_data( ".rsrc\0\0", 8 ); /* Name */
871 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
872 put_dword( 3 * section_align );/* VirtualAddress */
873 put_dword( resources_size ); /* SizeOfRawData */
874 put_dword( 3 * file_align ); /* PointerToRawData */
875 put_dword( 0 ); /* PointerToRelocations */
876 put_dword( 0 ); /* PointerToLinenumbers */
877 put_word( 0 ); /* NumberOfRelocations */
878 put_word( 0 ); /* NumberOfLinenumbers */
879 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
882 /* .text contents */
883 align_output( file_align );
884 if (spec->characteristics & IMAGE_FILE_DLL)
885 put_data( dll_code_section, sizeof(dll_code_section) );
886 else
887 put_data( exe_code_section, sizeof(exe_code_section) );
889 /* .reloc contents */
890 align_output( file_align );
891 put_dword( 0 ); /* VirtualAddress */
892 put_dword( 0 ); /* SizeOfBlock */
894 /* .rsrc contents */
895 if (resources_size)
897 align_output( file_align );
898 put_data( resources, resources_size );
900 flush_output_buffer();
904 /*******************************************************************
905 * output_def_file
907 * Build a Win32 def file from a spec file.
909 void output_def_file( DLLSPEC *spec, int include_private )
911 DLLSPEC *spec32 = NULL;
912 const char *name;
913 int i, total;
915 if (spec->type == SPEC_WIN16)
917 spec32 = alloc_dll_spec();
918 add_16bit_exports( spec32, spec );
919 spec = spec32;
922 if (spec_file_name)
923 output( "; File generated automatically from %s; do not edit!\n\n",
924 spec_file_name );
925 else
926 output( "; File generated automatically; do not edit!\n\n" );
928 output( "LIBRARY %s\n\n", spec->file_name);
929 output( "EXPORTS\n");
931 /* Output the exports and relay entry points */
933 for (i = total = 0; i < spec->nb_entry_points; i++)
935 const ORDDEF *odp = &spec->entry_points[i];
936 int is_data = 0;
938 if (odp->name) name = odp->name;
939 else if (odp->export_name) name = odp->export_name;
940 else continue;
942 if (!(odp->flags & FLAG_PRIVATE)) total++;
943 else if (!include_private) continue;
945 if (odp->type == TYPE_STUB) continue;
947 output( " %s", name );
949 switch(odp->type)
951 case TYPE_EXTERN:
952 is_data = 1;
953 /* fall through */
954 case TYPE_VARARGS:
955 case TYPE_CDECL:
956 case TYPE_THISCALL:
957 /* try to reduce output */
958 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
959 output( "=%s", odp->link_name );
960 break;
961 case TYPE_STDCALL:
963 int at_param = get_args_size( odp );
964 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
965 if (odp->flags & FLAG_FORWARD)
967 output( "=%s", odp->link_name );
969 else if (strcmp(name, odp->link_name)) /* try to reduce output */
971 output( "=%s", odp->link_name );
972 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
974 break;
976 default:
977 assert(0);
979 output( " @%d", odp->ordinal );
980 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
981 if (is_data) output( " DATA" );
982 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
983 output( "\n" );
985 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
986 if (spec32) free_dll_spec( spec32 );