Skip to content

Commit c22588c

Browse files
committed
[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.
1 parent 15de239 commit c22588c

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

lld/ELF/EhFrame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void EhReader::skipAugP() {
119119
uint8_t enc = readByte();
120120
if ((enc & 0xf0) == DW_EH_PE_aligned)
121121
failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
122-
size_t size = getAugPSize(ctx, enc);
122+
size_t size = getAugPSize(isec->getCtx(), enc);
123123
if (size == 0)
124124
failOn(d.data() - 1, "unknown FDE encoding");
125125
if (size >= d.size())

lld/ELF/InputFiles.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ void parseFiles(Ctx &, const std::vector<InputFile *> &files);
4848

4949
// The root class of input files.
5050
class InputFile {
51-
protected:
51+
public:
5252
Ctx &ctx;
53+
54+
protected:
5355
std::unique_ptr<Symbol *[]> symbols;
5456
uint32_t numSymbols = 0;
5557
SmallVector<InputSectionBase *, 0> sections;

lld/ELF/InputSection.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
5252
uint32_t link, uint32_t info,
5353
uint32_t addralign, ArrayRef<uint8_t> data,
5454
StringRef name, Kind sectionKind)
55-
: SectionBase(sectionKind, name, flags, entsize, addralign, type, info,
56-
link),
57-
file(file), content_(data.data()), size(data.size()) {
55+
: SectionBase(sectionKind, file, name, flags, entsize, addralign, type,
56+
info, link),
57+
content_(data.data()), size(data.size()) {
5858
// In order to reduce memory allocation, we assume that mergeable
5959
// sections are smaller than 4 GiB, which is not an unreasonable
6060
// assumption as of 2017.
@@ -88,7 +88,7 @@ template <class ELFT>
8888
InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
8989
const typename ELFT::Shdr &hdr,
9090
StringRef name, Kind sectionKind)
91-
: InputSectionBase(&file, getFlags(ctx, hdr.sh_flags), hdr.sh_type,
91+
: InputSectionBase(&file, getFlags(file.ctx, hdr.sh_flags), hdr.sh_type,
9292
hdr.sh_entsize, hdr.sh_link, hdr.sh_info,
9393
hdr.sh_addralign, getSectionContents(file, hdr), name,
9494
sectionKind) {
@@ -185,6 +185,8 @@ RelsOrRelas<ELFT> InputSectionBase::relsOrRelas(bool supportsCrel) const {
185185
return ret;
186186
}
187187

188+
Ctx &SectionBase::getCtx() const { return file->ctx; }
189+
188190
uint64_t SectionBase::getOffset(uint64_t offset) const {
189191
switch (kind()) {
190192
case Output: {

lld/ELF/InputSection.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class SectionBase {
7878

7979
uint8_t partition = 1;
8080
uint32_t type;
81+
82+
// The file which contains this section. For InputSectionBase, its dynamic
83+
// type is usually ObjFile<ELFT>, but may be an InputFile of InternalKind
84+
// (for a synthetic section).
85+
InputFile *file;
86+
8187
StringRef name;
8288

8389
// The 1-indexed partition that this section is assigned to by the garbage
@@ -92,6 +98,7 @@ class SectionBase {
9298
uint32_t link;
9399
uint32_t info;
94100

101+
Ctx &getCtx() const;
95102
OutputSection *getOutputSection();
96103
const OutputSection *getOutputSection() const {
97104
return const_cast<SectionBase *>(this)->getOutputSection();
@@ -108,12 +115,12 @@ class SectionBase {
108115
void markDead() { partition = 0; }
109116

110117
protected:
111-
constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
112-
uint32_t entsize, uint32_t addralign, uint32_t type,
113-
uint32_t info, uint32_t link)
118+
constexpr SectionBase(Kind sectionKind, InputFile *file, StringRef name,
119+
uint64_t flags, uint32_t entsize, uint32_t addralign,
120+
uint32_t type, uint32_t info, uint32_t link)
114121
: sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
115-
name(name), flags(flags), addralign(addralign), entsize(entsize),
116-
link(link), info(info) {}
122+
file(file), name(name), flags(flags), addralign(addralign),
123+
entsize(entsize), link(link), info(info) {}
117124
};
118125

119126
struct SymbolAnchor {
@@ -150,11 +157,6 @@ class InputSectionBase : public SectionBase {
150157
return s->kind() != Output && s->kind() != Class;
151158
}
152159

153-
// The file which contains this section. Its dynamic type is usually
154-
// ObjFile<ELFT>, but may be an InputFile of InternalKind (for a synthetic
155-
// section).
156-
InputFile *file;
157-
158160
// Input sections are part of an output section. Special sections
159161
// like .eh_frame and merge sections are first combined into a
160162
// synthetic section that is then added to an output section. In all

lld/ELF/OutputSections.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
6666
}
6767

6868
OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
69-
: SectionBase(Output, name, flags, /*Entsize*/ 0, /*Alignment*/ 1, type,
70-
/*Info*/ 0, /*Link*/ 0) {}
69+
: SectionBase(Output, ctx.internalFile, name, flags, /*entsize=*/0,
70+
/*addralign=*/1, type,
71+
/*info=*/0, /*link=*/0) {}
7172

7273
// We allow sections of types listed below to merged into a
7374
// single progbits section. This is typically done by linker

lld/ELF/OutputSections.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ struct SectionClass final : public SectionBase {
150150
SmallVector<InputSectionDescription *, 0> commands;
151151
bool assigned = false;
152152

153-
SectionClass(StringRef name) : SectionBase(Class, name, 0, 0, 0, 0, 0, 0) {}
153+
SectionClass(StringRef name)
154+
: SectionBase(Class, nullptr, name, 0, 0, 0, 0, 0, 0) {}
154155
static bool classof(const SectionBase *s) { return s->kind() == Class; }
155156
};
156157

0 commit comments

Comments
 (0)