[lld] 0f3d646 - [ELF] Simplify findOrphanPos. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri May 31 22:35:36 PDT 2024
Author: Fangrui Song
Date: 2024-05-31T22:35:31-07:00
New Revision: 0f3d646cefbe00b4a1037dc68e9d76e5470e805f
URL: https://github.com/llvm/llvm-project/commit/0f3d646cefbe00b4a1037dc68e9d76e5470e805f
DIFF: https://github.com/llvm/llvm-project/commit/0f3d646cefbe00b4a1037dc68e9d76e5470e805f.diff
LOG: [ELF] Simplify findOrphanPos. NFC
Simplify the loop that considers sections of the same proximity. The two
involved conditions are due to:
* https://reviews.llvm.org/D111717 ("[ELF] Avoid adding an orphan section to a less suitable segment") and
* https://reviews.llvm.org/D112925 ("[ELF] Better resemble GNU ld when placing orphan sections into memory regions")
Added:
Modified:
lld/ELF/Writer.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 05f2e95def4b3..c90bbeec2779d 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -949,25 +949,22 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
}
if (!isa<OutputDesc>(*i))
return e;
- auto foundSec = &cast<OutputDesc>(*i)->osec;
-
- // Consider all existing sections with the same proximity.
- unsigned sortRank = sec->sortRank;
- if (script->hasPhdrsCommands() || !script->memoryRegions.empty())
- // Prevent the orphan section to be placed before the found section. If
- // custom program headers are defined, that helps to avoid adding it to a
- // previous segment and changing flags of that segment, for example, making
- // a read-only segment writable. If memory regions are defined, an orphan
- // section should continue the same region as the found section to better
- // resemble the behavior of GNU ld.
- sortRank = std::max(sortRank, foundSec->sortRank);
- for (; i != e; ++i) {
- auto *curSecDesc = dyn_cast<OutputDesc>(*i);
- if (!curSecDesc || !curSecDesc->osec.hasInputSections)
- continue;
- if (getRankProximity(sec, curSecDesc) != proximity ||
- sortRank < curSecDesc->osec.sortRank)
- break;
+
+ // If i's rank is larger, the orphan section can be placed before i.
+ //
+ // However, don't do this if custom program headers are defined. Otherwise,
+ // adding the orphan to a previous segment can change its flags, for example,
+ // making a read-only segment writable. If memory regions are defined, an
+ // orphan section should continue the same region as the found section to
+ // better resemble the behavior of GNU ld.
+ bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
+ if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
+ while (++i != e) {
+ auto *cur = dyn_cast<OutputDesc>(*i);
+ if (cur && cur->osec.hasInputSections &&
+ getRankProximity(sec, cur) != proximity)
+ break;
+ }
}
auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
More information about the llvm-commits
mailing list