[committed,2/3] RISC-V: add GNU property notes to assembly routines
Commit Message
From: Jesse Huang <jesse.huang@sifive.com>
Emit a NT_GNU_PROPERTY_TYPE_0 note from sys/asm.h and
libgloss/riscv/asm.h that advertises the control-flow integrity
features (landing pad and shadow stack) supported by the assembly
routines. Every object that includes these headers is then marked
CFI-enabled when built with -fcf-protection=[full|branch|return],
so the linker keeps the feature enabled for the whole image.
---
libgloss/riscv/asm.h | 53 +++++++++++++++++++++++++++++
newlib/libc/machine/riscv/sys/asm.h | 53 +++++++++++++++++++++++++++++
2 files changed, 106 insertions(+)
@@ -81,4 +81,57 @@ name:
.type name,@function; \
LPAD
+#define FEATURE_1_AND 0xc0000000
+/* Add a NT_GNU_PROPERTY_TYPE_0 note. */
+#if __riscv_xlen == 32
+# define GNU_PROPERTY(type, value) \
+ .pushsection .note.gnu.property, "a"; \
+ .p2align 2; \
+ .word 4; \
+ .word 12; \
+ .word 5; \
+ .asciz "GNU"; \
+ .word type; \
+ .word 4; \
+ .word value; \
+ .popsection;
+#else
+# define GNU_PROPERTY(type, value) \
+ .pushsection .note.gnu.property, "a"; \
+ .p2align 3; \
+ .word 4; \
+ .word 16; \
+ .word 5; \
+ .asciz "GNU"; \
+ .word type; \
+ .word 4; \
+ .word value; \
+ .word 0; \
+ .popsection;
+#endif
+
+/* Add GNU property note with the supported features to all asm code
+ where asm.h is included. */
+#undef __VALUE_FOR_FEATURE_1_AND
+#if defined (__riscv_landing_pad) || defined (__riscv_shadow_stack)
+# if defined (__riscv_landing_pad)
+# if defined (__riscv_shadow_stack)
+# define __VALUE_FOR_FEATURE_1_AND 0x3
+# else
+# define __VALUE_FOR_FEATURE_1_AND 0x1
+# endif
+# else
+# if defined (__riscv_shadow_stack)
+# define __VALUE_FOR_FEATURE_1_AND 0x2
+# else
+# error "What?"
+# endif
+# endif
+#endif
+
+#if defined (__VALUE_FOR_FEATURE_1_AND) && defined (__ASSEMBLER__)
+GNU_PROPERTY (FEATURE_1_AND, __VALUE_FOR_FEATURE_1_AND)
+#endif
+#undef __VALUE_FOR_FEATURE_1_AND
+
#endif /* _ASM_H */
@@ -81,4 +81,57 @@ name:
.type name,@function; \
LPAD
+#define FEATURE_1_AND 0xc0000000
+/* Add a NT_GNU_PROPERTY_TYPE_0 note. */
+#if __riscv_xlen == 32
+# define GNU_PROPERTY(type, value) \
+ .pushsection .note.gnu.property, "a"; \
+ .p2align 2; \
+ .word 4; \
+ .word 12; \
+ .word 5; \
+ .asciz "GNU"; \
+ .word type; \
+ .word 4; \
+ .word value; \
+ .popsection;
+#else
+# define GNU_PROPERTY(type, value) \
+ .pushsection .note.gnu.property, "a"; \
+ .p2align 3; \
+ .word 4; \
+ .word 16; \
+ .word 5; \
+ .asciz "GNU"; \
+ .word type; \
+ .word 4; \
+ .word value; \
+ .word 0; \
+ .popsection;
+#endif
+
+/* Add GNU property note with the supported features to all asm code
+ where asm.h is included. */
+#undef __VALUE_FOR_FEATURE_1_AND
+#if defined (__riscv_landing_pad) || defined (__riscv_shadow_stack)
+# if defined (__riscv_landing_pad)
+# if defined (__riscv_shadow_stack)
+# define __VALUE_FOR_FEATURE_1_AND 0x3
+# else
+# define __VALUE_FOR_FEATURE_1_AND 0x1
+# endif
+# else
+# if defined (__riscv_shadow_stack)
+# define __VALUE_FOR_FEATURE_1_AND 0x2
+# else
+# error "What?"
+# endif
+# endif
+#endif
+
+#if defined (__VALUE_FOR_FEATURE_1_AND) && defined (__ASSEMBLER__)
+GNU_PROPERTY (FEATURE_1_AND, __VALUE_FOR_FEATURE_1_AND)
+#endif
+#undef __VALUE_FOR_FEATURE_1_AND
+
#endif /* sys/asm.h */