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
34 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
35 #define EH_NONCONTINUABLE 0x01
41 # define __ASM_SKIP ".space"
43 # define __ASM_SKIP ".skip"
46 static int string_compare( const void *ptr1
, const void *ptr2
)
48 const char * const *str1
= ptr1
;
49 const char * const *str2
= ptr2
;
50 return strcmp( *str1
, *str2
);
54 /*******************************************************************
57 * Generate an internal name for an entry point. Used for stubs etc.
59 static const char *make_internal_name( const ORDDEF
*odp
, DLLSPEC
*spec
, const char *prefix
)
61 static char buffer
[256];
62 if (odp
->name
|| odp
->export_name
)
65 sprintf( buffer
, "__wine_%s_%s_%s", prefix
, spec
->file_name
,
66 odp
->name
? odp
->name
: odp
->export_name
);
67 /* make sure name is a legal C identifier */
68 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
69 if (!*p
) return buffer
;
71 sprintf( buffer
, "__wine_%s_%s_%d", prefix
, make_c_identifier(spec
->file_name
), odp
->ordinal
);
75 /*******************************************************************
76 * declare_weak_function
78 * Output a prototype for a weak function.
80 static void declare_weak_function( FILE *outfile
, const char *ret_type
, const char *name
, const char *params
)
82 fprintf( outfile
, "#ifdef __GNUC__\n" );
83 fprintf( outfile
, "# ifdef __APPLE__\n" );
84 fprintf( outfile
, "extern %s %s(%s) __attribute__((weak_import));\n", ret_type
, name
, params
);
85 fprintf( outfile
, "static %s (*__wine_spec_weak_%s)(%s) = %s;\n", ret_type
, name
, params
, name
);
86 fprintf( outfile
, "#define %s __wine_spec_weak_%s\n", name
, name
);
87 fprintf( outfile
, "# else\n" );
88 fprintf( outfile
, "extern %s %s(%s) __attribute__((weak));\n", ret_type
, name
, params
);
89 fprintf( outfile
, "# endif\n" );
90 fprintf( outfile
, "#else\n" );
91 fprintf( outfile
, "extern %s %s(%s);\n", ret_type
, name
, params
);
92 fprintf( outfile
, "static void __asm__dummy_%s(void)", name
);
93 fprintf( outfile
, " { asm(\".weak " __ASM_NAME("%s") "\"); }\n", name
);
94 fprintf( outfile
, "#endif\n\n" );
98 /*******************************************************************
101 * Output the debug channels.
103 static int output_debug( FILE *outfile
)
107 if (!nb_debug_channels
) return 0;
108 qsort( debug_channels
, nb_debug_channels
, sizeof(debug_channels
[0]), string_compare
);
110 for (i
= 0; i
< nb_debug_channels
; i
++)
111 fprintf( outfile
, "char __wine_dbch_%s[] = \"\\003%s\";\n",
112 debug_channels
[i
], debug_channels
[i
] );
114 fprintf( outfile
, "\nstatic char * const debug_channels[%d] =\n{\n", nb_debug_channels
);
115 for (i
= 0; i
< nb_debug_channels
; i
++)
117 fprintf( outfile
, " __wine_dbch_%s", debug_channels
[i
] );
118 if (i
< nb_debug_channels
- 1) fprintf( outfile
, ",\n" );
120 fprintf( outfile
, "\n};\n\n" );
121 fprintf( outfile
, "static void *debug_registration;\n\n" );
123 return nb_debug_channels
;
127 /*******************************************************************
130 * Output the export table for a Win32 module.
132 static int output_exports( FILE *outfile
, int nr_exports
, DLLSPEC
*spec
)
134 int i
, fwd_size
= 0, total_size
= 0;
136 if (!nr_exports
) return 0;
138 fprintf( outfile
, "asm(\".data\\n\"\n" );
139 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
140 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_exports") ":\\n\"\n" );
142 /* export directory header */
144 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* Characteristics */
145 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* TimeDateStamp */
146 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* MajorVersion/MinorVersion */
147 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names\\n\"\n" ); /* Name */
148 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->base
); /* Base */
149 fprintf( outfile
, " \"\\t.long %d\\n\"\n", nr_exports
); /* NumberOfFunctions */
150 fprintf( outfile
, " \"\\t.long %d\\n\"\n", spec
->nb_names
); /* NumberOfNames */
151 fprintf( outfile
, " \"\\t.long __wine_spec_exports_funcs\\n\"\n" ); /* AddressOfFunctions */
154 fprintf( outfile
, " \"\\t.long __wine_spec_exp_name_ptrs\\n\"\n" ); /* AddressOfNames */
155 fprintf( outfile
, " \"\\t.long __wine_spec_exp_ordinals\\n\"\n" ); /* AddressOfNameOrdinals */
159 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNames */
160 fprintf( outfile
, " \"\\t.long 0\\n\"\n" ); /* AddressOfNameOrdinals */
162 total_size
+= 10 * sizeof(int);
164 /* output the function pointers */
166 fprintf( outfile
, " \"__wine_spec_exports_funcs:\\n\"\n" );
167 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
169 ORDDEF
*odp
= spec
->ordinals
[i
];
170 if (!odp
) fprintf( outfile
, " \"\\t.long 0\\n\"\n" );
171 else switch(odp
->type
)
177 if (!(odp
->flags
& FLAG_FORWARD
))
179 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
180 (odp
->flags
& FLAG_REGISTER
) ? make_internal_name( odp
, spec
, "regs" )
185 fprintf( outfile
, " \"\\t.long __wine_spec_forwards+%d\\n\"\n", fwd_size
);
186 fwd_size
+= strlen(odp
->link_name
) + 1;
190 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n",
191 make_internal_name( odp
, spec
, "stub" ) );
197 total_size
+= (spec
->limit
- spec
->base
+ 1) * sizeof(int);
201 /* output the function name pointers */
203 int namepos
= strlen(spec
->file_name
) + 1;
205 fprintf( outfile
, " \"__wine_spec_exp_name_ptrs:\\n\"\n" );
206 for (i
= 0; i
< spec
->nb_names
; i
++)
208 fprintf( outfile
, " \"\\t.long __wine_spec_exp_names+%d\\n\"\n", namepos
);
209 namepos
+= strlen(spec
->names
[i
]->name
) + 1;
211 total_size
+= spec
->nb_names
* sizeof(int);
214 /* output the function names */
216 fprintf( outfile
, " \"\\t.text\\n\"\n" );
217 fprintf( outfile
, " \"__wine_spec_exp_names:\\n\"\n" );
218 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", spec
->file_name
);
219 for (i
= 0; i
< spec
->nb_names
; i
++)
220 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", spec
->names
[i
]->name
);
221 fprintf( outfile
, " \"\\t.data\\n\"\n" );
225 /* output the function ordinals */
227 fprintf( outfile
, " \"__wine_spec_exp_ordinals:\\n\"\n" );
228 for (i
= 0; i
< spec
->nb_names
; i
++)
230 fprintf( outfile
, " \"\\t" __ASM_SHORT
" %d\\n\"\n",
231 spec
->names
[i
]->ordinal
- spec
->base
);
233 total_size
+= spec
->nb_names
* sizeof(short);
234 if (spec
->nb_names
% 2)
236 fprintf( outfile
, " \"\\t" __ASM_SHORT
" 0\\n\"\n" );
237 total_size
+= sizeof(short);
241 /* output forward strings */
245 fprintf( outfile
, " \"__wine_spec_forwards:\\n\"\n" );
246 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
248 ORDDEF
*odp
= spec
->ordinals
[i
];
249 if (odp
&& (odp
->flags
& FLAG_FORWARD
))
250 fprintf( outfile
, " \"\\t" __ASM_STRING
" \\\"%s\\\"\\n\"\n", odp
->link_name
);
252 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
253 total_size
+= (fwd_size
+ 3) & ~3;
260 for (i
= spec
->base
; i
<= spec
->limit
; i
++)
262 ORDDEF
*odp
= spec
->ordinals
[i
];
263 unsigned int j
, args
, mask
= 0;
266 /* skip nonexistent entry points */
267 if (!odp
) goto ignore
;
268 /* skip non-functions */
269 if ((odp
->type
!= TYPE_STDCALL
) && (odp
->type
!= TYPE_CDECL
)) goto ignore
;
270 /* skip norelay and forward entry points */
271 if (odp
->flags
& (FLAG_NORELAY
|FLAG_FORWARD
)) goto ignore
;
273 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
275 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
276 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
278 if ((odp
->flags
& FLAG_RET64
) && (j
< 16)) mask
|= 0x80000000;
280 name
= odp
->link_name
;
281 args
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
282 if (odp
->flags
& FLAG_REGISTER
) name
= make_internal_name( odp
, spec
, "regs" );
287 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
288 fprintf( outfile
, " \"\\tret $%d\\n\"\n", args
);
289 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
292 fprintf( outfile
, " \"\\tjmp " __ASM_NAME("%s") "\\n\"\n", name
);
293 fprintf( outfile
, " \"\\tret\\n\"\n" );
294 fprintf( outfile
, " \"\\t" __ASM_SHORT
" %d\\n\"\n", args
);
295 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") ",0x%08x\\n\"\n", name
, mask
);
303 fprintf( outfile
, " \"\\t.long 0,0,0,0\\n\"\n" );
307 fprintf( outfile
, " \"\\t.text\\n\"\n" );
308 fprintf( outfile
, " \"\\t.align %d\\n\"\n", get_alignment(4) );
309 fprintf( outfile
, ");\n\n" );
315 /*******************************************************************
318 * Output the functions for stub entry points
320 static void output_stub_funcs( FILE *outfile
, DLLSPEC
*spec
)
324 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
326 ORDDEF
*odp
= &spec
->entry_points
[i
];
327 if (odp
->type
!= TYPE_STUB
) continue;
328 fprintf( outfile
, "#ifdef __GNUC__\n" );
329 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
330 fprintf( outfile
, "#endif\n\n" );
331 fprintf( outfile
, "struct exc_record {\n" );
332 fprintf( outfile
, " unsigned int code, flags;\n" );
333 fprintf( outfile
, " void *rec, *addr;\n" );
334 fprintf( outfile
, " unsigned int params;\n" );
335 fprintf( outfile
, " const void *info[15];\n" );
336 fprintf( outfile
, "};\n\n" );
337 fprintf( outfile
, "extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
338 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
339 fprintf( outfile
, " struct exc_record rec;\n" );
340 fprintf( outfile
, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB
);
341 fprintf( outfile
, " rec.flags = %d;\n", EH_NONCONTINUABLE
);
342 fprintf( outfile
, " rec.rec = 0;\n" );
343 fprintf( outfile
, " rec.params = 2;\n" );
344 fprintf( outfile
, " rec.info[0] = \"%s\";\n", spec
->file_name
);
345 fprintf( outfile
, " rec.info[1] = func;\n" );
346 fprintf( outfile
, "#ifdef __GNUC__\n" );
347 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
348 fprintf( outfile
, "#else\n" );
349 fprintf( outfile
, " rec.addr = 0;\n" );
350 fprintf( outfile
, "#endif\n" );
351 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
355 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
357 const ORDDEF
*odp
= &spec
->entry_points
[i
];
358 if (odp
->type
!= TYPE_STUB
) continue;
359 fprintf( outfile
, "void %s(void) ", make_internal_name( odp
, spec
, "stub" ) );
361 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->name
);
362 else if (odp
->export_name
)
363 fprintf( outfile
, "{ __wine_unimplemented(\"%s\"); }\n", odp
->export_name
);
365 fprintf( outfile
, "{ __wine_unimplemented(\"%d\"); }\n", odp
->ordinal
);
370 /*******************************************************************
371 * output_register_funcs
373 * Output the functions for register entry points
375 static void output_register_funcs( FILE *outfile
, DLLSPEC
*spec
)
380 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
382 const ORDDEF
*odp
= &spec
->entry_points
[i
];
383 if (odp
->type
!= TYPE_STDCALL
&& odp
->type
!= TYPE_CDECL
) continue;
384 if (!(odp
->flags
& FLAG_REGISTER
)) continue;
385 if (odp
->flags
& FLAG_FORWARD
) continue;
386 name
= make_internal_name( odp
, spec
, "regs" );
388 "asm(\".align %d\\n\\t\"\n"
389 " \"" __ASM_FUNC("%s") "\\n\\t\"\n"
390 " \"" __ASM_NAME("%s") ":\\n\\t\"\n"
391 " \"call " __ASM_NAME("__wine_call_from_32_regs") "\\n\\t\"\n"
392 " \".long " __ASM_NAME("%s") "\\n\\t\"\n"
393 " \".byte %d,%d\");\n",
395 name
, name
, odp
->link_name
,
396 strlen(odp
->u
.func
.arg_types
) * sizeof(int),
397 (odp
->type
== TYPE_CDECL
) ? 0 : (strlen(odp
->u
.func
.arg_types
) * sizeof(int)) );
402 /*******************************************************************
405 * Output code for calling a dll constructor and destructor.
407 void output_dll_init( FILE *outfile
, const char *constructor
, const char *destructor
)
409 fprintf( outfile
, "#ifndef __GNUC__\n" );
410 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
411 fprintf( outfile
, "#endif\n" );
413 #if defined(__i386__)
416 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
417 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
418 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
422 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
423 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
424 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
426 #elif defined(__sparc__)
429 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
430 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", constructor
);
431 fprintf( outfile
, " \"\\tnop\\n\"\n" );
432 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
436 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
437 fprintf( outfile
, " \"\\tcall " __ASM_NAME("%s") "\\n\"\n", destructor
);
438 fprintf( outfile
, " \"\\tnop\\n\"\n" );
439 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
441 #elif defined(__powerpc__)
443 /* Mach-O doesn't have an init section */
446 fprintf( outfile
, "asm(\"\\t.mod_init_func\\n\"\n" );
447 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
448 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", constructor
);
449 fprintf( outfile
, " \"\\t.text\\n\");\n" );
453 fprintf( outfile
, "asm(\"\\t.mod_term_func\\n\"\n" );
454 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
455 fprintf( outfile
, " \"\\t.long " __ASM_NAME("%s") "\\n\"\n", destructor
);
456 fprintf( outfile
, " \"\\t.text\\n\");\n" );
458 # else /* __APPLE__ */
461 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
462 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", constructor
);
463 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
467 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
468 fprintf( outfile
, " \"\\tbl " __ASM_NAME("%s") "\\n\"\n", destructor
);
469 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
471 # endif /* __APPLE__ */
472 #elif defined(__ALPHA__)
475 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
476 fprintf( outfile
, " \"\\tjsr $26," __ASM_NAME("%s") "\\n\"\n", constructor
);
477 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
481 fprintf( outfile
, "asm(\"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
482 fprintf( outfile
, " \"\\tjsr $26," __ASM_NAME("%s") "\\n\"\n", destructor
);
483 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
486 #error You need to define the DLL constructor for your architecture
488 fprintf( outfile
, "#ifndef __GNUC__\n" );
489 fprintf( outfile
, "}\n" );
490 fprintf( outfile
, "#endif\n" );
494 /*******************************************************************
497 * Build a Win32 C file from a spec file.
499 void BuildSpec32File( FILE *outfile
, DLLSPEC
*spec
)
501 int exports_size
= 0;
502 int nr_exports
, nr_imports
;
504 const char *init_func
= spec
->init_func
;
506 #ifdef HAVE_GETPAGESIZE
507 page_size
= getpagesize();
508 #elif defined(__svr4__)
509 page_size
= sysconf(_SC_PAGESIZE
);
510 #elif defined(_WINDOWS)
514 page_size
= si
.dwPageSize
;
517 # error Cannot get the page size on this platform
520 nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
521 resolve_imports( spec
);
522 output_standard_file_header( outfile
);
524 /* Reserve some space for the PE header */
526 fprintf( outfile
, "extern char __wine_spec_pe_header[];\n" );
527 fprintf( outfile
, "#ifndef __GNUC__\n" );
528 fprintf( outfile
, "static void __asm__dummy_header(void) {\n" );
529 fprintf( outfile
, "#endif\n" );
530 fprintf( outfile
, "asm(\".text\\n\\t\"\n" );
531 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(page_size
) );
532 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP
" 65536\\n\\t\"\n" );
533 fprintf( outfile
, " \".data\\n\\t\"\n" );
534 fprintf( outfile
, " \".align %d\\n\"\n", get_alignment(4) );
535 fprintf( outfile
, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" );
536 fprintf( outfile
, "#ifndef __GNUC__\n" );
537 fprintf( outfile
, "}\n" );
538 fprintf( outfile
, "#endif\n" );
541 fprintf( outfile
, "static char _end[4];\n" );
543 fprintf( outfile
, "extern char _end[];\n" );
546 fprintf( outfile
, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" );
549 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
551 fprintf( outfile
, "#define __stdcall\n\n" );
556 /* Output the stub functions */
558 output_stub_funcs( outfile
, spec
);
560 fprintf( outfile
, "#ifndef __GNUC__\n" );
561 fprintf( outfile
, "static void __asm__dummy(void) {\n" );
562 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
564 /* Output code for all register functions */
566 output_register_funcs( outfile
, spec
);
568 /* Output the exports and relay entry points */
570 exports_size
= output_exports( outfile
, nr_exports
, spec
);
572 fprintf( outfile
, "#ifndef __GNUC__\n" );
573 fprintf( outfile
, "}\n" );
574 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
577 /* Output the DLL imports */
579 nr_imports
= output_imports( outfile
, spec
);
581 /* Output the resources */
583 output_resources( outfile
, spec
);
585 /* Output the entry point function */
587 fprintf( outfile
, "static int __wine_spec_init_state;\n" );
588 fprintf( outfile
, "extern int __wine_main_argc;\n" );
589 fprintf( outfile
, "extern char **__wine_main_argv;\n" );
590 fprintf( outfile
, "extern char **__wine_main_environ;\n" );
591 fprintf( outfile
, "extern unsigned short **__wine_main_wargv;\n" );
593 fprintf( outfile
, "extern _dyld_func_lookup(char *, void *);" );
594 fprintf( outfile
, "static void __wine_spec_hidden_init(int argc, char** argv, char** envp)\n" );
595 fprintf( outfile
, "{\n" );
596 fprintf( outfile
, " void (*init)(void);\n" );
597 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_make_delayed_module_initializer_calls\", (unsigned long *)&init);\n" );
598 fprintf( outfile
, " init();\n" );
599 fprintf( outfile
, "}\n" );
600 fprintf( outfile
, "static void __wine_spec_hidden_fini()\n" );
601 fprintf( outfile
, "{\n" );
602 fprintf( outfile
, " void (*fini)(void);\n" );
603 fprintf( outfile
, " _dyld_func_lookup(\"__dyld_mod_term_funcs\", (unsigned long *)&fini);\n" );
604 fprintf( outfile
, " fini();\n" );
605 fprintf( outfile
, "}\n" );
606 fprintf( outfile
, "#define _init __wine_spec_hidden_init\n" );
607 fprintf( outfile
, "#define _fini __wine_spec_hidden_fini\n" );
609 fprintf( outfile
, "extern void _init(int, char**, char**);\n" );
610 fprintf( outfile
, "extern void _fini();\n" );
613 if (spec
->characteristics
& IMAGE_FILE_DLL
)
616 fprintf( outfile
, "extern int __stdcall %s( void*, unsigned int, void* );\n\n", init_func
);
619 declare_weak_function( outfile
, "int __stdcall", "DllMain", "void*, unsigned int, void*" );
620 init_func
= "DllMain";
623 "static int __stdcall __wine_dll_main( void *inst, unsigned int reason, void *reserved )\n"
626 " if (reason == %d && __wine_spec_init_state == 1)\n"
627 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
628 " ret = %s ? %s( inst, reason, reserved ) : 1;\n"
629 " if (reason == %d && __wine_spec_init_state == 1) _fini();\n"
632 DLL_PROCESS_ATTACH
, init_func
, init_func
, DLL_PROCESS_DETACH
);
633 init_func
= "__wine_dll_main";
635 else switch(spec
->subsystem
)
637 case IMAGE_SUBSYSTEM_NATIVE
:
639 fprintf( outfile
, "extern int __stdcall %s( void*, void* );\n\n", init_func
);
642 declare_weak_function( outfile
, "int __stdcall", "DriverEntry", "void*, void*");
643 init_func
= "DriverEntry";
646 "static int __stdcall __wine_driver_entry( void *obj, void *path )\n"
649 " if (__wine_spec_init_state == 1)\n"
650 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n"
651 " ret = %s ? %s( obj, path ) : 0;\n"
652 " if (__wine_spec_init_state == 1) _fini();\n"
655 init_func
, init_func
);
656 init_func
= "__wine_driver_entry";
658 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
659 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
661 fprintf( outfile
, "extern int %s( int argc, char *argv[] );\n", init_func
);
664 declare_weak_function( outfile
, "int", "main", "int argc, char *argv[]" );
665 declare_weak_function( outfile
, "int", "wmain", "int argc, unsigned short *argv[]" );
666 declare_weak_function( outfile
, "int __stdcall", "WinMain", "void *,void *,char *,int" );
669 "\ntypedef struct {\n"
670 " unsigned int cb;\n"
671 " char *lpReserved, *lpDesktop, *lpTitle;\n"
672 " unsigned int dwX, dwY, dwXSize, dwYSize;\n"
673 " unsigned int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags;\n"
674 " unsigned short wShowWindow, cbReserved2;\n"
675 " char *lpReserved2;\n"
676 " void *hStdInput, *hStdOutput, *hStdError;\n"
678 "extern char * __stdcall GetCommandLineA(void);\n"
679 "extern void * __stdcall GetModuleHandleA(char *);\n"
680 "extern void __stdcall GetStartupInfoA(STARTUPINFOA *);\n"
681 "extern void __stdcall ExitProcess(unsigned int);\n"
682 "static void __wine_exe_main(void)\n"
685 " if (__wine_spec_init_state == 1)\n"
686 " _init( __wine_main_argc, __wine_main_argv, __wine_main_environ );\n" );
689 " ret = %s( __wine_main_argc, __wine_main_argv );\n", init_func
);
693 " STARTUPINFOA info;\n"
694 " char *cmdline = GetCommandLineA();\n"
695 " int bcount=0, in_quotes=0;\n"
696 " while (*cmdline) {\n"
697 " if ((*cmdline=='\\t' || *cmdline==' ') && !in_quotes) break;\n"
698 " else if (*cmdline=='\\\\') bcount++;\n"
699 " else if (*cmdline=='\\\"') {\n"
700 " if ((bcount & 1)==0) in_quotes=!in_quotes;\n"
706 " while (*cmdline=='\\t' || *cmdline==' ') cmdline++;\n"
707 " GetStartupInfoA( &info );\n"
708 " if (!(info.dwFlags & 1)) info.wShowWindow = 1;\n"
709 " ret = WinMain( GetModuleHandleA(0), 0, cmdline, info.wShowWindow );\n"
711 " else if (wmain) ret = wmain( __wine_main_argc, __wine_main_wargv );\n"
712 " else ret = main( __wine_main_argc, __wine_main_argv );\n" );
714 " if (__wine_spec_init_state == 1) _fini();\n"
715 " ExitProcess( ret );\n"
717 init_func
= "__wine_exe_main";
721 /* Output the NT header */
723 /* this is the IMAGE_NT_HEADERS structure, but we cannot include winnt.h here */
724 fprintf( outfile
, "static const struct image_nt_headers\n{\n" );
725 fprintf( outfile
, " int Signature;\n" );
726 fprintf( outfile
, " struct file_header {\n" );
727 fprintf( outfile
, " short Machine;\n" );
728 fprintf( outfile
, " short NumberOfSections;\n" );
729 fprintf( outfile
, " int TimeDateStamp;\n" );
730 fprintf( outfile
, " void *PointerToSymbolTable;\n" );
731 fprintf( outfile
, " int NumberOfSymbols;\n" );
732 fprintf( outfile
, " short SizeOfOptionalHeader;\n" );
733 fprintf( outfile
, " short Characteristics;\n" );
734 fprintf( outfile
, " } FileHeader;\n" );
735 fprintf( outfile
, " struct opt_header {\n" );
736 fprintf( outfile
, " short Magic;\n" );
737 fprintf( outfile
, " char MajorLinkerVersion, MinorLinkerVersion;\n" );
738 fprintf( outfile
, " int SizeOfCode;\n" );
739 fprintf( outfile
, " int SizeOfInitializedData;\n" );
740 fprintf( outfile
, " int SizeOfUninitializedData;\n" );
741 fprintf( outfile
, " void *AddressOfEntryPoint;\n" );
742 fprintf( outfile
, " void *BaseOfCode;\n" );
743 fprintf( outfile
, " void *BaseOfData;\n" );
744 fprintf( outfile
, " void *ImageBase;\n" );
745 fprintf( outfile
, " int SectionAlignment;\n" );
746 fprintf( outfile
, " int FileAlignment;\n" );
747 fprintf( outfile
, " short MajorOperatingSystemVersion;\n" );
748 fprintf( outfile
, " short MinorOperatingSystemVersion;\n" );
749 fprintf( outfile
, " short MajorImageVersion;\n" );
750 fprintf( outfile
, " short MinorImageVersion;\n" );
751 fprintf( outfile
, " short MajorSubsystemVersion;\n" );
752 fprintf( outfile
, " short MinorSubsystemVersion;\n" );
753 fprintf( outfile
, " int Win32VersionValue;\n" );
754 fprintf( outfile
, " void *SizeOfImage;\n" );
755 fprintf( outfile
, " int SizeOfHeaders;\n" );
756 fprintf( outfile
, " int CheckSum;\n" );
757 fprintf( outfile
, " short Subsystem;\n" );
758 fprintf( outfile
, " short DllCharacteristics;\n" );
759 fprintf( outfile
, " int SizeOfStackReserve;\n" );
760 fprintf( outfile
, " int SizeOfStackCommit;\n" );
761 fprintf( outfile
, " int SizeOfHeapReserve;\n" );
762 fprintf( outfile
, " int SizeOfHeapCommit;\n" );
763 fprintf( outfile
, " int LoaderFlags;\n" );
764 fprintf( outfile
, " int NumberOfRvaAndSizes;\n" );
765 fprintf( outfile
, " struct { const void *VirtualAddress; int Size; } DataDirectory[%d];\n",
766 IMAGE_NUMBEROF_DIRECTORY_ENTRIES
);
767 fprintf( outfile
, " } OptionalHeader;\n" );
768 fprintf( outfile
, "} nt_header = {\n" );
769 fprintf( outfile
, " 0x%04x,\n", IMAGE_NT_SIGNATURE
); /* Signature */
771 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_I386
); /* Machine */
772 #elif defined(__powerpc__)
773 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_POWERPC
); /* Machine */
774 #elif defined(__ALPHA__)
775 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_ALPHA
); /* Machine */
777 fprintf( outfile
, " { 0x%04x,\n", IMAGE_FILE_MACHINE_UNKNOWN
); /* Machine */
779 fprintf( outfile
, " 0, 0, 0, 0,\n" );
780 fprintf( outfile
, " sizeof(nt_header.OptionalHeader),\n" ); /* SizeOfOptionalHeader */
781 fprintf( outfile
, " 0x%04x },\n", spec
->characteristics
); /* Characteristics */
783 fprintf( outfile
, " { 0x%04x,\n", IMAGE_NT_OPTIONAL_HDR_MAGIC
); /* Magic */
784 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorLinkerVersion */
785 fprintf( outfile
, " 0, 0, 0,\n" ); /* SizeOfCode/Data */
786 fprintf( outfile
, " %s,\n", init_func
); /* AddressOfEntryPoint */
787 fprintf( outfile
, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */
788 fprintf( outfile
, " __wine_spec_pe_header,\n" ); /* ImageBase */
789 fprintf( outfile
, " %ld,\n", page_size
); /* SectionAlignment */
790 fprintf( outfile
, " %ld,\n", page_size
); /* FileAlignment */
791 fprintf( outfile
, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */
792 fprintf( outfile
, " 0, 0,\n" ); /* Major/MinorImageVersion */
793 fprintf( outfile
, " %d,\n", spec
->subsystem_major
); /* MajorSubsystemVersion */
794 fprintf( outfile
, " %d,\n", spec
->subsystem_minor
); /* MinorSubsystemVersion */
795 fprintf( outfile
, " 0,\n" ); /* Win32VersionValue */
796 fprintf( outfile
, " _end,\n" ); /* SizeOfImage */
797 fprintf( outfile
, " %ld,\n", page_size
); /* SizeOfHeaders */
798 fprintf( outfile
, " 0,\n" ); /* CheckSum */
799 fprintf( outfile
, " 0x%04x,\n", spec
->subsystem
);/* Subsystem */
800 fprintf( outfile
, " 0,\n" ); /* DllCharacteristics */
801 fprintf( outfile
, " %d, %ld,\n", /* SizeOfStackReserve/Commit */
802 (spec
->stack_size
? spec
->stack_size
: 1024) * 1024, page_size
);
803 fprintf( outfile
, " %d, %ld,\n", /* SizeOfHeapReserve/Commit */
804 (spec
->heap_size
? spec
->heap_size
: 1024) * 1024, page_size
);
805 fprintf( outfile
, " 0,\n" ); /* LoaderFlags */
806 fprintf( outfile
, " %d,\n", IMAGE_NUMBEROF_DIRECTORY_ENTRIES
); /* NumberOfRvaAndSizes */
807 fprintf( outfile
, " {\n" );
808 fprintf( outfile
, " { %s, %d },\n", /* IMAGE_DIRECTORY_ENTRY_EXPORT */
809 exports_size
? "__wine_spec_exports" : "0", exports_size
);
810 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_IMPORT */
811 nr_imports
? "&imports" : "0", nr_imports
? "sizeof(imports)" : "0" );
812 fprintf( outfile
, " { %s, %s },\n", /* IMAGE_DIRECTORY_ENTRY_RESOURCE */
813 spec
->nb_resources
? "&resources" : "0",
814 spec
->nb_resources
? "sizeof(resources)" : "0" );
815 fprintf( outfile
, " }\n }\n};\n\n" );
817 /* Output the DLL constructor */
820 "void __wine_spec_init(void)\n"
822 " extern void __wine_dll_register( const struct image_nt_headers *, const char * );\n"
823 " __wine_spec_init_state = 1;\n"
824 " __wine_dll_register( &nt_header, \"%s\" );\n"
828 output_dll_init( outfile
, "__wine_spec_init_ctor", NULL
);
830 "void __wine_spec_init_ctor(void)\n"
832 " if (__wine_spec_init_state) return;\n"
833 " __wine_spec_init();\n"
834 " __wine_spec_init_state = 2;\n"
839 /*******************************************************************
842 * Build a Win32 def file from a spec file.
844 void BuildDef32File( FILE *outfile
, DLLSPEC
*spec
)
850 fprintf( outfile
, "; File generated automatically from %s; do not edit!\n\n",
853 fprintf( outfile
, "; File generated automatically; do not edit!\n\n" );
855 fprintf(outfile
, "LIBRARY %s\n\n", spec
->file_name
);
857 fprintf(outfile
, "EXPORTS\n");
859 /* Output the exports and relay entry points */
861 for(i
= 0; i
< spec
->nb_entry_points
; i
++)
863 const ORDDEF
*odp
= &spec
->entry_points
[i
];
867 if (odp
->name
) name
= odp
->name
;
868 else if (odp
->type
== TYPE_STUB
) name
= make_internal_name( odp
, spec
, "stub" );
869 else if (odp
->export_name
) name
= odp
->export_name
;
870 else name
= make_internal_name( odp
, spec
, "noname_export" );
872 fprintf(outfile
, " %s", name
);
881 /* try to reduce output */
882 if(strcmp(name
, odp
->link_name
) || (odp
->flags
& FLAG_FORWARD
))
883 fprintf(outfile
, "=%s", odp
->link_name
);
887 int at_param
= strlen(odp
->u
.func
.arg_types
) * sizeof(int);
888 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
889 if (odp
->flags
& FLAG_FORWARD
)
891 fprintf(outfile
, "=%s", odp
->link_name
);
893 else if (strcmp(name
, odp
->link_name
)) /* try to reduce output */
895 fprintf(outfile
, "=%s", odp
->link_name
);
896 if (!kill_at
) fprintf(outfile
, "@%d", at_param
);
904 const char *check
= name
+ strlen(name
);
905 while (name
!= check
&&
906 '0' <= check
[-1] && check
[-1] <= '9')
910 if (name
!= check
&& check
!= name
+ strlen(name
) &&
913 fprintf(outfile
, "%s", check
- 1);
916 if (NULL
!= odp
->name
)
918 fprintf(outfile
, "=%s", make_internal_name( odp
, spec
, "stub" ));
925 fprintf( outfile
, " @%d", odp
->ordinal
);
926 #if 0 /* MinGW binutils cannot handle this correctly */
927 if (!odp
->name
) fprintf( outfile
, " NONAME" );
929 if (!odp
->name
&& (odp
->type
== TYPE_STUB
|| odp
->export_name
)) fprintf( outfile
, " NONAME" );
931 if (is_data
) fprintf( outfile
, " DATA" );
933 /* MinGW binutils cannot handle this correctly */
934 if (odp
->flags
& FLAG_PRIVATE
) fprintf( outfile
, " PRIVATE" );
936 fprintf( outfile
, "\n" );
941 /*******************************************************************
944 * Build the debugging channels source file.
946 void BuildDebugFile( FILE *outfile
, const char *srcdir
, char **argv
)
953 if (!parse_debug_channels( srcdir
, *argv
++ )) exit(1);
956 output_standard_file_header( outfile
);
957 nr_debug
= output_debug( outfile
);
960 fprintf( outfile
, "/* no debug channels found for this module */\n" );
964 if (output_file_name
)
966 if ((p
= strrchr( output_file_name
, '/' ))) p
++;
967 prefix
= xstrdup( p
? p
: output_file_name
);
968 if ((p
= strchr( prefix
, '.' ))) *p
= 0;
969 strcpy( p
, make_c_identifier(p
) );
971 else prefix
= xstrdup( "_" );
973 /* Output the DLL constructor */
977 "void __wine_dbg_%s_init(void) __attribute__((constructor));\n"
978 "void __wine_dbg_%s_fini(void) __attribute__((destructor));\n"
980 "static void __asm__dummy_dll_init(void) {\n",
983 #if defined(__i386__)
984 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
985 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
986 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
987 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
988 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
989 #elif defined(__sparc__)
990 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
991 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
992 fprintf( outfile
, " \"\\tnop\\n\"\n" );
993 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
994 fprintf( outfile
, " \"\\tcall " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
995 fprintf( outfile
, " \"\\tnop\\n\"\n" );
996 fprintf( outfile
, " \"\\t.section\t\\\".text\\\"\\n\");\n" );
997 #elif defined(__powerpc__)
999 fprintf( outfile
, "asm(\"\\t.mod_init_func\\n\"\n" );
1000 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
1001 fprintf( outfile
, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
1002 fprintf( outfile
, " \"\\t.text\\n\");\n" );
1003 fprintf( outfile
, "asm(\"\\t.mod_term_func\\n\"\n" );
1004 fprintf( outfile
, " \"\\t.align 2\\n\"\n" );
1005 fprintf( outfile
, " \"\\t.long " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
1006 fprintf( outfile
, " \"\\t.text\\n\");\n" );
1008 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
1009 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
1010 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
1011 fprintf( outfile
, " \"\\tbl " __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
1012 fprintf( outfile
, " \"\\t.text\\n\");\n" );
1014 #elif defined(__ALPHA__)
1015 fprintf( outfile
, "asm(\"\\t.section\\t\\\".init\\\" ,\\\"ax\\\"\\n\"\n" );
1016 fprintf( outfile
, " \"\\tjsr $26," __ASM_NAME("__wine_dbg_%s_init") "\\n\"\n", prefix
);
1017 fprintf( outfile
, " \"\\t.section\\t\\\".fini\\\" ,\\\"ax\\\"\\n\"\n" );
1018 fprintf( outfile
, " \"\\tjsr $26," __ASM_NAME("__wine_dbg_%s_fini") "\\n\"\n", prefix
);
1019 fprintf( outfile
, " \"\\t.section\\t\\\".text\\\"\\n\");\n" );
1021 #error You need to define the DLL constructor for your architecture
1023 fprintf( outfile
, "}\n#endif /* defined(__GNUC__) */\n\n" );
1026 "void __wine_dbg_%s_init(void)\n"
1028 " extern void *__wine_dbg_register( char * const *, int );\n"
1029 " if (!debug_registration) debug_registration = __wine_dbg_register( debug_channels, %d );\n"
1030 "}\n\n", prefix
, nr_debug
);
1032 "void __wine_dbg_%s_fini(void)\n"
1034 " extern void __wine_dbg_unregister( void* );\n"
1035 " __wine_dbg_unregister( debug_registration );\n"
1042 /*******************************************************************
1045 * Build a PE DLL C file from a spec file.
1047 void BuildPedllFile( FILE *outfile
, DLLSPEC
*spec
)
1051 nr_exports
= spec
->base
<= spec
->limit
? spec
->limit
- spec
->base
+ 1 : 0;
1052 output_standard_file_header( outfile
);
1056 /* Output the stub functions */
1058 output_stub_funcs( outfile
, spec
);