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>
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 */
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 */
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
) );
121 /* make a name table empty */
122 static inline void empty_name_table( struct name_table
*table
)
126 for (i
= 0; i
< table
->count
; i
++) free( table
->names
[i
] );
130 /* locate a name in a (sorted) list */
131 static inline const char *find_name( const char *name
, const struct name_table
*table
)
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
);
153 if (table
) res
= bsearch( &odp
, table
, size
, sizeof(*table
), func_cmp
);
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
);
168 /* check whether a given dll is imported in delayed mode */
169 static int is_delayed_import( const char *name
)
173 for (i
= 0; i
< delayed_imports
.count
; i
++)
175 if (!strcmp( delayed_imports
.names
[i
], name
)) return 1;
180 /* check whether a given dll has already been imported */
181 static struct import
*is_already_imported( const char *name
)
185 for (i
= 0; i
< nb_imports
; i
++)
187 if (!strcmp( dll_imports
[i
]->spec
->file_name
, name
)) return dll_imports
[i
];
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
)
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)
211 /* find the .def import library for a given dll */
212 static char *find_library( const char *name
)
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
);
225 /* read in the list of exported symbols of an import library */
226 static int read_import_lib( struct import
*imp
)
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
);
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
))
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
) );
269 qsort( imp
->exports
, imp
->nb_exports
, sizeof(*imp
->exports
), func_cmp
);
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
)
280 const char *basename
= strrchr( filename
, '/' );
281 if (!basename
) basename
= filename
;
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;
290 ret
= xmalloc( strlen(name
) + 5 );
293 if (!strchr( ret
, '.' )) strcat( ret
, ".dll" );
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
);
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
;
321 if (nb_errors
) exit(1);
325 /* add a library to the list of delayed imports */
326 void add_delayed_import( const char *name
)
329 char *fullname
= get_dll_name( name
, NULL
);
331 add_name( &delayed_imports
, fullname
);
332 if ((imp
= is_already_imported( fullname
)) && !imp
->delay
)
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) );
347 if (imp
->delay
) nb_delayed
--;
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
)
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
;
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
)
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;
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
)
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 */
432 /* flag the dll exports that link to an undefined symbol */
433 static void check_undefined_exports( DLLSPEC
*spec
)
437 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
439 ORDDEF
*odp
= &spec
->entry_points
[i
];
440 if (odp
->type
== TYPE_STUB
) continue;
441 if (odp
->flags
& FLAG_FORWARD
) continue;
442 if (find_name( odp
->link_name
, &undef_symbols
))
450 if (link_ext_symbols
)
452 odp
->flags
|= FLAG_EXT_LINK
;
453 add_name( &ext_link_imports
, odp
->link_name
);
455 else error( "%s:%d: function '%s' not defined\n",
456 spec
->src_name
, odp
->lineno
, odp
->link_name
);
459 error( "%s:%d: external symbol '%s' is not a function\n",
460 spec
->src_name
, odp
->lineno
, odp
->link_name
);
467 /* create a .o file that references all the undefined symbols we want to resolve */
468 static char *create_undef_symbols_file( DLLSPEC
*spec
)
470 char *as_file
, *obj_file
;
475 as_file
= get_temp_file_name( output_file_name
, ".s" );
476 if (!(f
= fopen( as_file
, "w" ))) fatal_error( "Cannot create %s\n", as_file
);
477 fprintf( f
, "\t.data\n" );
479 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
481 ORDDEF
*odp
= &spec
->entry_points
[i
];
482 if (odp
->type
== TYPE_STUB
) continue;
483 if (odp
->flags
& FLAG_FORWARD
) continue;
484 fprintf( f
, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp
->link_name
) );
486 for (j
= 0; j
< extra_ld_symbols
.count
; j
++)
487 fprintf( f
, "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols
.names
[j
]) );
490 obj_file
= get_temp_file_name( output_file_name
, ".o" );
491 assemble_file( as_file
, obj_file
);
495 /* combine a list of object files with ld into a single object file */
496 /* returns the name of the combined file */
497 static const char *ldcombine_files( DLLSPEC
*spec
, char **argv
)
499 unsigned int i
, len
= 0;
500 char *cmd
, *p
, *ld_tmp_file
, *undef_file
;
503 undef_file
= create_undef_symbols_file( spec
);
504 len
+= strlen(undef_file
) + 1;
505 ld_tmp_file
= get_temp_file_name( output_file_name
, ".o" );
506 if (!ld_command
) ld_command
= xstrdup("ld");
507 for (i
= 0; argv
[i
]; i
++) len
+= strlen(argv
[i
]) + 1;
508 cmd
= p
= xmalloc( len
+ strlen(ld_tmp_file
) + 8 + strlen(ld_command
) );
509 p
+= sprintf( cmd
, "%s -r -o %s %s", ld_command
, ld_tmp_file
, undef_file
);
510 for (i
= 0; argv
[i
]; i
++)
511 p
+= sprintf( p
, " %s", argv
[i
] );
512 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
514 if (err
) fatal_error( "%s -r failed with status %d\n", ld_command
, err
);
519 /* read in the list of undefined symbols */
520 void read_undef_symbols( DLLSPEC
*spec
, char **argv
)
524 char *cmd
, buffer
[1024], name_prefix
[16];
528 if (!argv
[0]) return;
530 add_extra_undef_symbols( spec
);
532 strcpy( name_prefix
, asm_name("") );
533 prefix_len
= strlen( name_prefix
);
535 name
= ldcombine_files( spec
, argv
);
537 if (!nm_command
) nm_command
= xstrdup("nm");
538 cmd
= xmalloc( strlen(nm_command
) + strlen(name
) + 5 );
539 sprintf( cmd
, "%s -u %s", nm_command
, name
);
540 if (!(f
= popen( cmd
, "r" )))
541 fatal_error( "Cannot execute '%s'\n", cmd
);
543 while (fgets( buffer
, sizeof(buffer
), f
))
545 char *p
= buffer
+ strlen(buffer
) - 1;
546 if (p
< buffer
) continue;
547 if (*p
== '\n') *p
-- = 0;
549 while (*p
== ' ') p
++;
550 if (p
[0] == 'U' && p
[1] == ' ' && p
[2]) p
+= 2;
551 if (prefix_len
&& !strncmp( p
, name_prefix
, prefix_len
)) p
+= prefix_len
;
552 add_name( &undef_symbols
, p
);
554 if ((err
= pclose( f
))) warning( "%s failed with status %d\n", cmd
, err
);
558 /* resolve the imports for a Win32 module */
559 int resolve_imports( DLLSPEC
*spec
)
562 unsigned int j
, removed
;
565 sort_names( &ignore_symbols
);
567 for (i
= 0; i
< nb_imports
; i
++)
569 struct import
*imp
= dll_imports
[i
];
571 for (j
= removed
= 0; j
< undef_symbols
.count
; j
++)
573 if (find_name( undef_symbols
.names
[j
], &ignore_symbols
)) continue;
574 odp
= find_export( undef_symbols
.names
[j
], imp
->exports
, imp
->nb_exports
);
577 add_import_func( imp
, odp
);
578 remove_name( &undef_symbols
, j
-- );
582 if (!removed
&& check_unused( imp
, spec
))
584 /* the dll is not used, get rid of it */
585 warning( "%s imported but no symbols used\n", imp
->spec
->file_name
);
586 remove_import_dll( i
);
591 sort_names( &undef_symbols
);
592 check_undefined_exports( spec
);
597 /* output the get_pc thunk if needed */
598 void output_get_pc_thunk(void)
600 if (target_cpu
!= CPU_x86
) return;
602 output( "\n\t.text\n" );
603 output( "\t.align %d\n", get_alignment(4) );
604 output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
605 output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
606 output( "\tpopl %%eax\n" );
607 output( "\tpushl %%eax\n" );
609 output_function_size( "__wine_spec_get_pc_thunk_eax" );
612 /* output a single import thunk */
613 static void output_import_thunk( const char *name
, const char *table
, int pos
)
615 output( "\n\t.align %d\n", get_alignment(4) );
616 output( "\t%s\n", func_declaration(name
) );
617 output( "%s\n", asm_globl(name
) );
624 output( "\tjmp *(%s+%d)\n", table
, pos
);
628 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
629 output( "1:\tjmp *%s+%d-1b(%%eax)\n", table
, pos
);
633 output( "\tjmpq *%s+%d(%%rip)\n", table
, pos
);
638 output( "\tsethi %%hi(%s+%d), %%g1\n", table
, pos
);
639 output( "\tld [%%g1+%%lo(%s+%d)], %%g1\n", table
, pos
);
640 output( "\tjmp %%g1\n" );
645 /* Hmpf. Stupid sparc assembler always interprets global variable
646 names as GOT offsets, so we have to do it the long way ... */
647 output( "\tsave %%sp, -96, %%sp\n" );
648 output( "0:\tcall 1f\n" );
650 output( "1:\tsethi %%hi(%s+%d-0b), %%g1\n", table
, pos
);
651 output( "\tor %%g1, %%lo(%s+%d-0b), %%g1\n", table
, pos
);
652 output( "\tld [%%g1+%%o7], %%g1\n" );
653 output( "\tjmp %%g1\n" );
654 output( "\trestore\n" );
658 output( "\tlda $0,%s\n", table
);
659 output( "\tlda $0,%d($0)\n", pos
);
660 output( "\tjmp $31,($0)\n" );
663 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
664 output( "\tstw %s, 0(%s)\n", ppc_reg(9), ppc_reg(1) );
665 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
666 output( "\tstw %s, 0(%s)\n", ppc_reg(8), ppc_reg(1) );
667 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1) );
668 output( "\tstw %s, 0(%s)\n", ppc_reg(7), ppc_reg(1) );
669 if (target_platform
== PLATFORM_APPLE
)
671 output( "\tlis %s, ha16(%s+%d)\n", ppc_reg(9), table
, pos
);
672 output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(8), table
, pos
, ppc_reg(9) );
676 output( "\tlis %s, (%s+%d)@h\n", ppc_reg(9), table
, pos
);
677 output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(8), table
, pos
, ppc_reg(9) );
679 output( "\tlwz %s, 0(%s)\n", ppc_reg(7), ppc_reg(8) );
680 output( "\tmtctr %s\n", ppc_reg(7) );
681 output( "\tlwz %s, 0(%s)\n", ppc_reg(7), ppc_reg(1) );
682 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
683 output( "\tlwz %s, 0(%s)\n", ppc_reg(8), ppc_reg(1) );
684 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
685 output( "\tlwz %s, 0(%s)\n", ppc_reg(9), ppc_reg(1) );
686 output( "\taddi %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1) );
687 output( "\tbctr\n" );
690 output_function_size( name
);
693 /* check if we need an import directory */
694 int has_imports(void)
696 return (nb_imports
- nb_delayed
) > 0;
699 /* output the import table of a Win32 module */
700 static void output_immediate_imports(void)
703 const char *dll_name
;
705 if (nb_imports
== nb_delayed
) return; /* no immediate imports */
707 /* main import header */
709 output( "\n/* import table */\n" );
710 output( "\n\t.data\n" );
711 output( "\t.align %d\n", get_alignment(4) );
712 output( ".L__wine_spec_imports:\n" );
716 for (i
= j
= 0; i
< nb_imports
; i
++)
718 if (dll_imports
[i
]->delay
) continue;
719 dll_name
= make_c_identifier( dll_imports
[i
]->spec
->file_name
);
720 output( "\t.long 0\n" ); /* OriginalFirstThunk */
721 output( "\t.long 0\n" ); /* TimeDateStamp */
722 output( "\t.long 0\n" ); /* ForwarderChain */
723 output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */
725 output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */
726 j
* get_ptr_size() );
727 j
+= dll_imports
[i
]->nb_imports
+ 1;
729 output( "\t.long 0\n" ); /* OriginalFirstThunk */
730 output( "\t.long 0\n" ); /* TimeDateStamp */
731 output( "\t.long 0\n" ); /* ForwarderChain */
732 output( "\t.long 0\n" ); /* Name */
733 output( "\t.long 0\n" ); /* FirstThunk */
735 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
736 output( ".L__wine_spec_import_data_ptrs:\n" );
737 for (i
= 0; i
< nb_imports
; i
++)
739 if (dll_imports
[i
]->delay
) continue;
740 dll_name
= make_c_identifier( dll_imports
[i
]->spec
->file_name
);
741 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
743 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
744 if (!(odp
->flags
& FLAG_NONAME
))
745 output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
746 get_asm_ptr_keyword(), dll_name
, odp
->name
);
749 if (get_ptr_size() == 8)
750 output( "\t.quad 0x800000000000%04x\n", odp
->ordinal
);
752 output( "\t.long 0x8000%04x\n", odp
->ordinal
);
755 output( "\t%s 0\n", get_asm_ptr_keyword() );
757 output( ".L__wine_spec_imports_end:\n" );
759 for (i
= 0; i
< nb_imports
; i
++)
761 if (dll_imports
[i
]->delay
) continue;
762 dll_name
= make_c_identifier( dll_imports
[i
]->spec
->file_name
);
763 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
765 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
766 if (!(odp
->flags
& FLAG_NONAME
))
768 output( "\t.align %d\n", get_alignment(2) );
769 output( ".L__wine_spec_import_data_%s_%s:\n", dll_name
, odp
->name
);
770 output( "\t%s %d\n", get_asm_short_keyword(), odp
->ordinal
);
771 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->name
);
776 for (i
= 0; i
< nb_imports
; i
++)
778 if (dll_imports
[i
]->delay
) continue;
779 dll_name
= make_c_identifier( dll_imports
[i
]->spec
->file_name
);
780 output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
781 dll_name
, get_asm_string_keyword(), dll_imports
[i
]->spec
->file_name
);
785 /* output the import thunks of a Win32 module */
786 static void output_immediate_import_thunks(void)
789 int nb_imm
= nb_imports
- nb_delayed
;
790 static const char import_thunks
[] = "__wine_spec_import_thunks";
794 output( "\n/* immediate import thunks */\n\n" );
795 output( "\t.text\n" );
796 output( "\t.align %d\n", get_alignment(8) );
797 output( "%s:\n", asm_name(import_thunks
));
799 for (i
= pos
= 0; i
< nb_imports
; i
++)
801 if (dll_imports
[i
]->delay
) continue;
802 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++, pos
+= get_ptr_size())
804 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
805 output_import_thunk( odp
->name
? odp
->name
: odp
->export_name
,
806 ".L__wine_spec_import_data_ptrs", pos
);
808 pos
+= get_ptr_size();
810 output_function_size( import_thunks
);
813 /* output the delayed import table of a Win32 module */
814 static void output_delayed_imports( const DLLSPEC
*spec
)
818 if (!nb_delayed
) return;
820 output( "\n/* delayed imports */\n\n" );
821 output( "\t.data\n" );
822 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
823 output( "%s\n", asm_globl("__wine_spec_delay_imports") );
827 for (i
= j
= mod
= 0; i
< nb_imports
; i
++)
829 if (!dll_imports
[i
]->delay
) continue;
830 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
831 output( "\t%s .L__wine_delay_name_%d\n", /* szName */
832 get_asm_ptr_keyword(), i
);
833 output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
834 get_asm_ptr_keyword(), mod
* get_ptr_size() );
835 output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
836 get_asm_ptr_keyword(), j
* get_ptr_size() );
837 output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
838 get_asm_ptr_keyword(), j
* get_ptr_size() );
839 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
840 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
841 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
842 j
+= dll_imports
[i
]->nb_imports
;
845 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
846 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
847 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
848 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
849 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
850 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
851 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
852 output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
854 output( "\n.L__wine_delay_IAT:\n" );
855 for (i
= 0; i
< nb_imports
; i
++)
857 if (!dll_imports
[i
]->delay
) continue;
858 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
860 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
861 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
862 output( "\t%s .L__wine_delay_imp_%d_%s\n",
863 get_asm_ptr_keyword(), i
, name
);
867 output( "\n.L__wine_delay_INT:\n" );
868 for (i
= 0; i
< nb_imports
; i
++)
870 if (!dll_imports
[i
]->delay
) continue;
871 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
873 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
875 output( "\t%s %d\n", get_asm_ptr_keyword(), odp
->ordinal
);
877 output( "\t%s .L__wine_delay_data_%d_%s\n",
878 get_asm_ptr_keyword(), i
, odp
->name
);
882 output( "\n.L__wine_delay_modules:\n" );
883 for (i
= 0; i
< nb_imports
; i
++)
885 if (dll_imports
[i
]->delay
) output( "\t%s 0\n", get_asm_ptr_keyword() );
888 for (i
= 0; i
< nb_imports
; i
++)
890 if (!dll_imports
[i
]->delay
) continue;
891 output( ".L__wine_delay_name_%d:\n", i
);
892 output( "\t%s \"%s\"\n",
893 get_asm_string_keyword(), dll_imports
[i
]->spec
->file_name
);
896 for (i
= 0; i
< nb_imports
; i
++)
898 if (!dll_imports
[i
]->delay
) continue;
899 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
901 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
902 if (!odp
->name
) continue;
903 output( ".L__wine_delay_data_%d_%s:\n", i
, odp
->name
);
904 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp
->name
);
907 output_function_size( "__wine_spec_delay_imports" );
910 /* output the delayed import thunks of a Win32 module */
911 static void output_delayed_import_thunks( const DLLSPEC
*spec
)
913 int i
, idx
, j
, pos
, extra_stack_storage
= 0;
914 static const char delayed_import_loaders
[] = "__wine_spec_delayed_import_loaders";
915 static const char delayed_import_thunks
[] = "__wine_spec_delayed_import_thunks";
917 if (!nb_delayed
) return;
919 output( "\n/* delayed import thunks */\n\n" );
920 output( "\t.text\n" );
921 output( "\t.align %d\n", get_alignment(8) );
922 output( "%s:\n", asm_name(delayed_import_loaders
));
923 output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
924 output( "%s:\n", asm_name("__wine_delay_load_asm") );
928 output( "\tpushl %%ecx\n" );
929 output( "\tpushl %%edx\n" );
930 output( "\tpushl %%eax\n" );
931 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
932 output( "\tpopl %%edx\n" );
933 output( "\tpopl %%ecx\n" );
934 output( "\tjmp *%%eax\n" );
937 output( "\tpushq %%rdi\n" );
938 output( "\tpushq %%rsi\n" );
939 output( "\tpushq %%rdx\n" );
940 output( "\tpushq %%rcx\n" );
941 output( "\tpushq %%r8\n" );
942 output( "\tpushq %%r9\n" );
943 output( "\tsubq $8,%%rsp\n" );
944 output( "\tmovq %%r11,%%rdi\n" );
945 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
946 output( "\taddq $8,%%rsp\n" );
947 output( "\tpopq %%r9\n" );
948 output( "\tpopq %%r8\n" );
949 output( "\tpopq %%rcx\n" );
950 output( "\tpopq %%rdx\n" );
951 output( "\tpopq %%rsi\n" );
952 output( "\tpopq %%rdi\n" );
953 output( "\tjmp *%%rax\n" );
956 output( "\tsave %%sp, -96, %%sp\n" );
957 output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
958 output( "\tmov %%g1, %%o0\n" );
959 output( "\tjmp %%o0\n" );
960 output( "\trestore\n" );
963 output( "\tjsr $26,%s\n", asm_name("__wine_spec_delay_load") );
964 output( "\tjmp $31,($0)\n" );
967 if (target_platform
== PLATFORM_APPLE
) extra_stack_storage
= 56;
969 /* Save all callee saved registers into a stackframe. */
970 output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage
, ppc_reg(1));
971 output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage
, ppc_reg(1));
972 output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage
, ppc_reg(1));
973 output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage
, ppc_reg(1));
974 output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage
, ppc_reg(1));
975 output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage
, ppc_reg(1));
976 output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage
, ppc_reg(1));
977 output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage
, ppc_reg(1));
978 output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage
, ppc_reg(1));
979 output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage
, ppc_reg(1));
980 output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage
, ppc_reg(1));
982 /* r0 -> r3 (arg1) */
983 output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
985 /* save return address */
986 output( "\tmflr %s\n", ppc_reg(0));
987 output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage
, ppc_reg(1));
989 /* Call the __wine_delay_load function, arg1 is arg1. */
990 output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
992 /* Load return value from call into ctr register */
993 output( "\tmtctr %s\n", ppc_reg(3));
995 /* restore all saved registers and drop stackframe. */
996 output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage
, ppc_reg(1));
997 output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage
, ppc_reg(1));
998 output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage
, ppc_reg(1));
999 output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage
, ppc_reg(1));
1000 output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage
, ppc_reg(1));
1001 output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage
, ppc_reg(1));
1002 output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage
, ppc_reg(1));
1003 output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage
, ppc_reg(1));
1004 output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage
, ppc_reg(1));
1005 output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage
, ppc_reg(1));
1007 /* Load return value from call into return register */
1008 output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage
, ppc_reg(1));
1009 output( "\tmtlr %s\n", ppc_reg(0));
1010 output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage
);
1012 /* branch to ctr register. */
1013 output( "\tbctr\n");
1016 output_function_size( "__wine_delay_load_asm" );
1019 for (i
= idx
= 0; i
< nb_imports
; i
++)
1021 if (!dll_imports
[i
]->delay
) continue;
1022 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++)
1024 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
1025 const char *name
= odp
->name
? odp
->name
: odp
->export_name
;
1027 output( ".L__wine_delay_imp_%d_%s:\n", i
, name
);
1031 output( "\tmovl $%d, %%eax\n", (idx
<< 16) | j
);
1032 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1035 output( "\tmovq $%d,%%r11\n", (idx
<< 16) | j
);
1036 output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
1039 output( "\tset %d, %%g1\n", (idx
<< 16) | j
);
1040 output( "\tb,a %s\n", asm_name("__wine_delay_load_asm") );
1043 output( "\tlda $0,%d($31)\n", j
);
1044 output( "\tldah $0,%d($0)\n", idx
);
1045 output( "\tjmp $31,%s\n", asm_name("__wine_delay_load_asm") );
1048 switch(target_platform
)
1050 case PLATFORM_APPLE
:
1051 /* On Darwin we can use r0 and r2 */
1052 /* Upper part in r2 */
1053 output( "\tlis %s, %d\n", ppc_reg(2), idx
);
1054 /* Lower part + r2 -> r0, Note we can't use r0 directly */
1055 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j
);
1056 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1059 /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
1060 /* Save r13 on the stack */
1061 output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
1062 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1063 /* Upper part in r13 */
1064 output( "\tlis %s, %d\n", ppc_reg(13), idx
);
1065 /* Lower part + r13 -> r0, Note we can't use r0 directly */
1066 output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j
);
1068 output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
1069 output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
1070 output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
1078 output_function_size( delayed_import_loaders
);
1080 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
1081 output( "%s:\n", asm_name(delayed_import_thunks
));
1082 for (i
= pos
= 0; i
< nb_imports
; i
++)
1084 if (!dll_imports
[i
]->delay
) continue;
1085 for (j
= 0; j
< dll_imports
[i
]->nb_imports
; j
++, pos
+= get_ptr_size())
1087 ORDDEF
*odp
= dll_imports
[i
]->imports
[j
];
1088 output_import_thunk( odp
->name
? odp
->name
: odp
->export_name
,
1089 ".L__wine_delay_IAT", pos
);
1092 output_function_size( delayed_import_thunks
);
1095 /* output import stubs for exported entry points that link to external symbols */
1096 static void output_external_link_imports( DLLSPEC
*spec
)
1098 unsigned int i
, pos
;
1100 if (!ext_link_imports
.count
) return; /* nothing to do */
1102 sort_names( &ext_link_imports
);
1104 /* get rid of duplicate names */
1105 for (i
= 1; i
< ext_link_imports
.count
; i
++)
1107 if (!strcmp( ext_link_imports
.names
[i
-1], ext_link_imports
.names
[i
] ))
1108 remove_name( &ext_link_imports
, i
-- );
1111 output( "\n/* external link thunks */\n\n" );
1112 output( "\t.data\n" );
1113 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1114 output( ".L__wine_spec_external_links:\n" );
1115 for (i
= 0; i
< ext_link_imports
.count
; i
++)
1116 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports
.names
[i
]) );
1118 output( "\n\t.text\n" );
1119 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
1120 output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
1122 for (i
= pos
= 0; i
< ext_link_imports
.count
; i
++)
1125 sprintf( buffer
, "__wine_spec_ext_link_%s", ext_link_imports
.names
[i
] );
1126 output_import_thunk( buffer
, ".L__wine_spec_external_links", pos
);
1127 pos
+= get_ptr_size();
1129 output_function_size( "__wine_spec_external_link_thunks" );
1132 /*******************************************************************
1135 * Output the functions for stub entry points
1137 void output_stubs( DLLSPEC
*spec
)
1139 const char *name
, *exp_name
;
1142 if (!has_stubs( spec
)) return;
1144 output( "\n/* stub functions */\n\n" );
1145 output( "\t.text\n" );
1147 for (i
= pos
= 0; i
< spec
->nb_entry_points
; i
++)
1149 ORDDEF
*odp
= &spec
->entry_points
[i
];
1150 if (odp
->type
!= TYPE_STUB
) continue;
1152 name
= get_stub_name( odp
, spec
);
1153 exp_name
= odp
->name
? odp
->name
: odp
->export_name
;
1154 output( "\t.align %d\n", get_alignment(4) );
1155 output( "\t%s\n", func_declaration(name
) );
1156 output( "%s:\n", asm_name(name
) );
1157 output( "\tsubl $4,%%esp\n" );
1161 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
1165 output( "\tleal .L__wine_stub_strings+%d-1b(%%eax),%%ecx\n", pos
);
1166 output( "\tpushl %%ecx\n" );
1167 pos
+= strlen(exp_name
) + 1;
1170 output( "\tpushl $%d\n", odp
->ordinal
);
1171 output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
1172 output( "\tpushl %%ecx\n" );
1178 output( "\tpushl $.L__wine_stub_strings+%d\n", pos
);
1179 pos
+= strlen(exp_name
) + 1;
1182 output( "\tpushl $%d\n", odp
->ordinal
);
1183 output( "\tpushl $.L__wine_spec_file_name\n" );
1185 output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
1186 output_function_size( name
);
1191 output( "\t%s\n", get_asm_string_section() );
1192 output( ".L__wine_stub_strings:\n" );
1193 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
1195 ORDDEF
*odp
= &spec
->entry_points
[i
];
1196 if (odp
->type
!= TYPE_STUB
) continue;
1197 exp_name
= odp
->name
? odp
->name
: odp
->export_name
;
1199 output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name
);
1204 /* output the import and delayed import tables of a Win32 module */
1205 void output_imports( DLLSPEC
*spec
)
1207 output_immediate_imports();
1208 output_delayed_imports( spec
);
1209 output_immediate_import_thunks();
1210 output_delayed_import_thunks( spec
);
1211 output_external_link_imports( spec
);
1212 if (nb_imports
|| ext_link_imports
.count
|| has_stubs(spec
) || has_relays(spec
))
1213 output_get_pc_thunk();