Unregister 16-bit dlls on module unload.
[wine/multimedia.git] / tools / winebuild / spec32.c
blob9fb749d376f1511a9a1bef433d4a6687065ee519
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
32 #include "winbase.h"
33 #include "wine/exception.h"
34 #include "build.h"
37 static int string_compare( const void *ptr1, const void *ptr2 )
39 const char * const *str1 = ptr1;
40 const char * const *str2 = ptr2;
41 return strcmp( *str1, *str2 );
45 /*******************************************************************
46 * make_internal_name
48 * Generate an internal name for an entry point. Used for stubs etc.
50 static const char *make_internal_name( const ORDDEF *odp, const char *prefix )
52 static char buffer[256];
53 if (odp->name[0])
55 char *p;
56 sprintf( buffer, "__wine_%s_%s_%s", prefix, DLLName, odp->name );
57 /* make sure name is a legal C identifier */
58 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
59 if (!*p) return buffer;
61 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(DLLName), odp->ordinal );
62 return buffer;
65 /*******************************************************************
66 * AssignOrdinals
68 * Assign ordinals to all entry points.
70 static void AssignOrdinals(void)
72 int i, ordinal;
74 if ( !nb_names ) return;
76 /* start assigning from Base, or from 1 if no ordinal defined yet */
77 if (Base == MAX_ORDINALS) Base = 1;
78 for (i = 0, ordinal = Base; i < nb_names; i++)
80 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
81 while (Ordinals[ordinal]) ordinal++;
82 if (ordinal >= MAX_ORDINALS)
84 current_line = Names[i]->lineno;
85 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
87 Names[i]->ordinal = ordinal;
88 Ordinals[ordinal] = Names[i];
90 if (ordinal > Limit) Limit = ordinal;
94 /*******************************************************************
95 * output_debug
97 * Output the debug channels.
99 static int output_debug( FILE *outfile )
101 int i;
103 if (!nb_debug_channels) return 0;
104 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
106 for (i = 0; i < nb_debug_channels; i++)
107 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
108 debug_channels[i], debug_channels[i] );
110 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
111 for (i = 0; i < nb_debug_channels; i++)
113 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
114 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
116 fprintf( outfile, "\n};\n\n" );
117 fprintf( outfile, "static void *debug_registration;\n\n" );
119 return nb_debug_channels;
123 /*******************************************************************
124 * output_exports
126 * Output the export table for a Win32 module.
128 static int output_exports( FILE *outfile, int nr_exports )
130 int i, fwd_size = 0, total_size = 0;
131 char *p;
133 if (!nr_exports) return 0;
135 fprintf( outfile, "asm(\".data\\n\"\n" );
136 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
137 fprintf( outfile, " \"" PREFIX "__wine_spec_exports:\\n\"\n" );
139 /* export directory header */
141 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
142 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
144 fprintf( outfile, " \"\\t.long " PREFIX "dllname\\n\"\n" ); /* Name */
145 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
146 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
148 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
149 if (nb_names)
151 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
152 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
154 else
156 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
157 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
159 total_size += 10 * sizeof(int);
161 /* output the function pointers */
163 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
164 for (i = Base; i <= Limit; i++)
166 ORDDEF *odp = Ordinals[i];
167 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
168 else switch(odp->type)
170 case TYPE_EXTERN:
171 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", odp->link_name );
172 break;
173 case TYPE_STDCALL:
174 case TYPE_VARARGS:
175 case TYPE_CDECL:
176 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n",
177 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
178 break;
179 case TYPE_STUB:
180 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "stub" ) );
181 break;
182 case TYPE_VARIABLE:
183 fprintf( outfile, " \"\\t.long " PREFIX "%s\\n\"\n", make_internal_name( odp, "var" ) );
184 break;
185 case TYPE_FORWARD:
186 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
187 fwd_size += strlen(odp->link_name) + 1;
188 break;
189 default:
190 assert(0);
193 total_size += (Limit - Base + 1) * sizeof(int);
195 if (nb_names)
197 /* output the function name pointers */
199 int namepos = 0;
201 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
202 for (i = 0; i < nb_names; i++)
204 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
205 namepos += strlen(Names[i]->name) + 1;
207 total_size += nb_names * sizeof(int);
209 /* output the function names */
211 fprintf( outfile, " \"\\t.text 1\\n\"\n" );
212 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
213 for (i = 0; i < nb_names; i++)
214 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
215 fprintf( outfile, " \"\\t.data\\n\"\n" );
217 /* output the function ordinals */
219 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
220 for (i = 0; i < nb_names; i++)
222 fprintf( outfile, " \"\\t.short %d\\n\"\n", Names[i]->ordinal - Base );
224 total_size += nb_names * sizeof(short);
225 if (nb_names % 2)
227 fprintf( outfile, " \"\\t.short 0\\n\"\n" );
228 total_size += sizeof(short);
232 /* output forward strings */
234 if (fwd_size)
236 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
237 for (i = Base; i <= Limit; i++)
239 ORDDEF *odp = Ordinals[i];
240 if (odp && odp->type == TYPE_FORWARD)
241 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
243 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
244 total_size += (fwd_size + 3) & ~3;
247 /* output relays */
249 if (debugging)
251 for (i = Base; i <= Limit; i++)
253 ORDDEF *odp = Ordinals[i];
254 unsigned int j, args, mask = 0;
255 const char *name;
257 /* skip non-existent entry points */
258 if (!odp) goto ignore;
259 /* skip non-functions */
260 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
261 /* skip norelay entry points */
262 if (odp->flags & FLAG_NORELAY) goto ignore;
264 for (j = 0; odp->u.func.arg_types[j]; j++)
266 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
267 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
269 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
271 name = odp->link_name;
272 args = strlen(odp->u.func.arg_types) * sizeof(int);
273 if (odp->flags & FLAG_REGISTER)
275 name = make_internal_name( odp, "regs" );
276 args |= 0x8000;
279 switch(odp->type)
281 case TYPE_STDCALL:
282 fprintf( outfile, " \"\\tjmp " PREFIX "%s\\n\"\n", name );
283 fprintf( outfile, " \"\\tret $0x%04x\\n\"\n", args );
284 fprintf( outfile, " \"\\t.long " PREFIX "%s,0x%08x\\n\"\n", name, mask );
285 break;
286 case TYPE_CDECL:
287 fprintf( outfile, " \"\\tjmp " PREFIX "%s\\n\"\n", name );
288 fprintf( outfile, " \"\\tret\\n\"\n" );
289 fprintf( outfile, " \"\\t.short 0x%04x\\n\"\n", args );
290 fprintf( outfile, " \"\\t.long " PREFIX "%s,0x%08x\\n\"\n", name, mask );
291 break;
292 default:
293 assert(0);
295 continue;
297 ignore:
298 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
303 /* output __wine_dllexport symbols */
305 for (i = 0; i < nb_names; i++)
307 if (Names[i]->flags & FLAG_NOIMPORT) continue;
308 /* check for invalid characters in the name */
309 for (p = Names[i]->name; *p; p++)
310 if (!isalnum(*p) && *p != '_' && *p != '.') break;
311 if (*p) continue;
312 fprintf( outfile, " \"\\t.globl " PREFIX "__wine_dllexport_%s_%s\\n\"\n",
313 make_c_identifier(DLLName), Names[i]->name );
314 fprintf( outfile, " \"" PREFIX "__wine_dllexport_%s_%s:\\n\"\n",
315 make_c_identifier(DLLName), Names[i]->name );
317 fprintf( outfile, " \"\\t.long 0xffffffff\\n\"\n" );
319 /* output variables */
321 for (i = 0; i < nb_entry_points; i++)
323 ORDDEF *odp = EntryPoints[i];
324 if (odp->type == TYPE_VARIABLE)
326 int j;
327 fprintf( outfile, " \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
328 fprintf( outfile, " \"\\t.long " );
329 for (j = 0; j < odp->u.var.n_values; j++)
331 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
332 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
334 fprintf( outfile, "\\n\"\n" );
338 fprintf( outfile, ");\n\n" );
340 return total_size;
344 /*******************************************************************
345 * output_stub_funcs
347 * Output the functions for stub entry points
349 static void output_stub_funcs( FILE *outfile )
351 int i;
353 for (i = 0; i < nb_entry_points; i++)
355 ORDDEF *odp = EntryPoints[i];
356 if (odp->type != TYPE_STUB) continue;
357 fprintf( outfile, "#ifdef __GNUC__\n" );
358 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
359 fprintf( outfile, "#endif\n\n" );
360 fprintf( outfile, "struct exc_record {\n" );
361 fprintf( outfile, " unsigned int code, flags;\n" );
362 fprintf( outfile, " void *rec, *addr;\n" );
363 fprintf( outfile, " unsigned int params;\n" );
364 fprintf( outfile, " const void *info[15];\n" );
365 fprintf( outfile, "};\n\n" );
366 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
367 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
368 fprintf( outfile, " struct exc_record rec;\n" );
369 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
370 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
371 fprintf( outfile, " rec.rec = 0;\n" );
372 fprintf( outfile, " rec.params = 2;\n" );
373 fprintf( outfile, " rec.info[0] = dllname;\n" );
374 fprintf( outfile, " rec.info[1] = func;\n" );
375 fprintf( outfile, "#ifdef __GNUC__\n" );
376 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
377 fprintf( outfile, "#else\n" );
378 fprintf( outfile, " rec.addr = 0;\n" );
379 fprintf( outfile, "#endif\n" );
380 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
381 break;
384 for (i = 0; i < nb_entry_points; i++)
386 ORDDEF *odp = EntryPoints[i];
387 if (odp->type != TYPE_STUB) continue;
388 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
389 if (odp->name[0])
390 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
391 else
392 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
397 /*******************************************************************
398 * output_register_funcs
400 * Output the functions for register entry points
402 static void output_register_funcs( FILE *outfile )
404 const char *name;
405 int i;
407 for (i = 0; i < nb_entry_points; i++)
409 ORDDEF *odp = EntryPoints[i];
410 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
411 if (!(odp->flags & FLAG_REGISTER)) continue;
412 name = make_internal_name( odp, "regs" );
413 fprintf( outfile,
414 "asm(\".align %d\\n\\t\"\n"
415 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
416 " \"" PREFIX "%s:\\n\\t\"\n"
417 " \"call " PREFIX "__wine_call_from_32_regs\\n\\t\"\n"
418 " \".long " PREFIX "%s\\n\\t\"\n"
419 " \".byte %d,%d\");\n",
420 get_alignment(4),
421 name, name, odp->link_name,
422 strlen(odp->u.func.arg_types) * sizeof(int),
423 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
428 /*******************************************************************
429 * output_dll_init
431 * Output code for calling a dll constructor and destructor.
433 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
435 fprintf( outfile, "#ifndef __GNUC__\n" );
436 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
437 fprintf( outfile, "#endif\n" );
439 #if defined(__i386__)
440 if (constructor)
442 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
443 fprintf( outfile, " \"\\tcall " PREFIX "%s\\n\"\n", constructor );
444 fprintf( outfile, " \"\\t.previous\\n\");\n" );
446 if (destructor)
448 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
449 fprintf( outfile, " \"\\tcall " PREFIX "%s\\n\"\n", destructor );
450 fprintf( outfile, " \"\\t.previous\\n\");\n" );
452 #elif defined(__sparc__)
453 if (constructor)
455 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
456 fprintf( outfile, " \"\\tcall " PREFIX "%s\\n\"\n", constructor );
457 fprintf( outfile, " \"\\tnop\\n\"\n" );
458 fprintf( outfile, " \"\\t.previous\\n\");\n" );
460 if (destructor)
462 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
463 fprintf( outfile, " \"\\tcall " PREFIX "%s\\n\"\n", destructor );
464 fprintf( outfile, " \"\\tnop\\n\"\n" );
465 fprintf( outfile, " \"\\t.previous\\n\");\n" );
467 #elif defined(__PPC__)
468 if (constructor)
470 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
471 fprintf( outfile, " \"\\tbl " PREFIX "%s\\n\"\n", constructor );
472 fprintf( outfile, " \"\\t.previous\\n\");\n" );
474 if (destructor)
476 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
477 fprintf( outfile, " \"\\tbl " PREFIX "%s\\n\"\n", destructor );
478 DLLName );
479 fprintf( outfile, " \"\\t.previous\\n\");\n" );
481 #else
482 #error You need to define the DLL constructor for your architecture
483 #endif
484 fprintf( outfile, "#ifndef __GNUC__\n" );
485 fprintf( outfile, "}\n" );
486 fprintf( outfile, "#endif\n" );
490 /*******************************************************************
491 * BuildSpec32File
493 * Build a Win32 C file from a spec file.
495 void BuildSpec32File( FILE *outfile )
497 int exports_size = 0;
498 int nr_exports, nr_imports, nr_resources;
499 int characteristics, subsystem;
500 DWORD page_size;
501 char constructor[100];
503 #ifdef HAVE_GETPAGESIZE
504 page_size = getpagesize();
505 #elif defined(__svr4__)
506 page_size = sysconf(_SC_PAGESIZE);
507 #elif defined(_WINDOWS)
509 SYSTEM_INFO si;
510 GetSystemInfo(&si);
511 page_size = si.dwPageSize;
513 #else
514 # error Cannot get the page size on this platform
515 #endif
517 AssignOrdinals();
518 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
520 resolve_imports();
521 output_standard_file_header( outfile );
523 /* Reserve some space for the PE header */
525 fprintf( outfile, "extern char pe_header[];\n" );
526 fprintf( outfile, "asm(\".section .text\\n\\t\"\n" );
527 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
528 fprintf( outfile, " \"" PREFIX "pe_header:\\t.fill %ld,1,0\\n\\t\");\n", page_size );
530 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
531 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
533 #ifdef __i386__
534 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
535 #else
536 fprintf( outfile, "#define __stdcall\n\n" );
537 #endif
539 if (nr_exports)
541 /* Output the stub functions */
543 output_stub_funcs( outfile );
545 fprintf( outfile, "#ifndef __GNUC__\n" );
546 fprintf( outfile, "static void __asm__dummy(void) {\n" );
547 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
549 /* Output code for all register functions */
551 output_register_funcs( outfile );
553 /* Output the exports and relay entry points */
555 exports_size = output_exports( outfile, nr_exports );
557 fprintf( outfile, "#ifndef __GNUC__\n" );
558 fprintf( outfile, "}\n" );
559 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
562 /* Output the DLL imports */
564 nr_imports = output_imports( outfile );
566 /* Output the resources */
568 nr_resources = output_resources( outfile );
570 /* Output LibMain function */
572 characteristics = subsystem = 0;
573 switch(SpecMode)
575 case SPEC_MODE_DLL:
576 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
577 characteristics = IMAGE_FILE_DLL;
578 break;
579 case SPEC_MODE_GUIEXE:
580 if (!init_func) init_func = "WinMain";
581 fprintf( outfile,
582 "\ntypedef struct {\n"
583 " unsigned int cb;\n"
584 " char *lpReserved, *lpDesktop, *lpTitle;\n"
585 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
586 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
587 " unsigned short wShowWindow, cbReserved2;\n"
588 " char *lpReserved2;\n"
589 " void *hStdInput, *hStdOutput, *hStdError;\n"
590 "} STARTUPINFOA;\n"
591 "int _ARGC;\n"
592 "char **_ARGV;\n"
593 "extern int __stdcall %s(void *,void *,char *,int);\n"
594 "extern char * __stdcall GetCommandLineA(void);\n"
595 "extern void * __stdcall GetModuleHandleA(char *);\n"
596 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
597 "extern void __stdcall ExitProcess(unsigned int);\n"
598 "static void __wine_exe_main(void)\n"
599 "{\n"
600 " extern int __wine_main_argc;\n"
601 " extern char **__wine_main_argv;\n"
602 " STARTUPINFOA info;\n"
603 " char *cmdline = GetCommandLineA();\n"
604 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
605 " if (*cmdline) cmdline++;\n"
606 " GetStartupInfoA( &info );\n"
607 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
608 " _ARGC = __wine_main_argc;\n"
609 " _ARGV = __wine_main_argv;\n"
610 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
611 "}\n\n", init_func, init_func );
612 init_func = "__wine_exe_main";
613 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
614 break;
615 case SPEC_MODE_GUIEXE_UNICODE:
616 if (!init_func) init_func = "WinMain";
617 fprintf( outfile,
618 "\ntypedef unsigned short WCHAR;\n"
619 "typedef struct {\n"
620 " unsigned int cb;\n"
621 " char *lpReserved, *lpDesktop, *lpTitle;\n"
622 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
623 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
624 " unsigned short wShowWindow, cbReserved2;\n"
625 " char *lpReserved2;\n"
626 " void *hStdInput, *hStdOutput, *hStdError;\n"
627 "} STARTUPINFOA;\n"
628 "int _ARGC;\n"
629 "WCHAR **_ARGV;\n"
630 "extern int __stdcall %s(void *,void *,char *,int);\n"
631 "extern char * __stdcall GetCommandLineA(void);\n"
632 "extern void * __stdcall GetModuleHandleA(char *);\n"
633 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
634 "extern void __stdcall ExitProcess(unsigned int);\n"
635 "static void __wine_exe_main(void)\n"
636 "{\n"
637 " extern int __wine_main_argc;\n"
638 " extern WCHAR **__wine_main_wargv;\n"
639 " STARTUPINFOA info;\n"
640 " char *cmdline = GetCommandLineA();\n"
641 " while (*cmdline && *cmdline != ' ') cmdline++;\n"
642 " if (*cmdline) cmdline++;\n"
643 " GetStartupInfoA( &info );\n"
644 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
645 " _ARGC = __wine_main_argc;\n"
646 " _ARGV = __wine_main_wargv;\n"
647 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
648 "}\n\n", init_func, init_func );
649 init_func = "__wine_exe_main";
650 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
651 break;
652 case SPEC_MODE_CUIEXE:
653 if (!init_func) init_func = "main";
654 fprintf( outfile,
655 "\nint _ARGC;\n"
656 "char **_ARGV;\n"
657 "extern void __stdcall ExitProcess(int);\n"
658 "static void __wine_exe_main(void)\n"
659 "{\n"
660 " extern int %s( int argc, char *argv[] );\n"
661 " extern int __wine_main_argc;\n"
662 " extern char **__wine_main_argv;\n"
663 " _ARGC = __wine_main_argc;\n"
664 " _ARGV = __wine_main_argv;\n"
665 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
666 "}\n\n", init_func, init_func );
667 init_func = "__wine_exe_main";
668 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
669 break;
670 case SPEC_MODE_CUIEXE_UNICODE:
671 if (!init_func) init_func = "wmain";
672 fprintf( outfile,
673 "\ntypedef unsigned short WCHAR;\n"
674 "int _ARGC;\n"
675 "WCHAR **_ARGV;\n"
676 "extern void __stdcall ExitProcess(int);\n"
677 "static void __wine_exe_main(void)\n"
678 "{\n"
679 " extern int %s( int argc, WCHAR *argv[] );\n"
680 " extern int __wine_main_argc;\n"
681 " extern WCHAR **__wine_main_wargv;\n"
682 " _ARGC = __wine_main_argc;\n"
683 " _ARGV = __wine_main_wargv;\n"
684 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
685 "}\n\n", init_func, init_func );
686 init_func = "__wine_exe_main";
687 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
688 break;
691 /* Output the NT header */
693 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
694 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
695 fprintf( outfile, " int Signature;\n" );
696 fprintf( outfile, " struct file_header {\n" );
697 fprintf( outfile, " short Machine;\n" );
698 fprintf( outfile, " short NumberOfSections;\n" );
699 fprintf( outfile, " int TimeDateStamp;\n" );
700 fprintf( outfile, " void *PointerToSymbolTable;\n" );
701 fprintf( outfile, " int NumberOfSymbols;\n" );
702 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
703 fprintf( outfile, " short Characteristics;\n" );
704 fprintf( outfile, " } FileHeader;\n" );
705 fprintf( outfile, " struct opt_header {\n" );
706 fprintf( outfile, " short Magic;\n" );
707 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
708 fprintf( outfile, " int SizeOfCode;\n" );
709 fprintf( outfile, " int SizeOfInitializedData;\n" );
710 fprintf( outfile, " int SizeOfUninitializedData;\n" );
711 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
712 fprintf( outfile, " void *BaseOfCode;\n" );
713 fprintf( outfile, " void *BaseOfData;\n" );
714 fprintf( outfile, " void *ImageBase;\n" );
715 fprintf( outfile, " int SectionAlignment;\n" );
716 fprintf( outfile, " int FileAlignment;\n" );
717 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
718 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
719 fprintf( outfile, " short MajorImageVersion;\n" );
720 fprintf( outfile, " short MinorImageVersion;\n" );
721 fprintf( outfile, " short MajorSubsystemVersion;\n" );
722 fprintf( outfile, " short MinorSubsystemVersion;\n" );
723 fprintf( outfile, " int Win32VersionValue;\n" );
724 fprintf( outfile, " int SizeOfImage;\n" );
725 fprintf( outfile, " int SizeOfHeaders;\n" );
726 fprintf( outfile, " int CheckSum;\n" );
727 fprintf( outfile, " short Subsystem;\n" );
728 fprintf( outfile, " short DllCharacteristics;\n" );
729 fprintf( outfile, " int SizeOfStackReserve;\n" );
730 fprintf( outfile, " int SizeOfStackCommit;\n" );
731 fprintf( outfile, " int SizeOfHeapReserve;\n" );
732 fprintf( outfile, " int SizeOfHeapCommit;\n" );
733 fprintf( outfile, " int LoaderFlags;\n" );
734 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
735 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
736 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
737 fprintf( outfile, " } OptionalHeader;\n" );
738 fprintf( outfile, "} nt_header = {\n" );
739 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
741 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
742 fprintf( outfile, " 0, 0, 0, 0,\n" );
743 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
744 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
746 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
747 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
748 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
749 fprintf( outfile, " %s,\n", init_func ? init_func : "0" ); /* AddressOfEntryPoint */
750 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
751 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
752 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
753 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
754 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
755 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
756 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
757 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
758 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
759 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
760 fprintf( outfile, " 0,\n" ); /* CheckSum */
761 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
762 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
763 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
764 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
765 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
766 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
767 fprintf( outfile, " {\n" );
768 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
769 exports_size ? "__wine_spec_exports" : "0", exports_size );
770 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
771 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
772 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
773 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
774 fprintf( outfile, " }\n }\n};\n\n" );
776 /* Output the DLL constructor */
778 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLName) );
779 output_dll_init( outfile, constructor, NULL );
781 fprintf( outfile,
782 "void %s(void)\n"
783 "{\n"
784 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
785 " extern void *__wine_dbg_register( char * const *, int );\n"
786 " __wine_dll_register( &nt_header, \"%s\" );\n"
787 "}\n",
788 constructor, DLLFileName );
792 /*******************************************************************
793 * BuildDef32File
795 * Build a Win32 def file from a spec file.
797 void BuildDef32File(FILE *outfile)
799 int i;
801 AssignOrdinals();
803 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
804 input_file_name );
806 fprintf(outfile, "LIBRARY %s\n\n", DLLFileName);
808 fprintf(outfile, "EXPORTS\n");
810 /* Output the exports and relay entry points */
812 for(i = 0; i < nb_entry_points; i++)
814 ORDDEF *odp = EntryPoints[i];
815 if(!odp || !*odp->name || (odp->flags & FLAG_NOIMPORT)) continue;
816 if (odp->type == TYPE_STUB) continue;
818 fprintf(outfile, " %s", odp->name);
820 switch(odp->type)
822 case TYPE_EXTERN:
823 case TYPE_VARARGS:
824 case TYPE_CDECL:
825 case TYPE_VARIABLE:
826 /* try to reduce output */
827 if(strcmp(odp->name, odp->link_name))
828 fprintf(outfile, "=%s", odp->link_name);
829 break;
830 case TYPE_STDCALL:
832 #ifdef NEED_STDCALL_DECORATION
833 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
834 fprintf(outfile, "@%d", at_param);
835 #endif /* NEED_STDCALL_DECORATION */
836 /* try to reduce output */
837 if(strcmp(odp->name, odp->link_name))
839 fprintf(outfile, "=%s", odp->link_name);
840 #ifdef NEED_STDCALL_DECORATION
841 fprintf(outfile, "@%d", at_param);
842 #endif /* NEED_STDCALL_DECORATION */
844 break;
846 case TYPE_FORWARD:
847 fprintf(outfile, "=%s", odp->link_name);
848 break;
849 default:
850 assert(0);
852 fprintf(outfile, " @%d\n", odp->ordinal);
857 /*******************************************************************
858 * BuildDebugFile
860 * Build the debugging channels source file.
862 void BuildDebugFile( FILE *outfile )
864 int nr_debug;
865 char *prefix, *p;
867 output_standard_file_header( outfile );
868 nr_debug = output_debug( outfile );
869 if (!nr_debug)
871 fprintf( outfile, "/* no debug channels found for this module */\n" );
872 return;
875 if (output_file_name)
877 if ((p = strrchr( output_file_name, '/' ))) p++;
878 prefix = xstrdup( p ? p : output_file_name );
879 if ((p = strchr( prefix, '.' ))) *p = 0;
880 strcpy( p, make_c_identifier(p) );
882 else prefix = xstrdup( "_" );
884 /* Output the DLL constructor */
886 fprintf( outfile,
887 "#ifdef __GNUC__\n"
888 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
889 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
890 "#else\n"
891 "static void __asm__dummy_dll_init(void) {\n",
892 prefix, prefix );
894 #if defined(__i386__)
895 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
896 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
897 fprintf( outfile, " \"\\t.previous\\n\");\n" );
898 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
899 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
900 fprintf( outfile, " \"\\t.previous\\n\");\n" );
901 #elif defined(__sparc__)
902 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
903 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
904 fprintf( outfile, " \"\\tnop\\n\"\n" );
905 fprintf( outfile, " \"\\t.previous\\n\");\n" );
906 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
907 fprintf( outfile, " \"\\tcall " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
908 fprintf( outfile, " \"\\tnop\\n\"\n" );
909 fprintf( outfile, " \"\\t.previous\\n\");\n" );
910 #elif defined(__PPC__)
911 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
912 fprintf( outfile, " \"\\tbl " PREFIX "__wine_dbg_%s_init\\n\"\n", prefix );
913 fprintf( outfile, " \"\\t.previous\\n\");\n" );
914 fprintf( outfile, "asm(\"\\t.section\t.fini ,\\\"ax\\\"\\n\"\n" );
915 fprintf( outfile, " \"\\tbl " PREFIX "__wine_dbg_%s_fini\\n\"\n", prefix );
916 fprintf( outfile, " \"\\t.previous\\n\");\n" );
917 #else
918 #error You need to define the DLL constructor for your architecture
919 #endif
920 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
922 fprintf( outfile,
923 "\n#ifdef __GNUC__\n"
924 "static\n"
925 "#endif\n"
926 "void __wine_dbg_%s_init(void)\n"
927 "{\n"
928 " extern void *__wine_dbg_register( char * const *, int );\n"
929 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
930 "}\n", prefix, nr_debug );
931 fprintf( outfile,
932 "\n#ifdef __GNUC__\n"
933 "static\n"
934 "#endif\n"
935 "void __wine_dbg_%s_fini(void)\n"
936 "{\n"
937 " extern void __wine_dbg_unregister( void* );\n"
938 " __wine_dbg_unregister( debug_registration );\n"
939 "}\n", prefix );
941 free( prefix );