[llvm-branch-commits] [llvm] release/23.x: [MachO] Preserve weak linkage for aliases (#198148) (PR #211997)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Jul 25 01:12:08 PDT 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/211997
Backport 29575a3
Requested by: @AsakuraMizu
>From f4a085a8a641dad0cda3e31edda5aed66343a9e1 Mon Sep 17 00:00:00 2001
From: syhhyl <syhhyl926 at 126.com>
Date: Wed, 22 Jul 2026 20:25:55 +0800
Subject: [PATCH] [MachO] Preserve weak linkage for aliases (#198148)
Mach-O aliases with weak or linkonce linkage were emitted as weak
references, which is appropriate for undefined references but not for
alias definitions. Emit Mach-O aliases through the same linkage path as
other global definitions so weak aliases get .weak_definition.
When writing aliased symbols, keep the aliasee flags and include the
alias symbol's own flags so N_WEAK_DEF is preserved in the Mach-O n_desc
field.
Fixes #111321
#196047 was closed as a duplicate of #111321.
(cherry picked from commit 29575a3460b43132c7d973b502830897b02874a9)
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 4 +++-
llvm/lib/MC/MachObjectWriter.cpp | 7 ++++++-
llvm/test/CodeGen/AArch64/macho-weak-alias.ll | 17 +++++++++++++++++
3 files changed, 26 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/macho-weak-alias.ll
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index f3ac25f25fcc1..3d7834b3883c2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2646,7 +2646,9 @@ void AsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
return;
}
- if (GA.hasExternalLinkage() || !MAI.getWeakRefDirective())
+ if (MAI.isMachO())
+ emitLinkage(&GA, Name);
+ else if (GA.hasExternalLinkage() || !MAI.getWeakRefDirective())
OutStreamer->emitSymbolAttribute(Name, MCSA_Global);
else if (GA.hasWeakLinkage() || GA.hasLinkOnceLinkage())
OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference);
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index e274d886c22d8..657f6f40fd9e9 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -443,7 +443,12 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) {
// The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
// value.
bool EncodeAsAltEntry = IsAlias && OrigSymbol.isAltEntry();
- W.write<uint16_t>(Symbol->getEncodedFlags(EncodeAsAltEntry));
+ uint16_t Flags = Symbol->getEncodedFlags(EncodeAsAltEntry);
+ // Preserve the aliasee's flags while adding alias-specific flags,
+ // such as N_WEAK_DEF emitted by .weak_definition.
+ if (IsAlias)
+ Flags |= OrigSymbol.getEncodedFlags(EncodeAsAltEntry);
+ W.write<uint16_t>(Flags);
if (is64Bit())
W.write<uint64_t>(Address);
else
diff --git a/llvm/test/CodeGen/AArch64/macho-weak-alias.ll b/llvm/test/CodeGen/AArch64/macho-weak-alias.ll
new file mode 100644
index 0000000000000..a936cea618b38
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/macho-weak-alias.ll
@@ -0,0 +1,17 @@
+; RUN: llc -mtriple=aarch64-apple-macosx13.0.0 -filetype=obj %s -o %t.o
+; RUN: llvm-nm -m %t.o | FileCheck %s
+
+define internal void @foo_internal() {
+ ret void
+}
+
+ at foo_default = weak_odr alias void (), ptr @foo_internal
+ at foo_hidden = weak_odr hidden alias void (), ptr @foo_internal
+
+define weak_odr hidden void @foo_defined() {
+ ret void
+}
+
+; CHECK-DAG: weak external _foo_default
+; CHECK-DAG: weak private external _foo_hidden
+; CHECK-DAG: weak private external _foo_defined
More information about the llvm-branch-commits
mailing list