aboutsummaryrefslogtreecommitdiff
path: root/ld/emultempl
diff options
context:
space:
mode:
authorEric Botcazou <ebotcazou@gcc.gnu.org>2018-04-04 12:07:50 +0200
committerEric Botcazou <ebotcazou@gcc.gnu.org>2018-04-04 12:07:50 +0200
commit317ff0084bc68bfae2eabd82015e9239a68b5195 (patch)
treee2d47e32cc864ebc5eee583b6496c0614753fae2 /ld/emultempl
parent4fb0d2b912ad079263ed98a3c4d78e5a7ccc93b0 (diff)
downloadgdb-317ff0084bc68bfae2eabd82015e9239a68b5195.zip
gdb-317ff0084bc68bfae2eabd82015e9239a68b5195.tar.gz
gdb-317ff0084bc68bfae2eabd82015e9239a68b5195.tar.bz2
Speed up direct linking with DLLs on Windows (1/2).
This patch deals with the auto-import feature. There are 2 versions of this feature: the original one, which was piggybacked on the OS loader with an optional help from the runtime (--enable-auto-import --enable-runtime-pseudo-reloc-v1) and is still the one mostly documented in the sources and manual; the enhanced one by Kai Tietz, which is entirely piggybacked on the runtime (--enable-auto-import --enable-runtime-pseudo-reloc-v2) and is the default for Mingw and Cygwin nowadays. The implementation is inefficient because of pe[p]_find_data_imports: for every undefined symbol, the function walks the entire set of relocations for all the input files and does a direct name comparison for each of them. This is easily fixable by using a hash-based map for v1 and a simple hash table for v2. This patch leaves v1 alone and only changes v2. It also factors out pe[p]_find_data_imports into a common function, removes old cruft left and right, and attempts to better separate the implementations of v1 and v2 in the code. ld/ * emultempl/pe.em (U_SIZE): Delete. (pe_data_import_dll): Likewise. (make_import_fixup): Return void, take 4th parameter and pass it down in call to pe_create_import_fixup. (pe_find_data_imports): Move to... (gld_${EMULATION_NAME}_after_open): Run the stdcall fixup pass after the auto-import pass and add a guard before running the latter. * emultempl/pep.em (U_SIZE): Delete. (pep_data_import_dll): Likewise. (make_import_fixup): Return void, take 4th parameter and pass it down in call to pe_create_import_fixup. (pep_find_data_imports): Move to... (gld_${EMULATION_NAME}_after_open): Run the stdcall fixup pass after the auto-import pass and add a guard before running the latter. * pe-dll.c (runtime_pseudp_reloc_v2_init): Change type to bfd_boolean. (pe_walk_relocs_of_symbol): Rename into... (pe_walk_relocs): ...this. Add 2 more parameters,4th parameter to the callback prototype and pass 4th parameter in calls to the callback. If the import hash table is present, invoke the callback on the reloc if the symbol name is in the table. (pe_find_data_imports): ...here. Take 2 parameters. Build an import hash table for the pseudo-relocation support version 2. When it is built, walk the relocations only once at the end; when it is not, do not build a fixup when the symbol isn't part of an import table. Issue the associated warning only after a first fixup is built. (tmp_seq2): Delete. (make_singleton_name_imp): Likewise. (make_import_fixup_mark): Return const char * and a stable string. (make_import_fixup_entry): Do not deal with the pseudo-relocation support version 2. (make_runtime_pseudo_reloc): Factor out code and fix formatting. (pe_create_import_fixup): Add 5th parameter. Clearly separate the pseudo-relocation support version 2 from the rest. Fix formatting. * pe-dll.h (pe_walk_relocs_of_symbol): Delete. (pe_find_data_imports): Declare. (pe_create_import_fixup): Add 5th parameter. * pep-dll.c (pe_data_import_dll): Delete. (pe_find_data_imports): Define. (pe_walk_relocs_of_symbol): Delete. * pep-dll.h (pep_walk_relocs_of_symbol): Delete. (pep_find_data_imports): Declare. (pep_create_import_fixup): Add 5th parameter. * ld.texinfo (--enable-auto-import): Adjust to new implementation.
Diffstat (limited to 'ld/emultempl')
-rw-r--r--ld/emultempl/pe.em143
-rw-r--r--ld/emultempl/pep.em121
2 files changed, 20 insertions, 244 deletions
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 5cc933a..06cfe7d 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -385,11 +385,6 @@ typedef struct
#define U(CSTR) \
((is_underscoring () == 0) ? CSTR : "_" CSTR)
-/* Get size of constant string for a possible underscore prefixed
- C visible symbol. */
-#define U_SIZE(CSTR) \
- (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
-
#define D(field,symbol,def,usc) {&pe.field, sizeof (pe.field), def, symbol, 0, usc}
static definfo init[] =
@@ -1022,13 +1017,6 @@ gld_${EMULATION_NAME}_after_parse (void)
after_parse_default ();
}
-/* pe-dll.c directly accesses pe_data_import_dll,
- so it must be defined outside of #ifdef DLL_SUPPORT.
- Note - this variable is deliberately not initialised.
- This allows it to be treated as a common varaible, and only
- exist in one incarnation in a multiple target enabled linker. */
-char * pe_data_import_dll;
-
#ifdef DLL_SUPPORT
static struct bfd_link_hash_entry *pe_undef_found_sym;
@@ -1129,11 +1117,12 @@ pe_fixup_stdcalls (void)
}
}
-static int
-make_import_fixup (arelent *rel, asection *s, char *name)
+static void
+make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
{
struct bfd_symbol *sym = *rel->sym_ptr_ptr;
char addend[4];
+ bfd_vma _addend;
if (pe_dll_extra_pe_debug)
printf ("arelent: %s@%#lx: add=%li\n", sym->name,
@@ -1143,117 +1132,8 @@ make_import_fixup (arelent *rel, asection *s, char *name)
einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
s->owner, s, rel->address);
- pe_create_import_fixup (rel, s, bfd_get_32 (s->owner, addend), name);
-
- return 1;
-}
-
-static void
-pe_find_data_imports (void)
-{
- struct bfd_link_hash_entry *undef, *sym;
- size_t namelen;
- char *buf, *name;
-
- if (link_info.pei386_auto_import == 0)
- return;
-
- namelen = 0;
- for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
- {
- if (undef->type == bfd_link_hash_undefined)
- {
- size_t len = strlen (undef->root.string);
- if (namelen < len)
- namelen = len;
- }
- }
- if (namelen == 0)
- return;
-
- /* We are being a bit cunning here. The buffer will have space for
- prefixes at the beginning. The prefix is modified here and in a
- number of functions called from this function. */
-#define PREFIX_LEN 32
- buf = xmalloc (PREFIX_LEN + namelen + 1);
- name = buf + PREFIX_LEN;
-
- for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
- {
- if (undef->type == bfd_link_hash_undefined)
- {
- char *impname;
-
- if (pe_dll_extra_pe_debug)
- printf ("%s:%s\n", __FUNCTION__, undef->root.string);
-
- strcpy (name, undef->root.string);
- impname = name - (sizeof "__imp_" - 1);
- memcpy (impname, "__imp_", sizeof "__imp_" - 1);
-
- sym = bfd_link_hash_lookup (link_info.hash, impname, 0, 0, 1);
-
- if (sym && sym->type == bfd_link_hash_defined)
- {
- bfd *b = sym->u.def.section->owner;
- asymbol **symbols;
- int nsyms, i;
-
- if (link_info.pei386_auto_import == -1)
- {
- static bfd_boolean warned = FALSE;
-
- info_msg (_("Info: resolving %s by linking to %s "
- "(auto-import)\n"), name, impname);
-
- /* PR linker/4844. */
- if (! warned)
- {
- warned = TRUE;
- einfo (_("%P: warning: auto-importing has been activated "
- "without --enable-auto-import specified on the "
- "command line; this should work unless it "
- "involves constant data structures referencing "
- "symbols from auto-imported DLLs\n"));
- }
- }
-
- if (!bfd_generic_link_read_symbols (b))
- {
- einfo (_("%F%P: %pB: could not read symbols: %E\n"), b);
- return;
- }
-
- symbols = bfd_get_outsymbols (b);
- nsyms = bfd_get_symcount (b);
-
- for (i = 0; i < nsyms; i++)
- {
- if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
- continue;
-
- if (pe_dll_extra_pe_debug)
- printf ("->%s\n", symbols[i]->name);
-
- pe_data_import_dll = (char *) (symbols[i]->name
- + U_SIZE ("_head_") - 1);
- break;
- }
-
- pe_walk_relocs_of_symbol (&link_info, name, make_import_fixup);
-
- /* Let's differentiate it somehow from defined. */
- undef->type = bfd_link_hash_defweak;
- /* We replace original name with __imp_ prefixed, this
- 1) may trash memory 2) leads to duplicate symbol generation.
- Still, IMHO it's better than having name polluted. */
- undef->root.string = sym->root.string;
- undef->u.def.value = sym->u.def.value;
- undef->u.def.section = sym->u.def.section;
- }
- }
- }
- free (buf);
+ _addend = bfd_get_32 (s->owner, addend);
+ pe_create_import_fixup (rel, s, _addend, name, symname);
}
static bfd_boolean
@@ -1523,16 +1403,15 @@ gld_${EMULATION_NAME}_after_open (void)
pe_output_file_set_long_section_names (link_info.output_bfd);
#ifdef DLL_SUPPORT
- if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
- pe_fixup_stdcalls ();
-
pe_process_import_defs (link_info.output_bfd, &link_info);
- pe_find_data_imports ();
+ if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
+ pe_find_data_imports (U ("_head_"), make_import_fixup);
- /* As possibly new symbols are added by imports, we rerun
- stdcall/fastcall fixup here. */
- if (pe_enable_stdcall_fixup) /* -1=warn or 1=disable */
+ /* The implementation of the feature is rather dumb and would cause the
+ compilation time to go through the roof if there are many undefined
+ symbols in the link, so it needs to be run after auto-import. */
+ if (pe_enable_stdcall_fixup) /* -1=warn or 1=enable */
pe_fixup_stdcalls ();
#if defined (TARGET_IS_i386pe) \
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 96942ec..1a7394e 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -356,11 +356,6 @@ typedef struct
#define U(CSTR) \
((is_underscoring () == 0) ? CSTR : "_" CSTR)
-/* Get size of constant string for a possible underscore prefixed
- C visible symbol. */
-#define U_SIZE(CSTR) \
- (sizeof (CSTR) + (is_underscoring () == 0 ? 0 : 1))
-
#define D(field,symbol,def,usc) {&pep.field, sizeof (pep.field), def, symbol, 0, usc}
static definfo init[] =
@@ -966,13 +961,6 @@ gld_${EMULATION_NAME}_after_parse (void)
after_parse_default ();
}
-/* pep-dll.c directly accesses pep_data_import_dll,
- so it must be defined outside of #ifdef DLL_SUPPORT.
- Note - this variable is deliberately not initialised.
- This allows it to be treated as a common varaible, and only
- exist in one incarnation in a multiple target enabled linker. */
-char * pep_data_import_dll;
-
#ifdef DLL_SUPPORT
static struct bfd_link_hash_entry *pep_undef_found_sym;
@@ -1074,8 +1062,8 @@ pep_fixup_stdcalls (void)
}
}
-static int
-make_import_fixup (arelent *rel, asection *s, char *name)
+static void
+make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
{
struct bfd_symbol *sym = *rel->sym_ptr_ptr;
char addend[8];
@@ -1128,98 +1116,8 @@ make_import_fixup (arelent *rel, asection *s, char *name)
printf (" pcrel");
printf (" %d bit rel.\n", (int) rel->howto->bitsize);
}
- pep_create_import_fixup (rel, s, _addend, name);
- return 1;
-}
-
-static void
-pep_find_data_imports (void)
-{
- struct bfd_link_hash_entry *undef, *sym;
- size_t namelen;
- char *buf, *name;
-
- if (link_info.pei386_auto_import == 0)
- return;
-
- namelen = 0;
- for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
- {
- if (undef->type == bfd_link_hash_undefined)
- {
- size_t len = strlen (undef->root.string);
- if (namelen < len)
- namelen = len;
- }
- }
- if (namelen == 0)
- return;
-
- /* We are being a bit cunning here. The buffer will have space for
- prefixes at the beginning. The prefix is modified here and in a
- number of functions called from this function. */
-#define PREFIX_LEN 32
- buf = xmalloc (PREFIX_LEN + namelen + 1);
- name = buf + PREFIX_LEN;
-
- for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
- {
- if (undef->type == bfd_link_hash_undefined)
- {
- char *impname;
-
- if (pep_dll_extra_pe_debug)
- printf ("%s:%s\n", __FUNCTION__, undef->root.string);
-
- strcpy (name, undef->root.string);
- impname = name - (sizeof "__imp_" - 1);
- memcpy (impname, "__imp_", sizeof "__imp_" - 1);
-
- sym = bfd_link_hash_lookup (link_info.hash, impname, 0, 0, 1);
-
- if (sym && sym->type == bfd_link_hash_defined)
- {
- bfd *b = sym->u.def.section->owner;
- asymbol **symbols;
- int nsyms, i;
-
- if (!bfd_generic_link_read_symbols (b))
- {
- einfo (_("%F%P: %pB: could not read symbols: %E\n"), b);
- return;
- }
-
- symbols = bfd_get_outsymbols (b);
- nsyms = bfd_get_symcount (b);
-
- for (i = 0; i < nsyms; i++)
- {
- if (! CONST_STRNEQ (symbols[i]->name, U ("_head_")))
- continue;
-
- if (pep_dll_extra_pe_debug)
- printf ("->%s\n", symbols[i]->name);
-
- pep_data_import_dll = (char *) (symbols[i]->name
- + U_SIZE ("_head_") - 1);
- break;
- }
-
- pep_walk_relocs_of_symbol (&link_info, name, make_import_fixup);
-
- /* Let's differentiate it somehow from defined. */
- undef->type = bfd_link_hash_defweak;
- /* We replace original name with __imp_ prefixed, this
- 1) may trash memory 2) leads to duplicate symbol generation.
- Still, IMHO it's better than having name polluted. */
- undef->root.string = sym->root.string;
- undef->u.def.value = sym->u.def.value;
- undef->u.def.section = sym->u.def.section;
- }
- }
- }
- free (buf);
+ pep_create_import_fixup (rel, s, _addend, name, symname);
}
static bfd_boolean
@@ -1491,16 +1389,15 @@ gld_${EMULATION_NAME}_after_open (void)
pep_output_file_set_long_section_names (link_info.output_bfd);
#ifdef DLL_SUPPORT
- if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
- pep_fixup_stdcalls ();
-
pep_process_import_defs (link_info.output_bfd, &link_info);
- pep_find_data_imports ();
+ if (link_info.pei386_auto_import) /* -1=warn or 1=enable */
+ pep_find_data_imports (U ("_head_"), make_import_fixup);
- /* As possibly new symbols are added by imports, we rerun
- stdcall/fastcall fixup here. */
- if (pep_enable_stdcall_fixup) /* -1=warn or 1=disable */
+ /* The implementation of the feature is rather dumb and would cause the
+ compilation time to go through the roof if there are many undefined
+ symbols in the link, so it needs to be run after auto-import. */
+ if (pep_enable_stdcall_fixup) /* -1=warn or 1=enable */
pep_fixup_stdcalls ();
#ifndef TARGET_IS_i386pep