Skip to content

Commit 43e3871

Browse files
committed
[ELF] Make section member orders consistent
SectionBase, InputSectionBase, InputSection, MergeInputSection, and OutputSection have different member orders. Make them consistent and adopt the order similar to the raw Elf64_Shdr.
1 parent 3cecf17 commit 43e3871

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

lld/ELF/Arch/PPC64.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ static void writeSequence(Ctx &ctx, const char *prefix, int from,
288288
// The full section content has the extent of [begin, end). We drop unused
289289
// instructions and write [first,end).
290290
auto *sec = make<InputSection>(
291-
ctx.internalFile, SHF_ALLOC, SHT_PROGBITS, 4,
291+
ctx.internalFile, ".text", SHT_PROGBITS, SHF_ALLOC, /*addralign=*/4,
292+
/*entsize=*/0,
292293
ArrayRef(reinterpret_cast<uint8_t *>(buf.data() + first),
293-
4 * (buf.size() - first)),
294-
".text");
294+
4 * (buf.size() - first)));
295295
ctx.inputSections.push_back(sec);
296296
for (Defined *sym : defined) {
297297
sym->section = sec;

lld/ELF/InputFiles.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,9 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
903903
// simply handle such sections as non-mergeable ones. Degrading like this
904904
// is acceptable because section merging is optional.
905905
if (auto *ms = dyn_cast<MergeInputSection>(s)) {
906-
s = makeThreadLocal<InputSection>(
907-
ms->file, ms->flags, ms->type, ms->addralign,
908-
ms->contentMaybeDecompress(), ms->name);
906+
s = makeThreadLocal<InputSection>(ms->file, ms->name, ms->type,
907+
ms->flags, ms->addralign, 0,
908+
ms->contentMaybeDecompress());
909909
sections[info] = s;
910910
}
911911

@@ -1849,8 +1849,9 @@ void BitcodeFile::postParse() {
18491849

18501850
void BinaryFile::parse() {
18511851
ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());
1852-
auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1853-
8, data, ".data");
1852+
auto *section =
1853+
make<InputSection>(this, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
1854+
/*addralign=*/8, /*entsize=*/0, data);
18541855
sections.push_back(section);
18551856

18561857
// For each input file foo that is embedded to a result as a binary

lld/ELF/InputSection.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,
5252
return check(file.getObj().getSectionContents(hdr));
5353
}
5454

55-
InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
56-
uint32_t type, uint64_t entsize,
57-
uint32_t link, uint32_t info,
58-
uint32_t addralign, ArrayRef<uint8_t> data,
59-
StringRef name, Kind sectionKind)
60-
: SectionBase(sectionKind, file, name, flags, entsize, addralign, type,
61-
info, link),
55+
InputSectionBase::InputSectionBase(InputFile *file, StringRef name,
56+
uint32_t type, uint64_t flags, uint32_t link,
57+
uint32_t info, uint32_t addralign,
58+
uint32_t entsize, ArrayRef<uint8_t> data,
59+
Kind sectionKind)
60+
: SectionBase(sectionKind, file, name, type, flags, link, info, addralign,
61+
entsize),
6262
content_(data.data()), size(data.size()) {
6363
// In order to reduce memory allocation, we assume that mergeable
6464
// sections are smaller than 4 GiB, which is not an unreasonable
@@ -95,10 +95,10 @@ template <class ELFT>
9595
InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
9696
const typename ELFT::Shdr &hdr,
9797
StringRef name, Kind sectionKind)
98-
: InputSectionBase(&file, getFlags(file.ctx, hdr.sh_flags), hdr.sh_type,
99-
hdr.sh_entsize, hdr.sh_link, hdr.sh_info,
100-
hdr.sh_addralign, getSectionContents(file, hdr), name,
101-
sectionKind) {
98+
: InputSectionBase(&file, name, hdr.sh_type,
99+
getFlags(file.ctx, hdr.sh_flags), hdr.sh_link,
100+
hdr.sh_info, hdr.sh_addralign, hdr.sh_entsize,
101+
getSectionContents(file, hdr), sectionKind) {
102102
// We reject object files having insanely large alignments even though
103103
// they are allowed by the spec. I think 4GB is a reasonable limitation.
104104
// We might want to relax this in the future.
@@ -355,18 +355,19 @@ std::string InputSectionBase::getObjMsg(uint64_t off) const {
355355

356356
PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source,
357357
InputSectionDescription &isd)
358-
: InputSection(source.file, source.flags, source.type, source.addralign, {},
359-
source.name, SectionBase::Spill),
358+
: InputSection(source.file, source.name, source.type, source.flags,
359+
source.addralign, source.addralign, {}, SectionBase::Spill),
360360
isd(&isd) {}
361361

362-
InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
362+
InputSection InputSection::discarded(nullptr, "", 0, 0, 0, 0,
363+
ArrayRef<uint8_t>());
363364

364-
InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type,
365-
uint32_t addralign, ArrayRef<uint8_t> data,
366-
StringRef name, Kind k)
367-
: InputSectionBase(f, flags, type,
368-
/*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, addralign, data,
369-
name, k) {
365+
InputSection::InputSection(InputFile *f, StringRef name, uint32_t type,
366+
uint64_t flags, uint32_t addralign, uint32_t entsize,
367+
ArrayRef<uint8_t> data, Kind k)
368+
: InputSectionBase(f, name, type, flags,
369+
/*link=*/0, /*info=*/0, addralign, /*entsize=*/entsize,
370+
data, k) {
370371
assert(f || this == &InputSection::discarded);
371372
}
372373

@@ -1437,12 +1438,13 @@ MergeInputSection::MergeInputSection(ObjFile<ELFT> &f,
14371438
StringRef name)
14381439
: InputSectionBase(f, header, name, InputSectionBase::Merge) {}
14391440

1440-
MergeInputSection::MergeInputSection(Ctx &ctx, uint64_t flags, uint32_t type,
1441-
uint64_t entsize, ArrayRef<uint8_t> data,
1442-
StringRef name)
1443-
: InputSectionBase(ctx.internalFile, flags, type, entsize, /*link=*/0,
1441+
MergeInputSection::MergeInputSection(Ctx &ctx, StringRef name, uint32_t type,
1442+
uint64_t flags, uint64_t entsize,
1443+
ArrayRef<uint8_t> data)
1444+
: InputSectionBase(ctx.internalFile, name, type, flags, /*link=*/0,
14441445
/*info=*/0,
1445-
/*alignment=*/entsize, data, name, SectionBase::Merge) {}
1446+
/*addralign=*/entsize, entsize, data,
1447+
SectionBase::Merge) {}
14461448

14471449
// This function is called after we obtain a complete list of input sections
14481450
// that need to be linked. This is responsible to split section contents

lld/ELF/InputSection.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ class SectionBase {
9393

9494
// These corresponds to the fields in Elf_Shdr.
9595
uint64_t flags;
96-
uint32_t addralign;
97-
uint32_t entsize;
9896
uint32_t link;
9997
uint32_t info;
98+
uint32_t addralign;
99+
uint32_t entsize;
100100

101101
Ctx &getCtx() const;
102102
OutputSection *getOutputSection();
@@ -116,11 +116,11 @@ class SectionBase {
116116

117117
protected:
118118
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)
119+
uint32_t type, uint64_t flags, uint32_t link,
120+
uint32_t info, uint32_t addralign, uint32_t entsize)
121121
: sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
122-
file(file), name(name), flags(flags), addralign(addralign),
123-
entsize(entsize), link(link), info(info) {}
122+
file(file), name(name), flags(flags), link(link), info(info),
123+
addralign(addralign), entsize(entsize) {}
124124
};
125125

126126
struct SymbolAnchor {
@@ -148,9 +148,9 @@ class InputSectionBase : public SectionBase {
148148
InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
149149
StringRef name, Kind sectionKind);
150150

151-
InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
152-
uint64_t entsize, uint32_t link, uint32_t info,
153-
uint32_t addralign, ArrayRef<uint8_t> data, StringRef name,
151+
InputSectionBase(InputFile *file, StringRef name, uint32_t type,
152+
uint64_t flags, uint32_t link, uint32_t info,
153+
uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
154154
Kind sectionKind);
155155

156156
static bool classof(const SectionBase *s) {
@@ -315,8 +315,8 @@ class MergeInputSection : public InputSectionBase {
315315
template <class ELFT>
316316
MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
317317
StringRef name);
318-
MergeInputSection(Ctx &, uint64_t flags, uint32_t type, uint64_t entsize,
319-
ArrayRef<uint8_t> data, StringRef name);
318+
MergeInputSection(Ctx &, StringRef name, uint32_t type, uint64_t flags,
319+
uint64_t entsize, ArrayRef<uint8_t> data);
320320

321321
static bool classof(const SectionBase *s) { return s->kind() == Merge; }
322322
void splitIntoPieces();
@@ -394,8 +394,9 @@ class EhInputSection : public InputSectionBase {
394394
// .eh_frame. It also includes the synthetic sections themselves.
395395
class InputSection : public InputSectionBase {
396396
public:
397-
InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign,
398-
ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
397+
InputSection(InputFile *f, StringRef name, uint32_t type, uint64_t flags,
398+
uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
399+
Kind k = Regular);
399400
template <class ELFT>
400401
InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
401402
StringRef name);
@@ -473,8 +474,8 @@ class SyntheticSection : public InputSection {
473474
Ctx &ctx;
474475
SyntheticSection(Ctx &ctx, uint64_t flags, uint32_t type, uint32_t addralign,
475476
StringRef name)
476-
: InputSection(ctx.internalFile, flags, type, addralign, {}, name,
477-
InputSectionBase::Synthetic),
477+
: InputSection(ctx.internalFile, name, type, flags, addralign,
478+
/*entsize=*/0, {}, InputSectionBase::Synthetic),
478479
ctx(ctx) {}
479480

480481
virtual ~SyntheticSection() = default;

lld/ELF/OutputSections.cpp

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

6868
OutputSection::OutputSection(Ctx &ctx, StringRef name, uint32_t type,
6969
uint64_t flags)
70-
: SectionBase(Output, ctx.internalFile, name, flags, /*entsize=*/0,
71-
/*addralign=*/1, type,
72-
/*info=*/0, /*link=*/0),
70+
: SectionBase(Output, ctx.internalFile, name, type, flags, /*link=*/0,
71+
/*info=*/0, /*addralign=*/1, /*entsize=*/0),
7372
ctx(ctx) {}
7473

7574
uint64_t OutputSection::getLMA() const {

lld/ELF/SyntheticSections.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static ArrayRef<uint8_t> getVersion(Ctx &ctx) {
8989
// The returned object is a mergeable string section.
9090
MergeInputSection *elf::createCommentSection(Ctx &ctx) {
9191
auto *sec =
92-
make<MergeInputSection>(ctx, SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
93-
getVersion(ctx), ".comment");
92+
make<MergeInputSection>(ctx, ".comment", SHT_PROGBITS,
93+
SHF_MERGE | SHF_STRINGS, 1, getVersion(ctx));
9494
sec->splitIntoPieces();
9595
return sec;
9696
}
@@ -273,8 +273,9 @@ InputSection *elf::createInterpSection(Ctx &ctx) {
273273
StringRef s = ctx.saver.save(ctx.arg.dynamicLinker);
274274
ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
275275

276-
return make<InputSection>(ctx.internalFile, SHF_ALLOC, SHT_PROGBITS, 1,
277-
contents, ".interp");
276+
return make<InputSection>(ctx.internalFile, ".interp", SHT_PROGBITS,
277+
SHF_ALLOC,
278+
/*addralign=*/1, /*entsize=*/0, contents);
278279
}
279280

280281
Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,

0 commit comments

Comments
 (0)