server: Print process/thread id instead of pointer in protocol errors.
[wine/multimedia.git] / tools / winebuild / spec32.c
blobcf0c54b9d32e11980b1f5f256867e0dd19bfbf84
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 "windef.h"
34 #include "winbase.h"
35 #include "wine/exception.h"
36 #include "build.h"
39 /* check if entry point needs a relay thunk */
40 static inline int needs_relay( const ORDDEF *odp )
42 /* skip nonexistent entry points */
43 if (!odp) return 0;
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;
48 return 1;
51 /* check if dll will output relay thunks */
52 int has_relays( DLLSPEC *spec )
54 unsigned int i;
56 if (target_cpu != CPU_x86) return 0;
58 for (i = spec->base; i <= spec->limit; i++)
60 ORDDEF *odp = spec->ordinals[i];
61 if (needs_relay( odp )) return 1;
63 return 0;
66 /*******************************************************************
67 * output_relay_debug
69 * Output entry points for relay debugging
71 static void output_relay_debug( FILE *outfile, DLLSPEC *spec )
73 unsigned int i, j, args, flags;
75 /* first the table of entry point offsets */
77 fprintf( outfile, "\t%s\n", get_asm_rodata_section() );
78 fprintf( outfile, "\t.align %d\n", get_alignment(4) );
79 fprintf( outfile, ".L__wine_spec_relay_entry_point_offsets:\n" );
81 for (i = spec->base; i <= spec->limit; i++)
83 ORDDEF *odp = spec->ordinals[i];
85 if (needs_relay( odp ))
86 fprintf( outfile, "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
87 else
88 fprintf( outfile, "\t.long 0\n" );
91 /* then the table of argument types */
93 fprintf( outfile, "\t.align %d\n", get_alignment(4) );
94 fprintf( outfile, ".L__wine_spec_relay_arg_types:\n" );
96 for (i = spec->base; i <= spec->limit; i++)
98 ORDDEF *odp = spec->ordinals[i];
99 unsigned int mask = 0;
101 if (needs_relay( odp ))
103 for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
105 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
106 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
109 fprintf( outfile, "\t.long 0x%08x\n", mask );
112 /* then the relay thunks */
114 fprintf( outfile, "\t.text\n" );
115 fprintf( outfile, "__wine_spec_relay_entry_points:\n" );
116 fprintf( outfile, "\tnop\n" ); /* to avoid 0 offset */
118 for (i = spec->base; i <= spec->limit; i++)
120 ORDDEF *odp = spec->ordinals[i];
122 if (!needs_relay( odp )) continue;
124 fprintf( outfile, "\t.align %d\n", get_alignment(4) );
125 fprintf( outfile, ".L__wine_spec_relay_entry_point_%d:\n", i );
127 if (odp->flags & FLAG_REGISTER)
128 fprintf( outfile, "\tpushl %%eax\n" );
129 else
130 fprintf( outfile, "\tpushl %%esp\n" );
132 args = strlen(odp->u.func.arg_types);
133 flags = 0;
134 if (odp->flags & FLAG_RET64) flags |= 1;
135 if (odp->type == TYPE_STDCALL) flags |= 2;
136 fprintf( outfile, "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
138 if (UsePIC)
140 fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
141 fprintf( outfile, "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
143 else fprintf( outfile, "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
144 fprintf( outfile, "\tpushl %%eax\n" );
146 if (odp->flags & FLAG_REGISTER)
148 fprintf( outfile, "\tcall *8(%%eax)\n" );
150 else
152 fprintf( outfile, "\tcall *4(%%eax)\n" );
153 if (odp->type == TYPE_STDCALL)
154 fprintf( outfile, "\tret $%u\n", args * get_ptr_size() );
155 else
156 fprintf( outfile, "\tret\n" );
161 /*******************************************************************
162 * output_exports
164 * Output the export table for a Win32 module.
166 static void output_exports( FILE *outfile, DLLSPEC *spec )
168 int i, fwd_size = 0;
169 int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
171 if (!nr_exports) return;
173 fprintf( outfile, "\n/* export table */\n\n" );
174 fprintf( outfile, "\t.data\n" );
175 fprintf( outfile, "\t.align %d\n", get_alignment(4) );
176 fprintf( outfile, ".L__wine_spec_exports:\n" );
178 /* export directory header */
180 fprintf( outfile, "\t.long 0\n" ); /* Characteristics */
181 fprintf( outfile, "\t.long 0\n" ); /* TimeDateStamp */
182 fprintf( outfile, "\t.long 0\n" ); /* MajorVersion/MinorVersion */
183 fprintf( outfile, "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
184 fprintf( outfile, "\t.long %u\n", spec->base ); /* Base */
185 fprintf( outfile, "\t.long %u\n", nr_exports ); /* NumberOfFunctions */
186 fprintf( outfile, "\t.long %u\n", spec->nb_names ); /* NumberOfNames */
187 fprintf( outfile, "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
188 if (spec->nb_names)
190 fprintf( outfile, "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
191 fprintf( outfile, "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" ); /* AddressOfNameOrdinals */
193 else
195 fprintf( outfile, "\t.long 0\n" ); /* AddressOfNames */
196 fprintf( outfile, "\t.long 0\n" ); /* AddressOfNameOrdinals */
199 /* output the function pointers */
201 fprintf( outfile, "\n.L__wine_spec_exports_funcs:\n" );
202 for (i = spec->base; i <= spec->limit; i++)
204 ORDDEF *odp = spec->ordinals[i];
205 if (!odp) fprintf( outfile, "\t.long 0\n" );
206 else switch(odp->type)
208 case TYPE_EXTERN:
209 case TYPE_STDCALL:
210 case TYPE_VARARGS:
211 case TYPE_CDECL:
212 if (odp->flags & FLAG_FORWARD)
214 fprintf( outfile, "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
215 fwd_size += strlen(odp->link_name) + 1;
217 else if (odp->flags & FLAG_EXT_LINK)
219 fprintf( outfile, "\t%s %s_%s\n",
220 get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
222 else
224 fprintf( outfile, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
226 break;
227 case TYPE_STUB:
228 fprintf( outfile, "\t%s %s\n", get_asm_ptr_keyword(),
229 asm_name( get_stub_name( odp, spec )) );
230 break;
231 default:
232 assert(0);
236 if (spec->nb_names)
238 /* output the function name pointers */
240 int namepos = strlen(spec->file_name) + 1;
242 fprintf( outfile, "\n.L__wine_spec_exp_name_ptrs:\n" );
243 for (i = 0; i < spec->nb_names; i++)
245 fprintf( outfile, "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
246 namepos += strlen(spec->names[i]->name) + 1;
249 /* output the function ordinals */
251 fprintf( outfile, "\n.L__wine_spec_exp_ordinals:\n" );
252 for (i = 0; i < spec->nb_names; i++)
254 fprintf( outfile, "\t%s %d\n",
255 get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
257 if (spec->nb_names % 2)
259 fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );
263 /* output the export name strings */
265 fprintf( outfile, "\n.L__wine_spec_exp_names:\n" );
266 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
267 for (i = 0; i < spec->nb_names; i++)
268 fprintf( outfile, "\t%s \"%s\"\n",
269 get_asm_string_keyword(), spec->names[i]->name );
271 /* output forward strings */
273 if (fwd_size)
275 fprintf( outfile, "\n.L__wine_spec_forwards:\n" );
276 for (i = spec->base; i <= spec->limit; i++)
278 ORDDEF *odp = spec->ordinals[i];
279 if (odp && (odp->flags & FLAG_FORWARD))
280 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
283 fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
284 fprintf( outfile, ".L__wine_spec_exports_end:\n" );
286 /* output relays */
288 /* we only support relay debugging on i386 */
289 if (target_cpu != CPU_x86)
291 fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );
292 return;
295 fprintf( outfile, ".L__wine_spec_relay_descr:\n" );
296 fprintf( outfile, "\t%s 0xdeb90001\n", get_asm_ptr_keyword() ); /* magic */
297 fprintf( outfile, "\t%s 0,0\n", get_asm_ptr_keyword() ); /* relay funcs */
298 fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() ); /* private data */
299 fprintf( outfile, "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
300 fprintf( outfile, "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
301 fprintf( outfile, "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
303 output_relay_debug( outfile, spec );
307 /*******************************************************************
308 * output_asm_constructor
310 * Output code for calling a dll constructor.
312 static void output_asm_constructor( FILE *outfile, const char *constructor )
314 if (target_platform == PLATFORM_APPLE)
316 /* Mach-O doesn't have an init section */
317 fprintf( outfile, "\n\t.mod_init_func\n" );
318 fprintf( outfile, "\t.align %d\n", get_alignment(4) );
319 fprintf( outfile, "\t.long %s\n", asm_name(constructor) );
321 else
323 fprintf( outfile, "\n\t.section \".init\",\"ax\"\n" );
324 switch(target_cpu)
326 case CPU_x86:
327 case CPU_x86_64:
328 fprintf( outfile, "\tcall %s\n", asm_name(constructor) );
329 break;
330 case CPU_SPARC:
331 fprintf( outfile, "\tcall %s\n", asm_name(constructor) );
332 fprintf( outfile, "\tnop\n" );
333 break;
334 case CPU_ALPHA:
335 fprintf( outfile, "\tjsr $26,%s\n", asm_name(constructor) );
336 break;
337 case CPU_POWERPC:
338 fprintf( outfile, "\tbl %s\n", asm_name(constructor) );
339 break;
345 /*******************************************************************
346 * BuildSpec32File
348 * Build a Win32 C file from a spec file.
350 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
352 int machine = 0;
353 unsigned int page_size = get_page_size();
355 resolve_imports( spec );
356 output_standard_file_header( outfile );
358 /* Reserve some space for the PE header */
360 fprintf( outfile, "\t.text\n" );
361 fprintf( outfile, "\t.align %d\n", get_alignment(page_size) );
362 fprintf( outfile, "__wine_spec_pe_header:\n" );
363 if (target_platform == PLATFORM_APPLE)
364 fprintf( outfile, "\t.space 65536\n" );
365 else
366 fprintf( outfile, "\t.skip 65536\n" );
368 /* Output the NT header */
370 fprintf( outfile, "\n\t.data\n" );
371 fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
372 fprintf( outfile, "%s\n", asm_globl("__wine_spec_nt_header") );
373 fprintf( outfile, ".L__wine_spec_rva_base:\n" );
375 fprintf( outfile, "\t.long 0x%04x\n", IMAGE_NT_SIGNATURE ); /* Signature */
376 switch(target_cpu)
378 case CPU_x86: machine = IMAGE_FILE_MACHINE_I386; break;
379 case CPU_x86_64: machine = IMAGE_FILE_MACHINE_AMD64; break;
380 case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
381 case CPU_ALPHA: machine = IMAGE_FILE_MACHINE_ALPHA; break;
382 case CPU_SPARC: machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
384 fprintf( outfile, "\t%s 0x%04x\n", /* Machine */
385 get_asm_short_keyword(), machine );
386 fprintf( outfile, "\t%s 0\n", /* NumberOfSections */
387 get_asm_short_keyword() );
388 fprintf( outfile, "\t.long 0\n" ); /* TimeDateStamp */
389 fprintf( outfile, "\t.long 0\n" ); /* PointerToSymbolTable */
390 fprintf( outfile, "\t.long 0\n" ); /* NumberOfSymbols */
391 fprintf( outfile, "\t%s %d\n", /* SizeOfOptionalHeader */
392 get_asm_short_keyword(),
393 get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
394 fprintf( outfile, "\t%s 0x%04x\n", /* Characteristics */
395 get_asm_short_keyword(), spec->characteristics );
396 fprintf( outfile, "\t%s 0x%04x\n", /* Magic */
397 get_asm_short_keyword(),
398 get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
399 fprintf( outfile, "\t.byte 0\n" ); /* MajorLinkerVersion */
400 fprintf( outfile, "\t.byte 0\n" ); /* MinorLinkerVersion */
401 fprintf( outfile, "\t.long 0\n" ); /* SizeOfCode */
402 fprintf( outfile, "\t.long 0\n" ); /* SizeOfInitializedData */
403 fprintf( outfile, "\t.long 0\n" ); /* SizeOfUninitializedData */
404 /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
405 fprintf( outfile, "\t%s %s\n", /* AddressOfEntryPoint */
406 get_asm_ptr_keyword(), asm_name(spec->init_func) );
407 if (get_ptr_size() == 4)
409 fprintf( outfile, "\t.long 0\n" ); /* BaseOfCode */
410 fprintf( outfile, "\t.long 0\n" ); /* BaseOfData */
412 fprintf( outfile, "\t%s __wine_spec_pe_header\n", /* ImageBase */
413 get_asm_ptr_keyword() );
414 fprintf( outfile, "\t.long %u\n", page_size ); /* SectionAlignment */
415 fprintf( outfile, "\t.long %u\n", page_size ); /* FileAlignment */
416 fprintf( outfile, "\t%s 1,0\n", /* Major/MinorOperatingSystemVersion */
417 get_asm_short_keyword() );
418 fprintf( outfile, "\t%s 0,0\n", /* Major/MinorImageVersion */
419 get_asm_short_keyword() );
420 fprintf( outfile, "\t%s %u,%u\n", /* Major/MinorSubsystemVersion */
421 get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
422 fprintf( outfile, "\t.long 0\n" ); /* Win32VersionValue */
423 fprintf( outfile, "\t.long %s-.L__wine_spec_rva_base\n", /* SizeOfImage */
424 asm_name("_end") );
425 fprintf( outfile, "\t.long %u\n", page_size ); /* SizeOfHeaders */
426 fprintf( outfile, "\t.long 0\n" ); /* CheckSum */
427 fprintf( outfile, "\t%s 0x%04x\n", /* Subsystem */
428 get_asm_short_keyword(), spec->subsystem );
429 fprintf( outfile, "\t%s 0\n", /* DllCharacteristics */
430 get_asm_short_keyword() );
431 fprintf( outfile, "\t%s %u,%u\n", /* SizeOfStackReserve/Commit */
432 get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
433 fprintf( outfile, "\t%s %u,%u\n", /* SizeOfHeapReserve/Commit */
434 get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
435 fprintf( outfile, "\t.long 0\n" ); /* LoaderFlags */
436 fprintf( outfile, "\t.long 16\n" ); /* NumberOfRvaAndSizes */
438 if (spec->base <= spec->limit) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
439 fprintf( outfile, "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
440 ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
441 else
442 fprintf( outfile, "\t.long 0,0\n" );
444 if (has_imports()) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
445 fprintf( outfile, "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
446 ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
447 else
448 fprintf( outfile, "\t.long 0,0\n" );
450 if (spec->nb_resources) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
451 fprintf( outfile, "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
452 ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
453 else
454 fprintf( outfile, "\t.long 0,0\n" );
456 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[3] */
457 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[4] */
458 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[5] */
459 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[6] */
460 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[7] */
461 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[8] */
462 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[9] */
463 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[10] */
464 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[11] */
465 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[12] */
466 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[13] */
467 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[14] */
468 fprintf( outfile, "\t.long 0,0\n" ); /* DataDirectory[15] */
470 fprintf( outfile, "\n\t%s\n", get_asm_string_section() );
471 fprintf( outfile, "%s\n", asm_globl("__wine_spec_file_name") );
472 fprintf( outfile, ".L__wine_spec_file_name:\n" );
473 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
474 if (target_platform == PLATFORM_APPLE)
475 fprintf( outfile, "\t.lcomm %s,4\n", asm_name("_end") );
477 output_stubs( outfile, spec );
478 output_exports( outfile, spec );
479 output_imports( outfile, spec );
480 output_resources( outfile, spec );
481 output_asm_constructor( outfile, "__wine_spec_init_ctor" );
482 output_gnu_stack_note( outfile );
486 /*******************************************************************
487 * BuildDef32File
489 * Build a Win32 def file from a spec file.
491 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
493 const char *name;
494 int i, total;
496 if (spec_file_name)
497 fprintf( outfile, "; File generated automatically from %s; do not edit!\n\n",
498 spec_file_name );
499 else
500 fprintf( outfile, "; File generated automatically; do not edit!\n\n" );
502 fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
504 fprintf(outfile, "EXPORTS\n");
506 /* Output the exports and relay entry points */
508 for (i = total = 0; i < spec->nb_entry_points; i++)
510 const ORDDEF *odp = &spec->entry_points[i];
511 int is_data = 0;
513 if (!odp) continue;
515 if (odp->name) name = odp->name;
516 else if (odp->export_name) name = odp->export_name;
517 else continue;
519 if (!(odp->flags & FLAG_PRIVATE)) total++;
521 if (odp->type == TYPE_STUB) continue;
523 fprintf(outfile, " %s", name);
525 switch(odp->type)
527 case TYPE_EXTERN:
528 is_data = 1;
529 /* fall through */
530 case TYPE_VARARGS:
531 case TYPE_CDECL:
532 /* try to reduce output */
533 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
534 fprintf(outfile, "=%s", odp->link_name);
535 break;
536 case TYPE_STDCALL:
538 int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
539 if (!kill_at) fprintf(outfile, "@%d", at_param);
540 if (odp->flags & FLAG_FORWARD)
542 fprintf(outfile, "=%s", odp->link_name);
544 else if (strcmp(name, odp->link_name)) /* try to reduce output */
546 fprintf(outfile, "=%s", odp->link_name);
547 if (!kill_at) fprintf(outfile, "@%d", at_param);
549 break;
551 default:
552 assert(0);
554 fprintf( outfile, " @%d", odp->ordinal );
555 if (!odp->name) fprintf( outfile, " NONAME" );
556 if (is_data) fprintf( outfile, " DATA" );
557 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
558 fprintf( outfile, "\n" );
560 if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );