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
26 #include "wine/port.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 */
59 /* skip non-functions */
66 if (odp
->u
.func
.nb_args
!= -1) break;
71 /* skip norelay and forward entry points */
72 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) return 0;
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
)
87 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
&&
88 target_cpu
!= CPU_ARM
&& target_cpu
!= CPU_ARM64
)
91 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
93 ORDDEF
*odp
= spec
->ordinals
[i
];
94 if (needs_relay( odp
)) return 1;
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] )
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;
121 if (get_ptr_size() == 4)
123 str
[i
] = (odp
->u
.func
.args
[i
] == ARG_INT64
) ? 'j' : 'k';
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" );
141 strcpy( str
+ i
, "I" );
144 static void output_data_directories( const char *names
[16] )
148 for (i
= 0; i
< 16; 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 /*******************************************************************
162 static char *build_args_string( DLLSPEC
*spec
)
164 int i
, count
= 0, len
= 1;
166 char str
[MAX_ARGUMENTS
+ 2];
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
);
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
);
191 funcs
[i
]->u
.func
.args_str_offset
= p
- buffer
;
197 /*******************************************************************
200 * Output entry points for relay debugging
202 static void output_relay_debug( DLLSPEC
*spec
)
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
);
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;
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" );
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
));
273 output_cfi( ".cfi_endproc" );
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" );
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
)
317 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
320 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
323 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
326 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
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) );
344 output_cfi( ".cfi_endproc" );
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" );
356 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp
, 2 ) ? "xmm2" : "r8" );
358 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp
, 1 ) ? "xmm1" : "rdx" );
360 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp
, 0 ) ? "xmm0" : "rcx" );
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" );
368 output_cfi( ".cfi_endproc" );
377 /*******************************************************************
380 * Output the export table for a Win32 module.
382 void output_exports( DLLSPEC
*spec
)
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();
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 */
410 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
411 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
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
)
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
);
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
);
451 output( "\t%s %s\n", func_ptr
, asm_name( get_link_name( odp
)));
455 output( "\t%s %s\n", func_ptr
, asm_name( get_stub_name( odp
, spec
)) );
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" );
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 */
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
);
520 if (target_platform
== PLATFORM_WINDOWS
)
522 output( "\t.data\n" );
523 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
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
];
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" );
569 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
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
)));
579 output( "\t.byte 0x48\n" ); /* hotpatch prolog */
580 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp
)));
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
) );
610 output( "\n\t.section \".init\",\"ax\"\n" );
611 output( "\tcall %s\n", asm_name(constructor
) );
614 output( "\n\t.section \".text\",\"ax\"\n" );
615 output( "\tblx %s\n", asm_name(constructor
) );
619 output( "\n\t.section \".init\",\"ax\"\n" );
620 output( "\tbl %s\n", asm_name(constructor
) );
627 /*******************************************************************
630 * Output the module data.
632 void output_module( DLLSPEC
*spec
)
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 */
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" );
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
);
660 output( "\n\t.section \".init\",\"ax\"\n" );
661 output( "\tjmp 1f\n" );
664 output( "\n\t.section \".text\",\"ax\"\n" );
665 output( "\tb 1f\n" );
669 output( "\n\t.section \".init\",\"ax\"\n" );
670 output( "\tb 1f\n" );
673 output( "__wine_spec_pe_header:\n" );
674 output( "\t.skip %u\n", 65536 + page_size
);
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 */
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 */
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 */
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] */
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 /*******************************************************************
765 * Build a Win32 C file from a spec file.
767 void output_spec32_file( DLLSPEC
*spec
)
769 needs_get_pc_thunk
= 0;
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();
783 /*******************************************************************
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 */
832 put_word( 0 ); /* e_oemid */
833 put_word( 0 ); /* e_oeminfo */
834 put_dword( 0 ); /* e_res2 */
841 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
843 put_dword( 0x4550 ); /* Signature */
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
);
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] */
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 */
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 */
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 */
961 align_output( file_align
);
962 if (spec
->characteristics
& IMAGE_FILE_DLL
)
963 put_data( dll_code_section
, sizeof(dll_code_section
) );
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 */
975 align_output( file_align
);
976 put_data( resources
, resources_size
);
978 flush_output_buffer();
982 /*******************************************************************
985 * Build a Win32 def file from a spec file.
987 void output_def_file( DLLSPEC
*spec
, int import_only
)
989 DLLSPEC
*spec32
= NULL
;
993 if (spec
->type
== SPEC_WIN16
)
995 spec32
= alloc_dll_spec();
996 add_16bit_exports( spec32
, spec
);
1001 output( "; File generated automatically from %s; do not edit!\n\n",
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
;
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
);
1035 /* try to reduce output */
1036 if(!import_only
&& (strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
)))
1037 output( "=%s", odp
->link_name
);
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
));
1051 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", get_args_size( odp
));
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" );
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
[] )
1076 unsigned short e_magic
;
1077 unsigned short unused
[29];
1078 unsigned int e_lfanew
;
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
] );