Skip to content

Commit 6cfa40e

Browse files
ilovepitstellar
authored andcommitted
[RISCV][lld] Set the type of TLSDESC relocation's referenced local symbol to STT_NOTYPE
When adding fixups for RISCV_TLSDESC_ADD_LO and RISCV_TLSDESC_LOAD_LO, the local label added for RISCV TLSDESC relocations have STT_TLS set, which is incorrect. Instead, these labels should have `STT_NOTYPE`. This patch stops adding such fixups and avoid setting the STT_TLS on these symbols. Failing to do so can cause LLD to emit an error `has an STT_TLS symbol but doesn't have an SHF_TLS section`. We additionally, adjust how LLD services these relocations to avoid errors with incompatible relocation and symbol types. Reviewers: topperc, MaskRay Reviewed By: MaskRay Pull Request: #85817 (cherry picked from commit dfe4ca9)
1 parent 1184a9c commit 6cfa40e

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

lld/ELF/Relocations.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,10 @@ template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
14801480

14811481
// Process TLS relocations, including TLS optimizations. Note that
14821482
// R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
1483-
if (sym.isTls()) {
1483+
//
1484+
// Some RISCV TLSDESC relocations reference a local NOTYPE symbol,
1485+
// but we need to process them in handleTlsRelocation.
1486+
if (sym.isTls() || oneof<R_TLSDESC_PC, R_TLSDESC_CALL>(expr)) {
14841487
if (unsigned processed =
14851488
handleTlsRelocation(type, sym, *sec, offset, addend, expr)) {
14861489
i += processed - 1;

lld/test/ELF/riscv-tlsdesc-relax.s

+8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
# GD64-NEXT: c.add a0, tp
3434
# GD64-NEXT: jal {{.*}} <foo>
3535
## &.got[c]-. = 0x20c0+8 - 0x1020 = 0x10a8
36+
# GD64-LABEL: <.Ltlsdesc_hi1>:
3637
# GD64-NEXT: 1020: auipc a4, 0x1
3738
# GD64-NEXT: ld a5, 0xa8(a4)
3839
# GD64-NEXT: addi a0, a4, 0xa8
3940
# GD64-NEXT: jalr t0, 0x0(a5)
4041
# GD64-NEXT: c.add a0, tp
4142
## &.got[c]-. = 0x20c0+8 - 0x1032 = 0x1096
43+
# GD64-LABEL: <.Ltlsdesc_hi2>:
4244
# GD64-NEXT: 1032: auipc a6, 0x1
4345
# GD64-NEXT: ld a7, 0x96(a6)
4446
# GD64-NEXT: addi a0, a6, 0x96
@@ -64,13 +66,15 @@
6466
# LE64-NEXT: jal {{.*}} <foo>
6567
# LE64-NEXT: R_RISCV_JAL foo
6668
# LE64-NEXT: R_RISCV_RELAX *ABS*
69+
# LE64-LABEL: <.Ltlsdesc_hi1>:
6770
# LE64-NEXT: addi a0, zero, 0x7ff
6871
# LE64-NEXT: R_RISCV_TLSDESC_HI20 b
6972
# LE64-NEXT: R_RISCV_RELAX *ABS*
7073
# LE64-NEXT: R_RISCV_TLSDESC_LOAD_LO12 .Ltlsdesc_hi1
7174
# LE64-NEXT: R_RISCV_TLSDESC_ADD_LO12 .Ltlsdesc_hi1
7275
# LE64-NEXT: R_RISCV_TLSDESC_CALL .Ltlsdesc_hi1
7376
# LE64-NEXT: c.add a0, tp
77+
# LE64-LABEL: <.Ltlsdesc_hi2>:
7478
# LE64-NEXT: addi zero, zero, 0x0
7579
# LE64-NEXT: R_RISCV_TLSDESC_HI20 b
7680
# LE64-NEXT: addi zero, zero, 0x0
@@ -93,9 +97,11 @@
9397
# LE64A-NEXT: addi a0, a0, -0x479
9498
# LE64A-NEXT: c.add a0, tp
9599
# LE64A-NEXT: jal {{.*}} <foo>
100+
# LE64A-LABEL: <.Ltlsdesc_hi1>:
96101
# LE64A-NEXT: lui a0, 0x2
97102
# LE64A-NEXT: addi a0, a0, -0x479
98103
# LE64A-NEXT: c.add a0, tp
104+
# LE64A-LABEL: <.Ltlsdesc_hi2>:
99105
# LE64A-NEXT: addi zero, zero, 0x0
100106
# LE64A-NEXT: addi zero, zero, 0x0
101107
# LE64A-NEXT: lui a0, 0x2
@@ -115,10 +121,12 @@
115121
# IE64-NEXT: c.add a0, tp
116122
# IE64-NEXT: jal {{.*}} <foo>
117123
## &.got[c]-. = 0x120e0+8 - 0x11018 = 0x10d0
124+
# IE64-LABEL: <.Ltlsdesc_hi1>:
118125
# IE64-NEXT: 11018: auipc a0, 0x1
119126
# IE64-NEXT: ld a0, 0xd0(a0)
120127
# IE64-NEXT: c.add a0, tp
121128
## &.got[c]-. = 0x120e0+8 - 0x1102a = 0x10be
129+
# IE64-LABEL: <.Ltlsdesc_hi2>:
122130
# IE64-NEXT: addi zero, zero, 0x0
123131
# IE64-NEXT: addi zero, zero, 0x0
124132
# IE64-NEXT: 1102a: auipc a0, 0x1

lld/test/ELF/riscv-tlsdesc.s

+16-11
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
# RUN: ld.lld -e 0 -z now a.32.o c.32.so -o a.32.ie
3030
# RUN: llvm-objdump --no-show-raw-insn -M no-aliases -h -d a.32.ie | FileCheck %s --check-prefix=IE32
3131

32-
# RUN: llvm-mc -triple=riscv64 -filetype=obj d.s -o d.64.o
33-
# RUN: not ld.lld -shared -soname=d.64.so -o d.64.so d.64.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
32+
## Prior to https://github.com/llvm/llvm-project/pull/85817 the local TLSDESC
33+
## labels would be marked STT_TLS, resulting in an error "has an STT_TLS symbol but doesn't have an SHF_TLS section"
3434

35+
# RUN: llvm-mc -triple=riscv64 -filetype=obj d.s -o d.64.o
36+
# RUN: ld.lld -shared -soname=d.64.so -o d.64.so d.64.o --fatal-warnings
3537
# RUN: llvm-mc -triple=riscv32 -filetype=obj d.s -o d.32.o --defsym ELF32=1
36-
# RUN: not ld.lld -shared -soname=d.32.so -o d.32.so d.32.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
38+
# RUN: ld.lld -shared -soname=d.32.so -o d.32.so d.32.o --fatal-warnings
3739

3840
# GD64-RELA: .rela.dyn {
3941
# GD64-RELA-NEXT: 0x2408 R_RISCV_TLSDESC - 0x7FF
@@ -74,35 +76,37 @@
7476
# GD64-NEXT: add a0, a0, tp
7577

7678
## &.got[b]-. = 0x23e0+40 - 0x12f4 = 0x1114
77-
# GD64-NEXT: 12f4: auipc a2, 0x1
79+
# GD64: 12f4: auipc a2, 0x1
7880
# GD64-NEXT: ld a3, 0x114(a2)
7981
# GD64-NEXT: addi a0, a2, 0x114
8082
# GD64-NEXT: jalr t0, 0x0(a3)
8183
# GD64-NEXT: add a0, a0, tp
8284

8385
## &.got[c]-. = 0x23e0+24 - 0x1308 = 0x10f0
84-
# GD64-NEXT: 1308: auipc a4, 0x1
86+
# GD64: 1308: auipc a4, 0x1
8587
# GD64-NEXT: ld a5, 0xf0(a4)
8688
# GD64-NEXT: addi a0, a4, 0xf0
8789
# GD64-NEXT: jalr t0, 0x0(a5)
8890
# GD64-NEXT: add a0, a0, tp
8991

9092
# NOREL: no relocations
9193

92-
# LE64-LABEL: <.text>:
94+
# LE64-LABEL: <.Ltlsdesc_hi0>:
9395
## st_value(a) = 8
9496
# LE64-NEXT: addi zero, zero, 0x0
9597
# LE64-NEXT: addi zero, zero, 0x0
9698
# LE64-NEXT: addi zero, zero, 0x0
9799
# LE64-NEXT: addi a0, zero, 0x8
98100
# LE64-NEXT: add a0, a0, tp
99101
## st_value(b) = 2047
102+
# LE64-LABEL: <.Ltlsdesc_hi1>:
100103
# LE64-NEXT: addi zero, zero, 0x0
101104
# LE64-NEXT: addi zero, zero, 0x0
102105
# LE64-NEXT: addi zero, zero, 0x0
103106
# LE64-NEXT: addi a0, zero, 0x7ff
104107
# LE64-NEXT: add a0, a0, tp
105108
## st_value(c) = 2048
109+
# LE64-LABEL: <.Ltlsdesc_hi2>:
106110
# LE64-NEXT: addi zero, zero, 0x0
107111
# LE64-NEXT: addi zero, zero, 0x0
108112
# LE64-NEXT: lui a0, 0x1
@@ -116,18 +120,20 @@
116120
# IE64: .got 00000010 00000000000123a8
117121

118122
## a and b are optimized to use LE. c is optimized to IE.
119-
# IE64-LABEL: <.text>:
123+
# IE64-LABEL: <.Ltlsdesc_hi0>:
120124
# IE64-NEXT: addi zero, zero, 0x0
121125
# IE64-NEXT: addi zero, zero, 0x0
122126
# IE64-NEXT: addi zero, zero, 0x0
123127
# IE64-NEXT: addi a0, zero, 0x8
124128
# IE64-NEXT: add a0, a0, tp
129+
# IE64-LABEL: <.Ltlsdesc_hi1>:
125130
# IE64-NEXT: addi zero, zero, 0x0
126131
# IE64-NEXT: addi zero, zero, 0x0
127132
# IE64-NEXT: addi zero, zero, 0x0
128133
# IE64-NEXT: addi a0, zero, 0x7ff
129134
# IE64-NEXT: add a0, a0, tp
130135
## &.got[c]-. = 0x123a8+8 - 0x112b8 = 0x10f8
136+
# IE64-LABEL: <.Ltlsdesc_hi2>:
131137
# IE64-NEXT: addi zero, zero, 0x0
132138
# IE64-NEXT: addi zero, zero, 0x0
133139
# IE64-NEXT: 112b8: auipc a0, 0x1
@@ -136,29 +142,28 @@
136142

137143
# IE32: .got 00000008 00012248
138144

139-
# IE32-LABEL: <.text>:
145+
# IE32-LABEL: <.Ltlsdesc_hi0>:
140146
## st_value(a) = 8
141147
# IE32-NEXT: addi zero, zero, 0x0
142148
# IE32-NEXT: addi zero, zero, 0x0
143149
# IE32-NEXT: addi zero, zero, 0x0
144150
# IE32-NEXT: addi a0, zero, 0x8
145151
# IE32-NEXT: add a0, a0, tp
146152
## st_value(b) = 2047
153+
# IE32-LABEL: <.Ltlsdesc_hi1>:
147154
# IE32-NEXT: addi zero, zero, 0x0
148155
# IE32-NEXT: addi zero, zero, 0x0
149156
# IE32-NEXT: addi zero, zero, 0x0
150157
# IE32-NEXT: addi a0, zero, 0x7ff
151158
# IE32-NEXT: add a0, a0, tp
152159
## &.got[c]-. = 0x12248+4 - 0x111cc = 0x1080
160+
# IE32-LABEL: <.Ltlsdesc_hi2>:
153161
# IE32-NEXT: addi zero, zero, 0x0
154162
# IE32-NEXT: addi zero, zero, 0x0
155163
# IE32-NEXT: 111cc: auipc a0, 0x1
156164
# IE32-NEXT: lw a0, 0x80(a0)
157165
# IE32-NEXT: add a0, a0, tp
158166

159-
## FIXME This should not pass, but the code MC layer needs a fix to prevent this.
160-
# BADTLSLABEL: error: d.{{.*}}.o has an STT_TLS symbol but doesn't have an SHF_TLS section
161-
162167
#--- a.s
163168
.macro load dst, src
164169
.ifdef ELF32

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
207207
case VK_RISCV_TLS_GOT_HI:
208208
case VK_RISCV_TLS_GD_HI:
209209
case VK_RISCV_TLSDESC_HI:
210-
case VK_RISCV_TLSDESC_ADD_LO:
211-
case VK_RISCV_TLSDESC_LOAD_LO:
212210
break;
213211
}
214212

0 commit comments

Comments
 (0)