From 78f71e12a6316827658481ac3cef0ffe0b8a8f8c Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Fri, 25 Oct 2024 12:32:27 +0300 Subject: [PATCH 01/13] [PAC][lld][AArch64][ELF] Support signed TLSDESC Support `R_AARCH64_AUTH_TLSDESC_ADR_PAGE21`, `R_AARCH64_AUTH_TLSDESC_LD64_LO12` and `R_AARCH64_AUTH_TLSDESC_LD64_LO12` static TLSDESC relocations. --- lld/ELF/Arch/AArch64.cpp | 8 ++ lld/ELF/InputSection.cpp | 2 + lld/ELF/Relocations.cpp | 38 +++++++- lld/ELF/Relocations.h | 2 + lld/ELF/Symbols.h | 1 + lld/ELF/SyntheticSections.cpp | 5 + lld/test/ELF/aarch64-tlsdesc-pauth.s | 134 +++++++++++++++++++++++++++ 7 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 lld/test/ELF/aarch64-tlsdesc-pauth.s diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp index b63551d0f682e..9538dd4a70bae 100644 --- a/lld/ELF/Arch/AArch64.cpp +++ b/lld/ELF/Arch/AArch64.cpp @@ -157,9 +157,14 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s, return RE_AARCH64_AUTH; case R_AARCH64_TLSDESC_ADR_PAGE21: return RE_AARCH64_TLSDESC_PAGE; + case R_AARCH64_AUTH_TLSDESC_ADR_PAGE21: + return RE_AARCH64_AUTH_TLSDESC_PAGE; case R_AARCH64_TLSDESC_LD64_LO12: case R_AARCH64_TLSDESC_ADD_LO12: return R_TLSDESC; + case R_AARCH64_AUTH_TLSDESC_LD64_LO12: + case R_AARCH64_AUTH_TLSDESC_ADD_LO12: + return RE_AARCH64_AUTH_TLSDESC; case R_AARCH64_TLSDESC_CALL: return R_TLSDESC_CALL; case R_AARCH64_TLSLE_ADD_TPREL_HI12: @@ -545,6 +550,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel, case R_AARCH64_ADR_PREL_PG_HI21: case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: case R_AARCH64_TLSDESC_ADR_PAGE21: + case R_AARCH64_AUTH_TLSDESC_ADR_PAGE21: checkInt(ctx, loc, val, 33, rel); [[fallthrough]]; case R_AARCH64_ADR_PREL_PG_HI21_NC: @@ -597,6 +603,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel, case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: case R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC: case R_AARCH64_TLSDESC_LD64_LO12: + case R_AARCH64_AUTH_TLSDESC_LD64_LO12: checkAlignment(ctx, loc, val, 8, rel); write32Imm12(loc, getBits(val, 3, 11)); break; @@ -671,6 +678,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel, break; case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: case R_AARCH64_TLSDESC_ADD_LO12: + case R_AARCH64_AUTH_TLSDESC_ADD_LO12: write32Imm12(loc, val); break; case R_AARCH64_TLSDESC: diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index efa7ba3e7cb06..e92003c856682 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -966,12 +966,14 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r, case R_SIZE: return r.sym->getSize() + a; case R_TLSDESC: + case RelExpr::R_AARCH64_AUTH_TLSDESC: return ctx.in.got->getTlsDescAddr(*r.sym) + a; case R_TLSDESC_PC: return ctx.in.got->getTlsDescAddr(*r.sym) + a - p; case R_TLSDESC_GOTPLT: return ctx.in.got->getTlsDescAddr(*r.sym) + a - ctx.in.gotPlt->getVA(); case RE_AARCH64_TLSDESC_PAGE: + case RE_AARCH64_AUTH_TLSDESC_PAGE: return getAArch64Page(ctx.in.got->getTlsDescAddr(*r.sym) + a) - getAArch64Page(p); case RE_LOONGARCH_TLSDESC_PAGE_PC: diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 77b90172bc1c2..47c1a460ab225 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1328,6 +1328,36 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, return 1; } + auto fatalBothAuthAndNonAuth = [&sym]() { + fatal("both AUTH and non-AUTH TLSDESC entries for '" + sym.getName() + + "' requested, but only one type of TLSDESC entry per symbol is " + "supported"); + }; + + // Do not optimize signed TLSDESC as described in pauthabielf64 to LE/IE. + // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions + // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). + if (oneof( + expr)) { + assert(ctx.arg.emachine == EM_AARCH64); + if (!sym.hasFlag(NEEDS_TLSDESC)) + sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); + else if (!sym.hasFlag(NEEDS_TLSDESC_AUTH)) + fatalBothAuthAndNonAuth(); + sec->addReloc({expr, type, offset, addend, &sym}); + return 1; + } + + if (sym.hasFlag(NEEDS_TLSDESC_AUTH)) { + assert(ctx.arg.emachine == EM_AARCH64); + // TLSDESC_CALL hint relocation probably should not be emitted by compiler + // with signed TLSDESC enabled since it does not give any value, but leave a + // check against that just in case someone uses it. + if (expr != R_TLSDESC_CALL) + fatalBothAuthAndNonAuth(); + return 1; + } + bool isRISCV = ctx.arg.emachine == EM_RISCV; if (oneofaddTlsDescEntry(sym); + RelType tlsDescRel = ctx.target->tlsDescRel; + if (flags & NEEDS_TLSDESC_AUTH) { + assert(ctx.arg.emachine == EM_AARCH64); + tlsDescRel = ELF::R_AARCH64_AUTH_TLSDESC; + } ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( - ctx.target->tlsDescRel, *got, got->getTlsDescOffset(sym), sym, - ctx.target->tlsDescRel); + tlsDescRel, *got, got->getTlsDescOffset(sym), sym, tlsDescRel); } if (flags & NEEDS_TLSGD) { got->addDynTlsEntry(sym); diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h index fde25a230b72e..d2a77bc953109 100644 --- a/lld/ELF/Relocations.h +++ b/lld/ELF/Relocations.h @@ -99,6 +99,8 @@ enum RelExpr { RE_AARCH64_PAGE_PC, RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC, RE_AARCH64_TLSDESC_PAGE, + RE_AARCH64_AUTH_TLSDESC_PAGE, + RE_AARCH64_AUTH_TLSDESC, RE_AARCH64_AUTH, RE_ARM_PCA, RE_ARM_SBREL, diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index a59faf1037cb2..7f409dd38fcb8 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -53,6 +53,7 @@ enum { NEEDS_TLSIE = 1 << 8, NEEDS_GOT_AUTH = 1 << 9, NEEDS_GOT_NONAUTH = 1 << 10, + NEEDS_TLSDESC_AUTH = 1 << 11, }; // The base class for real symbol classes. diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 10cbfe19b3b0a..da22d93f24b50 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -678,6 +678,11 @@ bool GotSection::addTlsDescEntry(const Symbol &sym) { assert(sym.auxIdx == ctx.symAux.size() - 1); ctx.symAux.back().tlsDescIdx = numEntries; numEntries += 2; + if (sym.hasFlag(NEEDS_TLSDESC_AUTH)) { + assert(ctx.arg.emachine == EM_AARCH64); + authEntries.push_back({(numEntries - 2) * ctx.arg.wordsize, true}); + authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, false}); + } return true; } diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s new file mode 100644 index 0000000000000..36505f652b0c5 --- /dev/null +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -0,0 +1,134 @@ +// REQUIRES: aarch64 +// RUN: rm -rf %t && split-file %s %t && cd %t + +//--- a.s + +.section .tbss,"awT",@nobits +.global a +a: +.xword 0 + +//--- ok.s + +// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth ok.s -o ok.o +// RUN: ld.lld -shared ok.o -o ok.so +// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn ok.so | \ +// RUN: FileCheck -DP=20 -DA=896 -DB=912 -DC=928 %s +// RUN: llvm-readobj -r -x .got ok.so | FileCheck --check-prefix=REL \ +// RUN: -DP1=20 -DA1=380 -DB1=390 -DC1=3A0 -DP2=020 -DA2=380 -DB2=390 -DC2=3a0 %s + +// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth a.s -o a.so.o +// RUN: ld.lld -shared a.so.o -soname=so -o a.so +// RUN: ld.lld ok.o a.so -o ok +// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn ok | \ +// RUN: FileCheck -DP=220 -DA=936 -DB=952 -DC=968 %s +// RUN: llvm-readobj -r -x .got ok | FileCheck --check-prefix=REL \ +// RUN: -DP1=220 -DA1=3A8 -DB1=3B8 -DC1=3C8 -DP2=220 -DA2=3a8 -DB2=3b8 -DC2=3c8 %s + + .text + adrp x0, :tlsdesc_auth:a + ldr x16, [x0, :tlsdesc_auth_lo12:a] + add x0, x0, :tlsdesc_auth_lo12:a + .tlsdesccall a + blraa x16, x0 + +// CHECK: adrp x0, 0x[[P]]000 +// CHECK-NEXT: ldr x16, [x0, #[[A]]] +// CHECK-NEXT: add x0, x0, #[[A]] +// CHECK-NEXT: blraa x16, x0 + +// Create relocation against local TLS symbols where linker should +// create target specific dynamic TLSDESC relocation where addend is +// the symbol VMA in tls block. + + adrp x0, :tlsdesc_auth:local1 + ldr x16, [x0, :tlsdesc_auth_lo12:local1] + add x0, x0, :tlsdesc_auth_lo12:local1 + .tlsdesccall local1 + blraa x16, x0 + +// CHECK: adrp x0, 0x[[P]]000 +// CHECK-NEXT: ldr x16, [x0, #[[B]]] +// CHECK-NEXT: add x0, x0, #[[B]] +// CHECK-NEXT: blraa x16, x0 + + adrp x0, :tlsdesc_auth:local2 + ldr x16, [x0, :tlsdesc_auth_lo12:local2] + add x0, x0, :tlsdesc_auth_lo12:local2 + .tlsdesccall local2 + blraa x16, x0 + +// CHECK: adrp x0, 0x[[P]]000 +// CHECK-NEXT: ldr x16, [x0, #[[C]]] +// CHECK-NEXT: add x0, x0, #[[C]] +// CHECK-NEXT: blraa x16, x0 + + .section .tbss,"awT",@nobits + .type local1,@object + .p2align 2 +local1: + .word 0 + .size local1, 4 + + .type local2,@object + .p2align 3 +local2: + .xword 0 + .size local2, 8 + + +// R_AARCH64_AUTH_TLSDESC - 0x0 -> start of tls block +// R_AARCH64_AUTH_TLSDESC - 0x8 -> align (sizeof (local1), 8) + +// REL: Relocations [ +// REL-NEXT: Section (5) .rela.dyn { +// REL-NEXT: 0x[[P1]][[B1]] R_AARCH64_AUTH_TLSDESC - 0x0 +// REL-NEXT: 0x[[P1]][[C1]] R_AARCH64_AUTH_TLSDESC - 0x8 +// REL-NEXT: 0x[[P1]][[A1]] R_AARCH64_AUTH_TLSDESC a 0x0 +// REL-NEXT: } +// REL-NEXT: ] + +// REL: Hex dump of section '.got': +// REL-NEXT: 0x00[[P2]][[A2]] 00000000 00000080 00000000 000000a0 +// REL-NEXT: 0x00[[P2]][[B2]] 00000000 00000080 00000000 000000a0 +// REL-NEXT: 0x00[[P2]][[C2]] 00000000 00000080 00000000 000000a0 +// ^^ +// 0b10000000 bit 63 address diversity = true, bits 61..60 key = IA +// ^^ +// 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA + +//--- err1.s + +// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o +// RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 %s +// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported + .text + adrp x0, :tlsdesc_auth:a + ldr x16, [x0, :tlsdesc_auth_lo12:a] + add x0, x0, :tlsdesc_auth_lo12:a + .tlsdesccall a + blraa x16, x0 + + adrp x0, :tlsdesc:a + ldr x1, [x0, :tlsdesc_lo12:a] + add x0, x0, :tlsdesc_lo12:a + .tlsdesccall a + blr x1 + +//--- err2.s + +// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o +// RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 %s +// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported + .text + adrp x0, :tlsdesc:a + ldr x1, [x0, :tlsdesc_lo12:a] + add x0, x0, :tlsdesc_lo12:a + .tlsdesccall a + blr x1 + + adrp x0, :tlsdesc_auth:a + ldr x16, [x0, :tlsdesc_auth_lo12:a] + add x0, x0, :tlsdesc_auth_lo12:a + .tlsdesccall a + blraa x16, x0 From 5e81d2a52b806f5189bce5210950309312c2d13c Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Fri, 1 Nov 2024 13:31:14 +0300 Subject: [PATCH 02/13] Address review comments --- lld/ELF/Relocations.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 47c1a460ab225..daba65d8d4ffe 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1334,7 +1334,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, "supported"); }; - // Do not optimize signed TLSDESC as described in pauthabielf64 to LE/IE. + // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). if (oneof( From e00c829b9e921a5b1978da185cbdc26bf0d4767a Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sun, 10 Nov 2024 20:02:24 +0300 Subject: [PATCH 03/13] Use /// for comments in tests --- lld/test/ELF/aarch64-tlsdesc-pauth.s | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index 36505f652b0c5..3c920c98cac6d 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -37,9 +37,9 @@ a: // CHECK-NEXT: add x0, x0, #[[A]] // CHECK-NEXT: blraa x16, x0 -// Create relocation against local TLS symbols where linker should -// create target specific dynamic TLSDESC relocation where addend is -// the symbol VMA in tls block. +/// Create relocation against local TLS symbols where linker should +/// create target specific dynamic TLSDESC relocation where addend is +/// the symbol VMA in tls block. adrp x0, :tlsdesc_auth:local1 ldr x16, [x0, :tlsdesc_auth_lo12:local1] @@ -92,10 +92,10 @@ local2: // REL-NEXT: 0x00[[P2]][[A2]] 00000000 00000080 00000000 000000a0 // REL-NEXT: 0x00[[P2]][[B2]] 00000000 00000080 00000000 000000a0 // REL-NEXT: 0x00[[P2]][[C2]] 00000000 00000080 00000000 000000a0 -// ^^ -// 0b10000000 bit 63 address diversity = true, bits 61..60 key = IA -// ^^ -// 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA +/// ^^ +/// 0b10000000 bit 63 address diversity = true, bits 61..60 key = IA +/// ^^ +/// 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA //--- err1.s From bde335027e5a5f9ed4cd022a946356132d3c0d7a Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sun, 10 Nov 2024 20:38:36 +0300 Subject: [PATCH 04/13] Use `Err(ctx)` instead of `fatal` and use `getLocation` in error message --- lld/ELF/Relocations.cpp | 19 +++++++++++-------- lld/test/ELF/aarch64-tlsdesc-pauth.s | 8 ++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index daba65d8d4ffe..566f035642353 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1328,10 +1328,11 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, return 1; } - auto fatalBothAuthAndNonAuth = [&sym]() { - fatal("both AUTH and non-AUTH TLSDESC entries for '" + sym.getName() + - "' requested, but only one type of TLSDESC entry per symbol is " - "supported"); + auto errBothAuthAndNonAuth = [this, &sym, offset]() { + Err(ctx) << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() + << "' requested, but only one type of TLSDESC entry per symbol is " + "supported" + << getLocation(ctx, *sec, sym, offset); }; // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). @@ -1340,10 +1341,12 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, if (oneof( expr)) { assert(ctx.arg.emachine == EM_AARCH64); - if (!sym.hasFlag(NEEDS_TLSDESC)) + if (!sym.hasFlag(NEEDS_TLSDESC)) { sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); - else if (!sym.hasFlag(NEEDS_TLSDESC_AUTH)) - fatalBothAuthAndNonAuth(); + } else if (!sym.hasFlag(NEEDS_TLSDESC_AUTH)) { + errBothAuthAndNonAuth(); + return 1; + } sec->addReloc({expr, type, offset, addend, &sym}); return 1; } @@ -1354,7 +1357,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, // with signed TLSDESC enabled since it does not give any value, but leave a // check against that just in case someone uses it. if (expr != R_TLSDESC_CALL) - fatalBothAuthAndNonAuth(); + errBothAuthAndNonAuth(); return 1; } diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index 3c920c98cac6d..92e54c02662e4 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -101,7 +101,9 @@ local2: // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o // RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 %s -// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR1-NEXT: >>> defined in err1.o +// ERR1-NEXT: >>> referenced by err1.o:(.text+0x10) .text adrp x0, :tlsdesc_auth:a ldr x16, [x0, :tlsdesc_auth_lo12:a] @@ -119,7 +121,9 @@ local2: // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o // RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 %s -// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR2-NEXT: >>> defined in err2.o +// ERR2-NEXT: >>> referenced by err2.o:(.text+0x10) .text adrp x0, :tlsdesc:a ldr x1, [x0, :tlsdesc_lo12:a] From 19f5abe4ac93339bce0871f5fbb3c92cffc1ba55 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sun, 10 Nov 2024 21:59:47 +0300 Subject: [PATCH 05/13] Check multiple errors emitted --- lld/ELF/Relocations.cpp | 5 +++-- lld/test/ELF/aarch64-tlsdesc-pauth.s | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 566f035642353..b1fb1abab9663 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1341,9 +1341,10 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, if (oneof( expr)) { assert(ctx.arg.emachine == EM_AARCH64); - if (!sym.hasFlag(NEEDS_TLSDESC)) { + uint16_t flags = sym.flags.load(std::memory_order_relaxed); + if (!(flags & NEEDS_TLSDESC)) { sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); - } else if (!sym.hasFlag(NEEDS_TLSDESC_AUTH)) { + } else if (!(flags & NEEDS_TLSDESC_AUTH)) { errBothAuthAndNonAuth(); return 1; } diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index 92e54c02662e4..106a1b729fbf6 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -104,6 +104,12 @@ local2: // ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported // ERR1-NEXT: >>> defined in err1.o // ERR1-NEXT: >>> referenced by err1.o:(.text+0x10) +// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR1-NEXT: >>> defined in err1.o +// ERR1-NEXT: >>> referenced by err1.o:(.text+0x14) +// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR1-NEXT: >>> defined in err1.o +// ERR1-NEXT: >>> referenced by err1.o:(.text+0x18) .text adrp x0, :tlsdesc_auth:a ldr x16, [x0, :tlsdesc_auth_lo12:a] @@ -124,6 +130,12 @@ local2: // ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported // ERR2-NEXT: >>> defined in err2.o // ERR2-NEXT: >>> referenced by err2.o:(.text+0x10) +// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR2-NEXT: >>> defined in err2.o +// ERR2-NEXT: >>> referenced by err2.o:(.text+0x14) +// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported +// ERR2-NEXT: >>> defined in err2.o +// ERR2-NEXT: >>> referenced by err2.o:(.text+0x18) .text adrp x0, :tlsdesc:a ldr x1, [x0, :tlsdesc_lo12:a] From 5d699b601dc1e88cbdc3b384c5618848a488afd7 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 2 Dec 2024 01:16:23 +0300 Subject: [PATCH 06/13] Use `Err(ctx)` instead of `fatal` and use `getLocation` in error message --- lld/ELF/Relocations.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index b1fb1abab9663..4c6647ab5d86b 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1329,10 +1329,11 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, } auto errBothAuthAndNonAuth = [this, &sym, offset]() { - Err(ctx) << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() - << "' requested, but only one type of TLSDESC entry per symbol is " - "supported" - << getLocation(ctx, *sec, sym, offset); + auto diag = Err(ctx); + diag << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() + << "' requested, but only one type of TLSDESC entry per symbol is " + "supported"; + printLocation(diag, *sec, sym, offset); }; // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). From 6444d7d308319485238c291012c09bec639fc187 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 2 Dec 2024 01:23:21 +0300 Subject: [PATCH 07/13] Address review comments --- lld/test/ELF/aarch64-tlsdesc-pauth.s | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index 106a1b729fbf6..71106267f1282 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -2,14 +2,12 @@ // RUN: rm -rf %t && split-file %s %t && cd %t //--- a.s - .section .tbss,"awT",@nobits .global a a: .xword 0 //--- ok.s - // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth ok.s -o ok.o // RUN: ld.lld -shared ok.o -o ok.so // RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn ok.so | \ @@ -98,9 +96,8 @@ local2: /// 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA //--- err1.s - // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o -// RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 %s +// RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 --implicit-check-not=error: %s // ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported // ERR1-NEXT: >>> defined in err1.o // ERR1-NEXT: >>> referenced by err1.o:(.text+0x10) @@ -124,9 +121,8 @@ local2: blr x1 //--- err2.s - // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o -// RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 %s +// RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 --implicit-check-not=error: %s // ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported // ERR2-NEXT: >>> defined in err2.o // ERR2-NEXT: >>> referenced by err2.o:(.text+0x10) From ebde5e5f92267159cce97a9feabc2940547392eb Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Wed, 4 Dec 2024 14:55:52 +0300 Subject: [PATCH 08/13] Fix build after #118424 --- lld/ELF/InputSection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index e92003c856682..42ef530b79d89 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -966,7 +966,7 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r, case R_SIZE: return r.sym->getSize() + a; case R_TLSDESC: - case RelExpr::R_AARCH64_AUTH_TLSDESC: + case RE_AARCH64_AUTH_TLSDESC: return ctx.in.got->getTlsDescAddr(*r.sym) + a; case R_TLSDESC_PC: return ctx.in.got->getTlsDescAddr(*r.sym) + a - p; From 9ba61ee30d0686d556858d17c0f7fa14196e2060 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Wed, 4 Dec 2024 15:02:07 +0300 Subject: [PATCH 09/13] Fix formatting --- lld/ELF/Relocations.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 4c6647ab5d86b..99403694fb683 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1339,8 +1339,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). - if (oneof( - expr)) { + if (oneof(expr)) { assert(ctx.arg.emachine == EM_AARCH64); uint16_t flags = sym.flags.load(std::memory_order_relaxed); if (!(flags & NEEDS_TLSDESC)) { From 57176f4238ef232d35b20dbbe3f8cda33759d639 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sun, 8 Dec 2024 16:35:07 +0300 Subject: [PATCH 10/13] Address review comments --- lld/ELF/Relocations.cpp | 41 ++++++++++------------------ lld/ELF/Symbols.h | 1 + lld/ELF/SyntheticSections.cpp | 10 +++---- lld/ELF/SyntheticSections.h | 1 + lld/test/ELF/aarch64-tlsdesc-pauth.s | 36 ++++++++---------------- 5 files changed, 33 insertions(+), 56 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 99403694fb683..1f7a66ee18699 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1328,39 +1328,19 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, return 1; } - auto errBothAuthAndNonAuth = [this, &sym, offset]() { - auto diag = Err(ctx); - diag << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() - << "' requested, but only one type of TLSDESC entry per symbol is " - "supported"; - printLocation(diag, *sec, sym, offset); - }; - // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). if (oneof(expr)) { - assert(ctx.arg.emachine == EM_AARCH64); - uint16_t flags = sym.flags.load(std::memory_order_relaxed); - if (!(flags & NEEDS_TLSDESC)) { - sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); - } else if (!(flags & NEEDS_TLSDESC_AUTH)) { - errBothAuthAndNonAuth(); - return 1; - } + sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); sec->addReloc({expr, type, offset, addend, &sym}); return 1; } - if (sym.hasFlag(NEEDS_TLSDESC_AUTH)) { - assert(ctx.arg.emachine == EM_AARCH64); - // TLSDESC_CALL hint relocation probably should not be emitted by compiler - // with signed TLSDESC enabled since it does not give any value, but leave a - // check against that just in case someone uses it. - if (expr != R_TLSDESC_CALL) - errBothAuthAndNonAuth(); - return 1; - } + // TLSDESC_CALL hint relocation should not be emitted by compiler with signed + // TLSDESC enabled. + if (expr == R_TLSDESC_CALL) + sym.setFlags(NEEDS_TLSDESC_NONAUTH); bool isRISCV = ctx.arg.emachine == EM_RISCV; @@ -1371,7 +1351,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, // set NEEDS_TLSDESC on the label. if (expr != R_TLSDESC_CALL) { if (!isRISCV || type == R_RISCV_TLSDESC_HI20) - sym.setFlags(NEEDS_TLSDESC); + sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_NONAUTH); sec->addReloc({expr, type, offset, addend, &sym}); } return 1; @@ -1881,10 +1861,17 @@ void elf::postScanRelocations(Ctx &ctx) { GotSection *got = ctx.in.got.get(); if (flags & NEEDS_TLSDESC) { + if ((flags & NEEDS_TLSDESC_AUTH) && (flags & NEEDS_TLSDESC_NONAUTH)) { + auto diag = Err(ctx); + diag << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() + << "' requested, but only one type of TLSDESC entry per symbol is " + "supported"; + return; + } got->addTlsDescEntry(sym); RelType tlsDescRel = ctx.target->tlsDescRel; if (flags & NEEDS_TLSDESC_AUTH) { - assert(ctx.arg.emachine == EM_AARCH64); + got->addTlsDescAuthEntry(); tlsDescRel = ELF::R_AARCH64_AUTH_TLSDESC; } ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index 7f409dd38fcb8..48df6f60db864 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -54,6 +54,7 @@ enum { NEEDS_GOT_AUTH = 1 << 9, NEEDS_GOT_NONAUTH = 1 << 10, NEEDS_TLSDESC_AUTH = 1 << 11, + NEEDS_TLSDESC_NONAUTH = 1 << 12, }; // The base class for real symbol classes. diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index da22d93f24b50..eb07d82fc9601 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -678,14 +678,14 @@ bool GotSection::addTlsDescEntry(const Symbol &sym) { assert(sym.auxIdx == ctx.symAux.size() - 1); ctx.symAux.back().tlsDescIdx = numEntries; numEntries += 2; - if (sym.hasFlag(NEEDS_TLSDESC_AUTH)) { - assert(ctx.arg.emachine == EM_AARCH64); - authEntries.push_back({(numEntries - 2) * ctx.arg.wordsize, true}); - authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, false}); - } return true; } +void GotSection::addTlsDescAuthEntry() { + authEntries.push_back({(numEntries - 2) * ctx.arg.wordsize, true}); + authEntries.push_back({(numEntries - 1) * ctx.arg.wordsize, false}); +} + bool GotSection::addDynTlsEntry(const Symbol &sym) { assert(sym.auxIdx == ctx.symAux.size() - 1); ctx.symAux.back().tlsGdIdx = numEntries; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 9fcee3b481af0..c977562f0b174 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -114,6 +114,7 @@ class GotSection final : public SyntheticSection { void addEntry(const Symbol &sym); void addAuthEntry(const Symbol &sym); bool addTlsDescEntry(const Symbol &sym); + void addTlsDescAuthEntry(); bool addDynTlsEntry(const Symbol &sym); bool addTlsIndex(); uint32_t getTlsDescOffset(const Symbol &sym) const; diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index 71106267f1282..dca57241d7f18 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -27,7 +27,6 @@ a: adrp x0, :tlsdesc_auth:a ldr x16, [x0, :tlsdesc_auth_lo12:a] add x0, x0, :tlsdesc_auth_lo12:a - .tlsdesccall a blraa x16, x0 // CHECK: adrp x0, 0x[[P]]000 @@ -42,7 +41,6 @@ a: adrp x0, :tlsdesc_auth:local1 ldr x16, [x0, :tlsdesc_auth_lo12:local1] add x0, x0, :tlsdesc_auth_lo12:local1 - .tlsdesccall local1 blraa x16, x0 // CHECK: adrp x0, 0x[[P]]000 @@ -53,7 +51,6 @@ a: adrp x0, :tlsdesc_auth:local2 ldr x16, [x0, :tlsdesc_auth_lo12:local2] add x0, x0, :tlsdesc_auth_lo12:local2 - .tlsdesccall local2 blraa x16, x0 // CHECK: adrp x0, 0x[[P]]000 @@ -98,47 +95,38 @@ local2: //--- err1.s // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o // RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 --implicit-check-not=error: %s -// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR1-NEXT: >>> defined in err1.o -// ERR1-NEXT: >>> referenced by err1.o:(.text+0x10) -// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR1-NEXT: >>> defined in err1.o -// ERR1-NEXT: >>> referenced by err1.o:(.text+0x14) -// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR1-NEXT: >>> defined in err1.o -// ERR1-NEXT: >>> referenced by err1.o:(.text+0x18) +// ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported .text adrp x0, :tlsdesc_auth:a ldr x16, [x0, :tlsdesc_auth_lo12:a] add x0, x0, :tlsdesc_auth_lo12:a - .tlsdesccall a blraa x16, x0 adrp x0, :tlsdesc:a ldr x1, [x0, :tlsdesc_lo12:a] add x0, x0, :tlsdesc_lo12:a - .tlsdesccall a blr x1 //--- err2.s // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o // RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 --implicit-check-not=error: %s -// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR2-NEXT: >>> defined in err2.o -// ERR2-NEXT: >>> referenced by err2.o:(.text+0x10) -// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR2-NEXT: >>> defined in err2.o -// ERR2-NEXT: >>> referenced by err2.o:(.text+0x14) -// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported -// ERR2-NEXT: >>> defined in err2.o -// ERR2-NEXT: >>> referenced by err2.o:(.text+0x18) +// ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported .text adrp x0, :tlsdesc:a ldr x1, [x0, :tlsdesc_lo12:a] add x0, x0, :tlsdesc_lo12:a - .tlsdesccall a blr x1 + adrp x0, :tlsdesc_auth:a + ldr x16, [x0, :tlsdesc_auth_lo12:a] + add x0, x0, :tlsdesc_auth_lo12:a + blraa x16, x0 + +//--- err3.s +// RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err3.s -o err3.o +// RUN: not ld.lld -shared err3.o -o err3.so 2>&1 | FileCheck --check-prefix=ERR3 --implicit-check-not=error: %s +// ERR3: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported + .text adrp x0, :tlsdesc_auth:a ldr x16, [x0, :tlsdesc_auth_lo12:a] add x0, x0, :tlsdesc_auth_lo12:a From 6fe78fd864c17c908825b07ae1b24a5ba6b68d66 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 16 Dec 2024 21:06:04 +0300 Subject: [PATCH 11/13] Do not specify output for error test cases --- lld/test/ELF/aarch64-tlsdesc-pauth.s | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lld/test/ELF/aarch64-tlsdesc-pauth.s b/lld/test/ELF/aarch64-tlsdesc-pauth.s index dca57241d7f18..bf0ae4a87f322 100644 --- a/lld/test/ELF/aarch64-tlsdesc-pauth.s +++ b/lld/test/ELF/aarch64-tlsdesc-pauth.s @@ -94,7 +94,7 @@ local2: //--- err1.s // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err1.s -o err1.o -// RUN: not ld.lld -shared err1.o -o err1.so 2>&1 | FileCheck --check-prefix=ERR1 --implicit-check-not=error: %s +// RUN: not ld.lld -shared err1.o 2>&1 | FileCheck --check-prefix=ERR1 --implicit-check-not=error: %s // ERR1: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported .text adrp x0, :tlsdesc_auth:a @@ -109,7 +109,7 @@ local2: //--- err2.s // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err2.s -o err2.o -// RUN: not ld.lld -shared err2.o -o err2.so 2>&1 | FileCheck --check-prefix=ERR2 --implicit-check-not=error: %s +// RUN: not ld.lld -shared err2.o 2>&1 | FileCheck --check-prefix=ERR2 --implicit-check-not=error: %s // ERR2: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported .text adrp x0, :tlsdesc:a @@ -124,7 +124,7 @@ local2: //--- err3.s // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-linux -mattr=+pauth err3.s -o err3.o -// RUN: not ld.lld -shared err3.o -o err3.so 2>&1 | FileCheck --check-prefix=ERR3 --implicit-check-not=error: %s +// RUN: not ld.lld -shared err3.o 2>&1 | FileCheck --check-prefix=ERR3 --implicit-check-not=error: %s // ERR3: error: both AUTH and non-AUTH TLSDESC entries for 'a' requested, but only one type of TLSDESC entry per symbol is supported .text adrp x0, :tlsdesc_auth:a From ffbc37fed7da7b2fdf5634ace6779dccd2ffe994 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 13 Jan 2025 08:33:46 +0300 Subject: [PATCH 12/13] Address review comments --- lld/ELF/Relocations.cpp | 52 ++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 1f7a66ee18699..302033649eb5b 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1292,6 +1292,27 @@ static unsigned handleMipsTlsRelocation(Ctx &ctx, RelType type, Symbol &sym, return 0; } +static unsigned handleAArch64PAuthTlsRelocation(InputSectionBase *sec, + RelExpr expr, RelType type, + uint64_t offset, Symbol &sym, + int64_t addend) { + // Do not optimize signed TLSDESC to LE/IE (as described in pauthabielf64). + // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions + // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). + if (oneof(expr)) { + sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); + sec->addReloc({expr, type, offset, addend, &sym}); + return 1; + } + + // TLSDESC_CALL hint relocation should not be emitted by compiler with signed + // TLSDESC enabled. + if (expr == R_TLSDESC_CALL) + sym.setFlags(NEEDS_TLSDESC_NONAUTH); + + return 0; +} + // Notes about General Dynamic and Local Dynamic TLS models below. They may // require the generation of a pair of GOT entries that have associated dynamic // relocations. The pair of GOT entries created are of the form GOT[e0] Module @@ -1302,6 +1323,11 @@ static unsigned handleMipsTlsRelocation(Ctx &ctx, RelType type, Symbol &sym, unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) { + if (ctx.arg.emachine == EM_AARCH64) + if (unsigned processed = handleAArch64PAuthTlsRelocation( + sec, expr, type, offset, sym, addend)) + return processed; + if (expr == R_TPREL || expr == R_TPREL_NEG) { if (ctx.arg.shared) { auto diag = Err(ctx); @@ -1328,20 +1354,6 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, return 1; } - // Do not optimize signed TLSDESC (as described in pauthabielf64 to LE/IE). - // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions - // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC). - if (oneof(expr)) { - sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH); - sec->addReloc({expr, type, offset, addend, &sym}); - return 1; - } - - // TLSDESC_CALL hint relocation should not be emitted by compiler with signed - // TLSDESC enabled. - if (expr == R_TLSDESC_CALL) - sym.setFlags(NEEDS_TLSDESC_NONAUTH); - bool isRISCV = ctx.arg.emachine == EM_RISCV; if (oneofaddReloc({expr, type, offset, addend, &sym}); } return 1; @@ -1862,10 +1876,10 @@ void elf::postScanRelocations(Ctx &ctx) { if (flags & NEEDS_TLSDESC) { if ((flags & NEEDS_TLSDESC_AUTH) && (flags & NEEDS_TLSDESC_NONAUTH)) { - auto diag = Err(ctx); - diag << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() - << "' requested, but only one type of TLSDESC entry per symbol is " - "supported"; + Err(ctx) + << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName() + << "' requested, but only one type of TLSDESC entry per symbol is " + "supported"; return; } got->addTlsDescEntry(sym); From 5f2ee82947af1039b5c4cd4347cfefea7c3ba967 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Wed, 22 Jan 2025 10:19:42 +0300 Subject: [PATCH 13/13] Address review comments --- lld/ELF/Relocations.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 302033649eb5b..76b151b93d517 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1323,7 +1323,9 @@ static unsigned handleAArch64PAuthTlsRelocation(InputSectionBase *sec, unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, int64_t addend) { - if (ctx.arg.emachine == EM_AARCH64) + bool isAArch64 = ctx.arg.emachine == EM_AARCH64; + + if (isAArch64) if (unsigned processed = handleAArch64PAuthTlsRelocation( sec, expr, type, offset, sym, addend)) return processed; @@ -1362,10 +1364,10 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type, // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} reference a label. Do not // set NEEDS_TLSDESC on the label. if (expr != R_TLSDESC_CALL) { - if (!isRISCV || type == R_RISCV_TLSDESC_HI20) - sym.setFlags( - NEEDS_TLSDESC | - (ctx.arg.emachine == EM_AARCH64 ? NEEDS_TLSDESC_NONAUTH : 0)); + if (isAArch64) + sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_NONAUTH); + else if (!isRISCV || type == R_RISCV_TLSDESC_HI20) + sym.setFlags(NEEDS_TLSDESC); sec->addReloc({expr, type, offset, addend, &sym}); } return 1;