Propsheet pages need WS_EX_CONTROLPARENT style.
[wine.git] / tools / winebuild / spec32.c
blob1dea09d0ed709d619f0f619618cba05d8be5879d
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 <stdarg.h>
31 #include <string.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wine/exception.h"
36 #include "build.h"
39 #ifdef __APPLE__
40 # define __ASM_SKIP ".space"
41 #else
42 # define __ASM_SKIP ".skip"
43 #endif
45 static int string_compare( const void *ptr1, const void *ptr2 )
47 const char * const *str1 = ptr1;
48 const char * const *str2 = ptr2;
49 return strcmp( *str1, *str2 );
53 /*******************************************************************
54 * make_internal_name
56 * Generate an internal name for an entry point. Used for stubs etc.
58 static const char *make_internal_name( const ORDDEF *odp, DLLSPEC *spec, const char *prefix )
60 static char buffer[256];
61 if (odp->name || odp->export_name)
63 char *p;
64 sprintf( buffer, "__wine_%s_%s_%s", prefix, spec->file_name,
65 odp->name ? odp->name : odp->export_name );
66 /* make sure name is a legal C identifier */
67 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
68 if (!*p) return buffer;
70 sprintf( buffer, "__wine_%s_%s_%d", prefix, make_c_identifier(spec->file_name), odp->ordinal );
71 return buffer;
74 /*******************************************************************
75 * declare_weak_function
77 * Output a prototype for a weak function.
79 static void declare_weak_function( FILE *outfile, const char *ret_type, const char *name, const char *params)
81 fprintf( outfile, "#ifdef __GNUC__\n" );
82 fprintf( outfile, "# ifdef __APPLE__\n" );
83 fprintf( outfile, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type, name, params );
84 fprintf( outfile, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type, name, params, name );
85 fprintf( outfile, "#define %s __wine_spec_weak_%s\n", name, name );
86 fprintf( outfile, "# else\n" );
87 fprintf( outfile, "extern %s %s(%s) __attribute__((weak));\n", ret_type, name, params );
88 fprintf( outfile, "# endif\n" );
89 fprintf( outfile, "#else\n" );
90 fprintf( outfile, "extern %s %s(%s);\n", ret_type, name, params );
91 fprintf( outfile, "static void __asm__dummy_%s(void)", name );
92 fprintf( outfile, " { asm(\".weak " __ASM_NAME("%s") "\"); }\n", name );
93 fprintf( outfile, "#endif\n\n" );
97 /*******************************************************************
98 * output_debug
100 * Output the debug channels.
102 static int output_debug( FILE *outfile )
104 int i;
106 if (!nb_debug_channels) return 0;
107 qsort( debug_channels, nb_debug_channels, sizeof(debug_channels[0]), string_compare );
109 for (i = 0; i < nb_debug_channels; i++)
110 fprintf( outfile, "char __wine_dbch_%s[] = \"\\003%s\";\n",
111 debug_channels[i], debug_channels[i] );
113 fprintf( outfile, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels );
114 for (i = 0; i < nb_debug_channels; i++)
116 fprintf( outfile, " __wine_dbch_%s", debug_channels[i] );
117 if (i < nb_debug_channels - 1) fprintf( outfile, ",\n" );
119 fprintf( outfile, "\n};\n\n" );
120 fprintf( outfile, "static void *debug_registration;\n\n" );
122 return nb_debug_channels;
126 /*******************************************************************
127 * output_exports
129 * Output the export table for a Win32 module.
131 static int output_exports( FILE *outfile, int nr_exports, DLLSPEC *spec )
133 int i, fwd_size = 0, total_size = 0;
135 if (!nr_exports) return 0;
137 fprintf( outfile, "asm(\".data\\n\"\n" );
138 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
139 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
141 /* export directory header */
143 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
144 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
145 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
146 fprintf( outfile, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
147 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->base ); /* Base */
148 fprintf( outfile, " \"\\t.long %d\\n\"\n", nr_exports ); /* NumberOfFunctions */
149 fprintf( outfile, " \"\\t.long %d\\n\"\n", spec->nb_names ); /* NumberOfNames */
150 fprintf( outfile, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
151 if (spec->nb_names)
153 fprintf( outfile, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
154 fprintf( outfile, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
156 else
158 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
159 fprintf( outfile, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
161 total_size += 10 * sizeof(int);
163 /* output the function pointers */
165 fprintf( outfile, " \"__wine_spec_exports_funcs:\\n\"\n" );
166 for (i = spec->base; i <= spec->limit; i++)
168 ORDDEF *odp = spec->ordinals[i];
169 if (!odp) fprintf( outfile, " \"\\t.long 0\\n\"\n" );
170 else switch(odp->type)
172 case TYPE_EXTERN:
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, spec, "regs" )
180 : odp->link_name );
182 else
184 fprintf( outfile, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size );
185 fwd_size += strlen(odp->link_name) + 1;
187 break;
188 case TYPE_STUB:
189 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
190 make_internal_name( odp, spec, "stub" ) );
191 break;
192 default:
193 assert(0);
196 total_size += (spec->limit - spec->base + 1) * sizeof(int);
198 if (spec->nb_names)
200 /* output the function name pointers */
202 int namepos = strlen(spec->file_name) + 1;
204 fprintf( outfile, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
205 for (i = 0; i < spec->nb_names; i++)
207 fprintf( outfile, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos );
208 namepos += strlen(spec->names[i]->name) + 1;
210 total_size += spec->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 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", spec->file_name );
218 for (i = 0; i < spec->nb_names; i++)
219 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", spec->names[i]->name );
220 fprintf( outfile, " \"\\t.data\\n\"\n" );
222 if (spec->nb_names)
224 /* output the function ordinals */
226 fprintf( outfile, " \"__wine_spec_exp_ordinals:\\n\"\n" );
227 for (i = 0; i < spec->nb_names; i++)
229 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n",
230 spec->names[i]->ordinal - spec->base );
232 total_size += spec->nb_names * sizeof(short);
233 if (spec->nb_names % 2)
235 fprintf( outfile, " \"\\t" __ASM_SHORT " 0\\n\"\n" );
236 total_size += sizeof(short);
240 /* output forward strings */
242 if (fwd_size)
244 fprintf( outfile, " \"__wine_spec_forwards:\\n\"\n" );
245 for (i = spec->base; i <= spec->limit; i++)
247 ORDDEF *odp = spec->ordinals[i];
248 if (odp && (odp->flags & FLAG_FORWARD))
249 fprintf( outfile, " \"\\t" __ASM_STRING " \\\"%s\\\"\\n\"\n", odp->link_name );
251 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
252 total_size += (fwd_size + 3) & ~3;
255 /* output relays */
257 if (debugging)
259 for (i = spec->base; i <= spec->limit; i++)
261 ORDDEF *odp = spec->ordinals[i];
262 unsigned int j, args, mask = 0;
263 const char *name;
265 /* skip non-existent entry points */
266 if (!odp) goto ignore;
267 /* skip non-functions */
268 if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) goto ignore;
269 /* skip norelay and forward entry points */
270 if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) goto ignore;
272 for (j = 0; odp->u.func.arg_types[j]; j++)
274 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
275 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
277 if ((odp->flags & FLAG_RET64) && (j < 16)) mask |= 0x80000000;
279 name = odp->link_name;
280 args = strlen(odp->u.func.arg_types) * sizeof(int);
281 if (odp->flags & FLAG_REGISTER) name = make_internal_name( odp, spec, "regs" );
283 switch(odp->type)
285 case TYPE_STDCALL:
286 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
287 fprintf( outfile, " \"\\tret $%d\\n\"\n", args );
288 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
289 break;
290 case TYPE_CDECL:
291 fprintf( outfile, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name );
292 fprintf( outfile, " \"\\tret\\n\"\n" );
293 fprintf( outfile, " \"\\t" __ASM_SHORT " %d\\n\"\n", args );
294 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name, mask );
295 break;
296 default:
297 assert(0);
299 continue;
301 ignore:
302 fprintf( outfile, " \"\\t.long 0,0,0,0\\n\"\n" );
306 fprintf( outfile, " \"\\t.text\\n\"\n" );
307 fprintf( outfile, " \"\\t.align %d\\n\"\n", get_alignment(4) );
308 fprintf( outfile, ");\n\n" );
310 return total_size;
314 /*******************************************************************
315 * output_stub_funcs
317 * Output the functions for stub entry points
319 static void output_stub_funcs( FILE *outfile, DLLSPEC *spec )
321 int i;
323 for (i = 0; i < spec->nb_entry_points; i++)
325 ORDDEF *odp = &spec->entry_points[i];
326 if (odp->type != TYPE_STUB) continue;
327 fprintf( outfile, "#ifdef __GNUC__\n" );
328 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
329 fprintf( outfile, "#endif\n\n" );
330 fprintf( outfile, "struct exc_record {\n" );
331 fprintf( outfile, " unsigned int code, flags;\n" );
332 fprintf( outfile, " void *rec, *addr;\n" );
333 fprintf( outfile, " unsigned int params;\n" );
334 fprintf( outfile, " const void *info[15];\n" );
335 fprintf( outfile, "};\n\n" );
336 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
337 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
338 fprintf( outfile, " struct exc_record rec;\n" );
339 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
340 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
341 fprintf( outfile, " rec.rec = 0;\n" );
342 fprintf( outfile, " rec.params = 2;\n" );
343 fprintf( outfile, " rec.info[0] = \"%s\";\n", spec->file_name );
344 fprintf( outfile, " rec.info[1] = func;\n" );
345 fprintf( outfile, "#ifdef __GNUC__\n" );
346 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
347 fprintf( outfile, "#else\n" );
348 fprintf( outfile, " rec.addr = 0;\n" );
349 fprintf( outfile, "#endif\n" );
350 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
351 break;
354 for (i = 0; i < spec->nb_entry_points; i++)
356 const ORDDEF *odp = &spec->entry_points[i];
357 if (odp->type != TYPE_STUB) continue;
358 fprintf( outfile, "void %s(void) ", make_internal_name( odp, spec, "stub" ) );
359 if (odp->name)
360 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->name );
361 else if (odp->export_name)
362 fprintf( outfile, "{ __wine_unimplemented(\"%s\"); }\n", odp->export_name );
363 else
364 fprintf( outfile, "{ __wine_unimplemented(\"%d\"); }\n", odp->ordinal );
369 /*******************************************************************
370 * output_register_funcs
372 * Output the functions for register entry points
374 static void output_register_funcs( FILE *outfile, DLLSPEC *spec )
376 const char *name;
377 int i;
379 for (i = 0; i < spec->nb_entry_points; i++)
381 const ORDDEF *odp = &spec->entry_points[i];
382 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
383 if (!(odp->flags & FLAG_REGISTER)) continue;
384 if (odp->flags & FLAG_FORWARD) continue;
385 name = make_internal_name( odp, spec, "regs" );
386 fprintf( outfile,
387 "asm(\".align %d\\n\\t\"\n"
388 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
389 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
390 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
391 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
392 " \".byte %d,%d\");\n",
393 get_alignment(4),
394 name, name, odp->link_name,
395 strlen(odp->u.func.arg_types) * sizeof(int),
396 (odp->type == TYPE_CDECL) ? 0 : (strlen(odp->u.func.arg_types) * sizeof(int)) );
401 /*******************************************************************
402 * output_dll_init
404 * Output code for calling a dll constructor and destructor.
406 void output_dll_init( FILE *outfile, const char *constructor, const char *destructor )
408 fprintf( outfile, "#ifndef __GNUC__\n" );
409 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
410 fprintf( outfile, "#endif\n" );
412 #if defined(__i386__)
413 if (constructor)
415 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
416 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
417 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
419 if (destructor)
421 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
422 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor );
423 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
425 #elif defined(__sparc__)
426 if (constructor)
428 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
429 fprintf( outfile, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor );
430 fprintf( outfile, " \"\\tnop\\n\"\n" );
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, " \"\\tnop\\n\"\n" );
438 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
440 #elif defined(__powerpc__)
441 # ifdef __APPLE__
442 /* Mach-O doesn't have an init section */
443 if (constructor)
445 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
446 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
447 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor );
448 fprintf( outfile, " \"\\t.text\\n\");\n" );
450 if (destructor)
452 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
453 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
454 fprintf( outfile, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor );
455 fprintf( outfile, " \"\\t.text\\n\");\n" );
457 # else /* __APPLE__ */
458 if (constructor)
460 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
461 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor );
462 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
464 if (destructor)
466 fprintf( outfile, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
467 fprintf( outfile, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor );
468 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
470 # endif /* __APPLE__ */
471 #else
472 #error You need to define the DLL constructor for your architecture
473 #endif
474 fprintf( outfile, "#ifndef __GNUC__\n" );
475 fprintf( outfile, "}\n" );
476 fprintf( outfile, "#endif\n" );
480 /*******************************************************************
481 * BuildSpec32File
483 * Build a Win32 C file from a spec file.
485 void BuildSpec32File( FILE *outfile, DLLSPEC *spec )
487 int exports_size = 0;
488 int nr_exports, nr_imports;
489 DWORD page_size;
490 const char *init_func = spec->init_func;
492 #ifdef HAVE_GETPAGESIZE
493 page_size = getpagesize();
494 #elif defined(__svr4__)
495 page_size = sysconf(_SC_PAGESIZE);
496 #elif defined(_WINDOWS)
498 SYSTEM_INFO si;
499 GetSystemInfo(&si);
500 page_size = si.dwPageSize;
502 #else
503 # error Cannot get the page size on this platform
504 #endif
506 nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
507 resolve_imports( spec );
508 output_standard_file_header( outfile );
510 /* Reserve some space for the PE header */
512 fprintf( outfile, "extern char __wine_spec_pe_header[];\n" );
513 fprintf( outfile, "#ifndef __GNUC__\n" );
514 fprintf( outfile, "static void __asm__dummy_header(void) {\n" );
515 fprintf( outfile, "#endif\n" );
516 fprintf( outfile, "asm(\".text\\n\\t\"\n" );
517 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) );
518 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\"\n" );
519 fprintf( outfile, " \".data\\n\\t\"\n" );
520 fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) );
521 fprintf( outfile, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
522 fprintf( outfile, "#ifndef __GNUC__\n" );
523 fprintf( outfile, "}\n" );
524 fprintf( outfile, "#endif\n" );
526 #ifdef __APPLE__
527 fprintf( outfile, "static char _end[4];\n" );
528 #else
529 fprintf( outfile, "extern char _end[];\n" );
530 #endif
532 fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
534 #ifdef __i386__
535 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
536 #else
537 fprintf( outfile, "#define __stdcall\n\n" );
538 #endif
540 if (nr_exports)
542 /* Output the stub functions */
544 output_stub_funcs( outfile, spec );
546 fprintf( outfile, "#ifndef __GNUC__\n" );
547 fprintf( outfile, "static void __asm__dummy(void) {\n" );
548 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
550 /* Output code for all register functions */
552 output_register_funcs( outfile, spec );
554 /* Output the exports and relay entry points */
556 exports_size = output_exports( outfile, nr_exports, spec );
558 fprintf( outfile, "#ifndef __GNUC__\n" );
559 fprintf( outfile, "}\n" );
560 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
563 /* Output the DLL imports */
565 nr_imports = output_imports( outfile, spec );
567 /* Output the resources */
569 output_resources( outfile, spec );
571 /* Output the entry point function */
573 fprintf( outfile, "static int __wine_spec_init_state;\n" );
574 fprintf( outfile, "extern int __wine_main_argc;\n" );
575 fprintf( outfile, "extern char **__wine_main_argv;\n" );
576 fprintf( outfile, "extern char **__wine_main_environ;\n" );
577 fprintf( outfile, "extern unsigned short **__wine_main_wargv;\n" );
578 #ifdef __APPLE__
579 fprintf( outfile, "extern _dyld_func_lookup(char *, void *);" );
580 fprintf( outfile, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
581 fprintf( outfile, "{\n" );
582 fprintf( outfile, " void (*init)(void);\n" );
583 fprintf( outfile, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
584 fprintf( outfile, " init();\n" );
585 fprintf( outfile, "}\n" );
586 fprintf( outfile, "static void __wine_spec_hidden_fini()\n" );
587 fprintf( outfile, "{\n" );
588 fprintf( outfile, " void (*fini)(void);\n" );
589 fprintf( outfile, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
590 fprintf( outfile, " fini();\n" );
591 fprintf( outfile, "}\n" );
592 fprintf( outfile, "#define _init __wine_spec_hidden_init\n" );
593 fprintf( outfile, "#define _fini __wine_spec_hidden_fini\n" );
594 #else
595 fprintf( outfile, "extern void _init(int, char**, char**);\n" );
596 fprintf( outfile, "extern void _fini();\n" );
597 #endif
599 if (spec->characteristics & IMAGE_FILE_DLL)
601 if (init_func)
602 fprintf( outfile, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func );
603 else
605 declare_weak_function( outfile, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
606 init_func = "DllMain";
608 fprintf( outfile,
609 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
610 "{\n"
611 " int ret;\n"
612 " if (reason == %d && __wine_spec_init_state == 1)\n"
613 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
614 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
615 " if (reason == %d && __wine_spec_init_state == 1) _fini();\n"
616 " return ret;\n"
617 "}\n",
618 DLL_PROCESS_ATTACH, init_func, init_func, DLL_PROCESS_DETACH );
619 init_func = "__wine_dll_main";
621 else switch(spec->subsystem)
623 case IMAGE_SUBSYSTEM_NATIVE:
624 if (init_func)
625 fprintf( outfile, "extern int __stdcall %s( void*, void* );\n\n", init_func );
626 else
628 declare_weak_function( outfile, "int __stdcall", "DriverEntry", "void*, void*");
629 init_func = "DriverEntry";
631 fprintf( outfile,
632 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
633 "{\n"
634 " int ret;\n"
635 " if (__wine_spec_init_state == 1)\n"
636 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
637 " ret = %s ? %s( obj, path ) : 0;\n"
638 " if (__wine_spec_init_state == 1) _fini();\n"
639 " return ret;\n"
640 "}\n",
641 init_func, init_func );
642 init_func = "__wine_driver_entry";
643 break;
644 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
645 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
646 if (init_func)
647 fprintf( outfile, "extern int %s( int argc, char *argv[] );\n", init_func );
648 else
650 declare_weak_function( outfile, "int", "main", "int argc, char *argv[]" );
651 declare_weak_function( outfile, "int", "wmain", "int argc, unsigned short *argv[]" );
652 declare_weak_function( outfile, "int __stdcall", "WinMain", "void *,void *,char *,int" );
654 fprintf( outfile,
655 "\ntypedef struct {\n"
656 " unsigned int cb;\n"
657 " char *lpReserved, *lpDesktop, *lpTitle;\n"
658 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
659 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
660 " unsigned short wShowWindow, cbReserved2;\n"
661 " char *lpReserved2;\n"
662 " void *hStdInput, *hStdOutput, *hStdError;\n"
663 "} STARTUPINFOA;\n"
664 "extern char * __stdcall GetCommandLineA(void);\n"
665 "extern void * __stdcall GetModuleHandleA(char *);\n"
666 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
667 "extern void __stdcall ExitProcess(unsigned int);\n"
668 "static void __wine_exe_main(void)\n"
669 "{\n"
670 " int ret;\n"
671 " if (__wine_spec_init_state == 1)\n"
672 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
673 if (init_func)
674 fprintf( outfile,
675 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func );
676 else
677 fprintf( outfile,
678 " if (WinMain) {\n"
679 " STARTUPINFOA info;\n"
680 " char *cmdline = GetCommandLineA();\n"
681 " int bcount=0, in_quotes=0;\n"
682 " while (*cmdline) {\n"
683 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
684 " else if (*cmdline=='\\\\') bcount++;\n"
685 " else if (*cmdline=='\\\"') {\n"
686 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
687 " bcount=0;\n"
688 " }\n"
689 " else bcount=0;\n"
690 " cmdline++;\n"
691 " }\n"
692 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
693 " GetStartupInfoA( &info );\n"
694 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
695 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
696 " }\n"
697 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
698 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
699 fprintf( outfile,
700 " if (__wine_spec_init_state == 1) _fini();\n"
701 " ExitProcess( ret );\n"
702 "}\n\n" );
703 init_func = "__wine_exe_main";
704 break;
707 /* Output the NT header */
709 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
710 fprintf( outfile, "static const struct image_nt_headers\n{\n" );
711 fprintf( outfile, " int Signature;\n" );
712 fprintf( outfile, " struct file_header {\n" );
713 fprintf( outfile, " short Machine;\n" );
714 fprintf( outfile, " short NumberOfSections;\n" );
715 fprintf( outfile, " int TimeDateStamp;\n" );
716 fprintf( outfile, " void *PointerToSymbolTable;\n" );
717 fprintf( outfile, " int NumberOfSymbols;\n" );
718 fprintf( outfile, " short SizeOfOptionalHeader;\n" );
719 fprintf( outfile, " short Characteristics;\n" );
720 fprintf( outfile, " } FileHeader;\n" );
721 fprintf( outfile, " struct opt_header {\n" );
722 fprintf( outfile, " short Magic;\n" );
723 fprintf( outfile, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
724 fprintf( outfile, " int SizeOfCode;\n" );
725 fprintf( outfile, " int SizeOfInitializedData;\n" );
726 fprintf( outfile, " int SizeOfUninitializedData;\n" );
727 fprintf( outfile, " void *AddressOfEntryPoint;\n" );
728 fprintf( outfile, " void *BaseOfCode;\n" );
729 fprintf( outfile, " void *BaseOfData;\n" );
730 fprintf( outfile, " void *ImageBase;\n" );
731 fprintf( outfile, " int SectionAlignment;\n" );
732 fprintf( outfile, " int FileAlignment;\n" );
733 fprintf( outfile, " short MajorOperatingSystemVersion;\n" );
734 fprintf( outfile, " short MinorOperatingSystemVersion;\n" );
735 fprintf( outfile, " short MajorImageVersion;\n" );
736 fprintf( outfile, " short MinorImageVersion;\n" );
737 fprintf( outfile, " short MajorSubsystemVersion;\n" );
738 fprintf( outfile, " short MinorSubsystemVersion;\n" );
739 fprintf( outfile, " int Win32VersionValue;\n" );
740 fprintf( outfile, " void *SizeOfImage;\n" );
741 fprintf( outfile, " int SizeOfHeaders;\n" );
742 fprintf( outfile, " int CheckSum;\n" );
743 fprintf( outfile, " short Subsystem;\n" );
744 fprintf( outfile, " short DllCharacteristics;\n" );
745 fprintf( outfile, " int SizeOfStackReserve;\n" );
746 fprintf( outfile, " int SizeOfStackCommit;\n" );
747 fprintf( outfile, " int SizeOfHeapReserve;\n" );
748 fprintf( outfile, " int SizeOfHeapCommit;\n" );
749 fprintf( outfile, " int LoaderFlags;\n" );
750 fprintf( outfile, " int NumberOfRvaAndSizes;\n" );
751 fprintf( outfile, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
752 IMAGE_NUMBEROF_DIRECTORY_ENTRIES );
753 fprintf( outfile, " } OptionalHeader;\n" );
754 fprintf( outfile, "} nt_header = {\n" );
755 fprintf( outfile, " 0x%04x,\n", IMAGE_NT_SIGNATURE ); /* Signature */
756 #ifdef __i386__
757 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386 ); /* Machine */
758 #elif defined(__powerpc__)
759 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC ); /* Machine */
760 #else
761 fprintf( outfile, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN ); /* Machine */
762 #endif
763 fprintf( outfile, " 0, 0, 0, 0,\n" );
764 fprintf( outfile, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
765 fprintf( outfile, " 0x%04x },\n", spec->characteristics ); /* Characteristics */
767 fprintf( outfile, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC ); /* Magic */
768 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
769 fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
770 fprintf( outfile, " %s,\n", init_func ); /* AddressOfEntryPoint */
771 fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
772 fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */
773 fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */
774 fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */
775 fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
776 fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */
777 fprintf( outfile, " %d,\n", spec->subsystem_major ); /* MajorSubsystemVersion */
778 fprintf( outfile, " %d,\n", spec->subsystem_minor ); /* MinorSubsystemVersion */
779 fprintf( outfile, " 0,\n" ); /* Win32VersionValue */
780 fprintf( outfile, " _end,\n" ); /* SizeOfImage */
781 fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */
782 fprintf( outfile, " 0,\n" ); /* CheckSum */
783 fprintf( outfile, " 0x%04x,\n", spec->subsystem );/* Subsystem */
784 fprintf( outfile, " 0,\n" ); /* DllCharacteristics */
785 fprintf( outfile, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
786 (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
787 fprintf( outfile, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
788 (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
789 fprintf( outfile, " 0,\n" ); /* LoaderFlags */
790 fprintf( outfile, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES ); /* NumberOfRvaAndSizes */
791 fprintf( outfile, " {\n" );
792 fprintf( outfile, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
793 exports_size ? "__wine_spec_exports" : "0", exports_size );
794 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
795 nr_imports ? "&imports" : "0", nr_imports ? "sizeof(imports)" : "0" );
796 fprintf( outfile, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
797 spec->nb_resources ? "&resources" : "0",
798 spec->nb_resources ? "sizeof(resources)" : "0" );
799 fprintf( outfile, " }\n }\n};\n\n" );
801 /* Output the DLL constructor */
803 fprintf( outfile,
804 "void __wine_spec_init(void)\n"
805 "{\n"
806 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
807 " __wine_spec_init_state = 1;\n"
808 " __wine_dll_register( &nt_header, \"%s\" );\n"
809 "}\n\n",
810 spec->file_name );
812 output_dll_init( outfile, "__wine_spec_init_ctor", NULL );
813 fprintf( outfile,
814 "void __wine_spec_init_ctor(void)\n"
815 "{\n"
816 " if (__wine_spec_init_state) return;\n"
817 " __wine_spec_init();\n"
818 " __wine_spec_init_state = 2;\n"
819 "}\n" );
823 /*******************************************************************
824 * BuildDef32File
826 * Build a Win32 def file from a spec file.
828 void BuildDef32File( FILE *outfile, DLLSPEC *spec )
830 const char *name;
831 int i;
833 fprintf(outfile, "; File generated automatically from %s; do not edit!\n\n",
834 input_file_name );
836 fprintf(outfile, "LIBRARY %s\n\n", spec->file_name);
838 fprintf(outfile, "EXPORTS\n");
840 /* Output the exports and relay entry points */
842 for(i = 0; i < spec->nb_entry_points; i++)
844 const ORDDEF *odp = &spec->entry_points[i];
845 int is_data = 0;
847 if (!odp) continue;
848 if (odp->flags & FLAG_REGISTER) continue;
849 if (odp->type == TYPE_STUB) continue;
851 if (odp->name) name = odp->name;
852 else if (odp->export_name) name = odp->export_name;
853 else continue;
855 fprintf(outfile, " %s", name);
857 switch(odp->type)
859 case TYPE_EXTERN:
860 is_data = 1;
861 /* fall through */
862 case TYPE_VARARGS:
863 case TYPE_CDECL:
864 /* try to reduce output */
865 if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
866 fprintf(outfile, "=%s", odp->link_name);
867 break;
868 case TYPE_STDCALL:
870 int at_param = strlen(odp->u.func.arg_types) * sizeof(int);
871 if (!kill_at) fprintf(outfile, "@%d", at_param);
872 if (odp->flags & FLAG_FORWARD)
874 fprintf(outfile, "=%s", odp->link_name);
876 else if (strcmp(name, odp->link_name)) /* try to reduce output */
878 fprintf(outfile, "=%s", odp->link_name);
879 if (!kill_at) fprintf(outfile, "@%d", at_param);
881 break;
883 default:
884 assert(0);
886 fprintf( outfile, " @%d", odp->ordinal );
887 if (!odp->name) fprintf( outfile, " NONAME" );
888 if (is_data) fprintf( outfile, " DATA" );
889 if (odp->flags & FLAG_PRIVATE) fprintf( outfile, " PRIVATE" );
890 fprintf( outfile, "\n" );
895 /*******************************************************************
896 * BuildDebugFile
898 * Build the debugging channels source file.
900 void BuildDebugFile( FILE *outfile, const char *srcdir, char **argv )
902 int nr_debug;
903 char *prefix, *p;
905 while (*argv)
907 if (!parse_debug_channels( srcdir, *argv++ )) exit(1);
910 output_standard_file_header( outfile );
911 nr_debug = output_debug( outfile );
912 if (!nr_debug)
914 fprintf( outfile, "/* no debug channels found for this module */\n" );
915 return;
918 if (output_file_name)
920 if ((p = strrchr( output_file_name, '/' ))) p++;
921 prefix = xstrdup( p ? p : output_file_name );
922 if ((p = strchr( prefix, '.' ))) *p = 0;
923 strcpy( p, make_c_identifier(p) );
925 else prefix = xstrdup( "_" );
927 /* Output the DLL constructor */
929 fprintf( outfile,
930 "#ifdef __GNUC__\n"
931 "void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
932 "void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
933 "#else\n"
934 "static void __asm__dummy_dll_init(void) {\n",
935 prefix, prefix );
937 #if defined(__i386__)
938 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
939 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
940 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
941 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
942 fprintf( outfile, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
943 #elif defined(__sparc__)
944 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
945 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
946 fprintf( outfile, " \"\\tnop\\n\"\n" );
947 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
948 fprintf( outfile, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
949 fprintf( outfile, " \"\\tnop\\n\"\n" );
950 fprintf( outfile, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
951 #elif defined(__powerpc__)
952 # ifdef __APPLE__
953 fprintf( outfile, "asm(\"\\t.mod_init_func\\n\"\n" );
954 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
955 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
956 fprintf( outfile, " \"\\t.text\\n\");\n" );
957 fprintf( outfile, "asm(\"\\t.mod_term_func\\n\"\n" );
958 fprintf( outfile, " \"\\t.align 2\\n\"\n" );
959 fprintf( outfile, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
960 fprintf( outfile, " \"\\t.text\\n\");\n" );
961 # else
962 fprintf( outfile, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
963 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix );
964 fprintf( outfile, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
965 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix );
966 fprintf( outfile, " \"\\t.text\\n\");\n" );
967 # endif
968 #else
969 #error You need to define the DLL constructor for your architecture
970 #endif
971 fprintf( outfile, "}\n#endif /* defined(__GNUC__) */\n\n" );
973 fprintf( outfile,
974 "void __wine_dbg_%s_init(void)\n"
975 "{\n"
976 " extern void *__wine_dbg_register( char * const *, int );\n"
977 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
978 "}\n\n", prefix, nr_debug );
979 fprintf( outfile,
980 "void __wine_dbg_%s_fini(void)\n"
981 "{\n"
982 " extern void __wine_dbg_unregister( void* );\n"
983 " __wine_dbg_unregister( debug_registration );\n"
984 "}\n", prefix );
986 free( prefix );