[PATCH] D39094: [ELF] - Cleanup of processSectionCommands().

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 19 07:37:39 PDT 2017


grimar created this revision.
Herald added a subscriber: emaste.

The way we handle ONLY_IF_RO/OLY_IF_RW constraints in
processSectionCommands is a bit tricky. If input sections
does no satisfy given constraint we remove command from
commands list. It seems too complex, what we can do instead
is to make the OutputCommand empty. So that at later steps
LLD will remove it just like it deal with other empty output commands.
That allows to simplify the loop a bit.


https://reviews.llvm.org/D39094

Files:
  ELF/LinkerScript.cpp
  ELF/OutputSections.cpp


Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -116,6 +116,8 @@
       Type = SHT_PROGBITS;
     }
   }
+  if (Noload)
+    Type = SHT_NOBITS;
 
   IS->Parent = this;
   Flags |= IS->Flags;
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -359,15 +359,16 @@
   Ctx = make_unique<AddressState>();
   Ctx->OutSec = Aether;
 
+  size_t I = 0;
   // Add input sections to output sections.
-  for (size_t I = 0; I < SectionCommands.size(); ++I) {
+  for (BaseCommand *Base : SectionCommands) {
     // Handle symbol assignments outside of any output section.
-    if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) {
+    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
       addSymbol(Cmd);
       continue;
     }
 
-    if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) {
+    if (auto *Sec = dyn_cast<OutputSection>(Base)) {
       std::vector<InputSection *> V = createInputSectionList(*Sec);
 
       // The output section name `/DISCARD/' is special.
@@ -382,13 +383,12 @@
       // sections satisfy a given constraint. If not, a directive is handled
       // as if it wasn't present from the beginning.
       //
-      // Because we'll iterate over SectionCommands many more times, the easiest
-      // way to "make it as if it wasn't present" is to just remove it.
+      // Because we'll iterate over SectionCommands many more times, the easy
+      // way to "make it as if it wasn't present" is to make it empty.
       if (!matchConstraints(V, Sec->Constraint)) {
         for (InputSectionBase *S : V)
           S->Assigned = false;
-        SectionCommands.erase(SectionCommands.begin() + I);
-        --I;
+        Sec->SectionCommands.clear();
         continue;
       }
 
@@ -410,21 +410,11 @@
       // Add input sections to an output section.
       for (InputSection *S : V)
         Sec->addSection(S);
+
+      Sec->SectionIndex = I++;
     }
   }
   Ctx = nullptr;
-
-  // Output sections are emitted in the exact same order as
-  // appeared in SECTIONS command, so we know their section indices.
-  for (size_t I = 0; I < SectionCommands.size(); ++I) {
-    auto *Sec = dyn_cast<OutputSection>(SectionCommands[I]);
-    if (!Sec)
-      continue;
-    assert(Sec->SectionIndex == INT_MAX);
-    Sec->SectionIndex = I;
-    if (Sec->Noload)
-      Sec->Type = SHT_NOBITS;
-  }
 }
 
 // If no SECTIONS command was given, we create simple SectionCommands


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39094.119584.patch
Type: text/x-patch
Size: 2615 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171019/c7ca8b6a/attachment.bin>


More information about the llvm-commits mailing list