Don't list register functions in the .def file.
[wine.git] / tools / winebuild / spec32.c
blobc6ac6330023dbc0c32bc23f024e0583b78172270
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;
132 char *p;
134 if (!nr_exports) return 0;
136 fprintf( outfile, "asm(\".data\\n\"\n" );
137 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
138 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
140 /* export directory header */
142 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
144 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
145 fprintf( outfile, " \"\\t.long " __ASM_NAME("dllname") "\\n\"\n" ); /* Name */
146 fprintf( outfile, " \"\\t.long %d\\n\"\n", Base ); /* Base */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
148 fprintf( outfile, " \"\\t.long %d\\n\"\n", nb_names ); /* NumberOfNames */
149 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
150 if (nb_names)
152 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
153 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
155 else
157 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
158 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
160 total_size += 10 * sizeof(int);
162 /* output the function pointers */
164 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
165 for (i = Base; i <= Limit; i++)
167 ORDDEF *odp = Ordinals[i];
168 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
169 else switch(odp->type)
171 case TYPE_EXTERN:
172 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", odp->link_name );
173 break;
174 case TYPE_STDCALL:
175 case TYPE_VARARGS:
176 case TYPE_CDECL:
177 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
178 (odp->flags & FLAG_REGISTER) ? make_internal_name(odp,"regs") : odp->link_name );
179 break;
180 case TYPE_STUB:
181 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "stub" ) );
182 break;
183 case TYPE_VARIABLE:
184 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", make_internal_name( odp, "var" ) );
185 break;
186 case TYPE_FORWARD:
187 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
188 fwd_size += strlen(odp->link_name) + 1;
189 break;
190 default:
191 assert(0);
194 total_size += (Limit - Base + 1) * sizeof(int);
196 if (nb_names)
198 /* output the function name pointers */
200 int namepos = 0;
202 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
203 for (i = 0; i < nb_names; i++)
205 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
206 namepos += strlen(Names[i]->name) + 1;
208 total_size += nb_names * sizeof(int);
210 /* output the function names */
212 fprintf( outfile, " \"\\t.text\\n\"\n" );
213 fprintf( outfile, " \"__wine_spec_exp_names:\\n\"\n" );
214 for (i = 0; i < nb_names; i++)
215 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", Names[i]->name );
216 fprintf( outfile, " \"\\t.data\\n\"\n" );
218 /* output the function ordinals */
220 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
221 for (i = 0; i < nb_names; i++)
223 fprintf( outfile, " \"\\t.short %d\\n\"\n", Names[i]->ordinal - Base );
225 total_size += nb_names * sizeof(short);
226 if (nb_names % 2)
228 fprintf( outfile, " \"\\t.short 0\\n\"\n" );
229 total_size += sizeof(short);
233 /* output forward strings */
235 if (fwd_size)
237 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
238 for (i = Base; i <= Limit; i++)
240 ORDDEF *odp = Ordinals[i];
241 if (odp && odp->type == TYPE_FORWARD)
242 fprintf( outfile, " \"\\t" STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
244 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
245 total_size += (fwd_size + 3) & ~3;
248 /* output relays */
250 if (debugging)
252 for (i = Base; i <= Limit; i++)
254 ORDDEF *odp = Ordinals[i];
255 unsigned int j, args, mask = 0;
256 const char *name;
258 /* skip non-existent entry points */
259 if (!odp) goto ignore;
260 /* skip non-functions */
261 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
262 /* skip norelay entry points */
263 if (odp->flags & FLAG_NORELAY) goto ignore;
265 for (j = 0; odp->u.func.arg_types[j]; j++)
267 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
268 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
270 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
272 name = odp->link_name;
273 args = strlen(odp->u.func.arg_types) * sizeof(int);
274 if (odp->flags & FLAG_REGISTER)
276 name = make_internal_name( odp, "regs" );
277 args |= 0x8000;
280 switch(odp->type)
282 case TYPE_STDCALL:
283 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
284 fprintf( outfile, " \"\\tret $0x%04x\\n\"\n", args );
285 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
286 break;
287 case TYPE_CDECL:
288 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
289 fprintf( outfile, " \"\\tret\\n\"\n" );
290 fprintf( outfile, " \"\\t.short 0x%04x\\n\"\n", args );
291 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
292 break;
293 default:
294 assert(0);
296 continue;
298 ignore:
299 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
304 /* output __wine_dllexport symbols */
306 for (i = 0; i < nb_names; i++)
308 if (Names[i]->flags & FLAG_NOIMPORT) continue;
309 /* check for invalid characters in the name */
310 for (p = Names[i]->name; *p; p++)
311 if (!isalnum(*p) && *p != '_' && *p != '.') break;
312 if (*p) continue;
313 fprintf( outfile, " \"\\t.globl " __ASM_NAME("__wine_dllexport_%s_%d_%s") "\\n\"\n",
314 make_c_identifier(DLLFileName), i, Names[i]->name );
315 fprintf( outfile, " \"" __ASM_NAME("__wine_dllexport_%s_%d_%s") ":\\n\"\n",
316 make_c_identifier(DLLFileName), i, Names[i]->name );
319 /* output ordinal exports */
321 for (i = 0; i < nb_entry_points; i++)
323 ORDDEF *odp = EntryPoints[i];
325 if (odp->flags & FLAG_NOIMPORT) continue;
326 if (odp->name || !odp->export_name) continue;
327 /* check for invalid characters in the name */
328 for (p = odp->export_name; *p; p++)
329 if (!isalnum(*p) && *p != '_' && *p != '.') break;
330 if (*p) continue;
331 fprintf( outfile, " \"\\t.globl " __ASM_NAME("__wine_ordexport_%s_%d_%s") "\\n\"\n",
332 make_c_identifier(DLLFileName), odp->ordinal, odp->export_name );
333 fprintf( outfile, " \"" __ASM_NAME("__wine_ordexport_%s_%d_%s") ":\\n\"\n",
334 make_c_identifier(DLLFileName), odp->ordinal, odp->export_name );
336 fprintf( outfile, " \"\\t.long 0xffffffff\\n\"\n" );
338 /* output variables */
340 for (i = 0; i < nb_entry_points; i++)
342 ORDDEF *odp = EntryPoints[i];
343 if (odp->type == TYPE_VARIABLE)
345 int j;
346 fprintf( outfile, " \"%s:\\n\"\n", make_internal_name( odp, "var" ) );
347 fprintf( outfile, " \"\\t.long " );
348 for (j = 0; j < odp->u.var.n_values; j++)
350 fprintf( outfile, "0x%08x", odp->u.var.values[j] );
351 if (j < odp->u.var.n_values-1) fputc( ',', outfile );
353 fprintf( outfile, "\\n\"\n" );
357 fprintf( outfile, " \"\\t.text\\n\"\n" );
358 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
359 fprintf( outfile, ");\n\n" );
361 return total_size;
365 /*******************************************************************
366 * output_stub_funcs
368 * Output the functions for stub entry points
370 static void output_stub_funcs( FILE *outfile )
372 int i;
374 for (i = 0; i < nb_entry_points; i++)
376 ORDDEF *odp = EntryPoints[i];
377 if (odp->type != TYPE_STUB) continue;
378 fprintf( outfile, "#ifdef __GNUC__\n" );
379 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
380 fprintf( outfile, "#endif\n\n" );
381 fprintf( outfile, "struct exc_record {\n" );
382 fprintf( outfile, " unsigned int code, flags;\n" );
383 fprintf( outfile, " void *rec, *addr;\n" );
384 fprintf( outfile, " unsigned int params;\n" );
385 fprintf( outfile, " const void *info[15];\n" );
386 fprintf( outfile, "};\n\n" );
387 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
388 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
389 fprintf( outfile, " struct exc_record rec;\n" );
390 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
391 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
392 fprintf( outfile, " rec.rec = 0;\n" );
393 fprintf( outfile, " rec.params = 2;\n" );
394 fprintf( outfile, " rec.info[0] = dllname;\n" );
395 fprintf( outfile, " rec.info[1] = func;\n" );
396 fprintf( outfile, "#ifdef __GNUC__\n" );
397 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
398 fprintf( outfile, "#else\n" );
399 fprintf( outfile, " rec.addr = 0;\n" );
400 fprintf( outfile, "#endif\n" );
401 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
402 break;
405 for (i = 0; i < nb_entry_points; i++)
407 ORDDEF *odp = EntryPoints[i];
408 if (odp->type != TYPE_STUB) continue;
409 fprintf( outfile, "void %s(void) ", make_internal_name( odp, "stub" ) );
410 if (odp->name)
411 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
412 else if (odp->export_name)
413 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
414 else
415 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
420 /*******************************************************************
421 * output_register_funcs
423 * Output the functions for register entry points
425 static void output_register_funcs( FILE *outfile )
427 const char *name;
428 int i;
430 for (i = 0; i < nb_entry_points; i++)
432 ORDDEF *odp = EntryPoints[i];
433 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
434 if (!(odp->flags & FLAG_REGISTER)) continue;
435 name = make_internal_name( odp, "regs" );
436 fprintf( outfile,
437 "asm(\".align %d\\n\\t\"\n"
438 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
439 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
440 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
441 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
442 " \".byte %d,%d\");\n",
443 get_alignment(4),
444 name, name, odp->link_name,
445 strlen(odp->u.func.arg_types) * sizeof(int),
446 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
451 /*******************************************************************
452 * output_dll_init
454 * Output code for calling a dll constructor and destructor.
456 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
458 fprintf( outfile, "#ifndef __GNUC__\n" );
459 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
460 fprintf( outfile, "#endif\n" );
462 #if defined(__i386__)
463 if (constructor)
465 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
466 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
467 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
469 if (destructor)
471 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
472 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
473 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
475 #elif defined(__sparc__)
476 if (constructor)
478 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
479 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
480 fprintf( outfile, " \"\\tnop\\n\"\n" );
481 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
483 if (destructor)
485 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
486 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
487 fprintf( outfile, " \"\\tnop\\n\"\n" );
488 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
490 #elif defined(__PPC__)
491 if (constructor)
493 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
494 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
495 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
497 if (destructor)
499 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
500 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
501 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
503 #else
504 #error You need to define the DLL constructor for your architecture
505 #endif
506 fprintf( outfile, "#ifndef __GNUC__\n" );
507 fprintf( outfile, "}\n" );
508 fprintf( outfile, "#endif\n" );
512 /*******************************************************************
513 * BuildSpec32File
515 * Build a Win32 C file from a spec file.
517 void BuildSpec32File( FILE *outfile )
519 int exports_size = 0;
520 int nr_exports, nr_imports, nr_resources;
521 int characteristics, subsystem;
522 DWORD page_size;
523 char constructor[100];
525 #ifdef HAVE_GETPAGESIZE
526 page_size = getpagesize();
527 #elif defined(__svr4__)
528 page_size = sysconf(_SC_PAGESIZE);
529 #elif defined(_WINDOWS)
531 SYSTEM_INFO si;
532 GetSystemInfo(&si);
533 page_size = si.dwPageSize;
535 #else
536 # error Cannot get the page size on this platform
537 #endif
539 AssignOrdinals();
540 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
542 resolve_imports();
543 output_standard_file_header( outfile );
545 /* Reserve some space for the PE header */
547 fprintf( outfile, "extern char pe_header[];\n" );
548 fprintf( outfile, "#ifndef __GNUC__\n" );
549 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
550 fprintf( outfile, "#endif\n" );
551 fprintf( outfile, "asm(\".section \\\".text\\\"\\n\\t\"\n" );
552 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
553 fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t.skip 65536\\n\\t\");\n" );
554 fprintf( outfile, "#ifndef __GNUC__\n" );
555 fprintf( outfile, "}\n" );
556 fprintf( outfile, "#endif\n" );
558 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLFileName );
559 fprintf( outfile, "extern int __wine_spec_exports[];\n\n" );
561 #ifdef __i386__
562 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
563 #else
564 fprintf( outfile, "#define __stdcall\n\n" );
565 #endif
567 if (nr_exports)
569 /* Output the stub functions */
571 output_stub_funcs( outfile );
573 fprintf( outfile, "#ifndef __GNUC__\n" );
574 fprintf( outfile, "static void __asm__dummy(void) {\n" );
575 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
577 /* Output code for all register functions */
579 output_register_funcs( outfile );
581 /* Output the exports and relay entry points */
583 exports_size = output_exports( outfile, nr_exports );
585 fprintf( outfile, "#ifndef __GNUC__\n" );
586 fprintf( outfile, "}\n" );
587 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
590 /* Output the DLL imports */
592 nr_imports = output_imports( outfile );
594 /* Output the resources */
596 nr_resources = output_resources( outfile );
598 /* Output LibMain function */
600 characteristics = subsystem = 0;
601 switch(SpecMode)
603 case SPEC_MODE_DLL:
604 if (init_func) fprintf( outfile, "extern void %s();\n", init_func );
605 characteristics = IMAGE_FILE_DLL;
606 break;
607 case SPEC_MODE_GUIEXE:
608 if (!init_func) init_func = "WinMain";
609 fprintf( outfile,
610 "\ntypedef struct {\n"
611 " unsigned int cb;\n"
612 " char *lpReserved, *lpDesktop, *lpTitle;\n"
613 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
614 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
615 " unsigned short wShowWindow, cbReserved2;\n"
616 " char *lpReserved2;\n"
617 " void *hStdInput, *hStdOutput, *hStdError;\n"
618 "} STARTUPINFOA;\n"
619 "int _ARGC;\n"
620 "char **_ARGV;\n"
621 "extern int __stdcall %s(void *,void *,char *,int);\n"
622 "extern char * __stdcall GetCommandLineA(void);\n"
623 "extern void * __stdcall GetModuleHandleA(char *);\n"
624 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
625 "extern void __stdcall ExitProcess(unsigned int);\n"
626 "static void __wine_exe_main(void)\n"
627 "{\n"
628 " extern int __wine_main_argc;\n"
629 " extern char **__wine_main_argv;\n"
630 " STARTUPINFOA info;\n"
631 " char *cmdline = GetCommandLineA();\n"
632 " int bcount=0, in_quotes=0;\n"
633 " while (*cmdline) {\n"
634 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
635 " else if (*cmdline=='\\\\') bcount++;\n"
636 " else if (*cmdline=='\\\"') {\n"
637 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
638 " bcount=0;\n"
639 " }\n"
640 " else bcount=0;\n"
641 " cmdline++;\n"
642 " }\n"
643 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
644 " GetStartupInfoA( &info );\n"
645 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
646 " _ARGC = __wine_main_argc;\n"
647 " _ARGV = __wine_main_argv;\n"
648 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
649 "}\n\n", init_func, init_func );
650 init_func = "__wine_exe_main";
651 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
652 break;
653 case SPEC_MODE_GUIEXE_UNICODE:
654 if (!init_func) init_func = "WinMain";
655 fprintf( outfile,
656 "\ntypedef unsigned short WCHAR;\n"
657 "typedef struct {\n"
658 " unsigned int cb;\n"
659 " char *lpReserved, *lpDesktop, *lpTitle;\n"
660 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
661 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
662 " unsigned short wShowWindow, cbReserved2;\n"
663 " char *lpReserved2;\n"
664 " void *hStdInput, *hStdOutput, *hStdError;\n"
665 "} STARTUPINFOA;\n"
666 "int _ARGC;\n"
667 "WCHAR **_ARGV;\n"
668 "extern int __stdcall %s(void *,void *,char *,int);\n"
669 "extern char * __stdcall GetCommandLineA(void);\n"
670 "extern void * __stdcall GetModuleHandleA(char *);\n"
671 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
672 "extern void __stdcall ExitProcess(unsigned int);\n"
673 "static void __wine_exe_main(void)\n"
674 "{\n"
675 " extern int __wine_main_argc;\n"
676 " extern WCHAR **__wine_main_wargv;\n"
677 " STARTUPINFOA info;\n"
678 " char *cmdline = GetCommandLineA();\n"
679 " int bcount=0, in_quotes=0;\n"
680 " while (*cmdline) {\n"
681 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
682 " else if (*cmdline=='\\\\') bcount++;\n"
683 " else if (*cmdline=='\\\"') {\n"
684 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
685 " bcount=0;\n"
686 " }\n"
687 " else bcount=0;\n"
688 " cmdline++;\n"
689 " }\n"
690 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
691 " GetStartupInfoA( &info );\n"
692 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
693 " _ARGC = __wine_main_argc;\n"
694 " _ARGV = __wine_main_wargv;\n"
695 " ExitProcess( %s( GetModuleHandleA(0), 0, cmdline, info.wShowWindow ) );\n"
696 "}\n\n", init_func, init_func );
697 init_func = "__wine_exe_main";
698 subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
699 break;
700 case SPEC_MODE_CUIEXE:
701 if (!init_func) init_func = "main";
702 fprintf( outfile,
703 "\nint _ARGC;\n"
704 "char **_ARGV;\n"
705 "extern void __stdcall ExitProcess(int);\n"
706 "static void __wine_exe_main(void)\n"
707 "{\n"
708 " extern int %s( int argc, char *argv[] );\n"
709 " extern int __wine_main_argc;\n"
710 " extern char **__wine_main_argv;\n"
711 " _ARGC = __wine_main_argc;\n"
712 " _ARGV = __wine_main_argv;\n"
713 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
714 "}\n\n", init_func, init_func );
715 init_func = "__wine_exe_main";
716 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
717 break;
718 case SPEC_MODE_CUIEXE_UNICODE:
719 if (!init_func) init_func = "wmain";
720 fprintf( outfile,
721 "\ntypedef unsigned short WCHAR;\n"
722 "int _ARGC;\n"
723 "WCHAR **_ARGV;\n"
724 "extern void __stdcall ExitProcess(int);\n"
725 "static void __wine_exe_main(void)\n"
726 "{\n"
727 " extern int %s( int argc, WCHAR *argv[] );\n"
728 " extern int __wine_main_argc;\n"
729 " extern WCHAR **__wine_main_wargv;\n"
730 " _ARGC = __wine_main_argc;\n"
731 " _ARGV = __wine_main_wargv;\n"
732 " ExitProcess( %s( _ARGC, _ARGV ) );\n"
733 "}\n\n", init_func, init_func );
734 init_func = "__wine_exe_main";
735 subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
736 break;
739 /* Output the NT header */
741 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
742 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
743 fprintf( outfile, " int Signature;\n" );
744 fprintf( outfile, " struct file_header {\n" );
745 fprintf( outfile, " short Machine;\n" );
746 fprintf( outfile, " short NumberOfSections;\n" );
747 fprintf( outfile, " int TimeDateStamp;\n" );
748 fprintf( outfile, " void *PointerToSymbolTable;\n" );
749 fprintf( outfile, " int NumberOfSymbols;\n" );
750 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
751 fprintf( outfile, " short Characteristics;\n" );
752 fprintf( outfile, " } FileHeader;\n" );
753 fprintf( outfile, " struct opt_header {\n" );
754 fprintf( outfile, " short Magic;\n" );
755 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
756 fprintf( outfile, " int SizeOfCode;\n" );
757 fprintf( outfile, " int SizeOfInitializedData;\n" );
758 fprintf( outfile, " int SizeOfUninitializedData;\n" );
759 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
760 fprintf( outfile, " void *BaseOfCode;\n" );
761 fprintf( outfile, " void *BaseOfData;\n" );
762 fprintf( outfile, " void *ImageBase;\n" );
763 fprintf( outfile, " int SectionAlignment;\n" );
764 fprintf( outfile, " int FileAlignment;\n" );
765 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
766 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
767 fprintf( outfile, " short MajorImageVersion;\n" );
768 fprintf( outfile, " short MinorImageVersion;\n" );
769 fprintf( outfile, " short MajorSubsystemVersion;\n" );
770 fprintf( outfile, " short MinorSubsystemVersion;\n" );
771 fprintf( outfile, " int Win32VersionValue;\n" );
772 fprintf( outfile, " int SizeOfImage;\n" );
773 fprintf( outfile, " int SizeOfHeaders;\n" );
774 fprintf( outfile, " int CheckSum;\n" );
775 fprintf( outfile, " short Subsystem;\n" );
776 fprintf( outfile, " short DllCharacteristics;\n" );
777 fprintf( outfile, " int SizeOfStackReserve;\n" );
778 fprintf( outfile, " int SizeOfStackCommit;\n" );
779 fprintf( outfile, " int SizeOfHeapReserve;\n" );
780 fprintf( outfile, " int SizeOfHeapCommit;\n" );
781 fprintf( outfile, " int LoaderFlags;\n" );
782 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
783 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
784 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
785 fprintf( outfile, " } OptionalHeader;\n" );
786 fprintf( outfile, "} nt_header = {\n" );
787 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
789 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
790 fprintf( outfile, " 0, 0, 0, 0,\n" );
791 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
792 fprintf( outfile, " 0x%04x },\n", characteristics ); /* Characteristics */
794 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
795 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
796 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
797 fprintf( outfile, " %s,\n", init_func ? init_func : "0" ); /* AddressOfEntryPoint */
798 fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */
799 fprintf( outfile, " pe_header,\n" ); /* ImageBase */
800 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
801 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
802 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
803 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
804 fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */
805 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
806 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */
807 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
808 fprintf( outfile, " 0,\n" ); /* CheckSum */
809 fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */
810 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
811 fprintf( outfile, " %d, 0,\n", stack_size*1024 ); /* SizeOfStackReserve/Commit */
812 fprintf( outfile, " %d, 0,\n", DLLHeapSize*1024 );/* SizeOfHeapReserve/Commit */
813 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
814 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
815 fprintf( outfile, " {\n" );
816 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
817 exports_size ? "__wine_spec_exports" : "0", exports_size );
818 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
819 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
820 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
821 nr_resources ? "&resources" : "0", nr_resources ? "sizeof(resources)" : "0" );
822 fprintf( outfile, " }\n }\n};\n\n" );
824 /* Output the DLL constructor */
826 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLFileName) );
827 output_dll_init( outfile, constructor, NULL );
829 fprintf( outfile,
830 "void %s(void)\n"
831 "{\n"
832 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
833 " extern void *__wine_dbg_register( char * const *, int );\n"
834 " __wine_dll_register( &nt_header, \"%s\" );\n"
835 "}\n",
836 constructor, DLLFileName );
840 /*******************************************************************
841 * BuildDef32File
843 * Build a Win32 def file from a spec file.
845 void BuildDef32File(FILE *outfile)
847 const char *name;
848 int i;
850 AssignOrdinals();
852 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
853 input_file_name );
855 fprintf(outfile, "LIBRARY %s\n\n", DLLFileName);
857 fprintf(outfile, "EXPORTS\n");
859 /* Output the exports and relay entry points */
861 for(i = 0; i < nb_entry_points; i++)
863 ORDDEF *odp = EntryPoints[i];
865 if (!odp) continue;
866 if (odp->flags & (FLAG_NOIMPORT|FLAG_REGISTER)) continue;
867 if (odp->type == TYPE_STUB) continue;
869 if (odp->name) name = odp->name;
870 else if (odp->export_name) name = odp->export_name;
871 else continue;
873 fprintf(outfile, " %s", name);
875 switch(odp->type)
877 case TYPE_EXTERN:
878 case TYPE_VARARGS:
879 case TYPE_CDECL:
880 case TYPE_VARIABLE:
881 /* try to reduce output */
882 if(strcmp(name, odp->link_name))
883 fprintf(outfile, "=%s", odp->link_name);
884 break;
885 case TYPE_STDCALL:
887 #ifdef NEED_STDCALL_DECORATION
888 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
889 fprintf(outfile, "@%d", at_param);
890 #endif /* NEED_STDCALL_DECORATION */
891 /* try to reduce output */
892 if(strcmp(name, odp->link_name))
894 fprintf(outfile, "=%s", odp->link_name);
895 #ifdef NEED_STDCALL_DECORATION
896 fprintf(outfile, "@%d", at_param);
897 #endif /* NEED_STDCALL_DECORATION */
899 break;
901 case TYPE_FORWARD:
902 fprintf(outfile, "=%s", odp->link_name);
903 break;
904 default:
905 assert(0);
907 fprintf(outfile, " @%d%s\n", odp->ordinal, odp->name ? "" : " NONAME" );
912 /*******************************************************************
913 * BuildDebugFile
915 * Build the debugging channels source file.
917 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
919 int nr_debug;
920 char *prefix, *p;
922 while (*argv) parse_debug_channels( srcdir, *argv++ );
924 output_standard_file_header( outfile );
925 nr_debug = output_debug( outfile );
926 if (!nr_debug)
928 fprintf( outfile, "/* no debug channels found for this module */\n" );
929 return;
932 if (output_file_name)
934 if ((p = strrchr( output_file_name, '/' ))) p++;
935 prefix = xstrdup( p ? p : output_file_name );
936 if ((p = strchr( prefix, '.' ))) *p = 0;
937 strcpy( p, make_c_identifier(p) );
939 else prefix = xstrdup( "_" );
941 /* Output the DLL constructor */
943 fprintf( outfile,
944 "#ifdef __GNUC__\n"
945 "static void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
946 "static void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
947 "#else\n"
948 "static void __asm__dummy_dll_init(void) {\n",
949 prefix, prefix );
951 #if defined(__i386__)
952 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
953 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
954 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
955 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
956 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
957 #elif defined(__sparc__)
958 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
959 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
960 fprintf( outfile, " \"\\tnop\\n\"\n" );
961 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
962 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
963 fprintf( outfile, " \"\\tnop\\n\"\n" );
964 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
965 #elif defined(__PPC__)
966 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
967 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
968 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
969 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
970 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
971 #else
972 #error You need to define the DLL constructor for your architecture
973 #endif
974 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n" );
976 fprintf( outfile,
977 "\n#ifdef __GNUC__\n"
978 "static\n"
979 "#endif\n"
980 "void __wine_dbg_%s_init(void)\n"
981 "{\n"
982 " extern void *__wine_dbg_register( char * const *, int );\n"
983 " debug_registration = __wine_dbg_register( debug_channels, %d );\n"
984 "}\n", prefix, nr_debug );
985 fprintf( outfile,
986 "\n#ifdef __GNUC__\n"
987 "static\n"
988 "#endif\n"
989 "void __wine_dbg_%s_fini(void)\n"
990 "{\n"
991 " extern void __wine_dbg_unregister( void* );\n"
992 " __wine_dbg_unregister( debug_registration );\n"
993 "}\n", prefix );
995 free( prefix );