[PATCH] LoongArch: Fix GOT allocation for TLS PCREL20 relocations
zhaozhou
zhaozhou@loongson.cn
Thu Nov 5 11:31:49 GMT 2026
Record GOT references for R_LARCH_TLS_{LD,GD,DESC}_PCREL20_S2 in
loongarch_elf_check_relocs, otherwise unallocated GOT entries will cause
the link crash when reaching final relocation.
PR ld/34809
---
bfd/elfnn-loongarch.c | 3 +++
.../ld-loongarch-elf/ld-loongarch-elf.exp | 2 ++
ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.d | 17 +++++++++++++++++
.../ld-loongarch-elf/tls-pcrel20-got.ld | 8 ++++++++
ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.s | 16 ++++++++++++++++
.../ld-loongarch-elf/tls-pcrel20-got32.d | 15 +++++++++++++++
6 files changed, 61 insertions(+)
create mode 100644 ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.d
create mode 100644 ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.ld
create mode 100644 ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.s
create mode 100644 ld/testsuite/ld-loongarch-elf/tls-pcrel20-got32.d
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 1c94748360f..9fa0febd64f 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -1254,6 +1254,8 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
case R_LARCH_TLS_GD_PC_HI20:
case R_LARCH_TLS_GD_PCADD_HI20:
case R_LARCH_TLS_GD_HI20:
+ case R_LARCH_TLS_LD_PCREL20_S2:
+ case R_LARCH_TLS_GD_PCREL20_S2:
case R_LARCH_SOP_PUSH_TLS_GD:
if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
r_symndx,
@@ -1293,6 +1295,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
case R_LARCH_TLS_DESC_PC_HI20:
case R_LARCH_TLS_DESC_PCADD_HI20:
case R_LARCH_TLS_DESC_HI20:
+ case R_LARCH_TLS_DESC_PCREL20_S2:
if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
r_symndx,
GOT_TLS_GDESC,
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 8d43c0802c1..ce174f54c9e 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -55,6 +55,7 @@ if [istarget "loongarch64-*-*"] {
run_dump_test "relax-sym-size-2"
run_dump_test "check_got_relax"
run_dump_test "unresolved-ignore-all"
+ run_dump_test "tls-pcrel20-got"
# Tidy up, to not leave huge intermediate files.
remote_file host delete tmpdir/relax-medium-call.o
@@ -62,6 +63,7 @@ if [istarget "loongarch64-*-*"] {
}
if [istarget "loongarch32-*-*"] {
+ run_dump_test "tls-pcrel20-got32"
run_dump_test "macro_op_32"
run_dump_test "syscall"
run_dump_test "disas-jirl-32"
diff --git a/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.d b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.d
new file mode 100644
index 00000000000..7fe1798fdc2
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.d
@@ -0,0 +1,17 @@
+#as: -mno-relax
+#ld: --no-relax -T tls-pcrel20-got.ld
+#readelf: -Wr -x .got
+#skip: loongarch32-*-*
+
+Relocation section '.rela.got' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+0+20018 +0+e R_LARCH_TLS_DESC64 +0
+
+Hex dump of section '.got':
+ NOTE: This section has relocations against it, but these have NOT been applied to this dump.
+ 0x00020000 00000000 00000000 01000000 00000000 .*
+ 0x00020010 00000000 00000000 00000000 00000000 .*
+ 0x00020020 00000000 00000000 01000000 00000000 .*
+ 0x00020030 00000000 00000000 +.*
+
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.ld b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.ld
new file mode 100644
index 00000000000..b4b51229336
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.ld
@@ -0,0 +1,8 @@
+SECTIONS
+{
+ .text 0x10000 : { *(.text) }
+ .got 0x20000 : { *(.got) }
+ .got.plt : { *(.got.plt) }
+ .rela.got : { *(.rela.got) }
+ .tdata 0x30000 : { *(.tdata) }
+}
diff --git a/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.s b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.s
new file mode 100644
index 00000000000..3ce4febd6ef
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got.s
@@ -0,0 +1,16 @@
+ .section .tdata,"awT",@progbits
+ .globl ld_tls_var, gd_tls_var, desc_tls_var
+ .type ld_tls_var, @tls_object
+ .type gd_tls_var, @tls_object
+ .type desc_tls_var, @tls_object
+ld_tls_var:
+gd_tls_var:
+desc_tls_var:
+ .dword 0
+
+ .text
+ .globl _start
+_start:
+ pcaddi $a0, %ld_pcrel_20(ld_tls_var)
+ pcaddi $a0, %gd_pcrel_20(gd_tls_var)
+ pcaddi $a0, %desc_pcrel_20(desc_tls_var)
diff --git a/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got32.d b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got32.d
new file mode 100644
index 00000000000..06bcc4c0b0e
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/tls-pcrel20-got32.d
@@ -0,0 +1,15 @@
+#source: tls-pcrel20-got.s
+#as: -mabi=ilp32d -mno-relax
+#ld: -melf32loongarch -no-relax -T tls-pcrel20-got.ld
+#readelf: -Wr -x .got
+
+Relocation section '.rela.got' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Sym. Value +Symbol's Name \+ Addend
+0+2000c +0+d R_LARCH_TLS_DESC32 +0
+
+Hex dump of section '.got':
+ NOTE: This section has relocations against it, but these have NOT been applied to this dump.
+ 0x00020000 00000000 01000000 00000000 00000000 .*
+ 0x00020010 00000000 01000000 00000000 +.*
+
+#pass
--
2.20.1
More information about the Binutils
mailing list