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] PR ld/21402: Skip PIE indirect5 and indirect6 tests on i386


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


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