[lld] r295341 - Share more output section creation code.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 09:32:27 PST 2017


Author: rafael
Date: Thu Feb 16 11:32:26 2017
New Revision: 295341

URL: http://llvm.org/viewvc/llvm-project?rev=295341&view=rev
Log:
Share more output section creation code.

We can do this now that the linker script and the writer agree on
which sections should be combined.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=295341&r1=295340&r2=295341&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Thu Feb 16 11:32:26 2017
@@ -303,18 +303,6 @@ LinkerScript<ELFT>::createInputSectionLi
 }
 
 template <class ELFT>
-void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
-                                    InputSectionBase<ELFT> *Sec,
-                                    StringRef Name) {
-  OutputSectionBase *OutSec;
-  bool IsNew;
-  std::tie(OutSec, IsNew) = Factory.create(Sec, Name);
-  if (IsNew)
-    OutputSections->push_back(OutSec);
-  OutSec->addSection(Sec);
-}
-
-template <class ELFT>
 void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
   for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
     auto Iter = Opt.Commands.begin() + I;
@@ -377,7 +365,7 @@ void LinkerScript<ELFT>::processCommands
 
       // Add input sections to an output section.
       for (InputSectionBase<ELFT> *S : V)
-        addSection(Factory, S, Cmd->Name);
+        Factory.addInputSec(S, Cmd->Name);
     }
   }
 }
@@ -388,7 +376,7 @@ void LinkerScript<ELFT>::addOrphanSectio
     OutputSectionFactory<ELFT> &Factory) {
   for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
     if (S->Live && !S->OutSec)
-      addSection(Factory, S, getOutputSectionName(S->Name));
+      Factory.addInputSec(S, getOutputSectionName(S->Name));
 }
 
 template <class ELFT> static bool isTbss(OutputSectionBase *Sec) {

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=295341&r1=295340&r2=295341&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Thu Feb 16 11:32:26 2017
@@ -277,8 +277,6 @@ public:
 private:
   void computeInputSections(InputSectionDescription *);
 
-  void addSection(OutputSectionFactory<ELFT> &Factory,
-                  InputSectionBase<ELFT> *Sec, StringRef Name);
   void discard(ArrayRef<InputSectionBase<ELFT> *> V);
 
   std::vector<InputSectionBase<ELFT> *>

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=295341&r1=295340&r2=295341&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Feb 16 11:32:26 2017
@@ -554,10 +554,10 @@ static SectionKey createKey(InputSection
   return SectionKey{OutsecName, Flags, Alignment};
 }
 
-template <class ELFT> OutputSectionFactory<ELFT>::OutputSectionFactory() {}
-
-template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
-
+template <class ELFT>
+OutputSectionFactory<ELFT>::OutputSectionFactory(
+    std::vector<OutputSectionBase *> &OutputSections)
+    : OutputSections(OutputSections) {}
 
 static uint64_t getIncompatibleFlags(uint64_t Flags) {
   return Flags & (SHF_ALLOC | SHF_TLS);
@@ -576,34 +576,43 @@ static bool canMergeToProgbits(unsigned
 }
 
 template <class ELFT>
-std::pair<OutputSectionBase *, bool>
-OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
-                                   StringRef OutsecName) {
-  SectionKey Key = createKey(C, OutsecName);
-  uintX_t Flags = getOutFlags(C);
+void OutputSectionFactory<ELFT>::addInputSec(InputSectionBase<ELFT> *IS,
+                                             StringRef OutsecName) {
+  if (!IS->Live) {
+    reportDiscarded(IS);
+    return;
+  }
+
+  SectionKey Key = createKey(IS, OutsecName);
+  uintX_t Flags = getOutFlags(IS);
   OutputSectionBase *&Sec = Map[Key];
   if (Sec) {
-    if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(C->Flags))
+    if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
       error("Section has flags incompatible with others with the same name " +
-            toString(C));
-    if (Sec->Type != C->Type) {
-      if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(C->Type))
+            toString(IS));
+    if (Sec->Type != IS->Type) {
+      if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
         Sec->Type = SHT_PROGBITS;
       else
         error("Section has different type from others with the same name " +
-              toString(C));
+              toString(IS));
     }
     Sec->Flags |= Flags;
-    return {Sec, false};
+  } else {
+    uint32_t Type = IS->Type;
+    if (IS->kind() == InputSectionBase<ELFT>::EHFrame) {
+      Out<ELFT>::EhFrame->addSection(IS);
+      return;
+    }
+    Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
+    OutputSections.push_back(Sec);
   }
 
-  uint32_t Type = C->Type;
-  if (C->kind() == InputSectionBase<ELFT>::EHFrame)
-    return {Out<ELFT>::EhFrame, false};
-  Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
-  return {Sec, true};
+  Sec->addSection(IS);
 }
 
+template <class ELFT> OutputSectionFactory<ELFT>::~OutputSectionFactory() {}
+
 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
   return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
 }

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=295341&r1=295340&r2=295341&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Thu Feb 16 11:32:26 2017
@@ -212,12 +212,13 @@ template <class ELFT> class OutputSectio
   typedef typename ELFT::uint uintX_t;
 
 public:
-  OutputSectionFactory();
+  OutputSectionFactory(std::vector<OutputSectionBase *> &OutputSections);
   ~OutputSectionFactory();
-  std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
-                                              StringRef OutsecName);
+  void addInputSec(InputSectionBase<ELFT> *IS, StringRef OutsecName);
+
 private:
   llvm::SmallDenseMap<SectionKey, OutputSectionBase *> Map;
+  std::vector<OutputSectionBase *> &OutputSections;
 };
 
 template <class ELFT> uint64_t getHeaderSize() {

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=295341&r1=295340&r2=295341&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Feb 16 11:32:26 2017
@@ -53,7 +53,6 @@ private:
   void copyLocalSymbols();
   void addSectionSymbols();
   void addReservedSymbols();
-  void addInputSec(InputSectionBase<ELFT> *S);
   void createSections();
   void forEachRelSec(std::function<void(InputSectionBase<ELFT> &)> Fn);
   void sortSections();
@@ -79,7 +78,7 @@ private:
   std::unique_ptr<FileOutputBuffer> Buffer;
 
   std::vector<OutputSectionBase *> OutputSections;
-  OutputSectionFactory<ELFT> Factory;
+  OutputSectionFactory<ELFT> Factory{OutputSections};
 
   void addRelIpltSymbols();
   void addStartEndSymbols();
@@ -918,27 +917,10 @@ void Writer<ELFT>::forEachRelSec(
   }
 }
 
-template <class ELFT>
-void Writer<ELFT>::addInputSec(InputSectionBase<ELFT> *IS) {
-  if (!IS)
-    return;
-
-  if (!IS->Live) {
-    reportDiscarded(IS);
-    return;
-  }
-  OutputSectionBase *Sec;
-  bool IsNew;
-  StringRef OutsecName = getOutputSectionName(IS->Name);
-  std::tie(Sec, IsNew) = Factory.create(IS, OutsecName);
-  if (IsNew)
-    OutputSections.push_back(Sec);
-  Sec->addSection(IS);
-}
-
 template <class ELFT> void Writer<ELFT>::createSections() {
   for (InputSectionBase<ELFT> *IS : Symtab<ELFT>::X->Sections)
-    addInputSec(IS);
+    if (IS)
+      Factory.addInputSec(IS, getOutputSectionName(IS->Name));
 
   sortBySymbolsOrder<ELFT>(OutputSections);
   sortInitFini<ELFT>(findSection(".init_array"));




More information about the llvm-commits mailing list