[llvm] [MachO] Refine alias n_desc flag ownership (name flags from alias, not aliasee) (PR #212001)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 25 02:03:35 PDT 2026
https://github.com/syhhyl created https://github.com/llvm/llvm-project/pull/212001
Refines the fix in #198148.
## Problem
#198148 used `Flags |= aliasFlags` to preserve `N_WEAK_DEF` on Mach-O aliases. This OR-all approach has two issues:
1. **Strong alias inherits weak from aliasee**: `strong_alias = weak_target` incorrectly gets `N_WEAK_DEF`.
2. **Encoding bits corrupted**: `REFERENCE_TYPE` (low 3 bits) and library ordinal (high 8 bits) are enum/encoding fields, not independent flag bits, and must not be OR'd.
## Principle
`bar = foo` just means bar shares foo's address. The `n_desc` flags fall into two categories:
- **Name attributes** (from bar): `N_WEAK_DEF`, `N_WEAK_REF`, `N_ALT_ENTRY`, `N_NO_DEAD_STRIP` — describe the name's own linkage and liveness.
- **Location attributes** (from foo): `N_ARM_THUMB_DEF`, `N_COLD_FUNC`, `N_SYMBOL_RESOLVER`, `REFERENCE_TYPE` — describe the code at the target address.
## Fix
Replace the OR-all with a `NameMask`:
```
uint16_t AliaseeFlags = Symbol->getEncodedFlags(false);
uint16_t AliasFlags = OrigSymbol.getEncodedFlags(EncodeAsAltEntry);
constexpr uint16_t NameMask = N_WEAK_DEF | N_WEAK_REF | N_ALT_ENTRY | N_NO_DEAD_STRIP;
Flags = (AliaseeFlags & ~NameMask) | (AliasFlags & NameMask);
```
## Test coverage
`llvm/test/CodeGen/AArch64/macho-weak-alias.ll` verifies:
| Scenario | Expected |
|---|---|
| weak alias to strong aliasee | N_WEAK_DEF (0x80) |
| strong alias to weak aliasee | (0x0) |
| linkonce_odr unnamed_addr alias | N_WEAK_DEF and N_WEAK_REF (0xC0) |
| aliasee has N_NO_DEAD_STRIP (via llvm.used) | alias does NOT inherit |
| aliasee has N_WEAK_DEF | alias does NOT inherit |
Uses both `llvm-nm -m` for readability and `llvm-readobj --symbols` for exact `n_desc` verification.
>From a71613829a7ffd2e5798f3154790490200d7dd04 Mon Sep 17 00:00:00 2001
From: syhhyl <syhhyl926 at 126.com>
Date: Sat, 25 Jul 2026 16:43:28 +0800
Subject: [PATCH] [MachO] Refine alias n_desc flag ownership
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
For alias symbols (bar = foo), n_desc mixes two kinds of flags:
- Name attributes (from bar): N_WEAK_DEF, N_WEAK_REF, N_ALT_ENTRY,
N_NO_DEAD_STRIP — describe the name's own linkage and liveness.
- Location attributes (from foo): N_ARM_THUMB_DEF, N_COLD_FUNC,
N_SYMBOL_RESOLVER, REFERENCE_TYPE — describe the code at the
target address.
The previous OR-all approach (29575a3460b4) fixed the case where
a weak alias lost its N_WEAK_DEF, but incorrectly propagated the
aliasee's weak bits to strong aliases, and OR'd encoding bits
like REFERENCE_TYPE that must not be combined.
Replace OR-all with a NameMask that isolates name attributes from
the alias and location attributes from the aliasee.
Fixes #111321 (refines the fix in #198148).
---
llvm/lib/MC/MachObjectWriter.cpp | 15 ++--
llvm/test/CodeGen/AArch64/macho-weak-alias.ll | 73 ++++++++++++++++++-
2 files changed, 79 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 657f6f40fd9e9..edf4bc3f740e4 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -443,11 +443,16 @@ 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();
- 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);
+ uint16_t Flags;
+ if (!IsAlias) {
+ Flags = Symbol->getEncodedFlags(EncodeAsAltEntry);
+ } else {
+ uint16_t AliaseeFlags = Symbol->getEncodedFlags(false);
+ uint16_t AliasFlags = OrigSymbol.getEncodedFlags(EncodeAsAltEntry);
+ constexpr uint16_t NameMask = MachO::N_WEAK_DEF | MachO::N_WEAK_REF |
+ MachO::N_ALT_ENTRY | MachO::N_NO_DEAD_STRIP;
+ Flags = (AliaseeFlags & ~NameMask) | (AliasFlags & NameMask);
+ }
W.write<uint16_t>(Flags);
if (is64Bit())
W.write<uint64_t>(Address);
diff --git a/llvm/test/CodeGen/AArch64/macho-weak-alias.ll b/llvm/test/CodeGen/AArch64/macho-weak-alias.ll
index a936cea618b38..48adcdaf154a0 100644
--- a/llvm/test/CodeGen/AArch64/macho-weak-alias.ll
+++ b/llvm/test/CodeGen/AArch64/macho-weak-alias.ll
@@ -1,17 +1,82 @@
; RUN: llc -mtriple=aarch64-apple-macosx13.0.0 -filetype=obj %s -o %t.o
-; RUN: llvm-nm -m %t.o | FileCheck %s
+; RUN: llvm-nm -m %t.o | FileCheck --check-prefix=NM %s
+; RUN: llvm-readobj --symbols %t.o | FileCheck --check-prefix=SYMBOL %s
+
+; llvm.used gives foo_internal N_NO_DEAD_STRIP.
+; Under the name-flag rule, aliases must NOT inherit it.
+ at llvm.used = appending global [1 x ptr] [ptr @foo_internal], section "llvm.metadata"
define internal void @foo_internal() {
ret void
}
+; Weak alias to internal target: bar sets N_WEAK_DEF, foo has N_NO_DEAD_STRIP.
+; Result: N_WEAK_DEF only (0x80), no N_NO_DEAD_STRIP.
@foo_default = weak_odr alias void (), ptr @foo_internal
+
+; Weak hidden alias: bar sets N_WEAK_DEF + hidden.
@foo_hidden = weak_odr hidden alias void (), ptr @foo_internal
+; Baseline: ordinary weak hidden definition.
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
+; Weak aliasee target.
+define weak_odr void @foo_weak_aliasee() {
+ ret void
+}
+
+; Strong alias to weak target: bar has no weak, foo has N_WEAK_DEF.
+; Result: no weak flags (0x0).
+ at foo_strong_alias = alias void (), ptr @foo_weak_aliasee
+
+; Auto-hide weak: linkonce_odr unnamed_addr -> emit .weak_def_can_be_hidden.
+; Result: N_WEAK_DEF | N_WEAK_REF (0xC0).
+ at foo_auto = linkonce_odr unnamed_addr alias void (), ptr @foo_internal
+
+; NM-DAG: weak external _foo_default
+; NM-DAG: weak private external _foo_hidden
+; NM-DAG: weak private external _foo_defined
+; NM-DAG: {{^[0-9a-fA-F]+ \(__TEXT,__text\) external _foo_strong_alias$}}
+; NM-DAG: weak external automatically hidden _foo_auto
+; NM-DAG: non-external [no dead strip] _foo_internal
+
+; _foo_internal: N_NO_DEAD_STRIP from llvm.used (0x20).
+; SYMBOL: Name: _foo_internal
+; SYMBOL-NEXT: Type: Section (0xE)
+; SYMBOL-NEXT: Section: __text
+; SYMBOL-NEXT: RefType: UndefinedNonLazy (0x0)
+; SYMBOL-NEXT: Flags [ (0x20)
+; SYMBOL-NEXT: NoDeadStrip (0x20)
+; SYMBOL-NEXT: ]
+
+; _foo_auto: 0xC0 = N_WEAK_DEF | N_WEAK_REF.
+; SYMBOL: Name: _foo_auto
+; SYMBOL-NEXT: Extern
+; SYMBOL-NEXT: Type: Section (0xE)
+; SYMBOL-NEXT: Section: __text
+; SYMBOL-NEXT: RefType: UndefinedNonLazy (0x0)
+; SYMBOL-NEXT: Flags [ (0xC0)
+; SYMBOL-NEXT: WeakDef (0x80)
+; SYMBOL-NEXT: WeakRef (0x40)
+; SYMBOL-NEXT: ]
+
+; _foo_default: 0x80 = N_WEAK_DEF, no N_NO_DEAD_STRIP inherited.
+; SYMBOL: Name: _foo_default
+; SYMBOL-NEXT: Extern
+; SYMBOL-NEXT: Type: Section (0xE)
+; SYMBOL-NEXT: Section: __text
+; SYMBOL-NEXT: RefType: UndefinedNonLazy (0x0)
+; SYMBOL-NEXT: Flags [ (0x80)
+; SYMBOL-NEXT: WeakDef (0x80)
+; SYMBOL-NEXT: ]
+
+; _foo_strong_alias: 0x0, no weak inherited from foo.
+; SYMBOL: Name: _foo_strong_alias
+; SYMBOL-NEXT: Extern
+; SYMBOL-NEXT: Type: Section (0xE)
+; SYMBOL-NEXT: Section: __text
+; SYMBOL-NEXT: RefType: UndefinedNonLazy (0x0)
+; SYMBOL-NEXT: Flags [ (0x0)
+; SYMBOL-NEXT: ]
More information about the llvm-commits
mailing list