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 .L__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 if (thumb_mode
) output( "\t.thumb_func\n" );
253 output( "__wine_spec_relay_entry_points:\n" );
254 output( "\tnop\n" ); /* to avoid 0 offset */
256 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
258 ORDDEF
*odp
= spec
->ordinals
[i
];
260 if (!needs_relay( odp
)) continue;
265 output( "\t.align %d\n", get_alignment(4) );
266 output( "\t.long 0x90909090,0x90909090\n" );
267 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
268 output_cfi( ".cfi_startproc" );
269 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
270 if (odp
->flags
& (FLAG_THISCALL
| FLAG_FASTCALL
)) /* add the register arguments */
272 output( "\tpopl %%eax\n" );
273 if ((odp
->flags
& FLAG_FASTCALL
) && get_args_size( odp
) > 4) output( "\tpushl %%edx\n" );
274 output( "\tpushl %%ecx\n" );
275 output( "\tpushl %%eax\n" );
277 output( "\tpushl $%u\n", (odp
->u
.func
.args_str_offset
<< 16) | (i
- spec
->base
) );
278 output_cfi( ".cfi_adjust_cfa_offset 4" );
282 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
283 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
284 needs_get_pc_thunk
= 1;
286 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
287 output( "\tpushl %%eax\n" );
288 output_cfi( ".cfi_adjust_cfa_offset 4" );
290 output( "\tcall *4(%%eax)\n" );
291 output_cfi( ".cfi_adjust_cfa_offset -8" );
292 if (odp
->type
== TYPE_STDCALL
)
293 output( "\tret $%u\n", get_args_size( odp
));
296 output_cfi( ".cfi_endproc" );
301 unsigned int mask
, val
, count
= 0;
302 int j
, has_float
= 0;
304 if (strcmp( float_abi_option
, "soft" ))
305 for (j
= 0; j
< odp
->u
.func
.nb_args
&& !has_float
; j
++)
306 has_float
= is_float_arg( odp
, j
);
308 val
= (odp
->u
.func
.args_str_offset
<< 16) | (i
- spec
->base
);
309 output( "\t.align %d\n", get_alignment(4) );
310 if (thumb_mode
) output( "\t.thumb_func\n" );
311 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
312 output_cfi( ".cfi_startproc" );
313 output( "\tpush {r0-r3}\n" );
314 output( "\tmov r2, SP\n");
315 if (has_float
) output( "\tvpush {s0-s15}\n" );
316 output( "\tpush {LR}\n" );
317 output( "\tsub SP, #4\n");
318 for (mask
= 0xff; mask
; mask
<<= 8)
319 if (val
& mask
) output( "\t%s r1,#%u\n", count
++ ? "add" : "mov", val
& mask
);
320 if (!count
) output( "\tmov r1,#0\n" );
321 output( "\tldr r0, 2f\n");
322 if (UsePIC
) output( "1:\tadd r0, PC\n");
323 output( "\tldr IP, [r0, #4]\n");
324 output( "\tblx IP\n");
325 output( "\tldr IP, [SP, #4]\n" );
326 output( "\tadd SP, #%u\n", 24 + (has_float
? 64 : 0) );
327 output( "\tbx IP\n");
328 if (UsePIC
) output( "2:\t.long .L__wine_spec_relay_descr-1b-%u\n", thumb_mode
? 4 : 8 );
329 else output( "2:\t.long .L__wine_spec_relay_descr\n" );
330 output_cfi( ".cfi_endproc" );
335 output( "\t.align %d\n", get_alignment(4) );
336 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
337 output_cfi( ".cfi_startproc" );
338 switch (odp
->u
.func
.nb_args
)
342 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
345 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
348 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
351 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
355 output( "\tmov x2, SP\n");
356 output( "\tstp x29, x30, [SP,#-16]!\n" );
357 output( "\tstp x8, x9, [SP,#-16]!\n" );
358 output( "\tmov w1, #%u\n", odp
->u
.func
.args_str_offset
<< 16 );
359 if (i
- spec
->base
) output( "\tadd w1, w1, #%u\n", i
- spec
->base
);
360 output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_relay_descr") );
361 output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_relay_descr") );
362 output( "\tldr x3, [x0, #8]\n");
363 output( "\tblr x3\n");
364 output( "\tadd SP, SP, #16\n" );
365 output( "\tldp x29, x30, [SP], #16\n" );
366 if (odp
->u
.func
.nb_args
)
367 output( "\tadd SP, SP, #%u\n", 8 * ((min(odp
->u
.func
.nb_args
, 8) + 1) & ~1) );
369 output_cfi( ".cfi_endproc" );
373 output( "\t.align %d\n", get_alignment(4) );
374 output( "\t.long 0x90909090,0x90909090\n" );
375 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
376 output_cfi( ".cfi_startproc" );
377 switch (odp
->u
.func
.nb_args
)
379 default: output( "\tmovq %%%s,32(%%rsp)\n", is_float_arg( odp
, 3 ) ? "xmm3" : "r9" );
381 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp
, 2 ) ? "xmm2" : "r8" );
383 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp
, 1 ) ? "xmm1" : "rdx" );
385 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp
, 0 ) ? "xmm0" : "rcx" );
389 output( "\tmovl $%u,%%edx\n", (odp
->u
.func
.args_str_offset
<< 16) | (i
- spec
->base
) );
390 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
391 output( "\tcallq *8(%%rcx)\n" );
393 output_cfi( ".cfi_endproc" );
402 /*******************************************************************
405 * Output the export table for a Win32 module.
407 void output_exports( DLLSPEC
*spec
)
410 int needs_imports
= 0;
411 int needs_relay
= has_relays( spec
);
412 int nr_exports
= get_exports_count( spec
);
413 const char *func_ptr
= (target_platform
== PLATFORM_WINDOWS
) ? ".rva" : get_asm_ptr_keyword();
416 if (!nr_exports
) return;
418 output( "\n/* export table */\n\n" );
419 output( "\t%s\n", get_asm_export_section() );
420 output( "\t.align %d\n", get_alignment(4) );
421 output( ".L__wine_spec_exports:\n" );
423 /* export directory header */
425 output( "\t.long 0\n" ); /* Characteristics */
426 output( "\t.long %u\n", hash_filename(spec
->file_name
) ); /* TimeDateStamp */
427 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
428 output_rva( ".L__wine_spec_exp_names" ); /* Name */
429 output( "\t.long %u\n", spec
->base
); /* Base */
430 output( "\t.long %u\n", nr_exports
); /* NumberOfFunctions */
431 output( "\t.long %u\n", spec
->nb_names
); /* NumberOfNames */
432 output_rva( ".L__wine_spec_exports_funcs " ); /* AddressOfFunctions */
435 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
436 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
440 output( "\t.long 0\n" ); /* AddressOfNames */
441 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
444 /* output the function pointers */
446 output( "\n.L__wine_spec_exports_funcs:\n" );
447 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
449 ORDDEF
*odp
= spec
->ordinals
[i
];
450 if (!odp
) output( "\t%s 0\n",
451 (target_platform
== PLATFORM_WINDOWS
) ? ".long" : get_asm_ptr_keyword() );
452 else switch(odp
->type
)
458 if (odp
->flags
& FLAG_FORWARD
)
460 output( "\t%s .L__wine_spec_forwards+%u\n", func_ptr
, fwd_size
);
461 fwd_size
+= strlen(odp
->link_name
) + 1;
463 else if ((odp
->flags
& FLAG_IMPORT
) && (target_cpu
== CPU_x86
|| target_cpu
== CPU_x86_64
))
465 name
= odp
->name
? odp
->name
: odp
->export_name
;
466 if (name
) output( "\t%s %s_%s\n", func_ptr
, asm_name("__wine_spec_imp"), name
);
467 else output( "\t%s %s_%u\n", func_ptr
, asm_name("__wine_spec_imp"), i
);
470 else if (odp
->flags
& FLAG_EXT_LINK
)
472 output( "\t%s %s_%s\n", func_ptr
, asm_name("__wine_spec_ext_link"), odp
->link_name
);
476 output( "\t%s %s\n", func_ptr
, asm_name( get_link_name( odp
)));
480 output( "\t%s %s\n", func_ptr
, asm_name( get_stub_name( odp
, spec
)) );
489 /* output the function name pointers */
491 int namepos
= strlen(spec
->file_name
) + 1;
493 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
494 for (i
= 0; i
< spec
->nb_names
; i
++)
496 output_rva( ".L__wine_spec_exp_names + %u", namepos
);
497 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
500 /* output the function ordinals */
502 output( "\n.L__wine_spec_exp_ordinals:\n" );
503 for (i
= 0; i
< spec
->nb_names
; i
++)
505 output( "\t.short %d\n", spec
->names
[i
]->ordinal
- spec
->base
);
507 if (spec
->nb_names
% 2)
509 output( "\t.short 0\n" );
515 output( "\t.long 0xdeb90002\n" ); /* magic */
516 if (target_platform
== PLATFORM_WINDOWS
) output_rva( ".L__wine_spec_relay_descr" );
517 else output( "\t.long 0\n" );
520 /* output the export name strings */
522 output( "\n.L__wine_spec_exp_names:\n" );
523 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
524 for (i
= 0; i
< spec
->nb_names
; i
++)
525 output( "\t%s \"%s\"\n",
526 get_asm_string_keyword(), spec
->names
[i
]->name
);
528 /* output forward strings */
532 output( "\n.L__wine_spec_forwards:\n" );
533 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
535 ORDDEF
*odp
= spec
->ordinals
[i
];
536 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
537 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->link_name
);
545 if (target_platform
== PLATFORM_WINDOWS
)
547 output( "\t.data\n" );
548 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
552 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
553 output( ".L__wine_spec_exports_end:\n" );
556 output( ".L__wine_spec_relay_descr:\n" );
557 output( "\t%s 0xdeb90002\n", get_asm_ptr_keyword() ); /* magic */
558 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* relay func */
559 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
560 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
561 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
562 output( "\t%s .L__wine_spec_relay_args_string\n", get_asm_ptr_keyword() );
564 output_relay_debug( spec
);
566 else if (target_platform
!= PLATFORM_WINDOWS
)
568 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
569 output( ".L__wine_spec_exports_end:\n" );
570 output( "\t%s 0\n", get_asm_ptr_keyword() );
573 /* output import thunks */
575 if (!needs_imports
) return;
576 output( "\t.text\n" );
577 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
579 ORDDEF
*odp
= spec
->ordinals
[i
];
581 if (!(odp
->flags
& FLAG_IMPORT
)) continue;
583 name
= odp
->name
? odp
->name
: odp
->export_name
;
585 output( "\t.align %d\n", get_alignment(4) );
586 output( "\t.long 0x90909090,0x90909090\n" );
587 if (name
) output( "%s_%s:\n", asm_name("__wine_spec_imp"), name
);
588 else output( "%s_%u:\n", asm_name("__wine_spec_imp"), i
);
589 output_cfi( ".cfi_startproc" );
594 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
597 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
598 output( "1:\tjmp *__imp_%s-1b(%%eax)\n", asm_name( get_link_name( odp
)));
599 needs_get_pc_thunk
= 1;
601 else output( "\tjmp *__imp_%s\n", asm_name( get_link_name( odp
)));
604 output( "\t.byte 0x48,0x8d,0xa4,0x24,0x00,0x00,0x00,0x00\n" ); /* hotpatch prolog */
605 output( "\tjmp *__imp_%s(%%rip)\n", asm_name( get_link_name( odp
)));
610 output_cfi( ".cfi_endproc" );
615 /*******************************************************************
618 * Output the module data.
620 void output_module( DLLSPEC
*spec
)
624 unsigned int page_size
= get_page_size();
625 const char *data_dirs
[16] = { NULL
};
627 /* Reserve some space for the PE header */
629 switch (target_platform
)
631 case PLATFORM_WINDOWS
:
632 return; /* nothing to do */
634 output( "\t.text\n" );
635 output( "\t.align %d\n", get_alignment(page_size
) );
636 output( "__wine_spec_pe_header:\n" );
637 output( "\t.space 65536\n" );
639 case PLATFORM_SOLARIS
:
640 output( "\n\t.section \".text\",\"ax\"\n" );
641 output( "__wine_spec_pe_header:\n" );
642 output( "\t.skip %u\n", 65536 + page_size
);
649 output( "\n\t.section \".init\",\"ax\"\n" );
650 output( "\tjmp 1f\n" );
653 output( "\n\t.section \".text\",\"ax\"\n" );
654 output( "\tb 1f\n" );
658 output( "\n\t.section \".init\",\"ax\"\n" );
659 output( "\tb 1f\n" );
662 output( "__wine_spec_pe_header:\n" );
663 output( "\t.skip %u\n", 65536 + page_size
);
668 /* Output the NT header */
670 output( "\n\t.data\n" );
671 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
672 output( "\t.globl %s\n", asm_name("__wine_spec_nt_header") );
673 output( "%s:\n", asm_name("__wine_spec_nt_header") );
674 output( ".L__wine_spec_rva_base:\n" );
676 output( "\t.long 0x4550\n" ); /* Signature */
679 case CPU_x86
: machine
= IMAGE_FILE_MACHINE_I386
; break;
680 case CPU_x86_64
: machine
= IMAGE_FILE_MACHINE_AMD64
; break;
681 case CPU_POWERPC
: machine
= IMAGE_FILE_MACHINE_POWERPC
; break;
682 case CPU_ARM
: machine
= IMAGE_FILE_MACHINE_ARMNT
; break;
683 case CPU_ARM64
: machine
= IMAGE_FILE_MACHINE_ARM64
; break;
685 output( "\t.short 0x%04x\n", /* Machine */
687 output( "\t.short 0\n" ); /* NumberOfSections */
688 output( "\t.long %u\n", hash_filename(spec
->file_name
) ); /* TimeDateStamp */
689 output( "\t.long 0\n" ); /* PointerToSymbolTable */
690 output( "\t.long 0\n" ); /* NumberOfSymbols */
691 output( "\t.short %d\n", /* SizeOfOptionalHeader */
692 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
: IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
);
693 output( "\t.short 0x%04x\n", /* Characteristics */
694 spec
->characteristics
);
695 output( "\t.short 0x%04x\n", /* Magic */
696 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
: IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
697 output( "\t.byte 7\n" ); /* MajorLinkerVersion */
698 output( "\t.byte 10\n" ); /* MinorLinkerVersion */
699 output( "\t.long 0\n" ); /* SizeOfCode */
700 output( "\t.long 0\n" ); /* SizeOfInitializedData */
701 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
703 for (i
= 0; i
< spec_extra_ld_symbols
.count
; i
++)
704 output( "\t.globl %s\n", asm_name(spec_extra_ld_symbols
.str
[i
]) );
706 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
707 output( "\t%s %s\n", /* AddressOfEntryPoint */
708 get_asm_ptr_keyword(), spec
->init_func
? asm_name(spec
->init_func
) : "0" );
709 if (get_ptr_size() == 4)
711 output( "\t.long 0\n" ); /* BaseOfCode */
712 output( "\t.long 0\n" ); /* BaseOfData */
714 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
715 get_asm_ptr_keyword() );
716 output( "\t.long %u\n", page_size
); /* SectionAlignment */
717 output( "\t.long %u\n", page_size
); /* FileAlignment */
718 output( "\t.short 1,0\n" ); /* Major/MinorOperatingSystemVersion */
719 output( "\t.short 0,0\n" ); /* Major/MinorImageVersion */
720 output( "\t.short %u,%u\n", /* Major/MinorSubsystemVersion */
721 spec
->subsystem_major
, spec
->subsystem_minor
);
722 output( "\t.long 0\n" ); /* Win32VersionValue */
723 output_rva( "%s", asm_name("_end") ); /* SizeOfImage */
724 output( "\t.long %u\n", page_size
); /* SizeOfHeaders */
725 output( "\t.long 0\n" ); /* CheckSum */
726 output( "\t.short 0x%04x\n", /* Subsystem */
728 output( "\t.short 0x%04x\n", /* DllCharacteristics */
729 spec
->dll_characteristics
);
730 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
731 get_asm_ptr_keyword(), (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
732 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
733 get_asm_ptr_keyword(), (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
734 output( "\t.long 0\n" ); /* LoaderFlags */
735 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
737 if (get_exports_count( spec
))
738 data_dirs
[0] = ".L__wine_spec_exports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
740 data_dirs
[1] = ".L__wine_spec_imports"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
741 if (spec
->nb_resources
)
742 data_dirs
[2] = ".L__wine_spec_resources"; /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
744 output_data_directories( data_dirs
);
746 if (target_platform
== PLATFORM_APPLE
)
747 output( "\t.lcomm %s,4\n", asm_name("_end") );
751 /*******************************************************************
754 * Build a Win32 C file from a spec file.
756 void output_spec32_file( DLLSPEC
*spec
)
758 needs_get_pc_thunk
= 0;
760 output_standard_file_header();
761 output_module( spec
);
762 output_stubs( spec
);
763 output_exports( spec
);
764 output_imports( spec
);
765 output_syscalls( spec
);
766 if (needs_get_pc_thunk
) output_get_pc_thunk();
767 output_resources( spec
);
768 output_gnu_stack_note();
773 /*******************************************************************
776 * Build a fake binary module from a spec file.
778 void output_fake_module( DLLSPEC
*spec
)
780 static const unsigned char dll_code_section
[] = { 0x31, 0xc0, /* xor %eax,%eax */
781 0xc2, 0x0c, 0x00 }; /* ret $12 */
783 static const unsigned char exe_code_section
[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
784 0xc2, 0x04, 0x00 }; /* ret $4 */
786 const unsigned int page_size
= get_page_size();
787 const unsigned int section_align
= page_size
;
788 const unsigned int file_align
= 0x200;
789 const unsigned int reloc_size
= 8;
790 const unsigned int lfanew
= 0x40 + sizeof(fakedll_signature
);
791 const unsigned int nb_sections
= 2 + (spec
->nb_resources
!= 0);
792 const unsigned int text_size
= (spec
->characteristics
& IMAGE_FILE_DLL
) ?
793 sizeof(dll_code_section
) : sizeof(exe_code_section
);
794 unsigned char *resources
;
795 unsigned int resources_size
;
796 unsigned int image_size
= 3 * section_align
;
798 resolve_imports( spec
);
799 output_bin_resources( spec
, 3 * section_align
);
800 resources
= output_buffer
;
801 resources_size
= output_buffer_pos
;
802 if (resources_size
) image_size
+= (resources_size
+ section_align
- 1) & ~(section_align
- 1);
804 init_output_buffer();
806 put_word( 0x5a4d ); /* e_magic */
807 put_word( 0x40 ); /* e_cblp */
808 put_word( 0x01 ); /* e_cp */
809 put_word( 0 ); /* e_crlc */
810 put_word( lfanew
/ 16 ); /* e_cparhdr */
811 put_word( 0x0000 ); /* e_minalloc */
812 put_word( 0xffff ); /* e_maxalloc */
813 put_word( 0x0000 ); /* e_ss */
814 put_word( 0x00b8 ); /* e_sp */
815 put_word( 0 ); /* e_csum */
816 put_word( 0 ); /* e_ip */
817 put_word( 0 ); /* e_cs */
818 put_word( lfanew
); /* e_lfarlc */
819 put_word( 0 ); /* e_ovno */
820 put_dword( 0 ); /* e_res */
822 put_word( 0 ); /* e_oemid */
823 put_word( 0 ); /* e_oeminfo */
824 put_dword( 0 ); /* e_res2 */
831 put_data( fakedll_signature
, sizeof(fakedll_signature
) );
833 put_dword( 0x4550 ); /* Signature */
836 case CPU_x86
: put_word( IMAGE_FILE_MACHINE_I386
); break;
837 case CPU_x86_64
: put_word( IMAGE_FILE_MACHINE_AMD64
); break;
838 case CPU_POWERPC
: put_word( IMAGE_FILE_MACHINE_POWERPC
); break;
839 case CPU_ARM
: put_word( IMAGE_FILE_MACHINE_ARMNT
); break;
840 case CPU_ARM64
: put_word( IMAGE_FILE_MACHINE_ARM64
); break;
842 put_word( nb_sections
); /* NumberOfSections */
843 put_dword( hash_filename(spec
->file_name
) ); /* TimeDateStamp */
844 put_dword( 0 ); /* PointerToSymbolTable */
845 put_dword( 0 ); /* NumberOfSymbols */
846 put_word( get_ptr_size() == 8 ?
847 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
:
848 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
); /* SizeOfOptionalHeader */
849 put_word( spec
->characteristics
); /* Characteristics */
850 put_word( get_ptr_size() == 8 ?
851 IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
852 IMAGE_NT_OPTIONAL_HDR32_MAGIC
); /* Magic */
853 put_byte( 7 ); /* MajorLinkerVersion */
854 put_byte( 10 ); /* MinorLinkerVersion */
855 put_dword( text_size
); /* SizeOfCode */
856 put_dword( 0 ); /* SizeOfInitializedData */
857 put_dword( 0 ); /* SizeOfUninitializedData */
858 put_dword( section_align
); /* AddressOfEntryPoint */
859 put_dword( section_align
); /* BaseOfCode */
860 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
861 put_pword( 0x10000000 ); /* ImageBase */
862 put_dword( section_align
); /* SectionAlignment */
863 put_dword( file_align
); /* FileAlignment */
864 put_word( 1 ); /* MajorOperatingSystemVersion */
865 put_word( 0 ); /* MinorOperatingSystemVersion */
866 put_word( 0 ); /* MajorImageVersion */
867 put_word( 0 ); /* MinorImageVersion */
868 put_word( spec
->subsystem_major
); /* MajorSubsystemVersion */
869 put_word( spec
->subsystem_minor
); /* MinorSubsystemVersion */
870 put_dword( 0 ); /* Win32VersionValue */
871 put_dword( image_size
); /* SizeOfImage */
872 put_dword( file_align
); /* SizeOfHeaders */
873 put_dword( 0 ); /* CheckSum */
874 put_word( spec
->subsystem
); /* Subsystem */
875 put_word( spec
->dll_characteristics
); /* DllCharacteristics */
876 put_pword( (spec
->stack_size
? spec
->stack_size
: 1024) * 1024 ); /* SizeOfStackReserve */
877 put_pword( page_size
); /* SizeOfStackCommit */
878 put_pword( (spec
->heap_size
? spec
->heap_size
: 1024) * 1024 ); /* SizeOfHeapReserve */
879 put_pword( page_size
); /* SizeOfHeapCommit */
880 put_dword( 0 ); /* LoaderFlags */
881 put_dword( 16 ); /* NumberOfRvaAndSizes */
883 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
884 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
885 if (resources_size
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
887 put_dword( 3 * section_align
);
888 put_dword( resources_size
);
896 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
897 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
898 put_dword( 2 * section_align
); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
899 put_dword( reloc_size
);
900 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
901 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
902 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
903 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
904 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
905 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
906 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
907 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
908 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
909 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
912 put_data( ".text\0\0", 8 ); /* Name */
913 put_dword( section_align
); /* VirtualSize */
914 put_dword( section_align
); /* VirtualAddress */
915 put_dword( text_size
); /* SizeOfRawData */
916 put_dword( file_align
); /* PointerToRawData */
917 put_dword( 0 ); /* PointerToRelocations */
918 put_dword( 0 ); /* PointerToLinenumbers */
919 put_word( 0 ); /* NumberOfRelocations */
920 put_word( 0 ); /* NumberOfLinenumbers */
921 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
924 put_data( ".reloc\0", 8 ); /* Name */
925 put_dword( section_align
); /* VirtualSize */
926 put_dword( 2 * section_align
);/* VirtualAddress */
927 put_dword( reloc_size
); /* SizeOfRawData */
928 put_dword( 2 * file_align
); /* PointerToRawData */
929 put_dword( 0 ); /* PointerToRelocations */
930 put_dword( 0 ); /* PointerToLinenumbers */
931 put_word( 0 ); /* NumberOfRelocations */
932 put_word( 0 ); /* NumberOfLinenumbers */
933 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
938 put_data( ".rsrc\0\0", 8 ); /* Name */
939 put_dword( (resources_size
+ section_align
- 1) & ~(section_align
- 1) ); /* VirtualSize */
940 put_dword( 3 * section_align
);/* VirtualAddress */
941 put_dword( resources_size
); /* SizeOfRawData */
942 put_dword( 3 * file_align
); /* PointerToRawData */
943 put_dword( 0 ); /* PointerToRelocations */
944 put_dword( 0 ); /* PointerToLinenumbers */
945 put_word( 0 ); /* NumberOfRelocations */
946 put_word( 0 ); /* NumberOfLinenumbers */
947 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
951 align_output( file_align
);
952 if (spec
->characteristics
& IMAGE_FILE_DLL
)
953 put_data( dll_code_section
, sizeof(dll_code_section
) );
955 put_data( exe_code_section
, sizeof(exe_code_section
) );
957 /* .reloc contents */
958 align_output( file_align
);
959 put_dword( 0 ); /* VirtualAddress */
960 put_dword( 0 ); /* SizeOfBlock */
965 align_output( file_align
);
966 put_data( resources
, resources_size
);
968 flush_output_buffer();
972 /*******************************************************************
975 * Build a Win32 def file from a spec file.
977 void output_def_file( DLLSPEC
*spec
, int import_only
)
979 DLLSPEC
*spec32
= NULL
;
983 if (spec
->type
== SPEC_WIN16
)
985 spec32
= alloc_dll_spec();
986 add_16bit_exports( spec32
, spec
);
991 output( "; File generated automatically from %s; do not edit!\n\n",
994 output( "; File generated automatically; do not edit!\n\n" );
996 output( "LIBRARY %s\n\n", spec
->file_name
);
997 output( "EXPORTS\n");
999 /* Output the exports and relay entry points */
1001 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
1003 const ORDDEF
*odp
= &spec
->entry_points
[i
];
1004 int is_data
= 0, is_private
= odp
->flags
& FLAG_PRIVATE
;
1006 if (odp
->name
) name
= odp
->name
;
1007 else if (odp
->export_name
) name
= odp
->export_name
;
1010 if (!is_private
) total
++;
1011 if (import_only
&& odp
->type
== TYPE_STUB
) continue;
1013 if ((odp
->flags
& FLAG_FASTCALL
) && target_platform
== PLATFORM_WINDOWS
)
1014 name
= strmake( "@%s", name
);
1016 output( " %s", name
);
1025 /* try to reduce output */
1026 if(!import_only
&& (strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
)))
1027 output( "=%s", odp
->link_name
);
1031 int at_param
= get_args_size( odp
);
1032 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
1033 if (import_only
) break;
1034 if (odp
->flags
& FLAG_FORWARD
)
1035 output( "=%s", odp
->link_name
);
1036 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
1037 output( "=%s", get_link_name( odp
));
1041 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", get_args_size( odp
));
1047 output( " @%d", odp
->ordinal
);
1048 if (!odp
->name
|| (odp
->flags
& FLAG_ORDINAL
)) output( " NONAME" );
1049 if (is_data
) output( " DATA" );
1050 if (is_private
) output( " PRIVATE" );
1053 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);
1054 if (spec32
) free_dll_spec( spec32
);
1058 /*******************************************************************
1059 * make_builtin_files
1061 void make_builtin_files( char *argv
[] )
1066 unsigned short e_magic
;
1067 unsigned short unused
[29];
1068 unsigned int e_lfanew
;
1071 for (i
= 0; argv
[i
]; i
++)
1073 if ((fd
= open( argv
[i
], O_RDWR
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", argv
[i
] );
1074 if (read( fd
, &header
, sizeof(header
) ) == sizeof(header
) && !memcmp( &header
.e_magic
, "MZ", 2 ))
1076 if (header
.e_lfanew
< sizeof(header
) + sizeof(builtin_signature
))
1077 fatal_error( "%s: Not enough space (%x) for Wine signature\n", argv
[i
], header
.e_lfanew
);
1078 write( fd
, builtin_signature
, sizeof(builtin_signature
) );
1080 else fatal_error( "%s: Unrecognized file format\n", argv
[i
] );
1085 static void fixup_elf32( const char *name
, int fd
, void *header
, size_t header_size
)
1089 unsigned char e_ident
[16];
1090 unsigned short e_type
;
1091 unsigned short e_machine
;
1092 unsigned int e_version
;
1093 unsigned int e_entry
;
1094 unsigned int e_phoff
;
1095 unsigned int e_shoff
;
1096 unsigned int e_flags
;
1097 unsigned short e_ehsize
;
1098 unsigned short e_phentsize
;
1099 unsigned short e_phnum
;
1100 unsigned short e_shentsize
;
1101 unsigned short e_shnum
;
1102 unsigned short e_shstrndx
;
1106 unsigned int p_type
;
1107 unsigned int p_offset
;
1108 unsigned int p_vaddr
;
1109 unsigned int p_paddr
;
1110 unsigned int p_filesz
;
1111 unsigned int p_memsz
;
1112 unsigned int p_flags
;
1113 unsigned int p_align
;
1121 unsigned int i
, size
;
1123 if (header_size
< sizeof(*elf
)) return;
1124 if (elf
->e_ident
[6] != 1 /* EV_CURRENT */) return;
1126 size
= elf
->e_phnum
* elf
->e_phentsize
;
1127 phdr
= xmalloc( size
);
1128 lseek( fd
, elf
->e_phoff
, SEEK_SET
);
1129 if (read( fd
, phdr
, size
) != size
) return;
1131 for (i
= 0; i
< elf
->e_phnum
; i
++)
1133 if (phdr
->p_type
== 2 /* PT_DYNAMIC */ ) break;
1134 phdr
= (void *)((char *)phdr
+ elf
->e_phentsize
);
1136 if (i
== elf
->e_phnum
) return;
1138 dyn
= xmalloc( phdr
->p_filesz
);
1139 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1140 if (read( fd
, dyn
, phdr
->p_filesz
) != phdr
->p_filesz
) return;
1141 for (i
= 0; i
< phdr
->p_filesz
/ sizeof(*dyn
) && dyn
[i
].d_tag
; i
++)
1143 switch (dyn
[i
].d_tag
)
1145 case 25: dyn
[i
].d_tag
= 0x60009990; break; /* DT_INIT_ARRAY */
1146 case 27: dyn
[i
].d_tag
= 0x60009991; break; /* DT_INIT_ARRAYSZ */
1147 case 12: dyn
[i
].d_tag
= 0x60009992; break; /* DT_INIT */
1150 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1151 write( fd
, dyn
, phdr
->p_filesz
);
1154 static void fixup_elf64( const char *name
, int fd
, void *header
, size_t header_size
)
1158 unsigned char e_ident
[16];
1159 unsigned short e_type
;
1160 unsigned short e_machine
;
1161 unsigned int e_version
;
1162 unsigned __int64 e_entry
;
1163 unsigned __int64 e_phoff
;
1164 unsigned __int64 e_shoff
;
1165 unsigned int e_flags
;
1166 unsigned short e_ehsize
;
1167 unsigned short e_phentsize
;
1168 unsigned short e_phnum
;
1169 unsigned short e_shentsize
;
1170 unsigned short e_shnum
;
1171 unsigned short e_shstrndx
;
1175 unsigned int p_type
;
1176 unsigned int p_flags
;
1177 unsigned __int64 p_offset
;
1178 unsigned __int64 p_vaddr
;
1179 unsigned __int64 p_paddr
;
1180 unsigned __int64 p_filesz
;
1181 unsigned __int64 p_memsz
;
1182 unsigned __int64 p_align
;
1186 unsigned __int64 d_tag
;
1187 unsigned __int64 d_val
;
1190 unsigned int i
, size
;
1192 if (header_size
< sizeof(*elf
)) return;
1193 if (elf
->e_ident
[6] != 1 /* EV_CURRENT */) return;
1195 size
= elf
->e_phnum
* elf
->e_phentsize
;
1196 phdr
= xmalloc( size
);
1197 lseek( fd
, elf
->e_phoff
, SEEK_SET
);
1198 if (read( fd
, phdr
, size
) != size
) return;
1200 for (i
= 0; i
< elf
->e_phnum
; i
++)
1202 if (phdr
->p_type
== 2 /* PT_DYNAMIC */ ) break;
1203 phdr
= (void *)((char *)phdr
+ elf
->e_phentsize
);
1205 if (i
== elf
->e_phnum
) return;
1207 dyn
= xmalloc( phdr
->p_filesz
);
1208 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1209 if (read( fd
, dyn
, phdr
->p_filesz
) != phdr
->p_filesz
) return;
1210 for (i
= 0; i
< phdr
->p_filesz
/ sizeof(*dyn
) && dyn
[i
].d_tag
; i
++)
1212 switch (dyn
[i
].d_tag
)
1214 case 25: dyn
[i
].d_tag
= 0x60009990; break; /* DT_INIT_ARRAY */
1215 case 27: dyn
[i
].d_tag
= 0x60009991; break; /* DT_INIT_ARRAYSZ */
1216 case 12: dyn
[i
].d_tag
= 0x60009992; break; /* DT_INIT */
1219 lseek( fd
, phdr
->p_offset
, SEEK_SET
);
1220 write( fd
, dyn
, phdr
->p_filesz
);
1223 /*******************************************************************
1224 * fixup_constructors
1226 void fixup_constructors( char *argv
[] )
1229 unsigned int header
[64];
1231 for (i
= 0; argv
[i
]; i
++)
1233 if ((fd
= open( argv
[i
], O_RDWR
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", argv
[i
] );
1234 size
= read( fd
, &header
, sizeof(header
) );
1237 if (!memcmp( header
, "\177ELF\001", 5 )) fixup_elf32( argv
[i
], fd
, header
, size
);
1238 else if (!memcmp( header
, "\177ELF\002", 5 )) fixup_elf64( argv
[i
], fd
, header
, size
);