Automatically detect whether the entry point is main or WinMain
[wine.git] / tools / winebuild / import.c
blob404bce424e1a841b5200e22d6fc1eb9e80bab836
1 /*
2 * DLL imports support
4 * Copyright 2000, 2004 Alexandre Julliard
5 * Copyright 2000 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "build.h"
38 struct import
40 DLLSPEC *spec; /* description of the imported dll */
41 int delay; /* delay or not dll loading ? */
42 ORDDEF **exports; /* functions exported from this dll */
43 int nb_exports; /* number of exported functions */
44 ORDDEF **imports; /* functions we want to import from this dll */
45 int nb_imports; /* number of imported functions */
48 static char **undef_symbols; /* list of undefined symbols */
49 static int nb_undef_symbols = -1;
50 static int undef_size;
52 static char **ignore_symbols; /* list of symbols to ignore */
53 static int nb_ignore_symbols;
54 static int ignore_size;
56 static char *ld_tmp_file; /* ld temp file name */
58 static struct import **dll_imports = NULL;
59 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
60 static int nb_delayed = 0; /* number of delayed dlls */
61 static int total_imports = 0; /* total number of imported functions */
62 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
64 /* list of symbols that are ignored by default */
65 static const char * const default_ignored_symbols[] =
67 "abs",
68 "acos",
69 "asin",
70 "atan",
71 "atan2",
72 "atof",
73 "atoi",
74 "atol",
75 "bsearch",
76 "ceil",
77 "cos",
78 "cosh",
79 "exp",
80 "fabs",
81 "floor",
82 "fmod",
83 "frexp",
84 "labs",
85 "log",
86 "log10",
87 "memchr",
88 "memcmp",
89 "memcpy",
90 "memmove",
91 "memset",
92 "modf",
93 "pow",
94 "qsort",
95 "sin",
96 "sinh",
97 "sqrt",
98 "strcat",
99 "strchr",
100 "strcmp",
101 "strcpy",
102 "strcspn",
103 "strlen",
104 "strncat",
105 "strncmp",
106 "strncpy",
107 "strpbrk",
108 "strrchr",
109 "strspn",
110 "strstr",
111 "tan",
112 "tanh"
115 #ifdef __powerpc__
116 # ifdef __APPLE__
117 # define ppc_high(mem) "ha16(" mem ")"
118 # define ppc_low(mem) "lo16(" mem ")"
119 static const char * const ppc_reg[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
120 "r8", "r9", "r10","r11","r12","r13","r14","r15",
121 "r16","r17","r18","r19","r20","r21","r22","r23",
122 "r24","r25","r26","r27","r28","r29","r30","r31" };
123 # else /* __APPLE__ */
124 # define ppc_high(mem) "(" mem ")@hi"
125 # define ppc_low(mem) "(" mem ")@l"
126 static const char * const ppc_reg[32] = { "0", "1", "2", "3", "4", "5", "6", "7",
127 "8", "9", "10","11","12","13","14","15",
128 "16","17","18","19","20","21","22","23",
129 "24","25","26","27","28","29","30","31" };
130 # endif /* __APPLE__ */
131 #endif /* __powerpc__ */
133 /* compare function names; helper for resolve_imports */
134 static int name_cmp( const void *name, const void *entry )
136 return strcmp( *(char **)name, *(char **)entry );
139 /* compare function names; helper for resolve_imports */
140 static int func_cmp( const void *func1, const void *func2 )
142 const ORDDEF *odp1 = *(const ORDDEF **)func1;
143 const ORDDEF *odp2 = *(const ORDDEF **)func2;
144 return strcmp( odp1->name ? odp1->name : odp1->export_name,
145 odp2->name ? odp2->name : odp2->export_name );
148 /* locate a symbol in a (sorted) list */
149 inline static const char *find_symbol( const char *name, char **table, int size )
151 char **res = NULL;
153 if (table) {
154 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
157 return res ? *res : NULL;
160 /* locate an export in a (sorted) export list */
161 inline static ORDDEF *find_export( const char *name, ORDDEF **table, int size )
163 ORDDEF func, *odp, **res = NULL;
165 func.name = (char *)name;
166 func.ordinal = -1;
167 odp = &func;
168 if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
169 return res ? *res : NULL;
172 /* sort a symbol table */
173 inline static void sort_symbols( char **table, int size )
175 if (table )
176 qsort( table, size, sizeof(*table), name_cmp );
179 /* free an import structure */
180 static void free_imports( struct import *imp )
182 free( imp->exports );
183 free( imp->imports );
184 free_dll_spec( imp->spec );
185 free( imp );
188 /* remove the temp file at exit */
189 static void remove_ld_tmp_file(void)
191 if (ld_tmp_file) unlink( ld_tmp_file );
194 /* check whether a given dll has already been imported */
195 static int is_already_imported( const char *name )
197 int i;
199 for (i = 0; i < nb_imports; i++)
201 if (!strcmp( dll_imports[i]->spec->file_name, name )) return 1;
203 return 0;
206 /* open the .so library for a given dll in a specified path */
207 static char *try_library_path( const char *path, const char *name )
209 char *buffer;
210 int fd;
212 buffer = xmalloc( strlen(path) + strlen(name) + 9 );
213 sprintf( buffer, "%s/lib%s.def", path, name );
215 /* check if the file exists */
216 if ((fd = open( buffer, O_RDONLY )) != -1)
218 close( fd );
219 return buffer;
221 free( buffer );
222 return NULL;
225 /* open the .so library for a given dll */
226 static char *open_library( const char *name )
228 char *fullname;
229 int i;
231 for (i = 0; i < nb_lib_paths; i++)
233 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
235 fatal_error( "could not open .def file for %s\n", name );
236 return NULL;
239 /* read in the list of exported symbols of an import library */
240 static int read_import_lib( const char *name, struct import *imp )
242 FILE *f;
243 char *fullname;
244 int i, ret;
245 DLLSPEC *spec = imp->spec;
247 imp->exports = NULL;
248 imp->nb_exports = 0;
250 fullname = open_library( name );
251 f = open_input_file( NULL, fullname );
252 free( fullname );
254 ret = parse_def_file( f, spec );
255 close_input_file( f );
256 if (!ret) return 0;
257 if (is_already_imported( spec->file_name )) return 0;
259 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
261 for (i = 0; i < spec->nb_entry_points; i++)
263 ORDDEF *odp = &spec->entry_points[i];
265 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
266 if (odp->flags & FLAG_PRIVATE) continue;
267 imp->exports[imp->nb_exports++] = odp;
269 imp->exports = xrealloc( imp->exports, imp->nb_exports * sizeof(*imp->exports) );
270 if (imp->nb_exports)
271 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
272 return 1;
275 /* add a dll to the list of imports */
276 void add_import_dll( const char *name, int delay )
278 struct import *imp;
279 char *fullname;
281 fullname = xmalloc( strlen(name) + 5 );
282 strcpy( fullname, name );
283 if (!strchr( fullname, '.' )) strcat( fullname, ".dll" );
285 /* check if we already imported it */
286 if (is_already_imported( fullname ))
288 free( fullname );
289 return;
292 imp = xmalloc( sizeof(*imp) );
293 imp->spec = alloc_dll_spec();
294 imp->spec->file_name = fullname;
295 imp->delay = delay;
296 imp->imports = NULL;
297 imp->nb_imports = 0;
298 if (delay) nb_delayed++;
300 if (read_import_lib( name, imp ))
302 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
303 dll_imports[nb_imports++] = imp;
305 else
307 free_imports( imp );
308 if (nb_errors) exit(1);
312 /* remove an imported dll, based on its index in the dll_imports array */
313 static void remove_import_dll( int index )
315 struct import *imp = dll_imports[index];
317 memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
318 nb_imports--;
319 if (imp->delay) nb_delayed--;
320 free_imports( imp );
323 /* initialize the list of ignored symbols */
324 static void init_ignored_symbols(void)
326 int i;
328 nb_ignore_symbols = sizeof(default_ignored_symbols)/sizeof(default_ignored_symbols[0]);
329 ignore_size = nb_ignore_symbols + 32;
330 ignore_symbols = xmalloc( ignore_size * sizeof(*ignore_symbols) );
331 for (i = 0; i < nb_ignore_symbols; i++)
332 ignore_symbols[i] = xstrdup( default_ignored_symbols[i] );
335 /* add a symbol to the ignored symbol list */
336 /* if the name starts with '-' the symbol is removed instead */
337 void add_ignore_symbol( const char *name )
339 int i;
341 if (!ignore_symbols) init_ignored_symbols(); /* first time around, fill list with defaults */
343 if (name[0] == '-') /* remove it */
345 if (!name[1]) /* remove everything */
347 for (i = 0; i < nb_ignore_symbols; i++) free( ignore_symbols[i] );
348 nb_ignore_symbols = 0;
350 else
352 for (i = 0; i < nb_ignore_symbols; i++)
354 if (!strcmp( ignore_symbols[i], name+1 ))
356 free( ignore_symbols[i] );
357 memmove( &ignore_symbols[i], &ignore_symbols[i+1], nb_ignore_symbols - i - 1 );
358 nb_ignore_symbols--;
363 else
365 if (nb_ignore_symbols == ignore_size)
367 ignore_size += 32;
368 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
370 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
374 /* add a function to the list of imports from a given dll */
375 static void add_import_func( struct import *imp, ORDDEF *func )
377 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
378 imp->imports[imp->nb_imports++] = func;
379 total_imports++;
380 if (imp->delay) total_delayed++;
383 /* add a symbol to the undef list */
384 inline static void add_undef_symbol( const char *name )
386 if (nb_undef_symbols == undef_size)
388 undef_size += 128;
389 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
391 undef_symbols[nb_undef_symbols++] = xstrdup( name );
394 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
395 static int remove_symbol_holes(void)
397 int i, off;
398 for (i = off = 0; i < nb_undef_symbols; i++)
400 if (!undef_symbols[i]) off++;
401 else undef_symbols[i - off] = undef_symbols[i];
403 nb_undef_symbols -= off;
404 return off;
407 /* add a symbol to the extra list, but only if needed */
408 static int add_extra_symbol( const char **extras, int *count, const char *name, const DLLSPEC *spec )
410 int i;
412 if (!find_symbol( name, undef_symbols, nb_undef_symbols ))
414 /* check if the symbol is being exported by this dll */
415 for (i = 0; i < spec->nb_entry_points; i++)
417 ORDDEF *odp = &spec->entry_points[i];
418 if (odp->type == TYPE_STDCALL ||
419 odp->type == TYPE_CDECL ||
420 odp->type == TYPE_VARARGS ||
421 odp->type == TYPE_EXTERN)
423 if (odp->name && !strcmp( odp->name, name )) return 0;
426 extras[*count] = name;
427 (*count)++;
429 return 1;
432 /* add the extra undefined symbols that will be contained in the generated spec file itself */
433 static void add_extra_undef_symbols( const DLLSPEC *spec )
435 const char *extras[10];
436 int i, count = 0, nb_stubs = 0, nb_regs = 0;
437 int kernel_imports = 0, ntdll_imports = 0;
439 sort_symbols( undef_symbols, nb_undef_symbols );
441 for (i = 0; i < spec->nb_entry_points; i++)
443 ORDDEF *odp = &spec->entry_points[i];
444 if (odp->type == TYPE_STUB) nb_stubs++;
445 if (odp->flags & FLAG_REGISTER) nb_regs++;
448 /* add symbols that will be contained in the spec file itself */
449 if (!(spec->characteristics & IMAGE_FILE_DLL))
451 switch (spec->subsystem)
453 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
454 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
455 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA", spec );
456 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA", spec );
457 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA", spec );
458 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess", spec );
459 break;
462 if (nb_delayed)
464 kernel_imports += add_extra_symbol( extras, &count, "LoadLibraryA", spec );
465 kernel_imports += add_extra_symbol( extras, &count, "GetProcAddress", spec );
467 if (nb_regs)
468 ntdll_imports += add_extra_symbol( extras, &count, "__wine_call_from_32_regs", spec );
469 if (nb_delayed || nb_stubs)
470 ntdll_imports += add_extra_symbol( extras, &count, "RtlRaiseException", spec );
472 /* make sure we import the dlls that contain these functions */
473 if (kernel_imports) add_import_dll( "kernel32", 0 );
474 if (ntdll_imports) add_import_dll( "ntdll", 0 );
476 if (count)
478 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
479 sort_symbols( undef_symbols, nb_undef_symbols );
483 /* check if a given imported dll is not needed, taking forwards into account */
484 static int check_unused( const struct import* imp, const DLLSPEC *spec )
486 int i;
487 const char *file_name = imp->spec->file_name;
488 size_t len = strlen( file_name );
489 const char *p = strchr( file_name, '.' );
490 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
492 for (i = spec->base; i <= spec->limit; i++)
494 ORDDEF *odp = spec->ordinals[i];
495 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
496 if (!strncasecmp( odp->link_name, file_name, len ) &&
497 odp->link_name[len] == '.')
498 return 0; /* found a forward, it is used */
500 return 1;
503 /* combine a list of object files with ld into a single object file */
504 /* returns the name of the combined file */
505 static const char *ldcombine_files( char **argv )
507 int i, len = 0;
508 char *cmd;
509 int fd, err;
511 if (output_file_name && output_file_name[0])
513 ld_tmp_file = xmalloc( strlen(output_file_name) + 10 );
514 strcpy( ld_tmp_file, output_file_name );
515 strcat( ld_tmp_file, ".XXXXXX.o" );
517 else ld_tmp_file = xstrdup( "/tmp/winebuild.tmp.XXXXXX.o" );
519 if ((fd = mkstemps( ld_tmp_file, 2 ) == -1)) fatal_error( "could not generate a temp file\n" );
520 close( fd );
521 atexit( remove_ld_tmp_file );
523 for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
524 cmd = xmalloc( len + strlen(ld_tmp_file) + 10 );
525 sprintf( cmd, "ld -r -o %s", ld_tmp_file );
526 for (i = 0; argv[i]; i++) sprintf( cmd + strlen(cmd), " %s", argv[i] );
527 err = system( cmd );
528 if (err) fatal_error( "ld -r failed with status %d\n", err );
529 free( cmd );
530 return ld_tmp_file;
533 /* read in the list of undefined symbols */
534 void read_undef_symbols( char **argv )
536 FILE *f;
537 char buffer[1024];
538 int err;
539 const char *name;
541 if (!argv[0]) return;
543 undef_size = nb_undef_symbols = 0;
545 /* if we have multiple object files, link them together */
546 if (argv[1]) name = ldcombine_files( argv );
547 else name = argv[0];
549 sprintf( buffer, "nm -u %s", name );
550 if (!(f = popen( buffer, "r" )))
551 fatal_error( "Cannot execute '%s'\n", buffer );
553 while (fgets( buffer, sizeof(buffer), f ))
555 char *p = buffer + strlen(buffer) - 1;
556 if (p < buffer) continue;
557 if (*p == '\n') *p-- = 0;
558 p = buffer;
559 while (*p == ' ') p++;
560 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
561 add_undef_symbol( p );
563 if ((err = pclose( f ))) warning( "nm -u %s error %d\n", name, err );
566 static void remove_ignored_symbols(void)
568 int i;
570 if (!ignore_symbols) init_ignored_symbols();
571 sort_symbols( ignore_symbols, nb_ignore_symbols );
572 for (i = 0; i < nb_undef_symbols; i++)
574 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
576 free( undef_symbols[i] );
577 undef_symbols[i] = NULL;
580 remove_symbol_holes();
583 /* resolve the imports for a Win32 module */
584 int resolve_imports( DLLSPEC *spec )
586 int i, j;
588 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
590 add_extra_undef_symbols( spec );
591 remove_ignored_symbols();
593 for (i = 0; i < nb_imports; i++)
595 struct import *imp = dll_imports[i];
597 for (j = 0; j < nb_undef_symbols; j++)
599 ORDDEF *odp = find_export( undef_symbols[j], imp->exports, imp->nb_exports );
600 if (odp)
602 add_import_func( imp, odp );
603 free( undef_symbols[j] );
604 undef_symbols[j] = NULL;
607 /* remove all the holes in the undef symbols list */
608 if (!remove_symbol_holes() && check_unused( imp, spec ))
610 /* the dll is not used, get rid of it */
611 warning( "%s imported but no symbols used\n", imp->spec->file_name );
612 remove_import_dll( i );
613 i--;
616 return 1;
619 /* output the import table of a Win32 module */
620 static int output_immediate_imports( FILE *outfile )
622 int i, j, pos;
623 int nb_imm = nb_imports - nb_delayed;
625 if (!nb_imm) goto done;
627 /* main import header */
629 fprintf( outfile, "\nstatic struct {\n" );
630 fprintf( outfile, " struct {\n" );
631 fprintf( outfile, " void *OriginalFirstThunk;\n" );
632 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
633 fprintf( outfile, " unsigned int ForwarderChain;\n" );
634 fprintf( outfile, " const char *Name;\n" );
635 fprintf( outfile, " void *FirstThunk;\n" );
636 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
637 fprintf( outfile, " const char *data[%d];\n",
638 total_imports - total_delayed + nb_imm );
639 fprintf( outfile, "} imports = {\n {\n" );
641 /* list of dlls */
643 for (i = j = 0; i < nb_imports; i++)
645 if (dll_imports[i]->delay) continue;
646 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
647 dll_imports[i]->spec->file_name, j );
648 j += dll_imports[i]->nb_imports + 1;
651 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
652 fprintf( outfile, " },\n {\n" );
654 /* list of imported functions */
656 for (i = 0; i < nb_imports; i++)
658 if (dll_imports[i]->delay) continue;
659 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
660 for (j = 0; j < dll_imports[i]->nb_imports; j++)
662 ORDDEF *odp = dll_imports[i]->imports[j];
663 if (!(odp->flags & FLAG_NONAME))
665 unsigned short ord = odp->ordinal;
666 fprintf( outfile, " \"\\%03o\\%03o%s\",\n",
667 *(unsigned char *)&ord, *((unsigned char *)&ord + 1), odp->name );
669 else
670 fprintf( outfile, " (char *)%d,\n", odp->ordinal );
672 fprintf( outfile, " 0,\n" );
674 fprintf( outfile, " }\n};\n\n" );
676 /* thunks for imported functions */
678 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
679 pos = 20 * (nb_imm + 1); /* offset of imports.data from start of imports */
680 fprintf( outfile, "asm(\".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
681 for (i = 0; i < nb_imports; i++)
683 if (dll_imports[i]->delay) continue;
684 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
686 ORDDEF *odp = dll_imports[i]->imports[j];
687 const char *name = odp->name ? odp->name : odp->export_name;
688 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name );
689 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name );
690 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t", name);
692 #if defined(__i386__)
693 if (strstr( name, "__wine_call_from_16" ))
694 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
695 else
696 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
697 #elif defined(__sparc__)
698 if ( !UsePIC )
700 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
701 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
702 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
704 else
706 /* Hmpf. Stupid sparc assembler always interprets global variable
707 names as GOT offsets, so we have to do it the long way ... */
708 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
709 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
710 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
711 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
712 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
713 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
716 #elif defined(__powerpc__)
717 fprintf(outfile, "\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
718 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
719 fprintf(outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
720 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
721 fprintf(outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
722 fprintf(outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
724 fprintf(outfile, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("imports") "+ %d") "\\n\"\n", ppc_reg[9], pos);
725 fprintf(outfile, "\t\"\\tla %s, " ppc_low (__ASM_NAME("imports") "+ %d") "(%s)\\n\"\n", ppc_reg[8], pos, ppc_reg[9]);
726 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[8]);
727 fprintf(outfile, "\t\"\\tmtctr %s\\n\"\n", ppc_reg[7]);
729 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
730 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
731 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
732 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
733 fprintf(outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
734 fprintf(outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
735 fprintf(outfile, "\t\"\\tbctr\\n");
736 #else
737 #error You need to define import thunks for your architecture!
738 #endif
739 fprintf( outfile, "\"\n" );
741 pos += 4;
743 fprintf( outfile, "\".text\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
745 done:
746 return nb_imm;
749 /* output the delayed import table of a Win32 module */
750 static int output_delayed_imports( FILE *outfile, const DLLSPEC *spec )
752 int i, idx, j, pos;
754 if (!nb_delayed) goto done;
756 for (i = 0; i < nb_imports; i++)
758 if (!dll_imports[i]->delay) continue;
759 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
760 for (j = 0; j < dll_imports[i]->nb_imports; j++)
762 ORDDEF *odp = dll_imports[i]->imports[j];
763 const char *name = odp->name ? odp->name : odp->export_name;
764 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n", i, name );
767 fprintf( outfile, "\n" );
768 fprintf( outfile, "static struct {\n" );
769 fprintf( outfile, " struct ImgDelayDescr {\n" );
770 fprintf( outfile, " unsigned int grAttrs;\n" );
771 fprintf( outfile, " const char *szName;\n" );
772 fprintf( outfile, " void **phmod;\n" );
773 fprintf( outfile, " void **pIAT;\n" );
774 fprintf( outfile, " const char **pINT;\n" );
775 fprintf( outfile, " void* pBoundIAT;\n" );
776 fprintf( outfile, " void* pUnloadIAT;\n" );
777 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
778 fprintf( outfile, " } imp[%d];\n", nb_delayed );
779 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
780 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
781 fprintf( outfile, "} delay_imports = {\n" );
782 fprintf( outfile, " {\n" );
783 for (i = j = 0; i < nb_imports; i++)
785 if (!dll_imports[i]->delay) continue;
786 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
787 dll_imports[i]->spec->file_name, i, j, j );
788 j += dll_imports[i]->nb_imports;
790 fprintf( outfile, " },\n {\n" );
791 for (i = 0; i < nb_imports; i++)
793 if (!dll_imports[i]->delay) continue;
794 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
795 for (j = 0; j < dll_imports[i]->nb_imports; j++)
797 ORDDEF *odp = dll_imports[i]->imports[j];
798 const char *name = odp->name ? odp->name : odp->export_name;
799 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, name );
802 fprintf( outfile, " },\n {\n" );
803 for (i = 0; i < nb_imports; i++)
805 if (!dll_imports[i]->delay) continue;
806 fprintf( outfile, " /* %s */\n", dll_imports[i]->spec->file_name );
807 for (j = 0; j < dll_imports[i]->nb_imports; j++)
809 ORDDEF *odp = dll_imports[i]->imports[j];
810 if (!odp->name)
811 fprintf( outfile, " (char *)%d,\n", odp->ordinal );
812 else
813 fprintf( outfile, " \"%s\",\n", odp->name );
816 fprintf( outfile, " }\n};\n\n" );
818 /* check if there's some stub defined. if so, exception struct
819 * is already defined, so don't emit it twice
821 for (i = 0; i < spec->nb_entry_points; i++) if (spec->entry_points[i].type == TYPE_STUB) break;
823 if (i == spec->nb_entry_points) {
824 fprintf( outfile, "struct exc_record {\n" );
825 fprintf( outfile, " unsigned int code, flags;\n" );
826 fprintf( outfile, " void *rec, *addr;\n" );
827 fprintf( outfile, " unsigned int params;\n" );
828 fprintf( outfile, " const void *info[15];\n" );
829 fprintf( outfile, "};\n\n" );
830 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
833 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
834 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
835 fprintf( outfile, "\n" );
837 fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
838 fprintf( outfile, "{\n" );
839 fprintf( outfile, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
840 fprintf( outfile, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
841 fprintf( outfile, " void **pIAT = imd->pIAT + nr;\n" );
842 fprintf( outfile, " const char** pINT = imd->pINT + nr;\n" );
843 fprintf( outfile, " void *fn;\n\n" );
845 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
846 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT)))\n");
847 fprintf( outfile, " /* patch IAT with final value */\n" );
848 fprintf( outfile, " return *pIAT = fn;\n" );
849 fprintf( outfile, " else {\n");
850 fprintf( outfile, " struct exc_record rec;\n" );
851 fprintf( outfile, " rec.code = 0x80000100;\n" );
852 fprintf( outfile, " rec.flags = 1;\n" );
853 fprintf( outfile, " rec.rec = 0;\n" );
854 fprintf( outfile, " rec.params = 2;\n" );
855 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
856 fprintf( outfile, " rec.info[1] = *pINT;\n" );
857 fprintf( outfile, "#ifdef __GNUC__\n" );
858 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
859 fprintf( outfile, "#else\n" );
860 fprintf( outfile, " rec.addr = 0;\n" );
861 fprintf( outfile, "#endif\n" );
862 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
863 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
864 fprintf( outfile, " }\n}\n\n" );
866 fprintf( outfile, "#ifndef __GNUC__\n" );
867 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
868 fprintf( outfile, "#endif\n" );
870 fprintf( outfile, "asm(\".align %d\\n\"\n", get_alignment(8) );
871 fprintf( outfile, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
872 fprintf( outfile, " \"" __ASM_NAME("__wine_delay_load_asm") ":\\n\"\n" );
873 #if defined(__i386__)
874 fprintf( outfile, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
875 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
876 fprintf( outfile, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
877 #elif defined(__sparc__)
878 fprintf( outfile, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
879 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
880 fprintf( outfile, " \"\\tmov %%g1, %%o0\\n\"\n" );
881 fprintf( outfile, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
882 #elif defined(__powerpc__)
883 /* Save all callee saved registers into a stackframe. */
884 fprintf( outfile, " \"\\tstwu %s, -48(%s)\\n\"\n", ppc_reg[1], ppc_reg[1]);
885 fprintf( outfile, " \"\\tstw %s, 4(%s)\\n\"\n", ppc_reg[3], ppc_reg[1]);
886 fprintf( outfile, " \"\\tstw %s, 8(%s)\\n\"\n", ppc_reg[4], ppc_reg[1]);
887 fprintf( outfile, " \"\\tstw %s, 12(%s)\\n\"\n", ppc_reg[5], ppc_reg[1]);
888 fprintf( outfile, " \"\\tstw %s, 16(%s)\\n\"\n", ppc_reg[6], ppc_reg[1]);
889 fprintf( outfile, " \"\\tstw %s, 20(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
890 fprintf( outfile, " \"\\tstw %s, 24(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
891 fprintf( outfile, " \"\\tstw %s, 28(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
892 fprintf( outfile, " \"\\tstw %s, 32(%s)\\n\"\n", ppc_reg[10], ppc_reg[1]);
893 fprintf( outfile, " \"\\tstw %s, 36(%s)\\n\"\n", ppc_reg[11], ppc_reg[1]);
894 fprintf( outfile, " \"\\tstw %s, 40(%s)\\n\"\n", ppc_reg[12], ppc_reg[1]);
896 /* r0 -> r3 (arg1) */
897 fprintf( outfile, " \"\\tmr %s, %s\\n\"\n", ppc_reg[3], ppc_reg[0]);
899 /* save return address */
900 fprintf( outfile, " \"\\tmflr %s\\n\"\n", ppc_reg[0]);
901 fprintf( outfile, " \"\\tstw %s, 44(%s)\\n\"\n", ppc_reg[0], ppc_reg[1]);
903 /* Call the __wine_delay_load function, arg1 is arg1. */
904 fprintf( outfile, " \"\\tbl " __ASM_NAME("__wine_delay_load") "\\n\"\n");
906 /* Load return value from call into ctr register */
907 fprintf( outfile, " \"\\tmtctr %s\\n\"\n", ppc_reg[3]);
909 /* restore all saved registers and drop stackframe. */
910 fprintf( outfile, " \"\\tlwz %s, 4(%s)\\n\"\n", ppc_reg[3], ppc_reg[1]);
911 fprintf( outfile, " \"\\tlwz %s, 8(%s)\\n\"\n", ppc_reg[4], ppc_reg[1]);
912 fprintf( outfile, " \"\\tlwz %s, 12(%s)\\n\"\n", ppc_reg[5], ppc_reg[1]);
913 fprintf( outfile, " \"\\tlwz %s, 16(%s)\\n\"\n", ppc_reg[6], ppc_reg[1]);
914 fprintf( outfile, " \"\\tlwz %s, 20(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
915 fprintf( outfile, " \"\\tlwz %s, 24(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
916 fprintf( outfile, " \"\\tlwz %s, 28(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
917 fprintf( outfile, " \"\\tlwz %s, 32(%s)\\n\"\n", ppc_reg[10], ppc_reg[1]);
918 fprintf( outfile, " \"\\tlwz %s, 36(%s)\\n\"\n", ppc_reg[11], ppc_reg[1]);
919 fprintf( outfile, " \"\\tlwz %s, 40(%s)\\n\"\n", ppc_reg[12], ppc_reg[1]);
921 /* Load return value from call into return register */
922 fprintf( outfile, " \"\\tlwz %s, 44(%s)\\n\"\n", ppc_reg[0], ppc_reg[1]);
923 fprintf( outfile, " \"\\tmtlr %s\\n\"\n", ppc_reg[0]);
924 fprintf( outfile, " \"\\taddi %s, %s, 48\\n\"\n", ppc_reg[1], ppc_reg[1]);
926 /* branch to ctr register. */
927 fprintf( outfile, "\"bctr\\n\"\n");
928 #else
929 #error You need to defined delayed import thunks for your architecture!
930 #endif
932 for (i = idx = 0; i < nb_imports; i++)
934 if (!dll_imports[i]->delay) continue;
935 for (j = 0; j < dll_imports[i]->nb_imports; j++)
937 char buffer[128];
938 ORDDEF *odp = dll_imports[i]->imports[j];
939 const char *name = odp->name ? odp->name : odp->export_name;
941 sprintf( buffer, "__wine_delay_imp_%d_%s", i, name );
942 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
943 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", buffer );
944 #if defined(__i386__)
945 fprintf( outfile, " \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
946 fprintf( outfile, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
947 #elif defined(__sparc__)
948 fprintf( outfile, " \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
949 fprintf( outfile, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
950 #elif defined(__powerpc__)
951 /* g0 is a function scratch register or so I understand. */
952 /* First load the upper half-word, and then the lower part */
953 fprintf( outfile, " \"\\tlis %s, %d\\n\"\n", ppc_reg[0], idx);
954 fprintf( outfile, " \"\\tli %s, %d\\n\"\n", ppc_reg[0], j);
955 fprintf( outfile, " \"\\tb " __ASM_NAME("__wine_delay_load_asm") "\\n\"\n");
956 #else
957 #error You need to defined delayed import thunks for your architecture!
958 #endif
960 idx++;
963 fprintf( outfile, "\n \".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
964 pos = nb_delayed * 32;
965 for (i = 0; i < nb_imports; i++)
967 if (!dll_imports[i]->delay) continue;
968 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
970 ORDDEF *odp = dll_imports[i]->imports[j];
971 const char *name = odp->name ? odp->name : odp->export_name;
973 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", name );
974 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", name );
975 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t\"", name );
976 #if defined(__i386__)
977 if (strstr( name, "__wine_call_from_16" ))
978 fprintf( outfile, "\".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n\"", pos );
979 else
980 fprintf( outfile, "\"jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n\"", pos );
981 #elif defined(__sparc__)
982 if ( !UsePIC )
984 fprintf( outfile, "\"sethi %%hi(delay_imports+%d), %%g1\\n\\t\"", pos );
985 fprintf( outfile, "\"ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t\"", pos );
986 fprintf( outfile, "\"jmp %%g1\\n\\tnop\\n\"" );
988 else
990 /* Hmpf. Stupid sparc assembler always interprets global variable
991 names as GOT offsets, so we have to do it the long way ... */
992 fprintf( outfile, "\"save %%sp, -96, %%sp\\n\"" );
993 fprintf( outfile, "\"0:\\tcall 1f\\n\\tnop\\n\"" );
994 fprintf( outfile, "\"1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t\"", pos );
995 fprintf( outfile, "\"or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t\"", pos );
996 fprintf( outfile, "\"ld [%%g1+%%o7], %%g1\\n\\t\"" );
997 fprintf( outfile, "\"jmp %%g1\\n\\trestore\\n\"" );
1000 #elif defined(__powerpc__)
1001 fprintf( outfile, "\t\"addi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1002 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
1003 fprintf( outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1004 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
1005 fprintf( outfile, "\t\"\\taddi %s, %s, -0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1006 fprintf( outfile, "\t\"\\tstw %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
1007 fprintf( outfile, "\t\"\\tlis %s, " ppc_high(__ASM_NAME("imports") "+ %d") "\\n\"\n", ppc_reg[9], pos);
1008 fprintf( outfile, "\t\"\\tla %s, " ppc_low (__ASM_NAME("imports") "+ %d") "(%s)\\n\"\n", ppc_reg[8], pos, ppc_reg[9]);
1009 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[8]);
1010 fprintf( outfile, "\t\"\\tmtctr %s\\n\"\n", ppc_reg[7]);
1012 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[7], ppc_reg[1]);
1013 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1014 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[8], ppc_reg[1]);
1015 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1016 fprintf( outfile, "\t\"\\tlwz %s, 0(%s)\\n\"\n", ppc_reg[9], ppc_reg[1]);
1017 fprintf( outfile, "\t\"\\taddi %s, %s, 0x4\\n\"\n", ppc_reg[1], ppc_reg[1]);
1018 fprintf( outfile, "\t\"\\tbctr\\n\"");
1019 #else
1020 #error You need to define delayed import thunks for your architecture!
1021 #endif
1022 fprintf( outfile, "\n" );
1025 fprintf( outfile, "\".text\");\n" );
1026 fprintf( outfile, "#ifndef __GNUC__\n" );
1027 fprintf( outfile, "}\n" );
1028 fprintf( outfile, "#endif\n" );
1029 fprintf( outfile, "\n" );
1031 done:
1032 return nb_delayed;
1035 /* output the import and delayed import tables of a Win32 module
1036 * returns number of DLLs exported in 'immediate' mode
1038 int output_imports( FILE *outfile, DLLSPEC *spec )
1040 output_delayed_imports( outfile, spec );
1041 return output_immediate_imports( outfile );