nickdesaulniers created this revision. nickdesaulniers added reviewers: MaskRay, pcc, xiangzhangllvm. Herald added subscribers: dexonsmith, hiraditya, kristof.beyls. nickdesaulniers requested review of this revision. Herald added projects: clang, LLVM. Herald added subscribers: llvm-commits, cfe-commits.
D88631 <https://reviews.llvm.org/D88631> added initial support for: - -mstack-protector-guard= - -mstack-protector-guard-reg= - -mstack-protector-guard-offset= flags, and D100919 <https://reviews.llvm.org/D100919> extended these to AArch64. Unfortunately, these flags aren't retained for LTO. Make them module attributes rather than TargetOptions. Link: https://github.com/ClangBuiltLinux/linux/issues/1378 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D102742 Files: clang/lib/CodeGen/CodeGenModule.cpp llvm/include/llvm/IR/Module.h llvm/lib/IR/Module.cpp llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Index: llvm/lib/Target/AArch64/AArch64InstrInfo.cpp =================================================================== --- llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -1904,10 +1904,10 @@ } Register Reg = MI.getOperand(0).getReg(); - TargetOptions Options = MI.getParent()->getParent()->getTarget().Options; - if (Options.StackProtectorGuard == StackProtectorGuards::SysReg) { + Module &M = *MBB.getParent()->getFunction().getParent(); + if (M.getStackProtectorGuard() == "sysreg") { const AArch64SysReg::SysReg *SrcReg = - AArch64SysReg::lookupSysRegByName(Options.StackProtectorGuardReg); + AArch64SysReg::lookupSysRegByName(M.getStackProtectorGuardReg()); if (!SrcReg) report_fatal_error("Unknown SysReg for Stack Protector Guard Register"); @@ -1915,7 +1915,7 @@ BuildMI(MBB, MI, DL, get(AArch64::MRS)) .addDef(Reg, RegState::Renamable) .addImm(SrcReg->Encoding); - int Offset = Options.StackProtectorGuardOffset; + int Offset = M.getStackProtectorGuardOffset(); if (Offset >= 0 && Offset <= 32760 && Offset % 8 == 0) { // ldr xN, [xN, #offset] BuildMI(MBB, MI, DL, get(AArch64::LDRXui)) Index: llvm/lib/IR/Module.cpp =================================================================== --- llvm/lib/IR/Module.cpp +++ llvm/lib/IR/Module.cpp @@ -686,6 +686,42 @@ addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind)); } +StringRef Module::getStackProtectorGuard() const { + Metadata *MD = getModuleFlag("stack-protector-guard"); + if (auto *MDS = dyn_cast_or_null<MDString>(MD)) + return MDS->getString(); + return {}; +} + +void Module::setStackProtectorGuard(StringRef Kind) { + MDString *ID = MDString::get(getContext(), Kind); + addModuleFlag(ModFlagBehavior::Warning, "stack-protector-guard", ID); +} + +StringRef Module::getStackProtectorGuardReg() const { + Metadata *MD = getModuleFlag("stack-protector-guard-reg"); + if (auto *MDS = dyn_cast_or_null<MDString>(MD)) + return MDS->getString(); + return {}; +} + +void Module::setStackProtectorGuardReg(StringRef Reg) { + MDString *ID = MDString::get(getContext(), Reg); + addModuleFlag(ModFlagBehavior::Warning, "stack-protector-guard-reg", ID); +} + +int Module::getStackProtectorGuardOffset() const { + Metadata *MD = getModuleFlag("stack-protector-guard-offset"); + if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD)) + return CI->getSExtValue(); + return 0; +} + +void Module::setStackProtectorGuardOffset(int offset) { + addModuleFlag(ModFlagBehavior::Warning, "stack-protector-guard-offset", + offset); +} + void Module::setSDKVersion(const VersionTuple &V) { SmallVector<unsigned, 3> Entries; Entries.push_back(V.getMajor()); Index: llvm/include/llvm/IR/Module.h =================================================================== --- llvm/include/llvm/IR/Module.h +++ llvm/include/llvm/IR/Module.h @@ -895,6 +895,14 @@ FramePointerKind getFramePointer() const; void setFramePointer(FramePointerKind Kind); + // enum class StackProtectorGuards { None, TLS, Global, SysReg }; + StringRef getStackProtectorGuard() const; + void setStackProtectorGuard(StringRef Kind); + StringRef getStackProtectorGuardReg() const; + void setStackProtectorGuardReg(StringRef Reg); + int getStackProtectorGuardOffset() const; + void setStackProtectorGuardOffset(int offset); + /// @name Utility functions for querying and setting the build SDK version /// @{ Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -773,6 +773,15 @@ if (!getCodeGenOpts().RecordCommandLine.empty()) EmitCommandLineMetadata(); + if (getCodeGenOpts().StackProtectorGuard != "") + getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard); + if (getCodeGenOpts().StackProtectorGuardReg != "none") + getModule().setStackProtectorGuardReg( + getCodeGenOpts().StackProtectorGuardReg); + if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX) + getModule().setStackProtectorGuardOffset( + getCodeGenOpts().StackProtectorGuardOffset); + getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames); EmitBackendOptionsMetadata(getCodeGenOpts());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits