[lld] r276538 - Split LinkerScript::createSections.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 23 18:06:19 PDT 2016


Author: ruiu
Date: Sat Jul 23 20:06:18 2016
New Revision: 276538

URL: http://llvm.org/viewvc/llvm-project?rev=276538&view=rev
Log:
Split LinkerScript::createSections.

createSections is getting longer, so it is probably time to split.

Differential Revision: https://reviews.llvm.org/D22730

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

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=276538&r1=276537&r2=276538&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sat Jul 23 20:06:18 2016
@@ -261,26 +261,15 @@ std::vector<OutputSectionBase<ELFT> *>
 LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
   typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile;
   std::vector<OutputSectionBase<ELFT> *> Result;
-  DenseSet<OutputSectionBase<ELFT> *> Removed;
 
   // 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,
-                         ConstraintKind Constraint) {
+  auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
     OutputSectionBase<ELFT> *Sec;
     bool IsNew;
     std::tie(Sec, IsNew) = Factory.create(C, Name);
     if (IsNew)
       Result.push_back(Sec);
-    if ((!(C->getSectionHdr()->sh_flags & SHF_WRITE)) &&
-        Constraint == ReadWrite) {
-      Removed.insert(Sec);
-      return;
-    }
-    if ((C->getSectionHdr()->sh_flags & SHF_WRITE) && Constraint == ReadOnly) {
-      Removed.insert(Sec);
-      return;
-    }
     Sec->addSection(C);
   };
 
@@ -306,7 +295,7 @@ LinkerScript<ELFT>::createSections(Outpu
             if (OutCmd->Name == "/DISCARD/")
               S->Live = false;
             else
-              AddInputSec(S, OutCmd->Name, OutCmd->Constraint);
+              AddInputSec(S, OutCmd->Name);
           }
         }
       }
@@ -318,18 +307,48 @@ LinkerScript<ELFT>::createSections(Outpu
     for (InputSectionBase<ELFT> *S : F->getSections()) {
       if (!isDiscarded(S)) {
         if (!S->OutSec)
-          AddInputSec(S, getOutputSectionName(S), NoConstraint);
+          AddInputSec(S, getOutputSectionName(S));
       } else
         reportDiscarded(S, F);
     }
 
-  // Remove from the output all the sections which did not met the constraints.
-  Result.erase(std::remove_if(Result.begin(), Result.end(),
-                              [&](OutputSectionBase<ELFT> *Sec) {
-                                return Removed.count(Sec);
-                              }),
-               Result.end());
-  return Result;
+  // Remove from the output all the sections which did not meet
+  // the optional constraints.
+  return filter(Result);
+}
+
+// Process ONLY_IF_RO and ONLY_IF_RW.
+template <class ELFT>
+std::vector<OutputSectionBase<ELFT> *>
+LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
+  // Sections and OutputSectionCommands are parallel arrays.
+  // In this loop, we remove output sections if they don't satisfy
+  // requested properties.
+  auto It = Sections.begin();
+  for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
+    auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
+    if (!Cmd)
+      continue;
+
+    OutputSectionBase<ELFT> *Sec = *It;
+    switch (Cmd->Constraint) {
+    case ConstraintKind::NoConstraint:
+      ++It;
+      continue;
+    case ConstraintKind::ReadOnly:
+      if (Sec->getFlags() & SHF_WRITE)
+        break;
+      ++It;
+      continue;
+    case ConstraintKind::ReadWrite:
+      if (!(Sec->getFlags() & SHF_WRITE))
+        break;
+      ++It;
+      continue;
+    }
+    Sections.erase(It);
+  }
+  return Sections;
 }
 
 template <class ELFT>

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=276538&r1=276537&r2=276538&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Sat Jul 23 20:06:18 2016
@@ -130,6 +130,9 @@ private:
   // "ScriptConfig" is a bit too long, so define a short name for it.
   ScriptConfiguration &Opt = *ScriptConfig;
 
+  std::vector<OutputSectionBase<ELFT> *>
+  filter(std::vector<OutputSectionBase<ELFT> *> &Sections);
+
   int getSectionIndex(StringRef Name);
   std::vector<size_t> getPhdrIndicesForSection(StringRef Name);
   void dispatchAssignment(SymbolAssignment *Cmd);




More information about the llvm-commits mailing list