public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: binutils@sourceware.org
Subject: [PATCH] elf: Always honor the first definition in an archive
Date: Thu, 14 Mar 2024 10:05:56 -0700	[thread overview]
Message-ID: <20240314170556.444220-1-hjl.tools@gmail.com> (raw)

GCC doesn't put builtin function symbol references, which are defined in
the shared C library, in the IR symbol table.  When linker rescans
archives for newly added symbol references generated from the IR inputs,
it skips definitions of the builtin functions in archives.

Add archive_hash to bfd_link_info to track which symbols are defined in
archives first and always use them to resolve any references.

bfd/

	PR ld/31482
	* elflink.c (elf_link_add_object_symbols): Initialize archive_hash
	for an IR input.
	(_bfd_elf_archive_symbol_lookup): Add the unreferenced symbol to
	archive hash.
	(elf_link_add_archive_symbols): Use the symbol definition in
	archive if symbol is defined first in archive.

include/

	PR ld/31482
	* bfdlink.h (bfd_link_info): Add archive_hash.

ld/

	PR ld/31482
	* testsuite/ld-plugin/lto.exp: Add PR ld/31482 tests.
	* testsuite/ld-plugin/pass1.out: New file.
	* testsuite/ld-plugin/pr31482a.c: Likewise.
	* testsuite/ld-plugin/pr31482b.c: Likewise.
	* testsuite/ld-plugin/pr31482c.c: Likewise.
---
 bfd/elflink.c                     | 43 ++++++++++++++++++++++++++++---
 include/bfdlink.h                 |  3 +++
 ld/testsuite/ld-plugin/lto.exp    | 24 +++++++++++++++++
 ld/testsuite/ld-plugin/pass1.out  |  1 +
 ld/testsuite/ld-plugin/pr31482a.c |  8 ++++++
 ld/testsuite/ld-plugin/pr31482b.c |  9 +++++++
 ld/testsuite/ld-plugin/pr31482c.c |  9 +++++++
 7 files changed, 94 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pass1.out
 create mode 100644 ld/testsuite/ld-plugin/pr31482a.c
 create mode 100644 ld/testsuite/ld-plugin/pr31482b.c
 create mode 100644 ld/testsuite/ld-plugin/pr31482c.c

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 5a6cb07b2ce..066f94aa882 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -4265,7 +4265,21 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
     }
 
   if ((abfd->flags & DYNAMIC) == 0)
-    dynamic = false;
+    {
+      dynamic = false;
+      if ((abfd->flags & BFD_PLUGIN) != 0
+	  && info->archive_hash == NULL)
+	{
+	  /* Initialize archive_hash for an IR input.  */
+	  info->archive_hash = (struct bfd_hash_table *)
+	    xmalloc (sizeof (struct bfd_hash_table));
+	  if (!bfd_hash_table_init (info->archive_hash,
+				    bfd_hash_newfunc,
+				    sizeof (struct bfd_hash_entry)))
+	    info->callbacks->einfo
+	      (_("%F%P: archive_hash failed to initialize: %E\n"));
+	}
+    }
   else
     {
       dynamic = true;
@@ -5963,7 +5977,20 @@ _bfd_elf_archive_symbol_lookup (bfd *abfd,
 
   p = strchr (name, ELF_VER_CHR);
   if (p == NULL || p[1] != ELF_VER_CHR)
-    return h;
+    {
+      if (info->archive_hash != NULL)
+	{
+	  /* Add this symbol to archive hash since it is defined in an
+	     archive first.  */
+	  struct bfd_hash_entry *e
+	    = bfd_hash_lookup (info->archive_hash, name, true, false);
+	  if (e == NULL)
+	    info->callbacks->einfo
+	      (_("%F%P: %pB: failed to add %s to archive hash\n"),
+	       abfd, name);
+	}
+      return h;
+    }
 
   /* First check with only one `@'.  */
   len = strlen (name);
@@ -6102,7 +6129,17 @@ elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
 	      if (h->type != bfd_link_hash_undefweak)
 		/* Symbol must be defined.  Don't check it again.  */
 		included[i] = true;
-	      continue;
+
+	      /* Ignore the archive if the symbol isn't defined in a
+		 shared object.  */
+	      if (!((struct elf_link_hash_entry *) h)->def_dynamic)
+		continue;
+	      /* Ignore the dynamic definition if symbol is first
+		 defined in an archive.  */
+	      if (info->archive_hash != NULL
+		  && bfd_hash_lookup (info->archive_hash, symdef->name,
+				      false, false) == NULL)
+		continue;
 	    }
 
 	  /* We need to include this archive member.  */
diff --git a/include/bfdlink.h b/include/bfdlink.h
index eac07d78364..ef3fe06c24e 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -616,6 +616,9 @@ struct bfd_link_info
   /* Hash table handled by BFD.  */
   struct bfd_link_hash_table *hash;
 
+  /* Hash table of symbols which are first defined in archives.  */
+  struct bfd_hash_table *archive_hash;
+
   /* Hash table of symbols to keep.  This is NULL unless strip is
      strip_some.  */
   struct bfd_hash_table *keep_hash;
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index cf1691fec6d..984b2f6f6b2 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -539,6 +539,22 @@ set lto_link_elf_tests [list \
    "" \
    "pr30281.so" \
   ] \
+  [list \
+   "Build pr31482b.a" \
+   "" \
+   "" \
+   {pr31482b.c} \
+   "" \
+   "pr31482b.a" \
+  ] \
+  [list \
+   "Build pr31482c.so" \
+   "-shared" \
+   "-fPIC" \
+   {pr31482c.c} \
+   "" \
+   "pr31482c.so" \
+  ] \
 ]
 
 # PR 14918 checks that libgcc is not spuriously included in a shared link of
@@ -722,6 +738,14 @@ set lto_run_elf_shared_tests [list \
    {-Wl,--as-needed,-R,tmpdir} {} \
    {lto-19c.c} {lto-19.exe} {pass.out} {-flto -O2} {c} {} \
    {tmpdir/liblto-19.so tmpdir/liblto-19.a}] \
+  [list {pr31482a} \
+   {-Wl,--as-needed,-R,tmpdir} {} \
+   {pr31482a.c} {pr31482a.exe} {pass.out} {-flto} {c} {} \
+   {tmpdir/pr31482b.a tmpdir/pr31482c.so}] \
+  [list {pr31482b} \
+   {-Wl,--as-needed,-R,tmpdir} {} \
+   {pr31482a.c} {pr31482b.exe} {pass1.out} {-flto} {c} {} \
+   {tmpdir/pr31482c.so tmpdir/pr31482b.a}] \
 ]
 
 # LTO run-time tests for ELF
diff --git a/ld/testsuite/ld-plugin/pass1.out b/ld/testsuite/ld-plugin/pass1.out
new file mode 100644
index 00000000000..7ef22e9a431
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pass1.out
@@ -0,0 +1 @@
+PASS
diff --git a/ld/testsuite/ld-plugin/pr31482a.c b/ld/testsuite/ld-plugin/pr31482a.c
new file mode 100644
index 00000000000..0693e471123
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr31482a.c
@@ -0,0 +1,8 @@
+#include <stdlib.h>
+
+int
+main()
+{
+  abort ();
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr31482b.c b/ld/testsuite/ld-plugin/pr31482b.c
new file mode 100644
index 00000000000..3c241735c16
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr31482b.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+abort (void)
+{
+  printf ("PASS\n");
+  exit (0);
+}
diff --git a/ld/testsuite/ld-plugin/pr31482c.c b/ld/testsuite/ld-plugin/pr31482c.c
new file mode 100644
index 00000000000..5c3b5099b52
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr31482c.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+abort (void)
+{
+  printf ("PASS1\n");
+  exit (0);
+}
-- 
2.44.0


                 reply	other threads:[~2024-03-14 17:06 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=20240314170556.444220-1-hjl.tools@gmail.com \
    --to=hjl.tools@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).