Hi David and Jackson,
My understanding (purely based on what I’ve read so please correct me if I am wrong) is that GCC enables LTO by default (i.e. no flags specified) if any linked libraries have LTO enabled and that adding the “-fno-lto” flag is needed to explicitly disable LTO in this case.
Were you seeing that if an LTO-enabled library was linked to other code compiled without LTO, that the linking would fail? That is, LTO needed to be enabled or disabled for all compilation units for successful linking.
Regards, Brian
From: David Hu David.Hu2@arm.com Sent: Friday, April 4, 2025 3:37 AM To: Quach, Brian brian@ti.com; Jackson Cooper-Driver Jackson.Cooper-Driver@arm.com; tf-m@lists.trustedfirmware.org; Antonio De Angelis Antonio.DeAngelis@arm.com Cc: nd nd@arm.com Subject: [EXTERNAL] RE: Link Time Optimization
Hi Biran, > How did you overcome the issue: “We met a similar issue when we tried to optimize one component in TF-M with LTO. ”? We didn’t. I cannot recall the details. We were concerned about the side effect of LTO and halt the link optimization. ZjQcmQRYFpfptBannerStart This message was sent from outside of Texas Instruments.
Do not click links or open attachments unless you recognize the source of this email and know the content is safe.
ZjQcmQRYFpfptBannerEnd Hi Biran,
How did you overcome the issue: “We met a similar issue when we tried to optimize one component in TF-M with LTO.”?
We didn’t. I cannot recall the details. We were concerned about the side effect of LTO and halt the link optimization.
If explicitly disabling LTO at link time is an option, it seems worthwhile to add the flag just in case.
It sounds like a use-case/platform-specific link flag. But I don’t have any strong opinions on this. May I ask for more clarification about disabling LTO in TF-M: is this explicit disablement used to intentionally stop TF-M from linking to libraries with LTO enabled? If it is, it makes sense to me to add it and additional comments as a protection.
Best regards, David
From: Quach, Brian <brian@ti.commailto:brian@ti.com> Sent: Wednesday, April 2, 2025 6:23 PM To: David Hu <David.Hu2@arm.commailto:David.Hu2@arm.com>; Jackson Cooper-Driver <Jackson.Cooper-Driver@arm.commailto:Jackson.Cooper-Driver@arm.com>; tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org; Antonio De Angelis <Antonio.DeAngelis@arm.commailto:Antonio.DeAngelis@arm.com> Cc: nd <nd@arm.commailto:nd@arm.com> Subject: RE: Link Time Optimization
Hi David,
Your understanding is correct. For our specific use case, we link link TF-M secure image to a library which has LTO enabled (we don’t have an LTO-disabled version of the library).
Since TFM was not tested with LTO, I want to ensure that it is disabled. If explicitly disabling LTO at link time is an option, it seems worthwhile to add the flag just in case.
How did you overcome the issue: “We met a similar issue when we tried to optimize one component in TF-M with LTO.”? AFAIK, using LTO for a single component is something that is feasible (but not as effective as using it for the whole project).
Regards, Brian
From: David Hu <David.Hu2@arm.commailto:David.Hu2@arm.com> Sent: Wednesday, April 2, 2025 11:44 AM To: Quach, Brian <brian@ti.commailto:brian@ti.com>; Jackson Cooper-Driver <Jackson.Cooper-Driver@arm.commailto:Jackson.Cooper-Driver@arm.com>; tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org; Antonio De Angelis <Antonio.DeAngelis@arm.commailto:Antonio.DeAngelis@arm.com> Cc: nd <nd@arm.commailto:nd@arm.com> Subject: [EXTERNAL] RE: Link Time Optimization
Hi Brian, It is the expected behavior of LTO. The whole project should be built with LTO enabled if any library used is enabled with LTO. We met a similar issue when we tried to optimize one component in TF-M with LTO. IIRC, no pre-built binaries ZjQcmQRYFpfptBannerStart This message was sent from outside of Texas Instruments.
Do not click links or open attachments unless you recognize the source of this email and know the content is safe.
ZjQcmQRYFpfptBannerEnd Hi Brian,
It is the expected behavior of LTO. The whole project should be built with LTO enabled if any library used is enabled with LTO. We met a similar issue when we tried to optimize one component in TF-M with LTO.
IIRC, no pre-built binaries (maybe CMSIS ones) required by TF-M is built with LTO. It should be unnecessary for TF-M to explicitly disable LTO in linker configuration.
Please correct me if I misunderstood your case. You link TF-M secure image to those binaries with LTO enabled. Imho, TF-M may be built with LTO enabled then in your case. But it might not be a common case in TF-M as I mentioned above.
Best regards, David
From: Quach, Brian via TF-M <tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org> Sent: Wednesday, April 2, 2025 5:11 PM To: Jackson Cooper-Driver <Jackson.Cooper-Driver@arm.commailto:Jackson.Cooper-Driver@arm.com>; tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org; Antonio De Angelis <Antonio.DeAngelis@arm.commailto:Antonio.DeAngelis@arm.com> Subject: [TF-M] Re: Link Time Optimization
Hi Jackson,
The issue is that some of our libraries which are linked into the secure image have LTO enabled.
Per https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.htmlhttps://urldefense.com/v3/__https:/gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html__;!!G3vK!SGuv2oTspQaxI_RF8uvX6BLXIgrEVzcY0ToF6N8TBQ6vg0BosISqHmEiX9k9-1z5oft-yv-Jig$: The important thing to keep in mind is that to enable link-time optimizations you need to use the GCC driver to perform the link step. GCC automatically performs link-time optimization if any of the objects involved were compiled with the -flto command-line option. You can always override the automatic decision to do link-time optimization by passing -fno-lto to the link command.
So it seems that the -fno-lto flag must be used to explicitly opt out of LTO.
It might be wise to add that flag to toolchain_GNUARM.cmake to ensure no one is unknowingly having LTO enabled as you have not analyzed the impact.
Regards, Brian
From: Jackson Cooper-Driver <Jackson.Cooper-Driver@arm.commailto:Jackson.Cooper-Driver@arm.com> Sent: Wednesday, April 2, 2025 1:46 AM To: tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org; Quach, Brian <brian@ti.commailto:brian@ti.com>; Antonio De Angelis <Antonio.DeAngelis@arm.commailto:Antonio.DeAngelis@arm.com> Subject: [EXTERNAL] Re: Link Time Optimization
Hi Brian, Worth just saying that GCC does not enable any optimizations by default. LTO needs to be explicitly enabled on the command line using -flto - see the GCC docs here - https: //gcc. gnu. org/onlinedocs/gcc/Optimize-Options. html CMake also ZjQcmQRYFpfptBannerStart This message was sent from outside of Texas Instruments.
Do not click links or open attachments unless you recognize the source of this email and know the content is safe.
ZjQcmQRYFpfptBannerEnd Hi Brian,
Worth just saying that GCC does not enable any optimizations by default. LTO needs to be explicitly enabled on the command line using -flto - see the GCC docs here - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.htmlhttps://urldefense.com/v3/__https:/gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html__;!!G3vK!VU8ng_LK4ryG6ctsxN9oC0C_W4zybehjT8Hvkiv80VjMfcPsFIYjBLlgN3deEvFCTGiWzOk49XdJ2UjQOA$
CMake also has a way to enable LTO using a specific CMake flag which you can set - details here - https://stackoverflow.com/questions/31355692/how-do-i-enable-link-time-optim...https://urldefense.com/v3/__https:/stackoverflow.com/questions/31355692/how-do-i-enable-link-time-optimization-lto-with-cmake__;!!G3vK!VU8ng_LK4ryG6ctsxN9oC0C_W4zybehjT8Hvkiv80VjMfcPsFIYjBLlgN3deEvFCTGiWzOk49Xct_H-d_Q$ But we do not currently do that in TF-M
Thanks, Jackson ________________________________ From: Antonio De Angelis via TF-M <tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org> Sent: 01 April 2025 22:44 To: tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org <tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org>; Quach, Brian <brian@ti.commailto:brian@ti.com> Subject: [TF-M] Re: Link Time Optimization
Hi Brian,
no, TF-M does not use it (on purpose), i.e. if GCC enables it by default, the impact of LTO has not been assessed.
Thanks, Antonio ________________________________ From: Quach, Brian via TF-M <tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org> Sent: Tuesday, April 1, 2025 16:01 To: tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org <tf-m@lists.trustedfirmware.orgmailto:tf-m@lists.trustedfirmware.org> Subject: [TF-M] Link Time Optimization
Hi, does TF-M use LTO? I didn’t see any flags to disable it in toolchain_GNUARM.cmake. I think for GCC, it’s enabled by default if no flag is set.
Regards,
Brian Quach
SimpleLink MCU
Texas Instruments Inc.
12500 TI Blvd, MS F-4000
Dallas, TX 75243
214-479-4076