Skip to content

Commit 307a175

Browse files
authored
Merge d96ae8f into d682af8
2 parents d682af8 + d96ae8f commit 307a175

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ fn prefix_and_suffix<'tcx>(
181181
}
182182
}
183183
Linkage::Internal => {
184-
// write nothing
184+
// LTO can fail when internal linkage is used.
185+
emit_fatal("naked functions may not have internal linkage")
185186
}
186187
Linkage::Common => emit_fatal("Functions may not have common linkage"),
187188
Linkage::AvailableExternally => {

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,16 @@ fn internalize_symbols<'tcx>(
580580
}
581581
}
582582

583+
// When LTO inlines the caller of a naked function, it will attempt but fail to make the
584+
// naked function symbol visible. To ensure that LTO works correctly, do not default
585+
// naked functions to internal linkage and default visibility.
586+
if let MonoItem::Fn(instance) = item {
587+
let flags = cx.tcx.codegen_instance_attrs(instance.def).flags;
588+
if flags.contains(CodegenFnAttrFlags::NAKED) {
589+
continue;
590+
}
591+
}
592+
583593
// If we got here, we did not find any uses from other CGUs, so
584594
// it's fine to make this monomorphization internal.
585595
data.linkage = Linkage::Internal;

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@ revisions: macos-x86 macos-aarch64 linux-x86
2+
//@ add-core-stubs
3+
//@ assembly-output: emit-asm
4+
//
5+
//@[macos-aarch64] compile-flags: --target aarch64-apple-darwin
6+
//@[macos-aarch64] needs-llvm-components: aarch64
7+
//
8+
//@[macos-x86] compile-flags: --target x86_64-apple-darwin
9+
//@[macos-x86] needs-llvm-components: x86
10+
//
11+
//@[linux-x86] compile-flags: --target x86_64-unknown-linux-gnu
12+
//@[linux-x86] needs-llvm-components: x86
13+
14+
#![crate_type = "lib"]
15+
#![feature(no_core, asm_experimental_arch)]
16+
#![no_core]
17+
18+
// Tests that naked functions that are not externally linked (e.g. via `no_mangle`)
19+
// are marked as `Visibility::Hidden` and emit `.private_extern` or `.hidden`.
20+
//
21+
// Without this directive, LTO may fail because the symbol is not visible.
22+
// See also https://github.com/rust-lang/rust/issues/148307.
23+
24+
extern crate minicore;
25+
use minicore::*;
26+
27+
// CHECK: .p2align 2
28+
// macos-x86,macos-aarch64: .private_extern
29+
// linux-x86: .globl
30+
// linux-x86: .hidden
31+
// CHECK: ret
32+
#[unsafe(naked)]
33+
extern "C" fn ret() {
34+
naked_asm!("ret")
35+
}
36+
37+
// CHECK-LABEL: entry
38+
#[no_mangle]
39+
pub fn entry() {
40+
ret()
41+
}

0 commit comments

Comments
 (0)