LTO default behavior in GCC 12 for shared libraries versus static libraries
0 reputation · 05 Jul 2023, 21:14 UTC
0 reputation · 05 Jul 2023, 21:14 UTC
Goal: Assess whether GCC 12 could enable link‑time optimization by default for static libraries when the -fuse-linker-plugin flag is present, similar to the behavior already applied to shared libraries.
Constraints: The documentation explicitly states that static libraries still require -flto to activate LTO, and the change to shared‑library defaults does not extend to static archives. Release notes leave the decision unresolved, and variations in target architecture or linker support may affect the safety of such a default.
Questions: Should GCC 12 extend the default LTO behavior to static libraries under -fuse-linker-plugin? What would be the impact on build times, binary size, and compatibility with existing build systems? Are there circumstances where enabling LTO by default for static libraries could lead to link‑time failures?
According to the GCC 12 release notes and documentation, when the -fuse-linker-plugin option is present, link‑time optimization (LTO) is enabled by default for shared objects (.so files). This default does **not** extend to static archives (.a files); static libraries still require an explicit -flto (or -flto=thin) flag to participate in LTO.
-flto the archive’s members are treated as ordinary object files, and the plugin does not attempt to scan the whole archive for GIMPLE because doing so would change link semantics (ordering, weak symbols, linker‑script expectations).-flto flag to indicate that their object files were compiled with LTO‑compatible GIMPLE..a would force the compiler to generate GIMPLE for all translation units and the linker to run a whole‑program LTO step, increasing both compile‑ and link‑time, especially for large third‑party static libraries.-flto, but the gain would only appear for libraries that actually benefit from whole‑program analysis..o files inside an archive or on linker scripts that expect regular object sections. Switching to LTO by default could break those assumptions, leading to missing‑section errors or changes in symbol resolution.-flto (or with -fno-lto-objects). If the linker plugin tried to pull GIMPLE from them, it would find none and emit warnings like "no GIMPLE found in member", which could become errors under -Werror.--whole-archive or custom linker scripts that expect specific section names (.init, .fini) that LTO may rename or discard.Keep the current behavior: add -flto (or -flto=thin) explicitly when you want static libraries to participate in LTO. Only consider changing the default if you can verify that all static dependencies are built with LTO‑compatible GIMPLE and that your linker scripts and build rules do not depend on ordinary object ordering.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.