Added an unknown VxD error code.
[wine] / tools / winebuild / import.c
1 /*
2  * DLL imports support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *           2000 Eric Pouech
6  */
7
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "config.h"
14 #include "winnt.h"
15 #include "build.h"
16
17 struct import
18 {
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 */
26 };
27
28 static char **undef_symbols;  /* list of undefined symbols */
29 static int nb_undef_symbols = -1;
30 static int undef_size;
31
32 static char **ignore_symbols; /* list of symbols to ignore */
33 static int nb_ignore_symbols;
34 static int ignore_size;
35
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 */
41
42 /* compare function names; helper for resolve_imports */
43 static int name_cmp( const void *name, const void *entry )
44 {
45     return strcmp( *(char **)name, *(char **)entry );
46 }
47
48 /* locate a symbol in a (sorted) list */
49 inline static const char *find_symbol( const char *name, char **table, int size )
50 {
51     char **res = NULL;
52
53     if (table) {
54         res = bsearch( &name, table, size, sizeof(*table), name_cmp );
55     }
56
57     return res ? *res : NULL;
58 }
59
60 /* sort a symbol table */
61 inline static void sort_symbols( char **table, int size )
62 {
63     if (table )
64         qsort( table, size, sizeof(*table), name_cmp );
65 }
66
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 )
69 {
70     char *buffer, *p;
71     int fd;
72
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;
80     close( fd );
81     return buffer;
82 }
83
84 /* open the .so library for a given dll */
85 static char *open_library( const char *name )
86 {
87     char *fullname;
88     int i;
89
90     for (i = 0; i < nb_lib_paths; i++)
91     {
92         if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
93     }
94     if (!(fullname = try_library_path( ".", name )))
95         fatal_error( "could not open .so file for %s\n", name );
96     return fullname;
97 }
98
99 /* read in the list of exported symbols of a .so */
100 static void read_exported_symbols( const char *name, struct import *imp )
101 {
102     FILE *f;
103     char buffer[1024];
104     char *fullname, *cmdline;
105     const char *ext;
106     int size, err;
107
108     imp->exports    = NULL;
109     imp->nb_exports = size = 0;
110
111     if (!(ext = strrchr( name, '.' ))) ext = name + strlen(name);
112
113     if (!(fullname = open_library( name ))) return;
114     cmdline = xmalloc( strlen(fullname) + 4 );
115     sprintf( cmdline, "nm %s", fullname );
116     free( fullname );
117
118     if (!(f = popen( cmdline, "r" )))
119         fatal_error( "Cannot execute '%s'\n", cmdline );
120
121     while (fgets( buffer, sizeof(buffer), f ))
122     {
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;
127         p += 17;
128         if (strncmp( p, name, ext - name )) continue;
129         p += ext - name;
130         if (*p++ != '_') continue;
131
132         if (imp->nb_exports == size)
133         {
134             size += 128;
135             imp->exports = xrealloc( imp->exports, size * sizeof(*imp->exports) );
136         }
137         imp->exports[imp->nb_exports++] = xstrdup( p );
138     }
139     if ((err = pclose( f ))) fatal_error( "%s error %d\n", cmdline, err );
140     free( cmdline );
141     sort_symbols( imp->exports, imp->nb_exports );
142 }
143
144 /* add a dll to the list of imports */
145 void add_import_dll( const char *name, int delay )
146 {
147     struct import *imp = xmalloc( sizeof(*imp) );
148     imp->dll        = xstrdup( name );
149     imp->delay      = delay;
150     imp->imports    = NULL;
151     imp->nb_imports = 0;
152     /* GetToken for the file name has swallowed the '\n', hence pointing to next line */
153     imp->lineno = current_line - 1;
154
155     if (delay) nb_delayed++;
156     read_exported_symbols( name, imp );
157
158     dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
159     dll_imports[nb_imports++] = imp;
160 }
161
162 /* Add a symbol to the ignored symbol list */
163 void add_ignore_symbol( const char *name )
164 {
165     if (nb_ignore_symbols == ignore_size)
166     {
167         ignore_size += 32;
168         ignore_symbols = xrealloc( ignore_symbols, ignore_size * sizeof(*ignore_symbols) );
169     }
170     ignore_symbols[nb_ignore_symbols++] = xstrdup( name );
171 }
172
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 )
175 {
176     imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
177     imp->imports[imp->nb_imports++] = xstrdup( name );
178     total_imports++;
179     if (imp->delay) total_delayed++;
180 }
181
182 /* add a symbol to the undef list */
183 inline static void add_undef_symbol( const char *name )
184 {
185     if (nb_undef_symbols == undef_size)
186     {
187         undef_size += 128;
188         undef_symbols = xrealloc( undef_symbols, undef_size * sizeof(*undef_symbols) );
189     }
190     undef_symbols[nb_undef_symbols++] = xstrdup( name );
191 }
192
193 /* remove all the holes in the undefined symbol list; return the number of removed symbols */
194 static int remove_symbol_holes(void)
195 {
196     int i, off;
197     for (i = off = 0; i < nb_undef_symbols; i++)
198     {
199         if (!undef_symbols[i]) off++;
200         else undef_symbols[i - off] = undef_symbols[i];
201     }
202     nb_undef_symbols -= off;
203     return off;
204 }
205
206 /* add the extra undefined symbols that will be contained in the generated spec file itself */
207 static void add_extra_undef_symbols(void)
208 {
209     const char *extras[8];
210     int i, count = 0;
211
212 #define ADD_SYM(name) \
213     do { if (!find_symbol( extras[count] = (name), undef_symbols, \
214                            nb_undef_symbols )) count++; } while(0)
215
216     sort_symbols( undef_symbols, nb_undef_symbols );
217
218     /* add symbols that will be contained in the spec file itself */
219     switch (SpecMode)
220     {
221     case SPEC_MODE_DLL:
222         break;
223     case SPEC_MODE_GUIEXE:
224         ADD_SYM( "GetCommandLineA" );
225         ADD_SYM( "GetStartupInfoA" );
226         ADD_SYM( "GetModuleHandleA" );
227         /* fall through */
228     case SPEC_MODE_CUIEXE:
229         ADD_SYM( "__wine_get_main_args" );
230         ADD_SYM( "ExitProcess" );
231         break;
232     case SPEC_MODE_GUIEXE_UNICODE:
233         ADD_SYM( "GetCommandLineA" );
234         ADD_SYM( "GetStartupInfoA" );
235         ADD_SYM( "GetModuleHandleA" );
236         /* fall through */
237     case SPEC_MODE_CUIEXE_UNICODE:
238         ADD_SYM( "__wine_get_wmain_args" );
239         ADD_SYM( "ExitProcess" );
240         break;
241     }
242     ADD_SYM( "RtlRaiseException" );
243     if (nb_delayed)
244     {
245        ADD_SYM( "LoadLibraryA" );
246        ADD_SYM( "GetProcAddress" );
247     }
248
249     if (count)
250     {
251         for (i = 0; i < count; i++) add_undef_symbol( extras[i] );
252         sort_symbols( undef_symbols, nb_undef_symbols );
253     }
254 }
255
256 /* warn if a given dll is not used, but check forwards first */
257 static void warn_unused( const struct import* imp )
258 {
259     int i, curline;
260     size_t len = strlen(imp->dll);
261     const char *p = strchr( imp->dll, '.' );
262     if (p && !strcasecmp( p, ".dll" )) len = p - imp->dll;
263
264     for (i = Base; i <= Limit; i++)
265     {
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 */
271     }
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;
277 }
278
279 /* read in the list of undefined symbols */
280 void read_undef_symbols( const char *name )
281 {
282     FILE *f;
283     char buffer[1024];
284     int err;
285
286     undef_size = nb_undef_symbols = 0;
287
288     sprintf( buffer, "nm -u %s", name );
289     if (!(f = popen( buffer, "r" )))
290         fatal_error( "Cannot execute '%s'\n", buffer );
291
292     while (fgets( buffer, sizeof(buffer), f ))
293     {
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 );
299     }
300     if ((err = pclose( f ))) fatal_error( "nm -u %s error %d\n", name, err );
301 }
302
303 static void remove_ignored_symbols(void)
304 {
305     int i;
306
307     sort_symbols( ignore_symbols, nb_ignore_symbols );
308     for (i = 0; i < nb_undef_symbols; i++)
309     {
310         if (find_symbol( undef_symbols[i], ignore_symbols, nb_ignore_symbols ))
311         {
312             free( undef_symbols[i] );
313             undef_symbols[i] = NULL;
314         }
315     }
316     remove_symbol_holes();
317 }
318
319 /* resolve the imports for a Win32 module */
320 int resolve_imports( void )
321 {
322     int i, j;
323
324     if (nb_undef_symbols == -1) return 0; /* no symbol file specified */
325
326     add_extra_undef_symbols();
327     remove_ignored_symbols();
328
329     for (i = 0; i < nb_imports; i++)
330     {
331         struct import *imp = dll_imports[i];
332
333         for (j = 0; j < nb_undef_symbols; j++)
334         {
335             const char *res = find_symbol( undef_symbols[j], imp->exports, imp->nb_exports );
336             if (res)
337             {
338                 add_import_func( imp, res );
339                 free( undef_symbols[j] );
340                 undef_symbols[j] = NULL;
341             }
342         }
343         /* remove all the holes in the undef symbols list */
344         if (!remove_symbol_holes()) warn_unused( imp );
345     }
346     return 1;
347 }
348
349 /* output the import table of a Win32 module */
350 static int output_immediate_imports( FILE *outfile )
351 {
352     int i, j, pos;
353     int nb_imm = nb_imports - nb_delayed;
354
355     if (!nb_imm) goto done;
356
357     /* main import header */
358
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" );
370
371     /* list of dlls */
372
373     for (i = j = 0; i < nb_imports; i++)
374     {
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;
379     }
380
381     fprintf( outfile, "    { 0, 0, 0, 0, 0 },\n" );
382     fprintf( outfile, "  },\n  {\n" );
383
384     /* list of imported functions */
385
386     for (i = 0; i < nb_imports; i++)
387     {
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" );
393     }
394     fprintf( outfile, "  }\n};\n\n" );
395
396     /* thunks for imported functions */
397
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 %d\\n\"\n", get_alignment(8) );
401     for (i = 0; i < nb_imports; i++)
402     {
403         if (dll_imports[i]->delay) continue;
404         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
405         {
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] );
410
411
412
413
414             fprintf( outfile, "    \"" PREFIX "%s:\\n\\t", dll_imports[i]->imports[j] );
415
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 );
419             else
420                 fprintf( outfile, "jmp *(imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
421 #elif defined(__sparc__)
422             if ( !UsePIC )
423             {
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" );
427             }
428             else
429             {
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" );
438             }
439
440 #elif defined(__PPC__)
441             fprintf(outfile, "\taddi 1, 1, -0x4\\n\"\n");
442             fprintf(outfile, "\t\"\\tstw 9, 0(1)\\n\"\n");
443             fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
444             fprintf(outfile, "\t\"\\tstw 8, 0(1)\\n\"\n");
445             fprintf(outfile, "\t\"\\taddi 1, 1, -0x4\\n\"\n");
446             fprintf(outfile, "\t\"\\tstw 7, 0(1)\\n\"\n");
447
448             fprintf(outfile, "\t\"\\tlis 9,imports+%d@ha\\n\"\n", pos);
449             fprintf(outfile, "\t\"\\tla 8,imports+%d@l(9)\\n\"\n", pos);
450             fprintf(outfile, "\t\"\\tlwz 7, 0(8)\\n\"\n");
451             fprintf(outfile, "\t\"\\tmtctr 7\\n\"\n");
452
453             fprintf(outfile, "\t\"\\tlwz 7, 0(1)\\n\"\n");
454             fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
455             fprintf(outfile, "\t\"\\tlwz 8, 0(1)\\n\"\n");
456             fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
457             fprintf(outfile, "\t\"\\tlwz 9, 0(1)\\n\"\n");
458             fprintf(outfile, "\t\"\\taddi 1, 1, 0x4\\n\"\n");
459             fprintf(outfile, "\t\"\\tbctr\\n");
460 #else
461 #error You need to define import thunks for your architecture!
462 #endif
463             fprintf( outfile, "\"\n" );
464         }
465         pos += 4;
466     }
467     fprintf( outfile, "\".previous\");\n#ifndef __GNUC__\n}\n#endif\n\n" );
468
469  done:
470     return nb_imm;
471 }
472
473 /* output the delayed import table of a Win32 module */
474 static int output_delayed_imports( FILE *outfile )
475 {
476     int i, idx, j, pos;
477
478     if (!nb_delayed) goto done;
479
480     for (i = 0; i < nb_imports; i++)
481     {
482         if (!dll_imports[i]->delay) continue;
483         fprintf( outfile, "static void *__wine_delay_imp_%d_hmod;\n", i);
484         for (j = 0; j < dll_imports[i]->nb_imports; j++)
485         {
486             fprintf( outfile, "void __wine_delay_imp_%d_%s();\n",
487                      i, dll_imports[i]->imports[j] );
488         }
489     }
490     fprintf( outfile, "\n" );
491     fprintf( outfile, "static struct {\n" );
492     fprintf( outfile, "  struct ImgDelayDescr {\n" );
493     fprintf( outfile, "    unsigned int  grAttrs;\n" );
494     fprintf( outfile, "    const char   *szName;\n" );
495     fprintf( outfile, "    void        **phmod;\n" );
496     fprintf( outfile, "    void        **pIAT;\n" );
497     fprintf( outfile, "    const char  **pINT;\n" );
498     fprintf( outfile, "    void*         pBoundIAT;\n" );
499     fprintf( outfile, "    void*         pUnloadIAT;\n" );
500     fprintf( outfile, "    unsigned long dwTimeStamp;\n" );
501     fprintf( outfile, "  } imp[%d];\n", nb_delayed );
502     fprintf( outfile, "  void         *IAT[%d];\n", total_delayed );
503     fprintf( outfile, "  const char   *INT[%d];\n", total_delayed );
504     fprintf( outfile, "} delay_imports = {\n" );
505     fprintf( outfile, "  {\n" );
506     for (i = j = 0; i < nb_imports; i++)
507     {
508         if (!dll_imports[i]->delay) continue;
509         fprintf( outfile, "    { 0, \"%s\", &__wine_delay_imp_%d_hmod, &delay_imports.IAT[%d], &delay_imports.INT[%d], 0, 0, 0 },\n",
510                  dll_imports[i]->dll, i, j, j );
511         j += dll_imports[i]->nb_imports;
512     }
513     fprintf( outfile, "  },\n  {\n" );
514     for (i = 0; i < nb_imports; i++)
515     {
516         if (!dll_imports[i]->delay) continue;
517         fprintf( outfile, "    /* %s */\n", dll_imports[i]->dll );
518         for (j = 0; j < dll_imports[i]->nb_imports; j++)
519         {
520             fprintf( outfile, "    &__wine_delay_imp_%d_%s,\n", i, dll_imports[i]->imports[j] );
521         }
522     }
523     fprintf( outfile, "  },\n  {\n" );
524     for (i = 0; i < nb_imports; i++)
525     {
526         if (!dll_imports[i]->delay) continue;
527         fprintf( outfile, "    /* %s */\n", dll_imports[i]->dll );
528         for (j = 0; j < dll_imports[i]->nb_imports; j++)
529         {
530             fprintf( outfile, "    \"\\0\\0%s\",\n", dll_imports[i]->imports[j] );
531         }
532     }
533     fprintf( outfile, "  }\n};\n\n" );
534
535     /* check if there's some stub defined. if so, exception struct 
536      *  is already defined, so don't emit it twice 
537      */
538     for (i = 0; i < nb_entry_points; i++) if (EntryPoints[i]->type == TYPE_STUB) break;
539
540     if (i == nb_entry_points) {
541        fprintf( outfile, "struct exc_record {\n" );
542        fprintf( outfile, "  unsigned int code, flags;\n" );
543        fprintf( outfile, "  void *rec, *addr;\n" );
544        fprintf( outfile, "  unsigned int params;\n" );
545        fprintf( outfile, "  const void *info[15];\n" );
546        fprintf( outfile, "};\n\n" );
547        fprintf( outfile, "extern void __stdcall RtlRaiseException( struct exc_record * );\n" );
548     }
549
550     fprintf( outfile, "extern void * __stdcall LoadLibraryA(const char*);\n");
551     fprintf( outfile, "extern void * __stdcall GetProcAddress(void *, const char*);\n");
552     fprintf( outfile, "\n" );
553
554     fprintf( outfile, "void *__stdcall __wine_delay_load( int idx_nr )\n" );
555     fprintf( outfile, "{\n" );
556     fprintf( outfile, "  int idx = idx_nr >> 16, nr = idx_nr & 0xffff;\n" );
557     fprintf( outfile, "  struct ImgDelayDescr *imd = delay_imports.imp + idx;\n" );
558     fprintf( outfile, "  void **pIAT = imd->pIAT + nr;\n" );
559     fprintf( outfile, "  const char** pINT = imd->pINT + nr;\n" );
560     fprintf( outfile, "  void *fn;\n\n" );
561
562     fprintf( outfile, "  if (!*imd->phmod) *imd->phmod = LoadLibraryA(imd->szName);\n" );
563     fprintf( outfile, "  if (*imd->phmod && (fn = GetProcAddress(*imd->phmod, *pINT + 2)))\n");
564     fprintf( outfile, "    /* patch IAT with final value */\n" );
565     fprintf( outfile, "    return *pIAT = fn;\n" );
566     fprintf( outfile, "  else {\n");
567     fprintf( outfile, "    struct exc_record rec;\n" );
568     fprintf( outfile, "    rec.code    = 0x80000100;\n" );
569     fprintf( outfile, "    rec.flags   = 1;\n" );
570     fprintf( outfile, "    rec.rec     = 0;\n" );
571     fprintf( outfile, "    rec.params  = 2;\n" );
572     fprintf( outfile, "    rec.info[0] = imd->szName;\n" );
573     fprintf( outfile, "    rec.info[1] = *pINT + 2;\n" );
574     fprintf( outfile, "#ifdef __GNUC__\n" );
575     fprintf( outfile, "    rec.addr = __builtin_return_address(1);\n" );
576     fprintf( outfile, "#else\n" );
577     fprintf( outfile, "    rec.addr = 0;\n" );
578     fprintf( outfile, "#endif\n" );
579     fprintf( outfile, "    for (;;) RtlRaiseException( &rec );\n" );
580     fprintf( outfile, "    return 0; /* shouldn't go here */\n" );
581     fprintf( outfile, "  }\n}\n\n" );
582
583     fprintf( outfile, "#ifndef __GNUC__\n" );
584     fprintf( outfile, "static void __asm__dummy_delay_import(void) {\n" );
585     fprintf( outfile, "#endif\n" );
586
587     fprintf( outfile, "asm(\".align %d\\n\"\n", get_alignment(8) );
588     fprintf( outfile, "    \"\\t" __ASM_FUNC("__wine_delay_load_asm") "\\n\"\n" );
589     fprintf( outfile, "    \"" PREFIX "__wine_delay_load_asm:\\n\"\n" );
590 #if defined(__i386__)
591     fprintf( outfile, "    \"\\tpushl %%ecx\\n\\tpushl %%edx\\n\\tpushl %%eax\\n\"\n" );
592     fprintf( outfile, "    \"\\tcall __wine_delay_load\\n\"\n" );
593     fprintf( outfile, "    \"\\tpopl %%edx\\n\\tpopl %%ecx\\n\\tjmp *%%eax\\n\"\n" );
594 #elif defined(__sparc__)
595     fprintf( outfile, "    \"\\tsave %%sp, -96, %%sp\\n\"\n" );
596     fprintf( outfile, "    \"\\tcall __wine_delay_load\\n\"\n" );
597     fprintf( outfile, "    \"\\tmov %%g1, %%o0\\n\"\n" );
598     fprintf( outfile, "    \"\\tjmp %%o0\\n\\trestore\\n\"\n" );
599 #elif defined(__PPC__)
600     fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
601 #else
602 #error You need to defined delayed import thunks for your architecture!
603 #endif
604
605     for (i = idx = 0; i < nb_imports; i++)
606     {
607         if (!dll_imports[i]->delay) continue;
608         for (j = 0; j < dll_imports[i]->nb_imports; j++)
609         {
610             char buffer[128];
611             sprintf( buffer, "__wine_delay_imp_%d_%s", i, dll_imports[i]->imports[j]);
612             fprintf( outfile, "    \"\\t" __ASM_FUNC("%s") "\\n\"\n", buffer );
613             fprintf( outfile, "    \"" PREFIX "%s:\\n\"\n", buffer );
614 #if defined(__i386__)
615             fprintf( outfile, "    \"\\tmovl $%d, %%eax\\n\"\n", (idx << 16) | j );
616             fprintf( outfile, "    \"\\tjmp __wine_delay_load_asm\\n\"\n" );
617 #elif defined(__sparc__)
618             fprintf( outfile, "    \"\\tset %d, %%g1\\n\"\n", (idx << 16) | j );
619             fprintf( outfile, "    \"\\tb,a __wine_delay_load_asm\\n\"\n" );
620 #elif defined(__PPC__)
621             fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
622 #else
623 #error You need to defined delayed import thunks for your architecture!
624 #endif
625         }
626         idx++;
627     }
628
629     fprintf( outfile, "\n    \".data\\n\\t.align %d\\n\"\n", get_alignment(8) );
630     pos = nb_delayed * 32;
631     for (i = 0; i < nb_imports; i++)
632     {
633         if (!dll_imports[i]->delay) continue;
634         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += 4)
635         {
636             fprintf( outfile, "    \"\\t" __ASM_FUNC("%s") "\\n\"\n",
637                      dll_imports[i]->imports[j] );
638             fprintf( outfile, "    \"\\t.globl " PREFIX "%s\\n\"\n",
639                      dll_imports[i]->imports[j] );
640             fprintf( outfile, "    \"" PREFIX "%s:\\n\\t", dll_imports[i]->imports[j] );
641 #if defined(__i386__)
642             if (strstr( dll_imports[i]->imports[j], "__wine_call_from_16" ))
643                 fprintf( outfile, ".byte 0x2e\\n\\tjmp *(delay_imports+%d)\\n\\tnop\\n", pos );
644             else
645                 fprintf( outfile, "jmp *(delay_imports+%d)\\n\\tmovl %%esi,%%esi\\n", pos );
646 #elif defined(__sparc__)
647             if ( !UsePIC )
648             {
649                 fprintf( outfile, "sethi %%hi(delay_imports+%d), %%g1\\n\\t", pos );
650                 fprintf( outfile, "ld [%%g1+%%lo(delay_imports+%d)], %%g1\\n\\t", pos );
651                 fprintf( outfile, "jmp %%g1\\n\\tnop\\n" );
652             }
653             else
654             {
655                 /* Hmpf.  Stupid sparc assembler always interprets global variable
656                    names as GOT offsets, so we have to do it the long way ... */
657                 fprintf( outfile, "save %%sp, -96, %%sp\\n" );
658                 fprintf( outfile, "0:\\tcall 1f\\n\\tnop\\n" );
659                 fprintf( outfile, "1:\\tsethi %%hi(delay_imports+%d-0b), %%g1\\n\\t", pos );
660                 fprintf( outfile, "or %%g1, %%lo(delay_imports+%d-0b), %%g1\\n\\t", pos );
661                 fprintf( outfile, "ld [%%g1+%%o7], %%g1\\n\\t" );
662                 fprintf( outfile, "jmp %%g1\\n\\trestore\\n" );
663             }
664
665 #elif defined(__PPC__)
666             fprintf(outfile, "#error: DELAYED IMPORTS NOT SUPPORTED ON PPC!!!\n");
667 #else
668 #error You need to define delayed import thunks for your architecture!
669 #endif
670             fprintf( outfile, "\"\n" );
671         }
672     }
673     fprintf( outfile, "\".previous\");\n" );
674     fprintf( outfile, "#ifndef __GNUC__\n" );
675     fprintf( outfile, "}\n" );
676     fprintf( outfile, "#endif\n" );
677     fprintf( outfile, "\n" );
678
679  done:
680     return nb_delayed;
681 }
682
683 /* output the import and delayed import tables of a Win32 module
684  * returns number of DLLs exported in 'immediate' mode
685  */
686 int output_imports( FILE *outfile )
687 {
688    output_delayed_imports( outfile );
689    return output_immediate_imports( outfile );
690 }