Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -949,6 +949,14 @@ // Order Thunks in ascending OutSecOff auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) { + if (A->OutSecOff == B->OutSecOff) { + // If we have two ThunkSections at the same address and one has a + // specific Target then the specific Target comes last as control may + // fall through from the Thunk to the Target + if (!A->getTargetInputSection() && B->getTargetInputSection()) + return true; + return false; + } return A->OutSecOff < B->OutSecOff; }; std::stable_sort(ThunkBegin, ThunkEnd, ThunkCmp); @@ -960,12 +968,18 @@ // std::merge requires a strict weak ordering. if (A->OutSecOff < B->OutSecOff) return true; - if (A->OutSecOff == B->OutSecOff) + if (A->OutSecOff == B->OutSecOff) { + auto *TA = dyn_cast(A); + auto *TB = dyn_cast(B); // 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; + if (TA && TA->getTargetInputSection() == B) + return true; + else if (TA && !TB && !TA->getTargetInputSection()) + // In general place Thunk Sections without specific targets before + // non-Thunk Sections + return true; + } return false; }; std::merge(OS->Sections.begin(), OS->Sections.end(), ThunkBegin, ThunkEnd,