diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index fb74a3153d9e..6bcfc6f96783 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -850,23 +850,25 @@ template void Writer::createSections() { // This function add Out::* sections to OutputSections. template void Writer::addPredefinedSections() { + auto Add = [&](OutputSectionBase *C) { + if (C) + OutputSections.push_back(C); + }; + // This order is not the same as the final output order // because we sort the sections using their attributes below. - if (Out::SymTab) - OutputSections.push_back(Out::SymTab); - OutputSections.push_back(Out::ShStrTab); - if (Out::StrTab) - OutputSections.push_back(Out::StrTab); + Add(Out::SymTab); + Add(Out::ShStrTab); + Add(Out::StrTab); if (isOutputDynamic()) { - OutputSections.push_back(Out::DynSymTab); - if (Out::GnuHashTab) - OutputSections.push_back(Out::GnuHashTab); - if (Out::HashTab) - OutputSections.push_back(Out::HashTab); - OutputSections.push_back(Out::Dynamic); - OutputSections.push_back(Out::DynStrTab); + Add(Out::DynSymTab); + Add(Out::GnuHashTab); + Add(Out::HashTab); + Add(Out::Dynamic); + Add(Out::DynStrTab); if (Out::RelaDyn->hasRelocs()) - OutputSections.push_back(Out::RelaDyn); + Add(Out::RelaDyn); + // This is a MIPS specific section to hold a space within the data segment // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry. // See "Dynamic section" in Chapter 5 in the following document: @@ -877,14 +879,14 @@ template void Writer::addPredefinedSections() { Out::MipsRldMap->setSize(ELFT::Is64Bits ? 8 : 4); Out::MipsRldMap->updateAlign(ELFT::Is64Bits ? 8 : 4); OwningSections.emplace_back(Out::MipsRldMap); - OutputSections.push_back(Out::MipsRldMap); + Add(Out::MipsRldMap); } } // We always need to add rel[a].plt to output if it has entries. // Even during static linking it can contain R_[*]_IRELATIVE relocations. if (Out::RelaPlt && Out::RelaPlt->hasRelocs()) { - OutputSections.push_back(Out::RelaPlt); + Add(Out::RelaPlt); Out::RelaPlt->Static = !isOutputDynamic(); } @@ -899,11 +901,11 @@ template void Writer::addPredefinedSections() { needsGot = true; if (needsGot) - OutputSections.push_back(Out::Got); + Add(Out::Got); if (Out::GotPlt && !Out::GotPlt->empty()) - OutputSections.push_back(Out::GotPlt); + Add(Out::GotPlt); if (!Out::Plt->empty()) - OutputSections.push_back(Out::Plt); + Add(Out::Plt); } // The linker is expected to define SECNAME_start and SECNAME_end