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
18 static int name_compare( const void *name1, const void *name2 )
20 ORDDEF *odp1 = *(ORDDEF **)name1;
21 ORDDEF *odp2 = *(ORDDEF **)name2;
22 return strcmp( odp1->name, odp2->name );
25 /*******************************************************************
28 * Assign ordinals to all entry points.
30 static void AssignOrdinals(void)
34 if ( !nb_names ) return;
36 /* sort the list of names */
37 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
39 /* check for duplicate names */
40 for (i = 0; i < nb_names - 1; i++)
42 if (!strcmp( Names[i]->name, Names[i+1]->name ))
44 current_line = max( Names[i]->lineno, Names[i+1]->lineno );
45 fatal_error( "'%s' redefined (previous definition at line %d)\n",
46 Names[i]->name, min( Names[i]->lineno, Names[i+1]->lineno ) );
50 /* start assigning from Base, or from 1 if no ordinal defined yet */
51 if (Base == MAX_ORDINALS) Base = 1;
52 for (i = 0, ordinal = Base; i < nb_names; i++)
54 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
55 while (Ordinals[ordinal]) ordinal++;
56 if (ordinal >= MAX_ORDINALS)
58 current_line = Names[i]->lineno;
59 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
61 Names[i]->ordinal = ordinal;
62 Ordinals[ordinal] = Names[i];
64 if (ordinal > Limit) Limit = ordinal;
68 /*******************************************************************
71 * Output the export table for a Win32 module.
73 static void output_exports( FILE *outfile, int nr_exports, int nr_names, int fwd_size )
77 if (!nr_exports) return;
79 fprintf( outfile, "\n\n/* exports */\n\n" );
80 fprintf( outfile, "typedef void (*func_ptr)();\n" );
81 fprintf( outfile, "static struct {\n" );
82 fprintf( outfile, " struct {\n" );
83 fprintf( outfile, " unsigned int Characteristics;\n" );
84 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
85 fprintf( outfile, " unsigned short MajorVersion;\n" );
86 fprintf( outfile, " unsigned short MinorVersion;\n" );
87 fprintf( outfile, " const char *Name;\n" );
88 fprintf( outfile, " unsigned int Base;\n" );
89 fprintf( outfile, " unsigned int NumberOfFunctions;\n" );
90 fprintf( outfile, " unsigned int NumberOfNames;\n" );
91 fprintf( outfile, " func_ptr *AddressOfFunctions;\n" );
92 fprintf( outfile, " const char **AddressOfNames;\n" );
93 fprintf( outfile, " unsigned short *AddressOfNameOrdinals;\n" );
94 fprintf( outfile, " func_ptr functions[%d];\n", nr_exports );
97 fprintf( outfile, " const char *names[%d];\n", nb_names );
98 fprintf( outfile, " unsigned short ordinals[%d];\n", nb_names );
99 if (nb_names % 2) fprintf( outfile, " unsigned short pad1;\n" );
103 fprintf( outfile, " char forwards[%d];\n", (fwd_size + 3) & ~3 );
105 fprintf( outfile, " } exp;\n" );
108 fprintf( outfile, " struct {\n" );
109 fprintf( outfile, " unsigned char jmp;\n" );
110 fprintf( outfile, " unsigned char addr[4];\n" );
111 fprintf( outfile, " unsigned char ret;\n" );
112 fprintf( outfile, " unsigned short args;\n" );
113 fprintf( outfile, " func_ptr orig;\n" );
114 fprintf( outfile, " unsigned int argtypes;\n" );
115 fprintf( outfile, " } relay[%d];\n", nr_exports );
116 #endif /* __i386__ */
118 fprintf( outfile, "} exports = {\n {\n" );
119 fprintf( outfile, " 0,\n" ); /* Characteristics */
120 fprintf( outfile, " 0,\n" ); /* TimeDateStamp */
121 fprintf( outfile, " 0,\n" ); /* MajorVersion */
122 fprintf( outfile, " 0,\n" ); /* MinorVersion */
123 fprintf( outfile, " dllname,\n" ); /* Name */
124 fprintf( outfile, " %d,\n", Base ); /* Base */
125 fprintf( outfile, " %d,\n", nr_exports ); /* NumberOfFunctions */
126 fprintf( outfile, " %d,\n", nb_names ); /* NumberOfNames */
127 fprintf( outfile, " exports.exp.functions,\n" ); /* AddressOfFunctions */
130 fprintf( outfile, " exports.exp.names,\n" ); /* AddressOfNames */
131 fprintf( outfile, " exports.exp.ordinals,\n" ); /* AddressOfNameOrdinals */
135 fprintf( outfile, " 0,\n" ); /* AddressOfNames */
136 fprintf( outfile, " 0,\n" ); /* AddressOfNameOrdinals */
139 /* output the function addresses */
141 fprintf( outfile, " {\n " );
142 for (i = Base; i <= Limit; i++)
144 ORDDEF *odp = Ordinals[i];
145 if (!odp) fprintf( outfile, "0" );
146 else switch(odp->type)
149 fprintf( outfile, "%s", odp->u.ext.link_name );
155 fprintf( outfile, "%s", odp->u.func.link_name);
158 fprintf( outfile, "__stub_%d", i );
161 fprintf( outfile, "__regs_%d", i );
164 fprintf( outfile, "(func_ptr)&exports.exp.forwards[%d] /* %s */",
165 fwd_pos, odp->u.fwd.link_name );
166 fwd_pos += strlen(odp->u.fwd.link_name) + 1;
171 if (i < Limit) fprintf( outfile, ",\n " );
172 else fprintf( outfile, "\n },\n" );
177 /* output the function names */
179 fprintf( outfile, " {\n" );
180 for (i = 0; i < nb_names; i++)
182 if (i) fprintf( outfile, ",\n" );
183 fprintf( outfile, " \"%s\"", Names[i]->name );
185 fprintf( outfile, "\n },\n" );
187 /* output the function ordinals */
189 fprintf( outfile, " {\n " );
190 for (i = 0; i < nb_names; i++)
192 fprintf( outfile, "%4d", Names[i]->ordinal - Base );
195 fputc( ',', outfile );
196 if ((i % 8) == 7) fprintf( outfile, "\n " );
199 fprintf( outfile, "\n },\n" );
200 if (nb_names % 2) fprintf( outfile, " 0,\n" );
203 /* output forwards */
207 for (i = Base; i <= Limit; i++)
209 ORDDEF *odp = Ordinals[i];
210 if (odp && odp->type == TYPE_FORWARD)
211 fprintf( outfile, " \"%s\\0\"\n", odp->u.fwd.link_name );
218 fprintf( outfile, " },\n {\n" );
219 for (i = Base; i <= Limit; i++)
221 ORDDEF *odp = Ordinals[i];
223 if (odp && ((odp->type == TYPE_STDCALL) ||
224 (odp->type == TYPE_STDCALL64) ||
225 (odp->type == TYPE_CDECL) ||
226 (odp->type == TYPE_REGISTER)))
228 unsigned int j, mask = 0;
229 for (j = 0; odp->u.func.arg_types[j]; j++)
231 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
232 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
238 if (j < 16) mask |= 0x80000000;
241 fprintf( outfile, " { 0xe9, { 0,0,0,0 }, 0xc2, 0x%04x, %s, 0x%08x }",
242 strlen(odp->u.func.arg_types) * sizeof(int),
243 odp->u.func.link_name, mask );
246 fprintf( outfile, " { 0xe9, { 0,0,0,0 }, 0xc3, 0x%04x, %s, 0x%08x }",
247 strlen(odp->u.func.arg_types) * sizeof(int),
248 odp->u.func.link_name, mask );
251 fprintf( outfile, " { 0xe9, { 0,0,0,0 }, 0xc3, 0x%04x, __regs_%d, 0x%08x }",
252 0x8000 | (strlen(odp->u.func.arg_types) * sizeof(int)), i, mask );
258 else fprintf( outfile, " { 0, { 0,0,0,0 }, 0, 0, 0, 0 }" );
260 if (i < Limit) fprintf( outfile, ",\n" );
262 #endif /* __i386__ */
264 fprintf( outfile, " }\n};\n" );
268 /*******************************************************************
271 * Build a Win32 C file from a spec file.
273 void BuildSpec32File( FILE *outfile )
276 int i, fwd_size = 0, have_regs = FALSE;
277 int nr_exports, nr_imports;
278 int characteristics, subsystem;
279 const char *init_func;
282 #ifdef HAVE_GETPAGESIZE
283 page_size = getpagesize();
286 page_size = sysconf(_SC_PAGESIZE);
288 # error Cannot get the page size on this platform
293 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
295 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
297 fprintf( outfile, "extern void BUILTIN32_Unimplemented( const char *dllname, const char *funcname );\n\n" );
299 /* Reserve some space for the PE header */
301 fprintf( outfile, "extern char pe_header[];\n" );
302 fprintf( outfile, "asm(\".section .text\\n\\t\"\n" );
303 fprintf( outfile, " \".align %ld\\n\"\n", page_size );
304 fprintf( outfile, " \"pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size );
306 fprintf( outfile, "static const char dllname[] = \"%s\";\n", DLLName );
308 /* Output the DLL functions prototypes */
310 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
315 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
321 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
324 fwd_size += strlen(odp->u.fwd.link_name) + 1;
327 fprintf( outfile, "extern void __regs_%d();\n", odp->ordinal );
333 "static void __stub_%d() { BUILTIN32_Unimplemented(dllname,\"%s\"); }\n",
334 odp->ordinal, odp->name );
337 "static void __stub_%d() { BUILTIN32_Unimplemented(dllname,\"%d\"); }\n",
338 odp->ordinal, odp->ordinal );
342 fprintf(stderr,"build: function type %d not available for Win32\n",
348 /* Output code for all register functions */
352 fprintf( outfile, "#ifndef __GNUC__\n" );
353 fprintf( outfile, "static void __asm__dummy(void) {\n" );
354 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
355 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
357 if (odp->type != TYPE_REGISTER) continue;
359 "asm(\".align 4\\n\\t\"\n"
360 " \".type " PREFIX "__regs_%d,@function\\n\\t\"\n"
361 " \"" PREFIX "__regs_%d:\\n\\t\"\n"
362 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
363 " \".long " PREFIX "%s\\n\\t\"\n"
364 " \".byte %d,%d\");\n",
365 odp->ordinal, odp->ordinal, odp->u.func.link_name,
366 4 * strlen(odp->u.func.arg_types),
367 4 * strlen(odp->u.func.arg_types) );
369 fprintf( outfile, "#ifndef __GNUC__\n" );
370 fprintf( outfile, "}\n" );
371 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
374 /* Output the exports and relay entry points */
376 output_exports( outfile, nr_exports, nb_names, fwd_size );
378 /* Output the DLL imports */
380 nr_imports = output_imports( outfile );
382 /* Output LibMain function */
384 init_func = DLLInitFunc[0] ? DLLInitFunc : NULL;
385 characteristics = subsystem = 0;
389 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
390 characteristics = IMAGE_FILE_DLL;
392 case SPEC_MODE_GUIEXE:
393 if (!init_func) init_func = "WinMain";
395 "\n#include <winbase.h>\n"
396 "static void exe_main(void)\n"
398 " extern int PASCAL %s(HINSTANCE,HINSTANCE,LPSTR,INT);\n"
399 " STARTUPINFOA info;\n"
400 " LPSTR cmdline = GetCommandLineA();\n"
401 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
402 " if (*cmdline) cmdline++;\n"
403 " GetStartupInfoA( &info );\n"
404 " if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = 1;\n"
405 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
406 "}\n\n", init_func, init_func );
408 "int main( int argc, char *argv[] )\n"
410 " extern void PROCESS_InitWinelib( int, char ** );\n"
411 " PROCESS_InitWinelib( argc, argv );\n"
414 init_func = "exe_main";
415 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
417 case SPEC_MODE_CUIEXE:
418 if (!init_func) init_func = "wine_main";
420 "\n#include <winbase.h>\n"
421 "static void exe_main(void)\n"
423 " extern int %s( int argc, char *argv[] );\n"
424 " extern int _ARGC;\n"
425 " extern char **_ARGV;\n"
426 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
427 "}\n\n", init_func, init_func );
429 "int main( int argc, char *argv[] )\n"
431 " extern void PROCESS_InitWinelib( int, char ** );\n"
432 " PROCESS_InitWinelib( argc, argv );\n"
435 init_func = "exe_main";
436 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
438 case SPEC_MODE_GUIEXE_NO_MAIN:
439 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
440 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
442 case SPEC_MODE_CUIEXE_NO_MAIN:
443 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
444 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
448 /* Output the DLL descriptor */
450 if (rsrc_name[0]) fprintf( outfile, "extern char %s[];\n\n", rsrc_name );
452 /* Output the NT header */
454 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
455 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
456 fprintf( outfile, " int Signature;\n" );
457 fprintf( outfile, " struct file_header {\n" );
458 fprintf( outfile, " short Machine;\n" );
459 fprintf( outfile, " short NumberOfSections;\n" );
460 fprintf( outfile, " int TimeDateStamp;\n" );
461 fprintf( outfile, " void *PointerToSymbolTable;\n" );
462 fprintf( outfile, " int NumberOfSymbols;\n" );
463 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
464 fprintf( outfile, " short Characteristics;\n" );
465 fprintf( outfile, " } FileHeader;\n" );
466 fprintf( outfile, " struct opt_header {\n" );
467 fprintf( outfile, " short Magic;\n" );
468 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
469 fprintf( outfile, " int SizeOfCode;\n" );
470 fprintf( outfile, " int SizeOfInitializedData;\n" );
471 fprintf( outfile, " int SizeOfUninitializedData;\n" );
472 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
473 fprintf( outfile, " void *BaseOfCode;\n" );
474 fprintf( outfile, " void *BaseOfData;\n" );
475 fprintf( outfile, " void *ImageBase;\n" );
476 fprintf( outfile, " int SectionAlignment;\n" );
477 fprintf( outfile, " int FileAlignment;\n" );
478 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
479 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
480 fprintf( outfile, " short MajorImageVersion;\n" );
481 fprintf( outfile, " short MinorImageVersion;\n" );
482 fprintf( outfile, " short MajorSubsystemVersion;\n" );
483 fprintf( outfile, " short MinorSubsystemVersion;\n" );
484 fprintf( outfile, " int Win32VersionValue;\n" );
485 fprintf( outfile, " int SizeOfImage;\n" );
486 fprintf( outfile, " int SizeOfHeaders;\n" );
487 fprintf( outfile, " int CheckSum;\n" );
488 fprintf( outfile, " short Subsystem;\n" );
489 fprintf( outfile, " short DllCharacteristics;\n" );
490 fprintf( outfile, " int SizeOfStackReserve;\n" );
491 fprintf( outfile, " int SizeOfStackCommit;\n" );
492 fprintf( outfile, " int SizeOfHeapReserve;\n" );
493 fprintf( outfile, " int SizeOfHeapCommit;\n" );
494 fprintf( outfile, " int LoaderFlags;\n" );
495 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
496 fprintf( outfile, " struct { void *VirtualAddress; int Size; } DataDirectory[%d];\n",
497 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
498 fprintf( outfile, " } OptionalHeader;\n" );
499 fprintf( outfile, "} nt_header = {\n" );
500 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
502 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
503 fprintf( outfile, " 0, 0, 0, 0,\n" );
504 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
505 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
507 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
508 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
509 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
510 fprintf( outfile, " %s,\n", init_func ? init_func : "0" ); /* AddressOfEntryPoint */
511 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
512 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
513 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
514 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
515 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
516 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
517 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
518 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
519 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
520 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
521 fprintf( outfile, " 0,\n" ); /* CheckSum */
522 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
523 fprintf( outfile, " 0, 0, 0, 0, 0, 0,\n" );
524 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
525 fprintf( outfile, " {\n" );
526 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
527 nr_exports ? "&exports" : "0", nr_exports ? "sizeof(exports.exp)" : "0" );
528 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
529 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
530 fprintf( outfile, " { %s, 0 },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
531 rsrc_name[0] ? rsrc_name : "0" );
532 fprintf( outfile, " }\n }\n};\n\n" );
534 /* Output the DLL constructor */
536 fprintf( outfile, "#ifdef __GNUC__\n" );
537 fprintf( outfile, "static void %s_init(void) __attribute__((constructor));\n", DLLName );
538 fprintf( outfile, "#else /* defined(__GNUC__) */\n" );
539 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
540 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
541 fprintf( outfile, " \"\\tcall %s_init\\n\"\n", DLLName );
542 fprintf( outfile, " \"\\t.previous\\n\");\n" );
543 fprintf( outfile, "}\n" );
544 fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
545 fprintf( outfile, "static void %s_init(void)\n{\n", DLLName );
546 fprintf( outfile, " extern void BUILTIN32_RegisterDLL( const struct image_nt_headers *, const char * );\n" );
547 fprintf( outfile, " BUILTIN32_RegisterDLL( &nt_header, \"%s\" );\n}\n", DLLFileName );