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>
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) {
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);
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) {
--- /dev/null
+.ident "bar"
--- /dev/null
+# 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
# 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