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