@@ -6045,6 +6045,120 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
60456045 }
60466046}
60476047
6048+ // Check if the function attribute is not present and set it.
6049+ static void setFunctionAttrIfNotSet (Function &F, StringRef FnAttrName,
6050+ StringRef Value) {
6051+ if (!F.hasFnAttribute (FnAttrName))
6052+ F.addFnAttr (FnAttrName, Value);
6053+ }
6054+
6055+ // Check if the function attribute is not present and set it if needed.
6056+ // If the attribute is "false" then removes it.
6057+ // If the attribute is "true" resets it to a valueless attribute.
6058+ static void ConvertFunctionAttr (Function &F, bool Set, StringRef FnAttrName) {
6059+ if (!F.hasFnAttribute (FnAttrName)) {
6060+ if (Set)
6061+ F.addFnAttr (FnAttrName);
6062+ } else {
6063+ auto A = F.getFnAttribute (FnAttrName);
6064+ if (" false" == A.getValueAsString ())
6065+ F.removeFnAttr (FnAttrName);
6066+ else if (" true" == A.getValueAsString ()) {
6067+ F.removeFnAttr (FnAttrName);
6068+ F.addFnAttr (FnAttrName);
6069+ }
6070+ }
6071+ }
6072+
6073+ void llvm::copyModuleAttrToFunctions (Module &M) {
6074+ Triple T (M.getTargetTriple ());
6075+ if (!T.isThumb () && !T.isARM () && !T.isAArch64 ())
6076+ return ;
6077+
6078+ uint64_t BTEValue = 0 ;
6079+ uint64_t BPPLRValue = 0 ;
6080+ uint64_t GCSValue = 0 ;
6081+ uint64_t SRAValue = 0 ;
6082+ uint64_t SRAALLValue = 0 ;
6083+ uint64_t SRABKeyValue = 0 ;
6084+
6085+ NamedMDNode *ModFlags = M.getModuleFlagsMetadata ();
6086+ if (ModFlags) {
6087+ for (unsigned I = 0 , E = ModFlags->getNumOperands (); I != E; ++I) {
6088+ MDNode *Op = ModFlags->getOperand (I);
6089+ if (Op->getNumOperands () != 3 )
6090+ continue ;
6091+
6092+ MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand (1 ));
6093+ auto *CI = mdconst::dyn_extract<ConstantInt>(Op->getOperand (2 ));
6094+ if (!ID || !CI)
6095+ continue ;
6096+
6097+ StringRef IDStr = ID->getString ();
6098+ uint64_t *ValPtr = IDStr == " branch-target-enforcement" ? &BTEValue
6099+ : IDStr == " branch-protection-pauth-lr" ? &BPPLRValue
6100+ : IDStr == " guarded-control-stack" ? &GCSValue
6101+ : IDStr == " sign-return-address" ? &SRAValue
6102+ : IDStr == " sign-return-address-all" ? &SRAALLValue
6103+ : IDStr == " sign-return-address-with-bkey"
6104+ ? &SRABKeyValue
6105+ : nullptr ;
6106+ if (!ValPtr)
6107+ continue ;
6108+
6109+ *ValPtr = CI->getZExtValue ();
6110+ if (*ValPtr == 2 )
6111+ return ;
6112+ }
6113+ }
6114+
6115+ bool BTE = BTEValue == 1 ;
6116+ bool BPPLR = BPPLRValue == 1 ;
6117+ bool GCS = GCSValue == 1 ;
6118+ bool SRA = SRAValue == 1 ;
6119+
6120+ StringRef SignTypeValue = " non-leaf" ;
6121+ if (SRA && SRAALLValue == 1 )
6122+ SignTypeValue = " all" ;
6123+
6124+ StringRef SignKeyValue = " a_key" ;
6125+ if (SRA && SRABKeyValue == 1 )
6126+ SignKeyValue = " b_key" ;
6127+
6128+ for (Function &F : M.getFunctionList ()) {
6129+ if (F.isDeclaration ())
6130+ continue ;
6131+
6132+ if (SRA) {
6133+ setFunctionAttrIfNotSet (F, " sign-return-address" , SignTypeValue);
6134+ setFunctionAttrIfNotSet (F, " sign-return-address-key" , SignKeyValue);
6135+ } else {
6136+ if (auto A = F.getFnAttribute (" sign-return-address" );
6137+ A.isValid () && " none" == A.getValueAsString ()) {
6138+ F.removeFnAttr (" sign-return-address" );
6139+ F.removeFnAttr (" sign-return-address-key" );
6140+ }
6141+ }
6142+ ConvertFunctionAttr (F, BTE, " branch-target-enforcement" );
6143+ ConvertFunctionAttr (F, BPPLR, " branch-protection-pauth-lr" );
6144+ ConvertFunctionAttr (F, GCS, " guarded-control-stack" );
6145+ }
6146+
6147+ if (BTE)
6148+ M.setModuleFlag (llvm::Module::Min, " branch-target-enforcement" , 2 );
6149+ if (BPPLR)
6150+ M.setModuleFlag (llvm::Module::Min, " branch-protection-pauth-lr" , 2 );
6151+ if (GCS)
6152+ M.setModuleFlag (llvm::Module::Min, " guarded-control-stack" , 2 );
6153+ if (SRA) {
6154+ M.setModuleFlag (llvm::Module::Min, " sign-return-address" , 2 );
6155+ if (SRAALLValue == 1 )
6156+ M.setModuleFlag (llvm::Module::Min, " sign-return-address-all" , 2 );
6157+ if (SRABKeyValue == 1 )
6158+ M.setModuleFlag (llvm::Module::Min, " sign-return-address-with-bkey" , 2 );
6159+ }
6160+ }
6161+
60486162static bool isOldLoopArgument (Metadata *MD) {
60496163 auto *T = dyn_cast_or_null<MDTuple>(MD);
60506164 if (!T)
0 commit comments