[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.
This commit is contained in:
Fangrui Song 2024-10-10 22:15:10 -07:00
parent 15de239406
commit c22588c7cd
6 changed files with 27 additions and 19 deletions

View File

@ -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())

View File

@ -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;

View File

@ -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: {

View File

@ -78,6 +78,12 @@ public:
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 @@ public:
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 @@ public:
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 @@ public:
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

View File

@ -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

View File

@ -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; }
};