crt/ssp: fix stack smashing protection bootstrapping
Fix stack protection bootstrapping issue where the guard initialization
function itself triggers false positive stack overflow detection.
This issue occurs when the mingw-w64 CRT library is built with GCC's
-fstack-protector-strong flag, which effectively renders any resulting
binary (whether statically or dynamically linked) corrupted. The final
binary crashes during startup, before it even reaches the main function.
The problem is that the init() function starts with __stack_chk_guard = 0
but initializes it to a random value, causing the stack protection check
to always fail (0 != random) at function exit.
By introducing attribute __no_stack_protector__ we prevent early testing
of the uninitialized guard value.
Signed-off-by: Igor Kostenko <work.kerogi@gmail.com>
diff --git a/mingw-w64-crt/ssp/stack_chk_guard.c b/mingw-w64-crt/ssp/stack_chk_guard.c
index 3ff22e0..be43d8c 100644
--- a/mingw-w64-crt/ssp/stack_chk_guard.c
+++ b/mingw-w64-crt/ssp/stack_chk_guard.c
@@ -10,7 +10,11 @@
void *__stack_chk_guard;
-static void __cdecl __attribute__((__constructor__)) init(void)
+// 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__))
+static void __cdecl init(void)
{
unsigned int ui;
if (__stack_chk_guard != 0)