--
Best regards,
LIU Hao

From bf179ba7bde94cfed5ade73757523a887a8e4c5b Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mo...@126.com>
Date: Tue, 15 Jul 2025 10:32:42 +0800
Subject: [PATCH] crt: Check for compiler support for `no_stack_protector`

This commit contains two changes:

1. `no_stack_protector` is applied only when one of the `-fstack-protector*`
   options is in effect, for example, when someone builds the CRT with such
   an option in their `CFLAGS`.

2. When the attribute is required but is not supported by the compiler, it
   produces broken executables. Now in this an error is triggered.

Signed-off-by: LIU Hao <lh_mo...@126.com>
---
 mingw-w64-crt/ssp/stack_chk_guard.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-crt/ssp/stack_chk_guard.c 
b/mingw-w64-crt/ssp/stack_chk_guard.c
index 8f0ca8581..6a4d862cc 100644
--- a/mingw-w64-crt/ssp/stack_chk_guard.c
+++ b/mingw-w64-crt/ssp/stack_chk_guard.c
@@ -10,10 +10,18 @@
 
 void *__stack_chk_guard;
 
+#if defined __SSP__ || defined __SSP_STRONG__ || defined __SSP_ALL__
 // This function requires `no_stack_protector` because it changes the
 // value of `__stack_chk_guard`, causing stack checks to fail before
 // returning from this function.
-__attribute__((__constructor__, __no_stack_protector__))
+# if (defined __GNUC__ && __GNUC__ >= 11) || (defined __clang__ && 
__clang_major__ >= 7)
+__attribute__((__no_stack_protector__))
+# else
+#  error This file can not be built with stack protector enabled. Remove \
+         -fstack-protector* options from CFLAGS.
+# endif
+#endif
+__attribute__((__constructor__))
 static void __cdecl init(void)
 {
   unsigned int ui;
-- 
2.50.1

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to