vkd3d: Disable printf format checks.
[wine.git] / tools / winebuild / spec32.c
blob6e3a094131078ff4f3b151a952051d72e178c581
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"
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <string.h>
32 #include "build.h"
34 #define IMAGE_FILE_MACHINE_UNKNOWN 0
35 #define IMAGE_FILE_MACHINE_I386 0x014c
36 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
37 #define IMAGE_FILE_MACHINE_AMD64 0x8664
38 #define IMAGE_FILE_MACHINE_ARMNT 0x01C4
39 #define IMAGE_FILE_MACHINE_ARM64 0xaa64
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 int needs_get_pc_thunk = 0;
50 static const char builtin_signature[32] = "Wine builtin DLL";
51 static const char fakedll_signature[32] = "Wine placeholder DLL";
52 static struct strarray spec_extra_ld_symbols = { 0 }; /* list of extra symbols that ld should resolve */
54 /* add a symbol to the list of extra symbols that ld must resolve */
55 void add_spec_extra_ld_symbol( const char *name )
57 strarray_add( &spec_extra_ld_symbols, name );
60 static unsigned int hash_filename( const char *name )
62 /* FNV-1 hash */
63 unsigned int ret = 2166136261u;
64 while (*name) ret = (ret * 16777619) ^ *name++;
65 return ret;
68 /* check if entry point needs a relay thunk */
69 static inline int needs_relay( const ORDDEF *odp )
71 /* skip nonexistent entry points */
72 if (!odp) return 0;
73 /* skip non-functions */
74 switch (odp->type)
76 case TYPE_STDCALL:
77 case TYPE_CDECL:
78 break;
79 case TYPE_STUB:
80 if (odp->u.func.nb_args != -1) break;
81 /* fall through */
82 default:
83 return 0;
85 /* skip norelay and forward entry points */
86 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
87 return 1;
90 static int is_float_arg( const ORDDEF *odp, int arg )
92 if (arg >= odp->u.func.nb_args) return 0;
93 return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
96 /* check if dll will output relay thunks */
97 static int has_relays( DLLSPEC *spec )
99 int i;
101 if (target.cpu == CPU_ARM64EC) return 0;
103 for (i = spec->base; i <= spec->limit; i++)
105 ORDDEF *odp = spec->ordinals[i];
106 if (needs_relay( odp )) return 1;
108 return 0;
111 static int get_exports_count( DLLSPEC *spec )
113 if (spec->base > spec->limit) return 0;
114 return spec->limit - spec->base + 1;
117 static int cmp_func_args( const void *p1, const void *p2 )
119 const ORDDEF *odp1 = *(const ORDDEF **)p1;
120 const ORDDEF *odp2 = *(const ORDDEF **)p2;
122 return odp2->u.func.nb_args - odp1->u.func.nb_args;
125 static void get_arg_string( ORDDEF *odp, char str[MAX_ARGUMENTS + 1] )
127 int i;
129 for (i = 0; i < odp->u.func.nb_args; i++)
131 switch (odp->u.func.args[i])
133 case ARG_STR: str[i] = 's'; break;
134 case ARG_WSTR: str[i] = 'w'; break;
135 case ARG_FLOAT: str[i] = 'f'; break;
136 case ARG_DOUBLE: str[i] = 'd'; break;
137 case ARG_INT64:
138 case ARG_INT128:
139 if (get_ptr_size() == 4)
141 str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
142 break;
144 /* fall through */
145 case ARG_LONG:
146 case ARG_PTR:
147 default:
148 str[i] = 'i';
149 break;
152 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) str[0] = 't';
153 if ((odp->flags & FLAG_FASTCALL) && odp->u.func.nb_args > 1) str[1] = 't';
155 /* append return value */
156 if (get_ptr_size() == 4 && (odp->flags & FLAG_RET64))
157 strcpy( str + i, "J" );
158 else
159 strcpy( str + i, "I" );
162 static void output_data_directories( const char *names[16] )
164 int i;
166 for (i = 0; i < 16; i++)
168 if (names[i])
170 output_rva( "%s", names[i] );
171 output( "\t.long %s_end - %s\n", names[i], names[i] );
173 else output( "\t.long 0,0\n" );
177 /*******************************************************************
178 * build_args_string
180 static char *build_args_string( DLLSPEC *spec )
182 int i, count = 0, len = 1;
183 char *p, *buffer;
184 char str[MAX_ARGUMENTS + 2];
185 ORDDEF **funcs;
187 funcs = xmalloc( (spec->limit + 1 - spec->base) * sizeof(*funcs) );
188 for (i = spec->base; i <= spec->limit; i++)
190 ORDDEF *odp = spec->ordinals[i];
192 if (!needs_relay( odp )) continue;
193 funcs[count++] = odp;
194 len += odp->u.func.nb_args + 1;
196 /* sort functions by decreasing number of arguments */
197 qsort( funcs, count, sizeof(*funcs), cmp_func_args );
198 buffer = xmalloc( len );
199 buffer[0] = 0;
200 /* build the arguments string, reusing substrings where possible */
201 for (i = 0; i < count; i++)
203 get_arg_string( funcs[i], str );
204 if (!(p = strstr( buffer, str )))
206 p = buffer + strlen( buffer );
207 strcpy( p, str );
209 funcs[i]->u.func.args_str_offset = p - buffer;
211 free( funcs );
212 return buffer;
215 /*******************************************************************
216 * output_relay_debug
218 * Output entry points for relay debugging
220 static void output_relay_debug( DLLSPEC *spec )
222 int i;
224 /* first the table of entry point offsets */
226 output( "\t%s\n", get_asm_rodata_section() );
227 output( "\t.balign 4\n" );
228 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
230 for (i = spec->base; i <= spec->limit; i++)
232 ORDDEF *odp = spec->ordinals[i];
234 if (needs_relay( odp ))
235 output( "\t.long __wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
236 else
237 output( "\t.long 0\n" );
240 /* then the strings of argument types */
242 output( ".L__wine_spec_relay_args_string:\n" );
243 output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( spec ));
245 /* then the relay thunks */
247 output( "\t.text\n" );
248 if (thumb_mode) output( "\t.thumb_func\n" );
249 output( "__wine_spec_relay_entry_points:\n" );
250 output( "\tnop\n" ); /* to avoid 0 offset */
252 for (i = spec->base; i <= spec->limit; i++)
254 ORDDEF *odp = spec->ordinals[i];
256 if (!needs_relay( odp )) continue;
258 switch (target.cpu)
260 case CPU_i386:
261 output( "\t.balign 4\n" );
262 output( "\t.long 0x90909090,0x90909090\n" );
263 output( "__wine_spec_relay_entry_point_%d:\n", i );
264 output_cfi( ".cfi_startproc" );
265 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
266 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) /* add the register arguments */
268 output( "\tpopl %%eax\n" );
269 if ((odp->flags & FLAG_FASTCALL) && get_args_size( odp ) > 4) output( "\tpushl %%edx\n" );
270 output( "\tpushl %%ecx\n" );
271 output( "\tpushl %%eax\n" );
273 output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
274 output_cfi( ".cfi_adjust_cfa_offset 4" );
276 if (UsePIC)
278 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
279 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
280 needs_get_pc_thunk = 1;
282 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
283 output( "\tpushl %%eax\n" );
284 output_cfi( ".cfi_adjust_cfa_offset 4" );
286 output( "\tcall *4(%%eax)\n" );
287 output_cfi( ".cfi_adjust_cfa_offset -8" );
288 if (odp->type == TYPE_STDCALL)
289 output( "\tret $%u\n", get_args_size( odp ));
290 else
291 output( "\tret\n" );
292 output_cfi( ".cfi_endproc" );
293 break;
295 case CPU_ARM:
297 int j, has_float = 0;
299 if (strcmp( float_abi_option, "soft" ))
300 for (j = 0; j < odp->u.func.nb_args && !has_float; j++)
301 has_float = is_float_arg( odp, j );
303 output( "\t.balign 4\n" );
304 output( "__wine_spec_relay_entry_point_%d:\n", i );
305 output( "\tpush {r0-r3}\n" );
306 output( "\tmov r2, SP\n");
307 if (has_float) output( "\tvpush {s0-s15}\n" );
308 output( "\tpush {LR}\n" );
309 output( "\tsub SP, #4\n");
310 output( "\tmovw r1,#%u\n", i - spec->base );
311 output( "\tmovt r1,#%u\n", odp->u.func.args_str_offset );
312 if (UsePIC)
314 output( "\tldr r0, 2f\n");
315 output( "1:\tadd r0, PC\n");
317 else
319 output( "\tmovw r0, :lower16:.L__wine_spec_relay_descr\n" );
320 output( "\tmovt r0, :upper16:.L__wine_spec_relay_descr\n" );
322 output( "\tldr IP, [r0, #4]\n");
323 output( "\tblx IP\n");
324 output( "\tldr IP, [SP, #4]\n" );
325 output( "\tadd SP, #%u\n", 24 + (has_float ? 64 : 0) );
326 output( "\tbx IP\n");
327 if (UsePIC) output( "2:\t.long .L__wine_spec_relay_descr-1b-%u\n", thumb_mode ? 4 : 8 );
328 break;
331 case CPU_ARM64:
333 int stack_size = 16 * ((min(odp->u.func.nb_args, 8) + 1) / 2);
335 output( "\t.balign 4\n" );
336 output( "__wine_spec_relay_entry_point_%d:\n", i );
337 output_seh( ".seh_proc __wine_spec_relay_entry_point_%d", i );
338 output( "\tstp x29, x30, [sp, #-%u]!\n", stack_size + 16 );
339 output_seh( ".seh_save_fplr_x %u", stack_size + 16 );
340 output( "\tmov x29, sp\n" );
341 output_seh( ".seh_set_fp" );
342 output_seh( ".seh_endprologue" );
343 switch (stack_size)
345 case 64: output( "\tstp x6, x7, [sp, #64]\n" );
346 /* fall through */
347 case 48: output( "\tstp x4, x5, [sp, #48]\n" );
348 /* fall through */
349 case 32: output( "\tstp x2, x3, [sp, #32]\n" );
350 /* fall through */
351 case 16: output( "\tstp x0, x1, [sp, #16]\n" );
352 /* fall through */
353 default: break;
355 output( "\tadd x2, sp, #16\n");
356 output( "\tstp x8, x9, [SP,#-16]!\n" );
357 output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
358 if (i - spec->base) output( "\tadd w1, w1, #%u\n", i - spec->base );
359 output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_relay_descr") );
360 output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_relay_descr") );
361 output( "\tldr x3, [x0, #8]\n");
362 output( "\tblr x3\n");
363 output( "\tmov sp, x29\n" );
364 output( "\tldp x29, x30, [sp], #%u\n", stack_size + 16 );
365 output( "\tret\n");
366 output_seh( ".seh_endproc" );
367 break;
370 case CPU_x86_64:
371 output( "\t.balign 4\n" );
372 output( "\t.long 0x90909090,0x90909090\n" );
373 output( "__wine_spec_relay_entry_point_%d:\n", i );
374 output_seh( ".seh_proc __wine_spec_relay_entry_point_%d", i );
375 output_seh( ".seh_endprologue" );
376 switch (odp->u.func.nb_args)
378 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
379 /* fall through */
380 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
381 /* fall through */
382 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
383 /* fall through */
384 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
385 /* fall through */
386 case 0: break;
388 output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
389 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
390 output( "\tcallq *8(%%rcx)\n" );
391 output( "\tret\n" );
392 output_seh( ".seh_endproc" );
393 break;
395 default:
396 assert(0);
401 /*******************************************************************
402 * output_exports
404 * Output the export table for a Win32 module.
406 void output_exports( DLLSPEC *spec )
408 int i, fwd_size = 0;
409 int needs_imports = 0;
410 int needs_relay = has_relays( spec );
411 int nr_exports = get_exports_count( spec );
412 const char *func_ptr = is_pe() ? ".rva" : get_asm_ptr_keyword();
413 const char *name;
415 if (!nr_exports) return;
417 output( "\n/* export table */\n\n" );
418 output( "\t%s\n", get_asm_export_section() );
419 output( "\t.balign 4\n" );
420 output( ".L__wine_spec_exports:\n" );
422 /* export directory header */
424 output( "\t.long 0\n" ); /* Characteristics */
425 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
426 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
427 output_rva( ".L__wine_spec_exp_names" ); /* Name */
428 output( "\t.long %u\n", spec->base ); /* Base */
429 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
430 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
431 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
432 if (spec->nb_names)
434 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
435 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
437 else
439 output( "\t.long 0\n" ); /* AddressOfNames */
440 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
443 /* output the function pointers */
445 output( "\n.L__wine_spec_exports_funcs:\n" );
446 for (i = spec->base; i <= spec->limit; i++)
448 ORDDEF *odp = spec->ordinals[i];
449 if (!odp) output( "\t%s 0\n", is_pe() ? ".long" : get_asm_ptr_keyword() );
450 else switch(odp->type)
452 case TYPE_EXTERN:
453 case TYPE_STDCALL:
454 case TYPE_VARARGS:
455 case TYPE_CDECL:
456 if (odp->flags & FLAG_FORWARD)
458 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr, fwd_size );
459 fwd_size += strlen(odp->link_name) + 1;
461 else if ((odp->flags & FLAG_IMPORT) && (target.cpu == CPU_i386 || target.cpu == CPU_x86_64))
463 name = odp->name ? odp->name : odp->export_name;
464 if (name) output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_imp"), name );
465 else output( "\t%s %s_%u\n", func_ptr, asm_name("__wine_spec_imp"), i );
466 needs_imports = 1;
468 else if (odp->flags & FLAG_EXT_LINK)
470 output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_ext_link"), odp->link_name );
472 else
474 output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
476 break;
477 case TYPE_STUB:
478 output( "\t%s %s\n", func_ptr, asm_name( get_stub_name( odp, spec )) );
479 break;
480 default:
481 assert(0);
485 if (spec->nb_names)
487 /* output the function name pointers */
489 int namepos = strlen(spec->file_name) + 1;
491 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
492 for (i = 0; i < spec->nb_names; i++)
494 output_rva( ".L__wine_spec_exp_names + %u", namepos );
495 namepos += strlen(spec->names[i]->name) + 1;
498 /* output the function ordinals */
500 output( "\n.L__wine_spec_exp_ordinals:\n" );
501 for (i = 0; i < spec->nb_names; i++)
503 output( "\t.short %d\n", spec->names[i]->ordinal - spec->base );
505 if (spec->nb_names % 2)
507 output( "\t.short 0\n" );
511 if (needs_relay)
513 output( "\t.long 0xdeb90002\n" ); /* magic */
514 if (is_pe()) output_rva( ".L__wine_spec_relay_descr" );
515 else output( "\t.long 0\n" );
518 /* output the export name strings */
520 output( "\n.L__wine_spec_exp_names:\n" );
521 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
522 for (i = 0; i < spec->nb_names; i++)
523 output( "\t%s \"%s\"\n",
524 get_asm_string_keyword(), spec->names[i]->name );
526 /* output forward strings */
528 if (fwd_size)
530 output( "\n.L__wine_spec_forwards:\n" );
531 for (i = spec->base; i <= spec->limit; i++)
533 ORDDEF *odp = spec->ordinals[i];
534 if (odp && (odp->flags & FLAG_FORWARD))
535 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
539 /* output relays */
541 if (needs_relay)
543 if (is_pe())
545 output( "\t.data\n" );
546 output( "\t.balign %u\n", get_ptr_size() );
548 else
550 output( "\t.balign %u\n", get_ptr_size() );
551 output( ".L__wine_spec_exports_end:\n" );
554 output( ".L__wine_spec_relay_descr:\n" );
555 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
556 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
557 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
558 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
559 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
560 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
562 output_relay_debug( spec );
564 else if (!is_pe())
566 output( "\t.balign %u\n", get_ptr_size() );
567 output( ".L__wine_spec_exports_end:\n" );
568 output( "\t%s 0\n", get_asm_ptr_keyword() );
571 /* output import thunks */
573 if (!needs_imports) return;
574 output( "\t.text\n" );
575 for (i = spec->base; i <= spec->limit; i++)
577 ORDDEF *odp = spec->ordinals[i];
578 if (!odp) continue;
579 if (!(odp->flags & FLAG_IMPORT)) continue;
581 name = odp->name ? odp->name : odp->export_name;
583 output( "\t.balign 4\n" );
584 output( "\t.long 0x90909090,0x90909090\n" );
585 if (name) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name );
586 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i );
588 switch (target.cpu)
590 case CPU_i386:
591 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
592 if (UsePIC)
594 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
595 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp )));
596 needs_get_pc_thunk = 1;
598 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp )));
599 break;
600 case CPU_x86_64:
601 output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
602 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp )));
603 break;
604 default:
605 assert(0);
611 /*******************************************************************
612 * output_module
614 * Output the module data.
616 void output_module( DLLSPEC *spec )
618 int machine = 0;
619 int i;
620 unsigned int page_size = get_page_size();
621 const char *data_dirs[16] = { NULL };
623 /* Reserve some space for the PE header */
625 switch (target.platform)
627 case PLATFORM_MINGW:
628 case PLATFORM_WINDOWS:
629 return; /* nothing to do */
630 case PLATFORM_APPLE:
631 output( "\t.text\n" );
632 output( "\t.balign %u\n", page_size );
633 output( "__wine_spec_pe_header:\n" );
634 output( "\t.space 65536\n" );
635 break;
636 case PLATFORM_SOLARIS:
637 output( "\n\t.section \".text\",\"ax\"\n" );
638 output( "__wine_spec_pe_header:\n" );
639 output( "\t.skip %u\n", 65536 + page_size );
640 break;
641 default:
642 switch (target.cpu)
644 case CPU_i386:
645 case CPU_x86_64:
646 output( "\n\t.section \".init\",\"ax\"\n" );
647 output( "\tjmp 1f\n" );
648 break;
649 case CPU_ARM:
650 output( "\n\t.section \".text\",\"ax\"\n" );
651 output( "\tb 1f\n" );
652 break;
653 case CPU_ARM64:
654 output( "\n\t.section \".init\",\"ax\"\n" );
655 output( "\tb 1f\n" );
656 break;
657 case CPU_ARM64EC:
658 assert( 0 );
659 break;
661 output( "__wine_spec_pe_header:\n" );
662 output( "\t.skip %u\n", 65536 + page_size );
663 output( "1:\n" );
664 break;
667 /* Output the NT header */
669 output( "\n\t.data\n" );
670 output( "\t.balign %u\n", get_ptr_size() );
671 output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
672 output( "%s:\n", asm_name("__wine_spec_nt_header") );
673 output( ".L__wine_spec_rva_base:\n" );
675 output( "\t.long 0x4550\n" ); /* Signature */
676 switch (target.cpu)
678 case CPU_i386: machine = IMAGE_FILE_MACHINE_I386; break;
679 case CPU_ARM64EC:
680 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
681 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
682 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
684 output( "\t.short 0x%04x\n", /* Machine */
685 machine );
686 output( "\t.short 0\n" ); /* NumberOfSections */
687 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
688 output( "\t.long 0\n" ); /* PointerToSymbolTable */
689 output( "\t.long 0\n" ); /* NumberOfSymbols */
690 output( "\t.short %d\n", /* SizeOfOptionalHeader */
691 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
692 output( "\t.short 0x%04x\n", /* Characteristics */
693 spec->characteristics );
694 output( "\t.short 0x%04x\n", /* Magic */
695 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
696 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
697 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
698 output( "\t.long 0\n" ); /* SizeOfCode */
699 output( "\t.long 0\n" ); /* SizeOfInitializedData */
700 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
702 for (i = 0; i < spec_extra_ld_symbols.count; i++)
703 output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols.str[i]) );
705 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
706 output( "\t%s %s\n", /* AddressOfEntryPoint */
707 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
708 if (get_ptr_size() == 4)
710 output( "\t.long 0\n" ); /* BaseOfCode */
711 output( "\t.long 0\n" ); /* BaseOfData */
713 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
714 get_asm_ptr_keyword() );
715 output( "\t.long %u\n", page_size ); /* SectionAlignment */
716 output( "\t.long %u\n", page_size ); /* FileAlignment */
717 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
718 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
719 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
720 spec->subsystem_major, spec->subsystem_minor );
721 output( "\t.long 0\n" ); /* Win32VersionValue */
722 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
723 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
724 output( "\t.long 0\n" ); /* CheckSum */
725 output( "\t.short 0x%04x\n", /* Subsystem */
726 spec->subsystem );
727 output( "\t.short 0x%04x\n", /* DllCharacteristics */
728 spec->dll_characteristics );
729 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
730 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
731 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
732 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
733 output( "\t.long 0\n" ); /* LoaderFlags */
734 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
736 if (get_exports_count( spec ))
737 data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
738 if (has_imports())
739 data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
740 if (spec->nb_resources)
741 data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
742 if (has_delay_imports())
743 data_dirs[13] = ".L__wine_spec_delay_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
745 output_data_directories( data_dirs );
747 if (target.platform == PLATFORM_APPLE)
748 output( "\t.lcomm %s,4\n", asm_name("_end") );
752 /*******************************************************************
753 * output_spec32_file
755 * Build a Win32 C file from a spec file.
757 void output_spec32_file( DLLSPEC *spec )
759 needs_get_pc_thunk = 0;
760 open_output_file();
761 output_standard_file_header();
762 output_module( spec );
763 output_stubs( spec );
764 output_exports( spec );
765 output_imports( spec );
766 if (needs_get_pc_thunk) output_get_pc_thunk();
767 output_resources( spec );
768 output_gnu_stack_note();
769 close_output_file();
773 struct sec_data
775 char name[8];
776 const void *ptr;
777 unsigned int size;
778 unsigned int flags;
779 unsigned int file_size;
780 unsigned int virt_size;
781 unsigned int filepos;
782 unsigned int rva;
785 struct dir_data
787 unsigned int rva;
788 unsigned int size;
791 struct exp_data
793 unsigned int rva;
794 const char *name;
797 static struct
799 unsigned int section_align;
800 unsigned int file_align;
801 unsigned int sec_count;
802 unsigned int exp_count;
803 struct dir_data dir[16];
804 struct sec_data sec[8];
805 struct exp_data exp[8];
806 } pe;
808 static void set_dir( unsigned int idx, unsigned int rva, unsigned int size )
810 pe.dir[idx].rva = rva;
811 pe.dir[idx].size = size;
814 static void add_export( unsigned int rva, const char *name )
816 pe.exp[pe.exp_count].rva = rva;
817 pe.exp[pe.exp_count].name = name;
818 pe.exp_count++;
821 static unsigned int current_rva(void)
823 if (!pe.sec_count) return pe.section_align;
824 return pe.sec[pe.sec_count - 1].rva + pe.sec[pe.sec_count - 1].virt_size;
827 static unsigned int current_filepos(void)
829 if (!pe.sec_count) return pe.file_align;
830 return pe.sec[pe.sec_count - 1].filepos + pe.sec[pe.sec_count - 1].file_size;
833 static unsigned int flush_output_to_section( const char *name, int dir_idx, unsigned int flags )
835 struct sec_data *sec = &pe.sec[pe.sec_count];
837 if (!output_buffer_pos) return 0;
839 strncpy( sec->name, name, sizeof(sec->name) );
840 sec->ptr = output_buffer;
841 sec->size = output_buffer_pos;
842 sec->flags = flags;
843 sec->rva = current_rva();
844 sec->filepos = current_filepos();
845 sec->file_size = (sec->size + pe.file_align - 1) & ~(pe.file_align - 1);
846 sec->virt_size = (sec->size + pe.section_align - 1) & ~(pe.section_align - 1);
847 if (dir_idx >= 0) set_dir( dir_idx, sec->rva, sec->size );
848 init_output_buffer();
849 pe.sec_count++;
850 return sec->size;
853 static void output_pe_exports( DLLSPEC *spec )
855 unsigned int i, exp_count = get_exports_count( spec );
856 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
857 unsigned int pos, str_rva = exp_rva + 4 * exp_count + 6 * spec->nb_names;
859 if (!spec->nb_entry_points) return;
861 init_output_buffer();
862 put_dword( 0 ); /* Characteristics */
863 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
864 put_word( 0 ); /* MajorVersion */
865 put_word( 0 ); /* MinorVersion */
866 put_dword( str_rva ); /* Name */
867 put_dword( spec->base ); /* Base */
868 put_dword( exp_count ); /* NumberOfFunctions */
869 put_dword( spec->nb_names ); /* NumberOfNames */
870 put_dword( exp_rva ); /* AddressOfFunctions */
871 if (spec->nb_names)
873 put_dword( exp_rva + 4 * exp_count ); /* AddressOfNames */
874 put_dword( exp_rva + 4 * exp_count + 4 * spec->nb_names ); /* AddressOfNameOrdinals */
876 else
878 put_dword( 0 ); /* AddressOfNames */
879 put_dword( 0 ); /* AddressOfNameOrdinals */
882 /* functions */
883 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < spec->nb_names; i++)
884 pos += strlen( spec->names[i]->name ) + 1;
885 for (i = spec->base; i <= spec->limit; i++)
887 ORDDEF *odp = spec->ordinals[i];
888 if (odp && (odp->flags & FLAG_FORWARD))
890 put_dword( pos );
891 pos += strlen(odp->link_name) + 1;
893 else put_dword( 0 );
896 /* names */
897 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < spec->nb_names; i++)
899 put_dword( pos );
900 pos += strlen(spec->names[i]->name) + 1;
903 /* ordinals */
904 for (i = 0; i < spec->nb_names; i++) put_word( spec->names[i]->ordinal - spec->base );
906 /* strings */
907 put_data( spec->file_name, strlen(spec->file_name) + 1 );
908 for (i = 0; i < spec->nb_names; i++)
909 put_data( spec->names[i]->name, strlen(spec->names[i]->name) + 1 );
911 for (i = spec->base; i <= spec->limit; i++)
913 ORDDEF *odp = spec->ordinals[i];
914 if (odp && (odp->flags & FLAG_FORWARD)) put_data( odp->link_name, strlen(odp->link_name) + 1 );
917 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
918 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
922 /* apiset hash table */
923 struct apiset_hash_entry
925 unsigned int hash;
926 unsigned int index;
929 static int apiset_hash_cmp( const void *h1, const void *h2 )
931 const struct apiset_hash_entry *entry1 = h1;
932 const struct apiset_hash_entry *entry2 = h2;
934 if (entry1->hash > entry2->hash) return 1;
935 if (entry1->hash < entry2->hash) return -1;
936 return 0;
939 static void output_apiset_section( const struct apiset *apiset )
941 struct apiset_hash_entry *hash;
942 struct apiset_entry *e;
943 unsigned int i, j, str_pos, value_pos, hash_pos, size;
945 init_output_buffer();
947 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
948 str_pos = value_pos;
949 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
950 str_pos += 0x14 * max( 1, e->val_count ); /* values */
952 hash_pos = str_pos + ((apiset->str_pos * 2 + 3) & ~3);
953 size = hash_pos + apiset->count * 8; /* hashes */
955 /* header */
957 put_dword( 6 ); /* Version */
958 put_dword( size ); /* Size */
959 put_dword( 0 ); /* Flags */
960 put_dword( apiset->count ); /* Count */
961 put_dword( 0x1c ); /* EntryOffset */
962 put_dword( hash_pos ); /* HashOffset */
963 put_dword( apiset_hash_factor ); /* HashFactor */
965 /* name entries */
967 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
968 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
970 put_dword( 1 ); /* Flags */
971 put_dword( str_pos + e->name_off * 2 ); /* NameOffset */
972 put_dword( e->name_len * 2 ); /* NameLength */
973 put_dword( e->hash_len * 2 ); /* HashedLength */
974 put_dword( value_pos ); /* ValueOffset */
975 put_dword( max( 1, e->val_count )); /* ValueCount */
976 value_pos += 0x14 * max( 1, e->val_count );
979 /* values */
981 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
983 if (!e->val_count)
985 put_dword( 0 ); /* Flags */
986 put_dword( 0 ); /* NameOffset */
987 put_dword( 0 ); /* NameLength */
988 put_dword( 0 ); /* ValueOffset */
989 put_dword( 0 ); /* ValueLength */
991 else for (j = 0; j < e->val_count; j++)
993 put_dword( 0 ); /* Flags */
994 if (e->values[j].name_off)
996 put_dword( str_pos + e->values[j].name_off * 2 ); /* NameOffset */
997 put_dword( e->values[j].name_len * 2 ); /* NameLength */
999 else
1001 put_dword( 0 ); /* NameOffset */
1002 put_dword( 0 ); /* NameLength */
1004 put_dword( str_pos + e->values[j].val_off * 2 ); /* ValueOffset */
1005 put_dword( e->values[j].val_len * 2 ); /* ValueLength */
1009 /* strings */
1011 for (i = 0; i < apiset->str_pos; i++) put_word( apiset->strings[i] );
1012 align_output( 4 );
1014 /* hash table */
1016 hash = xmalloc( apiset->count * sizeof(*hash) );
1017 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1019 hash[i].hash = e->hash;
1020 hash[i].index = i;
1022 qsort( hash, apiset->count, sizeof(*hash), apiset_hash_cmp );
1023 for (i = 0; i < apiset->count; i++)
1025 put_dword( hash[i].hash );
1026 put_dword( hash[i].index );
1028 free( hash );
1030 flush_output_to_section( ".apiset", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1034 static void output_pe_file( DLLSPEC *spec, const char signature[32] )
1036 const unsigned int lfanew = 0x40 + 32;
1037 unsigned int i, code_size = 0, data_size = 0;
1039 init_output_buffer();
1041 for (i = 0; i < pe.sec_count; i++)
1042 if (pe.sec[i].flags & 0x20) /* CNT_CODE */
1043 code_size += pe.sec[i].file_size;
1045 /* .rsrc section */
1046 if (spec->type == SPEC_WIN32)
1048 output_bin_resources( spec, current_rva() );
1049 flush_output_to_section( ".rsrc", 2 /* IMAGE_DIRECTORY_ENTRY_RESOURCE */,
1050 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1053 /* .reloc section */
1054 if (code_size)
1056 put_dword( 0 ); /* VirtualAddress */
1057 put_dword( 0 ); /* Size */
1058 flush_output_to_section( ".reloc", 5 /* IMAGE_DIRECTORY_ENTRY_BASERELOC */,
1059 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ );
1062 for (i = 0; i < pe.sec_count; i++)
1063 if ((pe.sec[i].flags & 0x60) == 0x40) /* CNT_INITIALIZED_DATA */
1064 data_size += pe.sec[i].file_size;
1066 put_word( 0x5a4d ); /* e_magic */
1067 put_word( 0x40 ); /* e_cblp */
1068 put_word( 0x01 ); /* e_cp */
1069 put_word( 0 ); /* e_crlc */
1070 put_word( lfanew / 16 ); /* e_cparhdr */
1071 put_word( 0x0000 ); /* e_minalloc */
1072 put_word( 0xffff ); /* e_maxalloc */
1073 put_word( 0x0000 ); /* e_ss */
1074 put_word( 0x00b8 ); /* e_sp */
1075 put_word( 0 ); /* e_csum */
1076 put_word( 0 ); /* e_ip */
1077 put_word( 0 ); /* e_cs */
1078 put_word( lfanew ); /* e_lfarlc */
1079 put_word( 0 ); /* e_ovno */
1080 put_dword( 0 ); /* e_res */
1081 put_dword( 0 );
1082 put_word( 0 ); /* e_oemid */
1083 put_word( 0 ); /* e_oeminfo */
1084 put_dword( 0 ); /* e_res2 */
1085 put_dword( 0 );
1086 put_dword( 0 );
1087 put_dword( 0 );
1088 put_dword( 0 );
1089 put_dword( lfanew );
1091 put_data( signature, 32 );
1093 put_dword( 0x4550 ); /* Signature */
1094 switch (target.cpu)
1096 case CPU_i386: put_word( IMAGE_FILE_MACHINE_I386 ); break;
1097 case CPU_ARM64EC:
1098 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
1099 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
1100 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
1102 put_word( pe.sec_count ); /* NumberOfSections */
1103 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1104 put_dword( 0 ); /* PointerToSymbolTable */
1105 put_dword( 0 ); /* NumberOfSymbols */
1106 put_word( get_ptr_size() == 8 ?
1107 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
1108 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
1109 put_word( spec->characteristics ); /* Characteristics */
1110 put_word( get_ptr_size() == 8 ?
1111 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
1112 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
1113 put_byte( 7 ); /* MajorLinkerVersion */
1114 put_byte( 10 ); /* MinorLinkerVersion */
1115 put_dword( code_size ); /* SizeOfCode */
1116 put_dword( data_size ); /* SizeOfInitializedData */
1117 put_dword( 0 ); /* SizeOfUninitializedData */
1118 put_dword( code_size ? pe.sec[0].rva : 0 ); /* AddressOfEntryPoint */
1119 put_dword( code_size ? pe.sec[0].rva : 0 ); /* BaseOfCode */
1120 if (get_ptr_size() == 4)
1122 put_dword( 0 ); /* BaseOfData */
1123 put_dword( 0x10000000 ); /* ImageBase */
1125 else
1127 put_dword( 0x80000000 ); /* ImageBase */
1128 put_dword( 0x00000001 );
1130 put_dword( pe.section_align ); /* SectionAlignment */
1131 put_dword( pe.file_align ); /* FileAlignment */
1132 put_word( 1 ); /* MajorOperatingSystemVersion */
1133 put_word( 0 ); /* MinorOperatingSystemVersion */
1134 put_word( 0 ); /* MajorImageVersion */
1135 put_word( 0 ); /* MinorImageVersion */
1136 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
1137 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
1138 put_dword( 0 ); /* Win32VersionValue */
1139 put_dword( current_rva() ); /* SizeOfImage */
1140 put_dword( pe.file_align ); /* SizeOfHeaders */
1141 put_dword( 0 ); /* CheckSum */
1142 put_word( spec->subsystem ); /* Subsystem */
1143 put_word( spec->dll_characteristics ); /* DllCharacteristics */
1144 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
1145 put_pword( pe.section_align ); /* SizeOfStackCommit */
1146 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
1147 put_pword( pe.section_align ); /* SizeOfHeapCommit */
1148 put_dword( 0 ); /* LoaderFlags */
1149 put_dword( 16 ); /* NumberOfRvaAndSizes */
1151 /* image directories */
1152 for (i = 0; i < 16; i++)
1154 put_dword( pe.dir[i].rva ); /* VirtualAddress */
1155 put_dword( pe.dir[i].size ); /* Size */
1158 /* sections */
1159 for (i = 0; i < pe.sec_count; i++)
1161 put_data( pe.sec[i].name, 8 ); /* Name */
1162 put_dword( pe.sec[i].size ); /* VirtualSize */
1163 put_dword( pe.sec[i].rva ); /* VirtualAddress */
1164 put_dword( pe.sec[i].file_size ); /* SizeOfRawData */
1165 put_dword( pe.sec[i].filepos ); /* PointerToRawData */
1166 put_dword( 0 ); /* PointerToRelocations */
1167 put_dword( 0 ); /* PointerToLinenumbers */
1168 put_word( 0 ); /* NumberOfRelocations */
1169 put_word( 0 ); /* NumberOfLinenumbers */
1170 put_dword( pe.sec[i].flags ); /* Characteristics */
1172 align_output( pe.file_align );
1174 /* section data */
1175 for (i = 0; i < pe.sec_count; i++)
1177 put_data( pe.sec[i].ptr, pe.sec[i].size );
1178 align_output( pe.file_align );
1181 flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
1184 /*******************************************************************
1185 * output_fake_module
1187 * Build a fake binary module from a spec file.
1189 void output_fake_module( DLLSPEC *spec )
1191 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
1192 0xc2, 0x0c, 0x00 }; /* ret $12 */
1194 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
1195 0xc2, 0x04, 0x00 }; /* ret $4 */
1196 unsigned int i;
1198 resolve_imports( spec );
1199 pe.section_align = get_page_size();
1200 pe.file_align = 0x200;
1201 init_output_buffer();
1203 /* .text section */
1204 if (spec->characteristics & IMAGE_FILE_DLL) put_data( dll_code_section, sizeof(dll_code_section) );
1205 else put_data( exe_code_section, sizeof(exe_code_section) );
1206 flush_output_to_section( ".text", -1, 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ );
1208 if (spec->type == SPEC_WIN16)
1210 add_export( current_rva(), "__wine_spec_dos_header" );
1212 /* .rdata section */
1213 output_fake_module16( spec );
1214 if (spec->main_module)
1216 add_export( current_rva() + output_buffer_pos, "__wine_spec_main_module" );
1217 put_data( spec->main_module, strlen(spec->main_module) + 1 );
1219 flush_output_to_section( ".rdata", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1222 /* .edata section */
1223 if (pe.exp_count)
1225 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
1226 unsigned int pos, str_rva = exp_rva + 10 * pe.exp_count;
1228 put_dword( 0 ); /* Characteristics */
1229 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1230 put_word( 0 ); /* MajorVersion */
1231 put_word( 0 ); /* MinorVersion */
1232 put_dword( str_rva ); /* Name */
1233 put_dword( 1 ); /* Base */
1234 put_dword( pe.exp_count ); /* NumberOfFunctions */
1235 put_dword( pe.exp_count ); /* NumberOfNames */
1236 put_dword( exp_rva ); /* AddressOfFunctions */
1237 put_dword( exp_rva + 4 * pe.exp_count ); /* AddressOfNames */
1238 put_dword( exp_rva + 8 * pe.exp_count ); /* AddressOfNameOrdinals */
1240 /* functions */
1241 for (i = 0; i < pe.exp_count; i++) put_dword( pe.exp[i].rva );
1242 /* names */
1243 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < pe.exp_count; i++)
1245 put_dword( pos );
1246 pos += strlen( pe.exp[i].name ) + 1;
1248 /* ordinals */
1249 for (i = 0; i < pe.exp_count; i++) put_word( i );
1250 /* strings */
1251 put_data( spec->file_name, strlen(spec->file_name) + 1 );
1252 for (i = 0; i < pe.exp_count; i++) put_data( pe.exp[i].name, strlen(pe.exp[i].name) + 1 );
1253 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1254 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1257 output_pe_file( spec, fakedll_signature );
1261 /*******************************************************************
1262 * output_data_module
1264 * Build a data-only module from a spec file.
1266 void output_data_module( DLLSPEC *spec )
1268 pe.section_align = pe.file_align = get_page_size();
1270 output_pe_exports( spec );
1271 if (spec->apiset.count) output_apiset_section( &spec->apiset );
1272 output_pe_file( spec, builtin_signature );
1276 /*******************************************************************
1277 * output_def_file
1279 * Build a Win32 def file from a spec file.
1281 void output_def_file( DLLSPEC *spec, int import_only )
1283 DLLSPEC *spec32 = NULL;
1284 const char *name;
1285 int i, total;
1287 if (spec->type == SPEC_WIN16)
1289 spec32 = alloc_dll_spec();
1290 add_16bit_exports( spec32, spec );
1291 spec = spec32;
1294 if (spec_file_name)
1295 output( "; File generated automatically from %s; do not edit!\n\n",
1296 spec_file_name );
1297 else
1298 output( "; File generated automatically; do not edit!\n\n" );
1300 output( "LIBRARY %s\n\n", spec->file_name);
1301 output( "EXPORTS\n");
1303 /* Output the exports and relay entry points */
1305 for (i = total = 0; i < spec->nb_entry_points; i++)
1307 const ORDDEF *odp = &spec->entry_points[i];
1308 int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1310 if (odp->name) name = odp->name;
1311 else if (odp->export_name) name = odp->export_name;
1312 else continue;
1314 if (!is_private) total++;
1315 if (import_only && odp->type == TYPE_STUB) continue;
1317 if ((odp->flags & FLAG_FASTCALL) && is_pe())
1318 name = strmake( "@%s", name );
1320 output( " %s", name );
1322 switch(odp->type)
1324 case TYPE_EXTERN:
1325 is_data = 1;
1326 /* fall through */
1327 case TYPE_VARARGS:
1328 case TYPE_CDECL:
1329 /* try to reduce output */
1330 if(!import_only && (strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD)))
1331 output( "=%s", odp->link_name );
1332 break;
1333 case TYPE_STDCALL:
1335 int at_param = get_args_size( odp );
1336 if (!kill_at && target.cpu == CPU_i386) output( "@%d", at_param );
1337 if (import_only) break;
1338 if (odp->flags & FLAG_FORWARD)
1339 output( "=%s", odp->link_name );
1340 else if (strcmp(name, odp->link_name)) /* try to reduce output */
1341 output( "=%s", get_link_name( odp ));
1342 break;
1344 case TYPE_STUB:
1345 if (!kill_at && target.cpu == CPU_i386) output( "@%d", get_args_size( odp ));
1346 is_private = 1;
1347 break;
1348 default:
1349 assert(0);
1351 output( " @%d", odp->ordinal );
1352 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1353 if (is_data) output( " DATA" );
1354 if (is_private) output( " PRIVATE" );
1355 output( "\n" );
1357 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1358 if (spec32) free_dll_spec( spec32 );
1362 /*******************************************************************
1363 * make_builtin_files
1365 void make_builtin_files( struct strarray files )
1367 int i, fd;
1368 struct
1370 unsigned short e_magic;
1371 unsigned short unused[29];
1372 unsigned int e_lfanew;
1373 } header;
1375 for (i = 0; i < files.count; i++)
1377 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1378 fatal_perror( "Cannot open %s", files.str[i] );
1379 if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1381 if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1382 fatal_error( "%s: Not enough space (%x) for Wine signature\n", files.str[i], header.e_lfanew );
1383 write( fd, builtin_signature, sizeof(builtin_signature) );
1385 if (prefer_native)
1387 unsigned int pos = header.e_lfanew + 0x5e; /* OptionalHeader.DllCharacteristics */
1388 unsigned short dll_charact;
1389 lseek( fd, pos, SEEK_SET );
1390 if (read( fd, &dll_charact, sizeof(dll_charact) ) == sizeof(dll_charact))
1392 dll_charact |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
1393 lseek( fd, pos, SEEK_SET );
1394 write( fd, &dll_charact, sizeof(dll_charact) );
1398 else fatal_error( "%s: Unrecognized file format\n", files.str[i] );
1399 close( fd );
1403 static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1405 struct
1407 unsigned char e_ident[16];
1408 unsigned short e_type;
1409 unsigned short e_machine;
1410 unsigned int e_version;
1411 unsigned int e_entry;
1412 unsigned int e_phoff;
1413 unsigned int e_shoff;
1414 unsigned int e_flags;
1415 unsigned short e_ehsize;
1416 unsigned short e_phentsize;
1417 unsigned short e_phnum;
1418 unsigned short e_shentsize;
1419 unsigned short e_shnum;
1420 unsigned short e_shstrndx;
1421 } *elf = header;
1422 struct
1424 unsigned int p_type;
1425 unsigned int p_offset;
1426 unsigned int p_vaddr;
1427 unsigned int p_paddr;
1428 unsigned int p_filesz;
1429 unsigned int p_memsz;
1430 unsigned int p_flags;
1431 unsigned int p_align;
1432 } *phdr;
1433 struct
1435 unsigned int d_tag;
1436 unsigned int d_val;
1437 } *dyn;
1439 unsigned int i, size;
1441 if (header_size < sizeof(*elf)) return;
1442 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1444 size = elf->e_phnum * elf->e_phentsize;
1445 phdr = xmalloc( size );
1446 lseek( fd, elf->e_phoff, SEEK_SET );
1447 if (read( fd, phdr, size ) != size) return;
1449 for (i = 0; i < elf->e_phnum; i++)
1451 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1452 phdr = (void *)((char *)phdr + elf->e_phentsize);
1454 if (i == elf->e_phnum) return;
1456 dyn = xmalloc( phdr->p_filesz );
1457 lseek( fd, phdr->p_offset, SEEK_SET );
1458 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1459 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1461 switch (dyn[i].d_tag)
1463 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1464 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1465 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1468 lseek( fd, phdr->p_offset, SEEK_SET );
1469 write( fd, dyn, phdr->p_filesz );
1472 static void fixup_elf64( const char *name, int fd, void *header, size_t header_size )
1474 struct
1476 unsigned char e_ident[16];
1477 unsigned short e_type;
1478 unsigned short e_machine;
1479 unsigned int e_version;
1480 unsigned __int64 e_entry;
1481 unsigned __int64 e_phoff;
1482 unsigned __int64 e_shoff;
1483 unsigned int e_flags;
1484 unsigned short e_ehsize;
1485 unsigned short e_phentsize;
1486 unsigned short e_phnum;
1487 unsigned short e_shentsize;
1488 unsigned short e_shnum;
1489 unsigned short e_shstrndx;
1490 } *elf = header;
1491 struct
1493 unsigned int p_type;
1494 unsigned int p_flags;
1495 unsigned __int64 p_offset;
1496 unsigned __int64 p_vaddr;
1497 unsigned __int64 p_paddr;
1498 unsigned __int64 p_filesz;
1499 unsigned __int64 p_memsz;
1500 unsigned __int64 p_align;
1501 } *phdr;
1502 struct
1504 unsigned __int64 d_tag;
1505 unsigned __int64 d_val;
1506 } *dyn;
1508 unsigned int i, size;
1510 if (header_size < sizeof(*elf)) return;
1511 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1513 size = elf->e_phnum * elf->e_phentsize;
1514 phdr = xmalloc( size );
1515 lseek( fd, elf->e_phoff, SEEK_SET );
1516 if (read( fd, phdr, size ) != size) return;
1518 for (i = 0; i < elf->e_phnum; i++)
1520 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1521 phdr = (void *)((char *)phdr + elf->e_phentsize);
1523 if (i == elf->e_phnum) return;
1525 dyn = xmalloc( phdr->p_filesz );
1526 lseek( fd, phdr->p_offset, SEEK_SET );
1527 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1528 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1530 switch (dyn[i].d_tag)
1532 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1533 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1534 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1537 lseek( fd, phdr->p_offset, SEEK_SET );
1538 write( fd, dyn, phdr->p_filesz );
1541 /*******************************************************************
1542 * fixup_constructors
1544 void fixup_constructors( struct strarray files )
1546 int i, fd, size;
1547 unsigned int header[64];
1549 for (i = 0; i < files.count; i++)
1551 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1552 fatal_perror( "Cannot open %s", files.str[i] );
1553 size = read( fd, &header, sizeof(header) );
1554 if (size > 5)
1556 if (!memcmp( header, "\177ELF\001", 5 )) fixup_elf32( files.str[i], fd, header, size );
1557 else if (!memcmp( header, "\177ELF\002", 5 )) fixup_elf64( files.str[i], fd, header, size );
1559 close( fd );