This deduplicate code as both WinMainCRTStartup() and mainCRTStartup()
entry points are just calling __tmainCRTStartup() function.
---
 mingw-w64-crt/crt/crtexe.c | 57 +++++++++++++-------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)

diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index bd069c772cc6..7913b75d7891 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -59,6 +59,9 @@ 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;
@@ -84,51 +87,19 @@ __mingw_invalidParameterHandler (const wchar_t * 
__UNUSED_PARAM_1(expression),
 static int __tmainCRTStartup (void);
 
 int WinMainCRTStartup (void);
-
 __attribute__((used)) /* required due to GNU LD bug: 
https://sourceware.org/bugzilla/show_bug.cgi?id=30300 */
 int WinMainCRTStartup (void)
 {
-#if defined(__i386__)
-  EXCEPTION_REGISTRATION_RECORD exception_record = {
-    .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword (0),
-    .Handler = __mingw_SEH_error_handler,
-  };
-  __writefsdword (0, (DWORD)&exception_record); /* register SEH handler */
-#elif defined(SEH_INLINE_ASM)
-  asm volatile (".seh_handler %p0, " ASM_EXCEPT :: "i" 
(__mingw_SEH_error_handler)); /* applies for the whole function */
-#endif
   __mingw_app_type = 1;
-  int ret = __tmainCRTStartup ();
-#if defined(__i386__)
-  __writefsdword (0, (DWORD)exception_record.Next); /* unregister SEH handler 
*/
-#endif
-  return ret;
+  return __tmainCRTStartup ();
 }
 
 int mainCRTStartup (void);
-
-#if defined(__x86_64__) && !defined(__SEH__)
-int __mingw_init_ehandler (void);
-#endif
-
 __attribute__((used)) /* required due to GNU LD bug: 
https://sourceware.org/bugzilla/show_bug.cgi?id=30300 */
 int mainCRTStartup (void)
 {
-#if defined(__i386__)
-  EXCEPTION_REGISTRATION_RECORD exception_record = {
-    .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword (0),
-    .Handler = __mingw_SEH_error_handler,
-  };
-  __writefsdword (0, (DWORD)&exception_record); /* register SEH handler */
-#elif defined(SEH_INLINE_ASM)
-  asm volatile (".seh_handler %p0, " ASM_EXCEPT :: "i" 
(__mingw_SEH_error_handler)); /* applies for the whole function */
-#endif
   __mingw_app_type = 0;
-  int ret = __tmainCRTStartup ();
-#if defined(__i386__)
-  __writefsdword (0, (DWORD)exception_record.Next); /* unregister SEH handler 
*/
-#endif
-  return ret;
+  return __tmainCRTStartup ();
 }
 
 static
@@ -140,6 +111,18 @@ __attribute__((force_align_arg_pointer))
 __declspec(noinline) int
 __tmainCRTStartup (void)
 {
+#if defined(__i386__)
+    EXCEPTION_REGISTRATION_RECORD exception_record = {
+      .Next = (EXCEPTION_REGISTRATION_RECORD *)__readfsdword (0),
+      .Handler = __mingw_SEH_error_handler,
+    };
+    __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 %p0, " ASM_EXCEPT :: "i" 
(__mingw_SEH_error_handler)); /* statically register error SEH 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 */
+#endif
+
     void *lock_free = NULL;
     void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase;
     BOOL nested = FALSE;
@@ -173,9 +156,6 @@ __tmainCRTStartup (void)
        setvbuf (stderr, NULL, _IONBF, 0);
 
        _pei386_runtime_relocator ();
-#if defined(__x86_64__) && !defined(__SEH__)
-       __mingw_init_ehandler ();
-#endif
        _set_invalid_parameter_handler (__mingw_invalidParameterHandler);
        _fpreset ();
 
@@ -243,6 +223,9 @@ __tmainCRTStartup (void)
     if (has_cctor == 0)
       _cexit ();
 
+#if defined(__i386__)
+  __writefsdword (0, (DWORD)exception_record.Next); /* dynamically unregister 
SEH error handler */
+#endif
   return ret;
 }
 
-- 
2.20.1



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

Reply via email to