Rename __do_global_ctors to __mingw_do_global_ctors and __do_global_dtors to __mingw_do_global_dtors to make it explicit that these two functions are internal for mingw-w64 runtime and not supposed to be used or called by anything else.
Explicitly call __mingw_do_global_ctors() in mingw-w64 startup sequence instead of the __main() function, and make gcc's __main() function completely empty, as it is not needed anymore. Function __main() is inserted into every function main() by gcc, so the symbol for function __main() has to exist in mingw-w64 runtime even if it does nothing. Move atexit(__mingw_do_global_dtors) call from __mingw_do_global_ctors() to mingw-w64 shutdown sequence. For exe builds use _crt_atexit() registration and for dll builds call __mingw_do_global_dtors() function from the _CRT_INIT(DLL_PROCESS_DETACH) function. --- mingw-w64-crt/Makefile.am | 2 +- mingw-w64-crt/crt/crtdll.c | 7 +++-- mingw-w64-crt/crt/crtexe.c | 7 +++-- .../crt/{gccmain.c => gcc_ctors_dtors.c} | 30 ++++--------------- mingw-w64-crt/crt/gcc_main.c | 22 ++++++++++++++ 5 files changed, 38 insertions(+), 30 deletions(-) rename mingw-w64-crt/crt/{gccmain.c => gcc_ctors_dtors.c} (56%) create mode 100644 mingw-w64-crt/crt/gcc_main.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index a296bd804779..2665013e2155 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -114,7 +114,7 @@ src_libntoskrnl=libsrc/memcmp.c src_libdloadhelper=libsrc/dloadhelper.c misc/delay-f.c src_libmingw32=include/oscalls.h include/internal.h include/sect_attribs.h \ - crt/crtexewin.c crt/dll_argv.c crt/gccmain.c crt/natstart.c crt/pseudo-reloc-list.c crt/wildcard.c \ + crt/crtexewin.c crt/dll_argv.c crt/gcc_main.c crt/gcc_ctors_dtors.c crt/natstart.c crt/pseudo-reloc-list.c crt/wildcard.c \ crt/charmax.c crt/ucrtexewin.c crt/dllargv.c crt/_newmode.c crt/tlssup.c crt/xncommod.c \ crt/cinitexe.c crt/merr.c crt/pesect.c crt/udllargc.c crt/xthdloc.c \ crt/mingw_custom.c crt/mingw_helpers.c \ diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c index 6bee08f596ab..5b96a9df981e 100644 --- a/mingw-w64-crt/crt/crtdll.c +++ b/mingw-w64-crt/crt/crtdll.c @@ -21,7 +21,8 @@ #if defined(__x86_64__) && !defined(__SEH__) extern int __mingw_init_ehandler (void); #endif -extern void __main (); +extern void __mingw_do_global_ctors (void); +extern void __mingw_do_global_dtors (void); extern void _pei386_runtime_relocator (void); extern _PIFV __xi_a[]; extern _PIFV __xi_z[]; @@ -83,7 +84,7 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) if (ret != 0) goto i__leave; _initterm (__xc_a, __xc_z); - __main (); + __mingw_do_global_ctors (); __native_startup_state = __initialized; } @@ -124,6 +125,8 @@ i__leave: else { _execute_onexit_table(&atexit_table); + __mingw_do_global_dtors (); + __native_startup_state = __uninitialized; } if (! nested) diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c index 94bad6aaff27..e5f9254c005f 100644 --- a/mingw-w64-crt/crt/crtexe.c +++ b/mingw-w64-crt/crt/crtexe.c @@ -43,8 +43,10 @@ extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback; extern int __mingw_app_type; +extern void __mingw_do_global_ctors (void); +extern void __mingw_do_global_dtors (void); + static int argc; -extern void __main(void); static _TCHAR **argv; static _TCHAR **envp; @@ -216,7 +218,8 @@ __tmainCRTStartup (void) _amsg_exit (8); /* _RT_SPACEARG */ _initterm (__xc_a, __xc_z); - __main (); /* C++ initialization. */ + __mingw_do_global_ctors (); + _crt_atexit (__mingw_do_global_dtors); __native_startup_state = __initialized; } diff --git a/mingw-w64-crt/crt/gccmain.c b/mingw-w64-crt/crt/gcc_ctors_dtors.c similarity index 56% rename from mingw-w64-crt/crt/gccmain.c rename to mingw-w64-crt/crt/gcc_ctors_dtors.c index 13ef04fdba20..f470cd7de792 100644 --- a/mingw-w64-crt/crt/gccmain.c +++ b/mingw-w64-crt/crt/gcc_ctors_dtors.c @@ -4,20 +4,15 @@ * No warranty is given; refer to the file DISCLAIMER.PD within this package. */ -#include <windows.h> -#include <stdlib.h> -#include <setjmp.h> - typedef void (*func_ptr) (void); extern func_ptr __CTOR_LIST__[]; extern func_ptr __DTOR_LIST__[]; -void __do_global_dtors (void); -void __do_global_ctors (void); -void __main (void); +void __mingw_do_global_dtors (void); +void __mingw_do_global_ctors (void); void -__do_global_dtors (void) +__mingw_do_global_dtors (void) { static func_ptr *p = __DTOR_LIST__ + 1; @@ -29,9 +24,9 @@ __do_global_dtors (void) } void -__do_global_ctors (void) +__mingw_do_global_ctors (void) { - unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0]; + unsigned long nptrs = (unsigned long) __CTOR_LIST__[0]; unsigned long i; if (nptrs == (unsigned long) -1) @@ -43,19 +38,4 @@ __do_global_ctors (void) { __CTOR_LIST__[i] (); } - - atexit (__do_global_dtors); -} - -static int initialized = 0; - -__attribute__((used)) /* required for gcc -flto -Ofast */ -void -__main (void) -{ - if (!initialized) - { - initialized = 1; - __do_global_ctors (); - } } diff --git a/mingw-w64-crt/crt/gcc_main.c b/mingw-w64-crt/crt/gcc_main.c new file mode 100644 index 000000000000..367b8de7ef98 --- /dev/null +++ b/mingw-w64-crt/crt/gcc_main.c @@ -0,0 +1,22 @@ +/** + * 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. + */ + +void __main (void); + +__attribute__((used)) /* required for gcc -flto -Ofast */ +void +__main (void) +{ + /* gcc during compilation of function named main() inserts at the beginning + * of the execution a call to the function __main(). gcc expects that the + * function __main() will execute all global C++ constructors which are + * emitted by gcc itself. mingw-w64 runtime executes all gcc's global C++ + * constructors in mingw-w64 startup code (in crtdll.c and crtexe.c) before + * executing function main. So the __main() function should do nothing as + * when this function is called, all global C++ constructors were already + * executed. + */ +} -- 2.20.1 _______________________________________________ Mingw-w64-public mailing list Mingw-w64-public@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mingw-w64-public