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 #include "wine/exception.h"
39 /* check if entry point needs a relay thunk */
40 static inline int needs_relay( const ORDDEF
*odp
)
42 /* skip nonexistent entry points */
44 /* skip non-functions */
45 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) return 0;
46 /* skip norelay and forward entry points */
47 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) return 0;
51 /* check if dll will output relay thunks */
52 int has_relays( DLLSPEC
*spec
)
56 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
) return 0;
58 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
60 ORDDEF
*odp
= spec
->ordinals
[i
];
61 if (needs_relay( odp
)) return 1;
66 /*******************************************************************
69 * Output entry points for relay debugging
71 static void output_relay_debug( DLLSPEC
*spec
)
74 unsigned int j
, args
, flags
;
76 /* first the table of entry point offsets */
78 output( "\t%s\n", get_asm_rodata_section() );
79 output( "\t.align %d\n", get_alignment(4) );
80 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
82 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
84 ORDDEF
*odp
= spec
->ordinals
[i
];
86 if (needs_relay( odp
))
87 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i
);
89 output( "\t.long 0\n" );
92 /* then the table of argument types */
94 output( "\t.align %d\n", get_alignment(4) );
95 output( ".L__wine_spec_relay_arg_types:\n" );
97 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
99 ORDDEF
*odp
= spec
->ordinals
[i
];
100 unsigned int mask
= 0;
102 if (needs_relay( odp
))
104 for (j
= 0; j
< 16 && odp
->u
.func
.arg_types
[j
]; j
++)
106 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
107 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
110 output( "\t.long 0x%08x\n", mask
);
113 /* then the relay thunks */
115 output( "\t.text\n" );
116 output( "__wine_spec_relay_entry_points:\n" );
117 output( "\tnop\n" ); /* to avoid 0 offset */
119 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
121 ORDDEF
*odp
= spec
->ordinals
[i
];
123 if (!needs_relay( odp
)) continue;
125 output( "\t.align %d\n", get_alignment(4) );
126 output( ".L__wine_spec_relay_entry_point_%d:\n", i
);
128 args
= strlen(odp
->u
.func
.arg_types
);
134 if (odp
->flags
& FLAG_REGISTER
)
135 output( "\tpushl %%eax\n" );
137 output( "\tpushl %%esp\n" );
139 if (odp
->flags
& FLAG_RET64
) flags
|= 1;
140 if (odp
->type
== TYPE_STDCALL
) flags
|= 2;
141 output( "\tpushl $%u\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
145 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
146 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
148 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
149 output( "\tpushl %%eax\n" );
151 if (odp
->flags
& FLAG_REGISTER
)
153 output( "\tcall *8(%%eax)\n" );
157 output( "\tcall *4(%%eax)\n" );
158 if (odp
->type
== TYPE_STDCALL
)
159 output( "\tret $%u\n", args
* get_ptr_size() );
166 output( "\tmovq %%rcx,8(%%rsp)\n" );
167 output( "\tmovq %%rdx,16(%%rsp)\n" );
168 output( "\tmovq %%r8,24(%%rsp)\n" );
169 output( "\tmovq %%r9,32(%%rsp)\n" );
170 output( "\tmovq %%rsp,%%r8\n" );
171 output( "\tmovq $%u,%%rdx\n", (flags
<< 24) | (args
<< 16) | (i
- spec
->base
) );
172 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
173 output( "\tsubq $40,%%rsp\n" );
174 output( "\tcallq *8(%%rcx)\n" );
175 output( "\taddq $40,%%rsp\n" );
185 /*******************************************************************
188 * Output the export table for a Win32 module.
190 static void output_exports( DLLSPEC
*spec
)
193 int nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
195 if (!nr_exports
) return;
197 output( "\n/* export table */\n\n" );
198 output( "\t.data\n" );
199 output( "\t.align %d\n", get_alignment(4) );
200 output( ".L__wine_spec_exports:\n" );
202 /* export directory header */
204 output( "\t.long 0\n" ); /* Characteristics */
205 output( "\t.long 0\n" ); /* TimeDateStamp */
206 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
207 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
208 output( "\t.long %u\n", spec
->base
); /* Base */
209 output( "\t.long %u\n", nr_exports
); /* NumberOfFunctions */
210 output( "\t.long %u\n", spec
->nb_names
); /* NumberOfNames */
211 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
214 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
215 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
219 output( "\t.long 0\n" ); /* AddressOfNames */
220 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
223 /* output the function pointers */
225 output( "\n.L__wine_spec_exports_funcs:\n" );
226 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
228 ORDDEF
*odp
= spec
->ordinals
[i
];
229 if (!odp
) output( "\t%s 0\n", get_asm_ptr_keyword() );
230 else switch(odp
->type
)
236 if (odp
->flags
& FLAG_FORWARD
)
238 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size
);
239 fwd_size
+= strlen(odp
->link_name
) + 1;
241 else if (odp
->flags
& FLAG_EXT_LINK
)
243 output( "\t%s %s_%s\n",
244 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp
->link_name
);
248 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp
->link_name
) );
252 output( "\t%s %s\n", get_asm_ptr_keyword(),
253 asm_name( get_stub_name( odp
, spec
)) );
262 /* output the function name pointers */
264 int namepos
= strlen(spec
->file_name
) + 1;
266 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
267 for (i
= 0; i
< spec
->nb_names
; i
++)
269 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos
);
270 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
273 /* output the function ordinals */
275 output( "\n.L__wine_spec_exp_ordinals:\n" );
276 for (i
= 0; i
< spec
->nb_names
; i
++)
279 get_asm_short_keyword(), spec
->names
[i
]->ordinal
- spec
->base
);
281 if (spec
->nb_names
% 2)
283 output( "\t%s 0\n", get_asm_short_keyword() );
287 /* output the export name strings */
289 output( "\n.L__wine_spec_exp_names:\n" );
290 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
291 for (i
= 0; i
< spec
->nb_names
; i
++)
292 output( "\t%s \"%s\"\n",
293 get_asm_string_keyword(), spec
->names
[i
]->name
);
295 /* output forward strings */
299 output( "\n.L__wine_spec_forwards:\n" );
300 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
302 ORDDEF
*odp
= spec
->ordinals
[i
];
303 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
304 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->link_name
);
307 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
308 output( ".L__wine_spec_exports_end:\n" );
312 /* we only support relay debugging on i386 and x86_64 */
313 if (target_cpu
!= CPU_x86
&& target_cpu
!= CPU_x86_64
)
315 output( "\t%s 0\n", get_asm_ptr_keyword() );
319 output( ".L__wine_spec_relay_descr:\n" );
320 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
321 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
322 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
323 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
324 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
325 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
327 output_relay_debug( spec
);
331 /*******************************************************************
332 * output_asm_constructor
334 * Output code for calling a dll constructor.
336 static void output_asm_constructor( const char *constructor
)
338 if (target_platform
== PLATFORM_APPLE
)
340 /* Mach-O doesn't have an init section */
341 output( "\n\t.mod_init_func\n" );
342 output( "\t.align %d\n", get_alignment(4) );
343 output( "\t.long %s\n", asm_name(constructor
) );
347 output( "\n\t.section \".init\",\"ax\"\n" );
352 output( "\tcall %s\n", asm_name(constructor
) );
355 output( "\tcall %s\n", asm_name(constructor
) );
359 output( "\tjsr $26,%s\n", asm_name(constructor
) );
362 output( "\tbl %s\n", asm_name(constructor
) );
369 /*******************************************************************
372 * Build a Win32 C file from a spec file.
374 void BuildSpec32File( DLLSPEC
*spec
)
377 unsigned int page_size
= get_page_size();
379 resolve_imports( spec
);
380 output_standard_file_header();
382 /* Reserve some space for the PE header */
384 switch (target_platform
)
387 output( "\t.text\n" );
388 output( "\t.align %d\n", get_alignment(page_size
) );
389 output( "__wine_spec_pe_header:\n" );
390 output( "\t.space 65536\n" );
392 case PLATFORM_SOLARIS
:
393 output( "\n\t.section \".text\",\"ax\"\n" );
394 output( "__wine_spec_pe_header:\n" );
395 output( "\t.skip %u\n", 65536 + page_size
);
398 output( "\n\t.section \".init\",\"ax\"\n" );
405 output( "\tjmp 1f\n" );
408 output( "\tb 1f\n" );
411 output( "__wine_spec_pe_header:\n" );
412 output( "\t.skip %u\n", 65536 + page_size
);
417 /* Output the NT header */
419 output( "\n\t.data\n" );
420 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
421 output( "%s\n", asm_globl("__wine_spec_nt_header") );
422 output( ".L__wine_spec_rva_base:\n" );
424 output( "\t.long 0x%04x\n", IMAGE_NT_SIGNATURE
); /* Signature */
427 case CPU_x86
: machine
= IMAGE_FILE_MACHINE_I386
; break;
428 case CPU_x86_64
: machine
= IMAGE_FILE_MACHINE_AMD64
; break;
429 case CPU_POWERPC
: machine
= IMAGE_FILE_MACHINE_POWERPC
; break;
430 case CPU_ALPHA
: machine
= IMAGE_FILE_MACHINE_ALPHA
; break;
431 case CPU_SPARC
: machine
= IMAGE_FILE_MACHINE_UNKNOWN
; break;
433 output( "\t%s 0x%04x\n", /* Machine */
434 get_asm_short_keyword(), machine
);
435 output( "\t%s 0\n", /* NumberOfSections */
436 get_asm_short_keyword() );
437 output( "\t.long 0\n" ); /* TimeDateStamp */
438 output( "\t.long 0\n" ); /* PointerToSymbolTable */
439 output( "\t.long 0\n" ); /* NumberOfSymbols */
440 output( "\t%s %d\n", /* SizeOfOptionalHeader */
441 get_asm_short_keyword(),
442 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER
: IMAGE_SIZEOF_NT_OPTIONAL32_HEADER
);
443 output( "\t%s 0x%04x\n", /* Characteristics */
444 get_asm_short_keyword(), spec
->characteristics
);
445 output( "\t%s 0x%04x\n", /* Magic */
446 get_asm_short_keyword(),
447 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
: IMAGE_NT_OPTIONAL_HDR32_MAGIC
);
448 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
449 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
450 output( "\t.long 0\n" ); /* SizeOfCode */
451 output( "\t.long 0\n" ); /* SizeOfInitializedData */
452 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
453 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
454 output( "\t%s %s\n", /* AddressOfEntryPoint */
455 get_asm_ptr_keyword(), asm_name(spec
->init_func
) );
456 if (get_ptr_size() == 4)
458 output( "\t.long 0\n" ); /* BaseOfCode */
459 output( "\t.long 0\n" ); /* BaseOfData */
461 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
462 get_asm_ptr_keyword() );
463 output( "\t.long %u\n", page_size
); /* SectionAlignment */
464 output( "\t.long %u\n", page_size
); /* FileAlignment */
465 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
466 get_asm_short_keyword() );
467 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
468 get_asm_short_keyword() );
469 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
470 get_asm_short_keyword(), spec
->subsystem_major
, spec
->subsystem_minor
);
471 output( "\t.long 0\n" ); /* Win32VersionValue */
472 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
474 output( "\t.long %u\n", page_size
); /* SizeOfHeaders */
475 output( "\t.long 0\n" ); /* CheckSum */
476 output( "\t%s 0x%04x\n", /* Subsystem */
477 get_asm_short_keyword(), spec
->subsystem
);
478 output( "\t%s 0x%04x\n", /* DllCharacteristics */
479 get_asm_short_keyword(), spec
->dll_characteristics
);
480 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
481 get_asm_ptr_keyword(), (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
482 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
483 get_asm_ptr_keyword(), (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
484 output( "\t.long 0\n" ); /* LoaderFlags */
485 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
487 if (spec
->base
<= spec
->limit
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
488 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
489 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
491 output( "\t.long 0,0\n" );
493 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
494 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
495 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
497 output( "\t.long 0,0\n" );
499 if (spec
->nb_resources
) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
500 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
501 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
503 output( "\t.long 0,0\n" );
505 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
506 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
507 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
508 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
509 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
510 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
511 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
512 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
513 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
514 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
515 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
516 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
517 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
519 output( "\n\t%s\n", get_asm_string_section() );
520 output( "%s\n", asm_globl("__wine_spec_file_name") );
521 output( ".L__wine_spec_file_name:\n" );
522 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec
->file_name
);
523 if (target_platform
== PLATFORM_APPLE
)
524 output( "\t.lcomm %s,4\n", asm_name("_end") );
526 output_stubs( spec
);
527 output_exports( spec
);
528 output_imports( spec
);
529 output_resources( spec
);
530 output_asm_constructor( "__wine_spec_init_ctor" );
531 output_gnu_stack_note();
535 /*******************************************************************
538 * Build a Win32 def file from a spec file.
540 void BuildDef32File( DLLSPEC
*spec
)
546 output( "; File generated automatically from %s; do not edit!\n\n",
549 output( "; File generated automatically; do not edit!\n\n" );
551 output( "LIBRARY %s\n\n", spec
->file_name
);
552 output( "EXPORTS\n");
554 /* Output the exports and relay entry points */
556 for (i
= total
= 0; i
< spec
->nb_entry_points
; i
++)
558 const ORDDEF
*odp
= &spec
->entry_points
[i
];
563 if (odp
->name
) name
= odp
->name
;
564 else if (odp
->export_name
) name
= odp
->export_name
;
567 if (!(odp
->flags
& FLAG_PRIVATE
)) total
++;
569 if (odp
->type
== TYPE_STUB
) continue;
571 output( " %s", name
);
580 /* try to reduce output */
581 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
582 output( "=%s", odp
->link_name
);
586 int at_param
= strlen(odp
->u
.func
.arg_types
) * get_ptr_size();
587 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
588 if (odp
->flags
& FLAG_FORWARD
)
590 output( "=%s", odp
->link_name
);
592 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
594 output( "=%s", odp
->link_name
);
595 if (!kill_at
&& target_cpu
== CPU_x86
) output( "@%d", at_param
);
602 output( " @%d", odp
->ordinal
);
603 if (!odp
->name
|| (odp
->flags
& FLAG_ORDINAL
)) output( " NONAME" );
604 if (is_data
) output( " DATA" );
605 if (odp
->flags
& FLAG_PRIVATE
) output( " PRIVATE" );
608 if (!total
) warning( "%s: Import library doesn't export anything\n", spec
->file_name
);