For DLL builds, move calling __main(), _pei386_runtime_relocator() and
__mingw_init_ehandler() functions from __DllMainCRTStartup() into
_CRT_INIT() function.

This allow applications with custom entry point, which would call
_CRT_INIT() manually, to work correctly.

Also _CRT_INIT() contains guard which prevents duplicate code execution.
---
 mingw-w64-crt/crt/crtdll.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 6028d0c776f0..3d12d3f82c5f 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -32,6 +32,9 @@
 #include <sect_attribs.h>
 #include <locale.h>
 
+#if defined(__x86_64__) && !defined(__SEH__)
+extern int __mingw_init_ehandler (void);
+#endif
 extern void __cdecl _initterm(_PVFV *,_PVFV *);
 extern void __main ();
 extern void _pei386_runtime_relocator (void);
@@ -98,10 +101,16 @@ 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 = _initterm_e (__xi_a, __xi_z);
          if (ret != 0)
            goto i__leave;
          _initterm (__xc_a, __xc_z);
+         __main ();
+
          __native_startup_state = __initialized;
        }
 i__leave:
@@ -154,9 +163,6 @@ i__leave:
 static WINBOOL __DllMainCRTStartup (HANDLE, DWORD, LPVOID);
 
 WINBOOL WINAPI DllMainCRTStartup (HANDLE, DWORD, LPVOID);
-#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 */
 WINBOOL WINAPI
@@ -183,12 +189,6 @@ __DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
        retcode = FALSE;
        goto i__leave;
     }
-  _pei386_runtime_relocator ();
-
-#if defined(__x86_64__) && !defined(__SEH__)
-  if (dwReason == DLL_PROCESS_ATTACH)
-    __mingw_init_ehandler ();
-#endif
 
   if (dwReason == DLL_PROCESS_ATTACH || dwReason == DLL_THREAD_ATTACH)
     {
@@ -203,8 +203,6 @@ __DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
            goto i__leave;
          }
     }
-  if (dwReason == DLL_PROCESS_ATTACH)
-    __main ();
   retcode = DllMain(hDllHandle,dwReason,lpreserved);
   if (dwReason == DLL_PROCESS_ATTACH && ! retcode)
     {
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to