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