Skip to content

Commit a3bad9a

Browse files
committed
[ELF] Pass Ctx &
1 parent 51a2f50 commit a3bad9a

File tree

6 files changed

+41
-38
lines changed

6 files changed

+41
-38
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,17 @@ void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
196196
// Used for handling linker symbol assignments, for both finalizing
197197
// their values and doing early declarations. Returns true if symbol
198198
// should be defined from linker script.
199-
static bool shouldDefineSym(SymbolAssignment *cmd) {
199+
static bool shouldDefineSym(Ctx &ctx, SymbolAssignment *cmd) {
200200
if (cmd->name == ".")
201201
return false;
202202

203-
return !cmd->provide || LinkerScript::shouldAddProvideSym(cmd->name);
203+
return !cmd->provide || LinkerScript::shouldAddProvideSym(ctx, cmd->name);
204204
}
205205

206206
// Called by processSymbolAssignments() to assign definitions to
207207
// linker-script-defined symbols.
208208
void LinkerScript::addSymbol(SymbolAssignment *cmd) {
209-
if (!shouldDefineSym(cmd))
209+
if (!shouldDefineSym(ctx, cmd))
210210
return;
211211

212212
// Define a symbol.
@@ -240,7 +240,7 @@ void LinkerScript::addSymbol(SymbolAssignment *cmd) {
240240
// This function is called from LinkerScript::declareSymbols.
241241
// It creates a placeholder symbol if needed.
242242
void LinkerScript::declareSymbol(SymbolAssignment *cmd) {
243-
if (!shouldDefineSym(cmd))
243+
if (!shouldDefineSym(ctx, cmd))
244244
return;
245245

246246
uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
@@ -1798,30 +1798,30 @@ void LinkerScript::addScriptReferencedSymbolsToSymTable() {
17981798
DenseSet<StringRef> added;
17991799
SmallVector<const SmallVector<StringRef, 0> *, 0> symRefsVec;
18001800
for (const auto &[name, symRefs] : provideMap)
1801-
if (LinkerScript::shouldAddProvideSym(name) && added.insert(name).second)
1801+
if (shouldAddProvideSym(ctx, name) && added.insert(name).second)
18021802
symRefsVec.push_back(&symRefs);
18031803
while (symRefsVec.size()) {
18041804
for (StringRef name : *symRefsVec.pop_back_val()) {
18051805
reference(name);
18061806
// Prevent the symbol from being discarded by --gc-sections.
18071807
referencedSymbols.push_back(name);
18081808
auto it = provideMap.find(name);
1809-
if (it != provideMap.end() && shouldAddProvideSym(name) &&
1809+
if (it != provideMap.end() && shouldAddProvideSym(ctx, name) &&
18101810
added.insert(name).second) {
18111811
symRefsVec.push_back(&it->second);
18121812
}
18131813
}
18141814
}
18151815
}
18161816

1817-
bool LinkerScript::shouldAddProvideSym(StringRef symName) {
1817+
bool LinkerScript::shouldAddProvideSym(Ctx &ctx, StringRef symName) {
18181818
// This function is called before and after garbage collection. To prevent
18191819
// undefined references from the RHS, the result of this function for a
18201820
// symbol must be the same for each call. We use isUsedInRegularObj to not
18211821
// change the return value of a demoted symbol. The exportDynamic condition,
18221822
// while not so accurate, allows PROVIDE to define a symbol referenced by a
18231823
// DSO.
1824-
Symbol *sym = elf::ctx.symtab->find(symName);
1824+
Symbol *sym = ctx.symtab->find(symName);
18251825
return sym && !sym->isDefined() && !sym->isCommon() &&
18261826
(sym->isUsedInRegularObj || sym->exportDynamic);
18271827
}

lld/ELF/LinkerScript.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class LinkerScript final {
389389
// Returns true if the PROVIDE symbol should be added to the link.
390390
// A PROVIDE symbol is added to the link only if it satisfies an
391391
// undefined reference.
392-
static bool shouldAddProvideSym(StringRef symName);
392+
static bool shouldAddProvideSym(Ctx &, StringRef symName);
393393

394394
// SECTIONS command list.
395395
SmallVector<SectionCommand *, 0> sectionCommands;

lld/ELF/Relocations.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,
917917
}
918918

919919
template <class PltSection, class GotPltSection>
920-
static void addPltEntry(PltSection &plt, GotPltSection &gotPlt,
920+
static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,
921921
RelocationBaseSection &rel, RelType type, Symbol &sym) {
922922
plt.addEntry(sym);
923923
gotPlt.addEntry(sym);
@@ -1756,7 +1756,7 @@ static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {
17561756
directSym->allocateAux(ctx);
17571757
auto &dyn =
17581758
ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn;
1759-
addPltEntry(*ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,
1759+
addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,
17601760
*directSym);
17611761
sym.allocateAux(ctx);
17621762
ctx.symAux.back().pltIdx = ctx.symAux[directSym->auxIdx].pltIdx;
@@ -1796,7 +1796,7 @@ void elf::postScanRelocations(Ctx &ctx) {
17961796
if (flags & NEEDS_GOT)
17971797
addGotEntry(ctx, sym);
17981798
if (flags & NEEDS_PLT)
1799-
addPltEntry(*ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,
1799+
addPltEntry(ctx, *ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,
18001800
ctx.target->pltRel, sym);
18011801
if (flags & NEEDS_COPY) {
18021802
if (sym.isObject()) {
@@ -2427,7 +2427,7 @@ void elf::hexagonTLSSymbolUpdate(Ctx &ctx) {
24272427
if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
24282428
if (needEntry) {
24292429
sym->allocateAux(ctx);
2430-
addPltEntry(*ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,
2430+
addPltEntry(ctx, *ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,
24312431
ctx.target->pltRel, *sym);
24322432
needEntry = false;
24332433
}

lld/ELF/SyntheticSections.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
290290
return s;
291291
}
292292

293-
static size_t getHashSize() {
293+
static size_t getHashSize(Ctx &ctx) {
294294
switch (ctx.arg.buildId) {
295295
case BuildIdKind::Fast:
296296
return 8;
@@ -362,7 +362,7 @@ size_t GnuPropertySection::getSize() const {
362362

363363
BuildIdSection::BuildIdSection(Ctx &ctx)
364364
: SyntheticSection(ctx, SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),
365-
hashSize(getHashSize()) {}
365+
hashSize(getHashSize(ctx)) {}
366366

367367
void BuildIdSection::writeTo(uint8_t *buf) {
368368
write32(buf, 4); // Name size
@@ -1208,7 +1208,7 @@ bool GotPltSection::isNeeded() const {
12081208
return !entries.empty() || hasGotPltOffRel;
12091209
}
12101210

1211-
static StringRef getIgotPltName() {
1211+
static StringRef getIgotPltName(Ctx &ctx) {
12121212
// On ARM the IgotPltSection is part of the GotSection.
12131213
if (ctx.arg.emachine == EM_ARM)
12141214
return ".got";
@@ -1226,7 +1226,7 @@ static StringRef getIgotPltName() {
12261226
IgotPltSection::IgotPltSection(Ctx &ctx)
12271227
: SyntheticSection(ctx, SHF_ALLOC | SHF_WRITE,
12281228
ctx.arg.emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1229-
ctx.target->gotEntrySize, getIgotPltName()) {}
1229+
ctx.target->gotEntrySize, getIgotPltName(ctx)) {}
12301230

12311231
void IgotPltSection::addEntry(Symbol &sym) {
12321232
assert(ctx.symAux.back().pltIdx == entries.size());
@@ -1605,7 +1605,7 @@ uint64_t DynamicReloc::getOffset() const {
16051605
return inputSec->getVA(offsetInSec);
16061606
}
16071607

1608-
int64_t DynamicReloc::computeAddend() const {
1608+
int64_t DynamicReloc::computeAddend(Ctx &ctx) const {
16091609
switch (kind) {
16101610
case AddendOnly:
16111611
assert(sym == nullptr);
@@ -1704,17 +1704,18 @@ void RelocationBaseSection::finalizeContents() {
17041704
}
17051705
}
17061706

1707-
void DynamicReloc::computeRaw(SymbolTableBaseSection *symt) {
1707+
void DynamicReloc::computeRaw(Ctx &ctx, SymbolTableBaseSection *symt) {
17081708
r_offset = getOffset();
17091709
r_sym = getSymIndex(symt);
1710-
addend = computeAddend();
1710+
addend = computeAddend(ctx);
17111711
kind = AddendOnly; // Catch errors
17121712
}
17131713

17141714
void RelocationBaseSection::computeRels() {
17151715
SymbolTableBaseSection *symTab = getPartition().dynSymTab.get();
1716-
parallelForEach(relocs,
1717-
[symTab](DynamicReloc &rel) { rel.computeRaw(symTab); });
1716+
parallelForEach(relocs, [&ctx = ctx, symTab](DynamicReloc &rel) {
1717+
rel.computeRaw(ctx, symTab);
1718+
});
17181719

17191720
auto irelative = std::stable_partition(
17201721
relocs.begin() + numRelativeRelocs, relocs.end(),
@@ -1853,7 +1854,7 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
18531854
r.r_offset = rel.getOffset();
18541855
r.setSymbolAndType(rel.getSymIndex(getPartition().dynSymTab.get()),
18551856
rel.type, false);
1856-
r.r_addend = ctx.arg.isRela ? rel.computeAddend() : 0;
1857+
r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
18571858

18581859
if (r.getType(ctx.arg.isMips64EL) == ctx.target->relativeRel)
18591860
relatives.push_back(r);
@@ -3438,6 +3439,7 @@ readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj,
34383439
// by uniquifying them by name.
34393440
static std::pair<SmallVector<GdbIndexSection::GdbSymbol, 0>, size_t>
34403441
createSymbols(
3442+
Ctx &ctx,
34413443
ArrayRef<SmallVector<GdbIndexSection::NameAttrEntry, 0>> nameAttrs,
34423444
const SmallVector<GdbIndexSection::GdbChunk, 0> &chunks) {
34433445
using GdbSymbol = GdbIndexSection::GdbSymbol;
@@ -3563,7 +3565,8 @@ std::unique_ptr<GdbIndexSection> GdbIndexSection::create(Ctx &ctx) {
35633565

35643566
auto ret = std::make_unique<GdbIndexSection>(ctx);
35653567
ret->chunks = std::move(chunks);
3566-
std::tie(ret->symbols, ret->size) = createSymbols(nameAttrs, ret->chunks);
3568+
std::tie(ret->symbols, ret->size) =
3569+
createSymbols(ctx, nameAttrs, ret->chunks);
35673570

35683571
// Count the areas other than the constant pool.
35693572
ret->size += sizeof(GdbIndexHeader) + ret->computeSymtabSize() * 8;
@@ -3784,9 +3787,9 @@ bool VersionTableSection::isNeeded() const {
37843787
(getPartition().verDef || getPartition().verNeed->isNeeded());
37853788
}
37863789

3787-
void elf::addVerneed(Symbol *ss) {
3788-
auto &file = cast<SharedFile>(*ss->file);
3789-
if (ss->versionId == VER_NDX_GLOBAL)
3790+
void elf::addVerneed(Ctx &ctx, Symbol &ss) {
3791+
auto &file = cast<SharedFile>(*ss.file);
3792+
if (ss.versionId == VER_NDX_GLOBAL)
37903793
return;
37913794

37923795
if (file.vernauxs.empty())
@@ -3796,10 +3799,10 @@ void elf::addVerneed(Symbol *ss) {
37963799
// already allocated one. The verdef identifiers cover the range
37973800
// [1..getVerDefNum(ctx)]; this causes the vernaux identifiers to start from
37983801
// getVerDefNum(ctx)+1.
3799-
if (file.vernauxs[ss->versionId] == 0)
3800-
file.vernauxs[ss->versionId] = ++SharedFile::vernauxNum + getVerDefNum(ctx);
3802+
if (file.vernauxs[ss.versionId] == 0)
3803+
file.vernauxs[ss.versionId] = ++SharedFile::vernauxNum + getVerDefNum(ctx);
38013804

3802-
ss->versionId = file.vernauxs[ss->versionId];
3805+
ss.versionId = file.vernauxs[ss.versionId];
38033806
}
38043807

38053808
template <class ELFT>
@@ -4659,7 +4662,7 @@ size_t MemtagGlobalDescriptors::getSize() const {
46594662
return createMemtagGlobalDescriptors(ctx, symbols);
46604663
}
46614664

4662-
static OutputSection *findSection(StringRef name) {
4665+
static OutputSection *findSection(Ctx &ctx, StringRef name) {
46634666
for (SectionCommand *cmd : ctx.script->sectionCommands)
46644667
if (auto *osd = dyn_cast<OutputDesc>(cmd))
46654668
if (osd->osec.name == name)
@@ -4715,7 +4718,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
47154718
// .data.rel.ro.bss so that we match in the .data.rel.ro output section.
47164719
// This makes sure our relro is contiguous.
47174720
bool hasDataRelRo =
4718-
ctx.script->hasSectionsCommand && findSection(".data.rel.ro");
4721+
ctx.script->hasSectionsCommand && findSection(ctx, ".data.rel.ro");
47194722
ctx.in.bssRelRo = std::make_unique<BssSection>(
47204723
ctx, hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
47214724
add(*ctx.in.bssRelRo);

lld/ELF/SyntheticSections.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,14 @@ class DynamicReloc {
411411
public:
412412
enum Kind {
413413
/// The resulting dynamic relocation does not reference a symbol (#sym must
414-
/// be nullptr) and uses #addend as the result of computeAddend().
414+
/// be nullptr) and uses #addend as the result of computeAddend(ctx).
415415
AddendOnly,
416416
/// The resulting dynamic relocation will not reference a symbol: #sym is
417417
/// only used to compute the addend with InputSection::getRelocTargetVA().
418418
/// Useful for various relative and TLS relocations (e.g. R_X86_64_TPOFF64).
419419
AddendOnlyWithTargetVA,
420420
/// The resulting dynamic relocation references symbol #sym from the dynamic
421-
/// symbol table and uses #addend as the value of computeAddend().
421+
/// symbol table and uses #addend as the value of computeAddend(ctx).
422422
AgainstSymbol,
423423
/// The resulting dynamic relocation references symbol #sym from the dynamic
424424
/// symbol table and uses InputSection::getRelocTargetVA() + #addend for the
@@ -458,9 +458,9 @@ class DynamicReloc {
458458
/// Computes the addend of the dynamic relocation. Note that this is not the
459459
/// same as the #addend member variable as it may also include the symbol
460460
/// address/the address of the corresponding GOT entry/etc.
461-
int64_t computeAddend() const;
461+
int64_t computeAddend(Ctx &) const;
462462

463-
void computeRaw(SymbolTableBaseSection *symt);
463+
void computeRaw(Ctx &, SymbolTableBaseSection *symt);
464464

465465
Symbol *sym;
466466
const OutputSection *outputSec = nullptr;
@@ -1443,7 +1443,7 @@ Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
14431443
uint64_t value, uint64_t size,
14441444
InputSectionBase &section);
14451445

1446-
void addVerneed(Symbol *ss);
1446+
void addVerneed(Ctx &, Symbol &ss);
14471447

14481448
// Linker generated per-partition sections.
14491449
struct Partition {

lld/ELF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18791879
ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
18801880
if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
18811881
if (file->isNeeded && !sym->isUndefined())
1882-
addVerneed(sym);
1882+
addVerneed(ctx, *sym);
18831883
}
18841884
}
18851885

0 commit comments

Comments
 (0)