[PATCH] D37520: [ELF] - Fix removing of unused synthetic sections.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 8 06:54:47 PDT 2017


grimar updated this revision to Diff 114357.
grimar added a comment.

- Rewrote comment.


https://reviews.llvm.org/D37520

Files:
  ELF/Writer.cpp
  test/ELF/linkerscript/unused-synthetic.s


Index: test/ELF/linkerscript/unused-synthetic.s
===================================================================
--- test/ELF/linkerscript/unused-synthetic.s
+++ test/ELF/linkerscript/unused-synthetic.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: echo "SECTIONS { \
-# RUN:    .got  : { *(.got) } \
+# RUN:    .got  : { *(.got) *(.got) } \
 # RUN:    .plt  : { *(.plt) } \
 # RUN:    .text : { *(.text) } \
 # RUN:  }" > %t.script
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1201,21 +1201,19 @@
     if ((SS == InX::Got || SS == InX::MipsGot) && ElfSym::GlobalOffsetTable)
       continue;
 
-    std::vector<BaseCommand *>::iterator Empty = OS->Commands.end();
-    for (auto I = OS->Commands.begin(), E = OS->Commands.end(); I != E; ++I) {
-      BaseCommand *B = *I;
-      if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
+    // Remove unused synthetic section.
+    for (BaseCommand *B : OS->Commands)
+      if (auto *ISD = dyn_cast<InputSectionDescription>(B))
         llvm::erase_if(ISD->Sections,
                        [=](InputSection *IS) { return IS == SS; });
-        if (ISD->Sections.empty())
-          Empty = I;
-      }
-    }
-    if (Empty != OS->Commands.end())
-      OS->Commands.erase(Empty);
 
-    // If there are no other sections in the output section, remove it from the
-    // output.
+    // Remove whole section description commands if them became empty.
+    llvm::erase_if(OS->Commands, [](BaseCommand *B) {
+      auto *ISD = dyn_cast<InputSectionDescription>(B);
+      return ISD && ISD->Sections.empty();
+    });
+
+    // Remove whole output section if it is empty finally.
     if (OS->Commands.empty())
       llvm::erase_if(Script->Opt.Commands,
                      [&](BaseCommand *Cmd) { return Cmd == OS; });


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37520.114357.patch
Type: text/x-patch
Size: 1968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170908/6674130d/attachment.bin>


More information about the llvm-commits mailing list