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]

Re: [PATCH] ld: Set non_ir_ref_regular on symbols referenced in regular objects


On Mon, Nov 27, 2017 at 2:57 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Mon, Nov 27, 2017 at 11:29 AM, H.J. Lu <hongjiu.lu@intel.com> wrote:
>> 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.
>>
>
> Here is a better patch.
>

The updated path to check for badly formatted input files.


-- 
H.J.
From c523a17b030a74265fd5927431d0326792e39aee Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 27 Nov 2017 11:27:59 -0800
Subject: [PATCH] ld: Set non_ir_ref_regular on symbols referenced in regular
 objects

If linker plugin is enabled, set non_ir_ref_regular on symbols referenced
in regular objects so that linker plugin will get the correct symbol
resolution.

bfd/

	PR ld/22502
	* elflink.c (_bfd_elf_merge_symbol): Also skip definition from
	an IR object.
	(elf_link_add_object_symbols): If linker plugin is enabled, set
	non_ir_ref_regular on symbols referenced in regular objects so
	that linker plugin will get the correct symbol resolution.

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                     | 68 +++++++++++++++++++++++++++++++++++++--
 ld/testsuite/ld-plugin/lto.exp    |  9 ++++++
 ld/testsuite/ld-plugin/pr22502a.c | 16 +++++++++
 ld/testsuite/ld-plugin/pr22502b.c |  3 ++
 4 files changed, 93 insertions(+), 3 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..ddd088c144 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,
@@ -4931,6 +4934,65 @@ error_free_dyn:
 	}
     }
 
+  if (info->lto_plugin_active
+      && !bfd_link_relocatable (info)
+      && (abfd->flags & BFD_PLUGIN) == 0
+      && !just_syms
+      && extsymcount)
+    {
+      int r_sym_shift;
+
+      if (bed->s->arch_size == 32)
+	r_sym_shift = 8;
+      else
+	r_sym_shift = 32;
+
+      /* If linker plugin is enabled, set non_ir_ref_regular on symbols
+	 referenced in regular objects so that linker plugin will get
+	 the correct symbol resolution.  */
+
+      sym_hash = elf_sym_hashes (abfd);
+      for (s = abfd->sections; s != NULL; s = s->next)
+	{
+	  Elf_Internal_Rela *internal_relocs;
+	  Elf_Internal_Rela *rel, *relend;
+
+	  /* Don't check relocations in excluded sections.  */
+	  if ((s->flags & SEC_RELOC) == 0
+	      || s->reloc_count == 0
+	      || (s->flags & SEC_EXCLUDE) != 0
+	      || ((info->strip == strip_all
+		   || info->strip == strip_debugger)
+		  && (s->flags & SEC_DEBUGGING) != 0))
+	    continue;
+
+	  internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
+						       NULL,
+						       info->keep_memory);
+	  if (internal_relocs == NULL)
+	    goto error_free_vers;
+
+	  rel = internal_relocs;
+	  relend = rel + s->reloc_count;
+	  for ( ; rel < relend; rel++)
+	    {
+	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
+	      struct elf_link_hash_entry *h;
+
+	      /* Skip local symbols.  */
+	      if (r_symndx < extsymoff)
+		continue;
+
+	      h = sym_hash[r_symndx - extsymoff];
+	      if (h != NULL)
+		h->root.non_ir_ref_regular = 1;
+	    }
+
+	  if (elf_section_data (s)->relocs != internal_relocs)
+	    free (internal_relocs);
+	}
+    }
+
   if (extversym != NULL)
     {
       free (extversym);
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]