[lld] [ELF] Fix TLSDESC=>IE when there is no TLS section (PR #98569)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 11 17:51:43 PDT 2024
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/98569
>From 39663d0e3d4e475cdb265e06de839bdab6ff96ef Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 11 Jul 2024 17:38:38 -0700
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
lld/ELF/Arch/RISCV.cpp | 4 ++--
lld/ELF/InputSection.cpp | 7 ++++++-
lld/test/ELF/riscv-tlsdesc.s | 23 +++++++++++++++++++++++
3 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index faacc8f834be7..6af89ce3517b7 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -631,8 +631,8 @@ void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
continue;
case R_RELAX_TLS_GD_TO_LE:
// See the comment in handleTlsRelocation. For TLSDESC=>IE,
- // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12,CALL} also reach here. If isToIe is
- // true, this is actually TLSDESC=>IE optimization.
+ // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12,CALL} also reach here. If isToLe is
+ // false, this is actually TLSDESC=>IE optimization.
if (rel.type == R_RISCV_TLSDESC_HI20) {
tlsdescVal = val;
isToLe = true;
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 4420be77f6685..6ea4453581894 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -660,7 +660,12 @@ static int64_t getTlsTpOffset(const Symbol &s) {
return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000;
case EM_LOONGARCH:
case EM_RISCV:
- return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1));
+ // See the comment in handleTlsRelocation. For TLSDESC=>IE,
+ // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} also reach here. While
+ // `tls` may be null, the return value is ignored.
+ if (s.type != STT_TLS)
+ return 0;
+ return s.getVA(0) + (tls ? tls->p_vaddr & (tls->p_align - 1) : 0);
// Variant 2.
case EM_HEXAGON:
diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index 935ecbddfbfff..05d94d7538f92 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -37,6 +37,11 @@
# RUN: llvm-mc -triple=riscv32 -filetype=obj d.s -o d.32.o --defsym ELF32=1
# RUN: ld.lld -shared -soname=d.32.so -o d.32.so d.32.o --fatal-warnings
+## The output has no TLS section.
+# RUN: llvm-mc -filetype=obj -triple=riscv64 a1.s -o a1.64.o
+# RUN: ld.lld -pie a1.64.o c.64.so -o a1.64
+# RUN: llvm-objdump --no-show-raw-insn -M no-aliases -Rd a1.64 | FileCheck %s --check-prefix=IE64A
+
# GD64-RELA: .rela.dyn {
# GD64-RELA-NEXT: 0x2408 R_RISCV_TLSDESC - 0x7FF
# GD64-RELA-NEXT: 0x23E8 R_RISCV_TLSDESC a 0x0
@@ -164,6 +169,15 @@
# IE32-NEXT: lw a0, 0x80(a0)
# IE32-NEXT: add a0, a0, tp
+# IE64A-LABEL: 0000000000002340 R_RISCV_TLS_TPREL64 c
+## &.got[c]-. = 0x2340 - 0x1258 = 0x10e8
+# IE64A-LABEL: <.Ltlsdesc_hi2>:
+# IE64A-NEXT: addi zero, zero, 0x0
+# IE64A-NEXT: addi zero, zero, 0x0
+# IE64A-NEXT: 1258: auipc a0, 0x1
+# IE64A-NEXT: ld a0, 0xe8(a0)
+# IE64A-NEXT: add a0, a0, tp
+
#--- a.s
.macro load dst, src
.ifdef ELF32
@@ -202,6 +216,15 @@ a:
b:
.zero 1
+#--- a1.s
+## a.s without TLS definitions.
+.Ltlsdesc_hi2:
+ auipc a4, %tlsdesc_hi(c)
+ ld a5, %tlsdesc_load_lo(.Ltlsdesc_hi2)(a4)
+ addi a0, a4, %tlsdesc_add_lo(.Ltlsdesc_hi2)
+ jalr t0, 0(a5), %tlsdesc_call(.Ltlsdesc_hi2)
+ add a0, a0, tp
+
#--- c.s
.tbss
.globl c
>From 99fd36552c81e19e7bc044f89de9777da3050e59 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 11 Jul 2024 17:47:43 -0700
Subject: [PATCH 2/3] update desc
Created using spr 1.3.5-bogner
---
lld/test/ELF/riscv-tlsdesc.s | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index 05d94d7538f92..2314ddf4a7fea 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -37,7 +37,7 @@
# RUN: llvm-mc -triple=riscv32 -filetype=obj d.s -o d.32.o --defsym ELF32=1
# RUN: ld.lld -shared -soname=d.32.so -o d.32.so d.32.o --fatal-warnings
-## The output has no TLS section.
+## The output has a TLS reference but no TLS section.
# RUN: llvm-mc -filetype=obj -triple=riscv64 a1.s -o a1.64.o
# RUN: ld.lld -pie a1.64.o c.64.so -o a1.64
# RUN: llvm-objdump --no-show-raw-insn -M no-aliases -Rd a1.64 | FileCheck %s --check-prefix=IE64A
>From ea3107077d8861f5deb75c360fb60c001f6808f7 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 11 Jul 2024 17:51:34 -0700
Subject: [PATCH 3/3] improve test
Created using spr 1.3.5-bogner
---
lld/ELF/InputSection.cpp | 2 +-
lld/test/ELF/riscv-tlsdesc.s | 4 +++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 6ea4453581894..12ab1f1eac808 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -665,7 +665,7 @@ static int64_t getTlsTpOffset(const Symbol &s) {
// `tls` may be null, the return value is ignored.
if (s.type != STT_TLS)
return 0;
- return s.getVA(0) + (tls ? tls->p_vaddr & (tls->p_align - 1) : 0);
+ return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1));
// Variant 2.
case EM_HEXAGON:
diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index 2314ddf4a7fea..6c0dd9a247a4e 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -169,7 +169,9 @@
# IE32-NEXT: lw a0, 0x80(a0)
# IE32-NEXT: add a0, a0, tp
-# IE64A-LABEL: 0000000000002340 R_RISCV_TLS_TPREL64 c
+# IE64A: OFFSET TYPE VALUE
+# IE64A-NEXT: 0000000000002340 R_RISCV_TLS_TPREL64 c
+# IE64A-EMPTY:
## &.got[c]-. = 0x2340 - 0x1258 = 0x10e8
# IE64A-LABEL: <.Ltlsdesc_hi2>:
# IE64A-NEXT: addi zero, zero, 0x0
More information about the llvm-commits
mailing list