Skip to content

Commit

Permalink
[ELF] Pass Ctx & to Symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Oct 12, 2024
1 parent a62768c commit dbd1971
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 100 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/Arch/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ template <typename ELFT> void elf::writeARMCmseImportLib(Ctx &ctx) {
for (auto &p : ctx.symtab->cmseSymMap) {
Defined *d = cast<Defined>(p.second.sym);
impSymTab->addSymbol(makeDefined(
ctx.internalFile, d->getName(), d->computeBinding(),
ctx.internalFile, d->getName(), d->computeBinding(ctx),
/*stOther=*/0, STT_FUNC, d->getVA(), d->getSize(), nullptr));
}

Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/Arch/PPC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ static bool addOptional(Ctx &ctx, StringRef name, uint64_t value,
Symbol *sym = ctx.symtab->find(name);
if (!sym || sym->isDefined())
return false;
sym->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, STV_HIDDEN,
STT_FUNC, value,
/*size=*/0, /*section=*/nullptr});
sym->resolve(ctx, Defined{ctx.internalFile, StringRef(), STB_GLOBAL,
STV_HIDDEN, STT_FUNC, value,
/*size=*/0, /*section=*/nullptr});
defined.push_back(cast<Defined>(sym));
return true;
}
Expand Down
18 changes: 9 additions & 9 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ static void handleUndefined(Ctx &ctx, Symbol *sym, const char *option) {

if (!sym->isLazy())
return;
sym->extract();
sym->extract(ctx);
if (!ctx.arg.whyExtract.empty())
ctx.whyExtractRecords.emplace_back(option, sym->file, *sym);
}
Expand Down Expand Up @@ -2217,7 +2217,7 @@ static void handleLibcall(Ctx &ctx, StringRef name) {
if (sym && sym->isLazy() && isa<BitcodeFile>(sym->file)) {
if (!ctx.arg.whyExtract.empty())
ctx.whyExtractRecords.emplace_back("<libcall>", sym->file, *sym);
sym->extract();
sym->extract(ctx);
}
}

Expand Down Expand Up @@ -2416,7 +2416,7 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
// or DSOs, so we conservatively mark them as address-significant.
bool icfSafe = ctx.arg.icf == ICFLevel::Safe;
for (Symbol *sym : ctx.symtab->getSymbols())
if (sym->includeInDynsym())
if (sym->includeInDynsym(ctx))
markAddrsig(icfSafe, sym);

// Visit the address-significance table in each object file and mark each
Expand Down Expand Up @@ -2465,7 +2465,7 @@ static void readSymbolPartitionSection(Ctx &ctx, InputSectionBase *s) {
sym = readEntry(s->file, rels.rels);
else
sym = readEntry(s->file, rels.relas);
if (!isa_and_nonnull<Defined>(sym) || !sym->includeInDynsym())
if (!isa_and_nonnull<Defined>(sym) || !sym->includeInDynsym(ctx))
return;

StringRef partName = reinterpret_cast<const char *>(s->content().data());
Expand Down Expand Up @@ -2551,7 +2551,7 @@ void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
if (!ctx.arg.relocatable)
for (Symbol *sym : obj->getGlobalSymbols())
if (sym->hasVersionSuffix)
sym->parseSymbolVersion();
sym->parseSymbolVersion(ctx);
ctx.objectFiles.push_back(obj);
}
}
Expand Down Expand Up @@ -2648,12 +2648,12 @@ static void combineVersionedSymbol(Ctx &ctx, Symbol &sym,
// If both foo@v1 and foo@@v1 are defined and non-weak, report a
// duplicate definition error.
if (sym.isDefined()) {
sym2->checkDuplicate(cast<Defined>(sym));
sym2->resolve(cast<Defined>(sym));
sym2->checkDuplicate(ctx, cast<Defined>(sym));
sym2->resolve(ctx, cast<Defined>(sym));
} else if (sym.isUndefined()) {
sym2->resolve(cast<Undefined>(sym));
sym2->resolve(ctx, cast<Undefined>(sym));
} else {
sym2->resolve(cast<SharedSymbol>(sym));
sym2->resolve(ctx, cast<SharedSymbol>(sym));
}
// Eliminate foo@v1 from the symbol table.
sym.symbolKind = Symbol::PlaceholderKind;
Expand Down
44 changes: 22 additions & 22 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,14 +1156,14 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
fatal(toString(this) + ": common symbol '" + sym->getName() +
"' has invalid alignment: " + Twine(value));
hasCommonSyms = true;
sym->resolve(
CommonSymbol{this, StringRef(), binding, stOther, type, value, size});
sym->resolve(ctx, CommonSymbol{this, StringRef(), binding, stOther, type,
value, size});
continue;
}

// Handle global defined symbols. Defined::section will be set in postParse.
sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size,
nullptr});
sym->resolve(ctx, Defined{this, StringRef(), binding, stOther, type, value,
size, nullptr});
}

// Undefined symbols (excluding those defined relative to non-prevailing
Expand All @@ -1175,8 +1175,8 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
for (unsigned i : undefineds) {
const Elf_Sym &eSym = eSyms[i];
Symbol *sym = symbols[i];
sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other,
eSym.getType()});
sym->resolve(ctx, Undefined{this, StringRef(), eSym.getBinding(),
eSym.st_other, eSym.getType()});
sym->isUsedInRegularObj = true;
sym->referenced = true;
}
Expand Down Expand Up @@ -1759,20 +1759,20 @@ static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,
int c = objSym.getComdatIndex();
if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
Undefined newSym(&f, StringRef(), binding, visibility, type);
sym->resolve(newSym);
sym->resolve(ctx, newSym);
sym->referenced = true;
return;
}

if (objSym.isCommon()) {
sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT,
objSym.getCommonAlignment(),
objSym.getCommonSize()});
sym->resolve(ctx, CommonSymbol{&f, StringRef(), binding, visibility,
STT_OBJECT, objSym.getCommonAlignment(),
objSym.getCommonSize()});
} else {
Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr);
if (objSym.canBeOmittedFromSymbolTable())
newSym.exportDynamic = false;
sym->resolve(newSym);
sym->resolve(ctx, newSym);
}
}

Expand Down Expand Up @@ -1813,7 +1813,7 @@ void BitcodeFile::parseLazy() {
irSym.Name = uniqueSaver().save(irSym.getName());
if (!irSym.isUndefined()) {
auto *sym = ctx.symtab->insert(irSym.getName());
sym->resolve(LazySymbol{*this});
sym->resolve(ctx, LazySymbol{*this});
symbols[i] = sym;
}
}
Expand Down Expand Up @@ -1849,15 +1849,15 @@ void BinaryFile::parse() {

llvm::StringSaver &saver = lld::saver();

ctx.symtab->addAndCheckDuplicate(Defined{this, saver.save(s + "_start"),
STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
0, 0, section});
ctx.symtab->addAndCheckDuplicate(Defined{this, saver.save(s + "_end"),
STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
data.size(), 0, section});
ctx.symtab->addAndCheckDuplicate(Defined{this, saver.save(s + "_size"),
STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
data.size(), 0, nullptr});
ctx.symtab->addAndCheckDuplicate(ctx, Defined{this, saver.save(s + "_start"),
STB_GLOBAL, STV_DEFAULT,
STT_OBJECT, 0, 0, section});
ctx.symtab->addAndCheckDuplicate(
ctx, Defined{this, saver.save(s + "_end"), STB_GLOBAL, STV_DEFAULT,
STT_OBJECT, data.size(), 0, section});
ctx.symtab->addAndCheckDuplicate(
ctx, Defined{this, saver.save(s + "_size"), STB_GLOBAL, STV_DEFAULT,
STT_OBJECT, data.size(), 0, nullptr});
}

InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
Expand Down Expand Up @@ -1906,7 +1906,7 @@ template <class ELFT> void ObjFile<ELFT>::parseLazy() {
if (eSyms[i].st_shndx == SHN_UNDEF)
continue;
symbols[i] = symtab->insert(CHECK(eSyms[i].getName(stringTable), this));
symbols[i]->resolve(LazySymbol{*this});
symbols[i]->resolve(ctx, LazySymbol{*this});
if (!lazy)
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ void BitcodeCompiler::add(BitcodeFile &f) {
// 5) Symbols that will be referenced after linker wrapping is performed.
r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
sym->referencedAfterWrap ||
(r.Prevailing && sym->includeInDynsym()) ||
(r.Prevailing && sym->includeInDynsym(ctx)) ||
usedStartStop.count(objSym.getSectionName());
// Identify symbols exported dynamically, and that therefore could be
// referenced by a shared library not visible to the linker.
r.ExportDynamic =
sym->computeBinding() != STB_LOCAL &&
sym->computeBinding(ctx) != STB_LOCAL &&
(ctx.arg.exportDynamic || sym->exportDynamic || sym->inDynamicList);
const auto *dr = dyn_cast<Defined>(sym);
r.FinalDefinitionInLinkageUnit =
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
// Preserve externally-visible symbols if the symbols defined by this
// file can interpose other ELF file's symbols at runtime.
for (Symbol *sym : ctx.symtab->getSymbols())
if (sym->includeInDynsym() && sym->partition == partition)
if (sym->includeInDynsym(ctx) && sym->partition == partition)
markSymbol(sym);

// If this isn't the main partition, that's all that we need to preserve.
Expand Down
12 changes: 6 additions & 6 deletions lld/ELF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ Symbol *SymbolTable::insert(StringRef name) {

// This variant of addSymbol is used by BinaryFile::parse to check duplicate
// symbol errors.
Symbol *SymbolTable::addAndCheckDuplicate(const Defined &newSym) {
Symbol *SymbolTable::addAndCheckDuplicate(Ctx &ctx, const Defined &newSym) {
Symbol *sym = insert(newSym.getName());
if (sym->isDefined())
sym->checkDuplicate(newSym);
sym->resolve(newSym);
sym->checkDuplicate(ctx, newSym);
sym->resolve(ctx, newSym);
sym->isUsedInRegularObj = true;
return sym;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ bool SymbolTable::assignExactVersion(SymbolVersion ver, uint16_t versionId,
for (Symbol *sym : syms) {
// For a non-local versionId, skip symbols containing version info because
// symbol versions specified by symbol names take precedence over version
// scripts. See parseSymbolVersion().
// scripts. See parseSymbolVersion(ctx).
if (!includeNonDefault && versionId != VER_NDX_LOCAL &&
sym->getName().contains('@'))
continue;
Expand Down Expand Up @@ -353,10 +353,10 @@ void SymbolTable::scanVersionScript() {
// Let them parse and update their names to exclude version suffix.
for (Symbol *sym : symVector)
if (sym->hasVersionSuffix)
sym->parseSymbolVersion();
sym->parseSymbolVersion(ctx);

// isPreemptible is false at this point. To correctly compute the binding of a
// Defined (which is used by includeInDynsym()), we need to know if it is
// Defined (which is used by includeInDynsym(ctx)), we need to know if it is
// VER_NDX_LOCAL or not. Compute symbol versions before handling
// --dynamic-list.
handleDynamicList();
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class SymbolTable {

template <typename T> Symbol *addSymbol(const T &newSym) {
Symbol *sym = insert(newSym.getName());
sym->resolve(newSym);
sym->resolve(ctx, newSym);
return sym;
}
Symbol *addAndCheckDuplicate(const Defined &newSym);
Symbol *addAndCheckDuplicate(Ctx &, const Defined &newSym);

void scanVersionScript();

Expand Down
Loading

0 comments on commit dbd1971

Please sign in to comment.