Skip to content

Commit

Permalink
[AArch64][PAC][clang][ELF] Support PAuth ABI compatibility tag
Browse files Browse the repository at this point in the history
Emit PAuth ABI compatibility tag values as llvm module flags:
- `aarch64-elf-pauthabi-platform`
- `aarch64-elf-pauthabi-version`
  • Loading branch information
kovdan01 committed Feb 16, 2024
1 parent c8d71df commit a0cc186
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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"
Expand Down Expand Up @@ -1075,6 +1076,23 @@ 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.PointerAuthCalls << 0) |
(LangOpts.PointerAuthReturns << 1) |
(LangOpts.PointerAuthVTPtrAddressDiscrimination << 2) |
(LangOpts.PointerAuthVTPtrTypeDiscrimination << 3) |
(LangOpts.PointerAuthInitFini << 4);
if (PAuthABIVersion != 0) {
getModule().addModuleFlag(
llvm::Module::Error, "aarch64-elf-pauthabi-platform",
llvm::ELF::GNU_PROPERTY_AARCH64_FEATURE_PAUTH_PLATFORM_LINUX);
getModule().addModuleFlag(llvm::Module::Error,
"aarch64-elf-pauthabi-version",
PAuthABIVersion);
}
}
}

if (!CodeGenOpts.MemoryProfileOutput.empty()) {
Expand Down
28 changes: 28 additions & 0 deletions clang/test/CodeGen/aarch64-elf-pauthabi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -mbranch-protection=pauthabi %s | FileCheck %s --check-prefix=ALL
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -fptrauth-returns %s | FileCheck %s --check-prefix=RET
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -fptrauth-calls %s | FileCheck %s --check-prefix=CALL
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -fptrauth-calls -fptrauth-vtable-pointer-address-discrimination %s | FileCheck %s --check-prefix=VPTRADDR
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -fptrauth-calls -fptrauth-vtable-pointer-type-discrimination %s | FileCheck %s --check-prefix=VPTRTYPE
// RUN: %clang -target aarch64-linux -S -emit-llvm -o - -fptrauth-calls -fptrauth-init-fini %s | FileCheck %s --check-prefix=INITFINI

// REQUIRES: aarch64-registered-target

// ALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 31}

// RET: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// RET: !{i32 1, !"aarch64-elf-pauthabi-version", i32 2}

// CALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// CALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1}

// VPTRADDR: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// VPTRADDR: !{i32 1, !"aarch64-elf-pauthabi-version", i32 5}

// VPTRTYPE: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// VPTRTYPE: !{i32 1, !"aarch64-elf-pauthabi-version", i32 9}

// INITFINI: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 2}
// INITFINI: !{i32 1, !"aarch64-elf-pauthabi-version", i32 17}

void foo() {}

0 comments on commit a0cc186

Please sign in to comment.