From 4052daa8a6100aaab1995114215c0120c9051a2a Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Sun, 9 Aug 2026 14:36:12 +0200 Subject: [PATCH] fix(build): keep per-function sections when generating code at LTO link time ESP-IDF compiles every component with -ffunction-sections/-fdata-sections and links with -Wl,--gc-sections, so that code and data nothing references does not reach the image. With CONFIG_COMPILER_LTO_LINKTIME the object files carry GIMPLE instead of machine code and code generation is deferred to the link, where those two options no longer apply. The LTO partition is therefore emitted as a single .text/.rodata, and since --gc-sections works at section granularity it can only keep or drop that section as a whole. Some function in it is always live, so nothing is dropped. The options are recorded in the object file (in the .gnu.lto_.opts section), but they do not reach the code generator. Code generation at link time runs as a separate compiler invocation (LTRANS) whose command line lto-wrapper reconstructs from those recorded options, and append_compiler_options() in gcc/lto-wrapper.cc forwards only CL_TARGET options plus a small hard-coded list: -fPIC/-fpic/-fPIE/-fpie, -fcommon, -fgnu-tm, -fopenmp/-fopenacc, -fcf-protection=, (-fasynchronous-)unwind-tables, -g, -O/-Os/-Og/-Ofast/-Oz and the diagnostics formatting options. Everything else hits the default arm and is dropped. -ffunction-sections/-fdata-sections are neither. They are plain Common options in common.opt without the Optimization marker, so they are not part of the per-function state that is streamed with each function (which is why -O2 and -Os do survive per translation unit under LTO), and they are not target options either. They reach LTRANS only if they are repeated on the link command line. Pass them next to -flto=auto, in both build systems. This can be verified on any LTO build by adding -save-temps to the link options and inspecting the generated .ltrans.mk, which contains the literal LTRANS command line: grep -o -- "-ffunction-sections\|-fdata-sections" build/*.ltrans.mk Measured on esp32c3 with -Os and CONFIG_COMPILER_LTO_COMPILETIME, application binary size in bytes: no LTO LTO LTO+fix fix saves hello_world 116112 119792 115808 -3984 (-3.33%) wifi/getting_started 734720 792960 732976 -59984 (-7.56%) Without this change, enabling LTO produces a larger image than not using LTO at all; with it, LTO is size neutral to slightly positive. Two effects contribute. The dead code inside the partition itself stays, which scales with how much code was compiled with LTO. On top of that, every function retained this way keeps whatever it references alive as well, transitively and across object boundaries: constant data, other functions, and sections of objects that were not compiled with LTO at all. That second effect is not bounded by the size of the LTO partition and can dominate. In the wifi/getting_started case a single retained function, esp_crt_bundle_attach(), is the only referrer of the mbedTLS certificate bundle, so a 55 KB .rodata blob stayed in an application that never uses TLS. A plain non-LTO build collects all of it, which is what this change restores. Signed-off-by: Frantisek Hrbata --- CMakeLists.txt | 9 +++++++++ tools/cmakev2/project.cmake | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4c7b187c0c..9e1f266487b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -294,6 +294,15 @@ endif() if(CONFIG_COMPILER_LTO_LINKTIME AND NOT BOOTLOADER_BUILD AND NOT ESP_TEE_BUILD) list(APPEND link_options "-flto=auto") + # LTO generates code at link time, so the -ffunction-sections/-fdata-sections + # applied to compile_options doesn't reach it: the LTRANS recompilation takes + # its code generation options from the link command line. Without them the + # LTO partition is emitted as a single .text/.data, which defeats + # -Wl,--gc-sections (it can only drop whole sections) and keeps unreferenced + # functions in the image. + list(APPEND link_options "-ffunction-sections" + "-fdata-sections") + if(CONFIG_APP_REPRODUCIBLE_BUILD) # LTO generates code at link time, where the path remapping applied to # compile_options doesn't take effect, so pass it to the linker as well. diff --git a/tools/cmakev2/project.cmake b/tools/cmakev2/project.cmake index 8d9caeb14de..8db05462d6b 100644 --- a/tools/cmakev2/project.cmake +++ b/tools/cmakev2/project.cmake @@ -443,6 +443,14 @@ function(__init_project_configuration) endif() if(CONFIG_COMPILER_LTO_LINKTIME AND set_compiler_lto) list(APPEND link_options "-flto=auto") + # LTO generates code at link time, so the -ffunction-sections/-fdata-sections + # applied to compile_options doesn't reach it: the LTRANS recompilation takes + # its code generation options from the link command line. Without them the + # LTO partition is emitted as a single .text/.data, which defeats + # -Wl,--gc-sections (it can only drop whole sections) and keeps unreferenced + # functions in the image. + list(APPEND link_options "-ffunction-sections" + "-fdata-sections") else() list(APPEND compile_options "-fno-lto") endif()