diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index bf613405c1f7..dcb5cfccdbe7 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -105,59 +105,60 @@ private: template static bool shouldUseRela() { return ELFT::Is64Bits; } template void elf2::writeResult(SymbolTable *Symtab) { - // Initialize output sections that are handled by Writer specially. - // Don't reorder because the order of initialization matters. - InterpSection Interp; - Out::Interp = &Interp; - StringTableSection ShStrTab(".shstrtab", false); - Out::ShStrTab = &ShStrTab; - StringTableSection StrTab(".strtab", false); - if (!Config->StripAll) - Out::StrTab = &StrTab; - else - Out::StrTab = nullptr; - StringTableSection DynStrTab(".dynstr", true); - Out::DynStrTab = &DynStrTab; - GotSection Got; - Out::Got = &Got; - GotPltSection GotPlt; - if (Target->UseLazyBinding) - Out::GotPlt = &GotPlt; - else - Out::GotPlt = nullptr; - PltSection Plt; - Out::Plt = &Plt; - std::unique_ptr> SymTab; - if (!Config->StripAll) { - SymTab.reset(new SymbolTableSection(*Symtab, *Out::StrTab)); - Out::SymTab = SymTab.get(); - } else { - Out::SymTab = nullptr; - } - SymbolTableSection DynSymTab(*Symtab, *Out::DynStrTab); - Out::DynSymTab = &DynSymTab; - HashTableSection HashTab; - if (Config->SysvHash) - Out::HashTab = &HashTab; - else - Out::HashTab = nullptr; - GnuHashTableSection GnuHashTab; - if (Config->GnuHash) - Out::GnuHashTab = &GnuHashTab; - else - Out::GnuHashTab = nullptr; + // Create singleton output sections. bool IsRela = shouldUseRela(); - RelocationSection RelaDyn(IsRela ? ".rela.dyn" : ".rel.dyn", IsRela); - Out::RelaDyn = &RelaDyn; - RelocationSection RelaPlt(IsRela ? ".rela.plt" : ".rel.plt", IsRela); - if (Target->UseLazyBinding) - Out::RelaPlt = &RelaPlt; - else - Out::RelaPlt = nullptr; DynamicSection Dynamic(*Symtab); - Out::Dynamic = &Dynamic; EhFrameHeader EhFrameHdr; + GotSection Got; + InterpSection Interp; + PltSection Plt; + RelocationSection RelaDyn(IsRela ? ".rela.dyn" : ".rel.dyn", IsRela); + StringTableSection DynStrTab(".dynstr", true); + StringTableSection ShStrTab(".shstrtab", false); + SymbolTableSection DynSymTab(*Symtab, DynStrTab); + + // Instantiate optional output sections if they are needed. + std::unique_ptr> GnuHashTab; + std::unique_ptr> GotPlt; + std::unique_ptr> HashTab; + std::unique_ptr> RelaPlt; + std::unique_ptr> StrTab; + std::unique_ptr> SymTabSec; + + if (Config->GnuHash) + GnuHashTab.reset(new GnuHashTableSection); + if (Config->SysvHash) + HashTab.reset(new HashTableSection); + if (Target->UseLazyBinding) { + StringRef S = IsRela ? ".rela.plt" : ".rel.plt"; + GotPlt.reset(new GotPltSection); + RelaPlt.reset(new RelocationSection(S, IsRela)); + } + if (!Config->StripAll) { + StrTab.reset(new StringTableSection(".strtab", false)); + SymTabSec.reset(new SymbolTableSection(*Symtab, *StrTab)); + } + + Out::DynStrTab = &DynStrTab; + Out::DynSymTab = &DynSymTab; + Out::Dynamic = &Dynamic; Out::EhFrameHdr = &EhFrameHdr; + Out::GnuHashTab = GnuHashTab.get(); + Out::Got = &Got; + Out::GotPlt = GotPlt.get(); + Out::HashTab = HashTab.get(); + Out::Interp = &Interp; + Out::Plt = &Plt; + Out::RelaDyn = &RelaDyn; + Out::RelaPlt = RelaPlt.get(); + Out::ShStrTab = &ShStrTab; + Out::StrTab = StrTab.get(); + Out::SymTab = SymTabSec.get(); + Out::Bss = nullptr; + Out::MipsRldMap = nullptr; + Out::Opd = nullptr; + Out::OpdBuf = nullptr; + Out::TlsPhdr = nullptr; Writer(*Symtab).run(); }