Skip to content

Commit ca1f0d4

Browse files
authored
[lld][AArch64][ELF][PAC] Support .relr.auth.dyn section (#87635)
Support `R_AARCH64_AUTH_RELATIVE` relocation compression as described in https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#relocation-compression
1 parent 89ee3ae commit ca1f0d4

File tree

6 files changed

+216
-47
lines changed

6 files changed

+216
-47
lines changed

lld/ELF/Arch/AArch64.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,19 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
428428
case R_AARCH64_PREL64:
429429
write64(loc, val);
430430
break;
431+
case R_AARCH64_AUTH_ABS64:
432+
// If val is wider than 32 bits, the relocation must have been moved from
433+
// .relr.auth.dyn to .rela.dyn, and the addend write is not needed.
434+
//
435+
// If val fits in 32 bits, we have two potential scenarios:
436+
// * True RELR: Write the 32-bit `val`.
437+
// * RELA: Even if the value now fits in 32 bits, it might have been
438+
// converted from RELR during an iteration in
439+
// finalizeAddressDependentContent(). Writing the value is harmless
440+
// because dynamic linking ignores it.
441+
if (isInt<32>(val))
442+
write32(loc, val);
443+
break;
431444
case R_AARCH64_ADD_ABS_LO12_NC:
432445
or32AArch64Imm(loc, val);
433446
break;

lld/ELF/Relocations.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -898,9 +898,9 @@ static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
898898
isec.addReloc({expr, type, offsetInSec, addend, &sym});
899899
if (shard)
900900
part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back(
901-
{&isec, offsetInSec});
901+
{&isec, isec.relocs().size() - 1});
902902
else
903-
part.relrDyn->relocs.push_back({&isec, offsetInSec});
903+
part.relrDyn->relocs.push_back({&isec, isec.relocs().size() - 1});
904904
return;
905905
}
906906
part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec,
@@ -1154,6 +1154,12 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11541154
// relative relocation. Use a symbolic relocation instead.
11551155
if (sym.isPreemptible) {
11561156
part.relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
1157+
} else if (part.relrAuthDyn && sec->addralign >= 2 && offset % 2 == 0) {
1158+
// When symbol values are determined in
1159+
// finalizeAddressDependentContent, some .relr.auth.dyn relocations
1160+
// may be moved to .rela.dyn.
1161+
sec->addReloc({expr, type, offset, addend, &sym});
1162+
part.relrAuthDyn->relocs.push_back({sec, sec->relocs().size() - 1});
11571163
} else {
11581164
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset,
11591165
DynamicReloc::AddendOnlyWithTargetVA, sym,

lld/ELF/SyntheticSections.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,12 @@ DynamicSection<ELFT>::computeContents() {
14201420
addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
14211421
sizeof(Elf_Relr));
14221422
}
1423+
if (part.relrAuthDyn && part.relrAuthDyn->getParent() &&
1424+
!part.relrAuthDyn->relocs.empty()) {
1425+
addInSec(DT_AARCH64_AUTH_RELR, *part.relrAuthDyn);
1426+
addInt(DT_AARCH64_AUTH_RELRSZ, part.relrAuthDyn->getParent()->size);
1427+
addInt(DT_AARCH64_AUTH_RELRENT, sizeof(Elf_Relr));
1428+
}
14231429
if (isMain && in.relaPlt->isNeeded()) {
14241430
addInSec(DT_JMPREL, *in.relaPlt);
14251431
entries.emplace_back(DT_PLTRELSZ, addPltRelSz());
@@ -1731,10 +1737,13 @@ template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
17311737
}
17321738
}
17331739

1734-
RelrBaseSection::RelrBaseSection(unsigned concurrency)
1735-
: SyntheticSection(SHF_ALLOC,
1736-
config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR,
1737-
config->wordsize, ".relr.dyn"),
1740+
RelrBaseSection::RelrBaseSection(unsigned concurrency, bool isAArch64Auth)
1741+
: SyntheticSection(
1742+
SHF_ALLOC,
1743+
isAArch64Auth
1744+
? SHT_AARCH64_AUTH_RELR
1745+
: (config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR),
1746+
config->wordsize, isAArch64Auth ? ".relr.auth.dyn" : ".relr.dyn"),
17381747
relocsVec(concurrency) {}
17391748

17401749
void RelrBaseSection::mergeRels() {
@@ -2002,8 +2011,8 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
20022011
}
20032012

20042013
template <class ELFT>
2005-
RelrSection<ELFT>::RelrSection(unsigned concurrency)
2006-
: RelrBaseSection(concurrency) {
2014+
RelrSection<ELFT>::RelrSection(unsigned concurrency, bool isAArch64Auth)
2015+
: RelrBaseSection(concurrency, isAArch64Auth) {
20072016
this->entsize = config->wordsize;
20082017
}
20092018

@@ -4774,6 +4783,9 @@ template <class ELFT> void elf::createSyntheticSections() {
47744783
if (config->relrPackDynRelocs) {
47754784
part.relrDyn = std::make_unique<RelrSection<ELFT>>(threadCount);
47764785
add(*part.relrDyn);
4786+
part.relrAuthDyn = std::make_unique<RelrSection<ELFT>>(
4787+
threadCount, /*isAArch64Auth=*/true);
4788+
add(*part.relrAuthDyn);
47774789
}
47784790

47794791
if (!config->relocatable) {

lld/ELF/SyntheticSections.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,9 @@ class RelocationBaseSection : public SyntheticSection {
548548
static bool classof(const SectionBase *d) {
549549
return SyntheticSection::classof(d) &&
550550
(d->type == llvm::ELF::SHT_RELA || d->type == llvm::ELF::SHT_REL ||
551-
d->type == llvm::ELF::SHT_RELR);
551+
d->type == llvm::ELF::SHT_RELR ||
552+
(d->type == llvm::ELF::SHT_AARCH64_AUTH_RELR &&
553+
config->emachine == llvm::ELF::EM_AARCH64));
552554
}
553555
int32_t dynamicTag, sizeDynamicTag;
554556
SmallVector<DynamicReloc, 0> relocs;
@@ -596,15 +598,17 @@ class AndroidPackedRelocationSection final : public RelocationBaseSection {
596598
};
597599

598600
struct RelativeReloc {
599-
uint64_t getOffset() const { return inputSec->getVA(offsetInSec); }
601+
uint64_t getOffset() const {
602+
return inputSec->getVA(inputSec->relocs()[relocIdx].offset);
603+
}
600604

601605
const InputSectionBase *inputSec;
602-
uint64_t offsetInSec;
606+
size_t relocIdx;
603607
};
604608

605609
class RelrBaseSection : public SyntheticSection {
606610
public:
607-
RelrBaseSection(unsigned concurrency);
611+
RelrBaseSection(unsigned concurrency, bool isAArch64Auth = false);
608612
void mergeRels();
609613
bool isNeeded() const override {
610614
return !relocs.empty() ||
@@ -622,7 +626,7 @@ template <class ELFT> class RelrSection final : public RelrBaseSection {
622626
using Elf_Relr = typename ELFT::Relr;
623627

624628
public:
625-
RelrSection(unsigned concurrency);
629+
RelrSection(unsigned concurrency, bool isAArch64Auth = false);
626630

627631
bool updateAllocSize() override;
628632
size_t getSize() const override { return relrRelocs.size() * this->entsize; }
@@ -1460,6 +1464,7 @@ struct Partition {
14601464
std::unique_ptr<PackageMetadataNote> packageMetadataNote;
14611465
std::unique_ptr<RelocationBaseSection> relaDyn;
14621466
std::unique_ptr<RelrBaseSection> relrDyn;
1467+
std::unique_ptr<RelrBaseSection> relrAuthDyn;
14631468
std::unique_ptr<VersionDefinitionSection> verDef;
14641469
std::unique_ptr<SyntheticSection> verNeed;
14651470
std::unique_ptr<VersionTableSection> verSym;

lld/ELF/Writer.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -1454,9 +1454,32 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
14541454
in.mipsGot->updateAllocSize();
14551455

14561456
for (Partition &part : partitions) {
1457+
// The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
1458+
// encode the signing schema. We've put relocations in .relr.auth.dyn
1459+
// during RelocationScanner::processAux, but the target VA for some of
1460+
// them might be wider than 32 bits. We can only know the final VA at this
1461+
// point, so move relocations with large values from .relr.auth.dyn to
1462+
// .rela.dyn. See also AArch64::relocate.
1463+
if (part.relrAuthDyn) {
1464+
auto it = llvm::remove_if(
1465+
part.relrAuthDyn->relocs, [&part](const RelativeReloc &elem) {
1466+
const Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];
1467+
if (isInt<32>(reloc.sym->getVA(reloc.addend)))
1468+
return false;
1469+
part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,
1470+
reloc.offset,
1471+
DynamicReloc::AddendOnlyWithTargetVA,
1472+
*reloc.sym, reloc.addend, R_ABS});
1473+
return true;
1474+
});
1475+
changed |= (it != part.relrAuthDyn->relocs.end());
1476+
part.relrAuthDyn->relocs.erase(it, part.relrAuthDyn->relocs.end());
1477+
}
14571478
changed |= part.relaDyn->updateAllocSize();
14581479
if (part.relrDyn)
14591480
changed |= part.relrDyn->updateAllocSize();
1481+
if (part.relrAuthDyn)
1482+
changed |= part.relrAuthDyn->updateAllocSize();
14601483
if (part.memtagGlobalDescriptors)
14611484
changed |= part.memtagGlobalDescriptors->updateAllocSize();
14621485
}
@@ -1614,6 +1637,14 @@ static void removeUnusedSyntheticSections() {
16141637
auto *sec = cast<SyntheticSection>(s);
16151638
if (sec->getParent() && sec->isNeeded())
16161639
return false;
1640+
// .relr.auth.dyn relocations may be moved to .rela.dyn in
1641+
// finalizeAddressDependentContent, making .rela.dyn no longer empty.
1642+
// Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but
1643+
// we would fail to remove it here.
1644+
if (config->emachine == EM_AARCH64 && config->relrPackDynRelocs)
1645+
if (auto *relSec = dyn_cast<RelocationBaseSection>(sec))
1646+
if (relSec == mainPart->relaDyn.get())
1647+
return false;
16171648
unused.insert(sec);
16181649
return true;
16191650
});
@@ -1926,6 +1957,10 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
19261957
part.relrDyn->mergeRels();
19271958
finalizeSynthetic(part.relrDyn.get());
19281959
}
1960+
if (part.relrAuthDyn) {
1961+
part.relrAuthDyn->mergeRels();
1962+
finalizeSynthetic(part.relrAuthDyn.get());
1963+
}
19291964

19301965
finalizeSynthetic(part.dynSymTab.get());
19311966
finalizeSynthetic(part.gnuHashTab.get());

0 commit comments

Comments
 (0)