crt: Fix building LTO version of mingw-w64 crt libraries
Currently when linking target application with LTO builds of mingw-w64
libraries, it is needed to manually specify entry point of mingw-w64 crt
startup function via linker flag -Wl,-e,_mainCRTStartup (for console
executables) or -Wl,-e,_WinMainCRTStartup (for GUI executables) or
-Wl,-e,_DllMainCRTStartup@12 (for DLL libraries). Otherwise LTO compiler
drops startup function generates output binary totally broken and
non-working (executable crashes during during startup).
This looks like a bug in LTO compiler or GNU linker that it drops default
entry point because GNU linker has already hardcoded symbol names of
default entry points.
There is a simple way to prevent dropping some symbol by LTO compiler and
it is by marking that symbol by __attribute__((used)).
So mark all crt startup functions by __attribute__((used)) to ensure that
they will be always present in final executable or DLL library. With this
change it is not needed to manually specify -e flag with entry point name.
GNU linker tries to reference also _pei386_runtime_relocator() function
when it generates pseudo-reloc data and complains if LTO compiler drops it.
So mark this function as used too.
`__pei386_runtime_relocator' referenced in section `.rdata' of ertr000001.o: defined in discarded section `.text' of lib32_libmingw32_a-pseudo-reloc.o (symbol from plugin)
collect2: error: ld returned 1 exit status
Note that gcc specs defined in mingw32.h already contains "-e" flag in its
*link: section, so this bug does not affect building of DLL libraries with
default mingw32.h gcc config file. So marking DllMainCRTStartup() as used
is not necessary for default gcc builds but for completeness and for some
custom gcc builds with modified specs definitions, mark also startup
function DllMainCRTStartup() as used.
It would be a nice to fix this bug in LTO compiler and GNU linker and
allows to build mingw-w64 startup files without __attribute__((used)).
File crtexe.c contains startup function for both Console and GUI
executables and obviously always only one is used. So LTO compiler has a
chance can drop GUI startup function WinMainCRTStartup() for console
executable (and vice-versa) but only when WinMainCRTStartup() is not marked
as used... And same applies for _pei386_runtime_relocator() function which
is needed only when linker generates pseudo-reloc data for executable.
Signed-off-by: LIU Hao <lh_mouse@126.com>
3 files changed