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][Binutils][AArch64] Fix LD crash on weak and undefined TLS symbols. (PR/24602).


Hi All,

This patch fixes a few linker crashes due to TLS code reaching an assert when it
shouldn't.

The first scenario is with weak TLS symbols that remain weak during linking.  In
this case the mid-end would not have seen a TLS symbol and so wouldn't have
allocated the TLS section.  We currently assert here and the linker crashes with
a not very useful message.

This patch changes this to return the value 0 for the TLS symbol in question
emulating what lld and gold and other BFD targets do.  However because weak TLS
is implementation defined and we don't define any behavior for it I also emit a
warning to the user to inform them of such.

Secondly when a strong TLS reference is undefined. The linker crashes even after
it correctly reported that there is an undefined reference.  This changes it so
that it gracefully exits and reports a useful error.

build on native hardware and regtested on
  aarch64-none-elf, aarch64-none-elf (32 bit host),
  aarch64-none-linux-gnu, aarch64-none-linux-gnu (32 bit host)

Cross-compiled and regtested on
  aarch64-none-linux-gnu, aarch64_be-none-linux-gnu

and no issues.

Ok for master? and for backport to binutils-2.32 after some stew?

Thanks,
Tamar

bfd/ChangeLog:

2019-08-19  Tamar Christina  <tamar.christina@arm.com>

	PR ld/24601
	* elfnn-aarch64.c (aarch64_relocate): Handle weak TLS and undefined TLS.
	Also Pass input_bfd to _bfd_aarch64_elf_resolve_relocation.
	* elfxx-aarch64.c (_bfd_aarch64_elf_resolve_relocation): Use it.
	* elfxx-aarch64.h (_bfd_aarch64_elf_resolve_relocation): Emit warning
	for weak TLS.

ld/ChangeLog:

2019-08-19  Tamar Christina  <tamar.christina@arm.com>

	PR ld/24601
	* testsuite/ld-aarch64/aarch64-elf.exp (undef-tls, weak-tls): New.
	* testsuite/ld-aarch64/undef-tls.d: New test.
	* testsuite/ld-aarch64/undef-tls.s: New test.
	* testsuite/ld-aarch64/weak-tls.d: New test.
	* testsuite/ld-aarch64/weak-tls.s: New test.

-- 
diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index ba9e97c1e9be74b7911da5af4620347a9f9cc1fc..72416a7f83b97c6b78f80daf77e8310e7fbf9c8e 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -3004,7 +3004,8 @@ aarch64_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
 	   + offset);
 
   r_type = elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type);
-  value = _bfd_aarch64_elf_resolve_relocation (r_type, place, value, 0, FALSE);
+  value = _bfd_aarch64_elf_resolve_relocation (input_bfd, r_type, place,
+					       value, 0, FALSE);
   return _bfd_aarch64_elf_put_addend (input_bfd,
 				      input_section->contents + offset, r_type,
 				      howto, value) == bfd_reloc_ok;
@@ -5585,7 +5586,8 @@ bad_ifunc_reloc:
 	  /* FALLTHROUGH */
 	case BFD_RELOC_AARCH64_CALL26:
 	case BFD_RELOC_AARCH64_JUMP26:
-	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+	  value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						       place, value,
 						       signed_addend,
 						       weak_undef_p);
 	  return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type,
@@ -5662,7 +5664,8 @@ bad_ifunc_reloc:
 	    addend = (globals->root.sgot->output_section->vma
 		      + globals->root.sgot->output_offset);
 
-	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+	  value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						       place, value,
 						       addend, weak_undef_p);
 	  return _bfd_aarch64_elf_put_addend (input_bfd, hit_data, bfd_r_type, howto, value);
 	case BFD_RELOC_AARCH64_ADD_LO12:
@@ -5840,7 +5843,8 @@ bad_ifunc_reloc:
 	    signed_addend = 0;
 	  }
       }
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   signed_addend, weak_undef_p);
       *unresolved_reloc_p = FALSE;
       break;
@@ -5900,7 +5904,8 @@ bad_ifunc_reloc:
     case BFD_RELOC_AARCH64_MOVW_G2_S:
     case BFD_RELOC_AARCH64_MOVW_G3:
     case BFD_RELOC_AARCH64_TSTBR14:
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   signed_addend, weak_undef_p);
       break;
 
@@ -5945,7 +5950,8 @@ bad_ifunc_reloc:
 	  if (aarch64_relocation_aginst_gp_p (bfd_r_type))
 	    addend = (globals->root.sgot->output_section->vma
 		      + globals->root.sgot->output_offset);
-	  value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+	  value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						       place, value,
 						       addend, weak_undef_p);
 	}
       else
@@ -5992,7 +5998,8 @@ bad_ifunc_reloc:
 	if (aarch64_relocation_aginst_gp_p (bfd_r_type))
 	  addend = base_got->output_section->vma + base_got->output_offset;
 
-	value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+	value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						     place, value,
 						     addend, weak_undef_p);
       }
 
@@ -6029,7 +6036,8 @@ bad_ifunc_reloc:
 	       + globals->root.sgot->output_section->vma
 	       + globals->root.sgot->output_offset);
 
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   0, weak_undef_p);
       *unresolved_reloc_p = FALSE;
       break;
@@ -6042,7 +6050,8 @@ bad_ifunc_reloc:
 	return bfd_reloc_notsupported;
 
       value = symbol_got_offset (input_bfd, h, r_symndx);
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   0, weak_undef_p);
       *unresolved_reloc_p = FALSE;
       break;
@@ -6063,10 +6072,26 @@ bad_ifunc_reloc:
     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
     case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
-						   signed_addend - dtpoff_base (info),
-						   weak_undef_p);
-      break;
+      {
+	if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
+	  {
+	    int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
+	    _bfd_error_handler
+	      /* xgettext:c-format */
+	      (_("%pB: TLS relocation %s against undefined symbol `%s'"),
+		 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
+		 h->root.root.string);
+	    bfd_set_error (bfd_error_bad_value);
+	    return bfd_reloc_notsupported;
+	  }
+
+	bfd_vma def_value
+	  = weak_undef_p ? 0 : signed_addend - dtpoff_base (info);
+	value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						     place, value,
+						     def_value, weak_undef_p);
+	break;
+      }
 
     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
     case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
@@ -6084,11 +6109,27 @@ bad_ifunc_reloc:
     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
     case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
-						   signed_addend - tpoff_base (info),
-						   weak_undef_p);
-      *unresolved_reloc_p = FALSE;
-      break;
+      {
+	if (!(weak_undef_p || elf_hash_table (info)->tls_sec))
+	  {
+	    int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START;
+	    _bfd_error_handler
+	      /* xgettext:c-format */
+	      (_("%pB: TLS relocation %s against undefined symbol `%s'"),
+		 input_bfd, elfNN_aarch64_howto_table[howto_index].name,
+		 h->root.root.string);
+	    bfd_set_error (bfd_error_bad_value);
+	    return bfd_reloc_notsupported;
+	  }
+
+	bfd_vma def_value
+	  = weak_undef_p ? 0 : signed_addend - tpoff_base (info);
+	value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						     place, value,
+						     def_value, weak_undef_p);
+        *unresolved_reloc_p = FALSE;
+	break;
+      }
 
     case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
@@ -6103,7 +6144,8 @@ bad_ifunc_reloc:
 	       + globals->root.sgotplt->output_offset
 	       + globals->sgotplt_jump_table_size);
 
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   0, weak_undef_p);
       *unresolved_reloc_p = FALSE;
       break;
@@ -6121,7 +6163,8 @@ bad_ifunc_reloc:
       value -= (globals->root.sgot->output_section->vma
 		+ globals->root.sgot->output_offset);
 
-      value = _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, value,
+      value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+						   place, value,
 						   0, weak_undef_p);
       *unresolved_reloc_p = FALSE;
       break;
diff --git a/bfd/elfxx-aarch64.h b/bfd/elfxx-aarch64.h
index 9ab13bb865bc3e31c35f6d00c83e074a72f0ff55..f5988529d7aaaa886f89ba3a61e632e1216bd75d 100644
--- a/bfd/elfxx-aarch64.h
+++ b/bfd/elfxx-aarch64.h
@@ -40,8 +40,8 @@ _bfd_aarch64_elf_put_addend (bfd *, bfd_byte *, bfd_reloc_code_real_type,
 			     reloc_howto_type *, bfd_signed_vma);
 
 extern bfd_vma
-_bfd_aarch64_elf_resolve_relocation (bfd_reloc_code_real_type, bfd_vma, bfd_vma,
-				     bfd_vma, bfd_boolean);
+_bfd_aarch64_elf_resolve_relocation (bfd *, bfd_reloc_code_real_type, bfd_vma,
+				     bfd_vma, bfd_vma, bfd_boolean);
 
 extern bfd_boolean
 _bfd_aarch64_elf_grok_prstatus (bfd *, Elf_Internal_Note *);
diff --git a/bfd/elfxx-aarch64.c b/bfd/elfxx-aarch64.c
index 33af6c22e16b7534ed2fcc8e499fafd498d6835e..1f6928071313d91479055e45aac4199590404bba 100644
--- a/bfd/elfxx-aarch64.c
+++ b/bfd/elfxx-aarch64.c
@@ -395,10 +395,12 @@ _bfd_aarch64_elf_put_addend (bfd *abfd,
 }
 
 bfd_vma
-_bfd_aarch64_elf_resolve_relocation (bfd_reloc_code_real_type r_type,
+_bfd_aarch64_elf_resolve_relocation (bfd *input_bfd,
+				     bfd_reloc_code_real_type r_type,
 				     bfd_vma place, bfd_vma value,
 				     bfd_vma addend, bfd_boolean weak_undef_p)
 {
+  bfd_boolean tls_reloc = TRUE;
   switch (r_type)
     {
     case BFD_RELOC_AARCH64_NONE:
@@ -446,6 +448,8 @@ _bfd_aarch64_elf_resolve_relocation (bfd_reloc_code_real_type r_type,
     case BFD_RELOC_AARCH64_MOVW_G2_NC:
     case BFD_RELOC_AARCH64_MOVW_G2_S:
     case BFD_RELOC_AARCH64_MOVW_G3:
+      tls_reloc = FALSE;
+      /* fall-through.  */
     case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
     case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
     case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
@@ -466,6 +470,15 @@ _bfd_aarch64_elf_resolve_relocation (bfd_reloc_code_real_type r_type,
     case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
     case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
     case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
+      /* Weak Symbols and TLS relocations are implementation defined.  For this
+	 case we choose to emit 0.  */
+      if (weak_undef_p && tls_reloc)
+	{
+	  _bfd_error_handler (_("%pB: warning: Weak TLS is implementation "
+				"defined and may not work as expected"),
+				input_bfd);
+	  value = place;
+	}
       value = value + addend;
       break;
 
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index ab8d15271c9a767782ec0145c1a036399ea791bf..631fde0f31079cad8d7258fd39005bc881954280 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -424,3 +424,5 @@ run_dump_test "bti-pac-plt-1"
 run_dump_test "bti-pac-plt-2"
 
 run_dump_test "bti-warn"
+run_dump_test "weak-tls"
+run_dump_test "undef-tls"
diff --git a/ld/testsuite/ld-aarch64/undef-tls.d b/ld/testsuite/ld-aarch64/undef-tls.d
new file mode 100644
index 0000000000000000000000000000000000000000..8b43ca4c1de6858faa40f0d6530b448403c1fafa
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/undef-tls.d
@@ -0,0 +1,17 @@
+#source: undef-tls.s
+#ld: -e0 --emit-relocs
+#objdump: -dr
+#...
+#error:.*: in function `get':.*
+#error:.*: undefined reference to `tls'.*
+#error:.*: TLS relocation R_AARCH64_TLSLE_ADD_TPREL_HI12 against undefined symbol `tls'.*
+#error:.*: dangerous relocation: unsupported relocation.*
+#error:.*: undefined reference to `tls'.*
+#error:.*: TLS relocation R_AARCH64_TLSLE_ADD_TPREL_LO12_NC against undefined symbol `tls'.*
+#error:.*: dangerous relocation: unsupported relocation.*
+#error:.*: undefined reference to `dtl'.*
+#error:.*: TLS relocation R_AARCH64_TLSLD_ADD_DTPREL_HI12 against undefined symbol `dtl'.*
+#error:.*: dangerous relocation: unsupported relocation.*
+#error:.*: undefined reference to `dtl'.*
+#error:.*: TLS relocation R_AARCH64_TLSLD_ADD_DTPREL_LO12 against undefined symbol `dtl'.*
+#error:.*: dangerous relocation: unsupported relocation.*
diff --git a/ld/testsuite/ld-aarch64/undef-tls.s b/ld/testsuite/ld-aarch64/undef-tls.s
new file mode 100644
index 0000000000000000000000000000000000000000..6d57afbb540e4aa6ab0762a5949124fa55ecbd29
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/undef-tls.s
@@ -0,0 +1,13 @@
+        .global get
+        .type   get, %function
+get:
+.LFB0:
+        mrs     x0, tpidr_el0
+        add     x0, x0, #:tprel_hi12:tls, lsl #12
+        add     x0, x0, #:tprel_lo12_nc:tls
+	add	x0, x0, #:dtprel_hi12:dtl
+	add	x0, x0, #:dtprel_lo12:dtl
+        ret
+.LFE0:
+        .size   get, .-get
+
diff --git a/ld/testsuite/ld-aarch64/weak-tls.d b/ld/testsuite/ld-aarch64/weak-tls.d
new file mode 100644
index 0000000000000000000000000000000000000000..a8269406892fc7e9d94a528a799398171ff53a85
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/weak-tls.d
@@ -0,0 +1,8 @@
+#source: weak-tls.s
+#ld: -e0 --emit-relocs
+#objdump: -dr
+#...
+#error:.*: warning: Weak TLS is implementation defined and may not work as expected.*
+#error:.*: warning: Weak TLS is implementation defined and may not work as expected.*
+#error:.*: in function `get':.*
+#error:.*: relocation truncated to fit: R_AARCH64_TLSLD_ADD_DTPREL_LO12 against undefined symbol `dtl'.*
diff --git a/ld/testsuite/ld-aarch64/weak-tls.s b/ld/testsuite/ld-aarch64/weak-tls.s
new file mode 100644
index 0000000000000000000000000000000000000000..3520a85589c786566c7da7d321f6077ac3d8f2c4
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/weak-tls.s
@@ -0,0 +1,16 @@
+        .global get
+        .type   get, %function
+	.global dtl
+	.weak dtl
+get:
+.LFB0:
+        mrs     x0, tpidr_el0
+        add     x0, x0, #:tprel_hi12:tls, lsl #12
+        add     x0, x0, #:tprel_lo12_nc:tls
+	add	x0, x0, #:dtprel_hi12:dtl
+	add	x0, x0, #:dtprel_lo12:dtl
+        ret
+.LFE0:
+        .size   get, .-get
+        .weak   tls
+


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