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