[lld] r325763 - [ELF] - Introduce getInputSections() helper.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 22 01:55:28 PST 2018


Author: grimar
Date: Thu Feb 22 01:55:28 2018
New Revision: 325763

URL: http://llvm.org/viewvc/llvm-project?rev=325763&view=rev
Log:
[ELF] - Introduce getInputSections() helper.

We sometimes need to iterate over input sections for a given
output section. It is not very convinent because we have to iterate
over section descriptions.
Patch introduces getInputSections helper, it simplifies things.

Differential revision: https://reviews.llvm.org/D43574

Modified:
    lld/trunk/ELF/MapFile.cpp
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/MapFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MapFile.cpp?rev=325763&r1=325762&r2=325763&view=diff
==============================================================================
--- lld/trunk/ELF/MapFile.cpp (original)
+++ lld/trunk/ELF/MapFile.cpp Thu Feb 22 01:55:28 2018
@@ -137,17 +137,11 @@ void elf::writeMapFile() {
     OS << OSec->Name << '\n';
 
     // Dump symbols for each input section.
-    for (BaseCommand *Base : OSec->SectionCommands) {
-      auto *ISD = dyn_cast<InputSectionDescription>(Base);
-      if (!ISD)
-        continue;
-      for (InputSection *IS : ISD->Sections) {
-        writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
-                    IS->Alignment);
-        OS << indent(1) << toString(IS) << '\n';
-        for (Symbol *Sym : SectionSyms[IS])
-          OS << SymStr[Sym] << '\n';
-      }
+    for (InputSection *IS : getInputSections(OSec)) {
+      writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment);
+      OS << indent(1) << toString(IS) << '\n';
+      for (Symbol *Sym : SectionSyms[IS])
+        OS << SymStr[Sym] << '\n';
     }
   }
 }

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=325763&r1=325762&r2=325763&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Thu Feb 22 01:55:28 2018
@@ -231,12 +231,7 @@ template <class ELFT> void OutputSection
   }
 
   // Write leading padding.
-  std::vector<InputSection *> Sections;
-  for (BaseCommand *Cmd : SectionCommands)
-    if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
-      for (InputSection *IS : ISD->Sections)
-        if (IS->Live)
-          Sections.push_back(IS);
+  std::vector<InputSection *> Sections = getInputSections(this);
   uint32_t Filler = getFiller();
   if (Filler)
     fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler);
@@ -281,17 +276,13 @@ static void finalizeShtGroup(OutputSecti
 }
 
 template <class ELFT> void OutputSection::finalize() {
-  InputSection *First = nullptr;
-  for (BaseCommand *Base : SectionCommands) {
-    if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
-      if (ISD->Sections.empty())
-        continue;
-      if (First == nullptr)
-        First = ISD->Sections.front();
-    }
-    if (isa<ByteCommand>(Base) && Type == SHT_NOBITS)
-      Type = SHT_PROGBITS;
-  }
+  if (Type == SHT_NOBITS)
+    for (BaseCommand *Base : SectionCommands)
+      if (isa<ByteCommand>(Base))
+        Type = SHT_PROGBITS;
+
+  std::vector<InputSection *> V = getInputSections(this);
+  InputSection *First = V.empty() ? nullptr : V[0];
 
   if (Flags & SHF_LINK_ORDER) {
     // We must preserve the link order dependency of sections with the
@@ -394,6 +385,14 @@ int elf::getPriority(StringRef S) {
   return V;
 }
 
+std::vector<InputSection *> elf::getInputSections(OutputSection *OS) {
+  std::vector<InputSection *> Ret;
+  for (BaseCommand *Base : OS->SectionCommands)
+    if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
+      Ret.insert(Ret.end(), ISD->Sections.begin(), ISD->Sections.end());
+  return Ret;
+}
+
 // Sorts input sections by section name suffixes, so that .foo.N comes
 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
 // We want to keep the original order if the priorities are the same

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=325763&r1=325762&r2=325763&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Thu Feb 22 01:55:28 2018
@@ -120,6 +120,8 @@ private:
 
 int getPriority(StringRef S);
 
+std::vector<InputSection *> getInputSections(OutputSection* OS);
+
 // All output sections that are handled by the linker specially are
 // globally accessible. Writer initializes them, so don't use them
 // until Writer is initialized.

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=325763&r1=325762&r2=325763&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Thu Feb 22 01:55:28 2018
@@ -2579,12 +2579,9 @@ void ARMExidxSentinelSection::writeTo(ui
 
 // The sentinel has to be removed if there are no other .ARM.exidx entries.
 bool ARMExidxSentinelSection::empty() const {
-  OutputSection *OS = getParent();
-  for (auto *B : OS->SectionCommands)
-    if (auto *ISD = dyn_cast<InputSectionDescription>(B))
-      for (auto *S : ISD->Sections)
-        if (!isa<ARMExidxSentinelSection>(S))
-          return false;
+  for (InputSection *IS : getInputSections(getParent()))
+    if (!isa<ARMExidxSentinelSection>(IS))
+      return false;
   return true;
 }
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=325763&r1=325762&r2=325763&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Feb 22 01:55:28 2018
@@ -1392,12 +1392,7 @@ static void removeUnusedSyntheticSection
 
     // If there are no other alive sections or commands left in the output
     // section description, we remove it from the output.
-    bool IsEmpty = llvm::all_of(OS->SectionCommands, [](BaseCommand *B) {
-      if (auto *ISD = dyn_cast<InputSectionDescription>(B))
-        return ISD->Sections.empty();
-      return false;
-    });
-    if (IsEmpty)
+    if (getInputSections(OS).empty())
       OS->Live = false;
   }
 }




More information about the llvm-commits mailing list