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