From: Rui Ueyama Date: Mon, 5 Dec 2016 21:37:16 +0000 (+0000) Subject: Split removeUnusedSyntheticSections into two functions. X-Git-Tag: llvmorg-4.0.0-rc1~2957 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=29e7a19e179d9cbeed9bd343b6972a39725452b6;p=platform%2Fupstream%2Fllvm.git Split removeUnusedSyntheticSections into two functions. llvm-svn: 288707 --- diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 28a9563..5f9ad3e 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -60,6 +60,7 @@ private: void sortSections(); void finalizeSections(); void addPredefinedSections(); + void removeEmptyOutputSections(); std::vector createPhdrs(); void addPtArmExid(std::vector &Phdrs); @@ -910,29 +911,38 @@ finalizeSynthetic(const std::vector *> &Sections) { } // We need to add input synthetic sections early in createSyntheticSections() -// to make them visible from linkescript side. But not all sections are always -// required to be in output. For example we don't need dynamic section content -// sometimes. This function filters out such unused sections from output. -template -static void removeUnusedSyntheticSections(std::vector &V) { +// to make them visible from linker scripts before we can see whether they +// will be needed or not. As a result, some syntehtic input sections could be +// left empty. We remove such sections in this function. +template static void removeEmptySyntheticSections() { // Input synthetic sections are placed after all regular ones. We iterate over // them all and exit at first non-synthetic. - for (InputSectionBase *S : llvm::reverse(Symtab::X->Sections)) { - SyntheticSection *SS = dyn_cast>(S); - if (!SS) + for (InputSectionBase *Sec : llvm::reverse(Symtab::X->Sections)) { + SyntheticSection *In = dyn_cast>(S); + if (!In) return; - if (!SS->empty() || !SS->OutSec) + if (!In->empty() || !In->OutSec) continue; - OutputSection *OutSec = cast>(SS->OutSec); - OutSec->Sections.erase( - std::find(OutSec->Sections.begin(), OutSec->Sections.end(), SS)); - // If there is no other sections in output section, remove it from output. - if (OutSec->Sections.empty()) - V.erase(std::find(V.begin(), V.end(), OutSec)); + OutputSection *Out = cast>(In->OutSec); + Out->Sections.erase( + std::find(Out->Sections.begin(), Out->Sections.end(), In)); } } +// This function removes empty output sections so that LLD doesn't create +// empty sections. Such output sections are usually results of linker scripts. +template void Writer::removeEmptyOutputSections() { + OutputSections.erase( + std::remove_if(OutputSections.begin(), OutputSections.end(), + [](OutputSectionBase *Sec) { + if (auto *S = dyn_cast>(Sec)) + return S->Sections.empty(); + return false; + }), + OutputSections.end()); +} + // Create output section objects and add them to OutputSections. template void Writer::finalizeSections() { Out::DebugInfo = findSection(".debug_info"); @@ -993,7 +1003,8 @@ template void Writer::finalizeSections() { // So far we have added sections from input object files. // This function adds linker-created Out::* sections. addPredefinedSections(); - removeUnusedSyntheticSections(OutputSections); + removeEmptySyntheticSections(); + removeEmptyOutputSections(); sortSections();