[PATCH 1/1] Cygwin: Fix SEH and signal handling on AArch64

Igor Podgainoi Igor.Podgainoi@arm.com
Fri Mar 27 12:43:12 GMT 2026


This patch adds the SEH_CODE macro (defined in exception.h), allowing
a single EXCEPTION_HANDLER_DATA metadata definition to be used on both
AArch64 and x86_64 architectures.

It also fixes an issue related to stack replacement in _dll_crt0 that
impacts SEH and signal handling, where due to an epilogue optimization
on AArch64 the epilogue might appear before _main_tls->call. However,
after the stack replacement this optimization becomes broken.

This patch prevents an epilogue from appearing before _main_tls->call
on AArch64 by inserting a compiler barrier after _main_tls->call.

Tests fixed on AArch64:
winsup.api/mmaptest03.exe
winsup.api/shmtest.exe (partially)
winsup.api/ltp/mmap05.exe
winsup.api/ltp/munmap01.exe
winsup.api/ltp/munmap02.exe

Signed-off-by: Evgeny Karpov <evgeny.karpov@arm.com>
Signed-off-by: Igor Podgainoi <igor.podgainoi@arm.com>
---
 winsup/cygwin/dcrt0.cc                   |  6 ++++++
 winsup/cygwin/local_includes/exception.h | 13 ++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index e080aa41b..e7a837673 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -1065,6 +1065,12 @@ _dll_crt0 ()
   fesetenv (FE_DFL_ENV);
   _main_tls = &_my_tls;
   _main_tls->call ((DWORD (*) (void *, void *)) dll_crt0_1, NULL);
+#if defined(__aarch64__)
+  /* Add a compiler barrier to prevent the epilogue from appearing before
+     _main_tls->call. The epilogue optimization should not be applied after
+     the stack replacement.  */
+  __asm__ ("");
+#endif
 }
 
 void
diff --git a/winsup/cygwin/local_includes/exception.h b/winsup/cygwin/local_includes/exception.h
index b26f8ba17..fdc4d3e40 100644
--- a/winsup/cygwin/local_includes/exception.h
+++ b/winsup/cygwin/local_includes/exception.h
@@ -10,9 +10,13 @@ details. */
 
 #if defined (__aarch64__)
 #define EXCEPTION_HANDLE_REF "_ZN9exception6handleEP17_EXCEPTION_RECORDPvP8_CONTEXTP25_DISPATCHER_CONTEXT_ARM64"
-#define EXCEPTION_HANDLER_DATA
-#else
+/* An SEH directive that switches back to the code section.  */
+#define SEH_CODE ".text"
+#elif defined(__x86_64__)
 #define EXCEPTION_HANDLE_REF "_ZN9exception6handleEP17_EXCEPTION_RECORDPvP8_CONTEXTP19_DISPATCHER_CONTEXT"
+#define SEH_CODE ".seh_code"
+#endif
+
 #define EXCEPTION_HANDLER_DATA \
   asm volatile ("\n\
   1:									\n\
@@ -21,9 +25,8 @@ details. */
       @except								\n\
     .seh_handlerdata							\n\
     .long 1								\n\
-    .rva 1b, 2f, 2f, 2f							\n\
-    .seh_code								\n")
-#endif
+    .rva 1b, 2f, 2f, 2f							\n"\
+    SEH_CODE "								\n")
 
 class exception
 {
-- 
2.43.0


More information about the Cygwin-patches mailing list