tools/nolibc: fix segfaults on compilers without attribute no_stack_protector
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 24 May 2023 06:44:44 +0000 (08:44 +0200)
committerPaul E. McKenney <paulmck@kernel.org>
Fri, 9 Jun 2023 18:46:08 +0000 (11:46 -0700)
Not all compilers, notably GCC < 10, have support for
__attribute__((no_stack_protector)).
Fall back to a mechanism that also works there.

Tested with GCC 9.5.0 from kernel.org crosstools.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/arch-aarch64.h
tools/include/nolibc/arch-arm.h
tools/include/nolibc/arch-i386.h
tools/include/nolibc/arch-loongarch.h
tools/include/nolibc/arch-mips.h
tools/include/nolibc/arch-riscv.h
tools/include/nolibc/arch-x86_64.h
tools/include/nolibc/compiler.h
tools/include/nolibc/stackprotector.h

index 64ec65b4ee38dc9d5ba9dc02f286a8354d55a05e..11f294a406b7c64369cbe9e567a67341638dcf87 100644 (file)
@@ -175,7 +175,7 @@ char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
 /* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
index 924169522cf7051ecf6772bef9098648ab47ef47..45b89ffe8247c897faa4344b4255ecb2d5fd97ee 100644 (file)
@@ -202,7 +202,7 @@ char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
 /* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
index 37f813912957d4dd04e02d575247314bcdc0bd67..3d672d925e9e2603a555cb0d6739c44b22254ac1 100644 (file)
@@ -190,7 +190,7 @@ const unsigned long *_auxv __attribute__((weak));
  * 2) The deepest stack frame should be set to zero
  *
  */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
index d8ea7e787df4b1f8c170e303eef1fa0891946e2d..ad3f266e709303ba7c48862aa0c3cdac1c2dd9ba 100644 (file)
@@ -172,7 +172,7 @@ const unsigned long *_auxv __attribute__((weak));
 #endif
 
 /* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
index 9860236e5340b3859a8390ea32840f0737f78a50..db24e0837a39bfbb1ab002dbb923d0e21166bca1 100644 (file)
@@ -182,7 +182,7 @@ char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
 /* startup code, note that it's called __start on MIPS */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) __start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector __start(void)
 {
        __asm__ volatile (
                /*".set nomips16\n"*/
index 86616aeb77a0e9bff020dd70049f09ad12fb18f3..a2e8564e66d6a86a9cca400f0caa6eb1ace013b2 100644 (file)
@@ -180,7 +180,7 @@ char **environ __attribute__((weak));
 const unsigned long *_auxv __attribute__((weak));
 
 /* startup code */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
                ".option push\n"
index 485a7ff72a878887b3d635230bd94cf0d48485d2..6fc4d83927429da2e335b237e39c909430420dad 100644 (file)
@@ -190,7 +190,7 @@ const unsigned long *_auxv __attribute__((weak));
  * 2) The deepest stack frame should be zero (the %rbp).
  *
  */
-void __attribute__((weak,noreturn,optimize("omit-frame-pointer"),no_stack_protector)) _start(void)
+void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void)
 {
        __asm__ volatile (
 #ifdef _NOLIBC_STACKPROTECTOR
index 57da75cea799a8d744918d1917c0fa10a6e76bc5..beddc3665d690e9990920aaedcb5d9558415d2b3 100644 (file)
 
 #endif /* defined(__SSP__) ... */
 
+#if defined(__has_attribute)
+#  if __has_attribute(no_stack_protector)
+#    define __no_stack_protector __attribute__((no_stack_protector))
+#  else
+#    define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
+#  endif
+#else
+#  define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
+#endif /* defined(__has_attribute) */
+
 #endif /* _NOLIBC_COMPILER_H */
index 0a89e2b89ca614b16f9c15e2d702c5d025c93754..88f7b2d098ff706c642fb49fbd1abe56926b080f 100644 (file)
@@ -37,7 +37,7 @@ void __stack_chk_fail_local(void)
 __attribute__((weak,section(".data.nolibc_stack_chk")))
 uintptr_t __stack_chk_guard;
 
-__attribute__((weak,no_stack_protector,section(".text.nolibc_stack_chk")))
+__attribute__((weak,section(".text.nolibc_stack_chk"))) __no_stack_protector
 void __stack_chk_init(void)
 {
        my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);