Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1020,13 +1020,26 @@ 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 because depends on +// other section contents. Dynamic symbol table section must be the +// first one for the same reasons. Dynamic string table also should go after +// others because some sections may add strings there during finalizing. +template static void finalizeSyntheticSections() { + finalizeSynthetic(In::DynSymTab); + for (InputSectionBase *S : InputSections) + if (SyntheticSection *SS = dyn_cast(S)) + if (SS != In::DynSymTab && SS != In::Dynamic && + SS != In::DynStrTab) + finalizeSynthetic(SS); + finalizeSynthetic(In::DynStrTab); + finalizeSynthetic(In::Dynamic); } // We need to add input synthetic sections early in createSyntheticSections() @@ -1083,7 +1096,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 +1169,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}); + finalizeSyntheticSections(); } template void Writer::addPredefinedSections() {