Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ 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: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ 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); @@ -215,7 +208,8 @@ return; R.Sec->Live = true; 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: test/ELF/debug-gc.s =================================================================== --- test/ELF/debug-gc.s +++ 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