Index: lld/trunk/ELF/InputFiles.cpp =================================================================== --- lld/trunk/ELF/InputFiles.cpp +++ lld/trunk/ELF/InputFiles.cpp @@ -406,8 +406,12 @@ // from the output, so returning `nullptr` for the normal case. // However, if -emit-relocs is given, we need to leave them in the output. // (Some post link analysis tools need this information.) - if (Config->EmitRelocs) - return make>(this, &Sec, Name); + if (Config->EmitRelocs) { + InputSection *RelocSec = make>(this, &Sec, Name); + // We will not emit relocation section if target was discarded. + Target->DependentSections.push_back(RelocSec); + return RelocSec; + } return nullptr; } } Index: lld/trunk/test/ELF/linkerscript/emit-relocs-discard.s =================================================================== --- lld/trunk/test/ELF/linkerscript/emit-relocs-discard.s +++ lld/trunk/test/ELF/linkerscript/emit-relocs-discard.s @@ -0,0 +1,14 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: echo "SECTIONS { /DISCARD/ : { *(.bbb) } }" > %t.script +# RUN: ld.lld --emit-relocs --script %t.script %t.o -o %t1 +# RUN: llvm-readobj -r %t1 | FileCheck %s + +# CHECK: Relocations [ +# CHECK-NEXT: ] + +.section .aaa,"",@progbits +.Lfoo: + +.section .bbb,"",@progbits +.long .Lfoo Index: lld/trunk/test/ELF/linkerscript/emit-relocs-ehframe-discard.s =================================================================== --- lld/trunk/test/ELF/linkerscript/emit-relocs-ehframe-discard.s +++ lld/trunk/test/ELF/linkerscript/emit-relocs-ehframe-discard.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: echo "SECTIONS { /DISCARD/ : { *(.eh_frame) } }" > %t.script +# RUN: ld.lld --emit-relocs --script %t.script %t1.o -o %t +# RUN: llvm-objdump -section-headers %t | FileCheck %s + +# CHECK-NOT: .rela.eh_frame + +.section .foo,"ax",@progbits +.cfi_startproc +.cfi_endproc