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