在 2025-12-25 22:32, Pali Rohár 写道:
Hello,

I'm looking at how SEH exception handling is working and I found in
mingw-w64 crtexe.c code that _gnu_exception_handler for processing of
signals is installed for non-i386 builds two times.

Once it is installed as SEH handler for mainCRTStartup() function (hence
which covers whole program as the mainCRTStartup entry point) and then
it is installed also as UnhandledExceptionFilter in __tmainCRTStartup()
as top level filter, which is called when no other SEH handler takes the
exception.

In my opinion it is redundant to have registered the
_gnu_exception_handler two times as a top level handler.

It looks like `UnhandledExceptionFilter()` was added in d8f66c01384fe1d42479ee779d7bd1f5aaf975d7 in 2007, where SEH support was possibly incomplete. SEH on non-i386 doesn't use the TEB anyway, so maybe it was necessary to register it for experimental purposes. It is not necessary any more.

(The commit also introduced VEH, which was probably a mistake, because VEH would always take precedence over SEH. VEH was removed in d7a421a9574ebaa8b5dd0e6ed42bc32534ac4543 in 2009.)


On i386 code, the mainCRTStartup function does not install SEH handler,
so for i386 code it makes sense to have UnhandledExceptionFilter. But it
is needed to have UnhandledExceptionFilter also for non-i386 code?

This isn't necessary; see below.


Anyway, is there any particular reason why we do not register SEH
handler in mainCRTStartup() also for i386 builds? I know that gnu
assembler does not provide .seh_handler directive for 32-bit x86 so
different approach and ifdef for it is needed. But I think that for
consistency between i386 and non-i386 builds we could have same code
paths. And then usage of UnhandledExceptionFilter would not be needed.

Right. It's better to register this handler in `mainCRTStartup()`, maybe like 
this:

   EXCEPTION_REGISTRATION_RECORD record = { .Next = (void*) __readfsdword(0),
                                            .Handler = 
__mingw_SEH_error_handler };
   __writefsdword(0, (DWORD) &record);

and to unregister it:

   __writefsdword(0, (DWORD) record.Next);





--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

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

Reply via email to