From a7f7884df82c2ea5069a5a51622147f6553d1cff Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 20 Jul 2016 17:19:03 +0000 Subject: [PATCH] Simplify output section ownership. This patch simplifies output section management by making Factory class have ownership of sections that creates. Differential Revision: https://reviews.llvm.org/D22575 llvm-svn: 276141 --- lld/ELF/LinkerScript.cpp | 7 ++++--- lld/ELF/LinkerScript.h | 5 +++-- lld/ELF/OutputSections.cpp | 1 + lld/ELF/OutputSections.h | 1 + lld/ELF/Writer.cpp | 20 +++++++------------- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index f0eff8e..75d7b22 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -208,9 +208,10 @@ bool LinkerScript::shouldKeep(InputSectionBase *S) { } template -std::vector>> +std::vector *> LinkerScript::createSections(OutputSectionFactory &Factory) { - std::vector>> Result; + std::vector *> Result; + // Add input section to output section. If there is no output section yet, // then create it and add to output section list. auto AddInputSec = [&](InputSectionBase *C, StringRef Name) { @@ -218,7 +219,7 @@ LinkerScript::createSections(OutputSectionFactory &Factory) { bool IsNew; std::tie(Sec, IsNew) = Factory.create(C, Name); if (IsNew) - Result.emplace_back(Sec); + Result.push_back(Sec); Sec->addSection(C); }; diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index a56ac2d..3ef318d 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -92,12 +92,13 @@ template class LinkerScript { public: typedef PhdrEntry Phdr; + std::vector *> + createSections(OutputSectionFactory &Factory); + StringRef getOutputSection(InputSectionBase *S); ArrayRef getFiller(StringRef Name); bool isDiscarded(InputSectionBase *S); bool shouldKeep(InputSectionBase *S); - std::vector>> - createSections(OutputSectionFactory &Factory); void assignAddresses(ArrayRef *> S); int compareSections(StringRef A, StringRef B); void addScriptedSymbols(); diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 50b9401..cd5d496 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -1770,6 +1770,7 @@ OutputSectionFactory::create(InputSectionBase *C, Sec = new MipsOptionsOutputSection(); break; } + OwningSections.emplace_back(Sec); return {Sec, true}; } diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index 5fdf8de..f0a7196 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -685,6 +685,7 @@ private: Key createKey(InputSectionBase *C, StringRef OutsecName); llvm::SmallDenseMap *> Map; + std::vector>> OwningSections; }; template BuildIdSection *Out::BuildId; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index ad9602b..5b6c5f6 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -48,7 +48,7 @@ private: void copyLocalSymbols(); void addReservedSymbols(); - std::vector>> createSections(); + std::vector *> createSections(); void finalizeSections(); void addPredefinedSections(); bool needsGot(); @@ -218,13 +218,9 @@ template void Writer::run() { copyLocalSymbols(); addReservedSymbols(); - std::vector>> Sections = - ScriptConfig->DoLayout ? Script::X->createSections(Factory) - : createSections(); - - for (std::unique_ptr> &S : Sections) - OutputSections.push_back(S.get()); - + OutputSections = ScriptConfig->DoLayout + ? Script::X->createSections(Factory) + : createSections(); finalizeSections(); if (HasError) return; @@ -637,9 +633,8 @@ template static void sortCtorsDtors(OutputSectionBase *S) { } template -std::vector>> -Writer::createSections() { - std::vector>> Result; +std::vector *> Writer::createSections() { + std::vector *> Result; for (const std::unique_ptr> &F : Symtab.getObjectFiles()) { @@ -652,8 +647,7 @@ Writer::createSections() { bool IsNew; std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C)); if (IsNew) - Result.emplace_back(Sec); - + Result.push_back(Sec); Sec->addSection(C); } } -- 2.7.4