Hello, I have addressed all found issues with setjmp/longjmp functions
and in the attachment I'm sending a new version of all changes (this
replaces all previous setjmp/longjmp changes).

Kirill run CI testing on github:
https://github.com/maiddaisuki/mingw-w64/actions/runs/40458503497

We have enabled also testing against ucrtapp and we figured out that
there are failing more complex math tests for clang+ucrtapp+i686/x86_64
combination (for all other combinations tests are passing). And these
complex math tests are failing also without these changes.

During development more setjmp/longjmp tests were failing on arm64ec
(with all libs) and with ucrtapp (on all arch). I think that I have
addressed all setjmp/longjmp issues in ucrtapp and I have tried to
address also arm64ec issues. But as I do not have arm64ec testing env,
I have just written the arm64ec code blindly and let the github CI to
run it if it pass. So I would kindly ask if you could review the arm64ec
changes more deeply.


Note that there is one missing thing for i386 setjmp/longjmp: The
_setjmp3 CRT function is taking the trylevel parameter and pointer to
unwind function. Both of these parameters are required when calling the
setjmp from the function which register itself its own SEH handler which
also handles __try / __except blocks. gcc does not support __try /
__except keywords, so does not have to care about these in-between
try/except jumps. But clang supports __try / __except blocks, so calling
setjmp() inside such function still will be SEH-unsafe (as before).
To fix this issue and make the setjmp/longjmp fully SEH-safe on i686
clang, it is needed cooperation with compiler. More precisely getting
access to the current "trylevel" on the called place and pointer to
"unwind" function specific just for that function.

If somebody wants to follow and fix this remaining i686 setjmp issue, it
would be nice to have builtins from compiler for it. And then the 32-bit
x86 setjmp() macro could be extended to call those builtins to properly
call the _setjmp3 CRT function.
>From eff0c6ad7b724d2755721d98e815ffd4cf36958d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 27 Dec 2025 15:25:07 +0100
Subject: [PATCH 01/25] crt: Fix mingw-w64 longjmp() implementation when
 calling with value 0

ISO C for longjmp says:
The longjmp function cannot cause the setjmp macro to return the value 0;
if val is 0, the setjmp macro returns the value 1.

Add missing handling of value 0.
---
 mingw-w64-crt/misc/longjmp.S       | 10 +++++++++-
 mingw-w64-crt/misc/mingw_longjmp.S |  6 +++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/misc/longjmp.S b/mingw-w64-crt/misc/longjmp.S
index b5f81cffc0f0..a7f04d529b7f 100644
--- a/mingw-w64-crt/misc/longjmp.S
+++ b/mingw-w64-crt/misc/longjmp.S
@@ -14,6 +14,8 @@ __MINGW_USYMBOL(longjmp):
 #if defined(__i386__)
         movl    4(%esp),%ecx         /* jmp_buf */
         movl    8(%esp),%eax         /* retval */
+        cmpl    $1,%eax              /* check that retval is non-zero */
+        adcl    $0,%eax              /* if not then increase by one via CF */
         movl    0(%ecx),%ebp         /* jmp_buf.Ebp */
         movl    4(%ecx),%ebx         /* jmp_buf.Ebx */
         movl    8(%ecx),%edi         /* jmp_buf.Edi */
@@ -23,6 +25,8 @@ __MINGW_USYMBOL(longjmp):
         jmp     *20(%ecx)            /* jmp_buf.Eip */
 #elif defined(__x86_64__)
         movq    %rdx,%rax            /* retval */
+        cmpq    $1,%rax              /* check that retval is non-zero */
+        adcq    $0,%rax              /* if not then increase by one via CF */
         movq    0x8(%rcx),%rbx       /* jmp_buf->Rbx */
         movq    0x18(%rcx),%rbp      /* jmp_buf->Rbp */
         movq    0x20(%rcx),%rsi      /* jmp_buf->Rsi */
@@ -69,6 +73,9 @@ __MINGW_USYMBOL(longjmp):
         vldr    d14, [r0, #0x60]     /* jmp_buf->D[6] */
         vldr    d15, [r0, #0x68]     /* jmp_buf->D[7] */
         mov     r0,  r1              /* retval */
+        cmp     r1,  #1
+        it      ls
+        movls   r0,  #1
         bx      r2
 #elif defined(__aarch64__)
         ldp     x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */
@@ -87,7 +94,8 @@ __MINGW_USYMBOL(longjmp):
         ldp     d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */
         ldp     d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */
         ldp     d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */
-        mov     x0,  x1               /* retval */
+        cmp     x1,  #1
+        csinc   x0,  x1,  xzr,  hi    /* retval */
         ret
 #endif
 
diff --git a/mingw-w64-crt/misc/mingw_longjmp.S 
b/mingw-w64-crt/misc/mingw_longjmp.S
index bec0ae157a29..5863f0d88652 100644
--- a/mingw-w64-crt/misc/mingw_longjmp.S
+++ b/mingw-w64-crt/misc/mingw_longjmp.S
@@ -81,6 +81,9 @@ __MINGW_USYMBOL(__mingw_longjmp):
        vldr    d14, [r0, #0x60]     /* jmp_buf->D[6] */
        vldr    d15, [r0, #0x68]     /* jmp_buf->D[7] */
        mov     r0,  r1              /* retval */
+       cmp     r1,  #1
+       it      ls
+       movls   r0,  #1
        bx      r2
 #elif defined(_ARM64_) || defined(__aarch64__)
        ldp     x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */
@@ -99,7 +102,8 @@ __MINGW_USYMBOL(__mingw_longjmp):
        ldp     d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */
        ldp     d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */
        ldp     d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */
-       mov     x0,  x1               /* retval */
+       cmp     x1,  #1
+       csinc   x0,  x1,  xzr,  hi    /* retval */
        ret
 #endif
 #endif
-- 
2.20.1


>From 078fa560ae3d63031528ee53e5fc69963d9672a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 23 Aug 2026 20:59:52 +0200
Subject: [PATCH 02/25] crt: test: Extend t_setjmp test with different longjmp
 values

---
 mingw-w64-crt/testcases/t_setjmp.c | 36 +++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/mingw-w64-crt/testcases/t_setjmp.c 
b/mingw-w64-crt/testcases/t_setjmp.c
index 0a455771e2cf..b6abb21ffc1c 100644
--- a/mingw-w64-crt/testcases/t_setjmp.c
+++ b/mingw-w64-crt/testcases/t_setjmp.c
@@ -6,26 +6,40 @@
 
 jmp_buf buf;
 
-void __attribute__((noreturn)) foo (void)
+void __attribute__((noreturn)) call_longjmp (int val)
 {
-  printf ("Call longjmp\n");
-  longjmp (buf, 1);
+  printf ("Call longjmp with %d\n", val);
+  longjmp (buf, val);
 }
 
 int main ()
 {
   mingw_test_init ();
 
-  if (setjmp (buf))
+  for (volatile int in_val = -300; in_val <= 300; in_val++)
     {
-      printf ("Caught\n");
-    }
-  else
-    {
-      foo ();
-      printf ("Should not be reached.\n");
-      return 1;
+      printf ("Call setjmp\n");
+      volatile int in_longjmp = 0;
+      int out_val = setjmp (buf);
+      if ((in_val != 0 && out_val == in_val) || (in_val == 0 && out_val == 1)) 
/* longjmp (0) cause that setjmp () returns 1 */
+        {
+          printf ("Caught %d\n", out_val);
+        }
+      else if (!in_longjmp && out_val == 0)
+        {
+          in_longjmp = 1;
+          call_longjmp (in_val);
+          printf ("Should not be reached.\n");
+          return 1;
+        }
+      else
+        {
+          printf ("Unexpected value %d from %s\n", out_val, in_longjmp ? 
"longjmp" : "setjmp");
+          return 1;
+        }
+      printf ("\n");
     }
 
+  printf ("SUCCESS\n");
   return 0;
 }
-- 
2.20.1


>From fad3abc0e83099c88e549875cbbc264a377c54fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Mon, 28 Sep 2026 12:56:04 +0200
Subject: [PATCH 03/25] crt: Add _setjmpex and __intrinsic_setjmpex symbol
 aliases for arm32 UCRT builds

Function _setjmpex is available in arm32 msvcr110.dll, msvcr120.dll,
msvcr120_app.dll and in msvcrt.dll DLL libs. It is also available in
arm32 vcruntime140.dll DLL library via symbol __intrinsic_setjmpex which
msvc toolchain uses for linking UCRT applications.

For some unknown reasons this function is not available in arm32
api-ms-win-crt-private-l1-1-0.dll and ucrtbase.dll libraries which
mingw-w64 uses for UCRT builds. But it is available in arm64 versions
of those libraries.

When application includes setjmpex.h header file, the _setjmpex symbol is
emitted for setjmp() calls. So without the _setjmpex symbol in libucrt.a
library, the final linking for UCRT builds on arm32 is failing.

arm32 msvcr110.dll, msvcr120.dll, msvcr120_app.dll and msvcrt.dll DLL libs
have _setjmpex symbol with the same functionality as _setjmp symbol.
Both functions store the second argument (frame pointer) into the jmp_buf
structure.

So for arm32 UCRT define _setjmpex and __intrinsic_setjmpex symbols as
aliases to the __intrinsic_setjmp symbol. This prevents applications from
linking to the vcruntime140.dll when they are including setjmpex.h file.
---
 mingw-w64-crt/def-include/crt-aliases.def.in                  | 2 ++
 mingw-w64-crt/lib-common/api-ms-win-crt-private-l1-1-0.def.in | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/mingw-w64-crt/def-include/crt-aliases.def.in 
b/mingw-w64-crt/def-include/crt-aliases.def.in
index 64e53d555b96..09a97ceaba95 100644
--- a/mingw-w64-crt/def-include/crt-aliases.def.in
+++ b/mingw-w64-crt/def-include/crt-aliases.def.in
@@ -351,7 +351,9 @@ F_I386(_setjmp3 == _setjmp)
 
 #ifdef UCRTBASE
 F_NON_ARM64(_setjmp == __intrinsic_setjmp)
+F_ARM32(__intrinsic_setjmpex == __intrinsic_setjmp)
 F64(_setjmpex == __intrinsic_setjmpex)
+F_ARM32(_setjmpex == __intrinsic_setjmp)
 #endif
 
 #ifndef NO_STRCMPI_ALIAS
diff --git a/mingw-w64-crt/lib-common/api-ms-win-crt-private-l1-1-0.def.in 
b/mingw-w64-crt/lib-common/api-ms-win-crt-private-l1-1-0.def.in
index a2bce9fcafb7..73340fdd46b8 100644
--- a/mingw-w64-crt/lib-common/api-ms-win-crt-private-l1-1-0.def.in
+++ b/mingw-w64-crt/lib-common/api-ms-win-crt-private-l1-1-0.def.in
@@ -48,7 +48,9 @@ F_I386(__intrinsic_abnormal_termination)
 F_NON_ARM64(__intrinsic_setjmp)
 F_NON_ARM64(_setjmp == __intrinsic_setjmp)
 F64(__intrinsic_setjmpex)
+F_ARM32(__intrinsic_setjmpex == __intrinsic_setjmp)
 F64(_setjmpex == __intrinsic_setjmpex)
+F_ARM32(_setjmpex == __intrinsic_setjmp)
 __processing_throw
 __report_gsfailure
 __std_exception_copy
-- 
2.20.1


>From 04e3adb54be186d9a1501f5a615e3eb2652b5615 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Thu, 20 Aug 2026 18:10:21 +0200
Subject: [PATCH 04/25] crt: test: Add new t_setjmpex test which is using
 setjmpex.h

---
 mingw-w64-crt/testcases/Makefile.am  | 1 +
 mingw-w64-crt/testcases/t_setjmp.c   | 4 ++++
 mingw-w64-crt/testcases/t_setjmpex.c | 2 ++
 3 files changed, 7 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_setjmpex.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 6a65a4dbef1b..4fd3aec1b6a6 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -156,6 +156,7 @@ testcase_progs = \
   t_readdir \
   t_safe_flush \
   t_setjmp \
+  t_setjmpex \
   t_sigfpe \
   t_sigv \
   t_snprintf \
diff --git a/mingw-w64-crt/testcases/t_setjmp.c 
b/mingw-w64-crt/testcases/t_setjmp.c
index b6abb21ffc1c..3986b12792d4 100644
--- a/mingw-w64-crt/testcases/t_setjmp.c
+++ b/mingw-w64-crt/testcases/t_setjmp.c
@@ -1,4 +1,8 @@
+#ifdef TEST_SETJMPEX
+#include <setjmpex.h>
+#else
 #include <setjmp.h>
+#endif
 #include <stdio.h>
 
 /* mingw-w64 headers */
diff --git a/mingw-w64-crt/testcases/t_setjmpex.c 
b/mingw-w64-crt/testcases/t_setjmpex.c
new file mode 100644
index 000000000000..6cdba9839a70
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_setjmpex.c
@@ -0,0 +1,2 @@
+#define TEST_SETJMPEX
+#include "t_setjmp.c"
-- 
2.20.1


>From e261f37a8fcd868aa31f5f26e513d63f2600e382 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Wed, 19 Aug 2026 21:44:14 +0200
Subject: [PATCH 05/25] crt: test: Add new t_longjmp_unwind test which verifies
 that unwind handler is called from longjmp exactly once

---
 mingw-w64-crt/testcases/Makefile.am        |   1 +
 mingw-w64-crt/testcases/t_longjmp_unwind.c | 231 +++++++++++++++++++++
 2 files changed, 232 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_longjmp_unwind.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 4fd3aec1b6a6..b4120e6d5833 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -127,6 +127,7 @@ testcase_progs = \
   t_iswctype \
   t_ivma \
   t_lfs \
+  t_longjmp_unwind \
   t_lseeki64 \
   t_main \
   t_main_assert \
diff --git a/mingw-w64-crt/testcases/t_longjmp_unwind.c 
b/mingw-w64-crt/testcases/t_longjmp_unwind.c
new file mode 100644
index 000000000000..10516fb07053
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_longjmp_unwind.c
@@ -0,0 +1,231 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <excpt.h>
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+/* mingw-w64 headers */
+#include "libtest.h"
+
+#if defined(__SEH__) && (!defined(__clang__) || __clang_major__ >= 7)
+#define SEH_INLINE_ASM
+#ifdef __arm__
+#define ASM_SEH_UNWIND "%%unwind"
+#elif defined(__aarch64__) || defined(__arm64ec__)
+#define ASM_SEH_UNWIND "@unwind, @except" /* on arm64 the unwind handler is 
called when both @unwind and @except are specified */
+#else
+#define ASM_SEH_UNWIND "@unwind"
+#endif
+#endif
+
+static int outer_unwind_called = 0;
+static int target_unwind_called = 0;
+static int inner_unwind_called = 0;
+static jmp_buf buf;
+
+#if defined(__i386__)
+/* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
+__attribute__((force_align_arg_pointer))
+#endif
+static EXCEPTION_DISPOSITION __cdecl outer_unwind_handler(EXCEPTION_RECORD 
*ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT 
*ContextRecord __attribute__((unused)), PVOID DispatcherContext 
__attribute__((unused)))
+{
+  printf("System called outer SEH unwind handler with flags 0x%lx\n", 
ExceptionRecord->ExceptionFlags);
+  if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
+    outer_unwind_called++;
+  return ExceptionContinueSearch;
+}
+
+#if defined(__i386__)
+/* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
+__attribute__((force_align_arg_pointer))
+#endif
+static EXCEPTION_DISPOSITION __cdecl target_unwind_handler(EXCEPTION_RECORD 
*ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT 
*ContextRecord __attribute__((unused)), PVOID DispatcherContext 
__attribute__((unused)))
+{
+  printf("System called target SEH unwind handler with flags 0x%lx\n", 
ExceptionRecord->ExceptionFlags);
+  if ((ExceptionRecord->ExceptionFlags & (EXCEPTION_UNWINDING | 
EXCEPTION_TARGET_UNWIND)) == (EXCEPTION_UNWINDING | EXCEPTION_TARGET_UNWIND))
+    target_unwind_called++;
+  return ExceptionContinueSearch;
+}
+
+#if defined(__i386__)
+/* We need to make sure that we align the stack to 16 bytes for the sake of 
SSE */
+__attribute__((force_align_arg_pointer))
+#endif
+static EXCEPTION_DISPOSITION __cdecl inner_unwind_handler(EXCEPTION_RECORD 
*ExceptionRecord, PVOID EstablisherFrame __attribute__((unused)), CONTEXT 
*ContextRecord __attribute__((unused)), PVOID DispatcherContext 
__attribute__((unused)))
+{
+  printf("System called inner SEH unwind handler with flags 0x%lx\n", 
ExceptionRecord->ExceptionFlags);
+  if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
+    inner_unwind_called++;
+  return ExceptionContinueSearch;
+}
+
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmissing-noreturn"
+#endif
+#if !defined(__i386__) && defined(SEH_INLINE_ASM)
+__attribute__((noinline)) /* required for asm .seh_handler directive */
+#endif
+#if defined(__i386__)
+__attribute__((noinline)) /* prevents optimizing out exception_record 
structure before calling longjmp() */
+#endif
+static void register_inner_unwind_handler_and_call_longjmp(void)
+{
+#if defined(__i386__)
+  EXCEPTION_REGISTRATION_RECORD inner_exception_record = {
+    .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0),
+    .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)inner_unwind_handler,
+  };
+  __writefsdword(0, (DWORD)&inner_exception_record);
+#elif defined(SEH_INLINE_ASM)
+  asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" 
(inner_unwind_handler));
+#else
+  return; /* should not happen; triggers runtime error */
+#endif
+
+  printf("Calling longjmp() and unregistering inner SEH unwind handler\n");
+  longjmp(buf, 1);
+
+  /* NOTE: longjmp does not return and on i386 it unregister all SEH handlers 
up to the target one */
+
+  printf("Unregistering inner SEH unwind handler\n");
+#if defined(__i386__)
+  if (__readfsdword(0) != (DWORD)&inner_exception_record) {
+    printf("ERROR: Cannot unregister inner SEH unwind handler because of 
corrupted list of SEH records\n");
+    return;
+  }
+  __writefsdword(0, (DWORD)inner_exception_record.Next);
+#endif
+}
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+
+#if !defined(__i386__) && defined(SEH_INLINE_ASM)
+__attribute__((noinline)) /* required for asm .seh_handler directive */
+#endif
+static int 
register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(void)
+{
+#if defined(__i386__)
+  EXCEPTION_REGISTRATION_RECORD target_exception_record = {
+    .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0),
+    .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)target_unwind_handler,
+  };
+  __writefsdword(0, (DWORD)&target_exception_record);
+#elif defined(SEH_INLINE_ASM)
+  asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" 
(target_unwind_handler));
+#else
+  return 1; /* should not happen; triggers runtime error */
+#endif
+
+  printf("Calling setjmp()\n");
+
+  if (!setjmp(buf)) {
+    printf("Registering inner SEH unwind handler\n");
+    register_inner_unwind_handler_and_call_longjmp();
+    printf("ERROR: longjmp() returned\n");
+    return 1;
+  }
+
+  printf("setjmp() returned from longjmp()\n");
+
+  printf("Unregistering target SEH unwind handler\n");
+#if defined(__i386__)
+  if (__readfsdword(0) != (DWORD)&target_exception_record) {
+    printf("ERROR: Cannot unregister target SEH unwind handler because of 
corrupted list of SEH records\n");
+    return 1;
+  }
+  __writefsdword(0, (DWORD)target_exception_record.Next);
+#endif
+  return 0;
+}
+
+#if !defined(__i386__) && defined(SEH_INLINE_ASM)
+__attribute__((noinline)) /* required for asm .seh_handler directive */
+#endif
+static int 
register_outer_unwind_handler_and_register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp(void)
+{
+#if defined(__i386__)
+  EXCEPTION_REGISTRATION_RECORD outer_exception_record = {
+    .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword(0),
+    .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)outer_unwind_handler,
+  };
+  __writefsdword(0, (DWORD)&outer_exception_record);
+#elif defined(SEH_INLINE_ASM)
+  asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" 
(outer_unwind_handler));
+#else
+  return 1; /* should not happen; triggers runtime error */
+#endif
+
+  printf("Registering target SEH unwind handler\n");
+  int ret = 
register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp();
+
+  printf("Unregistering outer SEH unwind handler\n");
+#if defined(__i386__)
+  if (__readfsdword(0) != (DWORD)&outer_exception_record) {
+    printf("ERROR: Cannot unregister outer SEH unwind handler because of 
corrupted list of SEH records\n");
+    return 1;
+  }
+  __writefsdword(0, (DWORD)outer_exception_record.Next);
+#endif
+  return ret;
+}
+
+int main(int argc, char **argv)
+{
+  mingw_test_init();
+
+#if !defined(__i386__) && !defined(SEH_INLINE_ASM)
+  printf("SKIP: No SEH support\n");
+  return 77;
+#endif
+
+  outer_unwind_called = 0;
+  target_unwind_called = 0;
+  inner_unwind_called = 0;
+
+  printf("Registering outer SEH unwind handler\n");
+  int ret = 
register_outer_unwind_handler_and_register_target_unwind_handler_and_call_setjmp_and_register_inner_unwind_handler_and_call_longjmp();
+
+  if (outer_unwind_called != 0) {
+    printf("ERROR: Outer SEH unwind handler was called\n");
+    ret = 1;
+  } else {
+    printf("OK: Outer SEH unwind handler was not called\n");
+  }
+
+#ifdef __i386__
+  /* i386 fs:0 based SEH does not call the last (target) handler */
+  if (target_unwind_called != 0) {
+    printf("ERROR: Target SEH unwind handler was called\n");
+    ret = 1;
+  } else {
+    printf("OK: Target SEH unwind handler was not called\n");
+  }
+#else
+  /* non-i386 .pdata based SEH always calls the last target handler with the 
EXCEPTION_TARGET_UNWIND flag */
+  if (target_unwind_called == 0) {
+    printf("ERROR: Target SEH unwind handler was not called\n");
+    ret = 1;
+  } else if (target_unwind_called != 1) {
+    printf("ERROR: Target SEH unwind handler was called more times (%d)\n", 
target_unwind_called);
+    ret = 1;
+  } else {
+    printf("OK: Target SEH unwind handler was called exactly once\n");
+  }
+#endif
+
+  if (inner_unwind_called == 0) {
+    printf("ERROR: Inner SEH unwind handler was not called\n");
+    ret = 1;
+  } else if (inner_unwind_called != 1) {
+    printf("ERROR: Inner SEH unwind handler was called more times (%d)\n", 
inner_unwind_called);
+    ret = 1;
+  } else {
+    printf("OK: Inner SEH unwind handler was called exactly once\n");
+  }
+
+  return ret;
+}
-- 
2.20.1


>From b7f931cc173ceb9b93faba3cf0313a7ce407e4fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Fri, 9 Oct 2026 01:26:34 +0200
Subject: [PATCH 06/25] crt: test: Add new t_longjmp_nounwind test which
 verifies that __USE_MINGW_SETJMP_NON_SEH does not call SEH unwind handler

This test is currently disabled on i386 and arm64ec because setjmp.h does
not implement the __USE_MINGW_SETJMP_NON_SEH option for these platforms.
Trying to use it cause failures on i386 (SEH handlers are called) and on
arm64ec (NULL pointer dereference occurs because arm64ec longjmp is always
trying to dereference jmp_buf->Frame, which is NULL for non-SEH variant).
---
 mingw-w64-crt/testcases/Makefile.am          |  1 +
 mingw-w64-crt/testcases/t_longjmp_nounwind.c |  3 ++
 mingw-w64-crt/testcases/t_longjmp_unwind.c   | 57 ++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_longjmp_nounwind.c

diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index b4120e6d5833..c239a9e93a35 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -127,6 +127,7 @@ testcase_progs = \
   t_iswctype \
   t_ivma \
   t_lfs \
+  t_longjmp_nounwind \
   t_longjmp_unwind \
   t_lseeki64 \
   t_main \
diff --git a/mingw-w64-crt/testcases/t_longjmp_nounwind.c 
b/mingw-w64-crt/testcases/t_longjmp_nounwind.c
new file mode 100644
index 000000000000..0461f920a9b1
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_longjmp_nounwind.c
@@ -0,0 +1,3 @@
+#define TEST_NOUNWIND
+#define __USE_MINGW_SETJMP_NON_SEH
+#include "t_longjmp_unwind.c"
diff --git a/mingw-w64-crt/testcases/t_longjmp_unwind.c 
b/mingw-w64-crt/testcases/t_longjmp_unwind.c
index 10516fb07053..dcabbfe75bc8 100644
--- a/mingw-w64-crt/testcases/t_longjmp_unwind.c
+++ b/mingw-w64-crt/testcases/t_longjmp_unwind.c
@@ -61,6 +61,13 @@ static EXCEPTION_DISPOSITION __cdecl 
inner_unwind_handler(EXCEPTION_RECORD *Exce
   return ExceptionContinueSearch;
 }
 
+#ifdef TEST_NOUNWIND
+#if defined(__i386__)
+static EXCEPTION_REGISTRATION_RECORD *inner_exception_record_ptr;
+static EXCEPTION_REGISTRATION_RECORD inner_exception_record_copy;
+#endif
+#endif
+
 #ifdef __clang__
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wmissing-noreturn"
@@ -79,13 +86,21 @@ static void 
register_inner_unwind_handler_and_call_longjmp(void)
     .Handler = (PEXCEPTION_ROUTINE)(INT_PTR)inner_unwind_handler,
   };
   __writefsdword(0, (DWORD)&inner_exception_record);
+#ifdef TEST_NOUNWIND
+  inner_exception_record_ptr = &inner_exception_record;
+  inner_exception_record_copy = inner_exception_record;
+#endif
 #elif defined(SEH_INLINE_ASM)
   asm volatile (".seh_handler %c0, " ASM_SEH_UNWIND :: "i" 
(inner_unwind_handler));
 #else
   return; /* should not happen; triggers runtime error */
 #endif
 
+#if defined(TEST_NOUNWIND) && defined(__i386__)
+  printf("Calling longjmp()\n");
+#else
   printf("Calling longjmp() and unregistering inner SEH unwind handler\n");
+#endif
   longjmp(buf, 1);
 
   /* NOTE: longjmp does not return and on i386 it unregister all SEH handlers 
up to the target one */
@@ -131,6 +146,17 @@ static int 
register_target_unwind_handler_and_call_setjmp_and_register_inner_unw
 
   printf("setjmp() returned from longjmp()\n");
 
+#ifdef TEST_NOUNWIND
+#if defined(__i386__)
+  printf("Unregistering inner SEH unwind handler\n");
+  if (__readfsdword(0) != (DWORD)inner_exception_record_ptr) {
+    printf("ERROR: Cannot unregister inner SEH unwind handler because of 
corrupted list of SEH records\n");
+    return 1;
+  }
+  __writefsdword(0, (DWORD)inner_exception_record_copy.Next);
+#endif
+#endif
+
   printf("Unregistering target SEH unwind handler\n");
 #if defined(__i386__)
   if (__readfsdword(0) != (DWORD)&target_exception_record) {
@@ -177,9 +203,22 @@ int main(int argc, char **argv)
 {
   mingw_test_init();
 
+#ifndef TEST_NOUNWIND
 #if !defined(__i386__) && !defined(SEH_INLINE_ASM)
   printf("SKIP: No SEH support\n");
   return 77;
+#endif
+#endif
+
+#ifdef TEST_NOUNWIND
+#if defined(__i386__)
+  printf("SKIP: __USE_MINGW_SETJMP_NON_SEH is broken on i386\n");
+  return 77;
+#endif
+#if defined(__arm64ec__)
+  printf("SKIP: __USE_MINGW_SETJMP_NON_SEH is broken on arm64ec\n");
+  return 77;
+#endif
 #endif
 
   outer_unwind_called = 0;
@@ -196,6 +235,7 @@ int main(int argc, char **argv)
     printf("OK: Outer SEH unwind handler was not called\n");
   }
 
+#ifndef TEST_NOUNWIND
 #ifdef __i386__
   /* i386 fs:0 based SEH does not call the last (target) handler */
   if (target_unwind_called != 0) {
@@ -216,7 +256,16 @@ int main(int argc, char **argv)
     printf("OK: Target SEH unwind handler was called exactly once\n");
   }
 #endif
+#else
+  if (target_unwind_called != 0) {
+    printf("ERROR: Target SEH unwind handler was called\n");
+    ret = 1;
+  } else {
+    printf("OK: Target SEH unwind handler was not called\n");
+  }
+#endif
 
+#ifndef TEST_NOUNWIND
   if (inner_unwind_called == 0) {
     printf("ERROR: Inner SEH unwind handler was not called\n");
     ret = 1;
@@ -226,6 +275,14 @@ int main(int argc, char **argv)
   } else {
     printf("OK: Inner SEH unwind handler was called exactly once\n");
   }
+#else
+  if (inner_unwind_called != 0) {
+    printf("ERROR: Inner SEH unwind handler was called\n");
+    ret = 1;
+  } else {
+    printf("OK: Inner SEH unwind handler was not called\n");
+  }
+#endif
 
   return ret;
 }
-- 
2.20.1


>From 6339c50cca27daff57bae355f65cfb3afa09e1e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 27 Sep 2026 14:55:42 +0200
Subject: [PATCH 07/25] crt: Add i386 __mingw_longjmp function for all builds

This new i386 __mingw_longjmp function will do same thing as i386 CRT
longjmp function, but will not invoke SEH unwinding. So same as arm32 and
arm64 __mingw_longjmp function.

msvcrt/UCRT longjmp and _longjmpex functions are always calling SEH
unwinding, so the simple __mingw_longjmp function written in assembly is
the easiest option how to achieve long jump without SEH unwinding.

Use this new i386 function __mingw_longjmp for implementing the
__USE_MINGW_SETJMP_NON_SEH behavior on i386 and change the longjmp macro
inside the setjmp.h header file.

Also unskip the failing i386 t_longjmp_nounwind test.
---
 mingw-w64-crt/misc/mingw_longjmp.S         | 16 +++++++++++++++-
 mingw-w64-crt/testcases/t_longjmp_unwind.c |  4 ----
 mingw-w64-headers/crt/setjmp.h             |  4 ++++
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/mingw-w64-crt/misc/mingw_longjmp.S 
b/mingw-w64-crt/misc/mingw_longjmp.S
index 5863f0d88652..4d2da261aca6 100644
--- a/mingw-w64-crt/misc/mingw_longjmp.S
+++ b/mingw-w64-crt/misc/mingw_longjmp.S
@@ -55,11 +55,25 @@ __MINGW_USYMBOL(__mingw_setjmp):
        mov     x0,  #0
        ret
 #endif
+#endif
 
+#if defined(__i386__) || defined(_ARM_) || defined(__arm__) || 
defined(_ARM64_) || defined(__aarch64__)
        .globl __MINGW_USYMBOL(__mingw_longjmp)
        .def    __MINGW_USYMBOL(__mingw_longjmp);       .scl    2;      .type   
32;     .endef
 __MINGW_USYMBOL(__mingw_longjmp):
-#if defined(_ARM_) || defined(__arm__)
+#if defined(__i386__)
+       movl    4(%esp),%ecx         /* jmp_buf */
+       movl    8(%esp),%eax         /* retval */
+       cmpl    $1,%eax              /* check that retval is non-zero */
+       adcl    $0,%eax              /* if not then increase by one via CF */
+       movl    0(%ecx),%ebp         /* jmp_buf->Ebp */
+       movl    4(%ecx),%ebx         /* jmp_buf->Ebx */
+       movl    8(%ecx),%edi         /* jmp_buf->Edi */
+       movl    12(%ecx),%esi        /* jmp_buf->Esi */
+       movl    16(%ecx),%esp        /* jmp_buf->Esp */
+       addl    $4,%esp              /* get rid of return address */
+       jmpl    *20(%ecx)            /* jmp_buf->Eip */
+#elif defined(_ARM_) || defined(__arm__)
        ldr     r4,  [r0, #0x4]      /* jmp_buf->R4 */
        ldr     r5,  [r0, #0x8]      /* jmp_buf->R5 */
        ldr     r6,  [r0, #0xc]      /* jmp_buf->R6 */
diff --git a/mingw-w64-crt/testcases/t_longjmp_unwind.c 
b/mingw-w64-crt/testcases/t_longjmp_unwind.c
index dcabbfe75bc8..7dc554e2551c 100644
--- a/mingw-w64-crt/testcases/t_longjmp_unwind.c
+++ b/mingw-w64-crt/testcases/t_longjmp_unwind.c
@@ -211,10 +211,6 @@ int main(int argc, char **argv)
 #endif
 
 #ifdef TEST_NOUNWIND
-#if defined(__i386__)
-  printf("SKIP: __USE_MINGW_SETJMP_NON_SEH is broken on i386\n");
-  return 77;
-#endif
 #if defined(__arm64ec__)
   printf("SKIP: __USE_MINGW_SETJMP_NON_SEH is broken on arm64ec\n");
   return 77;
diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index 49e29a0e0160..2306f8437a77 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -204,6 +204,8 @@ _CRTIMP int __cdecl __attribute__ 
((__nothrow__,__returns_twice__)) _setjmpex(jm
 
 #if defined(__arm__) || defined(__aarch64__)
 int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
__mingw_setjmp(jmp_buf _Buf);
+#endif
+#if defined(__i386__) || defined(__arm__) || defined(__aarch64__)
 __MINGW_ATTRIB_NORETURN __attribute__ ((__nothrow__)) void 
__mingw_longjmp(jmp_buf _Buf, int _Value);
 #endif
 
@@ -257,6 +259,8 @@ _CRT_END_C_HEADER
 #endif
 #if defined(_INC_SETJMPEX) && defined(__i386__)
 #  define longjmp _longjmpex
+#elif !defined(_INC_SETJMPEX) && defined(__USE_MINGW_SETJMP_NON_SEH) && 
defined(__i386__)
+#  define longjmp __mingw_longjmp
 #elif !defined(_INC_SETJMPEX) && (!defined(__SEH__) || 
defined(__USE_MINGW_SETJMP_NON_SEH)) && (defined(__arm__) || 
defined(__aarch64__))
 #  define longjmp __mingw_longjmp
 #else
-- 
2.20.1


>From 26ff4f79a3debaa05fe0ee5b8f3c3edfac4a4c34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 1 Feb 2026 14:01:33 +0100
Subject: [PATCH 08/25] crt: Fill all i386 jmp_buf members in mingw-w64
 _setjmp() function for ucrtapp builds

Inspect current SEH record and its try level, like the real msvcrt/UCRT 
_setjmp() function.
---
 mingw-w64-crt/misc/setjmp.S | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
index a2f1dc0bb61d..ee194856046d 100644
--- a/mingw-w64-crt/misc/setjmp.S
+++ b/mingw-w64-crt/misc/setjmp.S
@@ -45,6 +45,14 @@ __MINGW_USYMBOL(_setjmp):
         movl    %esp,16(%ecx)        /* jmp_buf.Esp */
         movl    0(%esp),%eax
         movl    %eax,20(%ecx)        /* jmp_buf.Eip */
+        movl    %fs:0,%eax           /* current SEH record */
+        movl    %eax,24(%ecx)        /* jmp_buf.Registration */
+        movl    $-1,%edx
+        cmpl    $-1,%eax             /* SEH is top level -> do not inspect SEH 
try_level */
+        je      .L_setjmp_skip_SEH_try_level
+        movl    12(%eax),%edx
+        .L_setjmp_skip_SEH_try_level:
+        movl    %edx,28(%ecx)        /* jmp_buf.TryLevel */
         xorl    %eax, %eax
         retl
 
-- 
2.20.1


>From ba3af17b8a6a4dc7fab520e36a7d625079ad29ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 1 Feb 2026 14:05:33 +0100
Subject: [PATCH 09/25] crt: Properly implement i386 mingw-w64 _setjmp3()
 function for ucrtapp builds

i386 _setjmp3() function takes more parameters and fills more members in
jmp_buf. Also it allows to skip inspection of current SEH record.

As lot of code is common between _setjmp() and _setjmp3() functions,
create a new common local function setjmp_common() and call it from both
exported functions.
---
 mingw-w64-crt/misc/setjmp.S | 56 +++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
index ee194856046d..acd17478a4c7 100644
--- a/mingw-w64-crt/misc/setjmp.S
+++ b/mingw-w64-crt/misc/setjmp.S
@@ -35,8 +35,62 @@
 
 #if defined(__i386__)
 __MINGW_USYMBOL(_setjmp3):
+        pushl   4(%esp)
+        /* edx == -1 -> setjmp_common will NOT inspect SEH try_level */
+        /* edx != -1 -> setjmp_common will inspect SEH try_level */
+        /* Do not inspect SEH try_level when _Count >= 2 -> means that 
try_level is provided as _TryLevel param */
+        cmpl    $2,8(%esp)
+        setge   %dl
+        movzbl  %dl,%edx
+        neg     %edx
+        calll   __MINGW_LSYMBOL(setjmp_common)
+        popl    %ecx                 /* jmp_buf */
+
+        movl    %esp,16(%ecx)        /* jmp_buf.Esp */
+        movl    0(%esp),%eax
+        movl    %eax,20(%ecx)        /* jmp_buf.Eip */
+        movl    $0x56433230,32(%ecx) /* jmp_buf.Cookie */
+        movl    $0,36(%ecx)          /* jmp_buf.UnwindFunc */
+
+        movl    24(%ecx),%eax        /* load jmp_buf.Registration */
+        cmpl    $-1,%eax
+        je      .L_setjmp3_end
+
+        movl    8(%esp),%eax         /* _Count */
+        test    %eax,%eax
+        je      .L_setjmp3_end
+
+        movl    12(%esp),%edx        /* _UnwindFunc */
+        movl    %edx,36(%ecx)        /* jmp_buf.UnwindFunc */
+        decl    %eax
+        jz      .L_setjmp3_end
+
+        movl    16(%esp),%edx        /* _TryLevel */
+        movl    %edx,28(%ecx)        /* jmp_buf.TryLevel */
+        decl    %eax
+        jz      .L_setjmp3_end
+
+        cmpl    $6,%eax
+        jbe     .L_setjmp3_copy_unwind_data
+        movl    $6,%eax              /* array size of jmp_buf.UnwindData[] is 
6 */
+        .L_setjmp3_copy_unwind_data:
+        pushl   %esi
+        pushl   %edi
+        leal    28(%esp),%esi        /* _UnwindData */
+        leal    40(%ecx),%edi        /* jmp_buf.UnwindData */
+        movl    %eax,%ecx
+        rep movsl (%esi),(%edi)
+        popl    %edi
+        popl    %esi
+
+        .L_setjmp3_end:
+        xorl    %eax,%eax
+        retl
+
 __MINGW_USYMBOL(__intrinsic_setjmp):
 __MINGW_USYMBOL(_setjmp):
+        xorl    %edx,%edx            /* edx != -1 -> setjmp_common will 
inspect SEH try_level */
+__MINGW_LSYMBOL(setjmp_common):
         movl    4(%esp),%ecx         /* jmp_buf */
         movl    %ebp,0(%ecx)         /* jmp_buf.Ebp */
         movl    %ebx,4(%ecx)         /* jmp_buf.Ebx */
@@ -47,6 +101,8 @@ __MINGW_USYMBOL(_setjmp):
         movl    %eax,20(%ecx)        /* jmp_buf.Eip */
         movl    %fs:0,%eax           /* current SEH record */
         movl    %eax,24(%ecx)        /* jmp_buf.Registration */
+        cmpl    $-1,%edx             /* setjmp_common was called to not 
inspect SEH try_level */
+        je      .L_setjmp_skip_SEH_try_level
         movl    $-1,%edx
         cmpl    $-1,%eax             /* SEH is top level -> do not inspect SEH 
try_level */
         je      .L_setjmp_skip_SEH_try_level
-- 
2.20.1


>From 70c5ff36d2c063b5af4018d323a4840894b3ba10 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Mon, 28 Sep 2026 13:23:01 +0200
Subject: [PATCH 10/25] crt: Fix mingw-w64 amd64
 setjmp/__intrinsic_setjmp/_setjmp and arm32 __intrinsic_setjmp/_setjmp
 functions for ucrtapp builds

amd64 msvcrt.dll/msvcr80-120 setjmp and _setjmp functions, and amd64 UCRT
__intrinsic_setjmp function are not clearing the second (%rdx) argument,
they are storing it into the jmp_buf structure. Same applies for arm32
msvcrt.dll/msvcr110-120 _setjmp function and arm32 UCRT __intrinsic_setjmp
function.

Fix the mingw-w64 implementations of those functions to match the msvcrt
and UCRT behavior.
---
 mingw-w64-crt/misc/setjmp.S | 2 --
 1 file changed, 2 deletions(-)

diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
index acd17478a4c7..4be52b06edb6 100644
--- a/mingw-w64-crt/misc/setjmp.S
+++ b/mingw-w64-crt/misc/setjmp.S
@@ -116,7 +116,6 @@ __MINGW_LSYMBOL(setjmp_common):
 __MINGW_USYMBOL(setjmp):
 __MINGW_USYMBOL(__intrinsic_setjmp):
 __MINGW_USYMBOL(_setjmp):
-        xorq    %rdx,%rdx
 __MINGW_USYMBOL(__intrinsic_setjmpex):
 __MINGW_USYMBOL(_setjmpex):
         movq    %rdx,(%rcx)          /* jmp_buf->Frame */
@@ -150,7 +149,6 @@ __MINGW_USYMBOL(_setjmpex):
 #elif defined(__arm__)
 __MINGW_USYMBOL(__intrinsic_setjmp):
 __MINGW_USYMBOL(_setjmp):
-        mov     r1,  #0
 __MINGW_USYMBOL(__intrinsic_setjmpex):
 __MINGW_USYMBOL(_setjmpex):
         str     r1,  [r0]            /* jmp_buf->Frame */
-- 
2.20.1


>From f7cd5a9a680a033160fff85a1769c55d15ab54ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Fri, 16 Oct 2026 13:24:55 +0200
Subject: [PATCH 11/25] crt: Define setjmp function aliases for arm64ec ucrtapp
 builds

Currently including the setjmp.h and using setjmp() macro function for
arm64ec ucrtapp builds cause linker error:

    ld.lld: error: undefined symbol: __declspec(dllimport) _setjmp (EC symbol)
    clang-23: error: linker command failed with exit code 1 (use -v to see 
invocation)

This is because for arm64ec ucrtapp builds is provided only the
__intrinsic_setjmpex symbol, not the _setjmp symbol.

So define all setjmp symbol aliases (which are available for amd64 builds)
for arm64ec ucrtapp builds.
---
 mingw-w64-crt/misc/arm64ec/setjmp.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mingw-w64-crt/misc/arm64ec/setjmp.c 
b/mingw-w64-crt/misc/arm64ec/setjmp.c
index ef5012a5fd6f..ac8d6f0386fb 100644
--- a/mingw-w64-crt/misc/arm64ec/setjmp.c
+++ b/mingw-w64-crt/misc/arm64ec/setjmp.c
@@ -108,4 +108,13 @@ int __attribute__((naked)) __intrinsic_setjmpex( jmp_buf 
buf, void *frame )
          ".seh_endproc" );
 }
 
+#undef setjmp
+int __attribute__((alias("__intrinsic_setjmpex"))) setjmp( jmp_buf buf, void * 
frame );
+
+int __attribute__((alias("__intrinsic_setjmpex"))) __intrinsic_setjmp( jmp_buf 
buf, void * frame );
+
+int __attribute__((alias("__intrinsic_setjmpex"))) _setjmp( jmp_buf buf, void 
* frame );
+
+int __attribute__((alias("__intrinsic_setjmpex"))) _setjmpex( jmp_buf buf, 
void * frame );
+
 #endif
-- 
2.20.1


>From 2390b6f4b75963e9b78d1c9bfe20fb2cde32aaa1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Wed, 7 Oct 2026 16:56:14 +0200
Subject: [PATCH 12/25] crt: Define imp symbols in setjmp.S for ucrtapp builds

setjmp.h references setjmp functions via import symbols.
---
 mingw-w64-crt/misc/arm64ec/setjmp.c |  5 +++++
 mingw-w64-crt/misc/setjmp.S         | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/mingw-w64-crt/misc/arm64ec/setjmp.c 
b/mingw-w64-crt/misc/arm64ec/setjmp.c
index ac8d6f0386fb..7dcb87eedd63 100644
--- a/mingw-w64-crt/misc/arm64ec/setjmp.c
+++ b/mingw-w64-crt/misc/arm64ec/setjmp.c
@@ -107,14 +107,19 @@ int __attribute__((naked)) __intrinsic_setjmpex( jmp_buf 
buf, void *frame )
          "b \"#do_setjmpex\"\n\t"
          ".seh_endproc" );
 }
+int (*__MINGW_IMP_SYMBOL(__intrinsic_setjmpex))( jmp_buf buf, void * frame ) = 
__intrinsic_setjmpex;
 
 #undef setjmp
 int __attribute__((alias("__intrinsic_setjmpex"))) setjmp( jmp_buf buf, void * 
frame );
+extern int 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex)))))(*__MINGW_IMP_SYMBOL(setjmp))(
 jmp_buf buf, void * frame );
 
 int __attribute__((alias("__intrinsic_setjmpex"))) __intrinsic_setjmp( jmp_buf 
buf, void * frame );
+extern int 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex)))))(*__MINGW_IMP_SYMBOL(__intrinsic_setjmp))(
 jmp_buf buf, void * frame );
 
 int __attribute__((alias("__intrinsic_setjmpex"))) _setjmp( jmp_buf buf, void 
* frame );
+extern int 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex)))))(*__MINGW_IMP_SYMBOL(_setjmp))(
 jmp_buf buf, void * frame );
 
 int __attribute__((alias("__intrinsic_setjmpex"))) _setjmpex( jmp_buf buf, 
void * frame );
+extern int 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex)))))(*__MINGW_IMP_SYMBOL(_setjmpex))(
 jmp_buf buf, void * frame );
 
 #endif
diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
index 4be52b06edb6..5fa967722cf8 100644
--- a/mingw-w64-crt/misc/setjmp.S
+++ b/mingw-w64-crt/misc/setjmp.S
@@ -13,11 +13,19 @@
         .def    __MINGW_USYMBOL(__intrinsic_setjmp); .scl 2; .type 32; .endef
         .globl  __MINGW_USYMBOL(_setjmp)
         .def    __MINGW_USYMBOL(_setjmp); .scl 2; .type 32; .endef
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(__intrinsic_setjmp))
+        .set    __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(__intrinsic_setjmp)), 
__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp))
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp))
+#ifndef __i386__
+        .set    __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)), 
__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex))
+#endif
 #endif
 
 #ifdef __x86_64__
         .globl  __MINGW_USYMBOL(setjmp)
         .def    __MINGW_USYMBOL(setjmp); .scl 2; .type 32; .endef
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(setjmp))
+        .set    __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(setjmp)), 
__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp))
 #endif
 
 #ifndef __i386__
@@ -25,11 +33,29 @@
         .def    __MINGW_USYMBOL(__intrinsic_setjmpex); .scl 2; .type 32; .endef
         .globl  __MINGW_USYMBOL(_setjmpex)
         .def    __MINGW_USYMBOL(_setjmpex); .scl 2; .type 32; .endef
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex))
+        .set    __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(__intrinsic_setjmpex)), 
__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex))
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex))
+.data
+__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex)):
+#ifdef _WIN64
+        .quad   __MINGW_USYMBOL(_setjmpex)
+#else
+        .long   __MINGW_USYMBOL(_setjmpex)
+#endif
+.text
 #endif
 
 #ifdef __i386__
         .globl  __MINGW_USYMBOL(_setjmp3)
         .def    __MINGW_USYMBOL(_setjmp3); .scl 2; .type 32; .endef
+        .globl  __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp3))
+.data
+__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp3)):
+        .long   __MINGW_USYMBOL(_setjmp3)
+__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)):
+        .long   __MINGW_USYMBOL(_setjmp)
+.text
 #endif
 
 
-- 
2.20.1


>From c8465836854991757bfd9122ccbfca160da21067 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 27 Dec 2025 14:24:01 +0100
Subject: [PATCH 13/25] crt: Provide _setjmp3() function for crtdll.dll and
 msvcrt10.dll builds

Implement _setjmp3 function as wrapper around the _setjmp function
which is available in crtdll.dll and msvcrt10.dll CRT libraries.
---
 mingw-w64-crt/Makefile.am                    |  1 +
 mingw-w64-crt/def-include/crt-aliases.def.in | 14 --------------
 mingw-w64-crt/lib32/crtdll.def.in            |  1 -
 mingw-w64-crt/lib32/msvcrt10.def.in          |  1 -
 mingw-w64-crt/misc/_setjmp3.S                | 11 +++++++++++
 mingw-w64-crt/misc/setjmp.S                  | 12 ++++++++++++
 6 files changed, 24 insertions(+), 16 deletions(-)
 create mode 100644 mingw-w64-crt/misc/_setjmp3.S

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 4e434d68c4fa..e1d7d8cba9d8 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -918,6 +918,7 @@ src_pre_msvcrt20=\
   misc/__tzname.c \
   misc/_beginthreadex.c \
   misc/_endthreadex.c \
+  misc/_setjmp3.S \
   stdio/_wfindfirst32.c \
   stdio/_wfindnext32.c \
   stdio/_wstat32.c \
diff --git a/mingw-w64-crt/def-include/crt-aliases.def.in 
b/mingw-w64-crt/def-include/crt-aliases.def.in
index 09a97ceaba95..656b89911317 100644
--- a/mingw-w64-crt/def-include/crt-aliases.def.in
+++ b/mingw-w64-crt/def-include/crt-aliases.def.in
@@ -335,20 +335,6 @@ strncasecmp == _strnicmp
 ; Some symbols in some version of CRT library were added and some other 
symbols were removed or renamed
 ; This list provides some level of backward and forward compatibility
 
-#ifdef WITH_SETJMP3_ALIAS
-; crtdll.dll and msvcrt10.dll have only old _setjmp function which does not 
take
-; additional variadic arguments and uses smaller jmpbuf structure.
-; mingw-w64 calls _setjmp3 only with zero additional arguments and because 
number
-; of additional arguments is passed on the stack which is cleanup by the 
caller,
-; it means that the mingw-w64 usage of _setjmp3 is ABI compatible with the old
-; _setjmp function which is available also in crtdll.dll and msvcrt10.dll libs.
-; It heavily depends on the mingw-w64-headers/crt/setjmp.h implementation.
-; So this definition allows crtdll.dll and msvcrt10.dll applications to call
-; setjmp() macro from setjmp.h, which expands to _setjmp3() function call and
-; which is aliased to _setjmp symbol for crtdll.dll and msvcrt10.dll libraries.
-F_I386(_setjmp3 == _setjmp)
-#endif
-
 #ifdef UCRTBASE
 F_NON_ARM64(_setjmp == __intrinsic_setjmp)
 F_ARM32(__intrinsic_setjmpex == __intrinsic_setjmp)
diff --git a/mingw-w64-crt/lib32/crtdll.def.in 
b/mingw-w64-crt/lib32/crtdll.def.in
index 4a4ec068ab84..ad5c7bdfc506 100644
--- a/mingw-w64-crt/lib32/crtdll.def.in
+++ b/mingw-w64-crt/lib32/crtdll.def.in
@@ -640,5 +640,4 @@ _pgmptr DATA == _pgmptr_dll
 #define NO_STRCMPI_ALIAS
 #define WITH_NEXTAFTER_ALIAS
 #define WITH_GET_PUT_WCHAR_ALIASES
-#define WITH_SETJMP3_ALIAS
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in 
b/mingw-w64-crt/lib32/msvcrt10.def.in
index 9d013e2ffdc6..f116cfec8ad6 100644
--- a/mingw-w64-crt/lib32/msvcrt10.def.in
+++ b/mingw-w64-crt/lib32/msvcrt10.def.in
@@ -1302,5 +1302,4 @@ wscanf
 #define NO_STRCMPI_ALIAS
 #define WITH_NEXTAFTER_ALIAS
 #define WITH_GET_PUT_WCHAR_ALIASES
-#define WITH_SETJMP3_ALIAS
 #include "crt-aliases.def.in"
diff --git a/mingw-w64-crt/misc/_setjmp3.S b/mingw-w64-crt/misc/_setjmp3.S
new file mode 100644
index 000000000000..0b81c4c2dd9a
--- /dev/null
+++ b/mingw-w64-crt/misc/_setjmp3.S
@@ -0,0 +1,11 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <_mingw_mac.h>
+
+/* Define _setjmp3 as wrapper around the _setjmp */
+#define I386_SETJMP3_ONLY
+#include "setjmp.S"
diff --git a/mingw-w64-crt/misc/setjmp.S b/mingw-w64-crt/misc/setjmp.S
index 5fa967722cf8..3e9d6175a11f 100644
--- a/mingw-w64-crt/misc/setjmp.S
+++ b/mingw-w64-crt/misc/setjmp.S
@@ -9,6 +9,7 @@
 #ifndef __arm64ec__
 
 #ifndef __aarch64__
+#ifndef I386_SETJMP3_ONLY
         .globl  __MINGW_USYMBOL(__intrinsic_setjmp)
         .def    __MINGW_USYMBOL(__intrinsic_setjmp); .scl 2; .type 32; .endef
         .globl  __MINGW_USYMBOL(_setjmp)
@@ -20,6 +21,7 @@
         .set    __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)), 
__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex))
 #endif
 #endif
+#endif
 
 #ifdef __x86_64__
         .globl  __MINGW_USYMBOL(setjmp)
@@ -53,8 +55,10 @@ __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmpex)):
 .data
 __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp3)):
         .long   __MINGW_USYMBOL(_setjmp3)
+#ifndef I386_SETJMP3_ONLY
 __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)):
         .long   __MINGW_USYMBOL(_setjmp)
+#endif
 .text
 #endif
 
@@ -62,6 +66,11 @@ __MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp)):
 #if defined(__i386__)
 __MINGW_USYMBOL(_setjmp3):
         pushl   4(%esp)
+#ifdef I386_SETJMP3_ONLY
+        /* local setjmp_common function is not being compiled, and we have 
only CRT _setjmp function */
+        /* _setjmp always inspects SEH try_level and we will overwrite 
jmp_buf.TryLevel below if needed */
+        calll   *__MINGW_USYMBOL(__MINGW_IMP_SYMBOL(_setjmp))
+#else
         /* edx == -1 -> setjmp_common will NOT inspect SEH try_level */
         /* edx != -1 -> setjmp_common will inspect SEH try_level */
         /* Do not inspect SEH try_level when _Count >= 2 -> means that 
try_level is provided as _TryLevel param */
@@ -70,6 +79,7 @@ __MINGW_USYMBOL(_setjmp3):
         movzbl  %dl,%edx
         neg     %edx
         calll   __MINGW_LSYMBOL(setjmp_common)
+#endif
         popl    %ecx                 /* jmp_buf */
 
         movl    %esp,16(%ecx)        /* jmp_buf.Esp */
@@ -113,6 +123,7 @@ __MINGW_USYMBOL(_setjmp3):
         xorl    %eax,%eax
         retl
 
+#ifndef I386_SETJMP3_ONLY
 __MINGW_USYMBOL(__intrinsic_setjmp):
 __MINGW_USYMBOL(_setjmp):
         xorl    %edx,%edx            /* edx != -1 -> setjmp_common will 
inspect SEH try_level */
@@ -137,6 +148,7 @@ __MINGW_LSYMBOL(setjmp_common):
         movl    %edx,28(%ecx)        /* jmp_buf.TryLevel */
         xorl    %eax, %eax
         retl
+#endif
 
 #elif defined(__x86_64__)
 __MINGW_USYMBOL(setjmp):
-- 
2.20.1


>From 2dcf22bce49a34528cc43e593ff64111ae8c2e41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 31 Jan 2026 15:14:03 +0100
Subject: [PATCH 14/25] crt: Provide msvcrt/UCRT compatible longjmp and
 _longjmpex symbols for crtdll.dll builds

In msvcrt and UCRT the longjmp() function is calling SEH unwind and
basically is aliased to _longjmpex() function.

crtdll.dll does not provide _longjmpex() function and its longjmp()
function is not calling SEH unwind.

So rename its longjmp symbol to __crtdll_longjmp and provide longjmp and
_longjmpex implementations by mingw-w64.
---
 mingw-w64-crt/Makefile.am              |  1 +
 mingw-w64-crt/lib32/crtdll.def.in      |  2 +-
 mingw-w64-crt/misc/crtdll__longjmpex.c | 50 ++++++++++++++++++++++++++
 mingw-w64-headers/crt/setjmp.h         |  4 +--
 4 files changed, 54 insertions(+), 3 deletions(-)
 create mode 100644 mingw-w64-crt/misc/crtdll__longjmpex.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index e1d7d8cba9d8..d84f539ad9a3 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -1112,6 +1112,7 @@ src_crtdll=\
   $(src_pre_msvcr120) \
   misc/crtdll_stat.h \
   misc/crtdll__getmainargs.c \
+  misc/crtdll__longjmpex.c \
   misc/crtdll_fstat.c \
   misc/crtdll_stat.c \
   misc/__p__mbctype.c
diff --git a/mingw-w64-crt/lib32/crtdll.def.in 
b/mingw-w64-crt/lib32/crtdll.def.in
index ad5c7bdfc506..c95cf013cd67 100644
--- a/mingw-w64-crt/lib32/crtdll.def.in
+++ b/mingw-w64-crt/lib32/crtdll.def.in
@@ -417,7 +417,7 @@ localeconv
 localtime
 log
 log10
-longjmp
+__crtdll_longjmp == longjmp ; crtdll.dll's longjmp is incompatible with 
msvcrt.dll longjmp, real longjmp provided as wrapper
 malloc
 mblen
 mbstowcs
diff --git a/mingw-w64-crt/misc/crtdll__longjmpex.c 
b/mingw-w64-crt/misc/crtdll__longjmpex.c
new file mode 100644
index 000000000000..fb070ac7594e
--- /dev/null
+++ b/mingw-w64-crt/misc/crtdll__longjmpex.c
@@ -0,0 +1,50 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#if defined(__i386__)
+
+#include <setjmp.h>
+#include <windows.h>
+
+void __cdecl _global_unwind2(EXCEPTION_REGISTRATION_RECORD *frame);
+void __cdecl _local_unwind2(EXCEPTION_REGISTRATION_RECORD *frame, int 
trylevel);
+__MINGW_ATTRIB_NORETURN __attribute__((nothrow)) void __cdecl 
__crtdll_longjmp(jmp_buf buf, int value);
+
+__MINGW_ATTRIB_NORETURN
+__attribute__((nothrow))
+void __cdecl _longjmpex(jmp_buf buf, int value)
+{
+  _JUMP_BUFFER *bufptr = (void *)buf;
+
+  _Static_assert(sizeof(bufptr->Registration) == sizeof(void *), 
"_JUMP_BUFFER.Registration has not pointer size");
+  _Static_assert(sizeof(bufptr->UnwindFunc) == sizeof(void *), 
"_JUMP_BUFFER.UnwindFunc has not pointer size");
+
+  /* crtdll longjmp() completely ignores Registration, TryLevel and UnwindFunc 
fields.
+   * So do all unwinds manually before calling the real crtdll longjmp().
+   */
+
+  if (bufptr->Registration != __readfsdword(0))
+    _global_unwind2((void *)bufptr->Registration);
+
+  if (bufptr->Registration != 0) {
+    if (!IsBadReadPtr(&bufptr->Cookie, sizeof(bufptr->Cookie)) && 
bufptr->Cookie == 0x56433230) {
+      if (bufptr->UnwindFunc != 0)
+        ((void(__stdcall*)(const _JUMP_BUFFER *))bufptr->UnwindFunc)(bufptr);
+    } else {
+      _local_unwind2((void *)bufptr->Registration, bufptr->TryLevel);
+    }
+  }
+
+  __crtdll_longjmp(buf, value);
+}
+void (__cdecl *__MINGW_IMP_SYMBOL(_longjmpex))(jmp_buf buf, int value) = 
_longjmpex;
+
+/* In msvcrt and UCRT the longjmp() function is calling SEH unwind and is 
aliased to _longjmpex() function. */
+#undef longjmp
+void __attribute__((alias("_longjmpex"))) __cdecl longjmp(jmp_buf buf, int 
value);
+extern void 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_longjmpex)))))(__cdecl
 *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf buf, int value);
+
+#endif
diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index 2306f8437a77..7871c02f7abb 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -25,8 +25,8 @@ _CRT_BEGIN_C_HEADER
     unsigned long Registration;
     unsigned long TryLevel;
     /* Following fields are only for new _setjmp3(), the are not for old 
_setjmp(). */
-    unsigned long Cookie;
-    unsigned long UnwindFunc;
+    unsigned long Cookie; /* 0x56433230 */
+    unsigned long UnwindFunc; /* void(__stdcall*)(const _JUMP_BUFFER *) */
     unsigned long UnwindData[6];
   } _JUMP_BUFFER;
 
-- 
2.20.1


>From 0d878af8855640841c15afd7cc4195c7d17bdb4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Wed, 19 Aug 2026 21:27:16 +0200
Subject: [PATCH 15/25] crt: Provide msvcrt/UCRT compatible longjmp and
 _longjmpex symbols for msvcrt10.dll builds

msvcrt10.dll longjmp() function is mostly compatible with the msvcrt.dll
_longjmpex() function. What it misses is support for calling jmp_buf
UnwindFunc, instead it is is always calling local_unwind2() even for
incompatible SEH establisher frames.

Add mingw-w64 wrapper around the msvcrt10.dll longjmp() function. Process
the unsupported Cookie and UnwindFunc fields and then set TryLevel to the
value which stops the repeated local_unwind2() execution (called by the
real msvcrt10.dll longjmp).
---
 mingw-w64-crt/Makefile.am                |  3 +-
 mingw-w64-crt/lib32/msvcrt10.def.in      |  2 +-
 mingw-w64-crt/misc/msvcrt10__longjmpex.c | 77 ++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
 create mode 100644 mingw-w64-crt/misc/msvcrt10__longjmpex.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index d84f539ad9a3..1155547b42be 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -1128,7 +1128,8 @@ src_msvcrt10=\
   $(src_pre_msvcr100) \
   $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
-  misc/crtdll__getmainargs.c
+  misc/crtdll__getmainargs.c \
+  misc/msvcrt10__longjmpex.c
 
 src_msvcrt20=\
   $(src_pre_msvcrt40) \
diff --git a/mingw-w64-crt/lib32/msvcrt10.def.in 
b/mingw-w64-crt/lib32/msvcrt10.def.in
index f116cfec8ad6..a4448532eab7 100644
--- a/mingw-w64-crt/lib32/msvcrt10.def.in
+++ b/mingw-w64-crt/lib32/msvcrt10.def.in
@@ -1192,7 +1192,7 @@ localeconv
 localtime
 log
 log10
-longjmp
+__msvcrt10_longjmp == longjmp ; msvcrt10.dll's longjmp is incompatible with 
msvcrt.dll longjmp, real longjmp provided as wrapper
 malloc
 mblen
 mbstowcs
diff --git a/mingw-w64-crt/misc/msvcrt10__longjmpex.c 
b/mingw-w64-crt/misc/msvcrt10__longjmpex.c
new file mode 100644
index 000000000000..7b54b129a2cb
--- /dev/null
+++ b/mingw-w64-crt/misc/msvcrt10__longjmpex.c
@@ -0,0 +1,77 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#if defined(__i386__)
+
+#include <setjmp.h>
+#include <windows.h>
+
+void __cdecl _global_unwind2(EXCEPTION_REGISTRATION_RECORD *frame);
+__MINGW_ATTRIB_NORETURN __attribute__((nothrow)) void __cdecl 
__msvcrt10_longjmp(jmp_buf buf, int value);
+
+struct msvcrt10_establisher_frame {
+  EXCEPTION_REGISTRATION_RECORD ExceptionRecord;
+  const void *ScopeTable;
+  DWORD TryLevel;
+};
+
+__MINGW_ATTRIB_NORETURN
+__attribute__((nothrow))
+void __cdecl _longjmpex(jmp_buf buf, int value)
+{
+  _JUMP_BUFFER *bufptr = (void *)buf;
+
+  _Static_assert(sizeof(bufptr->Registration) == sizeof(void *), 
"_JUMP_BUFFER.Registration has not pointer size");
+  _Static_assert(sizeof(bufptr->UnwindFunc) == sizeof(void *), 
"_JUMP_BUFFER.UnwindFunc has not pointer size");
+
+  /* msvcrt10 longjmp() for non-zero bufptr->Registration is unconditionally
+   * calling local_unwind2() with bufptr->Registration as SEH establisher frame
+   * and with bufptr->TryLevel as try level.
+   *
+   * msvcrt10 local_unwind2() is expecting that the passed SEH establisher 
frame
+   * is of msvcrt10 format which contains additional fields: scope table and 
try level.
+   * It will do nothing if the SEH establisher frame's try level is -1 or if 
it is
+   * same as passed try level from function argument. Otherwise it will 
dereference
+   * scope table pointer and calls specified unwind handlers.
+   *
+   * Therefore the msvcrt10 longjmp() can be called only if the 
bufptr->Registration
+   * is valid SEH establisher frame in msvcrt10 format (with additional 
fields).
+   * All SEH handlers registered by mingw-w64 are just standard SEH establisher
+   * frame structure without any additional msvcrt10 fields. To prevent using 
and
+   * dereferencing the scope table pointer (which is after the standard SEH
+   * establisher frame), set the bufptr->TryLevel to value which msvcrt10
+   * local_unwind2() function reads, to the value from address where is the
+   * try level in the msvcrt10 SEH establisher frame format. It is some value
+   * on the stack which does not belong to the SEH establisher frame, but 
reading
+   * it should not cause any memory corruption.
+   *
+   * As msvcrt10 longjmp() does not support bufptr->Cookie and 
bufptr->UnwindFunc
+   * fields, for the non-zero bufptr->UnwindFunc case, it is needed to do all
+   * unwinds manually. msvcrt10 longjmp() is always calling the 
_global_unwind2(),
+   * but the bufptr->UnwindFunc has to be called after _global_unwind2().
+   * As _global_unwind2() is unregistering SEH handler (up to the specified 
one),
+   * it is idempotent and so can be called multiple times.
+   */
+
+  if (bufptr->Registration != 0 && !IsBadReadPtr(&bufptr->Cookie, 
sizeof(bufptr->Cookie)) && bufptr->Cookie == 0x56433230) {
+    if (bufptr->UnwindFunc != 0) {
+      if (bufptr->Registration != __readfsdword(0))
+        _global_unwind2((void *)bufptr->Registration);
+      ((void(__stdcall*)(const _JUMP_BUFFER *))bufptr->UnwindFunc)(bufptr);
+    }
+    bufptr->TryLevel = ((struct msvcrt10_establisher_frame 
*)bufptr->Registration)->TryLevel;
+  }
+
+  __msvcrt10_longjmp(buf, value);
+}
+void (__cdecl *__MINGW_IMP_SYMBOL(_longjmpex))(jmp_buf buf, int value) = 
_longjmpex;
+
+/* In msvcrt and UCRT the longjmp() function is calling SEH unwind and is 
aliased to _longjmpex() function. */
+#undef longjmp
+void __attribute__((alias("_longjmpex"))) __cdecl longjmp(jmp_buf buf, int 
value);
+extern void 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_longjmpex)))))(__cdecl
 *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf buf, int value);
+
+#endif
-- 
2.20.1


>From 5f8a9a7ae3a07795288cdacf28a9bbbb972f7299 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Thu, 8 Oct 2026 18:21:32 +0200
Subject: [PATCH 16/25] crt: Provide msvcrt/UCRT compatible longjmp and
 _longjmpex symbols for i386 ucrtapp builds

The mingw-w64 i386 longjmp implementation in mingw-w64-crt/misc/longjmp.S
used for ucrtapp builds behaves as the crtdll.dll longjmp symbol. It does
not call SEH unwind.

So implement a new _longjmpex.c file and provides a real msvcrt/UCRT
compatible longjmp and _longjmpex functions. Use it instead of longjmp.S.

The _local_unwind2 part is not implemented yet, because its code path is
not called by the code generated by mingw-w64 setjmp.h header file and its
implementation would be non-trivial.
---
 mingw-w64-crt/Makefile.am       |  1 +
 mingw-w64-crt/misc/_longjmpex.c | 79 +++++++++++++++++++++++++++++++++
 mingw-w64-crt/misc/longjmp.S    | 16 +------
 3 files changed, 82 insertions(+), 14 deletions(-)
 create mode 100644 mingw-w64-crt/misc/_longjmpex.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 1155547b42be..a4e94ad34e3e 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -506,6 +506,7 @@ src_ucrtbase=\
 
 # Files included in libucrtapp.a
 src_ucrtapp=\
+  misc/_longjmpex.c \
   misc/longjmp.S \
   misc/setjmp.S \
   string/memchr.c \
diff --git a/mingw-w64-crt/misc/_longjmpex.c b/mingw-w64-crt/misc/_longjmpex.c
new file mode 100644
index 000000000000..98c318a5dda5
--- /dev/null
+++ b/mingw-w64-crt/misc/_longjmpex.c
@@ -0,0 +1,79 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#if defined(__i386__)
+
+#include <setjmp.h>
+#include <windows.h>
+
+__MINGW_ATTRIB_NORETURN
+__attribute__((nothrow))
+void __cdecl _longjmpex(jmp_buf buf, int value)
+{
+  _JUMP_BUFFER *bufptr = (void *)buf;
+
+  _Static_assert(sizeof(bufptr->Registration) == sizeof(void *), 
"_JUMP_BUFFER.Registration has not pointer size");
+  _Static_assert(sizeof(bufptr->UnwindFunc) == sizeof(void *), 
"_JUMP_BUFFER.UnwindFunc has not pointer size");
+
+  if (bufptr->Registration != __readfsdword(0)) {
+    /* Call: global_unwind2((void *)bufptr->Registration);
+     * Function global_unwind2() is just calling the function RtlUnwind().
+     * Function RtlUnwind() cannot be easily called from C source code easily
+     * because it does not return via the return address placed on the stack
+     * but it returns to the address in the second function argument on the 
stack.
+     * And also it clobbers 3 additional callee saved ebx, esi and edi 
registers.
+     * So call the RtlUnwind((void *)bufptr->Registration, ret_addr, NULL, 
NULL);
+     * via the inline assembly.
+     */
+    asm volatile(
+      "pushl $0\n\t"
+      "pushl $0\n\t"
+      "pushl $1f\n\t" /* label after calll */
+      "pushl %%eax\n\t"
+      "calll _RtlUnwind@16\n\t"
+      "1:"
+      :
+      : "a" (bufptr->Registration) /* cannot be passed as esp-relative memory 
reference */
+      : "ecx", "edx", /* additional stdcall clobbered registers */
+        "ebx", "esi", "edi", /* additional RtlUnwind clobbered registers */
+        "cc", "memory"
+    );
+  }
+
+  if (bufptr->Registration != 0) {
+    if (!IsBadReadPtr(&bufptr->Cookie, sizeof(bufptr->Cookie)) && 
bufptr->Cookie == 0x56433230) {
+      if (bufptr->UnwindFunc != 0)
+        ((void(__stdcall*)(const _JUMP_BUFFER *))bufptr->UnwindFunc)(bufptr);
+    } else {
+      /* Call _local_unwind2((void *)bufptr->Registration, bufptr->TryLevel);
+       * TODO: mingw-w64 does not provide _local_unwind2() implementation yet.
+       */
+    }
+  }
+
+  if (value == 0) value = 1;
+  asm volatile(
+    "movl 0(%%ecx),%%ebp\n\t"  /* jmp_buf->Ebp */
+    "movl 4(%%ecx),%%ebx\n\t"  /* jmp_buf->Ebx */
+    "movl 8(%%ecx),%%edi\n\t"  /* jmp_buf->Edi */
+    "movl 12(%%ecx),%%esi\n\t" /* jmp_buf->Esi */
+    "movl 16(%%ecx),%%esp\n\t" /* jmp_buf->Esp */
+    "addl $4,%%esp\n\t"        /* get rid of return address */
+    "jmpl *20(%%ecx)"          /* jmp_buf->Eip */
+    :
+    : "a" (value), "c" (buf)
+    : "memory"
+  );
+  __builtin_unreachable();
+}
+void (__cdecl *__MINGW_IMP_SYMBOL(_longjmpex))(jmp_buf buf, int value) = 
_longjmpex;
+
+/* In msvcrt and UCRT the longjmp() function is calling SEH unwind and is 
aliased to _longjmpex() function. */
+#undef longjmp
+void __attribute__((alias("_longjmpex"))) __cdecl longjmp(jmp_buf buf, int 
value);
+extern void 
__attribute__((alias(__MINGW64_STRINGIFY(__MINGW_IMP_SYMBOL(_longjmpex)))))(__cdecl
 *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf buf, int value);
+
+#endif
diff --git a/mingw-w64-crt/misc/longjmp.S b/mingw-w64-crt/misc/longjmp.S
index a7f04d529b7f..9dce4c19b2be 100644
--- a/mingw-w64-crt/misc/longjmp.S
+++ b/mingw-w64-crt/misc/longjmp.S
@@ -6,24 +6,12 @@
 
 #include <_mingw_mac.h>
 
-#ifndef __arm64ec__
+#if !defined(__i386__) && !defined(__arm64ec__)
         .globl  __MINGW_USYMBOL(longjmp)
         .def    __MINGW_USYMBOL(longjmp); .scl 2; .type 32; .endef
 
 __MINGW_USYMBOL(longjmp):
-#if defined(__i386__)
-        movl    4(%esp),%ecx         /* jmp_buf */
-        movl    8(%esp),%eax         /* retval */
-        cmpl    $1,%eax              /* check that retval is non-zero */
-        adcl    $0,%eax              /* if not then increase by one via CF */
-        movl    0(%ecx),%ebp         /* jmp_buf.Ebp */
-        movl    4(%ecx),%ebx         /* jmp_buf.Ebx */
-        movl    8(%ecx),%edi         /* jmp_buf.Edi */
-        movl    12(%ecx),%esi        /* jmp_buf.Esi */
-        movl    16(%ecx),%esp        /* jmp_buf.Esp */
-        addl    $4,%esp              /* get rid of return address */
-        jmp     *20(%ecx)            /* jmp_buf.Eip */
-#elif defined(__x86_64__)
+#if defined(__x86_64__)
         movq    %rdx,%rax            /* retval */
         cmpq    $1,%rax              /* check that retval is non-zero */
         adcq    $0,%rax              /* if not then increase by one via CF */
-- 
2.20.1


>From 64e5d459e511f7977fd8f370a8e35e642355f256 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 27 Dec 2025 14:25:14 +0100
Subject: [PATCH 17/25] headers: Fix definition of setjmp for 32-bit x86 builds

If UnwindFunc and TryLevel arguments for 32-bit x86 _setjmp3() function are
not specified then CRT _setjmp3() function automatically fills them from
the current SEH registration frame. It expects that the SEH frame data are
compatible with the _except_handler<N>, something which is automatically
setup by MS CL compiler with __try / __except / __finally support.

These expectations do not apply for mingw-w64 code generally as in the most
cases there is registered only one top level SEH frame, which does not have
SEH frame data of _except_handler<N> format.

UnwindFunc and TryLevel arguments also have to be in the _except_handler<N>
format and CRT can pass them to the _local_unwind function. If some non-CRT
SEH handler is registered then all those local CRT unwinding parts related
to nested __try blocks have to be avoided. It can be achieved by explicitly
setting the local UnwindFunc to NULL and current TryLevel to -1.

So explicitly set UnwindFunc to NULL and TryLevel to -1 in our setjmp()
macro. This prevents _setjmp3() to inspect content of current SEH frame.
---
 mingw-w64-headers/crt/setjmp.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index 7871c02f7abb..d759f4865163 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -194,7 +194,7 @@ _CRTIMP __MINGW_ATTRIB_NORETURN __attribute__ 
((__nothrow__)) void __cdecl _long
 
 #ifdef __i386__
 _CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp(jmp_buf _Buf); /* old i386 crtdll setjmp.h */
-_CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp3(jmp_buf _Buf, int _Count, ...); /* new i386 msvcrt20+ setjmp.h and 
setjmpex.h */
+_CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp3(jmp_buf _Buf, int _Count, ... /*void *_UnwindFunc, int _TryLevel, 
6xDWORD _UnwindData...*/); /* new i386 msvcrt20+ setjmp.h and setjmpex.h */
 #else
 #ifndef __aarch64__
 _CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp(jmp_buf _Buf, void *_Frame); /* for non-i386 and non-aarch64 setjmp.h */
@@ -229,7 +229,7 @@ _CRT_END_C_HEADER
 #  undef setjmp
 #endif
 #if defined(__i386__)
-#  define setjmp(BUF) _setjmp3((BUF), 0)
+#  define setjmp(BUF) _setjmp3((BUF), 2, NULL /*UnwindFunc*/, -1 /*TryLevel*/)
 #elif !defined(_INC_SETJMPEX) && (!defined(__SEH__) || 
defined(__USE_MINGW_SETJMP_NON_SEH))
 #  if defined(__arm__) || defined(__aarch64__)
 #    define setjmp(BUF) __mingw_setjmp((BUF))
-- 
2.20.1


>From bdbc1655d17ffc154186b27b51365a703d332247 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Mon, 12 Oct 2026 14:09:11 +0200
Subject: [PATCH 18/25] crt: Change amd64, arm32 and arm64 longjmp()
 implementation for ucrtapp builds

Native msvcrt and UCRT longjmp() functions on amd64, arm32 and arm64 are
always doing SEH unwind. The longjmp functionality is done via the RtlUnwind()
call with STATUS_LONGJUMP exception code. Only longjmp on amd64 is doing
fallback without SEH unwind when tieh jmp_buf Frame is not filled.

Implement same behavior for ucrtapp builds. For simplicity implement this
code in C (instead of asm) and for the amd64 fallback part use inline asm.
---
 mingw-w64-crt/Makefile.am    |  2 +-
 mingw-w64-crt/misc/longjmp.S | 98 ------------------------------------
 mingw-w64-crt/misc/longjmp.c | 74 +++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 99 deletions(-)
 delete mode 100644 mingw-w64-crt/misc/longjmp.S
 create mode 100644 mingw-w64-crt/misc/longjmp.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index a4e94ad34e3e..7d367b1e03ac 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -507,7 +507,7 @@ src_ucrtbase=\
 # Files included in libucrtapp.a
 src_ucrtapp=\
   misc/_longjmpex.c \
-  misc/longjmp.S \
+  misc/longjmp.c \
   misc/setjmp.S \
   string/memchr.c \
   string/memcmp.c \
diff --git a/mingw-w64-crt/misc/longjmp.S b/mingw-w64-crt/misc/longjmp.S
deleted file mode 100644
index 9dce4c19b2be..000000000000
--- a/mingw-w64-crt/misc/longjmp.S
+++ /dev/null
@@ -1,98 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-
-#include <_mingw_mac.h>
-
-#if !defined(__i386__) && !defined(__arm64ec__)
-        .globl  __MINGW_USYMBOL(longjmp)
-        .def    __MINGW_USYMBOL(longjmp); .scl 2; .type 32; .endef
-
-__MINGW_USYMBOL(longjmp):
-#if defined(__x86_64__)
-        movq    %rdx,%rax            /* retval */
-        cmpq    $1,%rax              /* check that retval is non-zero */
-        adcq    $0,%rax              /* if not then increase by one via CF */
-        movq    0x8(%rcx),%rbx       /* jmp_buf->Rbx */
-        movq    0x18(%rcx),%rbp      /* jmp_buf->Rbp */
-        movq    0x20(%rcx),%rsi      /* jmp_buf->Rsi */
-        movq    0x28(%rcx),%rdi      /* jmp_buf->Rdi */
-        movq    0x30(%rcx),%r12      /* jmp_buf->R12 */
-        movq    0x38(%rcx),%r13      /* jmp_buf->R13 */
-        movq    0x40(%rcx),%r14      /* jmp_buf->R14 */
-        movq    0x48(%rcx),%r15      /* jmp_buf->R15 */
-        ldmxcsr 0x58(%rcx)           /* jmp_buf->MxCsr */
-        fnclex
-        fldcw   0x5c(%rcx)           /* jmp_buf->FpCsr */
-        movdqa  0x60(%rcx),%xmm6     /* jmp_buf->Xmm6 */
-        movdqa  0x70(%rcx),%xmm7     /* jmp_buf->Xmm7 */
-        movdqa  0x80(%rcx),%xmm8     /* jmp_buf->Xmm8 */
-        movdqa  0x90(%rcx),%xmm9     /* jmp_buf->Xmm9 */
-        movdqa  0xa0(%rcx),%xmm10    /* jmp_buf->Xmm10 */
-        movdqa  0xb0(%rcx),%xmm11    /* jmp_buf->Xmm11 */
-        movdqa  0xc0(%rcx),%xmm12    /* jmp_buf->Xmm12 */
-        movdqa  0xd0(%rcx),%xmm13    /* jmp_buf->Xmm13 */
-        movdqa  0xe0(%rcx),%xmm14    /* jmp_buf->Xmm14 */
-        movdqa  0xf0(%rcx),%xmm15    /* jmp_buf->Xmm15 */
-        movq    0x50(%rcx),%rdx      /* jmp_buf->Rip */
-        movq    0x10(%rcx),%rsp      /* jmp_buf->Rsp */
-        jmp     *%rdx
-#elif defined(__arm__)
-        ldr     r4,  [r0, #0x4]      /* jmp_buf->R4 */
-        ldr     r5,  [r0, #0x8]      /* jmp_buf->R5 */
-        ldr     r6,  [r0, #0xc]      /* jmp_buf->R6 */
-        ldr     r7,  [r0, #0x10]     /* jmp_buf->R7 */
-        ldr     r8,  [r0, #0x14]     /* jmp_buf->R8 */
-        ldr     r9,  [r0, #0x18]     /* jmp_buf->R9 */
-        ldr     r10, [r0, #0x1c]     /* jmp_buf->R10 */
-        ldr     r11, [r0, #0x20]     /* jmp_buf->R11 */
-        ldr     sp,  [r0, #0x24]     /* jmp_buf->Sp */
-        ldr     r2,  [r0, #0x28]     /* jmp_buf->Pc */
-        ldr     r3,  [r0, #0x2c]     /* jmp_buf->Fpscr */
-        vmsr    fpscr, r3
-        vldr    d8,  [r0, #0x30]     /* jmp_buf->D[0] */
-        vldr    d9,  [r0, #0x38]     /* jmp_buf->D[1] */
-        vldr    d10, [r0, #0x40]     /* jmp_buf->D[2] */
-        vldr    d11, [r0, #0x48]     /* jmp_buf->D[3] */
-        vldr    d12, [r0, #0x50]     /* jmp_buf->D[4] */
-        vldr    d13, [r0, #0x58]     /* jmp_buf->D[5] */
-        vldr    d14, [r0, #0x60]     /* jmp_buf->D[6] */
-        vldr    d15, [r0, #0x68]     /* jmp_buf->D[7] */
-        mov     r0,  r1              /* retval */
-        cmp     r1,  #1
-        it      ls
-        movls   r0,  #1
-        bx      r2
-#elif defined(__aarch64__)
-        ldp     x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */
-        ldp     x21, x22, [x0, #0x20] /* jmp_buf->X21, X22 */
-        ldp     x23, x24, [x0, #0x30] /* jmp_buf->X23, X24 */
-        ldp     x25, x26, [x0, #0x40] /* jmp_buf->X25, X26 */
-        ldp     x27, x28, [x0, #0x50] /* jmp_buf->X27, X28 */
-        ldp     x29, x30, [x0, #0x60] /* jmp_buf->Fp,  Lr  */
-        ldr     x2,       [x0, #0x70] /* jmp_buf->Sp */
-        mov     sp,  x2
-        ldr     w2,       [x0, #0x78] /* jmp_buf->Fpcr */
-        msr     fpcr, x2
-        ldr     w2,       [x0, #0x7c] /* jmp_buf->Fpsr */
-        msr     fpsr, x2
-        ldp     d8,  d9,  [x0, #0x80] /* jmp_buf->D[0-1] */
-        ldp     d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */
-        ldp     d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */
-        ldp     d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */
-        cmp     x1,  #1
-        csinc   x0,  x1,  xzr,  hi    /* retval */
-        ret
-#endif
-
-        .data
-        .globl  __MINGW_IMP_LSYMBOL(longjmp)
-__MINGW_IMP_LSYMBOL(longjmp):
-#ifdef _WIN64
-        .quad   __MINGW_USYMBOL(longjmp)
-#else
-        .long   __MINGW_USYMBOL(longjmp)
-#endif
-#endif /* __arm64ec__ */
diff --git a/mingw-w64-crt/misc/longjmp.c b/mingw-w64-crt/misc/longjmp.c
new file mode 100644
index 000000000000..65cdcd4724c4
--- /dev/null
+++ b/mingw-w64-crt/misc/longjmp.c
@@ -0,0 +1,74 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#if !defined(__i386__)
+
+#include <windows.h>
+#include <setjmp.h>
+
+#if defined(__x86_64__)
+#define IP Rip
+#elif defined(__arm__)
+#define IP Pc
+#elif defined(__aarch64__)
+#define IP Lr
+#else
+#error Unknown platform
+#endif
+
+#undef longjmp
+__MINGW_ATTRIB_NORETURN __attribute__((nothrow))
+void __cdecl longjmp(jmp_buf buf, int value)
+{
+    if (value == 0) value = 1;
+#if defined(__x86_64__) && !defined(__arm64ec__)
+    /* only amd64 longjmp fallback to no-SEH unwind variant when SEH frame is 
NULL */
+    if (__builtin_expect(((_JUMP_BUFFER *)buf)->Frame == 0, 0)) {
+        asm volatile (
+            "movq    0x08(%%rcx),%%rbx\n\t"      /* jmp_buf->Rbx */
+            "movq    0x18(%%rcx),%%rbp\n\t"      /* jmp_buf->Rbp */
+            "movq    0x20(%%rcx),%%rsi\n\t"      /* jmp_buf->Rsi */
+            "movq    0x28(%%rcx),%%rdi\n\t"      /* jmp_buf->Rdi */
+            "movq    0x30(%%rcx),%%r12\n\t"      /* jmp_buf->R12 */
+            "movq    0x38(%%rcx),%%r13\n\t"      /* jmp_buf->R13 */
+            "movq    0x40(%%rcx),%%r14\n\t"      /* jmp_buf->R14 */
+            "movq    0x48(%%rcx),%%r15\n\t"      /* jmp_buf->R15 */
+            "ldmxcsr 0x58(%%rcx)\n\t"            /* jmp_buf->MxCsr */
+            "fnclex\n\t"
+            "fldcw   0x5c(%%rcx)\n\t"            /* jmp_buf->FpCsr */
+            "movdqa  0x60(%%rcx),%%xmm6\n\t"     /* jmp_buf->Xmm6 */
+            "movdqa  0x70(%%rcx),%%xmm7\n\t"     /* jmp_buf->Xmm7 */
+            "movdqa  0x80(%%rcx),%%xmm8\n\t"     /* jmp_buf->Xmm8 */
+            "movdqa  0x90(%%rcx),%%xmm9\n\t"     /* jmp_buf->Xmm9 */
+            "movdqa  0xa0(%%rcx),%%xmm10\n\t"    /* jmp_buf->Xmm10 */
+            "movdqa  0xb0(%%rcx),%%xmm11\n\t"    /* jmp_buf->Xmm11 */
+            "movdqa  0xc0(%%rcx),%%xmm12\n\t"    /* jmp_buf->Xmm12 */
+            "movdqa  0xd0(%%rcx),%%xmm13\n\t"    /* jmp_buf->Xmm13 */
+            "movdqa  0xe0(%%rcx),%%xmm14\n\t"    /* jmp_buf->Xmm14 */
+            "movdqa  0xf0(%%rcx),%%xmm15\n\t"    /* jmp_buf->Xmm15 */
+            "movq    0x50(%%rcx),%%rdx\n\t"      /* jmp_buf->Rip */
+            "movq    0x10(%%rcx),%%rsp\n\t"      /* jmp_buf->Rsp */
+            "jmp     *%%rdx"
+            :
+            : "a" (value), "c" (buf)
+            : "memory"
+        );
+        __builtin_unreachable();
+    }
+#endif
+    EXCEPTION_RECORD rec;
+    rec.ExceptionCode = STATUS_LONGJUMP;
+    rec.ExceptionFlags = 0;
+    rec.ExceptionRecord = NULL;
+    rec.ExceptionAddress = NULL;
+    rec.NumberParameters = 1;
+    rec.ExceptionInformation[0] = (ULONG_PTR)buf;
+    RtlUnwind((PVOID)((_JUMP_BUFFER *)buf)->Frame, (PVOID)((_JUMP_BUFFER 
*)buf)->IP, &rec, (PVOID)(ULONG_PTR)(value));
+    __builtin_unreachable();
+}
+void (__cdecl *__MINGW_IMP_SYMBOL(longjmp))(jmp_buf, int) = longjmp;
+
+#endif
-- 
2.20.1


>From 03abec8ea09942229286232b59c99839d6b91656 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Fri, 16 Oct 2026 13:09:34 +0200
Subject: [PATCH 19/25] crt: Remove duplicate misc/arm64ec/longjmp.c for
 ucrtapp

Exactly same arm64ec functionality for ucrtapp is already provided by 
misc/longjmp.c.
---
 mingw-w64-crt/Makefile.am            |  1 -
 mingw-w64-crt/misc/arm64ec/longjmp.c | 35 ----------------------------
 2 files changed, 36 deletions(-)
 delete mode 100644 mingw-w64-crt/misc/arm64ec/longjmp.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 7d367b1e03ac..1dde6c6abe59 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -524,7 +524,6 @@ src_ucrtapp=\
 
 if ARM64EC
 src_ucrtapp_arm64=\
-  misc/arm64ec/longjmp.c \
   misc/arm64ec/setjmp.c
 endif
 
diff --git a/mingw-w64-crt/misc/arm64ec/longjmp.c 
b/mingw-w64-crt/misc/arm64ec/longjmp.c
deleted file mode 100644
index e295f31d68e8..000000000000
--- a/mingw-w64-crt/misc/arm64ec/longjmp.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-
-#ifdef __arm64ec__
-
-#undef __MSVCRT_VERSION__
-#define _UCRT
-
-#include <setjmp.h>
-#include <windef.h>
-#include <winbase.h>
-
-void __cdecl longjmp( jmp_buf b, int retval )
-{
-    _JUMP_BUFFER *buf = (_JUMP_BUFFER *)b;
-    EXCEPTION_RECORD rec;
-
-    if (!retval) retval = 1;
-
-    rec.ExceptionCode = STATUS_LONGJUMP;
-    rec.ExceptionFlags = 0;
-    rec.ExceptionRecord = NULL;
-    rec.ExceptionAddress = NULL;
-    rec.NumberParameters = 1;
-    rec.ExceptionInformation[0] = (DWORD_PTR)buf;
-    RtlUnwind( (void *)buf->Frame, (void *)buf->Rip, &rec, IntToPtr(retval) );
-    __builtin_unreachable();
-}
-
-void (__cdecl *__MINGW_IMP_SYMBOL(longjmp))( jmp_buf b, int retval ) = longjmp;
-
-#endif
-- 
2.20.1


>From 78b9bb98bcd7a20c8aa339eaeb2bb6ef44d52679 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Fri, 9 Oct 2026 00:44:31 +0200
Subject: [PATCH 20/25] github: Run CI test cases also against ucrtapp

Just recompile and rerun test cases during the ucrt build as ucrt and
ucrtapp should be ABI compatible. This will save CI time and avoids
recompiling of whole code base.

For linking test cases against ucrtapp, use gcc option -mcrtdll=ucrtapp or
clang option -lucrtapp (which replaces the implicit -lucrt library).

The testing against clang ucrtapp on i686 and x86_64 is currently failing
on complex math tests. These failures are there for a longer time, so mark
clang ucrtapp testing with "continue-on-error: true" for now until those
complex math issues are resolved. There is no failure for gcc ucrtapp.
---
 .github/workflows/build.yml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6ff48bc09576..ccab3860fc55 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -686,6 +686,15 @@ jobs:
           export PATH=/llvm-mingw/bin:$PATH
           cd mingw-w64-crt/build
           make -j$(nproc) check -k -O --no-print-directory || { cat 
testcases/test-suite.log; exit 1; }
+      - name: Run mingw-w64-crt testcases for ucrtapp
+        if: matrix.arch != 'armv7' && matrix.crt == 'ucrt'
+        continue-on-error: true
+        run: |
+          export PATH=/llvm-mingw/bin:$PATH
+          cd mingw-w64-crt/build
+          make -j$(nproc) -C testcases clean
+          make -j$(nproc) check -k -O --no-print-directory LDFLAGS='-static 
-lucrtapp' TESTS=
+          make -j$(nproc) check -k -O --no-print-directory LDFLAGS='-static 
-lucrtapp' || { cat testcases/test-suite.log; exit 1; }
       - name: Build winpthreads and tests
         run: |
           export PATH=/llvm-mingw/bin:$PATH
@@ -777,6 +786,14 @@ jobs:
           export PATH=/gcc-mingw/bin:$PATH
           ${{matrix.arch}}-w64-mingw32-gcc -std=c99 -march=i386 -o 
t_intrinc.exe mingw-w64-crt/testcases/t_intrinc.c 
mingw-w64-crt/testcases/libtest.c
           ./t_intrinc.exe
+      - name: Run testcases for ucrtapp
+        if: matrix.crt == 'ucrt'
+        run: |
+          export PATH=/gcc-mingw/bin:$PATH
+          cd mingw-w64-crt/build
+          make -j$(nproc) -C testcases clean
+          make -j$(nproc) check -k -O --no-print-directory LDFLAGS='-static 
-mcrtdll=ucrtapp' TESTS=
+          make -j$(nproc) check -k -O --no-print-directory LDFLAGS='-static 
-mcrtdll=ucrtapp' || { cat testcases/test-suite.log; exit 1; }
       - name: Build and test winpthreads
         run: |
           export PATH=/gcc-mingw/bin:$PATH
-- 
2.20.1


>From 6f213ed61ffae30f87667378a66f0c060e5e65b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Mon, 12 Oct 2026 13:45:28 +0200
Subject: [PATCH 21/25] headers: In setjmpex.h check for compiler SEH support

Inclusion of setjmpex.h header file forces compiler to use SEH-safe
setjmp/longjmp functions. If the code generated by the compiler is not
SEH-compatible then throw a compiler error. It applies for non-i386
platforms when __SEH__ macro is not defined by compiler itself (for example
when compiler is non-SEH or when SEH unwind data were explicitly requested
to be omitted by gcc -fno-asynchronous-unwind-tables option). This prevents
generating incompatible / wrong code by compiler when setjmpex.h is
explicitly requested by application.
---
 mingw-w64-headers/crt/setjmpex.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mingw-w64-headers/crt/setjmpex.h b/mingw-w64-headers/crt/setjmpex.h
index a1017c3e9e4c..bb103a69547c 100644
--- a/mingw-w64-headers/crt/setjmpex.h
+++ b/mingw-w64-headers/crt/setjmpex.h
@@ -6,5 +6,8 @@
 #ifndef _INC_SETJMPEX
 /* setjmp.h defines setjmp() and longjmp() based on _INC_SETJMPEX */
 #define _INC_SETJMPEX
+#if !defined(__i386__) && !defined(__SEH__)
+#error setjmpex.h for non-i386 platforms requires SEH
+#endif
 #include <setjmp.h>
 #endif
-- 
2.20.1


>From 5774916f1658611832cd128e74d7d38f4265b3e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Mon, 12 Oct 2026 13:50:25 +0200
Subject: [PATCH 22/25] headers: Fix and improve setjmp() and longjmp() macros
 in setjmp.h

On amd64 _setjmp and _setjmpex functions require current stack pointer
address (%rsp) as its second argument, not the frame pointer address.
So it is needed to pass __builtin_stack_address() or mingw_getsp().

On arm32, arm64 and arm64ec these functions require stack pointer address
from the function entry point. So it is needed to pass __builtin_sponentry().
Specially arm64ec does not follow the amd64 abi.

On amd64 and arm32 follows the setjmpex.h usage and define setjmp macro as
expansion to _setjmpex or _setjmp function based on _INC_SETJMPEX. arm64
libraries do not provide _setjmp function, so always use _setjmpex only.

On i386 follows the setjmpex.h usage and define longjmp macro as expansion
to longjmp or _longjmpex function based on _INC_SETJMPEX.

This is the msvc behavior and can be verified from the assembler output
generate by the cl.exe.

Also if the current vs frame vs entry point stack pointer address is mixed
and not correctly supplied then the t_longjmp_unwind test is randomly
failing on error that not all SEH error handlers are called. This applies
also in the case if for the arm64ec is supplied current stack pointer
address instead of stack pointer address from the function entry point.

As extension to this behavior, follows the __USE_MINGW_SETJMP_NON_SEH macro
usage. On i386, arm32 and arm64 expands the longjmp macro to the
__mingw_longjmp function. In all cases, use the CRT functions for setjmp.
It is not needed to call mingw-w64 emulation of setjmp as CRT function can
avoid filling SEH frame into the jmp_buf structure.
---
 mingw-w64-headers/crt/setjmp.h | 69 ++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 28 deletions(-)

diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index d759f4865163..19f1ce257774 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -220,51 +220,64 @@ _CRT_END_C_HEADER
  * It allows '#include <setjmpex.h>' to override the existing setjmp() and 
longjmp() definitions.
  */
 
-#pragma push_macro("__has_builtin")
-#ifndef __has_builtin
-  #define __has_builtin(x) 0
-#endif
-
 #ifdef setjmp
 #  undef setjmp
 #endif
-#if defined(__i386__)
+#if defined(__i386__) /* i386 _setjmp and _setjmp3 functions always store SEH 
registration frame for unwind */
 #  define setjmp(BUF) _setjmp3((BUF), 2, NULL /*UnwindFunc*/, -1 /*TryLevel*/)
-#elif !defined(_INC_SETJMPEX) && (!defined(__SEH__) || 
defined(__USE_MINGW_SETJMP_NON_SEH))
-#  if defined(__arm__) || defined(__aarch64__)
-#    define setjmp(BUF) __mingw_setjmp((BUF))
+#elif defined(__x86_64__) && !defined(__arm64ec__) /* amd64 requires current 
stack pointer address */
+#  if defined(_INC_SETJMPEX)
+#    if (defined(__GNUC__) && __GNUC__ >= 14) || (defined(__clang__) && 
__clang_major__ >= 22)
+#      define setjmp(BUF) _setjmpex((BUF), __builtin_stack_address())
+#    else
+#      define setjmp(BUF) _setjmpex((BUF), mingw_getsp())
+#    endif
+#  elif defined(__SEH__) && !defined(__USE_MINGW_SETJMP_NON_SEH)
+#    if (defined(__GNUC__) && __GNUC__ >= 14) || (defined(__clang__) && 
__clang_major__ >= 22)
+#      define setjmp(BUF) _setjmp((BUF), __builtin_stack_address())
+#    else
+#      define setjmp(BUF) _setjmp((BUF), mingw_getsp())
+#    endif
 #  else
 #    define setjmp(BUF) _setjmp((BUF), NULL)
 #  endif
-#elif !defined(_INC_SETJMPEX) && !defined(__aarch64__)
-#  if __has_builtin(__builtin_sponentry)
+#elif defined(__arm__) || defined(__arm64ec__) /* arm32 and arm64ec require 
stack pointer address from the function entry point */
+#  if defined(_INC_SETJMPEX)
+#    define setjmp(BUF) _setjmpex((BUF), __builtin_sponentry())
+#  elif defined(__SEH__) && !defined(__USE_MINGW_SETJMP_NON_SEH)
 #    define setjmp(BUF) _setjmp((BUF), __builtin_sponentry())
-#  elif (__MINGW_GCC_VERSION < 40702) && !defined(__clang__)
-#    define setjmp(BUF) _setjmp((BUF), mingw_getsp())
 #  else
-#    define setjmp(BUF) _setjmp((BUF), __builtin_frame_address(0))
+#    define setjmp(BUF) _setjmp((BUF), NULL)
 #  endif
-#else
-#  if __has_builtin(__builtin_sponentry)
+#elif defined(__aarch64__) /* arm64 requires stack pointer address from the 
function entry point and arm64 does not have _setjmp function */
+#  if defined(_INC_SETJMPEX) || (defined(__SEH__) && 
!defined(__USE_MINGW_SETJMP_NON_SEH))
 #    define setjmp(BUF) _setjmpex((BUF), __builtin_sponentry())
-#  elif (__MINGW_GCC_VERSION < 40702) && !defined(__clang__)
-#    define setjmp(BUF) _setjmpex((BUF), mingw_getsp())
 #  else
-#    define setjmp(BUF) _setjmpex((BUF), __builtin_frame_address(0))
+#    define setjmp(BUF) _setjmpex((BUF), NULL)
 #  endif
+#else
+#  error Unknown platform
 #endif
 
 #ifdef longjmp
 #  undef longjmp
 #endif
-#if defined(_INC_SETJMPEX) && defined(__i386__)
-#  define longjmp _longjmpex
-#elif !defined(_INC_SETJMPEX) && defined(__USE_MINGW_SETJMP_NON_SEH) && 
defined(__i386__)
-#  define longjmp __mingw_longjmp
-#elif !defined(_INC_SETJMPEX) && (!defined(__SEH__) || 
defined(__USE_MINGW_SETJMP_NON_SEH)) && (defined(__arm__) || 
defined(__aarch64__))
-#  define longjmp __mingw_longjmp
-#else
+#if defined(__i386__) /* i386 longjmp function always uses SEH registration 
frame for unwind */
+#  if defined(_INC_SETJMPEX)
+#    define longjmp _longjmpex
+#  elif !defined(__USE_MINGW_SETJMP_NON_SEH)
+#    define longjmp longjmp
+#  else
+#    define longjmp __mingw_longjmp
+#  endif
+#elif defined(__x86_64__) /* amd64 longjmp function uses SEH frame for unwind 
on demand only when is non-NULL */
 #  define longjmp longjmp
+#elif defined(__arm__) || defined(__aarch64__) /* arm32 and arm64 longjmp 
functions always use SEH frame for unwind */
+#  if defined(_INC_SETJMPEX) || (defined(__SEH__) && 
!defined(__USE_MINGW_SETJMP_NON_SEH))
+#    define longjmp longjmp
+#  else
+#    define longjmp __mingw_longjmp
+#  endif
+#else
+#  error Unknown platform
 #endif
-
-#pragma pop_macro("__has_builtin")
-- 
2.20.1


>From 55824a356b096038141104057e59898dfa2601aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Tue, 20 Oct 2026 11:32:55 +0200
Subject: [PATCH 23/25] crt: Implement __mingw_longjmp for arm64ec builds

__mingw_longjmp function is the longjmp variant which is not invoking SEH
unwind but directly changes registers.

It is used for the __USE_MINGW_SETJMP_NON_SEH behavior on arm systems
because arm32, arm64 and also arm64ec longjmp always requires valid SEH
frame in jmp_buf and is always doing SEH unwind.

This change fixes the setjmp.h __USE_MINGW_SETJMP_NON_SEH for arm64ec.

Also unskip the failing arm64ec t_longjmp_nounwind test.
---
 mingw-w64-crt/Makefile.am                     |  1 +
 .../misc/arm64ec/__mingw_mxcsr_to_fpcsr.c     | 35 +++++++++++++++++++
 mingw-w64-crt/misc/mingw_longjmp.S            | 30 +++++++++++++++-
 mingw-w64-crt/testcases/t_longjmp_unwind.c    |  7 ----
 mingw-w64-headers/crt/setjmp.h                |  6 ++--
 5 files changed, 68 insertions(+), 11 deletions(-)
 create mode 100644 mingw-w64-crt/misc/arm64ec/__mingw_mxcsr_to_fpcsr.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 1dde6c6abe59..2af0d0fec3f0 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -1292,6 +1292,7 @@ src_libmingwex=\
   math/powi.def.h       math/sqrt.def.h    \
   math/cephes_mconf.h   math/fp_consts.h   math/pi_const.h \
   \
+  misc/arm64ec/__mingw_mxcsr_to_fpcsr.c \
   misc/mingw_longjmp.S \
   misc/mingw_getsp.S \
   misc/alarm.c \
diff --git a/mingw-w64-crt/misc/arm64ec/__mingw_mxcsr_to_fpcsr.c 
b/mingw-w64-crt/misc/arm64ec/__mingw_mxcsr_to_fpcsr.c
new file mode 100644
index 000000000000..8aaeb2a2ba61
--- /dev/null
+++ b/mingw-w64-crt/misc/arm64ec/__mingw_mxcsr_to_fpcsr.c
@@ -0,0 +1,35 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#ifdef __arm64ec__
+
+unsigned long long __mingw_mxcsr_to_fpcsr(unsigned mxcsr);
+unsigned long long __mingw_mxcsr_to_fpcsr(unsigned mxcsr)
+{
+  unsigned fpsr = 0;
+  if (mxcsr & 0x0001)    fpsr |= 0x0001;      /* invalid operation */
+  if (mxcsr & 0x0004)    fpsr |= 0x0002;      /* zero-divide */
+  if (mxcsr & 0x0008)    fpsr |= 0x0004;      /* overflow */
+  if (mxcsr & 0x0010)    fpsr |= 0x0008;      /* underflow */
+  if (mxcsr & 0x0020)    fpsr |= 0x0010;      /* precision */
+  if (mxcsr & 0x0002)    fpsr |= 0x0080;      /* denormal */
+
+  unsigned fpcr = 0;
+  if (!(mxcsr & 0x0080)) fpcr |= 0x0000100;   /* invalid operation mask */
+  if (!(mxcsr & 0x0200)) fpcr |= 0x0000200;   /* zero-divide mask */
+  if (!(mxcsr & 0x0400)) fpcr |= 0x0000400;   /* overflow mask */
+  if (!(mxcsr & 0x0800)) fpcr |= 0x0000800;   /* underflow mask */
+  if (!(mxcsr & 0x1000)) fpcr |= 0x0001000;   /* precision mask */
+  if (!(mxcsr & 0x0100)) fpcr |= 0x0008000;   /* denormal mask */
+  if (mxcsr & 0x0040)    fpcr |= 0x0080000;   /* denormals are zero */
+  if (mxcsr & 0x4000)    fpcr |= 0x0400000;   /* round up */
+  if (mxcsr & 0x2000)    fpcr |= 0x0800000;   /* round down */
+  if (mxcsr & 0x8000)    fpcr |= 0x1000000;   /* flush to zero */
+
+  return ((unsigned long long)fpsr << 32) | fpcr;
+}
+
+#endif
diff --git a/mingw-w64-crt/misc/mingw_longjmp.S 
b/mingw-w64-crt/misc/mingw_longjmp.S
index 4d2da261aca6..b44ccfd4124a 100644
--- a/mingw-w64-crt/misc/mingw_longjmp.S
+++ b/mingw-w64-crt/misc/mingw_longjmp.S
@@ -57,7 +57,7 @@ __MINGW_USYMBOL(__mingw_setjmp):
 #endif
 #endif
 
-#if defined(__i386__) || defined(_ARM_) || defined(__arm__) || 
defined(_ARM64_) || defined(__aarch64__)
+#if defined(__i386__) || defined(_ARM_) || defined(__arm__) || 
defined(_ARM64_) || defined(__aarch64__) || defined(__arm64ec__)
        .globl __MINGW_USYMBOL(__mingw_longjmp)
        .def    __MINGW_USYMBOL(__mingw_longjmp);       .scl    2;      .type   
32;     .endef
 __MINGW_USYMBOL(__mingw_longjmp):
@@ -119,5 +119,33 @@ __MINGW_USYMBOL(__mingw_longjmp):
        cmp     x1,  #1
        csinc   x0,  x1,  xzr,  hi    /* retval */
        ret
+#elif defined(__arm64ec__)
+       mov     x19, x0
+       mov     x20, x1
+       ldr     x0,       [x0, #0x58] /* jmp_buf->MxCsr */
+       bl      "#__mingw_mxcsr_to_fpcsr"
+       uxtw    x2,  w0
+       msr     fpcr, x2
+       lsr     x2,  x0,  #32
+       msr     fpsr, x2
+       mov     x0,  x19
+       mov     x1,  x20
+
+       ldr     x27,      [x0, #0x08] /* jmp_buf->Rbx */
+       ldr     x2,       [x0, #0x10] /* jmp_buf->Rsp */
+       mov     sp,  x2
+       ldr     x29,      [x0, #0x18] /* jmp_buf->Rbp */
+       ldp     x25, x26, [x0, #0x20] /* jmp_buf->Rsi,Rdi */
+       ldp     x19, x20, [x0, #0x30] /* jmp_buf->R12,R13 */
+       ldp     x21, x22, [x0, #0x40] /* jmp_buf->R14,R15 */
+       ldr     x30,      [x0, #0x50] /* jmp_buf->Rip */
+       ldp     d6,  d7,  [x0, #0x60] /* jmp_buf->Xmm6,Xmm7 */
+       ldp     d8,  d9,  [x0, #0x80] /* jmp_buf->Xmm8,Xmm9 */
+       ldp     d10, d11, [x0, #0xa0] /* jmp_buf->Xmm10,Xmm11 */
+       ldp     d12, d13, [x0, #0xc0] /* jmp_buf->Xmm12,Xmm13 */
+       ldp     d14, d15, [x0, #0xe0] /* jmp_buf->Xmm14,Xmm15 */
+       cmp     x1,  #1
+       csinc   x0,  x1,  xzr,  hi    /* retval */
+       ret
 #endif
 #endif
diff --git a/mingw-w64-crt/testcases/t_longjmp_unwind.c 
b/mingw-w64-crt/testcases/t_longjmp_unwind.c
index 7dc554e2551c..3b02ab8f0cfe 100644
--- a/mingw-w64-crt/testcases/t_longjmp_unwind.c
+++ b/mingw-w64-crt/testcases/t_longjmp_unwind.c
@@ -208,13 +208,6 @@ int main(int argc, char **argv)
   printf("SKIP: No SEH support\n");
   return 77;
 #endif
-#endif
-
-#ifdef TEST_NOUNWIND
-#if defined(__arm64ec__)
-  printf("SKIP: __USE_MINGW_SETJMP_NON_SEH is broken on arm64ec\n");
-  return 77;
-#endif
 #endif
 
   outer_unwind_called = 0;
diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index 19f1ce257774..15e5b4f14e86 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -205,7 +205,7 @@ _CRTIMP int __cdecl __attribute__ 
((__nothrow__,__returns_twice__)) _setjmpex(jm
 #if defined(__arm__) || defined(__aarch64__)
 int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
__mingw_setjmp(jmp_buf _Buf);
 #endif
-#if defined(__i386__) || defined(__arm__) || defined(__aarch64__)
+#if defined(__i386__) || defined(__arm__) || defined(__aarch64__) || 
defined(__arm64ec__)
 __MINGW_ATTRIB_NORETURN __attribute__ ((__nothrow__)) void 
__mingw_longjmp(jmp_buf _Buf, int _Value);
 #endif
 
@@ -270,9 +270,9 @@ _CRT_END_C_HEADER
 #  else
 #    define longjmp __mingw_longjmp
 #  endif
-#elif defined(__x86_64__) /* amd64 longjmp function uses SEH frame for unwind 
on demand only when is non-NULL */
+#elif defined(__x86_64__) && !defined(__arm64ec__) /* amd64 longjmp function 
uses SEH frame for unwind on demand only when is non-NULL */
 #  define longjmp longjmp
-#elif defined(__arm__) || defined(__aarch64__) /* arm32 and arm64 longjmp 
functions always use SEH frame for unwind */
+#elif defined(__arm__) || defined(__aarch64__) || defined(__arm64ec__) /* 
arm32, arm64 and arm64ec longjmp functions always use SEH frame for unwind */
 #  if defined(_INC_SETJMPEX) || (defined(__SEH__) && 
!defined(__USE_MINGW_SETJMP_NON_SEH))
 #    define longjmp longjmp
 #  else
-- 
2.20.1


>From c13500f8500a0ca048692b1ed1af1c2795158659 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 11 Oct 2026 20:17:47 +0200
Subject: [PATCH 24/25] crt: Remove unused arm function __mingw_setjmp

This function is not used anymore, its usage was replaced by _setjmpex(buf, 
NULL)
which has same functionality. So remove this function.
---
 mingw-w64-crt/misc/mingw_longjmp.S | 52 ------------------------------
 mingw-w64-headers/crt/setjmp.h     |  3 --
 2 files changed, 55 deletions(-)

diff --git a/mingw-w64-crt/misc/mingw_longjmp.S 
b/mingw-w64-crt/misc/mingw_longjmp.S
index b44ccfd4124a..3b194f5baf2a 100644
--- a/mingw-w64-crt/misc/mingw_longjmp.S
+++ b/mingw-w64-crt/misc/mingw_longjmp.S
@@ -5,58 +5,6 @@
  */
 #include <_mingw_mac.h>
 
-#if defined(_ARM_) || defined(__arm__) || defined(_ARM64_) || 
defined(__aarch64__)
-       .globl __MINGW_USYMBOL(__mingw_setjmp)
-       .def    __MINGW_USYMBOL(__mingw_setjmp);        .scl    2;      .type   
32;     .endef
-__MINGW_USYMBOL(__mingw_setjmp):
-#if defined(_ARM_) || defined(__arm__)
-       mov     r1,  #0
-       str     r1,  [r0]            /* jmp_buf->Frame */
-       str     r4,  [r0, #0x4]      /* jmp_buf->R4 */
-       str     r5,  [r0, #0x8]      /* jmp_buf->R5 */
-       str     r6,  [r0, #0xc]      /* jmp_buf->R6 */
-       str     r7,  [r0, #0x10]     /* jmp_buf->R7 */
-       str     r8,  [r0, #0x14]     /* jmp_buf->R8 */
-       str     r9,  [r0, #0x18]     /* jmp_buf->R9 */
-       str     r10, [r0, #0x1c]     /* jmp_buf->R10 */
-       str     r11, [r0, #0x20]     /* jmp_buf->R11 */
-       str     sp,  [r0, #0x24]     /* jmp_buf->Sp */
-       str     lr,  [r0, #0x28]     /* jmp_buf->Pc */
-       vmrs    r2,  fpscr
-       str     r2,  [r0, #0x2c]     /* jmp_buf->Fpscr */
-       vstr    d8,  [r0, #0x30]     /* jmp_buf->D[0] */
-       vstr    d9,  [r0, #0x38]     /* jmp_buf->D[1] */
-       vstr    d10, [r0, #0x40]     /* jmp_buf->D[2] */
-       vstr    d11, [r0, #0x48]     /* jmp_buf->D[3] */
-       vstr    d12, [r0, #0x50]     /* jmp_buf->D[4] */
-       vstr    d13, [r0, #0x58]     /* jmp_buf->D[5] */
-       vstr    d14, [r0, #0x60]     /* jmp_buf->D[6] */
-       vstr    d15, [r0, #0x68]     /* jmp_buf->D[7] */
-       mov     r0,  #0
-       bx      lr
-#elif defined(_ARM64_) || defined(__aarch64__)
-       str     xzr, [x0]             /* jmp_buf->Frame */
-       stp     x19, x20, [x0, #0x10] /* jmp_buf->X19, X20 */
-       stp     x21, x22, [x0, #0x20] /* jmp_buf->X21, X22 */
-       stp     x23, x24, [x0, #0x30] /* jmp_buf->X23, X24 */
-       stp     x25, x26, [x0, #0x40] /* jmp_buf->X25, X26 */
-       stp     x27, x28, [x0, #0x50] /* jmp_buf->X27, X28 */
-       stp     x29, x30, [x0, #0x60] /* jmp_buf->Fp,  Lr  */
-       mov     x2,  sp
-       str     x2,       [x0, #0x70] /* jmp_buf->Sp */
-       mrs     x2,  fpcr
-       str     w2,       [x0, #0x78] /* jmp_buf->Fpcr */
-       mrs     x2,  fpsr
-       str     w2,       [x0, #0x7c] /* jmp_buf->Fpsr */
-       stp     d8,  d9,  [x0, #0x80] /* jmp_buf->D[0-1] */
-       stp     d10, d11, [x0, #0x90] /* jmp_buf->D[2-3] */
-       stp     d12, d13, [x0, #0xa0] /* jmp_buf->D[4-5] */
-       stp     d14, d15, [x0, #0xb0] /* jmp_buf->D[6-7] */
-       mov     x0,  #0
-       ret
-#endif
-#endif
-
 #if defined(__i386__) || defined(_ARM_) || defined(__arm__) || 
defined(_ARM64_) || defined(__aarch64__) || defined(__arm64ec__)
        .globl __MINGW_USYMBOL(__mingw_longjmp)
        .def    __MINGW_USYMBOL(__mingw_longjmp);       .scl    2;      .type   
32;     .endef
diff --git a/mingw-w64-headers/crt/setjmp.h b/mingw-w64-headers/crt/setjmp.h
index 15e5b4f14e86..2870be5ed233 100644
--- a/mingw-w64-headers/crt/setjmp.h
+++ b/mingw-w64-headers/crt/setjmp.h
@@ -202,9 +202,6 @@ _CRTIMP int __cdecl __attribute__ 
((__nothrow__,__returns_twice__)) _setjmp(jmp_
 _CRTIMP int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmpex(jmp_buf _Buf, void *_Frame); /* for non-i386 setjmpex.h and aarch64 
setjmp.h */
 #endif
 
-#if defined(__arm__) || defined(__aarch64__)
-int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
__mingw_setjmp(jmp_buf _Buf);
-#endif
 #if defined(__i386__) || defined(__arm__) || defined(__aarch64__) || 
defined(__arm64ec__)
 __MINGW_ATTRIB_NORETURN __attribute__ ((__nothrow__)) void 
__mingw_longjmp(jmp_buf _Buf, int _Value);
 #endif
-- 
2.20.1


>From 0a007fa92ad8614a1af3365486d6ae2b220e540b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Thu, 22 Oct 2026 14:00:44 +0200
Subject: [PATCH 25/25] headers: Remove setjmp and longjmp functions
 definitions from intrin.h

All of setjmp and longjmp functions and macros are correctly defined in the
setjmp.h header file. Some of definitions in intrin.h are not correct.
So remove all of them from intrin.h.
---
 mingw-w64-headers/crt/intrin.h | 16 ----------------
 1 file changed, 16 deletions(-)

diff --git a/mingw-w64-headers/crt/intrin.h b/mingw-w64-headers/crt/intrin.h
index 87ad933f2f3e..951a61389857 100644
--- a/mingw-w64-headers/crt/intrin.h
+++ b/mingw-w64-headers/crt/intrin.h
@@ -34,9 +34,6 @@
 #ifndef RC_INVOKED
 
 #include <crtdefs.h>
-#ifndef __CYGWIN__
-#include <setjmp.h>
-#endif
 #include <stddef.h>
 #include <psdk_inc/intrin-impl.h>
 
@@ -412,7 +409,6 @@ _CRT_BEGIN_C_HEADER
     __MACHINEIA64(void __lfetchfault_excl(int,void const *))
     __MACHINEIA64(__MINGW_EXTENSION __int64 __load128(void *,__int64 *))
     __MACHINEIA64(__MINGW_EXTENSION __int64 __load128_acq(void *,__int64 *))
-    __MACHINEZ(void __cdecl longjmp(jmp_buf,int))
 
     /* __MACHINE(unsigned long __cdecl _lrotl(unsigned long,int)) moved to 
psdk_inc/intrin-impl.h */
     /* __MACHINE(unsigned long __cdecl _lrotr(unsigned long,int)) moved to 
psdk_inc/intrin-impl.h */
@@ -498,18 +494,6 @@ _CRT_BEGIN_C_HEADER
 #define _rotr64 __rorq
     __MACHINEIA64(void __rsm(int))
     __MACHINEIA64(void __rum(int))
-#ifndef __CYGWIN__
-#ifndef USE_NO_MINGW_SETJMP_TWO_ARGS
-    __MACHINEIA32(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp(jmp_buf))
-    __MACHINEX64(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp(jmp_buf,void *))
-    __MACHINEIA64(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmpex(jmp_buf,void *))
-    __MACHINEX64(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmpex(jmp_buf,void *))
-#else
-    __MACHINE(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmp(jmp_buf))
-    __MACHINEIA64(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmpex(jmp_buf))
-    __MACHINEX64(int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) 
_setjmpex(jmp_buf))
-#endif
-#endif
     __MACHINEIA64(__MINGW_EXTENSION void __setReg(int,unsigned __int64))
     __MACHINEARMX(void _SmulAdd_SL_ACC(int,int))
     __MACHINEARMX(void _SmulAddPack_2SW_ACC(int,int))
-- 
2.20.1

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to