[PATCH] LoongArch: gas: Fix TLS LE relaxation with -mno-relax
zhaozhou
zhaozhou@loongson.cn
Fri Nov 13 01:43:22 GMT 2026
The TLS LE sequence is:
lu12i.w $a0, %le_hi20_r(sym)
add.d $a0, $a0, $tp, %le_add_r(sym)
addi.d $a0, $a0, %le_lo12_r(sym)
With -mno-relax, GAS still emits R_LARCH_RELAX for le_add_r, but not for
le_hi20_r or le_lo12_r. When linker relaxation is enabled, ld may delete
the add.d, but the addi.d src-reg not write to TP, produce a wrong TLS
address.
Fix TLS LE only emit the le_add_r RELAX marker when LARCH_opts.relax is
enabled.
---
gas/config/tc-loongarch.c | 11 ++++++----
.../gas/loongarch/tls-le-r-norelax.d | 22 +++++++++++++++++++
.../gas/loongarch/tls-le-r-norelax.s | 11 ++++++++++
3 files changed, 40 insertions(+), 4 deletions(-)
create mode 100644 gas/testsuite/gas/loongarch/tls-le-r-norelax.d
create mode 100644 gas/testsuite/gas/loongarch/tls-le-r-norelax.s
diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c
index 60caf4891c8..4cf49692566 100644
--- a/gas/config/tc-loongarch.c
+++ b/gas/config/tc-loongarch.c
@@ -817,10 +817,13 @@ loongarch_args_parser_can_match_arg_helper (char esc_ch1, char esc_ch2,
&& (ip->reloc_info[ip->reloc_num].type == tls_reloc_type))
{
ip->reloc_num += reloc_num;
- ip->reloc_info[ip->reloc_num].type = BFD_RELOC_LARCH_RELAX;
- ip->reloc_info[ip->reloc_num].value = const_0;
- ip->reloc_num++;
- ip->linker_relax = true;
+ if (LARCH_opts.relax)
+ {
+ ip->reloc_info[ip->reloc_num].type = BFD_RELOC_LARCH_RELAX;
+ ip->reloc_info[ip->reloc_num].value = const_0;
+ ip->reloc_num++;
+ ip->linker_relax = true;
+ }
}
else
ip->match_now = 0;
diff --git a/gas/testsuite/gas/loongarch/tls-le-r-norelax.d b/gas/testsuite/gas/loongarch/tls-le-r-norelax.d
new file mode 100644
index 00000000000..90624286adc
--- /dev/null
+++ b/gas/testsuite/gas/loongarch/tls-le-r-norelax.d
@@ -0,0 +1,22 @@
+#as: -mno-relax
+#objdump: -dr
+#skip: loongarch32-*-*
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+00000000.* <.text>:
+[ ]+0:[ ]+14000004[ ]+lu12i.w[ ]+\$a0,[ ]+0
+[ ]+0:[ ]+R_LARCH_TLS_LE_HI20_R[ ]+TLSL1
+[ ]+4:[ ]+001090a5[ ]+add.d[ ]+\$a1,[ ]+\$a1,[ ]+\$a0
+[ ]+4:[ ]+R_LARCH_TLS_LE_ADD_R[ ]+TLSL1
+[ ]+8:[ ]+29800085[ ]+st.w[ ]+\$a1,[ ]+\$a0,[ ]+0
+[ ]+8:[ ]+R_LARCH_TLS_LE_LO12_R[ ]+TLSL1
+[ ]+c:[ ]+14000004[ ]+lu12i.w[ ]+\$a0,[ ]+0
+[ ]+c:[ ]+R_LARCH_TLS_LE_HI20_R[ ]+TLSL1\+0x8
+[ ]+10:[ ]+001090a5[ ]+add.d[ ]+\$a1,[ ]+\$a1,[ ]+\$a0
+[ ]+10:[ ]+R_LARCH_TLS_LE_ADD_R[ ]+TLSL1\+0x8
+[ ]+14:[ ]+29800085[ ]+st.w[ ]+\$a1,[ ]+\$a0,[ ]+0
+[ ]+14:[ ]+R_LARCH_TLS_LE_LO12_R[ ]+TLSL1\+0x8
diff --git a/gas/testsuite/gas/loongarch/tls-le-r-norelax.s b/gas/testsuite/gas/loongarch/tls-le-r-norelax.s
new file mode 100644
index 00000000000..55a46f2d213
--- /dev/null
+++ b/gas/testsuite/gas/loongarch/tls-le-r-norelax.s
@@ -0,0 +1,11 @@
+/* Test TLS_LE_R relocs with norelax. */
+.text
+/* New TLS Insn. */
+lu12i.w $r4,%le_hi20_r(TLSL1)
+add.d $r5,$r5,$r4,%le_add_r(TLSL1)
+st.w $r5,$r4,%le_lo12_r(TLSL1)
+
+/* New TLS Insn with addend. */
+lu12i.w $r4,%le_hi20_r(TLSL1 + 0x8)
+add.d $r5,$r5,$r4,%le_add_r(TLSL1 + 0x8)
+st.w $r5,$r4,%le_lo12_r(TLSL1 + 0x8)
--
2.20.1
More information about the Binutils
mailing list