[clang] [Driver] Mark -arch as TargetSpecific (PR #74365)

2023-12-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Unfortunately, the case I was initially trying to fix still has a 
problem after applying this patch. Consider 
`LLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-musl`. In such a case, running `clang 
-arch arm64e -c test.c -###` will show us `"-triple" 
"aarch64-unknown-linux-musl"`. As far as I understood from the comment 
https://github.com/llvm/llvm-project/pull/72821#issuecomment-1839435929, a 
warning `-Wunused-command-line-argument` should be emitted - but it is not 
(this does not change even if we really compile to the object file instead of 
just printing cli args with `-###`).

Are we supposed to pass `-target` to clang in addition to `-arch` if we want to 
be sure to compile with Apple's triple? It might be reasonable, but in such 
case many tests which use `-arch` without `-target` actually might run with 
undesired non-Apple triple - I find such behavior a bit misleading. Is this "by 
design"?

https://github.com/llvm/llvm-project/pull/74365
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Mark -arch as TargetSpecific (PR #74365)

2023-12-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

OK, thanks for such a detailed explanation! Closing 
https://github.com/llvm/llvm-project/pull/72821 as not needed.

https://github.com/llvm/llvm-project/pull/74365
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Enhance handling of Apple-specific '-arch'/'-target' option values (PR #72821)

2023-12-05 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 closed 
https://github.com/llvm/llvm-project/pull/72821
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Enhance handling of Apple-specific '-arch'/'-target' option values (PR #72821)

2023-11-19 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/72821

The '-arch' option itself is Apple-specific, so, if '-target' is not passed 
explicitely, we try to construct an Apple triple. If the default triple is 
Apple-specific, we just update it's arch so it matches the one passed via 
'-arch' option, otherwise, we construct a '-apple-darwin10' triple "from 
scratch".

The arm64e arch value is also Apple-specific, so, if we have 'arm64e' or 
'arm64e-apple' triple, append missing parts to it so it becomes 
'arm64e-apple-darwin10'.

See tests in Driver/apple-specific-options.c for detailed description of how 
different cases are handled.

>From a1ad61f5194c1ba577222e9381d01f836432db32 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 17 Nov 2023 06:33:33 +0300
Subject: [PATCH] [clang] Enhance handling of Apple-specific '-arch'/'-target'
 option values

The '-arch' option itself is Apple-specific, so, if '-target' is not passed
explicitely, we try to construct an Apple triple. If the default triple
is Apple-specific, we just update it's arch so it matches the one passed
via '-arch' option, otherwise, we construct a '-apple-darwin10' triple
"from scratch". Passing just '-arch' without '-target' seems as a common
practice in Apple's tests, and it previously led to undesireable triple
values deduced if a person had, say, linux-specific
LLVM_DEFAULT_TARGET_TRIPLE set.

The arm64e arch value is also Apple-specific, so, if we have 'arm64e' or
'arm64e-apple' triple, append missing parts to it so it becomes
'arm64e-apple-darwin10'.

See tests in Driver/apple-specific-options.c for detailed description of how
different cases are handled.
---
 clang/lib/Driver/Driver.cpp| 28 ++
 clang/test/Driver/apple-specific-options.c | 60 ++
 2 files changed, 88 insertions(+)
 create mode 100644 clang/test/Driver/apple-specific-options.c

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 6f5ff8141032677..4663189933cc1c2 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -521,6 +521,34 @@ static llvm::Triple computeTargetTriple(const Driver &D,
   if (TargetTriple.contains("-unknown-gnu") || 
TargetTriple.contains("-pc-gnu"))
 Target.setOSName("hurd");
 
+  auto SetDefaultAppleTarget = [&Target]() {
+if (Target.getVendorName().empty())
+  Target.setVendor(llvm::Triple::Apple);
+if (Target.getVendor() == llvm::Triple::Apple &&
+Target.getOSAndEnvironmentName().empty())
+  Target.setOSName("darwin10");
+  };
+
+  // Since '-arch' is an Apple-specific option, construct a default Apple 
triple
+  // when '-target' is not explicitely passed.
+  if (Args.hasArg(options::OPT_arch) && !Args.hasArg(options::OPT_target)) {
+StringRef ArchName = Args.getLastArg(options::OPT_arch)->getValue();
+if (Target.isOSBinFormatMachO()) {
+  // The default triple is already Apple-specific - just update the arch.
+  tools::darwin::setTripleTypeForMachOArchName(Target, ArchName, Args);
+} else {
+  // The default triple is not Apple-specific - construct a new one to 
avoid
+  // handling unrelated info from the default one (e.g. environment).
+  Target = llvm::Triple(ArchName);
+  SetDefaultAppleTarget();
+}
+  }
+
+  // Since arm64e arch is Apple-specific, set VendorName and OS correspondingly
+  // if not set already.
+  if (Target.getArchName() == "arm64e")
+SetDefaultAppleTarget();
+
   // Handle Apple-specific options available here.
   if (Target.isOSBinFormatMachO()) {
 // If an explicit Darwin arch name is given, that trumps all.
diff --git a/clang/test/Driver/apple-specific-options.c 
b/clang/test/Driver/apple-specific-options.c
new file mode 100644
index 000..b683bf5a3de3a8f
--- /dev/null
+++ b/clang/test/Driver/apple-specific-options.c
@@ -0,0 +1,60 @@
+// Without '-target' explicitly passed, construct the default triple.
+// If the LLVM_DEFAULT_TARGET_TRIPLE is a Darwin triple, change it's 
architecture
+// to a one passed via '-arch'. Otherwise, use '-apple-darwin10'.
+
+// RUN: %clang -arch x86_64 -c %s -### 2>&1 | \
+// RUN:   FileCheck %s --check-prefix ARCH
+
+// ARCH: "-triple" "x86_64-apple-
+
+// For non-Darwin explicitly passed '-target', ignore '-arch'.
+
+// RUN: %clang -arch arm64 -target x86_64-unknown-linux -c %s -### 2>&1 | \
+// RUN:   FileCheck %s --check-prefix ARCH_NON_DARWIN1
+
+// ARCH_NON_DARWIN1: "-triple" "x86_64-unknown-linux"
+
+// RUN: %clang -arch arm64 -target x86_64-apple -c %s -### 2>&1 | \
+// RUN:   FileCheck %s --check-prefix ARCH_NON_DARWIN2
+
+// ARCH_NON_DARWIN2: "-triple" "x86_64-apple"
+
+
+// For Darwin explicitly passed '-target', the '-arch' option overrides the 
architecture
+
+// RUN: %clang -arch arm64 -target x86_64-apple-ios7.0.0 -c %s -### 2>&1 | \
+// RUN:   FileCheck %s --check-prefix ARCH_DARWIN
+
+// ARCH_DARWIN: "-triple" "arm64-apple-ios7.0.0"
+
+
+// For 'arm64e' and 'arm6

[clang] [clang] Enhance handling of Apple-specific '-arch'/'-target' option values (PR #72821)

2023-11-19 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/72821
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Enhance handling of Apple-specific '-arch'/'-target' option values (PR #72821)

2023-12-04 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay @TNorthover @Artem-B Would be glad to see you review on the changes

https://github.com/llvm/llvm-project/pull/72821
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lld] [clang-tools-extra] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/72714

>From c493d78e6c482bb530189de05b79e7082a224fab Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 28 Sep 2023 03:14:35 +0300
Subject: [PATCH 1/5] [lld][AArch64][ELF][PAC] Support AUTH relocations and
 AUTH ELF marking

This patch adds lld support for:

- Dynamic R_AARCH64_AUTH_* relocations (including RELR compressed AUTH
  relocations) as described here:
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations

- .note.AARCH64-PAUTH-ABI-tag section as defined here
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking

Co-authored-by: Peter Collingbourne 
---
 lld/ELF/Arch/AArch64.cpp |   5 +
 lld/ELF/Config.h |   4 +
 lld/ELF/Driver.cpp   |  57 +-
 lld/ELF/InputFiles.cpp   |  44 
 lld/ELF/InputFiles.h |   1 +
 lld/ELF/Relocations.cpp  |  26 +
 lld/ELF/SyntheticSections.cpp|  44 ++--
 lld/ELF/SyntheticSections.h  |  19 +++-
 lld/ELF/Writer.cpp   |  17 +++
 lld/test/ELF/aarch64-feature-pauth.s |  83 ++
 lld/test/ELF/aarch64-ptrauth.s   | 156 +++
 11 files changed, 445 insertions(+), 11 deletions(-)
 create mode 100644 lld/test/ELF/aarch64-feature-pauth.s
 create mode 100644 lld/test/ELF/aarch64-ptrauth.s

diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 048f0ec30ebd28..6828d3f57c10e8 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -112,6 +112,7 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   case R_AARCH64_MOVW_UABS_G2:
   case R_AARCH64_MOVW_UABS_G2_NC:
   case R_AARCH64_MOVW_UABS_G3:
+  case R_AARCH64_AUTH_ABS64:
 return R_ABS;
   case R_AARCH64_TLSDESC_ADR_PAGE21:
 return R_AARCH64_TLSDESC_PAGE;
@@ -395,6 +396,10 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
   case R_AARCH64_PREL64:
 write64(loc, val);
 break;
+  case R_AARCH64_AUTH_ABS64:
+checkIntUInt(loc, val, 32, rel);
+write32(loc, val);
+break;
   case R_AARCH64_ADD_ABS_LO12_NC:
 or32AArch64Imm(loc, val);
 break;
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 56229334f9a44a..1b633a79842769 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -187,6 +187,7 @@ struct Config {
   llvm::StringRef cmseOutputLib;
   StringRef zBtiReport = "none";
   StringRef zCetReport = "none";
+  StringRef zPauthReport = "none";
   llvm::StringRef ltoBasicBlockSections;
   std::pair thinLTOObjectSuffixReplace;
   llvm::StringRef thinLTOPrefixReplaceOld;
@@ -275,6 +276,7 @@ struct Config {
   bool relocatable;
   bool relrGlibc = false;
   bool relrPackDynRelocs = false;
+  bool relrPackAuthDynRelocs = false;
   llvm::DenseSet saveTempsArgs;
   llvm::SmallVector, 0> shuffleSections;
   bool singleRoRx;
@@ -492,6 +494,8 @@ struct Ctx {
   void reset();
 
   llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
+
+  SmallVector aarch64PauthAbiTag;
 };
 
 LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6bef09eeca015a..4e8e9eb86ecf77 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -65,6 +65,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/raw_ostream.h"
+#include 
 #include 
 #include 
 #include 
@@ -459,6 +460,8 @@ static void checkOptions() {
   error("-z force-bti only supported on AArch64");
 if (config->zBtiReport != "none")
   error("-z bti-report only supported on AArch64");
+if (config->zPauthReport != "none")
+  error("-z pauth-report only supported on AArch64");
   }
 
   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
@@ -558,6 +561,7 @@ constexpr const char *knownZFlags[] = {
 "nognustack",
 "nokeep-text-section-prefix",
 "nopack-relative-relocs",
+"nopack-relative-auth-relocs",
 "norelro",
 "noseparate-code",
 "nostart-stop-gc",
@@ -566,6 +570,7 @@ constexpr const char *knownZFlags[] = {
 "origin",
 "pac-plt",
 "pack-relative-relocs",
+"pack-relative-auth-relocs",
 "rel",
 "rela",
 "relro",
@@ -583,7 +588,7 @@ constexpr const char *knownZFlags[] = {
 static bool isKnownZFlag(StringRef s) {
   return llvm::is_contained(knownZFlags, s) ||
  s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
- s.starts_with("cet-report=") ||
+ s.starts_with("cet-report=") || s.starts_with("pauth-report=") ||
  s.starts_with("dead-reloc-in-nonalloc=") ||
  s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
  s.starts_with("start-stop-visibility=");
@@ -1514,7 +1519,8 @@ static void readConfigs(opt::InputArgList &args) {
   }
 
   auto reports = {std::make_pair("bti-

[clang] [lld] [clang-tools-extra] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,156 @@
+// REQUIRES: aarch64
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64 %p/Inputs/shared2.s -o %t.so.o
+// RUN: ld.lld -shared %t.so.o -soname=so -o %t.so
+// RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t.o
+// RUN: ld.lld -pie -z nopack-relative-auth-relocs %t.o %t.so -o %t2
+// RUN: llvm-readobj -r %t2 | FileCheck --check-prefix=UNPACKED %s
+
+// UNPACKED:  Section ({{.+}}) .rela.dyn {
+// UNPACKED-NEXT:   0x30680 R_AARCH64_AUTH_RELATIVE - 0x1
+// UNPACKED-NEXT:   0x30688 R_AARCH64_AUTH_RELATIVE - 0x2
+// UNPACKED-NEXT:   0x30690 R_AARCH64_AUTH_RELATIVE - 0x3
+// UNPACKED-NEXT:   0x30698 R_AARCH64_AUTH_RELATIVE - 0x4
+// UNPACKED-NEXT:   0x306A0 R_AARCH64_AUTH_RELATIVE - 0x5
+// UNPACKED-NEXT:   0x306A8 R_AARCH64_AUTH_RELATIVE - 0x6
+// UNPACKED-NEXT:   0x306B0 R_AARCH64_AUTH_RELATIVE - 0x7
+// UNPACKED-NEXT:   0x306B8 R_AARCH64_AUTH_RELATIVE - 0x8
+// UNPACKED-NEXT:   0x306C8 R_AARCH64_AUTH_RELATIVE - 0x1
+// UNPACKED-NEXT:   0x306D0 R_AARCH64_AUTH_RELATIVE - 0x2
+// UNPACKED-NEXT:   0x306D8 R_AARCH64_AUTH_RELATIVE - 0x3
+// UNPACKED-NEXT:   0x306E0 R_AARCH64_AUTH_RELATIVE - 0x4
+// UNPACKED-NEXT:   0x306E8 R_AARCH64_AUTH_RELATIVE - 0x5
+// UNPACKED-NEXT:   0x306F0 R_AARCH64_AUTH_RELATIVE - 0x6
+// UNPACKED-NEXT:   0x306F8 R_AARCH64_AUTH_RELATIVE - 0x7
+// UNPACKED-NEXT:   0x30710 R_AARCH64_AUTH_RELATIVE - 0x1
+// UNPACKED-NEXT:   0x30718 R_AARCH64_AUTH_RELATIVE - 0x2
+// UNPACKED-NEXT:   0x30720 R_AARCH64_AUTH_RELATIVE - 0x3
+// UNPACKED-NEXT:   0x30728 R_AARCH64_AUTH_RELATIVE - 0x4
+// UNPACKED-NEXT:   0x30730 R_AARCH64_AUTH_RELATIVE - 0x5
+// UNPACKED-NEXT:   0x30738 R_AARCH64_AUTH_RELATIVE - 0x6
+// UNPACKED-NEXT:   0x30740 R_AARCH64_AUTH_RELATIVE - 0x7
+// UNPACKED-NEXT:   0x30748 R_AARCH64_AUTH_RELATIVE - 0x8
+// UNPACKED-NEXT:   0x30750 R_AARCH64_AUTH_RELATIVE - 0x9
+// UNPACKED-NEXT:   0x30759 R_AARCH64_AUTH_RELATIVE - 0xA
+// UNPACKED-NEXT:   0x306C0 R_AARCH64_AUTH_ABS64 bar2 0x1
+// UNPACKED-NEXT:   0x30708 R_AARCH64_AUTH_ABS64 bar2 0x0
+// UNPACKED-NEXT:   0x30761 R_AARCH64_AUTH_ABS64 bar2 0x0
+// UNPACKED-NEXT:   0x30769 R_AARCH64_AUTH_ABS64 bar2 0x0
+// UNPACKED-NEXT:   0x30771 R_AARCH64_AUTH_ABS64 bar2 0x1
+// UNPACKED-NEXT:   0x30779 R_AARCH64_AUTH_ABS64 bar2 0x1
+// UNPACKED-NEXT:   0x30781 R_AARCH64_AUTH_ABS64 bar2 0x0
+// UNPACKED-NEXT:   0x30700 R_AARCH64_AUTH_ABS64 zed2 0x0
+// UNPACKED-NEXT: }
+
+// RUN: ld.lld -pie -z pack-relative-auth-relocs %t.o %t.so -o %t2
+// RUN: llvm-readobj -S --dynamic-table %t2 | FileCheck 
--check-prefix=RELR-HEADERS %s
+// RUN: llvm-readobj -r --raw-relr %t2 | FileCheck --check-prefix=RAW-RELR %s
+// RUN: llvm-readobj -r %t2 | FileCheck --check-prefix=RELR %s
+
+// RELR-HEADERS:   Index: 1
+// RELR-HEADERS-NEXT:  Name: .dynsym
+
+// RELR-HEADERS:   Name: .relr.auth.dyn
+// RELR-HEADERS-NEXT:  Type: SHT_AARCH64_AUTH_RELR
+// RELR-HEADERS-NEXT:  Flags [ (0x2)
+// RELR-HEADERS-NEXT:SHF_ALLOC (0x2)
+// RELR-HEADERS-NEXT:  ]
+// RELR-HEADERS-NEXT:  Address: [[ADDR:.*]]
+// RELR-HEADERS-NEXT:  Offset: [[ADDR]]
+// RELR-HEADERS-NEXT:  Size: 16
+// RELR-HEADERS-NEXT:  Link: 0
+// RELR-HEADERS-NEXT:  Info: 0
+// RELR-HEADERS-NEXT:  AddressAlignment: 8
+// RELR-HEADERS-NEXT:  EntrySize: 8
+
+// RELR-HEADERS:   0x7012 AARCH64_AUTH_RELR[[ADDR]]
+// RELR-HEADERS:   0x7011 AARCH64_AUTH_RELRSZ  16 (bytes)
+// RELR-HEADERS:   0x7013 AARCH64_AUTH_RELRENT 8 (bytes)
+
+/// SHT_RELR section contains address/bitmap entries
+/// encoding the offsets for relative relocation.
+// RAW-RELR:   Section ({{.+}}) .relr.auth.dyn {
+// RAW-RELR-NEXT:  0x30480
+// RAW-RELR-NEXT:  0x7FCFEFF
+// RAW-RELR-NEXT:  }
+
+/// Decoded SHT_RELR section is same as UNPACKED,
+/// but contains only the relative relocations.
+/// Any relative relocations with odd offset stay in SHT_RELA.
+
+// RELR:  Section ({{.+}}) .rela.dyn {
+// RELR-NEXT:   0x30559 R_AARCH64_AUTH_RELATIVE - 0xA
+// RELR-NEXT:   0x304C0 R_AARCH64_AUTH_ABS64 bar2 0x1
+// RELR-NEXT:   0x30508 R_AARCH64_AUTH_ABS64 bar2 0x0
+// RELR-NEXT:   0x30561 R_AARCH64_AUTH_ABS64 bar2 0x0
+// RELR-NEXT:   0x30569 R_AARCH64_AUTH_ABS64 bar2 0x0
+// RELR-NEXT:   0x30571 R_AARCH64_AUTH_ABS64 bar2 0x1
+// RELR-NEXT:   0x30579 R_AARCH64_AUTH_ABS64 bar2 0x1
+// RELR-NEXT:   0x30581 R_AARCH64_AUTH_ABS64 bar2 0x0
+// RELR-NEXT:   0x30500 R_AARCH64_AUTH_ABS64 zed2 0x0
+// RELR-NEXT: }
+// RELR-NEXT: Section ({{.+}}) .relr.auth.dyn {
+// RELR-NEXT:   0x30480 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x30488 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x30490 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x30498 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x304A0 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x304A8 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x304B0 R_AARCH64_RELATIVE -
+// RELR-NEXT:   0x304B8 R_AARCH64_

[clang] [lld] [clang-tools-extra] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,156 @@
+// REQUIRES: aarch64

kovdan01 wrote:

Renamed aarch64-ptrauth.s to aarch64-reloc-pauth.s to make naming consistent 
with existing tests, aarch64-feature-pauth.s checks the 
`.note.AARCH64-PAUTH-ABI-tag` stuff. See 
b791da9dd02f8b3bf59d2d235181f945844d3039.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lld] [clang-tools-extra] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits


@@ -1444,6 +1444,32 @@ template  void 
RelocationScanner::scanOne(RelTy *&i) {
 }
   }
 
+  if (config->emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {
+// Assume relocations from relocatable objects are RELA.
+assert(RelTy::IsRela);
+std::lock_guard lock(relocMutex);
+// For a preemptible symbol, we can't use a relative relocation. For an
+// undefined symbol, we can't compute offset at link-time and use a 
relative
+// relocation. Use a symbolic relocation instead.
+Partition &part = sec->getPartition();
+if (sym.isPreemptible || sym.isUndefined()) {
+  part.relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
+} else if (part.relrAuthDyn && sec->addralign >= 2 && offset % 2 == 0 &&
+   isInt<32>(sym.getVA(addend))) {
+  // Implicit addend is below 32-bits so we can use the compressed
+  // relative relocation section. The R_AARCH64_AUTH_RELATIVE
+  // has a smaller addend fielf as bits [63:32] encode the signing-schema.
+  sec->addReloc({expr, type, offset, addend, &sym});
+  part.relrAuthDyn->relocsVec[parallel::getThreadIndex()].push_back(
+  {sec, offset});
+} else {
+  part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset,
+  DynamicReloc::AddendOnlyWithTargetVA, sym, 
addend,
+  R_ABS});
+}
+return;
+  }
+

kovdan01 wrote:

Yes, currently we just emit AUTH relocation even against read-only sections 
with `-z text`, which should not be the case and the error should be emitted 
(if I'm not mistaken). Thanks for bringing attention to this. I'll prepare a 
fix for that and submit it shortly. Things seem to be a little bit different 
from what we already have after `if (canWrite)`: with regular relocs, we emit a 
plt entry for function symbols, while with auth relocs we probably should just 
emit an error for all symbol kinds. Anyway, the fix seems easy enough.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [lld] [clang-tools-extra] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits


@@ -566,6 +570,7 @@ constexpr const char *knownZFlags[] = {
 "origin",
 "pac-plt",
 "pack-relative-relocs",
+"pack-relative-auth-relocs",

kovdan01 wrote:

Update on my previous comment: changing `getPackDynRelocs` as I suggested above 
is probably not the best option since android packed relocs just replace the 
regular relocation section (`lld::elf::Partition::relaDyn`), while relr relocs 
(both regular and auth ones) have their own separate sections 
(`lld::elf::Partition::relrDyn` and `lld::elf::Partition::relrAuthDyn`). So, 
changed the logic to what you have suggested - just use `pack-relative-relocs` 
option for both regular and auth relr relocations. See 
d3411595fbbe37a14b0187ff0d134bd105892931.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [lld] [clang-tools-extra] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-09 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

- Addressed the review comments except 
https://github.com/llvm/llvm-project/pull/72714/#discussion_r1423345089 (I'll 
submit a subsequent commit addressing that shortly).
- Fixed merge conflict appeared after #77300, see 
a021f15540300e032446825de805143f0f6214c4.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [lld] [clang] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-10 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/72714

>From c493d78e6c482bb530189de05b79e7082a224fab Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 28 Sep 2023 03:14:35 +0300
Subject: [PATCH 1/6] [lld][AArch64][ELF][PAC] Support AUTH relocations and
 AUTH ELF marking

This patch adds lld support for:

- Dynamic R_AARCH64_AUTH_* relocations (including RELR compressed AUTH
  relocations) as described here:
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations

- .note.AARCH64-PAUTH-ABI-tag section as defined here
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking

Co-authored-by: Peter Collingbourne 
---
 lld/ELF/Arch/AArch64.cpp |   5 +
 lld/ELF/Config.h |   4 +
 lld/ELF/Driver.cpp   |  57 +-
 lld/ELF/InputFiles.cpp   |  44 
 lld/ELF/InputFiles.h |   1 +
 lld/ELF/Relocations.cpp  |  26 +
 lld/ELF/SyntheticSections.cpp|  44 ++--
 lld/ELF/SyntheticSections.h  |  19 +++-
 lld/ELF/Writer.cpp   |  17 +++
 lld/test/ELF/aarch64-feature-pauth.s |  83 ++
 lld/test/ELF/aarch64-ptrauth.s   | 156 +++
 11 files changed, 445 insertions(+), 11 deletions(-)
 create mode 100644 lld/test/ELF/aarch64-feature-pauth.s
 create mode 100644 lld/test/ELF/aarch64-ptrauth.s

diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 048f0ec30ebd28..6828d3f57c10e8 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -112,6 +112,7 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   case R_AARCH64_MOVW_UABS_G2:
   case R_AARCH64_MOVW_UABS_G2_NC:
   case R_AARCH64_MOVW_UABS_G3:
+  case R_AARCH64_AUTH_ABS64:
 return R_ABS;
   case R_AARCH64_TLSDESC_ADR_PAGE21:
 return R_AARCH64_TLSDESC_PAGE;
@@ -395,6 +396,10 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
   case R_AARCH64_PREL64:
 write64(loc, val);
 break;
+  case R_AARCH64_AUTH_ABS64:
+checkIntUInt(loc, val, 32, rel);
+write32(loc, val);
+break;
   case R_AARCH64_ADD_ABS_LO12_NC:
 or32AArch64Imm(loc, val);
 break;
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 56229334f9a44a..1b633a79842769 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -187,6 +187,7 @@ struct Config {
   llvm::StringRef cmseOutputLib;
   StringRef zBtiReport = "none";
   StringRef zCetReport = "none";
+  StringRef zPauthReport = "none";
   llvm::StringRef ltoBasicBlockSections;
   std::pair thinLTOObjectSuffixReplace;
   llvm::StringRef thinLTOPrefixReplaceOld;
@@ -275,6 +276,7 @@ struct Config {
   bool relocatable;
   bool relrGlibc = false;
   bool relrPackDynRelocs = false;
+  bool relrPackAuthDynRelocs = false;
   llvm::DenseSet saveTempsArgs;
   llvm::SmallVector, 0> shuffleSections;
   bool singleRoRx;
@@ -492,6 +494,8 @@ struct Ctx {
   void reset();
 
   llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
+
+  SmallVector aarch64PauthAbiTag;
 };
 
 LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6bef09eeca015a..4e8e9eb86ecf77 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -65,6 +65,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/raw_ostream.h"
+#include 
 #include 
 #include 
 #include 
@@ -459,6 +460,8 @@ static void checkOptions() {
   error("-z force-bti only supported on AArch64");
 if (config->zBtiReport != "none")
   error("-z bti-report only supported on AArch64");
+if (config->zPauthReport != "none")
+  error("-z pauth-report only supported on AArch64");
   }
 
   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
@@ -558,6 +561,7 @@ constexpr const char *knownZFlags[] = {
 "nognustack",
 "nokeep-text-section-prefix",
 "nopack-relative-relocs",
+"nopack-relative-auth-relocs",
 "norelro",
 "noseparate-code",
 "nostart-stop-gc",
@@ -566,6 +570,7 @@ constexpr const char *knownZFlags[] = {
 "origin",
 "pac-plt",
 "pack-relative-relocs",
+"pack-relative-auth-relocs",
 "rel",
 "rela",
 "relro",
@@ -583,7 +588,7 @@ constexpr const char *knownZFlags[] = {
 static bool isKnownZFlag(StringRef s) {
   return llvm::is_contained(knownZFlags, s) ||
  s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
- s.starts_with("cet-report=") ||
+ s.starts_with("cet-report=") || s.starts_with("pauth-report=") ||
  s.starts_with("dead-reloc-in-nonalloc=") ||
  s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
  s.starts_with("start-stop-visibility=");
@@ -1514,7 +1519,8 @@ static void readConfigs(opt::InputArgList &args) {
   }
 
   auto reports = {std::make_pair("bti-

[clang-tools-extra] [lld] [clang] [llvm] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-10 Thread Daniil Kovalev via cfe-commits


@@ -1444,6 +1444,32 @@ template  void 
RelocationScanner::scanOne(RelTy *&i) {
 }
   }
 
+  if (config->emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {
+// Assume relocations from relocatable objects are RELA.
+assert(RelTy::IsRela);
+std::lock_guard lock(relocMutex);
+// For a preemptible symbol, we can't use a relative relocation. For an
+// undefined symbol, we can't compute offset at link-time and use a 
relative
+// relocation. Use a symbolic relocation instead.
+Partition &part = sec->getPartition();
+if (sym.isPreemptible || sym.isUndefined()) {
+  part.relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);
+} else if (part.relrAuthDyn && sec->addralign >= 2 && offset % 2 == 0 &&
+   isInt<32>(sym.getVA(addend))) {
+  // Implicit addend is below 32-bits so we can use the compressed
+  // relative relocation section. The R_AARCH64_AUTH_RELATIVE
+  // has a smaller addend fielf as bits [63:32] encode the signing-schema.
+  sec->addReloc({expr, type, offset, addend, &sym});
+  part.relrAuthDyn->relocsVec[parallel::getThreadIndex()].push_back(
+  {sec, offset});
+} else {
+  part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset,
+  DynamicReloc::AddendOnlyWithTargetVA, sym, 
addend,
+  R_ABS});
+}
+return;
+  }
+

kovdan01 wrote:

See fix in 594f8a0e8331b5d11f3efc58fcaa7eae4b9fd7b4. Basically, it disallows 
auth relocations if `canWrite` is false. Emitting plt entries for `STT_FUNC` 
symbols and copy relocations for `STT_OBJECT` ones looks meaningless in context 
of auth relocations. Existing error messages did not seem to make sense, so 
I've added a new if statement for this particular case with a different message 
text - not sure if it's applicable though. Please let me know your thoughts on 
the change.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[lld] [llvm] [clang] [clang-tools-extra] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-10 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Published updates on all issues you've mentioned - would be glad to 
see your comments on new changes.


https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] f854434 - [NVPTX] Enhance vectorization of ld.param & st.param

2022-03-24 Thread Daniil Kovalev via cfe-commits

Author: Daniil Kovalev
Date: 2022-03-24T12:25:36+03:00
New Revision: f854434f0f2a01027bdaad8e6fdac5a782fce291

URL: 
https://github.com/llvm/llvm-project/commit/f854434f0f2a01027bdaad8e6fdac5a782fce291
DIFF: 
https://github.com/llvm/llvm-project/commit/f854434f0f2a01027bdaad8e6fdac5a782fce291.diff

LOG: [NVPTX] Enhance vectorization of ld.param & st.param

Since function parameters and return values are passed via param space, we
can force special alignment for values hold in it which will add vectorization
options. This change may be done if the function has private or internal
linkage. Special alignment is forced during 2 phases.

1) Instruction selection lowering. Here we use special alignment for function
   prototypes (changing both own return value and parameters alignment), call
   lowering (changing both callee's return value and parameters alignment).

2) IR pass nvptx-lower-args. Here we change alignment of byval parameters that
   belong to param space (or are casted to it). We only handle cases when all
   uses of such parameters are loads from it. For such loads, we can change the
   alignment according to special type alignment and the load offset. Then,
   load-store-vectorizer IR pass will perform vectorization where alignment
   allows it.

Special alignment calculated as maximum from default ABI type alignment and
alignment 16. Alignment 16 is chosen because it's the maximum size of
vectorized ld.param & st.param.

Before specifying such special alignment, we should check if it is a multiple
of the alignment that the type already has. For example, if a value has an
enforced alignment of 64, default ABI alignment of 4 and special alignment
of 16, we should preserve 64.

This patch will be followed by a refactoring patch that removes duplicating
code in handling byval and non-byval arguments.

Differential Revision: https://reviews.llvm.org/D121549

Added: 
llvm/test/CodeGen/NVPTX/param-vectorize-device.ll
llvm/test/CodeGen/NVPTX/param-vectorize-kernel.ll

Modified: 
clang/test/CodeGenCUDA/device-fun-linkage.cu
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.h
llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp

Removed: 




diff  --git a/clang/test/CodeGenCUDA/device-fun-linkage.cu 
b/clang/test/CodeGenCUDA/device-fun-linkage.cu
index d1b9db261151b..d8ad6d438be9c 100644
--- a/clang/test/CodeGenCUDA/device-fun-linkage.cu
+++ b/clang/test/CodeGenCUDA/device-fun-linkage.cu
@@ -1,19 +1,32 @@
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
-// RUN:   -emit-llvm -o - %s \
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
 // RUN:   | FileCheck -check-prefix=NORDC %s
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
-// RUN:   -fgpu-rdc -emit-llvm -o - %s \
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
+// RUN:   | FileCheck -check-prefix=NORDC-NEG %s
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
 // RUN:   | FileCheck -check-prefix=RDC %s
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
+// RUN:   | FileCheck -check-prefix=RDC-NEG %s
 
 #include "Inputs/cuda.h"
 
-// NORDC: define internal void @_Z4funcIiEvv()
-// NORDC: define{{.*}} void @_Z6kernelIiEvv()
-// RDC: define weak_odr void @_Z4funcIiEvv()
-// RDC: define weak_odr void @_Z6kernelIiEvv()
-
 template  __device__ void func() {}
 template  __global__ void kernel() {}
 
 template __device__ void func();
+// NORDC: define internal void @_Z4funcIiEvv()
+// RDC:   define weak_odr void @_Z4funcIiEvv()
+
 template __global__ void kernel();
+// NORDC: define void @_Z6kernelIiEvv()
+// RDC:   define weak_odr void @_Z6kernelIiEvv()
+
+// Ensure that unused static device function is eliminated
+static __device__ void static_func() {}
+// NORDC-NEG-NOT: define{{.*}} void @_ZL13static_funcv()
+// RDC-NEG-NOT:   define{{.*}} void @_ZL13static_funcv()
+
+// Ensure that kernel function has external or weak_odr
+// linkage regardless static specifier
+static __global__ void static_kernel() {}
+// NORDC: define void @_ZL13static_kernelv()
+// RDC:   define weak_odr void @_ZL13static_kernelv()

diff  --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp 
b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 2516dff52efdf..e8322a0a8425b 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -329,7 +329,7 @@ MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol 
*Symbol) {
 void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
   const DataLayout &DL = getDataLayout();
   const NVPTXSubtarget &STI = TM.getSubtarget(*F);
-  const TargetLowering *TLI = STI.getTargetLowering();
+  const auto *TLI = cast(STI.getTargetLowering());
 
   Type *Ty = F->getReturnType();
 
@@

[clang] a034878 - Revert "[NVPTX] Enhance vectorization of ld.param & st.param"

2022-03-24 Thread Daniil Kovalev via cfe-commits

Author: Daniil Kovalev
Date: 2022-03-24T12:32:06+03:00
New Revision: a0348785649271e8c63a42bd4a83a2fefa96efe0

URL: 
https://github.com/llvm/llvm-project/commit/a0348785649271e8c63a42bd4a83a2fefa96efe0
DIFF: 
https://github.com/llvm/llvm-project/commit/a0348785649271e8c63a42bd4a83a2fefa96efe0.diff

LOG: Revert "[NVPTX] Enhance vectorization of ld.param & st.param"

This reverts commit f854434f0f2a01027bdaad8e6fdac5a782fce291.

Placed URL to wrong differential revision in commit message.

Added: 


Modified: 
clang/test/CodeGenCUDA/device-fun-linkage.cu
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.h
llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp

Removed: 
llvm/test/CodeGen/NVPTX/param-vectorize-device.ll
llvm/test/CodeGen/NVPTX/param-vectorize-kernel.ll



diff  --git a/clang/test/CodeGenCUDA/device-fun-linkage.cu 
b/clang/test/CodeGenCUDA/device-fun-linkage.cu
index d8ad6d438be9c..d1b9db261151b 100644
--- a/clang/test/CodeGenCUDA/device-fun-linkage.cu
+++ b/clang/test/CodeGenCUDA/device-fun-linkage.cu
@@ -1,32 +1,19 @@
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
+// RUN:   -emit-llvm -o - %s \
 // RUN:   | FileCheck -check-prefix=NORDC %s
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
-// RUN:   | FileCheck -check-prefix=NORDC-NEG %s
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
+// RUN:   -fgpu-rdc -emit-llvm -o - %s \
 // RUN:   | FileCheck -check-prefix=RDC %s
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
-// RUN:   | FileCheck -check-prefix=RDC-NEG %s
 
 #include "Inputs/cuda.h"
 
+// NORDC: define internal void @_Z4funcIiEvv()
+// NORDC: define{{.*}} void @_Z6kernelIiEvv()
+// RDC: define weak_odr void @_Z4funcIiEvv()
+// RDC: define weak_odr void @_Z6kernelIiEvv()
+
 template  __device__ void func() {}
 template  __global__ void kernel() {}
 
 template __device__ void func();
-// NORDC: define internal void @_Z4funcIiEvv()
-// RDC:   define weak_odr void @_Z4funcIiEvv()
-
 template __global__ void kernel();
-// NORDC: define void @_Z6kernelIiEvv()
-// RDC:   define weak_odr void @_Z6kernelIiEvv()
-
-// Ensure that unused static device function is eliminated
-static __device__ void static_func() {}
-// NORDC-NEG-NOT: define{{.*}} void @_ZL13static_funcv()
-// RDC-NEG-NOT:   define{{.*}} void @_ZL13static_funcv()
-
-// Ensure that kernel function has external or weak_odr
-// linkage regardless static specifier
-static __global__ void static_kernel() {}
-// NORDC: define void @_ZL13static_kernelv()
-// RDC:   define weak_odr void @_ZL13static_kernelv()

diff  --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp 
b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index e8322a0a8425b..2516dff52efdf 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -329,7 +329,7 @@ MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol 
*Symbol) {
 void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
   const DataLayout &DL = getDataLayout();
   const NVPTXSubtarget &STI = TM.getSubtarget(*F);
-  const auto *TLI = cast(STI.getTargetLowering());
+  const TargetLowering *TLI = STI.getTargetLowering();
 
   Type *Ty = F->getReturnType();
 
@@ -363,7 +363,7 @@ void NVPTXAsmPrinter::printReturnValStr(const Function *F, 
raw_ostream &O) {
   unsigned totalsz = DL.getTypeAllocSize(Ty);
   unsigned retAlignment = 0;
   if (!getAlign(*F, 0, retAlignment))
-retAlignment = TLI->getFunctionParamOptimizedAlign(F, Ty, DL).value();
+retAlignment = DL.getABITypeAlignment(Ty);
   O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz
 << "]";
 } else
@@ -1348,8 +1348,7 @@ void NVPTXAsmPrinter::emitFunctionParamList(const 
Function *F, raw_ostream &O) {
   const DataLayout &DL = getDataLayout();
   const AttributeList &PAL = F->getAttributes();
   const NVPTXSubtarget &STI = TM.getSubtarget(*F);
-  const auto *TLI = cast(STI.getTargetLowering());
-
+  const TargetLowering *TLI = STI.getTargetLowering();
   Function::const_arg_iterator I, E;
   unsigned paramIndex = 0;
   bool first = true;
@@ -1406,24 +1405,18 @@ void NVPTXAsmPrinter::emitFunctionParamList(const 
Function *F, raw_ostream &O) {
   }
 }
 
-auto getOptimalAlignForParam = [TLI, &DL, &PAL, F,
-paramIndex](Type *Ty) -> Align {
-  Align TypeAlign = TLI->getFunctionParamOptimizedAlign(F, Ty, DL);
-  MaybeAlign ParamAlign = PAL.getParamAlignment(paramIndex);
-  return max(TypeAlign, ParamAlign);
-};
-
 if (!PAL.hasParamAttr(paramIndex, Attribute::By

[clang] 828b63c - [NVPTX] Enhance vectorization of ld.param & st.param

2022-03-24 Thread Daniil Kovalev via cfe-commits

Author: Daniil Kovalev
Date: 2022-03-24T12:36:52+03:00
New Revision: 828b63c309439d3b0915205f1e5a159c69cb36b8

URL: 
https://github.com/llvm/llvm-project/commit/828b63c309439d3b0915205f1e5a159c69cb36b8
DIFF: 
https://github.com/llvm/llvm-project/commit/828b63c309439d3b0915205f1e5a159c69cb36b8.diff

LOG: [NVPTX] Enhance vectorization of ld.param & st.param

Since function parameters and return values are passed via param space, we
can force special alignment for values hold in it which will add vectorization
options. This change may be done if the function has private or internal
linkage. Special alignment is forced during 2 phases.

1) Instruction selection lowering. Here we use special alignment for function
   prototypes (changing both own return value and parameters alignment), call
   lowering (changing both callee's return value and parameters alignment).

2) IR pass nvptx-lower-args. Here we change alignment of byval parameters that
   belong to param space (or are casted to it). We only handle cases when all
   uses of such parameters are loads from it. For such loads, we can change the
   alignment according to special type alignment and the load offset. Then,
   load-store-vectorizer IR pass will perform vectorization where alignment
   allows it.

Special alignment calculated as maximum from default ABI type alignment and
alignment 16. Alignment 16 is chosen because it's the maximum size of
vectorized ld.param & st.param.

Before specifying such special alignment, we should check if it is a multiple
of the alignment that the type already has. For example, if a value has an
enforced alignment of 64, default ABI alignment of 4 and special alignment
of 16, we should preserve 64.

This patch will be followed by a refactoring patch that removes duplicating
code in handling byval and non-byval arguments.

Differential Revision: https://reviews.llvm.org/D120129

Added: 
llvm/test/CodeGen/NVPTX/param-vectorize-device.ll
llvm/test/CodeGen/NVPTX/param-vectorize-kernel.ll

Modified: 
clang/test/CodeGenCUDA/device-fun-linkage.cu
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.h
llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp

Removed: 




diff  --git a/clang/test/CodeGenCUDA/device-fun-linkage.cu 
b/clang/test/CodeGenCUDA/device-fun-linkage.cu
index d1b9db261151b..d8ad6d438be9c 100644
--- a/clang/test/CodeGenCUDA/device-fun-linkage.cu
+++ b/clang/test/CodeGenCUDA/device-fun-linkage.cu
@@ -1,19 +1,32 @@
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
-// RUN:   -emit-llvm -o - %s \
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
 // RUN:   | FileCheck -check-prefix=NORDC %s
-// RUN: %clang_cc1 -triple nvptx -fcuda-is-device \
-// RUN:   -fgpu-rdc -emit-llvm -o - %s \
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -emit-llvm -o - %s \
+// RUN:   | FileCheck -check-prefix=NORDC-NEG %s
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
 // RUN:   | FileCheck -check-prefix=RDC %s
+// RUN: %clang_cc1 -triple nvptx -fcuda-is-device -fgpu-rdc -emit-llvm -o - %s 
\
+// RUN:   | FileCheck -check-prefix=RDC-NEG %s
 
 #include "Inputs/cuda.h"
 
-// NORDC: define internal void @_Z4funcIiEvv()
-// NORDC: define{{.*}} void @_Z6kernelIiEvv()
-// RDC: define weak_odr void @_Z4funcIiEvv()
-// RDC: define weak_odr void @_Z6kernelIiEvv()
-
 template  __device__ void func() {}
 template  __global__ void kernel() {}
 
 template __device__ void func();
+// NORDC: define internal void @_Z4funcIiEvv()
+// RDC:   define weak_odr void @_Z4funcIiEvv()
+
 template __global__ void kernel();
+// NORDC: define void @_Z6kernelIiEvv()
+// RDC:   define weak_odr void @_Z6kernelIiEvv()
+
+// Ensure that unused static device function is eliminated
+static __device__ void static_func() {}
+// NORDC-NEG-NOT: define{{.*}} void @_ZL13static_funcv()
+// RDC-NEG-NOT:   define{{.*}} void @_ZL13static_funcv()
+
+// Ensure that kernel function has external or weak_odr
+// linkage regardless static specifier
+static __global__ void static_kernel() {}
+// NORDC: define void @_ZL13static_kernelv()
+// RDC:   define weak_odr void @_ZL13static_kernelv()

diff  --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp 
b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 2516dff52efdf..e8322a0a8425b 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -329,7 +329,7 @@ MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol 
*Symbol) {
 void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
   const DataLayout &DL = getDataLayout();
   const NVPTXSubtarget &STI = TM.getSubtarget(*F);
-  const TargetLowering *TLI = STI.getTargetLowering();
+  const auto *TLI = cast(STI.getTargetLowering());
 
   Type *Ty = F->getReturnType();
 
@@

[clang] [PAC][clang] Define ptrauth driver flags and preprocessor features (PR #85232)

2024-03-14 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/85232

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuthIntrinsics` in `LangOptions`, 
`ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuthCalls` in `LangOptions`, `ptrauth_calls` and 
`ptrauth_member_function_pointer_type_discrimination` preprocessor features;

- `-fptrauth-returns`: `PointerAuthReturns` in `LangOptions`, `ptrauth_returns` 
preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuthAuthTraps` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`: 
`PointerAuthVTPtrAddressDiscrimination` in `LangOptions`, 
`ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`: 
`PointerAuthVTPtrTypeDiscrimination` in `LangOptions`, 
`ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuthInitFini` in `LangOptions`, 
`ptrauth_init_fini` preprocessor feature;

The patch only defines the flags and having corresponding `LangOptions` set 
does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 

>From d0138c7f86e4bc1b883822143a6841646f946ba2 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 14 Mar 2024 12:19:26 +0300
Subject: [PATCH] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuthIntrinsics` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuthCalls` in `LangOptions`, `ptrauth_calls` and
  `ptrauth_member_function_pointer_type_discrimination` preprocessor features;

- `-fptrauth-returns`: `PointerAuthReturns` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuthAuthTraps` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuthVTPtrAddressDiscrimination` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuthVTPtrTypeDiscrimination` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuthInitFini` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature;

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   7 ++
 clang/include/clang/Basic/LangOptions.def |   8 ++
 clang/include/clang/Driver/Options.td |  26 +
 clang/lib/Driver/ToolChains/Clang.cpp |  32 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  33 +++
 clang/test/Driver/ptrauth.c   |  32 ++
 clang/test/Preprocessor/ptrauth.c | 113 ++
 7 files changed, 251 insertions(+)
 create mode 100644 clang/test/Driver/ptrauth.c
 create mode 100644 clang/test/Preprocessor/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 5fad5fc3623cb6..1c6236aa4f9748 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -101,6 +101,13 @@ FEATURE(memory_sanitizer,
 FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
+FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 472fd9f093a718..4b99e70298462f 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -161,6 +161,14 @@ LANGOPT(DllExportInlines  , 1, 1, "dllexported classes 
dllexport inline methods"
 LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed matching of template 
template arguments")
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
+LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOP

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-14 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/85235

Depends on #85231 and #85232

Emit PAuth ABI compatibility tag values as llvm module flags:
- `aarch64-elf-pauthabi-platform`
- `aarch64-elf-pauthabi-version`

For platform 0x1002 (llvm_linux), the version value bits correspond to the 
following LangOptions defined in #85232:

- bit 0: `PointerAuthIntrinsics`;
- bit 1: `PointerAuthCalls`;
- bit 2: `PointerAuthReturns`;
- bit 3: `PointerAuthAuthTraps`;
- bit 4: `PointerAuthVTPtrAddressDiscrimination`;
- bit 5: `PointerAuthVTPtrTypeDiscrimination`;
- bit 6: `PointerAuthInitFini`.

>From da135fa4ce8fb560b043cb3d5931528539d4e1ea Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Wed, 13 Mar 2024 23:02:04 +0300
Subject: [PATCH] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility
 tag

Emit PAuth ABI compatibility tag values as llvm module flags:
- `aarch64-elf-pauthabi-platform`
- `aarch64-elf-pauthabi-version`
---
 clang/lib/CodeGen/CodeGenModule.cpp   | 20 
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 61 +++
 2 files changed, 81 insertions(+)
 create mode 100644 clang/test/CodeGen/aarch64-elf-pauthabi.c

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 967319bdfc4571..a98dee86876abd 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -53,6 +53,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/AttributeMask.h"
 #include "llvm/IR/CallingConv.h"
@@ -1161,6 +1162,25 @@ void CodeGenModule::Release() {
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
 "sign-return-address-with-bkey", 1);
+
+if (getTriple().isOSLinux() && getTriple().isOSBinFormatELF()) {
+  uint64_t PAuthABIVersion =
+  (LangOpts.PointerAuthIntrinsics << 0) |
+  (LangOpts.PointerAuthCalls << 1) |
+  (LangOpts.PointerAuthReturns << 2) |
+  (LangOpts.PointerAuthAuthTraps << 3) |
+  (LangOpts.PointerAuthVTPtrAddressDiscrimination << 4) |
+  (LangOpts.PointerAuthVTPtrTypeDiscrimination << 5) |
+  (LangOpts.PointerAuthInitFini << 6);
+  if (PAuthABIVersion != 0) {
+getModule().addModuleFlag(llvm::Module::Error,
+  "aarch64-elf-pauthabi-platform",
+  
llvm::ELF::AARCH64_PAUTH_PLATFORM_LLVM_LINUX);
+getModule().addModuleFlag(llvm::Module::Error,
+  "aarch64-elf-pauthabi-version",
+  PAuthABIVersion);
+  }
+}
   }
 
   if (CodeGenOpts.StackClashProtector)
diff --git a/clang/test/CodeGen/aarch64-elf-pauthabi.c 
b/clang/test/CodeGen/aarch64-elf-pauthabi.c
new file mode 100644
index 00..8f3e2d9b274b5a
--- /dev/null
+++ b/clang/test/CodeGen/aarch64-elf-pauthabi.c
@@ -0,0 +1,61 @@
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-intrinsics \
+// RUN:   -fptrauth-calls \
+// RUN:   -fptrauth-returns \
+// RUN:   -fptrauth-auth-traps \
+// RUN:   -fptrauth-vtable-pointer-address-discrimination \
+// RUN:   -fptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fptrauth-init-fini %s | \
+// RUN:   FileCheck %s --check-prefix=ALL
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-intrinsics %s | FileCheck %s --check-prefix=INTRIN
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-calls %s | FileCheck %s --check-prefix=CALL
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-returns %s | FileCheck %s --check-prefix=RET
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-auth-traps %s | FileCheck %s --check-prefix=TRAP
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-calls -fptrauth-vtable-pointer-address-discrimination %s | 
\
+// RUN:   FileCheck %s --check-prefix=VPTRADDR
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-calls -fptrauth-vtable-pointer-type-discrimination %s | \
+// RUN:   FileCheck %s --check-prefix=VPTRTYPE
+
+// RUN: %clang -target aarch64-linux -S -emit-llvm -o - \
+// RUN:   -fptrauth-calls -fptrauth-init-fini %s | \
+// RUN:   FileCheck %s --check-prefix=INITFINI
+
+// REQUIRES: aarch64-registered-target
+
+// ALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
+// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 127}
+
+// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
+// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1}
+
+// CALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
+// CALL: !{i32 1, !"aarch64-elf-pauthabi-versi

[clang] [PAC][clang] Define ptrauth driver flags and preprocessor features (PR #85232)

2024-03-14 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85232

>From 1394471c06458bd9de1935d3d546348ba392b452 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 14 Mar 2024 12:19:26 +0300
Subject: [PATCH] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuthIntrinsics` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuthCalls` in `LangOptions`, `ptrauth_calls` and
  `ptrauth_member_function_pointer_type_discrimination` preprocessor features;

- `-fptrauth-returns`: `PointerAuthReturns` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuthAuthTraps` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuthVTPtrAddressDiscrimination` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuthVTPtrTypeDiscrimination` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuthInitFini` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature;

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   7 ++
 clang/include/clang/Basic/LangOptions.def |   8 ++
 clang/include/clang/Driver/Options.td |  26 +
 clang/lib/Driver/ToolChains/Clang.cpp |  31 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  33 +++
 clang/test/Driver/ptrauth.c   |  32 ++
 clang/test/Preprocessor/ptrauth.c | 113 ++
 7 files changed, 250 insertions(+)
 create mode 100644 clang/test/Driver/ptrauth.c
 create mode 100644 clang/test/Preprocessor/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 5fad5fc3623cb6..1c6236aa4f9748 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -101,6 +101,13 @@ FEATURE(memory_sanitizer,
 FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
+FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 472fd9f093a718..4b99e70298462f 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -161,6 +161,14 @@ LANGOPT(DllExportInlines  , 1, 1, "dllexported classes 
dllexport inline methods"
 LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed matching of template 
template arguments")
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
+LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
 COMPATIBLE_LANGOPT(RecoveryAST, 1, 1, "Preserve expressions in AST when 
encountering errors")
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index aca8c9b0d5487a..868b164d8f7174 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4085,6 +4085,32 @@ defm strict_return : BoolFOption<"strict-return",
 " of a non-void function as unreachable">,
   PosFlag>;
 
+let Group = f_Group in {
+  let Visibility = [ClangOption,CC1Option] in {
+def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
+  HelpText<"Enable pointer-authentication int

[clang] [llvm] [CodeGen][AArch64][FMV] PAC the stub_helper's frame on arm64e (PR #84704)

2024-03-14 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

> Can the triple checks be generalized to checking about pauth-enabled 
> subtarget feature? This way everything would work for ELF platforms 
> eventually.

@asl Since I'm not sure how particularly ifuncs should be handled, here are two 
answers for two different cases.

If we need just to check that pauth target feature is enabled, we can use 
`TM.getTargetFeatureString()` and see if it contains `+pauth`.

If the ifunc-related behavior is dependent on `-fptrauth-calls` (implicitly 
enabled on apple arm64e) and should not be enabled when, say, only 
`-fptrauth-returns` is used (in both cases the target feature string contains 
`+pauth`), on ELF we can add a check against (platform,version) tuple defining 
the ABI after #85236 is merged.

https://github.com/llvm/llvm-project/pull/84704
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-15 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-15 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 8e65b4402201f74d5312ed257c4abde4a6615964 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 14 Mar 2024 12:19:26 +0300
Subject: [PATCH 1/2] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   7 ++
 clang/include/clang/Basic/LangOptions.h   |  18 
 clang/include/clang/Driver/Options.td |  26 +
 clang/lib/Driver/ToolChains/Clang.cpp |  32 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  33 +++
 clang/test/Driver/ptrauth.c   |  32 ++
 clang/test/Preprocessor/ptrauth.c | 113 ++
 7 files changed, 261 insertions(+)
 create mode 100644 clang/test/Driver/ptrauth.c
 create mode 100644 clang/test/Preprocessor/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 5fad5fc3623cb6..182a44a1079ef2 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -101,6 +101,13 @@ FEATURE(memory_sanitizer,
 FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
+FEATURE(ptrauth_intrinsics, LangOpts.PointerAuth.intrinsics())
+FEATURE(ptrauth_calls, LangOpts.PointerAuth.calls())
+FEATURE(ptrauth_returns, LangOpts.PointerAuth.returns())
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuth.vtptrAddressDiscrimination())
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuth.vtptrTypeDiscrimination())
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuth.calls())
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuth.initFini())
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 862952d336ef31..e9ade66a8df2ac 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -532,6 +532,24 @@ class LangOptions : public LangOptionsBase {
   // implementation on real-world examples.
   std::string OpenACCMacroOverride;
 
+  struct {
+bool Flags[7] = {false};
+bool intrinsics() const { return Flags[0]; }
+bool &intrinsics() { return Flags[0]; }
+bool calls() const { return Flags[1]; }
+bool &calls() { return Flags[1]; }
+bool returns() const { return Flags[2]; }
+bool &returns() { return Flags[2]; }
+bool authTraps() const { return Flags[3]; }
+bool &authTraps() { return Flags[3]; }
+bool vtptrAddressDiscrimination() const { return Flags[4]; }
+bool &vtptrAddressDiscrimination() { return Flags[4]; }
+bool vtptrTypeDiscrimination() const { return Flags[5]; }
+bool &vtptrTypeDiscrimination() { return Flags[5]; }
+bool initFini() const { return Flags[6]; }
+bool &initFini() { return Flags[6]; }
+  } PointerAuth;
+
   LangOptions();
 
   /// Set language defaults for the given input language and
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index aca8c9b0d5487a..868b164d8f7174 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4085,6 +4085,32 @@ defm strict_return : BoolFOption<"strict-return",
 " of a non-void function as unreachable">,
   PosFlag>;
 
+let Group = f_Group in {
+  let Visibility = [ClangOption,CC1Option] in {
+def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
+  HelpText<"Enable pointer-authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"

[clang] [PAC][clang] Define ptrauth driver flags and preprocessor features (PR #85232)

2024-03-15 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 closed 
https://github.com/llvm/llvm-project/pull/85232
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-18 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From eef1d790d1ec8cb9e0dda2b534c4ef19002ade35 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 14 Mar 2024 12:19:26 +0300
Subject: [PATCH 1/2] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   7 ++
 clang/include/clang/Basic/LangOptions.h   |  18 
 clang/include/clang/Driver/Options.td |  26 +
 clang/lib/Driver/ToolChains/Clang.cpp |  31 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  33 +++
 clang/test/Driver/ptrauth.c   |  32 ++
 clang/test/Preprocessor/ptrauth.c | 113 ++
 7 files changed, 260 insertions(+)
 create mode 100644 clang/test/Driver/ptrauth.c
 create mode 100644 clang/test/Preprocessor/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 5fad5fc3623cb6..182a44a1079ef2 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -101,6 +101,13 @@ FEATURE(memory_sanitizer,
 FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
+FEATURE(ptrauth_intrinsics, LangOpts.PointerAuth.intrinsics())
+FEATURE(ptrauth_calls, LangOpts.PointerAuth.calls())
+FEATURE(ptrauth_returns, LangOpts.PointerAuth.returns())
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuth.vtptrAddressDiscrimination())
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuth.vtptrTypeDiscrimination())
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuth.calls())
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuth.initFini())
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 862952d336ef31..e9ade66a8df2ac 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -532,6 +532,24 @@ class LangOptions : public LangOptionsBase {
   // implementation on real-world examples.
   std::string OpenACCMacroOverride;
 
+  struct {
+bool Flags[7] = {false};
+bool intrinsics() const { return Flags[0]; }
+bool &intrinsics() { return Flags[0]; }
+bool calls() const { return Flags[1]; }
+bool &calls() { return Flags[1]; }
+bool returns() const { return Flags[2]; }
+bool &returns() { return Flags[2]; }
+bool authTraps() const { return Flags[3]; }
+bool &authTraps() { return Flags[3]; }
+bool vtptrAddressDiscrimination() const { return Flags[4]; }
+bool &vtptrAddressDiscrimination() { return Flags[4]; }
+bool vtptrTypeDiscrimination() const { return Flags[5]; }
+bool &vtptrTypeDiscrimination() { return Flags[5]; }
+bool initFini() const { return Flags[6]; }
+bool &initFini() { return Flags[6]; }
+  } PointerAuth;
+
   LangOptions();
 
   /// Set language defaults for the given input language and
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index aca8c9b0d5487a..868b164d8f7174 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4085,6 +4085,32 @@ defm strict_return : BoolFOption<"strict-return",
 " of a non-void function as unreachable">,
   PosFlag>;
 
+let Group = f_Group in {
+  let Visibility = [ClangOption,CC1Option] in {
+def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
+  HelpText<"Enable pointer-authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-19 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 919af72c09216838bfe586c3da503f5d74104a7d Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 19 Mar 2024 23:57:06 +0300
Subject: [PATCH 1/2] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   6 ++
 clang/include/clang/Basic/LangOptions.def |   6 ++
 clang/include/clang/Driver/Options.td |  18 
 clang/lib/Driver/ToolChains/Clang.cpp |  27 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  20 
 clang/test/Driver/ptrauth.c   |  32 +++
 clang/test/Preprocessor/ptrauth_feature.c | 107 +-
 7 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index eeed5f4751f2f4e..1c6236aa4f9748f 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -102,6 +102,12 @@ FEATURE(thread_sanitizer, 
LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
 FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 8ef6700ecdc78ec..4b99e70298462fe 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,12 @@ LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed 
matching of template t
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
 LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 29c226f4bd8da74..e624eed2a15316a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4110,8 +4110,26 @@ let Group = f_Group in {
   let Visibility = [ClangOption,CC1Option] in {
 def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
   HelpText<"Enable pointer authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"Enable signing and authentication of all indirect calls">;
+def fptrauth_returns : Flag<["-"], "fptrauth-returns">,
+  HelpText<"Enable signing and authentication of return addresses">;
+def fptrauth_auth_traps : Flag<["-"], "fptrauth-auth-traps">,
+   

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-19 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 919af72c09216838bfe586c3da503f5d74104a7d Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 19 Mar 2024 23:57:06 +0300
Subject: [PATCH 1/3] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   6 ++
 clang/include/clang/Basic/LangOptions.def |   6 ++
 clang/include/clang/Driver/Options.td |  18 
 clang/lib/Driver/ToolChains/Clang.cpp |  27 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  20 
 clang/test/Driver/ptrauth.c   |  32 +++
 clang/test/Preprocessor/ptrauth_feature.c | 107 +-
 7 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index eeed5f4751f2f4..1c6236aa4f9748 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -102,6 +102,12 @@ FEATURE(thread_sanitizer, 
LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
 FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 8ef6700ecdc78e..4b99e70298462f 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,12 @@ LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed 
matching of template t
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
 LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 29c226f4bd8da7..e624eed2a15316 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4110,8 +4110,26 @@ let Group = f_Group in {
   let Visibility = [ClangOption,CC1Option] in {
 def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
   HelpText<"Enable pointer authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"Enable signing and authentication of all indirect calls">;
+def fptrauth_returns : Flag<["-"], "fptrauth-returns">,
+  HelpText<"Enable signing and authentication of return addresses">;
+def fptrauth_auth_traps : Flag<["-"], "fptrauth-auth-traps">,
+  Hel

[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-20 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

A kind reminder regarding the PR - would be glad to see feedback from everyone 
interested.

https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-26 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 919af72c09216838bfe586c3da503f5d74104a7d Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 19 Mar 2024 23:57:06 +0300
Subject: [PATCH 1/7] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   6 ++
 clang/include/clang/Basic/LangOptions.def |   6 ++
 clang/include/clang/Driver/Options.td |  18 
 clang/lib/Driver/ToolChains/Clang.cpp |  27 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  20 
 clang/test/Driver/ptrauth.c   |  32 +++
 clang/test/Preprocessor/ptrauth_feature.c | 107 +-
 7 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index eeed5f4751f2f4..1c6236aa4f9748 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -102,6 +102,12 @@ FEATURE(thread_sanitizer, 
LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
 FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 8ef6700ecdc78e..4b99e70298462f 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,12 @@ LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed 
matching of template t
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
 LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 29c226f4bd8da7..e624eed2a15316 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4110,8 +4110,26 @@ let Group = f_Group in {
   let Visibility = [ClangOption,CC1Option] in {
 def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
   HelpText<"Enable pointer authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"Enable signing and authentication of all indirect calls">;
+def fptrauth_returns : Flag<["-"], "fptrauth-returns">,
+  HelpText<"Enable signing and authentication of return addresses">;
+def fptrauth_auth_traps : Flag<["-"], "fptrauth-auth-traps">,
+  Hel

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-26 Thread Daniil Kovalev via cfe-commits


@@ -7203,6 +7203,33 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
options::OPT_fno_ptrauth_intrinsics, false))
 CmdArgs.push_back("-fptrauth-intrinsics");
 
+  if (Args.hasFlag(options::OPT_fptrauth_calls, options::OPT_fno_ptrauth_calls,

kovdan01 wrote:

Thanks! Fixed in 68bf805e9edb108dd64037ef8ed7d588ece66513

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-26 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,32 @@
+// Check that we can manually enable specific ptrauth features.

kovdan01 wrote:

Thanks, see f3abb4897fb9cd56b00eb6344489bbc972c40c5b and 
4fd37cf125c13982ae0b6eb7254404fa3def6df5

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-26 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,32 @@
+// Check that we can manually enable specific ptrauth features.
+
+// RUN: %clang --target=aarch64 -c %s -### 2>&1 | FileCheck %s --check-prefix 
NONE
+// NONE: "-cc1"
+// NONE-NOT: "-fptrauth-intrinsics"

kovdan01 wrote:

Fixed, thanks, see 4fd37cf125c13982ae0b6eb7254404fa3def6df5

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag (PR #85235)

2024-03-26 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,61 @@
+// REQUIRES: aarch64-registered-target

kovdan01 wrote:

Ensured that it's not needed and deleted in 
2fd8f6606c6d287085582b82145b05a635fbf776, thanks

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [lldb] [PAC][lldb][Dwarf] Support `__ptrauth`-qualified types in user expressions (PR #84387)

2024-03-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/84387

>From 728f5644aebfafd2114e7e47a9b83ef057423997 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere 
Date: Tue, 20 Feb 2024 10:57:54 -0800
Subject: [PATCH 01/10] Upstream ptrauth changes to DWARFASTParserClang

---
 .../SymbolFile/DWARF/DWARFASTParserClang.cpp  | 57 +++
 1 file changed, 57 insertions(+)

diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 54d06b1115a229..67fe830e1aa70d 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -495,6 +495,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const 
SymbolContext &sc,
   case DW_TAG_const_type:
   case DW_TAG_restrict_type:
   case DW_TAG_volatile_type:
+  case DW_TAG_LLVM_ptrauth_type:
   case DW_TAG_atomic_type:
   case DW_TAG_unspecified_type: {
 type_sp = ParseTypeModifier(sc, die, attrs);
@@ -676,6 +677,62 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext 
&sc,
   case DW_TAG_volatile_type:
 encoding_data_type = Type::eEncodingIsVolatileUID;
 break;
+  case DW_TAG_LLVM_ptrauth_type: {
+DWARFDIE ptr_die = die.GetReferencedDIE(DW_AT_type);
+// FIXME: Fully resolving the type here may affect performance.
+Type *res_type = dwarf->ResolveType(ptr_die);
+if (!res_type)
+  break;
+attrs.type.Clear();
+encoding_data_type = Type::eEncodingIsUID;
+resolve_state = Type::ResolveState::Full;
+
+// Apply the ptrauth qualifier to the resolved type.
+auto *ptr_type =
+(clang::Type *)res_type->GetForwardCompilerType().GetOpaqueQualType();
+auto getAttr = [&](llvm::dwarf::Attribute Attr, unsigned defaultValue = 0) 
{
+  return die.GetAttributeValueAsUnsigned(Attr, defaultValue);
+};
+const unsigned key = getAttr(DW_AT_LLVM_ptrauth_key);
+const bool addr_disc = getAttr(DW_AT_LLVM_ptrauth_address_discriminated);
+const unsigned extra = getAttr(DW_AT_LLVM_ptrauth_extra_discriminator);
+const bool isapointer = getAttr(DW_AT_LLVM_ptrauth_isa_pointer);
+const bool authenticates_null_values =
+getAttr(DW_AT_LLVM_ptrauth_authenticates_null_values, 0);
+const bool is_restricted_integral = !ptr_type->isPointerType();
+const unsigned authentication_mode_int = getAttr(
+DW_AT_LLVM_ptrauth_authentication_mode,
+static_cast(clang::PointerAuthenticationMode::SignAndAuth));
+clang::PointerAuthenticationMode authentication_mode =
+clang::PointerAuthenticationMode::SignAndAuth;
+if (authentication_mode_int >=
+static_cast(clang::PointerAuthenticationMode::None) &&
+authentication_mode_int <=
+static_cast(
+clang::PointerAuthenticationMode::SignAndAuth)) {
+  authentication_mode = static_cast(
+  authentication_mode_int);
+} else {
+  dwarf->GetObjectFile()->GetModule()->ReportError(
+  "[{0:x16}]: invalid pointer authentication mode method {1:x4}",
+  die.GetOffset(), authentication_mode_int);
+}
+
+// FIXME: Use these variables when PointerAuthQualifier is more complete
+// upstream.
+(void)is_restricted_integral;
+
+clang::Qualifiers qualifiers;
+auto ptr_auth = clang::PointerAuthQualifier::Create(
+key, addr_disc, extra, authentication_mode, isapointer,
+authenticates_null_values);
+qualifiers.setPointerAuth(ptr_auth);
+auto &ctx = m_ast.getASTContext();
+auto qual_type = ctx.getQualifiedType(ptr_type, qualifiers);
+clang_type =
+CompilerType(m_ast.weak_from_this(), qual_type.getAsOpaquePtr());
+break;
+  }
   case DW_TAG_atomic_type:
 encoding_data_type = Type::eEncodingIsAtomicUID;
 break;

>From 8aa1ba0b05362b8960faac1945bb25c68ecb4b98 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 16:34:09 +0300
Subject: [PATCH 02/10] [PAC][lldb] Use `eEncodingIsLLVMPtrAuthUID` for
 `__ptrauth`-qualified types

---
 lldb/include/lldb/Symbol/Type.h   | 4 +++-
 .../Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp  | 2 +-
 lldb/source/Symbol/Type.cpp   | 8 +++-
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index acd1a769f13cd6..d55280b58bc4f7 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -401,7 +401,9 @@ class Type : public std::enable_shared_from_this, 
public UserID {
 /// This type is the type whose UID is m_encoding_uid as an atomic type.
 eEncodingIsAtomicUID,
 /// This type is the synthetic type whose UID is m_encoding_uid.
-eEncodingIsSyntheticUID
+eEncodingIsSyntheticUID,
+/// This type is a signed pointer.
+eEncodingIsLLVMPtrAuthUID
   };
 
   enum class ResolveState : 

[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-07 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/84384

This brings declarations of `PointerAuthQualifier` class and
`PointerAuthenticationMode` enum and related functions required for PAuth
support in lldb (see #TODO) from downstream Apple's code.

Co-authored-by: Ahmed Bougacha 
Co-authored-by: John McCall 



>From ef23d427b48687b62da9e1062886ddfcc1649b6a Mon Sep 17 00:00:00 2001
From: John McCall 
Date: Mon, 16 Dec 2019 20:31:25 -0500
Subject: [PATCH 1/4] Abstract serialization fixes for the Apple Clang changes.

---
 clang/include/clang/AST/AbstractBasicReader.h | 4 ++--
 clang/include/clang/AST/AbstractBasicWriter.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/AST/AbstractBasicReader.h 
b/clang/include/clang/AST/AbstractBasicReader.h
index 1f2797cc701458..ab036f1d445acc 100644
--- a/clang/include/clang/AST/AbstractBasicReader.h
+++ b/clang/include/clang/AST/AbstractBasicReader.h
@@ -213,9 +213,9 @@ class DataStreamBasicReader : public BasicReaderBase {
   }
 
   Qualifiers readQualifiers() {
-static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-uint32_t value = asImpl().readUInt32();
+uint64_t value = asImpl().readUInt64();
 return Qualifiers::fromOpaqueValue(value);
   }
 
diff --git a/clang/include/clang/AST/AbstractBasicWriter.h 
b/clang/include/clang/AST/AbstractBasicWriter.h
index 07afa388de2c17..8e42fcaad1d388 100644
--- a/clang/include/clang/AST/AbstractBasicWriter.h
+++ b/clang/include/clang/AST/AbstractBasicWriter.h
@@ -196,9 +196,9 @@ class DataStreamBasicWriter : public BasicWriterBase {
   }
 
   void writeQualifiers(Qualifiers value) {
-static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-asImpl().writeUInt32(value.getAsOpaqueValue());
+asImpl().writeUInt64(value.getAsOpaqueValue());
   }
 
   void writeExceptionSpecInfo(

>From 9e296a1a69158419960c265f12f52523db0c8e2a Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:34:24 +0300
Subject: [PATCH 2/4] [clang] Define `PointerAuthenticationMode` enum

---
 clang/include/clang/Basic/LangOptions.h | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 862952d336ef31..6fe7472d8ad0ca 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -57,6 +57,13 @@ enum class ShaderStage {
   Invalid,
 };
 
+enum class PointerAuthenticationMode : unsigned {
+  None,
+  Strip,
+  SignAndStrip,
+  SignAndAuth
+};
+
 /// Bitfields of LangOptions, split out from LangOptions in order to ensure 
that
 /// this large collection of bitfields is a trivial class type.
 class LangOptionsBase {

>From 80c27eef1352d0daa3f2eaea43499e54f093c6c1 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:31:03 +0300
Subject: [PATCH 3/4] [clang] Define `PointerAuthQualifier` class

Includes changes from the following commits from the branch
https://github.com/ahmedbougacha/llvm-project/tree/eng/arm64e-upstream-llvmorg

- Initial support  
https://github.com/ahmedbougacha/llvm-project/commit/cc7ba7eb1814e9b254c7d94aa0b78cb0e21acfc5
- ObjC isa signing 
https://github.com/ahmedbougacha/llvm-project/commit/c9ce0d408f1d9aeffc7b86256334220aec6de5a3

Also applies a fix from https://github.com/access-softek/llvm-project/pull/75

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/AST/Type.h| 216 +-
 .../include/clang/Basic/PointerAuthOptions.h  |  23 ++
 2 files changed, 228 insertions(+), 11 deletions(-)
 create mode 100644 clang/include/clang/Basic/PointerAuthOptions.h

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 1942b0e67f65a3..ca73f1c53e8e94 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -25,8 +25,10 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/ExceptionSpecificationType.h"
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/LangOptions.h"
 #include "clang/Basic/Linkage.h"
 #include "clang/Basic/PartialDiagnostic.h"
+#include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
 #include "clang/Basic/Visibility.h"
@@ -138,6 +140,166 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBi

[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-07 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

I've left 4 commits to distinguish code coming from different sources during
review. Here is the origin of the code for each of the commits:

- ef23d427b48687b62da9e1062886ddfcc1649b6a: 
https://github.com/apple/llvm-project/commit/a94321f13043a39b02fa7106b8270767651354cd
- 9e296a1a69158419960c265f12f52523db0c8e2a: 
https://github.com/ahmedbougacha/llvm-project/commit/017eede69b9b06ff432e6f817e046723cf3207f0
- 80c27eef1352d0daa3f2eaea43499e54f093c6c1:
  - 
https://github.com/ahmedbougacha/llvm-project/commit/cc7ba7eb1814e9b254c7d94aa0b78cb0e21acfc5
  - 
https://github.com/ahmedbougacha/llvm-project/commit/c9ce0d408f1d9aeffc7b86256334220aec6de5a3
  - https://github.com/access-softek/llvm-project/pull/75
- 0b75c0a774e1e8afb56de95485acf447f11ccd6d: 
https://github.com/ahmedbougacha/llvm-project/commit/cc7ba7eb1814e9b254c7d94aa0b78cb0e21acfc5

Commits from @ahmedbougacha's repo are taken from the 
eng/arm64e-upstream-llvmorg branch.



https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-07 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-07 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/84384

>From ef23d427b48687b62da9e1062886ddfcc1649b6a Mon Sep 17 00:00:00 2001
From: John McCall 
Date: Mon, 16 Dec 2019 20:31:25 -0500
Subject: [PATCH 1/4] Abstract serialization fixes for the Apple Clang changes.

---
 clang/include/clang/AST/AbstractBasicReader.h | 4 ++--
 clang/include/clang/AST/AbstractBasicWriter.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/AST/AbstractBasicReader.h 
b/clang/include/clang/AST/AbstractBasicReader.h
index 1f2797cc701458..ab036f1d445acc 100644
--- a/clang/include/clang/AST/AbstractBasicReader.h
+++ b/clang/include/clang/AST/AbstractBasicReader.h
@@ -213,9 +213,9 @@ class DataStreamBasicReader : public BasicReaderBase {
   }
 
   Qualifiers readQualifiers() {
-static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-uint32_t value = asImpl().readUInt32();
+uint64_t value = asImpl().readUInt64();
 return Qualifiers::fromOpaqueValue(value);
   }
 
diff --git a/clang/include/clang/AST/AbstractBasicWriter.h 
b/clang/include/clang/AST/AbstractBasicWriter.h
index 07afa388de2c17..8e42fcaad1d388 100644
--- a/clang/include/clang/AST/AbstractBasicWriter.h
+++ b/clang/include/clang/AST/AbstractBasicWriter.h
@@ -196,9 +196,9 @@ class DataStreamBasicWriter : public BasicWriterBase {
   }
 
   void writeQualifiers(Qualifiers value) {
-static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-asImpl().writeUInt32(value.getAsOpaqueValue());
+asImpl().writeUInt64(value.getAsOpaqueValue());
   }
 
   void writeExceptionSpecInfo(

>From 9e296a1a69158419960c265f12f52523db0c8e2a Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:34:24 +0300
Subject: [PATCH 2/4] [clang] Define `PointerAuthenticationMode` enum

---
 clang/include/clang/Basic/LangOptions.h | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 862952d336ef31..6fe7472d8ad0ca 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -57,6 +57,13 @@ enum class ShaderStage {
   Invalid,
 };
 
+enum class PointerAuthenticationMode : unsigned {
+  None,
+  Strip,
+  SignAndStrip,
+  SignAndAuth
+};
+
 /// Bitfields of LangOptions, split out from LangOptions in order to ensure 
that
 /// this large collection of bitfields is a trivial class type.
 class LangOptionsBase {

>From 71d7760aa9cc459d7bc0365fa079f64cb5704c96 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:31:03 +0300
Subject: [PATCH 3/4] [clang] Define `PointerAuthQualifier` class

Includes changes from the following commits from the branch
https://github.com/ahmedbougacha/llvm-project/tree/eng/arm64e-upstream-llvmorg

- Initial support  
https://github.com/ahmedbougacha/llvm-project/commit/cc7ba7eb1814e9b254c7d94aa0b78cb0e21acfc5
- ObjC isa signing 
https://github.com/ahmedbougacha/llvm-project/commit/c9ce0d408f1d9aeffc7b86256334220aec6de5a3

Also applies a fix from https://github.com/access-softek/llvm-project/pull/75

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/AST/Type.h| 215 +-
 .../include/clang/Basic/PointerAuthOptions.h  |  23 ++
 2 files changed, 227 insertions(+), 11 deletions(-)
 create mode 100644 clang/include/clang/Basic/PointerAuthOptions.h

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 1942b0e67f65a3..1741a3017f7280 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -25,8 +25,10 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/ExceptionSpecificationType.h"
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/LangOptions.h"
 #include "clang/Basic/Linkage.h"
 #include "clang/Basic/PartialDiagnostic.h"
+#include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
 #include "clang/Basic/Visibility.h"
@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits

[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-03-07 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-31 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/72714

>From c493d78e6c482bb530189de05b79e7082a224fab Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 28 Sep 2023 03:14:35 +0300
Subject: [PATCH 1/8] [lld][AArch64][ELF][PAC] Support AUTH relocations and
 AUTH ELF marking

This patch adds lld support for:

- Dynamic R_AARCH64_AUTH_* relocations (including RELR compressed AUTH
  relocations) as described here:
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations

- .note.AARCH64-PAUTH-ABI-tag section as defined here
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking

Co-authored-by: Peter Collingbourne 
---
 lld/ELF/Arch/AArch64.cpp |   5 +
 lld/ELF/Config.h |   4 +
 lld/ELF/Driver.cpp   |  57 +-
 lld/ELF/InputFiles.cpp   |  44 
 lld/ELF/InputFiles.h |   1 +
 lld/ELF/Relocations.cpp  |  26 +
 lld/ELF/SyntheticSections.cpp|  44 ++--
 lld/ELF/SyntheticSections.h  |  19 +++-
 lld/ELF/Writer.cpp   |  17 +++
 lld/test/ELF/aarch64-feature-pauth.s |  83 ++
 lld/test/ELF/aarch64-ptrauth.s   | 156 +++
 11 files changed, 445 insertions(+), 11 deletions(-)
 create mode 100644 lld/test/ELF/aarch64-feature-pauth.s
 create mode 100644 lld/test/ELF/aarch64-ptrauth.s

diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 048f0ec30ebd2..6828d3f57c10e 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -112,6 +112,7 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   case R_AARCH64_MOVW_UABS_G2:
   case R_AARCH64_MOVW_UABS_G2_NC:
   case R_AARCH64_MOVW_UABS_G3:
+  case R_AARCH64_AUTH_ABS64:
 return R_ABS;
   case R_AARCH64_TLSDESC_ADR_PAGE21:
 return R_AARCH64_TLSDESC_PAGE;
@@ -395,6 +396,10 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
   case R_AARCH64_PREL64:
 write64(loc, val);
 break;
+  case R_AARCH64_AUTH_ABS64:
+checkIntUInt(loc, val, 32, rel);
+write32(loc, val);
+break;
   case R_AARCH64_ADD_ABS_LO12_NC:
 or32AArch64Imm(loc, val);
 break;
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 56229334f9a44..1b633a7984276 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -187,6 +187,7 @@ struct Config {
   llvm::StringRef cmseOutputLib;
   StringRef zBtiReport = "none";
   StringRef zCetReport = "none";
+  StringRef zPauthReport = "none";
   llvm::StringRef ltoBasicBlockSections;
   std::pair thinLTOObjectSuffixReplace;
   llvm::StringRef thinLTOPrefixReplaceOld;
@@ -275,6 +276,7 @@ struct Config {
   bool relocatable;
   bool relrGlibc = false;
   bool relrPackDynRelocs = false;
+  bool relrPackAuthDynRelocs = false;
   llvm::DenseSet saveTempsArgs;
   llvm::SmallVector, 0> shuffleSections;
   bool singleRoRx;
@@ -492,6 +494,8 @@ struct Ctx {
   void reset();
 
   llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
+
+  SmallVector aarch64PauthAbiTag;
 };
 
 LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6bef09eeca015..4e8e9eb86ecf7 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -65,6 +65,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/raw_ostream.h"
+#include 
 #include 
 #include 
 #include 
@@ -459,6 +460,8 @@ static void checkOptions() {
   error("-z force-bti only supported on AArch64");
 if (config->zBtiReport != "none")
   error("-z bti-report only supported on AArch64");
+if (config->zPauthReport != "none")
+  error("-z pauth-report only supported on AArch64");
   }
 
   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
@@ -558,6 +561,7 @@ constexpr const char *knownZFlags[] = {
 "nognustack",
 "nokeep-text-section-prefix",
 "nopack-relative-relocs",
+"nopack-relative-auth-relocs",
 "norelro",
 "noseparate-code",
 "nostart-stop-gc",
@@ -566,6 +570,7 @@ constexpr const char *knownZFlags[] = {
 "origin",
 "pac-plt",
 "pack-relative-relocs",
+"pack-relative-auth-relocs",
 "rel",
 "rela",
 "relro",
@@ -583,7 +588,7 @@ constexpr const char *knownZFlags[] = {
 static bool isKnownZFlag(StringRef s) {
   return llvm::is_contained(knownZFlags, s) ||
  s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
- s.starts_with("cet-report=") ||
+ s.starts_with("cet-report=") || s.starts_with("pauth-report=") ||
  s.starts_with("dead-reloc-in-nonalloc=") ||
  s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
  s.starts_with("start-stop-visibility=");
@@ -1514,7 +1519,8 @@ static void readConfigs(opt::InputArgList &args) {
   }
 
   auto reports = {std::make_pair("bti-report

[clang-tools-extra] [llvm] [clang] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-31 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,83 @@
+# REQUIRES: aarch64
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu abi-tag1.s -o tag11.o
+# RUN: cp tag11.o tag12.o
+# RUN: ld.lld -shared tag11.o tag12.o -o tagok.so
+# RUN: llvm-readelf -n tagok.so | FileCheck --check-prefix OK %s
+
+# OK: AArch64 PAuth ABI tag: platform 0x2a, version 0x1
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu abi-tag2.s -o tag2.o
+# RUN: not ld.lld tag11.o tag12.o tag2.o -o /dev/null 2>&1 | FileCheck 
--check-prefix ERR1 %s
+
+# ERR1: error: incompatible values of AArch64 PAuth compatibility info found
+# ERR1: {{.*}}: 0x2A000{{1|2}}00
+# ERR1: {{.*}}: 0x2A000{{1|2}}00
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu abi-tag-errs.s -o errs.o
+# RUN: not ld.lld errs.o -o /dev/null 2>&1 | FileCheck --check-prefix ERR2 %s
+
+# ERR2:  error: {{.*}}: invalid type field value 42 (1 expected)
+# ERR2-NEXT: error: {{.*}}: invalid name field value XXX (ARM expected)
+# ERR2-NEXT: error: {{.*}}: AArch64 PAuth compatibility info is too short (at 
least 16 bytes expected)
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu abi-tag-short.s -o 
short.o
+# RUN: not ld.lld short.o -o /dev/null 2>&1 | FileCheck --check-prefix ERR3 %s
+
+# ERR3: error: {{.*}}: section is too short
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu no-info.s -o noinfo1.o
+# RUN: cp noinfo1.o noinfo2.o
+# RUN: not ld.lld -z pauth-report=error tag11.o noinfo1.o noinfo2.o -o 
/dev/null 2>&1 | FileCheck --check-prefix ERR4 %s
+# RUN: ld.lld -z pauth-report=warning tag11.o noinfo1.o noinfo2.o -o /dev/null 
2>&1 | FileCheck --check-prefix WARN %s
+# RUN: ld.lld -z pauth-report=none tag11.o noinfo1.o noinfo2.o -o /dev/null 
2>&1 | FileCheck --check-prefix NONE %s
+
+# ERR4:  error: {{.*}}noinfo1.o has no AArch64 PAuth compatibility info 
while {{.*}}tag11.o has one; either all or no input files must have it

kovdan01 wrote:

Changed `{{.*}}` to actual error locations, thanks. For `NONE-NOT` check 
directive, I've left `{{.*}}` so it checks that there are no error messages 
with a given template regardless file names.

https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang-tools-extra] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-31 Thread Daniil Kovalev via cfe-commits


@@ -543,7 +553,8 @@ class RelocationBaseSection : public SyntheticSection {
   static bool classof(const SectionBase *d) {
 return SyntheticSection::classof(d) &&
(d->type == llvm::ELF::SHT_RELA || d->type == llvm::ELF::SHT_REL ||
-d->type == llvm::ELF::SHT_RELR);
+d->type == llvm::ELF::SHT_RELR ||
+d->type == llvm::ELF::SHT_AARCH64_AUTH_RELR);

kovdan01 wrote:

Added, thanks

https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-31 Thread Daniil Kovalev via cfe-commits


@@ -1461,6 +1493,33 @@ template  void 
RelocationScanner::scanOne(RelTy *&i) {
 }
   }
 
+  // if (config->emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {

kovdan01 wrote:

Ah, thanks, removed that. The logic was moved to `RelocationScanner::processAux`

https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-26 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/72714

>From c493d78e6c482bb530189de05b79e7082a224fab Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 28 Sep 2023 03:14:35 +0300
Subject: [PATCH 1/6] [lld][AArch64][ELF][PAC] Support AUTH relocations and
 AUTH ELF marking

This patch adds lld support for:

- Dynamic R_AARCH64_AUTH_* relocations (including RELR compressed AUTH
  relocations) as described here:
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations

- .note.AARCH64-PAUTH-ABI-tag section as defined here
  
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking

Co-authored-by: Peter Collingbourne 
---
 lld/ELF/Arch/AArch64.cpp |   5 +
 lld/ELF/Config.h |   4 +
 lld/ELF/Driver.cpp   |  57 +-
 lld/ELF/InputFiles.cpp   |  44 
 lld/ELF/InputFiles.h |   1 +
 lld/ELF/Relocations.cpp  |  26 +
 lld/ELF/SyntheticSections.cpp|  44 ++--
 lld/ELF/SyntheticSections.h  |  19 +++-
 lld/ELF/Writer.cpp   |  17 +++
 lld/test/ELF/aarch64-feature-pauth.s |  83 ++
 lld/test/ELF/aarch64-ptrauth.s   | 156 +++
 11 files changed, 445 insertions(+), 11 deletions(-)
 create mode 100644 lld/test/ELF/aarch64-feature-pauth.s
 create mode 100644 lld/test/ELF/aarch64-ptrauth.s

diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 048f0ec30ebd283..6828d3f57c10e84 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -112,6 +112,7 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   case R_AARCH64_MOVW_UABS_G2:
   case R_AARCH64_MOVW_UABS_G2_NC:
   case R_AARCH64_MOVW_UABS_G3:
+  case R_AARCH64_AUTH_ABS64:
 return R_ABS;
   case R_AARCH64_TLSDESC_ADR_PAGE21:
 return R_AARCH64_TLSDESC_PAGE;
@@ -395,6 +396,10 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
   case R_AARCH64_PREL64:
 write64(loc, val);
 break;
+  case R_AARCH64_AUTH_ABS64:
+checkIntUInt(loc, val, 32, rel);
+write32(loc, val);
+break;
   case R_AARCH64_ADD_ABS_LO12_NC:
 or32AArch64Imm(loc, val);
 break;
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 56229334f9a44ae..1b633a79842769d 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -187,6 +187,7 @@ struct Config {
   llvm::StringRef cmseOutputLib;
   StringRef zBtiReport = "none";
   StringRef zCetReport = "none";
+  StringRef zPauthReport = "none";
   llvm::StringRef ltoBasicBlockSections;
   std::pair thinLTOObjectSuffixReplace;
   llvm::StringRef thinLTOPrefixReplaceOld;
@@ -275,6 +276,7 @@ struct Config {
   bool relocatable;
   bool relrGlibc = false;
   bool relrPackDynRelocs = false;
+  bool relrPackAuthDynRelocs = false;
   llvm::DenseSet saveTempsArgs;
   llvm::SmallVector, 0> shuffleSections;
   bool singleRoRx;
@@ -492,6 +494,8 @@ struct Ctx {
   void reset();
 
   llvm::raw_fd_ostream openAuxiliaryFile(llvm::StringRef, std::error_code &);
+
+  SmallVector aarch64PauthAbiTag;
 };
 
 LLVM_LIBRARY_VISIBILITY extern Ctx ctx;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6bef09eeca015aa..4e8e9eb86ecf77f 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -65,6 +65,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Support/raw_ostream.h"
+#include 
 #include 
 #include 
 #include 
@@ -459,6 +460,8 @@ static void checkOptions() {
   error("-z force-bti only supported on AArch64");
 if (config->zBtiReport != "none")
   error("-z bti-report only supported on AArch64");
+if (config->zPauthReport != "none")
+  error("-z pauth-report only supported on AArch64");
   }
 
   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
@@ -558,6 +561,7 @@ constexpr const char *knownZFlags[] = {
 "nognustack",
 "nokeep-text-section-prefix",
 "nopack-relative-relocs",
+"nopack-relative-auth-relocs",
 "norelro",
 "noseparate-code",
 "nostart-stop-gc",
@@ -566,6 +570,7 @@ constexpr const char *knownZFlags[] = {
 "origin",
 "pac-plt",
 "pack-relative-relocs",
+"pack-relative-auth-relocs",
 "rel",
 "rela",
 "relro",
@@ -583,7 +588,7 @@ constexpr const char *knownZFlags[] = {
 static bool isKnownZFlag(StringRef s) {
   return llvm::is_contained(knownZFlags, s) ||
  s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
- s.starts_with("cet-report=") ||
+ s.starts_with("cet-report=") || s.starts_with("pauth-report=") ||
  s.starts_with("dead-reloc-in-nonalloc=") ||
  s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
  s.starts_with("start-stop-visibility=");
@@ -1514,7 +1519,8 @@ static void readConfigs(opt::InputArgList &args) {
   }
 
   auto reports = {std::make_pair

[clang] [clang-tools-extra] [llvm] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-26 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Would be glad to see your comments on changes addressing your comments

https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [lld] [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (PR #72714)

2024-01-19 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay A kind reminder regarding the PR

https://github.com/llvm/llvm-project/pull/72714
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-06-30 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/97237

Enable the following ptrauth flags when `pauthabi` is passed as branch 
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816e..4ed1ece22b7aa 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
 ArgStringList &CmdArgs, bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9..dc63545a47a86 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-poin

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-06-30 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 milestoned 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Would be glad to see feedback on the changes from those who are interested.

https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Would be glad to see feedback on the changes from those who are interested.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Would be glad to see your feedback on the changes

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@ahmedbougacha 

> I should mention that in our world we generally don't expect these to be 
> common (other than in cc1 invocations), and they're generally used for 
> overriding default ABI behavior inferred from triples and deployment targets 
> and whatnot.

We also don't expect usage of these options to be common - instead, we propose 
`-mbranch-protection=pauthabi` option which enables a pre-defined set of 
ptrauth flags, see #97237.

I think that the main rationale for using comma-separated flags instead of a 
bunch of different flags is that it'll reduce unneeded `-fptrauth-` duplication 
- such duplication is probably undesirable even in cc1 invocations.

> and it should be doable to support the long spellings on top of these.

Regarding that: I'm not sure if it's a good idea to support both `-fptrauth-xxx 
-fptrauth-yyy` and `-fptrauth=xxx,yyy`. It'll result in additional logic for 
handling conflicts if the same flags are defined in different ways. It's 
probably not too complex, but it'll make things more messy, and I don't think 
it's what we are trying to achive.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-01 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay @ahmedbougacha @asl I suggest to move the discussion on 
comma-separated flags/current flags to an issue #97320. This PR is intended to 
introduce the signed GOT flag, and we already have a bunch of similar ptrauth 
flags, so, if we want to change the flags to a single comma-separated value, we 
need to do that as a separate refactoring PR but not here. As for this PR, I 
suggest to stick with old convention not to make this a blocker.

@MaskRay please let me know if you have other issues with proposed changes 
except suggestion to use comma-separated flags.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/2] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816e..4ed1ece22b7aa 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
 ArgStringList &CmdArgs, bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9..dc63545a47a86 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"
+//

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits


@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,

kovdan01 wrote:

I've added the comment, thanks! See fcd090caac9ede6b915db991819298bed4a5d44e

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits


@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,

kovdan01 wrote:

> See addOptInFlag.

Do you mean that we need to replace

```
  if (!DriverArgs.hasArg(options::OPT_fptrauth_xxx,
 options::OPT_fno_ptrauth_xxx))
CC1Args.push_back("-fptrauth-xxx");
```

with

```
  DriverArgs.addOptInFlag(CC1Args, options::OPT_fptrauth_xxx,
  options::OPT_fno_ptrauth_xxx);
```

...?

If so, this does not look correct - `addOptInFlag` would add the flag present 
(if any) in `DriverArgs` to `CC1Args`, but we want to append a list of ptrauth 
flags to cc1 args unconditionally if `pauthabi` is passed as branch protection.

Do I miss smth? I suppose I might have misunderstood you point.

> But the implementation seems quite different from the title/description.

Hmm, it actually looks consistent to me, the implementation seems matching the 
description from my point of view - we want to add a bunch of arguments with 
`-mbranch-protection=pauthabi` used as a shortcut, we do that. Could you please 
describe what is inconsistent between description and implementation in a bit 
more detail?

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits


@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)

kovdan01 wrote:

I suggest not to support `pauthabi` in combination with other branch protection 
options as for now. Here are the reasons why.

1. `pac-ret`: this and `-fptrauth-returns` (which is enabled by 
`-mbranch-protection=pauthabi`) are intended to do similar stuff, but the 
implementation differs.

   For `-mbranch-protection=pac-ret`:
   - `sign-return-address` llvm module flag is set (and optionally 
`sign-return-address-all` with `+leaf`);
   - `PAUTH_PROLOGUE` and `PAUTH_EPILOGUE` pseudo-instructions are emitted in 
`AArch64FrameLowering::emitPrologue` and `AArch64FrameLowering::emitEpilogue`; 
these pseudos are later expanded by `AArch64PointerAuth` pass;
   - both A and B keys can be used (depending on `+b-key` and corresponding 
function attribute `sign-return-address-key` or module flag 
`sign-return-address-with-bkey`);
   - using `pc` register as additional modifier is supported with `+pc` 
(corresponding module flag is `branch-protection-pauth-lr`).

   For `-fptrauth-returns` (I'll talk about downstream Apple implementation 
since many parts are not upstreamed yet, see, for example, 
https://github.com/apple/llvm-project/commit/13f9944a4c8993f9af32dc634e5d7a08cf0394e7):
   - `ptrauth-returns` attribute is set on functions we want this to be enabled;
   - actual codegen logic is implemented in 
`AArch64FrameLowering::emitPrologue` and `AArch64FrameLowering::emitEpilogue` - 
we emit actual instructions like `pacibsp` directly there;
   - B key is always used;
   - using `pc` register as additional modifier is **not** supported.

   If we try to enable both by `-mbranch-protection=pauthabi+pac-ret`, it'll 
result in incorrect code with duplicating sign/auth instructions. For example, 
for this:

   ```
   int a() {
 return b() + c();
   }
   ```

   We get this:

   ```
   a:
 paciasp
 pacibsp
 stp x29, x30, [sp, #-32]!   // 16-byte Folded Spill
 str x19, [sp, #16]  // 8-byte Folded Spill
 mov x29, sp
 bl  b
 mov w19, w0
 bl  c
 add w0, w0, w19
 ldr x19, [sp, #16]  // 8-byte Folded Reload
 ldp x29, x30, [sp], #32 // 16-byte Folded Reload
 autiasp
 retab
   ```

   A corresponding issue was already previously opened (mistakenly in mainline 
llvm repo while it was and actually still is an issue specific for Apple 
downstream). Links:
   - the issue in mainline repo 
https://github.com/llvm/llvm-project/issues/60239;
   - thread on Apple forum regarding the issue 
https://forums.developer.apple.com/forums/thread/724568;
   - the issue on Apple feedback portal (I was unable to open that actually but 
the link should be correct) 
https://feedbackassistant.apple.com/feedback/1196543.
 
   I'll probably re-open the issue in mainline repo when codegen support for 
`ptrauth-returns` is upstreamed. Alternatively, the Apple's downstream 
implementation for return address signing might be dropped since `pac-ret` 
seems to be more complete, and we can use `-fptrauth-returns` for setting the 
same return address signing options as `pac-ret+b-key`. Tagging @ahmedbougacha.

2. `+leaf` and `+pc`: these are only allowed with `pac-ret`, and while it's not 
clear how we'll resolve collisions between `pac-ret` and `ptrauth-returns` 
(which is part of `pauthabi`), it's probably better to just disallow 
`pauthabi+leaf` and `pauthabi+pc`.

3. `+b-key`: `ptrauth-returns` (which is part of `pauthabi`) already uses B key 
by default (but the codegen support is still present only in Apple downstream, 
see 
https://github.com/apple/llvm-project/commit/13f9944a4c8993f9af32dc634e5d7a08cf0394e7)

4. `gcs`: as far as I understand, guarded control stack is smth like what is 
usually called shadow stack. I'm not sure how it's supposed to work with return 
address signing - probably, these shouldn't be used together, so, since 
`pauthabi` implies return address signing, disallow `pauthabi+gcs`.

5. `bti`: depending on operand value (`c`, `j` or `jc`), the `bti` instruction 
inserted at beginning of valid call/jump targets checks that `PSTATE.BTYPE` 
matches the value set by `blr` and/or `br` instructions (see 
https://developer.arm.com/documentation/100076/0100/A64-Instruction-Set-Reference/A64-General-Instructions/B

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/3] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
 ArgStringList &CmdArgs, bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits


@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"

kovdan01 wrote:

Changed, thanks, see 1d81b91d88f4f93dc6cf9bbec0c5f7fb851f89ab

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/4] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
 ArgStringList &CmdArgs, bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits


@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)

kovdan01 wrote:

@DanielKristofKiss Thanks! Yes, I now see that authenticating branch 
instructions also set the `PSTATE.BTYPE`. So, I agree that we can allow 
pauthabi+bti. The codegen when `pauthabi+bti` is passed as branch protection 
looks OK on downstream:
- for functions which do not sign LR (w/o `pacibsp` in the beginning, e.g. leaf 
functions), a proper `bti` instruction is emitted in the beginning of the 
function;
- for function which sign LR (with `pacibsp` in the beginning), no additional 
`bti` instruction is emitted since `pacibsp` also has `bti` built-in, see 
`SetBTypeCompatible` in 
https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/PACIB--PACIB1716--PACIBSP--PACIBZ--PACIZB--Pointer-Authentication-Code-for-Instruction-address--using-key-B-

In mainline, codegen with `-fptrauth-returns` is not implemented yet, so I 
can't provide tests verifying generated assembly correctness right now. I'll 
add them as soon as codegen support for this flag is merged.

I've implemented a check which only allows `bti` with `pauthabi` and does not 
allow `pac-ret[+leaf,+pc,+b-key]` and `gcs`. See 
3067c934a957eed21cb3ae73404675242425e5cb

Tagging @smithp35



https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/5] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList &CmdArgs) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList &DriverArgs,
+ ArgStringList &CC1Args, const Driver &D) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
 ArgStringList &CmdArgs, bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
&TC, const ArgList &Args,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"

[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Ping: would be glad to see feedback on the changes from those who are 
interested.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Ping: would be glad to see feedback on the changes from those who are 
interested.

https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Thanks @tmatheson-arm! I'll merge this as soon as #96478 gets merged - there, 
we introduce `AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINIADDRDISC = 7`, 
so signed GOT should be bit 8. We can't use bit 8 for signed GOT right now 
since the implementation relies on contiguous set of flags - so, I'll wait for 
#96478 and change signed GOT bit position to 8.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -10685,6 +10689,26 @@ SDValue AArch64TargetLowering::LowerBR_JT(SDValue Op,
   return DAG.getNode(ISD::BRIND, DL, MVT::Other, JTInfo, SDValue(Dest, 0));
 }
 
+SDValue AArch64TargetLowering::LowerBRIND(SDValue Op, SelectionDAG &DAG) const 
{
+  MachineFunction &MF = DAG.getMachineFunction();

kovdan01 wrote:

Nit
```suggestion
  const MachineFunction &MF = DAG.getMachineFunction();
```

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 commented:

The changes mostly look reasonable, but there are several comments to be 
answered before the PR can be merged.

Regarding previous @efriedma-quic 's comment about computing the difference 
between two blockaddresses - I suppose it might be OK just to explicitly do not 
support this as for now and implement+test a proper error message (if it's too 
time-consuming to have full support right now).

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -412,6 +412,15 @@ class AArch64Subtarget final : public 
AArch64GenSubtargetInfo {
   /// Choose a method of checking LR before performing a tail call.
   AArch64PAuth::AuthCheckMethod getAuthenticatedLRCheckMethod() const;
 
+  /// Compute the integer discriminator for a given BlockAddress constant, if
+  /// blockaddress signing is enabled (using function attribute
+  /// "ptrauth-indirect-gotos").

kovdan01 wrote:

Nit: it might be worth explicitly saying that `std::nullopt` return value 
stands for disabled indirect gotos signing: someone might mistakenly think that 
`std::nullopt` might indicate absent discriminator (say, zero discr by default) 
with signing enabled.

Alternatively, you might consider adding a separate function for determining 
presence of "ptrauth-indirect-gotos" attribute and use just `uint16_t` as a 
return value for `getPtrAuthBlockAddressDiscriminator` (with an assertion 
inserted against presence of "ptrauth-indirect-gotos"). A drawback of such 
approach is that we might occasionally forget to check if indirect gotos are 
signed before calling get discr function, but a benefit is that function names 
will be more expressive IMHO - `getPtrAuthBlockAddressDiscriminator` will only 
get the discriminator and it'll not have a responsibility to check if the 
signing is enabled.

Feel free to ignore.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -10704,15 +10728,36 @@ SDValue 
AArch64TargetLowering::LowerConstantPool(SDValue Op,
 
 SDValue AArch64TargetLowering::LowerBlockAddress(SDValue Op,
SelectionDAG &DAG) const {
-  BlockAddressSDNode *BA = cast(Op);
+  BlockAddressSDNode *BAN = cast(Op);
+  const BlockAddress *BA = BAN->getBlockAddress();
+
+  if (std::optional BADisc =
+  Subtarget->getPtrAuthBlockAddressDiscriminator(*BA->getFunction())) {
+SDLoc DL(Op);

kovdan01 wrote:

Is there a test which covers this code path?

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -1866,6 +1876,20 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const 
MachineInstr &MI) {
   assert(STI->getInstrInfo()->getInstSizeInBytes(MI) >= InstsEmitted * 4);
 }
 
+const MCExpr *
+AArch64AsmPrinter::lowerBlockAddressConstant(const BlockAddress &BA) {
+  const MCExpr *BAE = AsmPrinter::lowerBlockAddressConstant(BA);
+  const Function &Fn = *BA.getFunction();
+
+  if (std::optional BADisc =
+  STI->getPtrAuthBlockAddressDiscriminator(Fn))
+return AArch64AuthMCExpr::create(BAE, *BADisc, AArch64PACKey::IA,
+ /* HasAddressDiversity= */ false,

kovdan01 wrote:

Nit: it looks like that across llvm it's more common not to leave spaces inside 
comment in such case 
https://llvm.org/docs/CodingStandards.html#comment-formatting

```suggestion
 /*HasAddressDiversity=*/ false,
```

Feel free to ignore

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -2516,6 +2516,10 @@ bool AArch64FastISel::selectIndirectBr(const Instruction 
*I) {
   if (AddrReg == 0)
 return false;
 
+  // Authenticated indirectbr is not implemented yet.

kovdan01 wrote:

It looks like that a test with FastISel ensuring that we fall back to 
SelectionDAG ISel is missing. I'm OK with adding missing tests later as a 
separate patch.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -3461,6 +3470,23 @@ bool AArch64InstructionSelector::select(MachineInstr &I) 
{
 return true;
   }
   case TargetOpcode::G_BLOCK_ADDR: {
+Function *BAFn = I.getOperand(1).getBlockAddress()->getFunction();
+if (std::optional BADisc =
+STI.getPtrAuthBlockAddressDiscriminator(*BAFn)) {
+  MIB.buildInstr(TargetOpcode::IMPLICIT_DEF, {AArch64::X16}, {});

kovdan01 wrote:

Is there a test which covers this code path?

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,106 @@
+; RUN: llc -mtriple arm64e-apple-darwin \
+; RUN:   -asm-verbose=false -aarch64-enable-collect-loh=false \
+; RUN:   -o - %s | FileCheck %s
+
+; RUN: llc -mtriple arm64e-apple-darwin \
+; RUN:   -global-isel -global-isel-abort=1 -verify-machineinstrs \
+; RUN:   -asm-verbose=false -aarch64-enable-collect-loh=false \
+; RUN:   -o - %s | FileCheck %s
+
+; The discriminator is the same for all blockaddresses in the function.

kovdan01 wrote:

Nit: it looks like that comments which are actually comments and not run/check 
lines use `;;` as prefix instead of `;` in newly added tests. Same applies 
below.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -1789,6 +1789,9 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
   options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
   Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
 options::OPT_fno_ptrauth_init_fini);
+

kovdan01 wrote:

Nit: there is no empty line between previous `Args.addOptInFlag(...)` 
invocations, so probably this new line should also be deleted.

Feel free to ignore

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 deleted 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 deleted 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@ahmedbougacha I've updated my review: I've misinterpreted logic a bit 
previously, and thought that a couple of codepaths are not covered by tests. 
It's not true, everything is OK, but please address @efriedma-quic 's concern 
described above so this can be approved and merged.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-04-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/84384

>From ef23d427b48687b62da9e1062886ddfcc1649b6a Mon Sep 17 00:00:00 2001
From: John McCall 
Date: Mon, 16 Dec 2019 20:31:25 -0500
Subject: [PATCH 1/5] Abstract serialization fixes for the Apple Clang changes.

---
 clang/include/clang/AST/AbstractBasicReader.h | 4 ++--
 clang/include/clang/AST/AbstractBasicWriter.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/include/clang/AST/AbstractBasicReader.h 
b/clang/include/clang/AST/AbstractBasicReader.h
index 1f2797cc701458..ab036f1d445acc 100644
--- a/clang/include/clang/AST/AbstractBasicReader.h
+++ b/clang/include/clang/AST/AbstractBasicReader.h
@@ -213,9 +213,9 @@ class DataStreamBasicReader : public BasicReaderBase {
   }
 
   Qualifiers readQualifiers() {
-static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(Qualifiers().getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-uint32_t value = asImpl().readUInt32();
+uint64_t value = asImpl().readUInt64();
 return Qualifiers::fromOpaqueValue(value);
   }
 
diff --git a/clang/include/clang/AST/AbstractBasicWriter.h 
b/clang/include/clang/AST/AbstractBasicWriter.h
index 07afa388de2c17..8e42fcaad1d388 100644
--- a/clang/include/clang/AST/AbstractBasicWriter.h
+++ b/clang/include/clang/AST/AbstractBasicWriter.h
@@ -196,9 +196,9 @@ class DataStreamBasicWriter : public BasicWriterBase {
   }
 
   void writeQualifiers(Qualifiers value) {
-static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint32_t),
+static_assert(sizeof(value.getAsOpaqueValue()) <= sizeof(uint64_t),
   "update this if the value size changes");
-asImpl().writeUInt32(value.getAsOpaqueValue());
+asImpl().writeUInt64(value.getAsOpaqueValue());
   }
 
   void writeExceptionSpecInfo(

>From 9e296a1a69158419960c265f12f52523db0c8e2a Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:34:24 +0300
Subject: [PATCH 2/5] [clang] Define `PointerAuthenticationMode` enum

---
 clang/include/clang/Basic/LangOptions.h | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 862952d336ef31..6fe7472d8ad0ca 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -57,6 +57,13 @@ enum class ShaderStage {
   Invalid,
 };
 
+enum class PointerAuthenticationMode : unsigned {
+  None,
+  Strip,
+  SignAndStrip,
+  SignAndAuth
+};
+
 /// Bitfields of LangOptions, split out from LangOptions in order to ensure 
that
 /// this large collection of bitfields is a trivial class type.
 class LangOptionsBase {

>From 71d7760aa9cc459d7bc0365fa079f64cb5704c96 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Thu, 7 Mar 2024 15:31:03 +0300
Subject: [PATCH 3/5] [clang] Define `PointerAuthQualifier` class

Includes changes from the following commits from the branch
https://github.com/ahmedbougacha/llvm-project/tree/eng/arm64e-upstream-llvmorg

- Initial support  
https://github.com/ahmedbougacha/llvm-project/commit/cc7ba7eb1814e9b254c7d94aa0b78cb0e21acfc5
- ObjC isa signing 
https://github.com/ahmedbougacha/llvm-project/commit/c9ce0d408f1d9aeffc7b86256334220aec6de5a3

Also applies a fix from https://github.com/access-softek/llvm-project/pull/75

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/AST/Type.h| 215 +-
 .../include/clang/Basic/PointerAuthOptions.h  |  23 ++
 2 files changed, 227 insertions(+), 11 deletions(-)
 create mode 100644 clang/include/clang/Basic/PointerAuthOptions.h

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 1942b0e67f65a3..1741a3017f7280 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -25,8 +25,10 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/ExceptionSpecificationType.h"
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/LangOptions.h"
 #include "clang/Basic/Linkage.h"
 #include "clang/Basic/PartialDiagnostic.h"
+#include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
 #include "clang/Basic/Visibility.h"
@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits

[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits


@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1)
+ << AuthenticationModeShift,
+IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits,
+IsaPointerBits = 1,
+IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift,
+AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits,
+AuthenticatesNullValuesBits = 1,
+AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1)
+  << AuthenticatesNullValuesShift,
+KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits,
+KeyBits = 10,
+KeyMask = ((1 << KeyBits) - 1) << KeyShift,
+DiscriminatorShift = KeyShift + KeyBits,
+DiscriminatorBits = 16,
+DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift,
+  };
+
+  // bits: |0  |1  |2..3  |4  |
+  //   |Enabled|Address|AuthenticationMode|ISA pointer|
+  // bits: |5|6..15|   16...31   |
+  //   |AuthenticatesNull|Key  |Discriminator|
+  uint32_t Data;
+
+  static_assert((EnabledBits + AddressDiscriminatedBits +
+ AuthenticationModeBits + IsaPointerBits +
+ AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) ==
+32,
+"PointerAuthQualifier should be exactly 32 bits");
+  static_assert((EnabledMask + AddressDiscriminatedMask +
+ AuthenticationModeMask + IsaPointerMask +
+ AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+  static_assert((EnabledMask ^ AddressDiscriminatedMask ^

kovdan01 wrote:

I think that both static assertions should be left (probably with messages 
changed to distinguish them - suggestions are welcome). What we want to check 
is that every single bit out of all 32 bits is present in one and only one of 
the constants (added a comment in code mentioning that).

If we only check the sum of the constants, we might miss a situation when some 
bits are present in more than one constant while others are not present at all, 
like `0b111 = 0b100 + 0b10 + 0b1 = 0b11 + 0b11 + 0b1`. Counting xor would 
reveal the issue.

If we only check the xor of the constants, we might miss a situation when all 
the bits are present, but some of them are present not once, but odd number of 
times, like `0b111 = 0b111 ^ 0b111 ^ 0b111`. Counting sum would reveal the 
issue (keeping in mind that arithmetic on `uint32_t` works on 2^32 modulo).

Since constants definition is not trivial, it's probably better to leave both 
assertions here to avoid potential. Please let me know if I miss smth.

https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits


@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1)
+ << AuthenticationModeShift,
+IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits,
+IsaPointerBits = 1,
+IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift,
+AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits,
+AuthenticatesNullValuesBits = 1,
+AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1)
+  << AuthenticatesNullValuesShift,
+KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits,
+KeyBits = 10,
+KeyMask = ((1 << KeyBits) - 1) << KeyShift,
+DiscriminatorShift = KeyShift + KeyBits,
+DiscriminatorBits = 16,
+DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift,
+  };
+
+  // bits: |0  |1  |2..3  |4  |
+  //   |Enabled|Address|AuthenticationMode|ISA pointer|
+  // bits: |5|6..15|   16...31   |
+  //   |AuthenticatesNull|Key  |Discriminator|
+  uint32_t Data;
+
+  static_assert((EnabledBits + AddressDiscriminatedBits +
+ AuthenticationModeBits + IsaPointerBits +
+ AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) ==
+32,
+"PointerAuthQualifier should be exactly 32 bits");
+  static_assert((EnabledMask + AddressDiscriminatedMask +
+ AuthenticationModeMask + IsaPointerMask +
+ AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+  static_assert((EnabledMask ^ AddressDiscriminatedMask ^
+ AuthenticationModeMask ^ IsaPointerMask ^
+ AuthenticatesNullValuesMask ^ KeyMask ^ DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+
+  PointerAuthQualifier(unsigned key, bool isAddressDiscriminated,
+   unsigned extraDiscriminator,
+   PointerAuthenticationMode authenticationMode,
+   bool isIsaPointer, bool authenticatesNullValues)
+  : Data(EnabledMask |
+ (isAddressDiscriminated
+  ? static_cast(AddressDiscriminatedMask)
+  : 0) |
+ (key << KeyShift) |
+ (unsigned(authenticationMode) << AuthenticationModeShift) |
+ (extraDiscriminator << DiscriminatorShift) |
+ (isIsaPointer << IsaPointerShift) |
+ (authenticatesNullValues << AuthenticatesNullValuesShift)) {
+assert(key <= KeyNoneInternal);
+assert(extraDiscriminator <= MaxDiscriminator);
+  }
+
+public:
+  enum {
+KeyNoneInternal = (1u << KeyBits) - 1,
+
+/// The maximum supported pointer-authentication key.
+MaxKey = KeyNoneInternal - 1,
+
+/// The maximum supported pointer-authentication discriminator.
+MaxDiscriminator = (1u << DiscriminatorBits) - 1
+  };
+
+public:
+  PointerAuthQualifier() : Data(0) {}
+
+  static PointerAuthQualifier
+  Create(int key, bool isAddressDiscriminated, unsigned extraDiscriminator,

kovdan01 wrote:

It should be one type everywhere, thanks for bringing attention to this. 
Changed to `unsigned` to keep things consistent with other parameters.


https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits


@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1)
+ << AuthenticationModeShift,
+IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits,
+IsaPointerBits = 1,
+IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift,
+AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits,
+AuthenticatesNullValuesBits = 1,
+AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1)
+  << AuthenticatesNullValuesShift,
+KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits,
+KeyBits = 10,
+KeyMask = ((1 << KeyBits) - 1) << KeyShift,
+DiscriminatorShift = KeyShift + KeyBits,
+DiscriminatorBits = 16,
+DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift,
+  };
+
+  // bits: |0  |1  |2..3  |4  |
+  //   |Enabled|Address|AuthenticationMode|ISA pointer|
+  // bits: |5|6..15|   16...31   |
+  //   |AuthenticatesNull|Key  |Discriminator|
+  uint32_t Data;
+
+  static_assert((EnabledBits + AddressDiscriminatedBits +
+ AuthenticationModeBits + IsaPointerBits +
+ AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) ==
+32,
+"PointerAuthQualifier should be exactly 32 bits");
+  static_assert((EnabledMask + AddressDiscriminatedMask +
+ AuthenticationModeMask + IsaPointerMask +
+ AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+  static_assert((EnabledMask ^ AddressDiscriminatedMask ^
+ AuthenticationModeMask ^ IsaPointerMask ^
+ AuthenticatesNullValuesMask ^ KeyMask ^ DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+
+  PointerAuthQualifier(unsigned key, bool isAddressDiscriminated,
+   unsigned extraDiscriminator,
+   PointerAuthenticationMode authenticationMode,
+   bool isIsaPointer, bool authenticatesNullValues)
+  : Data(EnabledMask |
+ (isAddressDiscriminated
+  ? static_cast(AddressDiscriminatedMask)
+  : 0) |
+ (key << KeyShift) |
+ (unsigned(authenticationMode) << AuthenticationModeShift) |

kovdan01 wrote:

Fixed, thanks, see 3517033bd3a8b676abd649562bcfc7eb4d278d09


https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits


@@ -138,6 +140,165 @@ using CanQualType = CanQual;
 #define TYPE(Class, Base) class Class##Type;
 #include "clang/AST/TypeNodes.inc"
 
+/// Pointer-authentication qualifiers.
+class PointerAuthQualifier {
+  enum : uint32_t {
+EnabledShift = 0,
+EnabledBits = 1,
+EnabledMask = 1 << EnabledShift,
+AddressDiscriminatedShift = EnabledShift + EnabledBits,
+AddressDiscriminatedBits = 1,
+AddressDiscriminatedMask = 1 << AddressDiscriminatedShift,
+AuthenticationModeShift =
+AddressDiscriminatedShift + AddressDiscriminatedBits,
+AuthenticationModeBits = 2,
+AuthenticationModeMask = ((1 << AuthenticationModeBits) - 1)
+ << AuthenticationModeShift,
+IsaPointerShift = AuthenticationModeShift + AuthenticationModeBits,
+IsaPointerBits = 1,
+IsaPointerMask = ((1 << IsaPointerBits) - 1) << IsaPointerShift,
+AuthenticatesNullValuesShift = IsaPointerShift + IsaPointerBits,
+AuthenticatesNullValuesBits = 1,
+AuthenticatesNullValuesMask = ((1 << AuthenticatesNullValuesBits) - 1)
+  << AuthenticatesNullValuesShift,
+KeyShift = AuthenticatesNullValuesShift + AuthenticatesNullValuesBits,
+KeyBits = 10,
+KeyMask = ((1 << KeyBits) - 1) << KeyShift,
+DiscriminatorShift = KeyShift + KeyBits,
+DiscriminatorBits = 16,
+DiscriminatorMask = ((1u << DiscriminatorBits) - 1) << DiscriminatorShift,
+  };
+
+  // bits: |0  |1  |2..3  |4  |
+  //   |Enabled|Address|AuthenticationMode|ISA pointer|
+  // bits: |5|6..15|   16...31   |
+  //   |AuthenticatesNull|Key  |Discriminator|
+  uint32_t Data;
+
+  static_assert((EnabledBits + AddressDiscriminatedBits +
+ AuthenticationModeBits + IsaPointerBits +
+ AuthenticatesNullValuesBits + KeyBits + DiscriminatorBits) ==
+32,
+"PointerAuthQualifier should be exactly 32 bits");
+  static_assert((EnabledMask + AddressDiscriminatedMask +
+ AuthenticationModeMask + IsaPointerMask +
+ AuthenticatesNullValuesMask + KeyMask + DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+  static_assert((EnabledMask ^ AddressDiscriminatedMask ^
+ AuthenticationModeMask ^ IsaPointerMask ^
+ AuthenticatesNullValuesMask ^ KeyMask ^ DiscriminatorMask) ==
+0x,
+"All masks should cover the entire bits");
+
+  PointerAuthQualifier(unsigned key, bool isAddressDiscriminated,
+   unsigned extraDiscriminator,
+   PointerAuthenticationMode authenticationMode,
+   bool isIsaPointer, bool authenticatesNullValues)
+  : Data(EnabledMask |
+ (isAddressDiscriminated
+  ? static_cast(AddressDiscriminatedMask)

kovdan01 wrote:

Fixed, thanks, see 3517033bd3a8b676abd649562bcfc7eb4d278d09


https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits


@@ -251,15 +423,16 @@ class Qualifiers {
   }
 
   // Deserialize qualifiers from an opaque representation.
-  static Qualifiers fromOpaqueValue(unsigned opaque) {
+  static Qualifiers fromOpaqueValue(uint64_t Opaque) {
 Qualifiers Qs;
-Qs.Mask = opaque;
+Qs.Mask = uint32_t(Opaque);

kovdan01 wrote:

Is this OK or should we prefer smth like `Opaque & 0x` or even `Opaque 
& std::numeric_limits::max()` (or smth else)?


https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

> Are there unittests where we could exercise these types?

@Michael137 Could you clarify a bit, what is the correct place for such a unit 
test? As for `ASTContext` methods similar to newly proposed 
`getPointerAuthType` (like `getQualifiedType` and `getCVRQualifiedType`), I've 
not found unit tests with them. As for `Qualifiers` class also touched in this 
PR, I've not found tests for it at all. Newly proposed `PointerAuthQualifier` 
qualifier, obviously, also has no unit tests now, but if we want to implement 
them, they should be placed somewhere near unit tests for `Qualifiers` which 
are absent as well.


https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang] Define `PointerAuthQualifier` and `PointerAuthenticationMode` (PR #84384)

2024-04-08 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Although functions in this file use different code styles for function 
arguments (both `camelCase` and `PascalCase`), I've changed the code style for 
function arguments of newly added functions to `PascalCase` as described in 
https://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly
 - see 3517033bd3a8b676abd649562bcfc7eb4d278d09.

I could also submit a subsequent PR fixing the code style  of function 
parameters in clang/include/clang/AST/Type.h and 
clang/include/clang/AST/ASTContext.h which are not in `PascalCase`. Please let 
me know if there are any objections on this.



https://github.com/llvm/llvm-project/pull/84384
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-04-12 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay A kind reminder regarding the PR - please let me know if latest 
updates address your previous comments and if there are other issues still 
present.

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Define ptrauth_string_discriminator builtin. (PR #93903)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 approved this pull request.


https://github.com/llvm/llvm-project/pull/93903
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/96159

Depends on #96158

Treat 7th bit of version value for llvm_linux platform as signed GOT flag.

- clang: define `PointerAuthELFGOT` LangOption and set 7th bit of 
`aarch64-elf-pauthabi-version` LLVM module flag correspondingly;

- llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version 
description of llvm_linux platform depending on whether the flag is set.

>From 4eeb1b4e82941681b6cafda8579d136e3e7cb09f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 18 Jun 2024 15:37:18 +0300
Subject: [PATCH] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info

Treat 7th bit of version value for llvm_linux platform as signed GOT flag.

- clang: define `PointerAuthELFGOT` LangOption and set 7th bit of
  `aarch64-elf-pauthabi-version` LLVM module flag correspondingly;

- llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version
  description of llvm_linux platform depending on whether the flag is set.
---
 clang/include/clang/Basic/LangOptions.def  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp|  6 --
 llvm/include/llvm/BinaryFormat/ELF.h   |  3 ++-
 .../AArch64/note-gnu-property-elf-pauthabi.ll  |  2 +-
 .../ELF/AArch64/aarch64-feature-pauth.s| 18 +-
 llvm/tools/llvm-readobj/ELFDumper.cpp  |  3 ++-
 6 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..bc99dad5cd55e 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthELFGOT, 1, 0, "authenticate pointers from GOT")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index dd4a665ebc78b..feac291e01b50 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1210,8 +1210,10 @@ void CodeGenModule::Release() {
   (LangOpts.PointerAuthVTPtrTypeDiscrimination
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
   (LangOpts.PointerAuthInitFini
-   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI);
-  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI ==
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI) |
+  (LangOpts.PointerAuthELFGOT
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT);
+  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT ==
 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
 "Update when new enum items are defined");
   if (PAuthABIVersion != 0) {
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index dfba180149916..2aa37bbed6656 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1774,8 +1774,9 @@ enum : unsigned {
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR = 4,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR = 5,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI = 6,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT = 7,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST =
-  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT,
 };
 
 // x86 processor feature bits.
diff --git a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll 
b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
index 728cffeba02a2..fb69a12b2f906 100644
--- a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
+++ b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
@@ -27,7 +27,7 @@
 ; OBJ: Displaying notes found in: .note.gnu.property
 ; OBJ-NEXT:   Owner Data size  Description
 ; OBJ-NEXT:   GNU   0x0018 NT_GNU_PROPERTY_TYPE_0 
(property note)
-; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 (PointerAuthIntrinsics, !PointerAuthCalls, PointerAuthReturns, 
!PointerAuthAuthTraps, PointerAuthVTPtrAddressDiscrimination, 
!PointerAuthVTPtrTypeDiscrimination, PointerAuthInitFini)
+; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 (PointerAut

[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/96160

Depends on #96159

Add `-fptrauth-elf-got` clang driver flag and set `ptrauth_elf_got` 
preprocessor feature and `PointerAuthELFGOT` LangOption correspondingly. For 
non-ELF triples, the driver flag is ignored and a warning is emitted.

>From f891f791dfe882389d83d3c4c4fb57d67a845c04 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 18 Jun 2024 15:38:18 +0300
Subject: [PATCH] [PAC][clang][Driver] Add signed GOT flag

Add `-fptrauth-elf-got` clang driver flag and set `ptrauth_elf_got`
preprocessor feature and `PointerAuthELFGOT` LangOption correspondingly.
For non-ELF triples, the driver flag is ignored and a warning is emitted.
---
 .../clang/Basic/DiagnosticDriverKinds.td  |  4 ++
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  7 +++
 clang/lib/Frontend/CompilerInvocation.cpp |  4 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 11 +++-
 clang/test/Driver/aarch64-ptrauth.c   |  9 +++-
 clang/test/Preprocessor/ptrauth_feature.c | 52 ++-
 8 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1ca2cb85565a1..28667b1eb239e 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -742,6 +742,10 @@ def warn_drv_fjmc_for_elf_only : Warning<
   "-fjmc works only for ELF; option ignored">,
   InGroup;
 
+def warn_drv_ptrauth_elf_got_for_elf_only : Warning<
+  "-fptrauth-elf-got works only for ELF; option ignored">,
+  InGroup;
+
 def warn_target_override_arm64ec : Warning<
   "/arm64EC has been overridden by specified target: %0; option ignored">,
   InGroup;
diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..569f4e1715af5 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_elf_got, LangOpts.PointerAuthELFGOT)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 112eb286eb075..e16c1a0d06a1b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4222,6 +4222,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   OptInCC1FFlag<"ptrauth-vtable-pointer-type-discrimination", "Enable type 
discrimination of vtable pointers">;
 defm ptrauth_init_fini : OptInCC1FFlag<"ptrauth-init-fini", "Enable signing of 
function pointers in init/fini arrays">;
+defm ptrauth_elf_got : OptInCC1FFlag<"ptrauth-elf-got", "Enable authentication 
of pointers from GOT (ELF only)">;
 }
 
 def fenable_matrix : Flag<["-"], "fenable-matrix">, Group,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 331cf6e713d89..5f55e79ec206b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1788,6 +1788,13 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
   options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
   Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
 options::OPT_fno_ptrauth_init_fini);
+
+  Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_elf_got,
+options::OPT_fno_ptrauth_elf_got);
+
+  if (Args.hasArg(options::OPT_fptrauth_elf_got))
+getToolChain().getDriver().Diag(
+diag::warn_drv_ptrauth_elf_got_for_elf_only);
 }
 
 void Clang::AddLoongArchTargetArgs(const ArgList &Args,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 58694e5399d58..97a5408a4c1e0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3361,6 +3361,8 @@ static void GeneratePointerAuthArgs(const LangOptions 
&Opts,
 GenerateArg(Consumer, OPT_fptrauth_vtable_pointer_type_discrimination);
   if (Opts.PointerAuthInitFini)
 GenerateArg(Consumer, OPT_fptrauth_init_fini);
+  if (Opts.PointerAuthELFGOT)
+GenerateArg(Consumer, OPT_fptrauth_elf_got);
 }
 
 static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
@@ -3374,6 +3376,7 @@ static void ParsePointerAuthArgs(LangOptions &Opts, 
ArgList &Args,
   Opts.PointerAuthVTPtrTypeDiscrimination =

[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   >