[PATCH] ld: Ignore the new weak definition if needed
H.J. Lu
hjl.tools@gmail.com
Mon Jul 20 13:34:29 GMT 2020
Ignore the new weak definition if the old definition comes from the LTO
IR object since plugin_notice will turn it into undefweak.
bfd/
PR ld/26262
* elflink.c (_bfd_elf_merge_symbol): Ignore the new weak
definition if the old definition comes from the LTO IR object.
ld/
PR ld/26262
* testsuite/ld-plugin/lto.exp: Run PR ld/26262 test.
* testsuite/ld-plugin/pr26262a.c: New file.
* testsuite/ld-plugin/pr26262b.c: Likewise.
* testsuite/ld-plugin/pr26262c.c: Likewise.
* testsuite/ld-plugin/pr26262d.c: Likewise.
---
bfd/elflink.c | 12 ++++++++++++
ld/testsuite/ld-plugin/lto.exp | 11 +++++++++++
ld/testsuite/ld-plugin/pr26262a.c | 17 +++++++++++++++++
ld/testsuite/ld-plugin/pr26262b.c | 5 +++++
ld/testsuite/ld-plugin/pr26262c.c | 16 ++++++++++++++++
ld/testsuite/ld-plugin/pr26262d.c | 6 ++++++
6 files changed, 67 insertions(+)
create mode 100644 ld/testsuite/ld-plugin/pr26262a.c
create mode 100644 ld/testsuite/ld-plugin/pr26262b.c
create mode 100644 ld/testsuite/ld-plugin/pr26262c.c
create mode 100644 ld/testsuite/ld-plugin/pr26262d.c
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 286fc11250..15d25d52d9 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1269,6 +1269,18 @@ _bfd_elf_merge_symbol (bfd *abfd,
&& h->root.type != bfd_link_hash_undefweak
&& h->root.type != bfd_link_hash_common);
+ /* NB: Ignore the new weak definition if the old definition comes
+ from the LTO IR object since plugin_notice will turn it into
+ undefweak. */
+ if (olddef
+ && oldbfd
+ && (oldbfd->flags & BFD_PLUGIN) != 0
+ && newweak)
+ {
+ *skip = TRUE;
+ return TRUE;
+ }
+
/* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
respectively, appear to be a function. */
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index 2d6ca6888b..3b9f8d4c5d 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -384,6 +384,12 @@ set lto_link_elf_tests [list \
"-shared -Wl,--exclude-libs,ALL tmpdir/pr25618a.o tmpdir/pr25618.a" \
"-fpic" \
{dummy.c} {{readelf {--dyn-syms --wide} pr25618.d}} "pr25618.so" "c++"] \
+ [list "Build pr26262c.o" \
+ "" "-O2 -fpic" \
+ {pr26262c.c} {} "" "c"] \
+ [list "Build pr26262d.o" \
+ "" "-O2 -fpic" \
+ {pr26262d.c} {} "" "c"] \
]
# PR 14918 checks that libgcc is not spuriously included in a shared link of
@@ -510,6 +516,11 @@ set lto_run_tests [list \
{pr26163b.c} "pr24406-2" "pass.out" \
"-flto -O2" "c" "" \
"tmpdir/pr26163a.o -Wl,--defsym,g=real_g"] \
+ [list "Run pr26262" \
+ "-O2 -flto" "" \
+ {pr26262a.c pr26262b.c} "pr26262" "pass.out" \
+ "-flto -O2" "c" "" \
+ "tmpdir/pr26262c.o tmpdir/pr26262d.o"] \
]
if { [at_least_gcc_version 4 7] } {
diff --git a/ld/testsuite/ld-plugin/pr26262a.c b/ld/testsuite/ld-plugin/pr26262a.c
new file mode 100644
index 0000000000..b1867cd578
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262a.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int counter;
+extern void foo (void);
+extern void bar (void);
+extern void xxx (void);
+
+int
+main(void)
+{
+ bar ();
+ foo ();
+ xxx ();
+ if (counter == 1)
+ printf ("PASS\n");
+ return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr26262b.c b/ld/testsuite/ld-plugin/pr26262b.c
new file mode 100644
index 0000000000..177e69c01e
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262b.c
@@ -0,0 +1,5 @@
+__attribute__ ((noinline))
+void
+bar (void)
+{
+}
diff --git a/ld/testsuite/ld-plugin/pr26262c.c b/ld/testsuite/ld-plugin/pr26262c.c
new file mode 100644
index 0000000000..91ec492ac7
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262c.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+
+extern int counter;
+
+void
+foo (void)
+{
+ counter++;
+}
+
+__attribute__((weak))
+void
+bar (void)
+{
+ abort ();
+}
diff --git a/ld/testsuite/ld-plugin/pr26262d.c b/ld/testsuite/ld-plugin/pr26262d.c
new file mode 100644
index 0000000000..fcc4941125
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr26262d.c
@@ -0,0 +1,6 @@
+extern void bar (void);
+void
+xxx (void)
+{
+ bar ();
+}
--
2.26.2
More information about the Binutils
mailing list