Skip to content

Commit

Permalink
[ELF] Move InStruct into 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.

llvm/Support/thread.h includes <thread>, which transitively includes
sstream in libc++ and uses ios_base::in, so we cannot use `#define in ctx.sec`.

`symtab, config, ctx` are now the only variables using
LLVM_LIBRARY_VISIBILITY.
  • Loading branch information
MaskRay committed Sep 16, 2024
1 parent 339282d commit e88b7ff
Show file tree
Hide file tree
Showing 24 changed files with 370 additions and 336 deletions.
10 changes: 5 additions & 5 deletions lld/ELF/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ int64_t AArch64::getImplicitAddend(const uint8_t *buf, RelType type) const {
}

void AArch64::writeGotPlt(uint8_t *buf, const Symbol &) const {
write64(buf, in.plt->getVA());
write64(buf, ctx.in.plt->getVA());
}

void AArch64::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
Expand All @@ -343,8 +343,8 @@ void AArch64::writePltHeader(uint8_t *buf) const {
};
memcpy(buf, pltData, sizeof(pltData));

uint64_t got = in.gotPlt->getVA();
uint64_t plt = in.plt->getVA();
uint64_t got = ctx.in.gotPlt->getVA();
uint64_t plt = ctx.in.plt->getVA();
relocateNoSym(buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
getAArch64Page(got + 16) - getAArch64Page(plt + 4));
relocateNoSym(buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, got + 16);
Expand Down Expand Up @@ -1003,8 +1003,8 @@ void AArch64BtiPac::writePltHeader(uint8_t *buf) const {
};
const uint8_t nopData[] = { 0x1f, 0x20, 0x03, 0xd5 }; // nop

uint64_t got = in.gotPlt->getVA();
uint64_t plt = in.plt->getVA();
uint64_t got = ctx.in.gotPlt->getVA();
uint64_t plt = ctx.in.plt->getVA();

if (btiHeader) {
// PltHeader is called indirectly by plt[N]. Prefix pltData with a BTI C
Expand Down
10 changes: 5 additions & 5 deletions lld/ELF/Arch/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ RelType ARM::getDynRel(RelType type) const {
}

void ARM::writeGotPlt(uint8_t *buf, const Symbol &) const {
write32(buf, in.plt->getVA());
write32(buf, ctx.in.plt->getVA());
}

void ARM::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
Expand All @@ -223,8 +223,8 @@ static void writePltHeaderLong(uint8_t *buf) {
write32(buf + 20, 0xd4d4d4d4); // Pad to 32-byte boundary
write32(buf + 24, 0xd4d4d4d4); // Pad to 32-byte boundary
write32(buf + 28, 0xd4d4d4d4);
uint64_t gotPlt = in.gotPlt->getVA();
uint64_t l1 = in.plt->getVA() + 8;
uint64_t gotPlt = ctx.in.gotPlt->getVA();
uint64_t l1 = ctx.in.plt->getVA() + 8;
write32(buf + 16, gotPlt - l1 - 8);
}

Expand All @@ -249,7 +249,7 @@ void ARM::writePltHeader(uint8_t *buf) const {
// At 0x8, we want to jump to .got.plt, the -16 accounts for 8 bytes from
// `pc` in the add instruction and 8 bytes for the `lr` adjustment.
//
uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 16;
uint64_t offset = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA() - 16;
assert(llvm::isUInt<32>(offset) && "This should always fit into a 32-bit offset");
write16(buf + 0, 0xb500);
// Split into two halves to support endianness correctly.
Expand Down Expand Up @@ -277,7 +277,7 @@ void ARM::writePltHeader(uint8_t *buf) const {
0xe5bef000, // ldr pc, [lr, #0x00000NNN] &(.got.plt -L1 - 4)
};

uint64_t offset = in.gotPlt->getVA() - in.plt->getVA() - 4;
uint64_t offset = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA() - 4;
if (!llvm::isUInt<27>(offset)) {
// We cannot encode the Offset, use the long form.
writePltHeaderLong(buf);
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Arch/Hexagon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ void Hexagon::writePltHeader(uint8_t *buf) const {
memcpy(buf, pltData, sizeof(pltData));

// Offset from PLT0 to the GOT.
uint64_t off = in.gotPlt->getVA() - in.plt->getVA();
uint64_t off = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA();
relocateNoSym(buf, R_HEX_B32_PCREL_X, off);
relocateNoSym(buf + 4, R_HEX_6_PCREL_X, off);
}
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/Arch/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ int64_t LoongArch::getImplicitAddend(const uint8_t *buf, RelType type) const {

void LoongArch::writeGotPlt(uint8_t *buf, const Symbol &s) const {
if (config->is64)
write64le(buf, in.plt->getVA());
write64le(buf, ctx.in.plt->getVA());
else
write32le(buf, in.plt->getVA());
write32le(buf, ctx.in.plt->getVA());
}

void LoongArch::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
Expand Down Expand Up @@ -341,7 +341,7 @@ void LoongArch::writePltHeader(uint8_t *buf) const {
// srli.[wd] $t1, $t1, (is64?1:2) ; t1 = &.got.plt[i] - &.got.plt[0]
// ld.[wd] $t0, $t0, Wordsize ; t0 = link_map
// jr $t3
uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
uint32_t offset = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA();
uint32_t sub = config->is64 ? SUB_D : SUB_W;
uint32_t ld = config->is64 ? LD_D : LD_W;
uint32_t addi = config->is64 ? ADDI_D : ADDI_W;
Expand Down
8 changes: 4 additions & 4 deletions lld/ELF/Arch/Mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ template <class ELFT> RelType MIPS<ELFT>::getDynRel(RelType type) const {

template <class ELFT>
void MIPS<ELFT>::writeGotPlt(uint8_t *buf, const Symbol &) const {
uint64_t va = in.plt->getVA();
uint64_t va = ctx.in.plt->getVA();
if (isMicroMips())
va |= 1;
write32(buf, va);
Expand Down Expand Up @@ -257,8 +257,8 @@ static void writeMicroRelocation16(uint8_t *loc, uint64_t v, uint8_t bitsSize,

template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *buf) const {
if (isMicroMips()) {
uint64_t gotPlt = in.gotPlt->getVA();
uint64_t plt = in.plt->getVA();
uint64_t gotPlt = ctx.in.gotPlt->getVA();
uint64_t plt = ctx.in.plt->getVA();
// Overwrite trap instructions written by Writer::writeTrapInstr.
memset(buf, 0, pltHeaderSize);

Expand Down Expand Up @@ -310,7 +310,7 @@ template <class ELFT> void MIPS<ELFT>::writePltHeader(uint8_t *buf) const {
write32(buf + 24, jalrInst); // jalr.hb $25 or jalr $25
write32(buf + 28, 0x2718fffe); // subu $24, $24, 2

uint64_t gotPlt = in.gotPlt->getVA();
uint64_t gotPlt = ctx.in.gotPlt->getVA();
writeValue(buf, gotPlt + 0x8000, 16, 16);
writeValue(buf + 4, gotPlt, 16, 0);
writeValue(buf + 8, gotPlt, 16, 0);
Expand Down
12 changes: 7 additions & 5 deletions lld/ELF/Arch/PPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ static void writeFromHalf16(uint8_t *loc, uint32_t insn) {
void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {
// Create canonical PLT entries for non-PIE code. Compilers don't generate
// non-GOT-non-PLT relocations referencing external functions for -fpie/-fPIE.
uint32_t glink = in.plt->getVA(); // VA of .glink
uint32_t glink = ctx.in.plt->getVA(); // VA of .glink
if (!config->isPic) {
for (const Symbol *sym : cast<PPC32GlinkSection>(*in.plt).canonical_plts) {
for (const Symbol *sym :
cast<PPC32GlinkSection>(*ctx.in.plt).canonical_plts) {
writePPC32PltCallStub(buf, sym->getGotPltVA(), nullptr, 0);
buf += 16;
glink += 16;
Expand All @@ -101,10 +102,10 @@ void elf::writePPC32GlinkSection(uint8_t *buf, size_t numEntries) {
// Then write PLTresolve(), which has two forms: PIC and non-PIC. PLTresolve()
// computes the PLT index (by computing the distance from the landing b to
// itself) and calls _dl_runtime_resolve() (in glibc).
uint32_t got = in.got->getVA();
uint32_t got = ctx.in.got->getVA();
const uint8_t *end = buf + 64;
if (config->isPic) {
uint32_t afterBcl = 4 * in.plt->getNumEntries() + 12;
uint32_t afterBcl = 4 * ctx.in.plt->getNumEntries() + 12;
uint32_t gotBcl = got + 4 - (glink + afterBcl);
write32(buf + 0, 0x3d6b0000 | ha(afterBcl)); // addis r11,r11,1f-glink@ha
write32(buf + 4, 0x7c0802a6); // mflr r0
Expand Down Expand Up @@ -192,7 +193,8 @@ void PPC::writeGotHeader(uint8_t *buf) const {

void PPC::writeGotPlt(uint8_t *buf, const Symbol &s) const {
// Address of the symbol resolver stub in .glink .
write32(buf, in.plt->getVA() + in.plt->headerSize + 4 * s.getPltIdx());
write32(buf,
ctx.in.plt->getVA() + ctx.in.plt->headerSize + 4 * s.getPltIdx());
}

bool PPC::needsThunk(RelExpr expr, RelType type, const InputFile *file,
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 @@ -210,7 +210,7 @@ uint64_t elf::getPPC64TocBase() {
// TOC starts where the first of these sections starts. We always create a
// .got when we see a relocation that uses it, so for us the start is always
// the .got.
uint64_t tocVA = in.got->getVA();
uint64_t tocVA = ctx.in.got->getVA();

// Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
// thus permitting a full 64 Kbytes segment. Note that the glibc startup
Expand Down Expand Up @@ -1155,7 +1155,7 @@ void PPC64::writePltHeader(uint8_t *buf) const {
// The 'bcl' instruction will set the link register to the address of the
// following instruction ('mflr r11'). Here we store the offset from that
// instruction to the first entry in the GotPlt section.
int64_t gotPltOffset = in.gotPlt->getVA() - (in.plt->getVA() + 8);
int64_t gotPltOffset = ctx.in.gotPlt->getVA() - (ctx.in.plt->getVA() + 8);
write64(buf + 52, gotPltOffset);
}

Expand Down
10 changes: 5 additions & 5 deletions lld/ELF/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ void RISCV::writeGotHeader(uint8_t *buf) const {

void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {
if (config->is64)
write64le(buf, in.plt->getVA());
write64le(buf, ctx.in.plt->getVA());
else
write32le(buf, in.plt->getVA());
write32le(buf, ctx.in.plt->getVA());
}

void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
Expand All @@ -230,7 +230,7 @@ void RISCV::writePltHeader(uint8_t *buf) const {
// srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0]
// l[wd] t0, Wordsize(t0); t0 = link_map
// jr t3
uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
uint32_t offset = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA();
uint32_t load = config->is64 ? LD : LW;
write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
Expand Down Expand Up @@ -1178,8 +1178,8 @@ mergeAttributesSection(const SmallVector<InputSectionBase *, 0> &sections) {
unsigned firstStackAlignValue = 0, xlen = 0;
bool hasArch = false;

in.riscvAttributes = std::make_unique<RISCVAttributesSection>();
auto &merged = static_cast<RISCVAttributesSection &>(*in.riscvAttributes);
ctx.in.riscvAttributes = std::make_unique<RISCVAttributesSection>();
auto &merged = static_cast<RISCVAttributesSection &>(*ctx.in.riscvAttributes);

// Collect all tags values from attributes section.
const auto &attributesTags = RISCVAttrs::getRISCVAttributeTags();
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Arch/SPARCV9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
};
memcpy(buf, pltData, sizeof(pltData));

uint64_t off = pltEntryAddr - in.plt->getVA();
uint64_t off = pltEntryAddr - ctx.in.plt->getVA();
relocateNoSym(buf, R_SPARC_22, off);
relocateNoSym(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
}
Expand Down
10 changes: 5 additions & 5 deletions lld/ELF/Arch/SystemZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ void SystemZ::writePltHeader(uint8_t *buf) const {
0x07, 0x00, // nopr
};
memcpy(buf, pltData, sizeof(pltData));
uint64_t got = in.got->getVA();
uint64_t plt = in.plt->getVA();
uint64_t got = ctx.in.got->getVA();
uint64_t plt = ctx.in.plt->getVA();
write32be(buf + 8, (got - plt - 6) >> 1);
}

void SystemZ::addPltHeaderSymbols(InputSection &isec) const {
// The PLT header needs a reference to _GLOBAL_OFFSET_TABLE_, so we
// must ensure the .got section is created even if otherwise unused.
in.got->hasGotOffRel.store(true, std::memory_order_relaxed);
ctx.in.got->hasGotOffRel.store(true, std::memory_order_relaxed);
}

void SystemZ::writePlt(uint8_t *buf, const Symbol &sym,
Expand All @@ -228,8 +228,8 @@ void SystemZ::writePlt(uint8_t *buf, const Symbol &sym,
memcpy(buf, inst, sizeof(inst));

write32be(buf + 2, (sym.getGotPltVA() - pltEntryAddr) >> 1);
write32be(buf + 24, (in.plt->getVA() - pltEntryAddr - 22) >> 1);
write32be(buf + 28, in.relaPlt->entsize * sym.getPltIdx());
write32be(buf + 24, (ctx.in.plt->getVA() - pltEntryAddr - 22) >> 1);
write32be(buf + 28, ctx.in.relaPlt->entsize * sym.getPltIdx());
}

int64_t SystemZ::getImplicitAddend(const uint8_t *buf, RelType type) const {
Expand Down
24 changes: 12 additions & 12 deletions lld/ELF/Arch/X86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,22 @@ void X86::writePltHeader(uint8_t *buf) const {
0x90, 0x90, 0x90, 0x90, // nop
};
memcpy(buf, pltData, sizeof(pltData));
uint32_t gotPlt = in.gotPlt->getVA();
uint32_t gotPlt = ctx.in.gotPlt->getVA();
write32le(buf + 2, gotPlt + 4);
write32le(buf + 8, gotPlt + 8);
}

void X86::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
unsigned relOff = ctx.in.relaPlt->entsize * sym.getPltIdx();
if (config->isPic) {
const uint8_t inst[] = {
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
0x68, 0, 0, 0, 0, // pushl $reloc_offset
0xe9, 0, 0, 0, 0, // jmp .PLT0@PC
};
memcpy(buf, inst, sizeof(inst));
write32le(buf + 2, sym.getGotPltVA() - in.gotPlt->getVA());
write32le(buf + 2, sym.getGotPltVA() - ctx.in.gotPlt->getVA());
} else {
const uint8_t inst[] = {
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
Expand All @@ -230,7 +230,7 @@ void X86::writePlt(uint8_t *buf, const Symbol &sym,
}

write32le(buf + 7, relOff);
write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16);
write32le(buf + 12, ctx.in.plt->getVA() - pltEntryAddr - 16);
}

int64_t X86::getImplicitAddend(const uint8_t *buf, RelType type) const {
Expand Down Expand Up @@ -532,7 +532,7 @@ IntelIBT::IntelIBT() { pltHeaderSize = 0; }

void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const {
uint64_t va =
in.ibtPlt->getVA() + IBTPltHeaderSize + s.getPltIdx() * pltEntrySize;
ctx.in.ibtPlt->getVA() + IBTPltHeaderSize + s.getPltIdx() * pltEntrySize;
write32le(buf, va);
}

Expand All @@ -545,7 +545,7 @@ void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym,
0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
};
memcpy(buf, inst, sizeof(inst));
write32le(buf + 6, sym.getGotPltVA() - in.gotPlt->getVA());
write32le(buf + 6, sym.getGotPltVA() - ctx.in.gotPlt->getVA());
return;
}

Expand Down Expand Up @@ -630,7 +630,7 @@ void RetpolinePic::writePltHeader(uint8_t *buf) const {

void RetpolinePic::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
unsigned relOff = ctx.in.relaPlt->entsize * sym.getPltIdx();
const uint8_t insn[] = {
0x50, // pushl %eax
0x8b, 0x83, 0, 0, 0, 0, // mov foo@GOT(%ebx), %eax
Expand All @@ -642,8 +642,8 @@ void RetpolinePic::writePlt(uint8_t *buf, const Symbol &sym,
};
memcpy(buf, insn, sizeof(insn));

uint32_t ebx = in.gotPlt->getVA();
unsigned off = pltEntryAddr - in.plt->getVA();
uint32_t ebx = ctx.in.gotPlt->getVA();
unsigned off = pltEntryAddr - ctx.in.plt->getVA();
write32le(buf + 3, sym.getGotPltVA() - ebx);
write32le(buf + 8, -off - 12 + 32);
write32le(buf + 13, -off - 17 + 18);
Expand Down Expand Up @@ -682,14 +682,14 @@ void RetpolineNoPic::writePltHeader(uint8_t *buf) const {
};
memcpy(buf, insn, sizeof(insn));

uint32_t gotPlt = in.gotPlt->getVA();
uint32_t gotPlt = ctx.in.gotPlt->getVA();
write32le(buf + 2, gotPlt + 4);
write32le(buf + 8, gotPlt + 8);
}

void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
uint64_t pltEntryAddr) const {
unsigned relOff = in.relaPlt->entsize * sym.getPltIdx();
unsigned relOff = ctx.in.relaPlt->entsize * sym.getPltIdx();
const uint8_t insn[] = {
0x50, // 0: pushl %eax
0xa1, 0, 0, 0, 0, // 1: mov foo_in_GOT, %eax
Expand All @@ -702,7 +702,7 @@ void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
};
memcpy(buf, insn, sizeof(insn));

unsigned off = pltEntryAddr - in.plt->getVA();
unsigned off = pltEntryAddr - ctx.in.plt->getVA();
write32le(buf + 2, sym.getGotPltVA());
write32le(buf + 7, -off - 11 + 32);
write32le(buf + 12, -off - 16 + 17);
Expand Down
Loading

0 comments on commit e88b7ff

Please sign in to comment.