[committed,2/3] RISC-V: add GNU property notes to assembly routines

Message ID 20260622105202.664207-2-kito.cheng@sifive.com
State New
Headers
Series [committed,1/3] RISC-V: introduce ENTRY/END assembler macros for function boilerplate |

Commit Message

Kito Cheng June 22, 2026, 10:52 a.m. UTC
  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(+)
  

Patch

diff --git a/libgloss/riscv/asm.h b/libgloss/riscv/asm.h
index a9190b1f4..8fe3bbcfa 100644
--- a/libgloss/riscv/asm.h
+++ b/libgloss/riscv/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 /* _ASM_H */
diff --git a/newlib/libc/machine/riscv/sys/asm.h b/newlib/libc/machine/riscv/sys/asm.h
index 0985d05bf..0b1893bf9 100644
--- a/newlib/libc/machine/riscv/sys/asm.h
+++ b/newlib/libc/machine/riscv/sys/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 */