Skip to content

Commit f859048

Browse files
committed
[ELF] Simplify findOrphanPos. NFC
When the orphan section is placed after i, incrementing then decreamenting is quite difficult to understand. Simplify the code to a single loop to make the intention clearer.
1 parent 26814bb commit f859048

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

lld/ELF/Writer.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,11 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
950950
if (!isa<OutputDesc>(*i))
951951
return e;
952952

953+
auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
954+
auto *osd = dyn_cast<OutputDesc>(cmd);
955+
return osd && osd->osec.hasInputSections;
956+
};
957+
953958
// If i's rank is larger, the orphan section can be placed before i.
954959
//
955960
// However, don't do this if custom program headers are defined. Otherwise,
@@ -959,30 +964,25 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
959964
// better resemble the behavior of GNU ld.
960965
bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
961966
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)
967+
for (auto j = ++i; j != e; ++j) {
968+
if (!isOutputSecWithInputSections(*j))
969+
continue;
970+
if (getRankProximity(sec, *j) != proximity)
966971
break;
972+
i = j + 1;
967973
}
974+
} else {
975+
for (; i != b; --i)
976+
if (isOutputSecWithInputSections(i[-1]))
977+
break;
968978
}
969979

970-
auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
971-
auto *osd = dyn_cast<OutputDesc>(cmd);
972-
return osd && osd->osec.hasInputSections;
973-
};
974-
auto j =
975-
std::find_if(std::make_reverse_iterator(i), std::make_reverse_iterator(b),
976-
isOutputSecWithInputSections);
977-
i = j.base();
978-
979980
// As a special case, if the orphan section is the last section, put
980981
// it at the very end, past any other commands.
981982
// This matches bfd's behavior and is convenient when the linker script fully
982983
// specifies the start of the file, but doesn't care about the end (the non
983984
// alloc sections for example).
984-
auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
985-
if (nextSec == e)
985+
if (std::find_if(i, e, isOutputSecWithInputSections) == e)
986986
return e;
987987

988988
while (i != e && shouldSkip(*i))

0 commit comments

Comments
 (0)