This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] ld: Set non_ir_ref_regular if relocs are checked after opening all inputs


If check_relocs is called after opening all inputs and the new defitinion
from an IR object overrides the old weak defitinion in a regular object,
set non_ir_ref_regular so that linker plugin will get the correct symbol
resolution.

OK for master?


H.J.
---
bfd/

	PR ld/22502
	* elflink.c (_bfd_elf_merge_symbol): Also skip definition from
	an IR object.
	* linker.c (_bfd_generic_link_add_one_symbol): If check_relocs
	is called after opening all inputs, set non_ir_ref_regular.

ld/

	PR ld/22502
	* testsuite/ld-plugin/lto.exp: Run PR ld/22502 test.
	* testsuite/ld-plugin/pr22502a.c: New file.
	* testsuite/ld-plugin/pr22502b.c: Likewise.
---
 bfd/elflink.c                     |  9 ++++++---
 bfd/linker.c                      | 14 +++++++++++++-
 ld/testsuite/ld-plugin/lto.exp    |  9 +++++++++
 ld/testsuite/ld-plugin/pr22502a.c | 16 ++++++++++++++++
 ld/testsuite/ld-plugin/pr22502b.c |  3 +++
 5 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 ld/testsuite/ld-plugin/pr22502a.c
 create mode 100644 ld/testsuite/ld-plugin/pr22502b.c

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 99f867dc75..4d94a5bbfa 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1555,10 +1555,13 @@ _bfd_elf_merge_symbol (bfd *abfd,
       sec = *psec;
     }
 
-  /* There are multiple definitions of a normal symbol.
-     Skip the default symbol as well.  */
+  /* There are multiple definitions of a normal symbol.  Skip the
+     default symbol as well as definition from an IR object.  */
   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
-      && !default_sym && h->def_regular)
+      && !default_sym && h->def_regular
+      && !(oldbfd != NULL
+	   && (oldbfd->flags & BFD_PLUGIN) != 0
+	   && (abfd->flags & BFD_PLUGIN) == 0))
     {
       /* Handle a multiple definition.  */
       (*info->callbacks->multiple_definition) (info, &h->root,
diff --git a/bfd/linker.c b/bfd/linker.c
index a96c6ed1dd..f1dbc3c342 100644
--- a/bfd/linker.c
+++ b/bfd/linker.c
@@ -1480,8 +1480,20 @@ _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
 	  {
 	    enum bfd_link_hash_type oldtype;
 
-	    /* Define a symbol.  */
 	    oldtype = h->type;
+
+	    /* If check_relocs is called after opening all inputs and
+	       the new defitinion from an IR object overrides the old
+	       weak defitinion in a regular object, set non_ir_ref_regular
+	       so that linker plugin will get the correct symbol
+	       resolution.  */
+	    if (info->check_relocs_after_open_input
+		&& (abfd->flags & BFD_PLUGIN) != 0
+		&& oldtype == bfd_link_hash_defweak
+		&& (h->u.def.section->owner->flags & BFD_PLUGIN) == 0)
+	      h->non_ir_ref_regular = 1;
+
+	    /* Define a symbol.  */
 	    if (action == DEFW)
 	      h->type = bfd_link_hash_defweak;
 	    else
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index d34479f378..56c852dda3 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -208,6 +208,12 @@ set lto_link_tests [list \
    "-flto -Wl,-plugin,$plug_so" "-flto" \
    {pr20321.c} {{warning ".*: duplicated plugin"}} \
    "pr20321" "c"] \
+  [list "Build pr22502a.o" \
+   "" "" \
+   {pr22502a.c}] \
+  [list "Build pr22502b.o" \
+   "$plug_opt" "-flto $lto_no_fat" \
+   {pr22502b.c}] \
 ]
 
 if { [at_least_gcc_version 4 7] } {
@@ -391,6 +397,9 @@ set lto_run_tests [list \
   [list "Run pr20267b" \
    "-O2 -flto tmpdir/pr20267a.o tmpdir/libpr20267b.a" "" \
    {dummy.c} "pr20267b" "pass.out" "-flto -O2" "c"] \
+  [list "Run pr22502" \
+   "-O2 -flto tmpdir/pr22502a.o tmpdir/pr22502b.o" "" \
+   {dummy.c} "pr20267" "pass.out" "-flto -O2" "c"] \
 ]
 
 if { [at_least_gcc_version 4 7] } {
diff --git a/ld/testsuite/ld-plugin/pr22502a.c b/ld/testsuite/ld-plugin/pr22502a.c
new file mode 100644
index 0000000000..0eaa1affd8
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr22502a.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+volatile int x;
+extern void abort ();
+
+__attribute__((weak))
+void foobar (void) { x++; }
+
+int main (void)
+{
+  foobar ();
+  if (x != -1)
+    abort ();
+  printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/pr22502b.c b/ld/testsuite/ld-plugin/pr22502b.c
new file mode 100644
index 0000000000..87389b99e4
--- /dev/null
+++ b/ld/testsuite/ld-plugin/pr22502b.c
@@ -0,0 +1,3 @@
+extern volatile int x;
+
+void foobar (void) { x--; }
-- 
2.14.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]