diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp --- a/lld/MachO/InputSection.cpp +++ b/lld/MachO/InputSection.cpp @@ -13,6 +13,7 @@ #include "lld/Common/Memory.h" #include "llvm/Support/Endian.h" +using namespace llvm; using namespace llvm::MachO; using namespace llvm::support; using namespace lld; diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -249,11 +249,17 @@ } // namespace void Writer::scanRelocations() { - for (InputSection *sect : inputSections) - for (Reloc &r : sect->relocs) - if (auto *s = r.target.dyn_cast()) - if (auto *dylibSymbol = dyn_cast(s)) + for (InputSection *isec : inputSections) { + for (Reloc &r : isec->relocs) { + if (auto *s = r.target.dyn_cast()) { + if (isa(s)) + error("undefined symbol " + s->getName() + ", referenced from " + + sys::path::filename(isec->file->getName())); + else if (auto *dylibSymbol = dyn_cast(s)) target->prepareDylibSymbolRelocation(*dylibSymbol, r.type); + } + } + } } void Writer::createLoadCommands() { diff --git a/lld/test/MachO/invalid/undefined-symbol.s b/lld/test/MachO/invalid/undefined-symbol.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/invalid/undefined-symbol.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o +# RUN: not lld -flavor darwinnew -Z -o %t %t.o 2>&1 | FileCheck %s -DBASENAME=%basename_t +# CHECK: error: undefined symbol _foo, referenced from [[BASENAME]] + +.globl _main +.text +_main: + callq _foo + movq $0, %rax + retq