On Wednesday 20 August 2025 15:25:03 Martin Storsjö wrote:
> On Sat, 16 Aug 2025, Pali Rohár wrote:
>
> > 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.
> > ---
>
> I feel uneasy about this change. It changes a lot, without presenting a
> compelling argument about why.
It was intended as a cleanup. __main is currently using atexit hooks to
execute c++ destructors but for dll builds this can be done in more
clean why by explicitly calling them at process detach event without
need to maintain per-dll atexit hook table and hence completely avoid
atexit call inclusion into every dll library.
> The interface around "__main" can be entrenched in many places, and code
> expecting to e.g. override the entry point and just call "__main" no longer
> will have working C++ constructors.
That already is not working. There is lot of stuff in crtexe.c and
crtdll.c which needs to be called before main / dllmain and is not in
__main. If somebody wants to override the default entry point for DLL
then the overwritten entry point has to call _CRT_INIT function, this is
documented somewhere in MS/WinAPI and mingw-w64 has also all required
init parts in _CRT_INIT function.
mingw-w64 is already calling gcc's __main() function in crt startup
code, so basically gcc is not required to insert manual call of this
function to the main function. Moreover gcc does not insert it into
wmain function (when compiling with gcc -municode), so it is already
inconsistent. Also gcc for linux does not insert this __main into main,
so I think it would make sense to allow produce gcc compiler which would
not do it neither for windows/mingw-w64.
> The code now uses _crt_atexit() instead of atexit(). Perhaps it is ok, I
> would need to study those interfaces and review that as a change on its own.
I can split it into separate followup change. In crtexe.c is atexit
defined as a simple wrapper around _crt_atexit, so this is just another
small cleanup.
> An explanation of why might help as well. If there is a reason to do that,
> it would be easier to handle review wise if it is split to a separate commit
> as well, with separate arguments.
>
> > {
> > - unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0];
> > + unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
> > unsigned long i;
>
> This change, to remove the intermediate cast, looks unrelated to the rest of
> the changes. Is there a good reason for why we should do that? In that case
> it could be a separate change. It is also possible that removing it may
> reintroduce warnings in some build configuration.
I removed it because I though that it is not needed and that ptrdiff_t
was the only reason why the new file requires to include any header
file.
Now when I'm thinking about it, ptrdiff_t is the wrong type at this
place. It should be uintptr_t, no? In memory there is stored unsigned
integer which has same width as pointer, meaning is number of members
and C type of memory for that variable/array is pointer. Meaning is not
difference between two pointers.
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public