Skip to content

Commit

Permalink
[ELF] - Do not crash when discarding sections that are referenced by …
Browse files Browse the repository at this point in the history
…others.

SHF_LINK_ORDER sections adds special ordering requirements.
Such sections references other sections. Previously we would crash
if section that other were referenced to was discarded by script.

Patch fixes that by discarding all dependent sections in that case.
It supports chained dependencies, testcase is provided.

Differential revision: https://reviews.llvm.org/D30033

llvm-svn: 295332
  • Loading branch information
George Rimar committed Feb 16, 2017
1 parent cb2d950 commit 505ac8d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -277,6 +277,11 @@ void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
for (InputSectionBase<ELFT> *S : V) {
S->Live = false;
reportDiscarded(S);

InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S);
if (!IS || IS->DependentSections.empty())
continue;
discard(IS->DependentSections);
}
}

Expand Down
32 changes: 32 additions & 0 deletions lld/test/ELF/linkerscript/discard-section-metadata.s
@@ -0,0 +1,32 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
# RUN: echo "SECTIONS { /DISCARD/ : { *(.foo) } }" > %t.script
# RUN: ld.lld -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 | FileCheck %s

# CHECK-NOT: .foo
# CHECK-NOT: .bar
# CHECK-NOT: .zed
# CHECK-NOT: .moo

## Sections dependency tree for testcase is:
## (.foo)
## | |
## | --(.bar)
## |
## --(.zed)
## |
## --(.moo)
##

.section .foo,"a"
.quad 0

.section .bar,"am",@progbits,.foo
.quad 0

.section .zed,"am",@progbits,.foo
.quad 0

.section .moo,"am",@progbits,.zed
.quad 0

0 comments on commit 505ac8d

Please sign in to comment.