Skip to content

Commit

Permalink
[ELF] Move getSymbol/getRelocTargetSym from ObjFile<ELFT> to InputFil…
Browse files Browse the repository at this point in the history
…e. NFC

This removes lots of unneeded `template getFile<ELFT>()`.
  • Loading branch information
MaskRay committed Mar 11, 2024
1 parent b7f97d3 commit f645560
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ addTaggedSymbolReferences(InputSectionBase &sec,
error("non-RELA relocations are not allowed with memtag globals");

for (const typename ELFT::Rela &rel : rels.relas) {
Symbol &sym = sec.getFile<ELFT>()->getRelocTargetSym(rel);
Symbol &sym = sec.file->getRelocTargetSym(rel);
// Linker-synthesized symbols such as __executable_start may be referenced
// as tagged in input objfiles, and we don't want them to be tagged. A
// cheap way to exclude them is the type check, but their type is
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Arch/PPC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ getRelaTocSymAndAddend(InputSectionBase *tocSec, uint64_t offset) {
uint64_t index = std::min<uint64_t>(offset / 8, relas.size() - 1);
for (;;) {
if (relas[index].r_offset == offset) {
Symbol &sym = tocSec->getFile<ELFT>()->getRelocTargetSym(relas[index]);
Symbol &sym = tocSec->file->getRelocTargetSym(relas[index]);
return {dyn_cast<Defined>(&sym), getAddend<ELFT>(relas[index])};
}
if (relas[index].r_offset < offset || index == 0)
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2302,9 +2302,9 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
Symbol *sym;
const RelsOrRelas<ELFT> rels = s->template relsOrRelas<ELFT>();
if (rels.areRelocsRel())
sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.rels[0]);
sym = &s->file->getRelocTargetSym(rels.rels[0]);
else
sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.relas[0]);
sym = &s->file->getRelocTargetSym(rels.relas[0]);
if (!isa<Defined>(sym) || !sym->includeInDynsym())
return;

Expand Down
16 changes: 8 additions & 8 deletions lld/ELF/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
uint64_t addA = getAddend<ELFT>(ra[i]);
uint64_t addB = getAddend<ELFT>(rb[i]);

Symbol &sa = secA->template getFile<ELFT>()->getRelocTargetSym(ra[i]);
Symbol &sb = secB->template getFile<ELFT>()->getRelocTargetSym(rb[i]);
Symbol &sa = secA->file->getRelocTargetSym(ra[i]);
Symbol &sb = secB->file->getRelocTargetSym(rb[i]);
if (&sa == &sb) {
if (addA == addB)
continue;
Expand Down Expand Up @@ -338,8 +338,8 @@ bool ICF<ELFT>::variableEq(const InputSection *secA, ArrayRef<RelTy> ra,

for (size_t i = 0; i < ra.size(); ++i) {
// The two sections must be identical.
Symbol &sa = secA->template getFile<ELFT>()->getRelocTargetSym(ra[i]);
Symbol &sb = secB->template getFile<ELFT>()->getRelocTargetSym(rb[i]);
Symbol &sa = secA->file->getRelocTargetSym(ra[i]);
Symbol &sb = secB->file->getRelocTargetSym(rb[i]);
if (&sa == &sb)
continue;

Expand Down Expand Up @@ -437,12 +437,12 @@ void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> fn) {

// Combine the hashes of the sections referenced by the given section into its
// hash.
template <class ELFT, class RelTy>
template <class RelTy>
static void combineRelocHashes(unsigned cnt, InputSection *isec,
ArrayRef<RelTy> rels) {
uint32_t hash = isec->eqClass[cnt % 2];
for (RelTy rel : rels) {
Symbol &s = isec->template getFile<ELFT>()->getRelocTargetSym(rel);
Symbol &s = isec->file->getRelocTargetSym(rel);
if (auto *d = dyn_cast<Defined>(&s))
if (auto *relSec = dyn_cast_or_null<InputSection>(d->section))
hash += relSec->eqClass[cnt % 2];
Expand Down Expand Up @@ -504,9 +504,9 @@ template <class ELFT> void ICF<ELFT>::run() {
parallelForEach(sections, [&](InputSection *s) {
const RelsOrRelas<ELFT> rels = s->template relsOrRelas<ELFT>();
if (rels.areRelocsRel())
combineRelocHashes<ELFT>(cnt, s, rels.rels);
combineRelocHashes(cnt, s, rels.rels);
else
combineRelocHashes<ELFT>(cnt, s, rels.relas);
combineRelocHashes(cnt, s, rels.relas);
});
}

Expand Down
23 changes: 12 additions & 11 deletions lld/ELF/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ class InputFile {
return {symbols.get(), numSymbols};
}

Symbol &getSymbol(uint32_t symbolIndex) const {
assert(fileKind == ObjKind);
if (symbolIndex >= numSymbols)
fatal(toString(this) + ": invalid symbol index");
return *this->symbols[symbolIndex];
}

template <typename RelT> Symbol &getRelocTargetSym(const RelT &rel) const {
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
return getSymbol(symIndex);
}

// Get filename to use for linker script processing.
StringRef getNameForScript() const;

Expand Down Expand Up @@ -242,19 +254,8 @@ template <class ELFT> class ObjFile : public ELFFileBase {
StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
const Elf_Shdr &sec);

Symbol &getSymbol(uint32_t symbolIndex) const {
if (symbolIndex >= numSymbols)
fatal(toString(this) + ": invalid symbol index");
return *this->symbols[symbolIndex];
}

uint32_t getSectionIndex(const Elf_Sym &sym) const;

template <typename RelT> Symbol &getRelocTargetSym(const RelT &rel) const {
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
return getSymbol(symIndex);
}

std::optional<llvm::DILineInfo> getDILineInfo(const InputSectionBase *,
uint64_t);
std::optional<std::pair<std::string, unsigned>>
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
if (!RelTy::IsRela)
addend += target.getImplicitAddend(bufLoc, type);

Symbol &sym = getFile<ELFT>()->getRelocTargetSym(rel);
Symbol &sym = this->file->getRelocTargetSym(rel);
RelExpr expr = target.getRelExpr(type, sym, bufLoc);
if (expr == R_NONE)
continue;
Expand All @@ -939,7 +939,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
val = *tombstone;
} else {
val = sym.getVA(addend) -
(getFile<ELFT>()->getRelocTargetSym(rels[i]).getVA(0) +
(this->file->getRelocTargetSym(rels[i]).getVA(0) +
getAddend<ELFT>(rels[i]));
}
if (overwriteULEB128(bufLoc, val) >= 0x80)
Expand Down
3 changes: 1 addition & 2 deletions lld/ELF/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ template <class ELFT>
template <class RelTy>
void MarkLive<ELFT>::resolveReloc(InputSectionBase &sec, RelTy &rel,
bool fromFDE) {
Symbol &sym = sec.getFile<ELFT>()->getRelocTargetSym(rel);

// If a symbol is referenced in a live section, it is used.
Symbol &sym = sec.file->getRelocTargetSym(rel);
sym.used = true;

if (auto *d = dyn_cast<Defined>(&sym)) {
Expand Down
5 changes: 2 additions & 3 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
Symbol *personality = nullptr;
unsigned firstRelI = cie.firstRelocation;
if (firstRelI != (unsigned)-1)
personality =
&cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
personality = &cie.sec->file->getRelocTargetSym(rels[firstRelI]);

// Search for an existing CIE by CIE contents/relocation target pair.
CieRecord *&rec = cieMap[{cie.data(), personality}];
Expand Down Expand Up @@ -396,7 +395,7 @@ Defined *EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
return nullptr;

const RelTy &rel = rels[firstRelI];
Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
Symbol &b = sec->file->getRelocTargetSym(rel);

// FDEs for garbage-collected or merged-by-ICF sections, or sections in
// another partition, are dead.
Expand Down

0 comments on commit f645560

Please sign in to comment.