[lld] r307043 - Move clearOutputSections earlier.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 3 09:54:39 PDT 2017


Author: rafael
Date: Mon Jul  3 09:54:39 2017
New Revision: 307043

URL: http://llvm.org/viewvc/llvm-project?rev=307043&view=rev
Log:
Move clearOutputSections earlier.

Now removeUnusedSyntheticSections operates entirely on the linker
script.

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=307043&r1=307042&r2=307043&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Jul  3 09:54:39 2017
@@ -1132,7 +1132,7 @@ static void applySynthetic(const std::ve
 // 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 the output.
-static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
+static void removeUnusedSyntheticSections() {
   // All input synthetic sections that can be empty are placed after
   // all regular ones. We iterate over them all and exit at first
   // non-synthetic.
@@ -1145,12 +1145,24 @@ static void removeUnusedSyntheticSection
       continue;
     if ((SS == InX::Got || SS == InX::MipsGot) && ElfSym::GlobalOffsetTable)
       continue;
-    OS->Sections.erase(std::find(OS->Sections.begin(), OS->Sections.end(), SS));
-    SS->Live = false;
+
+    OutputSectionCommand *Cmd = Script->getCmd(OS);
+    BaseCommand **Empty = nullptr;
+    for (BaseCommand *&B : Cmd->Commands) {
+      if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
+        auto I = std::find(ISD->Sections.begin(), ISD->Sections.end(), SS);
+        if (I != ISD->Sections.end())
+          ISD->Sections.erase(I);
+        if (ISD->Sections.empty())
+          Empty = &B;
+      }
+    }
+    if (Empty)
+      Cmd->Commands.erase(std::vector<BaseCommand *>::iterator(Empty));
+
     // If there are no other sections in the output section, remove it from the
     // output.
-    if (OS->Sections.empty()) {
-      V.erase(std::find(V.begin(), V.end(), OS));
+    if (Cmd->Commands.empty()) {
       // Also remove script commands matching the output section.
       auto &Cmds = Script->Opt.Commands;
       auto I = std::remove_if(Cmds.begin(), Cmds.end(), [&](BaseCommand *Cmd) {
@@ -1227,9 +1239,9 @@ template <class ELFT> void Writer<ELFT>:
     return;
 
   addPredefinedSections();
-  removeUnusedSyntheticSections(OutputSections);
-
   clearOutputSections();
+  removeUnusedSyntheticSections();
+
   sortSections();
 
   // Now that we have the final list, create a list of all the




More information about the llvm-commits mailing list