Put correct ordinal hints in the import table.
[wine/multimedia.git] / tools / winebuild / import.c
blob4d4da734d24a457d85b1be6d34902145ffc06379
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>
30 #include "build.h"
32 struct func
34 const char *name; /* function name */
35 int ordinal; /* function ordinal */
38 struct import
40 char *dll; /* dll name */
41 int delay; /* delay or not dll loading ? */
42 struct func *exports; /* functions exported from this dll */
43 int nb_exports; /* number of exported functions */
44 struct func *imports; /* functions we want to import from this dll */
45 int nb_imports; /* number of imported functions */
46 int lineno; /* line in .spec file where import is defined */
49 static char **undef_symbols; /* list of undefined symbols */
50 static int nb_undef_symbols = -1;
51 static int undef_size;
53 static char **ignore_symbols; /* list of symbols to ignore */
54 static int nb_ignore_symbols;
55 static int ignore_size;
57 static struct import **dll_imports = NULL;
58 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
59 static int nb_delayed = 0; /* number of delayed dlls */
60 static int total_imports = 0; /* total number of imported functions */
61 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
63 /* compare function names; helper for resolve_imports */
64 static int name_cmp( const void *name, const void *entry )
66 return strcmp( *(char **)name, *(char **)entry );
69 /* compare function names; helper for resolve_imports */
70 static int func_cmp( const void *func1, const void *func2 )
72 return strcmp( ((struct func *)func1)->name, ((struct func *)func2)->name );
75 /* locate a symbol in a (sorted) list */
76 inline static const char *find_symbol( const char *name, char **table, int size )
78 char **res = NULL;
80 if (table) {
81 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
84 return res ? *res : NULL;
87 /* locate an export in a (sorted) export list */
88 inline static struct func *find_export( const char *name, struct func *table, int size )
90 struct func func, *res = NULL;
92 func.name = name;
93 func.ordinal = -1;
94 if (table) res = bsearch( &func, table, size, sizeof(*table), func_cmp );
95 return res;
98 /* sort a symbol table */
99 inline static void sort_symbols( char **table, int size )
101 if (table )
102 qsort( table, size, sizeof(*table), name_cmp );
105 /* open the .so library for a given dll in a specified path */
106 static char *try_library_path( const char *path, const char *name )
108 char *buffer;
109 int fd;
111 buffer = xmalloc( strlen(path) + strlen(name) + 9 );
112 sprintf( buffer, "%s/%s", path, name );
113 strcat( buffer, ".so" );
114 /* check if the file exists */
115 if ((fd = open( buffer, O_RDONLY )) != -1)
117 close( fd );
118 return buffer;
120 free( buffer );
121 return NULL;
124 /* open the .so library for a given dll */
125 static char *open_library( const char *name )
127 char *fullname;
128 int i;
130 for (i = 0; i < nb_lib_paths; i++)
132 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
134 if (!(fullname = try_library_path( ".", name )))
135 fatal_error( "could not open .so file for %s\n", name );
136 return fullname;
139 /* read in the list of exported symbols of a .so */
140 static void read_exported_symbols( const char *name, struct import *imp )
142 FILE *f;
143 char buffer[1024], prefix[80];
144 char *fullname, *cmdline;
145 int size, err;
147 imp->exports = NULL;
148 imp->nb_exports = size = 0;
150 if (!(fullname = open_library( name ))) return;
151 cmdline = xmalloc( strlen(fullname) + 7 );
152 sprintf( cmdline, "nm -D %s", fullname );
153 free( fullname );
155 if (!(f = popen( cmdline, "r" )))
156 fatal_error( "Cannot execute '%s'\n", cmdline );
158 sprintf( prefix, "__wine_dllexport_%s_", make_c_identifier(name) );
160 while (fgets( buffer, sizeof(buffer), f ))
162 int ordinal = 0;
163 char *p = buffer + strlen(buffer) - 1;
164 if (p < buffer) continue;
165 if (*p == '\n') *p-- = 0;
166 if (!(p = strstr( buffer, prefix ))) continue;
167 p += strlen(prefix);
168 if (isdigit(*p))
170 ordinal = strtol( p, &p, 10 );
171 if (*p++ != '_') continue;
174 if (imp->nb_exports == size)
176 size += 128;
177 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
179 imp->exports[imp->nb_exports].name = xstrdup( p );
180 imp->exports[imp->nb_exports].ordinal = ordinal;
181 imp->nb_exports++;
183 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
184 free( cmdline );
185 if (imp->nb_exports)
186 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
189 /* add a dll to the list of imports */
190 void add_import_dll( const char *name, int delay )
192 struct import *imp;
193 char *fullname;
194 int i;
196 fullname = xmalloc( strlen(name) + 5 );
197 strcpy( fullname, name );
198 if (!strchr( fullname, '.' )) strcat( fullname, ".dll" );
200 /* check if we already imported it */
201 for (i = 0; i < nb_imports; i++)
203 if (!strcmp( dll_imports[i]->dll, fullname ))
205 free( fullname );
206 return;
210 imp = xmalloc( sizeof(*imp) );
211 imp->dll = fullname;
212 imp->delay = delay;
213 imp->imports = NULL;
214 imp->nb_imports = 0;
215 /* GetToken for the file name has swallowed the '\n', hence pointing to next line */
216 imp->lineno = current_line - 1;
218 if (delay) nb_delayed++;
219 read_exported_symbols( fullname, imp );
221 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
222 dll_imports[nb_imports++] = imp;
225 /* Add a symbol to the ignored symbol list */
226 void add_ignore_symbol( const char *name )
228 if (nb_ignore_symbols == ignore_size)
230 ignore_size += 32;
231 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
233 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
236 /* add a function to the list of imports from a given dll */
237 static void add_import_func( struct import *imp, const struct func *func )
239 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
240 imp->imports[imp->nb_imports].name = xstrdup( func->name );
241 imp->imports[imp->nb_imports].ordinal = func->ordinal;
242 imp->nb_imports++;
243 total_imports++;
244 if (imp->delay) total_delayed++;
247 /* add a symbol to the undef list */
248 inline static void add_undef_symbol( const char *name )
250 if (nb_undef_symbols == undef_size)
252 undef_size += 128;
253 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
255 undef_symbols[nb_undef_symbols++] = xstrdup( name );
258 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
259 static int remove_symbol_holes(void)
261 int i, off;
262 for (i = off = 0; i < nb_undef_symbols; i++)
264 if (!undef_symbols[i]) off++;
265 else undef_symbols[i - off] = undef_symbols[i];
267 nb_undef_symbols -= off;
268 return off;
271 /* add a symbol to the extra list, but only if needed */
272 static int add_extra_symbol( const char **extras, int *count, const char *name )
274 int i;
276 if (!find_symbol( name, undef_symbols, nb_undef_symbols ))
278 /* check if the symbol is being exported by this dll */
279 for (i = 0; i < nb_entry_points; i++)
281 ORDDEF *odp = EntryPoints[i];
282 if (odp->type == TYPE_STDCALL ||
283 odp->type == TYPE_CDECL ||
284 odp->type == TYPE_VARARGS ||
285 odp->type == TYPE_EXTERN)
287 if (!strcmp( odp->name, name )) return 0;
290 extras[*count] = name;
291 (*count)++;
293 return 1;
296 /* add the extra undefined symbols that will be contained in the generated spec file itself */
297 static void add_extra_undef_symbols(void)
299 const char *extras[10];
300 int i, count = 0, nb_stubs = 0, nb_regs = 0;
301 int kernel_imports = 0, ntdll_imports = 0;
303 sort_symbols( undef_symbols, nb_undef_symbols );
305 for (i = 0; i < nb_entry_points; i++)
307 ORDDEF *odp = EntryPoints[i];
308 if (odp->type == TYPE_STUB) nb_stubs++;
309 if (odp->flags & FLAG_REGISTER) nb_regs++;
312 /* add symbols that will be contained in the spec file itself */
313 switch (SpecMode)
315 case SPEC_MODE_DLL:
316 break;
317 case SPEC_MODE_GUIEXE:
318 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA" );
319 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA" );
320 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA" );
321 /* fall through */
322 case SPEC_MODE_CUIEXE:
323 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess" );
324 break;
325 case SPEC_MODE_GUIEXE_UNICODE:
326 kernel_imports += add_extra_symbol( extras, &count, "GetCommandLineA" );
327 kernel_imports += add_extra_symbol( extras, &count, "GetStartupInfoA" );
328 kernel_imports += add_extra_symbol( extras, &count, "GetModuleHandleA" );
329 /* fall through */
330 case SPEC_MODE_CUIEXE_UNICODE:
331 kernel_imports += add_extra_symbol( extras, &count, "ExitProcess" );
332 break;
334 if (nb_delayed)
336 kernel_imports += add_extra_symbol( extras, &count, "LoadLibraryA" );
337 kernel_imports += add_extra_symbol( extras, &count, "GetProcAddress" );
339 if (nb_regs)
340 ntdll_imports += add_extra_symbol( extras, &count, "__wine_call_from_32_regs" );
341 if (nb_delayed || nb_stubs)
342 ntdll_imports += add_extra_symbol( extras, &count, "RtlRaiseException" );
344 /* make sure we import the dlls that contain these functions */
345 if (kernel_imports) add_import_dll( "kernel32", 0 );
346 if (ntdll_imports) add_import_dll( "ntdll", 0 );
348 if (count)
350 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
351 sort_symbols( undef_symbols, nb_undef_symbols );
355 /* warn if a given dll is not used, but check forwards first */
356 static void warn_unused( const struct import* imp )
358 int i, curline;
359 size_t len = strlen(imp->dll);
360 const char *p = strchr( imp->dll, '.' );
361 if (p && !strcasecmp( p, ".dll" )) len = p - imp->dll;
363 for (i = Base; i <= Limit; i++)
365 ORDDEF *odp = Ordinals[i];
366 if (!odp || odp->type != TYPE_FORWARD) continue;
367 if (!strncasecmp( odp->link_name, imp->dll, len ) &&
368 odp->link_name[len] == '.')
369 return; /* found an import, do not warn */
371 /* switch current_line temporarily to the line of the import declaration */
372 curline = current_line;
373 current_line = imp->lineno;
374 warning( "%s imported but no symbols used\n", imp->dll );
375 current_line = curline;
378 /* read in the list of undefined symbols */
379 void read_undef_symbols( const char *name )
381 FILE *f;
382 char buffer[1024];
383 int err;
385 undef_size = nb_undef_symbols = 0;
387 sprintf( buffer, "nm -u %s", name );
388 if (!(f = popen( buffer, "r" )))
389 fatal_error( "Cannot execute '%s'\n", buffer );
391 while (fgets( buffer, sizeof(buffer), f ))
393 char *p = buffer + strlen(buffer) - 1;
394 if (p < buffer) continue;
395 if (*p == '\n') *p-- = 0;
396 p = buffer; while (*p == ' ') p++;
397 add_undef_symbol( p );
399 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
402 static void remove_ignored_symbols(void)
404 int i;
406 sort_symbols( ignore_symbols, nb_ignore_symbols );
407 for (i = 0; i < nb_undef_symbols; i++)
409 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
411 free( undef_symbols[i] );
412 undef_symbols[i] = NULL;
415 remove_symbol_holes();
418 /* resolve the imports for a Win32 module */
419 int resolve_imports( void )
421 int i, j;
423 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
425 add_extra_undef_symbols();
426 remove_ignored_symbols();
428 for (i = 0; i < nb_imports; i++)
430 struct import *imp = dll_imports[i];
432 for (j = 0; j < nb_undef_symbols; j++)
434 struct func *func = find_export( undef_symbols[j], imp->exports, imp->nb_exports );
435 if (func)
437 add_import_func( imp, func );
438 free( undef_symbols[j] );
439 undef_symbols[j] = NULL;
442 /* remove all the holes in the undef symbols list */
443 if (!remove_symbol_holes()) warn_unused( imp );
445 return 1;
448 /* output the import table of a Win32 module */
449 static int output_immediate_imports( FILE *outfile )
451 int i, j, pos;
452 int nb_imm = nb_imports - nb_delayed;
454 if (!nb_imm) goto done;
456 /* main import header */
458 fprintf( outfile, "\nstatic struct {\n" );
459 fprintf( outfile, " struct {\n" );
460 fprintf( outfile, " void *OriginalFirstThunk;\n" );
461 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
462 fprintf( outfile, " unsigned int ForwarderChain;\n" );
463 fprintf( outfile, " const char *Name;\n" );
464 fprintf( outfile, " void *FirstThunk;\n" );
465 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
466 fprintf( outfile, " const char *data[%d];\n",
467 total_imports - total_delayed + nb_imm );
468 fprintf( outfile, "} imports = {\n {\n" );
470 /* list of dlls */
472 for (i = j = 0; i < nb_imports; i++)
474 if (dll_imports[i]->delay) continue;
475 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
476 dll_imports[i]->dll, j );
477 j += dll_imports[i]->nb_imports + 1;
480 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
481 fprintf( outfile, " },\n {\n" );
483 /* list of imported functions */
485 for (i = 0; i < nb_imports; i++)
487 if (dll_imports[i]->delay) continue;
488 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
489 for (j = 0; j < dll_imports[i]->nb_imports; j++)
491 struct func *import = &dll_imports[i]->imports[j];
492 unsigned short ord = import->ordinal;
493 fprintf( outfile, " \"\\%03o\\%03o%s\",\n",
494 *(unsigned char *)&ord, *((unsigned char *)&ord + 1), import->name );
496 fprintf( outfile, " 0,\n" );
498 fprintf( outfile, " }\n};\n\n" );
500 /* thunks for imported functions */
502 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
503 pos = 20 * (nb_imm + 1); /* offset of imports.data from start of imports */
504 fprintf( outfile, "asm(\".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
505 for (i = 0; i < nb_imports; i++)
507 if (dll_imports[i]->delay) continue;
508 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
510 struct func *import = &dll_imports[i]->imports[j];
511 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", import->name );
512 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", import->name );
513 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t", import->name);
515 #if defined(__i386__)
516 if (strstr( import->name, "__wine_call_from_16" ))
517 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
518 else
519 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
520 #elif defined(__sparc__)
521 if ( !UsePIC )
523 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
524 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
525 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
527 else
529 /* Hmpf. Stupid sparc assembler always interprets global variable
530 names as GOT offsets, so we have to do it the long way ... */
531 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
532 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
533 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
534 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
535 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
536 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
539 #elif defined(__PPC__)
540 fprintf(outfile, "\taddi 1, 1, -0x4\\n\"\n");
541 fprintf(outfile, "\t\"\\tstw 9, 0(1)\\n\"\n");
542 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
543 fprintf(outfile, "\t\"\\tstw 8, 0(1)\\n\"\n");
544 fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
545 fprintf(outfile, "\t\"\\tstw 7, 0(1)\\n\"\n");
547 fprintf(outfile, "\t\"\\tlis 9,imports+%d@ha\\n\"\n", pos);
548 fprintf(outfile, "\t\"\\tla 8,imports+%d@l(9)\\n\"\n", pos);
549 fprintf(outfile, "\t\"\\tlwz 7, 0(8)\\n\"\n");
550 fprintf(outfile, "\t\"\\tmtctr 7\\n\"\n");
552 fprintf(outfile, "\t\"\\tlwz 7, 0(1)\\n\"\n");
553 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
554 fprintf(outfile, "\t\"\\tlwz 8, 0(1)\\n\"\n");
555 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
556 fprintf(outfile, "\t\"\\tlwz 9, 0(1)\\n\"\n");
557 fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
558 fprintf(outfile, "\t\"\\tbctr\\n");
559 #else
560 #error You need to define import thunks for your architecture!
561 #endif
562 fprintf( outfile, "\"\n" );
564 pos += 4;
566 fprintf( outfile, "\".section\\t\\\".text\\\"\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
568 done:
569 return nb_imm;
572 /* output the delayed import table of a Win32 module */
573 static int output_delayed_imports( FILE *outfile )
575 int i, idx, j, pos;
577 if (!nb_delayed) goto done;
579 for (i = 0; i < nb_imports; i++)
581 if (!dll_imports[i]->delay) continue;
582 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
583 for (j = 0; j < dll_imports[i]->nb_imports; j++)
585 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n",
586 i, dll_imports[i]->imports[j].name );
589 fprintf( outfile, "\n" );
590 fprintf( outfile, "static struct {\n" );
591 fprintf( outfile, " struct ImgDelayDescr {\n" );
592 fprintf( outfile, " unsigned int grAttrs;\n" );
593 fprintf( outfile, " const char *szName;\n" );
594 fprintf( outfile, " void **phmod;\n" );
595 fprintf( outfile, " void **pIAT;\n" );
596 fprintf( outfile, " const char **pINT;\n" );
597 fprintf( outfile, " void* pBoundIAT;\n" );
598 fprintf( outfile, " void* pUnloadIAT;\n" );
599 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
600 fprintf( outfile, " } imp[%d];\n", nb_delayed );
601 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
602 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
603 fprintf( outfile, "} delay_imports = {\n" );
604 fprintf( outfile, " {\n" );
605 for (i = j = 0; i < nb_imports; i++)
607 if (!dll_imports[i]->delay) continue;
608 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
609 dll_imports[i]->dll, i, j, j );
610 j += dll_imports[i]->nb_imports;
612 fprintf( outfile, " },\n {\n" );
613 for (i = 0; i < nb_imports; i++)
615 if (!dll_imports[i]->delay) continue;
616 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
617 for (j = 0; j < dll_imports[i]->nb_imports; j++)
619 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, dll_imports[i]->imports[j].name);
622 fprintf( outfile, " },\n {\n" );
623 for (i = 0; i < nb_imports; i++)
625 if (!dll_imports[i]->delay) continue;
626 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
627 for (j = 0; j < dll_imports[i]->nb_imports; j++)
629 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j].name );
632 fprintf( outfile, " }\n};\n\n" );
634 /* check if there's some stub defined. if so, exception struct
635 * is already defined, so don't emit it twice
637 for (i = 0; i < nb_entry_points; i++) if (EntryPoints[i]->type == TYPE_STUB) break;
639 if (i == nb_entry_points) {
640 fprintf( outfile, "struct exc_record {\n" );
641 fprintf( outfile, " unsigned int code, flags;\n" );
642 fprintf( outfile, " void *rec, *addr;\n" );
643 fprintf( outfile, " unsigned int params;\n" );
644 fprintf( outfile, " const void *info[15];\n" );
645 fprintf( outfile, "};\n\n" );
646 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
649 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
650 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
651 fprintf( outfile, "\n" );
653 fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
654 fprintf( outfile, "{\n" );
655 fprintf( outfile, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
656 fprintf( outfile, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
657 fprintf( outfile, " void **pIAT = imd->pIAT + nr;\n" );
658 fprintf( outfile, " const char** pINT = imd->pINT + nr;\n" );
659 fprintf( outfile, " void *fn;\n\n" );
661 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
662 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT + 2)))\n");
663 fprintf( outfile, " /* patch IAT with final value */\n" );
664 fprintf( outfile, " return *pIAT = fn;\n" );
665 fprintf( outfile, " else {\n");
666 fprintf( outfile, " struct exc_record rec;\n" );
667 fprintf( outfile, " rec.code = 0x80000100;\n" );
668 fprintf( outfile, " rec.flags = 1;\n" );
669 fprintf( outfile, " rec.rec = 0;\n" );
670 fprintf( outfile, " rec.params = 2;\n" );
671 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
672 fprintf( outfile, " rec.info[1] = *pINT + 2;\n" );
673 fprintf( outfile, "#ifdef __GNUC__\n" );
674 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
675 fprintf( outfile, "#else\n" );
676 fprintf( outfile, " rec.addr = 0;\n" );
677 fprintf( outfile, "#endif\n" );
678 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
679 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
680 fprintf( outfile, " }\n}\n\n" );
682 fprintf( outfile, "#ifndef __GNUC__\n" );
683 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
684 fprintf( outfile, "#endif\n" );
686 fprintf( outfile, "asm(\".align %d\\n\"\n", get_alignment(8) );
687 fprintf( outfile, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
688 fprintf( outfile, " \"" __ASM_NAME("__wine_delay_load_asm") ":\\n\"\n" );
689 #if defined(__i386__)
690 fprintf( outfile, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
691 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
692 fprintf( outfile, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
693 #elif defined(__sparc__)
694 fprintf( outfile, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
695 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
696 fprintf( outfile, " \"\\tmov %%g1, %%o0\\n\"\n" );
697 fprintf( outfile, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
698 #elif defined(__PPC__)
699 fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
700 #else
701 #error You need to defined delayed import thunks for your architecture!
702 #endif
704 for (i = idx = 0; i < nb_imports; i++)
706 if (!dll_imports[i]->delay) continue;
707 for (j = 0; j < dll_imports[i]->nb_imports; j++)
709 char buffer[128];
710 sprintf( buffer, "__wine_delay_imp_%d_%s", i, dll_imports[i]->imports[j].name );
711 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
712 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\"\n", buffer );
713 #if defined(__i386__)
714 fprintf( outfile, " \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
715 fprintf( outfile, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
716 #elif defined(__sparc__)
717 fprintf( outfile, " \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
718 fprintf( outfile, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
719 #elif defined(__PPC__)
720 fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
721 #else
722 #error You need to defined delayed import thunks for your architecture!
723 #endif
725 idx++;
728 fprintf( outfile, "\n \".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
729 pos = nb_delayed * 32;
730 for (i = 0; i < nb_imports; i++)
732 if (!dll_imports[i]->delay) continue;
733 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
735 struct func *import = &dll_imports[i]->imports[j];
736 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", import->name );
737 fprintf( outfile, " \"\\t.globl " __ASM_NAME("%s") "\\n\"\n", import->name );
738 fprintf( outfile, " \"" __ASM_NAME("%s") ":\\n\\t", import->name);
739 #if defined(__i386__)
740 if (strstr( import->name, "__wine_call_from_16" ))
741 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n", pos );
742 else
743 fprintf( outfile, "jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
744 #elif defined(__sparc__)
745 if ( !UsePIC )
747 fprintf( outfile, "sethi %%hi(delay_imports+%d), %%g1\\n\\t", pos );
748 fprintf( outfile, "ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t", pos );
749 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
751 else
753 /* Hmpf. Stupid sparc assembler always interprets global variable
754 names as GOT offsets, so we have to do it the long way ... */
755 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
756 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
757 fprintf( outfile, "1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t", pos );
758 fprintf( outfile, "or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t", pos );
759 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
760 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
763 #elif defined(__PPC__)
764 fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
765 #else
766 #error You need to define delayed import thunks for your architecture!
767 #endif
768 fprintf( outfile, "\"\n" );
771 fprintf( outfile, "\".section \\\".text\\\"\");\n" );
772 fprintf( outfile, "#ifndef __GNUC__\n" );
773 fprintf( outfile, "}\n" );
774 fprintf( outfile, "#endif\n" );
775 fprintf( outfile, "\n" );
777 done:
778 return nb_delayed;
781 /* output the import and delayed import tables of a Win32 module
782 * returns number of DLLs exported in 'immediate' mode
784 int output_imports( FILE *outfile )
786 output_delayed_imports( outfile );
787 return output_immediate_imports( outfile );