Skip to content

Commit 099a52f

Browse files
committed
[ELF] Reorder SectionBase/InputSectionBase members
Move `sectionKind` outside the bitfield and move bss/keepUnique to InputSectionBase. * sizeof(InputSection) decreases from 160 to 152 on 64-bit systems. * The numerous `sectionKind` accesses are faster.
1 parent a5af621 commit 099a52f

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

lld/ELF/Driver.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,8 +2375,9 @@ static void markAddrsig(bool icfSafe, Symbol *s) {
23752375
// We don't need to keep text sections unique under --icf=all even if they
23762376
// are address-significant.
23772377
if (auto *d = dyn_cast_or_null<Defined>(s))
2378-
if (d->section && (icfSafe || !(d->section->flags & SHF_EXECINSTR)))
2379-
d->section->keepUnique = true;
2378+
if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
2379+
if (icfSafe || !(sec->flags & SHF_EXECINSTR))
2380+
sec->keepUnique = true;
23802381
}
23812382

23822383
// Record sections that define symbols mentioned in --keep-unique <symbol>
@@ -2391,7 +2392,8 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
23912392
Warn(ctx) << "could not find symbol " << name << " to keep unique";
23922393
continue;
23932394
}
2394-
d->section->keepUnique = true;
2395+
if (auto *sec = dyn_cast<InputSectionBase>(d->section))
2396+
sec->keepUnique = true;
23952397
}
23962398

23972399
// --icf=all --ignore-data-address-equality means that we can ignore

lld/ELF/InputSection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ InputSectionBase::InputSectionBase(InputFile *file, StringRef name,
5959
Kind sectionKind)
6060
: SectionBase(sectionKind, file, name, type, flags, link, info, addralign,
6161
entsize),
62+
bss(0), decodedCrel(0), keepUnique(0), nopFiller(0),
6263
content_(data.data()), size(data.size()) {
6364
// In order to reduce memory allocation, we assume that mergeable
6465
// sections are smaller than 4 GiB, which is not an unreasonable

lld/ELF/InputSection.h

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,17 @@ template <class ELFT> struct RelsOrRelas {
5959
// sections.
6060
class SectionBase {
6161
public:
62-
enum Kind { Regular, Synthetic, Spill, EHFrame, Merge, Output, Class };
63-
64-
Kind kind() const { return (Kind)sectionKind; }
65-
66-
LLVM_PREFERRED_TYPE(Kind)
67-
uint8_t sectionKind : 3;
68-
69-
// The next two bit fields are only used by InputSectionBase, but we
70-
// put them here so the struct packs better.
71-
72-
LLVM_PREFERRED_TYPE(bool)
73-
uint8_t bss : 1;
74-
75-
// Set for sections that should not be folded by ICF.
76-
LLVM_PREFERRED_TYPE(bool)
77-
uint8_t keepUnique : 1;
62+
enum Kind : uint8_t {
63+
Regular,
64+
Synthetic,
65+
Spill,
66+
EHFrame,
67+
Merge,
68+
Output,
69+
Class,
70+
};
7871

79-
uint8_t partition = 1;
80-
uint32_t type;
72+
Kind kind() const { return sectionKind; }
8173

8274
// The file which contains this section. For InputSectionBase, its dynamic
8375
// type is usually ObjFile<ELFT>, but may be an InputFile of InternalKind
@@ -93,11 +85,18 @@ class SectionBase {
9385

9486
// These corresponds to the fields in Elf_Shdr.
9587
uint64_t flags;
88+
uint32_t type;
9689
uint32_t link;
9790
uint32_t info;
9891
uint32_t addralign;
9992
uint32_t entsize;
10093

94+
Kind sectionKind;
95+
uint8_t partition = 1;
96+
97+
// The next two bit fields are only used by InputSectionBase, but we
98+
// put them here so the struct packs better.
99+
101100
Ctx &getCtx() const;
102101
OutputSection *getOutputSection();
103102
const OutputSection *getOutputSection() const {
@@ -118,9 +117,9 @@ class SectionBase {
118117
constexpr SectionBase(Kind sectionKind, InputFile *file, StringRef name,
119118
uint32_t type, uint64_t flags, uint32_t link,
120119
uint32_t info, uint32_t addralign, uint32_t entsize)
121-
: sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
122-
file(file), name(name), flags(flags), link(link), info(info),
123-
addralign(addralign), entsize(entsize) {}
120+
: file(file), name(name), flags(flags), type(type), link(link),
121+
info(info), addralign(addralign), entsize(entsize),
122+
sectionKind(sectionKind) {}
124123
};
125124

126125
struct SymbolAnchor {
@@ -157,6 +156,25 @@ class InputSectionBase : public SectionBase {
157156
return s->kind() != Output && s->kind() != Class;
158157
}
159158

159+
LLVM_PREFERRED_TYPE(bool)
160+
uint8_t bss : 1;
161+
162+
// Whether this section is SHT_CREL and has been decoded to RELA by
163+
// relsOrRelas.
164+
LLVM_PREFERRED_TYPE(bool)
165+
uint8_t decodedCrel : 1;
166+
167+
// Set for sections that should not be folded by ICF.
168+
LLVM_PREFERRED_TYPE(bool)
169+
uint8_t keepUnique : 1;
170+
171+
// Whether the section needs to be padded with a NOP filler due to
172+
// deleteFallThruJmpInsn.
173+
LLVM_PREFERRED_TYPE(bool)
174+
uint8_t nopFiller : 1;
175+
176+
mutable bool compressed = false;
177+
160178
// Input sections are part of an output section. Special sections
161179
// like .eh_frame and merge sections are first combined into a
162180
// synthetic section that is then added to an output section. In all
@@ -176,16 +194,6 @@ class InputSectionBase : public SectionBase {
176194
// be reset to zero after uses.
177195
uint32_t bytesDropped = 0;
178196

179-
mutable bool compressed = false;
180-
181-
// Whether this section is SHT_CREL and has been decoded to RELA by
182-
// relsOrRelas.
183-
bool decodedCrel = false;
184-
185-
// Whether the section needs to be padded with a NOP filler due to
186-
// deleteFallThruJmpInsn.
187-
bool nopFiller = false;
188-
189197
void drop_back(unsigned num) {
190198
assert(bytesDropped + num < 256);
191199
bytesDropped += num;
@@ -467,7 +475,7 @@ class PotentialSpillSection : public InputSection {
467475
}
468476
};
469477

470-
static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
478+
static_assert(sizeof(InputSection) <= 152, "InputSection is too big");
471479

472480
class SyntheticSection : public InputSection {
473481
public:

lld/ELF/MapFile.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ static std::vector<Defined *> getSymbols(Ctx &ctx) {
5959
for (Symbol *b : file->getSymbols())
6060
if (auto *dr = dyn_cast<Defined>(b))
6161
if (!dr->isSection() && dr->section && dr->section->isLive() &&
62-
(dr->file == file || dr->hasFlag(NEEDS_COPY) || dr->section->bss))
62+
(dr->file == file || dr->hasFlag(NEEDS_COPY) ||
63+
(isa<SyntheticSection>(dr->section) &&
64+
cast<SyntheticSection>(dr->section)->bss)))
6365
v.push_back(dr);
6466
return v;
6567
}

lld/ELF/SyntheticSections.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ class BssSection final : public SyntheticSection {
177177
bool isNeeded() const override { return size != 0; }
178178
size_t getSize() const override { return size; }
179179

180-
static bool classof(const SectionBase *s) { return s->bss; }
180+
static bool classof(const SectionBase *s) {
181+
return isa<SyntheticSection>(s) && cast<SyntheticSection>(s)->bss;
182+
}
181183
uint64_t size;
182184
};
183185

0 commit comments

Comments
 (0)