comctl32: A couple fixes for tab icon offsets.
[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             odp->flags |= FLAG_EXT_LINK;
444             add_name( &ext_link_imports, odp->link_name );
445         }
446     }
447 }
448
449 /* create a .o file that references all the undefined symbols we want to resolve */
450 static char *create_undef_symbols_file( DLLSPEC *spec )
451 {
452     char *as_file, *obj_file;
453     unsigned int i;
454     FILE *f;
455
456     as_file = get_temp_file_name( output_file_name, ".s" );
457     if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
458     fprintf( f, "\t.data\n" );
459
460     for (i = 0; i < spec->nb_entry_points; i++)
461     {
462         ORDDEF *odp = &spec->entry_points[i];
463         if (odp->type == TYPE_STUB) continue;
464         if (odp->flags & FLAG_FORWARD) continue;
465         fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
466     }
467     for (i = 0; i < extra_ld_symbols.count; i++)
468         fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[i]) );
469     fclose( f );
470
471     obj_file = get_temp_file_name( output_file_name, ".o" );
472     assemble_file( as_file, obj_file );
473     return obj_file;
474 }
475
476 /* combine a list of object files with ld into a single object file */
477 /* returns the name of the combined file */
478 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
479 {
480     unsigned int i, len = 0;
481     char *cmd, *p, *ld_tmp_file, *undef_file;
482     int err;
483
484     undef_file = create_undef_symbols_file( spec );
485     len += strlen(undef_file) + 1;
486     ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
487     if (!ld_command) ld_command = xstrdup("ld");
488     for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
489     cmd = p = xmalloc( len + strlen(ld_tmp_file) + 8 + strlen(ld_command)  );
490     p += sprintf( cmd, "%s -r -o %s %s", ld_command, ld_tmp_file, undef_file );
491     for (i = 0; argv[i]; i++)
492         p += sprintf( p, " %s", argv[i] );
493     if (verbose) fprintf( stderr, "%s\n", cmd );
494     err = system( cmd );
495     if (err) fatal_error( "%s -r failed with status %d\n", ld_command, err );
496     free( cmd );
497     return ld_tmp_file;
498 }
499
500 /* read in the list of undefined symbols */
501 void read_undef_symbols( DLLSPEC *spec, char **argv )
502 {
503     size_t prefix_len;
504     FILE *f;
505     char *cmd, buffer[1024], name_prefix[16];
506     int err;
507     const char *name;
508
509     if (!argv[0]) return;
510
511     add_extra_undef_symbols( spec );
512
513     strcpy( name_prefix, asm_name("") );
514     prefix_len = strlen( name_prefix );
515
516     name = ldcombine_files( spec, argv );
517
518     if (!nm_command) nm_command = xstrdup("nm");
519     cmd = xmalloc( strlen(nm_command) + strlen(name) + 5 );
520     sprintf( cmd, "%s -u %s", nm_command, name );
521     if (!(f = popen( cmd, "r" )))
522         fatal_error( "Cannot execute '%s'\n", cmd );
523
524     while (fgets( buffer, sizeof(buffer), f ))
525     {
526         char *p = buffer + strlen(buffer) - 1;
527         if (p < buffer) continue;
528         if (*p == '\n') *p-- = 0;
529         p = buffer;
530         while (*p == ' ') p++;
531         if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
532         if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
533         add_name( &undef_symbols, p );
534     }
535     if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
536     free( cmd );
537 }
538
539 /* resolve the imports for a Win32 module */
540 int resolve_imports( DLLSPEC *spec )
541 {
542     unsigned int i, j, removed;
543     ORDDEF *odp;
544
545     sort_names( &ignore_symbols );
546
547     for (i = 0; i < nb_imports; i++)
548     {
549         struct import *imp = dll_imports[i];
550
551         for (j = removed = 0; j < undef_symbols.count; j++)
552         {
553             if (find_name( undef_symbols.names[j], &ignore_symbols )) continue;
554             odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
555             if (odp)
556             {
557                 add_import_func( imp, odp );
558                 remove_name( &undef_symbols, j-- );
559                 removed++;
560             }
561         }
562         if (!removed && check_unused( imp, spec ))
563         {
564             /* the dll is not used, get rid of it */
565             warning( "%s imported but no symbols used\n", imp->spec->file_name );
566             remove_import_dll( i );
567             i--;
568         }
569     }
570
571     sort_names( &undef_symbols );
572     check_undefined_exports( spec );
573
574     return 1;
575 }
576
577 /* output the get_pc thunk if needed */
578 void output_get_pc_thunk( FILE *outfile )
579 {
580     if (target_cpu != CPU_x86) return;
581     if (!UsePIC) return;
582     fprintf( outfile, "\n\t.text\n" );
583     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
584     fprintf( outfile, "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
585     fprintf( outfile, "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
586     fprintf( outfile, "\tpopl %%eax\n" );
587     fprintf( outfile, "\tpushl %%eax\n" );
588     fprintf( outfile, "\tret\n" );
589     output_function_size( outfile, "__wine_spec_get_pc_thunk_eax" );
590 }
591
592 /* output a single import thunk */
593 static void output_import_thunk( FILE *outfile, const char *name, const char *table, int pos )
594 {
595     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
596     fprintf( outfile, "\t%s\n", func_declaration(name) );
597     fprintf( outfile, "%s\n", asm_globl(name) );
598
599     switch(target_cpu)
600     {
601     case CPU_x86:
602         if (!UsePIC)
603         {
604             fprintf( outfile, "\tjmp *(%s+%d)\n", table, pos );
605         }
606         else
607         {
608             fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
609             fprintf( outfile, "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
610         }
611         break;
612     case CPU_x86_64:
613         fprintf( outfile, "\tjmpq *%s+%d(%%rip)\n", table, pos );
614         break;
615     case CPU_SPARC:
616         if ( !UsePIC )
617         {
618             fprintf( outfile, "\tsethi %%hi(%s+%d), %%g1\n", table, pos );
619             fprintf( outfile, "\tld [%%g1+%%lo(%s+%d)], %%g1\n", table, pos );
620             fprintf( outfile, "\tjmp %%g1\n" );
621             fprintf( outfile, "\tnop\n" );
622         }
623         else
624         {
625             /* Hmpf.  Stupid sparc assembler always interprets global variable
626                names as GOT offsets, so we have to do it the long way ... */
627             fprintf( outfile, "\tsave %%sp, -96, %%sp\n" );
628             fprintf( outfile, "0:\tcall 1f\n" );
629             fprintf( outfile, "\tnop\n" );
630             fprintf( outfile, "1:\tsethi %%hi(%s+%d-0b), %%g1\n", table, pos );
631             fprintf( outfile, "\tor %%g1, %%lo(%s+%d-0b), %%g1\n", table, pos );
632             fprintf( outfile, "\tld [%%g1+%%o7], %%g1\n" );
633             fprintf( outfile, "\tjmp %%g1\n" );
634             fprintf( outfile, "\trestore\n" );
635         }
636         break;
637     case CPU_ALPHA:
638         fprintf( outfile, "\tlda $0,%s\n", table );
639         fprintf( outfile, "\tlda $0,%d($0)\n", pos );
640         fprintf( outfile, "\tjmp $31,($0)\n" );
641         break;
642     case CPU_POWERPC:
643         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
644         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(9), ppc_reg(1) );
645         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
646         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(8), ppc_reg(1) );
647         fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
648         fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(7), ppc_reg(1) );
649         if (target_platform == PLATFORM_APPLE)
650         {
651             fprintf( outfile, "\tlis %s, ha16(%s+%d)\n", ppc_reg(9), table, pos );
652             fprintf( outfile, "\tla  %s, lo16(%s+%d)(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
653         }
654         else
655         {
656             fprintf( outfile, "\tlis %s, (%s+%d)@h\n", ppc_reg(9), table, pos );
657             fprintf( outfile, "\tla  %s, (%s+%d)@l(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
658         }
659         fprintf( outfile, "\tlwz  %s, 0(%s)\n", ppc_reg(7), ppc_reg(8) );
660         fprintf( outfile, "\tmtctr %s\n", ppc_reg(7) );
661         fprintf( outfile, "\tlwz  %s, 0(%s)\n",   ppc_reg(7), ppc_reg(1) );
662         fprintf( outfile, "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
663         fprintf( outfile, "\tlwz  %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, "\tlwz  %s, 0(%s)\n",   ppc_reg(9), ppc_reg(1) );
666         fprintf( outfile, "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
667         fprintf( outfile, "\tbctr\n" );
668         break;
669     }
670     output_function_size( outfile, name );
671 }
672
673 /* check if we need an import directory */
674 int has_imports(void)
675 {
676     return (nb_imports - nb_delayed) > 0;
677 }
678
679 /* output the import table of a Win32 module */
680 static void output_immediate_imports( FILE *outfile )
681 {
682     int i, j;
683     const char *dll_name;
684
685     if (nb_imports == nb_delayed) return;  /* no immediate imports */
686
687     /* main import header */
688
689     fprintf( outfile, "\n/* import table */\n" );
690     fprintf( outfile, "\n\t.data\n" );
691     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
692     fprintf( outfile, ".L__wine_spec_imports:\n" );
693
694     /* list of dlls */
695
696     for (i = j = 0; i < nb_imports; i++)
697     {
698         if (dll_imports[i]->delay) continue;
699         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
700         fprintf( outfile, "\t.long 0\n" );     /* OriginalFirstThunk */
701         fprintf( outfile, "\t.long 0\n" );     /* TimeDateStamp */
702         fprintf( outfile, "\t.long 0\n" );     /* ForwarderChain */
703         fprintf( outfile, "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
704                  dll_name );
705         fprintf( outfile, "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n",  /* FirstThunk */
706                  j * get_ptr_size() );
707         j += dll_imports[i]->nb_imports + 1;
708     }
709     fprintf( outfile, "\t.long 0\n" );     /* OriginalFirstThunk */
710     fprintf( outfile, "\t.long 0\n" );     /* TimeDateStamp */
711     fprintf( outfile, "\t.long 0\n" );     /* ForwarderChain */
712     fprintf( outfile, "\t.long 0\n" );     /* Name */
713     fprintf( outfile, "\t.long 0\n" );     /* FirstThunk */
714
715     fprintf( outfile, "\n\t.align %d\n", get_alignment(get_ptr_size()) );
716     fprintf( outfile, ".L__wine_spec_import_data_ptrs:\n" );
717     for (i = 0; i < nb_imports; i++)
718     {
719         if (dll_imports[i]->delay) continue;
720         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
721         for (j = 0; j < dll_imports[i]->nb_imports; j++)
722         {
723             ORDDEF *odp = dll_imports[i]->imports[j];
724             if (!(odp->flags & FLAG_NONAME))
725                 fprintf( outfile, "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
726                          get_asm_ptr_keyword(), dll_name, odp->name );
727             else
728             {
729                 if (get_ptr_size() == 8)
730                     fprintf( outfile, "\t.quad 0x800000000000%04x\n", odp->ordinal );
731                 else
732                     fprintf( outfile, "\t.long 0x8000%04x\n", odp->ordinal );
733             }
734         }
735         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );
736     }
737     fprintf( outfile, ".L__wine_spec_imports_end:\n" );
738
739     for (i = 0; i < nb_imports; i++)
740     {
741         if (dll_imports[i]->delay) continue;
742         dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
743         for (j = 0; j < dll_imports[i]->nb_imports; j++)
744         {
745             ORDDEF *odp = dll_imports[i]->imports[j];
746             if (!(odp->flags & FLAG_NONAME))
747             {
748                 fprintf( outfile, "\t.align %d\n", get_alignment(2) );
749                 fprintf( outfile, ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name );
750                 fprintf( outfile, "\t%s %d\n", get_asm_short_keyword(), odp->ordinal );
751                 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
752             }
753         }
754     }
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         fprintf( outfile, ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
761                  dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name );
762     }
763 }
764
765 /* output the import thunks of a Win32 module */
766 static void output_immediate_import_thunks( FILE *outfile )
767 {
768     int i, j, pos;
769     int nb_imm = nb_imports - nb_delayed;
770     static const char import_thunks[] = "__wine_spec_import_thunks";
771
772     if (!nb_imm) return;
773
774     fprintf( outfile, "\n/* immediate import thunks */\n\n" );
775     fprintf( outfile, "\t.text\n" );
776     fprintf( outfile, "\t.align %d\n", get_alignment(8) );
777     fprintf( outfile, "%s:\n", asm_name(import_thunks));
778
779     for (i = pos = 0; i < nb_imports; i++)
780     {
781         if (dll_imports[i]->delay) continue;
782         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
783         {
784             ORDDEF *odp = dll_imports[i]->imports[j];
785             output_import_thunk( outfile, odp->name ? odp->name : odp->export_name,
786                                  ".L__wine_spec_import_data_ptrs", pos );
787         }
788         pos += get_ptr_size();
789     }
790     output_function_size( outfile, import_thunks );
791 }
792
793 /* output the delayed import table of a Win32 module */
794 static void output_delayed_imports( FILE *outfile, const DLLSPEC *spec )
795 {
796     int i, j;
797
798     if (!nb_delayed) return;
799
800     fprintf( outfile, "\n/* delayed imports */\n\n" );
801     fprintf( outfile, "\t.data\n" );
802     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
803     fprintf( outfile, "%s\n", asm_globl("__wine_spec_delay_imports") );
804
805     /* list of dlls */
806
807     for (i = j = 0; i < nb_imports; i++)
808     {
809         if (!dll_imports[i]->delay) continue;
810         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* grAttrs */
811         fprintf( outfile, "\t%s .L__wine_delay_name_%d\n",       /* szName */
812                  get_asm_ptr_keyword(), i );
813         fprintf( outfile, "\t%s .L__wine_delay_modules+%d\n",    /* phmod */
814                  get_asm_ptr_keyword(), i * get_ptr_size() );
815         fprintf( outfile, "\t%s .L__wine_delay_IAT+%d\n",        /* pIAT */
816                  get_asm_ptr_keyword(), j * get_ptr_size() );
817         fprintf( outfile, "\t%s .L__wine_delay_INT+%d\n",        /* pINT */
818                  get_asm_ptr_keyword(), j * get_ptr_size() );
819         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pBoundIAT */
820         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pUnloadIAT */
821         fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* dwTimeStamp */
822         j += dll_imports[i]->nb_imports;
823     }
824     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* grAttrs */
825     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* szName */
826     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* phmod */
827     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pIAT */
828     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pINT */
829     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pBoundIAT */
830     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* pUnloadIAT */
831     fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );   /* dwTimeStamp */
832
833     fprintf( outfile, "\n.L__wine_delay_IAT:\n" );
834     for (i = 0; i < nb_imports; i++)
835     {
836         if (!dll_imports[i]->delay) continue;
837         for (j = 0; j < dll_imports[i]->nb_imports; j++)
838         {
839             ORDDEF *odp = dll_imports[i]->imports[j];
840             const char *name = odp->name ? odp->name : odp->export_name;
841             fprintf( outfile, "\t%s .L__wine_delay_imp_%d_%s\n",
842                      get_asm_ptr_keyword(), i, name );
843         }
844     }
845
846     fprintf( outfile, "\n.L__wine_delay_INT:\n" );
847     for (i = 0; i < nb_imports; i++)
848     {
849         if (!dll_imports[i]->delay) continue;
850         for (j = 0; j < dll_imports[i]->nb_imports; j++)
851         {
852             ORDDEF *odp = dll_imports[i]->imports[j];
853             if (!odp->name)
854                 fprintf( outfile, "\t%s %d\n", get_asm_ptr_keyword(), odp->ordinal );
855             else
856                 fprintf( outfile, "\t%s .L__wine_delay_data_%d_%s\n",
857                          get_asm_ptr_keyword(), i, odp->name );
858         }
859     }
860
861     fprintf( outfile, "\n.L__wine_delay_modules:\n" );
862     for (i = 0; i < nb_imports; i++)
863     {
864         if (dll_imports[i]->delay) fprintf( outfile, "\t%s 0\n", get_asm_ptr_keyword() );
865     }
866
867     for (i = 0; i < nb_imports; i++)
868     {
869         if (!dll_imports[i]->delay) continue;
870         fprintf( outfile, ".L__wine_delay_name_%d:\n", i );
871         fprintf( outfile, "\t%s \"%s\"\n",
872                  get_asm_string_keyword(), dll_imports[i]->spec->file_name );
873     }
874
875     for (i = 0; i < nb_imports; i++)
876     {
877         if (!dll_imports[i]->delay) continue;
878         for (j = 0; j < dll_imports[i]->nb_imports; j++)
879         {
880             ORDDEF *odp = dll_imports[i]->imports[j];
881             if (!odp->name) continue;
882             fprintf( outfile, ".L__wine_delay_data_%d_%s:\n", i, odp->name );
883             fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
884         }
885     }
886     output_function_size( outfile, "__wine_spec_delay_imports" );
887 }
888
889 /* output the delayed import thunks of a Win32 module */
890 static void output_delayed_import_thunks( FILE *outfile, const DLLSPEC *spec )
891 {
892     int i, idx, j, pos, extra_stack_storage = 0;
893     static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
894     static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
895
896     if (!nb_delayed) return;
897
898     fprintf( outfile, "\n/* delayed import thunks */\n\n" );
899     fprintf( outfile, "\t.text\n" );
900     fprintf( outfile, "\t.align %d\n", get_alignment(8) );
901     fprintf( outfile, "%s:\n", asm_name(delayed_import_loaders));
902     fprintf( outfile, "\t%s\n", func_declaration("__wine_delay_load_asm") );
903     fprintf( outfile, "%s:\n", asm_name("__wine_delay_load_asm") );
904     switch(target_cpu)
905     {
906     case CPU_x86:
907         fprintf( outfile, "\tpushl %%ecx\n" );
908         fprintf( outfile, "\tpushl %%edx\n" );
909         fprintf( outfile, "\tpushl %%eax\n" );
910         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
911         fprintf( outfile, "\tpopl %%edx\n" );
912         fprintf( outfile, "\tpopl %%ecx\n" );
913         fprintf( outfile, "\tjmp *%%eax\n" );
914         break;
915     case CPU_x86_64:
916         fprintf( outfile, "\tpushq %%rdi\n" );
917         fprintf( outfile, "\tsubq $8,%%rsp\n" );
918         fprintf( outfile, "\tmovq %%r11,%%rdi\n" );
919         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
920         fprintf( outfile, "\taddq $8,%%rsp\n" );
921         fprintf( outfile, "\tpopq %%rdi\n" );
922         fprintf( outfile, "\tjmp *%%rax\n" );
923         break;
924     case CPU_SPARC:
925         fprintf( outfile, "\tsave %%sp, -96, %%sp\n" );
926         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_delay_load") );
927         fprintf( outfile, "\tmov %%g1, %%o0\n" );
928         fprintf( outfile, "\tjmp %%o0\n" );
929         fprintf( outfile, "\trestore\n" );
930         break;
931     case CPU_ALPHA:
932         fprintf( outfile, "\tjsr $26,%s\n", asm_name("__wine_spec_delay_load") );
933         fprintf( outfile, "\tjmp $31,($0)\n" );
934         break;
935     case CPU_POWERPC:
936         if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
937
938         /* Save all callee saved registers into a stackframe. */
939         fprintf( outfile, "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
940         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(3),  4+extra_stack_storage, ppc_reg(1));
941         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(4),  8+extra_stack_storage, ppc_reg(1));
942         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
943         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
944         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
945         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
946         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
947         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
948         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
949         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
950
951         /* r0 -> r3 (arg1) */
952         fprintf( outfile, "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
953
954         /* save return address */
955         fprintf( outfile, "\tmflr %s\n", ppc_reg(0));
956         fprintf( outfile, "\tstw  %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
957
958         /* Call the __wine_delay_load function, arg1 is arg1. */
959         fprintf( outfile, "\tbl %s\n", asm_name("__wine_spec_delay_load") );
960
961         /* Load return value from call into ctr register */
962         fprintf( outfile, "\tmtctr %s\n", ppc_reg(3));
963
964         /* restore all saved registers and drop stackframe. */
965         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(3),  4+extra_stack_storage, ppc_reg(1));
966         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(4),  8+extra_stack_storage, ppc_reg(1));
967         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
968         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
969         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
970         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
971         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
972         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
973         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
974         fprintf( outfile, "\tlwz  %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
975
976         /* Load return value from call into return register */
977         fprintf( outfile, "\tlwz  %s,  %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
978         fprintf( outfile, "\tmtlr %s\n", ppc_reg(0));
979         fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1),  48+extra_stack_storage);
980
981         /* branch to ctr register. */
982         fprintf( outfile, "\tbctr\n");
983         break;
984     }
985     output_function_size( outfile, "__wine_delay_load_asm" );
986     fprintf( outfile, "\n" );
987
988     for (i = idx = 0; i < nb_imports; i++)
989     {
990         if (!dll_imports[i]->delay) continue;
991         for (j = 0; j < dll_imports[i]->nb_imports; j++)
992         {
993             ORDDEF *odp = dll_imports[i]->imports[j];
994             const char *name = odp->name ? odp->name : odp->export_name;
995
996             fprintf( outfile, ".L__wine_delay_imp_%d_%s:\n", i, name );
997             switch(target_cpu)
998             {
999             case CPU_x86:
1000                 fprintf( outfile, "\tmovl $%d, %%eax\n", (idx << 16) | j );
1001                 fprintf( outfile, "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1002                 break;
1003             case CPU_x86_64:
1004                 fprintf( outfile, "\tmovq $%d,%%r11\n", (idx << 16) | j );
1005                 fprintf( outfile, "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1006                 break;
1007             case CPU_SPARC:
1008                 fprintf( outfile, "\tset %d, %%g1\n", (idx << 16) | j );
1009                 fprintf( outfile, "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1010                 break;
1011             case CPU_ALPHA:
1012                 fprintf( outfile, "\tlda $0,%d($31)\n", j);
1013                 fprintf( outfile, "\tldah $0,%d($0)\n", idx);
1014                 fprintf( outfile, "\tjmp $31,%s\n", asm_name("__wine_delay_load_asm") );
1015                 break;
1016             case CPU_POWERPC:
1017                 switch(target_platform)
1018                 {
1019                 case PLATFORM_APPLE:
1020                     /* On Darwin we can use r0 and r2 */
1021                     /* Upper part in r2 */
1022                     fprintf( outfile, "\tlis %s, %d\n", ppc_reg(2), idx);
1023                     /* Lower part + r2 -> r0, Note we can't use r0 directly */
1024                     fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1025                     fprintf( outfile, "\tb %s\n", asm_name("__wine_delay_load_asm") );
1026                     break;
1027                 default:
1028                     /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1029                     /* Save r13 on the stack */
1030                     fprintf( outfile, "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1031                     fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(13), ppc_reg(1));
1032                     /* Upper part in r13 */
1033                     fprintf( outfile, "\tlis %s, %d\n", ppc_reg(13), idx);
1034                     /* Lower part + r13 -> r0, Note we can't use r0 directly */
1035                     fprintf( outfile, "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1036                     /* Restore r13 */
1037                     fprintf( outfile, "\tstw  %s, 0(%s)\n",    ppc_reg(13), ppc_reg(1));
1038                     fprintf( outfile, "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1039                     fprintf( outfile, "\tb %s\n", asm_name("__wine_delay_load_asm") );
1040                     break;
1041                 }
1042                 break;
1043             }
1044         }
1045         idx++;
1046     }
1047     output_function_size( outfile, delayed_import_loaders );
1048
1049     fprintf( outfile, "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1050     fprintf( outfile, "%s:\n", asm_name(delayed_import_thunks));
1051     for (i = pos = 0; i < nb_imports; i++)
1052     {
1053         if (!dll_imports[i]->delay) continue;
1054         for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
1055         {
1056             ORDDEF *odp = dll_imports[i]->imports[j];
1057             output_import_thunk( outfile, odp->name ? odp->name : odp->export_name,
1058                                  ".L__wine_delay_IAT", pos );
1059         }
1060     }
1061     output_function_size( outfile, delayed_import_thunks );
1062 }
1063
1064 /* output import stubs for exported entry points that link to external symbols */
1065 static void output_external_link_imports( FILE *outfile, DLLSPEC *spec )
1066 {
1067     unsigned int i, pos;
1068
1069     if (!ext_link_imports.count) return;  /* nothing to do */
1070
1071     sort_names( &ext_link_imports );
1072
1073     /* get rid of duplicate names */
1074     for (i = 1; i < ext_link_imports.count; i++)
1075     {
1076         if (!strcmp( ext_link_imports.names[i-1], ext_link_imports.names[i] ))
1077             remove_name( &ext_link_imports, i-- );
1078     }
1079
1080     fprintf( outfile, "\n/* external link thunks */\n\n" );
1081     fprintf( outfile, "\t.data\n" );
1082     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
1083     fprintf( outfile, ".L__wine_spec_external_links:\n" );
1084     for (i = 0; i < ext_link_imports.count; i++)
1085         fprintf( outfile, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.names[i]) );
1086
1087     fprintf( outfile, "\n\t.text\n" );
1088     fprintf( outfile, "\t.align %d\n", get_alignment(get_ptr_size()) );
1089     fprintf( outfile, "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1090
1091     for (i = pos = 0; i < ext_link_imports.count; i++)
1092     {
1093         char buffer[256];
1094         sprintf( buffer, "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
1095         output_import_thunk( outfile, buffer, ".L__wine_spec_external_links", pos );
1096         pos += get_ptr_size();
1097     }
1098     output_function_size( outfile, "__wine_spec_external_link_thunks" );
1099 }
1100
1101 /*******************************************************************
1102  *         output_stubs
1103  *
1104  * Output the functions for stub entry points
1105  */
1106 void output_stubs( FILE *outfile, DLLSPEC *spec )
1107 {
1108     const char *name, *exp_name;
1109     int i, pos;
1110
1111     if (!has_stubs( spec )) return;
1112
1113     fprintf( outfile, "\n/* stub functions */\n\n" );
1114     fprintf( outfile, "\t.text\n" );
1115
1116     for (i = pos = 0; i < spec->nb_entry_points; i++)
1117     {
1118         ORDDEF *odp = &spec->entry_points[i];
1119         if (odp->type != TYPE_STUB) continue;
1120
1121         name = get_stub_name( odp, spec );
1122         exp_name = odp->name ? odp->name : odp->export_name;
1123         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
1124         fprintf( outfile, "\t%s\n", func_declaration(name) );
1125         fprintf( outfile, "%s:\n", asm_name(name) );
1126         fprintf( outfile, "\tsubl $4,%%esp\n" );
1127
1128         if (UsePIC)
1129         {
1130             fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1131             fprintf( outfile, "1:" );
1132             if (exp_name)
1133             {
1134                 fprintf( outfile, "\tleal .L__wine_stub_strings+%d-1b(%%eax),%%ecx\n", pos );
1135                 fprintf( outfile, "\tpushl %%ecx\n" );
1136                 pos += strlen(exp_name) + 1;
1137             }
1138             else
1139                 fprintf( outfile, "\tpushl $%d\n", odp->ordinal );
1140             fprintf( outfile, "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1141             fprintf( outfile, "\tpushl %%ecx\n" );
1142         }
1143         else
1144         {
1145             if (exp_name)
1146             {
1147                 fprintf( outfile, "\tpushl $.L__wine_stub_strings+%d\n", pos );
1148                 pos += strlen(exp_name) + 1;
1149             }
1150             else
1151                 fprintf( outfile, "\tpushl $%d\n", odp->ordinal );
1152             fprintf( outfile, "\tpushl $.L__wine_spec_file_name\n" );
1153         }
1154         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1155         output_function_size( outfile, name );
1156     }
1157
1158     if (pos)
1159     {
1160         fprintf( outfile, "\t%s\n", get_asm_string_section() );
1161         fprintf( outfile, ".L__wine_stub_strings:\n" );
1162         for (i = 0; i < spec->nb_entry_points; i++)
1163         {
1164             ORDDEF *odp = &spec->entry_points[i];
1165             if (odp->type != TYPE_STUB) continue;
1166             exp_name = odp->name ? odp->name : odp->export_name;
1167             if (exp_name)
1168                 fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
1169         }
1170     }
1171 }
1172
1173 /* output the import and delayed import tables of a Win32 module */
1174 void output_imports( FILE *outfile, DLLSPEC *spec )
1175 {
1176     output_immediate_imports( outfile );
1177     output_delayed_imports( outfile, spec );
1178     output_immediate_import_thunks( outfile );
1179     output_delayed_import_thunks( outfile, spec );
1180     output_external_link_imports( outfile, spec );
1181     if (nb_imports || ext_link_imports.count || has_stubs(spec) || has_relays(spec))
1182         output_get_pc_thunk( outfile );
1183 }