[lld] 43e3871 - [ELF] Make section member orders consistent
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 23 14:22:30 PST 2024
Author: Fangrui Song
Date: 2024-11-23T14:22:24-08:00
New Revision: 43e3871a327b8e2ff57e16b46d5bc44beb430d91
URL: https://github.com/llvm/llvm-project/commit/43e3871a327b8e2ff57e16b46d5bc44beb430d91
DIFF: https://github.com/llvm/llvm-project/commit/43e3871a327b8e2ff57e16b46d5bc44beb430d91.diff
LOG: [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.
Added:
Modified:
lld/ELF/Arch/PPC64.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputSection.cpp
lld/ELF/InputSection.h
lld/ELF/OutputSections.cpp
lld/ELF/SyntheticSections.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 4dc9f93f5c6880..8dd1735ee1e886 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -288,10 +288,10 @@ static void writeSequence(Ctx &ctx, const char *prefix, int from,
// The full section content has the extent of [begin, end). We drop unused
// instructions and write [first,end).
auto *sec = make<InputSection>(
- ctx.internalFile, SHF_ALLOC, SHT_PROGBITS, 4,
+ ctx.internalFile, ".text", SHT_PROGBITS, SHF_ALLOC, /*addralign=*/4,
+ /*entsize=*/0,
ArrayRef(reinterpret_cast<uint8_t *>(buf.data() + first),
- 4 * (buf.size() - first)),
- ".text");
+ 4 * (buf.size() - first)));
ctx.inputSections.push_back(sec);
for (Defined *sym : defined) {
sym->section = sec;
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 3a0ae43b813f48..b89faa57985ceb 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -903,9 +903,9 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
// simply handle such sections as non-mergeable ones. Degrading like this
// is acceptable because section merging is optional.
if (auto *ms = dyn_cast<MergeInputSection>(s)) {
- s = makeThreadLocal<InputSection>(
- ms->file, ms->flags, ms->type, ms->addralign,
- ms->contentMaybeDecompress(), ms->name);
+ s = makeThreadLocal<InputSection>(ms->file, ms->name, ms->type,
+ ms->flags, ms->addralign, 0,
+ ms->contentMaybeDecompress());
sections[info] = s;
}
@@ -1849,8 +1849,9 @@ void BitcodeFile::postParse() {
void BinaryFile::parse() {
ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());
- auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
- 8, data, ".data");
+ auto *section =
+ make<InputSection>(this, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,
+ /*addralign=*/8, /*entsize=*/0, data);
sections.push_back(section);
// For each input file foo that is embedded to a result as a binary
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 1221f56dfe68a6..c24c3f35ed2c06 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -52,13 +52,13 @@ static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,
return check(file.getObj().getSectionContents(hdr));
}
-InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
- uint32_t type, uint64_t entsize,
- uint32_t link, uint32_t info,
- uint32_t addralign, ArrayRef<uint8_t> data,
- StringRef name, Kind sectionKind)
- : SectionBase(sectionKind, file, name, flags, entsize, addralign, type,
- info, link),
+InputSectionBase::InputSectionBase(InputFile *file, StringRef name,
+ uint32_t type, uint64_t flags, uint32_t link,
+ uint32_t info, uint32_t addralign,
+ uint32_t entsize, ArrayRef<uint8_t> data,
+ Kind sectionKind)
+ : SectionBase(sectionKind, file, name, type, flags, link, info, addralign,
+ entsize),
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
@@ -95,10 +95,10 @@ template <class ELFT>
InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
const typename ELFT::Shdr &hdr,
StringRef name, Kind sectionKind)
- : 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) {
+ : InputSectionBase(&file, name, hdr.sh_type,
+ getFlags(file.ctx, hdr.sh_flags), hdr.sh_link,
+ hdr.sh_info, hdr.sh_addralign, hdr.sh_entsize,
+ getSectionContents(file, hdr), sectionKind) {
// We reject object files having insanely large alignments even though
// they are allowed by the spec. I think 4GB is a reasonable limitation.
// We might want to relax this in the future.
@@ -355,18 +355,19 @@ std::string InputSectionBase::getObjMsg(uint64_t off) const {
PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source,
InputSectionDescription &isd)
- : InputSection(source.file, source.flags, source.type, source.addralign, {},
- source.name, SectionBase::Spill),
+ : InputSection(source.file, source.name, source.type, source.flags,
+ source.addralign, source.addralign, {}, SectionBase::Spill),
isd(&isd) {}
-InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
+InputSection InputSection::discarded(nullptr, "", 0, 0, 0, 0,
+ ArrayRef<uint8_t>());
-InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type,
- uint32_t addralign, ArrayRef<uint8_t> data,
- StringRef name, Kind k)
- : InputSectionBase(f, flags, type,
- /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, addralign, data,
- name, k) {
+InputSection::InputSection(InputFile *f, StringRef name, uint32_t type,
+ uint64_t flags, uint32_t addralign, uint32_t entsize,
+ ArrayRef<uint8_t> data, Kind k)
+ : InputSectionBase(f, name, type, flags,
+ /*link=*/0, /*info=*/0, addralign, /*entsize=*/entsize,
+ data, k) {
assert(f || this == &InputSection::discarded);
}
@@ -1437,12 +1438,13 @@ MergeInputSection::MergeInputSection(ObjFile<ELFT> &f,
StringRef name)
: InputSectionBase(f, header, name, InputSectionBase::Merge) {}
-MergeInputSection::MergeInputSection(Ctx &ctx, uint64_t flags, uint32_t type,
- uint64_t entsize, ArrayRef<uint8_t> data,
- StringRef name)
- : InputSectionBase(ctx.internalFile, flags, type, entsize, /*link=*/0,
+MergeInputSection::MergeInputSection(Ctx &ctx, StringRef name, uint32_t type,
+ uint64_t flags, uint64_t entsize,
+ ArrayRef<uint8_t> data)
+ : InputSectionBase(ctx.internalFile, name, type, flags, /*link=*/0,
/*info=*/0,
- /*alignment=*/entsize, data, name, SectionBase::Merge) {}
+ /*addralign=*/entsize, entsize, data,
+ SectionBase::Merge) {}
// This function is called after we obtain a complete list of input sections
// that need to be linked. This is responsible to split section contents
diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h
index 303452fed60d86..5d6b104a78fbaa 100644
--- a/lld/ELF/InputSection.h
+++ b/lld/ELF/InputSection.h
@@ -93,10 +93,10 @@ class SectionBase {
// These corresponds to the fields in Elf_Shdr.
uint64_t flags;
- uint32_t addralign;
- uint32_t entsize;
uint32_t link;
uint32_t info;
+ uint32_t addralign;
+ uint32_t entsize;
Ctx &getCtx() const;
OutputSection *getOutputSection();
@@ -116,11 +116,11 @@ class SectionBase {
protected:
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)
+ uint32_t type, uint64_t flags, uint32_t link,
+ uint32_t info, uint32_t addralign, uint32_t entsize)
: sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
- file(file), name(name), flags(flags), addralign(addralign),
- entsize(entsize), link(link), info(info) {}
+ file(file), name(name), flags(flags), link(link), info(info),
+ addralign(addralign), entsize(entsize) {}
};
struct SymbolAnchor {
@@ -148,9 +148,9 @@ class InputSectionBase : public SectionBase {
InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
StringRef name, Kind sectionKind);
- InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
- uint64_t entsize, uint32_t link, uint32_t info,
- uint32_t addralign, ArrayRef<uint8_t> data, StringRef name,
+ InputSectionBase(InputFile *file, StringRef name, uint32_t type,
+ uint64_t flags, uint32_t link, uint32_t info,
+ uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
Kind sectionKind);
static bool classof(const SectionBase *s) {
@@ -315,8 +315,8 @@ class MergeInputSection : public InputSectionBase {
template <class ELFT>
MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
StringRef name);
- MergeInputSection(Ctx &, uint64_t flags, uint32_t type, uint64_t entsize,
- ArrayRef<uint8_t> data, StringRef name);
+ MergeInputSection(Ctx &, StringRef name, uint32_t type, uint64_t flags,
+ uint64_t entsize, ArrayRef<uint8_t> data);
static bool classof(const SectionBase *s) { return s->kind() == Merge; }
void splitIntoPieces();
@@ -394,8 +394,9 @@ class EhInputSection : public InputSectionBase {
// .eh_frame. It also includes the synthetic sections themselves.
class InputSection : public InputSectionBase {
public:
- InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign,
- ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
+ InputSection(InputFile *f, StringRef name, uint32_t type, uint64_t flags,
+ uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
+ Kind k = Regular);
template <class ELFT>
InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
StringRef name);
@@ -473,8 +474,8 @@ class SyntheticSection : public InputSection {
Ctx &ctx;
SyntheticSection(Ctx &ctx, uint64_t flags, uint32_t type, uint32_t addralign,
StringRef name)
- : InputSection(ctx.internalFile, flags, type, addralign, {}, name,
- InputSectionBase::Synthetic),
+ : InputSection(ctx.internalFile, name, type, flags, addralign,
+ /*entsize=*/0, {}, InputSectionBase::Synthetic),
ctx(ctx) {}
virtual ~SyntheticSection() = default;
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 9bcbea250e7db7..31d14df9be71e9 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -67,9 +67,8 @@ void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
OutputSection::OutputSection(Ctx &ctx, StringRef name, uint32_t type,
uint64_t flags)
- : SectionBase(Output, ctx.internalFile, name, flags, /*entsize=*/0,
- /*addralign=*/1, type,
- /*info=*/0, /*link=*/0),
+ : SectionBase(Output, ctx.internalFile, name, type, flags, /*link=*/0,
+ /*info=*/0, /*addralign=*/1, /*entsize=*/0),
ctx(ctx) {}
uint64_t OutputSection::getLMA() const {
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 7e5e713513c473..5608378c77c0a2 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -89,8 +89,8 @@ static ArrayRef<uint8_t> getVersion(Ctx &ctx) {
// The returned object is a mergeable string section.
MergeInputSection *elf::createCommentSection(Ctx &ctx) {
auto *sec =
- make<MergeInputSection>(ctx, SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
- getVersion(ctx), ".comment");
+ make<MergeInputSection>(ctx, ".comment", SHT_PROGBITS,
+ SHF_MERGE | SHF_STRINGS, 1, getVersion(ctx));
sec->splitIntoPieces();
return sec;
}
@@ -273,8 +273,9 @@ InputSection *elf::createInterpSection(Ctx &ctx) {
StringRef s = ctx.saver.save(ctx.arg.dynamicLinker);
ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
- return make<InputSection>(ctx.internalFile, SHF_ALLOC, SHT_PROGBITS, 1,
- contents, ".interp");
+ return make<InputSection>(ctx.internalFile, ".interp", SHT_PROGBITS,
+ SHF_ALLOC,
+ /*addralign=*/1, /*entsize=*/0, contents);
}
Defined *elf::addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type,
More information about the llvm-commits
mailing list