- Implement custom draw support.
[wine/testsucceed.git] / tools / winebuild / import.c
blob5fd70c6eef4432039c087571697edcefc72bc225
1 /*
2 * DLL imports support
4 * Copyright 2000 Alexandre Julliard
5 * 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 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "build.h"
35 struct func
37 const char *name; /* function name */
38 int ordinal; /* function ordinal */
39 int ord_only; /* non-zero if function is imported by ordinal */
42 struct import
44 char *dll; /* dll name */
45 int delay; /* delay or not dll loading ? */
46 struct func *exports; /* functions exported from this dll */
47 int nb_exports; /* number of exported functions */
48 struct func *imports; /* functions we want to import from this dll */
49 int nb_imports; /* number of imported functions */
52 static char **undef_symbols; /* list of undefined symbols */
53 static int nb_undef_symbols = -1;
54 static int undef_size;
56 static char **ignore_symbols; /* list of symbols to ignore */
57 static int nb_ignore_symbols;
58 static int ignore_size;
60 static char *ld_tmp_file; /* ld temp file name */
62 static struct import **dll_imports = NULL;
63 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
64 static int nb_delayed = 0; /* number of delayed dlls */
65 static int total_imports = 0; /* total number of imported functions */
66 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
68 /* compare function names; helper for resolve_imports */
69 static int name_cmp( const void *name, const void *entry )
71 return strcmp( *(char **)name, *(char **)entry );
74 /* compare function names; helper for resolve_imports */
75 static int func_cmp( const void *func1, const void *func2 )
77 return strcmp( ((struct func *)func1)->name, ((struct func *)func2)->name );
80 /* locate a symbol in a (sorted) list */
81 inline static const char *find_symbol( const char *name, char **table, int size )
83 char **res = NULL;
85 if (table) {
86 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
89 return res ? *res : NULL;
92 /* locate an export in a (sorted) export list */
93 inline static struct func *find_export( const char *name, struct func *table, int size )
95 struct func func, *res = NULL;
97 func.name = name;
98 func.ordinal = -1;
99 if (table) res = bsearch( &func, table, size, sizeof(*table), func_cmp );
100 return res;
103 /* sort a symbol table */
104 inline static void sort_symbols( char **table, int size )
106 if (table )
107 qsort( table, size, sizeof(*table), name_cmp );
110 /* remove the temp file at exit */
111 static void remove_ld_tmp_file(void)
113 if (ld_tmp_file) unlink( ld_tmp_file );
116 /* open the .so library for a given dll in a specified path */
117 static char *try_library_path( const char *path, const char *name )
119 char *buffer;
120 int fd;
122 buffer = xmalloc( strlen(path) + strlen(name) + 9 );
123 sprintf( buffer, "%s/%s", path, name );
124 strcat( buffer, ".so" );
125 /* check if the file exists */
126 if ((fd = open( buffer, O_RDONLY )) != -1)
128 close( fd );
129 return buffer;
131 free( buffer );
132 return NULL;
135 /* open the .so library for a given dll */
136 static char *open_library( const char *name )
138 char *fullname;
139 int i;
141 for (i = 0; i < nb_lib_paths; i++)
143 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
145 if (!(fullname = try_library_path( ".", name )))
146 fatal_error( "could not open .so file for %s\n", name );
147 return fullname;
150 /* read in the list of exported symbols of a .so */
151 static void read_exported_symbols( const char *name, struct import *imp )
153 FILE *f;
154 char buffer[1024], prefix[80], ord_prefix[80];
155 char *fullname, *cmdline;
156 int size, err;
158 imp->exports = NULL;
159 imp->nb_exports = size = 0;
161 if (!(fullname = open_library( name ))) return;
162 cmdline = xmalloc( strlen(fullname) + 7 );
163 sprintf( cmdline, "nm -D %s", fullname );
164 free( fullname );
166 if (!(f = popen( cmdline, "r" )))
167 fatal_error( "Cannot execute '%s'\n", cmdline );
169 sprintf( prefix, "__wine_dllexport_%s_", make_c_identifier(name) );
170 sprintf( ord_prefix, "__wine_ordexport_%s_", make_c_identifier(name) );
172 while (fgets( buffer, sizeof(buffer), f ))
174 int ordinal = 0, ord_only = 0;
175 char *p = buffer + strlen(buffer) - 1;
176 if (p < buffer) continue;
177 if (*p == '\n') *p-- = 0;
178 if (!(p = strstr( buffer, prefix )))
180 if (!(p = strstr( buffer, ord_prefix ))) continue;
181 ord_only = 1;
183 p += strlen(prefix);
184 if (isdigit(*p))
186 ordinal = strtol( p, &p, 10 );
187 if (*p++ != '_') continue;
188 if (ordinal >= MAX_ORDINALS) continue;
190 if (ord_only && !ordinal) continue;
192 if (imp->nb_exports == size)
194 size += 128;
195 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
197 imp->exports[imp->nb_exports].name = xstrdup( p );
198 imp->exports[imp->nb_exports].ordinal = ordinal;
199 imp->exports[imp->nb_exports].ord_only = ord_only;
200 imp->nb_exports++;
202 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
203 free( cmdline );
204 if (imp->nb_exports)
205 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
208 /* add a dll to the list of imports */
209 void add_import_dll( const char *name, int delay )
211 struct import *imp;
212 char *fullname;
213 int i;
215 fullname = xmalloc( strlen(name) + 5 );
216 strcpy( fullname, name );
217 if (!strchr( fullname, '.' )) strcat( fullname, ".dll" );
219 /* check if we already imported it */
220 for (i = 0; i < nb_imports; i++)
222 if (!strcmp( dll_imports[i]->dll, fullname ))
224 free( fullname );
225 return;
229 imp = xmalloc( sizeof(*imp) );
230 imp->dll = fullname;
231 imp->delay = delay;
232 imp->imports = NULL;
233 imp->nb_imports = 0;
235 if (delay) nb_delayed++;
236 read_exported_symbols( fullname, imp );
238 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
239 dll_imports[nb_imports++] = imp;
242 /* Add a symbol to the ignored symbol list */
243 void add_ignore_symbol( const char *name )
245 if (nb_ignore_symbols == ignore_size)
247 ignore_size += 32;
248 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
250 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
253 /* add a function to the list of imports from a given dll */
254 static void add_import_func( struct import *imp, const struct func *func )
256 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
257 imp->imports[imp->nb_imports].name = xstrdup( func->name );
258 imp->imports[imp->nb_imports].ordinal = func->ordinal;
259 imp->imports[imp->nb_imports].ord_only = func->ord_only;
260 imp->nb_imports++;
261 total_imports++;
262 if (imp->delay) total_delayed++;
265 /* add a symbol to the undef list */
266 inline static void add_undef_symbol( const char *name )
268 if (nb_undef_symbols == undef_size)
270 undef_size += 128;
271 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
273 undef_symbols[nb_undef_symbols++] = xstrdup( name );
276 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
277 static int remove_symbol_holes(void)
279 int i, off;
280 for (i = off = 0; i < nb_undef_symbols; i++)
282 if (!undef_symbols[i]) off++;
283 else undef_symbols[i - off] = undef_symbols[i];
285 nb_undef_symbols -= off;
286 return off;
289 /* add a symbol to the extra list, but only if needed */
290 static int add_extra_symbol( const char **extras, int *count, const char *name )
292 int i;
294 if (!find_symbol( name, undef_symbols, nb_undef_symbols ))
296 /* check if the symbol is being exported by this dll */
297 for (i = 0; i < nb_entry_points; i++)
299 ORDDEF *odp = EntryPoints[i];
300 if (odp->type == TYPE_STDCALL ||
301 odp->type == TYPE_CDECL ||
302 odp->type == TYPE_VARARGS ||
303 odp->type == TYPE_EXTERN)
305 if (odp->name && !strcmp( odp->name, name )) return 0;
308 extras[*count] = name;
309 (*count)++;
311 return 1;
314 /* add the extra undefined symbols that will be contained in the generated spec file itself */
315 static void add_extra_undef_symbols(void)
317 const char *extras[10];
318 int i, count = 0, nb_stubs = 0, nb_regs = 0;
319 int kernel_imports = 0, ntdll_imports = 0;
321 sort_symbols( undef_symbols, nb_undef_symbols );
323 for (i = 0; i < nb_entry_points; i++)
325 ORDDEF *odp = EntryPoints[i];
326 if (odp->type == TYPE_STUB) nb_stubs++;
327 if (odp->flags & FLAG_REGISTER) nb_regs++;
330 /* add symbols that will be contained in the spec file itself */
331 switch (SpecMode)
333 case SPEC_MODE_DLL:
334 break;
335 case SPEC_MODE_GUIEXE:
336 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA" );
337 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA" );
338 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA" );
339 /* fall through */
340 case SPEC_MODE_CUIEXE:
341 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess" );
342 break;
343 case SPEC_MODE_GUIEXE_UNICODE:
344 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA" );
345 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA" );
346 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA" );
347 /* fall through */
348 case SPEC_MODE_CUIEXE_UNICODE:
349 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess" );
350 break;
352 if (nb_delayed)
354 kernel_imports += add_extra_symbol( extras, &count, "LoadLibraryA" );
355 kernel_imports += add_extra_symbol( extras, &count, "GetProcAddress" );
357 if (nb_regs)
358 ntdll_imports += add_extra_symbol( extras, &count, "__wine_call_from_32_regs" );
359 if (nb_delayed || nb_stubs)
360 ntdll_imports += add_extra_symbol( extras, &count, "RtlRaiseException" );
362 /* make sure we import the dlls that contain these functions */
363 if (kernel_imports) add_import_dll( "kernel32", 0 );
364 if (ntdll_imports) add_import_dll( "ntdll", 0 );
366 if (count)
368 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
369 sort_symbols( undef_symbols, nb_undef_symbols );
373 /* warn if a given dll is not used, but check forwards first */
374 static void warn_unused( const struct import* imp )
376 int i;
377 size_t len = strlen(imp->dll);
378 const char *p = strchr( imp->dll, '.' );
379 if (p && !strcasecmp( p, ".dll" )) len = p - imp->dll;
381 for (i = Base; i <= Limit; i++)
383 ORDDEF *odp = Ordinals[i];
384 if (!odp || odp->type != TYPE_FORWARD) continue;
385 if (!strncasecmp( odp->link_name, imp->dll, len ) &&
386 odp->link_name[len] == '.')
387 return; /* found an import, do not warn */
389 warning( "%s imported but no symbols used\n", imp->dll );
392 /* combine a list of object files with ld into a single object file */
393 /* returns the name of the combined file */
394 static const char *ldcombine_files( char **argv )
396 int i, len = 0;
397 char *cmd;
398 int fd, err;
400 if (output_file_name && output_file_name[0])
402 ld_tmp_file = xmalloc( strlen(output_file_name) + 8 );
403 strcpy( ld_tmp_file, output_file_name );
404 strcat( ld_tmp_file, ".XXXXXX" );
406 else ld_tmp_file = xstrdup( "/tmp/winebuild.tmp.XXXXXX" );
408 if ((fd = mkstemp( ld_tmp_file ) == -1)) fatal_error( "could not generate a temp file\n" );
409 close( fd );
410 atexit( remove_ld_tmp_file );
412 for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
413 cmd = xmalloc( len + strlen(ld_tmp_file) + 10 );
414 sprintf( cmd, "ld -r -o %s", ld_tmp_file );
415 for (i = 0; argv[i]; i++) sprintf( cmd + strlen(cmd), " %s", argv[i] );
416 err = system( cmd );
417 if (err) fatal_error( "ld -r failed with status %d\n", err );
418 free( cmd );
419 return ld_tmp_file;
422 /* read in the list of undefined symbols */
423 void read_undef_symbols( char **argv )
425 FILE *f;
426 char buffer[1024];
427 int err;
428 const char *name;
430 if (!argv[0]) return;
432 undef_size = nb_undef_symbols = 0;
434 /* if we have multiple object files, link them together */
435 if (argv[1]) name = ldcombine_files( argv );
436 else name = argv[0];
438 sprintf( buffer, "nm -u %s", name );
439 if (!(f = popen( buffer, "r" )))
440 fatal_error( "Cannot execute '%s'\n", buffer );
442 while (fgets( buffer, sizeof(buffer), f ))
444 char *p = buffer + strlen(buffer) - 1;
445 if (p < buffer) continue;
446 if (*p == '\n') *p-- = 0;
447 p = buffer; while (*p == ' ') p++;
448 add_undef_symbol( p );
450 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
453 static void remove_ignored_symbols(void)
455 int i;
457 sort_symbols( ignore_symbols, nb_ignore_symbols );
458 for (i = 0; i < nb_undef_symbols; i++)
460 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
462 free( undef_symbols[i] );
463 undef_symbols[i] = NULL;
466 remove_symbol_holes();
469 /* resolve the imports for a Win32 module */
470 int resolve_imports( void )
472 int i, j;
474 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
476 add_extra_undef_symbols();
477 remove_ignored_symbols();
479 for (i = 0; i < nb_imports; i++)
481 struct import *imp = dll_imports[i];
483 for (j = 0; j < nb_undef_symbols; j++)
485 struct func *func = find_export( undef_symbols[j], imp->exports, imp->nb_exports );
486 if (func)
488 add_import_func( imp, func );
489 free( undef_symbols[j] );
490 undef_symbols[j] = NULL;
493 /* remove all the holes in the undef symbols list */
494 if (!remove_symbol_holes()) warn_unused( imp );
496 return 1;
499 /* output the import table of a Win32 module */
500 static int output_immediate_imports( FILE *outfile )
502 int i, j, pos;
503 int nb_imm = nb_imports - nb_delayed;
505 if (!nb_imm) goto done;
507 /* main import header */
509 fprintf( outfile, "\nstatic struct {\n" );
510 fprintf( outfile, " struct {\n" );
511 fprintf( outfile, " void *OriginalFirstThunk;\n" );
512 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
513 fprintf( outfile, " unsigned int ForwarderChain;\n" );
514 fprintf( outfile, " const char *Name;\n" );
515 fprintf( outfile, " void *FirstThunk;\n" );
516 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
517 fprintf( outfile, " const char *data[%d];\n",
518 total_imports - total_delayed + nb_imm );
519 fprintf( outfile, "} imports = {\n {\n" );
521 /* list of dlls */
523 for (i = j = 0; i < nb_imports; i++)
525 if (dll_imports[i]->delay) continue;
526 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
527 dll_imports[i]->dll, j );
528 j += dll_imports[i]->nb_imports + 1;
531 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
532 fprintf( outfile, " },\n {\n" );
534 /* list of imported functions */
536 for (i = 0; i < nb_imports; i++)
538 if (dll_imports[i]->delay) continue;
539 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
540 for (j = 0; j < dll_imports[i]->nb_imports; j++)
542 struct func *import = &dll_imports[i]->imports[j];
543 if (!import->ord_only)
545 unsigned short ord = import->ordinal;
546 fprintf( outfile, " \"\\%03o\\%03o%s\",\n",
547 *(unsigned char *)&ord, *((unsigned char *)&ord + 1), import->name );
549 else
550 fprintf( outfile, " (char *)%d,\n", import->ordinal );
552 fprintf( outfile, " 0,\n" );
554 fprintf( outfile, " }\n};\n\n" );
556 /* thunks for imported functions */
558 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
559 pos = 20 * (nb_imm + 1); /* offset of imports.data from start of imports */
560 fprintf( outfile, "asm(\".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
561 for (i = 0; i < nb_imports; i++)
563 if (dll_imports[i]->delay) continue;
564 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
566 struct func *import = &dll_imports[i]->imports[j];
567 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", import->name );
568 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", import->name );
569 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t", import->name);
571 #if defined(__i386__)
572 if (strstr( import->name, "__wine_call_from_16" ))
573 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
574 else
575 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
576 #elif defined(__sparc__)
577 if ( !UsePIC )
579 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
580 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
581 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
583 else
585 /* Hmpf. Stupid sparc assembler always interprets global variable
586 names as GOT offsets, so we have to do it the long way ... */
587 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
588 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
589 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
590 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
591 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
592 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
595 #elif defined(__PPC__)
596 fprintf(outfile, "\taddi 1, 1, -0x4\\n\"\n");
597 fprintf(outfile, "\t\"\\tstw 9, 0(1)\\n\"\n");
598 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
599 fprintf(outfile, "\t\"\\tstw 8, 0(1)\\n\"\n");
600 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
601 fprintf(outfile, "\t\"\\tstw 7, 0(1)\\n\"\n");
603 fprintf(outfile, "\t\"\\tlis 9,imports+%d@ha\\n\"\n", pos);
604 fprintf(outfile, "\t\"\\tla 8,imports+%d@l(9)\\n\"\n", pos);
605 fprintf(outfile, "\t\"\\tlwz 7, 0(8)\\n\"\n");
606 fprintf(outfile, "\t\"\\tmtctr 7\\n\"\n");
608 fprintf(outfile, "\t\"\\tlwz 7, 0(1)\\n\"\n");
609 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
610 fprintf(outfile, "\t\"\\tlwz 8, 0(1)\\n\"\n");
611 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
612 fprintf(outfile, "\t\"\\tlwz 9, 0(1)\\n\"\n");
613 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
614 fprintf(outfile, "\t\"\\tbctr\\n");
615 #else
616 #error You need to define import thunks for your architecture!
617 #endif
618 fprintf( outfile, "\"\n" );
620 pos += 4;
622 fprintf( outfile, "\".section\\t\\\".text\\\"\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
624 done:
625 return nb_imm;
628 /* output the delayed import table of a Win32 module */
629 static int output_delayed_imports( FILE *outfile )
631 int i, idx, j, pos;
633 if (!nb_delayed) goto done;
635 for (i = 0; i < nb_imports; i++)
637 if (!dll_imports[i]->delay) continue;
638 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
639 for (j = 0; j < dll_imports[i]->nb_imports; j++)
641 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n",
642 i, dll_imports[i]->imports[j].name );
645 fprintf( outfile, "\n" );
646 fprintf( outfile, "static struct {\n" );
647 fprintf( outfile, " struct ImgDelayDescr {\n" );
648 fprintf( outfile, " unsigned int grAttrs;\n" );
649 fprintf( outfile, " const char *szName;\n" );
650 fprintf( outfile, " void **phmod;\n" );
651 fprintf( outfile, " void **pIAT;\n" );
652 fprintf( outfile, " const char **pINT;\n" );
653 fprintf( outfile, " void* pBoundIAT;\n" );
654 fprintf( outfile, " void* pUnloadIAT;\n" );
655 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
656 fprintf( outfile, " } imp[%d];\n", nb_delayed );
657 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
658 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
659 fprintf( outfile, "} delay_imports = {\n" );
660 fprintf( outfile, " {\n" );
661 for (i = j = 0; i < nb_imports; i++)
663 if (!dll_imports[i]->delay) continue;
664 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
665 dll_imports[i]->dll, i, j, j );
666 j += dll_imports[i]->nb_imports;
668 fprintf( outfile, " },\n {\n" );
669 for (i = 0; i < nb_imports; i++)
671 if (!dll_imports[i]->delay) continue;
672 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
673 for (j = 0; j < dll_imports[i]->nb_imports; j++)
675 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, dll_imports[i]->imports[j].name);
678 fprintf( outfile, " },\n {\n" );
679 for (i = 0; i < nb_imports; i++)
681 if (!dll_imports[i]->delay) continue;
682 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
683 for (j = 0; j < dll_imports[i]->nb_imports; j++)
685 struct func *import = &dll_imports[i]->imports[j];
686 if (import->ord_only)
687 fprintf( outfile, " (char *)%d,\n", import->ordinal );
688 else
689 fprintf( outfile, " \"%s\",\n", import->name );
692 fprintf( outfile, " }\n};\n\n" );
694 /* check if there's some stub defined. if so, exception struct
695 * is already defined, so don't emit it twice
697 for (i = 0; i < nb_entry_points; i++) if (EntryPoints[i]->type == TYPE_STUB) break;
699 if (i == nb_entry_points) {
700 fprintf( outfile, "struct exc_record {\n" );
701 fprintf( outfile, " unsigned int code, flags;\n" );
702 fprintf( outfile, " void *rec, *addr;\n" );
703 fprintf( outfile, " unsigned int params;\n" );
704 fprintf( outfile, " const void *info[15];\n" );
705 fprintf( outfile, "};\n\n" );
706 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
709 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
710 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
711 fprintf( outfile, "\n" );
713 fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
714 fprintf( outfile, "{\n" );
715 fprintf( outfile, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
716 fprintf( outfile, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
717 fprintf( outfile, " void **pIAT = imd->pIAT + nr;\n" );
718 fprintf( outfile, " const char** pINT = imd->pINT + nr;\n" );
719 fprintf( outfile, " void *fn;\n\n" );
721 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
722 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT)))\n");
723 fprintf( outfile, " /* patch IAT with final value */\n" );
724 fprintf( outfile, " return *pIAT = fn;\n" );
725 fprintf( outfile, " else {\n");
726 fprintf( outfile, " struct exc_record rec;\n" );
727 fprintf( outfile, " rec.code = 0x80000100;\n" );
728 fprintf( outfile, " rec.flags = 1;\n" );
729 fprintf( outfile, " rec.rec = 0;\n" );
730 fprintf( outfile, " rec.params = 2;\n" );
731 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
732 fprintf( outfile, " rec.info[1] = *pINT + 2;\n" );
733 fprintf( outfile, "#ifdef __GNUC__\n" );
734 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
735 fprintf( outfile, "#else\n" );
736 fprintf( outfile, " rec.addr = 0;\n" );
737 fprintf( outfile, "#endif\n" );
738 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
739 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
740 fprintf( outfile, " }\n}\n\n" );
742 fprintf( outfile, "#ifndef __GNUC__\n" );
743 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
744 fprintf( outfile, "#endif\n" );
746 fprintf( outfile, "asm(\".align %d\\n\"\n", get_alignment(8) );
747 fprintf( outfile, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
748 fprintf( outfile, " \"" __ASM_NAME("__wine_delay_load_asm") ":\\n\"\n" );
749 #if defined(__i386__)
750 fprintf( outfile, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
751 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
752 fprintf( outfile, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
753 #elif defined(__sparc__)
754 fprintf( outfile, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
755 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
756 fprintf( outfile, " \"\\tmov %%g1, %%o0\\n\"\n" );
757 fprintf( outfile, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
758 #elif defined(__PPC__)
759 /* Save all callee saved registers into a stackframe. */
760 fprintf( outfile, " \"\\tstwu %%r1, -48(%%r1)\\n\"\n" );
761 fprintf( outfile, " \"\\tstw %%r3, 4(%%r1)\\n\"\n" );
762 fprintf( outfile, " \"\\tstw %%r4, 8(%%r1)\\n\"\n" );
763 fprintf( outfile, " \"\\tstw %%r5, 12(%%r1)\\n\"\n" );
764 fprintf( outfile, " \"\\tstw %%r6, 16(%%r1)\\n\"\n" );
765 fprintf( outfile, " \"\\tstw %%r7, 20(%%r1)\\n\"\n" );
766 fprintf( outfile, " \"\\tstw %%r8, 24(%%r1)\\n\"\n" );
767 fprintf( outfile, " \"\\tstw %%r9, 28(%%r1)\\n\"\n" );
768 fprintf( outfile, " \"\\tstw %%r10, 32(%%r1)\\n\"\n" );
769 fprintf( outfile, " \"\\tstw %%r11, 36(%%r1)\\n\"\n" );
770 fprintf( outfile, " \"\\tstw %%r12, 40(%%r1)\\n\"\n" );
772 /* r0 -> r3 (arg1) */
773 fprintf( outfile, " \"\\tmr %%r3, %%r0\\n\"\n" );
775 /* save return address */
776 fprintf( outfile, " \"\\tmflr %%r0\\n\"\n" );
777 fprintf( outfile, " \"\\tstw %%r0, 44(%%r1)\\n\"\n" );
779 /* Call the __wine_delay_load function, arg1 is arg1. */
780 fprintf( outfile, " \"\\tbl __wine_delay_load\\n\"\n" );
782 /* Load return value from call into ctr register */
783 fprintf( outfile, " \"\\tmtctr %%r3\\n\"\n" );
785 /* restore all saved registers and drop stackframe. */
786 fprintf( outfile, " \"\\tlwz %%r3, 4(%%r1)\\n\"\n" );
787 fprintf( outfile, " \"\\tlwz %%r4, 8(%%r1)\\n\"\n" );
788 fprintf( outfile, " \"\\tlwz %%r5, 12(%%r1)\\n\"\n" );
789 fprintf( outfile, " \"\\tlwz %%r6, 16(%%r1)\\n\"\n" );
790 fprintf( outfile, " \"\\tlwz %%r7, 20(%%r1)\\n\"\n" );
791 fprintf( outfile, " \"\\tlwz %%r8, 24(%%r1)\\n\"\n" );
792 fprintf( outfile, " \"\\tlwz %%r9, 28(%%r1)\\n\"\n" );
793 fprintf( outfile, " \"\\tlwz %%r10, 32(%%r1)\\n\"\n" );
794 fprintf( outfile, " \"\\tlwz %%r11, 36(%%r1)\\n\"\n" );
795 fprintf( outfile, " \"\\tlwz %%r12, 40(%%r1)\\n\"\n" );
796 /* Load return value from call into return register */
797 fprintf( outfile, " \"\\tlwz %%r0, 44(%%r1)\\n\"\n" );
798 fprintf( outfile, " \"\\tmtlr %%r0\\n\"\n" );
799 fprintf( outfile, " \"\\taddi %%r1, %%r1, 48\\n\"\n" );
800 /* branch to ctr register. */
801 fprintf( outfile, " \"\\tbctr\\n\"\n" );
803 #else
804 #error You need to defined delayed import thunks for your architecture!
805 #endif
807 for (i = idx = 0; i < nb_imports; i++)
809 if (!dll_imports[i]->delay) continue;
810 for (j = 0; j < dll_imports[i]->nb_imports; j++)
812 char buffer[128];
813 sprintf( buffer, "__wine_delay_imp_%d_%s", i, dll_imports[i]->imports[j].name );
814 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
815 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", buffer );
816 #if defined(__i386__)
817 fprintf( outfile, " \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
818 fprintf( outfile, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
819 #elif defined(__sparc__)
820 fprintf( outfile, " \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
821 fprintf( outfile, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
822 #elif defined(__PPC__)
823 /* g0 is a function scratch register or so I understand. */
824 fprintf( outfile, " \"\\tli %%r0, %d\\n\"\n", (idx << 16) | j );
825 fprintf( outfile, " \"\\tb __wine_delay_load_asm\\n\"\n" );
826 #else
827 #error You need to defined delayed import thunks for your architecture!
828 #endif
830 idx++;
833 fprintf( outfile, "\n \".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
834 pos = nb_delayed * 32;
835 for (i = 0; i < nb_imports; i++)
837 if (!dll_imports[i]->delay) continue;
838 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
840 struct func *import = &dll_imports[i]->imports[j];
841 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", import->name );
842 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", import->name );
843 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t\"", import->name);
844 #if defined(__i386__)
845 if (strstr( import->name, "__wine_call_from_16" ))
846 fprintf( outfile, "\".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n\"", pos );
847 else
848 fprintf( outfile, "\"jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n\"", pos );
849 #elif defined(__sparc__)
850 if ( !UsePIC )
852 fprintf( outfile, "\"sethi %%hi(delay_imports+%d), %%g1\\n\\t\"", pos );
853 fprintf( outfile, "\"ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t\"", pos );
854 fprintf( outfile, "\"jmp %%g1\\n\\tnop\\n\"" );
856 else
858 /* Hmpf. Stupid sparc assembler always interprets global variable
859 names as GOT offsets, so we have to do it the long way ... */
860 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
861 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
862 fprintf( outfile, "1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t", pos );
863 fprintf( outfile, "or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t", pos );
864 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
865 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
868 #elif defined(__PPC__)
869 fprintf(outfile, "\t\"addi 1, 1, -0x4\\n\"\n");
870 fprintf(outfile, "\t\"\\tstw 9, 0(1)\\n\"\n");
871 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
872 fprintf(outfile, "\t\"\\tstw 8, 0(1)\\n\"\n");
873 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
874 fprintf(outfile, "\t\"\\tstw 7, 0(1)\\n\"\n");
876 fprintf(outfile, "\t\"\\tlis 9,delay_imports+%d@ha\\n\"\n", pos);
877 fprintf(outfile, "\t\"\\tla 8,delay_imports+%d@l(9)\\n\"\n", pos);
878 fprintf(outfile, "\t\"\\tlwz 7, 0(8)\\n\"\n");
879 fprintf(outfile, "\t\"\\tmtctr 7\\n\"\n");
881 fprintf(outfile, "\t\"\\tlwz 7, 0(1)\\n\"\n");
882 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
883 fprintf(outfile, "\t\"\\tlwz 8, 0(1)\\n\"\n");
884 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
885 fprintf(outfile, "\t\"\\tlwz 9, 0(1)\\n\"\n");
886 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
887 fprintf(outfile, "\t\"\\tbctr\\n\"");
889 /*fprintf(outfile, "\t\"li r0,delay_imports\\n\\t\"\n" );
890 fprintf(outfile, "\t\"lwz r0, %d(r0)\\n\\t\"\n", pos);
891 fprintf(outfile, "\t\"mtctr r0\\n\\t\"\n");
892 fprintf(outfile, "\t\"bctr\\n\"");*/
893 #else
894 #error You need to define delayed import thunks for your architecture!
895 #endif
896 fprintf( outfile, "\n" );
899 fprintf( outfile, "\".section \\\".text\\\"\");\n" );
900 fprintf( outfile, "#ifndef __GNUC__\n" );
901 fprintf( outfile, "}\n" );
902 fprintf( outfile, "#endif\n" );
903 fprintf( outfile, "\n" );
905 done:
906 return nb_delayed;
909 /* output the import and delayed import tables of a Win32 module
910 * returns number of DLLs exported in 'immediate' mode
912 int output_imports( FILE *outfile )
914 output_delayed_imports( outfile );
915 return output_immediate_imports( outfile );