@@ -5178,6 +5178,53 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
5178
5178
Arg.removeAttrs (AttributeFuncs::typeIncompatible (Arg.getType ()));
5179
5179
}
5180
5180
5181
+ // Check if the module attribute is present and not zero.
5182
+ static bool isModuleAttributeSet (Module &M, const StringRef &ModAttr) {
5183
+ const auto *Attr =
5184
+ mdconst::extract_or_null<ConstantInt>(M.getModuleFlag (ModAttr));
5185
+ return Attr && Attr->isOne ();
5186
+ }
5187
+
5188
+ // Check if the function attribute is not present and set it.
5189
+ static void SetFunctionAttrIfNotSet (Function &F, StringRef FnAttrName,
5190
+ StringRef Value) {
5191
+ if (!F.hasFnAttribute (FnAttrName))
5192
+ F.addFnAttr (FnAttrName, Value);
5193
+ }
5194
+
5195
+ void llvm::CopyModuleAttrToFunctions (Module &M) {
5196
+ Triple T (M.getTargetTriple ());
5197
+ if (!T.isThumb () && !T.isARM () && !T.isAArch64 ())
5198
+ return ;
5199
+
5200
+ StringRef SignTypeValue = " none" ;
5201
+ if (isModuleAttributeSet (M, " sign-return-address-all" ))
5202
+ 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" ;
5215
+
5216
+ for (Function &F : M.getFunctionList ()) {
5217
+ if (F.isDeclaration ())
5218
+ continue ;
5219
+
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);
5225
+ }
5226
+ }
5227
+
5181
5228
static bool isOldLoopArgument (Metadata *MD) {
5182
5229
auto *T = dyn_cast_or_null<MDTuple>(MD);
5183
5230
if (!T)
0 commit comments