include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / tools / winebuild / spec32.c
blobf06c40ca9b16de3d05de12e4b1f2c034f0783d84
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( struct exports *exports )
99 int i;
101 if (target.cpu == CPU_ARM64EC) return 0;
103 for (i = exports->base; i <= exports->limit; i++)
105 ORDDEF *odp = exports->ordinals[i];
106 if (needs_relay( odp )) return 1;
108 return 0;
111 static int get_exports_count( struct exports *exports )
113 if (exports->base > exports->limit) return 0;
114 return exports->limit - exports->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( struct exports *exports )
182 int i, count = 0, len = 1;
183 char *p, *buffer;
184 char str[MAX_ARGUMENTS + 2];
185 ORDDEF **funcs;
187 funcs = xmalloc( (exports->limit + 1 - exports->base) * sizeof(*funcs) );
188 for (i = exports->base; i <= exports->limit; i++)
190 ORDDEF *odp = exports->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( struct exports *exports )
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 = exports->base; i <= exports->limit; i++)
232 ORDDEF *odp = exports->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( exports ));
245 /* then the relay thunks */
247 output( "\t.text\n" );
248 output( "__wine_spec_relay_entry_points:\n" );
249 output( "\tnop\n" ); /* to avoid 0 offset */
251 for (i = exports->base; i <= exports->limit; i++)
253 ORDDEF *odp = exports->ordinals[i];
255 if (!needs_relay( odp )) continue;
257 switch (target.cpu)
259 case CPU_i386:
260 output( "\t.balign 4\n" );
261 output( "\t.long 0x90909090,0x90909090\n" );
262 output( "__wine_spec_relay_entry_point_%d:\n", i );
263 output_cfi( ".cfi_startproc" );
264 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
265 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) /* add the register arguments */
267 output( "\tpopl %%eax\n" );
268 if ((odp->flags & FLAG_FASTCALL) && get_args_size( odp ) > 4) output( "\tpushl %%edx\n" );
269 output( "\tpushl %%ecx\n" );
270 output( "\tpushl %%eax\n" );
272 output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - exports->base) );
273 output_cfi( ".cfi_adjust_cfa_offset 4" );
275 if (UsePIC)
277 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
278 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
279 needs_get_pc_thunk = 1;
281 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
282 output( "\tpushl %%eax\n" );
283 output_cfi( ".cfi_adjust_cfa_offset 4" );
285 output( "\tcall *4(%%eax)\n" );
286 output_cfi( ".cfi_adjust_cfa_offset -8" );
287 if (odp->type == TYPE_STDCALL)
288 output( "\tret $%u\n", get_args_size( odp ));
289 else
290 output( "\tret\n" );
291 output_cfi( ".cfi_endproc" );
292 break;
294 case CPU_ARM:
296 int j, has_float = 0;
298 for (j = 0; j < odp->u.func.nb_args && !has_float; j++)
299 has_float = is_float_arg( odp, j );
301 output( "\t.balign 4\n" );
302 output( "__wine_spec_relay_entry_point_%d:\n", i );
303 output( "\t.seh_proc __wine_spec_relay_entry_point_%d\n", i );
304 output( "\tpush {r0-r3}\n" );
305 output( "\t.seh_save_regs {r0-r3}\n" );
306 if (has_float)
308 output( "\tvpush {d0-d7}\n" );
309 output( "\t.seh_save_fregs {d0-d7}\n" );
311 output( "\tpush {r4,lr}\n" );
312 output( "\t.seh_save_regs {r4,lr}\n" );
313 output( "\t.seh_endprologue\n" );
314 output( "\tmovw r1,#%u\n", i - exports->base );
315 output( "\tmovt r1,#%u\n", odp->u.func.args_str_offset );
316 output( "\tmovw r0, :lower16:.L__wine_spec_relay_descr\n" );
317 output( "\tmovt r0, :upper16:.L__wine_spec_relay_descr\n" );
318 output( "\tldr IP, [r0, #4]\n");
319 output( "\tblx IP\n");
320 output( "\tldr IP, [SP, #4]\n" );
321 output( "\tadd SP, #%u\n", 24 + (has_float ? 64 : 0) );
322 output( "\tbx IP\n");
323 output( "\t.seh_endproc\n" );
324 break;
327 case CPU_ARM64:
329 int stack_size = 16 * ((min(odp->u.func.nb_args, 8) + 1) / 2);
331 output( "\t.balign 4\n" );
332 output( "__wine_spec_relay_entry_point_%d:\n", i );
333 output( "\t.seh_proc __wine_spec_relay_entry_point_%d\n", i );
334 output( "\tstp x29, x30, [sp, #-%u]!\n", stack_size + 16 );
335 output( "\t.seh_save_fplr_x %u\n", stack_size + 16 );
336 output( "\tmov x29, sp\n" );
337 output( "\t.seh_set_fp\n" );
338 output( "\t.seh_endprologue\n" );
339 switch (stack_size)
341 case 64: output( "\tstp x6, x7, [sp, #64]\n" );
342 /* fall through */
343 case 48: output( "\tstp x4, x5, [sp, #48]\n" );
344 /* fall through */
345 case 32: output( "\tstp x2, x3, [sp, #32]\n" );
346 /* fall through */
347 case 16: output( "\tstp x0, x1, [sp, #16]\n" );
348 /* fall through */
349 default: break;
351 output( "\tadd x2, sp, #16\n");
352 output( "\tstp x8, x9, [SP,#-16]!\n" );
353 output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
354 if (i - exports->base) output( "\tadd w1, w1, #%u\n", i - exports->base );
355 output( "\tadrp x0, .L__wine_spec_relay_descr\n" );
356 output( "\tadd x0, x0, #:lo12:.L__wine_spec_relay_descr\n" );
357 output( "\tldr x3, [x0, #8]\n");
358 output( "\tblr x3\n");
359 output( "\tmov sp, x29\n" );
360 output( "\tldp x29, x30, [sp], #%u\n", stack_size + 16 );
361 output( "\tret\n");
362 output( "\t.seh_endproc\n" );
363 break;
366 case CPU_x86_64:
367 output( "\t.balign 4\n" );
368 output( "\t.long 0x90909090,0x90909090\n" );
369 output( "__wine_spec_relay_entry_point_%d:\n", i );
370 output_seh( ".seh_proc __wine_spec_relay_entry_point_%d", i );
371 output_seh( ".seh_endprologue" );
372 switch (odp->u.func.nb_args)
374 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
375 /* fall through */
376 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
377 /* fall through */
378 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
379 /* fall through */
380 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
381 /* fall through */
382 case 0: break;
384 output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - exports->base) );
385 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
386 output( "\tcallq *8(%%rcx)\n" );
387 output( "\tret\n" );
388 output_seh( ".seh_endproc" );
389 break;
391 default:
392 assert(0);
397 /*******************************************************************
398 * output_exports
400 * Output the export table for a Win32 module.
402 void output_exports( DLLSPEC *spec )
404 struct exports *exports = &spec->exports;
405 int i, fwd_size = 0;
406 int needs_imports = 0;
407 int needs_relay = has_relays( exports );
408 int nr_exports = get_exports_count( exports );
409 const char *func_ptr = is_pe() ? ".rva" : get_asm_ptr_keyword();
410 const char *name;
412 if (!nr_exports) return;
414 /* ARM64EC exports are more tricky than other targets. For functions implemented in ARM64EC,
415 * linker generates x86_64 thunk and relevant metadata. Use .drectve section to pass export
416 * directives to the linker. */
417 if (target.cpu == CPU_ARM64EC)
419 output( "\t.section .drectve\n" );
420 for (i = exports->base; i <= exports->limit; i++)
422 ORDDEF *odp = exports->ordinals[i];
423 const char *symbol;
425 if (!odp) continue;
427 switch (odp->type)
429 case TYPE_EXTERN:
430 case TYPE_STDCALL:
431 case TYPE_VARARGS:
432 case TYPE_CDECL:
433 if (odp->flags & FLAG_FORWARD)
434 symbol = odp->link_name;
435 else if (odp->flags & FLAG_EXT_LINK)
436 symbol = strmake( "%s_%s", asm_name("__wine_spec_ext_link"), odp->link_name );
437 else
438 symbol = asm_name( get_link_name( odp ));
439 break;
440 case TYPE_STUB:
441 symbol = asm_name( get_stub_name( odp, spec ));
442 break;
443 default:
444 assert( 0 );
447 output( "\t.ascii \" -export:%s=%s,@%u%s%s\"\n",
448 odp->name ? odp->name : strmake( "_noname%u", i ),
449 symbol, i,
450 odp->name ? "" : ",NONAME",
451 odp->type == TYPE_EXTERN ? ",DATA" : "" );
453 return;
456 output( "\n/* export table */\n\n" );
457 output( "\t%s\n", get_asm_export_section() );
458 output( "\t.balign 4\n" );
459 output( ".L__wine_spec_exports:\n" );
461 /* export directory header */
463 output( "\t.long 0\n" ); /* Characteristics */
464 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
465 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
466 output_rva( ".L__wine_spec_exp_names" ); /* Name */
467 output( "\t.long %u\n", exports->base ); /* Base */
468 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
469 output( "\t.long %u\n", exports->nb_names ); /* NumberOfNames */
470 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
471 if (exports->nb_names)
473 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
474 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
476 else
478 output( "\t.long 0\n" ); /* AddressOfNames */
479 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
482 /* output the function pointers */
484 output( "\n.L__wine_spec_exports_funcs:\n" );
485 for (i = exports->base; i <= exports->limit; i++)
487 ORDDEF *odp = exports->ordinals[i];
488 if (!odp) output( "\t%s 0\n", is_pe() ? ".long" : get_asm_ptr_keyword() );
489 else switch(odp->type)
491 case TYPE_EXTERN:
492 case TYPE_STDCALL:
493 case TYPE_VARARGS:
494 case TYPE_CDECL:
495 if (odp->flags & FLAG_FORWARD)
497 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr, fwd_size );
498 fwd_size += strlen(odp->link_name) + 1;
500 else if ((odp->flags & FLAG_IMPORT) && (target.cpu == CPU_i386 || target.cpu == CPU_x86_64))
502 name = odp->name ? odp->name : odp->export_name;
503 if (name) output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_imp"), name );
504 else output( "\t%s %s_%u\n", func_ptr, asm_name("__wine_spec_imp"), i );
505 needs_imports = 1;
507 else if (odp->flags & FLAG_EXT_LINK)
509 output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_ext_link"), odp->link_name );
511 else
513 output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
515 break;
516 case TYPE_STUB:
517 output( "\t%s %s\n", func_ptr, asm_name( get_stub_name( odp, spec )) );
518 break;
519 default:
520 assert(0);
524 if (exports->nb_names)
526 /* output the function name pointers */
528 int namepos = strlen(spec->file_name) + 1;
530 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
531 for (i = 0; i < exports->nb_names; i++)
533 output_rva( ".L__wine_spec_exp_names + %u", namepos );
534 namepos += strlen(exports->names[i]->name) + 1;
537 /* output the function ordinals */
539 output( "\n.L__wine_spec_exp_ordinals:\n" );
540 for (i = 0; i < exports->nb_names; i++)
542 output( "\t.short %d\n", exports->names[i]->ordinal - exports->base );
544 if (exports->nb_names % 2)
546 output( "\t.short 0\n" );
550 if (needs_relay)
552 output( "\t.long 0xdeb90002\n" ); /* magic */
553 if (is_pe()) output_rva( ".L__wine_spec_relay_descr" );
554 else output( "\t.long 0\n" );
557 /* output the export name strings */
559 output( "\n.L__wine_spec_exp_names:\n" );
560 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
561 for (i = 0; i < exports->nb_names; i++)
562 output( "\t%s \"%s\"\n",
563 get_asm_string_keyword(), exports->names[i]->name );
565 /* output forward strings */
567 if (fwd_size)
569 output( "\n.L__wine_spec_forwards:\n" );
570 for (i = exports->base; i <= exports->limit; i++)
572 ORDDEF *odp = exports->ordinals[i];
573 if (odp && (odp->flags & FLAG_FORWARD))
574 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
578 /* output relays */
580 if (needs_relay)
582 if (is_pe())
584 output( "\t.data\n" );
585 output( "\t.balign %u\n", get_ptr_size() );
587 else
589 output( "\t.balign %u\n", get_ptr_size() );
590 output( ".L__wine_spec_exports_end:\n" );
593 output( ".L__wine_spec_relay_descr:\n" );
594 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
595 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
596 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
597 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
598 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
599 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
601 output_relay_debug( exports );
603 else if (!is_pe())
605 output( "\t.balign %u\n", get_ptr_size() );
606 output( ".L__wine_spec_exports_end:\n" );
607 output( "\t%s 0\n", get_asm_ptr_keyword() );
610 /* output import thunks */
612 if (!needs_imports) return;
613 output( "\t.text\n" );
614 for (i = exports->base; i <= exports->limit; i++)
616 ORDDEF *odp = exports->ordinals[i];
617 if (!odp) continue;
618 if (!(odp->flags & FLAG_IMPORT)) continue;
620 name = odp->name ? odp->name : odp->export_name;
622 output( "\t.balign 4\n" );
623 output( "\t.long 0x90909090,0x90909090\n" );
624 if (name) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name );
625 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i );
627 switch (target.cpu)
629 case CPU_i386:
630 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
631 if (UsePIC)
633 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
634 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp )));
635 needs_get_pc_thunk = 1;
637 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp )));
638 break;
639 case CPU_x86_64:
640 output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
641 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp )));
642 break;
643 default:
644 assert(0);
650 /*******************************************************************
651 * output_load_config
653 * Output the load configuration structure.
655 static void output_load_config(void)
657 if (!is_pe()) return;
659 output( "\n/* load_config */\n\n" );
660 output( "\t%s\n", get_asm_rodata_section() );
661 output( "\t.globl %s\n", asm_name( "_load_config_used" ));
662 output( "\t.balign %u\n", get_ptr_size() );
663 output( "%s:\n", asm_name( "_load_config_used" ));
664 output( "\t.long %u\n", get_ptr_size() == 8 ? 0x140 : 0xc0 ); /* Size */
665 output( "\t.long 0\n" ); /* TimeDateStamp */
666 output( "\t.short 0\n" ); /* MajorVersion */
667 output( "\t.short 0\n" ); /* MinorVersion */
668 output( "\t.long 0\n" ); /* GlobalFlagsClear */
669 output( "\t.long 0\n" ); /* GlobalFlagsSet */
670 output( "\t.long 0\n" ); /* CriticalSectionDefaultTimeout */
671 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DeCommitFreeBlockThreshold */
672 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DeCommitTotalFreeThreshold */
673 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* LockPrefixTable */
674 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* MaximumAllocationSize */
675 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* VirtualMemoryThreshold */
676 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* ProcessAffinityMask */
677 output( "\t.long 0\n" ); /* ProcessHeapFlags */
678 output( "\t.short 0\n" ); /* CSDVersion */
679 output( "\t.short 0\n" ); /* DependentLoadFlags */
680 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* EditList */
681 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SecurityCookie */
682 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SEHandlerTable */
683 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* SEHandlerCount */
684 if (target.cpu == CPU_ARM64EC)
686 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__guard_check_icall_fptr" ));
687 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__guard_dispatch_icall_fptr" ));
689 else
691 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFCheckFunctionPointer */
692 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFDispatchFunctionPointer */
694 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFFunctionTable */
695 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardCFFunctionCount */
696 if (target.cpu == CPU_ARM64EC)
697 output( "\t.long %s\n", asm_name( "__guard_flags" ));
698 else
699 output( "\t.long 0\n" ); /* GuardFlags */
700 output( "\t.short 0\n" ); /* CodeIntegrity.Flags */
701 output( "\t.short 0\n" ); /* CodeIntegrity.Catalog */
702 output( "\t.long 0\n" ); /* CodeIntegrity.CatalogOffset */
703 output( "\t.long 0\n" ); /* CodeIntegrity.Reserved */
704 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardAddressTakenIatEntryTable */
705 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardAddressTakenIatEntryCount */
706 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardLongJumpTargetTable */
707 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardLongJumpTargetCount */
708 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* DynamicValueRelocTable */
709 if (target.cpu == CPU_ARM64EC)
710 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( "__chpe_metadata" ));
711 else
712 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* CHPEMetadataPointer */
713 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFFailureRoutine */
714 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFFailureRoutineFunctionPointer */
715 output( "\t.long 0\n" ); /* DynamicValueRelocTableOffset */
716 output( "\t.short 0\n" ); /* DynamicValueRelocTableSection */
717 output( "\t.short 0\n" ); /* Reserved2 */
718 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardRFVerifyStackPointerFunctionPointer */
719 output( "\t.long 0\n" ); /* HotPatchTableOffset */
720 output( "\t.long 0\n" ); /* Reserved3 */
721 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* EnclaveConfigurationPointer */
722 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* VolatileMetadataPointer */
723 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardEHContinuationTable */
724 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardEHContinuationCount */
725 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGCheckFunctionPointer */
726 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGDispatchFunctionPointer */
727 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardXFGTableDispatchFunctionPointer */
728 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* CastGuardOsDeterminedFailureMode */
729 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* GuardMemcpyFunctionPointer */
733 /*******************************************************************
734 * output_module
736 * Output the module data.
738 void output_module( DLLSPEC *spec )
740 int machine = 0;
741 int i;
742 unsigned int page_size = get_page_size();
743 const char *data_dirs[16] = { NULL };
745 /* Reserve some space for the PE header */
747 switch (target.platform)
749 case PLATFORM_MINGW:
750 case PLATFORM_WINDOWS:
751 return; /* nothing to do */
752 case PLATFORM_APPLE:
753 output( "\t.text\n" );
754 output( "\t.balign %u\n", page_size );
755 output( "__wine_spec_pe_header:\n" );
756 output( "\t.space 65536\n" );
757 break;
758 case PLATFORM_SOLARIS:
759 output( "\n\t.section \".text\",\"ax\"\n" );
760 output( "__wine_spec_pe_header:\n" );
761 output( "\t.skip %u\n", 65536 + page_size );
762 break;
763 default:
764 output( "\n\t.section \".init\",\"ax\"\n" );
765 output( "\tjmp 1f\n" );
766 output( "__wine_spec_pe_header:\n" );
767 output( "\t.skip %u\n", 65536 + page_size );
768 output( "1:\n" );
769 break;
772 /* Output the NT header */
774 output( "\n\t.data\n" );
775 output( "\t.balign %u\n", get_ptr_size() );
776 output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
777 output( "%s:\n", asm_name("__wine_spec_nt_header") );
778 output( ".L__wine_spec_rva_base:\n" );
780 output( "\t.long 0x4550\n" ); /* Signature */
781 switch (target.cpu)
783 case CPU_i386: machine = IMAGE_FILE_MACHINE_I386; break;
784 case CPU_ARM64EC:
785 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
786 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
787 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
789 output( "\t.short 0x%04x\n", /* Machine */
790 machine );
791 output( "\t.short 0\n" ); /* NumberOfSections */
792 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
793 output( "\t.long 0\n" ); /* PointerToSymbolTable */
794 output( "\t.long 0\n" ); /* NumberOfSymbols */
795 output( "\t.short %d\n", /* SizeOfOptionalHeader */
796 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
797 output( "\t.short 0x%04x\n", /* Characteristics */
798 spec->characteristics );
799 output( "\t.short 0x%04x\n", /* Magic */
800 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
801 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
802 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
803 output( "\t.long 0\n" ); /* SizeOfCode */
804 output( "\t.long 0\n" ); /* SizeOfInitializedData */
805 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
807 for (i = 0; i < spec_extra_ld_symbols.count; i++)
808 output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols.str[i]) );
810 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
811 output( "\t%s %s\n", /* AddressOfEntryPoint */
812 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
813 if (get_ptr_size() == 4)
815 output( "\t.long 0\n" ); /* BaseOfCode */
816 output( "\t.long 0\n" ); /* BaseOfData */
818 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
819 get_asm_ptr_keyword() );
820 output( "\t.long %u\n", page_size ); /* SectionAlignment */
821 output( "\t.long %u\n", page_size ); /* FileAlignment */
822 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
823 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
824 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
825 spec->subsystem_major, spec->subsystem_minor );
826 output( "\t.long 0\n" ); /* Win32VersionValue */
827 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
828 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
829 output( "\t.long 0\n" ); /* CheckSum */
830 output( "\t.short 0x%04x\n", /* Subsystem */
831 spec->subsystem );
832 output( "\t.short 0x%04x\n", /* DllCharacteristics */
833 spec->dll_characteristics );
834 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
835 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
836 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
837 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
838 output( "\t.long 0\n" ); /* LoaderFlags */
839 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
841 if (get_exports_count( &spec->exports ))
842 data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
843 if (has_imports())
844 data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
845 if (spec->nb_resources)
846 data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
847 if (has_delay_imports())
848 data_dirs[13] = ".L__wine_spec_delay_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
850 output_data_directories( data_dirs );
852 if (target.platform == PLATFORM_APPLE)
853 output( "\t.lcomm %s,4\n", asm_name("_end") );
857 /*******************************************************************
858 * output_spec32_file
860 * Build a Win32 C file from a spec file.
862 void output_spec32_file( DLLSPEC *spec )
864 needs_get_pc_thunk = 0;
865 open_output_file();
866 output_standard_file_header();
867 output_module( spec );
868 output_stubs( spec );
869 output_exports( spec );
870 output_imports( spec );
871 if (needs_get_pc_thunk) output_get_pc_thunk();
872 output_load_config();
873 output_resources( spec );
874 output_gnu_stack_note();
875 close_output_file();
879 struct sec_data
881 char name[8];
882 const void *ptr;
883 unsigned int size;
884 unsigned int flags;
885 unsigned int file_size;
886 unsigned int virt_size;
887 unsigned int filepos;
888 unsigned int rva;
891 struct dir_data
893 unsigned int rva;
894 unsigned int size;
897 struct exp_data
899 unsigned int rva;
900 const char *name;
903 static struct
905 unsigned int section_align;
906 unsigned int file_align;
907 unsigned int sec_count;
908 unsigned int exp_count;
909 struct dir_data dir[16];
910 struct sec_data sec[8];
911 struct exp_data exp[8];
912 } pe;
914 static void set_dir( unsigned int idx, unsigned int rva, unsigned int size )
916 pe.dir[idx].rva = rva;
917 pe.dir[idx].size = size;
920 static void add_export( unsigned int rva, const char *name )
922 pe.exp[pe.exp_count].rva = rva;
923 pe.exp[pe.exp_count].name = name;
924 pe.exp_count++;
927 static unsigned int current_rva(void)
929 if (!pe.sec_count) return pe.section_align;
930 return pe.sec[pe.sec_count - 1].rva + pe.sec[pe.sec_count - 1].virt_size;
933 static unsigned int current_filepos(void)
935 if (!pe.sec_count) return pe.file_align;
936 return pe.sec[pe.sec_count - 1].filepos + pe.sec[pe.sec_count - 1].file_size;
939 static unsigned int flush_output_to_section( const char *name, int dir_idx, unsigned int flags )
941 struct sec_data *sec = &pe.sec[pe.sec_count];
943 if (!output_buffer_pos) return 0;
945 strncpy( sec->name, name, sizeof(sec->name) );
946 sec->ptr = output_buffer;
947 sec->size = output_buffer_pos;
948 sec->flags = flags;
949 sec->rva = current_rva();
950 sec->filepos = current_filepos();
951 sec->file_size = (sec->size + pe.file_align - 1) & ~(pe.file_align - 1);
952 sec->virt_size = (sec->size + pe.section_align - 1) & ~(pe.section_align - 1);
953 if (dir_idx >= 0) set_dir( dir_idx, sec->rva, sec->size );
954 init_output_buffer();
955 pe.sec_count++;
956 return sec->size;
959 static void output_pe_exports( DLLSPEC *spec )
961 struct exports *exports = &spec->exports;
962 unsigned int i, exp_count = get_exports_count( exports );
963 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
964 unsigned int pos, str_rva = exp_rva + 4 * exp_count + 6 * exports->nb_names;
966 if (!exports->nb_entry_points) return;
968 init_output_buffer();
969 put_dword( 0 ); /* Characteristics */
970 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
971 put_word( 0 ); /* MajorVersion */
972 put_word( 0 ); /* MinorVersion */
973 put_dword( str_rva ); /* Name */
974 put_dword( exports->base ); /* Base */
975 put_dword( exp_count ); /* NumberOfFunctions */
976 put_dword( exports->nb_names ); /* NumberOfNames */
977 put_dword( exp_rva ); /* AddressOfFunctions */
978 if (exports->nb_names)
980 put_dword( exp_rva + 4 * exp_count ); /* AddressOfNames */
981 put_dword( exp_rva + 4 * exp_count + 4 * exports->nb_names ); /* AddressOfNameOrdinals */
983 else
985 put_dword( 0 ); /* AddressOfNames */
986 put_dword( 0 ); /* AddressOfNameOrdinals */
989 /* functions */
990 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
991 pos += strlen( exports->names[i]->name ) + 1;
992 for (i = exports->base; i <= exports->limit; i++)
994 ORDDEF *odp = exports->ordinals[i];
995 if (odp && (odp->flags & FLAG_FORWARD))
997 put_dword( pos );
998 pos += strlen(odp->link_name) + 1;
1000 else put_dword( 0 );
1003 /* names */
1004 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < exports->nb_names; i++)
1006 put_dword( pos );
1007 pos += strlen(exports->names[i]->name) + 1;
1010 /* ordinals */
1011 for (i = 0; i < exports->nb_names; i++) put_word( exports->names[i]->ordinal - exports->base );
1013 /* strings */
1014 put_data( spec->file_name, strlen(spec->file_name) + 1 );
1015 for (i = 0; i < exports->nb_names; i++)
1016 put_data( exports->names[i]->name, strlen(exports->names[i]->name) + 1 );
1018 for (i = exports->base; i <= exports->limit; i++)
1020 ORDDEF *odp = exports->ordinals[i];
1021 if (odp && (odp->flags & FLAG_FORWARD)) put_data( odp->link_name, strlen(odp->link_name) + 1 );
1024 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1025 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1029 /* apiset hash table */
1030 struct apiset_hash_entry
1032 unsigned int hash;
1033 unsigned int index;
1036 static int apiset_hash_cmp( const void *h1, const void *h2 )
1038 const struct apiset_hash_entry *entry1 = h1;
1039 const struct apiset_hash_entry *entry2 = h2;
1041 if (entry1->hash > entry2->hash) return 1;
1042 if (entry1->hash < entry2->hash) return -1;
1043 return 0;
1046 static void output_apiset_section( const struct apiset *apiset )
1048 struct apiset_hash_entry *hash;
1049 struct apiset_entry *e;
1050 unsigned int i, j, str_pos, value_pos, hash_pos, size;
1052 init_output_buffer();
1054 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
1055 str_pos = value_pos;
1056 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1057 str_pos += 0x14 * max( 1, e->val_count ); /* values */
1059 hash_pos = str_pos + ((apiset->str_pos * 2 + 3) & ~3);
1060 size = hash_pos + apiset->count * 8; /* hashes */
1062 /* header */
1064 put_dword( 6 ); /* Version */
1065 put_dword( size ); /* Size */
1066 put_dword( 0 ); /* Flags */
1067 put_dword( apiset->count ); /* Count */
1068 put_dword( 0x1c ); /* EntryOffset */
1069 put_dword( hash_pos ); /* HashOffset */
1070 put_dword( apiset_hash_factor ); /* HashFactor */
1072 /* name entries */
1074 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
1075 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1077 put_dword( 1 ); /* Flags */
1078 put_dword( str_pos + e->name_off * 2 ); /* NameOffset */
1079 put_dword( e->name_len * 2 ); /* NameLength */
1080 put_dword( e->hash_len * 2 ); /* HashedLength */
1081 put_dword( value_pos ); /* ValueOffset */
1082 put_dword( max( 1, e->val_count )); /* ValueCount */
1083 value_pos += 0x14 * max( 1, e->val_count );
1086 /* values */
1088 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1090 if (!e->val_count)
1092 put_dword( 0 ); /* Flags */
1093 put_dword( 0 ); /* NameOffset */
1094 put_dword( 0 ); /* NameLength */
1095 put_dword( 0 ); /* ValueOffset */
1096 put_dword( 0 ); /* ValueLength */
1098 else for (j = 0; j < e->val_count; j++)
1100 put_dword( 0 ); /* Flags */
1101 if (e->values[j].name_off)
1103 put_dword( str_pos + e->values[j].name_off * 2 ); /* NameOffset */
1104 put_dword( e->values[j].name_len * 2 ); /* NameLength */
1106 else
1108 put_dword( 0 ); /* NameOffset */
1109 put_dword( 0 ); /* NameLength */
1111 put_dword( str_pos + e->values[j].val_off * 2 ); /* ValueOffset */
1112 put_dword( e->values[j].val_len * 2 ); /* ValueLength */
1116 /* strings */
1118 for (i = 0; i < apiset->str_pos; i++) put_word( apiset->strings[i] );
1119 align_output( 4 );
1121 /* hash table */
1123 hash = xmalloc( apiset->count * sizeof(*hash) );
1124 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1126 hash[i].hash = e->hash;
1127 hash[i].index = i;
1129 qsort( hash, apiset->count, sizeof(*hash), apiset_hash_cmp );
1130 for (i = 0; i < apiset->count; i++)
1132 put_dword( hash[i].hash );
1133 put_dword( hash[i].index );
1135 free( hash );
1137 flush_output_to_section( ".apiset", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1141 static void output_pe_file( DLLSPEC *spec, const char signature[32] )
1143 const unsigned int lfanew = 0x40 + 32;
1144 unsigned int i, code_size = 0, data_size = 0;
1146 init_output_buffer();
1148 for (i = 0; i < pe.sec_count; i++)
1149 if (pe.sec[i].flags & 0x20) /* CNT_CODE */
1150 code_size += pe.sec[i].file_size;
1152 /* .rsrc section */
1153 if (spec->type == SPEC_WIN32)
1155 output_bin_resources( spec, current_rva() );
1156 flush_output_to_section( ".rsrc", 2 /* IMAGE_DIRECTORY_ENTRY_RESOURCE */,
1157 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1160 /* .reloc section */
1161 if (code_size)
1163 put_dword( 0 ); /* VirtualAddress */
1164 put_dword( 0 ); /* Size */
1165 flush_output_to_section( ".reloc", 5 /* IMAGE_DIRECTORY_ENTRY_BASERELOC */,
1166 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ );
1169 for (i = 0; i < pe.sec_count; i++)
1170 if ((pe.sec[i].flags & 0x60) == 0x40) /* CNT_INITIALIZED_DATA */
1171 data_size += pe.sec[i].file_size;
1173 put_word( 0x5a4d ); /* e_magic */
1174 put_word( 0x40 ); /* e_cblp */
1175 put_word( 0x01 ); /* e_cp */
1176 put_word( 0 ); /* e_crlc */
1177 put_word( lfanew / 16 ); /* e_cparhdr */
1178 put_word( 0x0000 ); /* e_minalloc */
1179 put_word( 0xffff ); /* e_maxalloc */
1180 put_word( 0x0000 ); /* e_ss */
1181 put_word( 0x00b8 ); /* e_sp */
1182 put_word( 0 ); /* e_csum */
1183 put_word( 0 ); /* e_ip */
1184 put_word( 0 ); /* e_cs */
1185 put_word( lfanew ); /* e_lfarlc */
1186 put_word( 0 ); /* e_ovno */
1187 put_dword( 0 ); /* e_res */
1188 put_dword( 0 );
1189 put_word( 0 ); /* e_oemid */
1190 put_word( 0 ); /* e_oeminfo */
1191 put_dword( 0 ); /* e_res2 */
1192 put_dword( 0 );
1193 put_dword( 0 );
1194 put_dword( 0 );
1195 put_dword( 0 );
1196 put_dword( lfanew );
1198 put_data( signature, 32 );
1200 put_dword( 0x4550 ); /* Signature */
1201 switch (target.cpu)
1203 case CPU_i386: put_word( IMAGE_FILE_MACHINE_I386 ); break;
1204 case CPU_ARM64EC:
1205 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
1206 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
1207 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
1209 put_word( pe.sec_count ); /* NumberOfSections */
1210 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1211 put_dword( 0 ); /* PointerToSymbolTable */
1212 put_dword( 0 ); /* NumberOfSymbols */
1213 put_word( get_ptr_size() == 8 ?
1214 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
1215 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
1216 put_word( spec->characteristics ); /* Characteristics */
1217 put_word( get_ptr_size() == 8 ?
1218 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
1219 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
1220 put_byte( 7 ); /* MajorLinkerVersion */
1221 put_byte( 10 ); /* MinorLinkerVersion */
1222 put_dword( code_size ); /* SizeOfCode */
1223 put_dword( data_size ); /* SizeOfInitializedData */
1224 put_dword( 0 ); /* SizeOfUninitializedData */
1225 put_dword( code_size ? pe.sec[0].rva : 0 ); /* AddressOfEntryPoint */
1226 put_dword( code_size ? pe.sec[0].rva : 0 ); /* BaseOfCode */
1227 if (get_ptr_size() == 4)
1229 put_dword( 0 ); /* BaseOfData */
1230 put_dword( 0x10000000 ); /* ImageBase */
1232 else
1234 put_dword( 0x80000000 ); /* ImageBase */
1235 put_dword( 0x00000001 );
1237 put_dword( pe.section_align ); /* SectionAlignment */
1238 put_dword( pe.file_align ); /* FileAlignment */
1239 put_word( 1 ); /* MajorOperatingSystemVersion */
1240 put_word( 0 ); /* MinorOperatingSystemVersion */
1241 put_word( 0 ); /* MajorImageVersion */
1242 put_word( 0 ); /* MinorImageVersion */
1243 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
1244 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
1245 put_dword( 0 ); /* Win32VersionValue */
1246 put_dword( current_rva() ); /* SizeOfImage */
1247 put_dword( pe.file_align ); /* SizeOfHeaders */
1248 put_dword( 0 ); /* CheckSum */
1249 put_word( spec->subsystem ); /* Subsystem */
1250 put_word( spec->dll_characteristics ); /* DllCharacteristics */
1251 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
1252 put_pword( pe.section_align ); /* SizeOfStackCommit */
1253 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
1254 put_pword( pe.section_align ); /* SizeOfHeapCommit */
1255 put_dword( 0 ); /* LoaderFlags */
1256 put_dword( 16 ); /* NumberOfRvaAndSizes */
1258 /* image directories */
1259 for (i = 0; i < 16; i++)
1261 put_dword( pe.dir[i].rva ); /* VirtualAddress */
1262 put_dword( pe.dir[i].size ); /* Size */
1265 /* sections */
1266 for (i = 0; i < pe.sec_count; i++)
1268 put_data( pe.sec[i].name, 8 ); /* Name */
1269 put_dword( pe.sec[i].size ); /* VirtualSize */
1270 put_dword( pe.sec[i].rva ); /* VirtualAddress */
1271 put_dword( pe.sec[i].file_size ); /* SizeOfRawData */
1272 put_dword( pe.sec[i].filepos ); /* PointerToRawData */
1273 put_dword( 0 ); /* PointerToRelocations */
1274 put_dword( 0 ); /* PointerToLinenumbers */
1275 put_word( 0 ); /* NumberOfRelocations */
1276 put_word( 0 ); /* NumberOfLinenumbers */
1277 put_dword( pe.sec[i].flags ); /* Characteristics */
1279 align_output( pe.file_align );
1281 /* section data */
1282 for (i = 0; i < pe.sec_count; i++)
1284 put_data( pe.sec[i].ptr, pe.sec[i].size );
1285 align_output( pe.file_align );
1288 flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
1291 /*******************************************************************
1292 * output_fake_module
1294 * Build a fake binary module from a spec file.
1296 void output_fake_module( DLLSPEC *spec )
1298 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
1299 0xc2, 0x0c, 0x00 }; /* ret $12 */
1301 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
1302 0xc2, 0x04, 0x00 }; /* ret $4 */
1303 unsigned int i;
1305 resolve_imports( spec );
1306 pe.section_align = get_page_size();
1307 pe.file_align = 0x200;
1308 init_output_buffer();
1310 /* .text section */
1311 if (spec->characteristics & IMAGE_FILE_DLL) put_data( dll_code_section, sizeof(dll_code_section) );
1312 else put_data( exe_code_section, sizeof(exe_code_section) );
1313 flush_output_to_section( ".text", -1, 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ );
1315 if (spec->type == SPEC_WIN16)
1317 add_export( current_rva(), "__wine_spec_dos_header" );
1319 /* .rdata section */
1320 output_fake_module16( spec );
1321 if (spec->main_module)
1323 add_export( current_rva() + output_buffer_pos, "__wine_spec_main_module" );
1324 put_data( spec->main_module, strlen(spec->main_module) + 1 );
1326 flush_output_to_section( ".rdata", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1329 /* .edata section */
1330 if (pe.exp_count)
1332 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
1333 unsigned int pos, str_rva = exp_rva + 10 * pe.exp_count;
1335 put_dword( 0 ); /* Characteristics */
1336 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1337 put_word( 0 ); /* MajorVersion */
1338 put_word( 0 ); /* MinorVersion */
1339 put_dword( str_rva ); /* Name */
1340 put_dword( 1 ); /* Base */
1341 put_dword( pe.exp_count ); /* NumberOfFunctions */
1342 put_dword( pe.exp_count ); /* NumberOfNames */
1343 put_dword( exp_rva ); /* AddressOfFunctions */
1344 put_dword( exp_rva + 4 * pe.exp_count ); /* AddressOfNames */
1345 put_dword( exp_rva + 8 * pe.exp_count ); /* AddressOfNameOrdinals */
1347 /* functions */
1348 for (i = 0; i < pe.exp_count; i++) put_dword( pe.exp[i].rva );
1349 /* names */
1350 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < pe.exp_count; i++)
1352 put_dword( pos );
1353 pos += strlen( pe.exp[i].name ) + 1;
1355 /* ordinals */
1356 for (i = 0; i < pe.exp_count; i++) put_word( i );
1357 /* strings */
1358 put_data( spec->file_name, strlen(spec->file_name) + 1 );
1359 for (i = 0; i < pe.exp_count; i++) put_data( pe.exp[i].name, strlen(pe.exp[i].name) + 1 );
1360 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1361 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1364 output_pe_file( spec, fakedll_signature );
1368 /*******************************************************************
1369 * output_data_module
1371 * Build a data-only module from a spec file.
1373 void output_data_module( DLLSPEC *spec )
1375 pe.section_align = pe.file_align = get_page_size();
1377 output_pe_exports( spec );
1378 if (spec->apiset.count) output_apiset_section( &spec->apiset );
1379 output_pe_file( spec, builtin_signature );
1383 /*******************************************************************
1384 * output_def_file
1386 * Build a Win32 def file from a spec file.
1388 void output_def_file( DLLSPEC *spec, struct exports *exports, int import_only )
1390 DLLSPEC *spec32 = NULL;
1391 const char *name;
1392 int i, total;
1394 if (spec->type == SPEC_WIN16)
1396 spec32 = alloc_dll_spec();
1397 add_16bit_exports( spec32, spec );
1398 spec = spec32;
1399 exports = &spec->exports;
1402 if (spec_file_name)
1403 output( "; File generated automatically from %s; do not edit!\n\n",
1404 spec_file_name );
1405 else
1406 output( "; File generated automatically; do not edit!\n\n" );
1408 output( "LIBRARY %s\n\n", spec->file_name);
1409 output( "EXPORTS\n");
1411 /* Output the exports and relay entry points */
1413 for (i = total = 0; i < exports->nb_entry_points; i++)
1415 const ORDDEF *odp = exports->entry_points[i];
1416 int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1418 if (odp->name) name = odp->name;
1419 else if (odp->export_name) name = odp->export_name;
1420 else continue;
1422 if (!is_private) total++;
1423 if (import_only && odp->type == TYPE_STUB) continue;
1425 if ((odp->flags & FLAG_FASTCALL) && is_pe())
1426 name = strmake( "@%s", name );
1428 output( " %s", name );
1430 switch(odp->type)
1432 case TYPE_EXTERN:
1433 is_data = 1;
1434 /* fall through */
1435 case TYPE_VARARGS:
1436 case TYPE_CDECL:
1437 /* try to reduce output */
1438 if(!import_only && (strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD)))
1439 output( "=%s", odp->link_name );
1440 break;
1441 case TYPE_STDCALL:
1443 int at_param = get_args_size( odp );
1444 if (!kill_at && target.cpu == CPU_i386) output( "@%d", at_param );
1445 if (import_only) break;
1446 if (odp->flags & FLAG_FORWARD)
1447 output( "=%s", odp->link_name );
1448 else if (strcmp(name, odp->link_name)) /* try to reduce output */
1449 output( "=%s", get_link_name( odp ));
1450 break;
1452 case TYPE_STUB:
1453 if (!kill_at && target.cpu == CPU_i386) output( "@%d", get_args_size( odp ));
1454 is_private = 1;
1455 break;
1456 default:
1457 assert(0);
1459 output( " @%d", odp->ordinal );
1460 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1461 if (is_data) output( " DATA" );
1462 if (is_private) output( " PRIVATE" );
1463 output( "\n" );
1465 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1466 if (spec32) free_dll_spec( spec32 );
1470 /*******************************************************************
1471 * make_builtin_files
1473 void make_builtin_files( struct strarray files )
1475 int i, fd;
1476 struct
1478 unsigned short e_magic;
1479 unsigned short unused[29];
1480 unsigned int e_lfanew;
1481 } header;
1483 for (i = 0; i < files.count; i++)
1485 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1486 fatal_perror( "Cannot open %s", files.str[i] );
1487 if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1489 if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1490 fatal_error( "%s: Not enough space (%x) for Wine signature\n", files.str[i], header.e_lfanew );
1491 write( fd, builtin_signature, sizeof(builtin_signature) );
1493 if (prefer_native)
1495 unsigned int pos = header.e_lfanew + 0x5e; /* OptionalHeader.DllCharacteristics */
1496 unsigned short dll_charact;
1497 lseek( fd, pos, SEEK_SET );
1498 if (read( fd, &dll_charact, sizeof(dll_charact) ) == sizeof(dll_charact))
1500 dll_charact |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
1501 lseek( fd, pos, SEEK_SET );
1502 write( fd, &dll_charact, sizeof(dll_charact) );
1506 else fatal_error( "%s: Unrecognized file format\n", files.str[i] );
1507 close( fd );
1511 static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1513 struct
1515 unsigned char e_ident[16];
1516 unsigned short e_type;
1517 unsigned short e_machine;
1518 unsigned int e_version;
1519 unsigned int e_entry;
1520 unsigned int e_phoff;
1521 unsigned int e_shoff;
1522 unsigned int e_flags;
1523 unsigned short e_ehsize;
1524 unsigned short e_phentsize;
1525 unsigned short e_phnum;
1526 unsigned short e_shentsize;
1527 unsigned short e_shnum;
1528 unsigned short e_shstrndx;
1529 } *elf = header;
1530 struct
1532 unsigned int p_type;
1533 unsigned int p_offset;
1534 unsigned int p_vaddr;
1535 unsigned int p_paddr;
1536 unsigned int p_filesz;
1537 unsigned int p_memsz;
1538 unsigned int p_flags;
1539 unsigned int p_align;
1540 } *phdr;
1541 struct
1543 unsigned int d_tag;
1544 unsigned int d_val;
1545 } *dyn;
1547 unsigned int i, size;
1549 if (header_size < sizeof(*elf)) return;
1550 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1552 size = elf->e_phnum * elf->e_phentsize;
1553 phdr = xmalloc( size );
1554 lseek( fd, elf->e_phoff, SEEK_SET );
1555 if (read( fd, phdr, size ) != size) return;
1557 for (i = 0; i < elf->e_phnum; i++)
1559 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1560 phdr = (void *)((char *)phdr + elf->e_phentsize);
1562 if (i == elf->e_phnum) return;
1564 dyn = xmalloc( phdr->p_filesz );
1565 lseek( fd, phdr->p_offset, SEEK_SET );
1566 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1567 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1569 switch (dyn[i].d_tag)
1571 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1572 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1573 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1576 lseek( fd, phdr->p_offset, SEEK_SET );
1577 write( fd, dyn, phdr->p_filesz );
1580 static void fixup_elf64( const char *name, int fd, void *header, size_t header_size )
1582 struct
1584 unsigned char e_ident[16];
1585 unsigned short e_type;
1586 unsigned short e_machine;
1587 unsigned int e_version;
1588 unsigned __int64 e_entry;
1589 unsigned __int64 e_phoff;
1590 unsigned __int64 e_shoff;
1591 unsigned int e_flags;
1592 unsigned short e_ehsize;
1593 unsigned short e_phentsize;
1594 unsigned short e_phnum;
1595 unsigned short e_shentsize;
1596 unsigned short e_shnum;
1597 unsigned short e_shstrndx;
1598 } *elf = header;
1599 struct
1601 unsigned int p_type;
1602 unsigned int p_flags;
1603 unsigned __int64 p_offset;
1604 unsigned __int64 p_vaddr;
1605 unsigned __int64 p_paddr;
1606 unsigned __int64 p_filesz;
1607 unsigned __int64 p_memsz;
1608 unsigned __int64 p_align;
1609 } *phdr;
1610 struct
1612 unsigned __int64 d_tag;
1613 unsigned __int64 d_val;
1614 } *dyn;
1616 unsigned int i, size;
1618 if (header_size < sizeof(*elf)) return;
1619 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1621 size = elf->e_phnum * elf->e_phentsize;
1622 phdr = xmalloc( size );
1623 lseek( fd, elf->e_phoff, SEEK_SET );
1624 if (read( fd, phdr, size ) != size) return;
1626 for (i = 0; i < elf->e_phnum; i++)
1628 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1629 phdr = (void *)((char *)phdr + elf->e_phentsize);
1631 if (i == elf->e_phnum) return;
1633 dyn = xmalloc( phdr->p_filesz );
1634 lseek( fd, phdr->p_offset, SEEK_SET );
1635 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1636 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1638 switch (dyn[i].d_tag)
1640 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1641 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1642 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1645 lseek( fd, phdr->p_offset, SEEK_SET );
1646 write( fd, dyn, phdr->p_filesz );
1649 /*******************************************************************
1650 * fixup_constructors
1652 void fixup_constructors( struct strarray files )
1654 int i, fd, size;
1655 unsigned int header[64];
1657 for (i = 0; i < files.count; i++)
1659 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1660 fatal_perror( "Cannot open %s", files.str[i] );
1661 size = read( fd, &header, sizeof(header) );
1662 if (size > 5)
1664 if (!memcmp( header, "\177ELF\001", 5 )) fixup_elf32( files.str[i], fd, header, size );
1665 else if (!memcmp( header, "\177ELF\002", 5 )) fixup_elf64( files.str[i], fd, header, size );
1667 close( fd );