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] ld: Ignore the new weak definition if needed
Date: Mon, 20 Jul 2020 06:34:29 -0700	[thread overview]
Message-ID: <20200720133429.648959-1-hjl.tools@gmail.com> (raw)

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


             reply	other threads:[~2020-07-20 13:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-20 13:34 H.J. Lu [this message]
2020-07-20 23:16 ` Alan Modra
2020-07-20 23:22   ` H.J. Lu
2020-07-21 13:37     ` [PATCH] ld: Properly override the IR definition H.J. Lu
2020-07-22  1:01       ` Alan Modra
2020-07-22  1:50         ` H.J. Lu
2020-07-22 12:31           ` H.J. Lu
2020-07-22 12:44             ` Nick Clifton

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=20200720133429.648959-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).