Simplify output section ownership.
authorRui Ueyama <ruiu@google.com>
Wed, 20 Jul 2016 17:19:03 +0000 (17:19 +0000)
committerRui Ueyama <ruiu@google.com>
Wed, 20 Jul 2016 17:19:03 +0000 (17:19 +0000)
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
lld/ELF/LinkerScript.h
lld/ELF/OutputSections.cpp
lld/ELF/OutputSections.h
lld/ELF/Writer.cpp

index f0eff8e..75d7b22 100644 (file)
@@ -208,9 +208,10 @@ bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
 }
 
 template <class ELFT>
-std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
+std::vector<OutputSectionBase<ELFT> *>
 LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
-  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Result;
+  std::vector<OutputSectionBase<ELFT> *> 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<ELFT> *C, StringRef Name) {
@@ -218,7 +219,7 @@ LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
     bool IsNew;
     std::tie(Sec, IsNew) = Factory.create(C, Name);
     if (IsNew)
-      Result.emplace_back(Sec);
+      Result.push_back(Sec);
     Sec->addSection(C);
   };
 
index a56ac2d..3ef318d 100644 (file)
@@ -92,12 +92,13 @@ template <class ELFT> class LinkerScript {
 public:
   typedef PhdrEntry<ELFT> Phdr;
 
+  std::vector<OutputSectionBase<ELFT> *>
+  createSections(OutputSectionFactory<ELFT> &Factory);
+
   StringRef getOutputSection(InputSectionBase<ELFT> *S);
   ArrayRef<uint8_t> getFiller(StringRef Name);
   bool isDiscarded(InputSectionBase<ELFT> *S);
   bool shouldKeep(InputSectionBase<ELFT> *S);
-  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
-  createSections(OutputSectionFactory<ELFT> &Factory);
   void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
   int compareSections(StringRef A, StringRef B);
   void addScriptedSymbols();
index 50b9401..cd5d496 100644 (file)
@@ -1770,6 +1770,7 @@ OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
     Sec = new MipsOptionsOutputSection<ELFT>();
     break;
   }
+  OwningSections.emplace_back(Sec);
   return {Sec, true};
 }
 
index 5fdf8de..f0a7196 100644 (file)
@@ -685,6 +685,7 @@ private:
   Key createKey(InputSectionBase<ELFT> *C, StringRef OutsecName);
 
   llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
+  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
 };
 
 template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
index ad9602b..5b6c5f6 100644 (file)
@@ -48,7 +48,7 @@ private:
 
   void copyLocalSymbols();
   void addReservedSymbols();
-  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> createSections();
+  std::vector<OutputSectionBase<ELFT> *> createSections();
   void finalizeSections();
   void addPredefinedSections();
   bool needsGot();
@@ -218,13 +218,9 @@ template <class ELFT> void Writer<ELFT>::run() {
     copyLocalSymbols();
   addReservedSymbols();
 
-  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Sections =
-      ScriptConfig->DoLayout ? Script<ELFT>::X->createSections(Factory)
-                             : createSections();
-
-  for (std::unique_ptr<OutputSectionBase<ELFT>> &S : Sections)
-    OutputSections.push_back(S.get());
-
+  OutputSections = ScriptConfig->DoLayout
+                       ? Script<ELFT>::X->createSections(Factory)
+                       : createSections();
   finalizeSections();
   if (HasError)
     return;
@@ -637,9 +633,8 @@ template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
 }
 
 template <class ELFT>
-std::vector<std::unique_ptr<OutputSectionBase<ELFT>>>
-Writer<ELFT>::createSections() {
-  std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Result;
+std::vector<OutputSectionBase<ELFT> *> Writer<ELFT>::createSections() {
+  std::vector<OutputSectionBase<ELFT> *> Result;
 
   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
        Symtab.getObjectFiles()) {
@@ -652,8 +647,7 @@ Writer<ELFT>::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);
     }
   }