msxml3: Use standard dlopen() instead of the libwine wrappers.
[wine/zf.git] / tools / winebuild / spec32.c
blobecfe4c50113718002bf95c20319fac54f506c951
1 /*
2 * 32-bit spec files
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
33 #include "build.h"
35 #define IMAGE_FILE_MACHINE_UNKNOWN 0
36 #define IMAGE_FILE_MACHINE_I386 0x014c
37 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
38 #define IMAGE_FILE_MACHINE_AMD64 0x8664
39 #define IMAGE_FILE_MACHINE_ARMNT 0x01C4
40 #define IMAGE_FILE_MACHINE_ARM64 0xaa64
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
49 int needs_get_pc_thunk = 0;
51 static const char builtin_signature[32] = "Wine builtin DLL";
52 static const char fakedll_signature[32] = "Wine placeholder DLL";
54 /* check if entry point needs a relay thunk */
55 static inline int needs_relay( const ORDDEF *odp )
57 /* skip nonexistent entry points */
58 if (!odp) return 0;
59 /* skip non-functions */
60 switch (odp->type)
62 case TYPE_STDCALL:
63 case TYPE_CDECL:
64 break;
65 case TYPE_STUB:
66 if (odp->u.func.nb_args != -1) break;
67 /* fall through */
68 default:
69 return 0;
71 /* skip norelay and forward entry points */
72 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
73 return 1;
76 static int is_float_arg( const ORDDEF *odp, int arg )
78 if (arg >= odp->u.func.nb_args) return 0;
79 return (odp->u.func.args[arg] == ARG_FLOAT || odp->u.func.args[arg] == ARG_DOUBLE);
82 /* check if dll will output relay thunks */
83 static int has_relays( DLLSPEC *spec )
85 int i;
87 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64 &&
88 target_cpu != CPU_ARM && target_cpu != CPU_ARM64)
89 return 0;
91 for (i = spec->base; i <= spec->limit; i++)
93 ORDDEF *odp = spec->ordinals[i];
94 if (needs_relay( odp )) return 1;
96 return 0;
99 static int cmp_func_args( const void *p1, const void *p2 )
101 const ORDDEF *odp1 = *(const ORDDEF **)p1;
102 const ORDDEF *odp2 = *(const ORDDEF **)p2;
104 return odp2->u.func.nb_args - odp1->u.func.nb_args;
107 static void get_arg_string( ORDDEF *odp, char str[MAX_ARGUMENTS + 1] )
109 int i;
111 for (i = 0; i < odp->u.func.nb_args; i++)
113 switch (odp->u.func.args[i])
115 case ARG_STR: str[i] = 's'; break;
116 case ARG_WSTR: str[i] = 'w'; break;
117 case ARG_FLOAT: str[i] = 'f'; break;
118 case ARG_DOUBLE: str[i] = 'd'; break;
119 case ARG_INT64:
120 case ARG_INT128:
121 if (get_ptr_size() == 4)
123 str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
124 break;
126 /* fall through */
127 case ARG_LONG:
128 case ARG_PTR:
129 default:
130 str[i] = 'i';
131 break;
134 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) str[0] = 't';
135 if ((odp->flags & FLAG_FASTCALL) && odp->u.func.nb_args > 1) str[1] = 't';
137 /* append return value */
138 if (get_ptr_size() == 4 && (odp->flags & FLAG_RET64))
139 strcpy( str + i, "J" );
140 else
141 strcpy( str + i, "I" );
144 static void output_data_directories( const char *names[16] )
146 int i;
148 for (i = 0; i < 16; i++)
150 if (names[i])
152 output_rva( "%s", names[i] );
153 output( "\t.long %s_end - %s\n", names[i], names[i] );
155 else output( "\t.long 0,0\n" );
159 /*******************************************************************
160 * build_args_string
162 static char *build_args_string( DLLSPEC *spec )
164 int i, count = 0, len = 1;
165 char *p, *buffer;
166 char str[MAX_ARGUMENTS + 2];
167 ORDDEF **funcs;
169 funcs = xmalloc( (spec->limit + 1 - spec->base) * sizeof(*funcs) );
170 for (i = spec->base; i <= spec->limit; i++)
172 ORDDEF *odp = spec->ordinals[i];
174 if (!needs_relay( odp )) continue;
175 funcs[count++] = odp;
176 len += odp->u.func.nb_args + 1;
178 /* sort functions by decreasing number of arguments */
179 qsort( funcs, count, sizeof(*funcs), cmp_func_args );
180 buffer = xmalloc( len );
181 buffer[0] = 0;
182 /* build the arguments string, reusing substrings where possible */
183 for (i = 0; i < count; i++)
185 get_arg_string( funcs[i], str );
186 if (!(p = strstr( buffer, str )))
188 p = buffer + strlen( buffer );
189 strcpy( p, str );
191 funcs[i]->u.func.args_str_offset = p - buffer;
193 free( funcs );
194 return buffer;
197 /*******************************************************************
198 * output_relay_debug
200 * Output entry points for relay debugging
202 static void output_relay_debug( DLLSPEC *spec )
204 int i;
206 /* first the table of entry point offsets */
208 output( "\t%s\n", get_asm_rodata_section() );
209 output( "\t.align %d\n", get_alignment(4) );
210 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
212 for (i = spec->base; i <= spec->limit; i++)
214 ORDDEF *odp = spec->ordinals[i];
216 if (needs_relay( odp ))
217 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
218 else
219 output( "\t.long 0\n" );
222 /* then the strings of argument types */
224 output( ".L__wine_spec_relay_args_string:\n" );
225 output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( spec ));
227 /* then the relay thunks */
229 output( "\t.text\n" );
230 output( "__wine_spec_relay_entry_points:\n" );
231 output( "\tnop\n" ); /* to avoid 0 offset */
233 for (i = spec->base; i <= spec->limit; i++)
235 ORDDEF *odp = spec->ordinals[i];
237 if (!needs_relay( odp )) continue;
239 switch (target_cpu)
241 case CPU_x86:
242 output( "\t.align %d\n", get_alignment(4) );
243 output( "\t.long 0x90909090,0x90909090\n" );
244 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
245 output_cfi( ".cfi_startproc" );
246 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
247 if (odp->flags & (FLAG_THISCALL | FLAG_FASTCALL)) /* add the register arguments */
249 output( "\tpopl %%eax\n" );
250 if ((odp->flags & FLAG_FASTCALL) && get_args_size( odp ) > 4) output( "\tpushl %%edx\n" );
251 output( "\tpushl %%ecx\n" );
252 output( "\tpushl %%eax\n" );
254 output( "\tpushl $%u\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
255 output_cfi( ".cfi_adjust_cfa_offset 4" );
257 if (UsePIC)
259 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
260 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
261 needs_get_pc_thunk = 1;
263 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
264 output( "\tpushl %%eax\n" );
265 output_cfi( ".cfi_adjust_cfa_offset 4" );
267 output( "\tcall *4(%%eax)\n" );
268 output_cfi( ".cfi_adjust_cfa_offset -8" );
269 if (odp->type == TYPE_STDCALL)
270 output( "\tret $%u\n", get_args_size( odp ));
271 else
272 output( "\tret\n" );
273 output_cfi( ".cfi_endproc" );
274 break;
276 case CPU_ARM:
278 unsigned int mask, val, count = 0;
279 int j, has_float = 0;
281 if (strcmp( float_abi_option, "soft" ))
282 for (j = 0; j < odp->u.func.nb_args && !has_float; j++)
283 has_float = is_float_arg( odp, j );
285 val = (odp->u.func.args_str_offset << 16) | (i - spec->base);
286 output( "\t.align %d\n", get_alignment(4) );
287 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
288 output_cfi( ".cfi_startproc" );
289 output( "\tpush {r0-r3}\n" );
290 output( "\tmov r2, SP\n");
291 if (has_float) output( "\tvpush {s0-s15}\n" );
292 output( "\tpush {LR}\n" );
293 output( "\tsub SP, #4\n");
294 for (mask = 0xff; mask; mask <<= 8)
295 if (val & mask) output( "\t%s r1,#%u\n", count++ ? "add" : "mov", val & mask );
296 if (!count) output( "\tmov r1,#0\n" );
297 output( "\tldr r0, 2f\n");
298 output( "\tadd r0, PC\n");
299 output( "\tldr IP, [r0, #4]\n");
300 output( "1:\tblx IP\n");
301 output( "\tldr IP, [SP, #4]\n" );
302 output( "\tadd SP, #%u\n", 24 + (has_float ? 64 : 0) );
303 output( "\tbx IP\n");
304 output( "2:\t.long .L__wine_spec_relay_descr-1b\n" );
305 output_cfi( ".cfi_endproc" );
306 break;
309 case CPU_ARM64:
310 output( "\t.align %d\n", get_alignment(4) );
311 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
312 output_cfi( ".cfi_startproc" );
313 switch (odp->u.func.nb_args)
315 default:
316 case 8:
317 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
318 /* fall through */
319 case 6:
320 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
321 /* fall through */
322 case 4:
323 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
324 /* fall through */
325 case 2:
326 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
327 /* fall through */
328 case 0: break;
330 output( "\tmov x2, SP\n");
331 output( "\tstp x29, x30, [SP,#-16]!\n" );
332 output( "\tstp x8, x9, [SP,#-16]!\n" );
333 output( "\tmov w1, #%u\n", odp->u.func.args_str_offset << 16 );
334 if (i - spec->base) output( "\tadd w1, w1, #%u\n", i - spec->base );
335 output( "\tadrp x0, .L__wine_spec_relay_descr\n");
336 output( "\tadd x0, x0, #:lo12:.L__wine_spec_relay_descr\n");
337 output( "\tldr x3, [x0, #8]\n");
338 output( "\tblr x3\n");
339 output( "\tadd SP, SP, #16\n" );
340 output( "\tldp x29, x30, [SP], #16\n" );
341 if (odp->u.func.nb_args)
342 output( "\tadd SP, SP, #%u\n", 8 * ((min(odp->u.func.nb_args, 8) + 1) & ~1) );
343 output( "\tret\n");
344 output_cfi( ".cfi_endproc" );
345 break;
347 case CPU_x86_64:
348 output( "\t.align %d\n", get_alignment(4) );
349 output( "\t.long 0x90909090,0x90909090\n" );
350 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
351 output_cfi( ".cfi_startproc" );
352 switch (odp->u.func.nb_args)
354 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp, 3 ) ? "xmm3" : "r9" );
355 /* fall through */
356 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
357 /* fall through */
358 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
359 /* fall through */
360 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
361 /* fall through */
362 case 0: break;
364 output( "\tmovl $%u,%%edx\n", (odp->u.func.args_str_offset << 16) | (i - spec->base) );
365 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
366 output( "\tcallq *8(%%rcx)\n" );
367 output( "\tret\n" );
368 output_cfi( ".cfi_endproc" );
369 break;
371 default:
372 assert(0);
377 /*******************************************************************
378 * output_exports
380 * Output the export table for a Win32 module.
382 void output_exports( DLLSPEC *spec )
384 int i, fwd_size = 0;
385 int needs_imports = 0;
386 int needs_relay = has_relays( spec );
387 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
388 const char *func_ptr = (target_platform == PLATFORM_WINDOWS) ? ".rva" : get_asm_ptr_keyword();
389 const char *name;
391 if (!nr_exports) return;
393 output( "\n/* export table */\n\n" );
394 output( "\t%s\n", get_asm_export_section() );
395 output( "\t.align %d\n", get_alignment(4) );
396 output( ".L__wine_spec_exports:\n" );
398 /* export directory header */
400 output( "\t.long 0\n" ); /* Characteristics */
401 output( "\t.long 0\n" ); /* TimeDateStamp */
402 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
403 output_rva( ".L__wine_spec_exp_names" ); /* Name */
404 output( "\t.long %u\n", spec->base ); /* Base */
405 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
406 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
407 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
408 if (spec->nb_names)
410 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
411 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
413 else
415 output( "\t.long 0\n" ); /* AddressOfNames */
416 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
419 /* output the function pointers */
421 output( "\n.L__wine_spec_exports_funcs:\n" );
422 for (i = spec->base; i <= spec->limit; i++)
424 ORDDEF *odp = spec->ordinals[i];
425 if (!odp) output( "\t%s 0\n",
426 (target_platform == PLATFORM_WINDOWS) ? ".long" : get_asm_ptr_keyword() );
427 else 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)
435 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr, fwd_size );
436 fwd_size += strlen(odp->link_name) + 1;
438 else if ((odp->flags & FLAG_IMPORT) && (target_cpu == CPU_x86 || target_cpu == CPU_x86_64))
440 name = odp->name ? odp->name : odp->export_name;
441 if (name) output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_imp"), name );
442 else output( "\t%s %s_%u\n", func_ptr, asm_name("__wine_spec_imp"), i );
443 needs_imports = 1;
445 else if (odp->flags & FLAG_EXT_LINK)
447 output( "\t%s %s_%s\n", func_ptr, asm_name("__wine_spec_ext_link"), odp->link_name );
449 else
451 output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
453 break;
454 case TYPE_STUB:
455 output( "\t%s %s\n", func_ptr, asm_name( get_stub_name( odp, spec )) );
456 break;
457 default:
458 assert(0);
462 if (spec->nb_names)
464 /* output the function name pointers */
466 int namepos = strlen(spec->file_name) + 1;
468 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
469 for (i = 0; i < spec->nb_names; i++)
471 output_rva( ".L__wine_spec_exp_names + %u", namepos );
472 namepos += strlen(spec->names[i]->name) + 1;
475 /* output the function ordinals */
477 output( "\n.L__wine_spec_exp_ordinals:\n" );
478 for (i = 0; i < spec->nb_names; i++)
480 output( "\t.short %d\n", spec->names[i]->ordinal - spec->base );
482 if (spec->nb_names % 2)
484 output( "\t.short 0\n" );
488 if (needs_relay)
490 output( "\t.long 0xdeb90002\n" ); /* magic */
491 if (target_platform == PLATFORM_WINDOWS) output_rva( ".L__wine_spec_relay_descr" );
492 else output( "\t.long 0\n" );
495 /* output the export name strings */
497 output( "\n.L__wine_spec_exp_names:\n" );
498 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
499 for (i = 0; i < spec->nb_names; i++)
500 output( "\t%s \"%s\"\n",
501 get_asm_string_keyword(), spec->names[i]->name );
503 /* output forward strings */
505 if (fwd_size)
507 output( "\n.L__wine_spec_forwards:\n" );
508 for (i = spec->base; i <= spec->limit; i++)
510 ORDDEF *odp = spec->ordinals[i];
511 if (odp && (odp->flags & FLAG_FORWARD))
512 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
516 /* output relays */
518 if (needs_relay)
520 if (target_platform == PLATFORM_WINDOWS)
522 output( "\t.data\n" );
523 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
525 else
527 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
528 output( ".L__wine_spec_exports_end:\n" );
531 output( ".L__wine_spec_relay_descr:\n" );
532 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
533 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
534 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
535 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
536 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
537 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
539 output_relay_debug( spec );
541 else if (target_platform != PLATFORM_WINDOWS)
543 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
544 output( ".L__wine_spec_exports_end:\n" );
545 output( "\t%s 0\n", get_asm_ptr_keyword() );
548 /* output import thunks */
550 if (!needs_imports) return;
551 output( "\t.text\n" );
552 for (i = spec->base; i <= spec->limit; i++)
554 ORDDEF *odp = spec->ordinals[i];
555 if (!odp) continue;
556 if (!(odp->flags & FLAG_IMPORT)) continue;
558 name = odp->name ? odp->name : odp->export_name;
560 output( "\t.align %d\n", get_alignment(4) );
561 output( "\t.long 0x90909090,0x90909090\n" );
562 if (name) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name );
563 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i );
564 output_cfi( ".cfi_startproc" );
566 switch (target_cpu)
568 case CPU_x86:
569 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
570 if (UsePIC)
572 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
573 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp )));
574 needs_get_pc_thunk = 1;
576 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp )));
577 break;
578 case CPU_x86_64:
579 output( "\t.byte 0x48\n" ); /* hotpatch prolog */
580 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp )));
581 break;
582 default:
583 assert(0);
585 output_cfi( ".cfi_endproc" );
590 /*******************************************************************
591 * output_asm_constructor
593 * Output code for calling a dll constructor.
595 static void output_asm_constructor( const char *constructor )
597 if (target_platform == PLATFORM_APPLE)
599 /* Mach-O doesn't have an init section */
600 output( "\n\t.mod_init_func\n" );
601 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
602 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(constructor) );
604 else
606 switch(target_cpu)
608 case CPU_x86:
609 case CPU_x86_64:
610 output( "\n\t.section \".init\",\"ax\"\n" );
611 output( "\tcall %s\n", asm_name(constructor) );
612 break;
613 case CPU_ARM:
614 output( "\n\t.section \".text\",\"ax\"\n" );
615 output( "\tblx %s\n", asm_name(constructor) );
616 break;
617 case CPU_ARM64:
618 case CPU_POWERPC:
619 output( "\n\t.section \".init\",\"ax\"\n" );
620 output( "\tbl %s\n", asm_name(constructor) );
621 break;
627 /*******************************************************************
628 * output_module
630 * Output the module data.
632 void output_module( DLLSPEC *spec )
634 int machine = 0;
635 unsigned int page_size = get_page_size();
636 const char *data_dirs[16] = { NULL };
638 /* Reserve some space for the PE header */
640 switch (target_platform)
642 case PLATFORM_WINDOWS:
643 return; /* nothing to do */
644 case PLATFORM_APPLE:
645 output( "\t.text\n" );
646 output( "\t.align %d\n", get_alignment(page_size) );
647 output( "__wine_spec_pe_header:\n" );
648 output( "\t.space 65536\n" );
649 break;
650 case PLATFORM_SOLARIS:
651 output( "\n\t.section \".text\",\"ax\"\n" );
652 output( "__wine_spec_pe_header:\n" );
653 output( "\t.skip %u\n", 65536 + page_size );
654 break;
655 default:
656 switch(target_cpu)
658 case CPU_x86:
659 case CPU_x86_64:
660 output( "\n\t.section \".init\",\"ax\"\n" );
661 output( "\tjmp 1f\n" );
662 break;
663 case CPU_ARM:
664 output( "\n\t.section \".text\",\"ax\"\n" );
665 output( "\tb 1f\n" );
666 break;
667 case CPU_ARM64:
668 case CPU_POWERPC:
669 output( "\n\t.section \".init\",\"ax\"\n" );
670 output( "\tb 1f\n" );
671 break;
673 output( "__wine_spec_pe_header:\n" );
674 output( "\t.skip %u\n", 65536 + page_size );
675 output( "1:\n" );
676 break;
679 /* Output the NT header */
681 output( "\n\t.data\n" );
682 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
683 output( "%s\n", asm_globl("__wine_spec_nt_header") );
684 output( ".L__wine_spec_rva_base:\n" );
686 output( "\t.long 0x4550\n" ); /* Signature */
687 switch(target_cpu)
689 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
690 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
691 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
692 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARMNT; break;
693 case CPU_ARM64: machine = IMAGE_FILE_MACHINE_ARM64; break;
695 output( "\t.short 0x%04x\n", /* Machine */
696 machine );
697 output( "\t.short 0\n" ); /* NumberOfSections */
698 output( "\t.long 0\n" ); /* TimeDateStamp */
699 output( "\t.long 0\n" ); /* PointerToSymbolTable */
700 output( "\t.long 0\n" ); /* NumberOfSymbols */
701 output( "\t.short %d\n", /* SizeOfOptionalHeader */
702 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
703 output( "\t.short 0x%04x\n", /* Characteristics */
704 spec->characteristics );
705 output( "\t.short 0x%04x\n", /* Magic */
706 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
707 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
708 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
709 output( "\t.long 0\n" ); /* SizeOfCode */
710 output( "\t.long 0\n" ); /* SizeOfInitializedData */
711 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
712 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
713 output( "\t%s %s\n", /* AddressOfEntryPoint */
714 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
715 if (get_ptr_size() == 4)
717 output( "\t.long 0\n" ); /* BaseOfCode */
718 output( "\t.long 0\n" ); /* BaseOfData */
720 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
721 get_asm_ptr_keyword() );
722 output( "\t.long %u\n", page_size ); /* SectionAlignment */
723 output( "\t.long %u\n", page_size ); /* FileAlignment */
724 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
725 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
726 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
727 spec->subsystem_major, spec->subsystem_minor );
728 output( "\t.long 0\n" ); /* Win32VersionValue */
729 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
730 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
731 output( "\t.long 0\n" ); /* CheckSum */
732 output( "\t.short 0x%04x\n", /* Subsystem */
733 spec->subsystem );
734 output( "\t.short 0x%04x\n", /* DllCharacteristics */
735 spec->dll_characteristics );
736 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
737 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
738 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
739 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
740 output( "\t.long 0\n" ); /* LoaderFlags */
741 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
743 if (spec->base <= spec->limit)
744 data_dirs[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
745 if (has_imports())
746 data_dirs[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
747 if (spec->nb_resources)
748 data_dirs[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
750 output_data_directories( data_dirs );
752 output( "\n\t%s\n", get_asm_string_section() );
753 output( "%s\n", asm_globl("__wine_spec_file_name") );
754 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
755 if (target_platform == PLATFORM_APPLE)
756 output( "\t.lcomm %s,4\n", asm_name("_end") );
758 output_asm_constructor( "__wine_spec_init_ctor" );
762 /*******************************************************************
763 * output_spec32_file
765 * Build a Win32 C file from a spec file.
767 void output_spec32_file( DLLSPEC *spec )
769 needs_get_pc_thunk = 0;
770 open_output_file();
771 output_standard_file_header();
772 output_module( spec );
773 output_stubs( spec );
774 output_exports( spec );
775 output_imports( spec );
776 if (needs_get_pc_thunk) output_get_pc_thunk();
777 output_resources( spec );
778 output_gnu_stack_note();
779 close_output_file();
783 /*******************************************************************
784 * output_fake_module
786 * Build a fake binary module from a spec file.
788 void output_fake_module( DLLSPEC *spec )
790 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
791 0xc2, 0x0c, 0x00 }; /* ret $12 */
793 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
794 0xc2, 0x04, 0x00 }; /* ret $4 */
796 const unsigned int page_size = get_page_size();
797 const unsigned int section_align = page_size;
798 const unsigned int file_align = 0x200;
799 const unsigned int reloc_size = 8;
800 const unsigned int lfanew = 0x40 + sizeof(fakedll_signature);
801 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
802 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
803 sizeof(dll_code_section) : sizeof(exe_code_section);
804 unsigned char *resources;
805 unsigned int resources_size;
806 unsigned int image_size = 3 * section_align;
808 resolve_imports( spec );
809 output_bin_resources( spec, 3 * section_align );
810 resources = output_buffer;
811 resources_size = output_buffer_pos;
812 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
814 init_output_buffer();
816 put_word( 0x5a4d ); /* e_magic */
817 put_word( 0x40 ); /* e_cblp */
818 put_word( 0x01 ); /* e_cp */
819 put_word( 0 ); /* e_crlc */
820 put_word( lfanew / 16 ); /* e_cparhdr */
821 put_word( 0x0000 ); /* e_minalloc */
822 put_word( 0xffff ); /* e_maxalloc */
823 put_word( 0x0000 ); /* e_ss */
824 put_word( 0x00b8 ); /* e_sp */
825 put_word( 0 ); /* e_csum */
826 put_word( 0 ); /* e_ip */
827 put_word( 0 ); /* e_cs */
828 put_word( lfanew ); /* e_lfarlc */
829 put_word( 0 ); /* e_ovno */
830 put_dword( 0 ); /* e_res */
831 put_dword( 0 );
832 put_word( 0 ); /* e_oemid */
833 put_word( 0 ); /* e_oeminfo */
834 put_dword( 0 ); /* e_res2 */
835 put_dword( 0 );
836 put_dword( 0 );
837 put_dword( 0 );
838 put_dword( 0 );
839 put_dword( lfanew );
841 put_data( fakedll_signature, sizeof(fakedll_signature) );
843 put_dword( 0x4550 ); /* Signature */
844 switch(target_cpu)
846 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
847 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
848 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
849 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARMNT ); break;
850 case CPU_ARM64: put_word( IMAGE_FILE_MACHINE_ARM64 ); break;
852 put_word( nb_sections ); /* NumberOfSections */
853 put_dword( 0 ); /* TimeDateStamp */
854 put_dword( 0 ); /* PointerToSymbolTable */
855 put_dword( 0 ); /* NumberOfSymbols */
856 put_word( get_ptr_size() == 8 ?
857 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
858 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
859 put_word( spec->characteristics ); /* Characteristics */
860 put_word( get_ptr_size() == 8 ?
861 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
862 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
863 put_byte( 7 ); /* MajorLinkerVersion */
864 put_byte( 10 ); /* MinorLinkerVersion */
865 put_dword( text_size ); /* SizeOfCode */
866 put_dword( 0 ); /* SizeOfInitializedData */
867 put_dword( 0 ); /* SizeOfUninitializedData */
868 put_dword( section_align ); /* AddressOfEntryPoint */
869 put_dword( section_align ); /* BaseOfCode */
870 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
871 put_pword( 0x10000000 ); /* ImageBase */
872 put_dword( section_align ); /* SectionAlignment */
873 put_dword( file_align ); /* FileAlignment */
874 put_word( 1 ); /* MajorOperatingSystemVersion */
875 put_word( 0 ); /* MinorOperatingSystemVersion */
876 put_word( 0 ); /* MajorImageVersion */
877 put_word( 0 ); /* MinorImageVersion */
878 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
879 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
880 put_dword( 0 ); /* Win32VersionValue */
881 put_dword( image_size ); /* SizeOfImage */
882 put_dword( file_align ); /* SizeOfHeaders */
883 put_dword( 0 ); /* CheckSum */
884 put_word( spec->subsystem ); /* Subsystem */
885 put_word( spec->dll_characteristics ); /* DllCharacteristics */
886 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
887 put_pword( page_size ); /* SizeOfStackCommit */
888 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
889 put_pword( page_size ); /* SizeOfHeapCommit */
890 put_dword( 0 ); /* LoaderFlags */
891 put_dword( 16 ); /* NumberOfRvaAndSizes */
893 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
894 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
895 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
897 put_dword( 3 * section_align );
898 put_dword( resources_size );
900 else
902 put_dword( 0 );
903 put_dword( 0 );
906 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
907 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
908 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
909 put_dword( reloc_size );
910 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
911 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
912 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
913 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
914 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
915 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
916 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
917 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
918 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
919 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
921 /* .text section */
922 put_data( ".text\0\0", 8 ); /* Name */
923 put_dword( section_align ); /* VirtualSize */
924 put_dword( section_align ); /* VirtualAddress */
925 put_dword( text_size ); /* SizeOfRawData */
926 put_dword( file_align ); /* PointerToRawData */
927 put_dword( 0 ); /* PointerToRelocations */
928 put_dword( 0 ); /* PointerToLinenumbers */
929 put_word( 0 ); /* NumberOfRelocations */
930 put_word( 0 ); /* NumberOfLinenumbers */
931 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
933 /* .reloc section */
934 put_data( ".reloc\0", 8 ); /* Name */
935 put_dword( section_align ); /* VirtualSize */
936 put_dword( 2 * section_align );/* VirtualAddress */
937 put_dword( reloc_size ); /* SizeOfRawData */
938 put_dword( 2 * file_align ); /* PointerToRawData */
939 put_dword( 0 ); /* PointerToRelocations */
940 put_dword( 0 ); /* PointerToLinenumbers */
941 put_word( 0 ); /* NumberOfRelocations */
942 put_word( 0 ); /* NumberOfLinenumbers */
943 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
945 /* .rsrc section */
946 if (resources_size)
948 put_data( ".rsrc\0\0", 8 ); /* Name */
949 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
950 put_dword( 3 * section_align );/* VirtualAddress */
951 put_dword( resources_size ); /* SizeOfRawData */
952 put_dword( 3 * file_align ); /* PointerToRawData */
953 put_dword( 0 ); /* PointerToRelocations */
954 put_dword( 0 ); /* PointerToLinenumbers */
955 put_word( 0 ); /* NumberOfRelocations */
956 put_word( 0 ); /* NumberOfLinenumbers */
957 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
960 /* .text contents */
961 align_output( file_align );
962 if (spec->characteristics & IMAGE_FILE_DLL)
963 put_data( dll_code_section, sizeof(dll_code_section) );
964 else
965 put_data( exe_code_section, sizeof(exe_code_section) );
967 /* .reloc contents */
968 align_output( file_align );
969 put_dword( 0 ); /* VirtualAddress */
970 put_dword( 0 ); /* SizeOfBlock */
972 /* .rsrc contents */
973 if (resources_size)
975 align_output( file_align );
976 put_data( resources, resources_size );
978 flush_output_buffer();
982 /*******************************************************************
983 * output_def_file
985 * Build a Win32 def file from a spec file.
987 void output_def_file( DLLSPEC *spec, int import_only )
989 DLLSPEC *spec32 = NULL;
990 const char *name;
991 int i, total;
993 if (spec->type == SPEC_WIN16)
995 spec32 = alloc_dll_spec();
996 add_16bit_exports( spec32, spec );
997 spec = spec32;
1000 if (spec_file_name)
1001 output( "; File generated automatically from %s; do not edit!\n\n",
1002 spec_file_name );
1003 else
1004 output( "; File generated automatically; do not edit!\n\n" );
1006 output( "LIBRARY %s\n\n", spec->file_name);
1007 output( "EXPORTS\n");
1009 /* Output the exports and relay entry points */
1011 for (i = total = 0; i < spec->nb_entry_points; i++)
1013 const ORDDEF *odp = &spec->entry_points[i];
1014 int is_data = 0, is_private = odp->flags & FLAG_PRIVATE;
1016 if (odp->name) name = odp->name;
1017 else if (odp->export_name) name = odp->export_name;
1018 else continue;
1020 if (!is_private) total++;
1021 if (import_only && odp->type == TYPE_STUB) continue;
1023 if ((odp->flags & FLAG_FASTCALL) && target_platform == PLATFORM_WINDOWS)
1024 name = strmake( "@%s", name );
1026 output( " %s", name );
1028 switch(odp->type)
1030 case TYPE_EXTERN:
1031 is_data = 1;
1032 /* fall through */
1033 case TYPE_VARARGS:
1034 case TYPE_CDECL:
1035 /* try to reduce output */
1036 if(!import_only && (strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD)))
1037 output( "=%s", odp->link_name );
1038 break;
1039 case TYPE_STDCALL:
1041 int at_param = get_args_size( odp );
1042 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
1043 if (import_only) break;
1044 if (odp->flags & FLAG_FORWARD)
1045 output( "=%s", odp->link_name );
1046 else if (strcmp(name, odp->link_name)) /* try to reduce output */
1047 output( "=%s", get_link_name( odp ));
1048 break;
1050 case TYPE_STUB:
1051 if (!kill_at && target_cpu == CPU_x86) output( "@%d", get_args_size( odp ));
1052 is_private = 1;
1053 break;
1054 default:
1055 assert(0);
1057 output( " @%d", odp->ordinal );
1058 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
1059 if (is_data) output( " DATA" );
1060 if (is_private) output( " PRIVATE" );
1061 output( "\n" );
1063 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
1064 if (spec32) free_dll_spec( spec32 );
1068 /*******************************************************************
1069 * make_builtin_files
1071 void make_builtin_files( char *argv[] )
1073 int i, fd;
1074 struct
1076 unsigned short e_magic;
1077 unsigned short unused[29];
1078 unsigned int e_lfanew;
1079 } header;
1081 for (i = 0; argv[i]; i++)
1083 if ((fd = open( argv[i], O_RDWR | O_BINARY )) == -1) fatal_perror( "Cannot open %s", argv[i] );
1084 if (read( fd, &header, sizeof(header) ) == sizeof(header) && !memcmp( &header.e_magic, "MZ", 2 ))
1086 if (header.e_lfanew < sizeof(header) + sizeof(builtin_signature))
1087 fatal_error( "%s: Not enough space (%x) for Wine signature\n", argv[i], header.e_lfanew );
1088 write( fd, builtin_signature, sizeof(builtin_signature) );
1090 else fatal_error( "%s: Unrecognized file format\n", argv[i] );
1091 close( fd );