Skip to content

Commit 8d4dcc6

Browse files
syhhylllvmbot
authored andcommitted
[MachO] Preserve weak linkage for aliases (llvm#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 llvm#111321 llvm#196047 was closed as a duplicate of llvm#111321. (cherry picked from commit 29575a3)
1 parent bb9934d commit 8d4dcc6

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2646,7 +2646,9 @@ void AsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
26462646
return;
26472647
}
26482648

2649-
if (GA.hasExternalLinkage() || !MAI.getWeakRefDirective())
2649+
if (MAI.isMachO())
2650+
emitLinkage(&GA, Name);
2651+
else if (GA.hasExternalLinkage() || !MAI.getWeakRefDirective())
26502652
OutStreamer->emitSymbolAttribute(Name, MCSA_Global);
26512653
else if (GA.hasWeakLinkage() || GA.hasLinkOnceLinkage())
26522654
OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference);

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,12 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) {
443443
// The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
444444
// value.
445445
bool EncodeAsAltEntry = IsAlias && OrigSymbol.isAltEntry();
446-
W.write<uint16_t>(Symbol->getEncodedFlags(EncodeAsAltEntry));
446+
uint16_t Flags = Symbol->getEncodedFlags(EncodeAsAltEntry);
447+
// Preserve the aliasee's flags while adding alias-specific flags,
448+
// such as N_WEAK_DEF emitted by .weak_definition.
449+
if (IsAlias)
450+
Flags |= OrigSymbol.getEncodedFlags(EncodeAsAltEntry);
451+
W.write<uint16_t>(Flags);
447452
if (is64Bit())
448453
W.write<uint64_t>(Address);
449454
else
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: llc -mtriple=aarch64-apple-macosx13.0.0 -filetype=obj %s -o %t.o
2+
; RUN: llvm-nm -m %t.o | FileCheck %s
3+
4+
define internal void @foo_internal() {
5+
ret void
6+
}
7+
8+
@foo_default = weak_odr alias void (), ptr @foo_internal
9+
@foo_hidden = weak_odr hidden alias void (), ptr @foo_internal
10+
11+
define weak_odr hidden void @foo_defined() {
12+
ret void
13+
}
14+
15+
; CHECK-DAG: weak external _foo_default
16+
; CHECK-DAG: weak private external _foo_hidden
17+
; CHECK-DAG: weak private external _foo_defined

0 commit comments

Comments
 (0)