@@ -5178,8 +5178,8 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
51785178 Arg.removeAttrs (AttributeFuncs::typeIncompatible (Arg.getType ()));
51795179}
51805180
5181- // Check if the module attribute is present and not zero .
5182- static bool isModuleAttributeSet (Module &M, const StringRef &ModAttr) {
5181+ // Check if the module attribute is present and set to one .
5182+ static bool isModuleAttributeOne (Module &M, const StringRef &ModAttr) {
51835183 const auto *Attr =
51845184 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag (ModAttr));
51855185 return Attr && Attr->isOne ();
@@ -5197,31 +5197,36 @@ void llvm::CopyModuleAttrToFunctions(Module &M) {
51975197 if (!T.isThumb () && !T.isARM () && !T.isAArch64 ())
51985198 return ;
51995199
5200- StringRef SignTypeValue = " none" ;
5201- if (isModuleAttributeSet (M, " sign-return-address-all" ))
5200+ bool BTE = isModuleAttributeOne (M, " branch-target-enforcement" );
5201+ bool BPPLR = isModuleAttributeOne (M, " branch-protection-pauth-lr" );
5202+ bool GCS = isModuleAttributeOne (M, " guarded-control-stack" );
5203+ bool SRA = isModuleAttributeOne (M, " sign-return-address" );
5204+
5205+ if (!BTE && !BPPLR && !GCS && !SRA)
5206+ return ;
5207+
5208+ StringRef SignTypeValue = " non-leaf" ;
5209+ if (SRA && isModuleAttributeOne (M, " sign-return-address-all" ))
52025210 SignTypeValue = " all" ;
5203- else if (isModuleAttributeSet (M, " sign-return-address" ))
5204- SignTypeValue = " non-leaf" ;
5205-
5206- StringRef BTEValue =
5207- isModuleAttributeSet (M, " branch-target-enforcement" ) ? " true" : " false" ;
5208- StringRef BPPLValue =
5209- isModuleAttributeSet (M, " branch-protection-pauth-lr" ) ? " true" : " false" ;
5210- StringRef GCSValue =
5211- isModuleAttributeSet (M, " guarded-control-stack" ) ? " true" : " false" ;
5212- StringRef SignKeyValue =
5213- isModuleAttributeSet (M, " sign-return-address-with-bkey" ) ? " b_key"
5214- : " a_key" ;
5211+
5212+ StringRef SignKeyValue = " a_key" ;
5213+ if (SRA && isModuleAttributeOne (M, " sign-return-address-with-bkey" ))
5214+ SignKeyValue = " b_key" ;
52155215
52165216 for (Function &F : M.getFunctionList ()) {
52175217 if (F.isDeclaration ())
52185218 continue ;
52195219
5220- SetFunctionAttrIfNotSet (F, " sign-return-address" , SignTypeValue);
5221- SetFunctionAttrIfNotSet (F, " branch-target-enforcement" , BTEValue);
5222- SetFunctionAttrIfNotSet (F, " branch-protection-pauth-lr" , BPPLValue);
5223- SetFunctionAttrIfNotSet (F, " guarded-control-stack" , GCSValue);
5224- SetFunctionAttrIfNotSet (F, " sign-return-address-key" , SignKeyValue);
5220+ if (SRA) {
5221+ SetFunctionAttrIfNotSet (F, " sign-return-address" , SignTypeValue);
5222+ SetFunctionAttrIfNotSet (F, " sign-return-address-key" , SignKeyValue);
5223+ }
5224+ if (BTE)
5225+ SetFunctionAttrIfNotSet (F, " branch-target-enforcement" , " true" );
5226+ if (BPPLR)
5227+ SetFunctionAttrIfNotSet (F, " branch-protection-pauth-lr" , " true" );
5228+ if (GCS)
5229+ SetFunctionAttrIfNotSet (F, " guarded-control-stack" , " true" );
52255230 }
52265231}
52275232
0 commit comments