Don't output register function code if it's a forwarded entry point.
[wine/multimedia.git] / tools / winebuild / spec32.c
bloba5e9462b985653eedba3c7477f70af43fb3b311b
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 || odp->export_name)
55 char *p;
56 sprintf( buffer, "__wine_%s_%s_%s", prefix, dll_file_name,
57 odp->name ? odp->name : odp->export_name );
58 /* make sure name is a legal C identifier */
59 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
60 if (!*p) return buffer;
62 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(dll_file_name), odp->ordinal );
63 return buffer;
66 /*******************************************************************
67 * AssignOrdinals
69 * Assign ordinals to all entry points.
71 static void AssignOrdinals(void)
73 int i, ordinal;
75 if ( !nb_names ) return;
77 /* start assigning from Base, or from 1 if no ordinal defined yet */
78 if (Base == MAX_ORDINALS) Base = 1;
79 for (i = 0, ordinal = Base; i < nb_names; i++)
81 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
82 while (Ordinals[ordinal]) ordinal++;
83 if (ordinal >= MAX_ORDINALS)
85 current_line = Names[i]->lineno;
86 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
88 Names[i]->ordinal = ordinal;
89 Ordinals[ordinal] = Names[i];
91 if (ordinal > Limit) Limit = ordinal;
95 /*******************************************************************
96 * output_debug
98 * Output the debug channels.
100 static int output_debug( FILE *outfile )
102 int i;
104 if (!nb_debug_channels) return 0;
105 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
107 for (i = 0; i < nb_debug_channels; i++)
108 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
109 debug_channels[i], debug_channels[i] );
111 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
112 for (i = 0; i < nb_debug_channels; i++)
114 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
115 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
117 fprintf( outfile, "\n};\n\n" );
118 fprintf( outfile, "static void *debug_registration;\n\n" );
120 return nb_debug_channels;
124 /*******************************************************************
125 * output_exports
127 * Output the export table for a Win32 module.
129 static int output_exports( FILE *outfile, int nr_exports )
131 int i, fwd_size = 0, total_size = 0;
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, " \"" __ASM_NAME("__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 " __ASM_NAME("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 case TYPE_STDCALL:
172 case TYPE_VARARGS:
173 case TYPE_CDECL:
174 if (!(odp->flags & FLAG_FORWARD))
176 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
177 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
179 else
181 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
182 fwd_size += strlen(odp->link_name) + 1;
184 break;
185 case TYPE_STUB:
186 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
187 break;
188 default:
189 assert(0);
192 total_size += (Limit - Base + 1) * sizeof(int);
194 if (nb_names)
196 /* output the function name pointers */
198 int namepos = 0;
200 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
201 for (i = 0; i < nb_names; i++)
203 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
204 namepos += strlen(Names[i]->name) + 1;
206 total_size += nb_names * sizeof(int);
208 /* output the function names */
210 fprintf( outfile, " \"\\t.text\\n\"\n" );
211 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
212 for (i = 0; i < nb_names; i++)
213 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
214 fprintf( outfile, " \"\\t.data\\n\"\n" );
216 /* output the function ordinals */
218 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
219 for (i = 0; i < nb_names; i++)
221 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", Names[i]->ordinal - Base );
223 total_size += nb_names * sizeof(short);
224 if (nb_names % 2)
226 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
227 total_size += sizeof(short);
231 /* output forward strings */
233 if (fwd_size)
235 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
236 for (i = Base; i <= Limit; i++)
238 ORDDEF *odp = Ordinals[i];
239 if (odp && (odp->flags & FLAG_FORWARD))
240 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
242 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
243 total_size += (fwd_size + 3) & ~3;
246 /* output relays */
248 if (debugging)
250 for (i = Base; i <= Limit; i++)
252 ORDDEF *odp = Ordinals[i];
253 unsigned int j, args, mask = 0;
254 const char *name;
256 /* skip non-existent entry points */
257 if (!odp) goto ignore;
258 /* skip non-functions */
259 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
260 /* skip norelay and forward entry points */
261 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
263 for (j = 0; odp->u.func.arg_types[j]; j++)
265 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
266 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
268 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
270 name = odp->link_name;
271 args = strlen(odp->u.func.arg_types) * sizeof(int);
272 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
274 switch(odp->type)
276 case TYPE_STDCALL:
277 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
278 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
279 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
280 break;
281 case TYPE_CDECL:
282 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
283 fprintf( outfile, " \"\\tret\\n\"\n" );
284 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
285 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
286 break;
287 default:
288 assert(0);
290 continue;
292 ignore:
293 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
297 fprintf( outfile, " \"\\t.text\\n\"\n" );
298 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
299 fprintf( outfile, ");\n\n" );
301 return total_size;
305 /*******************************************************************
306 * output_stub_funcs
308 * Output the functions for stub entry points
310 static void output_stub_funcs( FILE *outfile )
312 int i;
314 for (i = 0; i < nb_entry_points; i++)
316 ORDDEF *odp = EntryPoints[i];
317 if (odp->type != TYPE_STUB) continue;
318 fprintf( outfile, "#ifdef __GNUC__\n" );
319 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
320 fprintf( outfile, "#endif\n\n" );
321 fprintf( outfile, "struct exc_record {\n" );
322 fprintf( outfile, " unsigned int code, flags;\n" );
323 fprintf( outfile, " void *rec, *addr;\n" );
324 fprintf( outfile, " unsigned int params;\n" );
325 fprintf( outfile, " const void *info[15];\n" );
326 fprintf( outfile, "};\n\n" );
327 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
328 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
329 fprintf( outfile, " struct exc_record rec;\n" );
330 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
331 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
332 fprintf( outfile, " rec.rec = 0;\n" );
333 fprintf( outfile, " rec.params = 2;\n" );
334 fprintf( outfile, " rec.info[0] = dllname;\n" );
335 fprintf( outfile, " rec.info[1] = func;\n" );
336 fprintf( outfile, "#ifdef __GNUC__\n" );
337 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
338 fprintf( outfile, "#else\n" );
339 fprintf( outfile, " rec.addr = 0;\n" );
340 fprintf( outfile, "#endif\n" );
341 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
342 break;
345 for (i = 0; i < nb_entry_points; i++)
347 ORDDEF *odp = EntryPoints[i];
348 if (odp->type != TYPE_STUB) continue;
349 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
350 if (odp->name)
351 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
352 else if (odp->export_name)
353 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
354 else
355 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
360 /*******************************************************************
361 * output_register_funcs
363 * Output the functions for register entry points
365 static void output_register_funcs( FILE *outfile )
367 const char *name;
368 int i;
370 for (i = 0; i < nb_entry_points; i++)
372 ORDDEF *odp = EntryPoints[i];
373 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
374 if (!(odp->flags & FLAG_REGISTER)) continue;
375 if (odp->flags & FLAG_FORWARD) continue;
376 name = make_internal_name( odp, "regs" );
377 fprintf( outfile,
378 "asm(\".align %d\\n\\t\"\n"
379 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
380 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
381 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
382 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
383 " \".byte %d,%d\");\n",
384 get_alignment(4),
385 name, name, odp->link_name,
386 strlen(odp->u.func.arg_types) * sizeof(int),
387 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
392 /*******************************************************************
393 * output_dll_init
395 * Output code for calling a dll constructor and destructor.
397 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
399 fprintf( outfile, "#ifndef __GNUC__\n" );
400 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
401 fprintf( outfile, "#endif\n" );
403 #if defined(__i386__)
404 if (constructor)
406 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
407 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
408 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
410 if (destructor)
412 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
413 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
414 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
416 #elif defined(__sparc__)
417 if (constructor)
419 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
420 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
421 fprintf( outfile, " \"\\tnop\\n\"\n" );
422 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
424 if (destructor)
426 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
427 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
428 fprintf( outfile, " \"\\tnop\\n\"\n" );
429 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
431 #elif defined(__PPC__)
432 if (constructor)
434 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
435 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
436 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
438 if (destructor)
440 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
441 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
442 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
444 #else
445 #error You need to define the DLL constructor for your architecture
446 #endif
447 fprintf( outfile, "#ifndef __GNUC__\n" );
448 fprintf( outfile, "}\n" );
449 fprintf( outfile, "#endif\n" );
453 /*******************************************************************
454 * BuildSpec32File
456 * Build a Win32 C file from a spec file.
458 void BuildSpec32File( FILE *outfile )
460 int exports_size = 0;
461 int nr_exports, nr_imports, nr_resources;
462 int characteristics, subsystem;
463 DWORD page_size;
464 char constructor[100];
466 #ifdef HAVE_GETPAGESIZE
467 page_size = getpagesize();
468 #elif defined(__svr4__)
469 page_size = sysconf(_SC_PAGESIZE);
470 #elif defined(_WINDOWS)
472 SYSTEM_INFO si;
473 GetSystemInfo(&si);
474 page_size = si.dwPageSize;
476 #else
477 # error Cannot get the page size on this platform
478 #endif
480 AssignOrdinals();
481 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
483 resolve_imports();
484 output_standard_file_header( outfile );
486 /* Reserve some space for the PE header */
488 fprintf( outfile, "extern char pe_header[];\n" );
489 fprintf( outfile, "#ifndef __GNUC__\n" );
490 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
491 fprintf( outfile, "#endif\n" );
492 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
493 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
494 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
495 fprintf( outfile, "#ifndef __GNUC__\n" );
496 fprintf( outfile, "}\n" );
497 fprintf( outfile, "#endif\n" );
499 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", dll_file_name );
500 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
502 #ifdef __i386__
503 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
504 #else
505 fprintf( outfile, "#define __stdcall\n\n" );
506 #endif
508 if (nr_exports)
510 /* Output the stub functions */
512 output_stub_funcs( outfile );
514 fprintf( outfile, "#ifndef __GNUC__\n" );
515 fprintf( outfile, "static void __asm__dummy(void) {\n" );
516 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
518 /* Output code for all register functions */
520 output_register_funcs( outfile );
522 /* Output the exports and relay entry points */
524 exports_size = output_exports( outfile, nr_exports );
526 fprintf( outfile, "#ifndef __GNUC__\n" );
527 fprintf( outfile, "}\n" );
528 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
531 /* Output the DLL imports */
533 nr_imports = output_imports( outfile );
535 /* Output the resources */
537 nr_resources = output_resources( outfile );
539 /* Output LibMain function */
541 characteristics = subsystem = 0;
542 switch(SpecMode)
544 case SPEC_MODE_DLL:
545 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
546 else
548 fprintf( outfile, "#ifdef __GNUC__\n" );
549 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
550 fprintf( outfile, "#else\n" );
551 fprintf( outfile, "extern void DllMain();\n" );
552 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
553 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
554 fprintf( outfile, "#endif\n" );
556 characteristics = IMAGE_FILE_DLL;
557 break;
558 case SPEC_MODE_GUIEXE:
559 if (!init_func) init_func = "WinMain";
560 fprintf( outfile,
561 "\ntypedef struct {\n"
562 " unsigned int cb;\n"
563 " char *lpReserved, *lpDesktop, *lpTitle;\n"
564 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
565 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
566 " unsigned short wShowWindow, cbReserved2;\n"
567 " char *lpReserved2;\n"
568 " void *hStdInput, *hStdOutput, *hStdError;\n"
569 "} STARTUPINFOA;\n"
570 "int _ARGC;\n"
571 "char **_ARGV;\n"
572 "extern int __stdcall %s(void *,void *,char *,int);\n"
573 "extern char * __stdcall GetCommandLineA(void);\n"
574 "extern void * __stdcall GetModuleHandleA(char *);\n"
575 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
576 "extern void __stdcall ExitProcess(unsigned int);\n"
577 "static void __wine_exe_main(void)\n"
578 "{\n"
579 " extern int __wine_main_argc;\n"
580 " extern char **__wine_main_argv;\n"
581 " STARTUPINFOA info;\n"
582 " char *cmdline = GetCommandLineA();\n"
583 " int bcount=0, in_quotes=0;\n"
584 " while (*cmdline) {\n"
585 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
586 " else if (*cmdline=='\\\\') bcount++;\n"
587 " else if (*cmdline=='\\\"') {\n"
588 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
589 " bcount=0;\n"
590 " }\n"
591 " else bcount=0;\n"
592 " cmdline++;\n"
593 " }\n"
594 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
595 " GetStartupInfoA( &info );\n"
596 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
597 " _ARGC = __wine_main_argc;\n"
598 " _ARGV = __wine_main_argv;\n"
599 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
600 "}\n\n", init_func, init_func );
601 init_func = "__wine_exe_main";
602 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
603 break;
604 case SPEC_MODE_GUIEXE_UNICODE:
605 if (!init_func) init_func = "WinMain";
606 fprintf( outfile,
607 "\ntypedef unsigned short WCHAR;\n"
608 "typedef struct {\n"
609 " unsigned int cb;\n"
610 " char *lpReserved, *lpDesktop, *lpTitle;\n"
611 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
612 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
613 " unsigned short wShowWindow, cbReserved2;\n"
614 " char *lpReserved2;\n"
615 " void *hStdInput, *hStdOutput, *hStdError;\n"
616 "} STARTUPINFOA;\n"
617 "int _ARGC;\n"
618 "WCHAR **_ARGV;\n"
619 "extern int __stdcall %s(void *,void *,char *,int);\n"
620 "extern char * __stdcall GetCommandLineA(void);\n"
621 "extern void * __stdcall GetModuleHandleA(char *);\n"
622 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
623 "extern void __stdcall ExitProcess(unsigned int);\n"
624 "static void __wine_exe_main(void)\n"
625 "{\n"
626 " extern int __wine_main_argc;\n"
627 " extern WCHAR **__wine_main_wargv;\n"
628 " STARTUPINFOA info;\n"
629 " char *cmdline = GetCommandLineA();\n"
630 " int bcount=0, in_quotes=0;\n"
631 " while (*cmdline) {\n"
632 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
633 " else if (*cmdline=='\\\\') bcount++;\n"
634 " else if (*cmdline=='\\\"') {\n"
635 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
636 " bcount=0;\n"
637 " }\n"
638 " else bcount=0;\n"
639 " cmdline++;\n"
640 " }\n"
641 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
642 " GetStartupInfoA( &info );\n"
643 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
644 " _ARGC = __wine_main_argc;\n"
645 " _ARGV = __wine_main_wargv;\n"
646 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
647 "}\n\n", init_func, init_func );
648 init_func = "__wine_exe_main";
649 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
650 break;
651 case SPEC_MODE_CUIEXE:
652 if (!init_func) init_func = "main";
653 fprintf( outfile,
654 "\nint _ARGC;\n"
655 "char **_ARGV;\n"
656 "extern void __stdcall ExitProcess(int);\n"
657 "static void __wine_exe_main(void)\n"
658 "{\n"
659 " extern int %s( int argc, char *argv[] );\n"
660 " extern int __wine_main_argc;\n"
661 " extern char **__wine_main_argv;\n"
662 " _ARGC = __wine_main_argc;\n"
663 " _ARGV = __wine_main_argv;\n"
664 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
665 "}\n\n", init_func, init_func );
666 init_func = "__wine_exe_main";
667 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
668 break;
669 case SPEC_MODE_CUIEXE_UNICODE:
670 if (!init_func) init_func = "wmain";
671 fprintf( outfile,
672 "\ntypedef unsigned short WCHAR;\n"
673 "int _ARGC;\n"
674 "WCHAR **_ARGV;\n"
675 "extern void __stdcall ExitProcess(int);\n"
676 "static void __wine_exe_main(void)\n"
677 "{\n"
678 " extern int %s( int argc, WCHAR *argv[] );\n"
679 " extern int __wine_main_argc;\n"
680 " extern WCHAR **__wine_main_wargv;\n"
681 " _ARGC = __wine_main_argc;\n"
682 " _ARGV = __wine_main_wargv;\n"
683 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
684 "}\n\n", init_func, init_func );
685 init_func = "__wine_exe_main";
686 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
687 break;
690 /* Output the NT header */
692 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
693 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
694 fprintf( outfile, " int Signature;\n" );
695 fprintf( outfile, " struct file_header {\n" );
696 fprintf( outfile, " short Machine;\n" );
697 fprintf( outfile, " short NumberOfSections;\n" );
698 fprintf( outfile, " int TimeDateStamp;\n" );
699 fprintf( outfile, " void *PointerToSymbolTable;\n" );
700 fprintf( outfile, " int NumberOfSymbols;\n" );
701 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
702 fprintf( outfile, " short Characteristics;\n" );
703 fprintf( outfile, " } FileHeader;\n" );
704 fprintf( outfile, " struct opt_header {\n" );
705 fprintf( outfile, " short Magic;\n" );
706 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
707 fprintf( outfile, " int SizeOfCode;\n" );
708 fprintf( outfile, " int SizeOfInitializedData;\n" );
709 fprintf( outfile, " int SizeOfUninitializedData;\n" );
710 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
711 fprintf( outfile, " void *BaseOfCode;\n" );
712 fprintf( outfile, " void *BaseOfData;\n" );
713 fprintf( outfile, " void *ImageBase;\n" );
714 fprintf( outfile, " int SectionAlignment;\n" );
715 fprintf( outfile, " int FileAlignment;\n" );
716 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
717 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
718 fprintf( outfile, " short MajorImageVersion;\n" );
719 fprintf( outfile, " short MinorImageVersion;\n" );
720 fprintf( outfile, " short MajorSubsystemVersion;\n" );
721 fprintf( outfile, " short MinorSubsystemVersion;\n" );
722 fprintf( outfile, " int Win32VersionValue;\n" );
723 fprintf( outfile, " int SizeOfImage;\n" );
724 fprintf( outfile, " int SizeOfHeaders;\n" );
725 fprintf( outfile, " int CheckSum;\n" );
726 fprintf( outfile, " short Subsystem;\n" );
727 fprintf( outfile, " short DllCharacteristics;\n" );
728 fprintf( outfile, " int SizeOfStackReserve;\n" );
729 fprintf( outfile, " int SizeOfStackCommit;\n" );
730 fprintf( outfile, " int SizeOfHeapReserve;\n" );
731 fprintf( outfile, " int SizeOfHeapCommit;\n" );
732 fprintf( outfile, " int LoaderFlags;\n" );
733 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
734 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
735 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
736 fprintf( outfile, " } OptionalHeader;\n" );
737 fprintf( outfile, "} nt_header = {\n" );
738 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
740 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
741 fprintf( outfile, " 0, 0, 0, 0,\n" );
742 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
743 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
745 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
746 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
747 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
748 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
749 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
750 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
751 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
752 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
753 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
754 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
755 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
756 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
757 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
758 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
759 fprintf( outfile, " 0,\n" ); /* CheckSum */
760 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
761 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
762 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
763 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
764 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
765 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
766 fprintf( outfile, " {\n" );
767 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
768 exports_size ? "__wine_spec_exports" : "0", exports_size );
769 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
770 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
771 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
772 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
773 fprintf( outfile, " }\n }\n};\n\n" );
775 /* Output the DLL constructor */
777 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(dll_file_name) );
778 output_dll_init( outfile, constructor, NULL );
780 fprintf( outfile,
781 "void %s(void)\n"
782 "{\n"
783 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
784 " extern void *__wine_dbg_register( char * const *, int );\n"
785 " __wine_dll_register( &nt_header, \"%s\" );\n"
786 "}\n",
787 constructor, dll_file_name );
791 /*******************************************************************
792 * BuildDef32File
794 * Build a Win32 def file from a spec file.
796 void BuildDef32File(FILE *outfile)
798 const char *name;
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", dll_file_name);
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 int is_data = 0;
817 if (!odp) continue;
818 if (odp->flags & FLAG_REGISTER) continue;
819 if (odp->type == TYPE_STUB) continue;
821 if (odp->name) name = odp->name;
822 else if (odp->export_name) name = odp->export_name;
823 else continue;
825 fprintf(outfile, " %s", name);
827 switch(odp->type)
829 case TYPE_EXTERN:
830 is_data = 1;
831 /* fall through */
832 case TYPE_VARARGS:
833 case TYPE_CDECL:
834 /* try to reduce output */
835 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
836 fprintf(outfile, "=%s", odp->link_name);
837 break;
838 case TYPE_STDCALL:
840 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
841 if (!kill_at) fprintf(outfile, "@%d", at_param);
842 if (odp->flags & FLAG_FORWARD)
844 fprintf(outfile, "=%s", odp->link_name);
846 else if (strcmp(name, odp->link_name)) /* try to reduce output */
848 fprintf(outfile, "=%s", odp->link_name);
849 if (!kill_at) fprintf(outfile, "@%d", at_param);
851 break;
853 default:
854 assert(0);
856 fprintf(outfile, " @%d%s%s\n", odp->ordinal,
857 odp->name ? "" : " NONAME", is_data ? " DATA" : "" );
862 /*******************************************************************
863 * BuildDebugFile
865 * Build the debugging channels source file.
867 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
869 int nr_debug;
870 char *prefix, *p;
872 while (*argv)
874 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
877 output_standard_file_header( outfile );
878 nr_debug = output_debug( outfile );
879 if (!nr_debug)
881 fprintf( outfile, "/* no debug channels found for this module */\n" );
882 return;
885 if (output_file_name)
887 if ((p = strrchr( output_file_name, '/' ))) p++;
888 prefix = xstrdup( p ? p : output_file_name );
889 if ((p = strchr( prefix, '.' ))) *p = 0;
890 strcpy( p, make_c_identifier(p) );
892 else prefix = xstrdup( "_" );
894 /* Output the DLL constructor */
896 fprintf( outfile,
897 "#ifdef __GNUC__\n"
898 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
899 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
900 "#else\n"
901 "static void __asm__dummy_dll_init(void) {\n",
902 prefix, prefix );
904 #if defined(__i386__)
905 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
906 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
907 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
908 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
909 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
910 #elif defined(__sparc__)
911 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
912 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
913 fprintf( outfile, " \"\\tnop\\n\"\n" );
914 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
915 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
916 fprintf( outfile, " \"\\tnop\\n\"\n" );
917 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
918 #elif defined(__PPC__)
919 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
920 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
921 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
922 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
923 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
924 #else
925 #error You need to define the DLL constructor for your architecture
926 #endif
927 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
929 fprintf( outfile,
930 "\n#ifdef __GNUC__\n"
931 "static\n"
932 "#endif\n"
933 "void __wine_dbg_%s_init(void)\n"
934 "{\n"
935 " extern void *__wine_dbg_register( char * const *, int );\n"
936 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
937 "}\n", prefix, nr_debug );
938 fprintf( outfile,
939 "\n#ifdef __GNUC__\n"
940 "static\n"
941 "#endif\n"
942 "void __wine_dbg_%s_fini(void)\n"
943 "{\n"
944 " extern void __wine_dbg_unregister( void* );\n"
945 " __wine_dbg_unregister( debug_registration );\n"
946 "}\n", prefix );
948 free( prefix );