On Saturday 17 December 2022 23:01:10 LIU Hao wrote:
> 在 2022-12-17 22:22, Pali Rohár 写道:
> > 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).
> > 
> > 
> 
> Wouldn't this cause executables to contain all three entry-point functions,
> which I think should be avoided?

_DllMainCRTStartup@12 is in different object file (dllcrt2.o) as the
other two function. gcc links only one object file for startup code. So
for DLL builds only _DllMainCRTStartup@12 entry point is included in
final DLL binary, others two not. __attribute__((used)) in source files
which are not linked into final executable has no effect.

In executable EXE builds is not _DllMainCRTStartup@12 included (because
gcc does not link dllcrt2.o for EXE file). But as I wrote above both
_mainCRTStartup and _WinMainCRTStartup startup functions are present in
every EXE file. Non-existence of __attribute__((used)) has no effect for
non-LTO builds because non-LTO compiler does cannot deduce that symbol
is unused.

With and also without this patch for non-LTO builds only one of those
function is referenced and called. Second one is not used by anybody,
not called by anybody and does not cause any issue. Just it is a dead
binary code which increase size of executable like bloatware.

With this patch both function are included into every EXE file also for
LTO builds, like for non-LTO builds.


So we would like to avoid having both _mainCRTStartup and
_WinMainCRTStartup in every EXE file. But due to mentioned gcc / ld bug
it is not possible for LTO builds now. And for non-LTO builds there is
no change by this patch.


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to