Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1092,21 +1092,23 @@ sortISDBySectionOrder(InputSectionDescription *ISD, const DenseMap &Order) { std::vector UnorderedSections; - std::vector OrderedSections; + std::vector> OrderedSections; uint64_t UnorderedSize = 0; for (InputSection *IS : ISD->Sections) { - if (!Order.count(IS)) { + auto I = Order.find(IS); + if (I == Order.end()) { UnorderedSections.push_back(IS); UnorderedSize += IS->getSize(); continue; } - OrderedSections.push_back(IS); + OrderedSections.push_back({IS, I->second}); } - std::sort(OrderedSections.begin(), OrderedSections.end(), - [&](InputSection *A, InputSection *B) { - return Order.lookup(A) < Order.lookup(B); - }); + std::sort( + OrderedSections.begin(), OrderedSections.end(), + [&](std::pair A, std::pair B) { + return A.second < B.second; + }); // Find an insertion point for the ordered section list in the unordered // section list. On targets with limited-range branches, this is the mid-point @@ -1147,10 +1149,12 @@ std::copy(UnorderedSections.begin(), UnorderedSections.begin() + UnorderedInsPt, ISD->Sections.begin()); - std::copy(OrderedSections.begin(), OrderedSections.end(), - ISD->Sections.begin() + UnorderedInsPt); + std::vector::iterator SectionsPos = + ISD->Sections.begin() + UnorderedInsPt; + for (std::pair P : OrderedSections) + *SectionsPos++ = P.first; std::copy(UnorderedSections.begin() + UnorderedInsPt, UnorderedSections.end(), - ISD->Sections.begin() + UnorderedInsPt + OrderedSections.size()); + SectionsPos); } static void sortSection(OutputSection *Sec,