From defb881754df013e337eb981bc54b5e83fd01fa4 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Sun, 7 May 2023 17:46:57 +0930 Subject: [PATCH] PR30343, LTO ignores linker reference to _pei386_runtime_relocator Make a reference to _pei386_runtime_relocator before LTO recompilation. This is done regardless of whether such a reference will be used, because it can't be known whether it is needed before LTO. I also found it necessary to enable long section names for the bfd created in make_runtime_pseudo_reloc, because otherwise when writing it out to the bfd-in-memory we get the section written as .rdata_r which when read back in leads to a linker warning ".rdata_r: section below image base" and likely runtime misbehaviour. PR 30343 * emultempl/pe.em (make_runtime_ref): New function. (gld${EMULATION_NAME}_before_plugin_all_symbols_read): New function. (LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ): Define. * emultempl/pep.em: Similarly to pe.em. * pe-dll.c (make_runtime_pseudo_reloc): Set long section names. --- ld/emultempl/pe.em | 33 +++++++++++++++++++++++++++++++++ ld/emultempl/pep.em | 33 +++++++++++++++++++++++++++++++++ ld/pe-dll.c | 1 + 3 files changed, 67 insertions(+) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 1f2a5f310be..4fe0cf203fb 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1255,6 +1255,28 @@ make_import_fixup (arelent *rel, asection *s, char *name, const char *symname) pe_create_import_fixup (rel, s, _addend, name, symname); } +static void +make_runtime_ref (void) +{ + const char *rr = U ("_pei386_runtime_relocator"); + struct bfd_link_hash_entry *h + = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info, + rr, true, false, true); + if (!h) + einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); + else + { + if (h->type == bfd_link_hash_new) + { + h->type = bfd_link_hash_undefined; + h->u.undef.abfd = NULL; + if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail) + bfd_link_add_undef (link_info.hash, h); + } + h->non_ir_ref_regular = true; + } +} + static bool pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED) { @@ -1459,6 +1481,16 @@ setup_build_id (bfd *ibfd) return false; } +static void +gld${EMULATION_NAME}_before_plugin_all_symbols_read (void) +{ +#ifdef DLL_SUPPORT + if (link_info.lto_plugin_active + && link_info.pei386_auto_import) + make_runtime_ref (); +#endif +} + static void gld${EMULATION_NAME}_after_open (void) { @@ -2488,6 +2520,7 @@ EOF fi LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse +LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation LDEMUL_FINISH=gld${EMULATION_NAME}_finish diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index ca4acaea148..5770df8ed0a 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1256,6 +1256,28 @@ make_import_fixup (arelent *rel, asection *s, char *name, const char *symname) pep_create_import_fixup (rel, s, _addend, name, symname); } +static void +make_runtime_ref (void) +{ + const char *rr = U ("_pei386_runtime_relocator"); + struct bfd_link_hash_entry *h + = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info, + rr, true, false, true); + if (!h) + einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); + else + { + if (h->type == bfd_link_hash_new) + { + h->type = bfd_link_hash_undefined; + h->u.undef.abfd = NULL; + if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail) + bfd_link_add_undef (link_info.hash, h); + } + h->non_ir_ref_regular = true; + } +} + static bool pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED) { @@ -1461,6 +1483,16 @@ setup_build_id (bfd *ibfd) return false; } +static void +gld${EMULATION_NAME}_before_plugin_all_symbols_read (void) +{ +#ifdef DLL_SUPPORT + if (link_info.lto_plugin_active + && link_info.pei386_auto_import) /* -1=warn or 1=enable */ + make_runtime_ref (); +#endif +} + static void gld${EMULATION_NAME}_after_open (void) { @@ -2323,6 +2355,7 @@ EOF fi LDEMUL_AFTER_PARSE=gld${EMULATION_NAME}_after_parse +LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=gld${EMULATION_NAME}_before_plugin_all_symbols_read LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation LDEMUL_FINISH=gld${EMULATION_NAME}_finish diff --git a/ld/pe-dll.c b/ld/pe-dll.c index ab13e3bb94a..8d74dba6af0 100644 --- a/ld/pe-dll.c +++ b/ld/pe-dll.c @@ -2720,6 +2720,7 @@ make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED, rt_rel = quick_section (abfd, ".rdata_runtime_pseudo_reloc", SEC_HAS_CONTENTS, 2); + bfd_coff_set_long_section_names (abfd, true); quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0); -- 2.30.2