[lld] c22588c - [ELF] Move InputSectionBase::file to SectionBase
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 22:15:16 PDT 2024
Author: Fangrui Song
Date: 2024-10-10T22:15:10-07:00
New Revision: c22588c7cdc5a82afd825ce90f21f922dedee98b
URL: https://github.com/llvm/llvm-project/commit/c22588c7cdc5a82afd825ce90f21f922dedee98b
DIFF: https://github.com/llvm/llvm-project/commit/c22588c7cdc5a82afd825ce90f21f922dedee98b.diff
LOG: [ELF] Move InputSectionBase::file to SectionBase
... and add getCtx (file->ctx). This allows InputSectionBase and
OutputSection to access ctx without taking an extra function argument.
Added:
Modified:
lld/ELF/EhFrame.cpp
lld/ELF/InputFiles.h
lld/ELF/InputSection.cpp
lld/ELF/InputSection.h
lld/ELF/OutputSections.cpp
lld/ELF/OutputSections.h
Removed:
################################################################################
diff --git a/lld/ELF/EhFrame.cpp b/lld/ELF/EhFrame.cpp
index d2d0e62e97ec45..f4c788fe610ae6 100644
--- a/lld/ELF/EhFrame.cpp
+++ b/lld/ELF/EhFrame.cpp
@@ -119,7 +119,7 @@ void EhReader::skipAugP() {
uint8_t enc = readByte();
if ((enc & 0xf0) == DW_EH_PE_aligned)
failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
- size_t size = getAugPSize(ctx, enc);
+ size_t size = getAugPSize(isec->getCtx(), enc);
if (size == 0)
failOn(d.data() - 1, "unknown FDE encoding");
if (size >= d.size())
diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index 0b54f92d1a2669..f80413b215047d 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -48,8 +48,10 @@ void parseFiles(Ctx &, const std::vector<InputFile *> &files);
// The root class of input files.
class InputFile {
-protected:
+public:
Ctx &ctx;
+
+protected:
std::unique_ptr<Symbol *[]> symbols;
uint32_t numSymbols = 0;
SmallVector<InputSectionBase *, 0> sections;
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 0885815a22a14a..90716f4f3675cc 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -52,9 +52,9 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
uint32_t link, uint32_t info,
uint32_t addralign, ArrayRef<uint8_t> data,
StringRef name, Kind sectionKind)
- : SectionBase(sectionKind, name, flags, entsize, addralign, type, info,
- link),
- file(file), content_(data.data()), size(data.size()) {
+ : SectionBase(sectionKind, file, name, flags, entsize, addralign, type,
+ info, link),
+ content_(data.data()), size(data.size()) {
// In order to reduce memory allocation, we assume that mergeable
// sections are smaller than 4 GiB, which is not an unreasonable
// assumption as of 2017.
@@ -88,7 +88,7 @@ template <class ELFT>
InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
const typename ELFT::Shdr &hdr,
StringRef name, Kind sectionKind)
- : InputSectionBase(&file, getFlags(ctx, hdr.sh_flags), hdr.sh_type,
+ : InputSectionBase(&file, getFlags(file.ctx, hdr.sh_flags), hdr.sh_type,
hdr.sh_entsize, hdr.sh_link, hdr.sh_info,
hdr.sh_addralign, getSectionContents(file, hdr), name,
sectionKind) {
@@ -185,6 +185,8 @@ RelsOrRelas<ELFT> InputSectionBase::relsOrRelas(bool supportsCrel) const {
return ret;
}
+Ctx &SectionBase::getCtx() const { return file->ctx; }
+
uint64_t SectionBase::getOffset(uint64_t offset) const {
switch (kind()) {
case Output: {
diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index bff9ec324d9bc5..8f69a957e11d7a 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -78,6 +78,12 @@ class SectionBase {
uint8_t partition = 1;
uint32_t type;
+
+ // The file which contains this section. For InputSectionBase, its dynamic
+ // type is usually ObjFile<ELFT>, but may be an InputFile of InternalKind
+ // (for a synthetic section).
+ InputFile *file;
+
StringRef name;
// The 1-indexed partition that this section is assigned to by the garbage
@@ -92,6 +98,7 @@ class SectionBase {
uint32_t link;
uint32_t info;
+ Ctx &getCtx() const;
OutputSection *getOutputSection();
const OutputSection *getOutputSection() const {
return const_cast<SectionBase *>(this)->getOutputSection();
@@ -108,12 +115,12 @@ class SectionBase {
void markDead() { partition = 0; }
protected:
- constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
- uint32_t entsize, uint32_t addralign, uint32_t type,
- uint32_t info, uint32_t link)
+ constexpr SectionBase(Kind sectionKind, InputFile *file, StringRef name,
+ uint64_t flags, uint32_t entsize, uint32_t addralign,
+ uint32_t type, uint32_t info, uint32_t link)
: sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
- name(name), flags(flags), addralign(addralign), entsize(entsize),
- link(link), info(info) {}
+ file(file), name(name), flags(flags), addralign(addralign),
+ entsize(entsize), link(link), info(info) {}
};
struct SymbolAnchor {
@@ -150,11 +157,6 @@ class InputSectionBase : public SectionBase {
return s->kind() != Output && s->kind() != Class;
}
- // The file which contains this section. Its dynamic type is usually
- // ObjFile<ELFT>, but may be an InputFile of InternalKind (for a synthetic
- // section).
- InputFile *file;
-
// Input sections are part of an output section. Special sections
// like .eh_frame and merge sections are first combined into a
// synthetic section that is then added to an output section. In all
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 408dbdc43d5481..864c30ca790508 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -66,8 +66,9 @@ void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
}
OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
- : SectionBase(Output, name, flags, /*Entsize*/ 0, /*Alignment*/ 1, type,
- /*Info*/ 0, /*Link*/ 0) {}
+ : SectionBase(Output, ctx.internalFile, name, flags, /*entsize=*/0,
+ /*addralign=*/1, type,
+ /*info=*/0, /*link=*/0) {}
// We allow sections of types listed below to merged into a
// single progbits section. This is typically done by linker
diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h
index 904206b20bc1cb..11977507e9268e 100644
--- a/lld/ELF/OutputSections.h
+++ b/lld/ELF/OutputSections.h
@@ -150,7 +150,8 @@ struct SectionClass final : public SectionBase {
SmallVector<InputSectionDescription *, 0> commands;
bool assigned = false;
- SectionClass(StringRef name) : SectionBase(Class, name, 0, 0, 0, 0, 0, 0) {}
+ SectionClass(StringRef name)
+ : SectionBase(Class, nullptr, name, 0, 0, 0, 0, 0, 0) {}
static bool classof(const SectionBase *s) { return s->kind() == Class; }
};
More information about the llvm-commits
mailing list