]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
RISC-V: Fix local GOT and reloc size calculation for TLS.
authorTatsuyuki Ishi <ishitatsuyuki@gmail.com>
Tue, 20 Feb 2024 17:55:48 +0000 (02:55 +0900)
committerNelson Chu <nelson@rivosinc.com>
Wed, 21 Feb 2024 06:58:43 +0000 (14:58 +0800)
The previous code did not account correctly for two cases:
* A TLS symbol can be referenced with multiple TLS types (although rare),
  in which case it only allocated the maximum slot size among the types,
  instead of the sum.
* TLS relocations are only needed for DLLs, unlike normal symbols which
  requires relocations for all PIE code.

Modify the logic to account for the two cases, so this fixes the redundant
dynamic R_RISCV_NONE in .rela.dyn when using --no-pie for TLS GD and IE.

Passed the gcc/binutils regressions of riscv-gnu-toolchain.

bfd/
    * elfnn-riscv.c (riscv_elf_size_dynamic_sections): Handle relocation
    sizing for TLS and non-TLS symbols differently, with the former
    requiring relocs on DLL while the latter requiring on PIE.
    Allocate GOT slots and relocation slots for each TLS type separately,
    accounting for the possibility of a TLS variable getting referenced by
    multiple symbols.
ld/
    * testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
    * testsuite/ld-riscv-elf/tls*: New testcase for TLS GD and IE, with
    symbols referred by both types and global and local symbols.

bfd/elfnn-riscv.c
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
ld/testsuite/ld-riscv-elf/tls.d [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/tls.s [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/tlsbin.d [new file with mode: 0644]
ld/testsuite/ld-riscv-elf/tlslib.s [new file with mode: 0644]

index 951b19a911df36b92625c41488cb4967ef998666..e5cecd79cf3d1766ab89f4c80005c9be7203509f 100644 (file)
@@ -1559,12 +1559,27 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
          if (*local_got > 0)
            {
              *local_got = s->size;
-             s->size += RISCV_ELF_WORD_BYTES;
-             if (*local_tls_type & GOT_TLS_GD)
-               s->size += RISCV_ELF_WORD_BYTES;
-             if (bfd_link_pic (info)
-                 || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
-               srel->size += sizeof (ElfNN_External_Rela);
+             if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))
+               {
+                 if (*local_tls_type & GOT_TLS_GD)
+                   {
+                     s->size += 2 * RISCV_ELF_WORD_BYTES;
+                     if (bfd_link_dll (info))
+                       srel->size += sizeof (ElfNN_External_Rela);
+                   }
+                 if (*local_tls_type & GOT_TLS_IE)
+                   {
+                     s->size += RISCV_ELF_WORD_BYTES;
+                     if (bfd_link_dll (info))
+                       srel->size += sizeof (ElfNN_External_Rela);
+                   }
+               }
+             else
+               {
+                 s->size += RISCV_ELF_WORD_BYTES;
+                 if (bfd_link_pic (info))
+                   srel->size += sizeof (ElfNN_External_Rela);
+               }
            }
          else
            *local_got = (bfd_vma) -1;
index 7e1281d826bbd2c59db9b49b50da974358d2148a..a1dd0e5e37ee417c6717fdf56f157ed172c5702a 100644 (file)
@@ -319,4 +319,12 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "pcrel-reloc-rel-pie"
     run_dump_test "pcrel-reloc-abs-nopie"
     run_dump_test "pcrel-reloc-abs-pie"
+
+    run_ld_link_tests {
+       { "Build shared library for TLS runtime"
+        "-shared" "" "" {tlslib.s}
+        {} "tlslib.so" }
+    }
+    run_dump_test "tls"
+    run_dump_test "tlsbin"
 }
diff --git a/ld/testsuite/ld-riscv-elf/tls.d b/ld/testsuite/ld-riscv-elf/tls.d
new file mode 100644 (file)
index 0000000..e7f2030
--- /dev/null
@@ -0,0 +1,15 @@
+#source: tls.s
+#ld: --shared tmpdir/tlslib.so
+#readelf: -Wr
+
+Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 5 entries:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +4
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0+ sg1 \+ 0
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPREL64 +0+ sg1 \+ 0
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +0+ sg1 \+ 0
+
+Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_JUMP_SLOT +0+ __tls_get_addr \+ 0
diff --git a/ld/testsuite/ld-riscv-elf/tls.s b/ld/testsuite/ld-riscv-elf/tls.s
new file mode 100644 (file)
index 0000000..79e9bc2
--- /dev/null
@@ -0,0 +1,28 @@
+       .section        .tbss,"awT",@nobits
+       .global sg1
+sg1:
+       .zero   4
+sl1:
+       .zero   4
+
+       .text
+       .globl  _start
+       .type   _start,@function
+_start:
+       /* GD, global var */
+       la.tls.gd       a0,sg1
+       call  __tls_get_addr
+
+       /* IE, global var */
+       la.tls.ie       a0,sg1
+       add     a0,a0,tp
+
+       /* GD, local var */
+       la.tls.gd       a0,sl1
+       call  __tls_get_addr
+
+       /* IE, local var */
+       la.tls.ie       a0,sl1
+       add     a0,a0,tp
+
+       ret
diff --git a/ld/testsuite/ld-riscv-elf/tlsbin.d b/ld/testsuite/ld-riscv-elf/tlsbin.d
new file mode 100644 (file)
index 0000000..cdcd51a
--- /dev/null
@@ -0,0 +1,7 @@
+#source: tls.s
+#ld: -no-pie tmpdir/tlslib.so
+#readelf: -Wr
+
+Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ R_RISCV_JUMP_SLOT +[0-9a-f]+ __tls_get_addr \+ 0
diff --git a/ld/testsuite/ld-riscv-elf/tlslib.s b/ld/testsuite/ld-riscv-elf/tlslib.s
new file mode 100644 (file)
index 0000000..17c7707
--- /dev/null
@@ -0,0 +1,6 @@
+       .text
+       /* Dummy.  */
+       .globl  __tls_get_addr
+       .type   __tls_get_addr,@function
+__tls_get_addr:
+       ret