push 150847dede558b39567907bd3738046f0ea247b2
[wine/hacks.git] / tools / winebuild / import.c
blobda4b1db764397f594394a0249549fb3bfca95e29
1 /*
2 * DLL imports support
4 * Copyright 2000, 2004 Alexandre Julliard
5 * Copyright 2000 Eric Pouech
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
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
38 #include "windef.h"
39 #include "winbase.h"
40 #include "build.h"
42 struct import
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 */
55 struct name_table
57 char **names;
58 unsigned int count, size;
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 */
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 */
74 static inline const char *ppc_reg( int reg )
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' */
84 /* compare function names; helper for resolve_imports */
85 static int name_cmp( const void *name, const void *entry )
87 return strcmp( *(const char* const *)name, *(const char* const *)entry );
90 /* compare function names; helper for resolve_imports */
91 static int func_cmp( const void *func1, const void *func2 )
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 );
99 /* add a name to a name table */
100 static inline void add_name( struct name_table *table, const char *name )
102 if (table->count == table->size)
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) );
108 table->names[table->count++] = xstrdup( name );
111 /* remove a name from a name table */
112 static inline void remove_name( struct name_table *table, unsigned int idx )
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--;
121 /* make a name table empty */
122 static inline void empty_name_table( struct name_table *table )
124 unsigned int i;
126 for (i = 0; i < table->count; i++) free( table->names[i] );
127 table->count = 0;
130 /* locate a name in a (sorted) list */
131 static inline const char *find_name( const char *name, const struct name_table *table )
133 char **res = NULL;
135 if (table->count) res = bsearch( &name, table->names, table->count, sizeof(*table->names), name_cmp );
136 return res ? *res : NULL;
139 /* sort a name table */
140 static inline void sort_names( struct name_table *table )
142 if (table->count) qsort( table->names, table->count, sizeof(*table->names), name_cmp );
145 /* locate an export in a (sorted) export list */
146 static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
148 ORDDEF func, *odp, **res = NULL;
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;
158 /* free an import structure */
159 static void free_imports( struct import *imp )
161 free( imp->exports );
162 free( imp->imports );
163 free_dll_spec( imp->spec );
164 free( imp->full_name );
165 free( imp );
168 /* check whether a given dll is imported in delayed mode */
169 static int is_delayed_import( const char *name )
171 unsigned int i;
173 for (i = 0; i < delayed_imports.count; i++)
175 if (!strcmp( delayed_imports.names[i], name )) return 1;
177 return 0;
180 /* check whether a given dll has already been imported */
181 static struct import *is_already_imported( const char *name )
183 int i;
185 for (i = 0; i < nb_imports; i++)
187 if (!strcmp( dll_imports[i]->spec->file_name, name )) return dll_imports[i];
189 return NULL;
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 )
195 char *buffer;
196 int fd;
198 buffer = xmalloc( strlen(path) + strlen(name) + 9 );
199 sprintf( buffer, "%s/lib%s.def", path, name );
201 /* check if the file exists */
202 if ((fd = open( buffer, O_RDONLY )) != -1)
204 close( fd );
205 return buffer;
207 free( buffer );
208 return NULL;
211 /* find the .def import library for a given dll */
212 static char *find_library( const char *name )
214 char *fullname;
215 int i;
217 for (i = 0; i < nb_lib_paths; i++)
219 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
221 fatal_error( "could not open .def file for %s\n", name );
222 return NULL;
225 /* read in the list of exported symbols of an import library */
226 static int read_import_lib( struct import *imp )
228 FILE *f;
229 int i, ret;
230 struct stat stat;
231 struct import *prev_imp;
232 DLLSPEC *spec = imp->spec;
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;
242 /* check if we already imported that library from a different file */
243 if ((prev_imp = is_already_imported( spec->file_name )))
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 */
251 if (is_delayed_import( spec->file_name ))
253 imp->delay = 1;
254 nb_delayed++;
257 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
259 for (i = 0; i < spec->nb_entry_points; i++)
261 ORDDEF *odp = &spec->entry_points[i];
263 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL) continue;
264 if (odp->flags & FLAG_PRIVATE) continue;
265 imp->exports[imp->nb_exports++] = odp;
267 imp->exports = xrealloc( imp->exports, imp->nb_exports * sizeof(*imp->exports) );
268 if (imp->nb_exports)
269 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
270 return 1;
273 /* build the dll exported name from the import lib name or path */
274 static char *get_dll_name( const char *name, const char *filename )
276 char *ret;
278 if (filename)
280 const char *basename = strrchr( filename, '/' );
281 if (!basename) basename = filename;
282 else basename++;
283 if (!strncmp( basename, "lib", 3 )) basename += 3;
284 ret = xmalloc( strlen(basename) + 5 );
285 strcpy( ret, basename );
286 if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
288 else
290 ret = xmalloc( strlen(name) + 5 );
291 strcpy( ret, name );
293 if (!strchr( ret, '.' )) strcat( ret, ".dll" );
294 return ret;
297 /* add a dll to the list of imports */
298 void add_import_dll( const char *name, const char *filename )
300 struct import *imp = xmalloc( sizeof(*imp) );
302 imp->spec = alloc_dll_spec();
303 imp->spec->file_name = get_dll_name( name, filename );
304 imp->delay = 0;
305 imp->imports = NULL;
306 imp->nb_imports = 0;
307 imp->exports = NULL;
308 imp->nb_exports = 0;
310 if (filename) imp->full_name = xstrdup( filename );
311 else imp->full_name = find_library( name );
313 if (read_import_lib( imp ))
315 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
316 dll_imports[nb_imports++] = imp;
318 else
320 free_imports( imp );
321 if (nb_errors) exit(1);
325 /* add a library to the list of delayed imports */
326 void add_delayed_import( const char *name )
328 struct import *imp;
329 char *fullname = get_dll_name( name, NULL );
331 add_name( &delayed_imports, fullname );
332 if ((imp = is_already_imported( fullname )) && !imp->delay)
334 imp->delay = 1;
335 nb_delayed++;
337 free( fullname );
340 /* remove an imported dll, based on its index in the dll_imports array */
341 static void remove_import_dll( int index )
343 struct import *imp = dll_imports[index];
345 memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
346 nb_imports--;
347 if (imp->delay) nb_delayed--;
348 free_imports( imp );
351 /* add a symbol to the ignored symbol list */
352 /* if the name starts with '-' the symbol is removed instead */
353 void add_ignore_symbol( const char *name )
355 unsigned int i;
357 if (name[0] == '-') /* remove it */
359 if (!name[1]) empty_name_table( &ignore_symbols ); /* remove everything */
360 else for (i = 0; i < ignore_symbols.count; i++)
362 if (!strcmp( ignore_symbols.names[i], name+1 )) remove_name( &ignore_symbols, i-- );
365 else add_name( &ignore_symbols, name );
368 /* add a symbol to the list of extra symbols that ld must resolve */
369 void add_extra_ld_symbol( const char *name )
371 add_name( &extra_ld_symbols, name );
374 /* add a function to the list of imports from a given dll */
375 static void add_import_func( struct import *imp, ORDDEF *func )
377 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
378 imp->imports[imp->nb_imports++] = func;
379 total_imports++;
380 if (imp->delay) total_delayed++;
383 /* get the default entry point for a given spec file */
384 static const char *get_default_entry_point( const DLLSPEC *spec )
386 if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
387 if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
388 return "__wine_spec_exe_entry";
391 /* check if the spec file exports any stubs */
392 static int has_stubs( const DLLSPEC *spec )
394 int i;
395 for (i = 0; i < spec->nb_entry_points; i++)
397 ORDDEF *odp = &spec->entry_points[i];
398 if (odp->type == TYPE_STUB) return 1;
400 return 0;
403 /* add the extra undefined symbols that will be contained in the generated spec file itself */
404 static void add_extra_undef_symbols( DLLSPEC *spec )
406 if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point(spec) );
407 add_extra_ld_symbol( spec->init_func );
408 if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
409 if (nb_delayed) add_extra_ld_symbol( "__wine_spec_delay_load" );
412 /* check if a given imported dll is not needed, taking forwards into account */
413 static int check_unused( const struct import* imp, const DLLSPEC *spec )
415 int i;
416 const char *file_name = imp->spec->file_name;
417 size_t len = strlen( file_name );
418 const char *p = strchr( file_name, '.' );
419 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
421 for (i = spec->base; i <= spec->limit; i++)
423 ORDDEF *odp = spec->ordinals[i];
424 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
425 if (!strncasecmp( odp->link_name, file_name, len ) &&
426 odp->link_name[len] == '.')
427 return 0; /* found a forward, it is used */
429 return 1;
432 /* check if a given forward does exist in one of the imported dlls */
433 static void check_undefined_forward( DLLSPEC *spec, ORDDEF *odp )
435 char *link_name, *api_name, *dll_name, *p;
436 int i, found = 0;
438 assert( odp->flags & FLAG_FORWARD );
440 link_name = xstrdup( odp->link_name );
441 p = strrchr( link_name, '.' );
442 *p = 0;
443 api_name = p + 1;
444 dll_name = get_dll_name( link_name, NULL );
446 for (i = 0; i < nb_imports; i++)
448 struct import *imp = dll_imports[i];
450 if (!strcasecmp( imp->spec->file_name, dll_name ))
452 if (find_export( api_name, imp->exports, imp->nb_exports ))
454 found = 1;
455 break;
460 free( link_name );
461 free( dll_name );
463 if (!found)
464 warning( "%s:%d: forward '%s' not found in the imported dll list\n",
465 spec->src_name, odp->lineno, odp->link_name );
468 /* flag the dll exports that link to an undefined symbol */
469 static void check_undefined_exports( DLLSPEC *spec )
471 int i;
473 for (i = 0; i < spec->nb_entry_points; i++)
475 ORDDEF *odp = &spec->entry_points[i];
476 if (odp->type == TYPE_STUB) continue;
477 if (odp->flags & FLAG_FORWARD)
479 check_undefined_forward( spec, odp );
480 continue;
482 if (find_name( odp->link_name, &undef_symbols ))
484 switch(odp->type)
486 case TYPE_PASCAL:
487 case TYPE_STDCALL:
488 case TYPE_CDECL:
489 case TYPE_VARARGS:
490 if (link_ext_symbols)
492 odp->flags |= FLAG_EXT_LINK;
493 add_name( &ext_link_imports, odp->link_name );
495 else error( "%s:%d: function '%s' not defined\n",
496 spec->src_name, odp->lineno, odp->link_name );
497 break;
498 default:
499 error( "%s:%d: external symbol '%s' is not a function\n",
500 spec->src_name, odp->lineno, odp->link_name );
501 break;
507 /* create a .o file that references all the undefined symbols we want to resolve */
508 static char *create_undef_symbols_file( DLLSPEC *spec )
510 char *as_file, *obj_file;
511 int i;
512 unsigned int j;
513 FILE *f;
515 as_file = get_temp_file_name( output_file_name, ".s" );
516 if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
517 fprintf( f, "\t.data\n" );
519 for (i = 0; i < spec->nb_entry_points; i++)
521 ORDDEF *odp = &spec->entry_points[i];
522 if (odp->type == TYPE_STUB) continue;
523 if (odp->flags & FLAG_FORWARD) continue;
524 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
526 for (j = 0; j < extra_ld_symbols.count; j++)
527 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[j]) );
528 fclose( f );
530 obj_file = get_temp_file_name( output_file_name, ".o" );
531 assemble_file( as_file, obj_file );
532 return obj_file;
535 /* combine a list of object files with ld into a single object file */
536 /* returns the name of the combined file */
537 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
539 unsigned int i, len = 0;
540 char *cmd, *p, *ld_tmp_file, *undef_file;
541 int err;
543 undef_file = create_undef_symbols_file( spec );
544 len += strlen(undef_file) + 1;
545 ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
546 if (!ld_command) ld_command = xstrdup("ld");
547 for (i = 0; argv[i]; i++) len += strlen(argv[i]) + 1;
548 cmd = p = xmalloc( len + strlen(ld_tmp_file) + 8 + strlen(ld_command) );
549 p += sprintf( cmd, "%s -r -o %s %s", ld_command, ld_tmp_file, undef_file );
550 for (i = 0; argv[i]; i++)
551 p += sprintf( p, " %s", argv[i] );
552 if (verbose) fprintf( stderr, "%s\n", cmd );
553 err = system( cmd );
554 if (err) fatal_error( "%s -r failed with status %d\n", ld_command, err );
555 free( cmd );
556 return ld_tmp_file;
559 /* read in the list of undefined symbols */
560 void read_undef_symbols( DLLSPEC *spec, char **argv )
562 size_t prefix_len;
563 FILE *f;
564 char *cmd, buffer[1024], name_prefix[16];
565 int err;
566 const char *name;
568 if (!argv[0]) return;
570 add_extra_undef_symbols( spec );
572 strcpy( name_prefix, asm_name("") );
573 prefix_len = strlen( name_prefix );
575 name = ldcombine_files( spec, argv );
577 if (!nm_command) nm_command = xstrdup("nm");
578 cmd = xmalloc( strlen(nm_command) + strlen(name) + 5 );
579 sprintf( cmd, "%s -u %s", nm_command, name );
580 if (!(f = popen( cmd, "r" )))
581 fatal_error( "Cannot execute '%s'\n", cmd );
583 while (fgets( buffer, sizeof(buffer), f ))
585 char *p = buffer + strlen(buffer) - 1;
586 if (p < buffer) continue;
587 if (*p == '\n') *p-- = 0;
588 p = buffer;
589 while (*p == ' ') p++;
590 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
591 if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
592 add_name( &undef_symbols, p );
594 if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
595 free( cmd );
598 /* resolve the imports for a Win32 module */
599 int resolve_imports( DLLSPEC *spec )
601 int i;
602 unsigned int j, removed;
603 ORDDEF *odp;
605 sort_names( &ignore_symbols );
607 for (i = 0; i < nb_imports; i++)
609 struct import *imp = dll_imports[i];
611 for (j = removed = 0; j < undef_symbols.count; j++)
613 if (find_name( undef_symbols.names[j], &ignore_symbols )) continue;
614 odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
615 if (odp)
617 add_import_func( imp, odp );
618 remove_name( &undef_symbols, j-- );
619 removed++;
622 if (!removed && check_unused( imp, spec ))
624 /* the dll is not used, get rid of it */
625 warning( "%s imported but no symbols used\n", imp->spec->file_name );
626 remove_import_dll( i );
627 i--;
631 sort_names( &undef_symbols );
632 check_undefined_exports( spec );
634 return 1;
637 /* output the get_pc thunk if needed */
638 void output_get_pc_thunk(void)
640 if (target_cpu != CPU_x86) return;
641 if (!UsePIC) return;
642 output( "\n\t.text\n" );
643 output( "\t.align %d\n", get_alignment(4) );
644 output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
645 output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
646 output( "\tpopl %%eax\n" );
647 output( "\tpushl %%eax\n" );
648 output( "\tret\n" );
649 output_function_size( "__wine_spec_get_pc_thunk_eax" );
652 /* output a single import thunk */
653 static void output_import_thunk( const char *name, const char *table, int pos )
655 output( "\n\t.align %d\n", get_alignment(4) );
656 output( "\t%s\n", func_declaration(name) );
657 output( "%s\n", asm_globl(name) );
659 switch(target_cpu)
661 case CPU_x86:
662 if (!UsePIC)
664 output( "\tjmp *(%s+%d)\n", table, pos );
666 else
668 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
669 output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
671 break;
672 case CPU_x86_64:
673 output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
674 break;
675 case CPU_SPARC:
676 if ( !UsePIC )
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" );
683 else
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" );
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_POWERPC:
703 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
704 output( "\tstw %s, 0(%s)\n", ppc_reg(9), ppc_reg(1) );
705 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
706 output( "\tstw %s, 0(%s)\n", ppc_reg(8), ppc_reg(1) );
707 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
708 output( "\tstw %s, 0(%s)\n", ppc_reg(7), ppc_reg(1) );
709 if (target_platform == PLATFORM_APPLE)
711 output( "\tlis %s, ha16(%s+%d)\n", ppc_reg(9), table, pos );
712 output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
714 else
716 output( "\tlis %s, (%s+%d)@h\n", ppc_reg(9), table, pos );
717 output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(8), table, pos, ppc_reg(9) );
719 output( "\tlwz %s, 0(%s)\n", ppc_reg(7), ppc_reg(8) );
720 output( "\tmtctr %s\n", ppc_reg(7) );
721 output( "\tlwz %s, 0(%s)\n", ppc_reg(7), ppc_reg(1) );
722 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
723 output( "\tlwz %s, 0(%s)\n", ppc_reg(8), ppc_reg(1) );
724 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
725 output( "\tlwz %s, 0(%s)\n", ppc_reg(9), ppc_reg(1) );
726 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
727 output( "\tbctr\n" );
728 break;
730 output_function_size( name );
733 /* check if we need an import directory */
734 int has_imports(void)
736 return (nb_imports - nb_delayed) > 0;
739 /* output the import table of a Win32 module */
740 static void output_immediate_imports(void)
742 int i, j;
743 const char *dll_name;
745 if (nb_imports == nb_delayed) return; /* no immediate imports */
747 /* main import header */
749 output( "\n/* import table */\n" );
750 output( "\n\t.data\n" );
751 output( "\t.align %d\n", get_alignment(4) );
752 output( ".L__wine_spec_imports:\n" );
754 /* list of dlls */
756 for (i = j = 0; i < nb_imports; i++)
758 if (dll_imports[i]->delay) continue;
759 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
760 output( "\t.long 0\n" ); /* OriginalFirstThunk */
761 output( "\t.long 0\n" ); /* TimeDateStamp */
762 output( "\t.long 0\n" ); /* ForwarderChain */
763 output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
764 dll_name );
765 output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */
766 j * get_ptr_size() );
767 j += dll_imports[i]->nb_imports + 1;
769 output( "\t.long 0\n" ); /* OriginalFirstThunk */
770 output( "\t.long 0\n" ); /* TimeDateStamp */
771 output( "\t.long 0\n" ); /* ForwarderChain */
772 output( "\t.long 0\n" ); /* Name */
773 output( "\t.long 0\n" ); /* FirstThunk */
775 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
776 output( ".L__wine_spec_import_data_ptrs:\n" );
777 for (i = 0; i < nb_imports; i++)
779 if (dll_imports[i]->delay) continue;
780 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
781 for (j = 0; j < dll_imports[i]->nb_imports; j++)
783 ORDDEF *odp = dll_imports[i]->imports[j];
784 if (!(odp->flags & FLAG_NONAME))
785 output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
786 get_asm_ptr_keyword(), dll_name, odp->name );
787 else
789 if (get_ptr_size() == 8)
790 output( "\t.quad 0x800000000000%04x\n", odp->ordinal );
791 else
792 output( "\t.long 0x8000%04x\n", odp->ordinal );
795 output( "\t%s 0\n", get_asm_ptr_keyword() );
797 output( ".L__wine_spec_imports_end:\n" );
799 for (i = 0; i < nb_imports; i++)
801 if (dll_imports[i]->delay) continue;
802 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
803 for (j = 0; j < dll_imports[i]->nb_imports; j++)
805 ORDDEF *odp = dll_imports[i]->imports[j];
806 if (!(odp->flags & FLAG_NONAME))
808 output( "\t.align %d\n", get_alignment(2) );
809 output( ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name );
810 output( "\t%s %d\n", get_asm_short_keyword(), odp->ordinal );
811 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
816 for (i = 0; i < nb_imports; i++)
818 if (dll_imports[i]->delay) continue;
819 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
820 output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
821 dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name );
825 /* output the import thunks of a Win32 module */
826 static void output_immediate_import_thunks(void)
828 int i, j, pos;
829 int nb_imm = nb_imports - nb_delayed;
830 static const char import_thunks[] = "__wine_spec_import_thunks";
832 if (!nb_imm) return;
834 output( "\n/* immediate import thunks */\n\n" );
835 output( "\t.text\n" );
836 output( "\t.align %d\n", get_alignment(8) );
837 output( "%s:\n", asm_name(import_thunks));
839 for (i = pos = 0; i < nb_imports; i++)
841 if (dll_imports[i]->delay) continue;
842 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
844 ORDDEF *odp = dll_imports[i]->imports[j];
845 output_import_thunk( odp->name ? odp->name : odp->export_name,
846 ".L__wine_spec_import_data_ptrs", pos );
848 pos += get_ptr_size();
850 output_function_size( import_thunks );
853 /* output the delayed import table of a Win32 module */
854 static void output_delayed_imports( const DLLSPEC *spec )
856 int i, j, mod;
858 if (!nb_delayed) return;
860 output( "\n/* delayed imports */\n\n" );
861 output( "\t.data\n" );
862 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
863 output( "%s\n", asm_globl("__wine_spec_delay_imports") );
865 /* list of dlls */
867 for (i = j = mod = 0; i < nb_imports; i++)
869 if (!dll_imports[i]->delay) continue;
870 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
871 output( "\t%s .L__wine_delay_name_%d\n", /* szName */
872 get_asm_ptr_keyword(), i );
873 output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
874 get_asm_ptr_keyword(), mod * get_ptr_size() );
875 output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
876 get_asm_ptr_keyword(), j * get_ptr_size() );
877 output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
878 get_asm_ptr_keyword(), j * get_ptr_size() );
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 j += dll_imports[i]->nb_imports;
883 mod++;
885 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
886 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
887 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
888 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
889 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
890 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
891 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
892 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
894 output( "\n.L__wine_delay_IAT:\n" );
895 for (i = 0; i < nb_imports; i++)
897 if (!dll_imports[i]->delay) continue;
898 for (j = 0; j < dll_imports[i]->nb_imports; j++)
900 ORDDEF *odp = dll_imports[i]->imports[j];
901 const char *name = odp->name ? odp->name : odp->export_name;
902 output( "\t%s .L__wine_delay_imp_%d_%s\n",
903 get_asm_ptr_keyword(), i, name );
907 output( "\n.L__wine_delay_INT:\n" );
908 for (i = 0; i < nb_imports; i++)
910 if (!dll_imports[i]->delay) continue;
911 for (j = 0; j < dll_imports[i]->nb_imports; j++)
913 ORDDEF *odp = dll_imports[i]->imports[j];
914 if (!odp->name)
915 output( "\t%s %d\n", get_asm_ptr_keyword(), odp->ordinal );
916 else
917 output( "\t%s .L__wine_delay_data_%d_%s\n",
918 get_asm_ptr_keyword(), i, odp->name );
922 output( "\n.L__wine_delay_modules:\n" );
923 for (i = 0; i < nb_imports; i++)
925 if (dll_imports[i]->delay) output( "\t%s 0\n", get_asm_ptr_keyword() );
928 for (i = 0; i < nb_imports; i++)
930 if (!dll_imports[i]->delay) continue;
931 output( ".L__wine_delay_name_%d:\n", i );
932 output( "\t%s \"%s\"\n",
933 get_asm_string_keyword(), dll_imports[i]->spec->file_name );
936 for (i = 0; i < nb_imports; i++)
938 if (!dll_imports[i]->delay) continue;
939 for (j = 0; j < dll_imports[i]->nb_imports; j++)
941 ORDDEF *odp = dll_imports[i]->imports[j];
942 if (!odp->name) continue;
943 output( ".L__wine_delay_data_%d_%s:\n", i, odp->name );
944 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
947 output_function_size( "__wine_spec_delay_imports" );
950 /* output the delayed import thunks of a Win32 module */
951 static void output_delayed_import_thunks( const DLLSPEC *spec )
953 int i, idx, j, pos, extra_stack_storage = 0;
954 static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
955 static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
957 if (!nb_delayed) return;
959 output( "\n/* delayed import thunks */\n\n" );
960 output( "\t.text\n" );
961 output( "\t.align %d\n", get_alignment(8) );
962 output( "%s:\n", asm_name(delayed_import_loaders));
963 output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
964 output( "%s:\n", asm_name("__wine_delay_load_asm") );
965 switch(target_cpu)
967 case CPU_x86:
968 output( "\tpushl %%ecx\n" );
969 output( "\tpushl %%edx\n" );
970 output( "\tpushl %%eax\n" );
971 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
972 output( "\tpopl %%edx\n" );
973 output( "\tpopl %%ecx\n" );
974 output( "\tjmp *%%eax\n" );
975 break;
976 case CPU_x86_64:
977 output( "\tpushq %%rdi\n" );
978 output( "\tpushq %%rsi\n" );
979 output( "\tpushq %%rdx\n" );
980 output( "\tpushq %%rcx\n" );
981 output( "\tpushq %%r8\n" );
982 output( "\tpushq %%r9\n" );
983 output( "\tsubq $8,%%rsp\n" );
984 output( "\tmovq %%r11,%%rdi\n" );
985 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
986 output( "\taddq $8,%%rsp\n" );
987 output( "\tpopq %%r9\n" );
988 output( "\tpopq %%r8\n" );
989 output( "\tpopq %%rcx\n" );
990 output( "\tpopq %%rdx\n" );
991 output( "\tpopq %%rsi\n" );
992 output( "\tpopq %%rdi\n" );
993 output( "\tjmp *%%rax\n" );
994 break;
995 case CPU_SPARC:
996 output( "\tsave %%sp, -96, %%sp\n" );
997 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
998 output( "\tmov %%g1, %%o0\n" );
999 output( "\tjmp %%o0\n" );
1000 output( "\trestore\n" );
1001 break;
1002 case CPU_ALPHA:
1003 output( "\tjsr $26,%s\n", asm_name("__wine_spec_delay_load") );
1004 output( "\tjmp $31,($0)\n" );
1005 break;
1006 case CPU_POWERPC:
1007 if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
1009 /* Save all callee saved registers into a stackframe. */
1010 output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
1011 output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1012 output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1013 output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1014 output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1015 output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1016 output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1017 output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1018 output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1019 output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1020 output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1022 /* r0 -> r3 (arg1) */
1023 output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
1025 /* save return address */
1026 output( "\tmflr %s\n", ppc_reg(0));
1027 output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1029 /* Call the __wine_delay_load function, arg1 is arg1. */
1030 output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
1032 /* Load return value from call into ctr register */
1033 output( "\tmtctr %s\n", ppc_reg(3));
1035 /* restore all saved registers and drop stackframe. */
1036 output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1037 output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1038 output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1039 output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1040 output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1041 output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1042 output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1043 output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1044 output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1045 output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1047 /* Load return value from call into return register */
1048 output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1049 output( "\tmtlr %s\n", ppc_reg(0));
1050 output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage);
1052 /* branch to ctr register. */
1053 output( "\tbctr\n");
1054 break;
1056 output_function_size( "__wine_delay_load_asm" );
1057 output( "\n" );
1059 for (i = idx = 0; i < nb_imports; i++)
1061 if (!dll_imports[i]->delay) continue;
1062 for (j = 0; j < dll_imports[i]->nb_imports; j++)
1064 ORDDEF *odp = dll_imports[i]->imports[j];
1065 const char *name = odp->name ? odp->name : odp->export_name;
1067 output( ".L__wine_delay_imp_%d_%s:\n", i, name );
1068 switch(target_cpu)
1070 case CPU_x86:
1071 output( "\tmovl $%d, %%eax\n", (idx << 16) | j );
1072 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1073 break;
1074 case CPU_x86_64:
1075 output( "\tmovq $%d,%%r11\n", (idx << 16) | j );
1076 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1077 break;
1078 case CPU_SPARC:
1079 output( "\tset %d, %%g1\n", (idx << 16) | j );
1080 output( "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1081 break;
1082 case CPU_ALPHA:
1083 output( "\tlda $0,%d($31)\n", j);
1084 output( "\tldah $0,%d($0)\n", idx);
1085 output( "\tjmp $31,%s\n", asm_name("__wine_delay_load_asm") );
1086 break;
1087 case CPU_POWERPC:
1088 switch(target_platform)
1090 case PLATFORM_APPLE:
1091 /* On Darwin we can use r0 and r2 */
1092 /* Upper part in r2 */
1093 output( "\tlis %s, %d\n", ppc_reg(2), idx);
1094 /* Lower part + r2 -> r0, Note we can't use r0 directly */
1095 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1096 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1097 break;
1098 default:
1099 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1100 /* Save r13 on the stack */
1101 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1102 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1103 /* Upper part in r13 */
1104 output( "\tlis %s, %d\n", ppc_reg(13), idx);
1105 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1106 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1107 /* Restore r13 */
1108 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1109 output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1110 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1111 break;
1113 break;
1116 idx++;
1118 output_function_size( delayed_import_loaders );
1120 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1121 output( "%s:\n", asm_name(delayed_import_thunks));
1122 for (i = pos = 0; i < nb_imports; i++)
1124 if (!dll_imports[i]->delay) continue;
1125 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
1127 ORDDEF *odp = dll_imports[i]->imports[j];
1128 output_import_thunk( odp->name ? odp->name : odp->export_name,
1129 ".L__wine_delay_IAT", pos );
1132 output_function_size( delayed_import_thunks );
1135 /* output import stubs for exported entry points that link to external symbols */
1136 static void output_external_link_imports( DLLSPEC *spec )
1138 unsigned int i, pos;
1140 if (!ext_link_imports.count) return; /* nothing to do */
1142 sort_names( &ext_link_imports );
1144 /* get rid of duplicate names */
1145 for (i = 1; i < ext_link_imports.count; i++)
1147 if (!strcmp( ext_link_imports.names[i-1], ext_link_imports.names[i] ))
1148 remove_name( &ext_link_imports, i-- );
1151 output( "\n/* external link thunks */\n\n" );
1152 output( "\t.data\n" );
1153 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1154 output( ".L__wine_spec_external_links:\n" );
1155 for (i = 0; i < ext_link_imports.count; i++)
1156 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.names[i]) );
1158 output( "\n\t.text\n" );
1159 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1160 output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1162 for (i = pos = 0; i < ext_link_imports.count; i++)
1164 char buffer[256];
1165 sprintf( buffer, "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
1166 output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
1167 pos += get_ptr_size();
1169 output_function_size( "__wine_spec_external_link_thunks" );
1172 /*******************************************************************
1173 * output_stubs
1175 * Output the functions for stub entry points
1177 void output_stubs( DLLSPEC *spec )
1179 const char *name, *exp_name;
1180 int i, count;
1182 if (!has_stubs( spec )) return;
1184 output( "\n/* stub functions */\n\n" );
1185 output( "\t.text\n" );
1187 for (i = count = 0; i < spec->nb_entry_points; i++)
1189 ORDDEF *odp = &spec->entry_points[i];
1190 if (odp->type != TYPE_STUB) continue;
1192 name = get_stub_name( odp, spec );
1193 exp_name = odp->name ? odp->name : odp->export_name;
1194 output( "\t.align %d\n", get_alignment(4) );
1195 output( "\t%s\n", func_declaration(name) );
1196 output( "%s:\n", asm_name(name) );
1197 output( "\tsubl $4,%%esp\n" );
1199 if (UsePIC)
1201 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1202 output( "1:" );
1203 if (exp_name)
1205 output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
1206 output( "\tpushl %%ecx\n" );
1207 count++;
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" );
1214 else
1216 if (exp_name)
1218 output( "\tpushl $.L%s_string\n", name );
1219 count++;
1221 else
1222 output( "\tpushl $%d\n", odp->ordinal );
1223 output( "\tpushl $.L__wine_spec_file_name\n" );
1225 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1226 output_function_size( name );
1229 if (count)
1231 output( "\t%s\n", get_asm_string_section() );
1232 for (i = 0; i < spec->nb_entry_points; i++)
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)
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 );
1247 /* output the import and delayed import tables of a Win32 module */
1248 void output_imports( DLLSPEC *spec )
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();