[lld] c15b588 - [lld/mac] Don't assert during thunk insertion if there are undefined symbols

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 3 09:22:56 PDT 2021


Author: Nico Weber
Date: 2021-09-03T12:22:41-04:00
New Revision: c15b5888527bce7c794270d76484068ff3dc73a4

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

LOG: [lld/mac] Don't assert during thunk insertion if there are undefined symbols

We end up calling resolveBranchVA(), which asserts for Undefineds.

As fix, just return early in Writer::run() if there are any diagnostics
after processing relocations (which is where undefined symbol errors are
emitted). This matches what the ELF port does.

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

Added: 
    lld/test/MachO/invalid/arm64-thunk-undefined.s

Modified: 
    lld/MachO/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index d9c9cf570054..fe5258125e35 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -1102,10 +1102,16 @@ template <class LP> void Writer::run() {
   if (config->entry && !isa<Undefined>(config->entry))
     prepareBranchTarget(config->entry);
   scanRelocations();
+
+  // Do not proceed if there was an undefined symbol.
+  if (errorCount())
+    return;
+
   if (in.stubHelper->isNeeded())
     in.stubHelper->setup();
   scanSymbols();
   createOutputSections<LP>();
+
   // After this point, we create no new segments; HOWEVER, we might
   // yet create branch-range extension thunks for architectures whose
   // hardware call instructions have limited range, e.g., ARM(64).

diff  --git a/lld/test/MachO/invalid/arm64-thunk-undefined.s b/lld/test/MachO/invalid/arm64-thunk-undefined.s
new file mode 100644
index 000000000000..849184e6a683
--- /dev/null
+++ b/lld/test/MachO/invalid/arm64-thunk-undefined.s
@@ -0,0 +1,24 @@
+# REQUIRES: aarch64
+
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
+## This shouldn't assert.
+# RUN: not %lld -arch arm64 -lSystem -o %t/thunk %t.o 2>&1 | FileCheck %s
+
+# CHECK: error: undefined symbol: _g
+
+.subsections_via_symbols
+
+.p2align 2
+
+.globl _main, _g
+
+.globl _main
+_main:
+  bl _g
+  ret
+
+_filler1:
+.space 0x4000000
+
+_filler2:
+.space 0x4000000


        


More information about the llvm-commits mailing list