@@ -5,7 +5,7 @@ use rustc_codegen_ssa::traits::*;
5
5
use rustc_hir:: def_id:: DefId ;
6
6
use rustc_middle:: middle:: codegen_fn_attrs:: { CodegenFnAttrFlags , PatchableFunctionEntry } ;
7
7
use rustc_middle:: ty:: { self , TyCtxt } ;
8
- use rustc_session:: config:: { FunctionReturn , OptLevel } ;
8
+ use rustc_session:: config:: { BranchProtection , FunctionReturn , OptLevel , PAuthKey , PacRet } ;
9
9
use rustc_span:: symbol:: sym;
10
10
use rustc_target:: spec:: { FramePointer , SanitizerSet , StackProbeType , StackProtector } ;
11
11
use smallvec:: SmallVec ;
@@ -405,8 +405,33 @@ pub fn from_fn_attrs<'ll, 'tcx>(
405
405
// And it is a module-level attribute, so the alternative is pulling naked functions into new LLVM modules.
406
406
// Otherwise LLVM's "naked" functions come with endbr prefixes per https://github.com/rust-lang/rust/issues/98768
407
407
to_add. push ( AttributeKind :: NoCfCheck . create_attr ( cx. llcx ) ) ;
408
- // Need this for AArch64.
409
- to_add. push ( llvm:: CreateAttrStringValue ( cx. llcx , "branch-target-enforcement" , "false" ) ) ;
408
+ if llvm_util:: get_version ( ) < ( 19 , 0 , 0 ) {
409
+ // Prior to LLVM 19, branch-target-enforcement was disabled by setting the attribute to
410
+ // the string "false". Now it is disabled by absence of the attribute.
411
+ to_add. push ( llvm:: CreateAttrStringValue ( cx. llcx , "branch-target-enforcement" , "false" ) ) ;
412
+ }
413
+ } else if llvm_util:: get_version ( ) >= ( 19 , 0 , 0 ) {
414
+ // For non-naked functions, set branch protection attributes on aarch64.
415
+ if let Some ( BranchProtection { bti, pac_ret } ) =
416
+ cx. sess ( ) . opts . unstable_opts . branch_protection
417
+ {
418
+ assert ! ( cx. sess( ) . target. arch == "aarch64" ) ;
419
+ if bti {
420
+ to_add. push ( llvm:: CreateAttrString ( cx. llcx , "branch-target-enforcement" ) ) ;
421
+ }
422
+ if let Some ( PacRet { leaf, key } ) = pac_ret {
423
+ to_add. push ( llvm:: CreateAttrStringValue (
424
+ cx. llcx ,
425
+ "sign-return-address" ,
426
+ if leaf { "all" } else { "non-leaf" } ,
427
+ ) ) ;
428
+ to_add. push ( llvm:: CreateAttrStringValue (
429
+ cx. llcx ,
430
+ "sign-return-address-key" ,
431
+ if key == PAuthKey :: A { "a_key" } else { "b_key" } ,
432
+ ) ) ;
433
+ }
434
+ }
410
435
}
411
436
if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR )
412
437
|| codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: ALLOCATOR_ZEROED )
0 commit comments