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
23 #include "wine/port.h"
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
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 */
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 */
56 unsigned int count, size;
59 static struct name_table undef_symbols; /* list of undefined symbols */
60 static struct name_table extra_ld_symbols; /* list of extra symbols that ld should resolve */
61 static struct name_table delayed_imports; /* list of delayed import dlls */
62 static struct name_table ext_link_imports; /* list of external symbols to link to */
64 static struct import **dll_imports = NULL;
65 static int nb_imports = 0; /* number of imported dlls (delayed or not) */
66 static int nb_delayed = 0; /* number of delayed dlls */
67 static int total_imports = 0; /* total number of imported functions */
68 static int total_delayed = 0; /* total number of imported functions in delayed DLLs */
71 static inline const char *ppc_reg( int reg )
73 static const char * const ppc_regs[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
74 "r8", "r9", "r10","r11","r12","r13","r14","r15",
75 "r16","r17","r18","r19","r20","r21","r22","r23",
76 "r24","r25","r26","r27","r28","r29","r30","r31" };
77 if (target_platform == PLATFORM_APPLE) return ppc_regs[reg];
78 return ppc_regs[reg] + 1; /* skip the 'r' */
81 /* compare function names; helper for resolve_imports */
82 static int name_cmp( const void *name, const void *entry )
84 return strcmp( *(const char* const *)name, *(const char* const *)entry );
87 /* compare function names; helper for resolve_imports */
88 static int func_cmp( const void *func1, const void *func2 )
90 const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
91 const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
92 return strcmp( odp1->name ? odp1->name : odp1->export_name,
93 odp2->name ? odp2->name : odp2->export_name );
96 /* add a name to a name table */
97 static inline void add_name( struct name_table *table, const char *name )
99 if (table->count == table->size)
101 table->size += (table->size / 2);
102 if (table->size < 32) table->size = 32;
103 table->names = xrealloc( table->names, table->size * sizeof(*table->names) );
105 table->names[table->count++] = xstrdup( name );
108 /* remove a name from a name table */
109 static inline void remove_name( struct name_table *table, unsigned int idx )
111 assert( idx < table->count );
112 free( table->names[idx] );
113 memmove( table->names + idx, table->names + idx + 1,
114 (table->count - idx - 1) * sizeof(*table->names) );
118 /* make a name table empty */
119 static inline void empty_name_table( struct name_table *table )
123 for (i = 0; i < table->count; i++) free( table->names[i] );
127 /* locate a name in a (sorted) list */
128 static inline const char *find_name( const char *name, const struct name_table *table )
132 if (table->count) res = bsearch( &name, table->names, table->count, sizeof(*table->names), name_cmp );
133 return res ? *res : NULL;
136 /* sort a name table */
137 static inline void sort_names( struct name_table *table )
139 if (table->count) qsort( table->names, table->count, sizeof(*table->names), name_cmp );
142 /* locate an export in a (sorted) export list */
143 static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
145 ORDDEF func, *odp, **res = NULL;
147 func.name = xstrdup(name);
150 if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
152 return res ? *res : NULL;
155 /* free an import structure */
156 static void free_imports( struct import *imp )
158 free( imp->exports );
159 free( imp->imports );
160 free_dll_spec( imp->spec );
161 free( imp->full_name );
165 /* check whether a given dll is imported in delayed mode */
166 static int is_delayed_import( const char *name )
170 for (i = 0; i < delayed_imports.count; i++)
172 if (!strcmp( delayed_imports.names[i], name )) return 1;
177 /* check whether a given dll has already been imported */
178 static struct import *is_already_imported( const char *name )
182 for (i = 0; i < nb_imports; i++)
184 if (!strcmp( dll_imports[i]->spec->file_name, name )) return dll_imports[i];
189 /* open the .so library for a given dll in a specified path */
190 static char *try_library_path( const char *path, const char *name )
195 buffer = strmake( "%s/lib%s.def", path, name );
197 /* check if the file exists */
198 if ((fd = open( buffer, O_RDONLY )) != -1)
207 /* find the .def import library for a given dll */
208 static char *find_library( const char *name )
213 for (i = 0; i < nb_lib_paths; i++)
215 if ((fullname = try_library_path( lib_path[i], name ))) return fullname;
217 fatal_error( "could not open .def file for %s\n", name );
221 /* read in the list of exported symbols of an import library */
222 static int read_import_lib( struct import *imp )
227 struct import *prev_imp;
228 DLLSPEC *spec = imp->spec;
230 f = open_input_file( NULL, imp->full_name );
231 fstat( fileno(f), &stat );
232 imp->dev = stat.st_dev;
233 imp->ino = stat.st_ino;
234 ret = parse_def_file( f, spec );
235 close_input_file( f );
238 /* check if we already imported that library from a different file */
239 if ((prev_imp = is_already_imported( spec->file_name )))
241 if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
242 fatal_error( "%s and %s have the same export name '%s'\n",
243 prev_imp->full_name, imp->full_name, spec->file_name );
244 return 0; /* the same file was already loaded, ignore this one */
247 if (is_delayed_import( spec->file_name ))
253 if (spec->nb_entry_points)
255 imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
256 for (i = 0; i < spec->nb_entry_points; i++)
257 imp->exports[imp->nb_exports++] = &spec->entry_points[i];
258 qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
263 /* build the dll exported name from the import lib name or path */
264 static char *get_dll_name( const char *name, const char *filename )
270 const char *basename = strrchr( filename, '/' );
271 if (!basename) basename = filename;
273 if (!strncmp( basename, "lib", 3 )) basename += 3;
274 ret = xmalloc( strlen(basename) + 5 );
275 strcpy( ret, basename );
276 if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
280 ret = xmalloc( strlen(name) + 5 );
283 if (!strchr( ret, '.' )) strcat( ret, ".dll" );
287 /* add a dll to the list of imports */
288 void add_import_dll( const char *name, const char *filename )
290 struct import *imp = xmalloc( sizeof(*imp) );
292 imp->spec = alloc_dll_spec();
293 imp->spec->file_name = get_dll_name( name, filename );
300 if (filename) imp->full_name = xstrdup( filename );
301 else imp->full_name = find_library( name );
303 if (read_import_lib( imp ))
305 dll_imports = xrealloc( dll_imports, (nb_imports+1) * sizeof(*dll_imports) );
306 dll_imports[nb_imports++] = imp;
311 if (nb_errors) exit(1);
315 /* add a library to the list of delayed imports */
316 void add_delayed_import( const char *name )
319 char *fullname = get_dll_name( name, NULL );
321 add_name( &delayed_imports, fullname );
322 if ((imp = is_already_imported( fullname )) && !imp->delay)
330 /* remove an imported dll, based on its index in the dll_imports array */
331 static void remove_import_dll( int index )
333 struct import *imp = dll_imports[index];
335 memmove( &dll_imports[index], &dll_imports[index+1], sizeof(imp) * (nb_imports - index - 1) );
337 if (imp->delay) nb_delayed--;
341 /* add a symbol to the list of extra symbols that ld must resolve */
342 void add_extra_ld_symbol( const char *name )
344 add_name( &extra_ld_symbols, name );
347 /* add a function to the list of imports from a given dll */
348 static void add_import_func( struct import *imp, ORDDEF *func )
350 imp->imports = xrealloc( imp->imports, (imp->nb_imports+1) * sizeof(*imp->imports) );
351 imp->imports[imp->nb_imports++] = func;
353 if (imp->delay) total_delayed++;
356 /* get the default entry point for a given spec file */
357 static const char *get_default_entry_point( const DLLSPEC *spec )
359 if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
360 if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
361 if (spec->type == SPEC_WIN16) return "__wine_spec_exe16_entry";
362 return "__wine_spec_exe_entry";
365 /* check if the spec file exports any stubs */
366 static int has_stubs( const DLLSPEC *spec )
369 for (i = 0; i < spec->nb_entry_points; i++)
371 ORDDEF *odp = &spec->entry_points[i];
372 if (odp->type == TYPE_STUB) return 1;
377 /* add the extra undefined symbols that will be contained in the generated spec file itself */
378 static void add_extra_undef_symbols( DLLSPEC *spec )
380 if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point(spec) );
381 add_extra_ld_symbol( spec->init_func );
382 if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
383 if (nb_delayed) add_extra_ld_symbol( "__wine_spec_delay_load" );
386 /* check if a given imported dll is not needed, taking forwards into account */
387 static int check_unused( const struct import* imp, const DLLSPEC *spec )
390 const char *file_name = imp->spec->file_name;
391 size_t len = strlen( file_name );
392 const char *p = strchr( file_name, '.' );
393 if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
395 for (i = spec->base; i <= spec->limit; i++)
397 ORDDEF *odp = spec->ordinals[i];
398 if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
399 if (!strncasecmp( odp->link_name, file_name, len ) &&
400 odp->link_name[len] == '.')
401 return 0; /* found a forward, it is used */
406 /* check if a given forward does exist in one of the imported dlls */
407 static void check_undefined_forwards( DLLSPEC *spec )
409 char *link_name, *api_name, *dll_name, *p;
412 for (i = 0; i < spec->nb_entry_points; i++)
414 ORDDEF *odp = &spec->entry_points[i];
416 if (!(odp->flags & FLAG_FORWARD)) continue;
418 link_name = xstrdup( odp->link_name );
419 p = strrchr( link_name, '.' );
422 dll_name = get_dll_name( link_name, NULL );
424 for (j = 0; j < nb_imports; j++)
426 struct import *imp = dll_imports[j];
428 if (strcasecmp( imp->spec->file_name, dll_name )) continue;
429 if (!find_export( api_name, imp->exports, imp->nb_exports ))
430 warning( "%s:%d: forward '%s' not found in %s\n",
431 spec->src_name, odp->lineno, odp->link_name, imp->spec->file_name );
435 warning( "%s:%d: forward '%s' not found in the imported dll list\n",
436 spec->src_name, odp->lineno, odp->link_name );
442 /* flag the dll exports that link to an undefined symbol */
443 static void check_undefined_exports( DLLSPEC *spec )
447 for (i = 0; i < spec->nb_entry_points; i++)
449 ORDDEF *odp = &spec->entry_points[i];
450 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
451 if (odp->flags & FLAG_FORWARD) continue;
452 if (find_name( odp->link_name, &undef_symbols ))
461 if (link_ext_symbols)
463 odp->flags |= FLAG_EXT_LINK;
464 add_name( &ext_link_imports, odp->link_name );
466 else error( "%s:%d: function '%s' not defined\n",
467 spec->src_name, odp->lineno, odp->link_name );
470 error( "%s:%d: external symbol '%s' is not a function\n",
471 spec->src_name, odp->lineno, odp->link_name );
478 /* create a .o file that references all the undefined symbols we want to resolve */
479 static char *create_undef_symbols_file( DLLSPEC *spec )
481 char *as_file, *obj_file;
486 as_file = get_temp_file_name( output_file_name, ".s" );
487 if (!(f = fopen( as_file, "w" ))) fatal_error( "Cannot create %s\n", as_file );
488 fprintf( f, "\t.data\n" );
490 for (i = 0; i < spec->nb_entry_points; i++)
492 ORDDEF *odp = &spec->entry_points[i];
493 if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
494 if (odp->flags & FLAG_FORWARD) continue;
495 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
497 for (j = 0; j < extra_ld_symbols.count; j++)
498 fprintf( f, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.names[j]) );
501 obj_file = get_temp_file_name( output_file_name, ".o" );
502 assemble_file( as_file, obj_file );
506 /* combine a list of object files with ld into a single object file */
507 /* returns the name of the combined file */
508 static const char *ldcombine_files( DLLSPEC *spec, char **argv )
510 char *ld_tmp_file, *undef_file;
511 struct strarray *args = get_ld_command();
513 undef_file = create_undef_symbols_file( spec );
514 ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
516 strarray_add( args, "-r", "-o", ld_tmp_file, undef_file, NULL );
517 strarray_addv( args, argv );
519 strarray_free( args );
523 /* read in the list of undefined symbols */
524 void read_undef_symbols( DLLSPEC *spec, char **argv )
528 const char *prog = get_nm_command();
529 char *cmd, buffer[1024], name_prefix[16];
533 if (!argv[0]) return;
535 add_extra_undef_symbols( spec );
537 strcpy( name_prefix, asm_name("") );
538 prefix_len = strlen( name_prefix );
540 name = ldcombine_files( spec, argv );
542 cmd = strmake( "%s -u %s", prog, name );
543 if (!(f = popen( cmd, "r" )))
544 fatal_error( "Cannot execute '%s'\n", cmd );
546 while (fgets( buffer, sizeof(buffer), f ))
548 char *p = buffer + strlen(buffer) - 1;
549 if (p < buffer) continue;
550 if (*p == '\n') *p-- = 0;
552 while (*p == ' ') p++;
553 if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
554 if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
555 add_name( &undef_symbols, p );
557 if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
561 /* resolve the imports for a Win32 module */
562 void resolve_imports( DLLSPEC *spec )
565 unsigned int j, removed;
568 check_undefined_forwards( spec );
570 for (i = 0; i < nb_imports; i++)
572 struct import *imp = dll_imports[i];
574 for (j = removed = 0; j < undef_symbols.count; j++)
576 odp = find_export( undef_symbols.names[j], imp->exports, imp->nb_exports );
579 if (odp->flags & FLAG_PRIVATE) continue;
580 if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL)
581 warning( "winebuild: Data export '%s' cannot be imported from %s\n",
582 odp->link_name, imp->spec->file_name );
585 add_import_func( imp, odp );
586 remove_name( &undef_symbols, j-- );
593 /* the dll is not used, get rid of it */
594 if (check_unused( imp, spec ))
595 warning( "winebuild: %s imported but no symbols used\n", imp->spec->file_name );
596 remove_import_dll( i );
601 sort_names( &undef_symbols );
602 check_undefined_exports( spec );
605 /* check if symbol is still undefined */
606 int is_undefined( const char *name )
608 return find_name( name, &undef_symbols ) != NULL;
611 /* output the get_pc thunk if needed */
612 void output_get_pc_thunk(void)
614 if (target_cpu != CPU_x86) return;
616 output( "\n\t.text\n" );
617 output( "\t.align %d\n", get_alignment(4) );
618 output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
619 output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
620 output_cfi( ".cfi_startproc" );
621 output( "\tmovl (%%esp),%%eax\n" );
623 output_cfi( ".cfi_endproc" );
624 output_function_size( "__wine_spec_get_pc_thunk_eax" );
627 /* output a single import thunk */
628 static void output_import_thunk( const char *name, const char *table, int pos )
630 output( "\n\t.align %d\n", get_alignment(4) );
631 output( "\t%s\n", func_declaration(name) );
632 output( "%s\n", asm_globl(name) );
633 output_cfi( ".cfi_startproc" );
640 output( "\tjmp *(%s+%d)\n", table, pos );
644 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
645 output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
649 output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
654 output( "\tsethi %%hi(%s+%d), %%g1\n", table, pos );
655 output( "\tld [%%g1+%%lo(%s+%d)], %%g1\n", table, pos );
656 output( "\tjmp %%g1\n" );
661 /* Hmpf. Stupid sparc assembler always interprets global variable
662 names as GOT offsets, so we have to do it the long way ... */
663 output( "\tsave %%sp, -96, %%sp\n" );
664 output( "0:\tcall 1f\n" );
666 output( "1:\tsethi %%hi(%s+%d-0b), %%g1\n", table, pos );
667 output( "\tor %%g1, %%lo(%s+%d-0b), %%g1\n", table, pos );
668 output( "\tld [%%g1+%%o7], %%g1\n" );
669 output( "\tjmp %%g1\n" );
670 output( "\trestore\n" );
674 output( "\tldr IP,[PC,#0]\n");
675 output( "\tldr PC,[IP,#%d]\n", pos);
676 output( "\t.long %s\n", table );
679 output( "\tmr %s, %s\n", ppc_reg(0), ppc_reg(31) );
680 if (target_platform == PLATFORM_APPLE)
682 output( "\tlis %s, ha16(%s+%d+32768)\n", ppc_reg(31), table, pos );
683 output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
687 output( "\tlis %s, (%s+%d+32768)@h\n", ppc_reg(31), table, pos );
688 output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
690 output( "\tlwz %s, 0(%s)\n", ppc_reg(31), ppc_reg(31) );
691 output( "\tmtctr %s\n", ppc_reg(31) );
692 output( "\tmr %s, %s\n", ppc_reg(31), ppc_reg(0) );
693 output( "\tbctr\n" );
696 output_cfi( ".cfi_endproc" );
697 output_function_size( name );
700 /* check if we need an import directory */
701 int has_imports(void)
703 return (nb_imports - nb_delayed) > 0;
706 /* output the import table of a Win32 module */
707 static void output_immediate_imports(void)
710 const char *dll_name;
712 if (nb_imports == nb_delayed) return; /* no immediate imports */
714 /* main import header */
716 output( "\n/* import table */\n" );
717 output( "\n\t.data\n" );
718 output( "\t.align %d\n", get_alignment(4) );
719 output( ".L__wine_spec_imports:\n" );
723 for (i = j = 0; i < nb_imports; i++)
725 if (dll_imports[i]->delay) continue;
726 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
727 output( "\t.long .L__wine_spec_import_data_names+%d-.L__wine_spec_rva_base\n", /* OriginalFirstThunk */
728 j * get_ptr_size() );
729 output( "\t.long 0\n" ); /* TimeDateStamp */
730 output( "\t.long 0\n" ); /* ForwarderChain */
731 output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
733 output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */
734 j * get_ptr_size() );
735 j += dll_imports[i]->nb_imports + 1;
737 output( "\t.long 0\n" ); /* OriginalFirstThunk */
738 output( "\t.long 0\n" ); /* TimeDateStamp */
739 output( "\t.long 0\n" ); /* ForwarderChain */
740 output( "\t.long 0\n" ); /* Name */
741 output( "\t.long 0\n" ); /* FirstThunk */
743 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
744 output( ".L__wine_spec_import_data_names:\n" );
745 for (i = 0; i < nb_imports; i++)
747 if (dll_imports[i]->delay) continue;
748 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
749 for (j = 0; j < dll_imports[i]->nb_imports; j++)
751 ORDDEF *odp = dll_imports[i]->imports[j];
752 if (!(odp->flags & FLAG_NONAME))
753 output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
754 get_asm_ptr_keyword(), dll_name, odp->name );
757 if (get_ptr_size() == 8)
758 output( "\t.quad 0x800000000000%04x\n", odp->ordinal );
760 output( "\t.long 0x8000%04x\n", odp->ordinal );
763 output( "\t%s 0\n", get_asm_ptr_keyword() );
765 output( ".L__wine_spec_import_data_ptrs:\n" );
766 for (i = 0; i < nb_imports; i++)
768 if (dll_imports[i]->delay) continue;
769 for (j = 0; j < dll_imports[i]->nb_imports; j++) output( "\t%s 0\n", get_asm_ptr_keyword() );
770 output( "\t%s 0\n", get_asm_ptr_keyword() );
772 output( ".L__wine_spec_imports_end:\n" );
774 for (i = 0; i < nb_imports; i++)
776 if (dll_imports[i]->delay) continue;
777 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
778 for (j = 0; j < dll_imports[i]->nb_imports; j++)
780 ORDDEF *odp = dll_imports[i]->imports[j];
781 if (!(odp->flags & FLAG_NONAME))
783 output( "\t.align %d\n", get_alignment(2) );
784 output( ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name );
785 output( "\t%s %d\n", get_asm_short_keyword(), odp->ordinal );
786 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
791 for (i = 0; i < nb_imports; i++)
793 if (dll_imports[i]->delay) continue;
794 dll_name = make_c_identifier( dll_imports[i]->spec->file_name );
795 output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
796 dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name );
800 /* output the import thunks of a Win32 module */
801 static void output_immediate_import_thunks(void)
804 int nb_imm = nb_imports - nb_delayed;
805 static const char import_thunks[] = "__wine_spec_import_thunks";
809 output( "\n/* immediate import thunks */\n\n" );
810 output( "\t.text\n" );
811 output( "\t.align %d\n", get_alignment(8) );
812 output( "%s:\n", asm_name(import_thunks));
814 for (i = pos = 0; i < nb_imports; i++)
816 if (dll_imports[i]->delay) continue;
817 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
819 ORDDEF *odp = dll_imports[i]->imports[j];
820 output_import_thunk( odp->name ? odp->name : odp->export_name,
821 ".L__wine_spec_import_data_ptrs", pos );
823 pos += get_ptr_size();
825 output_function_size( import_thunks );
828 /* output the delayed import table of a Win32 module */
829 static void output_delayed_imports( const DLLSPEC *spec )
833 if (!nb_delayed) return;
835 output( "\n/* delayed imports */\n\n" );
836 output( "\t.data\n" );
837 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
838 output( "%s\n", asm_globl("__wine_spec_delay_imports") );
842 for (i = j = mod = 0; i < nb_imports; i++)
844 if (!dll_imports[i]->delay) continue;
845 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
846 output( "\t%s .L__wine_delay_name_%d\n", /* szName */
847 get_asm_ptr_keyword(), i );
848 output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
849 get_asm_ptr_keyword(), mod * get_ptr_size() );
850 output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
851 get_asm_ptr_keyword(), j * get_ptr_size() );
852 output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
853 get_asm_ptr_keyword(), j * get_ptr_size() );
854 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
855 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
856 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
857 j += dll_imports[i]->nb_imports;
860 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
861 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
862 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
863 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
864 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
865 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
866 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
867 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
869 output( "\n.L__wine_delay_IAT:\n" );
870 for (i = 0; i < nb_imports; i++)
872 if (!dll_imports[i]->delay) continue;
873 for (j = 0; j < dll_imports[i]->nb_imports; j++)
875 ORDDEF *odp = dll_imports[i]->imports[j];
876 const char *name = odp->name ? odp->name : odp->export_name;
877 output( "\t%s .L__wine_delay_imp_%d_%s\n",
878 get_asm_ptr_keyword(), i, name );
882 output( "\n.L__wine_delay_INT:\n" );
883 for (i = 0; i < nb_imports; i++)
885 if (!dll_imports[i]->delay) continue;
886 for (j = 0; j < dll_imports[i]->nb_imports; j++)
888 ORDDEF *odp = dll_imports[i]->imports[j];
890 output( "\t%s %d\n", get_asm_ptr_keyword(), odp->ordinal );
892 output( "\t%s .L__wine_delay_data_%d_%s\n",
893 get_asm_ptr_keyword(), i, odp->name );
897 output( "\n.L__wine_delay_modules:\n" );
898 for (i = 0; i < nb_imports; i++)
900 if (dll_imports[i]->delay) output( "\t%s 0\n", get_asm_ptr_keyword() );
903 for (i = 0; i < nb_imports; i++)
905 if (!dll_imports[i]->delay) continue;
906 output( ".L__wine_delay_name_%d:\n", i );
907 output( "\t%s \"%s\"\n",
908 get_asm_string_keyword(), dll_imports[i]->spec->file_name );
911 for (i = 0; i < nb_imports; i++)
913 if (!dll_imports[i]->delay) continue;
914 for (j = 0; j < dll_imports[i]->nb_imports; j++)
916 ORDDEF *odp = dll_imports[i]->imports[j];
917 if (!odp->name) continue;
918 output( ".L__wine_delay_data_%d_%s:\n", i, odp->name );
919 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name );
922 output_function_size( "__wine_spec_delay_imports" );
925 /* output the delayed import thunks of a Win32 module */
926 static void output_delayed_import_thunks( const DLLSPEC *spec )
928 int i, idx, j, pos, extra_stack_storage = 0;
929 static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
930 static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
932 if (!nb_delayed) return;
934 output( "\n/* delayed import thunks */\n\n" );
935 output( "\t.text\n" );
936 output( "\t.align %d\n", get_alignment(8) );
937 output( "%s:\n", asm_name(delayed_import_loaders));
938 output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
939 output( "%s:\n", asm_name("__wine_delay_load_asm") );
940 output_cfi( ".cfi_startproc" );
944 output( "\tpushl %%ecx\n" );
945 output_cfi( ".cfi_adjust_cfa_offset 4" );
946 output( "\tpushl %%edx\n" );
947 output_cfi( ".cfi_adjust_cfa_offset 4" );
948 output( "\tpushl %%eax\n" );
949 output_cfi( ".cfi_adjust_cfa_offset 4" );
950 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
951 output_cfi( ".cfi_adjust_cfa_offset -4" );
952 output( "\tpopl %%edx\n" );
953 output_cfi( ".cfi_adjust_cfa_offset -4" );
954 output( "\tpopl %%ecx\n" );
955 output_cfi( ".cfi_adjust_cfa_offset -4" );
956 output( "\tjmp *%%eax\n" );
959 output( "\tsubq $88,%%rsp\n" );
960 output_cfi( ".cfi_adjust_cfa_offset 88" );
961 output( "\tmovq %%rdx,80(%%rsp)\n" );
962 output( "\tmovq %%rcx,72(%%rsp)\n" );
963 output( "\tmovq %%r8,64(%%rsp)\n" );
964 output( "\tmovq %%r9,56(%%rsp)\n" );
965 output( "\tmovq %%r10,48(%%rsp)\n" );
966 output( "\tmovq %%r11,40(%%rsp)\n" );
967 output( "\tmovq %%rax,%%rcx\n" );
968 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
969 output( "\tmovq 40(%%rsp),%%r11\n" );
970 output( "\tmovq 48(%%rsp),%%r10\n" );
971 output( "\tmovq 56(%%rsp),%%r9\n" );
972 output( "\tmovq 64(%%rsp),%%r8\n" );
973 output( "\tmovq 72(%%rsp),%%rcx\n" );
974 output( "\tmovq 80(%%rsp),%%rdx\n" );
975 output( "\taddq $88,%%rsp\n" );
976 output_cfi( ".cfi_adjust_cfa_offset -88" );
977 output( "\tjmp *%%rax\n" );
980 output( "\tsave %%sp, -96, %%sp\n" );
981 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
982 output( "\tmov %%g1, %%o0\n" );
983 output( "\tjmp %%o0\n" );
984 output( "\trestore\n" );
987 output( "\tstmfd SP!, {r4-r10,FP,LR}\n" );
988 output( "\tmov LR,PC\n");
989 output( "\tadd LR,LR,#8\n");
990 output( "\tldr PC,[PC,#-4]\n");
991 output( "\t.long %s\n", asm_name("__wine_spec_delay_load") );
992 output( "\tmov IP,r0\n");
993 output( "\tldmfd SP!, {r4-r10,FP,LR}\n" );
994 output( "\tldmfd SP!, {r0-r3}\n" );
995 output( "\tmov PC,IP\n");
998 if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
1000 /* Save all callee saved registers into a stackframe. */
1001 output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
1002 output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1003 output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1004 output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1005 output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1006 output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1007 output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1008 output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1009 output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1010 output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1011 output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1013 /* r0 -> r3 (arg1) */
1014 output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
1016 /* save return address */
1017 output( "\tmflr %s\n", ppc_reg(0));
1018 output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1020 /* Call the __wine_delay_load function, arg1 is arg1. */
1021 output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
1023 /* Load return value from call into ctr register */
1024 output( "\tmtctr %s\n", ppc_reg(3));
1026 /* restore all saved registers and drop stackframe. */
1027 output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
1028 output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
1029 output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
1030 output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
1031 output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
1032 output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
1033 output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
1034 output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
1035 output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
1036 output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
1038 /* Load return value from call into return register */
1039 output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
1040 output( "\tmtlr %s\n", ppc_reg(0));
1041 output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage);
1043 /* branch to ctr register. */
1044 output( "\tbctr\n");
1047 output_cfi( ".cfi_endproc" );
1048 output_function_size( "__wine_delay_load_asm" );
1051 for (i = idx = 0; i < nb_imports; i++)
1053 if (!dll_imports[i]->delay) continue;
1054 for (j = 0; j < dll_imports[i]->nb_imports; j++)
1056 ORDDEF *odp = dll_imports[i]->imports[j];
1057 const char *name = odp->name ? odp->name : odp->export_name;
1059 output( ".L__wine_delay_imp_%d_%s:\n", i, name );
1060 output_cfi( ".cfi_startproc" );
1064 output( "\tmovl $%d, %%eax\n", (idx << 16) | j );
1065 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1068 output( "\tmovq $%d,%%rax\n", (idx << 16) | j );
1069 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1072 output( "\tset %d, %%g1\n", (idx << 16) | j );
1073 output( "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1074 output( "\tnop\n" );
1077 output( "\tstmfd SP!, {r0-r3}\n" );
1078 output( "\tmov r0, #%d\n", idx );
1079 output( "\tmov r1, #16384\n" );
1080 output( "\tmul r1, r0, r1\n" );
1081 output( "\tmov r0, r1\n" );
1082 output( "\tmov r1, #4\n" );
1083 output( "\tmul r1, r0, r1\n" );
1084 output( "\tmov r0, r1\n" );
1085 output( "\tadd r0, #%d\n", j );
1086 output( "\tldr PC,[PC,#-4]\n");
1087 output( "\t.long %s\n", asm_name("__wine_delay_load_asm") );
1090 switch(target_platform)
1092 case PLATFORM_APPLE:
1093 /* On Darwin we can use r0 and r2 */
1094 /* Upper part in r2 */
1095 output( "\tlis %s, %d\n", ppc_reg(2), idx);
1096 /* Lower part + r2 -> r0, Note we can't use r0 directly */
1097 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
1098 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1101 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1102 /* Save r13 on the stack */
1103 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1104 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1105 /* Upper part in r13 */
1106 output( "\tlis %s, %d\n", ppc_reg(13), idx);
1107 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1108 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
1110 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1111 output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1112 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1117 output_cfi( ".cfi_endproc" );
1121 output_function_size( delayed_import_loaders );
1123 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1124 output( "%s:\n", asm_name(delayed_import_thunks));
1125 for (i = pos = 0; i < nb_imports; i++)
1127 if (!dll_imports[i]->delay) continue;
1128 for (j = 0; j < dll_imports[i]->nb_imports; j++, pos += get_ptr_size())
1130 ORDDEF *odp = dll_imports[i]->imports[j];
1131 output_import_thunk( odp->name ? odp->name : odp->export_name,
1132 ".L__wine_delay_IAT", pos );
1135 output_function_size( delayed_import_thunks );
1138 /* output import stubs for exported entry points that link to external symbols */
1139 static void output_external_link_imports( DLLSPEC *spec )
1141 unsigned int i, pos;
1143 if (!ext_link_imports.count) return; /* nothing to do */
1145 sort_names( &ext_link_imports );
1147 /* get rid of duplicate names */
1148 for (i = 1; i < ext_link_imports.count; i++)
1150 if (!strcmp( ext_link_imports.names[i-1], ext_link_imports.names[i] ))
1151 remove_name( &ext_link_imports, i-- );
1154 output( "\n/* external link thunks */\n\n" );
1155 output( "\t.data\n" );
1156 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1157 output( ".L__wine_spec_external_links:\n" );
1158 for (i = 0; i < ext_link_imports.count; i++)
1159 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.names[i]) );
1161 output( "\n\t.text\n" );
1162 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1163 output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1165 for (i = pos = 0; i < ext_link_imports.count; i++)
1167 char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.names[i] );
1168 output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
1170 pos += get_ptr_size();
1172 output_function_size( "__wine_spec_external_link_thunks" );
1175 /*******************************************************************
1178 * Output the functions for stub entry points
1180 void output_stubs( DLLSPEC *spec )
1182 const char *name, *exp_name;
1185 if (!has_stubs( spec )) return;
1187 output( "\n/* stub functions */\n\n" );
1188 output( "\t.text\n" );
1190 for (i = count = 0; i < spec->nb_entry_points; i++)
1192 ORDDEF *odp = &spec->entry_points[i];
1193 if (odp->type != TYPE_STUB) continue;
1195 name = get_stub_name( odp, spec );
1196 exp_name = odp->name ? odp->name : odp->export_name;
1197 output( "\t.align %d\n", get_alignment(4) );
1198 output( "\t%s\n", func_declaration(name) );
1199 output( "%s:\n", asm_name(name) );
1200 output_cfi( ".cfi_startproc" );
1205 /* flesh out the stub a bit to make safedisc happy */
1206 output(" \tnop\n" );
1207 output(" \tnop\n" );
1208 output(" \tnop\n" );
1209 output(" \tnop\n" );
1210 output(" \tnop\n" );
1211 output(" \tnop\n" );
1212 output(" \tnop\n" );
1213 output(" \tnop\n" );
1214 output(" \tnop\n" );
1216 output( "\tsubl $12,%%esp\n" );
1217 output_cfi( ".cfi_adjust_cfa_offset 12" );
1220 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1224 output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
1225 output( "\tmovl %%ecx,4(%%esp)\n" );
1229 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1230 output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1231 output( "\tmovl %%ecx,(%%esp)\n" );
1237 output( "\tmovl $.L%s_string,4(%%esp)\n", name );
1241 output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
1242 output( "\tmovl $.L__wine_spec_file_name,(%%esp)\n" );
1244 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1247 output( "\tsubq $8,%%rsp\n" );
1248 output_cfi( ".cfi_adjust_cfa_offset 8" );
1249 output( "\tleaq .L__wine_spec_file_name(%%rip),%%rdi\n" );
1252 output( "leaq .L%s_string(%%rip),%%rsi\n", name );
1256 output( "\tmovq $%d,%%rsi\n", odp->ordinal );
1257 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1262 output_cfi( ".cfi_endproc" );
1263 output_function_size( name );
1268 output( "\t%s\n", get_asm_string_section() );
1269 for (i = 0; i < spec->nb_entry_points; i++)
1271 ORDDEF *odp = &spec->entry_points[i];
1272 if (odp->type != TYPE_STUB) continue;
1273 exp_name = odp->name ? odp->name : odp->export_name;
1276 name = get_stub_name( odp, spec );
1277 output( ".L%s_string:\n", name );
1278 output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
1284 /* output the import and delayed import tables of a Win32 module */
1285 void output_imports( DLLSPEC *spec )
1287 output_immediate_imports();
1288 output_delayed_imports( spec );
1289 output_immediate_import_thunks();
1290 output_delayed_import_thunks( spec );
1291 output_external_link_imports( spec );
1292 if (nb_imports || ext_link_imports.count || has_stubs(spec) || has_relays(spec))
1293 output_get_pc_thunk();
1296 /* output an import library for a Win32 module and additional object files */
1297 void output_import_lib( DLLSPEC *spec, char **argv )
1299 struct strarray *args = strarray_init();
1302 if (target_platform != PLATFORM_WINDOWS)
1303 fatal_error( "Unix-style import libraries not supported yet\n" );
1305 def_file = get_temp_file_name( output_file_name, ".def" );
1306 fclose( output_file );
1307 if (!(output_file = fopen( def_file, "w" )))
1308 fatal_error( "Unable to create output file '%s'\n", def_file );
1309 output_def_file( spec, 0 );
1310 fclose( output_file );
1313 strarray_add( args, find_tool( "dlltool", NULL ), "-k", "-l", output_file_name, "-d", def_file, NULL );
1315 strarray_free( args );
1319 args = strarray_init();
1320 strarray_add( args, find_tool( "ar", NULL ), "rs", output_file_name, NULL );
1321 strarray_addv( args, argv );
1323 strarray_free( args );
1325 output_file_name = NULL;