Skip to content

Commit 0f9f86e

Browse files
committed
[clang][RISCV] Add Zicfilp CFI scheme preprocessor macros
These macros allow assembly files to know which CFI label to use when the target has Zicfilp enabled.
1 parent 71de8dc commit 0f9f86e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,26 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
224224
else
225225
Builder.defineMacro("__riscv_32e");
226226
}
227+
228+
if (ISAInfo->hasExtension("zicfilp") && Opts.CFProtectionBranch) {
229+
auto Scheme = Opts.getCFBranchLabelScheme();
230+
if (Scheme == CFBranchLabelSchemeKind::Default)
231+
Scheme = getDefaultCFBranchLabelScheme();
232+
233+
switch (Scheme) {
234+
case CFBranchLabelSchemeKind::Unlabeled:
235+
Builder.defineMacro("__riscv_landing_pad", "1");
236+
Builder.defineMacro("__riscv_landing_pad_unlabeled", "1");
237+
break;
238+
case CFBranchLabelSchemeKind::Default:
239+
llvm_unreachable("default cf-branch-label scheme should already be "
240+
"transformed to other scheme");
241+
case CFBranchLabelSchemeKind::FuncSig:
242+
Builder.defineMacro("__riscv_landing_pad", "1");
243+
Builder.defineMacro("__riscv_landing_pad_func_sig", "1");
244+
break;
245+
}
246+
}
227247
}
228248

229249
static constexpr Builtin::Info BuiltinInfo[] = {

clang/test/Preprocessor/riscv-target-features.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,3 +1781,35 @@
17811781
// RUN: %clang --target=riscv64-unknown-linux-gnu -mcpu=sifive-p450 -E -dM %s \
17821782
// RUN: -o - | FileCheck %s --check-prefix=CHECK-MISALIGNED-FAST
17831783
// CHECK-MISALIGNED-FAST: __riscv_misaligned_fast 1
1784+
1785+
// Landing Pad
1786+
1787+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1788+
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
1789+
// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \
1790+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s
1791+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1792+
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \
1793+
// RUN: -fcf-branch-label-scheme=unlabeled -E -dM %s -o - \
1794+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s
1795+
// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
1796+
// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}
1797+
1798+
// Default cf-branch-label-scheme is func-sig
1799+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1800+
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \
1801+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
1802+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1803+
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \
1804+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
1805+
1806+
// RUN: %clang --target=riscv32 -menable-experimental-extensions \
1807+
// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
1808+
// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \
1809+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
1810+
// RUN: %clang --target=riscv64 -menable-experimental-extensions \
1811+
// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \
1812+
// RUN: -fcf-branch-label-scheme=func-sig -E -dM %s -o - \
1813+
// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s
1814+
// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
1815+
// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}

0 commit comments

Comments
 (0)