Removed windows version checks from GetPrinterDriverDirectory tests.
[wine/multimedia.git] / tools / winebuild / spec32.c
blob444603acc48943fbff4a9c80017c7cea865f3728
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, DLLFileName,
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(DLLFileName), 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 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
172 break;
173 case TYPE_STDCALL:
174 case TYPE_VARARGS:
175 case TYPE_CDECL:
176 if (!(odp->flags & FLAG_FORWARD))
178 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
179 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
180 break;
182 /* else fall through */
183 case TYPE_FORWARD:
184 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
185 fwd_size += strlen(odp->link_name) + 1;
186 break;
187 case TYPE_STUB:
188 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
189 break;
190 case TYPE_VARIABLE:
191 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "var" ) );
192 break;
193 default:
194 assert(0);
197 total_size += (Limit - Base + 1) * sizeof(int);
199 if (nb_names)
201 /* output the function name pointers */
203 int namepos = 0;
205 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
206 for (i = 0; i < nb_names; i++)
208 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
209 namepos += strlen(Names[i]->name) + 1;
211 total_size += nb_names * sizeof(int);
213 /* output the function names */
215 fprintf( outfile, " \"\\t.text\\n\"\n" );
216 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
217 for (i = 0; i < nb_names; i++)
218 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
219 fprintf( outfile, " \"\\t.data\\n\"\n" );
221 /* output the function ordinals */
223 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
224 for (i = 0; i < nb_names; i++)
226 fprintf( outfile, " \"\\t.short %d\\n\"\n", Names[i]->ordinal - Base );
228 total_size += nb_names * sizeof(short);
229 if (nb_names % 2)
231 fprintf( outfile, " \"\\t.short 0\\n\"\n" );
232 total_size += sizeof(short);
236 /* output forward strings */
238 if (fwd_size)
240 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
241 for (i = Base; i <= Limit; i++)
243 ORDDEF *odp = Ordinals[i];
244 if (odp && (odp->flags & FLAG_FORWARD))
245 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
247 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
248 total_size += (fwd_size + 3) & ~3;
251 /* output relays */
253 if (debugging)
255 for (i = Base; i <= Limit; i++)
257 ORDDEF *odp = Ordinals[i];
258 unsigned int j, args, mask = 0;
259 const char *name;
261 /* skip non-existent entry points */
262 if (!odp) goto ignore;
263 /* skip non-functions */
264 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
265 /* skip norelay and forward entry points */
266 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
268 for (j = 0; odp->u.func.arg_types[j]; j++)
270 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
271 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
273 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
275 name = odp->link_name;
276 args = strlen(odp->u.func.arg_types) * sizeof(int);
277 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, "regs" );
279 switch(odp->type)
281 case TYPE_STDCALL:
282 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
283 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
284 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
285 break;
286 case TYPE_CDECL:
287 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
288 fprintf( outfile, " \"\\tret\\n\"\n" );
289 fprintf( outfile, " \"\\t.short %d\\n\"\n", args );
290 fprintf( outfile, " \"\\t.long " __ASM_NAME("%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" );
302 /* output variables */
304 for (i = 0; i < nb_entry_points; i++)
306 ORDDEF *odp = EntryPoints[i];
307 if (odp->type == TYPE_VARIABLE)
309 int j;
310 fprintf( outfile, " \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
311 fprintf( outfile, " \"\\t.long " );
312 for (j = 0; j < odp->u.var.n_values; j++)
314 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
315 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
317 fprintf( outfile, "\\n\"\n" );
321 fprintf( outfile, " \"\\t.text\\n\"\n" );
322 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
323 fprintf( outfile, ");\n\n" );
325 return total_size;
329 /*******************************************************************
330 * output_stub_funcs
332 * Output the functions for stub entry points
334 static void output_stub_funcs( FILE *outfile )
336 int i;
338 for (i = 0; i < nb_entry_points; i++)
340 ORDDEF *odp = EntryPoints[i];
341 if (odp->type != TYPE_STUB) continue;
342 fprintf( outfile, "#ifdef __GNUC__\n" );
343 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
344 fprintf( outfile, "#endif\n\n" );
345 fprintf( outfile, "struct exc_record {\n" );
346 fprintf( outfile, " unsigned int code, flags;\n" );
347 fprintf( outfile, " void *rec, *addr;\n" );
348 fprintf( outfile, " unsigned int params;\n" );
349 fprintf( outfile, " const void *info[15];\n" );
350 fprintf( outfile, "};\n\n" );
351 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
352 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
353 fprintf( outfile, " struct exc_record rec;\n" );
354 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
355 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
356 fprintf( outfile, " rec.rec = 0;\n" );
357 fprintf( outfile, " rec.params = 2;\n" );
358 fprintf( outfile, " rec.info[0] = dllname;\n" );
359 fprintf( outfile, " rec.info[1] = func;\n" );
360 fprintf( outfile, "#ifdef __GNUC__\n" );
361 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
362 fprintf( outfile, "#else\n" );
363 fprintf( outfile, " rec.addr = 0;\n" );
364 fprintf( outfile, "#endif\n" );
365 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
366 break;
369 for (i = 0; i < nb_entry_points; i++)
371 ORDDEF *odp = EntryPoints[i];
372 if (odp->type != TYPE_STUB) continue;
373 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
374 if (odp->name)
375 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
376 else if (odp->export_name)
377 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
378 else
379 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
384 /*******************************************************************
385 * output_register_funcs
387 * Output the functions for register entry points
389 static void output_register_funcs( FILE *outfile )
391 const char *name;
392 int i;
394 for (i = 0; i < nb_entry_points; i++)
396 ORDDEF *odp = EntryPoints[i];
397 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
398 if (!(odp->flags & FLAG_REGISTER)) continue;
399 name = make_internal_name( odp, "regs" );
400 fprintf( outfile,
401 "asm(\".align %d\\n\\t\"\n"
402 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
403 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
404 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
405 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
406 " \".byte %d,%d\");\n",
407 get_alignment(4),
408 name, name, odp->link_name,
409 strlen(odp->u.func.arg_types) * sizeof(int),
410 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
415 /*******************************************************************
416 * output_dll_init
418 * Output code for calling a dll constructor and destructor.
420 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
422 fprintf( outfile, "#ifndef __GNUC__\n" );
423 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
424 fprintf( outfile, "#endif\n" );
426 #if defined(__i386__)
427 if (constructor)
429 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
430 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
431 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
433 if (destructor)
435 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
436 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
437 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
439 #elif defined(__sparc__)
440 if (constructor)
442 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
443 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
444 fprintf( outfile, " \"\\tnop\\n\"\n" );
445 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
447 if (destructor)
449 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
450 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
451 fprintf( outfile, " \"\\tnop\\n\"\n" );
452 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
454 #elif defined(__PPC__)
455 if (constructor)
457 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
458 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
459 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
461 if (destructor)
463 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
464 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
465 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
467 #else
468 #error You need to define the DLL constructor for your architecture
469 #endif
470 fprintf( outfile, "#ifndef __GNUC__\n" );
471 fprintf( outfile, "}\n" );
472 fprintf( outfile, "#endif\n" );
476 /*******************************************************************
477 * BuildSpec32File
479 * Build a Win32 C file from a spec file.
481 void BuildSpec32File( FILE *outfile )
483 int exports_size = 0;
484 int nr_exports, nr_imports, nr_resources;
485 int characteristics, subsystem;
486 DWORD page_size;
487 char constructor[100];
489 #ifdef HAVE_GETPAGESIZE
490 page_size = getpagesize();
491 #elif defined(__svr4__)
492 page_size = sysconf(_SC_PAGESIZE);
493 #elif defined(_WINDOWS)
495 SYSTEM_INFO si;
496 GetSystemInfo(&si);
497 page_size = si.dwPageSize;
499 #else
500 # error Cannot get the page size on this platform
501 #endif
503 AssignOrdinals();
504 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
506 resolve_imports();
507 output_standard_file_header( outfile );
509 /* Reserve some space for the PE header */
511 fprintf( outfile, "extern char pe_header[];\n" );
512 fprintf( outfile, "#ifndef __GNUC__\n" );
513 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
514 fprintf( outfile, "#endif\n" );
515 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
516 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
517 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
518 fprintf( outfile, "#ifndef __GNUC__\n" );
519 fprintf( outfile, "}\n" );
520 fprintf( outfile, "#endif\n" );
522 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLFileName );
523 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
525 #ifdef __i386__
526 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
527 #else
528 fprintf( outfile, "#define __stdcall\n\n" );
529 #endif
531 if (nr_exports)
533 /* Output the stub functions */
535 output_stub_funcs( outfile );
537 fprintf( outfile, "#ifndef __GNUC__\n" );
538 fprintf( outfile, "static void __asm__dummy(void) {\n" );
539 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
541 /* Output code for all register functions */
543 output_register_funcs( outfile );
545 /* Output the exports and relay entry points */
547 exports_size = output_exports( outfile, nr_exports );
549 fprintf( outfile, "#ifndef __GNUC__\n" );
550 fprintf( outfile, "}\n" );
551 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
554 /* Output the DLL imports */
556 nr_imports = output_imports( outfile );
558 /* Output the resources */
560 nr_resources = output_resources( outfile );
562 /* Output LibMain function */
564 characteristics = subsystem = 0;
565 switch(SpecMode)
567 case SPEC_MODE_DLL:
568 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
569 else
571 fprintf( outfile, "#ifdef __GNUC__\n" );
572 fprintf( outfile, "extern void DllMain() __attribute__((weak));\n" );
573 fprintf( outfile, "#else\n" );
574 fprintf( outfile, "extern void DllMain();\n" );
575 fprintf( outfile, "static void __asm__dummy_dllmain(void)" );
576 fprintf( outfile, " { asm(\".weak " __ASM_NAME("DllMain") "\"); }\n" );
577 fprintf( outfile, "#endif\n" );
579 characteristics = IMAGE_FILE_DLL;
580 break;
581 case SPEC_MODE_GUIEXE:
582 if (!init_func) init_func = "WinMain";
583 fprintf( outfile,
584 "\ntypedef struct {\n"
585 " unsigned int cb;\n"
586 " char *lpReserved, *lpDesktop, *lpTitle;\n"
587 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
588 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
589 " unsigned short wShowWindow, cbReserved2;\n"
590 " char *lpReserved2;\n"
591 " void *hStdInput, *hStdOutput, *hStdError;\n"
592 "} STARTUPINFOA;\n"
593 "int _ARGC;\n"
594 "char **_ARGV;\n"
595 "extern int __stdcall %s(void *,void *,char *,int);\n"
596 "extern char * __stdcall GetCommandLineA(void);\n"
597 "extern void * __stdcall GetModuleHandleA(char *);\n"
598 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
599 "extern void __stdcall ExitProcess(unsigned int);\n"
600 "static void __wine_exe_main(void)\n"
601 "{\n"
602 " extern int __wine_main_argc;\n"
603 " extern char **__wine_main_argv;\n"
604 " STARTUPINFOA info;\n"
605 " char *cmdline = GetCommandLineA();\n"
606 " int bcount=0, in_quotes=0;\n"
607 " while (*cmdline) {\n"
608 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
609 " else if (*cmdline=='\\\\') bcount++;\n"
610 " else if (*cmdline=='\\\"') {\n"
611 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
612 " bcount=0;\n"
613 " }\n"
614 " else bcount=0;\n"
615 " cmdline++;\n"
616 " }\n"
617 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
618 " GetStartupInfoA( &info );\n"
619 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
620 " _ARGC = __wine_main_argc;\n"
621 " _ARGV = __wine_main_argv;\n"
622 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
623 "}\n\n", init_func, init_func );
624 init_func = "__wine_exe_main";
625 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
626 break;
627 case SPEC_MODE_GUIEXE_UNICODE:
628 if (!init_func) init_func = "WinMain";
629 fprintf( outfile,
630 "\ntypedef unsigned short WCHAR;\n"
631 "typedef struct {\n"
632 " unsigned int cb;\n"
633 " char *lpReserved, *lpDesktop, *lpTitle;\n"
634 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
635 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
636 " unsigned short wShowWindow, cbReserved2;\n"
637 " char *lpReserved2;\n"
638 " void *hStdInput, *hStdOutput, *hStdError;\n"
639 "} STARTUPINFOA;\n"
640 "int _ARGC;\n"
641 "WCHAR **_ARGV;\n"
642 "extern int __stdcall %s(void *,void *,char *,int);\n"
643 "extern char * __stdcall GetCommandLineA(void);\n"
644 "extern void * __stdcall GetModuleHandleA(char *);\n"
645 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
646 "extern void __stdcall ExitProcess(unsigned int);\n"
647 "static void __wine_exe_main(void)\n"
648 "{\n"
649 " extern int __wine_main_argc;\n"
650 " extern WCHAR **__wine_main_wargv;\n"
651 " STARTUPINFOA info;\n"
652 " char *cmdline = GetCommandLineA();\n"
653 " int bcount=0, in_quotes=0;\n"
654 " while (*cmdline) {\n"
655 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
656 " else if (*cmdline=='\\\\') bcount++;\n"
657 " else if (*cmdline=='\\\"') {\n"
658 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
659 " bcount=0;\n"
660 " }\n"
661 " else bcount=0;\n"
662 " cmdline++;\n"
663 " }\n"
664 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
665 " GetStartupInfoA( &info );\n"
666 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
667 " _ARGC = __wine_main_argc;\n"
668 " _ARGV = __wine_main_wargv;\n"
669 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
670 "}\n\n", init_func, init_func );
671 init_func = "__wine_exe_main";
672 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
673 break;
674 case SPEC_MODE_CUIEXE:
675 if (!init_func) init_func = "main";
676 fprintf( outfile,
677 "\nint _ARGC;\n"
678 "char **_ARGV;\n"
679 "extern void __stdcall ExitProcess(int);\n"
680 "static void __wine_exe_main(void)\n"
681 "{\n"
682 " extern int %s( int argc, char *argv[] );\n"
683 " extern int __wine_main_argc;\n"
684 " extern char **__wine_main_argv;\n"
685 " _ARGC = __wine_main_argc;\n"
686 " _ARGV = __wine_main_argv;\n"
687 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
688 "}\n\n", init_func, init_func );
689 init_func = "__wine_exe_main";
690 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
691 break;
692 case SPEC_MODE_CUIEXE_UNICODE:
693 if (!init_func) init_func = "wmain";
694 fprintf( outfile,
695 "\ntypedef unsigned short WCHAR;\n"
696 "int _ARGC;\n"
697 "WCHAR **_ARGV;\n"
698 "extern void __stdcall ExitProcess(int);\n"
699 "static void __wine_exe_main(void)\n"
700 "{\n"
701 " extern int %s( int argc, WCHAR *argv[] );\n"
702 " extern int __wine_main_argc;\n"
703 " extern WCHAR **__wine_main_wargv;\n"
704 " _ARGC = __wine_main_argc;\n"
705 " _ARGV = __wine_main_wargv;\n"
706 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
707 "}\n\n", init_func, init_func );
708 init_func = "__wine_exe_main";
709 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
710 break;
713 /* Output the NT header */
715 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
716 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
717 fprintf( outfile, " int Signature;\n" );
718 fprintf( outfile, " struct file_header {\n" );
719 fprintf( outfile, " short Machine;\n" );
720 fprintf( outfile, " short NumberOfSections;\n" );
721 fprintf( outfile, " int TimeDateStamp;\n" );
722 fprintf( outfile, " void *PointerToSymbolTable;\n" );
723 fprintf( outfile, " int NumberOfSymbols;\n" );
724 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
725 fprintf( outfile, " short Characteristics;\n" );
726 fprintf( outfile, " } FileHeader;\n" );
727 fprintf( outfile, " struct opt_header {\n" );
728 fprintf( outfile, " short Magic;\n" );
729 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
730 fprintf( outfile, " int SizeOfCode;\n" );
731 fprintf( outfile, " int SizeOfInitializedData;\n" );
732 fprintf( outfile, " int SizeOfUninitializedData;\n" );
733 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
734 fprintf( outfile, " void *BaseOfCode;\n" );
735 fprintf( outfile, " void *BaseOfData;\n" );
736 fprintf( outfile, " void *ImageBase;\n" );
737 fprintf( outfile, " int SectionAlignment;\n" );
738 fprintf( outfile, " int FileAlignment;\n" );
739 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
740 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
741 fprintf( outfile, " short MajorImageVersion;\n" );
742 fprintf( outfile, " short MinorImageVersion;\n" );
743 fprintf( outfile, " short MajorSubsystemVersion;\n" );
744 fprintf( outfile, " short MinorSubsystemVersion;\n" );
745 fprintf( outfile, " int Win32VersionValue;\n" );
746 fprintf( outfile, " int SizeOfImage;\n" );
747 fprintf( outfile, " int SizeOfHeaders;\n" );
748 fprintf( outfile, " int CheckSum;\n" );
749 fprintf( outfile, " short Subsystem;\n" );
750 fprintf( outfile, " short DllCharacteristics;\n" );
751 fprintf( outfile, " int SizeOfStackReserve;\n" );
752 fprintf( outfile, " int SizeOfStackCommit;\n" );
753 fprintf( outfile, " int SizeOfHeapReserve;\n" );
754 fprintf( outfile, " int SizeOfHeapCommit;\n" );
755 fprintf( outfile, " int LoaderFlags;\n" );
756 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
757 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
758 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
759 fprintf( outfile, " } OptionalHeader;\n" );
760 fprintf( outfile, "} nt_header = {\n" );
761 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
763 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
764 fprintf( outfile, " 0, 0, 0, 0,\n" );
765 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
766 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
768 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
769 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
770 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
771 fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */
772 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
773 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
774 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
775 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
776 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
777 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
778 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
779 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
780 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
781 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
782 fprintf( outfile, " 0,\n" ); /* CheckSum */
783 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
784 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
785 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
786 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
787 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
788 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
789 fprintf( outfile, " {\n" );
790 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
791 exports_size ? "__wine_spec_exports" : "0", exports_size );
792 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
793 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
794 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
795 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
796 fprintf( outfile, " }\n }\n};\n\n" );
798 /* Output the DLL constructor */
800 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLFileName) );
801 output_dll_init( outfile, constructor, NULL );
803 fprintf( outfile,
804 "void %s(void)\n"
805 "{\n"
806 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
807 " extern void *__wine_dbg_register( char * const *, int );\n"
808 " __wine_dll_register( &nt_header, \"%s\" );\n"
809 "}\n",
810 constructor, DLLFileName );
814 /*******************************************************************
815 * BuildDef32File
817 * Build a Win32 def file from a spec file.
819 void BuildDef32File(FILE *outfile)
821 const char *name;
822 int i;
824 AssignOrdinals();
826 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
827 input_file_name );
829 fprintf(outfile, "LIBRARY %s\n\n", DLLFileName);
831 fprintf(outfile, "EXPORTS\n");
833 /* Output the exports and relay entry points */
835 for(i = 0; i < nb_entry_points; i++)
837 ORDDEF *odp = EntryPoints[i];
838 int is_data = 0;
840 if (!odp) continue;
841 if (odp->flags & FLAG_REGISTER) continue;
842 if (odp->type == TYPE_STUB) continue;
844 if (odp->name) name = odp->name;
845 else if (odp->export_name) name = odp->export_name;
846 else continue;
848 fprintf(outfile, " %s", name);
850 switch(odp->type)
852 case TYPE_EXTERN:
853 case TYPE_VARIABLE:
854 is_data = 1;
855 /* fall through */
856 case TYPE_VARARGS:
857 case TYPE_CDECL:
858 /* try to reduce output */
859 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
860 fprintf(outfile, "=%s", odp->link_name);
861 break;
862 case TYPE_STDCALL:
864 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
865 if (!kill_at) fprintf(outfile, "@%d", at_param);
866 if (odp->flags & FLAG_FORWARD)
868 fprintf(outfile, "=%s", odp->link_name);
870 else if (strcmp(name, odp->link_name)) /* try to reduce output */
872 fprintf(outfile, "=%s", odp->link_name);
873 if (!kill_at) fprintf(outfile, "@%d", at_param);
875 break;
877 case TYPE_FORWARD:
878 fprintf(outfile, "=%s", odp->link_name);
879 break;
880 default:
881 assert(0);
883 fprintf(outfile, " @%d%s%s\n", odp->ordinal,
884 odp->name ? "" : " NONAME", is_data ? " DATA" : "" );
889 /*******************************************************************
890 * BuildDebugFile
892 * Build the debugging channels source file.
894 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
896 int nr_debug;
897 char *prefix, *p;
899 while (*argv) parse_debug_channels( srcdir, *argv++ );
901 output_standard_file_header( outfile );
902 nr_debug = output_debug( outfile );
903 if (!nr_debug)
905 fprintf( outfile, "/* no debug channels found for this module */\n" );
906 return;
909 if (output_file_name)
911 if ((p = strrchr( output_file_name, '/' ))) p++;
912 prefix = xstrdup( p ? p : output_file_name );
913 if ((p = strchr( prefix, '.' ))) *p = 0;
914 strcpy( p, make_c_identifier(p) );
916 else prefix = xstrdup( "_" );
918 /* Output the DLL constructor */
920 fprintf( outfile,
921 "#ifdef __GNUC__\n"
922 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
923 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
924 "#else\n"
925 "static void __asm__dummy_dll_init(void) {\n",
926 prefix, prefix );
928 #if defined(__i386__)
929 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
930 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
931 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
932 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
933 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
934 #elif defined(__sparc__)
935 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
936 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
937 fprintf( outfile, " \"\\tnop\\n\"\n" );
938 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
939 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
940 fprintf( outfile, " \"\\tnop\\n\"\n" );
941 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
942 #elif defined(__PPC__)
943 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
944 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
945 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
946 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
947 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
948 #else
949 #error You need to define the DLL constructor for your architecture
950 #endif
951 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
953 fprintf( outfile,
954 "\n#ifdef __GNUC__\n"
955 "static\n"
956 "#endif\n"
957 "void __wine_dbg_%s_init(void)\n"
958 "{\n"
959 " extern void *__wine_dbg_register( char * const *, int );\n"
960 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
961 "}\n", prefix, nr_debug );
962 fprintf( outfile,
963 "\n#ifdef __GNUC__\n"
964 "static\n"
965 "#endif\n"
966 "void __wine_dbg_%s_fini(void)\n"
967 "{\n"
968 " extern void __wine_dbg_unregister( void* );\n"
969 " __wine_dbg_unregister( debug_registration );\n"
970 "}\n", prefix );
972 free( prefix );