]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
elf: Skip the archive if the symbol isn't referenced
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 16 Apr 2024 22:48:21 +0000 (15:48 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 17 Apr 2024 00:45:23 +0000 (17:45 -0700)
Also skip the archive if the symbol isn't referenced by a regular object.

bfd/

PR ld/31644
* elflink.c (elf_link_add_archive_symbols): Also skip the archive
if the symbol isn't referenced by a regular object.

ld/

PR ld/31644
* testsuite/ld-plugin/lto.exp: Run PR ld/31644 tests.
* testsuite/ld-plugin/pr31644a.c: New test.
* testsuite/ld-plugin/pr31644b.c: Likewise.
* testsuite/ld-plugin/pr31644c.c: Likewise.

bfd/elflink.c
ld/testsuite/ld-plugin/lto.exp
ld/testsuite/ld-plugin/pr31644a.c [new file with mode: 0644]
ld/testsuite/ld-plugin/pr31644b.c [new file with mode: 0644]
ld/testsuite/ld-plugin/pr31644c.c [new file with mode: 0644]

index 321e3d5e2ff614daeb20ff41b340b3a93aa0b789..9c53bfcf7d4378404c892c0ca0d67755e0258546 100644 (file)
@@ -6261,9 +6261,11 @@ elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
 
              if (!is_elf_hash_table (info->hash))
                continue;
-             /* Ignore the archive if the symbol isn't defined in a
-                shared object.  */
-             if (!((struct elf_link_hash_entry *) h)->def_dynamic)
+             struct elf_link_hash_entry *eh
+               = (struct elf_link_hash_entry *) h;
+             /* Ignore the archive if the symbol isn't referenced by a
+                regular object or isn't defined in a shared object.  */
+             if (!eh->ref_regular || !eh->def_dynamic)
                continue;
              /* Ignore the dynamic definition if symbol is first
                 defined in this archive.  */
index 35ce38731d0c8d1386bca8c1de7d16be7410afea..7b4b3492a2f02e3771815114032166e6d45a7834 100644 (file)
@@ -579,6 +579,22 @@ set lto_link_elf_tests [list \
    "" \
    "pr31615d.so" \
   ] \
+  [list \
+   "Build pr31644b.a" \
+   "" \
+   "" \
+   {pr31644b.c} \
+   "" \
+   "pr31644b.a" \
+  ] \
+  [list \
+   "Build pr31644c.so" \
+   "-shared" \
+   "-fPIC" \
+   {pr31644c.c} \
+   "" \
+   "pr31644c.so" \
+  ] \
 ]
 
 # PR 14918 checks that libgcc is not spuriously included in a shared link of
@@ -788,6 +804,14 @@ set lto_run_elf_shared_tests [list \
    {pr31615a.c} {pr31615b.exe} {pass.out} {-O3 -flto} {c} {} \
    {-Wl,--as-needed tmpdir/pr31615c.so -Wl,--no-as-needed \
     tmpdir/pr31615d.so}] \
+  [list {pr31644a} \
+   {-Wl,-R,tmpdir} {} \
+   {pr31644a.c} {pr31644a.exe} {pass.out} {-flto} {c} {} \
+   {-Wl,--no-as-needed tmpdir/pr31644b.a tmpdir/pr31644c.so}] \
+  [list {pr31644b} \
+   {-Wl,-R,tmpdir} {} \
+   {pr31644a.c} {pr31644b.exe} {pass.out} {-flto} {c} {} \
+   {-Wl,--as-needed tmpdir/pr31644b.a tmpdir/pr31644c.so}] \
 ]
 
 # LTO run-time tests for ELF
diff --git a/ld/testsuite/ld-plugin/pr31644a.c b/ld/testsuite/ld-plugin/pr31644a.c
new file mode 100644 (file)
index 0000000..1c03a95
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+void
+bar (void)
+{
+}
+
+int
+main()
+{
+  printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr31644b.c b/ld/testsuite/ld-plugin/pr31644b.c
new file mode 100644 (file)
index 0000000..8b23ec8
--- /dev/null
@@ -0,0 +1,7 @@
+extern void bar (void);
+
+void
+foo (void)
+{
+  bar ();
+}
diff --git a/ld/testsuite/ld-plugin/pr31644c.c b/ld/testsuite/ld-plugin/pr31644c.c
new file mode 100644 (file)
index 0000000..3d54205
--- /dev/null
@@ -0,0 +1,5 @@
+__attribute__ ((weak))
+void
+foo (void)
+{
+}