Sync to wine64. Adds support for amd64 relays.. not that we use this feature.
[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 "winglue.h"
34 //#include "wine/exception.h"
35 #include "build.h"
36
37
38 /* check if entry point needs a relay thunk */
39 static inline int needs_relay( const ORDDEF *odp )
40 {
41 /* skip nonexistent entry points */
42 if (!odp) return 0;
43 /* skip non-functions */
44 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) return 0;
45 if ((odp->type == TYPE_CDECL) && (target_cpu == CPU_x86_64)) return 0;
46 /* skip norelay and forward entry points */
47 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
48 return 1;
49 }
50
51 /* check if dll will output relay thunks */
52 int has_relays( DLLSPEC *spec )
53 {
54 int i;
55
56 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64) return 0;
57
58 for (i = spec->base; i <= spec->limit; i++)
59 {
60 ORDDEF *odp = spec->ordinals[i];
61 if (needs_relay( odp )) return 1;
62 }
63 return 0;
64 }
65
66 /*******************************************************************
67 * make_internal_name
68 *
69 * Generate an internal name for an entry point. Used for stubs etc.
70 */
71 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
72 {
73 static char buffer[256];
74 if (odp->name || odp->export_name)
75 {
76 char *p;
77 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
78 odp->name ? odp->name : odp->export_name );
79 /* make sure name is a legal C identifier */
80 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
81 if (!*p) return buffer;
82 }
83 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
84 return buffer;
85 }
86
87 static void output_relay_debug_x86(DLLSPEC *spec, int i)
88 {
89 unsigned int args, flags;
90 ORDDEF *odp = spec->ordinals[i];
91
92 output( "\t.align %d\n", get_alignment(4) );
93 output( ".L__wine_spec_relay_entry_point_%d:\n", i );
94
95 if (odp->flags & FLAG_REGISTER)
96 output( "\tpushl %%eax\n" );
97 else
98 output( "\tpushl %%esp\n" );
99
100 args = strlen(odp->u.func.arg_types);
101 flags = 0;
102 if (odp->flags & FLAG_RET64) flags |= 1;
103 if (odp->type == TYPE_STDCALL) flags |= 2;
104 output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
105
106 if (UsePIC)
107 {
108 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
109 output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
110 }
111 else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
112 output( "\tpushl %%eax\n" );
113
114 if (odp->flags & FLAG_REGISTER)
115 {
116 output( "\tcall *8(%%eax)\n" );
117 }
118 else
119 {
120 output( "\tcall *4(%%eax)\n" );
121 if (odp->type == TYPE_STDCALL)
122 output( "\tret $%u\n", args * get_ptr_size() );
123 else
124 output( "\tret\n" );
125 }
126 }
127
128
129 static void output_relay_debug_amd64(DLLSPEC *spec, int i)
130 {
131 unsigned int args, flags;
132 ORDDEF *odp = spec->ordinals[i];
133
134 output( "\t.align %d\n", get_alignment(8) );
135 output( ".type __wine_spec_relay_entry_point_%d,@function\n", i );
136 output( "__wine_spec_relay_entry_point_%d:\n", i );
137
138 /* Save the original registers on the stack, so +relay can use them */
139 /* This makes the stack layout a bit weird:
140 * [ 4 longs reserved by abi ]
141 * [ rcx, rdx, r8, r9 ]
142 * [ fixed alignment ]
143 * [ ret address ]
144 * [ 4 longs reserved by abi ]
145 * [ possibly additonal args ]
146 */
147 output( "\tpushq %%rbp\n" );
148 output( "\tmovq %%rsp, %%rbp\n" );
149 output( "\tsubq $0x40, %%rsp\n" );
150 output( "\tmovq %%rcx, 0x20(%%rsp)\n" );
151 output( "\tmovq %%rdx, 0x28(%%rsp)\n" );
152 output( "\tmovq %%r8, 0x30(%%rsp)\n" );
153 output( "\tmovq %%r9, 0x38(%%rsp)\n" );
154
155 if (UsePIC)
156 {
157 output( "\tleaq .L__wine_spec_relay_descr(%%rip),%%rcx\n" );
158 }
159 else output( "\tmovq $.L__wine_spec_relay_descr,%%rcx\n" );
160
161 args = strlen(odp->u.func.arg_types);
162 flags = 0; /* For later use */
163 output( "\tmovq $%u, %%rdx\n", (flags << 24) | (args << 16) | (i - spec->base) );
164
165 output( "\tmovq %%rsp, %%r8\n" );
166
167 output( "\tcall *8(%%rcx)\n" );
168 output( "\tmovq 0x20(%%rsp), %%rcx\n" );
169 output( "\tmovq 0x28(%%rsp), %%rdx\n" );
170 output( "\tmovq 0x30(%%rsp), %%r8\n" );
171 output( "\tmovq 0x38(%%rsp), %%r9\n" );
172 output( "\taddq $0x40, %%rsp\n" );
173 output( "\tleaveq\n" );
174 output( "\tret\n" );
175 }
176
177 /*******************************************************************
178 * output_relay_debug
179 *
180 * Output entry points for relay debugging
181 */
182 static void output_relay_debug( DLLSPEC *spec )
183 {
184 int i;
185 unsigned int j;
186
187 /* first the table of entry point offsets */
188
189 output( "\t%s\n", get_asm_rodata_section() );
190 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
191 output( ".L__wine_spec_relay_entry_point_offsets:\n" );
192
193 for (i = spec->base; i <= spec->limit; i++)
194 {
195 ORDDEF *odp = spec->ordinals[i];
196
197 if (needs_relay( odp ))
198 output( "\t.long __wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
199 else
200 output( "\t.long 0\n" );
201 }
202
203 /* then the table of argument types */
204
205 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
206 output( ".L__wine_spec_relay_arg_types:\n" );
207
208 for (i = spec->base; i <= spec->limit; i++)
209 {
210 ORDDEF *odp = spec->ordinals[i];
211 unsigned int mask = 0;
212
213 if (needs_relay( odp ))
214 {
215 for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
216 {
217 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
218 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
219 }
220 }
221 output( "\t.long 0x%08x\n", mask );
222 }
223
224 /* then the relay thunks */
225
226 output( "\t.text\n" );
227 output( "__wine_spec_relay_entry_points:\n" );
228 output( "\tnop\n" ); /* to avoid 0 offset */
229
230 for (i = spec->base; i <= spec->limit; i++)
231 {
232 ORDDEF *odp = spec->ordinals[i];
233
234 if (!needs_relay( odp )) continue;
235
236 if (target_cpu == CPU_x86)
237 output_relay_debug_x86( spec, i );
238 else if (target_cpu == CPU_x86_64)
239 output_relay_debug_amd64( spec, i );
240 else
241 assert(0);
242 }
243 }
244
245 /*******************************************************************
246 * output_exports
247 *
248 * Output the export table for a Win32 module.
249 */
250 static void output_exports( DLLSPEC *spec )
251 {
252 int i, fwd_size = 0;
253 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
254
255 if (!nr_exports) return;
256
257 output( "\n/* export table */\n\n" );
258 output( "\t.data\n" );
259 output( "\t.align %d\n", get_alignment(4) );
260 output( ".L__wine_spec_exports:\n" );
261
262 /* export directory header */
263
264 output( "\t.long 0\n" ); /* Characteristics */
265 output( "\t.long 0\n" ); /* TimeDateStamp */
266 output( "\t.long 0\n" ); /* MajorVersion/MinorVersion */
267 output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
268 output( "\t.long %u\n", spec->base ); /* Base */
269 output( "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
270 output( "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
271 output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
272 if (spec->nb_names)
273 {
274 output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
275 output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
276 }
277 else
278 {
279 output( "\t.long 0\n" ); /* AddressOfNames */
280 output( "\t.long 0\n" ); /* AddressOfNameOrdinals */
281 }
282
283 /* output the function pointers */
284
285 output( "\n.L__wine_spec_exports_funcs:\n" );
286 for (i = spec->base; i <= spec->limit; i++)
287 {
288 ORDDEF *odp = spec->ordinals[i];
289 if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
290 else switch(odp->type)
291 {
292 case TYPE_EXTERN:
293 case TYPE_STDCALL:
294 case TYPE_VARARGS:
295 case TYPE_CDECL:
296 if (odp->flags & FLAG_FORWARD)
297 {
298 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
299 fwd_size += strlen(odp->link_name) + 1;
300 }
301 else if (odp->flags & FLAG_EXT_LINK)
302 {
303 output( "\t%s %s_%s\n",
304 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
305 }
306 else
307 {
308 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
309 }
310 break;
311 case TYPE_STUB:
312 output( "\t%s %s\n", get_asm_ptr_keyword(),
313 asm_name( get_stub_name( odp, spec )) );
314 break;
315 default:
316 assert(0);
317 }
318 }
319
320 if (spec->nb_names)
321 {
322 /* output the function name pointers */
323
324 int namepos = strlen(spec->file_name) + 1;
325
326 output( "\n.L__wine_spec_exp_name_ptrs:\n" );
327 for (i = 0; i < spec->nb_names; i++)
328 {
329 output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
330 namepos += strlen(spec->names[i]->name) + 1;
331 }
332
333 /* output the function ordinals */
334
335 output( "\n.L__wine_spec_exp_ordinals:\n" );
336 for (i = 0; i < spec->nb_names; i++)
337 {
338 output( "\t%s %d\n",
339 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
340 }
341 if (spec->nb_names % 2)
342 {
343 output( "\t%s 0\n", get_asm_short_keyword() );
344 }
345 }
346
347 /* output the export name strings */
348
349 output( "\n.L__wine_spec_exp_names:\n" );
350 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
351 for (i = 0; i < spec->nb_names; i++)
352 output( "\t%s \"%s\"\n",
353 get_asm_string_keyword(), spec->names[i]->name );
354
355 /* output forward strings */
356
357 if (fwd_size)
358 {
359 output( "\n.L__wine_spec_forwards:\n" );
360 for (i = spec->base; i <= spec->limit; i++)
361 {
362 ORDDEF *odp = spec->ordinals[i];
363 if (odp && (odp->flags & FLAG_FORWARD))
364 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
365 }
366 }
367 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
368 output( ".L__wine_spec_exports_end:\n" );
369
370 /* output relays */
371
372 /* we only support relay debugging on i386 and amd64 */
373 if (target_cpu != CPU_x86 && target_cpu != CPU_x86_64)
374 {
375 output( "\t%s 0\n", get_asm_ptr_keyword() );
376 return;
377 }
378
379 output( ".L__wine_spec_relay_descr:\n" );
380 output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
381 output( "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
382 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
383 output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
384 output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
385 output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
386
387 output_relay_debug( spec );
388 }
389
390
391 /*******************************************************************
392 * output_stub_funcs
393 *
394 * Output the functions for stub entry points
395 */
396 static void output_stub_funcs( DLLSPEC *spec )
397 {
398 int i;
399
400 #if 0
401 for (i = 0; i < spec->nb_entry_points; i++)
402 {
403 ORDDEF *odp = &spec->entry_points[i];
404 if (odp->type != TYPE_STUB) continue;
405 fprintf( outfile, "#ifdef __GNUC__\n" );
406 fprintf( outfile, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func ) __attribute__((noreturn));\n" );
407 fprintf( outfile, "#else\n" );
408 fprintf( outfile, "extern void __wine_spec_unimplemented_stub( const char *module, const char *func );\n" );
409 fprintf( outfile, "#endif\n\n" );
410 break;
411 }
412 #endif
413
414 for (i = 0; i < spec->nb_entry_points; i++)
415 {
416 const ORDDEF *odp = &spec->entry_points[i];
417 if (odp->type != TYPE_STUB) continue;
418 output( "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
419 if (odp->name)
420 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp->name );
421 else if (odp->export_name)
422 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%s\"); }\n", odp->export_name );
423 else
424 output( "{ __wine_spec_unimplemented_stub(__wine_spec_file_name, \"%d\"); }\n", odp->ordinal );
425 }
426 }
427
428
429 /*******************************************************************
430 * output_asm_constructor
431 *
432 * Output code for calling a dll constructor.
433 */
434 static void output_asm_constructor( const char *constructor )
435 {
436 if (target_platform == PLATFORM_APPLE)
437 {
438 /* Mach-O doesn't have an init section */
439 output( "\n\t.mod_init_func\n" );
440 output( "\t.align %d\n", get_alignment(4) );
441 output( "\t.long %s\n", asm_name(constructor) );
442 }
443 else
444 {
445 output( "\n\t.section \".init\",\"ax\"\n" );
446 switch(target_cpu)
447 {
448 case CPU_x86:
449 case CPU_x86_64:
450 output( "\tcall %s\n", asm_name(constructor) );
451 break;
452 case CPU_SPARC:
453 output( "\tcall %s\n", asm_name(constructor) );
454 output( "\tnop\n" );
455 break;
456 case CPU_ALPHA:
457 output( "\tjsr $26,%s\n", asm_name(constructor) );
458 break;
459 case CPU_POWERPC:
460 output( "\tbl %s\n", asm_name(constructor) );
461 break;
462 }
463 }
464 }
465
466
467 /*******************************************************************
468 * BuildSpec32File
469 *
470 * Build a Win32 C file from a spec file.
471 */
472 void BuildSpec32File( DLLSPEC *spec )
473 {
474 int machine = 0;
475 unsigned int page_size = get_page_size();
476
477 resolve_imports( spec );
478 output_standard_file_header();
479
480 /* Reserve some space for the PE header */
481
482 switch (target_platform)
483 {
484 case PLATFORM_APPLE:
485 output( "\t.text\n" );
486 output( "\t.align %d\n", get_alignment(page_size) );
487 output( "__wine_spec_pe_header:\n" );
488 output( "\t.space 65536\n" );
489 break;
490 case PLATFORM_SOLARIS:
491 output( "\n\t.section \".text\",\"ax\"\n" );
492 output( "__wine_spec_pe_header:\n" );
493 output( "\t.skip %u\n", 65536 + page_size );
494 break;
495 default:
496 output( "\n\t.section \".init\",\"ax\"\n" );
497 switch(target_cpu)
498 {
499 case CPU_x86:
500 case CPU_x86_64:
501 case CPU_ALPHA:
502 case CPU_SPARC:
503 output( "\tjmp 1f\n" );
504 break;
505 case CPU_POWERPC:
506 output( "\tb 1f\n" );
507 break;
508 }
509 output( "__wine_spec_pe_header:\n" );
510 output( "\t.skip %u\n", 65536 + page_size );
511 output( "1:\n" );
512 break;
513 }
514
515 /* Output the NT header */
516
517 output( "\n\t.data\n" );
518 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
519 output( "%s\n", asm_globl("__wine_spec_nt_header") );
520 output( ".L__wine_spec_rva_base:\n" );
521
522 output( "\t.long 0x%04x\n", IMAGE_NT_SIGNATURE ); /* Signature */
523 switch(target_cpu)
524 {
525 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
526 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
527 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
528 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
529 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
530 }
531 output( "\t%s 0x%04x\n", /* Machine */
532 get_asm_short_keyword(), machine );
533 output( "\t%s 0\n", /* NumberOfSections */
534 get_asm_short_keyword() );
535 output( "\t.long 0\n" ); /* TimeDateStamp */
536 output( "\t.long 0\n" ); /* PointerToSymbolTable */
537 output( "\t.long 0\n" ); /* NumberOfSymbols */
538 output( "\t%s %d\n", /* SizeOfOptionalHeader */
539 get_asm_short_keyword(),
540 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
541 output( "\t%s 0x%04x\n", /* Characteristics */
542 get_asm_short_keyword(), spec->characteristics );
543 output( "\t%s 0x%04x\n", /* Magic */
544 get_asm_short_keyword(),
545 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
546 output( "\t.byte 0\n" ); /* MajorLinkerVersion */
547 output( "\t.byte 0\n" ); /* MinorLinkerVersion */
548 output( "\t.long 0\n" ); /* SizeOfCode */
549 output( "\t.long 0\n" ); /* SizeOfInitializedData */
550 output( "\t.long 0\n" ); /* SizeOfUninitializedData */
551 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
552 output( "\t%s %s\n", /* AddressOfEntryPoint */
553 get_asm_ptr_keyword(), asm_name(spec->init_func) );
554 if (get_ptr_size() == 4)
555 {
556 output( "\t.long 0\n" ); /* BaseOfCode */
557 output( "\t.long 0\n" ); /* BaseOfData */
558 }
559 output( "\t%s __wine_spec_pe_header\n", /* ImageBase */
560 get_asm_ptr_keyword() );
561 output( "\t.long %u\n", page_size ); /* SectionAlignment */
562 output( "\t.long %u\n", page_size ); /* FileAlignment */
563 output( "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
564 get_asm_short_keyword() );
565 output( "\t%s 0,0\n", /* Major/MinorImageVersion */
566 get_asm_short_keyword() );
567 output( "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
568 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
569 output( "\t.long 0\n" ); /* Win32VersionValue */
570 output( "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
571 asm_name("_end") );
572 output( "\t.long %u\n", page_size ); /* SizeOfHeaders */
573 output( "\t.long 0\n" ); /* CheckSum */
574 output( "\t%s 0x%04x\n", /* Subsystem */
575 get_asm_short_keyword(), spec->subsystem );
576 output( "\t%s 0x%04x\n", /* DllCharacteristics */
577 get_asm_short_keyword(), spec->dll_characteristics );
578 output( "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
579 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
580 output( "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
581 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
582 output( "\t.long 0\n" ); /* LoaderFlags */
583 output( "\t.long 16\n" ); /* NumberOfRvaAndSizes */
584
585 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
586 output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
587 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
588 else
589 output( "\t.long 0,0\n" );
590
591 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
592 output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
593 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
594 else
595 output( "\t.long 0,0\n" );
596
597 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
598 output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
599 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
600 else
601 output( "\t.long 0,0\n" );
602
603 output( "\t.long 0,0\n" ); /* DataDirectory[3] */
604 output( "\t.long 0,0\n" ); /* DataDirectory[4] */
605 output( "\t.long 0,0\n" ); /* DataDirectory[5] */
606 output( "\t.long 0,0\n" ); /* DataDirectory[6] */
607 output( "\t.long 0,0\n" ); /* DataDirectory[7] */
608 output( "\t.long 0,0\n" ); /* DataDirectory[8] */
609 output( "\t.long 0,0\n" ); /* DataDirectory[9] */
610 output( "\t.long 0,0\n" ); /* DataDirectory[10] */
611 output( "\t.long 0,0\n" ); /* DataDirectory[11] */
612 output( "\t.long 0,0\n" ); /* DataDirectory[12] */
613 output( "\t.long 0,0\n" ); /* DataDirectory[13] */
614 output( "\t.long 0,0\n" ); /* DataDirectory[14] */
615 output( "\t.long 0,0\n" ); /* DataDirectory[15] */
616
617 output( "\n\t%s\n", get_asm_string_section() );
618 output( "%s\n", asm_globl("__wine_spec_file_name") );
619 output( ".L__wine_spec_file_name:\n" );
620 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
621 if (target_platform == PLATFORM_APPLE)
622 output( "\t.lcomm %s,4\n", asm_name("_end") );
623
624 output_stubs( spec );
625 output_exports( spec );
626 output_imports( spec );
627 output_resources( spec );
628 output_asm_constructor( "__wine_spec_init_ctor" );
629 output_gnu_stack_note();
630 }
631
632
633 /*******************************************************************
634 * BuildDef32File
635 *
636 * Build a Win32 def file from a spec file.
637 */
638 void BuildDef32File( DLLSPEC *spec )
639 {
640 const char *name;
641 int i, total;
642
643 if (spec_file_name)
644 output( "; File generated automatically from %s; do not edit!\n\n",
645 spec_file_name );
646 else
647 output( "; File generated automatically; do not edit!\n\n" );
648
649 output( "LIBRARY %s\n\n", spec->file_name);
650 output( "EXPORTS\n");
651
652 /* Output the exports and relay entry points */
653
654 for (i = total = 0; i < spec->nb_entry_points; i++)
655 {
656 const ORDDEF *odp = &spec->entry_points[i];
657 int is_data = 0;
658
659 if (!odp) continue;
660
661 if (odp->name) name = odp->name;
662 else if (odp->export_name) name = odp->export_name;
663 else continue;
664
665 if (!(odp->flags & FLAG_PRIVATE)) total++;
666
667 switch(odp->type)
668 {
669 case TYPE_EXTERN:
670 output( " %s", name );
671 is_data = 1;
672 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
673 output( "=%s", odp->link_name );
674 break;
675 case TYPE_VARARGS:
676 case TYPE_CDECL:
677 /* try to reduce output */
678 output( " %s", name );
679 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
680 output( "=%s", odp->link_name );
681 break;
682 case TYPE_STDCALL:
683 {
684 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
685 output( " %s", name );
686 if (!kill_at) output( "@%d", at_param );
687 if (odp->flags & FLAG_FORWARD)
688 {
689 output( "=%s", odp->link_name );
690 }
691 else if (strcmp(name, odp->link_name)) /* try to reduce output */
692 {
693 output( "=%s", odp->link_name );
694 if (!kill_at) output( "@%d", at_param );
695 }
696 break;
697 }
698 case TYPE_FASTCALL:
699 {
700 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
701 output( " " );
702 if (!kill_at) output( "@" );
703 output( "%s", name );
704 if (!kill_at) output( "@%d", at_param );
705 if (odp->flags & FLAG_FORWARD)
706 {
707 output( "=" );
708 if (!kill_at) output( "@" );
709 output( "%s", odp->link_name );
710 }
711 else if (strcmp(name, odp->link_name)) /* try to reduce output */
712 {
713 output( "=" );
714 if (!kill_at) output( "@" );
715 output( "%s", odp->link_name );
716 if (!kill_at) output( "@%d", at_param );
717 }
718 break;
719 }
720 case TYPE_STUB:
721 {
722 output( " %s", name );
723 if (!kill_at)
724 {
725 const char *check = name + strlen(name);
726 while (name != check &&
727 '0' <= check[-1] && check[-1] <= '9')
728 {
729 check--;
730 }
731 if (name != check && check != name + strlen(name) &&
732 '@' == check[-1])
733 {
734 output("%s", check - 1);
735 }
736 }
737 if (odp->name || odp->export_name)
738 {
739 output("=%s", make_internal_name( odp, spec, "stub" ));
740 }
741 break;
742 }
743 default:
744 assert(0);
745 }
746 output( " @%d", odp->ordinal );
747 if (!odp->name || (odp->flags & FLAG_ORDINAL)) output( " NONAME" );
748 if (is_data) output( " DATA" );
749 if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
750 output( "\n" );
751 }
752 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
753 }
754
755
756 /*******************************************************************
757 * BuildPedllFile
758 *
759 * Build a PE DLL C file from a spec file.
760 */
761 void BuildPedllFile( DLLSPEC *spec )
762 {
763 int i, has_stubs = 0;
764
765 output_standard_file_header();
766
767 for (i = 0; i < spec->nb_entry_points; i++)
768 {
769 const ORDDEF *odp = &spec->entry_points[i];
770 if (odp->type == TYPE_STUB)
771 {
772 has_stubs = 1;
773 break;
774 }
775 }
776
777 if (!has_stubs)
778 {
779 output( "/* This file is intentionally left blank */\n");
780 return;
781 }
782
783 output( "#include <windows.h>\n");
784 output( "#include <reactos/debug.h>\n");
785
786 output( "void __stdcall __wine_spec_unimplemented_stub( const char *module, const char *function )\n");
787 output( "{\n");
788 output( " DPRINT1(\"%%s hit stub for %%s\\n\",module,function);\n");
789 output( " SetLastError(ERROR_CALL_NOT_IMPLEMENTED);\n");
790 output( "}\n");
791
792 output( "static const char __wine_spec_file_name[] = \"%s\";\n\n", spec->file_name );
793
794 /* Output the stub functions */
795 output_stub_funcs( spec );
796 }