[llvm-branch-commits] [lld] [lld][MachO] Fix ObjC stubs from autolinked archives (PR #219743)
Kyungwoo Lee via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Sep 1 15:49:09 PDT 2026
https://github.com/kyulee-com updated https://github.com/llvm/llvm-project/pull/219743
>From 8fe4bbb2f4404904c9b0d519a821e6d9bafd869e Mon Sep 17 00:00:00 2001
From: Kyungwoo Lee <kyulee at meta.com>
Date: Sat, 29 Aug 2026 17:05:11 -0700
Subject: [PATCH] [lld][MachO] Fix ObjC stubs from autolinked archives
Move ObjC stub preparation after LC_LINKER_OPTION processing so archive members loaded via autolink can contribute _objc_msgSend$ selector stubs before selector references are built.
Previously those stubs missed __objc_methname setup; assert builds could fail in makeSelRef, and release builds could form an invalid selector reference.
---
lld/MachO/Driver.cpp | 3 +-
lld/test/MachO/arm64-objc-stubs-autolink.s | 35 ++++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 lld/test/MachO/arm64-objc-stubs-autolink.s
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 92d841dfd3ade..b05519f3a8c11 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -2453,7 +2453,6 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
createSyntheticSections();
createSyntheticSymbols();
- addSynthenticMethnames();
createAliases();
// If we are in "explicit exports" mode, hide everything that isn't
@@ -2471,6 +2470,8 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
if (config->thinLTOIndexOnly || config->emitLLVM)
return errorCount() == 0;
+ addSynthenticMethnames();
+
// LTO may emit a non-hidden (extern) object file symbol even if the
// corresponding bitcode symbol is hidden. In particular, this happens for
// cross-module references to hidden symbols under ThinLTO. Thus, if we
diff --git a/lld/test/MachO/arm64-objc-stubs-autolink.s b/lld/test/MachO/arm64-objc-stubs-autolink.s
new file mode 100644
index 0000000000000..d177eb8e7961d
--- /dev/null
+++ b/lld/test/MachO/arm64-objc-stubs-autolink.s
@@ -0,0 +1,35 @@
+# REQUIRES: aarch64
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/main.s \
+# RUN: -o %t/main.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/dep.s \
+# RUN: -o %t/dep.o
+# RUN: llvm-ar rcs %t/libdep.a %t/dep.o
+# RUN: %lld -arch arm64 -lSystem -o %t/out %t/main.o -L%t \
+# RUN: -objc_stubs_fast -U _objc_msgSend
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: --macho %t/out | FileCheck %s
+
+# CHECK: Contents of (__TEXT,__objc_stubs) section
+# CHECK-NEXT: _objc_msgSend$plain:
+# CHECK-NEXT: adrp x1,
+# CHECK-NEXT: ldr x1, {{.*}} ; Objc selector ref: plain
+# CHECK-NEXT: adrp x16,
+# CHECK-NEXT: ldr x16, {{.*}} ; literal pool symbol address: _objc_msgSend
+# CHECK-NEXT: br x16
+
+#--- main.s
+.linker_option "-ldep"
+.text
+.globl _main
+_main:
+ bl _dep
+ ret
+
+#--- dep.s
+.text
+.globl _dep
+_dep:
+ bl _objc_msgSend$plain
+ ret
More information about the llvm-branch-commits
mailing list