public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: PR30343, LTO ignores linker reference to _pei386_runtime_relocator
Date: Mon, 8 May 2023 14:04:37 +0930	[thread overview]
Message-ID: <ZFh73WJCS1aujdsO@squeak.grove.modra.org> (raw)

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.

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);
 

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-05-08  4:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZFh73WJCS1aujdsO@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).