Skip to content

Commit 530b5da

Browse files
Also stop emitting BTI prologues for naked functions
Same idea but for AArch64.
1 parent 92174f9 commit 530b5da

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ pub fn from_fn_attrs<'ll, 'tcx>(
303303
// And it is a module-level attribute, so the alternative is pulling naked functions into new LLVM modules.
304304
// Otherwise LLVM's "naked" functions come with endbr prefixes per https://github.com/rust-lang/rust/issues/98768
305305
to_add.push(AttributeKind::NoCfCheck.create_attr(cx.llcx));
306+
// Need this for AArch64.
307+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "branch-target-enforcement", "false"));
306308
}
307309
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
308310
// apply to return place instead of function (unlike all other attributes applied in this function)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-flags: -C no-prepopulate-passes -Zbranch-protection=bti
2+
// assembly-output: emit-asm
3+
// needs-asm-support
4+
// only-aarch64
5+
6+
#![crate_type = "lib"]
7+
#![feature(naked_functions)]
8+
use std::arch::asm;
9+
10+
// The problem at hand: Rust has adopted a fairly strict meaning for "naked functions",
11+
// meaning "no prologue whatsoever, no, really, not one instruction."
12+
// Unfortunately, aarch64's "branch target identification" works via hints at landing sites.
13+
// LLVM implements this via making sure of that, even for functions with the naked attribute.
14+
// So, we must emit an appropriate instruction instead!
15+
#[no_mangle]
16+
#[naked]
17+
pub unsafe extern "C" fn _hlt() -> ! {
18+
// CHECK-NOT: hint #34
19+
// CHECK: hlt #0x1
20+
asm!("hlt #1", options(noreturn))
21+
}

0 commit comments

Comments
 (0)