On Friday 03 July 2026 00:33:46 Pali Rohár wrote:
> On Friday 03 July 2026 01:00:15 Martin Storsjö wrote:
> > On Sun, 28 Jun 2026, Pali Rohár wrote:
> > 
> > > Evgeny, you bring this issue for arm64 and I have prepared a fix which
> > > is in the attachment of the previous email. Could you please check and
> > > test it? I really need feeback from your configuration / setup of
> > > toolchain to know if it address your issue. Because I do not have it.
> > > 
> > > Martin, if you have a time, could you do (code) review for first two
> > > changes which are for amd64 (which is rewriting / replacing the
> > > __mingw_init_ehandler implementation)?
> > 
> > Those patches feel like super overkill.
> > 
> > I don't think we should have code to construct SEH unwind code by hand here.
> > I really can't see why we should need to do that.
> 
> For example, it is needed for C signal handlers.
> 
> > Why do we need to complicate every detail 10x to make our code even more
> > inflated and unmaintainable?
> 
> Well, it was already there in the __mingw_init_ehandler function and which
> I have pointed out is broken. So I chose the direction to try to fix it
> and figure out how the whole picture is working.
> 
> > We can't just try to add some random inline assembly snippets and calls to
> > __builtin_frame_address() and hope that the compiler generates a specific
> > form of prolog for the function based on that. If we ever try to do that,
> > we're doing something really wrong. (And the wrong thing can also just be
> > that you're trying to fix a case which we shouldn't try to fix.)
> 
> In this case, I would propose to completely remove the
> __mingw_init_ehandler code. IMHO it does not make sense to have it in
> the current state when it is not working as expected.
> 
> > I explicit do _not_ want those patches merged.
> > 
> > 
> > For builds with SEH disabled, I don't think anybody expects all of "make
> > check" to pass. (Heck, it was only about a year ago that we even made "make
> > check" work at all again.) A toolchain can be very well functioning for a
> > lot of practical usecases without having those details handled in the right
> > way.
> > 
> > For builds with SEH disabled, just ifdef out whatever constructs don't
> > compile in that form.
> > 
> > // Martin
> 
> I understand your point about simplification. Sounds like better
> direction.
> 
> We can put signal function (and other related) in header files into
> ifdef block too, those things which are not available without SEH would
> not be possible to use.
> 
> This can simplify the code with the restrictions which toolchain have.
> And by completely removing the __mingw_init_ehandler, it can also
> decrease maintenance.
> 
> The question is what else is broken without the SEH support on non-i386
> platforms.

In the attachment are changes which remove __mingw_init_ehandler,
disable signal() function for non-SEH builds and remove #error lines,
so mingw-w64 runtime could be compiled also for non-SEH builds.
>From e9e14f28659fb9b77d5b9790b2c45ec1fa4b2507 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 4 Jul 2026 16:10:14 +0200
Subject: [PATCH 1/2] crt: Remove broken amd64 __mingw_SEH_error_handler code

This handler code does not register correct amd64 unwind codes and
therefore SEH handler does not work correctly.

Details are in the described in the email with proposed fix for this issue:
https://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/20260620145158.uudfuwcoyq4p5g3c@pali/#msg59348860

So remove this broken code for now.
---
 mingw-w64-crt/crt/crt_handler.c | 67 ---------------------------------
 mingw-w64-crt/crt/crtdll.c      |  6 ---
 mingw-w64-crt/crt/crtexe.c      |  5 ---
 3 files changed, 78 deletions(-)

diff --git a/mingw-w64-crt/crt/crt_handler.c b/mingw-w64-crt/crt/crt_handler.c
index 191c2da65751..c484e8e57a8c 100644
--- a/mingw-w64-crt/crt/crt_handler.c
+++ b/mingw-w64-crt/crt/crt_handler.c
@@ -6,77 +6,10 @@
 
 #include <windows.h>
 #include <excpt.h>
-#include <string.h>
 #include <stdlib.h>
-#include <malloc.h>
-#include <memory.h>
-#include <signal.h>
-#include <stdio.h>
 
 EXCEPTION_DISPOSITION __cdecl __mingw_SEH_error_handler(struct 
_EXCEPTION_RECORD *, void *, struct _CONTEXT *, void *);
 
-#if defined(__x86_64__) && !defined(_MSC_VER) && !defined(__SEH__)
-
-#pragma pack(push,1)
-typedef struct _UNWIND_INFO {
-  BYTE Version:3;
-  BYTE Flags:5;
-  BYTE PrologSize;
-  BYTE CountOfUnwindCodes;
-  BYTE FrameRegisterAndOffset;
-  ULONG AddressOfExceptionHandler;
-} UNWIND_INFO,*PUNWIND_INFO;
-#pragma pack(pop)
-
-PIMAGE_SECTION_HEADER _FindPESectionByName (const char *);
-PIMAGE_SECTION_HEADER _FindPESectionExec (size_t);
-PBYTE _GetPEImageBase (void);
-
-#define MAX_PDATA_ENTRIES 32
-static RUNTIME_FUNCTION emu_pdata[MAX_PDATA_ENTRIES];
-static UNWIND_INFO emu_xdata[MAX_PDATA_ENTRIES];
-
-int __mingw_init_ehandler (void);
-int
-__mingw_init_ehandler (void)
-{
-  static int was_here = 0;
-  size_t e = 0;
-  PIMAGE_SECTION_HEADER pSec;
-  PBYTE _ImageBase = _GetPEImageBase ();
-  
-  if (was_here || !_ImageBase)
-    return was_here;
-  was_here = 1;
-  if (_FindPESectionByName (".pdata") != NULL)
-    return 1;
-
-  e = 0;
-  /* Fill tables and entries.  */
-  while (e < MAX_PDATA_ENTRIES && (pSec = _FindPESectionExec (e)) != NULL)
-    {
-      emu_xdata[e].Version = 1;
-      emu_xdata[e].Flags = UNW_FLAG_EHANDLER;
-      emu_xdata[e].AddressOfExceptionHandler =
-       (DWORD)(size_t) ((LPBYTE)__mingw_SEH_error_handler - _ImageBase);
-      emu_pdata[e].BeginAddress = pSec->VirtualAddress;
-      emu_pdata[e].EndAddress = pSec->VirtualAddress + pSec->Misc.VirtualSize;
-      emu_pdata[e].UnwindData =
-       (DWORD)(size_t)((LPBYTE)&emu_xdata[e] - _ImageBase);
-      ++e;
-    }
-#ifdef _DEBUG_CRT
-  if (!e || e > MAX_PDATA_ENTRIES)
-    abort ();
-#endif
-  /* RtlAddFunctionTable.  */
-  if (e != 0)
-    RtlAddFunctionTable (emu_pdata, e, (DWORD64)_ImageBase);
-  return 1;
-}
-
-#endif
-
 #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))
diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 18e831ef9525..68f637b6b48b 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -17,9 +17,6 @@
 #include <sect_attribs.h>
 #include <locale.h>
 
-#if defined(__x86_64__) && !defined(__SEH__)
-extern int __mingw_init_ehandler (void);
-#endif
 extern void __main ();
 extern void _pei386_runtime_relocator (void);
 extern _PIFV __xi_a[];
@@ -72,9 +69,6 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
          __native_startup_state = __initializing;
          
          _pei386_runtime_relocator ();
-#if defined(__x86_64__) && !defined(__SEH__)
-         __mingw_init_ehandler ();
-#endif
          ret = _initialize_onexit_table (&atexit_table);
          if (ret != 0)
            goto i__leave;
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 86c3842f6162..369c2979a0b5 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -67,9 +67,6 @@ static int has_cctor = 0;
 
 extern void _pei386_runtime_relocator (void);
 EXCEPTION_DISPOSITION __cdecl __mingw_SEH_error_handler (struct 
_EXCEPTION_RECORD *, void *, struct _CONTEXT *, void *);
-#if defined(__x86_64__) && !defined(SEH_INLINE_ASM)
-int __mingw_init_ehandler (void);
-#endif
 static int duplicate_ppstrings (int ac, _TCHAR ***av);
 
 extern int _MINGW_INSTALL_DEBUG_MATHERR;
@@ -165,8 +162,6 @@ __tmainCRTStartup (void)
     __writefsdword (0, (DWORD)&exception_record); /* dynamically register SEH 
error handler, it is active until manually unregistered */
 #elif defined(SEH_INLINE_ASM)
     asm volatile (".seh_handler " ASM_SEH_PREFIX "%c0" ASM_SEH_SUFFIX ", " 
ASM_SEH_EXCEPT :: "i" (__mingw_SEH_error_handler)); /* statically register SEH 
error handler, it is active only in the current function */
-#elif defined(__x86_64__)
-    __mingw_init_ehandler (); /* dynamically register SEH error handler for 
all functions, it is active until program terminates */
 #else
 #error unsupported platform
 #endif
-- 
2.20.1


>From b9740f6aa5f60c0ea41d4fca42caf4c7ae1c1fd4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sat, 4 Jul 2026 16:20:13 +0200
Subject: [PATCH 2/2] crt: Enable compiling mingw-w64-crt without the SEH
 support

This produce mingw-w64 crt code without catching SEH exceptions and
dispatching them to C signal handlers. So also mark the signal() function
as not available on non-SEH builds on non-i386 plaforms.
---
 mingw-w64-crt/crt/crtexe.c     | 2 --
 mingw-w64-headers/crt/signal.h | 6 ++++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 369c2979a0b5..1a8af29565fd 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -162,8 +162,6 @@ __tmainCRTStartup (void)
     __writefsdword (0, (DWORD)&exception_record); /* dynamically register SEH 
error handler, it is active until manually unregistered */
 #elif defined(SEH_INLINE_ASM)
     asm volatile (".seh_handler " ASM_SEH_PREFIX "%c0" ASM_SEH_SUFFIX ", " 
ASM_SEH_EXCEPT :: "i" (__mingw_SEH_error_handler)); /* statically register SEH 
error handler, it is active only in the current function */
-#else
-#error unsupported platform
 #endif
 
     void *lock_free = NULL;
diff --git a/mingw-w64-headers/crt/signal.h b/mingw-w64-headers/crt/signal.h
index 097120192038..6e47f7729896 100644
--- a/mingw-w64-headers/crt/signal.h
+++ b/mingw-w64-headers/crt/signal.h
@@ -57,7 +57,13 @@ extern "C" {
   extern void **__cdecl __pxcptinfoptrs(void);
 #define _pxcptinfoptrs (*__pxcptinfoptrs())
 
+  /* mingw-w64 provides signal support for:
+   * - all builds for i386 platform
+   * - SEH builds for non-i386 platforms
+   */
+#if defined(__i386__) || (defined(__SEH__) && (!defined(__clang__) || 
__clang_major__ >= 7))
   __p_sig_fn_t __cdecl signal(int _SigNum,__p_sig_fn_t _Func);
+#endif
   int __cdecl raise(int _SigNum);
 
 #ifdef __cplusplus
-- 
2.20.1

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

Reply via email to