- Don't prepend fastcall forward's target with @. Fixes binding issues in videoprt...
[reactos.git] / reactos / tools / winebuild / spec32.c
1 /*
2 * 32-bit spec files
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "build.h"
34
35 #define IMAGE_FILE_MACHINE_UNKNOWN 0
36 #define IMAGE_FILE_MACHINE_I386 0x014c
37 #define IMAGE_FILE_MACHINE_ALPHA 0x0184
38 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
39 #define IMAGE_FILE_MACHINE_AMD64 0x8664
40 #define IMAGE_FILE_MACHINE_ARM 0x01C0
41
42 #define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224
43 #define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240
44
45 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
46 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
47 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
48
49 /* check if entry point needs a relay thunk */
50 static inline int needs_relay( const ORDDEF *odp )
51 {
52 /* skip nonexistent entry points */
53 if (!odp) return 0;
54 /* skip non-functions */
55 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) return 0;
56 /* skip norelay and forward entry points */
57 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
58 return 1;
59 }
60
61 /* check if dll will output relay thunks */
62 int has_relays( DLLSPEC *spec )
63 {
64 int i;
65
66 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
67
68 for (i = spec->base; i <= spec->limit; i++)
69 {
70 ORDDEF *odp = spec->ordinals[i];
71 if (needs_relay( odp )) return 1;
72 }
73 return 0;
74 }
75
76 /*******************************************************************
77 * make_internal_name
78 *
79 * Generate an internal name for an entry point. Used for stubs etc.
80 */
81 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
82 {
83 static char buffer[256];
84 if (odp->name || odp->export_name)
85 {
86 char *p;
87 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
88 odp->name ? odp->name : odp->export_name );
89 /* make sure name is a legal C identifier */
90 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
91 if (!*p) return buffer;
92 }
93 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
94 return buffer;
95 }
96
97
98 /*******************************************************************
99 * output_relay_debug
100 *
101 * Output entry points for relay debugging
102 */
103 static void output_relay_debug( DLLSPEC *spec )
104 {
105 int i;
106 unsigned int j, args, flags;
107
108 /* first the table of entry point offsets */
109
110 output( "\t%s\n", get_asm_rodata_section() );
111 output( "\t.align %d\n", get_alignment(4) );
112 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
113
114 for (i = spec->base; i <= spec->limit; i++)
115 {
116 ORDDEF *odp = spec->ordinals[i];
117
118 if (needs_relay( odp ))
119 output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
120 else
121 output( "\t.long 0\n" );
122 }
123
124 /* then the table of argument types */
125
126 output( "\t.align %d\n", get_alignment(4) );
127 output( ".L__wine_spec_relay_arg_types:\n" );
128
129 for (i = spec->base; i <= spec->limit; i++)
130 {
131 ORDDEF *odp = spec->ordinals[i];
132 unsigned int mask = 0;
133
134 if (needs_relay( odp ))
135 {
136 for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
137 {
138 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
139 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
140 }
141 }
142 output( "\t.long 0x%08x\n", mask );
143 }
144
145 /* then the relay thunks */
146
147 output( "\t.text\n" );
148 output( "__wine_spec_relay_entry_points:\n" );
149 output( "\tnop\n" ); /* to avoid 0 offset */
150
151 for (i = spec->base; i <= spec->limit; i++)
152 {
153 ORDDEF *odp = spec->ordinals[i];
154
155 if (!needs_relay( odp )) continue;
156
157 output( "\t.align %d\n", get_alignment(4) );
158 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
159
160 args = strlen(odp->u.func.arg_types);
161 flags = 0;
162
163 switch (target_cpu)
164 {
165 case CPU_x86:
166 if (odp->flags & FLAG_REGISTER)
167 output( "\tpushl %%eax\n" );
168 else
169 output( "\tpushl %%esp\n" );
170
171 if (odp->flags & FLAG_RET64) flags |= 1;
172 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
173
174 if (UsePIC)
175 {
176 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
177 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
178 }
179 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
180 output( "\tpushl %%eax\n" );
181
182 if (odp->flags & FLAG_REGISTER)
183 {
184 output( "\tcall *8(%%eax)\n" );
185 }
186 else
187 {
188 output( "\tcall *4(%%eax)\n" );
189 if (odp->type == TYPE_STDCALL)
190 output( "\tret $%u\n", args * get_ptr_size() );
191 else
192 output( "\tret\n" );
193 }
194 break;
195
196 case CPU_x86_64:
197 output( "\t.cfi_startproc\n" );
198 output( "\tsubq $40,%%rsp\n" );
199 output( "\t.cfi_adjust_cfa_offset 40\n" );
200 output( "\tmovq %%rcx,48(%%rsp)\n" );
201 output( "\tmovq %%rdx,56(%%rsp)\n" );
202 output( "\tmovq %%r8,64(%%rsp)\n" );
203 output( "\tmovq %%r9,72(%%rsp)\n" );
204 output( "\tleaq 40(%%rsp),%%r8\n" );
205 output( "\tmovq $%u,%%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
206 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
207 output( "\tcallq *%u(%%rcx)\n", (odp->flags & FLAG_REGISTER) ? 16 : 8 );
208 output( "\taddq $40,%%rsp\n" );
209 output( "\t.cfi_adjust_cfa_offset -40\n" );
210 output( "\tret\n" );
211 output( "\t.cfi_endproc\n" );
212 break;
213
214 default:
215 assert(0);
216 }
217 }
218 }
219
220 /*******************************************************************
221 * output_exports
222 *
223 * Output the export table for a Win32 module.
224 */
225 void output_exports( DLLSPEC *spec )
226 {
227 int i, fwd_size = 0;
228 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
229
230 if (!nr_exports) return;
231
232 output( "\n/* export table */\n\n" );
233 output( "\t.data\n" );
234 output( "\t.align %d\n", get_alignment(4) );
235 output( ".L__wine_spec_exports:\n" );
236
237 /* export directory header */
238
239 output( "\t.long 0\n" ); /* Characteristics */
240 output( "\t.long 0\n" ); /* TimeDateStamp */
241 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
242 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
243 output( "\t.long %u\n", spec->base ); /* Base */
244 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
245 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
246 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
247 if (spec->nb_names)
248 {
249 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
250 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
251 }
252 else
253 {
254 output( "\t.long 0\n" ); /* AddressOfNames */
255 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
256 }
257
258 /* output the function pointers */
259
260 output( "\n.L__wine_spec_exports_funcs:\n" );
261 for (i = spec->base; i <= spec->limit; i++)
262 {
263 ORDDEF *odp = spec->ordinals[i];
264 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
265 else switch(odp->type)
266 {
267 case TYPE_EXTERN:
268 case TYPE_STDCALL:
269 case TYPE_VARARGS:
270 case TYPE_CDECL:
271 if (odp->flags & FLAG_FORWARD)
272 {
273 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
274 fwd_size += strlen(odp->link_name) + 1;
275 }
276 else if (odp->flags & FLAG_EXT_LINK)
277 {
278 output( "\t%s %s_%s\n",
279 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
280 }
281 else
282 {
283 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
284 }
285 break;
286 case TYPE_STUB:
287 output( "\t%s %s\n", get_asm_ptr_keyword(),
288 asm_name( get_stub_name( odp, spec )) );
289 break;
290 default:
291 assert(0);
292 }
293 }
294
295 if (spec->nb_names)
296 {
297 /* output the function name pointers */
298
299 int namepos = strlen(spec->file_name) + 1;
300
301 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
302 for (i = 0; i < spec->nb_names; i++)
303 {
304 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
305 namepos += strlen(spec->names[i]->name) + 1;
306 }
307
308 /* output the function ordinals */
309
310 output( "\n.L__wine_spec_exp_ordinals:\n" );
311 for (i = 0; i < spec->nb_names; i++)
312 {
313 output( "\t%s %d\n",
314 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
315 }
316 if (spec->nb_names % 2)
317 {
318 output( "\t%s 0\n", get_asm_short_keyword() );
319 }
320 }
321
322 /* output the export name strings */
323
324 output( "\n.L__wine_spec_exp_names:\n" );
325 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
326 for (i = 0; i < spec->nb_names; i++)
327 output( "\t%s \"%s\"\n",
328 get_asm_string_keyword(), spec->names[i]->name );
329
330 /* output forward strings */
331
332 if (fwd_size)
333 {
334 output( "\n.L__wine_spec_forwards:\n" );
335 for (i = spec->base; i <= spec->limit; i++)
336 {
337 ORDDEF *odp = spec->ordinals[i];
338 if (odp && (odp->flags & FLAG_FORWARD))
339 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
340 }
341 }
342 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
343 output( ".L__wine_spec_exports_end:\n" );
344
345 /* output relays */
346
347 if (!has_relays( spec ))
348 {
349 output( "\t%s 0\n", get_asm_ptr_keyword() );
350 return;
351 }
352
353 output( ".L__wine_spec_relay_descr:\n" );
354 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
355 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
356 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
357 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
358 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
359 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
360
361 output_relay_debug( spec );
362 }
363
364
365 /*******************************************************************
366 * output_stub_funcs
367 *
368 * Output the functions for stub entry points
369 */
370 static void output_stub_funcs( DLLSPEC *spec )
371 {
372 int i;
373
374 #if 0
375 for (i = 0; i < spec->nb_entry_points; i++)
376 {
377 ORDDEF *odp = &spec->entry_points[i];
378 if (odp->type != TYPE_STUB) continue;
379 fprintf( outfile, "#ifdef __GNUC__\n" );
380 fprintf( outfile, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func ) __attribute__((noreturn));\n" );
381 fprintf( outfile, "#else\n" );
382 fprintf( outfile, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func );\n" );
383 fprintf( outfile, "#endif\n\n" );
384 break;
385 }
386 #endif
387
388 for (i = 0; i < spec->nb_entry_points; i++)
389 {
390 const ORDDEF *odp = &spec->entry_points[i];
391 if (odp->type != TYPE_STUB) continue;
392 output( "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
393 if (odp->name)
394 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp->name );
395 else if (odp->export_name)
396 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp->export_name );
397 else
398 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%d\"); }\n", odp->ordinal );
399 }
400 }
401
402
403 /*******************************************************************
404 * output_asm_constructor
405 *
406 * Output code for calling a dll constructor.
407 */
408 static void output_asm_constructor( const char *constructor )
409 {
410 if (target_platform == PLATFORM_APPLE)
411 {
412 /* Mach-O doesn't have an init section */
413 output( "\n\t.mod_init_func\n" );
414 output( "\t.align %d\n", get_alignment(4) );
415 output( "\t.long %s\n", asm_name(constructor) );
416 }
417 else
418 {
419 output( "\n\t.section \".init\",\"ax\"\n" );
420 switch(target_cpu)
421 {
422 case CPU_x86:
423 case CPU_x86_64:
424 output( "\tcall %s\n", asm_name(constructor) );
425 break;
426 case CPU_SPARC:
427 output( "\tcall %s\n", asm_name(constructor) );
428 output( "\tnop\n" );
429 break;
430 case CPU_ALPHA:
431 output( "\tjsr $26,%s\n", asm_name(constructor) );
432 break;
433 case CPU_ARM:
434 output( "\tblx %s\n", asm_name(constructor) );
435 break;
436 case CPU_POWERPC:
437 output( "\tbl %s\n", asm_name(constructor) );
438 break;
439 }
440 }
441 }
442
443
444 /*******************************************************************
445 * output_module
446 *
447 * Output the module data.
448 */
449 void output_module( DLLSPEC *spec )
450 {
451 int machine = 0;
452 unsigned int page_size = get_page_size();
453
454 /* Reserve some space for the PE header */
455
456 switch (target_platform)
457 {
458 case PLATFORM_APPLE:
459 output( "\t.text\n" );
460 output( "\t.align %d\n", get_alignment(page_size) );
461 output( "__wine_spec_pe_header:\n" );
462 output( "\t.space 65536\n" );
463 break;
464 case PLATFORM_SOLARIS:
465 output( "\n\t.section \".text\",\"ax\"\n" );
466 output( "__wine_spec_pe_header:\n" );
467 output( "\t.skip %u\n", 65536 + page_size );
468 break;
469 default:
470 output( "\n\t.section \".init\",\"ax\"\n" );
471 switch(target_cpu)
472 {
473 case CPU_x86:
474 case CPU_x86_64:
475 case CPU_ALPHA:
476 case CPU_SPARC:
477 output( "\tjmp 1f\n" );
478 break;
479 case CPU_ARM:
480 case CPU_POWERPC:
481 output( "\tb 1f\n" );
482 break;
483 }
484 output( "__wine_spec_pe_header:\n" );
485 output( "\t.skip %u\n", 65536 + page_size );
486 output( "1:\n" );
487 break;
488 }
489
490 /* Output the NT header */
491
492 output( "\n\t.data\n" );
493 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
494 output( "%s\n", asm_globl("__wine_spec_nt_header") );
495 output( ".L__wine_spec_rva_base:\n" );
496
497 output( "\t.long 0x4550\n" ); /* Signature */
498 switch(target_cpu)
499 {
500 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
501 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
502 case CPU_ARM: machine = IMAGE_FILE_MACHINE_ARM; break;
503 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
504 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
505 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
506 }
507 output( "\t%s 0x%04x\n", /* Machine */
508 get_asm_short_keyword(), machine );
509 output( "\t%s 0\n", /* NumberOfSections */
510 get_asm_short_keyword() );
511 output( "\t.long 0\n" ); /* TimeDateStamp */
512 output( "\t.long 0\n" ); /* PointerToSymbolTable */
513 output( "\t.long 0\n" ); /* NumberOfSymbols */
514 output( "\t%s %d\n", /* SizeOfOptionalHeader */
515 get_asm_short_keyword(),
516 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
517 output( "\t%s 0x%04x\n", /* Characteristics */
518 get_asm_short_keyword(), spec->characteristics );
519 output( "\t%s 0x%04x\n", /* Magic */
520 get_asm_short_keyword(),
521 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
522 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
523 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
524 output( "\t.long 0\n" ); /* SizeOfCode */
525 output( "\t.long 0\n" ); /* SizeOfInitializedData */
526 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
527 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
528 output( "\t%s %s\n", /* AddressOfEntryPoint */
529 get_asm_ptr_keyword(), spec->init_func ? asm_name(spec->init_func) : "0" );
530 if (get_ptr_size() == 4)
531 {
532 output( "\t.long 0\n" ); /* BaseOfCode */
533 output( "\t.long 0\n" ); /* BaseOfData */
534 }
535 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
536 get_asm_ptr_keyword() );
537 output( "\t.long %u\n", page_size ); /* SectionAlignment */
538 output( "\t.long %u\n", page_size ); /* FileAlignment */
539 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
540 get_asm_short_keyword() );
541 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
542 get_asm_short_keyword() );
543 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
544 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
545 output( "\t.long 0\n" ); /* Win32VersionValue */
546 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
547 asm_name("_end") );
548 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
549 output( "\t.long 0\n" ); /* CheckSum */
550 output( "\t%s 0x%04x\n", /* Subsystem */
551 get_asm_short_keyword(), spec->subsystem );
552 output( "\t%s 0x%04x\n", /* DllCharacteristics */
553 get_asm_short_keyword(), spec->dll_characteristics );
554 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
555 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
556 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
557 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
558 output( "\t.long 0\n" ); /* LoaderFlags */
559 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
560
561 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
562 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
563 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
564 else
565 output( "\t.long 0,0\n" );
566
567 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
568 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
569 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
570 else
571 output( "\t.long 0,0\n" );
572
573 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
574 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
575 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
576 else
577 output( "\t.long 0,0\n" );
578
579 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
580 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
581 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
582 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
583 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
584 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
585 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
586 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
587 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
588 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
589 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
590 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
591 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
592
593 output( "\n\t%s\n", get_asm_string_section() );
594 output( "%s\n", asm_globl("__wine_spec_file_name") );
595 output( ".L__wine_spec_file_name:\n" );
596 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
597 if (target_platform == PLATFORM_APPLE)
598 output( "\t.lcomm %s,4\n", asm_name("_end") );
599
600 output_asm_constructor( "__wine_spec_init_ctor" );
601 }
602
603
604 /*******************************************************************
605 * BuildSpec32File
606 *
607 * Build a Win32 C file from a spec file.
608 */
609 void BuildSpec32File( DLLSPEC *spec )
610 {
611 resolve_imports( spec );
612 output_standard_file_header();
613 output_module( spec );
614 output_stubs( spec );
615 output_exports( spec );
616 output_imports( spec );
617 output_resources( spec );
618 output_gnu_stack_note();
619 }
620
621
622 /*******************************************************************
623 * output_fake_module
624 *
625 * Build a fake binary module from a spec file.
626 */
627 void output_fake_module( DLLSPEC *spec )
628 {
629 static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
630 0xc2, 0x0c, 0x00 }; /* ret $12 */
631
632 static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
633 0xc2, 0x04, 0x00 }; /* ret $4 */
634
635 static const char fakedll_signature[] = "Wine placeholder DLL";
636 const unsigned int page_size = get_page_size();
637 const unsigned int section_align = page_size;
638 const unsigned int file_align = 0x200;
639 const unsigned int reloc_size = 8;
640 const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
641 const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
642 const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
643 sizeof(dll_code_section) : sizeof(exe_code_section);
644 unsigned char *resources;
645 unsigned int resources_size;
646 unsigned int image_size = 3 * section_align;
647
648 resolve_imports( spec );
649 output_bin_resources( spec, 3 * section_align );
650 resources = output_buffer;
651 resources_size = output_buffer_pos;
652 if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
653
654 init_output_buffer();
655
656 put_word( 0x5a4d ); /* e_magic */
657 put_word( 0x40 ); /* e_cblp */
658 put_word( 0x01 ); /* e_cp */
659 put_word( 0 ); /* e_crlc */
660 put_word( lfanew / 16 ); /* e_cparhdr */
661 put_word( 0x0000 ); /* e_minalloc */
662 put_word( 0xffff ); /* e_maxalloc */
663 put_word( 0x0000 ); /* e_ss */
664 put_word( 0x00b8 ); /* e_sp */
665 put_word( 0 ); /* e_csum */
666 put_word( 0 ); /* e_ip */
667 put_word( 0 ); /* e_cs */
668 put_word( lfanew ); /* e_lfarlc */
669 put_word( 0 ); /* e_ovno */
670 put_dword( 0 ); /* e_res */
671 put_dword( 0 );
672 put_word( 0 ); /* e_oemid */
673 put_word( 0 ); /* e_oeminfo */
674 put_dword( 0 ); /* e_res2 */
675 put_dword( 0 );
676 put_dword( 0 );
677 put_dword( 0 );
678 put_dword( 0 );
679 put_dword( lfanew );
680
681 put_data( fakedll_signature, sizeof(fakedll_signature) );
682 align_output( 16 );
683
684 put_dword( 0x4550 ); /* Signature */
685 switch(target_cpu)
686 {
687 case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
688 case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
689 case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
690 case CPU_ALPHA: put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
691 case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
692 case CPU_ARM: put_word( IMAGE_FILE_MACHINE_ARM ); break;
693 }
694 put_word( nb_sections ); /* NumberOfSections */
695 put_dword( 0 ); /* TimeDateStamp */
696 put_dword( 0 ); /* PointerToSymbolTable */
697 put_dword( 0 ); /* NumberOfSymbols */
698 put_word( get_ptr_size() == 8 ?
699 IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
700 IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
701 put_word( spec->characteristics ); /* Characteristics */
702 put_word( get_ptr_size() == 8 ?
703 IMAGE_NT_OPTIONAL_HDR64_MAGIC :
704 IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
705 put_byte( 0 ); /* MajorLinkerVersion */
706 put_byte( 0 ); /* MinorLinkerVersion */
707 put_dword( text_size ); /* SizeOfCode */
708 put_dword( 0 ); /* SizeOfInitializedData */
709 put_dword( 0 ); /* SizeOfUninitializedData */
710 put_dword( section_align ); /* AddressOfEntryPoint */
711 put_dword( section_align ); /* BaseOfCode */
712 if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
713 put_pword( 0x10000000 ); /* ImageBase */
714 put_dword( section_align ); /* SectionAlignment */
715 put_dword( file_align ); /* FileAlignment */
716 put_word( 1 ); /* MajorOperatingSystemVersion */
717 put_word( 0 ); /* MinorOperatingSystemVersion */
718 put_word( 0 ); /* MajorImageVersion */
719 put_word( 0 ); /* MinorImageVersion */
720 put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
721 put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
722 put_dword( 0 ); /* Win32VersionValue */
723 put_dword( image_size ); /* SizeOfImage */
724 put_dword( file_align ); /* SizeOfHeaders */
725 put_dword( 0 ); /* CheckSum */
726 put_word( spec->subsystem ); /* Subsystem */
727 put_word( spec->dll_characteristics ); /* DllCharacteristics */
728 put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
729 put_pword( page_size ); /* SizeOfStackCommit */
730 put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
731 put_pword( page_size ); /* SizeOfHeapCommit */
732 put_dword( 0 ); /* LoaderFlags */
733 put_dword( 16 ); /* NumberOfRvaAndSizes */
734
735 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
736 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
737 if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
738 {
739 put_dword( 3 * section_align );
740 put_dword( resources_size );
741 }
742 else
743 {
744 put_dword( 0 );
745 put_dword( 0 );
746 }
747
748 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
749 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
750 put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
751 put_dword( reloc_size );
752 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
753 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
754 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
755 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
756 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
757 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
758 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
759 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
760 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
761 put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
762
763 /* .text section */
764 put_data( ".text\0\0", 8 ); /* Name */
765 put_dword( section_align ); /* VirtualSize */
766 put_dword( section_align ); /* VirtualAddress */
767 put_dword( text_size ); /* SizeOfRawData */
768 put_dword( file_align ); /* PointerToRawData */
769 put_dword( 0 ); /* PointerToRelocations */
770 put_dword( 0 ); /* PointerToLinenumbers */
771 put_word( 0 ); /* NumberOfRelocations */
772 put_word( 0 ); /* NumberOfLinenumbers */
773 put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
774
775 /* .reloc section */
776 put_data( ".reloc\0", 8 ); /* Name */
777 put_dword( section_align ); /* VirtualSize */
778 put_dword( 2 * section_align );/* VirtualAddress */
779 put_dword( reloc_size ); /* SizeOfRawData */
780 put_dword( 2 * file_align ); /* PointerToRawData */
781 put_dword( 0 ); /* PointerToRelocations */
782 put_dword( 0 ); /* PointerToLinenumbers */
783 put_word( 0 ); /* NumberOfRelocations */
784 put_word( 0 ); /* NumberOfLinenumbers */
785 put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
786
787 /* .rsrc section */
788 if (resources_size)
789 {
790 put_data( ".rsrc\0\0", 8 ); /* Name */
791 put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
792 put_dword( 3 * section_align );/* VirtualAddress */
793 put_dword( resources_size ); /* SizeOfRawData */
794 put_dword( 3 * file_align ); /* PointerToRawData */
795 put_dword( 0 ); /* PointerToRelocations */
796 put_dword( 0 ); /* PointerToLinenumbers */
797 put_word( 0 ); /* NumberOfRelocations */
798 put_word( 0 ); /* NumberOfLinenumbers */
799 put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
800 }
801
802 /* .text contents */
803 align_output( file_align );
804 if (spec->characteristics & IMAGE_FILE_DLL)
805 put_data( dll_code_section, sizeof(dll_code_section) );
806 else
807 put_data( exe_code_section, sizeof(exe_code_section) );
808
809 /* .reloc contents */
810 align_output( file_align );
811 put_dword( 0 ); /* VirtualAddress */
812 put_dword( 0 ); /* SizeOfBlock */
813
814 /* .rsrc contents */
815 if (resources_size)
816 {
817 align_output( file_align );
818 put_data( resources, resources_size );
819 }
820 flush_output_buffer();
821 }
822
823
824 /*******************************************************************
825 * BuildDef32File
826 *
827 * Build a Win32 def file from a spec file.
828 */
829 void BuildDef32File( DLLSPEC *spec )
830 {
831 const char *name;
832 int i, total;
833
834 if (spec_file_name)
835 output( "; File generated automatically from %s; do not edit!\n\n",
836 spec_file_name );
837 else
838 output( "; File generated automatically; do not edit!\n\n" );
839
840 output( "LIBRARY %s\n\n", spec->file_name);
841 output( "EXPORTS\n");
842
843 /* Output the exports and relay entry points */
844
845 for (i = total = 0; i < spec->nb_entry_points; i++)
846 {
847 const ORDDEF *odp = &spec->entry_points[i];
848 int is_data = 0;
849
850 if (!odp) continue;
851
852 if (odp->name) name = odp->name;
853 else if (odp->export_name) name = odp->export_name;
854 else continue;
855
856 if (!(odp->flags & FLAG_PRIVATE)) total++;
857
858 switch(odp->type)
859 {
860 case TYPE_EXTERN:
861 is_data = 1;
862 /* fall through */
863 case TYPE_VARARGS:
864 case TYPE_CDECL:
865 /* try to reduce output */
866 output( " %s", name );
867 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
868 output( "=%s", odp->link_name );
869 break;
870 case TYPE_STDCALL:
871 {
872 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
873 output( " %s", name );
874 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
875 if (odp->flags & FLAG_FORWARD)
876 {
877 output( "=%s", odp->link_name );
878 }
879 else if (strcmp(name, odp->link_name)) /* try to reduce output */
880 {
881 output( "=%s", odp->link_name );
882 if (!kill_at && target_cpu == CPU_x86) output( "@%d", at_param );
883 }
884 break;
885 }
886 case TYPE_FASTCALL:
887 {
888 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
889 output( " " );
890 if (!kill_at) output( "@" );
891 output( "%s", name );
892 if (!kill_at) output( "@%d", at_param );
893 if (odp->flags & FLAG_FORWARD)
894 {
895 output( "=" );
896 output( "%s", odp->link_name );
897 }
898 else if (strcmp(name, odp->link_name)) /* try to reduce output */
899 {
900 output( "=" );
901 if (!kill_at) output( "@" );
902 output( "%s", odp->link_name );
903 if (!kill_at) output( "@%d", at_param );
904 }
905 break;
906 }
907 case TYPE_STUB:
908 {
909 output( " %s", name );
910 if (!kill_at)
911 {
912 const char *check = name + strlen(name);
913 while (name != check &&
914 '0' <= check[-1] && check[-1] <= '9')
915 {
916 check--;
917 }
918 if (name != check && check != name + strlen(name) &&
919 '@' == check[-1])
920 {
921 output("%s", check - 1);
922 }
923 }
924 if (odp->name || odp->export_name)
925 {
926 output("=%s", make_internal_name( odp, spec, "stub" ));
927 }
928 break;
929 }
930 default:
931 assert(0);
932 }
933 output( " @%d", odp->ordinal );
934 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
935 if (is_data) output( " DATA" );
936 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
937 output( "\n" );
938 }
939 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
940 }
941
942
943 /*******************************************************************
944 * BuildPedllFile
945 *
946 * Build a PE DLL C file from a spec file.
947 */
948 void BuildPedllFile( DLLSPEC *spec )
949 {
950 int i, has_stubs = 0;
951
952 output_standard_file_header();
953
954 for (i = 0; i < spec->nb_entry_points; i++)
955 {
956 const ORDDEF *odp = &spec->entry_points[i];
957 if (odp->type == TYPE_STUB)
958 {
959 has_stubs = 1;
960 break;
961 }
962 }
963
964 if (!has_stubs)
965 {
966 output( "/* This file is intentionally left blank */\n");
967 return;
968 }
969
970 output( "#include <stdarg.h>\n");
971 output( "#include \"windef.h\"\n");
972 output( "#include \"winbase.h\"\n");
973 output( "#include \"wine/config.h\"\n");
974 output( "#include \"wine/exception.h\"\n\n");
975
976 output( "void __wine_spec_unimplemented_stub( const char *module, const char *function )\n");
977 output( "{\n");
978 output( " ULONG_PTR args[2];\n");
979 output( "\n");
980 output( " args[0] = (ULONG_PTR)module;\n");
981 output( " args[1] = (ULONG_PTR)function;\n");
982 output( " RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );\n");
983 output( "}\n\n");
984
985 output( "static const char __wine_spec_file_name[] = \"%s\";\n\n", spec->file_name );
986
987 /* Output the stub functions */
988 output_stub_funcs( spec );
989 }