@@ -9,7 +9,7 @@ use rustc_hir::def_id::DefId;
9
9
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
10
10
use rustc_middle:: ty:: layout:: HasTyCtxt ;
11
11
use rustc_middle:: ty:: { self , TyCtxt } ;
12
- use rustc_session:: config:: OptLevel ;
12
+ use rustc_session:: config:: { BranchProtection , OptLevel , PAuthKey } ;
13
13
use rustc_session:: Session ;
14
14
use rustc_target:: spec:: abi:: Abi ;
15
15
use rustc_target:: spec:: { FramePointer , SanitizerSet , StackProbeType , StackProtector } ;
@@ -203,6 +203,58 @@ pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
203
203
}
204
204
}
205
205
206
+ pub fn set_branch_protection ( sess : & Session , llfn : & ' ll Value ) {
207
+ // Setting PAC/BTI function attributes is only necessary for LLVM 11 and earlier.
208
+ // For LLVM 12 and greater, module-level metadata attributes are set in
209
+ // `compiler/rustc_codegen_llvm/src/context.rs`.
210
+ if llvm_util:: get_version ( ) >= ( 12 , 0 , 0 ) {
211
+ return ;
212
+ }
213
+
214
+ let BranchProtection { bti, pac_ret : pac } = sess. opts . cg . branch_protection ;
215
+
216
+ if bti {
217
+ llvm:: AddFunctionAttrString (
218
+ llfn,
219
+ llvm:: AttributePlace :: Function ,
220
+ cstr ! ( "branch-target-enforcement" ) ,
221
+ ) ;
222
+ }
223
+
224
+ if let Some ( pac_opts) = pac {
225
+ if pac_opts. leaf {
226
+ llvm:: AddFunctionAttrStringValue (
227
+ llfn,
228
+ llvm:: AttributePlace :: Function ,
229
+ cstr ! ( "sign-return-address" ) ,
230
+ cstr ! ( "non-leaf" ) ,
231
+ ) ;
232
+ } else {
233
+ llvm:: AddFunctionAttrStringValue (
234
+ llfn,
235
+ llvm:: AttributePlace :: Function ,
236
+ cstr ! ( "sign-return-address" ) ,
237
+ cstr ! ( "all" ) ,
238
+ ) ;
239
+ }
240
+
241
+ match pac_opts. key {
242
+ PAuthKey :: A => llvm:: AddFunctionAttrStringValue (
243
+ llfn,
244
+ llvm:: AttributePlace :: Function ,
245
+ cstr ! ( "sign-return-address-key" ) ,
246
+ cstr ! ( "a_key" ) ,
247
+ ) ,
248
+ PAuthKey :: B => llvm:: AddFunctionAttrStringValue (
249
+ llfn,
250
+ llvm:: AttributePlace :: Function ,
251
+ cstr ! ( "sign-return-address-key" ) ,
252
+ cstr ! ( "b_key" ) ,
253
+ ) ,
254
+ }
255
+ }
256
+ }
257
+
206
258
pub ( crate ) fn default_optimisation_attrs ( sess : & Session , llfn : & ' ll Value ) {
207
259
match sess. opts . optimize {
208
260
OptLevel :: Size => {
0 commit comments