blob: 9231892d4068903bd407503ecf8f75ad8eb2424b [file] [log] [blame]
Alexandre Julliardc585a502000-09-27 23:40:43 +00001/*
2 * DLL imports support
3 *
4 * Copyright 2000 Alexandre Julliard
Eric Pouech5e32d162000-12-26 01:22:34 +00005 * 2000 Eric Pouech
Alexandre Julliardc585a502000-09-27 23:40:43 +00006 */
7
8#include <fcntl.h>
9#include <stdio.h>
10#include <unistd.h>
11
Dimitrie O. Paun31b6d092000-12-01 21:27:43 +000012#include "config.h"
Alexandre Julliardc585a502000-09-27 23:40:43 +000013#include "winnt.h"
14#include "build.h"
15
16struct import
17{
18 char *dll; /* dll name */
Eric Pouech5e32d162000-12-26 01:22:34 +000019 int delay; /* delay or not dll loading ? */
Alexandre Julliard000c13a2000-11-09 20:31:18 +000020 char **exports; /* functions exported from this dll */
21 int nb_exports; /* number of exported functions */
Alexandre Julliardc585a502000-09-27 23:40:43 +000022 char **imports; /* functions we want to import from this dll */
23 int nb_imports; /* number of imported functions */
Eric Pouech03350952000-12-06 03:32:26 +000024 int lineno; /* line in .spec file where import is defined */
Alexandre Julliardc585a502000-09-27 23:40:43 +000025};
26
Alexandre Julliard000c13a2000-11-09 20:31:18 +000027static char **undef_symbols; /* list of undefined symbols */
Alexandre Julliard0a8114c2000-11-12 03:45:55 +000028static int nb_undef_symbols = -1;
29static int undef_size;
Alexandre Julliardc585a502000-09-27 23:40:43 +000030
Jon Griffiths4f12e612000-12-14 22:18:22 +000031static char **ignore_symbols; /* list of symbols to ignore */
32static int nb_ignore_symbols;
33static int ignore_size;
34
Alexandre Julliardc585a502000-09-27 23:40:43 +000035static struct import **dll_imports = NULL;
Eric Pouech5e32d162000-12-26 01:22:34 +000036static int nb_imports = 0; /* number of imported dlls (delayed or not) */
37static int nb_delayed = 0; /* number of delayed dlls */
38static int total_imports = 0; /* total number of imported functions */
39static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
Alexandre Julliardc585a502000-09-27 23:40:43 +000040
Alexandre Julliard000c13a2000-11-09 20:31:18 +000041/* compare function names; helper for resolve_imports */
42static int name_cmp( const void *name, const void *entry )
43{
44 return strcmp( *(char **)name, *(char **)entry );
45}
46
47/* locate a symbol in a (sorted) list */
48inline static const char *find_symbol( const char *name, char **table, int size )
49{
Patrik Stridvall35d288b2000-12-18 03:13:52 +000050 char **res = NULL;
51
52 if (table) {
53 res = bsearch( &name, table, size, sizeof(*table), name_cmp );
54 }
55
Alexandre Julliard000c13a2000-11-09 20:31:18 +000056 return res ? *res : NULL;
57}
58
59/* sort a symbol table */
60inline static void sort_symbols( char **table, int size )
61{
Ulrich Weigand0108d832000-12-29 05:17:33 +000062 if (table )
63 qsort( table, size, sizeof(*table), name_cmp );
Alexandre Julliard000c13a2000-11-09 20:31:18 +000064}
65
66/* open the .so library for a given dll in a specified path */
67static char *try_library_path( const char *path, const char *name )
68{
69 char *buffer, *p;
70 int fd;
71
72 buffer = xmalloc( strlen(path) + strlen(name) + 8 );
73 sprintf( buffer, "%s/lib%s", path, name );
74 p = buffer + strlen(buffer) - 4;
75 if (!strcmp( p, ".dll" )) *p = 0;
76 strcat( buffer, ".so" );
77 /* check if the file exists */
78 if ((fd = open( buffer, O_RDONLY )) == -1) return NULL;
79 close( fd );
80 return buffer;
81}
82
83/* open the .so library for a given dll */
84static char *open_library( const char *name )
85{
86 char *fullname;
87 int i;
88
89 for (i = 0; i < nb_lib_paths; i++)
90 {
91 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
92 }
93 if (!(fullname = try_library_path( ".", name )))
94 fatal_error( "could not open .so file for %s\n", name );
95 return fullname;
96}
97
98/* read in the list of exported symbols of a .so */
99static void read_exported_symbols( const char *name, struct import *imp )
100{
101 FILE *f;
102 char buffer[1024];
103 char *fullname, *cmdline;
104 const char *ext;
105 int size, err;
106
107 imp->exports = NULL;
108 imp->nb_exports = size = 0;
109
110 if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
111
112 if (!(fullname = open_library( name ))) return;
113 cmdline = xmalloc( strlen(fullname) + 4 );
114 sprintf( cmdline, "nm %s", fullname );
115 free( fullname );
116
117 if (!(f = popen( cmdline, "r" )))
118 fatal_error( "Cannot execute '%s'\n", cmdline );
119
120 while (fgets( buffer, sizeof(buffer), f ))
121 {
122 char *p = buffer + strlen(buffer) - 1;
123 if (p < buffer) continue;
124 if (*p == '\n') *p-- = 0;
125 if (!(p = strstr( buffer, "__wine_dllexport_" ))) continue;
126 p += 17;
127 if (strncmp( p, name, ext - name )) continue;
128 p += ext - name;
129 if (*p++ != '_') continue;
130
131 if (imp->nb_exports == size)
132 {
133 size += 128;
134 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
135 }
136 imp->exports[imp->nb_exports++] = xstrdup( p );
137 }
138 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
139 free( cmdline );
140 sort_symbols( imp->exports, imp->nb_exports );
141}
142
Alexandre Julliardc585a502000-09-27 23:40:43 +0000143/* add a dll to the list of imports */
Eric Pouech5e32d162000-12-26 01:22:34 +0000144void add_import_dll( const char *name, int delay )
Alexandre Julliardc585a502000-09-27 23:40:43 +0000145{
146 struct import *imp = xmalloc( sizeof(*imp) );
147 imp->dll = xstrdup( name );
Eric Pouech5e32d162000-12-26 01:22:34 +0000148 imp->delay = delay;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000149 imp->imports = NULL;
150 imp->nb_imports = 0;
Eric Pouech03350952000-12-06 03:32:26 +0000151 /* GetToken for the file name has swallowed the '\n', hence pointing to next line */
152 imp->lineno = current_line - 1;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000153
Eric Pouech5e32d162000-12-26 01:22:34 +0000154 if (delay) nb_delayed++;
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000155 read_exported_symbols( name, imp );
156
Alexandre Julliardc585a502000-09-27 23:40:43 +0000157 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
158 dll_imports[nb_imports++] = imp;
159}
160
Jon Griffiths4f12e612000-12-14 22:18:22 +0000161/* Add a symbol to the ignored symbol list */
162void add_ignore_symbol( const char *name )
163{
164 if (nb_ignore_symbols == ignore_size)
165 {
166 ignore_size += 32;
167 ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
168 }
169 ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
170}
171
Alexandre Julliardc585a502000-09-27 23:40:43 +0000172/* add a function to the list of imports from a given dll */
173static void add_import_func( struct import *imp, const char *name )
174{
175 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
176 imp->imports[imp->nb_imports++] = xstrdup( name );
177 total_imports++;
Eric Pouech5e32d162000-12-26 01:22:34 +0000178 if (imp->delay) total_delayed++;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000179}
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000180
181/* add a symbol to the undef list */
182inline static void add_undef_symbol( const char *name )
183{
184 if (nb_undef_symbols == undef_size)
185 {
186 undef_size += 128;
187 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
188 }
189 undef_symbols[nb_undef_symbols++] = xstrdup( name );
190}
191
Jon Griffiths4f12e612000-12-14 22:18:22 +0000192/* remove all the holes in the undefined symbol list; return the number of removed symbols */
193static int remove_symbol_holes(void)
194{
195 int i, off;
196 for (i = off = 0; i < nb_undef_symbols; i++)
197 {
198 if (!undef_symbols[i]) off++;
199 else undef_symbols[i - off] = undef_symbols[i];
200 }
201 nb_undef_symbols -= off;
202 return off;
203}
204
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000205/* add the extra undefined symbols that will be contained in the generated spec file itself */
206static void add_extra_undef_symbols(void)
207{
208 const char *extras[8];
209 int i, count = 0;
210
211#define ADD_SYM(name) \
212 do { if (!find_symbol( extras[count] = (name), undef_symbols, \
213 nb_undef_symbols )) count++; } while(0)
214
215 sort_symbols( undef_symbols, nb_undef_symbols );
216
217 /* add symbols that will be contained in the spec file itself */
218 switch (SpecMode)
219 {
Alexandre Julliard909eff92000-12-15 03:38:11 +0000220 case SPEC_MODE_DLL:
221 break;
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000222 case SPEC_MODE_GUIEXE:
223 ADD_SYM( "GetCommandLineA" );
224 ADD_SYM( "GetStartupInfoA" );
225 ADD_SYM( "GetModuleHandleA" );
226 /* fall through */
227 case SPEC_MODE_CUIEXE:
228 ADD_SYM( "__wine_get_main_args" );
229 ADD_SYM( "ExitProcess" );
Alexandre Julliard909eff92000-12-15 03:38:11 +0000230 break;
231 case SPEC_MODE_GUIEXE_UNICODE:
232 ADD_SYM( "GetCommandLineA" );
233 ADD_SYM( "GetStartupInfoA" );
234 ADD_SYM( "GetModuleHandleA" );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000235 /* fall through */
Alexandre Julliard909eff92000-12-15 03:38:11 +0000236 case SPEC_MODE_CUIEXE_UNICODE:
237 ADD_SYM( "__wine_get_wmain_args" );
238 ADD_SYM( "ExitProcess" );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000239 break;
240 }
Alexandre Julliard909eff92000-12-15 03:38:11 +0000241 ADD_SYM( "RtlRaiseException" );
Eric Pouech5e32d162000-12-26 01:22:34 +0000242 if (nb_delayed)
243 {
244 ADD_SYM( "LoadLibraryA" );
245 ADD_SYM( "GetProcAddress" );
246 }
Alexandre Julliard909eff92000-12-15 03:38:11 +0000247
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000248 if (count)
249 {
250 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
251 sort_symbols( undef_symbols, nb_undef_symbols );
252 }
253}
254
Alexandre Julliard3570bfd2000-11-13 04:46:34 +0000255/* warn if a given dll is not used, but check forwards first */
Eric Pouech03350952000-12-06 03:32:26 +0000256static void warn_unused( const struct import* imp )
Alexandre Julliard3570bfd2000-11-13 04:46:34 +0000257{
Eric Pouech03350952000-12-06 03:32:26 +0000258 int i, curline;
259 size_t len = strlen(imp->dll);
260 const char *p = strchr( imp->dll, '.' );
261 if (p && !strcasecmp( p, ".dll" )) len = p - imp->dll;
Alexandre Julliard3570bfd2000-11-13 04:46:34 +0000262
263 for (i = Base; i <= Limit; i++)
264 {
265 ORDDEF *odp = Ordinals[i];
266 if (!odp || odp->type != TYPE_FORWARD) continue;
Alexandre Julliard66fed8c2000-12-15 23:04:40 +0000267 if (!strncasecmp( odp->link_name, imp->dll, len ) &&
268 odp->link_name[len] == '.')
Alexandre Julliard3570bfd2000-11-13 04:46:34 +0000269 return; /* found an import, do not warn */
270 }
Eric Pouech03350952000-12-06 03:32:26 +0000271 /* switch current_line temporarily to the line of the import declaration */
272 curline = current_line;
273 current_line = imp->lineno;
274 warning( "%s imported but no symbols used\n", imp->dll );
275 current_line = curline;
Alexandre Julliard3570bfd2000-11-13 04:46:34 +0000276}
277
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000278/* read in the list of undefined symbols */
279void read_undef_symbols( const char *name )
280{
281 FILE *f;
282 char buffer[1024];
283 int err;
284
285 undef_size = nb_undef_symbols = 0;
286
287 sprintf( buffer, "nm -u %s", name );
288 if (!(f = popen( buffer, "r" )))
289 fatal_error( "Cannot execute '%s'\n", buffer );
290
291 while (fgets( buffer, sizeof(buffer), f ))
292 {
293 char *p = buffer + strlen(buffer) - 1;
294 if (p < buffer) continue;
295 if (*p == '\n') *p-- = 0;
Ulrich Weigand0108d832000-12-29 05:17:33 +0000296 p = buffer; while (*p == ' ') p++;
297 add_undef_symbol( p );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000298 }
299 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
300}
301
Jon Griffiths4f12e612000-12-14 22:18:22 +0000302static void remove_ignored_symbols(void)
303{
304 int i;
305
306 sort_symbols( ignore_symbols, nb_ignore_symbols );
307 for (i = 0; i < nb_undef_symbols; i++)
308 {
309 if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
310 {
311 free( undef_symbols[i] );
312 undef_symbols[i] = NULL;
313 }
314 }
315 remove_symbol_holes();
316}
317
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000318/* resolve the imports for a Win32 module */
319int resolve_imports( FILE *outfile )
320{
Jon Griffiths4f12e612000-12-14 22:18:22 +0000321 int i, j;
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000322
Alexandre Julliard0a8114c2000-11-12 03:45:55 +0000323 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000324
325 add_extra_undef_symbols();
Jon Griffiths4f12e612000-12-14 22:18:22 +0000326 remove_ignored_symbols();
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000327
328 for (i = 0; i < nb_imports; i++)
329 {
330 struct import *imp = dll_imports[i];
331
332 for (j = 0; j < nb_undef_symbols; j++)
333 {
334 const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
335 if (res)
336 {
337 add_import_func( imp, res );
Jon Griffiths4f12e612000-12-14 22:18:22 +0000338 free( undef_symbols[j] );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000339 undef_symbols[j] = NULL;
340 }
341 }
342 /* remove all the holes in the undef symbols list */
Jon Griffiths4f12e612000-12-14 22:18:22 +0000343 if (!remove_symbol_holes()) warn_unused( imp );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000344 }
345 return 1;
346}
Alexandre Julliardc585a502000-09-27 23:40:43 +0000347
348/* output the import table of a Win32 module */
Eric Pouech5e32d162000-12-26 01:22:34 +0000349static int output_immediate_imports( FILE *outfile )
Alexandre Julliardc585a502000-09-27 23:40:43 +0000350{
351 int i, j, pos;
Eric Pouech5e32d162000-12-26 01:22:34 +0000352 int nb_imm = nb_imports - nb_delayed;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000353
Eric Pouech5e32d162000-12-26 01:22:34 +0000354 if (!nb_imm) goto done;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000355
356 /* main import header */
357
Marcus Meissnerc9ea5f12000-11-26 04:02:09 +0000358 fprintf( outfile, "\nstatic struct {\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000359 fprintf( outfile, " struct {\n" );
360 fprintf( outfile, " void *OriginalFirstThunk;\n" );
361 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
362 fprintf( outfile, " unsigned int ForwarderChain;\n" );
363 fprintf( outfile, " const char *Name;\n" );
364 fprintf( outfile, " void *FirstThunk;\n" );
Eric Pouech5e32d162000-12-26 01:22:34 +0000365 fprintf( outfile, " } imp[%d];\n", nb_imm+1 );
366 fprintf( outfile, " const char *data[%d];\n",
367 total_imports - total_delayed + nb_imm );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000368 fprintf( outfile, "} imports = {\n {\n" );
369
370 /* list of dlls */
371
372 for (i = j = 0; i < nb_imports; i++)
373 {
Eric Pouech5e32d162000-12-26 01:22:34 +0000374 if (dll_imports[i]->delay) continue;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000375 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
376 dll_imports[i]->dll, j );
377 j += dll_imports[i]->nb_imports + 1;
378 }
Eric Pouech5e32d162000-12-26 01:22:34 +0000379
Alexandre Julliardc585a502000-09-27 23:40:43 +0000380 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
381 fprintf( outfile, " },\n {\n" );
382
383 /* list of imported functions */
384
385 for (i = 0; i < nb_imports; i++)
386 {
Eric Pouech5e32d162000-12-26 01:22:34 +0000387 if (dll_imports[i]->delay) continue;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000388 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
389 for (j = 0; j < dll_imports[i]->nb_imports; j++)
390 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
391 fprintf( outfile, " 0,\n" );
392 }
393 fprintf( outfile, " }\n};\n\n" );
394
395 /* thunks for imported functions */
396
397 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
Eric Pouech5e32d162000-12-26 01:22:34 +0000398 pos = 20 * (nb_imm + 1); /* offset of imports.data from start of imports */
Ulrich Weigand0108d832000-12-29 05:17:33 +0000399 fprintf( outfile, "asm(\".data\\n\\t.align 8\\n\"\n" );
Eric Pouech5e32d162000-12-26 01:22:34 +0000400 for (i = 0; i < nb_imports; i++)
Alexandre Julliardc585a502000-09-27 23:40:43 +0000401 {
Eric Pouech5e32d162000-12-26 01:22:34 +0000402 if (dll_imports[i]->delay) continue;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000403 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
404 {
Dimitrie O. Paun31b6d092000-12-01 21:27:43 +0000405 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n",
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000406 dll_imports[i]->imports[j] );
407 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
408 dll_imports[i]->imports[j] );
Ulrich Weigand0108d832000-12-29 05:17:33 +0000409
410
411
412
413 fprintf( outfile, " \"" PREFIX "%s:\\n\\t", dll_imports[i]->imports[j] );
414
415#if defined(__i386__)
Marcus Meissnerc9ea5f12000-11-26 04:02:09 +0000416 if (strstr( dll_imports[i]->imports[j], "__wine_call_from_16" ))
Ulrich Weigand0108d832000-12-29 05:17:33 +0000417 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n", pos );
Marcus Meissnerc9ea5f12000-11-26 04:02:09 +0000418 else
Ulrich Weigand0108d832000-12-29 05:17:33 +0000419 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
420#elif defined(__sparc__)
421 if ( !UsePIC )
422 {
423 fprintf( outfile, "sethi %%hi(imports+%d), %%g1\\n\\t", pos );
424 fprintf( outfile, "ld [%%g1+%%lo(imports+%d)], %%g1\\n\\t", pos );
425 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
426 }
427 else
428 {
429 /* Hmpf. Stupid sparc assembler always interprets global variable
430 names as GOT offsets, so we have to do it the long way ... */
431 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
432 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
433 fprintf( outfile, "1:\\tsethi %%hi(imports+%d-0b), %%g1\\n\\t", pos );
434 fprintf( outfile, "or %%g1, %%lo(imports+%d-0b), %%g1\\n\\t", pos );
435 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
436 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
437 }
438#else
439#error You need to define import thunks for your architecture!
440#endif
441 fprintf( outfile, "\"\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000442 }
Ulrich Weigand775fc632000-12-29 17:44:40 +0000443 pos += 4;
Alexandre Julliardc585a502000-09-27 23:40:43 +0000444 }
Ulrich Weigand0108d832000-12-29 05:17:33 +0000445 fprintf( outfile, "\".previous\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000446
447 done:
Eric Pouech5e32d162000-12-26 01:22:34 +0000448 return nb_imm;
449}
450
451/* output the delayed import table of a Win32 module */
452static int output_delayed_imports( FILE *outfile )
453{
454 int i, idx, j, pos;
455
456 if (!nb_delayed) goto done;
457
458 for (i = 0; i < nb_imports; i++)
459 {
460 if (!dll_imports[i]->delay) continue;
461 fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
462 for (j = 0; j < dll_imports[i]->nb_imports; j++)
463 {
464 fprintf( outfile, "void __wine_delay_imp_%d_%s();\n",
465 i, dll_imports[i]->imports[j] );
466 }
467 }
468 fprintf( outfile, "\n" );
469 fprintf( outfile, "static struct {\n" );
470 fprintf( outfile, " struct ImgDelayDescr {\n" );
471 fprintf( outfile, " unsigned int grAttrs;\n" );
472 fprintf( outfile, " const char *szName;\n" );
473 fprintf( outfile, " void **phmod;\n" );
474 fprintf( outfile, " void **pIAT;\n" );
475 fprintf( outfile, " const char **pINT;\n" );
476 fprintf( outfile, " void* pBoundIAT;\n" );
477 fprintf( outfile, " void* pUnloadIAT;\n" );
478 fprintf( outfile, " unsigned long dwTimeStamp;\n" );
479 fprintf( outfile, " } imp[%d];\n", nb_delayed );
480 fprintf( outfile, " void *IAT[%d];\n", total_delayed );
481 fprintf( outfile, " const char *INT[%d];\n", total_delayed );
482 fprintf( outfile, "} delay_imports = {\n" );
483 fprintf( outfile, " {\n" );
484 for (i = j = 0; i < nb_imports; i++)
485 {
486 if (!dll_imports[i]->delay) continue;
487 fprintf( outfile, " { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
488 dll_imports[i]->dll, i, j, j );
489 j += dll_imports[i]->nb_imports;
490 }
491 fprintf( outfile, " },\n {\n" );
492 for (i = 0; i < nb_imports; i++)
493 {
494 if (!dll_imports[i]->delay) continue;
495 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
496 for (j = 0; j < dll_imports[i]->nb_imports; j++)
497 {
498 fprintf( outfile, " &__wine_delay_imp_%d_%s,\n", i, dll_imports[i]->imports[j] );
499 }
500 }
501 fprintf( outfile, " },\n {\n" );
502 for (i = 0; i < nb_imports; i++)
503 {
504 if (!dll_imports[i]->delay) continue;
505 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
506 for (j = 0; j < dll_imports[i]->nb_imports; j++)
507 {
508 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
509 }
510 }
511 fprintf( outfile, " }\n};\n\n" );
512
513 /* check if there's some stub defined. if so, exception struct
514 * is already defined, so don't emit it twice
515 */
516 for (i = 0; i < nb_entry_points; i++) if (EntryPoints[i]->type == TYPE_STUB) break;
517
518 if (i == nb_entry_points) {
519 fprintf( outfile, "struct exc_record {\n" );
520 fprintf( outfile, " unsigned int code, flags;\n" );
521 fprintf( outfile, " void *rec, *addr;\n" );
522 fprintf( outfile, " unsigned int params;\n" );
523 fprintf( outfile, " const void *info[15];\n" );
524 fprintf( outfile, "};\n\n" );
525 fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
526 }
527
528 fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
529 fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
530 fprintf( outfile, "\n" );
531
532 fprintf( outfile, "void *__stdcall __wine_delay_load(struct ImgDelayDescr* imd, void **pIAT)\n" );
533 fprintf( outfile, "{\n" );
534 fprintf( outfile, " int idx = pIAT - imd->pIAT;\n" );
535 fprintf( outfile, " const char** pINT = imd->pINT + idx;\n" );
536 fprintf( outfile, " void *fn;\n\n" );
537
538 fprintf( outfile, " if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
539 fprintf( outfile, " if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT + 2)))\n");
540 fprintf( outfile, " /* patch IAT with final value */\n" );
541 fprintf( outfile, " return *pIAT = fn;\n" );
542 fprintf( outfile, " else {\n");
543 fprintf( outfile, " struct exc_record rec;\n" );
544 fprintf( outfile, " rec.code = 0x80000100;\n" );
545 fprintf( outfile, " rec.flags = 1;\n" );
546 fprintf( outfile, " rec.rec = 0;\n" );
547 fprintf( outfile, " rec.params = 2;\n" );
548 fprintf( outfile, " rec.info[0] = imd->szName;\n" );
549 fprintf( outfile, " rec.info[1] = *pINT + 2;\n" );
550 fprintf( outfile, "#ifdef __GNUC__\n" );
551 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
552 fprintf( outfile, "#else\n" );
553 fprintf( outfile, " rec.addr = 0;\n" );
554 fprintf( outfile, "#endif\n" );
555 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n" );
556 fprintf( outfile, " return 0; /* shouldn't go here */\n" );
557 fprintf( outfile, " }\n}\n\n" );
558
559 fprintf( outfile, "#ifndef __GNUC__\n" );
560 fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
561 fprintf( outfile, "#endif\n" );
562 fprintf( outfile, "asm(\".align 8\\n\"\n" );
563 pos = nb_delayed * 32;
564 for (i = idx = 0; i < nb_imports; i++)
565 {
566 char buffer[128];
567
568 if (!dll_imports[i]->delay) continue;
569
570 sprintf(buffer, "__wine_dl_%d", i);
571 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
572 fprintf( outfile, " \"" PREFIX "%s:\\t", buffer );
573 fprintf( outfile, "push $delay_imports+%d\\n\"\n", idx * 32 );
574 fprintf( outfile, " \"\\tcall __wine_delay_load\\n\"\n" );
575 fprintf( outfile, " \"\\tpop %%edx\\n\\tpop %%ecx\\n\\tjmp *%%eax\\n\"\n" );
576
577 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
578 {
579 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n",
580 dll_imports[i]->imports[j] );
581 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
582 dll_imports[i]->imports[j] );
583 fprintf( outfile, " \"" PREFIX "%s:\\t", dll_imports[i]->imports[j] );
584 fprintf( outfile, "jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n\"\n", pos );
585
586 sprintf( buffer, "__wine_delay_imp_%d_%s", i, dll_imports[i]->imports[j]);
587 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
588 fprintf( outfile, " \"" PREFIX "%s:\\t", buffer );
589 fprintf( outfile, "push %%ecx\\n\\tpush %%edx\\n\"\n" );
590 fprintf( outfile, " \"\\tpush $delay_imports+%d\\n\"\n", pos );
591 fprintf( outfile, " \"\\tjmp __wine_dl_%d\\n\"\n", i );
592 }
593 idx++;
594 }
595
596 fprintf( outfile, ");\n" );
597 fprintf( outfile, "#ifndef __GNUC__\n" );
598 fprintf( outfile, "}\n" );
599 fprintf( outfile, "#endif\n" );
600 fprintf( outfile, "\n" );
601
602 done:
603 return nb_delayed;
604}
605
606/* output the import and delayed import tables of a Win32 module
607 * returns number of DLLs exported in 'immediate' mode
608 */
609int output_imports( FILE *outfile )
610{
611 output_delayed_imports( outfile );
612 return output_immediate_imports( outfile );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000613}