[PATCH v3] aarch64: Emit jump slot for conditional branch to undefined symbols
authorSiddhesh Poyarekar <siddesh.poyarekar@arm.com>
Tue, 19 May 2020 10:07:52 +0000 (11:07 +0100)
committerNick Clifton <nickc@redhat.com>
Tue, 19 May 2020 10:07:52 +0000 (11:07 +0100)
The linker silently writes out a conditional branch to 0 if the
target symbol in R_AARCH64_CONDBR19 or R_AARCH64_TSTBR14 relocations is
undefined.  Emit a PLT instead so that behaviour is the same for these
relocations as the llvm linker.

The special behaviour for undefined weak symbols, where conditional
branches to such symbols result in a branch unto themselves, has been
retained.  This is because the weak-undefined.s test explicitly checks
for that, leading me to conclude that it's expected behaviour.

bfd * elfnn-aarch64.c (elfNN_aarch64_final_link_relocate): Club
BFD_RELOC_AARCH64_BRANCH19 and BFD_RELOC_AARCH64_TSTBR14
cases with BFD_RELOC_AARCH64_JUMP26.
(elfNN_aarch64_check_relocs): Likewise.

ld * testsuite/ld-aarch64/aarch64-elf.exp: New test
emit-relocs-560.
* testsuite/ld-aarch64/emit-relocs-560.d: New file.
* testsuite/ld-aarch64/emit-relocs-560.s: New file.

bfd/ChangeLog
bfd/elfnn-aarch64.c
ld/ChangeLog
ld/testsuite/ld-aarch64/aarch64-elf.exp
ld/testsuite/ld-aarch64/emit-relocs-560.d [new file with mode: 0644]
ld/testsuite/ld-aarch64/emit-relocs-560.s [new file with mode: 0644]

index d9892235721b13537f30dcbeeb8ccb2785a56ee0..c10c1e93c9abb9c948c7ed21c1c2372d1a9033e9 100644 (file)
@@ -1,3 +1,10 @@
+2020-05-19  Siddhesh Poyarekar  <siddesh.poyarekar@arm.com>
+
+       * elfnn-aarch64.c (elfNN_aarch64_final_link_relocate): Club
+       BFD_RELOC_AARCH64_BRANCH19 and BFD_RELOC_AARCH64_TSTBR14
+       cases with BFD_RELOC_AARCH64_JUMP26.
+       (elfNN_aarch64_check_relocs): Likewise.
+
 2020-05-19  Alan Modra  <amodra@gmail.com>
 
        * aix5ppc-core.c (xcoff64_core_file_matches_executable_p): Use
index 4bb5707d2f453c4b0fc32eeba5d58f0e2ba30d75..02df893fcdd41d0ddaf31eb5b8c613615790da38 100644 (file)
@@ -5494,6 +5494,7 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
   bfd_vma orig_value = value;
   bfd_boolean resolved_to_zero;
   bfd_boolean abs_symbol_p;
+  bfd_boolean via_plt_p;
 
   globals = elf_aarch64_hash_table (info);
 
@@ -5515,6 +5516,8 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
                  : bfd_is_und_section (sym_sec));
   abs_symbol_p = h != NULL && bfd_is_abs_symbol (&h->root);
 
+  via_plt_p = (globals->root.splt != NULL && h != NULL
+              && h->plt.offset != (bfd_vma) - 1);
 
   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
      it here if it is defined in a non-shared object.  */
@@ -5850,12 +5853,23 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
        value += signed_addend;
       break;
 
+    case BFD_RELOC_AARCH64_BRANCH19:
+    case BFD_RELOC_AARCH64_TSTBR14:
+      /* A conditional branch to an undefined weak symbol is converted to a
+        branch to itself.  */
+      if (weak_undef_p && !via_plt_p)
+       {
+         value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
+                                                      place, value,
+                                                      signed_addend,
+                                                      weak_undef_p);
+         break;
+       }
+      /* Fall through.  */
     case BFD_RELOC_AARCH64_CALL26:
     case BFD_RELOC_AARCH64_JUMP26:
       {
        asection *splt = globals->root.splt;
-       bfd_boolean via_plt_p =
-         splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
 
        /* A call to an undefined weak symbol is converted to a jump to
           the next instruction unless a PLT entry will be created.
@@ -5943,7 +5957,6 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
     case BFD_RELOC_AARCH64_32:
 #endif
     case BFD_RELOC_AARCH64_ADD_LO12:
-    case BFD_RELOC_AARCH64_BRANCH19:
     case BFD_RELOC_AARCH64_LDST128_LO12:
     case BFD_RELOC_AARCH64_LDST16_LO12:
     case BFD_RELOC_AARCH64_LDST32_LO12:
@@ -5959,7 +5972,6 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto,
     case BFD_RELOC_AARCH64_MOVW_G2_NC:
     case BFD_RELOC_AARCH64_MOVW_G2_S:
     case BFD_RELOC_AARCH64_MOVW_G3:
-    case BFD_RELOC_AARCH64_TSTBR14:
       value = _bfd_aarch64_elf_resolve_relocation (input_bfd, bfd_r_type,
                                                   place, value,
                                                   signed_addend, weak_undef_p);
@@ -8022,6 +8034,8 @@ elfNN_aarch64_check_relocs (bfd *abfd, struct bfd_link_info *info,
            break;
          }
 
+       case BFD_RELOC_AARCH64_BRANCH19:
+       case BFD_RELOC_AARCH64_TSTBR14:
        case BFD_RELOC_AARCH64_CALL26:
        case BFD_RELOC_AARCH64_JUMP26:
          /* If this is a local symbol then we resolve it
index 730517124e919eab11f8fea7a3fdd3b23af0be94..cf566b3255911431281313d4706f5fd1dfee57d5 100644 (file)
@@ -1,3 +1,10 @@
+2020-05-19  Siddhesh Poyarekar  <siddesh.poyarekar@arm.com>
+
+       * testsuite/ld-aarch64/aarch64-elf.exp: New test
+       emit-relocs-560.
+       * testsuite/ld-aarch64/emit-relocs-560.d: New file.
+       * testsuite/ld-aarch64/emit-relocs-560.s: New file.
+
 2020-05-19  Alan Modra  <amodra@gmail.com>
 
        * emultempl/beos.em (sort_by_file_name): Use bfd_get_filename
index 297a3e96db95b70d92c7249ab52d3a136ef5e3f8..4c44fa1642b9fea081e78376a27e203f4c751af8 100644 (file)
@@ -236,6 +236,7 @@ run_dump_test_lp64 "emit-relocs-557"
 run_dump_test_lp64 "emit-relocs-558"
 run_dump_test_lp64 "emit-relocs-558-overflow"
 run_dump_test_lp64 "emit-relocs-559"
+run_dump_test_lp64 "emit-relocs-560"
 
 run_dump_test "reloc-overflow-bad"
 
diff --git a/ld/testsuite/ld-aarch64/emit-relocs-560.d b/ld/testsuite/ld-aarch64/emit-relocs-560.d
new file mode 100644 (file)
index 0000000..1535324
--- /dev/null
@@ -0,0 +1,8 @@
+#source: emit-relocs-560.s
+#ld: -shared
+#readelf: -r
+
+Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 2 entries:
+  Offset          Info           Type           Sym. Value    Sym. Name \+ Addend
+[0-9a-f]+  000100000402 R_AARCH64_JUMP_SL 0000000000000000 baz \+ 0
+[0-9a-f]+  000200000402 R_AARCH64_JUMP_SL 0000000000000000 bar \+ 0
diff --git a/ld/testsuite/ld-aarch64/emit-relocs-560.s b/ld/testsuite/ld-aarch64/emit-relocs-560.s
new file mode 100644 (file)
index 0000000..9529e8f
--- /dev/null
@@ -0,0 +1,3 @@
+foo:
+       tbz     x0, 1, bar
+       cbnz    x1, baz
This page took 0.031467 seconds and 4 git commands to generate.