On Thursday 17 July 2025 22:08:41 Pali Rohár wrote:
> When mingw-w64 is compiled with LTO support (-flto and -ffat-lto-objects in
> CFLAGS and LDFLAGS) then compiling any application with -Ofast fails.
>
> Calling "gcc -flto -Ofast" throws fatal error:
>
> `___main' referenced in section `.text.startup' of
> /tmp/ccXtREge.ltrans0.ltrans.o: defined in discarded section `.text' of
> lib32_libmingw32_a-gccmain.o (symbol from plugin)
> collect2: error: ld returned 1 exit status
>
> With -O2 or other level there is no error at all.
I have no idea what is triggering this issue. But for example on gcc-8 I
was able to trigger it also with -O2 on the small example which I sent for
PATCH 2/2 (to trigger it just remove __attribute__((used)) from __main.c
file).
But I have not been able to trigger this issue with -O2 on the mingw-w64
source code with gcc-8. I was able only with -Ofast param.
So looks like that -O2 is just enabling some optimization which can
sometimes trigger it and -Ofast has higher chance to trigger it.
I was not able to reproduce it with gcc-14 and ld-2.44.
> It looks like that function __main from file mingw-w64-crt/crt/gccmain.c is
> somehow automatically discarded by gcc / ld LTO compiler when using -Ofast.
>
> Fix this problem by manually adding __attribute__((used)).
> ---
> mingw-w64-crt/crt/gccmain.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/mingw-w64-crt/crt/gccmain.c b/mingw-w64-crt/crt/gccmain.c
> index fc0e35003cd1..13ef04fdba20 100644
> --- a/mingw-w64-crt/crt/gccmain.c
> +++ b/mingw-w64-crt/crt/gccmain.c
> @@ -49,6 +49,7 @@ __do_global_ctors (void)
>
> static int initialized = 0;
>
> +__attribute__((used)) /* required for gcc -flto -Ofast */
> void
> __main (void)
> {
> --
> 2.20.1
>
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public