/* Define architecture extension test macros. */
builtin_define_with_int_value ("__riscv_arch_test", 1);
+ if (TARGET_ZICFISS && ((flag_cf_protection & CF_RETURN) == CF_RETURN))
+ builtin_define ("__riscv_shadow_stack");
+
+ if (TARGET_ZICFILP && ((flag_cf_protection & CF_BRANCH) == CF_BRANCH))
+ {
+ builtin_define ("__riscv_landing_pad");
+ builtin_define ("__riscv_landing_pad_unlabeled");
+ }
+
const riscv_subset_list *subset_list = riscv_cmdline_subset_list ();
if (!subset_list)
return;
/* opt_pass methods: */
virtual bool gate (function *)
{
- return TARGET_ZICFILP;
+ return is_zicfilp_p ();
}
virtual unsigned int execute (function *)
rtx reg = RISCV_CALL_ADDRESS_TEMP (Pmode);
riscv_emit_move (reg, addr);
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
{
rtx sw_guarded = RISCV_CALL_ADDRESS_LPAD (Pmode);
emit_insn (gen_set_guarded (Pmode, reg));
return reg;
}
- if (TARGET_ZICFILP && REG_P (addr))
+ if (is_zicfilp_p () && REG_P (addr))
emit_insn (gen_set_lpl (Pmode, const0_rtx));
return addr;
if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM)
return false;
- if (regno == RETURN_ADDR_REGNUM && TARGET_ZICFISS)
+ if (regno == RETURN_ADDR_REGNUM && is_zicfiss_p ())
return true;
/* We must save every register used in this function. If this is not a
long GNU_PROPERTY_RISCV_FEATURE_1_AND = 0;
unsigned long feature_1_and = 0;
- if (TARGET_ZICFISS)
+ if (is_zicfilp_p ())
feature_1_and |= 0x1 << 0;
- if (TARGET_ZICFILP)
+ if (is_zicfiss_p ())
feature_1_and |= 0x1 << 1;
if (feature_1_and)
/* Mark the end of the (empty) prologue. */
emit_note (NOTE_INSN_PROLOGUE_END);
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
emit_insn(gen_lpad (const0_rtx));
/* Determine if we can use a sibcall to call FUNCTION directly. */
/* Convert -march and -mrvv-vector-bits to a chunks count. */
riscv_vector_chunks = riscv_convert_vector_chunks (opts);
+
+ if (opts->x_flag_cf_protection != CF_NONE)
+ {
+ if ((opts->x_flag_cf_protection & CF_RETURN) == CF_RETURN
+ && !TARGET_ZICFISS)
+ error ("%<-fcf-protection%> is not compatible with this target");
+
+ if ((opts->x_flag_cf_protection & CF_BRANCH) == CF_BRANCH
+ && !TARGET_ZICFILP)
+ error ("%<-fcf-protection%> is not compatible with this target");
+
+ opts->x_flag_cf_protection
+ = (cf_protection_level) (opts->x_flag_cf_protection | CF_SET);
+ }
}
/* Implement TARGET_OPTION_OVERRIDE. */
/* Work out the offsets of the pointers from the start of the
trampoline code. */
- if (!TARGET_ZICFILP)
+ if (!is_zicfilp_p ())
gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
else
gcc_assert (ARRAY_SIZE (trampoline_cfi) * 4 == TRAMPOLINE_CODE_SIZE);
unsigned insn_count = 0;
/* Insert lpad, if zicfilp is enabled. */
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
{
unsigned HOST_WIDE_INT lpad_code;
lpad_code = OPCODE_AUIPC | (0 << SHIFT_RD) | (lp_value << IMM_BITS);
insn_count++;
/* For zicfilp only, insert lui t2, 1, because use jr t0. */
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
{
unsigned HOST_WIDE_INT set_lpl_code;
set_lpl_code = OPCODE_LUI
static_chain_offset = TRAMPOLINE_CODE_SIZE;
target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
- if (!TARGET_ZICFILP)
+ if (!is_zicfilp_p ())
{
/* auipc t2, 0
l[wd] t0, (target_function_offset)(t2)
riscv_emit_move (operands[0], gen_lowpart (crc_mode, a0));
}
+bool is_zicfiss_p ()
+{
+ if (TARGET_ZICFISS && (flag_cf_protection & CF_RETURN))
+ return true;
+
+ return false;
+}
+
+bool is_zicfilp_p ()
+{
+ if (TARGET_ZICFILP && (flag_cf_protection & CF_BRANCH))
+ return true;
+
+ return false;
+}
+
bool need_shadow_stack_push_pop_p ()
{
- return TARGET_ZICFISS && riscv_save_return_addr_reg_p ();
+ return is_zicfiss_p () && riscv_save_return_addr_reg_p ();
}
/* Initialize the GCC target structure. */
/* Allocation boundary (in *bits*) for the code of a function. */
#define FUNCTION_BOUNDARY \
- (((TARGET_RVC || TARGET_ZCA) && !TARGET_ZICFILP) ? 16 : 32)
+ (((TARGET_RVC || TARGET_ZCA) && !is_zicfilp_p ()) ? 16 : 32)
/* The smallest supported stack boundary the calling convention supports. */
#define STACK_BOUNDARY \
/* Register in which static-chain is passed to a function. */
#define STATIC_CHAIN_REGNUM \
- ((TARGET_ZICFILP) ? (GP_TEMP_FIRST + 23) : (GP_TEMP_FIRST + 2))
+ ((is_zicfilp_p ()) ? (GP_TEMP_FIRST + 23) : (GP_TEMP_FIRST + 2))
/* Registers used as temporaries in prologue/epilogue code.
/* Trampolines are a block of code followed by two pointers. */
-#define TRAMPOLINE_CODE_SIZE ((TARGET_ZICFILP) ? 24 : 16)
+#define TRAMPOLINE_CODE_SIZE ((is_zicfilp_p ()) ? 24 : 16)
#define TRAMPOLINE_SIZE \
((Pmode == SImode) \
extern poly_int64 riscv_v_adjust_nunits (machine_mode, bool, int, int);
extern poly_int64 riscv_v_adjust_precision (enum machine_mode, int);
extern poly_int64 riscv_v_adjust_bytesize (enum machine_mode, int);
+extern bool is_zicfiss_p ();
+extern bool is_zicfilp_p ();
extern bool need_shadow_stack_push_pop_p ();
/* The number of bits and bytes in a RVV vector. */
#define BITS_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks * riscv_bytes_per_vector_chunk * 8))
[(set (pc) (match_operand 0 "register_operand"))]
""
{
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
emit_insn (gen_set_lpl (Pmode, const0_rtx));
operands[0] = force_reg (Pmode, operands[0]);
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
emit_use (gen_rtx_REG (Pmode, T2_REGNUM));
if (Pmode == SImode)
gen_rtx_LABEL_REF (Pmode, operands[1]),
NULL_RTX, 0, OPTAB_DIRECT);
- if (TARGET_ZICFILP)
+ if (is_zicfilp_p ())
{
rtx t2 = RISCV_CALL_ADDRESS_LPAD (GET_MODE (operands[0]));
emit_move_insn (t2, operands[0]);
(define_insn "tablejump<mode>"
[(set (pc) (match_operand:GPR 0 "register_operand" "l"))
(use (label_ref (match_operand 1 "" "")))]
- "!TARGET_ZICFILP"
+ "!is_zicfilp_p ()"
"jr\t%0"
[(set_attr "type" "jalr")
(set_attr "mode" "none")])
(define_insn "tablejump_cfi<mode>"
[(set (pc) (reg:GPR T2_REGNUM))
(use (label_ref (match_operand 0 "")))]
- "TARGET_ZICFILP"
+ "is_zicfilp_p ()"
"jr\tt2"
[(set_attr "type" "jalr")
(set_attr "mode" "none")])
/* { dg-do compile } */
/* { dg-options "-fcf-protection=full" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=full' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile } */
/* { dg-options "-fcf-protection=branch" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=branch' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile } */
/* { dg-options "-fcf-protection=return" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=return' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-options "-fcf-protection=none" } */
/* { dg-do compile } */
/* { dg-options "-fcf-protection" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=full' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile } */
/* { dg-options "-fcf-protection=branch" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=branch' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile } */
/* { dg-options "-fcf-protection=return" } */
+/* { dg-skip-if "" { "riscv*-*-*" } } */
/* { dg-error "'-fcf-protection=return' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */
/* { dg-do compile { target { riscv64*-*-* } } } */
-/* { dg-options "-march=rv64gc_zicfilp -mabi=lp64d" } */
+/* { dg-options "-march=rv64gc_zicfilp -mabi=lp64d -fcf-protection=branch" } */
void __attribute__ ((interrupt))
foo (void)
{
/* { dg-do compile { target { riscv64*-*-* } } } */
-/* { dg-options "-O2 -march=rv64gc_zicfiss -mabi=lp64d" } */
+/* { dg-options "-O2 -march=rv64gc_zicfiss -mabi=lp64d -fcf-protection=return" } */
/* { dg-skip-if "" { *-*-* } { "-O0" } } */
struct ad {
void *ae;
/* { dg-do compile { target { riscv64*-*-* } } } */
-/* { dg-options "-O0 -march=rv64gc_zicfiss -mabi=lp64d" } */
+/* { dg-options "-O0 -march=rv64gc_zicfiss -mabi=lp64d -fcf-protection=return" } */
void __attribute__ ((interrupt))
foo (void)
/* { dg-do compile { target { riscv64*-*-* } } } */
-/* { dg-options "-O2 -fPIE -march=rv64gc_zicfilp -mabi=lp64d" } */
+/* { dg-options "-O2 -fPIE -march=rv64gc_zicfilp -mabi=lp64d -fcf-protection=branch" } */
/* { dg-skip-if "" { *-*-* } { "-O0" } } */
extern void _dl_find_object_init (void);