Skip to content

Commit c579ec6

Browse files
authored
[Clang][AArch64] Add support for SHF_AARCH64_PURECODE ELF section flag (2/3) (#125688)
Add support for the new SHF_AARCH64_PURECODE ELF section flag: ARM-software/abi-aa#304 The general implementation follows the existing one for ARM targets. Simlarly to ARM targets, generating object files with the `SHF_AARCH64_PURECODE` flag set is enabled by the `-mexecute-only`/`-mpure-code` driver flag. Related PRs: * LLVM: #125687 * LLD: #125689
1 parent 564b756 commit c579ec6

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4725,9 +4725,9 @@ def mno_long_calls : Flag<["-"], "mno-long-calls">, Group<m_Group>,
47254725
HelpText<"Restore the default behaviour of not generating long calls">;
47264726
} // let Flags = [TargetSpecific]
47274727
def mexecute_only : Flag<["-"], "mexecute-only">, Group<m_arm_Features_Group>,
4728-
HelpText<"Disallow generation of data access to code sections (ARM only)">;
4728+
HelpText<"Disallow generation of data access to code sections (AArch64/ARM only)">;
47294729
def mno_execute_only : Flag<["-"], "mno-execute-only">, Group<m_arm_Features_Group>,
4730-
HelpText<"Allow generation of data access to code sections (ARM only)">;
4730+
HelpText<"Allow generation of data access to code sections (AArch64/ARM only)">;
47314731
let Flags = [TargetSpecific] in {
47324732
def mtp_mode_EQ : Joined<["-"], "mtp=">, Group<m_arm_Features_Group>, Values<"soft,cp15,tpidrurw,tpidruro,tpidrprw,el0,el1,el2,el3,tpidr_el0,tpidr_el1,tpidr_el2,tpidr_el3,tpidrro_el0,auto">,
47334733
HelpText<"Thread pointer access method. "

clang/lib/Driver/ToolChain.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ static void getAArch64MultilibFlags(const Driver &D,
210210
const llvm::opt::ArgList &Args,
211211
Multilib::flags_list &Result) {
212212
std::vector<StringRef> Features;
213-
tools::aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, false);
213+
tools::aarch64::getAArch64TargetFeatures(D, Triple, Args, Features,
214+
/*ForAS=*/false,
215+
/*ForMultilib=*/true);
214216
const auto UnifiedFeatures = tools::unifyTargetFeatures(Features);
215217
llvm::DenseSet<StringRef> FeatureSet(UnifiedFeatures.begin(),
216218
UnifiedFeatures.end());

clang/lib/Driver/ToolChains/Arch/AArch64.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
196196
const llvm::Triple &Triple,
197197
const ArgList &Args,
198198
std::vector<StringRef> &Features,
199-
bool ForAS) {
199+
bool ForAS, bool ForMultilib) {
200200
Arg *A;
201201
bool success = true;
202202
llvm::StringRef WaMArch;
@@ -332,6 +332,19 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
332332
} else if (Triple.isOSOpenBSD())
333333
Features.push_back("+strict-align");
334334

335+
// Generate execute-only output (no data access to code sections).
336+
// This only makes sense for the compiler, not for the assembler.
337+
// It's not needed for multilib selection and may hide an unused
338+
// argument diagnostic if the code is always run.
339+
if (!ForAS && !ForMultilib) {
340+
if (Arg *A = Args.getLastArg(options::OPT_mexecute_only,
341+
options::OPT_mno_execute_only)) {
342+
if (A->getOption().matches(options::OPT_mexecute_only)) {
343+
Features.push_back("+execute-only");
344+
}
345+
}
346+
}
347+
335348
if (Args.hasArg(options::OPT_ffixed_x1))
336349
Features.push_back("+reserve-x1");
337350

clang/lib/Driver/ToolChains/Arch/AArch64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace aarch64 {
2323
void getAArch64TargetFeatures(const Driver &D, const llvm::Triple &Triple,
2424
const llvm::opt::ArgList &Args,
2525
std::vector<llvm::StringRef> &Features,
26-
bool ForAS);
26+
bool ForAS, bool ForMultilib = false);
2727

2828
std::string getAArch64TargetCPU(const llvm::opt::ArgList &Args,
2929
const llvm::Triple &Triple, llvm::opt::Arg *&A);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang -target aarch64 -### %s 2>&1 | \
2+
// RUN: FileCheck %s -check-prefix CHECK-NO-EXECUTE-ONLY
3+
// RUN: %clang -target aarch64 -### -mexecute-only %s 2>&1 | \
4+
// RUN: FileCheck %s -check-prefix CHECK-EXECUTE-ONLY
5+
// RUN: %clang -target aarch64 -### -mexecute-only -mno-execute-only %s 2>&1 | \
6+
// RUN: FileCheck %s -check-prefix CHECK-NO-EXECUTE-ONLY
7+
8+
/// -mpure-code flag for GCC compatibility
9+
// RUN: %clang -target aarch64 -### %s 2>&1 | \
10+
// RUN: FileCheck %s -check-prefix CHECK-NO-EXECUTE-ONLY
11+
// RUN: %clang -target aarch64 -### -mpure-code %s 2>&1 | \
12+
// RUN: FileCheck %s -check-prefix CHECK-EXECUTE-ONLY
13+
// RUN: %clang -target aarch64 -### -mpure-code -mno-pure-code %s 2>&1 | \
14+
// RUN: FileCheck %s -check-prefix CHECK-NO-EXECUTE-ONLY
15+
16+
// CHECK-NO-EXECUTE-ONLY-NOT: "+execute-only"
17+
// CHECK-EXECUTE-ONLY: "+execute-only"
18+
19+
void a() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang --sysroot=%S/Inputs -c -fdriver-only -Werror --target=aarch64-unknown-linux-gnu \
2+
// RUN: -mexecute-only %s 2>&1 | count 0
3+
4+
// RUN: %clang -### --target=aarch64-unknown-linux-gnu -x assembler -mexecute-only %s -c -### 2>&1 | \
5+
// RUN: FileCheck %s --check-prefix=CHECK-NO-EXECUTE-ONLY-ASM
6+
// RUN: %clang -### --multi-lib-config=%S/Inputs/multilib/empty.yaml --sysroot= \
7+
// RUN: --target=aarch64-none-elf -x assembler -mexecute-only %s -c -### 2>&1 | \
8+
// RUN: FileCheck %s --check-prefix=CHECK-NO-EXECUTE-ONLY-ASM
9+
// CHECK-NO-EXECUTE-ONLY-ASM: warning: argument unused during compilation: '-mexecute-only'

clang/test/Driver/fsanitize.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,21 +1053,26 @@
10531053
// RUN: not %clang --target=x86_64-sie-ps5 -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-TARGET
10541054
// RUN: not %clang --target=x86_64-sie-ps5 -fsanitize=function -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-TARGET --check-prefix=CHECK-UBSAN-FUNCTION-TARGET
10551055
// RUN: %clang --target=x86_64-sie-ps5 -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED
1056-
// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
10571056

10581057
// RUN: not %clang --target=armv6t2-eabi -mexecute-only -fsanitize=function %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION-MEXECUTE-ONLY
10591058
// RUN: not %clang --target=armv6t2-eabi -mpure-code -fsanitize=function %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION-MPURE-CODE
10601059
// RUN: not %clang --target=armv6t2-eabi -mexecute-only -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-MEXECUTE-ONLY
10611060
// RUN: not %clang --target=armv6t2-eabi -mpure-code -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-MPURE-CODE
1062-
// RUN: %clang --target=armv6t2-eabi -mexecute-only -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED-VPTR
1061+
// RUN: %clang --target=armv6t2-eabi -mexecute-only -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED
1062+
1063+
// RUN: not %clang --target=aarch64 -mexecute-only -fsanitize=function %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION-MEXECUTE-ONLY
1064+
// RUN: not %clang --target=aarch64 -mpure-code -fsanitize=function %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION-MPURE-CODE
1065+
// RUN: not %clang --target=aarch64 -mexecute-only -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-MEXECUTE-ONLY
1066+
// RUN: not %clang --target=aarch64 -mpure-code -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI-MPURE-CODE
1067+
// RUN: %clang --target=aarch64 -mexecute-only -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED
10631068

10641069
// CHECK-UBSAN-KCFI-TARGET-DAG: error: unsupported option '-fsanitize=kcfi' for target 'x86_64-sie-ps5'
10651070
// CHECK-UBSAN-KCFI-MEXECUTE-ONLY-DAG: error: invalid argument '-fsanitize=kcfi' not allowed with '-mexecute-only'
10661071
// CHECK-UBSAN-KCFI-MPURE-CODE-DAG: error: invalid argument '-fsanitize=kcfi' not allowed with '-mpure-code'
10671072
// CHECK-UBSAN-FUNCTION-TARGET-DAG: error: unsupported option '-fsanitize=function' for target 'x86_64-sie-ps5'
10681073
// CHECK-UBSAN-FUNCTION-MEXECUTE-ONLY-DAG: error: invalid argument '-fsanitize=function' not allowed with '-mexecute-only'
10691074
// CHECK-UBSAN-FUNCTION-MPURE-CODE-DAG: error: invalid argument '-fsanitize=function' not allowed with '-mpure-code'
1070-
// CHECK-UBSAN-UNDEFINED-VPTR: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
1075+
// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
10711076

10721077
// * Test BareMetal toolchain sanitizer support *
10731078

0 commit comments

Comments
 (0)