Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1020,13 +1020,30 @@ Script::X->adjustSectionsAfterSorting(); } -template -static void finalizeSynthetic(const std::vector &Sections) { - for (SyntheticSection *SS : Sections) - if (SS && SS->OutSec && !SS->empty()) { - SS->finalizeContents(); - SS->OutSec->template assignOffsets(); - } +template static void finalizeSynthetic(SyntheticSection *SS) { + if (!SS || !SS->OutSec || SS->empty()) + return; + SS->finalizeContents(); + SS->OutSec->template assignOffsets(); +} + +// Dynamic section must be the last one in this list and dynamic +// symbol table section (DynSymTab) must be the first one. Dynamic +// string table should go after others because some sections may +// add strings there during finalizing. +template static void finalizeAllSynthetics() { + auto IsSpecial = [](SyntheticSection *SS) { + return SS == In::DynSymTab || SS == In::Dynamic || + SS == In::DynStrTab; + }; + + finalizeSynthetic(In::DynSymTab); + for (InputSectionBase *S : InputSections) + if (SyntheticSection *SS = dyn_cast(S)) + if (!IsSpecial(SS)) + finalizeSynthetic(SS); + finalizeSynthetic(In::DynStrTab); + finalizeSynthetic(In::Dynamic); } // We need to add input synthetic sections early in createSyntheticSections() @@ -1083,7 +1100,7 @@ // This responsible for splitting up .eh_frame section into // pieces. The relocation scan uses those peaces, so this has to be // earlier. - finalizeSynthetic({In::EhFrame}); + finalizeSynthetic(In::EhFrame); // Scan relocations. This must be done after every symbol is declared so that // we can correctly decide if a dynamic relocation is needed. @@ -1156,17 +1173,7 @@ for (OutputSection *Sec : OutputSections) Sec->finalize(); - // Dynamic section must be the last one in this list and dynamic - // symbol table section (DynSymTab) must be the first one. - finalizeSynthetic( - {In::DynSymTab, In::GnuHashTab, In::HashTab, - In::SymTab, In::ShStrTab, In::StrTab, - In::VerDef, In::DynStrTab, In::GdbIndex, - In::Got, In::MipsGot, In::IgotPlt, - In::GotPlt, In::RelaDyn, In::RelaIplt, - In::RelaPlt, In::Plt, In::Iplt, - In::Plt, In::EhFrameHdr, In::VerSym, - In::VerNeed, In::Dynamic}); + finalizeAllSynthetics(); } template void Writer::addPredefinedSections() {