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

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 08:26:58 PDT 2017


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

Code that removes unused synthetic sections has a bug
that leaves unused section in output if its description is duplicated
in linkerscript. Testcase shows the issue, patch fixes it.


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,9 +1,9 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: echo "SECTIONS { \
-# RUN:    .got  : { *(.got) } \
-# RUN:    .plt  : { *(.plt) } \
-# RUN:    .text : { *(.text) } \
+# RUN: echo "SECTIONS {                             \
+# RUN:    .got  : { *(.got) *(.got) }               \
+# RUN:    .plt  : { *(.plt) }                       \
+# RUN:    .text : { *(.text) }                      \
 # RUN:  }" > %t.script
 # RUN: ld.lld -shared -o %t.so --script %t.script %t.o
 
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1201,19 +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) {
+    for (auto I = OS->Commands.begin(); I != OS->Commands.end();) {
       BaseCommand *B = *I;
       if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
         auto P = std::find(ISD->Sections.begin(), ISD->Sections.end(), SS);
         if (P != ISD->Sections.end())
           ISD->Sections.erase(P);
-        if (ISD->Sections.empty())
-          Empty = I;
+        if (ISD->Sections.empty()) {
+          I = OS->Commands.erase(I);
+          continue;
+        }
       }
+      ++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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37520.114013.patch
Type: text/x-patch
Size: 1813 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170906/b2c1d247/attachment.bin>


More information about the llvm-commits mailing list