[lld] r315129 - Do not mutate Script->Opt.Commands from a leaf helper function.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 6 16:34:43 PDT 2017


Author: ruiu
Date: Fri Oct  6 16:34:43 2017
New Revision: 315129

URL: http://llvm.org/viewvc/llvm-project?rev=315129&view=rev
Log:
Do not mutate Script->Opt.Commands from a leaf helper function.

Factory::addInputSec added an output section to Script->Opt.Commands,
but that is too subtle. This patch makes it explicit so that it is easy
to see when a new element is added to Script->Opt.Commands.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    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=315129&r1=315128&r2=315129&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Oct  6 16:34:43 2017
@@ -417,6 +417,7 @@ void LinkerScript::processCommands(Outpu
       // Add input sections to an output section.
       for (InputSectionBase *S : V)
         Factory.addInputSec(S, Sec->Name, Sec);
+
       assert(Sec->SectionIndex == INT_MAX);
       Sec->SectionIndex = I;
       if (Sec->Noload)
@@ -466,10 +467,12 @@ void LinkerScript::addOrphanSections(Out
     if (OutputSection *Sec = findByName(
             makeArrayRef(Opt.Commands).slice(0, End), Name)) {
       Factory.addInputSec(S, Name, Sec);
-    } else {
-      Factory.addInputSec(S, Name, nullptr);
-      assert(S->getOutputSection()->SectionIndex == INT_MAX);
+      continue;
     }
+
+    if (OutputSection *OS = Factory.addInputSec(S, Name))
+      Script->Opt.Commands.push_back(OS);
+    assert(S->getOutputSection()->SectionIndex == INT_MAX);
   }
 }
 

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=315129&r1=315128&r2=315129&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Fri Oct  6 16:34:43 2017
@@ -206,8 +206,6 @@ static OutputSection *createSection(Inpu
   Sec->Flags = IS->Flags;
   Sec->addSection(cast<InputSection>(IS));
 
-  Script->Opt.Commands.push_back(Sec);
-
   return Sec;
 }
 
@@ -240,15 +238,17 @@ static void addSection(OutputSection *Se
 void OutputSectionFactory::addInputSec(InputSectionBase *IS,
                                        StringRef OutsecName,
                                        OutputSection *OS) {
-  if (!IS->Live) {
+  if (IS->Live)
+    addSection(OS, IS, OutsecName);
+  else
     reportDiscarded(IS);
-    return;
-  }
+}
 
-  // If we have destination output section - use it directly.
-  if (OS) {
-    addSection(OS, IS, OutsecName);
-    return;
+OutputSection *OutputSectionFactory::addInputSec(InputSectionBase *IS,
+                                                 StringRef OutsecName) {
+  if (!IS->Live) {
+    reportDiscarded(IS);
+    return nullptr;
   }
 
   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
@@ -258,10 +258,8 @@ void OutputSectionFactory::addInputSec(I
   // However, for the -r option, we want to pass through all section groups
   // as-is because adding/removing members or merging them with other groups
   // change their semantics.
-  if (IS->Type == SHT_GROUP || (IS->Flags & SHF_GROUP)) {
-    createSection(IS, OutsecName);
-    return;
-  }
+  if (IS->Type == SHT_GROUP || (IS->Flags & SHF_GROUP))
+    return createSection(IS, OutsecName);
 
   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
   // relocation sections .rela.foo and .rela.bar for example. Most tools do
@@ -274,19 +272,24 @@ void OutputSectionFactory::addInputSec(I
     auto *Sec = cast<InputSection>(IS);
     OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
 
-    if (Out->RelocationSection)
+    if (Out->RelocationSection) {
       addSection(Out->RelocationSection, IS, OutsecName);
-    else
-      Out->RelocationSection = createSection(IS, OutsecName);
-    return;
+      return nullptr;
+    }
+
+    Out->RelocationSection = createSection(IS, OutsecName);
+    return Out->RelocationSection;
   }
 
   SectionKey Key = createKey(IS, OutsecName);
   OutputSection *&Sec = Map[Key];
-  if (Sec)
+  if (Sec) {
     addSection(Sec, IS, OutsecName);
-  else
-    Sec = createSection(IS, OutsecName);
+    return nullptr;
+  }
+
+  Sec = createSection(IS, OutsecName);
+  return Sec;
 }
 
 OutputSectionFactory::~OutputSectionFactory() {}

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=315129&r1=315128&r2=315129&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Fri Oct  6 16:34:43 2017
@@ -164,6 +164,8 @@ public:
   OutputSectionFactory();
   ~OutputSectionFactory();
 
+  OutputSection *addInputSec(InputSectionBase *IS, StringRef OutsecName);
+
   void addInputSec(InputSectionBase *IS, StringRef OutsecName,
                    OutputSection *OS);
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=315129&r1=315128&r2=315129&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Oct  6 16:34:43 2017
@@ -876,13 +876,15 @@ void Writer<ELFT>::forEachRelSec(std::fu
 }
 
 template <class ELFT> void Writer<ELFT>::createSections() {
-  std::vector<BaseCommand *> Old = Script->Opt.Commands;
-  Script->Opt.Commands.clear();
+  std::vector<OutputSection *> Vec;
   for (InputSectionBase *IS : InputSections)
     if (IS)
-      Factory.addInputSec(IS, getOutputSectionName(IS->Name), nullptr);
-  Script->Opt.Commands.insert(Script->Opt.Commands.end(), Old.begin(),
-                              Old.end());
+      if (OutputSection *Sec =
+              Factory.addInputSec(IS, getOutputSectionName(IS->Name)))
+        Vec.push_back(Sec);
+
+  Script->Opt.Commands.insert(Script->Opt.Commands.begin(), Vec.begin(),
+                              Vec.end());
 
   Script->fabricateDefaultCommands();
   sortBySymbolsOrder();




More information about the llvm-commits mailing list