[PATCH] D135235: [BOLT] Change order of new sections
Maksim Panchenko via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 4 19:29:12 PDT 2022
maksfb created this revision.
maksfb added reviewers: yota9, Amir, ayermolo, rafauler.
Herald added a subscriber: treapster.
Herald added a project: All.
maksfb requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
While the order of new sections in the output binary was deterministic
in the past (i.e. there was no run-to-run variation), it wasn't always
rational as we used size to define the precedence of allocatable
sections within "code" or "data" groups (probably unintentionally).
Fix that by defining stricter section-ordering rules.
Other than the order of sections, this should be NFC.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D135235
Files:
bolt/include/bolt/Core/BinarySection.h
bolt/lib/Core/BinarySection.cpp
Index: bolt/lib/Core/BinarySection.cpp
===================================================================
--- bolt/lib/Core/BinarySection.cpp
+++ bolt/lib/Core/BinarySection.cpp
@@ -26,6 +26,8 @@
extern cl::opt<bool> HotData;
} // namespace opts
+uint64_t BinarySection::Count = 0;
+
bool BinarySection::isELF() const { return BC.isELF(); }
bool BinarySection::isMachO() const { return BC.isMachO(); }
Index: bolt/include/bolt/Core/BinarySection.h
===================================================================
--- bolt/include/bolt/Core/BinarySection.h
+++ bolt/include/bolt/Core/BinarySection.h
@@ -43,6 +43,9 @@
class BinarySection {
friend class BinaryContext;
+ /// Count the number of sections created.
+ static uint64_t Count;
+
BinaryContext &BC; // Owning BinaryContext
std::string Name; // Section name
const SectionRef Section; // SectionRef (may be null)
@@ -86,6 +89,7 @@
uint64_t OutputSize{0}; // Section size in the rewritten binary.
uint64_t OutputFileOffset{0}; // File offset in the rewritten binary file.
StringRef OutputContents; // Rewritten section contents.
+ const uint64_t SectionNumber; // Order in which the section was created.
unsigned SectionID{-1u}; // Unique ID used for address mapping.
// Set by ExecutableFileMemoryManager.
uint32_t Index{0}; // Section index in the output file.
@@ -147,13 +151,14 @@
Size(Section.getSize()), Alignment(Section.getAlignment()),
ELFType(Section.getELFType()), ELFFlags(Section.getELFFlags()),
Relocations(Section.Relocations),
- PendingRelocations(Section.PendingRelocations), OutputName(Name) {}
+ PendingRelocations(Section.PendingRelocations), OutputName(Name),
+ SectionNumber(++Count) {}
BinarySection(BinaryContext &BC, SectionRef Section)
: BC(BC), Name(getName(Section)), Section(Section),
Contents(getContents(Section)), Address(Section.getAddress()),
Size(Section.getSize()), Alignment(Section.getAlignment()),
- OutputName(Name) {
+ OutputName(Name), SectionNumber(++Count) {
if (isELF()) {
ELFType = ELFSectionRef(Section).getType();
ELFFlags = ELFSectionRef(Section).getFlags();
@@ -173,7 +178,7 @@
Contents(reinterpret_cast<const char *>(Data), Data ? Size : 0),
Address(0), Size(Size), Alignment(Alignment), ELFType(ELFType),
ELFFlags(ELFFlags), IsFinalized(true), OutputName(Name),
- OutputSize(Size), OutputContents(Contents) {
+ OutputSize(Size), OutputContents(Contents), SectionNumber(++Count) {
assert(Alignment > 0 && "section alignment must be > 0");
}
@@ -207,10 +212,34 @@
// Order sections by their immutable properties.
bool operator<(const BinarySection &Other) const {
- return (getAddress() < Other.getAddress() ||
- (getAddress() == Other.getAddress() &&
- (getSize() < Other.getSize() ||
- (getSize() == Other.getSize() && getName() < Other.getName()))));
+ // Allocatable before non-allocatable.
+ if (isAllocatable() != Other.isAllocatable())
+ return isAllocatable() > Other.isAllocatable();
+
+ // Input sections take precedence.
+ if (hasSectionRef() != Other.hasSectionRef())
+ return hasSectionRef() > Other.hasSectionRef();
+
+ // Compare allocatable input sections by their address.
+ if (getAddress() != Other.getAddress())
+ return getAddress() < Other.getAddress();
+ if (getAddress() && getSize() != Other.getSize())
+ return getSize() < Other.getSize();
+
+ // Code before data.
+ if (isText() != Other.isText())
+ return isText() > Other.isText();
+
+ // Read-only before writable.
+ if (isReadOnly() != Other.isReadOnly())
+ return isReadOnly() > Other.isReadOnly();
+
+ // BSS at the end.
+ if (isBSS() != Other.isBSS())
+ return isBSS() < Other.isBSS();
+
+ // Otherwise, preserve the order of creation.
+ return SectionNumber < Other.SectionNumber;
}
///
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135235.465259.patch
Type: text/x-patch
Size: 4123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221005/c7303966/attachment.bin>
More information about the llvm-commits
mailing list