Modified winebuild to use the __ASM_FUNC macro for greater portability.
[wine] / tools / winebuild / import.c
1 /*
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 "config.h"
12 #include "winnt.h"
13 #include "build.h"
14
15 struct import
16 {
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 */
22 };
23
24 static char **undef_symbols;  /* list of undefined symbols */
25 static int nb_undef_symbols = -1;
26 static int undef_size;
27
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 */
31
32
33 /* compare function names; helper for resolve_imports */
34 static int name_cmp( const void *name, const void *entry )
35 {
36     return strcmp( *(char **)name, *(char **)entry );
37 }
38
39 /* locate a symbol in a (sorted) list */
40 inline static const char *find_symbol( const char *name, char **table, int size )
41 {
42     char **res = bsearch( &name, table, size, sizeof(*table), name_cmp );
43     return res ? *res : NULL;
44 }
45
46 /* sort a symbol table */
47 inline static void sort_symbols( char **table, int size )
48 {
49     qsort( table, size, sizeof(*table), name_cmp );
50 }
51
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 )
54 {
55     char *buffer, *p;
56     int fd;
57
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;
67 }
68
69 /* open the .so library for a given dll */
70 static char *open_library( const char *name )
71 {
72     char *fullname;
73     int i;
74
75     for (i = 0; i < nb_lib_paths; i++)
76     {
77         if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
78     }
79     if (!(fullname = try_library_path( ".", name )))
80         fatal_error( "could not open .so file for %s\n", name );
81     return fullname;
82 }
83
84 /* read in the list of exported symbols of a .so */
85 static void read_exported_symbols( const char *name, struct import *imp )
86 {
87     FILE *f;
88     char buffer[1024];
89     char *fullname, *cmdline;
90     const char *ext;
91     int size, err;
92
93     imp->exports    = NULL;
94     imp->nb_exports = size = 0;
95
96     if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
97
98     if (!(fullname = open_library( name ))) return;
99     cmdline = xmalloc( strlen(fullname) + 4 );
100     sprintf( cmdline, "nm %s", fullname );
101     free( fullname );
102
103     if (!(f = popen( cmdline, "r" )))
104         fatal_error( "Cannot execute '%s'\n", cmdline );
105
106     while (fgets( buffer, sizeof(buffer), f ))
107     {
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;
116
117         if (imp->nb_exports == size)
118         {
119             size += 128;
120             imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
121         }
122         imp->exports[imp->nb_exports++] = xstrdup( p );
123     }
124     if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
125     free( cmdline );
126     sort_symbols( imp->exports, imp->nb_exports );
127 }
128
129 /* add a dll to the list of imports */
130 void add_import_dll( const char *name )
131 {
132     struct import *imp = xmalloc( sizeof(*imp) );
133     imp->dll        = xstrdup( name );
134     imp->imports    = NULL;
135     imp->nb_imports = 0;
136
137     read_exported_symbols( name, imp );
138
139     dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
140     dll_imports[nb_imports++] = imp;
141 }
142
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 )
145 {
146     imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
147     imp->imports[imp->nb_imports++] = xstrdup( name );
148     total_imports++;
149 }
150
151 /* add a symbol to the undef list */
152 inline static void add_undef_symbol( const char *name )
153 {
154     if (nb_undef_symbols == undef_size)
155     {
156         undef_size += 128;
157         undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
158     }
159     undef_symbols[nb_undef_symbols++] = xstrdup( name );
160 }
161
162 /* add the extra undefined symbols that will be contained in the generated spec file itself */
163 static void add_extra_undef_symbols(void)
164 {
165     const char *extras[8];
166     int i, count = 0;
167
168 #define ADD_SYM(name) \
169     do { if (!find_symbol( extras[count] = (name), undef_symbols, \
170                            nb_undef_symbols )) count++; } while(0)
171
172     sort_symbols( undef_symbols, nb_undef_symbols );
173
174     /* add symbols that will be contained in the spec file itself */
175     switch (SpecMode)
176     {
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;
191     }
192     if (count)
193     {
194         for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
195         sort_symbols( undef_symbols, nb_undef_symbols );
196     }
197 }
198
199 /* warn if a given dll is not used, but check forwards first */
200 static void warn_unused( const char *dllname )
201 {
202     int i;
203     size_t len = strlen(dllname);
204     const char *p = strchr( dllname, '.' );
205     if (p && !strcasecmp( p, ".dll" )) len = p - dllname;
206
207     for (i = Base; i <= Limit; i++)
208     {
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 */
214     }
215     warning( "%s imported but no symbols used\n", dllname );
216 }
217
218 /* read in the list of undefined symbols */
219 void read_undef_symbols( const char *name )
220 {
221     FILE *f;
222     char buffer[1024];
223     int err;
224
225     undef_size = nb_undef_symbols = 0;
226
227     sprintf( buffer, "nm -u %s", name );
228     if (!(f = popen( buffer, "r" )))
229         fatal_error( "Cannot execute '%s'\n", buffer );
230
231     while (fgets( buffer, sizeof(buffer), f ))
232     {
233         char *p = buffer + strlen(buffer) - 1;
234         if (p < buffer) continue;
235         if (*p == '\n') *p-- = 0;
236         add_undef_symbol( buffer );
237     }
238     if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
239 }
240
241 /* resolve the imports for a Win32 module */
242 int resolve_imports( FILE *outfile )
243 {
244     int i, j, off;
245     char **p;
246
247     if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
248
249     add_extra_undef_symbols();
250
251     for (i = 0; i < nb_imports; i++)
252     {
253         struct import *imp = dll_imports[i];
254
255         for (j = 0; j < nb_undef_symbols; j++)
256         {
257             const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
258             if (res)
259             {
260                 add_import_func( imp, res );
261                 undef_symbols[j] = NULL;
262             }
263         }
264         /* remove all the holes in the undef symbols list */
265         p = undef_symbols;
266         for (j = off = 0; j < nb_undef_symbols; j++)
267         {
268             if (!undef_symbols[j]) off++;
269             else undef_symbols[j - off] = undef_symbols[j];
270         }
271         nb_undef_symbols -= off;
272         if (!off) warn_unused( imp->dll );
273     }
274     return 1;
275 }
276
277 /* output the import table of a Win32 module */
278 int output_imports( FILE *outfile )
279 {
280     int i, j, pos;
281
282     if (!nb_imports) goto done;
283
284     /* main import header */
285
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" );
296
297     /* list of dlls */
298
299     for (i = j = 0; i < nb_imports; i++)
300     {
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;
304     }
305     fprintf( outfile, "    { 0, 0, 0, 0, 0 },\n" );
306     fprintf( outfile, "  },\n  {\n" );
307
308     /* list of imported functions */
309
310     for (i = 0; i < nb_imports; i++)
311     {
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" );
316     }
317     fprintf( outfile, "  }\n};\n\n" );
318
319     /* thunks for imported functions */
320
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)
325     {
326         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
327         {
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 );
337         }
338     }
339     fprintf( outfile, ");\n#ifndef __GNUC__\n}\n#endif\n\n" );
340
341  done:
342     return nb_imports;
343 }