[PATCH] D93428: [AArch64] Add bti note property when compiling asm files with -mbranch-protection=bti

Stephen Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 16 16:37:57 PST 2020


steplong created this revision.
Herald added subscribers: danielkiss, kristof.beyls.
steplong requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Generate the .note.gnu.property section with GNU_PROPERTY_AARCH64_FEATURE_1_BTI
when compiling assembly files with -mbranch-protection=bti.

See https://reviews.llvm.org/D81930 for generating the section when compiling
C/C++ files with -mbranch-protection=bti.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93428

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Clang.h
  clang/test/Driver/arm64-markbti.S


Index: clang/test/Driver/arm64-markbti.S
===================================================================
--- clang/test/Driver/arm64-markbti.S
+++ clang/test/Driver/arm64-markbti.S
@@ -1,10 +1,12 @@
 // REQUIRES: aarch64-registered-target
 
-// When -mmark-bti-property is passed the generated file object gets BTI marking.
+// When -mmark-bti-property or -mbranch-protection=bti is passed the generated file object gets BTI marking.
 // RUN: %clang -target arm64-linux-none -mmark-bti-property -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK  -check-prefix=CHECK_GEN %s
 // RUN: %clang -target arm64-linux-none -DNOTE_PRESENT -c %s -o - | llvm-readobj -n - | FileCheck -check-prefix=CHECK  -check-prefix=CHECK_PRESET %s
 // RUN: %clang -target arm64-linux-none -mmark-bti-property -DNOTE_PRESENT -c %s -o - | llvm-readobj -n - | FileCheck -check-prefix=CHECK  -check-prefix=CHECK_PRESET %s
 // RUN: %clang -target arm64-linux-none -mmark-bti-property -DNOTE_PRESENT -c %s -o - 2>&1 |  FileCheck -check-prefix=CHECK_WARNING %s
+// RUN: %clang -target arm64-linux-none -mbranch-protection=bti -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK  -check-prefix=CHECK_GEN %s
+// RUN: %clang -target arm64-linux-none -mbranch-protection=bti -DNOTE_PRESENT -c %s -o - 2>&1 |  FileCheck -check-prefix=CHECK_WARNING %s
 //
 // CHECK_WARNING: The .note.gnu.property is not emitted because it is already present.
 // CHECK: Name: .note.gnu.property
Index: clang/lib/Driver/ToolChains/Clang.h
===================================================================
--- clang/lib/Driver/ToolChains/Clang.h
+++ clang/lib/Driver/ToolChains/Clang.h
@@ -128,6 +128,8 @@
                         llvm::opt::ArgStringList &CmdArgs) const;
   void AddRISCVTargetArgs(const llvm::opt::ArgList &Args,
                           llvm::opt::ArgStringList &CmdArgs) const;
+  void AddAArch64TargetArgs(const llvm::opt::ArgList &Args,
+                            llvm::opt::ArgStringList &CmdArgs) const;
   bool hasGoodDiagnostics() const override { return true; }
   bool hasIntegratedAssembler() const override { return false; }
   bool hasIntegratedCPP() const override { return false; }
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -7015,6 +7015,29 @@
   CmdArgs.push_back(ABIName.data());
 }
 
+void ClangAs::AddAArch64TargetArgs(const ArgList &Args,
+                                   ArgStringList &CmdArgs) const {
+  bool BranchTargetEnforce = false;
+  const auto &D = getToolChain().getDriver();
+
+  if (Arg *A = Args.getLastArg(options::OPT_mbranch_protection_EQ)) {
+    StringRef Err;
+    llvm::AArch64::ParsedBranchProtection PBP;
+
+    if (!llvm::AArch64::parseBranchProtection(A->getValue(), PBP, Err)) {
+      D.Diag(diag::err_invalid_branch_protection)
+          << Err << A->getAsString(Args);
+    } else {
+      BranchTargetEnforce = PBP.BranchTargetEnforcement;
+    }
+  }
+
+  if (Args.hasArg(options::OPT_mmark_bti_property) || BranchTargetEnforce) {
+    CmdArgs.push_back("-mllvm");
+    CmdArgs.push_back("-aarch64-mark-bti-property");
+  }
+}
+
 void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
                            const InputInfo &Output, const InputInfoList &Inputs,
                            const ArgList &Args,
@@ -7193,10 +7216,7 @@
   case llvm::Triple::aarch64:
   case llvm::Triple::aarch64_32:
   case llvm::Triple::aarch64_be:
-    if (Args.hasArg(options::OPT_mmark_bti_property)) {
-      CmdArgs.push_back("-mllvm");
-      CmdArgs.push_back("-aarch64-mark-bti-property");
-    }
+    AddAArch64TargetArgs(Args, CmdArgs);
     break;
 
   case llvm::Triple::riscv32:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93428.312332.patch
Type: text/x-patch
Size: 3802 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201217/96dafc48/attachment-0001.bin>


More information about the cfe-commits mailing list