Index: lld/trunk/ELF/Relocations.h =================================================================== --- lld/trunk/ELF/Relocations.h +++ lld/trunk/ELF/Relocations.h @@ -126,7 +126,7 @@ bool createThunks(ArrayRef OutputSections); private: - void mergeThunks(OutputSection *OS, std::vector &Thunks); + void mergeThunks(); ThunkSection *getOSThunkSec(ThunkSection *&TS, OutputSection *OS); ThunkSection *getISThunkSec(InputSection *IS, OutputSection *OS); std::pair getThunk(SymbolBody &Body, uint32_t Type); @@ -138,7 +138,8 @@ llvm::DenseMap ThunkedSections; // Track the ThunksSections that need to be inserted into an OutputSection - std::map> ThunkSections; + std::map *, std::vector> + ThunkSections; }; // Return a int64_t to make sure we get the sign extension out of the way as Index: lld/trunk/ELF/Relocations.cpp =================================================================== --- lld/trunk/ELF/Relocations.cpp +++ lld/trunk/ELF/Relocations.cpp @@ -967,33 +967,38 @@ // in the Sections vector, and recalculate the InputSection output section // offsets. // This may invalidate any output section offsets stored outside of InputSection -void ThunkCreator::mergeThunks(OutputSection *OS, - std::vector &Thunks) { - // Order Thunks in ascending OutSecOff - auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) { - return A->OutSecOff < B->OutSecOff; - }; - std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp); - - // Merge sorted vectors of Thunks and InputSections by OutSecOff - std::vector Tmp; - Tmp.reserve(OS->Sections.size() + Thunks.size()); - auto MergeCmp = [](const InputSection *A, const InputSection *B) { - // std::merge requires a strict weak ordering. - if (A->OutSecOff < B->OutSecOff) - return true; - if (A->OutSecOff == B->OutSecOff) - // Check if Thunk is immediately before any specific Target InputSection - // for example Mips LA25 Thunks. - if (auto *TA = dyn_cast(A)) - if (TA && TA->getTargetInputSection() == B) - return true; - return false; - }; - std::merge(OS->Sections.begin(), OS->Sections.end(), Thunks.begin(), - Thunks.end(), std::back_inserter(Tmp), MergeCmp); - OS->Sections = std::move(Tmp); - OS->assignOffsets(); +void ThunkCreator::mergeThunks() { + for (auto &KV : ThunkSections) { + std::vector *ISR = KV.first; + std::vector &Thunks = KV.second; + + // Order Thunks in ascending OutSecOff + auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) { + return A->OutSecOff < B->OutSecOff; + }; + std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp); + + // Merge sorted vectors of Thunks and InputSections by OutSecOff + std::vector Tmp; + Tmp.reserve(ISR->size() + Thunks.size()); + auto MergeCmp = [](const InputSection *A, const InputSection *B) { + // std::merge requires a strict weak ordering. + if (A->OutSecOff < B->OutSecOff) + return true; + if (A->OutSecOff == B->OutSecOff) + // Check if Thunk is immediately before any specific Target InputSection + // for example Mips LA25 Thunks. + if (auto *TA = dyn_cast(A)) + if (TA && TA->getTargetInputSection() == B) + return true; + return false; + }; + std::merge(ISR->begin(), ISR->end(), Thunks.begin(), Thunks.end(), + std::back_inserter(Tmp), MergeCmp); + OutputSection *OS = Thunks.front()->getParent(); + OS->Sections = std::move(Tmp); + OS->assignOffsets(); + } } ThunkSection *ThunkCreator::getOSThunkSec(ThunkSection *&TS, @@ -1006,7 +1011,7 @@ break; } TS = make(OS, Off); - ThunkSections[OS].push_back(TS); + ThunkSections[&OS->Sections].push_back(TS); } return TS; } @@ -1017,7 +1022,7 @@ return TS; auto *TOS = IS->getParent(); TS = make(TOS, IS->OutSecOff); - ThunkSections[TOS].push_back(TS); + ThunkSections[&TOS->Sections].push_back(TS); ThunkedSections[IS] = TS; return TS; } @@ -1074,8 +1079,7 @@ } // Merge all created synthetic ThunkSections back into OutputSection - for (auto &KV : ThunkSections) - mergeThunks(KV.first, KV.second); + mergeThunks(); return !ThunkSections.empty(); }