Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
On Tuesday 08 September 2026 10:42:09 Pali Rohár wrote:
> On Tuesday 08 September 2026 13:45:01 LIU Hao wrote:
> > 在 2026-9-7 07:40, Pali Rohár 写道:
> > > Anyway, here is simple fixup change disables that test for gcc builds:
> > >
> > > diff --git a/mingw-w64-crt/testcases/t_noatexit_dll.c
> > > b/mingw-w64-crt/testcases/t_noatexit_dll.c
> > > index 4f8a86328933..e62ad5b1dcd5 100644
> > > --- a/mingw-w64-crt/testcases/t_noatexit_dll.c
> > > +++ b/mingw-w64-crt/testcases/t_noatexit_dll.c
> > > @@ -13,6 +13,13 @@ int main(void)
> > > {
> > > mingw_test_init();
> > >
> > > + /* gcc's crtbegin.o (libgcc/config/i386/cygming-crtbegin.c) injects
> > > atexit call into every output binary.
> > > + * So skip this test for all gcc builds (and exclude clang builds for
> > > which this test is passing). */
> > > +#if defined(__GNUC__) && !defined(__clang__)
> > > + printf("test skipped for gcc builds\n");
> > > + return 77;
> > > +#endif
> > > +
> > > HMODULE lib = LoadLibraryA("libnoatexit.dll");
> > > if (!lib) {
> > > DWORD error = GetLastError();
> >
> > This change looks fine and passes the CI (at least for now), so I have
> > pushed these patches with the change squashed into the test.
> >
> > I am not sure whether the issue is specific to `DWARF2_UNWIND_INFO`; I tried
> > `#if defined(__GNUC__) && !defined(__clang__) && !defined(__SEH__)` but it
> > didn't pass:
> > https://github.com/lhmouse/mingw-w64/actions/runs/34134697664/job/101837213788#step:6:3668
>
> The atexit callback function contains only #if DWARF2_UNWIND_INFO code.
> But the atexit call is injected for the #if DEFAULT_USE_CXA_ATEXIT case,
> it is injected also for non-DWARF code. So simple switching from DWARF
> to SEH is not sufficient, and so the test is expected to fail.
I have reported this issue to gcc bug tracker:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127366
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
On Tuesday 08 September 2026 13:45:01 LIU Hao wrote:
> 在 2026-9-7 07:40, Pali Rohár 写道:
> > Anyway, here is simple fixup change disables that test for gcc builds:
> >
> > diff --git a/mingw-w64-crt/testcases/t_noatexit_dll.c
> > b/mingw-w64-crt/testcases/t_noatexit_dll.c
> > index 4f8a86328933..e62ad5b1dcd5 100644
> > --- a/mingw-w64-crt/testcases/t_noatexit_dll.c
> > +++ b/mingw-w64-crt/testcases/t_noatexit_dll.c
> > @@ -13,6 +13,13 @@ int main(void)
> > {
> > mingw_test_init();
> >
> > + /* gcc's crtbegin.o (libgcc/config/i386/cygming-crtbegin.c) injects
> > atexit call into every output binary.
> > + * So skip this test for all gcc builds (and exclude clang builds for
> > which this test is passing). */
> > +#if defined(__GNUC__) && !defined(__clang__)
> > + printf("test skipped for gcc builds\n");
> > + return 77;
> > +#endif
> > +
> > HMODULE lib = LoadLibraryA("libnoatexit.dll");
> > if (!lib) {
> > DWORD error = GetLastError();
>
> This change looks fine and passes the CI (at least for now), so I have
> pushed these patches with the change squashed into the test.
>
> I am not sure whether the issue is specific to `DWARF2_UNWIND_INFO`; I tried
> `#if defined(__GNUC__) && !defined(__clang__) && !defined(__SEH__)` but it
> didn't pass:
> https://github.com/lhmouse/mingw-w64/actions/runs/34134697664/job/101837213788#step:6:3668
The atexit callback function contains only #if DWARF2_UNWIND_INFO code.
But the atexit call is injected for the #if DEFAULT_USE_CXA_ATEXIT case,
it is injected also for non-DWARF code. So simple switching from DWARF
to SEH is not sufficient, and so the test is expected to fail.
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
在 2026-9-7 07:40, Pali Rohár 写道:
Anyway, here is simple fixup change disables that test for gcc builds:
diff --git a/mingw-w64-crt/testcases/t_noatexit_dll.c
b/mingw-w64-crt/testcases/t_noatexit_dll.c
index 4f8a86328933..e62ad5b1dcd5 100644
--- a/mingw-w64-crt/testcases/t_noatexit_dll.c
+++ b/mingw-w64-crt/testcases/t_noatexit_dll.c
@@ -13,6 +13,13 @@ int main(void)
{
mingw_test_init();
+ /* gcc's crtbegin.o (libgcc/config/i386/cygming-crtbegin.c) injects atexit
call into every output binary.
+ * So skip this test for all gcc builds (and exclude clang builds for which
this test is passing). */
+#if defined(__GNUC__) && !defined(__clang__)
+ printf("test skipped for gcc builds\n");
+ return 77;
+#endif
+
HMODULE lib = LoadLibraryA("libnoatexit.dll");
if (!lib) {
DWORD error = GetLastError();
This change looks fine and passes the CI (at least for now), so I have pushed these patches with the
change squashed into the test.
I am not sure whether the issue is specific to `DWARF2_UNWIND_INFO`; I tried `#if defined(__GNUC__) &&
!defined(__clang__) && !defined(__SEH__)` but it didn't pass:
https://github.com/lhmouse/mingw-w64/actions/runs/34134697664/job/101837213788#step:6:3668
--
Best regards,
LIU Hao
OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
On Sunday 06 September 2026 19:29:27 Zhongteng Gui wrote:
> On 2026/09/06 19:14, Pali Rohár wrote:
> > Ok, I have figured out, where is the problem.
> >
> > There is additional injection of the atexit call from object file:
> > /lib/gcc/{target}/{version}/crtbegin.o
> > which symbols are referenced from another object file:
> > /lib/gcc/{target}/{version}/crtend.o
> >
> >
> > Note that in my setup there are two crtbegin.o and two crtend.o files:
> >
> > /usr/i686-w64-mingw32/lib/crtbegin.o
> > /usr/lib/gcc/i686-w64-mingw32/$GCCVER/crtbegin.o
> >
> > The first one comes from mingw-w64 sources mingw-w64-crt/crt/crtbegin.c
> > And second one from gcc sources libgcc/config/i386/cygming-crtbegin.c
> >
> > And seems that for some setups is gcc using the first one (from
> > mingw-w64 repo) and for others is using second one (from gcc repo).
> >
> > This is really a pain to figure out that the source code on which I'm
> > looking is not the one from which was generated the problematic object
> > file.
> >
> >
> > I do not know how to easily figure out which crtbegin.o file is going to
> > be linked into final test executable. So I would suggest to skip that
> > new test on gcc builds and let it enabled only for clang for now, where
> > is passing.
>
> Just FYI, GCC will generally prioritize its bundled files, unless explicitly
> overridden with `-B`. You can use `gcc -print-search-dirs` and see the
> `libraries:` to verify this. And, even if you added `-B`, e.g. `gcc -B
> -print-search-dirs`, gcc will first search `///`, then
> `` itself, then the default search path (with the first one end with
> `lib/gcc///`).
I see. But this is unsuitable inside the automated test itself (so test
could figure out if it itself should skipped or not).
> >
> > Source code of these gcc files are there:
> > https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtbegin.c
> > https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtend.c
> >
> > As can be seen the atexit call is inside the #if DEFAULT_USE_CXA_ATEXIT
> > and the referenced function named __gcc_deregister_frame has whole code
> > inside the #if DWARF2_UNWIND_INFO.
> >
> > So I would suggest to extend the #if condition for the atexit call to
> > something like: #if DEFAULT_USE_CXA_ATEXIT && DWARF2_UNWIND_INFO which
> > would prevent calling atexit in cases when callback is doing nothing.
> > And this also prevents injecting of atexit for non-dwarf builds. Dwarf
> > is mostly used only for 32-bit x86 mingw-w64 targets (others are SEH).
> >
> >
>
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
On Sunday 06 September 2026 13:14:31 Pali Rohár wrote:
> On Monday 31 August 2026 10:22:07 LIU Hao wrote:
> > 在 2026-8-30 23:24, LIU Hao 写道:
> > > 在 2026-8-29 18:18, Pali Rohár 写道:
> > > > Hello. When you have a time, could you look at the changes in my
> > > > previous email?
> > > >
> > > I have read these patches and I think these are fine. If they pass the
> > > CI, I can push them tomorrow (~12 hour later, or so).
> > >
> >
> > The new test doesn't actually pass:
> >
> >
> > https://github.com/lhmouse/mingw-w64/actions/runs/33319363895/job/99359904865#step:6:4062
>
> Ok, I have figured out, where is the problem.
>
> There is additional injection of the atexit call from object file:
> /lib/gcc/{target}/{version}/crtbegin.o
> which symbols are referenced from another object file:
> /lib/gcc/{target}/{version}/crtend.o
>
>
> Note that in my setup there are two crtbegin.o and two crtend.o files:
>
> /usr/i686-w64-mingw32/lib/crtbegin.o
> /usr/lib/gcc/i686-w64-mingw32/$GCCVER/crtbegin.o
>
> The first one comes from mingw-w64 sources mingw-w64-crt/crt/crtbegin.c
> And second one from gcc sources libgcc/config/i386/cygming-crtbegin.c
>
> And seems that for some setups is gcc using the first one (from
> mingw-w64 repo) and for others is using second one (from gcc repo).
>
> This is really a pain to figure out that the source code on which I'm
> looking is not the one from which was generated the problematic object
> file.
>
>
> I do not know how to easily figure out which crtbegin.o file is going to
> be linked into final test executable. So I would suggest to skip that
> new test on gcc builds and let it enabled only for clang for now, where
> is passing.
>
>
> Source code of these gcc files are there:
> https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtbegin.c
> https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtend.c
>
> As can be seen the atexit call is inside the #if DEFAULT_USE_CXA_ATEXIT
> and the referenced function named __gcc_deregister_frame has whole code
> inside the #if DWARF2_UNWIND_INFO.
>
> So I would suggest to extend the #if condition for the atexit call to
> something like: #if DEFAULT_USE_CXA_ATEXIT && DWARF2_UNWIND_INFO which
> would prevent calling atexit in cases when callback is doing nothing.
> And this also prevents injecting of atexit for non-dwarf builds. Dwarf
> is mostly used only for 32-bit x86 mingw-w64 targets (others are SEH).
>
>
> What do you think about it? (Disabling the test for gcc builds and
> improving the gcc's cygming-crtbegin.c as future direction)?
Anyway, here is simple fixup change disables that test for gcc builds:
diff --git a/mingw-w64-crt/testcases/t_noatexit_dll.c
b/mingw-w64-crt/testcases/t_noatexit_dll.c
index 4f8a86328933..e62ad5b1dcd5 100644
--- a/mingw-w64-crt/testcases/t_noatexit_dll.c
+++ b/mingw-w64-crt/testcases/t_noatexit_dll.c
@@ -13,6 +13,13 @@ int main(void)
{
mingw_test_init();
+ /* gcc's crtbegin.o (libgcc/config/i386/cygming-crtbegin.c) injects atexit
call into every output binary.
+ * So skip this test for all gcc builds (and exclude clang builds for which
this test is passing). */
+#if defined(__GNUC__) && !defined(__clang__)
+ printf("test skipped for gcc builds\n");
+ return 77;
+#endif
+
HMODULE lib = LoadLibraryA("libnoatexit.dll");
if (!lib) {
DWORD error = GetLastError();
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
On Monday 31 August 2026 10:22:07 LIU Hao wrote:
> 在 2026-8-30 23:24, LIU Hao 写道:
> > 在 2026-8-29 18:18, Pali Rohár 写道:
> > > Hello. When you have a time, could you look at the changes in my previous
> > > email?
> > >
> > I have read these patches and I think these are fine. If they pass the
> > CI, I can push them tomorrow (~12 hour later, or so).
> >
>
> The new test doesn't actually pass:
>
>
> https://github.com/lhmouse/mingw-w64/actions/runs/33319363895/job/99359904865#step:6:4062
Ok, I have figured out, where is the problem.
There is additional injection of the atexit call from object file:
/lib/gcc/{target}/{version}/crtbegin.o
which symbols are referenced from another object file:
/lib/gcc/{target}/{version}/crtend.o
Note that in my setup there are two crtbegin.o and two crtend.o files:
/usr/i686-w64-mingw32/lib/crtbegin.o
/usr/lib/gcc/i686-w64-mingw32/$GCCVER/crtbegin.o
The first one comes from mingw-w64 sources mingw-w64-crt/crt/crtbegin.c
And second one from gcc sources libgcc/config/i386/cygming-crtbegin.c
And seems that for some setups is gcc using the first one (from
mingw-w64 repo) and for others is using second one (from gcc repo).
This is really a pain to figure out that the source code on which I'm
looking is not the one from which was generated the problematic object
file.
I do not know how to easily figure out which crtbegin.o file is going to
be linked into final test executable. So I would suggest to skip that
new test on gcc builds and let it enabled only for clang for now, where
is passing.
Source code of these gcc files are there:
https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtbegin.c
https://github.com/gnutools/gcc/blob/master/libgcc/config/i386/cygming-crtend.c
As can be seen the atexit call is inside the #if DEFAULT_USE_CXA_ATEXIT
and the referenced function named __gcc_deregister_frame has whole code
inside the #if DWARF2_UNWIND_INFO.
So I would suggest to extend the #if condition for the atexit call to
something like: #if DEFAULT_USE_CXA_ATEXIT && DWARF2_UNWIND_INFO which
would prevent calling atexit in cases when callback is doing nothing.
And this also prevents injecting of atexit for non-dwarf builds. Dwarf
is mostly used only for 32-bit x86 mingw-w64 targets (others are SEH).
What do you think about it? (Disabling the test for gcc builds and
improving the gcc's cygming-crtbegin.c as future direction)?
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
在 2026-8-29 18:18, Pali Rohár 写道: Hello. When you have a time, could you look at the changes in my previous email? I have read these patches and I think these are fine. If they pass the CI, I can push them tomorrow (~12 hour later, or so). As usual, there are a few grammatical errors in the new comments, and a sentence of a previous paragraph is duplicated: diff --git a/mingw-w64-crt/crt/gccmain.c b/mingw-w64-crt/crt/gccmain.c index 2954f7181..7edf8360c 100644 --- a/mingw-w64-crt/crt/gccmain.c +++ b/mingw-w64-crt/crt/gccmain.c @@ -140,17 +140,15 @@ __main (void) * So the function __main() should expects that constructors were already * called and must prevent multiple execution of them. * - * Also application or DLL library can be build with the custom entry point + * Also application or DLL library can be built with the custom entry point * which does not call mingw-w64 startup code but calls this __main() function * manually and therefore this __main() function has to execute all global * C++ constructors and has to schedule calls for global C++ destructors. * - * gcc ABI function __do_global_ctors() do that: execute all global C++ - * constructors and has to schedule calls for global C++ destructors. - * But it does not contain guard to prevent duplicate execution. On the other - * hand, gcc ABI function __main() can be called multiple times and will not - * call constructors or destructor multiple times. So call __do_global_ctors() - * under own guard. + * gcc ABI function __do_global_ctors() does that. But it does not contain a + * guard to prevent duplicate execution. On the other hand, gcc ABI function + * __main() can be called multiple times and will not call constructors or + * destructor multiple times. So call __do_global_ctors() under a local guard. */ static int initialized = 0; if (!initialized) -- Best regards, LIU Hao OpenPGP_signature.asc Description: OpenPGP digital signature ___ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Re: [Mingw-w64-public] Document gccmain.c and avoid atexit() for DLL dtors
Hello. When you have a time, could you look at the changes in my previous email? On Saturday 15 August 2026 15:58:03 Pali Rohár wrote: > Hello, I was trying to understand what happened that libgcc cause > duplicate constructors and destructors calls. I have prepared changes > which align and document __do_global_dtors, __do_global_ctors and > __main functions. And re-introduce direct calls of dtors for DLL builds > which avoids statically linking atexit implementation into every DLL > library. Code is in the attachment. ___ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
