ws2_32: Test wait alertability in WSAGetOverlappedResult().
[wine.git] / tools / winebuild / spec32.c
blob0cf8d7836334ff5ed104cf35c477a18fa8789e97
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 for (i = spec->base; i <= spec->limit; i++)
103 ORDDEF *odp = spec->ordinals[i];
104 if (needs_relay( odp )) return 1;
106 return 0;
109 static int get_exports_count( DLLSPEC *spec )
111 if (spec->base > spec->limit) return 0;
112 return spec->limit - spec->base + 1;
115 static int cmp_func_args( const void *p1, const void *p2 )
117 const ORDDEF *odp1 = *(const ORDDEF **)p1;
118 const ORDDEF *odp2 = *(const ORDDEF **)p2;
120 return odp2->u.func.nb_args - odp1->u.func.nb_args;
123 static void get_arg_string( ORDDEF *odp, char str[MAX_ARGUMENTS + 1] )
125 int i;
127 for (i = 0; i < odp->u.func.nb_args; i++)
129 switch (odp->u.func.args[i])
131 case ARG_STR: str[i] = 's'; break;
132 case ARG_WSTR: str[i] = 'w'; break;
133 case ARG_FLOAT: str[i] = 'f'; break;
134 case ARG_DOUBLE: str[i] = 'd'; break;
135 case ARG_INT64:
136 case ARG_INT128:
137 if (get_ptr_size() == 4)
139 str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
140 break;
142 /* fall through */
143 case ARG_LONG:
144 case ARG_PTR:
145 default:
146 str[i] = 'i';
147 break;
150 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) str[0] = 't';
151 if ((odp->flags & FLAG_FASTCALL) && odp->u.func.nb_args > 1) str[1] = 't';
153 /* append return value */
154 if (get_ptr_size() == 4 && (odp->flags & FLAG_RET64))
155 strcpy( str + i, "J" );
156 else
157 strcpy( str + i, "I" );
160 static void output_data_directories( const char *names[16] )
162 int i;
164 for (i = 0; i < 16; i++)
166 if (names[i])
168 output_rva( "%s", names[i] );
169 output( "\t.long %s_end - %s\n", names[i], names[i] );
171 else output( "\t.long 0,0\n" );
175 /*******************************************************************
176 * build_args_string
178 static char *build_args_string( DLLSPEC *spec )
180 int i, count = 0, len = 1;
181 char *p, *buffer;
182 char str[MAX_ARGUMENTS + 2];
183 ORDDEF **funcs;
185 funcs = xmalloc( (spec->limit + 1 - spec->base) * sizeof(*funcs) );
186 for (i = spec->base; i <= spec->limit; i++)
188 ORDDEF *odp = spec->ordinals[i];
190 if (!needs_relay( odp )) continue;
191 funcs[count++] = odp;
192 len += odp->u.func.nb_args + 1;
194 /* sort functions by decreasing number of arguments */
195 qsort( funcs, count, sizeof(*funcs), cmp_func_args );
196 buffer = xmalloc( len );
197 buffer[0] = 0;
198 /* build the arguments string, reusing substrings where possible */
199 for (i = 0; i < count; i++)
201 get_arg_string( funcs[i], str );
202 if (!(p = strstr( buffer, str )))
204 p = buffer + strlen( buffer );
205 strcpy( p, str );
207 funcs[i]->u.func.args_str_offset = p - buffer;
209 free( funcs );
210 return buffer;
213 /*******************************************************************
214 * output_relay_debug
216 * Output entry points for relay debugging
218 static void output_relay_debug( DLLSPEC *spec )
220 int i;
222 /* first the table of entry point offsets */
224 output( "\t%s\n", get_asm_rodata_section() );
225 output( "\t.align %d\n", get_alignment(4) );
226 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
228 for (i = spec->base; i <= spec->limit; i++)
230 ORDDEF *odp = spec->ordinals[i];
232 if (needs_relay( odp ))
233 output( "\t.long __wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
234 else
235 output( "\t.long 0\n" );
238 /* then the strings of argument types */
240 output( ".L__wine_spec_relay_args_string:\n" );
241 output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( spec ));
243 /* then the relay thunks */
245 output( "\t.text\n" );
246 if (thumb_mode) output( "\t.thumb_func\n" );
247 output( "__wine_spec_relay_entry_points:\n" );
248 output( "\tnop\n" ); /* to avoid 0 offset */
250 for (i = spec->base; i <= spec->limit; i++)
252 ORDDEF *odp = spec->ordinals[i];
254 if (!needs_relay( odp )) continue;
256 switch (target.cpu)
258 case CPU_i386:
259 output( "\t.align %d\n", get_alignment(4) );
260 output( "\t.long 0x90909090,0x90909090\n" );
261 output( "__wine_spec_relay_entry_point_%d:\n", i );
262 output_cfi( ".cfi_startproc" );
263 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
264 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) /* add the register arguments */
266 output( "\tpopl %%eax\n" );
267 if ((odp->flags & FLAG_FASTCALL) && get_args_size( odp ) > 4) output( "\tpushl %%edx\n" );
268 output( "\tpushl %%ecx\n" );
269 output( "\tpushl %%eax\n" );
271 output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
272 output_cfi( ".cfi_adjust_cfa_offset 4" );
274 if (UsePIC)
276 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
277 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
278 needs_get_pc_thunk = 1;
280 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
281 output( "\tpushl %%eax\n" );
282 output_cfi( ".cfi_adjust_cfa_offset 4" );
284 output( "\tcall *4(%%eax)\n" );
285 output_cfi( ".cfi_adjust_cfa_offset -8" );
286 if (odp->type == TYPE_STDCALL)
287 output( "\tret $%u\n", get_args_size( odp ));
288 else
289 output( "\tret\n" );
290 output_cfi( ".cfi_endproc" );
291 break;
293 case CPU_ARM:
295 int j, has_float = 0;
297 if (strcmp( float_abi_option, "soft" ))
298 for (j = 0; j < odp->u.func.nb_args && !has_float; j++)
299 has_float = is_float_arg( odp, j );
301 output( "\t.align %d\n", get_alignment(4) );
302 output( "__wine_spec_relay_entry_point_%d:\n", i );
303 output_cfi( ".cfi_startproc" );
304 output( "\tpush {r0-r3}\n" );
305 output( "\tmov r2, SP\n");
306 if (has_float) output( "\tvpush {s0-s15}\n" );
307 output( "\tpush {LR}\n" );
308 output( "\tsub SP, #4\n");
309 output( "\tmovw r1,#%u\n", i - spec->base );
310 output( "\tmovt r1,#%u\n", odp->u.func.args_str_offset );
311 if (UsePIC)
313 output( "\tldr r0, 2f\n");
314 output( "1:\tadd r0, PC\n");
316 else
318 output( "\tmovw r0, :lower16:.L__wine_spec_relay_descr\n" );
319 output( "\tmovt r0, :upper16:.L__wine_spec_relay_descr\n" );
321 output( "\tldr IP, [r0, #4]\n");
322 output( "\tblx IP\n");
323 output( "\tldr IP, [SP, #4]\n" );
324 output( "\tadd SP, #%u\n", 24 + (has_float ? 64 : 0) );
325 output( "\tbx IP\n");
326 if (UsePIC) output( "2:\t.long .L__wine_spec_relay_descr-1b-%u\n", thumb_mode ? 4 : 8 );
327 output_cfi( ".cfi_endproc" );
328 break;
331 case CPU_ARM64:
332 output( "\t.align %d\n", get_alignment(4) );
333 output( "__wine_spec_relay_entry_point_%d:\n", i );
334 output_cfi( ".cfi_startproc" );
335 switch (odp->u.func.nb_args)
337 default:
338 case 8:
339 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
340 /* fall through */
341 case 6:
342 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
343 /* fall through */
344 case 4:
345 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
346 /* fall through */
347 case 2:
348 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
349 /* fall through */
350 case 0: break;
352 output( "\tmov x2, SP\n");
353 output( "\tstp x29, x30, [SP,#-16]!\n" );
354 output( "\tstp x8, x9, [SP,#-16]!\n" );
355 output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
356 if (i - spec->base) output( "\tadd w1, w1, #%u\n", i - spec->base );
357 output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_relay_descr") );
358 output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_relay_descr") );
359 output( "\tldr x3, [x0, #8]\n");
360 output( "\tblr x3\n");
361 output( "\tadd SP, SP, #16\n" );
362 output( "\tldp x29, x30, [SP], #16\n" );
363 if (odp->u.func.nb_args)
364 output( "\tadd SP, SP, #%u\n", 8 * ((min(odp->u.func.nb_args, 8) + 1) & ~1) );
365 output( "\tret\n");
366 output_cfi( ".cfi_endproc" );
367 break;
369 case CPU_x86_64:
370 output( "\t.align %d\n", get_alignment(4) );
371 output( "\t.long 0x90909090,0x90909090\n" );
372 output( "__wine_spec_relay_entry_point_%d:\n", i );
373 output_cfi( ".cfi_startproc" );
374 switch (odp->u.func.nb_args)
376 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
377 /* fall through */
378 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
379 /* fall through */
380 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
381 /* fall through */
382 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
383 /* fall through */
384 case 0: break;
386 output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
387 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
388 output( "\tcallq *8(%%rcx)\n" );
389 output( "\tret\n" );
390 output_cfi( ".cfi_endproc" );
391 break;
393 default:
394 assert(0);
399 /*******************************************************************
400 * output_exports
402 * Output the export table for a Win32 module.
404 void output_exports( DLLSPEC *spec )
406 int i, fwd_size = 0;
407 int needs_imports = 0;
408 int needs_relay = has_relays( spec );
409 int nr_exports = get_exports_count( spec );
410 const char *func_ptr = is_pe() ? ".rva" : get_asm_ptr_keyword();
411 const char *name;
413 if (!nr_exports) return;
415 output( "\n/* export table */\n\n" );
416 output( "\t%s\n", get_asm_export_section() );
417 output( "\t.align %d\n", get_alignment(4) );
418 output( ".L__wine_spec_exports:\n" );
420 /* export directory header */
422 output( "\t.long 0\n" ); /* Characteristics */
423 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
424 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
425 output_rva( ".L__wine_spec_exp_names" ); /* Name */
426 output( "\t.long %u\n", spec->base ); /* Base */
427 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
428 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
429 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
430 if (spec->nb_names)
432 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
433 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
435 else
437 output( "\t.long 0\n" ); /* AddressOfNames */
438 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
441 /* output the function pointers */
443 output( "\n.L__wine_spec_exports_funcs:\n" );
444 for (i = spec->base; i <= spec->limit; i++)
446 ORDDEF *odp = spec->ordinals[i];
447 if (!odp) output( "\t%s 0\n", is_pe() ? ".long" : get_asm_ptr_keyword() );
448 else switch(odp->type)
450 case TYPE_EXTERN:
451 case TYPE_STDCALL:
452 case TYPE_VARARGS:
453 case TYPE_CDECL:
454 if (odp->flags & FLAG_FORWARD)
456 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr, fwd_size );
457 fwd_size += strlen(odp->link_name) + 1;
459 else if ((odp->flags & FLAG_IMPORT) && (target.cpu == CPU_i386 || target.cpu == CPU_x86_64))
461 name = odp->name ? odp->name : odp->export_name;
462 if (name) output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_imp"), name );
463 else output( "\t%s %s_%u\n", func_ptr, asm_name("__wine_spec_imp"), i );
464 needs_imports = 1;
466 else if (odp->flags & FLAG_EXT_LINK)
468 output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_ext_link"), odp->link_name );
470 else
472 output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
474 break;
475 case TYPE_STUB:
476 output( "\t%s %s\n", func_ptr, asm_name( get_stub_name( odp, spec )) );
477 break;
478 default:
479 assert(0);
483 if (spec->nb_names)
485 /* output the function name pointers */
487 int namepos = strlen(spec->file_name) + 1;
489 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
490 for (i = 0; i < spec->nb_names; i++)
492 output_rva( ".L__wine_spec_exp_names + %u", namepos );
493 namepos += strlen(spec->names[i]->name) + 1;
496 /* output the function ordinals */
498 output( "\n.L__wine_spec_exp_ordinals:\n" );
499 for (i = 0; i < spec->nb_names; i++)
501 output( "\t.short %d\n", spec->names[i]->ordinal - spec->base );
503 if (spec->nb_names % 2)
505 output( "\t.short 0\n" );
509 if (needs_relay)
511 output( "\t.long 0xdeb90002\n" ); /* magic */
512 if (is_pe()) output_rva( ".L__wine_spec_relay_descr" );
513 else output( "\t.long 0\n" );
516 /* output the export name strings */
518 output( "\n.L__wine_spec_exp_names:\n" );
519 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
520 for (i = 0; i < spec->nb_names; i++)
521 output( "\t%s \"%s\"\n",
522 get_asm_string_keyword(), spec->names[i]->name );
524 /* output forward strings */
526 if (fwd_size)
528 output( "\n.L__wine_spec_forwards:\n" );
529 for (i = spec->base; i <= spec->limit; i++)
531 ORDDEF *odp = spec->ordinals[i];
532 if (odp && (odp->flags & FLAG_FORWARD))
533 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
537 /* output relays */
539 if (needs_relay)
541 if (is_pe())
543 output( "\t.data\n" );
544 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
546 else
548 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
549 output( ".L__wine_spec_exports_end:\n" );
552 output( ".L__wine_spec_relay_descr:\n" );
553 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
554 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
555 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
556 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
557 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
558 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
560 output_relay_debug( spec );
562 else if (!is_pe())
564 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
565 output( ".L__wine_spec_exports_end:\n" );
566 output( "\t%s 0\n", get_asm_ptr_keyword() );
569 /* output import thunks */
571 if (!needs_imports) return;
572 output( "\t.text\n" );
573 for (i = spec->base; i <= spec->limit; i++)
575 ORDDEF *odp = spec->ordinals[i];
576 if (!odp) continue;
577 if (!(odp->flags & FLAG_IMPORT)) continue;
579 name = odp->name ? odp->name : odp->export_name;
581 output( "\t.align %d\n", get_alignment(4) );
582 output( "\t.long 0x90909090,0x90909090\n" );
583 if (name) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name );
584 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i );
585 output_cfi( ".cfi_startproc" );
587 switch (target.cpu)
589 case CPU_i386:
590 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
591 if (UsePIC)
593 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
594 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp )));
595 needs_get_pc_thunk = 1;
597 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp )));
598 break;
599 case CPU_x86_64:
600 output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
601 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp )));
602 break;
603 default:
604 assert(0);
606 output_cfi( ".cfi_endproc" );
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.align %d\n", get_alignment(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;
658 output( "__wine_spec_pe_header:\n" );
659 output( "\t.skip %u\n", 65536 + page_size );
660 output( "1:\n" );
661 break;
664 /* Output the NT header */
666 output( "\n\t.data\n" );
667 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
668 output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
669 output( "%s:\n", asm_name("__wine_spec_nt_header") );
670 output( ".L__wine_spec_rva_base:\n" );
672 output( "\t.long 0x4550\n" ); /* Signature */
673 switch (target.cpu)
675 case CPU_i386: machine = IMAGE_FILE_MACHINE_I386; break;
676 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
677 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
678 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
680 output( "\t.short 0x%04x\n", /* Machine */
681 machine );
682 output( "\t.short 0\n" ); /* NumberOfSections */
683 output( "\t.long %u\n", hash_filename(spec->file_name) ); /* TimeDateStamp */
684 output( "\t.long 0\n" ); /* PointerToSymbolTable */
685 output( "\t.long 0\n" ); /* NumberOfSymbols */
686 output( "\t.short %d\n", /* SizeOfOptionalHeader */
687 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
688 output( "\t.short 0x%04x\n", /* Characteristics */
689 spec->characteristics );
690 output( "\t.short 0x%04x\n", /* Magic */
691 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
692 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
693 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
694 output( "\t.long 0\n" ); /* SizeOfCode */
695 output( "\t.long 0\n" ); /* SizeOfInitializedData */
696 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
698 for (i = 0; i < spec_extra_ld_symbols.count; i++)
699 output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols.str[i]) );
701 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
702 output( "\t%s %s\n", /* AddressOfEntryPoint */
703 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
704 if (get_ptr_size() == 4)
706 output( "\t.long 0\n" ); /* BaseOfCode */
707 output( "\t.long 0\n" ); /* BaseOfData */
709 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
710 get_asm_ptr_keyword() );
711 output( "\t.long %u\n", page_size ); /* SectionAlignment */
712 output( "\t.long %u\n", page_size ); /* FileAlignment */
713 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
714 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
715 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
716 spec->subsystem_major, spec->subsystem_minor );
717 output( "\t.long 0\n" ); /* Win32VersionValue */
718 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
719 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
720 output( "\t.long 0\n" ); /* CheckSum */
721 output( "\t.short 0x%04x\n", /* Subsystem */
722 spec->subsystem );
723 output( "\t.short 0x%04x\n", /* DllCharacteristics */
724 spec->dll_characteristics );
725 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
726 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
727 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
728 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
729 output( "\t.long 0\n" ); /* LoaderFlags */
730 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
732 if (get_exports_count( spec ))
733 data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
734 if (has_imports())
735 data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
736 if (spec->nb_resources)
737 data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
738 if (has_delay_imports())
739 data_dirs[13] = ".L__wine_spec_delay_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
741 output_data_directories( data_dirs );
743 if (target.platform == PLATFORM_APPLE)
744 output( "\t.lcomm %s,4\n", asm_name("_end") );
748 /*******************************************************************
749 * output_spec32_file
751 * Build a Win32 C file from a spec file.
753 void output_spec32_file( DLLSPEC *spec )
755 needs_get_pc_thunk = 0;
756 open_output_file();
757 output_standard_file_header();
758 output_module( spec );
759 output_stubs( spec );
760 output_exports( spec );
761 output_imports( spec );
762 output_syscalls( spec );
763 if (needs_get_pc_thunk) output_get_pc_thunk();
764 output_resources( spec );
765 output_gnu_stack_note();
766 close_output_file();
770 struct sec_data
772 char name[8];
773 const void *ptr;
774 unsigned int size;
775 unsigned int flags;
776 unsigned int file_size;
777 unsigned int virt_size;
778 unsigned int filepos;
779 unsigned int rva;
782 struct dir_data
784 unsigned int rva;
785 unsigned int size;
788 struct exp_data
790 unsigned int rva;
791 const char *name;
794 static struct
796 unsigned int section_align;
797 unsigned int file_align;
798 unsigned int sec_count;
799 unsigned int exp_count;
800 struct dir_data dir[16];
801 struct sec_data sec[8];
802 struct exp_data exp[8];
803 } pe;
805 static void set_dir( unsigned int idx, unsigned int rva, unsigned int size )
807 pe.dir[idx].rva = rva;
808 pe.dir[idx].size = size;
811 static void add_export( unsigned int rva, const char *name )
813 pe.exp[pe.exp_count].rva = rva;
814 pe.exp[pe.exp_count].name = name;
815 pe.exp_count++;
818 static unsigned int current_rva(void)
820 if (!pe.sec_count) return pe.section_align;
821 return pe.sec[pe.sec_count - 1].rva + pe.sec[pe.sec_count - 1].virt_size;
824 static unsigned int current_filepos(void)
826 if (!pe.sec_count) return pe.file_align;
827 return pe.sec[pe.sec_count - 1].filepos + pe.sec[pe.sec_count - 1].file_size;
830 static unsigned int flush_output_to_section( const char *name, int dir_idx, unsigned int flags )
832 struct sec_data *sec = &pe.sec[pe.sec_count];
834 if (!output_buffer_pos) return 0;
836 strncpy( sec->name, name, sizeof(sec->name) );
837 sec->ptr = output_buffer;
838 sec->size = output_buffer_pos;
839 sec->flags = flags;
840 sec->rva = current_rva();
841 sec->filepos = current_filepos();
842 sec->file_size = (sec->size + pe.file_align - 1) & ~(pe.file_align - 1);
843 sec->virt_size = (sec->size + pe.section_align - 1) & ~(pe.section_align - 1);
844 if (dir_idx >= 0) set_dir( dir_idx, sec->rva, sec->size );
845 init_output_buffer();
846 pe.sec_count++;
847 return sec->size;
850 static void output_pe_exports( DLLSPEC *spec )
852 unsigned int i, exp_count = get_exports_count( spec );
853 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
854 unsigned int pos, str_rva = exp_rva + 4 * exp_count + 6 * spec->nb_names;
856 if (!spec->nb_entry_points) return;
858 init_output_buffer();
859 put_dword( 0 ); /* Characteristics */
860 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
861 put_word( 0 ); /* MajorVersion */
862 put_word( 0 ); /* MinorVersion */
863 put_dword( str_rva ); /* Name */
864 put_dword( spec->base ); /* Base */
865 put_dword( exp_count ); /* NumberOfFunctions */
866 put_dword( spec->nb_names ); /* NumberOfNames */
867 put_dword( exp_rva ); /* AddressOfFunctions */
868 if (spec->nb_names)
870 put_dword( exp_rva + 4 * exp_count ); /* AddressOfNames */
871 put_dword( exp_rva + 4 * exp_count + 4 * spec->nb_names ); /* AddressOfNameOrdinals */
873 else
875 put_dword( 0 ); /* AddressOfNames */
876 put_dword( 0 ); /* AddressOfNameOrdinals */
879 /* functions */
880 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < spec->nb_names; i++)
881 pos += strlen( spec->names[i]->name ) + 1;
882 for (i = spec->base; i <= spec->limit; i++)
884 ORDDEF *odp = spec->ordinals[i];
885 if (odp && (odp->flags & FLAG_FORWARD))
887 put_dword( pos );
888 pos += strlen(odp->link_name) + 1;
890 else put_dword( 0 );
893 /* names */
894 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < spec->nb_names; i++)
896 put_dword( pos );
897 pos += strlen(spec->names[i]->name) + 1;
900 /* ordinals */
901 for (i = 0; i < spec->nb_names; i++) put_word( spec->names[i]->ordinal - spec->base );
903 /* strings */
904 put_data( spec->file_name, strlen(spec->file_name) + 1 );
905 for (i = 0; i < spec->nb_names; i++)
906 put_data( spec->names[i]->name, strlen(spec->names[i]->name) + 1 );
908 for (i = spec->base; i <= spec->limit; i++)
910 ORDDEF *odp = spec->ordinals[i];
911 if (odp && (odp->flags & FLAG_FORWARD)) put_data( odp->link_name, strlen(odp->link_name) + 1 );
914 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
915 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
919 /* apiset hash table */
920 struct apiset_hash_entry
922 unsigned int hash;
923 unsigned int index;
926 static int apiset_hash_cmp( const void *h1, const void *h2 )
928 const struct apiset_hash_entry *entry1 = h1;
929 const struct apiset_hash_entry *entry2 = h2;
931 if (entry1->hash > entry2->hash) return 1;
932 if (entry1->hash < entry2->hash) return -1;
933 return 0;
936 static void output_apiset_section( const struct apiset *apiset )
938 struct apiset_hash_entry *hash;
939 struct apiset_entry *e;
940 unsigned int i, j, str_pos, value_pos, hash_pos, size;
942 init_output_buffer();
944 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
945 str_pos = value_pos;
946 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
947 str_pos += 0x14 * max( 1, e->val_count ); /* values */
949 hash_pos = str_pos + ((apiset->str_pos * 2 + 3) & ~3);
950 size = hash_pos + apiset->count * 8; /* hashes */
952 /* header */
954 put_dword( 6 ); /* Version */
955 put_dword( size ); /* Size */
956 put_dword( 0 ); /* Flags */
957 put_dword( apiset->count ); /* Count */
958 put_dword( 0x1c ); /* EntryOffset */
959 put_dword( hash_pos ); /* HashOffset */
960 put_dword( apiset_hash_factor ); /* HashFactor */
962 /* name entries */
964 value_pos = 0x1c /* header */ + apiset->count * 0x18; /* names */
965 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
967 put_dword( 1 ); /* Flags */
968 put_dword( str_pos + e->name_off * 2 ); /* NameOffset */
969 put_dword( e->name_len * 2 ); /* NameLength */
970 put_dword( e->hash_len * 2 ); /* HashedLength */
971 put_dword( value_pos ); /* ValueOffset */
972 put_dword( max( 1, e->val_count )); /* ValueCount */
973 value_pos += 0x14 * max( 1, e->val_count );
976 /* values */
978 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
980 if (!e->val_count)
982 put_dword( 0 ); /* Flags */
983 put_dword( 0 ); /* NameOffset */
984 put_dword( 0 ); /* NameLength */
985 put_dword( 0 ); /* ValueOffset */
986 put_dword( 0 ); /* ValueLength */
988 else for (j = 0; j < e->val_count; j++)
990 put_dword( 0 ); /* Flags */
991 if (e->values[j].name_off)
993 put_dword( str_pos + e->values[j].name_off * 2 ); /* NameOffset */
994 put_dword( e->values[j].name_len * 2 ); /* NameLength */
996 else
998 put_dword( 0 ); /* NameOffset */
999 put_dword( 0 ); /* NameLength */
1001 put_dword( str_pos + e->values[j].val_off * 2 ); /* ValueOffset */
1002 put_dword( e->values[j].val_len * 2 ); /* ValueLength */
1006 /* strings */
1008 for (i = 0; i < apiset->str_pos; i++) put_word( apiset->strings[i] );
1009 align_output( 4 );
1011 /* hash table */
1013 hash = xmalloc( apiset->count * sizeof(*hash) );
1014 for (i = 0, e = apiset->entries; i < apiset->count; i++, e++)
1016 hash[i].hash = e->hash;
1017 hash[i].index = i;
1019 qsort( hash, apiset->count, sizeof(*hash), apiset_hash_cmp );
1020 for (i = 0; i < apiset->count; i++)
1022 put_dword( hash[i].hash );
1023 put_dword( hash[i].index );
1025 free( hash );
1027 flush_output_to_section( ".apiset", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1031 static void output_pe_file( DLLSPEC *spec, const char signature[32] )
1033 const unsigned int lfanew = 0x40 + 32;
1034 unsigned int i, code_size = 0, data_size = 0;
1036 init_output_buffer();
1038 for (i = 0; i < pe.sec_count; i++)
1039 if (pe.sec[i].flags & 0x20) /* CNT_CODE */
1040 code_size += pe.sec[i].file_size;
1042 /* .rsrc section */
1043 if (spec->type == SPEC_WIN32)
1045 output_bin_resources( spec, current_rva() );
1046 flush_output_to_section( ".rsrc", 2 /* IMAGE_DIRECTORY_ENTRY_RESOURCE */,
1047 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1050 /* .reloc section */
1051 if (code_size)
1053 put_dword( 0 ); /* VirtualAddress */
1054 put_dword( 0 ); /* Size */
1055 flush_output_to_section( ".reloc", 5 /* IMAGE_DIRECTORY_ENTRY_BASERELOC */,
1056 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ );
1059 for (i = 0; i < pe.sec_count; i++)
1060 if ((pe.sec[i].flags & 0x60) == 0x40) /* CNT_INITIALIZED_DATA */
1061 data_size += pe.sec[i].file_size;
1063 put_word( 0x5a4d ); /* e_magic */
1064 put_word( 0x40 ); /* e_cblp */
1065 put_word( 0x01 ); /* e_cp */
1066 put_word( 0 ); /* e_crlc */
1067 put_word( lfanew / 16 ); /* e_cparhdr */
1068 put_word( 0x0000 ); /* e_minalloc */
1069 put_word( 0xffff ); /* e_maxalloc */
1070 put_word( 0x0000 ); /* e_ss */
1071 put_word( 0x00b8 ); /* e_sp */
1072 put_word( 0 ); /* e_csum */
1073 put_word( 0 ); /* e_ip */
1074 put_word( 0 ); /* e_cs */
1075 put_word( lfanew ); /* e_lfarlc */
1076 put_word( 0 ); /* e_ovno */
1077 put_dword( 0 ); /* e_res */
1078 put_dword( 0 );
1079 put_word( 0 ); /* e_oemid */
1080 put_word( 0 ); /* e_oeminfo */
1081 put_dword( 0 ); /* e_res2 */
1082 put_dword( 0 );
1083 put_dword( 0 );
1084 put_dword( 0 );
1085 put_dword( 0 );
1086 put_dword( lfanew );
1088 put_data( signature, 32 );
1090 put_dword( 0x4550 ); /* Signature */
1091 switch (target.cpu)
1093 case CPU_i386: put_word( IMAGE_FILE_MACHINE_I386 ); break;
1094 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
1095 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
1096 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
1098 put_word( pe.sec_count ); /* NumberOfSections */
1099 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1100 put_dword( 0 ); /* PointerToSymbolTable */
1101 put_dword( 0 ); /* NumberOfSymbols */
1102 put_word( get_ptr_size() == 8 ?
1103 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
1104 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
1105 put_word( spec->characteristics ); /* Characteristics */
1106 put_word( get_ptr_size() == 8 ?
1107 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
1108 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
1109 put_byte( 7 ); /* MajorLinkerVersion */
1110 put_byte( 10 ); /* MinorLinkerVersion */
1111 put_dword( code_size ); /* SizeOfCode */
1112 put_dword( data_size ); /* SizeOfInitializedData */
1113 put_dword( 0 ); /* SizeOfUninitializedData */
1114 put_dword( code_size ? pe.sec[0].rva : 0 ); /* AddressOfEntryPoint */
1115 put_dword( code_size ? pe.sec[0].rva : 0 ); /* BaseOfCode */
1116 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
1117 put_pword( 0x10000000 ); /* ImageBase */
1118 put_dword( pe.section_align ); /* SectionAlignment */
1119 put_dword( pe.file_align ); /* FileAlignment */
1120 put_word( 1 ); /* MajorOperatingSystemVersion */
1121 put_word( 0 ); /* MinorOperatingSystemVersion */
1122 put_word( 0 ); /* MajorImageVersion */
1123 put_word( 0 ); /* MinorImageVersion */
1124 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
1125 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
1126 put_dword( 0 ); /* Win32VersionValue */
1127 put_dword( current_rva() ); /* SizeOfImage */
1128 put_dword( pe.file_align ); /* SizeOfHeaders */
1129 put_dword( 0 ); /* CheckSum */
1130 put_word( spec->subsystem ); /* Subsystem */
1131 put_word( spec->dll_characteristics ); /* DllCharacteristics */
1132 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
1133 put_pword( pe.section_align ); /* SizeOfStackCommit */
1134 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
1135 put_pword( pe.section_align ); /* SizeOfHeapCommit */
1136 put_dword( 0 ); /* LoaderFlags */
1137 put_dword( 16 ); /* NumberOfRvaAndSizes */
1139 /* image directories */
1140 for (i = 0; i < 16; i++)
1142 put_dword( pe.dir[i].rva ); /* VirtualAddress */
1143 put_dword( pe.dir[i].size ); /* Size */
1146 /* sections */
1147 for (i = 0; i < pe.sec_count; i++)
1149 put_data( pe.sec[i].name, 8 ); /* Name */
1150 put_dword( pe.sec[i].size ); /* VirtualSize */
1151 put_dword( pe.sec[i].rva ); /* VirtualAddress */
1152 put_dword( pe.sec[i].file_size ); /* SizeOfRawData */
1153 put_dword( pe.sec[i].filepos ); /* PointerToRawData */
1154 put_dword( 0 ); /* PointerToRelocations */
1155 put_dword( 0 ); /* PointerToLinenumbers */
1156 put_word( 0 ); /* NumberOfRelocations */
1157 put_word( 0 ); /* NumberOfLinenumbers */
1158 put_dword( pe.sec[i].flags ); /* Characteristics */
1160 align_output( pe.file_align );
1162 /* section data */
1163 for (i = 0; i < pe.sec_count; i++)
1165 put_data( pe.sec[i].ptr, pe.sec[i].size );
1166 align_output( pe.file_align );
1169 flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
1172 /*******************************************************************
1173 * output_fake_module
1175 * Build a fake binary module from a spec file.
1177 void output_fake_module( DLLSPEC *spec )
1179 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
1180 0xc2, 0x0c, 0x00 }; /* ret $12 */
1182 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
1183 0xc2, 0x04, 0x00 }; /* ret $4 */
1184 unsigned int i;
1186 resolve_imports( spec );
1187 pe.section_align = get_page_size();
1188 pe.file_align = 0x200;
1189 init_output_buffer();
1191 /* .text section */
1192 if (spec->characteristics & IMAGE_FILE_DLL) put_data( dll_code_section, sizeof(dll_code_section) );
1193 else put_data( exe_code_section, sizeof(exe_code_section) );
1194 flush_output_to_section( ".text", -1, 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ );
1196 if (spec->type == SPEC_WIN16)
1198 add_export( current_rva(), "__wine_spec_dos_header" );
1200 /* .rdata section */
1201 output_fake_module16( spec );
1202 if (spec->main_module)
1204 add_export( current_rva() + output_buffer_pos, "__wine_spec_main_module" );
1205 put_data( spec->main_module, strlen(spec->main_module) + 1 );
1207 flush_output_to_section( ".rdata", -1, 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1210 /* .edata section */
1211 if (pe.exp_count)
1213 unsigned int exp_rva = current_rva() + 40; /* sizeof(IMAGE_EXPORT_DIRECTORY) */
1214 unsigned int pos, str_rva = exp_rva + 10 * pe.exp_count;
1216 put_dword( 0 ); /* Characteristics */
1217 put_dword( hash_filename(spec->file_name) ); /* TimeDateStamp */
1218 put_word( 0 ); /* MajorVersion */
1219 put_word( 0 ); /* MinorVersion */
1220 put_dword( str_rva ); /* Name */
1221 put_dword( 1 ); /* Base */
1222 put_dword( pe.exp_count ); /* NumberOfFunctions */
1223 put_dword( pe.exp_count ); /* NumberOfNames */
1224 put_dword( exp_rva ); /* AddressOfFunctions */
1225 put_dword( exp_rva + 4 * pe.exp_count ); /* AddressOfNames */
1226 put_dword( exp_rva + 8 * pe.exp_count ); /* AddressOfNameOrdinals */
1228 /* functions */
1229 for (i = 0; i < pe.exp_count; i++) put_dword( pe.exp[i].rva );
1230 /* names */
1231 for (i = 0, pos = str_rva + strlen(spec->file_name) + 1; i < pe.exp_count; i++)
1233 put_dword( pos );
1234 pos += strlen( pe.exp[i].name ) + 1;
1236 /* ordinals */
1237 for (i = 0; i < pe.exp_count; i++) put_word( i );
1238 /* strings */
1239 put_data( spec->file_name, strlen(spec->file_name) + 1 );
1240 for (i = 0; i < pe.exp_count; i++) put_data( pe.exp[i].name, strlen(pe.exp[i].name) + 1 );
1241 flush_output_to_section( ".edata", 0 /* IMAGE_DIRECTORY_ENTRY_EXPORT */,
1242 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ );
1245 output_pe_file( spec, fakedll_signature );
1249 /*******************************************************************
1250 * output_data_module
1252 * Build a data-only module from a spec file.
1254 void output_data_module( DLLSPEC *spec )
1256 pe.section_align = pe.file_align = get_page_size();
1258 output_pe_exports( spec );
1259 if (spec->apiset.count) output_apiset_section( &spec->apiset );
1260 output_pe_file( spec, builtin_signature );
1264 /*******************************************************************
1265 * output_def_file
1267 * Build a Win32 def file from a spec file.
1269 void output_def_file( DLLSPEC *spec, int import_only )
1271 DLLSPEC *spec32 = NULL;
1272 const char *name;
1273 int i, total;
1275 if (spec->type == SPEC_WIN16)
1277 spec32 = alloc_dll_spec();
1278 add_16bit_exports( spec32, spec );
1279 spec = spec32;
1282 if (spec_file_name)
1283 output( "; File generated automatically from %s; do not edit!\n\n",
1284 spec_file_name );
1285 else
1286 output( "; File generated automatically; do not edit!\n\n" );
1288 output( "LIBRARY %s\n\n", spec->file_name);
1289 output( "EXPORTS\n");
1291 /* Output the exports and relay entry points */
1293 for (i = total = 0; i < spec->nb_entry_points; i++)
1295 const ORDDEF *odp = &spec->entry_points[i];
1296 int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1298 if (odp->name) name = odp->name;
1299 else if (odp->export_name) name = odp->export_name;
1300 else continue;
1302 if (!is_private) total++;
1303 if (import_only && odp->type == TYPE_STUB) continue;
1305 if ((odp->flags & FLAG_FASTCALL) && is_pe())
1306 name = strmake( "@%s", name );
1308 output( " %s", name );
1310 switch(odp->type)
1312 case TYPE_EXTERN:
1313 is_data = 1;
1314 /* fall through */
1315 case TYPE_VARARGS:
1316 case TYPE_CDECL:
1317 /* try to reduce output */
1318 if(!import_only && (strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD)))
1319 output( "=%s", odp->link_name );
1320 break;
1321 case TYPE_STDCALL:
1323 int at_param = get_args_size( odp );
1324 if (!kill_at && target.cpu == CPU_i386) output( "@%d", at_param );
1325 if (import_only) break;
1326 if (odp->flags & FLAG_FORWARD)
1327 output( "=%s", odp->link_name );
1328 else if (strcmp(name, odp->link_name)) /* try to reduce output */
1329 output( "=%s", get_link_name( odp ));
1330 break;
1332 case TYPE_STUB:
1333 if (!kill_at && target.cpu == CPU_i386) output( "@%d", get_args_size( odp ));
1334 is_private = 1;
1335 break;
1336 default:
1337 assert(0);
1339 output( " @%d", odp->ordinal );
1340 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1341 if (is_data) output( " DATA" );
1342 if (is_private) output( " PRIVATE" );
1343 output( "\n" );
1345 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1346 if (spec32) free_dll_spec( spec32 );
1350 /*******************************************************************
1351 * make_builtin_files
1353 void make_builtin_files( struct strarray files )
1355 int i, fd;
1356 struct
1358 unsigned short e_magic;
1359 unsigned short unused[29];
1360 unsigned int e_lfanew;
1361 } header;
1363 for (i = 0; i < files.count; i++)
1365 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1366 fatal_perror( "Cannot open %s", files.str[i] );
1367 if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1369 if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1370 fatal_error( "%s: Not enough space (%x) for Wine signature\n", files.str[i], header.e_lfanew );
1371 write( fd, builtin_signature, sizeof(builtin_signature) );
1373 if (prefer_native)
1375 unsigned int pos = header.e_lfanew + 0x5e; /* OptionalHeader.DllCharacteristics */
1376 unsigned short dll_charact;
1377 lseek( fd, pos, SEEK_SET );
1378 if (read( fd, &dll_charact, sizeof(dll_charact) ) == sizeof(dll_charact))
1380 dll_charact |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
1381 lseek( fd, pos, SEEK_SET );
1382 write( fd, &dll_charact, sizeof(dll_charact) );
1386 else fatal_error( "%s: Unrecognized file format\n", files.str[i] );
1387 close( fd );
1391 static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1393 struct
1395 unsigned char e_ident[16];
1396 unsigned short e_type;
1397 unsigned short e_machine;
1398 unsigned int e_version;
1399 unsigned int e_entry;
1400 unsigned int e_phoff;
1401 unsigned int e_shoff;
1402 unsigned int e_flags;
1403 unsigned short e_ehsize;
1404 unsigned short e_phentsize;
1405 unsigned short e_phnum;
1406 unsigned short e_shentsize;
1407 unsigned short e_shnum;
1408 unsigned short e_shstrndx;
1409 } *elf = header;
1410 struct
1412 unsigned int p_type;
1413 unsigned int p_offset;
1414 unsigned int p_vaddr;
1415 unsigned int p_paddr;
1416 unsigned int p_filesz;
1417 unsigned int p_memsz;
1418 unsigned int p_flags;
1419 unsigned int p_align;
1420 } *phdr;
1421 struct
1423 unsigned int d_tag;
1424 unsigned int d_val;
1425 } *dyn;
1427 unsigned int i, size;
1429 if (header_size < sizeof(*elf)) return;
1430 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1432 size = elf->e_phnum * elf->e_phentsize;
1433 phdr = xmalloc( size );
1434 lseek( fd, elf->e_phoff, SEEK_SET );
1435 if (read( fd, phdr, size ) != size) return;
1437 for (i = 0; i < elf->e_phnum; i++)
1439 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1440 phdr = (void *)((char *)phdr + elf->e_phentsize);
1442 if (i == elf->e_phnum) return;
1444 dyn = xmalloc( phdr->p_filesz );
1445 lseek( fd, phdr->p_offset, SEEK_SET );
1446 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1447 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1449 switch (dyn[i].d_tag)
1451 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1452 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1453 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1456 lseek( fd, phdr->p_offset, SEEK_SET );
1457 write( fd, dyn, phdr->p_filesz );
1460 static void fixup_elf64( const char *name, int fd, void *header, size_t header_size )
1462 struct
1464 unsigned char e_ident[16];
1465 unsigned short e_type;
1466 unsigned short e_machine;
1467 unsigned int e_version;
1468 unsigned __int64 e_entry;
1469 unsigned __int64 e_phoff;
1470 unsigned __int64 e_shoff;
1471 unsigned int e_flags;
1472 unsigned short e_ehsize;
1473 unsigned short e_phentsize;
1474 unsigned short e_phnum;
1475 unsigned short e_shentsize;
1476 unsigned short e_shnum;
1477 unsigned short e_shstrndx;
1478 } *elf = header;
1479 struct
1481 unsigned int p_type;
1482 unsigned int p_flags;
1483 unsigned __int64 p_offset;
1484 unsigned __int64 p_vaddr;
1485 unsigned __int64 p_paddr;
1486 unsigned __int64 p_filesz;
1487 unsigned __int64 p_memsz;
1488 unsigned __int64 p_align;
1489 } *phdr;
1490 struct
1492 unsigned __int64 d_tag;
1493 unsigned __int64 d_val;
1494 } *dyn;
1496 unsigned int i, size;
1498 if (header_size < sizeof(*elf)) return;
1499 if (elf->e_ident[6] != 1 /* EV_CURRENT */) return;
1501 size = elf->e_phnum * elf->e_phentsize;
1502 phdr = xmalloc( size );
1503 lseek( fd, elf->e_phoff, SEEK_SET );
1504 if (read( fd, phdr, size ) != size) return;
1506 for (i = 0; i < elf->e_phnum; i++)
1508 if (phdr->p_type == 2 /* PT_DYNAMIC */ ) break;
1509 phdr = (void *)((char *)phdr + elf->e_phentsize);
1511 if (i == elf->e_phnum) return;
1513 dyn = xmalloc( phdr->p_filesz );
1514 lseek( fd, phdr->p_offset, SEEK_SET );
1515 if (read( fd, dyn, phdr->p_filesz ) != phdr->p_filesz) return;
1516 for (i = 0; i < phdr->p_filesz / sizeof(*dyn) && dyn[i].d_tag; i++)
1518 switch (dyn[i].d_tag)
1520 case 25: dyn[i].d_tag = 0x60009994; break; /* DT_INIT_ARRAY */
1521 case 27: dyn[i].d_tag = 0x60009995; break; /* DT_INIT_ARRAYSZ */
1522 case 12: dyn[i].d_tag = 0x60009996; break; /* DT_INIT */
1525 lseek( fd, phdr->p_offset, SEEK_SET );
1526 write( fd, dyn, phdr->p_filesz );
1529 /*******************************************************************
1530 * fixup_constructors
1532 void fixup_constructors( struct strarray files )
1534 int i, fd, size;
1535 unsigned int header[64];
1537 for (i = 0; i < files.count; i++)
1539 if ((fd = open( files.str[i], O_RDWR | O_BINARY )) == -1)
1540 fatal_perror( "Cannot open %s", files.str[i] );
1541 size = read( fd, &header, sizeof(header) );
1542 if (size > 5)
1544 if (!memcmp( header, "\177ELF\001", 5 )) fixup_elf32( files.str[i], fd, header, size );
1545 else if (!memcmp( header, "\177ELF\002", 5 )) fixup_elf64( files.str[i], fd, header, size );
1547 close( fd );