winebuild: Support non-PIC mode for ARM targets.
[wine.git] / tools / winebuild / spec32.c
blobcf1dd353ae54b2af1b343f9fc504c0c268e4c691
1 /*
2 * 32-bit spec files
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
33 #include "build.h"
35 #define IMAGE_FILE_MACHINE_UNKNOWN 0
36 #define IMAGE_FILE_MACHINE_I386 0x014c
37 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
38 #define IMAGE_FILE_MACHINE_AMD64 0x8664
39 #define IMAGE_FILE_MACHINE_ARMNT 0x01C4
40 #define IMAGE_FILE_MACHINE_ARM64 0xaa64
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
49 int needs_get_pc_thunk = 0;
51 static const char builtin_signature[32] = "Wine builtin DLL";
52 static const char fakedll_signature[32] = "Wine placeholder DLL";
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 )
63 /* FNV-1 hash */
64 unsigned int ret = 2166136261u;
65 while (*name) ret = (ret * 16777619) ^ *name++;
66 return ret;
69 /* check if entry point needs a relay thunk */
70 static inline int needs_relay( const ORDDEF *odp )
72 /* skip nonexistent entry points */
73 if (!odp) return 0;
74 /* skip non-functions */
75 switch (odp->type)
77 case TYPE_STDCALL:
78 case TYPE_CDECL:
79 break;
80 case TYPE_STUB:
81 if (odp->u.func.nb_args != -1) break;
82 /* fall through */
83 default:
84 return 0;
86 /* skip norelay and forward entry points */
87 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
88 return 1;
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 )
100 int i;
102 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64 &&
103 target_cpu != CPU_ARM && target_cpu != CPU_ARM64)
104 return 0;
106 for (i = spec->base; i <= spec->limit; i++)
108 ORDDEF *odp = spec->ordinals[i];
109 if (needs_relay( odp )) return 1;
111 return 0;
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] )
131 int i;
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;
141 case ARG_INT64:
142 case ARG_INT128:
143 if (get_ptr_size() == 4)
145 str[i] = (odp->u.func.args[i] == ARG_INT64) ? 'j' : 'k';
146 break;
148 /* fall through */
149 case ARG_LONG:
150 case ARG_PTR:
151 default:
152 str[i] = 'i';
153 break;
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" );
162 else
163 strcpy( str + i, "I" );
166 static void output_data_directories( const char *names[16] )
168 int i;
170 for (i = 0; i < 16; i++)
172 if (names[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 /*******************************************************************
182 * build_args_string
184 static char *build_args_string( DLLSPEC *spec )
186 int i, count = 0, len = 1;
187 char *p, *buffer;
188 char str[MAX_ARGUMENTS + 2];
189 ORDDEF **funcs;
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 );
203 buffer[0] = 0;
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 );
211 strcpy( p, str );
213 funcs[i]->u.func.args_str_offset = p - buffer;
215 free( funcs );
216 return buffer;
219 /*******************************************************************
220 * output_relay_debug
222 * Output entry points for relay debugging
224 static void output_relay_debug( DLLSPEC *spec )
226 int i;
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 );
240 else
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;
262 switch (target_cpu)
264 case CPU_x86:
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" );
280 if (UsePIC)
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 ));
294 else
295 output( "\tret\n" );
296 output_cfi( ".cfi_endproc" );
297 break;
299 case CPU_ARM:
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" );
331 break;
334 case CPU_ARM64:
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)
340 default:
341 case 8:
342 case 7: output( "\tstp x6, x7, [SP,#-16]!\n" );
343 /* fall through */
344 case 6:
345 case 5: output( "\tstp x4, x5, [SP,#-16]!\n" );
346 /* fall through */
347 case 4:
348 case 3: output( "\tstp x2, x3, [SP,#-16]!\n" );
349 /* fall through */
350 case 2:
351 case 1: output( "\tstp x0, x1, [SP,#-16]!\n" );
352 /* fall through */
353 case 0: break;
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) );
368 output( "\tret\n");
369 output_cfi( ".cfi_endproc" );
370 break;
372 case CPU_x86_64:
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" );
380 /* fall through */
381 case 3: output( "\tmovq %%%s,24(%%rsp)\n", is_float_arg( odp, 2 ) ? "xmm2" : "r8" );
382 /* fall through */
383 case 2: output( "\tmovq %%%s,16(%%rsp)\n", is_float_arg( odp, 1 ) ? "xmm1" : "rdx" );
384 /* fall through */
385 case 1: output( "\tmovq %%%s,8(%%rsp)\n", is_float_arg( odp, 0 ) ? "xmm0" : "rcx" );
386 /* fall through */
387 case 0: break;
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" );
392 output( "\tret\n" );
393 output_cfi( ".cfi_endproc" );
394 break;
396 default:
397 assert(0);
402 /*******************************************************************
403 * output_exports
405 * Output the export table for a Win32 module.
407 void output_exports( DLLSPEC *spec )
409 int i, fwd_size = 0;
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();
414 const char *name;
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 */
433 if (spec->nb_names)
435 output_rva( ".L__wine_spec_exp_name_ptrs" ); /* AddressOfNames */
436 output_rva( ".L__wine_spec_exp_ordinals" ); /* AddressOfNameOrdinals */
438 else
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)
454 case TYPE_EXTERN:
455 case TYPE_STDCALL:
456 case TYPE_VARARGS:
457 case TYPE_CDECL:
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 );
468 needs_imports = 1;
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 );
474 else
476 output( "\t%s %s\n", func_ptr, asm_name( get_link_name( odp )));
478 break;
479 case TYPE_STUB:
480 output( "\t%s %s\n", func_ptr, asm_name( get_stub_name( odp, spec )) );
481 break;
482 default:
483 assert(0);
487 if (spec->nb_names)
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" );
513 if (needs_relay)
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 */
530 if (fwd_size)
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 );
541 /* output relays */
543 if (needs_relay)
545 if (target_platform == PLATFORM_WINDOWS)
547 output( "\t.data\n" );
548 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
550 else
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];
580 if (!odp) continue;
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" );
591 switch (target_cpu)
593 case CPU_x86:
594 output( "\t.byte 0x8b,0xff,0x55,0x8b,0xec,0x5d\n" ); /* hotpatch prolog */
595 if (UsePIC)
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 )));
602 break;
603 case CPU_x86_64:
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 )));
606 break;
607 default:
608 assert(0);
610 output_cfi( ".cfi_endproc" );
615 /*******************************************************************
616 * output_module
618 * Output the module data.
620 void output_module( DLLSPEC *spec )
622 int machine = 0;
623 int i;
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 */
633 case PLATFORM_APPLE:
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" );
638 break;
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 );
643 break;
644 default:
645 switch(target_cpu)
647 case CPU_x86:
648 case CPU_x86_64:
649 output( "\n\t.section \".init\",\"ax\"\n" );
650 output( "\tjmp 1f\n" );
651 break;
652 case CPU_ARM:
653 output( "\n\t.section \".text\",\"ax\"\n" );
654 output( "\tb 1f\n" );
655 break;
656 case CPU_ARM64:
657 case CPU_POWERPC:
658 output( "\n\t.section \".init\",\"ax\"\n" );
659 output( "\tb 1f\n" );
660 break;
662 output( "__wine_spec_pe_header:\n" );
663 output( "\t.skip %u\n", 65536 + page_size );
664 output( "1:\n" );
665 break;
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 */
677 switch(target_cpu)
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 */
686 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 */
727 spec->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] */
739 if (has_imports())
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 /*******************************************************************
752 * output_spec32_file
754 * Build a Win32 C file from a spec file.
756 void output_spec32_file( DLLSPEC *spec )
758 needs_get_pc_thunk = 0;
759 open_output_file();
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();
769 close_output_file();
773 /*******************************************************************
774 * output_fake_module
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 */
821 put_dword( 0 );
822 put_word( 0 ); /* e_oemid */
823 put_word( 0 ); /* e_oeminfo */
824 put_dword( 0 ); /* e_res2 */
825 put_dword( 0 );
826 put_dword( 0 );
827 put_dword( 0 );
828 put_dword( 0 );
829 put_dword( lfanew );
831 put_data( fakedll_signature, sizeof(fakedll_signature) );
833 put_dword( 0x4550 ); /* Signature */
834 switch(target_cpu)
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 );
890 else
892 put_dword( 0 );
893 put_dword( 0 );
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] */
911 /* .text section */
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 */
923 /* .reloc section */
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 */
935 /* .rsrc section */
936 if (resources_size)
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 */
950 /* .text contents */
951 align_output( file_align );
952 if (spec->characteristics & IMAGE_FILE_DLL)
953 put_data( dll_code_section, sizeof(dll_code_section) );
954 else
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 */
962 /* .rsrc contents */
963 if (resources_size)
965 align_output( file_align );
966 put_data( resources, resources_size );
968 flush_output_buffer();
972 /*******************************************************************
973 * output_def_file
975 * Build a Win32 def file from a spec file.
977 void output_def_file( DLLSPEC *spec, int import_only )
979 DLLSPEC *spec32 = NULL;
980 const char *name;
981 int i, total;
983 if (spec->type == SPEC_WIN16)
985 spec32 = alloc_dll_spec();
986 add_16bit_exports( spec32, spec );
987 spec = spec32;
990 if (spec_file_name)
991 output( "; File generated automatically from %s; do not edit!\n\n",
992 spec_file_name );
993 else
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;
1008 else continue;
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 );
1018 switch(odp->type)
1020 case TYPE_EXTERN:
1021 is_data = 1;
1022 /* fall through */
1023 case TYPE_VARARGS:
1024 case TYPE_CDECL:
1025 /* try to reduce output */
1026 if(!import_only && (strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD)))
1027 output( "=%s", odp->link_name );
1028 break;
1029 case TYPE_STDCALL:
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 ));
1038 break;
1040 case TYPE_STUB:
1041 if (!kill_at && target_cpu == CPU_x86) output( "@%d", get_args_size( odp ));
1042 is_private = 1;
1043 break;
1044 default:
1045 assert(0);
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" );
1051 output( "\n" );
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[] )
1063 int i, fd;
1064 struct
1066 unsigned short e_magic;
1067 unsigned short unused[29];
1068 unsigned int e_lfanew;
1069 } header;
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] );
1081 close( fd );
1085 static void fixup_elf32( const char *name, int fd, void *header, size_t header_size )
1087 struct
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;
1103 } *elf = header;
1104 struct
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;
1114 } *phdr;
1115 struct
1117 unsigned int d_tag;
1118 unsigned int d_val;
1119 } *dyn;
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 )
1156 struct
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;
1172 } *elf = header;
1173 struct
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;
1183 } *phdr;
1184 struct
1186 unsigned __int64 d_tag;
1187 unsigned __int64 d_val;
1188 } *dyn;
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[] )
1228 int i, fd, size;
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) );
1235 if (size > 5)
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 );
1240 close( fd );