Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -561,6 +561,15 @@ (PieceI->InputOff + PieceI->size() <= RI.r_offset)) ++PieceI; + // If this a reference to a local symbol in a discarded COMDAT section, + // emit a warning. + if (Body.isLocal()) { + auto *D = dyn_cast>(&Body); + if (D && D->Section == &InputSection::Discarded) + warning("relocation from " + File.getName() + " with offset " + + Twine(RI.r_offset) + " refers to discarded section"); + } + // Compute the offset of this section in the output section. We do it here // to try to compute it only once. uintX_t Offset; Index: test/ELF/comdat.s =================================================================== --- test/ELF/comdat.s +++ test/ELF/comdat.s @@ -1,6 +1,6 @@ // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat.s -o %t2.o -// RUN: ld.lld -shared %t.o %t.o %t2.o -o %t +// RUN: ld.lld -shared %t.o %t.o %t2.o -o %t 2>&1 | FileCheck --check-prefix=WARN %s // RUN: llvm-objdump -d %t | FileCheck %s // RUN: llvm-readobj -s -t %t | FileCheck --check-prefix=READ %s // REQUIRES: x86 @@ -9,6 +9,8 @@ foo: nop +// WARN: relocation from {{.*}}comdat.s{{.*}}.o with offset 1 refers to discarded section + // CHECK: Disassembly of section .text2: // CHECK-NEXT: foo: // CHECK-NEXT: 1000: {{.*}} nop