Don't GC non-alloc mergeable section pieces
authorEugene Leviant <evgeny.leviant@gmail.com>
Thu, 29 Sep 2016 10:27:10 +0000 (10:27 +0000)
committerEugene Leviant <evgeny.leviant@gmail.com>
Thu, 29 Sep 2016 10:27:10 +0000 (10:27 +0000)
Differential revision: https://reviews.llvm.org/D25033

llvm-svn: 282708

lld/ELF/InputSection.cpp
lld/ELF/MarkLive.cpp
lld/test/ELF/Inputs/comment-gc.s [new file with mode: 0644]
lld/test/ELF/comment-gc.s [new file with mode: 0644]
lld/test/ELF/debug-gc.s

index 36b7748..5d7312b 100644 (file)
@@ -572,9 +572,16 @@ template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
   else
     this->Pieces = splitNonStrings(Data, EntSize);
 
-  if (Config->GcSections)
-    for (uintX_t Off : LiveOffsets)
-      this->getSectionPiece(Off)->Live = true;
+  if (Config->GcSections) {
+    if (this->getSectionHdr()->sh_flags & SHF_ALLOC) {
+      for (uintX_t Off : LiveOffsets)
+        this->getSectionPiece(Off)->Live = true;
+      return;
+    }
+
+    for (SectionPiece &Piece : this->Pieces)
+      Piece.Live = true;
+  }
 }
 
 template <class ELFT>
index 724a8bf..4fadd03 100644 (file)
@@ -64,11 +64,6 @@ static typename ELFT::uint getAddend(InputSectionBase<ELFT> &Sec,
   return Rel.r_addend;
 }
 
-template <class ELFT> static bool IsAlloc(InputSectionBase<ELFT> &Sec) {
-  return (&Sec != &InputSection<ELFT>::Discarded) &&
-         (Sec.getSectionHdr()->sh_flags & SHF_ALLOC);
-}
-
 template <class ELFT, class RelT>
 static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
                                         RelT &Rel) {
@@ -76,8 +71,6 @@ static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
   auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
   if (!D || !D->Section)
     return {nullptr, 0};
-  if (!IsAlloc<ELFT>(Sec) && IsAlloc<ELFT>(*D->Section))
-    return {nullptr, 0};
   typename ELFT::uint Offset = D->Value;
   if (D->isSection())
     Offset += getAddend(Sec, Rel);
@@ -214,8 +207,12 @@ template <class ELFT> void elf::markLive() {
     if (R.Sec->Live)
       return;
     R.Sec->Live = true;
+    // Add input section to the queue. We don't want to consider relocations
+    // from non-allocatable input sections, because we can bring those
+    // allocatable sections to living which otherwise would be dead.
     if (InputSection<ELFT> *S = dyn_cast<InputSection<ELFT>>(R.Sec))
-      Q.push_back(S);
+      if (S->getSectionHdr()->sh_flags & SHF_ALLOC)
+        Q.push_back(S);
   };
 
   auto MarkSymbol = [&](const SymbolBody *Sym) {
diff --git a/lld/test/ELF/Inputs/comment-gc.s b/lld/test/ELF/Inputs/comment-gc.s
new file mode 100644 (file)
index 0000000..2453ce9
--- /dev/null
@@ -0,0 +1 @@
+.ident "bar"
diff --git a/lld/test/ELF/comment-gc.s b/lld/test/ELF/comment-gc.s
new file mode 100644 (file)
index 0000000..0ecd39c
--- /dev/null
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/comment-gc.s -o %t2.o
+# RUN: ld.lld %t.o %t2.o -o %t1 --gc-sections -shared
+# RUN: llvm-objdump -s %t1 | FileCheck %s
+
+# CHECK:      Contents of section .comment:
+# CHECK-NEXT:  0000 00666f6f 00626172 00 .foo.bar.
+
+.ident "foo"
+
+.globl _start
+_start:
+  nop
index 7dad696..1618471 100644 (file)
@@ -4,7 +4,7 @@
 # RUN: llvm-objdump -s %t1 | FileCheck %s
 
 # CHECK:      Contents of section .debug_str:
-# CHECK-NEXT:  0000 41414100 42424200   AAA.BBB.
+# CHECK-NEXT:  0000 41414100 42424200 43434300           AAA.BBB.CCC.
 # CHECK:      Contents of section .debug_info:
 # CHECK-NEXT:  0000 00000000 04000000