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";
53 static struct strarray spec_extra_ld_symbols
= { 0 }; /* list of extra symbols that ld should resolve */
55 /* add a symbol to the list of extra symbols that ld must resolve */
56 void add_spec_extra_ld_symbol( const char *name
)
58 strarray_add( &spec_extra_ld_symbols
, name
, NULL
);
61 static unsigned int hash_filename( const char *name
)
64 unsigned int ret
= 2166136261u;
65 while (*name
) ret
= (ret
* 16777619) ^ *name
++;
69 /* check if entry point needs a relay thunk */
70 static inline int needs_relay( const ORDDEF
*odp
)
72 /* skip nonexistent entry points */
74 /* skip non-functions */
81 if (odp
->u
.func
.nb_args
!= -1) break;
86 /* skip norelay and forward entry points */
87 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) return 0;
91 static int is_float_arg( const ORDDEF
*odp
, int arg
)
93 if (arg
>= odp
->u
.func
.nb_args
) return 0;
94 return (odp
->u
.func
.args
[arg
] == ARG_FLOAT
|| odp
->u
.func
.args
[arg
] == ARG_DOUBLE
);
97 /* check if dll will output relay thunks */
98 static int has_relays( DLLSPEC
*spec
)
102 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
&&
103 target_cpu
!= CPU_ARM
&& target_cpu
!= CPU_ARM64
)
106 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
108 ORDDEF
*odp
= spec
->ordinals
[i
];
109 if (needs_relay( odp
)) return 1;
114 static int get_exports_count( DLLSPEC
*spec
)
116 if (unix_lib
) return 0;
117 if (spec
->base
> spec
->limit
) return 0;
118 return spec
->limit
- spec
->base
+ 1;
121 static int cmp_func_args( const void *p1
, const void *p2
)
123 const ORDDEF
*odp1
= *(const ORDDEF
**)p1
;
124 const ORDDEF
*odp2
= *(const ORDDEF
**)p2
;
126 return odp2
->u
.func
.nb_args
- odp1
->u
.func
.nb_args
;
129 static void get_arg_string( ORDDEF
*odp
, char str
[MAX_ARGUMENTS
+ 1] )
133 for (i
= 0; i
< odp
->u
.func
.nb_args
; i
++)
135 switch (odp
->u
.func
.args
[i
])
137 case ARG_STR
: str
[i
] = 's'; break;
138 case ARG_WSTR
: str
[i
] = 'w'; break;
139 case ARG_FLOAT
: str
[i
] = 'f'; break;
140 case ARG_DOUBLE
: str
[i
] = 'd'; break;
143 if (get_ptr_size() == 4)
145 str
[i
] = (odp
->u
.func
.args
[i
] == ARG_INT64
) ? 'j' : 'k';
156 if (odp
->flags
& (FLAG_THISCALL
| FLAG_FASTCALL
)) str
[0] = 't';
157 if ((odp
->flags
& FLAG_FASTCALL
) && odp
->u
.func
.nb_args
> 1) str
[1] = 't';
159 /* append return value */
160 if (get_ptr_size() == 4 && (odp
->flags
& FLAG_RET64
))
161 strcpy( str
+ i
, "J" );
163 strcpy( str
+ i
, "I" );
166 static void output_data_directories( const char *names
[16] )
170 for (i
= 0; i
< 16; i
++)
174 output_rva( "%s", names
[i
] );
175 output( "\t.long %s_end - %s\n", names
[i
], names
[i
] );
177 else output( "\t.long 0,0\n" );
181 /*******************************************************************
184 static char *build_args_string( DLLSPEC
*spec
)
186 int i
, count
= 0, len
= 1;
188 char str
[MAX_ARGUMENTS
+ 2];
191 funcs
= xmalloc( (spec
->limit
+ 1 - spec
->base
) * sizeof(*funcs
) );
192 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
194 ORDDEF
*odp
= spec
->ordinals
[i
];
196 if (!needs_relay( odp
)) continue;
197 funcs
[count
++] = odp
;
198 len
+= odp
->u
.func
.nb_args
+ 1;
200 /* sort functions by decreasing number of arguments */
201 qsort( funcs
, count
, sizeof(*funcs
), cmp_func_args
);
202 buffer
= xmalloc( len
);
204 /* build the arguments string, reusing substrings where possible */
205 for (i
= 0; i
< count
; i
++)
207 get_arg_string( funcs
[i
], str
);
208 if (!(p
= strstr( buffer
, str
)))
210 p
= buffer
+ strlen( buffer
);
213 funcs
[i
]->u
.func
.args_str_offset
= p
- buffer
;
219 /*******************************************************************
222 * Output entry points for relay debugging
224 static void output_relay_debug( DLLSPEC
*spec
)
228 /* first the table of entry point offsets */
230 output( "\t%s\n", get_asm_rodata_section() );
231 output( "\t.align %d\n", get_alignment(4) );
232 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
234 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
236 ORDDEF
*odp
= spec
->ordinals
[i
];
238 if (needs_relay( odp
))
239 output( "\t.long __wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i
);
241 output( "\t.long 0\n" );
244 /* then the strings of argument types */
246 output( ".L__wine_spec_relay_args_string:\n" );
247 output( "\t%s \"%s\"\n", get_asm_string_keyword(), build_args_string( spec
));
249 /* then the relay thunks */
251 output( "\t.text\n" );
252 output( "__wine_spec_relay_entry_points:\n" );
253 output( "\tnop\n" ); /* to avoid 0 offset */
255 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
257 ORDDEF
*odp
= spec
->ordinals
[i
];
259 if (!needs_relay( odp
)) continue;
264 output( "\t.align %d\n", get_alignment(4) );
265 output( "\t.long 0x90909090,0x90909090\n" );
266 output( "__wine_spec_relay_entry_point_%d:\n", i
);
267 output_cfi( ".cfi_startproc" );
268 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
269 if (odp
->flags
& (FLAG_THISCALL
| FLAG_FASTCALL
)) /* add the register arguments */
271 output( "\tpopl %%eax\n" );
272 if ((odp
->flags
& FLAG_FASTCALL
) && get_args_size( odp
) > 4) output( "\tpushl %%edx\n" );
273 output( "\tpushl %%ecx\n" );
274 output( "\tpushl %%eax\n" );
276 output( "\tpushl $%u\n", (odp
->u
.func
.args_str_offset
<< 16) | (i
- spec
->base
) );
277 output_cfi( ".cfi_adjust_cfa_offset 4" );
281 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
282 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
283 needs_get_pc_thunk
= 1;
285 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
286 output( "\tpushl %%eax\n" );
287 output_cfi( ".cfi_adjust_cfa_offset 4" );
289 output( "\tcall *4(%%eax)\n" );
290 output_cfi( ".cfi_adjust_cfa_offset -8" );
291 if (odp
->type
== TYPE_STDCALL
)
292 output( "\tret $%u\n", get_args_size( odp
));
295 output_cfi( ".cfi_endproc" );
300 int j
, has_float
= 0;
302 if (strcmp( float_abi_option
, "soft" ))
303 for (j
= 0; j
< odp
->u
.func
.nb_args
&& !has_float
; j
++)
304 has_float
= is_float_arg( odp
, j
);
306 output( "\t.align %d\n", get_alignment(4) );
307 if (thumb_mode
) output( "\t.thumb_func\n" );
308 output( "__wine_spec_relay_entry_point_%d:\n", i
);
309 output_cfi( ".cfi_startproc" );
310 output( "\tpush {r0-r3}\n" );
311 output( "\tmov r2, SP\n");
312 if (has_float
) output( "\tvpush {s0-s15}\n" );
313 output( "\tpush {LR}\n" );
314 output( "\tsub SP, #4\n");
315 output( "\tmovw r1,#%u\n", i
- spec
->base
);
316 output( "\tmovt r1,#%u\n", odp
->u
.func
.args_str_offset
);
319 output( "\tldr r0, 2f\n");
320 output( "1:\tadd r0, PC\n");
324 output( "\tmovw r0, :lower16:.L__wine_spec_relay_descr\n" );
325 output( "\tmovt r0, :upper16:.L__wine_spec_relay_descr\n" );
327 output( "\tldr IP, [r0, #4]\n");
328 output( "\tblx IP\n");
329 output( "\tldr IP, [SP, #4]\n" );
330 output( "\tadd SP, #%u\n", 24 + (has_float
? 64 : 0) );
331 output( "\tbx IP\n");
332 if (UsePIC
) output( "2:\t.long .L__wine_spec_relay_descr-1b-%u\n", thumb_mode
? 4 : 8 );
333 output_cfi( ".cfi_endproc" );
338 output( "\t.align %d\n", get_alignment(4) );
339 output( "__wine_spec_relay_entry_point_%d:\n", i
);
340 output_cfi( ".cfi_startproc" );
341 switch (odp
->u
.func
.nb_args
)
345 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
348 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
351 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
354 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
358 output( "\tmov x2, SP\n");
359 output( "\tstp x29, x30, [SP,#-16]!\n" );
360 output( "\tstp x8, x9, [SP,#-16]!\n" );
361 output( "\tmov w1, #%u\n", odp
->u
.func
.args_str_offset
<< 16 );
362 if (i
- spec
->base
) output( "\tadd w1, w1, #%u\n", i
- spec
->base
);
363 output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_relay_descr") );
364 output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_relay_descr") );
365 output( "\tldr x3, [x0, #8]\n");
366 output( "\tblr x3\n");
367 output( "\tadd SP, SP, #16\n" );
368 output( "\tldp x29, x30, [SP], #16\n" );
369 if (odp
->u
.func
.nb_args
)
370 output( "\tadd SP, SP, #%u\n", 8 * ((min(odp
->u
.func
.nb_args
, 8) + 1) & ~1) );
372 output_cfi( ".cfi_endproc" );
376 output( "\t.align %d\n", get_alignment(4) );
377 output( "\t.long 0x90909090,0x90909090\n" );
378 output( "__wine_spec_relay_entry_point_%d:\n", i
);
379 output_cfi( ".cfi_startproc" );
380 switch (odp
->u
.func
.nb_args
)
382 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp
, 3 ) ? "xmm3" : "r9" );
384 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp
, 2 ) ? "xmm2" : "r8" );
386 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp
, 1 ) ? "xmm1" : "rdx" );
388 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp
, 0 ) ? "xmm0" : "rcx" );
392 output( "\tmovl $%u,%%edx\n", (odp
->u
.func
.args_str_offset
<< 16) | (i
- spec
->base
) );
393 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
394 output( "\tcallq *8(%%rcx)\n" );
396 output_cfi( ".cfi_endproc" );
405 /*******************************************************************
408 * Output the export table for a Win32 module.
410 void output_exports( DLLSPEC
*spec
)
413 int needs_imports
= 0;
414 int needs_relay
= has_relays( spec
);
415 int nr_exports
= get_exports_count( spec
);
416 const char *func_ptr
= is_pe() ? ".rva" : get_asm_ptr_keyword();
419 if (!nr_exports
) return;
421 output( "\n/* export table */\n\n" );
422 output( "\t%s\n", get_asm_export_section() );
423 output( "\t.align %d\n", get_alignment(4) );
424 output( ".L__wine_spec_exports:\n" );
426 /* export directory header */
428 output( "\t.long 0\n" ); /* Characteristics */
429 output( "\t.long %u\n", hash_filename(spec
->file_name
) ); /* TimeDateStamp */
430 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
431 output_rva( ".L__wine_spec_exp_names" ); /* Name */
432 output( "\t.long %u\n", spec
->base
); /* Base */
433 output( "\t.long %u\n", nr_exports
); /* NumberOfFunctions */
434 output( "\t.long %u\n", spec
->nb_names
); /* NumberOfNames */
435 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
438 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
439 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
443 output( "\t.long 0\n" ); /* AddressOfNames */
444 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
447 /* output the function pointers */
449 output( "\n.L__wine_spec_exports_funcs:\n" );
450 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
452 ORDDEF
*odp
= spec
->ordinals
[i
];
453 if (!odp
) output( "\t%s 0\n", is_pe() ? ".long" : get_asm_ptr_keyword() );
454 else switch(odp
->type
)
460 if (odp
->flags
& FLAG_FORWARD
)
462 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr
, fwd_size
);
463 fwd_size
+= strlen(odp
->link_name
) + 1;
465 else if ((odp
->flags
& FLAG_IMPORT
) && (target_cpu
== CPU_x86
|| target_cpu
== CPU_x86_64
))
467 name
= odp
->name
? odp
->name
: odp
->export_name
;
468 if (name
) output( "\t%s %s_%s\n", func_ptr
, asm_name("__wine_spec_imp"), name
);
469 else output( "\t%s %s_%u\n", func_ptr
, asm_name("__wine_spec_imp"), i
);
472 else if (odp
->flags
& FLAG_EXT_LINK
)
474 output( "\t%s %s_%s\n", func_ptr
, asm_name("__wine_spec_ext_link"), odp
->link_name
);
478 output( "\t%s %s\n", func_ptr
, asm_name( get_link_name( odp
)));
482 output( "\t%s %s\n", func_ptr
, asm_name( get_stub_name( odp
, spec
)) );
491 /* output the function name pointers */
493 int namepos
= strlen(spec
->file_name
) + 1;
495 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
496 for (i
= 0; i
< spec
->nb_names
; i
++)
498 output_rva( ".L__wine_spec_exp_names + %u", namepos
);
499 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
502 /* output the function ordinals */
504 output( "\n.L__wine_spec_exp_ordinals:\n" );
505 for (i
= 0; i
< spec
->nb_names
; i
++)
507 output( "\t.short %d\n", spec
->names
[i
]->ordinal
- spec
->base
);
509 if (spec
->nb_names
% 2)
511 output( "\t.short 0\n" );
517 output( "\t.long 0xdeb90002\n" ); /* magic */
518 if (is_pe()) output_rva( ".L__wine_spec_relay_descr" );
519 else output( "\t.long 0\n" );
522 /* output the export name strings */
524 output( "\n.L__wine_spec_exp_names:\n" );
525 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
526 for (i
= 0; i
< spec
->nb_names
; i
++)
527 output( "\t%s \"%s\"\n",
528 get_asm_string_keyword(), spec
->names
[i
]->name
);
530 /* output forward strings */
534 output( "\n.L__wine_spec_forwards:\n" );
535 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
537 ORDDEF
*odp
= spec
->ordinals
[i
];
538 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
539 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->link_name
);
549 output( "\t.data\n" );
550 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
554 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
555 output( ".L__wine_spec_exports_end:\n" );
558 output( ".L__wine_spec_relay_descr:\n" );
559 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
560 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
561 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
562 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
563 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
564 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
566 output_relay_debug( spec
);
570 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
571 output( ".L__wine_spec_exports_end:\n" );
572 output( "\t%s 0\n", get_asm_ptr_keyword() );
575 /* output import thunks */
577 if (!needs_imports
) return;
578 output( "\t.text\n" );
579 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
581 ORDDEF
*odp
= spec
->ordinals
[i
];
583 if (!(odp
->flags
& FLAG_IMPORT
)) continue;
585 name
= odp
->name
? odp
->name
: odp
->export_name
;
587 output( "\t.align %d\n", get_alignment(4) );
588 output( "\t.long 0x90909090,0x90909090\n" );
589 if (name
) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name
);
590 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i
);
591 output_cfi( ".cfi_startproc" );
596 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
599 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
600 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp
)));
601 needs_get_pc_thunk
= 1;
603 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp
)));
606 output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
607 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp
)));
612 output_cfi( ".cfi_endproc" );
617 /*******************************************************************
620 * Output the module data.
622 void output_module( DLLSPEC
*spec
)
626 unsigned int page_size
= get_page_size();
627 const char *data_dirs
[16] = { NULL
};
629 /* Reserve some space for the PE header */
631 switch (target_platform
)
634 case PLATFORM_WINDOWS
:
635 return; /* nothing to do */
637 output( "\t.text\n" );
638 output( "\t.align %d\n", get_alignment(page_size
) );
639 output( "__wine_spec_pe_header:\n" );
640 output( "\t.space 65536\n" );
642 case PLATFORM_SOLARIS
:
643 output( "\n\t.section \".text\",\"ax\"\n" );
644 output( "__wine_spec_pe_header:\n" );
645 output( "\t.skip %u\n", 65536 + page_size
);
652 output( "\n\t.section \".init\",\"ax\"\n" );
653 output( "\tjmp 1f\n" );
656 output( "\n\t.section \".text\",\"ax\"\n" );
657 output( "\tb 1f\n" );
661 output( "\n\t.section \".init\",\"ax\"\n" );
662 output( "\tb 1f\n" );
665 output( "__wine_spec_pe_header:\n" );
666 output( "\t.skip %u\n", 65536 + page_size
);
671 /* Output the NT header */
673 output( "\n\t.data\n" );
674 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
675 output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
676 output( "%s:\n", asm_name("__wine_spec_nt_header") );
677 output( ".L__wine_spec_rva_base:\n" );
679 output( "\t.long 0x4550\n" ); /* Signature */
682 case CPU_x86
: machine
= IMAGE_FILE_MACHINE_I386
; break;
683 case CPU_x86_64
: machine
= IMAGE_FILE_MACHINE_AMD64
; break;
684 case CPU_POWERPC
: machine
= IMAGE_FILE_MACHINE_POWERPC
; break;
685 case CPU_ARM
: machine
= IMAGE_FILE_MACHINE_ARMNT
; break;
686 case CPU_ARM64
: machine
= IMAGE_FILE_MACHINE_ARM64
; break;
688 output( "\t.short 0x%04x\n", /* Machine */
690 output( "\t.short 0\n" ); /* NumberOfSections */
691 output( "\t.long %u\n", hash_filename(spec
->file_name
) ); /* TimeDateStamp */
692 output( "\t.long 0\n" ); /* PointerToSymbolTable */
693 output( "\t.long 0\n" ); /* NumberOfSymbols */
694 output( "\t.short %d\n", /* SizeOfOptionalHeader */
695 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
: IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
);
696 output( "\t.short 0x%04x\n", /* Characteristics */
697 spec
->characteristics
);
698 output( "\t.short 0x%04x\n", /* Magic */
699 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
: IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
700 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
701 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
702 output( "\t.long 0\n" ); /* SizeOfCode */
703 output( "\t.long 0\n" ); /* SizeOfInitializedData */
704 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
706 for (i
= 0; i
< spec_extra_ld_symbols
.count
; i
++)
707 output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols
.str
[i
]) );
709 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
710 output( "\t%s %s\n", /* AddressOfEntryPoint */
711 get_asm_ptr_keyword(), spec
->init_func
? asm_name(spec
->init_func
) : "0" );
712 if (get_ptr_size() == 4)
714 output( "\t.long 0\n" ); /* BaseOfCode */
715 output( "\t.long 0\n" ); /* BaseOfData */
717 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
718 get_asm_ptr_keyword() );
719 output( "\t.long %u\n", page_size
); /* SectionAlignment */
720 output( "\t.long %u\n", page_size
); /* FileAlignment */
721 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
722 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
723 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
724 spec
->subsystem_major
, spec
->subsystem_minor
);
725 output( "\t.long 0\n" ); /* Win32VersionValue */
726 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
727 output( "\t.long %u\n", page_size
); /* SizeOfHeaders */
728 output( "\t.long 0\n" ); /* CheckSum */
729 output( "\t.short 0x%04x\n", /* Subsystem */
731 output( "\t.short 0x%04x\n", /* DllCharacteristics */
732 spec
->dll_characteristics
);
733 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
734 get_asm_ptr_keyword(), (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
735 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
736 get_asm_ptr_keyword(), (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
737 output( "\t.long 0\n" ); /* LoaderFlags */
738 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
740 if (get_exports_count( spec
))
741 data_dirs
[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
743 data_dirs
[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
744 if (spec
->nb_resources
)
745 data_dirs
[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
747 output_data_directories( data_dirs
);
749 if (target_platform
== PLATFORM_APPLE
)
750 output( "\t.lcomm %s,4\n", asm_name("_end") );
754 /*******************************************************************
757 * Build a Win32 C file from a spec file.
759 void output_spec32_file( DLLSPEC
*spec
)
761 needs_get_pc_thunk
= 0;
763 output_standard_file_header();
764 output_module( spec
);
765 output_stubs( spec
);
766 output_exports( spec
);
767 output_imports( spec
);
768 output_syscalls( spec
);
769 if (needs_get_pc_thunk
) output_get_pc_thunk();
770 output_resources( spec
);
771 output_gnu_stack_note();
776 /*******************************************************************
779 * Build a fake binary module from a spec file.
781 void output_fake_module( DLLSPEC
*spec
)
783 static const unsigned char dll_code_section
[] = { 0x31, 0xc0, /* xor %eax,%eax */
784 0xc2, 0x0c, 0x00 }; /* ret $12 */
786 static const unsigned char exe_code_section
[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
787 0xc2, 0x04, 0x00 }; /* ret $4 */
789 const unsigned int page_size
= get_page_size();
790 const unsigned int section_align
= page_size
;
791 const unsigned int file_align
= 0x200;
792 const unsigned int reloc_size
= 8;
793 const unsigned int lfanew
= 0x40 + sizeof(fakedll_signature
);
794 const unsigned int nb_sections
= 2 + (spec
->nb_resources
!= 0);
795 const unsigned int text_size
= (spec
->characteristics
& IMAGE_FILE_DLL
) ?
796 sizeof(dll_code_section
) : sizeof(exe_code_section
);
797 unsigned char *resources
;
798 unsigned int resources_size
;
799 unsigned int image_size
= 3 * section_align
;
801 resolve_imports( spec
);
802 output_bin_resources( spec
, 3 * section_align
);
803 resources
= output_buffer
;
804 resources_size
= output_buffer_pos
;
805 if (resources_size
) image_size
+= (resources_size
+ section_align
- 1) & ~(section_align
- 1);
807 init_output_buffer();
809 put_word( 0x5a4d ); /* e_magic */
810 put_word( 0x40 ); /* e_cblp */
811 put_word( 0x01 ); /* e_cp */
812 put_word( 0 ); /* e_crlc */
813 put_word( lfanew
/ 16 ); /* e_cparhdr */
814 put_word( 0x0000 ); /* e_minalloc */
815 put_word( 0xffff ); /* e_maxalloc */
816 put_word( 0x0000 ); /* e_ss */
817 put_word( 0x00b8 ); /* e_sp */
818 put_word( 0 ); /* e_csum */
819 put_word( 0 ); /* e_ip */
820 put_word( 0 ); /* e_cs */
821 put_word( lfanew
); /* e_lfarlc */
822 put_word( 0 ); /* e_ovno */
823 put_dword( 0 ); /* e_res */
825 put_word( 0 ); /* e_oemid */
826 put_word( 0 ); /* e_oeminfo */
827 put_dword( 0 ); /* e_res2 */
834 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
836 put_dword( 0x4550 ); /* Signature */
839 case CPU_x86
: put_word( IMAGE_FILE_MACHINE_I386
); break;
840 case CPU_x86_64
: put_word( IMAGE_FILE_MACHINE_AMD64
); break;
841 case CPU_POWERPC
: put_word( IMAGE_FILE_MACHINE_POWERPC
); break;
842 case CPU_ARM
: put_word( IMAGE_FILE_MACHINE_ARMNT
); break;
843 case CPU_ARM64
: put_word( IMAGE_FILE_MACHINE_ARM64
); break;
845 put_word( nb_sections
); /* NumberOfSections */
846 put_dword( hash_filename(spec
->file_name
) ); /* TimeDateStamp */
847 put_dword( 0 ); /* PointerToSymbolTable */
848 put_dword( 0 ); /* NumberOfSymbols */
849 put_word( get_ptr_size() == 8 ?
850 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
:
851 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
); /* SizeOfOptionalHeader */
852 put_word( spec
->characteristics
); /* Characteristics */
853 put_word( get_ptr_size() == 8 ?
854 IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
855 IMAGE_NT_OPTIONAL_HDR32_MAGIC
); /* Magic */
856 put_byte( 7 ); /* MajorLinkerVersion */
857 put_byte( 10 ); /* MinorLinkerVersion */
858 put_dword( text_size
); /* SizeOfCode */
859 put_dword( 0 ); /* SizeOfInitializedData */
860 put_dword( 0 ); /* SizeOfUninitializedData */
861 put_dword( section_align
); /* AddressOfEntryPoint */
862 put_dword( section_align
); /* BaseOfCode */
863 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
864 put_pword( 0x10000000 ); /* ImageBase */
865 put_dword( section_align
); /* SectionAlignment */
866 put_dword( file_align
); /* FileAlignment */
867 put_word( 1 ); /* MajorOperatingSystemVersion */
868 put_word( 0 ); /* MinorOperatingSystemVersion */
869 put_word( 0 ); /* MajorImageVersion */
870 put_word( 0 ); /* MinorImageVersion */
871 put_word( spec
->subsystem_major
); /* MajorSubsystemVersion */
872 put_word( spec
->subsystem_minor
); /* MinorSubsystemVersion */
873 put_dword( 0 ); /* Win32VersionValue */
874 put_dword( image_size
); /* SizeOfImage */
875 put_dword( file_align
); /* SizeOfHeaders */
876 put_dword( 0 ); /* CheckSum */
877 put_word( spec
->subsystem
); /* Subsystem */
878 put_word( spec
->dll_characteristics
); /* DllCharacteristics */
879 put_pword( (spec
->stack_size
? spec
->stack_size
: 1024) * 1024 ); /* SizeOfStackReserve */
880 put_pword( page_size
); /* SizeOfStackCommit */
881 put_pword( (spec
->heap_size
? spec
->heap_size
: 1024) * 1024 ); /* SizeOfHeapReserve */
882 put_pword( page_size
); /* SizeOfHeapCommit */
883 put_dword( 0 ); /* LoaderFlags */
884 put_dword( 16 ); /* NumberOfRvaAndSizes */
886 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
887 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
888 if (resources_size
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
890 put_dword( 3 * section_align
);
891 put_dword( resources_size
);
899 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
900 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
901 put_dword( 2 * section_align
); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
902 put_dword( reloc_size
);
903 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
904 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
905 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
906 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
907 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
908 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
909 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
910 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
911 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
912 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
915 put_data( ".text\0\0", 8 ); /* Name */
916 put_dword( section_align
); /* VirtualSize */
917 put_dword( section_align
); /* VirtualAddress */
918 put_dword( text_size
); /* SizeOfRawData */
919 put_dword( file_align
); /* PointerToRawData */
920 put_dword( 0 ); /* PointerToRelocations */
921 put_dword( 0 ); /* PointerToLinenumbers */
922 put_word( 0 ); /* NumberOfRelocations */
923 put_word( 0 ); /* NumberOfLinenumbers */
924 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
927 put_data( ".reloc\0", 8 ); /* Name */
928 put_dword( section_align
); /* VirtualSize */
929 put_dword( 2 * section_align
);/* VirtualAddress */
930 put_dword( reloc_size
); /* SizeOfRawData */
931 put_dword( 2 * file_align
); /* PointerToRawData */
932 put_dword( 0 ); /* PointerToRelocations */
933 put_dword( 0 ); /* PointerToLinenumbers */
934 put_word( 0 ); /* NumberOfRelocations */
935 put_word( 0 ); /* NumberOfLinenumbers */
936 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
941 put_data( ".rsrc\0\0", 8 ); /* Name */
942 put_dword( (resources_size
+ section_align
- 1) & ~(section_align
- 1) ); /* VirtualSize */
943 put_dword( 3 * section_align
);/* VirtualAddress */
944 put_dword( resources_size
); /* SizeOfRawData */
945 put_dword( 3 * file_align
); /* PointerToRawData */
946 put_dword( 0 ); /* PointerToRelocations */
947 put_dword( 0 ); /* PointerToLinenumbers */
948 put_word( 0 ); /* NumberOfRelocations */
949 put_word( 0 ); /* NumberOfLinenumbers */
950 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
954 align_output( file_align
);
955 if (spec
->characteristics
& IMAGE_FILE_DLL
)
956 put_data( dll_code_section
, sizeof(dll_code_section
) );
958 put_data( exe_code_section
, sizeof(exe_code_section
) );
960 /* .reloc contents */
961 align_output( file_align
);
962 put_dword( 0 ); /* VirtualAddress */
963 put_dword( 0 ); /* SizeOfBlock */
968 align_output( file_align
);
969 put_data( resources
, resources_size
);
971 flush_output_buffer();
975 /*******************************************************************
978 * Build a Win32 def file from a spec file.
980 void output_def_file( DLLSPEC
*spec
, int import_only
)
982 DLLSPEC
*spec32
= NULL
;
986 if (spec
->type
== SPEC_WIN16
)
988 spec32
= alloc_dll_spec();
989 add_16bit_exports( spec32
, spec
);
994 output( "; File generated automatically from %s; do not edit!\n\n",
997 output( "; File generated automatically; do not edit!\n\n" );
999 output( "LIBRARY %s\n\n", spec
->file_name
);
1000 output( "EXPORTS\n");
1002 /* Output the exports and relay entry points */
1004 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
1006 const ORDDEF
*odp
= &spec
->entry_points
[i
];
1007 int is_data
= 0, is_private
= odp
->flags
& FLAG_PRIVATE
;
1009 if (odp
->name
) name
= odp
->name
;
1010 else if (odp
->export_name
) name
= odp
->export_name
;
1013 if (!is_private
) total
++;
1014 if (import_only
&& odp
->type
== TYPE_STUB
) continue;
1016 if ((odp
->flags
& FLAG_FASTCALL
) && is_pe())
1017 name
= strmake( "@%s", name
);
1019 output( " %s", name
);
1028 /* try to reduce output */
1029 if(!import_only
&& (strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
)))
1030 output( "=%s", odp
->link_name
);
1034 int at_param
= get_args_size( odp
);
1035 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
1036 if (import_only
) break;
1037 if (odp
->flags
& FLAG_FORWARD
)
1038 output( "=%s", odp
->link_name
);
1039 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
1040 output( "=%s", get_link_name( odp
));
1044 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", get_args_size( odp
));
1050 output( " @%d", odp
->ordinal
);
1051 if (!odp
->name
|| (odp
->flags
& FLAG_ORDINAL
)) output( " NONAME" );
1052 if (is_data
) output( " DATA" );
1053 if (is_private
) output( " PRIVATE" );
1056 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);
1057 if (spec32
) free_dll_spec( spec32
);
1061 /*******************************************************************
1062 * make_builtin_files
1064 void make_builtin_files( char *argv
[] )
1069 unsigned short e_magic
;
1070 unsigned short unused
[29];
1071 unsigned int e_lfanew
;
1074 for (i
= 0; argv
[i
]; i
++)
1076 if ((fd
= open( argv
[i
], O_RDWR
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", argv
[i
] );
1077 if (read( fd
, &header
, sizeof(header
) ) == sizeof(header
) && !memcmp( &header
.e_magic
, "MZ", 2 ))
1079 if (header
.e_lfanew
< sizeof(header
) + sizeof(builtin_signature
))
1080 fatal_error( "%s: Not enough space (%x) for Wine signature\n", argv
[i
], header
.e_lfanew
);
1081 write( fd
, builtin_signature
, sizeof(builtin_signature
) );
1085 unsigned int pos
= header
.e_lfanew
+ 0x5e; /* OptionalHeader.DllCharacteristics */
1086 unsigned short dll_charact
;
1087 lseek( fd
, pos
, SEEK_SET
);
1088 if (read( fd
, &dll_charact
, sizeof(dll_charact
) ) == sizeof(dll_charact
))
1090 dll_charact
|= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE
;
1091 lseek( fd
, pos
, SEEK_SET
);
1092 write( fd
, &dll_charact
, sizeof(dll_charact
) );
1096 else fatal_error( "%s: Unrecognized file format\n", argv
[i
] );
1101 static void fixup_elf32( const char *name
, int fd
, void *header
, size_t header_size
)
1105 unsigned char e_ident
[16];
1106 unsigned short e_type
;
1107 unsigned short e_machine
;
1108 unsigned int e_version
;
1109 unsigned int e_entry
;
1110 unsigned int e_phoff
;
1111 unsigned int e_shoff
;
1112 unsigned int e_flags
;
1113 unsigned short e_ehsize
;
1114 unsigned short e_phentsize
;
1115 unsigned short e_phnum
;
1116 unsigned short e_shentsize
;
1117 unsigned short e_shnum
;
1118 unsigned short e_shstrndx
;
1122 unsigned int p_type
;
1123 unsigned int p_offset
;
1124 unsigned int p_vaddr
;
1125 unsigned int p_paddr
;
1126 unsigned int p_filesz
;
1127 unsigned int p_memsz
;
1128 unsigned int p_flags
;
1129 unsigned int p_align
;
1137 unsigned int i
, size
;
1139 if (header_size
< sizeof(*elf
)) return;
1140 if (elf
->e_ident
[6] != 1 /* EV_CURRENT */) return;
1142 size
= elf
->e_phnum
* elf
->e_phentsize
;
1143 phdr
= xmalloc( size
);
1144 lseek( fd
, elf
->e_phoff
, SEEK_SET
);
1145 if (read( fd
, phdr
, size
) != size
) return;
1147 for (i
= 0; i
< elf
->e_phnum
; i
++)
1149 if (phdr
->p_type
== 2 /* PT_DYNAMIC */ ) break;
1150 phdr
= (void *)((char *)phdr
+ elf
->e_phentsize
);
1152 if (i
== elf
->e_phnum
) return;
1154 dyn
= xmalloc( phdr
->p_filesz
);
1155 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1156 if (read( fd
, dyn
, phdr
->p_filesz
) != phdr
->p_filesz
) return;
1157 for (i
= 0; i
< phdr
->p_filesz
/ sizeof(*dyn
) && dyn
[i
].d_tag
; i
++)
1159 switch (dyn
[i
].d_tag
)
1161 case 25: dyn
[i
].d_tag
= 0x60009990; break; /* DT_INIT_ARRAY */
1162 case 27: dyn
[i
].d_tag
= 0x60009991; break; /* DT_INIT_ARRAYSZ */
1163 case 12: dyn
[i
].d_tag
= 0x60009992; break; /* DT_INIT */
1166 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1167 write( fd
, dyn
, phdr
->p_filesz
);
1170 static void fixup_elf64( const char *name
, int fd
, void *header
, size_t header_size
)
1174 unsigned char e_ident
[16];
1175 unsigned short e_type
;
1176 unsigned short e_machine
;
1177 unsigned int e_version
;
1178 unsigned __int64 e_entry
;
1179 unsigned __int64 e_phoff
;
1180 unsigned __int64 e_shoff
;
1181 unsigned int e_flags
;
1182 unsigned short e_ehsize
;
1183 unsigned short e_phentsize
;
1184 unsigned short e_phnum
;
1185 unsigned short e_shentsize
;
1186 unsigned short e_shnum
;
1187 unsigned short e_shstrndx
;
1191 unsigned int p_type
;
1192 unsigned int p_flags
;
1193 unsigned __int64 p_offset
;
1194 unsigned __int64 p_vaddr
;
1195 unsigned __int64 p_paddr
;
1196 unsigned __int64 p_filesz
;
1197 unsigned __int64 p_memsz
;
1198 unsigned __int64 p_align
;
1202 unsigned __int64 d_tag
;
1203 unsigned __int64 d_val
;
1206 unsigned int i
, size
;
1208 if (header_size
< sizeof(*elf
)) return;
1209 if (elf
->e_ident
[6] != 1 /* EV_CURRENT */) return;
1211 size
= elf
->e_phnum
* elf
->e_phentsize
;
1212 phdr
= xmalloc( size
);
1213 lseek( fd
, elf
->e_phoff
, SEEK_SET
);
1214 if (read( fd
, phdr
, size
) != size
) return;
1216 for (i
= 0; i
< elf
->e_phnum
; i
++)
1218 if (phdr
->p_type
== 2 /* PT_DYNAMIC */ ) break;
1219 phdr
= (void *)((char *)phdr
+ elf
->e_phentsize
);
1221 if (i
== elf
->e_phnum
) return;
1223 dyn
= xmalloc( phdr
->p_filesz
);
1224 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1225 if (read( fd
, dyn
, phdr
->p_filesz
) != phdr
->p_filesz
) return;
1226 for (i
= 0; i
< phdr
->p_filesz
/ sizeof(*dyn
) && dyn
[i
].d_tag
; i
++)
1228 switch (dyn
[i
].d_tag
)
1230 case 25: dyn
[i
].d_tag
= 0x60009990; break; /* DT_INIT_ARRAY */
1231 case 27: dyn
[i
].d_tag
= 0x60009991; break; /* DT_INIT_ARRAYSZ */
1232 case 12: dyn
[i
].d_tag
= 0x60009992; break; /* DT_INIT */
1235 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1236 write( fd
, dyn
, phdr
->p_filesz
);
1239 /*******************************************************************
1240 * fixup_constructors
1242 void fixup_constructors( char *argv
[] )
1245 unsigned int header
[64];
1247 for (i
= 0; argv
[i
]; i
++)
1249 if ((fd
= open( argv
[i
], O_RDWR
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", argv
[i
] );
1250 size
= read( fd
, &header
, sizeof(header
) );
1253 if (!memcmp( header
, "\177ELF\001", 5 )) fixup_elf32( argv
[i
], fd
, header
, size
);
1254 else if (!memcmp( header
, "\177ELF\002", 5 )) fixup_elf64( argv
[i
], fd
, header
, size
);