Skip to content

Commit 9178708

Browse files
authored
[PAC][lld][AArch64][ELF] Support signed TLSDESC (#113817)
Depends on #120010 Support `R_AARCH64_AUTH_TLSDESC_ADR_PAGE21`, `R_AARCH64_AUTH_TLSDESC_LD64_LO12` and `R_AARCH64_AUTH_TLSDESC_LD64_LO12` static relocations and `R_AARCH64_AUTH_TLSDESC` dynamic relocation. IE/LE optimization is not currently supported for AUTH TLSDESC.
1 parent 70f8040 commit 9178708

8 files changed

+198
-3
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,14 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
157157
return RE_AARCH64_AUTH;
158158
case R_AARCH64_TLSDESC_ADR_PAGE21:
159159
return RE_AARCH64_TLSDESC_PAGE;
160+
case R_AARCH64_AUTH_TLSDESC_ADR_PAGE21:
161+
return RE_AARCH64_AUTH_TLSDESC_PAGE;
160162
case R_AARCH64_TLSDESC_LD64_LO12:
161163
case R_AARCH64_TLSDESC_ADD_LO12:
162164
return R_TLSDESC;
165+
case R_AARCH64_AUTH_TLSDESC_LD64_LO12:
166+
case R_AARCH64_AUTH_TLSDESC_ADD_LO12:
167+
return RE_AARCH64_AUTH_TLSDESC;
163168
case R_AARCH64_TLSDESC_CALL:
164169
return R_TLSDESC_CALL;
165170
case R_AARCH64_TLSLE_ADD_TPREL_HI12:
@@ -545,6 +550,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
545550
case R_AARCH64_ADR_PREL_PG_HI21:
546551
case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
547552
case R_AARCH64_TLSDESC_ADR_PAGE21:
553+
case R_AARCH64_AUTH_TLSDESC_ADR_PAGE21:
548554
checkInt(ctx, loc, val, 33, rel);
549555
[[fallthrough]];
550556
case R_AARCH64_ADR_PREL_PG_HI21_NC:
@@ -597,6 +603,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
597603
case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
598604
case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
599605
case R_AARCH64_TLSDESC_LD64_LO12:
606+
case R_AARCH64_AUTH_TLSDESC_LD64_LO12:
600607
checkAlignment(ctx, loc, val, 8, rel);
601608
write32Imm12(loc, getBits(val, 3, 11));
602609
break;
@@ -671,6 +678,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
671678
break;
672679
case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
673680
case R_AARCH64_TLSDESC_ADD_LO12:
681+
case R_AARCH64_AUTH_TLSDESC_ADD_LO12:
674682
write32Imm12(loc, val);
675683
break;
676684
case R_AARCH64_TLSDESC:

lld/ELF/InputSection.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,14 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
966966
case R_SIZE:
967967
return r.sym->getSize() + a;
968968
case R_TLSDESC:
969+
case RE_AARCH64_AUTH_TLSDESC:
969970
return ctx.in.got->getTlsDescAddr(*r.sym) + a;
970971
case R_TLSDESC_PC:
971972
return ctx.in.got->getTlsDescAddr(*r.sym) + a - p;
972973
case R_TLSDESC_GOTPLT:
973974
return ctx.in.got->getTlsDescAddr(*r.sym) + a - ctx.in.gotPlt->getVA();
974975
case RE_AARCH64_TLSDESC_PAGE:
976+
case RE_AARCH64_AUTH_TLSDESC_PAGE:
975977
return getAArch64Page(ctx.in.got->getTlsDescAddr(*r.sym) + a) -
976978
getAArch64Page(p);
977979
case RE_LOONGARCH_TLSDESC_PAGE_PC:

lld/ELF/Relocations.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,27 @@ static unsigned handleMipsTlsRelocation(Ctx &ctx, RelType type, Symbol &sym,
12921292
return 0;
12931293
}
12941294

1295+
static unsigned handleAArch64PAuthTlsRelocation(InputSectionBase *sec,
1296+
RelExpr expr, RelType type,
1297+
uint64_t offset, Symbol &sym,
1298+
int64_t addend) {
1299+
// Do not optimize signed TLSDESC to LE/IE (as described in pauthabielf64).
1300+
// https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions
1301+
// > PAUTHELF64 only supports the descriptor based TLS (TLSDESC).
1302+
if (oneof<RE_AARCH64_AUTH_TLSDESC_PAGE, RE_AARCH64_AUTH_TLSDESC>(expr)) {
1303+
sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH);
1304+
sec->addReloc({expr, type, offset, addend, &sym});
1305+
return 1;
1306+
}
1307+
1308+
// TLSDESC_CALL hint relocation should not be emitted by compiler with signed
1309+
// TLSDESC enabled.
1310+
if (expr == R_TLSDESC_CALL)
1311+
sym.setFlags(NEEDS_TLSDESC_NONAUTH);
1312+
1313+
return 0;
1314+
}
1315+
12951316
// Notes about General Dynamic and Local Dynamic TLS models below. They may
12961317
// require the generation of a pair of GOT entries that have associated dynamic
12971318
// relocations. The pair of GOT entries created are of the form GOT[e0] Module
@@ -1302,6 +1323,13 @@ static unsigned handleMipsTlsRelocation(Ctx &ctx, RelType type, Symbol &sym,
13021323
unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
13031324
uint64_t offset, Symbol &sym,
13041325
int64_t addend) {
1326+
bool isAArch64 = ctx.arg.emachine == EM_AARCH64;
1327+
1328+
if (isAArch64)
1329+
if (unsigned processed = handleAArch64PAuthTlsRelocation(
1330+
sec, expr, type, offset, sym, addend))
1331+
return processed;
1332+
13051333
if (expr == R_TPREL || expr == R_TPREL_NEG) {
13061334
if (ctx.arg.shared) {
13071335
auto diag = Err(ctx);
@@ -1336,7 +1364,9 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
13361364
// R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} reference a label. Do not
13371365
// set NEEDS_TLSDESC on the label.
13381366
if (expr != R_TLSDESC_CALL) {
1339-
if (!isRISCV || type == R_RISCV_TLSDESC_HI20)
1367+
if (isAArch64)
1368+
sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_NONAUTH);
1369+
else if (!isRISCV || type == R_RISCV_TLSDESC_HI20)
13401370
sym.setFlags(NEEDS_TLSDESC);
13411371
sec->addReloc({expr, type, offset, addend, &sym});
13421372
}
@@ -1847,10 +1877,21 @@ void elf::postScanRelocations(Ctx &ctx) {
18471877
GotSection *got = ctx.in.got.get();
18481878

18491879
if (flags & NEEDS_TLSDESC) {
1880+
if ((flags & NEEDS_TLSDESC_AUTH) && (flags & NEEDS_TLSDESC_NONAUTH)) {
1881+
Err(ctx)
1882+
<< "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName()
1883+
<< "' requested, but only one type of TLSDESC entry per symbol is "
1884+
"supported";
1885+
return;
1886+
}
18501887
got->addTlsDescEntry(sym);
1888+
RelType tlsDescRel = ctx.target->tlsDescRel;
1889+
if (flags & NEEDS_TLSDESC_AUTH) {
1890+
got->addTlsDescAuthEntry();
1891+
tlsDescRel = ELF::R_AARCH64_AUTH_TLSDESC;
1892+
}
18511893
ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
1852-
ctx.target->tlsDescRel, *got, got->getTlsDescOffset(sym), sym,
1853-
ctx.target->tlsDescRel);
1894+
tlsDescRel, *got, got->getTlsDescOffset(sym), sym, tlsDescRel);
18541895
}
18551896
if (flags & NEEDS_TLSGD) {
18561897
got->addDynTlsEntry(sym);

lld/ELF/Relocations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ enum RelExpr {
9999
RE_AARCH64_PAGE_PC,
100100
RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
101101
RE_AARCH64_TLSDESC_PAGE,
102+
RE_AARCH64_AUTH_TLSDESC_PAGE,
103+
RE_AARCH64_AUTH_TLSDESC,
102104
RE_AARCH64_AUTH,
103105
RE_ARM_PCA,
104106
RE_ARM_SBREL,

lld/ELF/Symbols.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ enum {
5353
NEEDS_TLSIE = 1 << 8,
5454
NEEDS_GOT_AUTH = 1 << 9,
5555
NEEDS_GOT_NONAUTH = 1 << 10,
56+
NEEDS_TLSDESC_AUTH = 1 << 11,
57+
NEEDS_TLSDESC_NONAUTH = 1 << 12,
5658
};
5759

5860
// The base class for real symbol classes.

lld/ELF/SyntheticSections.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ bool GotSection::addTlsDescEntry(const Symbol &sym) {
681681
return true;
682682
}
683683

684+
void GotSection::addTlsDescAuthEntry() {
685+
authEntries.push_back({(numEntries - 2) * ctx.arg.wordsize, true});
686+
authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, false});
687+
}
688+
684689
bool GotSection::addDynTlsEntry(const Symbol &sym) {
685690
assert(sym.auxIdx == ctx.symAux.size() - 1);
686691
ctx.symAux.back().tlsGdIdx = numEntries;

lld/ELF/SyntheticSections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class GotSection final : public SyntheticSection {
114114
void addEntry(const Symbol &sym);
115115
void addAuthEntry(const Symbol &sym);
116116
bool addTlsDescEntry(const Symbol &sym);
117+
void addTlsDescAuthEntry();
117118
bool addDynTlsEntry(const Symbol &sym);
118119
bool addTlsIndex();
119120
uint32_t getTlsDescOffset(const Symbol &sym) const;

lld/test/ELF/aarch64-tlsdesc-pauth.s

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// REQUIRES: aarch64
2+
// RUN: rm -rf %t && split-file %s %t && cd %t
3+
4+
//--- a.s
5+
.section .tbss,"awT",@nobits
6+
.global a
7+
a:
8+
.xword 0
9+
10+
//--- ok.s
11+
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth ok.s -o ok.o
12+
// RUN: ld.lld -shared ok.o -o ok.so
13+
// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn ok.so | \
14+
// RUN: FileCheck -DP=20 -DA=896 -DB=912 -DC=928 %s
15+
// RUN: llvm-readobj -r -x .got ok.so | FileCheck --check-prefix=REL \
16+
// RUN: -DP1=20 -DA1=380 -DB1=390 -DC1=3A0 -DP2=020 -DA2=380 -DB2=390 -DC2=3a0 %s
17+
18+
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth a.s -o a.so.o
19+
// RUN: ld.lld -shared a.so.o -soname=so -o a.so
20+
// RUN: ld.lld ok.o a.so -o ok
21+
// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn ok | \
22+
// RUN: FileCheck -DP=220 -DA=936 -DB=952 -DC=968 %s
23+
// RUN: llvm-readobj -r -x .got ok | FileCheck --check-prefix=REL \
24+
// RUN: -DP1=220 -DA1=3A8 -DB1=3B8 -DC1=3C8 -DP2=220 -DA2=3a8 -DB2=3b8 -DC2=3c8 %s
25+
26+
.text
27+
adrp x0, :tlsdesc_auth:a
28+
ldr x16, [x0, :tlsdesc_auth_lo12:a]
29+
add x0, x0, :tlsdesc_auth_lo12:a
30+
blraa x16, x0
31+
32+
// CHECK: adrp x0, 0x[[P]]000
33+
// CHECK-NEXT: ldr x16, [x0, #[[A]]]
34+
// CHECK-NEXT: add x0, x0, #[[A]]
35+
// CHECK-NEXT: blraa x16, x0
36+
37+
/// Create relocation against local TLS symbols where linker should
38+
/// create target specific dynamic TLSDESC relocation where addend is
39+
/// the symbol VMA in tls block.
40+
41+
adrp x0, :tlsdesc_auth:local1
42+
ldr x16, [x0, :tlsdesc_auth_lo12:local1]
43+
add x0, x0, :tlsdesc_auth_lo12:local1
44+
blraa x16, x0
45+
46+
// CHECK: adrp x0, 0x[[P]]000
47+
// CHECK-NEXT: ldr x16, [x0, #[[B]]]
48+
// CHECK-NEXT: add x0, x0, #[[B]]
49+
// CHECK-NEXT: blraa x16, x0
50+
51+
adrp x0, :tlsdesc_auth:local2
52+
ldr x16, [x0, :tlsdesc_auth_lo12:local2]
53+
add x0, x0, :tlsdesc_auth_lo12:local2
54+
blraa x16, x0
55+
56+
// CHECK: adrp x0, 0x[[P]]000
57+
// CHECK-NEXT: ldr x16, [x0, #[[C]]]
58+
// CHECK-NEXT: add x0, x0, #[[C]]
59+
// CHECK-NEXT: blraa x16, x0
60+
61+
.section .tbss,"awT",@nobits
62+
.type local1,@object
63+
.p2align 2
64+
local1:
65+
.word 0
66+
.size local1, 4
67+
68+
.type local2,@object
69+
.p2align 3
70+
local2:
71+
.xword 0
72+
.size local2, 8
73+
74+
75+
// R_AARCH64_AUTH_TLSDESC - 0x0 -> start of tls block
76+
// R_AARCH64_AUTH_TLSDESC - 0x8 -> align (sizeof (local1), 8)
77+
78+
// REL: Relocations [
79+
// REL-NEXT: Section (5) .rela.dyn {
80+
// REL-NEXT: 0x[[P1]][[B1]] R_AARCH64_AUTH_TLSDESC - 0x0
81+
// REL-NEXT: 0x[[P1]][[C1]] R_AARCH64_AUTH_TLSDESC - 0x8
82+
// REL-NEXT: 0x[[P1]][[A1]] R_AARCH64_AUTH_TLSDESC a 0x0
83+
// REL-NEXT: }
84+
// REL-NEXT: ]
85+
86+
// REL: Hex dump of section '.got':
87+
// REL-NEXT: 0x00[[P2]][[A2]] 00000000 00000080 00000000 000000a0
88+
// REL-NEXT: 0x00[[P2]][[B2]] 00000000 00000080 00000000 000000a0
89+
// REL-NEXT: 0x00[[P2]][[C2]] 00000000 00000080 00000000 000000a0
90+
/// ^^
91+
/// 0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
92+
/// ^^
93+
/// 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
94+
95+
//--- err1.s
96+
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o
97+
// RUN: not ld.lld -shared err1.o 2>&1 | FileCheck --check-prefix=ERR1 --implicit-check-not=error: %s
98+
// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported
99+
.text
100+
adrp x0, :tlsdesc_auth:a
101+
ldr x16, [x0, :tlsdesc_auth_lo12:a]
102+
add x0, x0, :tlsdesc_auth_lo12:a
103+
blraa x16, x0
104+
105+
adrp x0, :tlsdesc:a
106+
ldr x1, [x0, :tlsdesc_lo12:a]
107+
add x0, x0, :tlsdesc_lo12:a
108+
blr x1
109+
110+
//--- err2.s
111+
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o
112+
// RUN: not ld.lld -shared err2.o 2>&1 | FileCheck --check-prefix=ERR2 --implicit-check-not=error: %s
113+
// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported
114+
.text
115+
adrp x0, :tlsdesc:a
116+
ldr x1, [x0, :tlsdesc_lo12:a]
117+
add x0, x0, :tlsdesc_lo12:a
118+
blr x1
119+
120+
adrp x0, :tlsdesc_auth:a
121+
ldr x16, [x0, :tlsdesc_auth_lo12:a]
122+
add x0, x0, :tlsdesc_auth_lo12:a
123+
blraa x16, x0
124+
125+
//--- err3.s
126+
// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err3.s -o err3.o
127+
// RUN: not ld.lld -shared err3.o 2>&1 | FileCheck --check-prefix=ERR3 --implicit-check-not=error: %s
128+
// ERR3: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported
129+
.text
130+
adrp x0, :tlsdesc_auth:a
131+
ldr x16, [x0, :tlsdesc_auth_lo12:a]
132+
add x0, x0, :tlsdesc_auth_lo12:a
133+
.tlsdesccall a
134+
blraa x16, x0

0 commit comments

Comments
 (0)