aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2024-05-31 23:10:43 -0700
committerFangrui Song <i@maskray.me>2024-05-31 23:10:43 -0700
commitf85904868b282aa56c8bef90f169ca5ecd9957f8 (patch)
tree8b766326f8de88f91440dc67e7ca325513c18517 /lld/ELF
parent26814bbd4f65934d4e231ce407ec1b62c9d81df7 (diff)
downloadllvm-f85904868b282aa56c8bef90f169ca5ecd9957f8.zip
llvm-f85904868b282aa56c8bef90f169ca5ecd9957f8.tar.gz
llvm-f85904868b282aa56c8bef90f169ca5ecd9957f8.tar.bz2
[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.
Diffstat (limited to 'lld/ELF')
-rw-r--r--lld/ELF/Writer.cpp30
1 files changed, 15 insertions, 15 deletions
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index c90bbee..58ae207 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -950,6 +950,11 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
if (!isa<OutputDesc>(*i))
return e;
+ auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
+ auto *osd = dyn_cast<OutputDesc>(cmd);
+ return osd && osd->osec.hasInputSections;
+ };
+
// 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,
@@ -959,30 +964,25 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
// 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)
+ for (auto j = ++i; j != e; ++j) {
+ if (!isOutputSecWithInputSections(*j))
+ continue;
+ if (getRankProximity(sec, *j) != proximity)
break;
+ i = j + 1;
}
+ } else {
+ for (; i != b; --i)
+ if (isOutputSecWithInputSections(i[-1]))
+ break;
}
- auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
- auto *osd = dyn_cast<OutputDesc>(cmd);
- return osd && osd->osec.hasInputSections;
- };
- auto j =
- std::find_if(std::make_reverse_iterator(i), std::make_reverse_iterator(b),
- isOutputSecWithInputSections);
- i = j.base();
-
// As a special case, if the orphan section is the last section, put
// it at the very end, past any other commands.
// This matches bfd's behavior and is convenient when the linker script fully
// specifies the start of the file, but doesn't care about the end (the non
// alloc sections for example).
- auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
- if (nextSec == e)
+ if (std::find_if(i, e, isOutputSecWithInputSections) == e)
return e;
while (i != e && shouldSkip(*i))