Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -277,6 +277,11 @@ for (InputSectionBase *S : V) { S->Live = false; reportDiscarded(S); + + InputSection *IS = dyn_cast>(S); + if (!IS || IS->DependentSections.empty()) + continue; + discard(IS->DependentSections); } } Index: lld/trunk/test/ELF/linkerscript/discard-section-metadata.s =================================================================== --- lld/trunk/test/ELF/linkerscript/discard-section-metadata.s +++ lld/trunk/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