blob: 60d2045f85e8bec7e8f54f5bb736f1a442ae8877 [file] [log] [blame]
Alexandre Julliardc585a502000-09-27 23:40:43 +00001/*
2 * DLL imports support
3 *
4 * Copyright 2000 Alexandre Julliard
5 */
6
7#include <fcntl.h>
8#include <stdio.h>
9#include <unistd.h>
10
11#include "winnt.h"
12#include "build.h"
13
14struct import
15{
16 char *dll; /* dll name */
Alexandre Julliard000c13a2000-11-09 20:31:18 +000017 char **exports; /* functions exported from this dll */
18 int nb_exports; /* number of exported functions */
Alexandre Julliardc585a502000-09-27 23:40:43 +000019 char **imports; /* functions we want to import from this dll */
20 int nb_imports; /* number of imported functions */
21};
22
Alexandre Julliard000c13a2000-11-09 20:31:18 +000023static char **undef_symbols; /* list of undefined symbols */
24static int nb_undef_symbols, undef_size;
Alexandre Julliardc585a502000-09-27 23:40:43 +000025
26static struct import **dll_imports = NULL;
27static int nb_imports = 0; /* number of imported dlls */
28static int total_imports = 0; /* total number of imported functions */
29
30
Alexandre Julliard000c13a2000-11-09 20:31:18 +000031/* compare function names; helper for resolve_imports */
32static int name_cmp( const void *name, const void *entry )
33{
34 return strcmp( *(char **)name, *(char **)entry );
35}
36
37/* locate a symbol in a (sorted) list */
38inline static const char *find_symbol( const char *name, char **table, int size )
39{
40 char **res = bsearch( &name, table, size, sizeof(*table), name_cmp );
41 return res ? *res : NULL;
42}
43
44/* sort a symbol table */
45inline static void sort_symbols( char **table, int size )
46{
47 qsort( table, size, sizeof(*table), name_cmp );
48}
49
50/* open the .so library for a given dll in a specified path */
51static char *try_library_path( const char *path, const char *name )
52{
53 char *buffer, *p;
54 int fd;
55
56 buffer = xmalloc( strlen(path) + strlen(name) + 8 );
57 sprintf( buffer, "%s/lib%s", path, name );
58 p = buffer + strlen(buffer) - 4;
59 if (!strcmp( p, ".dll" )) *p = 0;
60 strcat( buffer, ".so" );
61 /* check if the file exists */
62 if ((fd = open( buffer, O_RDONLY )) == -1) return NULL;
63 close( fd );
64 return buffer;
65}
66
67/* open the .so library for a given dll */
68static char *open_library( const char *name )
69{
70 char *fullname;
71 int i;
72
73 for (i = 0; i < nb_lib_paths; i++)
74 {
75 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
76 }
77 if (!(fullname = try_library_path( ".", name )))
78 fatal_error( "could not open .so file for %s\n", name );
79 return fullname;
80}
81
82/* read in the list of exported symbols of a .so */
83static void read_exported_symbols( const char *name, struct import *imp )
84{
85 FILE *f;
86 char buffer[1024];
87 char *fullname, *cmdline;
88 const char *ext;
89 int size, err;
90
91 imp->exports = NULL;
92 imp->nb_exports = size = 0;
93
94 if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
95
96 if (!(fullname = open_library( name ))) return;
97 cmdline = xmalloc( strlen(fullname) + 4 );
98 sprintf( cmdline, "nm %s", fullname );
99 free( fullname );
100
101 if (!(f = popen( cmdline, "r" )))
102 fatal_error( "Cannot execute '%s'\n", cmdline );
103
104 while (fgets( buffer, sizeof(buffer), f ))
105 {
106 char *p = buffer + strlen(buffer) - 1;
107 if (p < buffer) continue;
108 if (*p == '\n') *p-- = 0;
109 if (!(p = strstr( buffer, "__wine_dllexport_" ))) continue;
110 p += 17;
111 if (strncmp( p, name, ext - name )) continue;
112 p += ext - name;
113 if (*p++ != '_') continue;
114
115 if (imp->nb_exports == size)
116 {
117 size += 128;
118 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
119 }
120 imp->exports[imp->nb_exports++] = xstrdup( p );
121 }
122 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
123 free( cmdline );
124 sort_symbols( imp->exports, imp->nb_exports );
125}
126
Alexandre Julliardc585a502000-09-27 23:40:43 +0000127/* add a dll to the list of imports */
128void add_import_dll( const char *name )
129{
130 struct import *imp = xmalloc( sizeof(*imp) );
131 imp->dll = xstrdup( name );
132 imp->imports = NULL;
133 imp->nb_imports = 0;
134
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000135 read_exported_symbols( name, imp );
136
Alexandre Julliardc585a502000-09-27 23:40:43 +0000137 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
138 dll_imports[nb_imports++] = imp;
139}
140
Alexandre Julliardc585a502000-09-27 23:40:43 +0000141/* add a function to the list of imports from a given dll */
142static void add_import_func( struct import *imp, const char *name )
143{
144 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
145 imp->imports[imp->nb_imports++] = xstrdup( name );
146 total_imports++;
147}
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000148
149/* add a symbol to the undef list */
150inline static void add_undef_symbol( const char *name )
151{
152 if (nb_undef_symbols == undef_size)
153 {
154 undef_size += 128;
155 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
156 }
157 undef_symbols[nb_undef_symbols++] = xstrdup( name );
158}
159
160/* add the extra undefined symbols that will be contained in the generated spec file itself */
161static void add_extra_undef_symbols(void)
162{
163 const char *extras[8];
164 int i, count = 0;
165
166#define ADD_SYM(name) \
167 do { if (!find_symbol( extras[count] = (name), undef_symbols, \
168 nb_undef_symbols )) count++; } while(0)
169
170 sort_symbols( undef_symbols, nb_undef_symbols );
171
172 /* add symbols that will be contained in the spec file itself */
173 switch (SpecMode)
174 {
175 case SPEC_MODE_GUIEXE:
176 ADD_SYM( "GetCommandLineA" );
177 ADD_SYM( "GetStartupInfoA" );
178 ADD_SYM( "GetModuleHandleA" );
179 /* fall through */
180 case SPEC_MODE_CUIEXE:
181 ADD_SYM( "__wine_get_main_args" );
182 ADD_SYM( "ExitProcess" );
183 /* fall through */
184 case SPEC_MODE_DLL:
185 case SPEC_MODE_GUIEXE_NO_MAIN:
186 case SPEC_MODE_CUIEXE_NO_MAIN:
187 ADD_SYM( "RtlRaiseException" );
188 break;
189 }
190 if (count)
191 {
192 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
193 sort_symbols( undef_symbols, nb_undef_symbols );
194 }
195}
196
197/* read in the list of undefined symbols */
198void read_undef_symbols( const char *name )
199{
200 FILE *f;
201 char buffer[1024];
202 int err;
203
204 undef_size = nb_undef_symbols = 0;
205
206 sprintf( buffer, "nm -u %s", name );
207 if (!(f = popen( buffer, "r" )))
208 fatal_error( "Cannot execute '%s'\n", buffer );
209
210 while (fgets( buffer, sizeof(buffer), f ))
211 {
212 char *p = buffer + strlen(buffer) - 1;
213 if (p < buffer) continue;
214 if (*p == '\n') *p-- = 0;
215 add_undef_symbol( buffer );
216 }
217 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
218}
219
220/* resolve the imports for a Win32 module */
221int resolve_imports( FILE *outfile )
222{
223 int i, j, off;
224 char **p;
225
226 if (!nb_undef_symbols) return 0; /* no symbol file specified */
227
228 add_extra_undef_symbols();
229
230 for (i = 0; i < nb_imports; i++)
231 {
232 struct import *imp = dll_imports[i];
233
234 for (j = 0; j < nb_undef_symbols; j++)
235 {
236 const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
237 if (res)
238 {
239 add_import_func( imp, res );
240 undef_symbols[j] = NULL;
241 }
242 }
243 /* remove all the holes in the undef symbols list */
244 p = undef_symbols;
245 for (j = off = 0; j < nb_undef_symbols; j++)
246 {
247 if (!undef_symbols[j]) off++;
248 else undef_symbols[j - off] = undef_symbols[j];
249 }
250 nb_undef_symbols -= off;
251 if (!off) warning( "%s imported but no symbols used\n", imp->dll );
252 }
253 return 1;
254}
Alexandre Julliardc585a502000-09-27 23:40:43 +0000255
256/* output the import table of a Win32 module */
257int output_imports( FILE *outfile )
258{
259 int i, j, pos;
260
261 if (!nb_imports) goto done;
262
263 /* main import header */
264
265 fprintf( outfile, "\n\n/* imports */\n\n" );
266 fprintf( outfile, "static struct {\n" );
267 fprintf( outfile, " struct {\n" );
268 fprintf( outfile, " void *OriginalFirstThunk;\n" );
269 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
270 fprintf( outfile, " unsigned int ForwarderChain;\n" );
271 fprintf( outfile, " const char *Name;\n" );
272 fprintf( outfile, " void *FirstThunk;\n" );
273 fprintf( outfile, " } imp[%d];\n", nb_imports+1 );
274 fprintf( outfile, " const char *data[%d];\n", total_imports + nb_imports );
275 fprintf( outfile, "} imports = {\n {\n" );
276
277 /* list of dlls */
278
279 for (i = j = 0; i < nb_imports; i++)
280 {
281 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
282 dll_imports[i]->dll, j );
283 j += dll_imports[i]->nb_imports + 1;
284 }
285 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
286 fprintf( outfile, " },\n {\n" );
287
288 /* list of imported functions */
289
290 for (i = 0; i < nb_imports; i++)
291 {
292 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
293 for (j = 0; j < dll_imports[i]->nb_imports; j++)
294 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
295 fprintf( outfile, " 0,\n" );
296 }
297 fprintf( outfile, " }\n};\n\n" );
298
299 /* thunks for imported functions */
300
301 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
302 pos = 20 * (nb_imports + 1); /* offset of imports.data from start of imports */
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000303 fprintf( outfile, "asm(\".align 4\\n\"\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000304 for (i = 0; i < nb_imports; i++, pos += 4)
305 {
306 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
307 {
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000308 fprintf( outfile, " \"\\t.type " PREFIX "%s,@function\\n\"\n",
309 dll_imports[i]->imports[j] );
310 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
311 dll_imports[i]->imports[j] );
312 fprintf( outfile, " \"" PREFIX "%s:\\tjmp *(imports+%d)\\n\"\n",
Alexandre Julliardc585a502000-09-27 23:40:43 +0000313 dll_imports[i]->imports[j], pos );
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000314 fprintf( outfile, " \"\\tmovl %%esi,%%esi\\n\"\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000315 }
316 }
Alexandre Julliard000c13a2000-11-09 20:31:18 +0000317 fprintf( outfile, ");\n#ifndef __GNUC__\n}\n#endif\n\n" );
Alexandre Julliardc585a502000-09-27 23:40:43 +0000318
319 done:
320 return nb_imports;
321}