Skip to content

Commit 0f3d646

Browse files
committed
[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")
1 parent 59116e0 commit 0f3d646

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

lld/ELF/Writer.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -949,25 +949,22 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
949949
}
950950
if (!isa<OutputDesc>(*i))
951951
return e;
952-
auto foundSec = &cast<OutputDesc>(*i)->osec;
953-
954-
// Consider all existing sections with the same proximity.
955-
unsigned sortRank = sec->sortRank;
956-
if (script->hasPhdrsCommands() || !script->memoryRegions.empty())
957-
// Prevent the orphan section to be placed before the found section. If
958-
// custom program headers are defined, that helps to avoid adding it to a
959-
// previous segment and changing flags of that segment, for example, making
960-
// a read-only segment writable. If memory regions are defined, an orphan
961-
// section should continue the same region as the found section to better
962-
// resemble the behavior of GNU ld.
963-
sortRank = std::max(sortRank, foundSec->sortRank);
964-
for (; i != e; ++i) {
965-
auto *curSecDesc = dyn_cast<OutputDesc>(*i);
966-
if (!curSecDesc || !curSecDesc->osec.hasInputSections)
967-
continue;
968-
if (getRankProximity(sec, curSecDesc) != proximity ||
969-
sortRank < curSecDesc->osec.sortRank)
970-
break;
952+
953+
// If i's rank is larger, the orphan section can be placed before i.
954+
//
955+
// However, don't do this if custom program headers are defined. Otherwise,
956+
// adding the orphan to a previous segment can change its flags, for example,
957+
// making a read-only segment writable. If memory regions are defined, an
958+
// orphan section should continue the same region as the found section to
959+
// better resemble the behavior of GNU ld.
960+
bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
961+
if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
962+
while (++i != e) {
963+
auto *cur = dyn_cast<OutputDesc>(*i);
964+
if (cur && cur->osec.hasInputSections &&
965+
getRankProximity(sec, cur) != proximity)
966+
break;
967+
}
971968
}
972969

973970
auto isOutputSecWithInputSections = [](SectionCommand *cmd) {

0 commit comments

Comments
 (0)