From e82f5f10f07f1729d6ae965f7c220546aea3341b Mon Sep 17 00:00:00 2001 From: Jessica Clarke Date: Sat, 26 Jul 2025 21:33:01 +0100 Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?= =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5 [skip ci] --- lld/ELF/Config.h | 2 ++ lld/ELF/Driver.cpp | 1 + lld/ELF/Relocations.cpp | 13 ++++++------- lld/ELF/SyntheticSections.cpp | 17 +++++++---------- lld/ELF/SyntheticSections.h | 16 +++++++--------- lld/ELF/Target.cpp | 3 +-- lld/ELF/Writer.cpp | 3 +-- 7 files changed, 25 insertions(+), 30 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index d9639b06ca4bf..958e5caaf0dfa 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -701,6 +701,8 @@ struct Ctx : CommonLinkerContext { std::unique_ptr tar; // InputFile for linker created symbols with no source location. InputFile *internalFile = nullptr; + // Dummy Undefined for relocations without a symbol. + Undefined *dummySym = nullptr; // True if symbols can be exported (isExported) or preemptible. bool hasDynsym = false; // True if SHT_LLVM_SYMPART is used. diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 21d228eda6470..4dcf577ebcb16 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -3138,6 +3138,7 @@ template void LinkerDriver::link(opt::InputArgList &args) { ctx.symtab->insert(arg->getValue())->traced = true; ctx.internalFile = createInternalFile(ctx, ""); + ctx.dummySym = make(ctx.internalFile, "", STB_LOCAL, 0, 0); // Handle -u/--undefined before input files. If both a.a and b.so define foo, // -u foo a.a b.so will extract a.a. diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index bd22fe2f1aa25..ed53228359f86 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -887,7 +887,7 @@ static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt, gotPlt.addEntry(sym); rel.addReloc({type, &gotPlt, sym.getGotPltOffset(ctx), sym.isPreemptible ? DynamicReloc::AgainstSymbol - : DynamicReloc::AddendOnlyWithTargetVA, + : DynamicReloc::AddendOnly, sym, 0, R_ABS}); } @@ -927,8 +927,8 @@ static void addGotAuthEntry(Ctx &ctx, Symbol &sym) { // Signed GOT requires dynamic relocation. ctx.in.got->getPartition(ctx).relaDyn->addReloc( - {R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, - DynamicReloc::AddendOnlyWithTargetVA, sym, 0, R_ABS}); + {R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, DynamicReloc::AddendOnly, + sym, 0, R_ABS}); } static void addTpOffsetGotEntry(Ctx &ctx, Symbol &sym) { @@ -1160,8 +1160,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset, part.relrAuthDyn->relocs.push_back({sec, sec->relocs().size() - 1}); } else { part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset, - DynamicReloc::AddendOnlyWithTargetVA, sym, - addend, R_ABS}); + DynamicReloc::AddendOnly, sym, addend, + R_ABS}); } return; } @@ -1948,13 +1948,12 @@ void elf::postScanRelocations(Ctx &ctx) { GotSection *got = ctx.in.got.get(); if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) { - static Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0); if (ctx.arg.shared) ctx.mainPart->relaDyn->addReloc( {ctx.target->tlsModuleIndexRel, got, got->getTlsIndexOff()}); else got->addConstant({R_ADDEND, ctx.target->symbolicRel, - got->getTlsIndexOff(), 1, &dummy}); + got->getTlsIndexOff(), 1, ctx.dummySym}); } assert(ctx.symAux.size() == 1); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index efec41a737b62..690e9bf2d7d8b 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1122,8 +1122,8 @@ void MipsGotSection::build() { for (const std::pair &p : got.local16) { uint64_t offset = p.second * ctx.arg.wordsize; ctx.mainPart->relaDyn->addReloc({ctx.target->relativeRel, this, offset, - DynamicReloc::AddendOnlyWithTargetVA, - *p.first.first, p.first.second, R_ABS}); + DynamicReloc::AddendOnly, *p.first.first, + p.first.second, R_ABS}); } } } @@ -1648,17 +1648,14 @@ uint64_t DynamicReloc::getOffset() const { int64_t DynamicReloc::computeAddend(Ctx &ctx) const { switch (kind) { case AddendOnly: - assert(sym == nullptr); - return addend; - case AgainstSymbol: - assert(sym != nullptr); - return addend; - case AddendOnlyWithTargetVA: case AgainstSymbolWithTargetVA: { uint64_t ca = inputSec->getRelocTargetVA( ctx, Relocation{expr, type, 0, addend, sym}, getOffset()); return ctx.arg.is64 ? ca : SignExtend64<32>(ca); } + case AgainstSymbol: + assert(sym != nullptr); + return addend; case MipsMultiGotPage: assert(sym == nullptr); return getMipsPageAddr(outputSec->addr) + addend; @@ -1703,8 +1700,8 @@ void RelocationBaseSection::addAddendOnlyRelocIfNonPreemptible( addReloc({dynType, &isec, offsetInSec, DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); else - addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec, offsetInSec, - sym, 0, R_ABS, addendRelType); + addReloc(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym, 0, + R_ABS, addendRelType); } void RelocationBaseSection::mergeRels() { diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 5f01513630597..b72436372fbc1 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -419,13 +419,10 @@ class StringTableSection final : public SyntheticSection { class DynamicReloc { public: enum Kind { - /// The resulting dynamic relocation does not reference a symbol (#sym must - /// be nullptr) and uses #addend as the result of computeAddend(ctx). - AddendOnly, /// The resulting dynamic relocation will not reference a symbol: #sym is /// only used to compute the addend with InputSection::getRelocTargetVA(). /// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64). - AddendOnlyWithTargetVA, + AddendOnly, /// The resulting dynamic relocation references symbol #sym from the dynamic /// symbol table and uses #addend as the value of computeAddend(ctx). AgainstSymbol, @@ -438,7 +435,7 @@ class DynamicReloc { /// addresses of 64kb pages that lie inside the output section. MipsMultiGotPage, }; - /// This constructor records a relocation against a symbol. + /// This constructor records a normal relocation. DynamicReloc(RelType type, const InputSectionBase *inputSec, uint64_t offsetInSec, Kind kind, Symbol &sym, int64_t addend, RelExpr expr) @@ -447,8 +444,9 @@ class DynamicReloc { /// This constructor records a relative relocation with no symbol. DynamicReloc(RelType type, const InputSectionBase *inputSec, uint64_t offsetInSec, int64_t addend = 0) - : sym(nullptr), inputSec(inputSec), offsetInSec(offsetInSec), type(type), - addend(addend), kind(AddendOnly), expr(R_ADDEND) {} + : sym(inputSec->getCtx().dummySym), inputSec(inputSec), + offsetInSec(offsetInSec), type(type), addend(addend), kind(AddendOnly), + expr(R_ADDEND) {} /// This constructor records dynamic relocation settings used by the MIPS /// multi-GOT implementation. DynamicReloc(RelType type, const InputSectionBase *inputSec, @@ -528,8 +526,8 @@ class RelocationBaseSection : public SyntheticSection { uint64_t offsetInSec, Symbol &sym, int64_t addend, RelType addendRelType, RelExpr expr) { assert(expr != R_ADDEND && "expected non-addend relocation expression"); - addReloc(DynamicReloc::AddendOnlyWithTargetVA, dynType, isec, - offsetInSec, sym, addend, expr, addendRelType); + addReloc(DynamicReloc::AddendOnly, dynType, isec, offsetInSec, sym, + addend, expr, addendRelType); } /// Add a dynamic relocation using the target address of \p sym as the addend /// if \p sym is non-preemptible. Otherwise add a relocation against \p sym. diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index ad7d57d30668d..4946484074d05 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -105,10 +105,9 @@ ErrorPlace elf::getErrorPlace(Ctx &ctx, const uint8_t *loc) { if (isecLoc <= loc && loc < isecLoc + isec->getSize()) { std::string objLoc = isec->getLocation(loc - isecLoc); // Return object file location and source file location. - Undefined dummy(ctx.internalFile, "", STB_LOCAL, 0, 0); ELFSyncStream msg(ctx, DiagLevel::None); if (isec->file) - msg << isec->getSrcMsg(dummy, loc - isecLoc); + msg << isec->getSrcMsg(*ctx.dummySym, loc - isecLoc); return {isec, objLoc + ": ", std::string(msg.str())}; } } diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 15909daf51ab6..3e66ce074fcdf 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1573,8 +1573,7 @@ template void Writer::finalizeAddressDependentContent() { if (isInt<32>(reloc.sym->getVA(ctx, reloc.addend))) return false; part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec, - reloc.offset, - DynamicReloc::AddendOnlyWithTargetVA, + reloc.offset, DynamicReloc::AddendOnly, *reloc.sym, reloc.addend, R_ABS}); return true; }); From 8511366776c51ca01b1bf92bfe9445ff50acd14f Mon Sep 17 00:00:00 2001 From: Jessica Clarke Date: Sat, 26 Jul 2025 22:06:01 +0100 Subject: [PATCH 2/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?= =?UTF-8?q?anges=20introduced=20through=20rebase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5 [skip ci] --- lld/ELF/SyntheticSections.cpp | 4 +++- lld/ELF/SyntheticSections.h | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 690e9bf2d7d8b..da0cf1298d1ab 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1647,6 +1647,8 @@ uint64_t DynamicReloc::getOffset() const { int64_t DynamicReloc::computeAddend(Ctx &ctx) const { switch (kind) { + case Computed: + llvm_unreachable("addend already computed"); case AddendOnly: case AgainstSymbolWithTargetVA: { uint64_t ca = inputSec->getRelocTargetVA( @@ -1745,7 +1747,7 @@ void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) { r_offset = getOffset(); r_sym = getSymIndex(symt); addend = computeAddend(ctx); - kind = AddendOnly; // Catch errors + kind = Computed; // Catch errors } void RelocationBaseSection::computeRels() { diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index b72436372fbc1..8185417755d25 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -419,6 +419,9 @@ class StringTableSection final : public SyntheticSection { class DynamicReloc { public: enum Kind { + /// The resulting dynamic relocation has already had its addend computed. + /// Calling computeAddend() is an error. Only for internal use. + Computed, /// The resulting dynamic relocation will not reference a symbol: #sym is /// only used to compute the addend with InputSection::getRelocTargetVA(). /// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64). @@ -459,6 +462,7 @@ class DynamicReloc { uint64_t getOffset() const; uint32_t getSymIndex(SymbolTableBaseSection *symTab) const; bool needsDynSymIndex() const { + assert(kind != Computed && "cannot check kind after computeRaw"); return kind == AgainstSymbol || kind == AgainstSymbolWithTargetVA; }