Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -72,10 +72,12 @@ SectionIndex(INT_MAX) {} static bool compareByFilePosition(InputSection *A, InputSection *B) { - // Synthetic doesn't have link order dependecy, stable_sort will keep it last - if (A->kind() == InputSectionBase::Synthetic || - B->kind() == InputSectionBase::Synthetic) + // The .ARM.exidx sentinel is the only Synthetic we expect to see here, + // it needs to be sorted last. + if (A->kind() == InputSectionBase::Synthetic) return false; + else if (B->kind() == InputSectionBase::Synthetic) + return true; auto *LA = cast(A->getLinkOrderDep()); auto *LB = cast(B->getLinkOrderDep()); OutputSection *AOut = LA->OutSec; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -213,6 +213,7 @@ // Create linker-synthesized sections such as .got or .plt. // Such sections are of type input section. createSyntheticSections(); + addPredefinedSections(); combineMergableSections(); if (!Config->Relocatable) @@ -1186,9 +1187,6 @@ if (ErrorCount) return; - // So far we have added sections from input object files. - // This function adds linker-created Out::* sections. - addPredefinedSections(); removeUnusedSyntheticSections(OutputSections); sortSections(); @@ -1260,9 +1258,12 @@ template void Writer::addPredefinedSections() { // ARM ABI requires .ARM.exidx to be terminated by some piece of data. // We have the terminater synthetic section class. Add that at the end. - auto *OS = dyn_cast_or_null(findSection(".ARM.exidx")); - if (OS && !OS->Sections.empty() && !Config->Relocatable) - OS->addSection(make()); + if (Config->EMachine == EM_ARM && !Config->Relocatable && + std::find_if(InputSections.begin(), InputSections.end(), + [](InputSectionBase *S) { + return S->Name.startswith(".ARM.exidx"); + }) != InputSections.end()) + InputSections.push_back(make()); } // The linker is expected to define SECNAME_start and SECNAME_end