@@ -950,6 +950,11 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
950
950
if (!isa<OutputDesc>(*i))
951
951
return e;
952
952
953
+ auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
954
+ auto *osd = dyn_cast<OutputDesc>(cmd);
955
+ return osd && osd->osec .hasInputSections ;
956
+ };
957
+
953
958
// If i's rank is larger, the orphan section can be placed before i.
954
959
//
955
960
// However, don't do this if custom program headers are defined. Otherwise,
@@ -959,30 +964,25 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
959
964
// better resemble the behavior of GNU ld.
960
965
bool mustAfter = script->hasPhdrsCommands () || !script->memoryRegions .empty ();
961
966
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)
966
971
break ;
972
+ i = j + 1 ;
967
973
}
974
+ } else {
975
+ for (; i != b; --i)
976
+ if (isOutputSecWithInputSections (i[-1 ]))
977
+ break ;
968
978
}
969
979
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
-
979
980
// As a special case, if the orphan section is the last section, put
980
981
// it at the very end, past any other commands.
981
982
// This matches bfd's behavior and is convenient when the linker script fully
982
983
// specifies the start of the file, but doesn't care about the end (the non
983
984
// 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)
986
986
return e;
987
987
988
988
while (i != e && shouldSkip (*i))
0 commit comments