Comment 7 for bug 1743765

Revision history for this message
David Brown (davidbrown) wrote :

Since the code works when LTO is disabled, the most likely cause is code or data that is not being linked because its use cannot be traced back to main(). In C, all code other than the library start up routines is expected to be called directly or indirectly from main(). LTO traces this to see what code is actually used, and to drop code and data that can't legally be accessed from C. You might need to add "externally_visible" attributes to certain functions, or use "keep" in the linker file, in order to avoid dropping such code. Likely candidates include interrupt functions, "naked" functions, and parts of context switch routines in an RTOS.

Examining the map files is very useful for seeing such cases.

The other thing about LTO is that it can be a lot more sensitive to bugs in the C code that works on more limited compilers (or with more limited optimisations enabled), but is due to incorrect C usage. Common cases are to get type aliasing wrong (compile with "-fno-strict-aliasing" to turn off this optimisation), signed integer overflow leading to different code generation when merging functions across modules (try "-fwrapv" to test this hypothesis), and assuming that calling functions in other modules acts as a memory barrier (and thus you are missing "volatile" qualifiers or memory barriers).