Skip to content

Commit

Permalink
[ELF] Move target to Ctx. NFC
Browse files Browse the repository at this point in the history
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

Follow-up to driver (2022-10) and script (2024-08).
  • Loading branch information
MaskRay authored and cjdb committed Aug 23, 2024
1 parent 8aa8deb commit 442b6ad
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 231 deletions.
9 changes: 5 additions & 4 deletions lld/ELF/AArch64ErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ void Patch843419Section::writeTo(uint8_t *buf) {
write32le(buf, read32le(patchee->content().begin() + patcheeOffset));

// Apply any relocation transferred from the original patchee section.
target->relocateAlloc(*this, buf);
ctx.target->relocateAlloc(*this, buf);

// Return address is the next instruction after the one we have just copied.
uint64_t s = getLDSTAddr() + 4;
uint64_t p = patchSym->getVA() + 4;
target->relocateNoSym(buf + 4, R_AARCH64_JUMP26, s - p);
ctx.target->relocateNoSym(buf + 4, R_AARCH64_JUMP26, s - p);
}

void AArch64Err843419Patcher::init() {
Expand Down Expand Up @@ -483,7 +483,8 @@ void AArch64Err843419Patcher::insertPatches(
InputSectionDescription &isd, std::vector<Patch843419Section *> &patches) {
uint64_t isecLimit;
uint64_t prevIsecLimit = isd.sections.front()->outSecOff;
uint64_t patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();
uint64_t patchUpperBound =
prevIsecLimit + ctx.target->getThunkSectionSpacing();
uint64_t outSecAddr = isd.sections.front()->getParent()->addr;

// Set the outSecOff of patches to the place where we want to insert them.
Expand All @@ -500,7 +501,7 @@ void AArch64Err843419Patcher::insertPatches(
(*patchIt)->outSecOff = prevIsecLimit;
++patchIt;
}
patchUpperBound = prevIsecLimit + target->getThunkSectionSpacing();
patchUpperBound = prevIsecLimit + ctx.target->getThunkSectionSpacing();
}
prevIsecLimit = isecLimit;
}
Expand Down
13 changes: 7 additions & 6 deletions lld/ELF/ARMErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ static uint64_t getThumbDestAddr(uint64_t sourceAddr, uint32_t instr) {
write16le(buf + 2, instr & 0x0000ffff);
int64_t offset;
if (isBcc(instr))
offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP19);
offset = ctx.target->getImplicitAddend(buf, R_ARM_THM_JUMP19);
else if (isB(instr))
offset = target->getImplicitAddend(buf, R_ARM_THM_JUMP24);
offset = ctx.target->getImplicitAddend(buf, R_ARM_THM_JUMP24);
else
offset = target->getImplicitAddend(buf, R_ARM_THM_CALL);
offset = ctx.target->getImplicitAddend(buf, R_ARM_THM_CALL);
// A BLX instruction from Thumb to Arm may have an address that is
// not 4-byte aligned. As Arm instructions are always 4-byte aligned
// the instruction is calculated (from Arm ARM):
Expand All @@ -182,7 +182,7 @@ void Patch657417Section::writeTo(uint8_t *buf) {
write32le(buf, 0x9000f000);
// If we have a relocation then apply it.
if (!relocs().empty()) {
target->relocateAlloc(*this, buf);
ctx.target->relocateAlloc(*this, buf);
return;
}

Expand All @@ -197,7 +197,8 @@ void Patch657417Section::writeTo(uint8_t *buf) {
// state with a PC Bias of 4.
uint64_t pcBias = isBLX(instr) ? 8 : 4;
uint64_t p = getVA(pcBias);
target->relocateNoSym(buf, isARM ? R_ARM_JUMP24 : R_ARM_THM_JUMP24, s - p);
ctx.target->relocateNoSym(buf, isARM ? R_ARM_JUMP24 : R_ARM_THM_JUMP24,
s - p);
}

// Given a branch instruction spanning two 4KiB regions, at offset off from the
Expand Down Expand Up @@ -233,7 +234,7 @@ static bool patchInRange(const InputSection *isec, uint64_t off,
// after isec. As there can be more than one patch in the patch section we
// add 0x100 as contingency to account for worst case of 1 branch every 4KiB
// for a 1 MiB range.
return target->inBranchRange(
return ctx.target->inBranchRange(
isBcc(instr) ? R_ARM_THM_JUMP19 : R_ARM_THM_JUMP24, isec->getVA(off),
isec->getVA() + isec->getSize() + 0x100);
}
Expand Down
14 changes: 8 additions & 6 deletions lld/ELF/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ bool AArch64Relaxer::tryRelaxAdrpAdd(const Relocation &adrpRel,
write32le(buf + adrpRel.offset, 0xd503201f);
// adr x_<dest_reg>
write32le(buf + adrRel.offset, 0x10000000 | adrpDestReg);
target->relocate(buf + adrRel.offset, adrRel, val);
ctx.target->relocate(buf + adrRel.offset, adrRel, val);
return true;
}

Expand Down Expand Up @@ -854,11 +854,13 @@ bool AArch64Relaxer::tryRelaxAdrpLdr(const Relocation &adrpRel,
// add x_<dest reg>, x_<dest reg>
write32le(buf + addRel.offset, 0x91000000 | adrpDestReg | (adrpDestReg << 5));

target->relocate(buf + adrpSymRel.offset, adrpSymRel,
SignExtend64(getAArch64Page(sym.getVA()) -
getAArch64Page(secAddr + adrpSymRel.offset),
64));
target->relocate(buf + addRel.offset, addRel, SignExtend64(sym.getVA(), 64));
ctx.target->relocate(
buf + adrpSymRel.offset, adrpSymRel,
SignExtend64(getAArch64Page(sym.getVA()) -
getAArch64Page(secAddr + adrpSymRel.offset),
64));
ctx.target->relocate(buf + addRel.offset, addRel,
SignExtend64(sym.getVA(), 64));
tryRelaxAdrpAdd(adrpSymRel, addRel, secAddr, buf);
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/Arch/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1386,9 +1386,9 @@ void ArmCmseSGSection::writeTo(uint8_t *buf) {
write16(p + 2, 0xe97f);
write16(p + 4, 0xf000); // B.W S
write16(p + 6, 0xb000);
target->relocateNoSym(p + 4, R_ARM_THM_JUMP24,
s->acleSeSym->getVA() -
(getVA() + s->offset + s->size));
ctx.target->relocateNoSym(p + 4, R_ARM_THM_JUMP24,
s->acleSeSym->getVA() -
(getVA() + s->offset + s->size));
}
}

Expand Down
7 changes: 4 additions & 3 deletions lld/ELF/Arch/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ void LoongArch::writePltHeader(uint8_t *buf) const {
write32le(buf + 0, insn(PCADDU12I, R_T2, hi20(offset), 0));
write32le(buf + 4, insn(sub, R_T1, R_T1, R_T3));
write32le(buf + 8, insn(ld, R_T3, R_T2, lo12(offset)));
write32le(buf + 12, insn(addi, R_T1, R_T1, lo12(-target->pltHeaderSize - 12)));
write32le(buf + 12,
insn(addi, R_T1, R_T1, lo12(-ctx.target->pltHeaderSize - 12)));
write32le(buf + 16, insn(addi, R_T0, R_T2, lo12(offset)));
write32le(buf + 20, insn(srli, R_T1, R_T1, config->is64 ? 1 : 2));
write32le(buf + 24, insn(ld, R_T0, R_T0, config->wordsize));
Expand All @@ -374,8 +375,8 @@ void LoongArch::writePlt(uint8_t *buf, const Symbol &sym,
}

RelType LoongArch::getDynRel(RelType type) const {
return type == target->symbolicRel ? type
: static_cast<RelType>(R_LARCH_NONE);
return type == ctx.target->symbolicRel ? type
: static_cast<RelType>(R_LARCH_NONE);
}

RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/Arch/PPC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ static bool tryRelaxPPC64TocIndirection(const Relocation &rel,
return false;

// Add PPC64TocOffset that will be subtracted by PPC64::relocate().
static_cast<const PPC64 &>(*target).relaxGot(bufLoc, rel,
tocRelative + ppc64TocOffset);
static_cast<const PPC64 &>(*ctx.target)
.relaxGot(bufLoc, rel, tocRelative + ppc64TocOffset);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void RISCV::writePltHeader(uint8_t *buf) const {
write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset)));
write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12));
write32le(buf + 12, itype(ADDI, X_T1, X_T1, -ctx.target->pltHeaderSize - 12));
write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset)));
write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2));
write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize));
Expand All @@ -256,8 +256,8 @@ void RISCV::writePlt(uint8_t *buf, const Symbol &sym,
}

RelType RISCV::getDynRel(RelType type) const {
return type == target->symbolicRel ? type
: static_cast<RelType>(R_RISCV_NONE);
return type == ctx.target->symbolicRel ? type
: static_cast<RelType>(R_RISCV_NONE);
}

RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
Expand Down
2 changes: 2 additions & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Symbol;
class BitcodeCompiler;
class OutputSection;
class LinkerScript;
class TargetInfo;
struct Partition;
struct PhdrEntry;

Expand Down Expand Up @@ -485,6 +486,7 @@ struct DuplicateSymbol {
struct Ctx {
LinkerDriver driver;
LinkerScript *script;
TargetInfo *target;

// These variables are initialized by Writer and should not be used before
// Writer is initialized.
Expand Down
17 changes: 9 additions & 8 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void elf::errorOrWarn(const Twine &msg) {
void Ctx::reset() {
driver = LinkerDriver();
script = nullptr;
target = nullptr;

bufferStart = nullptr;
mainPart = nullptr;
Expand Down Expand Up @@ -2065,13 +2066,13 @@ void LinkerDriver::inferMachineType() {
// each target.
static uint64_t getMaxPageSize(opt::InputArgList &args) {
uint64_t val = args::getZOptionValue(args, OPT_z, "max-page-size",
target->defaultMaxPageSize);
ctx.target->defaultMaxPageSize);
if (!isPowerOf2_64(val)) {
error("max-page-size: value isn't a power of 2");
return target->defaultMaxPageSize;
return ctx.target->defaultMaxPageSize;
}
if (config->nmagic || config->omagic) {
if (val != target->defaultMaxPageSize)
if (val != ctx.target->defaultMaxPageSize)
warn("-z max-page-size set, but paging disabled by omagic or nmagic");
return 1;
}
Expand All @@ -2082,13 +2083,13 @@ static uint64_t getMaxPageSize(opt::InputArgList &args) {
// each target.
static uint64_t getCommonPageSize(opt::InputArgList &args) {
uint64_t val = args::getZOptionValue(args, OPT_z, "common-page-size",
target->defaultCommonPageSize);
ctx.target->defaultCommonPageSize);
if (!isPowerOf2_64(val)) {
error("common-page-size: value isn't a power of 2");
return target->defaultCommonPageSize;
return ctx.target->defaultCommonPageSize;
}
if (config->nmagic || config->omagic) {
if (val != target->defaultCommonPageSize)
if (val != ctx.target->defaultCommonPageSize)
warn("-z common-page-size set, but paging disabled by omagic or nmagic");
return 1;
}
Expand Down Expand Up @@ -3106,9 +3107,9 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
// The Target instance handles target-specific stuff, such as applying
// relocations or writing a PLT section. It also contains target-dependent
// values such as a default image base address.
target = getTarget();
ctx.target = getTarget();

config->eflags = target->calcEFlags();
config->eflags = ctx.target->calcEFlags();
// maxPageSize (sometimes called abi page size) is the maximum page size that
// the output can be run on. For example if the OS can use 4k or 64k page
// sizes then maxPageSize must be 64k for the output to be useable on both.
Expand Down
12 changes: 6 additions & 6 deletions lld/ELF/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void InputSection::copyRelocations(uint8_t *buf) {
template <class ELFT, class RelTy, class RelIt>
void InputSection::copyRelocations(uint8_t *buf,
llvm::iterator_range<RelIt> rels) {
const TargetInfo &target = *elf::target;
const TargetInfo &target = *elf::ctx.target;
InputSectionBase *sec = getRelocatedSection();
(void)sec->contentMaybeDecompress(); // uncompress if needed

Expand Down Expand Up @@ -950,7 +950,7 @@ uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
template <class ELFT, class RelTy>
void InputSection::relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels) {
const unsigned bits = sizeof(typename ELFT::uint) * 8;
const TargetInfo &target = *elf::target;
const TargetInfo &target = *elf::ctx.target;
const auto emachine = config->emachine;
const bool isDebug = isDebugSection(*this);
const bool isDebugLine = isDebug && name == ".debug_line";
Expand Down Expand Up @@ -1103,7 +1103,7 @@ void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) {
adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd);

if (flags & SHF_ALLOC) {
target->relocateAlloc(*this, buf);
ctx.target->relocateAlloc(*this, buf);
return;
}

Expand Down Expand Up @@ -1198,8 +1198,8 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,

if (Defined *f = getEnclosingFunction(rel.offset)) {
prologues.insert(f);
if (target->adjustPrologueForCrossSplitStack(buf + f->value, end,
f->stOther))
if (ctx.target->adjustPrologueForCrossSplitStack(buf + f->value, end,
f->stOther))
continue;
if (!getFile<ELFT>()->someNoSplitStack)
error(lld::toString(this) + ": " + f->getName() +
Expand All @@ -1208,7 +1208,7 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
}
}

if (target->needsMoreStackNonSplit)
if (ctx.target->needsMoreStackNonSplit)
switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls);
}

Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ LinkerScript::assignAddresses() {
dot = config->imageBase.value_or(0);
} else {
// Assign addresses to headers right now.
dot = target->getImageBase();
dot = ctx.target->getImageBase();
ctx.out.elfHeader->addr = dot;
ctx.out.programHeaders->addr = dot + ctx.out.elfHeader->size;
dot += getHeaderSize();
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ template <class ELFT> class MarkLive {
template <class ELFT>
static uint64_t getAddend(InputSectionBase &sec,
const typename ELFT::Rel &rel) {
return target->getImplicitAddend(sec.content().begin() + rel.r_offset,
rel.getType(config->isMips64EL));
return ctx.target->getImplicitAddend(sec.content().begin() + rel.r_offset,
rel.getType(config->isMips64EL));
}

template <class ELFT>
Expand Down
8 changes: 4 additions & 4 deletions lld/ELF/OutputSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static void nopInstrFill(uint8_t *buf, size_t size) {
unsigned i = 0;
if (size == 0)
return;
std::vector<std::vector<uint8_t>> nopFiller = *target->nopInstrs;
std::vector<std::vector<uint8_t>> nopFiller = *ctx.target->nopInstrs;
unsigned num = size / nopFiller.back().size();
for (unsigned c = 0; c < num; ++c) {
memcpy(buf + i, nopFiller.back().data(), nopFiller.back().size());
Expand Down Expand Up @@ -541,7 +541,7 @@ void OutputSection::writeTo(uint8_t *buf, parallel::TaskGroup &tg) {
else
end = buf + sections[i + 1]->outSecOff;
if (isec->nopFiller) {
assert(target->nopInstrs);
assert(ctx.target->nopInstrs);
nopInstrFill(start, end - start);
} else
fill(start, end - start, filler);
Expand Down Expand Up @@ -857,7 +857,7 @@ std::array<uint8_t, 4> OutputSection::getFiller() {
if (filler)
return *filler;
if (flags & SHF_EXECINSTR)
return target->trapInstr;
return ctx.target->trapInstr;
return {0, 0, 0, 0};
}

Expand Down Expand Up @@ -890,7 +890,7 @@ void OutputSection::checkDynRelAddends(const uint8_t *bufStart) {
int64_t writtenAddend =
relOsec->type == SHT_NOBITS
? 0
: target->getImplicitAddend(relocTarget, rel.type);
: ctx.target->getImplicitAddend(relocTarget, rel.type);
if (addend != writtenAddend)
internalLinkerError(
getErrorLocation(relocTarget),
Expand Down
Loading

0 comments on commit 442b6ad

Please sign in to comment.