[lld] r288707 - Split removeUnusedSyntheticSections into two functions.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 5 13:37:16 PST 2016


Author: ruiu
Date: Mon Dec  5 15:37:16 2016
New Revision: 288707

URL: http://llvm.org/viewvc/llvm-project?rev=288707&view=rev
Log:
Split removeUnusedSyntheticSections into two functions.

Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=288707&r1=288706&r2=288707&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Dec  5 15:37:16 2016
@@ -60,6 +60,7 @@ private:
   void sortSections();
   void finalizeSections();
   void addPredefinedSections();
+  void removeEmptyOutputSections();
 
   std::vector<Phdr> createPhdrs();
   void addPtArmExid(std::vector<Phdr> &Phdrs);
@@ -910,29 +911,38 @@ finalizeSynthetic(const std::vector<Synt
 }
 
 // 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 <class ELFT>
-static void removeUnusedSyntheticSections(std::vector<OutputSectionBase *> &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 <class ELFT> 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<ELFT> *S : llvm::reverse(Symtab<ELFT>::X->Sections)) {
-    SyntheticSection<ELFT> *SS = dyn_cast<SyntheticSection<ELFT>>(S);
-    if (!SS)
+  for (InputSectionBase<ELFT> *Sec : llvm::reverse(Symtab<ELFT>::X->Sections)) {
+    SyntheticSection<ELFT> *In = dyn_cast<SyntheticSection<ELFT>>(S);
+    if (!In)
       return;
-    if (!SS->empty() || !SS->OutSec)
+    if (!In->empty() || !In->OutSec)
       continue;
 
-    OutputSection<ELFT> *OutSec = cast<OutputSection<ELFT>>(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<ELFT> *Out = cast<OutputSection<ELFT>>(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 <class ELFT> void Writer<ELFT>::removeEmptyOutputSections() {
+  OutputSections.erase(
+      std::remove_if(OutputSections.begin(), OutputSections.end(),
+                     [](OutputSectionBase *Sec) {
+                       if (auto *S = dyn_cast<OutputSection<ELFT>>(Sec))
+                         return S->Sections.empty();
+                       return false;
+                     }),
+      OutputSections.end());
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::finalizeSections() {
   Out<ELFT>::DebugInfo = findSection(".debug_info");
@@ -993,7 +1003,8 @@ template <class ELFT> void Writer<ELFT>:
   // So far we have added sections from input object files.
   // This function adds linker-created Out<ELFT>::* sections.
   addPredefinedSections();
-  removeUnusedSyntheticSections<ELFT>(OutputSections);
+  removeEmptySyntheticSections<ELFT>();
+  removeEmptyOutputSections();
 
   sortSections();
 




More information about the llvm-commits mailing list