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
26 #include "wine/port.h"
39 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
40 #define EH_NONCONTINUABLE 0x01
46 # define __ASM_SKIP ".space"
48 # define __ASM_SKIP ".skip"
51 static int string_compare( const void *ptr1
, const void *ptr2
)
53 const char * const *str1
= ptr1
;
54 const char * const *str2
= ptr2
;
55 return strcmp( *str1
, *str2
);
59 /*******************************************************************
62 * Generate an internal name for an entry point. Used for stubs etc.
64 static const char *make_internal_name( const ORDDEF
*odp
, DLLSPEC
*spec
, const char *prefix
)
66 static char buffer
[256];
67 if (odp
->name
|| odp
->export_name
)
70 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, spec
->file_name
,
71 odp
->name
? odp
->name
: odp
->export_name
);
72 /* make sure name is a legal C identifier */
73 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
74 if (!*p
) return buffer
;
76 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, make_c_identifier(spec
->file_name
), odp
->ordinal
);
80 /*******************************************************************
81 * declare_weak_function
83 * Output a prototype for a weak function.
85 static void declare_weak_function( FILE *outfile
, const char *ret_type
, const char *name
, const char *params
)
87 fprintf( outfile
, "#ifdef __GNUC__\n" );
88 fprintf( outfile
, "# ifdef __APPLE__\n" );
89 fprintf( outfile
, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type
, name
, params
);
90 fprintf( outfile
, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type
, name
, params
, name
);
91 fprintf( outfile
, "#define %s __wine_spec_weak_%s\n", name
, name
);
92 fprintf( outfile
, "# else\n" );
93 fprintf( outfile
, "extern %s %s(%s) __attribute__((weak));\n", ret_type
, name
, params
);
94 fprintf( outfile
, "# endif\n" );
95 fprintf( outfile
, "#else\n" );
96 fprintf( outfile
, "extern %s %s(%s);\n", ret_type
, name
, params
);
97 fprintf( outfile
, "static void __asm__dummy_%s(void)", name
);
98 fprintf( outfile
, " { asm(\".weak " __ASM_NAME("%s") "\"); }\n", name
);
99 fprintf( outfile
, "#endif\n\n" );
103 /*******************************************************************
106 * Output the debug channels.
108 static int output_debug( FILE *outfile
)
112 if (!nb_debug_channels
) return 0;
113 qsort( debug_channels
, nb_debug_channels
, sizeof(debug_channels
[0]), string_compare
);
115 for (i
= 0; i
< nb_debug_channels
; i
++)
116 fprintf( outfile
, "char __wine_dbch_%s[] = \"\\003%s\";\n",
117 debug_channels
[i
], debug_channels
[i
] );
119 fprintf( outfile
, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels
);
120 for (i
= 0; i
< nb_debug_channels
; i
++)
122 fprintf( outfile
, " __wine_dbch_%s", debug_channels
[i
] );
123 if (i
< nb_debug_channels
- 1) fprintf( outfile
, ",\n" );
125 fprintf( outfile
, "\n};\n\n" );
126 fprintf( outfile
, "static void *debug_registration;\n\n" );
128 return nb_debug_channels
;
132 /*******************************************************************
135 * Output the export table for a Win32 module.
137 static int output_exports( FILE *outfile
, int nr_exports
, DLLSPEC
*spec
)
139 int i
, fwd_size
= 0, total_size
= 0;
141 if (!nr_exports
) return 0;
143 fprintf( outfile
, "asm(\".data\\n\"\n" );
144 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
145 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
147 /* export directory header */
149 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
150 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
151 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
152 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
153 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->base
); /* Base */
154 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nr_exports
); /* NumberOfFunctions */
155 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->nb_names
); /* NumberOfNames */
156 fprintf( outfile
, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
159 fprintf( outfile
, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
160 fprintf( outfile
, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
164 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
165 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
167 total_size
+= 10 * sizeof(int);
169 /* output the function pointers */
171 fprintf( outfile
, " \"__wine_spec_exports_funcs:\\n\"\n" );
172 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
174 ORDDEF
*odp
= spec
->ordinals
[i
];
175 if (!odp
) fprintf( outfile
, " \"\\t.long 0\\n\"\n" );
176 else switch(odp
->type
)
182 if (!(odp
->flags
& FLAG_FORWARD
))
184 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
185 (odp
->flags
& FLAG_REGISTER
) ? make_internal_name( odp
, spec
, "regs" )
190 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
191 fwd_size
+= strlen(odp
->link_name
) + 1;
195 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
196 make_internal_name( odp
, spec
, "stub" ) );
202 total_size
+= (spec
->limit
- spec
->base
+ 1) * sizeof(int);
206 /* output the function name pointers */
208 int namepos
= strlen(spec
->file_name
) + 1;
210 fprintf( outfile
, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
211 for (i
= 0; i
< spec
->nb_names
; i
++)
213 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos
);
214 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
216 total_size
+= spec
->nb_names
* sizeof(int);
219 /* output the function names */
221 fprintf( outfile
, " \"\\t.text\\n\"\n" );
222 fprintf( outfile
, " \"__wine_spec_exp_names:\\n\"\n" );
223 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", spec
->file_name
);
224 for (i
= 0; i
< spec
->nb_names
; i
++)
225 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", spec
->names
[i
]->name
);
226 fprintf( outfile
, " \"\\t.data\\n\"\n" );
230 /* output the function ordinals */
232 fprintf( outfile
, " \"__wine_spec_exp_ordinals:\\n\"\n" );
233 for (i
= 0; i
< spec
->nb_names
; i
++)
235 fprintf( outfile
, " \"\\t" __ASM_SHORT
" %d\\n\"\n",
236 spec
->names
[i
]->ordinal
- spec
->base
);
238 total_size
+= spec
->nb_names
* sizeof(short);
239 if (spec
->nb_names
% 2)
241 fprintf( outfile
, " \"\\t" __ASM_SHORT
" 0\\n\"\n" );
242 total_size
+= sizeof(short);
246 /* output forward strings */
250 fprintf( outfile
, " \"__wine_spec_forwards:\\n\"\n" );
251 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
253 ORDDEF
*odp
= spec
->ordinals
[i
];
254 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
255 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", odp
->link_name
);
257 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
258 total_size
+= (fwd_size
+ 3) & ~3;
265 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
267 ORDDEF
*odp
= spec
->ordinals
[i
];
268 unsigned int j
, args
, mask
= 0;
271 /* skip non-existent entry points */
272 if (!odp
) goto ignore
;
273 /* skip non-functions */
274 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) goto ignore
;
275 /* skip norelay and forward entry points */
276 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) goto ignore
;
278 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
280 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
281 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
283 if ((odp
->flags
& FLAG_RET64
) && (j
< 16)) mask
|= 0x80000000;
285 name
= odp
->link_name
;
286 args
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
287 if (odp
->flags
& FLAG_REGISTER
) name
= make_internal_name( odp
, spec
, "regs" );
292 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
293 fprintf( outfile
, " \"\\tret $%d\\n\"\n", args
);
294 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
297 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
298 fprintf( outfile
, " \"\\tret\\n\"\n" );
299 fprintf( outfile
, " \"\\t" __ASM_SHORT
" %d\\n\"\n", args
);
300 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
308 fprintf( outfile
, " \"\\t.long 0,0,0,0\\n\"\n" );
312 fprintf( outfile
, " \"\\t.text\\n\"\n" );
313 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
314 fprintf( outfile
, ");\n\n" );
320 /*******************************************************************
323 * Output the functions for stub entry points
325 static void output_stub_funcs( FILE *outfile
, DLLSPEC
*spec
)
329 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
331 ORDDEF
*odp
= &spec
->entry_points
[i
];
332 if (odp
->type
!= TYPE_STUB
) continue;
333 fprintf( outfile
, "#ifdef __GNUC__\n" );
334 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
335 fprintf( outfile
, "#endif\n\n" );
336 fprintf( outfile
, "struct exc_record {\n" );
337 fprintf( outfile
, " unsigned int code, flags;\n" );
338 fprintf( outfile
, " void *rec, *addr;\n" );
339 fprintf( outfile
, " unsigned int params;\n" );
340 fprintf( outfile
, " const void *info[15];\n" );
341 fprintf( outfile
, "};\n\n" );
342 fprintf( outfile
, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
343 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
344 fprintf( outfile
, " struct exc_record rec;\n" );
345 fprintf( outfile
, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB
);
346 fprintf( outfile
, " rec.flags = %d;\n", EH_NONCONTINUABLE
);
347 fprintf( outfile
, " rec.rec = 0;\n" );
348 fprintf( outfile
, " rec.params = 2;\n" );
349 fprintf( outfile
, " rec.info[0] = \"%s\";\n", spec
->file_name
);
350 fprintf( outfile
, " rec.info[1] = func;\n" );
351 fprintf( outfile
, "#ifdef __GNUC__\n" );
352 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
353 fprintf( outfile
, "#else\n" );
354 fprintf( outfile
, " rec.addr = 0;\n" );
355 fprintf( outfile
, "#endif\n" );
356 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
360 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
362 const ORDDEF
*odp
= &spec
->entry_points
[i
];
363 if (odp
->type
!= TYPE_STUB
) continue;
364 fprintf( outfile
, "void %s(void) ", make_internal_name( odp
, spec
, "stub" ) );
366 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->name
);
367 else if (odp
->export_name
)
368 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->export_name
);
370 fprintf( outfile
, "{ __wine_unimplemented(\"%d\"); }\n", odp
->ordinal
);
375 /*******************************************************************
376 * output_register_funcs
378 * Output the functions for register entry points
380 static void output_register_funcs( FILE *outfile
, DLLSPEC
*spec
)
385 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
387 const ORDDEF
*odp
= &spec
->entry_points
[i
];
388 if (odp
->type
!= TYPE_STDCALL
&& odp
->type
!= TYPE_CDECL
) continue;
389 if (!(odp
->flags
& FLAG_REGISTER
)) continue;
390 if (odp
->flags
& FLAG_FORWARD
) continue;
391 name
= make_internal_name( odp
, spec
, "regs" );
393 "asm(\".align %d\\n\\t\"\n"
394 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
395 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
396 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
397 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
398 " \".byte %d,%d\");\n",
400 name
, name
, odp
->link_name
,
401 strlen(odp
->u
.func
.arg_types
) * sizeof(int),
402 (odp
->type
== TYPE_CDECL
) ? 0 : (strlen(odp
->u
.func
.arg_types
) * sizeof(int)) );
407 /*******************************************************************
410 * Output code for calling a dll constructor and destructor.
412 void output_dll_init( FILE *outfile
, const char *constructor
, const char *destructor
)
414 fprintf( outfile
, "#ifndef __GNUC__\n" );
415 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
416 fprintf( outfile
, "#endif\n" );
418 #if defined(__i386__)
421 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
422 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
423 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
427 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
428 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
429 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
431 #elif defined(__sparc__)
434 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
435 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
436 fprintf( outfile
, " \"\\tnop\\n\"\n" );
437 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
441 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
442 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
443 fprintf( outfile
, " \"\\tnop\\n\"\n" );
444 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
446 #elif defined(__powerpc__)
448 /* Mach-O doesn't have an init section */
451 fprintf( outfile
, "asm(\"\\t.mod_init_func\\n\"\n" );
452 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
453 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor
);
454 fprintf( outfile
, " \"\\t.text\\n\");\n" );
458 fprintf( outfile
, "asm(\"\\t.mod_term_func\\n\"\n" );
459 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
460 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor
);
461 fprintf( outfile
, " \"\\t.text\\n\");\n" );
463 # else /* __APPLE__ */
466 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
467 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor
);
468 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
472 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
473 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor
);
474 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
476 # endif /* __APPLE__ */
478 #error You need to define the DLL constructor for your architecture
480 fprintf( outfile
, "#ifndef __GNUC__\n" );
481 fprintf( outfile
, "}\n" );
482 fprintf( outfile
, "#endif\n" );
486 /*******************************************************************
489 * Build a Win32 C file from a spec file.
491 void BuildSpec32File( FILE *outfile
, DLLSPEC
*spec
)
493 int exports_size
= 0;
494 int nr_exports
, nr_imports
;
496 const char *init_func
= spec
->init_func
;
498 #ifdef HAVE_GETPAGESIZE
499 page_size
= getpagesize();
500 #elif defined(__svr4__)
501 page_size
= sysconf(_SC_PAGESIZE
);
502 #elif defined(_WINDOWS)
506 page_size
= si
.dwPageSize
;
509 # error Cannot get the page size on this platform
512 nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
513 resolve_imports( spec
);
514 output_standard_file_header( outfile
);
516 /* Reserve some space for the PE header */
518 fprintf( outfile
, "extern char __wine_spec_pe_header[];\n" );
519 fprintf( outfile
, "#ifndef __GNUC__\n" );
520 fprintf( outfile
, "static void __asm__dummy_header(void) {\n" );
521 fprintf( outfile
, "#endif\n" );
522 fprintf( outfile
, "asm(\".text\\n\\t\"\n" );
523 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
524 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP
" 65536\\n\\t\"\n" );
525 fprintf( outfile
, " \".data\\n\\t\"\n" );
526 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(4) );
527 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
528 fprintf( outfile
, "#ifndef __GNUC__\n" );
529 fprintf( outfile
, "}\n" );
530 fprintf( outfile
, "#endif\n" );
533 fprintf( outfile
, "static char _end[4];\n" );
535 fprintf( outfile
, "extern char _end[];\n" );
538 fprintf( outfile
, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
541 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
543 fprintf( outfile
, "#define __stdcall\n\n" );
548 /* Output the stub functions */
550 output_stub_funcs( outfile
, spec
);
552 fprintf( outfile
, "#ifndef __GNUC__\n" );
553 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
554 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
556 /* Output code for all register functions */
558 output_register_funcs( outfile
, spec
);
560 /* Output the exports and relay entry points */
562 exports_size
= output_exports( outfile
, nr_exports
, spec
);
564 fprintf( outfile
, "#ifndef __GNUC__\n" );
565 fprintf( outfile
, "}\n" );
566 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
569 /* Output the DLL imports */
571 nr_imports
= output_imports( outfile
, spec
);
573 /* Output the resources */
575 output_resources( outfile
, spec
);
577 /* Output the entry point function */
579 fprintf( outfile
, "static int __wine_spec_init_state;\n" );
580 fprintf( outfile
, "extern int __wine_main_argc;\n" );
581 fprintf( outfile
, "extern char **__wine_main_argv;\n" );
582 fprintf( outfile
, "extern char **__wine_main_environ;\n" );
583 fprintf( outfile
, "extern unsigned short **__wine_main_wargv;\n" );
585 fprintf( outfile
, "extern _dyld_func_lookup(char *, void *);" );
586 fprintf( outfile
, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
587 fprintf( outfile
, "{\n" );
588 fprintf( outfile
, " void (*init)(void);\n" );
589 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
590 fprintf( outfile
, " init();\n" );
591 fprintf( outfile
, "}\n" );
592 fprintf( outfile
, "static void __wine_spec_hidden_fini()\n" );
593 fprintf( outfile
, "{\n" );
594 fprintf( outfile
, " void (*fini)(void);\n" );
595 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
596 fprintf( outfile
, " fini();\n" );
597 fprintf( outfile
, "}\n" );
598 fprintf( outfile
, "#define _init __wine_spec_hidden_init\n" );
599 fprintf( outfile
, "#define _fini __wine_spec_hidden_fini\n" );
601 fprintf( outfile
, "extern void _init(int, char**, char**);\n" );
602 fprintf( outfile
, "extern void _fini();\n" );
605 if (spec
->characteristics
& IMAGE_FILE_DLL
)
608 fprintf( outfile
, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func
);
611 declare_weak_function( outfile
, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
612 init_func
= "DllMain";
615 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
618 " if (reason == %d && __wine_spec_init_state == 1)\n"
619 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
620 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
621 " if (reason == %d && __wine_spec_init_state == 1) _fini();\n"
624 DLL_PROCESS_ATTACH
, init_func
, init_func
, DLL_PROCESS_DETACH
);
625 init_func
= "__wine_dll_main";
627 else switch(spec
->subsystem
)
629 case IMAGE_SUBSYSTEM_NATIVE
:
631 fprintf( outfile
, "extern int __stdcall %s( void*, void* );\n\n", init_func
);
634 declare_weak_function( outfile
, "int __stdcall", "DriverEntry", "void*, void*");
635 init_func
= "DriverEntry";
638 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
641 " if (__wine_spec_init_state == 1)\n"
642 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
643 " ret = %s ? %s( obj, path ) : 0;\n"
644 " if (__wine_spec_init_state == 1) _fini();\n"
647 init_func
, init_func
);
648 init_func
= "__wine_driver_entry";
650 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
651 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
653 fprintf( outfile
, "extern int %s( int argc, char *argv[] );\n", init_func
);
656 declare_weak_function( outfile
, "int", "main", "int argc, char *argv[]" );
657 declare_weak_function( outfile
, "int", "wmain", "int argc, unsigned short *argv[]" );
658 declare_weak_function( outfile
, "int __stdcall", "WinMain", "void *,void *,char *,int" );
661 "\ntypedef struct {\n"
662 " unsigned int cb;\n"
663 " char *lpReserved, *lpDesktop, *lpTitle;\n"
664 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
665 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
666 " unsigned short wShowWindow, cbReserved2;\n"
667 " char *lpReserved2;\n"
668 " void *hStdInput, *hStdOutput, *hStdError;\n"
670 "extern char * __stdcall GetCommandLineA(void);\n"
671 "extern void * __stdcall GetModuleHandleA(char *);\n"
672 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
673 "extern void __stdcall ExitProcess(unsigned int);\n"
674 "static void __wine_exe_main(void)\n"
677 " if (__wine_spec_init_state == 1)\n"
678 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
681 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func
);
685 " STARTUPINFOA info;\n"
686 " char *cmdline = GetCommandLineA();\n"
687 " int bcount=0, in_quotes=0;\n"
688 " while (*cmdline) {\n"
689 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
690 " else if (*cmdline=='\\\\') bcount++;\n"
691 " else if (*cmdline=='\\\"') {\n"
692 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
698 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
699 " GetStartupInfoA( &info );\n"
700 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
701 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
703 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
704 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
706 " if (__wine_spec_init_state == 1) _fini();\n"
707 " ExitProcess( ret );\n"
709 init_func
= "__wine_exe_main";
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
, " void *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 #elif defined(__powerpc__)
765 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC
); /* Machine */
767 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN
); /* Machine */
769 fprintf( outfile
, " 0, 0, 0, 0,\n" );
770 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
771 fprintf( outfile
, " 0x%04x },\n", spec
->characteristics
); /* Characteristics */
773 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
774 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
775 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
776 fprintf( outfile
, " %s,\n", init_func
); /* AddressOfEntryPoint */
777 fprintf( outfile
, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
778 fprintf( outfile
, " __wine_spec_pe_header,\n" ); /* ImageBase */
779 fprintf( outfile
, " %ld,\n", page_size
); /* SectionAlignment */
780 fprintf( outfile
, " %ld,\n", page_size
); /* FileAlignment */
781 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
782 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
783 fprintf( outfile
, " %d,\n", spec
->subsystem_major
); /* MajorSubsystemVersion */
784 fprintf( outfile
, " %d,\n", spec
->subsystem_minor
); /* MinorSubsystemVersion */
785 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
786 fprintf( outfile
, " _end,\n" ); /* SizeOfImage */
787 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfHeaders */
788 fprintf( outfile
, " 0,\n" ); /* CheckSum */
789 fprintf( outfile
, " 0x%04x,\n", spec
->subsystem
);/* Subsystem */
790 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
791 fprintf( outfile
, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
792 (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
793 fprintf( outfile
, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
794 (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
795 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
796 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
797 fprintf( outfile
, " {\n" );
798 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
799 exports_size
? "__wine_spec_exports" : "0", exports_size
);
800 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
801 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
802 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
803 spec
->nb_resources
? "&resources" : "0",
804 spec
->nb_resources
? "sizeof(resources)" : "0" );
805 fprintf( outfile
, " }\n }\n};\n\n" );
807 /* Output the DLL constructor */
810 "void __wine_spec_init(void)\n"
812 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
813 " __wine_spec_init_state = 1;\n"
814 " __wine_dll_register( &nt_header, \"%s\" );\n"
818 output_dll_init( outfile
, "__wine_spec_init_ctor", NULL
);
820 "void __wine_spec_init_ctor(void)\n"
822 " if (__wine_spec_init_state) return;\n"
823 " __wine_spec_init();\n"
824 " __wine_spec_init_state = 2;\n"
829 /*******************************************************************
832 * Build a Win32 def file from a spec file.
834 void BuildDef32File( FILE *outfile
, DLLSPEC
*spec
)
839 fprintf(outfile
, "; File generated automatically from %s; do not edit!\n\n",
842 fprintf(outfile
, "LIBRARY %s\n\n", spec
->file_name
);
844 fprintf(outfile
, "EXPORTS\n");
846 /* Output the exports and relay entry points */
848 for(i
= 0; i
< spec
->nb_entry_points
; i
++)
850 const ORDDEF
*odp
= &spec
->entry_points
[i
];
854 if (odp
->flags
& FLAG_REGISTER
) continue;
855 if (odp
->type
== TYPE_STUB
) continue;
857 if (odp
->name
) name
= odp
->name
;
858 else if (odp
->export_name
) name
= odp
->export_name
;
861 fprintf(outfile
, " %s", name
);
870 /* try to reduce output */
871 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
872 fprintf(outfile
, "=%s", odp
->link_name
);
876 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
877 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
878 if (odp
->flags
& FLAG_FORWARD
)
880 fprintf(outfile
, "=%s", odp
->link_name
);
882 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
884 fprintf(outfile
, "=%s", odp
->link_name
);
885 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
892 fprintf( outfile
, " @%d", odp
->ordinal
);
893 if (!odp
->name
) fprintf( outfile
, " NONAME" );
894 if (is_data
) fprintf( outfile
, " DATA" );
896 /* MinGW binutils cannot handle this correctly */
897 if (odp
->flags
& FLAG_PRIVATE
) fprintf( outfile
, " PRIVATE" );
899 fprintf( outfile
, "\n" );
904 /*******************************************************************
907 * Build the debugging channels source file.
909 void BuildDebugFile( FILE *outfile
, const char *srcdir
, char **argv
)
916 if (!parse_debug_channels( srcdir
, *argv
++ )) exit(1);
919 output_standard_file_header( outfile
);
920 nr_debug
= output_debug( outfile
);
923 fprintf( outfile
, "/* no debug channels found for this module */\n" );
927 if (output_file_name
)
929 if ((p
= strrchr( output_file_name
, '/' ))) p
++;
930 prefix
= xstrdup( p
? p
: output_file_name
);
931 if ((p
= strchr( prefix
, '.' ))) *p
= 0;
932 strcpy( p
, make_c_identifier(p
) );
934 else prefix
= xstrdup( "_" );
936 /* Output the DLL constructor */
940 "void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
941 "void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
943 "static void __asm__dummy_dll_init(void) {\n",
946 #if defined(__i386__)
947 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
948 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
949 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
950 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
951 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
952 #elif defined(__sparc__)
953 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
954 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
955 fprintf( outfile
, " \"\\tnop\\n\"\n" );
956 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
957 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
958 fprintf( outfile
, " \"\\tnop\\n\"\n" );
959 fprintf( outfile
, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
960 #elif defined(__powerpc__)
962 fprintf( outfile
, "asm(\"\\t.mod_init_func\\n\"\n" );
963 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
964 fprintf( outfile
, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
965 fprintf( outfile
, " \"\\t.text\\n\");\n" );
966 fprintf( outfile
, "asm(\"\\t.mod_term_func\\n\"\n" );
967 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
968 fprintf( outfile
, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
969 fprintf( outfile
, " \"\\t.text\\n\");\n" );
971 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
972 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
973 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
974 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
975 fprintf( outfile
, " \"\\t.text\\n\");\n" );
978 #error You need to define the DLL constructor for your architecture
980 fprintf( outfile
, "}\n#endif /* defined(__GNUC__) */\n\n" );
983 "void __wine_dbg_%s_init(void)\n"
985 " extern void *__wine_dbg_register( char * const *, int );\n"
986 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
987 "}\n\n", prefix
, nr_debug
);
989 "void __wine_dbg_%s_fini(void)\n"
991 " extern void __wine_dbg_unregister( void* );\n"
992 " __wine_dbg_unregister( debug_registration );\n"