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.
---
 mingw-w64-crt/crt/crtdll.c       | 1 +
 mingw-w64-crt/crt/crtexe.c       | 2 ++
 mingw-w64-crt/crt/pseudo-reloc.c | 1 +
 3 files changed, 4 insertions(+)

diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 08cd5922a849..e264d4e96418 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -142,6 +142,7 @@ WINBOOL WINAPI DllMainCRTStartup (HANDLE, DWORD, LPVOID);
 int __mingw_init_ehandler (void);
 #endif
 
+__attribute__((used)) /* required due to bug in gcc / ld */
 WINBOOL WINAPI
 DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
 {
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 1656b0ee0006..03bda5912025 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -146,6 +146,7 @@ static int __tmainCRTStartup (void);
 
 int WinMainCRTStartup (void);
 
+__attribute__((used)) /* required due to bug in gcc / ld */
 int WinMainCRTStartup (void)
 {
   int ret = 255;
@@ -176,6 +177,7 @@ int mainCRTStartup (void);
 int __mingw_init_ehandler (void);
 #endif
 
+__attribute__((used)) /* required due to bug in gcc / ld */
 int mainCRTStartup (void)
 {
   int ret = 255;
diff --git a/mingw-w64-crt/crt/pseudo-reloc.c b/mingw-w64-crt/crt/pseudo-reloc.c
index 5a64e0d37b18..d6eb089d461e 100644
--- a/mingw-w64-crt/crt/pseudo-reloc.c
+++ b/mingw-w64-crt/crt/pseudo-reloc.c
@@ -480,6 +480,7 @@ do_pseudo_reloc (void * start, void * end, void * base)
      }
 }
 
+__attribute__((used)) /* required due to bug in gcc / ld */
 void
 _pei386_runtime_relocator (void)
 {
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to