AArch64: Fix LD crash on weak and undefined TLS symbols. (PR/24602).
authorTamar Christina <tamar.christina@arm.com>
Thu, 22 Aug 2019 10:35:35 +0000 (11:35 +0100)
committerTamar Christina <tamar.christina@arm.com>
Thu, 22 Aug 2019 10:43:15 +0000 (11:43 +0100)
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.

bfd/ChangeLog:

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:

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.

bfd/ChangeLog
bfd/elfnn-aarch64.c
bfd/elfxx-aarch64.c
bfd/elfxx-aarch64.h
ld/ChangeLog
ld/testsuite/ld-aarch64/aarch64-elf.exp
ld/testsuite/ld-aarch64/undef-tls.d [new file with mode: 0644]
ld/testsuite/ld-aarch64/undef-tls.s [new file with mode: 0644]
ld/testsuite/ld-aarch64/weak-tls.d [new file with mode: 0644]
ld/testsuite/ld-aarch64/weak-tls.s [new file with mode: 0644]

index a725504c025473d20278ff6e5979935e8a1b2bdc..ef9eee4716e3eb97089af47af379934016401175 100644 (file)
@@ -1,3 +1,12 @@
+2019-08-22  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.
+
 2019-08-22  Alan Modra  <amodra@gmail.com>
 
        * elf32-arm.c (cmse_scan): Don't use ARM_GET_SYM_CMSE_SPCL,
index 4e6cbc9a3ad63c8d4485c9e43cf83b6aedef60a9..fd44505da4a0ae43537a5bf2a5aaa4522f860687 100644 (file)
@@ -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;
index 33af6c22e16b7534ed2fcc8e499fafd498d6835e..1f6928071313d91479055e45aac4199590404bba 100644 (file)
@@ -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;
 
index 9ab13bb865bc3e31c35f6d00c83e074a72f0ff55..f5988529d7aaaa886f89ba3a61e632e1216bd75d 100644 (file)
@@ -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 *);
index 4f71ce81bd3985ded1883426604c8c39b02027b6..ed646acffaefb8aa8a48c733c320ddc2e4a7a698 100644 (file)
@@ -1,3 +1,12 @@
+2019-08-22  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.
+
 2019-08-20  Tamar Christina  <tamar.christina@arm.com>
 
        * testsuite/ld-arm/cortex-a8-fix-b-plt.d: Update Testcase.
index ab8d15271c9a767782ec0145c1a036399ea791bf..631fde0f31079cad8d7258fd39005bc881954280 100644 (file)
@@ -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 (file)
index 0000000..8b43ca4
--- /dev/null
@@ -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 (file)
index 0000000..6d57afb
--- /dev/null
@@ -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 (file)
index 0000000..a826940
--- /dev/null
@@ -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 (file)
index 0000000..3520a85
--- /dev/null
@@ -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
+