PR30343, LTO ignores linker reference to _pei386_runtime_relocator
authorAlan Modra <amodra@gmail.com>
Sun, 7 May 2023 08:16:57 +0000 (17:46 +0930)
committerAlan Modra <amodra@gmail.com>
Mon, 8 May 2023 00:41:47 +0000 (10:11 +0930)
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
ld/emultempl/pep.em
ld/pe-dll.c

index 1f2a5f310be927e75fc695209a5806345a7b1df8..4fe0cf203fb7567d512cce305cc0bb813f31f696 100644 (file)
@@ -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
index ca4acaea1484d1537fc259a5713f5af0af963f37..5770df8ed0ad1c3ec4e5dfa358a0f754a3e84255 100644 (file)
@@ -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
index ab13e3bb94ad05adb605aae520461cf60d3b3b13..8d74dba6af0273971cd559a69129c9f5baac8a55 100644 (file)
@@ -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);