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] PR ld/17689: PIE copy relocations handling broken with ld.bfd


I checked this patch into master and 2.25 branch to handle weak alias for
PIE with copy reloc.  When there is a weak symbol with a real definition,
the processor independent code will have arranged for us to see the real
definition first.  We need to copy the needs_copy bit from the real
definition and check it when allowing copy reloc in PIE.


H.J.
---
bfd/

	PR ld/17689
	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add needs_copy.
	Change has_bnd_reloc to bit field.
	(elf_x86_64_link_hash_newfunc): Initialize needs_copy and
	has_bnd_reloc to 0.
	(elf_x86_64_check_relocs): Set has_bnd_reloc to 1 instead
	of TRUE.
	(elf_x86_64_adjust_dynamic_symbol): Copy needs_copy from the
	real definition to a weak symbol.
	(elf_x86_64_allocate_dynrelocs): Also check needs_copy of a
	weak symbol for PIE when discarding space for relocs against
	symbols which turn out to need copy relocs.
	(elf_x86_64_relocate_section): Also check needs_copy of a
	weak symbol for PIE with copy reloc.

ld/testsuite/

	PR ld/17689
	* ld-x86-64/pr17689.out: New file.
	* ld-x86-64/pr17689.rd: Likewise.
	* ld-x86-64/pr17689a.c: Likewise.
	* ld-x86-64/pr17689b.S: Likewise.

	* ld-x86-64/x86-64.exp: Run PR ld/17689 tests.
---
 bfd/ChangeLog                      | 17 +++++++++++++++++
 bfd/elf64-x86-64.c                 | 27 ++++++++++++++++++++-------
 ld/testsuite/ChangeLog             | 10 ++++++++++
 ld/testsuite/ld-x86-64/pr17689.out |  1 +
 ld/testsuite/ld-x86-64/pr17689.rd  |  3 +++
 ld/testsuite/ld-x86-64/pr17689a.c  | 10 ++++++++++
 ld/testsuite/ld-x86-64/pr17689b.S  | 12 ++++++++++++
 ld/testsuite/ld-x86-64/x86-64.exp  | 24 ++++++++++++++++++++++++
 8 files changed, 97 insertions(+), 7 deletions(-)
 create mode 100644 ld/testsuite/ld-x86-64/pr17689.out
 create mode 100644 ld/testsuite/ld-x86-64/pr17689.rd
 create mode 100644 ld/testsuite/ld-x86-64/pr17689a.c
 create mode 100644 ld/testsuite/ld-x86-64/pr17689b.S

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index d783e3e..23e4ba5 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,20 @@
+2014-12-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR ld/17689
+	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add needs_copy.
+	Change has_bnd_reloc to bit field.
+	(elf_x86_64_link_hash_newfunc): Initialize needs_copy and
+	has_bnd_reloc to 0.
+	(elf_x86_64_check_relocs): Set has_bnd_reloc to 1 instead
+	of TRUE.
+	(elf_x86_64_adjust_dynamic_symbol): Copy needs_copy from the
+	real definition to a weak symbol.
+	(elf_x86_64_allocate_dynrelocs): Also check needs_copy of a
+	weak symbol for PIE when discarding space for relocs against
+	symbols which turn out to need copy relocs.
+	(elf_x86_64_relocate_section): Also check needs_copy of a
+	weak symbol for PIE with copy reloc.
+
 2014-12-12  Alan Modra  <amodra@gmail.com>
 
 	PR 15228
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 40a2a87..4aff9b0 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -757,8 +757,15 @@ struct elf_x86_64_link_hash_entry
   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
   unsigned char tls_type;
 
+  /* TRUE if a weak symbol with a real definition needs a copy reloc.
+     When there is a weak symbol with a real definition, the processor
+     independent code will have arranged for us to see the real
+     definition first.  We need to copy the needs_copy bit from the
+     real definition and check it when allowing copy reloc in PIE.  */
+  unsigned int needs_copy : 1;
+
   /* TRUE if symbol has at least one BND relocation.  */
-  bfd_boolean has_bnd_reloc;
+  unsigned int has_bnd_reloc : 1;
 
   /* Information about the GOT PLT entry. Filled when there are both
      GOT and PLT relocations against the same function.  */
@@ -897,7 +904,8 @@ elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
       eh = (struct elf_x86_64_link_hash_entry *) entry;
       eh->dyn_relocs = NULL;
       eh->tls_type = GOT_UNKNOWN;
-      eh->has_bnd_reloc = FALSE;
+      eh->needs_copy = 0;
+      eh->has_bnd_reloc = 0;
       eh->plt_bnd.offset = (bfd_vma) -1;
       eh->plt_got.offset = (bfd_vma) -1;
       eh->tlsdesc_got = (bfd_vma) -1;
@@ -1665,7 +1673,7 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
 		      && (get_elf_x86_64_backend_data (abfd)
 			  == &elf_x86_64_arch_bed))
 		{
-		  elf_x86_64_hash_entry (h)->has_bnd_reloc = TRUE;
+		  elf_x86_64_hash_entry (h)->has_bnd_reloc = 1;
 
 		  /* Create the second PLT for Intel MPX support.  */
 		  if (htab->plt_bnd == NULL)
@@ -2373,7 +2381,11 @@ elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
       h->root.u.def.section = h->u.weakdef->root.u.def.section;
       h->root.u.def.value = h->u.weakdef->root.u.def.value;
       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
-	h->non_got_ref = h->u.weakdef->non_got_ref;
+	{
+	  eh = (struct elf_x86_64_link_hash_entry *) h;
+	  h->non_got_ref = h->u.weakdef->non_got_ref;
+	  eh->needs_copy = h->u.weakdef->needs_copy;
+	}
       return TRUE;
     }
 
@@ -2733,7 +2745,7 @@ elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	  /* For PIE, discard space for relocs against symbols which
 	     turn out to need copy relocs.  */
 	  else if (info->executable
-		   && h->needs_copy
+		   && (h->needs_copy || eh->needs_copy)
 		   && h->def_dynamic
 		   && !h->def_regular)
 	    eh->dyn_relocs = NULL;
@@ -4058,7 +4070,8 @@ elf_x86_64_relocate_section (bfd *output_bfd,
 		     defined locally or for a branch.  */
 		  fail = !h->def_regular && !branch;
 		}
-	      else if (!(info->executable && h->needs_copy))
+	      else if (!(info->executable
+			 && (h->needs_copy || eh->needs_copy)))
 		{
 		  /* Symbol doesn't need copy reloc and isn't referenced
 		     locally.  We only allow branch to symbol with
@@ -4121,7 +4134,7 @@ direct:
 	  if ((info->shared
 	       && !(info->executable
 		    && h != NULL
-		    && h->needs_copy
+		    && (h->needs_copy || eh->needs_copy)
 		    && IS_X86_64_PCREL_TYPE (r_type))
 	       && (h == NULL
 		   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
diff --git a/ld/testsuite/ChangeLog b/ld/testsuite/ChangeLog
index b27177e..bcdcf0b 100644
--- a/ld/testsuite/ChangeLog
+++ b/ld/testsuite/ChangeLog
@@ -1,3 +1,13 @@
+2014-12-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR ld/17689
+	* ld-x86-64/pr17689.out: New file.
+	* ld-x86-64/pr17689.rd: Likewise.
+	* ld-x86-64/pr17689a.c: Likewise.
+	* ld-x86-64/pr17689b.S: Likewise.
+
+	* ld-x86-64/x86-64.exp: Run PR ld/17689 tests.
+
 2014-12-06  Eric Botcazou  <ebotcazou@adacore.com>
 
 	* lib/ld-lib.exp (check_shared_lib_support): Return 0 for Visium.
diff --git a/ld/testsuite/ld-x86-64/pr17689.out b/ld/testsuite/ld-x86-64/pr17689.out
new file mode 100644
index 0000000..7ef22e9
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr17689.out
@@ -0,0 +1 @@
+PASS
diff --git a/ld/testsuite/ld-x86-64/pr17689.rd b/ld/testsuite/ld-x86-64/pr17689.rd
new file mode 100644
index 0000000..d720a9b
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr17689.rd
@@ -0,0 +1,3 @@
+#...
+[0-9a-f ]+R_X86_64_COPY+[0-9a-f ]+ +bar \+ 0
+#...
diff --git a/ld/testsuite/ld-x86-64/pr17689a.c b/ld/testsuite/ld-x86-64/pr17689a.c
new file mode 100644
index 0000000..5317668
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr17689a.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+char *bar = "PASS";
+extern char *bar_alias __attribute__ ((weak, alias ("bar")));
+
+void
+foo (char *x)
+{
+  printf ("%s\n", x);
+}
diff --git a/ld/testsuite/ld-x86-64/pr17689b.S b/ld/testsuite/ld-x86-64/pr17689b.S
new file mode 100644
index 0000000..c95f891
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr17689b.S
@@ -0,0 +1,12 @@
+	.text
+	.globl	main
+	.type	main, @function
+main:
+	subq	$8, %rsp
+	movq	bar_alias(%rip), %rdi
+	call	foo@PLT
+	xorl	%eax, %eax
+	addq	$8, %rsp
+	ret
+	.size	main, .-main
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 253f0c6..443d101 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -416,6 +416,22 @@ if { [isnative] && [which $CC] != 0 } {
 	    {{readelf {-Wr} copyreloc-main2.rd}} \
 	    "copyreloc-main" \
 	] \
+	[list \
+	    "Build pr17689.so" \
+	    "-shared" \
+	    "-fPIC" \
+	    { pr17689a.c } \
+	    {} \
+	    "pr17689.so" \
+	] \
+	[list \
+	    "Build pr17689 with PIE without -fPIE" \
+	    "tmpdir/pr17689.so -pie" \
+	    "" \
+	    { pr17689b.S } \
+	    {{readelf {-Wr} pr17689.rd}} \
+	    "pr17689" \
+	] \
     ]
 
     run_ld_link_exec_tests [] [list \
@@ -446,6 +462,14 @@ if { [isnative] && [which $CC] != 0 } {
 	    "copyreloc-main" \
 	    "copyreloc-main.out" \
 	] \
+	[list \
+	    "Run pr17689 with PIE without -fPIE" \
+	    "tmpdir/pr17689.so -pie" \
+	    "" \
+	    { pr17689b.S } \
+	    "pr17689" \
+	    "pr17689.out" \
+	] \
     ]
 
     if { [istarget "x86_64-*-linux*"] \
-- 
1.9.3


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