Skip to content

Commit 1ca6cf8

Browse files
committed
[PAC][clang][Driver] Add signed GOT flag
Depends on llvm#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.
1 parent 2336ef9 commit 1ca6cf8

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ def warn_drv_fjmc_for_elf_only : Warning<
751751
"-fjmc works only for ELF; option ignored">,
752752
InGroup<OptionIgnored>;
753753

754+
def warn_drv_ptrauth_elf_got_for_elf_only : Warning<
755+
"-fptrauth-elf-got works only for ELF; option ignored">,
756+
InGroup<OptionIgnored>;
757+
754758
def warn_target_override_arm64ec : Warning<
755759
"/arm64EC has been overridden by specified target: %0; option ignored">,
756760
InGroup<OptionIgnored>;

clang/include/clang/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ FEATURE(ptrauth_function_pointer_type_discrimination, LangOpts.PointerAuthFuncti
114114
FEATURE(ptrauth_indirect_gotos, LangOpts.PointerAuthIndirectGotos)
115115
FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
116116
FEATURE(ptrauth_init_fini_address_discrimination, LangOpts.PointerAuthInitFiniAddressDiscrimination)
117+
FEATURE(ptrauth_elf_got, LangOpts.PointerAuthELFGOT)
117118
EXTENSION(swiftcc,
118119
PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
119120
clang::TargetInfo::CCCR_OK)

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,6 +4263,7 @@ defm ptrauth_indirect_gotos : OptInCC1FFlag<"ptrauth-indirect-gotos",
42634263
defm ptrauth_init_fini : OptInCC1FFlag<"ptrauth-init-fini", "Enable signing of function pointers in init/fini arrays">;
42644264
defm ptrauth_init_fini_address_discrimination : OptInCC1FFlag<"ptrauth-init-fini-address-discrimination",
42654265
"Enable address discrimination of function pointers in init/fini arrays">;
4266+
defm ptrauth_elf_got : OptInCC1FFlag<"ptrauth-elf-got", "Enable authentication of pointers from GOT (ELF only)">;
42664267
}
42674268

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

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,14 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
18581858
Args.addOptInFlag(CmdArgs,
18591859
options::OPT_fptrauth_init_fini_address_discrimination,
18601860
options::OPT_fno_ptrauth_init_fini_address_discrimination);
1861+
1862+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_elf_got,
1863+
options::OPT_fno_ptrauth_elf_got);
1864+
1865+
if (Args.hasArg(options::OPT_fptrauth_elf_got) &&
1866+
Triple.getObjectFormat() != llvm::Triple::ELF)
1867+
getToolChain().getDriver().Diag(
1868+
diag::warn_drv_ptrauth_elf_got_for_elf_only);
18611869
}
18621870

18631871
void Clang::AddLoongArchTargetArgs(const ArgList &Args,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,8 @@ static void GeneratePointerAuthArgs(const LangOptions &Opts,
34373437
GenerateArg(Consumer, OPT_fptrauth_init_fini);
34383438
if (Opts.PointerAuthInitFiniAddressDiscrimination)
34393439
GenerateArg(Consumer, OPT_fptrauth_init_fini_address_discrimination);
3440+
if (Opts.PointerAuthELFGOT)
3441+
GenerateArg(Consumer, OPT_fptrauth_elf_got);
34403442
}
34413443

34423444
static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
@@ -3457,6 +3459,7 @@ static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
34573459
Opts.PointerAuthInitFini = Args.hasArg(OPT_fptrauth_init_fini);
34583460
Opts.PointerAuthInitFiniAddressDiscrimination =
34593461
Args.hasArg(OPT_fptrauth_init_fini_address_discrimination);
3462+
Opts.PointerAuthELFGOT = Args.hasArg(OPT_fptrauth_elf_got);
34603463
}
34613464

34623465
/// Check if input file kind and language standard are compatible.
@@ -4819,6 +4822,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
48194822
ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
48204823

48214824
ParsePointerAuthArgs(LangOpts, Args, Diags);
4825+
LangOpts.PointerAuthELFGOT &= T.isOSBinFormatELF();
48224826

48234827
ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes,
48244828
Diags);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
// RUN: -fptrauth-auth-traps \
66
// RUN: -fptrauth-vtable-pointer-address-discrimination \
77
// RUN: -fptrauth-vtable-pointer-type-discrimination \
8-
// RUN: -fptrauth-init-fini %s \
9-
// RUN: -fptrauth-init-fini-address-discrimination %s | \
8+
// RUN: -fptrauth-init-fini \
9+
// RUN: -fptrauth-init-fini-address-discrimination \
10+
// RUN: -fptrauth-elf-got %s | \
1011
// RUN: FileCheck %s --check-prefix=ALL
1112

1213
// RUN: %clang_cc1 -triple aarch64-linux -emit-llvm -o - \
@@ -37,8 +38,11 @@
3738
// RUN: -fptrauth-calls -fptrauth-init-fini -fptrauth-init-fini-address-discrimination %s | \
3839
// RUN: FileCheck %s --check-prefix=INITFINIADDR
3940

41+
// RUN: %clang_cc1 -triple aarch64-linux -emit-llvm -o - \
42+
// RUN: -fptrauth-elf-got %s | FileCheck %s --check-prefix=ELFGOT
43+
4044
// ALL: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
41-
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 255}
45+
// ALL: !{i32 1, !"aarch64-elf-pauthabi-version", i32 511}
4246

4347
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
4448
// INTRIN: !{i32 1, !"aarch64-elf-pauthabi-version", i32 1}
@@ -64,4 +68,7 @@
6468
// INITFINIADDR: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
6569
// INITFINIADDR: !{i32 1, !"aarch64-elf-pauthabi-version", i32 194}
6670

71+
// ELFGOT: !{i32 1, !"aarch64-elf-pauthabi-platform", i32 268435458}
72+
// ELFGOT: !{i32 1, !"aarch64-elf-pauthabi-version", i32 256}
73+
6774
void foo() {}

clang/test/Driver/aarch64-ptrauth.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
// RUN: %s 2>&1 | FileCheck %s --check-prefix=ALL
1919
// ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" "-fptrauth-returns" "-fptrauth-auth-traps" "-fptrauth-vtable-pointer-address-discrimination" "-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-type-info-vtable-pointer-discrimination" "-fptrauth-indirect-gotos" "-fptrauth-init-fini" "-fptrauth-init-fini-address-discrimination"
2020

21+
// RUN: %clang -### -c --target=aarch64-elf -fno-ptrauth-elf-got -fptrauth-elf-got %s 2>&1 | FileCheck %s --check-prefix=ELFGOT
22+
// ELFGOT: "-cc1"{{.*}} "-fptrauth-elf-got"
23+
// ELFGOT-NOT: warning: -fptrauth-elf-got works only for ELF; option ignored [-Woption-ignored]
24+
25+
// RUN: %clang -### -c --target=aarch64-darwin -fptrauth-elf-got %s 2>&1 | FileCheck %s --check-prefix=NOELFGOT
26+
// NOELFGOT: warning: -fptrauth-elf-got works only for ELF; option ignored [-Woption-ignored]
2127
// RUN: %clang -### -c --target=aarch64-linux -mabi=pauthtest %s 2>&1 | FileCheck %s --check-prefix=PAUTHABI1
2228
// RUN: %clang -### -c --target=aarch64-linux-pauthtest %s 2>&1 | FileCheck %s --check-prefix=PAUTHABI1
2329
// PAUTHABI1: "-cc1"{{.*}} "-triple" "aarch64-unknown-linux-pauthtest"
@@ -38,7 +44,7 @@
3844
// RUN: not %clang -### -c --target=x86_64 -fptrauth-intrinsics -fptrauth-calls -fptrauth-returns -fptrauth-auth-traps \
3945
// RUN: -fptrauth-vtable-pointer-address-discrimination -fptrauth-vtable-pointer-type-discrimination \
4046
// RUN: -fptrauth-type-info-vtable-pointer-discrimination -fptrauth-indirect-gotos -fptrauth-init-fini \
41-
// RUN: -fptrauth-init-fini-address-discrimination %s 2>&1 | FileCheck %s --check-prefix=ERR1
47+
// RUN: -fptrauth-init-fini-address-discrimination -fptrauth-elf-got %s 2>&1 | FileCheck %s --check-prefix=ERR1
4248
// ERR1: error: unsupported option '-fptrauth-intrinsics' for target '{{.*}}'
4349
// ERR1-NEXT: error: unsupported option '-fptrauth-calls' for target '{{.*}}'
4450
// ERR1-NEXT: error: unsupported option '-fptrauth-returns' for target '{{.*}}'
@@ -49,6 +55,7 @@
4955
// ERR1-NEXT: error: unsupported option '-fptrauth-indirect-gotos' for target '{{.*}}'
5056
// ERR1-NEXT: error: unsupported option '-fptrauth-init-fini' for target '{{.*}}'
5157
// ERR1-NEXT: error: unsupported option '-fptrauth-init-fini-address-discrimination' for target '{{.*}}'
58+
// ERR1-NEXT: error: unsupported option '-fptrauth-elf-got' for target '{{.*}}'
5259

5360
//// Only support PAuth ABI for Linux as for now.
5461
// RUN: not %clang -o /dev/null -c --target=aarch64-unknown -mabi=pauthtest %s 2>&1 | FileCheck %s --check-prefix=ERR2

clang/test/Preprocessor/ptrauth_feature.c

Lines changed: 21 additions & 10 deletions
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)