Skip to content

Commit fd3919e

Browse files
committed
[PAC][clang] Add signed GOT cc1 flag
Add `-fptrauth-elf-got` clang cc1 flag and set `ptrauth_elf_got` preprocessor feature and `PointerAuthELFGOT` LangOption correspondingly. No additional checks like ensuring OS binary format is ELF are performed: it should be done on clang driver level when a pauth-enabled environment implying signed GOT enabled is requested. If the cc1 flag is passed, "ptrauth-elf-got" IR module flag is set.
1 parent 1e4646d commit fd3919e

File tree

7 files changed

+45
-13
lines changed

7 files changed

+45
-13
lines changed

clang/include/clang/Basic/Features.def

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ FEATURE(ptrauth_function_pointer_type_discrimination, LangOpts.PointerAuthFuncti
116116
FEATURE(ptrauth_indirect_gotos, LangOpts.PointerAuthIndirectGotos)
117117
FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
118118
FEATURE(ptrauth_init_fini_address_discrimination, LangOpts.PointerAuthInitFiniAddressDiscrimination)
119+
FEATURE(ptrauth_elf_got, LangOpts.PointerAuthELFGOT)
119120
EXTENSION(swiftcc,
120121
PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
121122
clang::TargetInfo::CCCR_OK)

clang/include/clang/Driver/Options.td

+1
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,7 @@ defm ptrauth_indirect_gotos : OptInCC1FFlag<"ptrauth-indirect-gotos",
43564356
defm ptrauth_init_fini : OptInCC1FFlag<"ptrauth-init-fini", "Enable signing of function pointers in init/fini arrays">;
43574357
defm ptrauth_init_fini_address_discrimination : OptInCC1FFlag<"ptrauth-init-fini-address-discrimination",
43584358
"Enable address discrimination of function pointers in init/fini arrays">;
4359+
defm ptrauth_elf_got : OptInCC1FFlag<"ptrauth-elf-got", "Enable authentication of pointers from GOT (ELF only)">;
43594360
}
43604361

43614362
def fenable_matrix : Flag<["-"], "fenable-matrix">, Group<f_Group>,

clang/lib/CodeGen/CodeGenModule.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,9 @@ void CodeGenModule::Release() {
12131213
getModule().addModuleFlag(llvm::Module::Min,
12141214
"sign-return-address-with-bkey", 1);
12151215

1216+
if (LangOpts.PointerAuthELFGOT)
1217+
getModule().addModuleFlag(llvm::Module::Min, "ptrauth-elf-got", 1);
1218+
12161219
if (getTriple().isOSLinux()) {
12171220
assert(getTriple().isOSBinFormatELF());
12181221
using namespace llvm::ELF;

clang/lib/Frontend/CompilerInvocation.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3452,6 +3452,8 @@ static void GeneratePointerAuthArgs(const LangOptions &Opts,
34523452
GenerateArg(Consumer, OPT_fptrauth_init_fini);
34533453
if (Opts.PointerAuthInitFiniAddressDiscrimination)
34543454
GenerateArg(Consumer, OPT_fptrauth_init_fini_address_discrimination);
3455+
if (Opts.PointerAuthELFGOT)
3456+
GenerateArg(Consumer, OPT_fptrauth_elf_got);
34553457
}
34563458

34573459
static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
@@ -3472,6 +3474,7 @@ static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
34723474
Opts.PointerAuthInitFini = Args.hasArg(OPT_fptrauth_init_fini);
34733475
Opts.PointerAuthInitFiniAddressDiscrimination =
34743476
Args.hasArg(OPT_fptrauth_init_fini_address_discrimination);
3477+
Opts.PointerAuthELFGOT = Args.hasArg(OPT_fptrauth_elf_got);
34753478
}
34763479

34773480
/// Check if input file kind and language standard are compatible.

clang/test/CodeGen/AArch64/elf-pauthabi.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//// TODO: also test with -fptrauth-elf-got when the driver flag is supported
2-
31
// RUN: %clang_cc1 -triple aarch64-linux -emit-llvm -o - \
42
// RUN: -fptrauth-intrinsics \
53
// RUN: -fptrauth-calls \
@@ -9,6 +7,7 @@
97
// RUN: -fptrauth-vtable-pointer-type-discrimination \
108
// RUN: -fptrauth-init-fini \
119
// RUN: -fptrauth-init-fini-address-discrimination \
10+
// RUN: -fptrauth-elf-got \
1211
// RUN: -fptrauth-indirect-gotos \
1312
// RUN: -fptrauth-type-info-vtable-pointer-discrimination \
1413
// RUN: -fptrauth-function-pointer-type-discrimination %s | \
@@ -42,6 +41,9 @@
4241
// RUN: -fptrauth-calls -fptrauth-init-fini -fptrauth-init-fini-address-discrimination %s | \
4342
// RUN: FileCheck %s --check-prefix=INITFINIADDR
4443

44+
// RUN: %clang_cc1 -triple aarch64-linux -emit-llvm -o - \
45+
// RUN: -fptrauth-elf-got %s | FileCheck %s --check-prefix=ELFGOT
46+
4547
// RUN: %clang_cc1 -triple aarch64-linux -emit-llvm -o - \
4648
// RUN: -fptrauth-indirect-gotos %s | FileCheck %s --check-prefix=GOTOS
4749

@@ -54,7 +56,7 @@
5456
// RUN: FileCheck %s --check-prefix=FPTRTYPE
5557

5658
// ALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
57-
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 3839}
59+
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 4095}
5860

5961
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
6062
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1}
@@ -80,6 +82,9 @@
8082
// INITFINIADDR: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
8183
// INITFINIADDR: !{i32 1, !"aarch64-elf-pauthabi-version", i32 194}
8284

85+
// ELFGOT: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
86+
// ELFGOT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 256}
87+
8388
// GOTOS: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
8489
// GOTOS: !{i32 1, !"aarch64-elf-pauthabi-version", i32 512}
8590

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=OFF
2+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-elf-got -emit-llvm %s -o - | FileCheck %s --check-prefix=ELFGOT
3+
4+
// ELFGOT: !llvm.module.flags = !{
5+
// ELFGOT-SAME: !1
6+
// ELFGOT: !1 = !{i32 8, !"ptrauth-elf-got", i32 1}
7+
8+
// OFF-NOT: "ptrauth-

clang/test/Preprocessor/ptrauth_feature.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
22
//// For example, -fptrauth-init-fini will not affect codegen without -fptrauth-calls, but the preprocessor feature would be set anyway.
33

44
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-intrinsics | \
5-
// RUN: FileCheck %s --check-prefixes=INTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
5+
// RUN: FileCheck %s --check-prefixes=INTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
66

77
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-calls | \
8-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,CALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
8+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,CALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
99

1010
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-returns | \
11-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,RETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
11+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,RETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
1212

1313
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-vtable-pointer-address-discrimination | \
14-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,VPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
14+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,VPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
1515

1616
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-vtable-pointer-type-discrimination | \
17-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,VPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
17+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,VPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
1818

1919
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-type-info-vtable-pointer-discrimination | \
20-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,TYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
20+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,TYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
2121

2222
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-function-pointer-type-discrimination | \
23-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,FUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
23+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,FUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
2424

2525
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-init-fini | \
26-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,INITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS
26+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,INITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
2727

2828
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-init-fini-address-discrimination | \
29-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,INITFINI_ADDR_DISCR,NOGOTOS
29+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,INITFINI_ADDR_DISCR,NOGOTOS,NOELFGOT
3030

3131
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-indirect-gotos | \
32-
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,GOTOS
32+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,GOTOS,NOELFGOT
33+
34+
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-elf-got | \
35+
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,ELFGOT
3336

3437
#if __has_feature(ptrauth_intrinsics)
3538
// INTRIN: has_ptrauth_intrinsics
@@ -119,3 +122,11 @@ void has_ptrauth_indirect_gotos() {}
119122
// NOGOTOS: no_ptrauth_indirect_gotos
120123
void no_ptrauth_indirect_gotos() {}
121124
#endif
125+
126+
#if __has_feature(ptrauth_elf_got)
127+
// ELFGOT: has_ptrauth_elf_got
128+
void has_ptrauth_elf_got() {}
129+
#else
130+
// NOELFGOT: no_ptrauth_elf_got
131+
void no_ptrauth_elf_got() {}
132+
#endif

0 commit comments

Comments
 (0)