[lld-macho] Error on encountering undefined symbols
authorJez Ng <jezng@fb.com>
Mon, 18 May 2020 22:46:33 +0000 (15:46 -0700)
committerJez Ng <jezng@fb.com>
Tue, 2 Jun 2020 20:19:38 +0000 (13:19 -0700)
... instead of silently emitting a reference to the zero address.

Reviewed By: smeenai

Differential Revision: https://reviews.llvm.org/D80169

lld/MachO/InputSection.cpp
lld/MachO/Writer.cpp
lld/test/MachO/invalid/undefined-symbol.s [new file with mode: 0644]

index 84ed52d..894f5ff 100644 (file)
@@ -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;
index fd31f39..bdd314b 100644 (file)
@@ -249,11 +249,17 @@ private:
 } // namespace
 
 void Writer::scanRelocations() {
-  for (InputSection *sect : inputSections)
-    for (Reloc &r : sect->relocs)
-      if (auto *s = r.target.dyn_cast<Symbol *>())
-        if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s))
+  for (InputSection *isec : inputSections) {
+    for (Reloc &r : isec->relocs) {
+      if (auto *s = r.target.dyn_cast<Symbol *>()) {
+        if (isa<Undefined>(s))
+          error("undefined symbol " + s->getName() + ", referenced from " +
+                sys::path::filename(isec->file->getName()));
+        else if (auto *dylibSymbol = dyn_cast<DylibSymbol>(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 (file)
index 0000000..88eabfd
--- /dev/null
@@ -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