Skip to content

Commit 502098d

Browse files
committed
naked functions: emit .private_extern on macos
1 parent c6d42d7 commit 502098d

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ fn prefix_and_suffix<'tcx>(
180180
}
181181
}
182182
}
183-
Linkage::Internal => {
184-
// write nothing
185-
}
183+
Linkage::Internal => match asm_binary_format {
184+
BinaryFormat::MachO => {
185+
// See https://github.com/rust-lang/rust/issues/148307.
186+
writeln!(w, ".private_extern {asm_name}")?;
187+
}
188+
_ => { /* write nothing */ }
189+
},
186190
Linkage::Common => emit_fatal("Functions may not have common linkage"),
187191
Linkage::AvailableExternally => {
188192
// this would make the function equal an extern definition

tests/assembly-llvm/naked-functions/aix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//@[aix] needs-llvm-components: powerpc
1010

1111
#![crate_type = "lib"]
12-
#![feature(no_core, asm_experimental_arch, f128, linkage, fn_align)]
12+
#![feature(no_core, asm_experimental_arch)]
1313
#![no_core]
1414

1515
// tests that naked functions work for the `powerpc64-ibm-aix` target.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ revisions: x86 aarch64
2+
//@ add-core-stubs
3+
//@ assembly-output: emit-asm
4+
//
5+
//@[aarch64] compile-flags: --target aarch64-apple-darwin
6+
//@[aarch64] needs-llvm-components: aarch64
7+
//
8+
//@[x86] compile-flags: --target x86_64-apple-darwin
9+
//@[x86] needs-llvm-components: x86
10+
11+
#![crate_type = "lib"]
12+
#![feature(no_core, asm_experimental_arch)]
13+
#![no_core]
14+
15+
// tests that naked functions on macos emit `.private_extern {asm_name}`.
16+
//
17+
// Without this directive, LTO may fail because the symbol is not visible.
18+
// See also https://github.com/rust-lang/rust/issues/148307.
19+
20+
extern crate minicore;
21+
use minicore::*;
22+
23+
// CHECK: .p2align 2
24+
// CHECK: .private_extern
25+
// CHECK: ret
26+
#[unsafe(naked)]
27+
extern "C" fn ret() {
28+
naked_asm!("ret")
29+
}
30+
31+
#[no_mangle]
32+
pub fn entry() {
33+
ret()
34+
}

0 commit comments

Comments
 (0)