[lld] f645560 - [ELF] Move getSymbol/getRelocTargetSym from ObjFile<ELFT> to InputFile. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 10 23:01:30 PDT 2024
Author: Fangrui Song
Date: 2024-03-10T23:01:26-07:00
New Revision: f6455606bbbb02bbc155a713ae07eab1c7419041
URL: https://github.com/llvm/llvm-project/commit/f6455606bbbb02bbc155a713ae07eab1c7419041
DIFF: https://github.com/llvm/llvm-project/commit/f6455606bbbb02bbc155a713ae07eab1c7419041.diff
LOG: [ELF] Move getSymbol/getRelocTargetSym from ObjFile<ELFT> to InputFile. NFC
This removes lots of unneeded `template getFile<ELFT>()`.
Added:
Modified:
lld/ELF/Arch/AArch64.cpp
lld/ELF/Arch/PPC64.cpp
lld/ELF/Driver.cpp
lld/ELF/ICF.cpp
lld/ELF/InputFiles.h
lld/ELF/InputSection.cpp
lld/ELF/MarkLive.cpp
lld/ELF/SyntheticSections.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 71a1b1111e4298..30ccd68f7b7506 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -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
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 019c073bd541b6..657332deebfde1 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -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)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 24faa1753f1e3d..de4b2e345ac917 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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;
diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index 9d7251037fb6d6..2551c2e807b73f 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -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;
@@ -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;
@@ -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];
@@ -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);
});
}
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 0cbe00aa396acb..54de842a81cf35 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -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;
@@ -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>>
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index e033a715b59214..7508a1336c91a4 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -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;
@@ -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)
diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp
index 0073ed42112a85..93c66e81d2fa91 100644
--- a/lld/ELF/MarkLive.cpp
+++ b/lld/ELF/MarkLive.cpp
@@ -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)) {
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 66b3e835cabc57..206fb0f5376664 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -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}];
@@ -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.
More information about the llvm-commits
mailing list