[lld] r324463 - [ELF] - Remove unused synthetic sections correctly.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 01:11:07 PST 2018


Author: grimar
Date: Wed Feb  7 01:11:07 2018
New Revision: 324463

URL: http://llvm.org/viewvc/llvm-project?rev=324463&view=rev
Log:
[ELF] - Remove unused synthetic sections correctly.

This is PR35740 which now crashes
because we remove unused synthetic sections incorrectly.

We can keep input section description and corresponding output
section live even if it must be empty and dead. 
This results in a crash because SHF_LINK_ORDER handling code
tries to access first section which is nullptr in this case.

Patch fixes the issue.

Differential revision: https://reviews.llvm.org/D42681

Added:
    lld/trunk/test/ELF/linkerscript/unused-synthetic2.s
Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/linkerscript/unused-synthetic.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=324463&r1=324462&r2=324463&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Feb  7 01:11:07 2018
@@ -1346,23 +1346,21 @@ static void removeUnusedSyntheticSection
     if (!OS || !SS->empty())
       continue;
 
-    std::vector<BaseCommand *>::iterator Empty = OS->SectionCommands.end();
-    for (auto I = OS->SectionCommands.begin(), E = OS->SectionCommands.end();
-         I != E; ++I) {
-      BaseCommand *B = *I;
-      if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
+    // If we reach here, then SS is an unused synthetic section and we want to
+    // remove it from corresponding input section description of output section.
+    for (BaseCommand *B : OS->SectionCommands)
+      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->SectionCommands.end())
-      OS->SectionCommands.erase(Empty);
 
-    // If there are no other sections in the output section, remove it from the
-    // output.
-    if (OS->SectionCommands.empty())
+    // If there are no other alive sections or commands left in the output
+    // section description, we remove it from the output.
+    bool IsEmpty = llvm::all_of(OS->SectionCommands, [](BaseCommand *B) {
+      if (auto *ISD = dyn_cast<InputSectionDescription>(B))
+        return ISD->Sections.empty();
+      return false;
+    });
+    if (IsEmpty)
       OS->Live = false;
   }
 }

Modified: lld/trunk/test/ELF/linkerscript/unused-synthetic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/unused-synthetic.s?rev=324463&r1=324462&r2=324463&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/unused-synthetic.s (original)
+++ lld/trunk/test/ELF/linkerscript/unused-synthetic.s Wed Feb  7 01:11:07 2018
@@ -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

Added: lld/trunk/test/ELF/linkerscript/unused-synthetic2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/unused-synthetic2.s?rev=324463&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/unused-synthetic2.s (added)
+++ lld/trunk/test/ELF/linkerscript/unused-synthetic2.s Wed Feb  7 01:11:07 2018
@@ -0,0 +1,9 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=armv7-unknown-linux-gnueabi %s -o %t.o
+# RUN: echo "SECTIONS { .trap : { *(.ARM.exidx) *(.dummy) } }" > %t.script
+
+## We incorrectly removed unused synthetic sections and crashed before.
+## Check we do not crash and do not produce .trap output section.
+# RUN: ld.lld -shared -o %t.so --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
+# CHECK-NOT: .trap




More information about the llvm-commits mailing list