https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109368
Bug ID: 109368 Summary: LTO drops entry point symbol Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: lto Assignee: unassigned at gcc dot gnu.org Reporter: pali at kernel dot org CC: marxin at gcc dot gnu.org Target Milestone: --- Target: Mingw32 LTO for PE executables drops entry point symbol when the default entry point is used. There is no warning and just PE AddressOfEntryPoint is zeroed. Which results in broken PE binary. When non-default entry point is used and specified via -e option then LTO does not drop entry point symbol and generates working PE executable. Simple test case which does not use any system library or startup file: $ cat test-nostartfiles.c int mainCRTStartup(void) { return 0; } Default console binary has entry point mainCRTStartup() function (as hardcoded in LD sources). $ i686-w64-mingw32-gcc -Wall -Wextra -nostartfiles -nodefaultlibs -nostdlib test-nostartfiles.c -o test-nostartfiles.exe Without LTO it generates working PE binary which correctly returns 0 to system. It also has correct AddressOfEntryPoint field in PE: $ i686-w64-mingw32-objdump -p test-nostartfiles.exe | grep AddressOfEntryPoint AddressOfEntryPoint 00001000 When compiling with LTO it does not throw any warning but generates broken PE binary: $ i686-w64-mingw32-gcc -Wall -Wextra -nostartfiles -nodefaultlibs -nostdlib test-nostartfiles.c -o test-nostartfiles.exe -flto Trying to run it, it crashes and has zeroed AddressOfEntryPoint: $ i686-w64-mingw32-objdump -p test-nostartfiles.exe | grep AddressOfEntryPoint AddressOfEntryPoint 00000000 When non-default entry point is used (specified via -e option) then LTO works correctly and does not drop its entry point. $ cat test-nostartfiles2.c int my_entry(void) { return 0; } $ i686-w64-mingw32-gcc -Wall -Wextra -nostartfiles -nodefaultlibs -nostdlib -e _my_entry test-nostartfiles2.c -o test-nostartfiles2.exe -flto $ i686-w64-mingw32-objdump -p test-nostartfiles2.exe | grep AddressOfEntryPoint AddressOfEntryPoint 00001000 Compiled binary works fine. So there is a bug in LTO compiler that it drops entry point if default one is used (i.e. when entry point is not specified via -e option).