diff mbox series

[2/6] tools/nolibc: riscv: add stackprotector support

Message ID 20230408-nolibc-stackprotector-archs-v1-2-271f5c859c71@weissschuh.net
State Superseded
Headers show
Series tools/nolibc: add stackprotector support for more architectures | expand

Commit Message

Thomas Weißschuh April 17, 2023, 4:01 p.m. UTC
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/arch-riscv.h       | 7 ++++++-
 tools/testing/selftests/nolibc/Makefile | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

Comments

Palmer Dabbelt April 17, 2023, 7:35 p.m. UTC | #1
On Mon, 17 Apr 2023 09:01:32 PDT (-0700), linux@weissschuh.net wrote:
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  tools/include/nolibc/arch-riscv.h       | 7 ++++++-
>  tools/testing/selftests/nolibc/Makefile | 1 +
>  2 files changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h
> index 0d5f15fdedc4..b2ccffcc079f 100644
> --- a/tools/include/nolibc/arch-riscv.h
> +++ b/tools/include/nolibc/arch-riscv.h
> @@ -173,14 +173,19 @@ struct sys_stat_struct {
>  char **environ __attribute__((weak));
>  const unsigned long *_auxv __attribute__((weak));
>
> +#define __ARCH_SUPPORTS_STACK_PROTECTOR
> +
>  /* startup code */
> -void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)
> +void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
>  {
>  	__asm__ volatile (
>  		".option push\n"
>  		".option norelax\n"
>  		"lla   gp, __global_pointer$\n"
>  		".option pop\n"
> +#ifdef NOLIBC_STACKPROTECTOR
> +		"call __stack_chk_init\n"    /* initialize stack protector                          */
> +#endif
>  		"lw    a0, 0(sp)\n"          /* argc (a0) was in the stack                          */
>  		"add   a1, sp, "SZREG"\n"    /* argv (a1) = sp                                      */
>  		"slli  a2, a0, "PTRLOG"\n"   /* envp (a2) = SZREG*argc ...                          */
> diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
> index 3c8e3a6f8985..0a83ad388a16 100644
> --- a/tools/testing/selftests/nolibc/Makefile
> +++ b/tools/testing/selftests/nolibc/Makefile
> @@ -82,6 +82,7 @@ CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \
>  CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR)
>  CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR)
>  CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR)
> +CFLAGS_STKP_riscv = $(CFLAGS_STACKPROTECTOR)
>  CFLAGS_s390 = -m64
>  CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
>  		$(call cc-option,-fno-stack-protector) \

Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
diff mbox series

Patch

diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h
index 0d5f15fdedc4..b2ccffcc079f 100644
--- a/tools/include/nolibc/arch-riscv.h
+++ b/tools/include/nolibc/arch-riscv.h
@@ -173,14 +173,19 @@  struct sys_stat_struct {
 char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
+#define __ARCH_SUPPORTS_STACK_PROTECTOR
+
 /* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
 {
 	__asm__ volatile (
 		".option push\n"
 		".option norelax\n"
 		"lla   gp, __global_pointer$\n"
 		".option pop\n"
+#ifdef NOLIBC_STACKPROTECTOR
+		"call __stack_chk_init\n"    /* initialize stack protector                          */
+#endif
 		"lw    a0, 0(sp)\n"          /* argc (a0) was in the stack                          */
 		"add   a1, sp, "SZREG"\n"    /* argv (a1) = sp                                      */
 		"slli  a2, a0, "PTRLOG"\n"   /* envp (a2) = SZREG*argc ...                          */
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 3c8e3a6f8985..0a83ad388a16 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -82,6 +82,7 @@  CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \
 CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR)
 CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR)
 CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR)
+CFLAGS_STKP_riscv = $(CFLAGS_STACKPROTECTOR)
 CFLAGS_s390 = -m64
 CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
 		$(call cc-option,-fno-stack-protector) \