Skip to content

Commit

Permalink
[clang][RISCV] Add Zicfilp CFI scheme preprocessor macros
Browse files Browse the repository at this point in the history
These macros allow assembly files to know which CFI label to use when the target
has Zicfilp enabled.
  • Loading branch information
mylai-mtk committed Sep 23, 2024
1 parent 71de8dc commit 0f9f86e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
else
Builder.defineMacro("__riscv_32e");
}

if (ISAInfo->hasExtension("zicfilp") && Opts.CFProtectionBranch) {
auto Scheme = Opts.getCFBranchLabelScheme();
if (Scheme == CFBranchLabelSchemeKind::Default)
Scheme = getDefaultCFBranchLabelScheme();

switch (Scheme) {
case CFBranchLabelSchemeKind::Unlabeled:
Builder.defineMacro("__riscv_landing_pad", "1");
Builder.defineMacro("__riscv_landing_pad_unlabeled", "1");
break;
case CFBranchLabelSchemeKind::Default:
llvm_unreachable("default cf-branch-label scheme should already be "
"transformed to other scheme");
case CFBranchLabelSchemeKind::FuncSig:
Builder.defineMacro("__riscv_landing_pad", "1");
Builder.defineMacro("__riscv_landing_pad_func_sig", "1");
break;
}
}
}

static constexpr Builtin::Info BuiltinInfo[] = {
Expand Down
32 changes: 32 additions & 0 deletions clang/test/Preprocessor/riscv-target-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -1781,3 +1781,35 @@
// RUN: %clang --target=riscv64-unknown-linux-gnu -mcpu=sifive-p450 -E -dM %s \
// RUN: -o - | FileCheck %s --check-prefix=CHECK-MISALIGNED-FAST
// CHECK-MISALIGNED-FAST: __riscv_misaligned_fast 1

// Landing Pad

// RUN: %clang --target=riscv32 -menable-experimental-extensions \
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \
// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s
// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}

// Default cf-branch-label-scheme is func-sig
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s

// RUN: %clang --target=riscv32 -menable-experimental-extensions \
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \
// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}

0 comments on commit 0f9f86e

Please sign in to comment.