Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -64,6 +64,11 @@ 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) { @@ -71,6 +76,8 @@ 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); @@ -208,8 +215,7 @@ return; R.Sec->Live = true; if (InputSection *S = dyn_cast>(R.Sec)) - if (S->getSectionHdr()->sh_flags & SHF_ALLOC) - Q.push_back(S); + Q.push_back(S); }; auto MarkSymbol = [&](const SymbolBody *Sym) { Index: test/ELF/debug-gc.s =================================================================== --- test/ELF/debug-gc.s +++ test/ELF/debug-gc.s @@ -0,0 +1,22 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld %t.o -o %t1 --gc-sections +# RUN: llvm-objdump -s %t1 | FileCheck %s + +# CHECK: Contents of section .debug_str: +# CHECK-NEXT: 0000 41414100 42424200 AAA.BBB. +# CHECK: Contents of section .debug_info: +# CHECK-NEXT: 0000 00000000 04000000 + +.globl _start +_start: + +.section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "AAA" +.Linfo_string1: + .asciz "BBB" + +.section .debug_info,"",@progbits + .long .Linfo_string0 + .long .Linfo_string1