Modified winebuild to use the __ASM_FUNC macro for greater portability.
[wine/multimedia.git] / tools / winebuild / import.c
blobbbe37b1ba7d8e7414996ce0327f91cbc0244aeaa
1 /*
2 * DLL imports support
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <unistd.h>
11 #include "config.h"
12 #include "winnt.h"
13 #include "build.h"
15 struct import
17 char *dll; /* dll name */
18 char **exports; /* functions exported from this dll */
19 int nb_exports; /* number of exported functions */
20 char **imports; /* functions we want to import from this dll */
21 int nb_imports; /* number of imported functions */
24 static char **undef_symbols; /* list of undefined symbols */
25 static int nb_undef_symbols = -1;
26 static int undef_size;
28 static struct import **dll_imports = NULL;
29 static int nb_imports = 0; /* number of imported dlls */
30 static int total_imports = 0; /* total number of imported functions */
33 /* compare function names; helper for resolve_imports */
34 static int name_cmp( const void *name, const void *entry )
36 return strcmp( *(char **)name, *(char **)entry );
39 /* locate a symbol in a (sorted) list */
40 inline static const char *find_symbol( const char *name, char **table, int size )
42 char **res = bsearch( &name, table, size, sizeof(*table), name_cmp );
43 return res ? *res : NULL;
46 /* sort a symbol table */
47 inline static void sort_symbols( char **table, int size )
49 qsort( table, size, sizeof(*table), name_cmp );
52 /* open the .so library for a given dll in a specified path */
53 static char *try_library_path( const char *path, const char *name )
55 char *buffer, *p;
56 int fd;
58 buffer = xmalloc( strlen(path) + strlen(name) + 8 );
59 sprintf( buffer, "%s/lib%s", path, name );
60 p = buffer + strlen(buffer) - 4;
61 if (!strcmp( p, ".dll" )) *p = 0;
62 strcat( buffer, ".so" );
63 /* check if the file exists */
64 if ((fd = open( buffer, O_RDONLY )) == -1) return NULL;
65 close( fd );
66 return buffer;
69 /* open the .so library for a given dll */
70 static char *open_library( const char *name )
72 char *fullname;
73 int i;
75 for (i = 0; i < nb_lib_paths; i++)
77 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
79 if (!(fullname = try_library_path( ".", name )))
80 fatal_error( "could not open .so file for %s\n", name );
81 return fullname;
84 /* read in the list of exported symbols of a .so */
85 static void read_exported_symbols( const char *name, struct import *imp )
87 FILE *f;
88 char buffer[1024];
89 char *fullname, *cmdline;
90 const char *ext;
91 int size, err;
93 imp->exports = NULL;
94 imp->nb_exports = size = 0;
96 if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
98 if (!(fullname = open_library( name ))) return;
99 cmdline = xmalloc( strlen(fullname) + 4 );
100 sprintf( cmdline, "nm %s", fullname );
101 free( fullname );
103 if (!(f = popen( cmdline, "r" )))
104 fatal_error( "Cannot execute '%s'\n", cmdline );
106 while (fgets( buffer, sizeof(buffer), f ))
108 char *p = buffer + strlen(buffer) - 1;
109 if (p < buffer) continue;
110 if (*p == '\n') *p-- = 0;
111 if (!(p = strstr( buffer, "__wine_dllexport_" ))) continue;
112 p += 17;
113 if (strncmp( p, name, ext - name )) continue;
114 p += ext - name;
115 if (*p++ != '_') continue;
117 if (imp->nb_exports == size)
119 size += 128;
120 imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
122 imp->exports[imp->nb_exports++] = xstrdup( p );
124 if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
125 free( cmdline );
126 sort_symbols( imp->exports, imp->nb_exports );
129 /* add a dll to the list of imports */
130 void add_import_dll( const char *name )
132 struct import *imp = xmalloc( sizeof(*imp) );
133 imp->dll = xstrdup( name );
134 imp->imports = NULL;
135 imp->nb_imports = 0;
137 read_exported_symbols( name, imp );
139 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
140 dll_imports[nb_imports++] = imp;
143 /* add a function to the list of imports from a given dll */
144 static void add_import_func( struct import *imp, const char *name )
146 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
147 imp->imports[imp->nb_imports++] = xstrdup( name );
148 total_imports++;
151 /* add a symbol to the undef list */
152 inline static void add_undef_symbol( const char *name )
154 if (nb_undef_symbols == undef_size)
156 undef_size += 128;
157 undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
159 undef_symbols[nb_undef_symbols++] = xstrdup( name );
162 /* add the extra undefined symbols that will be contained in the generated spec file itself */
163 static void add_extra_undef_symbols(void)
165 const char *extras[8];
166 int i, count = 0;
168 #define ADD_SYM(name) \
169 do { if (!find_symbol( extras[count] = (name), undef_symbols, \
170 nb_undef_symbols )) count++; } while(0)
172 sort_symbols( undef_symbols, nb_undef_symbols );
174 /* add symbols that will be contained in the spec file itself */
175 switch (SpecMode)
177 case SPEC_MODE_GUIEXE:
178 ADD_SYM( "GetCommandLineA" );
179 ADD_SYM( "GetStartupInfoA" );
180 ADD_SYM( "GetModuleHandleA" );
181 /* fall through */
182 case SPEC_MODE_CUIEXE:
183 ADD_SYM( "__wine_get_main_args" );
184 ADD_SYM( "ExitProcess" );
185 /* fall through */
186 case SPEC_MODE_DLL:
187 case SPEC_MODE_GUIEXE_NO_MAIN:
188 case SPEC_MODE_CUIEXE_NO_MAIN:
189 ADD_SYM( "RtlRaiseException" );
190 break;
192 if (count)
194 for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
195 sort_symbols( undef_symbols, nb_undef_symbols );
199 /* warn if a given dll is not used, but check forwards first */
200 static void warn_unused( const char *dllname )
202 int i;
203 size_t len = strlen(dllname);
204 const char *p = strchr( dllname, '.' );
205 if (p && !strcasecmp( p, ".dll" )) len = p - dllname;
207 for (i = Base; i <= Limit; i++)
209 ORDDEF *odp = Ordinals[i];
210 if (!odp || odp->type != TYPE_FORWARD) continue;
211 if (!strncasecmp( odp->u.fwd.link_name, dllname, len ) &&
212 odp->u.fwd.link_name[len] == '.')
213 return; /* found an import, do not warn */
215 warning( "%s imported but no symbols used\n", dllname );
218 /* read in the list of undefined symbols */
219 void read_undef_symbols( const char *name )
221 FILE *f;
222 char buffer[1024];
223 int err;
225 undef_size = nb_undef_symbols = 0;
227 sprintf( buffer, "nm -u %s", name );
228 if (!(f = popen( buffer, "r" )))
229 fatal_error( "Cannot execute '%s'\n", buffer );
231 while (fgets( buffer, sizeof(buffer), f ))
233 char *p = buffer + strlen(buffer) - 1;
234 if (p < buffer) continue;
235 if (*p == '\n') *p-- = 0;
236 add_undef_symbol( buffer );
238 if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
241 /* resolve the imports for a Win32 module */
242 int resolve_imports( FILE *outfile )
244 int i, j, off;
245 char **p;
247 if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
249 add_extra_undef_symbols();
251 for (i = 0; i < nb_imports; i++)
253 struct import *imp = dll_imports[i];
255 for (j = 0; j < nb_undef_symbols; j++)
257 const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
258 if (res)
260 add_import_func( imp, res );
261 undef_symbols[j] = NULL;
264 /* remove all the holes in the undef symbols list */
265 p = undef_symbols;
266 for (j = off = 0; j < nb_undef_symbols; j++)
268 if (!undef_symbols[j]) off++;
269 else undef_symbols[j - off] = undef_symbols[j];
271 nb_undef_symbols -= off;
272 if (!off) warn_unused( imp->dll );
274 return 1;
277 /* output the import table of a Win32 module */
278 int output_imports( FILE *outfile )
280 int i, j, pos;
282 if (!nb_imports) goto done;
284 /* main import header */
286 fprintf( outfile, "\nstatic struct {\n" );
287 fprintf( outfile, " struct {\n" );
288 fprintf( outfile, " void *OriginalFirstThunk;\n" );
289 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
290 fprintf( outfile, " unsigned int ForwarderChain;\n" );
291 fprintf( outfile, " const char *Name;\n" );
292 fprintf( outfile, " void *FirstThunk;\n" );
293 fprintf( outfile, " } imp[%d];\n", nb_imports+1 );
294 fprintf( outfile, " const char *data[%d];\n", total_imports + nb_imports );
295 fprintf( outfile, "} imports = {\n {\n" );
297 /* list of dlls */
299 for (i = j = 0; i < nb_imports; i++)
301 fprintf( outfile, " { 0, 0, 0, \"%s\", &imports.data[%d] },\n",
302 dll_imports[i]->dll, j );
303 j += dll_imports[i]->nb_imports + 1;
305 fprintf( outfile, " { 0, 0, 0, 0, 0 },\n" );
306 fprintf( outfile, " },\n {\n" );
308 /* list of imported functions */
310 for (i = 0; i < nb_imports; i++)
312 fprintf( outfile, " /* %s */\n", dll_imports[i]->dll );
313 for (j = 0; j < dll_imports[i]->nb_imports; j++)
314 fprintf( outfile, " \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
315 fprintf( outfile, " 0,\n" );
317 fprintf( outfile, " }\n};\n\n" );
319 /* thunks for imported functions */
321 fprintf( outfile, "#ifndef __GNUC__\nstatic void __asm__dummy_import(void) {\n#endif\n\n" );
322 pos = 20 * (nb_imports + 1); /* offset of imports.data from start of imports */
323 fprintf( outfile, "asm(\".align 8\\n\"\n" );
324 for (i = 0; i < nb_imports; i++, pos += 4)
326 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
328 fprintf( outfile, " \"\\t" __ASM_FUNC("%s") "\\n\"\n",
329 dll_imports[i]->imports[j] );
330 fprintf( outfile, " \"\\t.globl " PREFIX "%s\\n\"\n",
331 dll_imports[i]->imports[j] );
332 fprintf( outfile, " \"" PREFIX "%s:\\t", dll_imports[i]->imports[j] );
333 if (strstr( dll_imports[i]->imports[j], "__wine_call_from_16" ))
334 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(imports+%d)\\n\\tnop\\n\"\n", pos );
335 else
336 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n\"\n", pos );
339 fprintf( outfile, ");\n#ifndef __GNUC__\n}\n#endif\n\n" );
341 done:
342 return nb_imports;