Index: ELF/Symbols.cpp =================================================================== --- ELF/Symbols.cpp +++ ELF/Symbols.cpp @@ -53,6 +53,11 @@ if (!SC) return D.Value; + if (!SC->Live) { + warn("relocation refers to discarded section"); + return D.Value; + } + uintX_t Offset = D.Value; if (D.isSection()) { Offset += Addend; Index: test/ELF/linkerscript/discard-section.s =================================================================== --- test/ELF/linkerscript/discard-section.s +++ test/ELF/linkerscript/discard-section.s @@ -1,10 +1,15 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t # RUN: echo "SECTIONS { /DISCARD/ : { *(.aaa*) } }" > %t.script -# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: ld.lld -o %t1 --script %t.script %t 2>&1 | FileCheck --check-prefix=WARN %s # RUN: llvm-objdump -section-headers %t1 | FileCheck %s + +# WARN: relocation refers to discarded section # CHECK-NOT: .aaa .section .aaa,"a" aaa: .quad 0 + +.section .zzz,"a" + .quad aaa