Skip to content

Commit

Permalink
Don't GC non-alloc mergeable section pieces
Browse files Browse the repository at this point in the history
Differential revision: https://reviews.llvm.org/D25033

llvm-svn: 282708
  • Loading branch information
eleviant777 committed Sep 29, 2016
1 parent 1abe30a commit d277884
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
13 changes: 10 additions & 3 deletions lld/ELF/InputSection.cpp
Expand Up @@ -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>
Expand Down
13 changes: 5 additions & 8 deletions lld/ELF/MarkLive.cpp
Expand Up @@ -64,20 +64,13 @@ 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) {
SymbolBody &B = Sec.getFile()->getRelocTargetSym(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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/Inputs/comment-gc.s
@@ -0,0 +1 @@
.ident "bar"
14 changes: 14 additions & 0 deletions lld/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
2 changes: 1 addition & 1 deletion lld/test/ELF/debug-gc.s
Expand Up @@ -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

Expand Down

0 comments on commit d277884

Please sign in to comment.