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