FS#6592 - Missing symbols while linking with MinGW and LTO

Attached to Project: OpenTTD
Opened by Grzegorz Duczyński (adf88) - Tuesday, 25 July 2017, 06:11 GMT
Type Bug
Category Core
Status New
Assigned To No-one
Operating System All
Severity Very Low
Priority Normal
Reported Version trunk
Due in Version Undecided
Due Date Undecided
Percent Complete 0%
Votes 0
Private No


When compiling with LTO enabled (--enable-lto), GCC (-fwhole-program) changes all external symbols into static, except of the 'main' function. This causes some missing symbols when linking Windows builds of OpenTTD (e.g. with MinGW):

* WinMain - Entry point. '-mwindows' and other Windows-specific options didn't help, happened in GCC 6, GCC 7 seems smarter.
* _safe_esp - Variable referenced inside inline assembler code. Probably all GCC versions.

I'm including a fix for the trunk. It adds '__attribute__ ((used))' to the two symbols.
This task depends upon

Comment by frosch (frosch) - Tuesday, 25 July 2017, 17:03 GMT
Does this mean that _safe_esp can now be declared static?
It looks to me the non-staticness tries to achieve the same thing.
Comment by Grzegorz Duczyński (adf88) - Tuesday, 25 July 2017, 17:19 GMT
Seems so, but I'm not sure about other compilers.
Comment by Grzegorz Duczyński (adf88) - Wednesday, 26 July 2017, 16:28 GMT
OK, I found out that 'volatile' works on _safe_esp in GCC 6 and 7. This would be a preferred way I think. I'm uploading updated patch.