d3d8: All objects created by device should keep reference to it.
[wine] / tools / winebuild / import.c
1 /*
2  * DLL imports support
3  *
4  * Copyright 2000, 2004 Alexandre Julliard
5  * Copyright 2000 Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "build.h"
41
42 struct import
43 {
44     DLLSPEC     *spec;        /* description of the imported dll */
45     char        *full_name;   /* full name of the input file */
46     dev_t        dev;         /* device/inode of the input file */
47     ino_t        ino;
48     int          delay;       /* delay or not dll loading ? */
49     ORDDEF     **exports;     /* functions exported from this dll */
50     int          nb_exports;  /* number of exported functions */
51     ORDDEF     **imports;     /* functions we want to import from this dll */
52     int          nb_imports;  /* number of imported functions */
53 };
54
55 struct name_table
56 {
57     char **names;
58     unsigned int count, size;
59 };
60
61 static struct name_table undef_symbols;    /* list of undefined symbols */
62 static struct name_table ignore_symbols;   /* list of symbols to ignore */
63 static struct name_table extra_ld_symbols; /* list of extra symbols that ld should resolve */
64 static struct name_table delayed_imports;  /* list of delayed import dlls */
65 static struct name_table ext_link_imports; /* list of external symbols to link to */
66
67 static struct import **dll_imports = NULL;
68 static int nb_imports = 0;      /* number of imported dlls (delayed or not) */
69 static int nb_delayed = 0;      /* number of delayed dlls */
70 static int total_imports = 0;   /* total number of imported functions */
71 static int total_delayed = 0;   /* total number of imported functions in delayed DLLs */
72
73
74 static inline const char *ppc_reg( int reg )
75 {
76     static const char * const ppc_regs[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
77                                                "r8", "r9", "r10","r11","r12","r13","r14","r15",
78                                                "r16","r17","r18","r19","r20","r21","r22","r23",
79                                                "r24","r25","r26","r27","r28","r29","r30","r31" };
80     if (target_platform == PLATFORM_APPLE) return ppc_regs[reg];
81     return ppc_regs[reg] + 1;  /* skip the 'r' */
82 }
83
84 /* compare function names; helper for resolve_imports */
85 static int name_cmp( const void *name, const void *entry )
86 {
87     return strcmp( *(const char* const *)name, *(const char* const *)entry );
88 }
89
90 /* compare function names; helper for resolve_imports */
91 static int func_cmp( const void *func1, const void *func2 )
92 {
93     const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
94     const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
95     return strcmp( odp1->name ? odp1->name : odp1->export_name,
96                    odp2->name ? odp2->name : odp2->export_name );
97 }
98
99 /* add a name to a name table */
100 inline static void add_name( struct name_table *table, const char *name )
101 {
102     if (table->count == table->size)
103     {
104         table->size += (table->size / 2);
105         if (table->size < 32) table->size = 32;
106         table->names = xrealloc( table->names, table->size * sizeof(*table->names) );
107     }
108     table->names[table->count++] = xstrdup( name );
109 }
110
111 /* remove a name from a name table */
112 inline static void remove_name( struct name_table *table, unsigned int idx )
113 {
114     assert( idx < table->count );
115     free( table->names[idx] );
116     memmove( table->names + idx, table->names + idx + 1,
117              (table->count - idx - 1) * sizeof(*table->names) );
118     table->count--;
119 }
120
121 /* make a name table empty */
122 inline static void empty_name_table( struct name_table *table )
123 {
124     unsigned int i;
125
126     for (i = 0; i < table->count; i++) free( table->names[i] );
127     table->count = 0;
128 }
129
130 /* locate a name in a (sorted) list */
131 inline static const char *find_name( const char *name, const struct name_table *table )
132 {
133     char **res = NULL;
134
135     if (table->count) res = bsearch( &name, table->names, table->count, sizeof(*table->names), name_cmp );
136     return res ? *res : NULL;
137 }
138
139 /* sort a name table */
140 inline static void sort_names( struct name_table *table )
141 {
142     if (table->count) qsort( table->names, table->count, sizeof(*table->names), name_cmp );
143 }
144
145 /* locate an export in a (sorted) export list */
146 inline static ORDDEF *find_export( const char *name, ORDDEF **table, int size )
147 {
148     ORDDEF func, *odp, **res = NULL;
149
150     func.name = (char *)name;
151     func.ordinal = -1;
152     odp = &func;
153     if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
154     return res ? *res : NULL;
155 }
156
157 /* free an import structure */
158 static void free_imports( struct import *imp )
159 {
160     free( imp->exports );
161     free( imp->imports );
162     free_dll_spec( imp->spec );
163     free( imp->full_name );
164     free( imp );
165 }
166
167 /* check whether a given dll is imported in delayed mode */
168 static int is_delayed_import( const char *name )
169 {
170     int i;
171
172     for (i = 0; i < delayed_imports.count; i++)
173     {
174         if (!strcmp( delayed_imports.names[i], name )) return 1;
175     }
176     return 0;
177 }
178
179 /* check whether a given dll has already been imported */
180 static struct import *is_already_imported( const char *name )
181 {
182     int i;
183
184     for (i = 0; i < nb_imports; i++)
185     {
186         if (!strcmp( dll_imports[i]->spec->file_name, name )) return dll_imports[i];
187     }
188     return NULL;
189 }
190
191 /* open the .so library for a given dll in a specified path */
192 static char *try_library_path( const char *path, const char *name )
193 {
194     char *buffer;
195     int fd;
196
197     buffer = xmalloc( strlen(path) + strlen(name) + 9 );
198     sprintf( buffer, "%s/lib%s.def", path, name );
199
200     /* check if the file exists */
201     if ((fd = open( buffer, O_RDONLY )) != -1)
202     {
203         close( fd );
204         return buffer;
205     }
206     free( buffer );
207     return NULL;
208 }
209
210 /* find the .def import library for a given dll */
211 static char *find_library( const char *name )
212 {
213     char *fullname;
214     int i;
215
216     for (i = 0; i < nb_lib_paths; i++)
217     {
218         if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
219     }
220     fatal_error( "could not open .def file for %s\n", name );
221     return NULL;
222 }
223
224 /* read in the list of exported symbols of an import library */
225 static int read_import_lib( struct import *imp )
226 {
227     FILE *f;
228     int i, ret;
229     struct stat stat;
230     struct import *prev_imp;
231     DLLSPEC *spec = imp->spec;
232
233     f = open_input_file( NULL, imp->full_name );
234     fstat( fileno(f), &stat );
235     imp->dev = stat.st_dev;
236     imp->ino = stat.st_ino;
237     ret = parse_def_file( f, spec );
238     close_input_file( f );
239     if (!ret) return 0;
240
241     /* check if we already imported that library from a different file */
242     if ((prev_imp = is_already_imported( spec->file_name )))
243     {
244         if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
245             fatal_error( "%s and %s have the same export name '%s'\n",
246                          prev_imp->full_name, imp->full_name, spec->file_name );
247         return 0;  /* the same file was already loaded, ignore this one */
248     }
249
250     if (is_delayed_import( spec->file_name ))
251     {
252         imp->delay = 1;
253         nb_delayed++;
254     }
255
256     imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
257
258     for (i = 0; i < spec->nb_entry_points; i++)
259     {
260         ORDDEF *odp = &spec->entry_points[i];
261
262         if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
263         if (odp->flags & FLAG_PRIVATE) continue;
264         imp->exports[imp->nb_exports++] = odp;
265     }
266     imp->exports = xrealloc( imp->exports, imp->nb_exports * sizeof(*imp->exports) );
267     if (imp->nb_exports)
268         qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
269     return 1;
270 }
271
272 /* build the dll exported name from the import lib name or path */
273 static char *get_dll_name( const char *name, const char *filename )
274 {
275     char *ret;
276
277     if (filename)
278     {
279         const char *basename = strrchr( filename, '/' );
280         if (!basename) basename = filename;
281         else basename++;
282         if (!strncmp( basename, "lib", 3 )) basename += 3;
283         ret = xmalloc( strlen(basename) + 5 );
284         strcpy( ret, basename );
285         if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
286     }
287     else
288     {
289         ret = xmalloc( strlen(name) + 5 );
290         strcpy( ret, name );
291     }
292     if (!strchr( ret, '.' )) strcat( ret, ".dll" );
293     return ret;
294 }
295
296 /* add a dll to the list of imports */
297 void add_import_dll( const char *name, const char *filename )
298 {
299     struct import *imp = xmalloc( sizeof(*imp) );
300
301     imp->spec            = alloc_dll_spec();
302     imp->spec->file_name = get_dll_name( name, filename );
303     imp->delay           = 0;
304     imp->imports         = NULL;
305     imp->nb_imports      = 0;
306     imp->exports         = NULL;
307     imp->nb_exports      = 0;
308
309     if (filename) imp->full_name = xstrdup( filename );
310     else imp->full_name = find_library( name );
311
312     if (read_import_lib( imp ))
313     {
314         dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
315         dll_imports[nb_imports++] = imp;
316     }
317     else
318     {
319         free_imports( imp );
320         if (nb_errors) exit(1);
321     }
322 }
323
324 /* add a library to the list of delayed imports */
325 void add_delayed_import( const char *name )
326 {
327     struct import *imp;
328     char *fullname = get_dll_name( name, NULL );
329
330     add_name( &delayed_imports, fullname );
331     if ((imp = is_already_imported( fullname )) && !imp->delay)
332     {
333         imp->delay = 1;
334         nb_delayed++;
335     }
336     free( fullname );
337 }
338
339 /* remove an imported dll, based on its index in the dll_imports array */
340 static void remove_import_dll( int index )
341 {
342     struct import *imp = dll_imports[index];
343
344     memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
345     nb_imports--;
346     if (imp->delay) nb_delayed--;
347     free_imports( imp );
348 }
349
350 /* add a symbol to the ignored symbol list */
351 /* if the name starts with '-' the symbol is removed instead */
352 void add_ignore_symbol( const char *name )
353 {
354     unsigned int i;
355
356     if (name[0] == '-')  /* remove it */
357     {
358         if (!name[1]) empty_name_table( &ignore_symbols );  /* remove everything */
359         else for (i = 0; i < ignore_symbols.count; i++)
360         {
361             if (!strcmp( ignore_symbols.names[i], name+1 )) remove_name( &ignore_symbols, i-- );
362         }
363     }
364     else add_name( &ignore_symbols, name );
365 }
366
367 /* add a symbol to the list of extra symbols that ld must resolve */
368 void add_extra_ld_symbol( const char *name )
369 {
370     add_name( &extra_ld_symbols, name );
371 }
372
373 /* add a function to the list of imports from a given dll */
374 static void add_import_func( struct import *imp, ORDDEF *func )
375 {
376     imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
377     imp->imports[imp->nb_imports++] = func;
378     total_imports++;
379     if (imp->delay) total_delayed++;
380 }
381
382 /* get the default entry point for a given spec file */
383 static const char *get_default_entry_point( const DLLSPEC *spec )
384 {
385     if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
386     if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
387     return "__wine_spec_exe_entry";
388 }
389
390 /* check if the spec file exports any stubs */
391 static int has_stubs( const DLLSPEC *spec )
392 {
393     int i;
394     for (i = 0; i < spec->nb_entry_points; i++)
395     {
396         ORDDEF *odp = &spec->entry_points[i];
397         if (odp->type == TYPE_STUB) return 1;
398     }
399     return 0;
400 }
401
402 /* add the extra undefined symbols that will be contained in the generated spec file itself */
403 static void add_extra_undef_symbols( DLLSPEC *spec )
404 {
405     if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point(spec) );
406     add_extra_ld_symbol( spec->init_func );
407     if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
408     if (nb_delayed) add_extra_ld_symbol( "__wine_spec_delay_load" );
409 }
410
411 /* check if a given imported dll is not needed, taking forwards into account */
412 static int check_unused( const struct import* imp, const DLLSPEC *spec )
413 {
414     int i;
415     const char *file_name = imp->spec->file_name;
416     size_t len = strlen( file_name );
417     const char *p = strchr( file_name, '.' );
418     if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
419
420     for (i = spec->base; i <= spec->limit; i++)
421     {
422         ORDDEF *odp = spec->ordinals[i];
423         if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
424         if (!strncasecmp( odp->link_name, file_name, len ) &&
425             odp->link_name[len] == '.')
426             return 0;  /* found a forward, it is used */
427     }
428     return 1;
429 }
430
431 /* flag the dll exports that link to an undefined symbol */
432 static void check_undefined_exports( DLLSPEC *spec )
433 {
434     int i;
435
436     for (i = 0; i < spec->nb_entry_points; i++)
437     {
438         ORDDEF *odp = &spec->entry_points[i];
439         if (odp->type == TYPE_STUB) continue;
440         if (odp->flags & FLAG_FORWARD) continue;
441         if (find_name( odp->link_name, &undef_symbols ))
442         {
443             switch(odp->type)
444             {
445             case TYPE_PASCAL:
446             case TYPE_STDCALL:
447             case TYPE_CDECL:
448             case TYPE_VARARGS:
449                 if (link_ext_symbols)
450                 {
451                     odp->flags |= FLAG_EXT_LINK;
452                     add_name( &ext_link_imports, odp->link_name );
453                 }
454                 else error( "%s:%d: function '%s' not defined\n",
455                             spec->src_name, odp->lineno, odp->link_name );
456                 break;
457             default:
458                 error( "%s:%d: external symbol '%s' is not a function\n",
459                        spec->src_name, odp->lineno, odp->link_name );
460                 break;
461             }
462         }
463     }
464 }
465
466 /* create a .o file that references all the undefined symbols we want to resolve */
467 static char *create_undef_symbols_file( DLLSPEC *spec )
468 {
469     char *as_file, *obj_file;
470     unsigned int i;
471     FILE *f;
472
473     as_file = get_temp_file_name( output_file_name, ".s" );
474     if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
475     fprintf( f, "\t.data\n" );
476
477     for (i = 0; i < spec->nb_entry_points; i++)
478     {
479         ORDDEF *odp = &spec->entry_points[i];
480         if (odp->type == TYPE_STUB) continue;
481         if (odp->flags & FLAG_FORWARD) continue;
482         fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
483     }
484     for (i = 0; i < extra_ld_symbols.count; i++)
485         fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[i]) );
486     fclose( f );
487
488     obj_file = get_temp_file_name( output_file_name, ".o" );
489     assemble_file( as_file, obj_file );
490     return obj_file;
491 }
492
493 /* combine a list of object files with ld into a single object file */
494 /* returns the name of the combined file */
495 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
496 {
497     unsigned int i, len = 0;
498     char *cmd, *p, *ld_tmp_file, *undef_file;
499     int err;
500
501     undef_file = create_undef_symbols_file( spec );
502     len += strlen(undef_file) + 1;
503     ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
504     if (!ld_command) ld_command = xstrdup("ld");
505     for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
506     cmd = p = xmalloc( len + strlen(ld_tmp_file) + 8 + strlen(ld_command)  );
507     p += sprintf( cmd, "%s -r -o %s %s", ld_command, ld_tmp_file, undef_file );
508     for (i = 0; argv[i]; i++)
509         p += sprintf( p, " %s", argv[i] );
510     if (verbose) fprintf( stderr, "%s\n", cmd );
511     err = system( cmd );
512     if (err) fatal_error( "%s -r failed with status %d\n", ld_command, err );
513     free( cmd );
514     return ld_tmp_file;
515 }
516
517 /* read in the list of undefined symbols */
518 void read_undef_symbols( DLLSPEC *spec, char **argv )
519 {
520     size_t prefix_len;
521     FILE *f;
522     char *cmd, buffer[1024], name_prefix[16];
523     int err;
524     const char *name;
525
526     if (!argv[0]) return;
527
528     add_extra_undef_symbols( spec );
529
530     strcpy( name_prefix, asm_name("") );
531     prefix_len = strlen( name_prefix );
532
533     name = ldcombine_files( spec, argv );
534
535     if (!nm_command) nm_command = xstrdup("nm");
536     cmd = xmalloc( strlen(nm_command) + strlen(name) + 5 );
537     sprintf( cmd, "%s -u %s", nm_command, name );
538     if (!(f = popen( cmd, "r" )))
539         fatal_error( "Cannot execute '%s'\n", cmd );
540
541     while (fgets( buffer, sizeof(buffer), f ))
542     {
543         char *p = buffer + strlen(buffer) - 1;
544         if (p < buffer) continue;
545         if (*p == '\n') *p-- = 0;
546         p = buffer;
547         while (*p == ' ') p++;
548         if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
549         if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
550         add_name( &undef_symbols, p );
551     }
552     if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
553     free( cmd );
554 }
555
556 /* resolve the imports for a Win32 module */
557 int resolve_imports( DLLSPEC *spec )
558 {
559     unsigned int i, j, removed;
560     ORDDEF *odp;
561
562     sort_names( &ignore_symbols );
563
564     for (i = 0; i < nb_imports; i++)
565     {
566         struct import *imp = dll_imports[i];
567
568         for (j = removed = 0; j < undef_symbols.count; j++)
569         {
570             if (find_name( undef_symbols.names[j], &ignore_symbols )) continue;
571             odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
572             if (odp)
573             {
574                 add_import_func( imp, odp );
575                 remove_name( &undef_symbols, j-- );
576                 removed++;
577             }
578         }
579         if (!removed && check_unused( imp, spec ))
580         {
581             /* the dll is not used, get rid of it */
582             warning( "%s imported but no symbols used\n", imp->spec->file_name );
583             remove_import_dll( i );
584             i--;
585         }
586     }
587
588     sort_names( &undef_symbols );
589     check_undefined_exports( spec );
590
591     return 1;
592 }
593
594 /* output the get_pc thunk if needed */
595 void output_get_pc_thunk( FILE *outfile )
596 {
597     if (target_cpu != CPU_x86) return;
598     if (!UsePIC) return;
599     fprintf( outfile, "\n\t.text\n" );
600     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
601     fprintf( outfile, "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
602     fprintf( outfile, "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
603     fprintf( outfile, "\tpopl %%eax\n" );
604     fprintf( outfile, "\tpushl %%eax\n" );
605     fprintf( outfile, "\tret\n" );
606     output_function_size( outfile, "__wine_spec_get_pc_thunk_eax" );
607 }
608
609 /* output a single import thunk */
610 static void output_import_thunk( FILE *outfile, const char *name, const char *table, int pos )
611 {
612     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
613     fprintf( outfile, "\t%s\n", func_declaration(name) );
614     fprintf( outfile, "%s\n", asm_globl(name) );
615
616     switch(target_cpu)
617     {
618     case CPU_x86:
619         if (!UsePIC)
620         {
621             fprintf( outfile, "\tjmp *(%s+%d)\n", table, pos );
622         }
623         else
624         {
625             fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
626             fprintf( outfile, "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
627         }
628         break;
629     case CPU_x86_64:
630         fprintf( outfile, "\tjmpq *%s+%d(%%rip)\n", table, pos );
631         break;
632     case CPU_SPARC:
633         if ( !UsePIC )
634         {
635             fprintf( outfile, "\tsethi %%hi(%s+%d), %%g1\n", table, pos );
636             fprintf( outfile, "\tld [%%g1+%%lo(%s+%d)], %%g1\n", table, pos );
637             fprintf( outfile, "\tjmp %%g1\n" );
638             fprintf( outfile, "\tnop\n" );
639         }
640         else
641         {
642             /* Hmpf.  Stupid sparc assembler always interprets global variable
643                names as GOT offsets, so we have to do it the long way ... */
644             fprintf( outfile, "\tsave %%sp, -96, %%sp\n" );
645             fprintf( outfile, "0:\tcall 1f\n" );
646             fprintf( outfile, "\tnop\n" );
647             fprintf( outfile, "1:\tsethi %%hi(%s+%d-0b), %%g1\n", table, pos );
648             fprintf( outfile, "\tor %%g1, %%lo(%s+%d-0b), %%g1\n", table, pos );
649             fprintf( outfile, "\tld [%%g1+%%o7], %%g1\n" );
650             fprintf( outfile, "\tjmp %%g1\n" );
651             fprintf( outfile, "\trestore\n" );
652         }
653         break;
654     case CPU_ALPHA:
655         fprintf( outfile, "\tlda $0,%s\n", table );
656         fprintf( outfile, "\tlda $0,%d($0)\n", pos );
657         fprintf( outfile, "\tjmp $31,($0)\n" );
658         break;
659     case CPU_POWERPC:
660         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
661         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(9), ppc_reg(1) );
662         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
663         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(8), ppc_reg(1) );
664         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
665         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(7), ppc_reg(1) );
666         if (target_platform == PLATFORM_APPLE)
667         {
668             fprintf( outfile, "\tlis %s, ha16(%s+%d)\n", ppc_reg(9), table, pos );
669             fprintf( outfile, "\tla  %s, lo16(%s+%d)(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
670         }
671         else
672         {
673             fprintf( outfile, "\tlis %s, (%s+%d)@h\n", ppc_reg(9), table, pos );
674             fprintf( outfile, "\tla  %s, (%s+%d)@l(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
675         }
676         fprintf( outfile, "\tlwz  %s, 0(%s)\n", ppc_reg(7), ppc_reg(8) );
677         fprintf( outfile, "\tmtctr %s\n", ppc_reg(7) );
678         fprintf( outfile, "\tlwz  %s, 0(%s)\n",   ppc_reg(7), ppc_reg(1) );
679         fprintf( outfile, "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
680         fprintf( outfile, "\tlwz  %s, 0(%s)\n",   ppc_reg(8), ppc_reg(1) );
681         fprintf( outfile, "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
682         fprintf( outfile, "\tlwz  %s, 0(%s)\n",   ppc_reg(9), ppc_reg(1) );
683         fprintf( outfile, "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
684         fprintf( outfile, "\tbctr\n" );
685         break;
686     }
687     output_function_size( outfile, name );
688 }
689
690 /* check if we need an import directory */
691 int has_imports(void)
692 {
693     return (nb_imports - nb_delayed) > 0;
694 }
695
696 /* output the import table of a Win32 module */
697 static void output_immediate_imports( FILE *outfile )
698 {
699     int i, j;
700     const char *dll_name;
701
702     if (nb_imports == nb_delayed) return;  /* no immediate imports */
703
704     /* main import header */
705
706     fprintf( outfile, "\n/* import table */\n" );
707     fprintf( outfile, "\n\t.data\n" );
708     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
709     fprintf( outfile, ".L__wine_spec_imports:\n" );
710
711     /* list of dlls */
712
713     for (i = j = 0; i < nb_imports; i++)
714     {
715         if (dll_imports[i]->delay) continue;
716         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
717         fprintf( outfile, "\t.long 0\n" );     /* OriginalFirstThunk */
718         fprintf( outfile, "\t.long 0\n" );     /* TimeDateStamp */
719         fprintf( outfile, "\t.long 0\n" );     /* ForwarderChain */
720         fprintf( outfile, "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
721                  dll_name );
722         fprintf( outfile, "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n",  /* FirstThunk */
723                  j * get_ptr_size() );
724         j += dll_imports[i]->nb_imports + 1;
725     }
726     fprintf( outfile, "\t.long 0\n" );     /* OriginalFirstThunk */
727     fprintf( outfile, "\t.long 0\n" );     /* TimeDateStamp */
728     fprintf( outfile, "\t.long 0\n" );     /* ForwarderChain */
729     fprintf( outfile, "\t.long 0\n" );     /* Name */
730     fprintf( outfile, "\t.long 0\n" );     /* FirstThunk */
731
732     fprintf( outfile, "\n\t.align %d\n", get_alignment(get_ptr_size()) );
733     fprintf( outfile, ".L__wine_spec_import_data_ptrs:\n" );
734     for (i = 0; i < nb_imports; i++)
735     {
736         if (dll_imports[i]->delay) continue;
737         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
738         for (j = 0; j < dll_imports[i]->nb_imports; j++)
739         {
740             ORDDEF *odp = dll_imports[i]->imports[j];
741             if (!(odp->flags & FLAG_NONAME))
742                 fprintf( outfile, "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
743                          get_asm_ptr_keyword(), dll_name, odp->name );
744             else
745             {
746                 if (get_ptr_size() == 8)
747                     fprintf( outfile, "\t.quad 0x800000000000%04x\n", odp->ordinal );
748                 else
749                     fprintf( outfile, "\t.long 0x8000%04x\n", odp->ordinal );
750             }
751         }
752         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );
753     }
754     fprintf( outfile, ".L__wine_spec_imports_end:\n" );
755
756     for (i = 0; i < nb_imports; i++)
757     {
758         if (dll_imports[i]->delay) continue;
759         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
760         for (j = 0; j < dll_imports[i]->nb_imports; j++)
761         {
762             ORDDEF *odp = dll_imports[i]->imports[j];
763             if (!(odp->flags & FLAG_NONAME))
764             {
765                 fprintf( outfile, "\t.align %d\n", get_alignment(2) );
766                 fprintf( outfile, ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name );
767                 fprintf( outfile, "\t%s %d\n", get_asm_short_keyword(), odp->ordinal );
768                 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
769             }
770         }
771     }
772
773     for (i = 0; i < nb_imports; i++)
774     {
775         if (dll_imports[i]->delay) continue;
776         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
777         fprintf( outfile, ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
778                  dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name );
779     }
780 }
781
782 /* output the import thunks of a Win32 module */
783 static void output_immediate_import_thunks( FILE *outfile )
784 {
785     int i, j, pos;
786     int nb_imm = nb_imports - nb_delayed;
787     static const char import_thunks[] = "__wine_spec_import_thunks";
788
789     if (!nb_imm) return;
790
791     fprintf( outfile, "\n/* immediate import thunks */\n\n" );
792     fprintf( outfile, "\t.text\n" );
793     fprintf( outfile, "\t.align %d\n", get_alignment(8) );
794     fprintf( outfile, "%s:\n", asm_name(import_thunks));
795
796     for (i = pos = 0; i < nb_imports; i++)
797     {
798         if (dll_imports[i]->delay) continue;
799         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
800         {
801             ORDDEF *odp = dll_imports[i]->imports[j];
802             output_import_thunk( outfile, odp->name ? odp->name : odp->export_name,
803                                  ".L__wine_spec_import_data_ptrs", pos );
804         }
805         pos += get_ptr_size();
806     }
807     output_function_size( outfile, import_thunks );
808 }
809
810 /* output the delayed import table of a Win32 module */
811 static void output_delayed_imports( FILE *outfile, const DLLSPEC *spec )
812 {
813     int i, j, mod;
814
815     if (!nb_delayed) return;
816
817     fprintf( outfile, "\n/* delayed imports */\n\n" );
818     fprintf( outfile, "\t.data\n" );
819     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
820     fprintf( outfile, "%s\n", asm_globl("__wine_spec_delay_imports") );
821
822     /* list of dlls */
823
824     for (i = j = mod = 0; i < nb_imports; i++)
825     {
826         if (!dll_imports[i]->delay) continue;
827         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* grAttrs */
828         fprintf( outfile, "\t%s .L__wine_delay_name_%d\n",       /* szName */
829                  get_asm_ptr_keyword(), i );
830         fprintf( outfile, "\t%s .L__wine_delay_modules+%d\n",    /* phmod */
831                  get_asm_ptr_keyword(), mod * get_ptr_size() );
832         fprintf( outfile, "\t%s .L__wine_delay_IAT+%d\n",        /* pIAT */
833                  get_asm_ptr_keyword(), j * get_ptr_size() );
834         fprintf( outfile, "\t%s .L__wine_delay_INT+%d\n",        /* pINT */
835                  get_asm_ptr_keyword(), j * get_ptr_size() );
836         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pBoundIAT */
837         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pUnloadIAT */
838         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* dwTimeStamp */
839         j += dll_imports[i]->nb_imports;
840         mod++;
841     }
842     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* grAttrs */
843     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* szName */
844     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* phmod */
845     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pIAT */
846     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pINT */
847     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pBoundIAT */
848     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pUnloadIAT */
849     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* dwTimeStamp */
850
851     fprintf( outfile, "\n.L__wine_delay_IAT:\n" );
852     for (i = 0; i < nb_imports; i++)
853     {
854         if (!dll_imports[i]->delay) continue;
855         for (j = 0; j < dll_imports[i]->nb_imports; j++)
856         {
857             ORDDEF *odp = dll_imports[i]->imports[j];
858             const char *name = odp->name ? odp->name : odp->export_name;
859             fprintf( outfile, "\t%s .L__wine_delay_imp_%d_%s\n",
860                      get_asm_ptr_keyword(), i, name );
861         }
862     }
863
864     fprintf( outfile, "\n.L__wine_delay_INT:\n" );
865     for (i = 0; i < nb_imports; i++)
866     {
867         if (!dll_imports[i]->delay) continue;
868         for (j = 0; j < dll_imports[i]->nb_imports; j++)
869         {
870             ORDDEF *odp = dll_imports[i]->imports[j];
871             if (!odp->name)
872                 fprintf( outfile, "\t%s %d\n", get_asm_ptr_keyword(), odp->ordinal );
873             else
874                 fprintf( outfile, "\t%s .L__wine_delay_data_%d_%s\n",
875                          get_asm_ptr_keyword(), i, odp->name );
876         }
877     }
878
879     fprintf( outfile, "\n.L__wine_delay_modules:\n" );
880     for (i = 0; i < nb_imports; i++)
881     {
882         if (dll_imports[i]->delay) fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );
883     }
884
885     for (i = 0; i < nb_imports; i++)
886     {
887         if (!dll_imports[i]->delay) continue;
888         fprintf( outfile, ".L__wine_delay_name_%d:\n", i );
889         fprintf( outfile, "\t%s \"%s\"\n",
890                  get_asm_string_keyword(), dll_imports[i]->spec->file_name );
891     }
892
893     for (i = 0; i < nb_imports; i++)
894     {
895         if (!dll_imports[i]->delay) continue;
896         for (j = 0; j < dll_imports[i]->nb_imports; j++)
897         {
898             ORDDEF *odp = dll_imports[i]->imports[j];
899             if (!odp->name) continue;
900             fprintf( outfile, ".L__wine_delay_data_%d_%s:\n", i, odp->name );
901             fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
902         }
903     }
904     output_function_size( outfile, "__wine_spec_delay_imports" );
905 }
906
907 /* output the delayed import thunks of a Win32 module */
908 static void output_delayed_import_thunks( FILE *outfile, const DLLSPEC *spec )
909 {
910     int i, idx, j, pos, extra_stack_storage = 0;
911     static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
912     static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
913
914     if (!nb_delayed) return;
915
916     fprintf( outfile, "\n/* delayed import thunks */\n\n" );
917     fprintf( outfile, "\t.text\n" );
918     fprintf( outfile, "\t.align %d\n", get_alignment(8) );
919     fprintf( outfile, "%s:\n", asm_name(delayed_import_loaders));
920     fprintf( outfile, "\t%s\n", func_declaration("__wine_delay_load_asm") );
921     fprintf( outfile, "%s:\n", asm_name("__wine_delay_load_asm") );
922     switch(target_cpu)
923     {
924     case CPU_x86:
925         fprintf( outfile, "\tpushl %%ecx\n" );
926         fprintf( outfile, "\tpushl %%edx\n" );
927         fprintf( outfile, "\tpushl %%eax\n" );
928         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
929         fprintf( outfile, "\tpopl %%edx\n" );
930         fprintf( outfile, "\tpopl %%ecx\n" );
931         fprintf( outfile, "\tjmp *%%eax\n" );
932         break;
933     case CPU_x86_64:
934         fprintf( outfile, "\tpushq %%rdi\n" );
935         fprintf( outfile, "\tsubq $8,%%rsp\n" );
936         fprintf( outfile, "\tmovq %%r11,%%rdi\n" );
937         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
938         fprintf( outfile, "\taddq $8,%%rsp\n" );
939         fprintf( outfile, "\tpopq %%rdi\n" );
940         fprintf( outfile, "\tjmp *%%rax\n" );
941         break;
942     case CPU_SPARC:
943         fprintf( outfile, "\tsave %%sp, -96, %%sp\n" );
944         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
945         fprintf( outfile, "\tmov %%g1, %%o0\n" );
946         fprintf( outfile, "\tjmp %%o0\n" );
947         fprintf( outfile, "\trestore\n" );
948         break;
949     case CPU_ALPHA:
950         fprintf( outfile, "\tjsr $26,%s\n", asm_name("__wine_spec_delay_load") );
951         fprintf( outfile, "\tjmp $31,($0)\n" );
952         break;
953     case CPU_POWERPC:
954         if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
955
956         /* Save all callee saved registers into a stackframe. */
957         fprintf( outfile, "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
958         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(3),  4+extra_stack_storage, ppc_reg(1));
959         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(4),  8+extra_stack_storage, ppc_reg(1));
960         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
961         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
962         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
963         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
964         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
965         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
966         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
967         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
968
969         /* r0 -> r3 (arg1) */
970         fprintf( outfile, "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
971
972         /* save return address */
973         fprintf( outfile, "\tmflr %s\n", ppc_reg(0));
974         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
975
976         /* Call the __wine_delay_load function, arg1 is arg1. */
977         fprintf( outfile, "\tbl %s\n", asm_name("__wine_spec_delay_load") );
978
979         /* Load return value from call into ctr register */
980         fprintf( outfile, "\tmtctr %s\n", ppc_reg(3));
981
982         /* restore all saved registers and drop stackframe. */
983         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(3),  4+extra_stack_storage, ppc_reg(1));
984         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(4),  8+extra_stack_storage, ppc_reg(1));
985         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
986         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
987         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
988         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
989         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
990         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
991         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
992         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
993
994         /* Load return value from call into return register */
995         fprintf( outfile, "\tlwz  %s,  %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
996         fprintf( outfile, "\tmtlr %s\n", ppc_reg(0));
997         fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1),  48+extra_stack_storage);
998
999         /* branch to ctr register. */
1000         fprintf( outfile, "\tbctr\n");
1001         break;
1002     }
1003     output_function_size( outfile, "__wine_delay_load_asm" );
1004     fprintf( outfile, "\n" );
1005
1006     for (i = idx = 0; i < nb_imports; i++)
1007     {
1008         if (!dll_imports[i]->delay) continue;
1009         for (j = 0; j < dll_imports[i]->nb_imports; j++)
1010         {
1011             ORDDEF *odp = dll_imports[i]->imports[j];
1012             const char *name = odp->name ? odp->name : odp->export_name;
1013
1014             fprintf( outfile, ".L__wine_delay_imp_%d_%s:\n", i, name );
1015             switch(target_cpu)
1016             {
1017             case CPU_x86:
1018                 fprintf( outfile, "\tmovl $%d, %%eax\n", (idx << 16) | j );
1019                 fprintf( outfile, "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1020                 break;
1021             case CPU_x86_64:
1022                 fprintf( outfile, "\tmovq $%d,%%r11\n", (idx << 16) | j );
1023                 fprintf( outfile, "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1024                 break;
1025             case CPU_SPARC:
1026                 fprintf( outfile, "\tset %d, %%g1\n", (idx << 16) | j );
1027                 fprintf( outfile, "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1028                 break;
1029             case CPU_ALPHA:
1030                 fprintf( outfile, "\tlda $0,%d($31)\n", j);
1031                 fprintf( outfile, "\tldah $0,%d($0)\n", idx);
1032                 fprintf( outfile, "\tjmp $31,%s\n", asm_name("__wine_delay_load_asm") );
1033                 break;
1034             case CPU_POWERPC:
1035                 switch(target_platform)
1036                 {
1037                 case PLATFORM_APPLE:
1038                     /* On Darwin we can use r0 and r2 */
1039                     /* Upper part in r2 */
1040                     fprintf( outfile, "\tlis %s, %d\n", ppc_reg(2), idx);
1041                     /* Lower part + r2 -> r0, Note we can't use r0 directly */
1042                     fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1043                     fprintf( outfile, "\tb %s\n", asm_name("__wine_delay_load_asm") );
1044                     break;
1045                 default:
1046                     /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1047                     /* Save r13 on the stack */
1048                     fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1049                     fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(13), ppc_reg(1));
1050                     /* Upper part in r13 */
1051                     fprintf( outfile, "\tlis %s, %d\n", ppc_reg(13), idx);
1052                     /* Lower part + r13 -> r0, Note we can't use r0 directly */
1053                     fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1054                     /* Restore r13 */
1055                     fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(13), ppc_reg(1));
1056                     fprintf( outfile, "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1057                     fprintf( outfile, "\tb %s\n", asm_name("__wine_delay_load_asm") );
1058                     break;
1059                 }
1060                 break;
1061             }
1062         }
1063         idx++;
1064     }
1065     output_function_size( outfile, delayed_import_loaders );
1066
1067     fprintf( outfile, "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1068     fprintf( outfile, "%s:\n", asm_name(delayed_import_thunks));
1069     for (i = pos = 0; i < nb_imports; i++)
1070     {
1071         if (!dll_imports[i]->delay) continue;
1072         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
1073         {
1074             ORDDEF *odp = dll_imports[i]->imports[j];
1075             output_import_thunk( outfile, odp->name ? odp->name : odp->export_name,
1076                                  ".L__wine_delay_IAT", pos );
1077         }
1078     }
1079     output_function_size( outfile, delayed_import_thunks );
1080 }
1081
1082 /* output import stubs for exported entry points that link to external symbols */
1083 static void output_external_link_imports( FILE *outfile, DLLSPEC *spec )
1084 {
1085     unsigned int i, pos;
1086
1087     if (!ext_link_imports.count) return;  /* nothing to do */
1088
1089     sort_names( &ext_link_imports );
1090
1091     /* get rid of duplicate names */
1092     for (i = 1; i < ext_link_imports.count; i++)
1093     {
1094         if (!strcmp( ext_link_imports.names[i-1], ext_link_imports.names[i] ))
1095             remove_name( &ext_link_imports, i-- );
1096     }
1097
1098     fprintf( outfile, "\n/* external link thunks */\n\n" );
1099     fprintf( outfile, "\t.data\n" );
1100     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
1101     fprintf( outfile, ".L__wine_spec_external_links:\n" );
1102     for (i = 0; i < ext_link_imports.count; i++)
1103         fprintf( outfile, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.names[i]) );
1104
1105     fprintf( outfile, "\n\t.text\n" );
1106     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
1107     fprintf( outfile, "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1108
1109     for (i = pos = 0; i < ext_link_imports.count; i++)
1110     {
1111         char buffer[256];
1112         sprintf( buffer, "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
1113         output_import_thunk( outfile, buffer, ".L__wine_spec_external_links", pos );
1114         pos += get_ptr_size();
1115     }
1116     output_function_size( outfile, "__wine_spec_external_link_thunks" );
1117 }
1118
1119 /*******************************************************************
1120  *         output_stubs
1121  *
1122  * Output the functions for stub entry points
1123  */
1124 void output_stubs( FILE *outfile, DLLSPEC *spec )
1125 {
1126     const char *name, *exp_name;
1127     int i, pos;
1128
1129     if (!has_stubs( spec )) return;
1130
1131     fprintf( outfile, "\n/* stub functions */\n\n" );
1132     fprintf( outfile, "\t.text\n" );
1133
1134     for (i = pos = 0; i < spec->nb_entry_points; i++)
1135     {
1136         ORDDEF *odp = &spec->entry_points[i];
1137         if (odp->type != TYPE_STUB) continue;
1138
1139         name = get_stub_name( odp, spec );
1140         exp_name = odp->name ? odp->name : odp->export_name;
1141         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
1142         fprintf( outfile, "\t%s\n", func_declaration(name) );
1143         fprintf( outfile, "%s:\n", asm_name(name) );
1144         fprintf( outfile, "\tsubl $4,%%esp\n" );
1145
1146         if (UsePIC)
1147         {
1148             fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1149             fprintf( outfile, "1:" );
1150             if (exp_name)
1151             {
1152                 fprintf( outfile, "\tleal .L__wine_stub_strings+%d-1b(%%eax),%%ecx\n", pos );
1153                 fprintf( outfile, "\tpushl %%ecx\n" );
1154                 pos += strlen(exp_name) + 1;
1155             }
1156             else
1157                 fprintf( outfile, "\tpushl $%d\n", odp->ordinal );
1158             fprintf( outfile, "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1159             fprintf( outfile, "\tpushl %%ecx\n" );
1160         }
1161         else
1162         {
1163             if (exp_name)
1164             {
1165                 fprintf( outfile, "\tpushl $.L__wine_stub_strings+%d\n", pos );
1166                 pos += strlen(exp_name) + 1;
1167             }
1168             else
1169                 fprintf( outfile, "\tpushl $%d\n", odp->ordinal );
1170             fprintf( outfile, "\tpushl $.L__wine_spec_file_name\n" );
1171         }
1172         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1173         output_function_size( outfile, name );
1174     }
1175
1176     if (pos)
1177     {
1178         fprintf( outfile, "\t%s\n", get_asm_string_section() );
1179         fprintf( outfile, ".L__wine_stub_strings:\n" );
1180         for (i = 0; i < spec->nb_entry_points; i++)
1181         {
1182             ORDDEF *odp = &spec->entry_points[i];
1183             if (odp->type != TYPE_STUB) continue;
1184             exp_name = odp->name ? odp->name : odp->export_name;
1185             if (exp_name)
1186                 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
1187         }
1188     }
1189 }
1190
1191 /* output the import and delayed import tables of a Win32 module */
1192 void output_imports( FILE *outfile, DLLSPEC *spec )
1193 {
1194     output_immediate_imports( outfile );
1195     output_delayed_imports( outfile, spec );
1196     output_immediate_import_thunks( outfile );
1197     output_delayed_import_thunks( outfile, spec );
1198     output_external_link_imports( outfile, spec );
1199     if (nb_imports || ext_link_imports.count || has_stubs(spec) || has_relays(spec))
1200         output_get_pc_thunk( outfile );
1201 }