Index: lld/trunk/ELF/InputSection.cpp =================================================================== --- lld/trunk/ELF/InputSection.cpp +++ lld/trunk/ELF/InputSection.cpp @@ -572,9 +572,16 @@ 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 Index: lld/trunk/ELF/MarkLive.cpp =================================================================== --- lld/trunk/ELF/MarkLive.cpp +++ lld/trunk/ELF/MarkLive.cpp @@ -64,11 +64,6 @@ return Rel.r_addend; } -template static bool IsAlloc(InputSectionBase &Sec) { - return (&Sec != &InputSection::Discarded) && - (Sec.getSectionHdr()->sh_flags & SHF_ALLOC); -} - template static ResolvedReloc resolveReloc(InputSectionBase &Sec, RelT &Rel) { @@ -76,8 +71,6 @@ auto *D = dyn_cast>(&B); if (!D || !D->Section) return {nullptr, 0}; - if (!IsAlloc(Sec) && IsAlloc(*D->Section)) - return {nullptr, 0}; typename ELFT::uint Offset = D->Value; if (D->isSection()) Offset += getAddend(Sec, Rel); @@ -214,8 +207,12 @@ 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 *S = dyn_cast>(R.Sec)) - Q.push_back(S); + if (S->getSectionHdr()->sh_flags & SHF_ALLOC) + Q.push_back(S); }; auto MarkSymbol = [&](const SymbolBody *Sym) { Index: lld/trunk/test/ELF/Inputs/comment-gc.s =================================================================== --- lld/trunk/test/ELF/Inputs/comment-gc.s +++ lld/trunk/test/ELF/Inputs/comment-gc.s @@ -0,0 +1 @@ +.ident "bar" Index: lld/trunk/test/ELF/comment-gc.s =================================================================== --- lld/trunk/test/ELF/comment-gc.s +++ lld/trunk/test/ELF/comment-gc.s @@ -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: lld/trunk/test/ELF/debug-gc.s =================================================================== --- lld/trunk/test/ELF/debug-gc.s +++ lld/trunk/test/ELF/debug-gc.s @@ -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