[lld] df2a577 - [lld-macho] Error on encountering undefined symbols

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 13:20:12 PDT 2020


Author: Jez Ng
Date: 2020-06-02T13:19:38-07:00
New Revision: df2a5778c32bb0757448f667d570d4e5fd18c16e

URL: https://github.com/llvm/llvm-project/commit/df2a5778c32bb0757448f667d570d4e5fd18c16e
DIFF: https://github.com/llvm/llvm-project/commit/df2a5778c32bb0757448f667d570d4e5fd18c16e.diff

LOG: [lld-macho] Error on encountering undefined symbols

... instead of silently emitting a reference to the zero address.

Reviewed By: smeenai

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

Added: 
    lld/test/MachO/invalid/undefined-symbol.s

Modified: 
    lld/MachO/InputSection.cpp
    lld/MachO/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 84ed52d70ce9..894f5ffabaf4 100644
--- 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
index fd31f39aa11b..bdd314b0f012 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -249,11 +249,17 @@ class LCLoadDylinker : public LoadCommand {
 } // 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
index 000000000000..88eabfd1ce02
--- /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


        


More information about the llvm-commits mailing list