public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
@ 2017-04-20 19:41 H.J. Lu
  2017-04-21  1:09 ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: H.J. Lu @ 2017-04-20 19:41 UTC (permalink / raw)
  To: binutils

On i386, since GOT reference is needed to access global symbols in PIE,
those symbols are made dynamic.  Crash happens when there is a reference
to the same global symbol with a different symbol type in a shared
object.  Since mixing different types of the same symbol doesn't work in
general, this patch skips those tests on i386 as well as compiles non-PIE
indirect5 and indirect6 tests with $NOPIE_LDFLAGS and $NOPIE_CFLAGS.

I am checking in this patch.

H.J.
---
	PR ld/21402
	* testsuite/ld-elf/indirect.exp: Pass $NOPIE_LDFLAGS and
	$NOPIE_CFLAGS to non-PIE indirect5 and indirect6 tests.  Skip
	PIE indirect5 and indirect6 tests on i386.
---
 ld/ChangeLog                     |  7 +++++++
 ld/testsuite/ld-elf/indirect.exp | 23 +++++++++++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index 7f5e389..fb88dc7 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,10 @@
+2017-04-20  H.J. Lu  <hongjiu.lu@intel.com>
+
+	PR ld/21402
+	* testsuite/ld-elf/indirect.exp: Pass $NOPIE_LDFLAGS and
+	$NOPIE_CFLAGS to non-PIE indirect5 and indirect6 tests.  Skip
+	PIE indirect5 and indirect6 tests on i386.
+
 2017-04-20  Maciej W. Rozycki  <macro@imgtec.com>
 
 	* testsuite/ld-mips-elf/mips-elf.exp: Join `__ehdr_start' tests.
diff --git a/ld/testsuite/ld-elf/indirect.exp b/ld/testsuite/ld-elf/indirect.exp
index 128d4a7..2fa374b 100644
--- a/ld/testsuite/ld-elf/indirect.exp
+++ b/ld/testsuite/ld-elf/indirect.exp
@@ -129,6 +129,8 @@ set testname "Indirect symbol 2"
 set cmd "$ld -shared  -o tmpdir/indirect2.so tmpdir/indirect2.o"
 check_link_message "$cmd" [list $string2 $string] "$testname"
 
+global NOPIE_CFLAGS NOPIE_LDFLAGS
+
 set run_tests {
     {"Run with libindirect3c.so 1"
      "-Wl,--no-as-needed tmpdir/indirect3a.o tmpdir/indirect3b.o tmpdir/libindirect3c.so" ""
@@ -155,17 +157,17 @@ set run_tests {
      "-Wl,--no-as-needed tmpdir/libindirect4c.so tmpdir/indirect4b.o tmpdir/indirect4a.o" ""
      {dummy.c} "indirect4d" "indirect4.out"}
     {"Run indirect5 1"
-     "-Wl,--no-as-needed tmpdir/libindirect5.so" ""
-     {indirect5a.c} "indirect5a" "indirect5.out"}
+     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libindirect5.so" ""
+     {indirect5a.c} "indirect5a" "indirect5.out" "$NOPIE_CFLAGS"}
     {"Run indirect5 2"
-     "-Wl,--no-as-needed tmpdir/indirect5a.o tmpdir/libindirect5.so" ""
-     {dummy.c} "indirect5b" "indirect5.out"}
+     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/indirect5a.o tmpdir/libindirect5.so" ""
+     {dummy.c} "indirect5b" "indirect5.out" "$NOPIE_CFLAGS"}
     {"Run indirect6 1"
-     "-Wl,--no-as-needed tmpdir/libindirect5.so" ""
-     {indirect6a.c} "indirect6a" "indirect5.out"}
+     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libindirect5.so" ""
+     {indirect6a.c} "indirect6a" "indirect5.out" "$NOPIE_CFLAGS"}
     {"Run indirect6 2"
-     "-Wl,--no-as-needed tmpdir/indirect6a.o tmpdir/libindirect5.so" ""
-     {dummy.c} "indirect6b" "indirect5.out"}
+     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/indirect6a.o tmpdir/libindirect5.so" ""
+     {dummy.c} "indirect6b" "indirect5.out" "$NOPIE_CFLAGS"}
     {"Run with libpr18720c.so 1"
      "-Wl,--no-as-needed tmpdir/pr18720a.o tmpdir/pr18720b.o tmpdir/libpr18720c.so" ""
      {check-ptr-eq.c} "pr18720a" "pr18720.out"}
@@ -220,6 +222,11 @@ foreach t [list indirect5a indirect5b indirect6a indirect6b] {
     }
 }
 
+# PR ld/21402: i386 doesn't support mixing different symbol types in PIE.
+if {[istarget "i?86-*-*"]} {
+    return
+}
+
 send_log "$CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie"
 catch "exec $CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie" exec_output
 send_log "$exec_output"
-- 
2.9.3

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
  2017-04-20 19:41 [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386 H.J. Lu
@ 2017-04-21  1:09 ` Alan Modra
  2017-04-21 18:37   ` H.J. Lu
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Modra @ 2017-04-21  1:09 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Thu, Apr 20, 2017 at 12:41:45PM -0700, H.J. Lu wrote:
> On i386, since GOT reference is needed to access global symbols in PIE,
> those symbols are made dynamic.  Crash happens when there is a reference
> to the same global symbol with a different symbol type in a shared
> object.  Since mixing different types of the same symbol doesn't work in
> general, this patch skips those tests on i386 as well as compiles non-PIE
> indirect5 and indirect6 tests with $NOPIE_LDFLAGS and $NOPIE_CFLAGS.
> 
> I am checking in this patch.

This is wrong.  i386 and other targets do need to handle different
symbol types.  The classic example is an executable defining a
variable called "time" or "times", which conflicts with the libc
functions of the same name.

Please revert the xfail.  It sets the wrong precedent for other target
maintainers.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
  2017-04-21  1:09 ` Alan Modra
@ 2017-04-21 18:37   ` H.J. Lu
  2017-04-24  4:10     ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: H.J. Lu @ 2017-04-21 18:37 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 975 bytes --]

On Thu, Apr 20, 2017 at 6:08 PM, Alan Modra <amodra@gmail.com> wrote:
> On Thu, Apr 20, 2017 at 12:41:45PM -0700, H.J. Lu wrote:
>> On i386, since GOT reference is needed to access global symbols in PIE,
>> those symbols are made dynamic.  Crash happens when there is a reference
>> to the same global symbol with a different symbol type in a shared
>> object.  Since mixing different types of the same symbol doesn't work in
>> general, this patch skips those tests on i386 as well as compiles non-PIE
>> indirect5 and indirect6 tests with $NOPIE_LDFLAGS and $NOPIE_CFLAGS.
>>
>> I am checking in this patch.
>
> This is wrong.  i386 and other targets do need to handle different
> symbol types.  The classic example is an executable defining a
> variable called "time" or "times", which conflicts with the libc
> functions of the same name.
>
> Please revert the xfail.  It sets the wrong precedent for other target
> maintainers.
>

I will check in this patch.


-- 
H.J.

[-- Attachment #2: 0001-i386-Avoid-dynamic-symbol-with-GOT-reference-in-PIE.patch --]
[-- Type: text/x-patch, Size: 5814 bytes --]

From ef6077f3e4708a12f2140684bb720aaee53889c4 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 21 Apr 2017 08:40:12 -0700
Subject: [PATCH] i386: Avoid dynamic symbol with GOT reference in PIE

GOT reference to global symbol in PIE will lead to dynamic symbol.  It
becomes a problem when "time" or "times" is defined as a variable in
an executable, clashing with functions of the same name in libc.  If
a symbol isn't undefined weak symbol, don't make it dynamic in PIE and
generate R_386_RELATIVE relocation.

bfd/

	PR ld/21402
	* elf32-i386.c (elf_i386_link_hash_entry): Add
	no_finish_dynamic_symbol.
	(elf_i386_link_hash_newfunc): Set no_finish_dynamic_symbol to 0.
	(elf_i386_allocate_dynrelocs): If a symbol isn't undefined weak
	symbol, don't make it dynamic in PIE.
	(elf_i386_relocate_section): If a symbol isn't dynamic in PIE,
	set no_finish_dynamic_symbol and generate R_386_RELATIVE
	relocation for R_386_GOT32
	(elf_i386_finish_dynamic_symbol): Abort if no_finish_dynamic_symbol
	isn't 0.

ld/

	PR ld/21402
	* testsuite/ld-elf/indirect.exp: Don't skip PIE indirect5 and
	indirect6 tests on i386.
---
 bfd/elf32-i386.c                 | 61 ++++++++++++++++++++++++++++------------
 ld/testsuite/ld-elf/indirect.exp |  5 ----
 2 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 7af6863..9e71974 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -820,6 +820,9 @@ struct elf_i386_link_hash_entry
   /* Symbol has non-GOT/non-PLT relocations in text sections.  */
   unsigned int has_non_got_reloc : 1;
 
+  /* Don't call finish_dynamic_symbol on this symbol.  */
+  unsigned int no_finish_dynamic_symbol : 1;
+
   /* 0: symbol isn't ___tls_get_addr.
      1: symbol is ___tls_get_addr.
      2: symbol is unknown.  */
@@ -959,6 +962,7 @@ elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
       eh->gotoff_ref = 0;
       eh->has_got_reloc = 0;
       eh->has_non_got_reloc = 0;
+      eh->no_finish_dynamic_symbol = 0;
       eh->tls_get_addr = 2;
       eh->func_pointer_refcount = 0;
       eh->plt_got.offset = (bfd_vma) -1;
@@ -2863,10 +2867,14 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       int tls_type = elf_i386_hash_entry(h)->tls_type;
 
       /* Make sure this symbol is output as a dynamic symbol.
-	 Undefined weak syms won't yet be marked as dynamic.  */
+	 Undefined weak syms won't yet be marked as dynamic.
+	 PR ld/21402: If this symbol isn't undefined weak symbol,
+	 don't make it dynamic in PIE.   */
       if (h->dynindx == -1
 	  && !h->forced_local
-	  && !resolved_to_zero)
+	  && !resolved_to_zero
+	  && (h->root.type == bfd_link_hash_undefweak
+	      || !bfd_link_pie (info)))
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
@@ -3818,6 +3826,7 @@ elf_i386_relocate_section (bfd *output_bfd,
       bfd_vma st_size;
       asection *resolved_plt;
       bfd_boolean resolved_to_zero;
+      bfd_boolean relative_reloc;
 
       r_type = ELF32_R_TYPE (rel->r_info);
       if (r_type == R_386_GNU_VTINHERIT
@@ -4268,6 +4277,7 @@ r_386_got32:
 	  if (htab->elf.sgot == NULL)
 	    abort ();
 
+	  relative_reloc = FALSE;
 	  if (h != NULL)
 	    {
 	      bfd_boolean dyn;
@@ -4301,6 +4311,17 @@ r_386_got32:
 		      bfd_put_32 (output_bfd, relocation,
 				  htab->elf.sgot->contents + off);
 		      h->got.offset |= 1;
+
+		      if (h->dynindx == -1
+			  && !h->forced_local
+			  && h->root.type != bfd_link_hash_undefweak
+			  && bfd_link_pie (info))
+			{
+			  /* PR ld/21402: If this symbol isn't dynamic
+			     in PIE, generate R_386_RELATIVE here.  */
+			  eh->no_finish_dynamic_symbol = 1;
+			  relative_reloc = TRUE;
+			}
 		    }
 		}
 	      else
@@ -4322,27 +4343,29 @@ r_386_got32:
 		{
 		  bfd_put_32 (output_bfd, relocation,
 			      htab->elf.sgot->contents + off);
+		  local_got_offsets[r_symndx] |= 1;
 
 		  if (bfd_link_pic (info))
-		    {
-		      asection *s;
-		      Elf_Internal_Rela outrel;
-
-		      s = htab->elf.srelgot;
-		      if (s == NULL)
-			abort ();
-
-		      outrel.r_offset = (htab->elf.sgot->output_section->vma
-					 + htab->elf.sgot->output_offset
-					 + off);
-		      outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
-		      elf_append_rel (output_bfd, s, &outrel);
-		    }
-
-		  local_got_offsets[r_symndx] |= 1;
+		    relative_reloc = TRUE;
 		}
 	    }
 
+	  if (relative_reloc)
+	    {
+	      asection *s;
+	      Elf_Internal_Rela outrel;
+
+	      s = htab->elf.srelgot;
+	      if (s == NULL)
+		abort ();
+
+	      outrel.r_offset = (htab->elf.sgot->output_section->vma
+				 + htab->elf.sgot->output_offset
+				 + off);
+	      outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
+	      elf_append_rel (output_bfd, s, &outrel);
+	    }
+
 	  if (off >= (bfd_vma) -2)
 	    abort ();
 
@@ -5321,6 +5344,8 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
   plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
 
   eh = (struct elf_i386_link_hash_entry *) h;
+  if (eh->no_finish_dynamic_symbol)
+    abort ();
 
   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
      resolved undefined weak symbols in executable so that their
diff --git a/ld/testsuite/ld-elf/indirect.exp b/ld/testsuite/ld-elf/indirect.exp
index 2fa374b..fd7d189 100644
--- a/ld/testsuite/ld-elf/indirect.exp
+++ b/ld/testsuite/ld-elf/indirect.exp
@@ -222,11 +222,6 @@ foreach t [list indirect5a indirect5b indirect6a indirect6b] {
     }
 }
 
-# PR ld/21402: i386 doesn't support mixing different symbol types in PIE.
-if {[istarget "i?86-*-*"]} {
-    return
-}
-
 send_log "$CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie"
 catch "exec $CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie" exec_output
 send_log "$exec_output"
-- 
2.9.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
  2017-04-21 18:37   ` H.J. Lu
@ 2017-04-24  4:10     ` Alan Modra
  2017-04-24 20:51       ` H.J. Lu
  2017-04-24 21:43       ` H.J. Lu
  0 siblings, 2 replies; 6+ messages in thread
From: Alan Modra @ 2017-04-24  4:10 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On Fri, Apr 21, 2017 at 11:37:34AM -0700, H.J. Lu wrote:
> @@ -2863,10 +2867,14 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
>        int tls_type = elf_i386_hash_entry(h)->tls_type;
>  
>        /* Make sure this symbol is output as a dynamic symbol.
> -	 Undefined weak syms won't yet be marked as dynamic.  */
> +	 Undefined weak syms won't yet be marked as dynamic.
> +	 PR ld/21402: If this symbol isn't undefined weak symbol,
> +	 don't make it dynamic in PIE.   */
>        if (h->dynindx == -1
>  	  && !h->forced_local
> -	  && !resolved_to_zero)
> +	  && !resolved_to_zero
> +	  && (h->root.type == bfd_link_hash_undefweak
> +	      || !bfd_link_pie (info)))
>  	{
>  	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
>  	    return FALSE;

This doesn't look correct to me.  Sure, it fixes the particular
testcase, but why do you make non-pie non-undefweak symbols dynamic
here?  I believe that only undefweak symbols should be made dynamic
here, and in all the other places in allocate_dynrelocs.

x86_64 too, but fixing allocate_dynrelocs for x86_64 causes no-plt-1e
to fail due to lack of a relative GOT dynamic reloc on 
	cmpq	func@GOTPCREL(%rip), %rax
in no-plt-check1.S.  Seems like x86_64 would need something like your
change to elf32-i386.c to emit relative GOT dynamic relocs in
relocate_section.  (For powerpc I decided to only handle plt and copy
relocs in finish_dynamic_symbol.  GOT dynamic relocs are all handled
in relocate_section as otherwise there is a lot of duplication and IMO
less comprehensible code.)

> @@ -3818,6 +3826,7 @@ elf_i386_relocate_section (bfd *output_bfd,
>        bfd_vma st_size;
>        asection *resolved_plt;
>        bfd_boolean resolved_to_zero;
> +      bfd_boolean relative_reloc;
>  
>        r_type = ELF32_R_TYPE (rel->r_info);
>        if (r_type == R_386_GNU_VTINHERIT
> @@ -4268,6 +4277,7 @@ r_386_got32:
>  	  if (htab->elf.sgot == NULL)
>  	    abort ();
>  
> +	  relative_reloc = FALSE;
>  	  if (h != NULL)
>  	    {
>  	      bfd_boolean dyn;
> @@ -4301,6 +4311,17 @@ r_386_got32:
>  		      bfd_put_32 (output_bfd, relocation,
>  				  htab->elf.sgot->contents + off);
>  		      h->got.offset |= 1;
> +
> +		      if (h->dynindx == -1
> +			  && !h->forced_local
> +			  && h->root.type != bfd_link_hash_undefweak
> +			  && bfd_link_pie (info))
> +			{
> +			  /* PR ld/21402: If this symbol isn't dynamic
> +			     in PIE, generate R_386_RELATIVE here.  */
> +			  eh->no_finish_dynamic_symbol = 1;
> +			  relative_reloc = TRUE;
> +			}
>  		    }
>  		}
>  	      else

This also looks odd.  Surely you need a relative reloc when pic, not
pie?  It seems to me you need one any time you won't be calling
finish_dynamic_symbol (which is where x86 sets most dynamic GOT
relocs) and have a symbol that doesn't resolve to zero.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
  2017-04-24  4:10     ` Alan Modra
@ 2017-04-24 20:51       ` H.J. Lu
  2017-04-24 21:43       ` H.J. Lu
  1 sibling, 0 replies; 6+ messages in thread
From: H.J. Lu @ 2017-04-24 20:51 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 3390 bytes --]

On Sun, Apr 23, 2017 at 9:09 PM, Alan Modra <amodra@gmail.com> wrote:
> On Fri, Apr 21, 2017 at 11:37:34AM -0700, H.J. Lu wrote:
>> @@ -2863,10 +2867,14 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
>>        int tls_type = elf_i386_hash_entry(h)->tls_type;
>>
>>        /* Make sure this symbol is output as a dynamic symbol.
>> -      Undefined weak syms won't yet be marked as dynamic.  */
>> +      Undefined weak syms won't yet be marked as dynamic.
>> +      PR ld/21402: If this symbol isn't undefined weak symbol,
>> +      don't make it dynamic in PIE.   */
>>        if (h->dynindx == -1
>>         && !h->forced_local
>> -       && !resolved_to_zero)
>> +       && !resolved_to_zero
>> +       && (h->root.type == bfd_link_hash_undefweak
>> +           || !bfd_link_pie (info)))
>>       {
>>         if (! bfd_elf_link_record_dynamic_symbol (info, h))
>>           return FALSE;
>
> This doesn't look correct to me.  Sure, it fixes the particular
> testcase, but why do you make non-pie non-undefweak symbols dynamic
> here?  I believe that only undefweak symbols should be made dynamic
> here, and in all the other places in allocate_dynrelocs.
>
> x86_64 too, but fixing allocate_dynrelocs for x86_64 causes no-plt-1e
> to fail due to lack of a relative GOT dynamic reloc on
>         cmpq    func@GOTPCREL(%rip), %rax
> in no-plt-check1.S.  Seems like x86_64 would need something like your
> change to elf32-i386.c to emit relative GOT dynamic relocs in
> relocate_section.  (For powerpc I decided to only handle plt and copy
> relocs in finish_dynamic_symbol.  GOT dynamic relocs are all handled
> in relocate_section as otherwise there is a lot of duplication and IMO
> less comprehensible code.)
>
>> @@ -3818,6 +3826,7 @@ elf_i386_relocate_section (bfd *output_bfd,
>>        bfd_vma st_size;
>>        asection *resolved_plt;
>>        bfd_boolean resolved_to_zero;
>> +      bfd_boolean relative_reloc;
>>
>>        r_type = ELF32_R_TYPE (rel->r_info);
>>        if (r_type == R_386_GNU_VTINHERIT
>> @@ -4268,6 +4277,7 @@ r_386_got32:
>>         if (htab->elf.sgot == NULL)
>>           abort ();
>>
>> +       relative_reloc = FALSE;
>>         if (h != NULL)
>>           {
>>             bfd_boolean dyn;
>> @@ -4301,6 +4311,17 @@ r_386_got32:
>>                     bfd_put_32 (output_bfd, relocation,
>>                                 htab->elf.sgot->contents + off);
>>                     h->got.offset |= 1;
>> +
>> +                   if (h->dynindx == -1
>> +                       && !h->forced_local
>> +                       && h->root.type != bfd_link_hash_undefweak
>> +                       && bfd_link_pie (info))
>> +                     {
>> +                       /* PR ld/21402: If this symbol isn't dynamic
>> +                          in PIE, generate R_386_RELATIVE here.  */
>> +                       eh->no_finish_dynamic_symbol = 1;
>> +                       relative_reloc = TRUE;
>> +                     }
>>                   }
>>               }
>>             else
>
> This also looks odd.  Surely you need a relative reloc when pic, not
> pie?  It seems to me you need one any time you won't be calling
> finish_dynamic_symbol (which is where x86 sets most dynamic GOT
> relocs) and have a symbol that doesn't resolve to zero.
>

I checked in this patch for i386.  I will look into x86-64.

-- 
H.J.

[-- Attachment #2: 0001-i386-Force-symbol-dynamic-if-it-isn-t-undefined-weak.patch --]
[-- Type: text/x-patch, Size: 3435 bytes --]

From 0dc9a308a1ec9675bf6b33bae9e8b1faa0bad9ed Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 24 Apr 2017 13:42:33 -0700
Subject: [PATCH] i386: Force symbol dynamic if it isn't undefined weak

Force symbol dynamic if it isn't undefined weak.  Generate R_386_RELATIVE
relocation for R_386_GOT32 relocation against non-dynamic symbol in PIC.

	PR ld/21402
	* elf32-i386.c (elf_i386_allocate_dynrelocs): If a symbol isn't
	undefined weak symbol, don't make it dynamic.
	(elf_i386_relocate_section): If a symbol isn't dynamic in PIC,
	set no_finish_dynamic_symbol and generate R_386_RELATIVE
	relocation for R_386_GOT32.
---
 bfd/ChangeLog    |  9 +++++++++
 bfd/elf32-i386.c | 17 ++++++++---------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 6b969d6..54e2f0d 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,5 +1,14 @@
 2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
 
+	PR ld/21402
+	* elf32-i386.c (elf_i386_allocate_dynrelocs): If a symbol isn't
+	undefined weak symbol, don't make it dynamic.
+	(elf_i386_relocate_section): If a symbol isn't dynamic in PIC,
+	set no_finish_dynamic_symbol and generate R_386_RELATIVE
+	relocation for R_386_GOT32.
+
+2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
+
 	PR ld/21425
 	* elf32-i386.c (ELF_MAXPAGESIZE): Set to 0x1000 for VxWorks.
 
diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 83f3b11..9a568ce 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -2748,7 +2748,8 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
 	  && !h->forced_local
-	  && !resolved_to_zero)
+	  && !resolved_to_zero
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
@@ -2867,14 +2868,11 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       int tls_type = elf_i386_hash_entry(h)->tls_type;
 
       /* Make sure this symbol is output as a dynamic symbol.
-	 Undefined weak syms won't yet be marked as dynamic.
-	 PR ld/21402: If this symbol isn't undefined weak symbol,
-	 don't make it dynamic in PIE.   */
+	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
 	  && !h->forced_local
 	  && !resolved_to_zero
-	  && (h->root.type == bfd_link_hash_undefweak
-	      || !bfd_link_pie (info)))
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
@@ -3035,7 +3033,8 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	     Undefined weak syms won't yet be marked as dynamic.  */
 	  if (h->dynindx == -1
 	      && !h->forced_local
-	      && !resolved_to_zero)
+	      && !resolved_to_zero
+	      && h->root.type == bfd_link_hash_undefweak)
 	    {
 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
 		return FALSE;
@@ -4315,10 +4314,10 @@ r_386_got32:
 		      if (h->dynindx == -1
 			  && !h->forced_local
 			  && h->root.type != bfd_link_hash_undefweak
-			  && bfd_link_pie (info))
+			  && bfd_link_pic (info))
 			{
 			  /* PR ld/21402: If this symbol isn't dynamic
-			     in PIE, generate R_386_RELATIVE here.  */
+			     in PIC, generate R_386_RELATIVE here.  */
 			  eh->no_finish_dynamic_symbol = 1;
 			  relative_reloc = TRUE;
 			}
-- 
2.9.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386
  2017-04-24  4:10     ` Alan Modra
  2017-04-24 20:51       ` H.J. Lu
@ 2017-04-24 21:43       ` H.J. Lu
  1 sibling, 0 replies; 6+ messages in thread
From: H.J. Lu @ 2017-04-24 21:43 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 3424 bytes --]

On Sun, Apr 23, 2017 at 9:09 PM, Alan Modra <amodra@gmail.com> wrote:
> On Fri, Apr 21, 2017 at 11:37:34AM -0700, H.J. Lu wrote:
>> @@ -2863,10 +2867,14 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
>>        int tls_type = elf_i386_hash_entry(h)->tls_type;
>>
>>        /* Make sure this symbol is output as a dynamic symbol.
>> -      Undefined weak syms won't yet be marked as dynamic.  */
>> +      Undefined weak syms won't yet be marked as dynamic.
>> +      PR ld/21402: If this symbol isn't undefined weak symbol,
>> +      don't make it dynamic in PIE.   */
>>        if (h->dynindx == -1
>>         && !h->forced_local
>> -       && !resolved_to_zero)
>> +       && !resolved_to_zero
>> +       && (h->root.type == bfd_link_hash_undefweak
>> +           || !bfd_link_pie (info)))
>>       {
>>         if (! bfd_elf_link_record_dynamic_symbol (info, h))
>>           return FALSE;
>
> This doesn't look correct to me.  Sure, it fixes the particular
> testcase, but why do you make non-pie non-undefweak symbols dynamic
> here?  I believe that only undefweak symbols should be made dynamic
> here, and in all the other places in allocate_dynrelocs.
>
> x86_64 too, but fixing allocate_dynrelocs for x86_64 causes no-plt-1e
> to fail due to lack of a relative GOT dynamic reloc on
>         cmpq    func@GOTPCREL(%rip), %rax
> in no-plt-check1.S.  Seems like x86_64 would need something like your
> change to elf32-i386.c to emit relative GOT dynamic relocs in
> relocate_section.  (For powerpc I decided to only handle plt and copy
> relocs in finish_dynamic_symbol.  GOT dynamic relocs are all handled
> in relocate_section as otherwise there is a lot of duplication and IMO
> less comprehensible code.)

This sounds a goo idea.  I will keep it in mind.

>> @@ -3818,6 +3826,7 @@ elf_i386_relocate_section (bfd *output_bfd,
>>        bfd_vma st_size;
>>        asection *resolved_plt;
>>        bfd_boolean resolved_to_zero;
>> +      bfd_boolean relative_reloc;
>>
>>        r_type = ELF32_R_TYPE (rel->r_info);
>>        if (r_type == R_386_GNU_VTINHERIT
>> @@ -4268,6 +4277,7 @@ r_386_got32:
>>         if (htab->elf.sgot == NULL)
>>           abort ();
>>
>> +       relative_reloc = FALSE;
>>         if (h != NULL)
>>           {
>>             bfd_boolean dyn;
>> @@ -4301,6 +4311,17 @@ r_386_got32:
>>                     bfd_put_32 (output_bfd, relocation,
>>                                 htab->elf.sgot->contents + off);
>>                     h->got.offset |= 1;
>> +
>> +                   if (h->dynindx == -1
>> +                       && !h->forced_local
>> +                       && h->root.type != bfd_link_hash_undefweak
>> +                       && bfd_link_pie (info))
>> +                     {
>> +                       /* PR ld/21402: If this symbol isn't dynamic
>> +                          in PIE, generate R_386_RELATIVE here.  */
>> +                       eh->no_finish_dynamic_symbol = 1;
>> +                       relative_reloc = TRUE;
>> +                     }
>>                   }
>>               }
>>             else
>
> This also looks odd.  Surely you need a relative reloc when pic, not
> pie?  It seems to me you need one any time you won't be calling
> finish_dynamic_symbol (which is where x86 sets most dynamic GOT
> relocs) and have a symbol that doesn't resolve to zero.
>

I checked in this patch for x86-64.

Thanks.

-- 
H.J.

[-- Attachment #2: 0001-x86-64-Force-symbol-dynamic-if-it-isn-t-undefined-we.patch --]
[-- Type: text/x-patch, Size: 8053 bytes --]

From 9e9821ddd80c0d0b3dda54d34cc8867f256d4583 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 24 Apr 2017 14:37:12 -0700
Subject: [PATCH] x86-64: Force symbol dynamic if it isn't undefined weak

Force symbol dynamic if it isn't undefined weak.  Generate relative
relocation for GOT reference against non-dynamic symbol in PIC to
avoid unnecessary dynamic symbols.

bfd/

	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add
	no_finish_dynamic_symbol.
	(elf_x86_64_link_hash_newfunc): Set no_finish_dynamic_symbol to
	0.
	(elf_x86_64_allocate_dynrelocs): If a symbol isn't undefined
	weak symbol, don't make it dynamic.
	(elf_x86_64_relocate_section): If a symbol isn't dynamic in PIC,
	set no_finish_dynamic_symbol and generate R_X86_64_RELATIVE
	relocation for GOT reference.
	(elf_x86_64_finish_dynamic_symbol): Abort if
	no_finish_dynamic_symbol isn't 0.

ld/

	* testsuite/ld-x86-64/no-plt.exp: Also check no-plt-1e.nd.
	* testsuite/ld-x86-64/no-plt-1e.nd: New file.
---
 bfd/ChangeLog                       | 14 ++++++++
 bfd/elf64-x86-64.c                  | 66 +++++++++++++++++++++++++------------
 ld/ChangeLog                        |  5 +++
 ld/testsuite/ld-x86-64/no-plt-1e.nd |  7 ++++
 ld/testsuite/ld-x86-64/no-plt.exp   |  2 +-
 5 files changed, 72 insertions(+), 22 deletions(-)
 create mode 100644 ld/testsuite/ld-x86-64/no-plt-1e.nd

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 54e2f0d..b902a76 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,5 +1,19 @@
 2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
 
+	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add
+	no_finish_dynamic_symbol.
+	(elf_x86_64_link_hash_newfunc): Set no_finish_dynamic_symbol to
+	0.
+	(elf_x86_64_allocate_dynrelocs): If a symbol isn't undefined
+	weak symbol, don't make it dynamic.
+	(elf_x86_64_relocate_section): If a symbol isn't dynamic in PIC,
+	set no_finish_dynamic_symbol and generate R_X86_64_RELATIVE
+	relocation for GOT reference.
+	(elf_x86_64_finish_dynamic_symbol): Abort if
+	no_finish_dynamic_symbol isn't 0.
+
+2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
+
 	PR ld/21402
 	* elf32-i386.c (elf_i386_allocate_dynrelocs): If a symbol isn't
 	undefined weak symbol, don't make it dynamic.
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 746b847..965ceb2 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -867,6 +867,9 @@ struct elf_x86_64_link_hash_entry
   /* TRUE if symbol has non-GOT/non-PLT relocations in text sections.  */
   unsigned int has_non_got_reloc : 1;
 
+  /* Don't call finish_dynamic_symbol on this symbol.  */
+  unsigned int no_finish_dynamic_symbol : 1;
+
   /* 0: symbol isn't __tls_get_addr.
      1: symbol is __tls_get_addr.
      2: symbol is unknown.  */
@@ -1022,6 +1025,7 @@ elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
       eh->has_bnd_reloc = 0;
       eh->has_got_reloc = 0;
       eh->has_non_got_reloc = 0;
+      eh->no_finish_dynamic_symbol = 0;
       eh->tls_get_addr = 2;
       eh->func_pointer_refcount = 0;
       eh->plt_bnd.offset = (bfd_vma) -1;
@@ -3218,7 +3222,8 @@ elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
 	  && !h->forced_local
-	  && !resolved_to_zero)
+	  && !resolved_to_zero
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
@@ -3338,7 +3343,8 @@ elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
 	  && !h->forced_local
-	  && !resolved_to_zero)
+	  && !resolved_to_zero
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
@@ -3475,6 +3481,7 @@ elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	  if (h->dynindx == -1
 	      && ! h->forced_local
 	      && ! resolved_to_zero
+	      && h->root.type == bfd_link_hash_undefweak
 	      && ! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return FALSE;
 
@@ -4270,6 +4277,7 @@ elf_x86_64_relocate_section (bfd *output_bfd,
       asection *base_got, *resolved_plt;
       bfd_vma st_size;
       bfd_boolean resolved_to_zero;
+      bfd_boolean relative_reloc;
 
       r_type = ELF32_R_TYPE (rel->r_info);
       if (r_type == (int) R_X86_64_GNU_VTINHERIT
@@ -4637,6 +4645,7 @@ do_ifunc_pointer:
 	  if (htab->elf.sgot == NULL)
 	    abort ();
 
+	  relative_reloc = FALSE;
 	  if (h != NULL)
 	    {
 	      bfd_boolean dyn;
@@ -4683,6 +4692,17 @@ do_ifunc_pointer:
 		      /* Note that this is harmless for the GOTPLT64 case,
 			 as -1 | 1 still is -1.  */
 		      h->got.offset |= 1;
+
+		      if (h->dynindx == -1
+			  && !h->forced_local
+			  && h->root.type != bfd_link_hash_undefweak
+			  && bfd_link_pic (info))
+			{
+			  /* If this symbol isn't dynamic in PIC,
+			     generate R_X86_64_RELATIVE here.  */
+			  eh->no_finish_dynamic_symbol = 1;
+			  relative_reloc = TRUE;
+			}
 		    }
 		}
 	      else
@@ -4704,30 +4724,32 @@ do_ifunc_pointer:
 		{
 		  bfd_put_64 (output_bfd, relocation,
 			      base_got->contents + off);
+		  local_got_offsets[r_symndx] |= 1;
 
 		  if (bfd_link_pic (info))
-		    {
-		      asection *s;
-		      Elf_Internal_Rela outrel;
-
-		      /* We need to generate a R_X86_64_RELATIVE reloc
-			 for the dynamic linker.  */
-		      s = htab->elf.srelgot;
-		      if (s == NULL)
-			abort ();
-
-		      outrel.r_offset = (base_got->output_section->vma
-					 + base_got->output_offset
-					 + off);
-		      outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
-		      outrel.r_addend = relocation;
-		      elf_append_rela (output_bfd, s, &outrel);
-		    }
-
-		  local_got_offsets[r_symndx] |= 1;
+		    relative_reloc = TRUE;
 		}
 	    }
 
+	  if (relative_reloc)
+	    {
+	      asection *s;
+	      Elf_Internal_Rela outrel;
+
+	      /* We need to generate a R_X86_64_RELATIVE reloc
+		 for the dynamic linker.  */
+	      s = htab->elf.srelgot;
+	      if (s == NULL)
+		abort ();
+
+	      outrel.r_offset = (base_got->output_section->vma
+				 + base_got->output_offset
+				 + off);
+	      outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
+	      outrel.r_addend = relocation;
+	      elf_append_rela (output_bfd, s, &outrel);
+	    }
+
 	  if (off >= (bfd_vma) -2)
 	    abort ();
 
@@ -5797,6 +5819,8 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
 	  : get_elf_x86_64_backend_data (output_bfd));
 
   eh = (struct elf_x86_64_link_hash_entry *) h;
+  if (eh->no_finish_dynamic_symbol)
+    abort ();
 
   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
      resolved undefined weak symbols in executable so that their
diff --git a/ld/ChangeLog b/ld/ChangeLog
index d40086e..ed3797e 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,5 +1,10 @@
 2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
 
+	* testsuite/ld-x86-64/no-plt.exp: Also check no-plt-1e.nd.
+	* testsuite/ld-x86-64/no-plt-1e.nd: New file.
+
+2017-04-24  H.J. Lu  <hongjiu.lu@intel.com>
+
 	* testsuite/ld-i386/pr12570a.d: Skip for nacl targets.
 	* testsuite/ld-i386/pr12570b.d: Likewise.
 
diff --git a/ld/testsuite/ld-x86-64/no-plt-1e.nd b/ld/testsuite/ld-x86-64/no-plt-1e.nd
new file mode 100644
index 0000000..c96760c
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/no-plt-1e.nd
@@ -0,0 +1,7 @@
+#nm: -g -D
+#target: x86_64-*-*
+
+#failif
+#...
+[0-9]+ +T +func
+#...
diff --git a/ld/testsuite/ld-x86-64/no-plt.exp b/ld/testsuite/ld-x86-64/no-plt.exp
index 5b5c817..9cecd03 100644
--- a/ld/testsuite/ld-x86-64/no-plt.exp
+++ b/ld/testsuite/ld-x86-64/no-plt.exp
@@ -111,7 +111,7 @@ run_cc_link_tests [list \
 	 tmpdir/no-plt-func1.o tmpdir/no-plt-extern1.o" \
 	"" \
 	{dummy.s} \
-	{{readelf -Wr no-plt-1e.rd} {objdump -dwrj.text no-plt-1e.dd}} \
+	{{readelf -Wr no-plt-1e.rd} {nm -gD no-plt-1e.nd} {objdump -dwrj.text no-plt-1e.dd}} \
 	"no-plt-1e" \
     ] \
     [list \
-- 
2.9.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-04-24 21:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-20 19:41 [PATCH] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386 H.J. Lu
2017-04-21  1:09 ` Alan Modra
2017-04-21 18:37   ` H.J. Lu
2017-04-24  4:10     ` Alan Modra
2017-04-24 20:51       ` H.J. Lu
2017-04-24 21:43       ` H.J. Lu

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