4 * Copyright 2000 Alexandre Julliard
19 char *dll; /* dll name */
20 int delay; /* delay or not dll loading ? */
21 char **exports; /* functions exported from this dll */
22 int nb_exports; /* number of exported functions */
23 char **imports; /* functions we want to import from this dll */
24 int nb_imports; /* number of imported functions */
25 int lineno; /* line in .spec file where import is defined */
28 static char **undef_symbols; /* list of undefined symbols */
29 static int nb_undef_symbols = -1;
30 static int undef_size;
32 static char **ignore_symbols; /* list of symbols to ignore */
33 static int nb_ignore_symbols;
34 static int ignore_size;
36 static struct import **dll_imports = NULL;
37 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
38 static int nb_delayed = 0; /* number of delayed dlls */
39 static int total_imports = 0; /* total number of imported functions */
40 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
42 /* compare function names; helper for resolve_imports */
43 static int name_cmp( const void *name, const void *entry )
45 return strcmp( *(char **)name, *(char **)entry );
48 /* locate a symbol in a (sorted) list */
49 inline static const char *find_symbol( const char *name, char **table, int size )
54 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
57 return res ? *res : NULL;
60 /* sort a symbol table */
61 inline static void sort_symbols( char **table, int size )
64 qsort( table, size, sizeof(*table), name_cmp );
67 /* open the .so library for a given dll in a specified path */
68 static char *try_library_path( const char *path, const char *name )
73 buffer = xmalloc( strlen(path) + strlen(name) + 8 );
74 sprintf( buffer, "%s/lib%s", path, name );
75 p = buffer + strlen(buffer) - 4;
76 if (!strcmp( p, ".dll" )) *p = 0;
77 strcat( buffer, ".so" );
78 /* check if the file exists */
79 if ((fd = open( buffer, O_RDONLY )) == -1) return NULL;
84 /* open the .so library for a given dll */
85 static char *open_library( const char *name )
90 for (i = 0; i < nb_lib_paths; i++)
92 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
94 if (!(fullname = try_library_path( ".", name )))
95 fatal_error( "could not open .so file for %s\n", name );
99 /* read in the list of exported symbols of a .so */
100 static void read_exported_symbols( const char *name, struct import *imp )
104 char *fullname, *cmdline;
109 imp->nb_exports = size = 0;
111 if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
113 if (!(fullname = open_library( name ))) return;
114 cmdline = xmalloc( strlen(fullname) + 4 );
115 sprintf( cmdline, "nm %s", fullname );
118 if (!(f = popen( cmdline, "r" )))
119 fatal_error( "Cannot execute '%s'\n", cmdline );
121 while (fgets( buffer, sizeof(buffer), f ))
123 char *p = buffer + strlen(buffer) - 1;
124 if (p < buffer) continue;
125 if (*p == '\n') *p-- = 0;
126 if (!(p = strstr( buffer, "__wine_dllexport_" ))) continue;
128 if (strncmp( p, name, ext - name )) continue;
130 if (*p++ != '_') continue;
132 if (imp->nb_exports == size)
135 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
137 imp->exports[imp->nb_exports++] = xstrdup( p );
139 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
141 sort_symbols( imp->exports, imp->nb_exports );
144 /* add a dll to the list of imports */
145 void add_import_dll( const char *name, int delay )
147 struct import *imp = xmalloc( sizeof(*imp) );
148 imp->dll = xstrdup( name );
152 /* GetToken for the file name has swallowed the '\n', hence pointing to next line */
153 imp->lineno = current_line - 1;
155 if (delay) nb_delayed++;
156 read_exported_symbols( name, imp );
158 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
159 dll_imports[nb_imports++] = imp;
162 /* Add a symbol to the ignored symbol list */
163 void add_ignore_symbol( const char *name )
165 if (nb_ignore_symbols == ignore_size)
168 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
170 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
173 /* add a function to the list of imports from a given dll */
174 static void add_import_func( struct import *imp, const char *name )
176 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
177 imp->imports[imp->nb_imports++] = xstrdup( name );
179 if (imp->delay) total_delayed++;
182 /* add a symbol to the undef list */
183 inline static void add_undef_symbol( const char *name )
185 if (nb_undef_symbols == undef_size)
188 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
190 undef_symbols[nb_undef_symbols++] = xstrdup( name );
193 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
194 static int remove_symbol_holes(void)
197 for (i = off = 0; i < nb_undef_symbols; i++)
199 if (!undef_symbols[i]) off++;
200 else undef_symbols[i - off] = undef_symbols[i];
202 nb_undef_symbols -= off;
206 /* add the extra undefined symbols that will be contained in the generated spec file itself */
207 static void add_extra_undef_symbols(void)
209 const char *extras[8];
212 #define ADD_SYM(name) \
213 do { if (!find_symbol( extras[count] = (name), undef_symbols, \
214 nb_undef_symbols )) count++; } while(0)
216 sort_symbols( undef_symbols, nb_undef_symbols );
218 /* add symbols that will be contained in the spec file itself */
223 case SPEC_MODE_GUIEXE:
224 ADD_SYM( "GetCommandLineA" );
225 ADD_SYM( "GetStartupInfoA" );
226 ADD_SYM( "GetModuleHandleA" );
228 case SPEC_MODE_CUIEXE:
229 ADD_SYM( "__wine_get_main_args" );
230 ADD_SYM( "ExitProcess" );
232 case SPEC_MODE_GUIEXE_UNICODE:
233 ADD_SYM( "GetCommandLineA" );
234 ADD_SYM( "GetStartupInfoA" );
235 ADD_SYM( "GetModuleHandleA" );
237 case SPEC_MODE_CUIEXE_UNICODE:
238 ADD_SYM( "__wine_get_wmain_args" );
239 ADD_SYM( "ExitProcess" );
242 ADD_SYM( "RtlRaiseException" );
245 ADD_SYM( "LoadLibraryA" );
246 ADD_SYM( "GetProcAddress" );
251 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
252 sort_symbols( undef_symbols, nb_undef_symbols );
256 /* warn if a given dll is not used, but check forwards first */
257 static void warn_unused( const struct import* imp )
260 size_t len = strlen(imp->dll);
261 const char *p = strchr( imp->dll, '.' );
262 if (p && !strcasecmp( p, ".dll" )) len = p - imp->dll;
264 for (i = Base; i <= Limit; i++)
266 ORDDEF *odp = Ordinals[i];
267 if (!odp || odp->type != TYPE_FORWARD) continue;
268 if (!strncasecmp( odp->link_name, imp->dll, len ) &&
269 odp->link_name[len] == '.')
270 return; /* found an import, do not warn */
272 /* switch current_line temporarily to the line of the import declaration */
273 curline = current_line;
274 current_line = imp->lineno;
275 warning( "%s imported but no symbols used\n", imp->dll );
276 current_line = curline;
279 /* read in the list of undefined symbols */
280 void read_undef_symbols( const char *name )
286 undef_size = nb_undef_symbols = 0;
288 sprintf( buffer, "nm -u %s", name );
289 if (!(f = popen( buffer, "r" )))
290 fatal_error( "Cannot execute '%s'\n", buffer );
292 while (fgets( buffer, sizeof(buffer), f ))
294 char *p = buffer + strlen(buffer) - 1;
295 if (p < buffer) continue;
296 if (*p == '\n') *p-- = 0;
297 p = buffer; while (*p == ' ') p++;
298 add_undef_symbol( p );
300 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
303 static void remove_ignored_symbols(void)
307 sort_symbols( ignore_symbols, nb_ignore_symbols );
308 for (i = 0; i < nb_undef_symbols; i++)
310 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
312 free( undef_symbols[i] );
313 undef_symbols[i] = NULL;
316 remove_symbol_holes();
319 /* resolve the imports for a Win32 module */
320 int resolve_imports( FILE *outfile )
324 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
326 add_extra_undef_symbols();
327 remove_ignored_symbols();
329 for (i = 0; i < nb_imports; i++)
331 struct import *imp = dll_imports[i];
333 for (j = 0; j < nb_undef_symbols; j++)
335 const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
338 add_import_func( imp, res );
339 free( undef_symbols[j] );
340 undef_symbols[j] = NULL;
343 /* remove all the holes in the undef symbols list */
344 if (!remove_symbol_holes()) warn_unused( imp );
349 /* output the import table of a Win32 module */
350 static int output_immediate_imports( FILE *outfile )
353 int nb_imm = nb_imports - nb_delayed;
355 if (!nb_imm) goto done;
357 /* main import header */
359 fprintf( outfile, "\nstatic struct {\n" );
360 fprintf( outfile, " struct {\n" );
361 fprintf( outfile, " void *OriginalFirstThunk;\n" );
362 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
363 fprintf( outfile, " unsigned int ForwarderChain;\n" );
364 fprintf( outfile, " const char *Name;\n" );
365 fprintf( outfile, " void *FirstThunk;\n" );
366 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
367 fprintf( outfile, " const char *data[%d];\n",
368 total_imports - total_delayed + nb_imm );
369 fprintf( outfile, "} imports = {\n {\n" );
373 for (i = j = 0; i < nb_imports; i++)
375 if (dll_imports[i]->delay) continue;
376 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
377 dll_imports[i]->dll, j );
378 j += dll_imports[i]->nb_imports + 1;
381 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
382 fprintf( outfile, " },\n {\n" );
384 /* list of imported functions */
386 for (i = 0; i < nb_imports; i++)
388 if (dll_imports[i]->delay) continue;
389 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
390 for (j = 0; j < dll_imports[i]->nb_imports; j++)
391 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
392 fprintf( outfile, " 0,\n" );
394 fprintf( outfile, " }\n};\n\n" );
396 /* thunks for imported functions */
398 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
399 pos = 20 * (nb_imm + 1); /* offset of imports.data from start of imports */
400 fprintf( outfile, "asm(\".data\\n\\t.align 8\\n\"\n" );
401 for (i = 0; i < nb_imports; i++)
403 if (dll_imports[i]->delay) continue;
404 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
406 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n",
407 dll_imports[i]->imports[j] );
408 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
409 dll_imports[i]->imports[j] );
414 fprintf( outfile, " \"" PREFIX "%s:\\n\\t", dll_imports[i]->imports[j] );
416 #if defined(__i386__)
417 if (strstr( dll_imports[i]->imports[j], "__wine_call_from_16" ))
418 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
420 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
421 #elif defined(__sparc__)
424 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
425 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
426 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
430 /* Hmpf. Stupid sparc assembler always interprets global variable
431 names as GOT offsets, so we have to do it the long way ... */
432 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
433 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
434 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
435 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
436 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
437 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
440 #error You need to define import thunks for your architecture!
442 fprintf( outfile, "\"\n" );
446 fprintf( outfile, "\".previous\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
452 /* output the delayed import table of a Win32 module */
453 static int output_delayed_imports( FILE *outfile )
457 if (!nb_delayed) goto done;
459 for (i = 0; i < nb_imports; i++)
461 if (!dll_imports[i]->delay) continue;
462 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
463 for (j = 0; j < dll_imports[i]->nb_imports; j++)
465 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n",
466 i, dll_imports[i]->imports[j] );
469 fprintf( outfile, "\n" );
470 fprintf( outfile, "static struct {\n" );
471 fprintf( outfile, " struct ImgDelayDescr {\n" );
472 fprintf( outfile, " unsigned int grAttrs;\n" );
473 fprintf( outfile, " const char *szName;\n" );
474 fprintf( outfile, " void **phmod;\n" );
475 fprintf( outfile, " void **pIAT;\n" );
476 fprintf( outfile, " const char **pINT;\n" );
477 fprintf( outfile, " void* pBoundIAT;\n" );
478 fprintf( outfile, " void* pUnloadIAT;\n" );
479 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
480 fprintf( outfile, " } imp[%d];\n", nb_delayed );
481 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
482 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
483 fprintf( outfile, "} delay_imports = {\n" );
484 fprintf( outfile, " {\n" );
485 for (i = j = 0; i < nb_imports; i++)
487 if (!dll_imports[i]->delay) continue;
488 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
489 dll_imports[i]->dll, i, j, j );
490 j += dll_imports[i]->nb_imports;
492 fprintf( outfile, " },\n {\n" );
493 for (i = 0; i < nb_imports; i++)
495 if (!dll_imports[i]->delay) continue;
496 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
497 for (j = 0; j < dll_imports[i]->nb_imports; j++)
499 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, dll_imports[i]->imports[j] );
502 fprintf( outfile, " },\n {\n" );
503 for (i = 0; i < nb_imports; i++)
505 if (!dll_imports[i]->delay) continue;
506 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
507 for (j = 0; j < dll_imports[i]->nb_imports; j++)
509 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
512 fprintf( outfile, " }\n};\n\n" );
514 /* check if there's some stub defined. if so, exception struct
515 * is already defined, so don't emit it twice
517 for (i = 0; i < nb_entry_points; i++) if (EntryPoints[i]->type == TYPE_STUB) break;
519 if (i == nb_entry_points) {
520 fprintf( outfile, "struct exc_record {\n" );
521 fprintf( outfile, " unsigned int code, flags;\n" );
522 fprintf( outfile, " void *rec, *addr;\n" );
523 fprintf( outfile, " unsigned int params;\n" );
524 fprintf( outfile, " const void *info[15];\n" );
525 fprintf( outfile, "};\n\n" );
526 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
529 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
530 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
531 fprintf( outfile, "\n" );
533 fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
534 fprintf( outfile, "{\n" );
535 fprintf( outfile, " int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
536 fprintf( outfile, " struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
537 fprintf( outfile, " void **pIAT = imd->pIAT + nr;\n" );
538 fprintf( outfile, " const char** pINT = imd->pINT + nr;\n" );
539 fprintf( outfile, " void *fn;\n\n" );
541 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
542 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT + 2)))\n");
543 fprintf( outfile, " /* patch IAT with final value */\n" );
544 fprintf( outfile, " return *pIAT = fn;\n" );
545 fprintf( outfile, " else {\n");
546 fprintf( outfile, " struct exc_record rec;\n" );
547 fprintf( outfile, " rec.code = 0x80000100;\n" );
548 fprintf( outfile, " rec.flags = 1;\n" );
549 fprintf( outfile, " rec.rec = 0;\n" );
550 fprintf( outfile, " rec.params = 2;\n" );
551 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
552 fprintf( outfile, " rec.info[1] = *pINT + 2;\n" );
553 fprintf( outfile, "#ifdef __GNUC__\n" );
554 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
555 fprintf( outfile, "#else\n" );
556 fprintf( outfile, " rec.addr = 0;\n" );
557 fprintf( outfile, "#endif\n" );
558 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
559 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
560 fprintf( outfile, " }\n}\n\n" );
562 fprintf( outfile, "#ifndef __GNUC__\n" );
563 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
564 fprintf( outfile, "#endif\n" );
566 fprintf( outfile, "asm(\".align 8\\n\"\n" );
567 fprintf( outfile, " \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
568 fprintf( outfile, " \"" PREFIX "__wine_delay_load_asm:\\n\"\n" );
569 #if defined(__i386__)
570 fprintf( outfile, " \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
571 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
572 fprintf( outfile, " \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
573 #elif defined(__sparc__)
574 fprintf( outfile, " \"\\tsave %%sp, -96, %%sp\\n\"\n" );
575 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
576 fprintf( outfile, " \"\\tmov %%g1, %%o0\\n\"\n" );
577 fprintf( outfile, " \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
579 #error You need to defined delayed import thunks for your architecture!
582 for (i = idx = 0; i < nb_imports; i++)
584 if (!dll_imports[i]->delay) continue;
585 for (j = 0; j < dll_imports[i]->nb_imports; j++)
588 sprintf( buffer, "__wine_delay_imp_%d_%s", i, dll_imports[i]->imports[j]);
589 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
590 fprintf( outfile, " \"" PREFIX "%s:\\n\"\n", buffer );
591 #if defined(__i386__)
592 fprintf( outfile, " \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
593 fprintf( outfile, " \"\\tjmp __wine_delay_load_asm\\n\"\n" );
594 #elif defined(__sparc__)
595 fprintf( outfile, " \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
596 fprintf( outfile, " \"\\tb,a __wine_delay_load_asm\\n\"\n" );
598 #error You need to defined delayed import thunks for your architecture!
604 fprintf( outfile, "\n \".data\\n\\t.align 8\\n\"\n" );
605 pos = nb_delayed * 32;
606 for (i = 0; i < nb_imports; i++)
608 if (!dll_imports[i]->delay) continue;
609 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
611 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n",
612 dll_imports[i]->imports[j] );
613 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
614 dll_imports[i]->imports[j] );
615 fprintf( outfile, " \"" PREFIX "%s:\\n\\t", dll_imports[i]->imports[j] );
616 #if defined(__i386__)
617 if (strstr( dll_imports[i]->imports[j], "__wine_call_from_16" ))
618 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n", pos );
620 fprintf( outfile, "jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
621 #elif defined(__sparc__)
624 fprintf( outfile, "sethi %%hi(delay_imports+%d), %%g1\\n\\t", pos );
625 fprintf( outfile, "ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t", pos );
626 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
630 /* Hmpf. Stupid sparc assembler always interprets global variable
631 names as GOT offsets, so we have to do it the long way ... */
632 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
633 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
634 fprintf( outfile, "1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t", pos );
635 fprintf( outfile, "or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t", pos );
636 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
637 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
640 #error You need to define import thunks for your architecture!
642 fprintf( outfile, "\"\n" );
645 fprintf( outfile, "\".previous\");\n" );
646 fprintf( outfile, "#ifndef __GNUC__\n" );
647 fprintf( outfile, "}\n" );
648 fprintf( outfile, "#endif\n" );
649 fprintf( outfile, "\n" );
655 /* output the import and delayed import tables of a Win32 module
656 * returns number of DLLs exported in 'immediate' mode
658 int output_imports( FILE *outfile )
660 output_delayed_imports( outfile );
661 return output_immediate_imports( outfile );