arm: Make the LTO conditions in arch/arm/config.mk take effect
Subsystems (2)
v2 cover arm: Make the LTO conditions in arch/arm/config.mk take effect
As requested in the mpc85xx LTO review [1], this sets LTO_ENABLE before arch/arm/config.mk is read, so that 32-bit ARM LTO builds of U-Boot proper leave out -ffunction-sections -fdata-sections.
v1 broke the K3 R5 link [2]. Patch 1 fixes the dangling reference in arch/arm/lib/cache-cp15.c that the missing -ffunction-sections exposed. Patch 2 keeps both flags on ARM64, where U-Boot proper would otherwise grow on 104 of 105 boards, by up to 30240 bytes on i.MX8M. Patch 3 keeps them with SKIP_RELOCATE_CODE, whose data relocation does not expect section anchors. Patch 4 keeps --gc-sections, without which 32-bit U-Boot proper would be 2080 to 10832 bytes larger.
Build-tested on all 208 ARM LTO boards in the U-Boot CI container. Patches 1 to 4 change no image. Patch 5 shrinks u-boot-nodtb.bin of the 102 32-bit boards by 896 to 4464 bytes. No other image changes, and NO_LTO=1 builds stay the same.
Boot-tested on a Turris Omnia, one of these 32-bit boards, loaded into RAM with kwboot. SPL and U-Boot print the same boot log as on main apart from the version, and the mmc, sf, i2c, usb and wdt commands give the same output.
Changes in v2:
- Add patch 1 for the K3 R5 link failure, patch 2 for ARM64 and patch 3 for SKIP_RELOCATE_CODE.
- Test all 208 LTO boards instead of the 30 with CONFIG_LTO=y, and move the size figures from the patch notes to this cover letter.
[1] https://lore.kernel.org/u-boot/20261012190720.GF3722450@bill-the-cat/
[2] https://lore.kernel.org/u-boot/20261020212819.GK720525@bill-the-cat/
v1: https://lore.kernel.org/u-boot/[email protected]/
Josef Schlehofer (5):
arm: cache-cp15: Do not reference the MMU helper without an MMU
arm: Keep -ffunction-sections -fdata-sections with LTO on ARM64
arm: Keep the section flags with LTO and SKIP_RELOCATE_CODE
arm: Keep --gc-sections with LTO
Makefile: Set LTO_ENABLE before including config.mk
Makefile | 14 +++++++------- arch/arm/config.mk | 4 +--- arch/arm/lib/cache-cp15.c | 2 ++ 3 files changed, 10 insertions(+), 10 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 1/5 arm: cache-cp15: Do not reference the MMU helper without an MMU
Since commit ee0fbf4ef766 ("arch: cache: cp15: Add mmu_set_region_dcache_behaviour() when SYS_DCACHE_OFF is enable"), mmu_set_region_dcache_behaviour() sits outside the CONFIG_SYS_ARM_MMU block and calls mmu_set_region_dcache_behaviour_phys(), which only exists inside it. On cores with an MPU, such as the Cortex-R5 of the K3 SoCs, the call is a dangling reference. It only links because -ffunction-sections puts the unused function in a section of its own, which --gc-sections drops. cache-cp15.o is built without LTO, so LTO cannot drop it instead.
Without -ffunction-sections, as the LTO conditions in arch/arm/config.mk intend, the K3 R5 builds fail to link:
arch/arm/lib/cache-cp15.c:331: undefined reference to `mmu_set_region_dcache_behaviour_phys'
Define the function for the D-cache on only when CONFIG_SYS_ARM_MMU is set, as before that commit. A caller on an MPU core then still fails to link. An IS_ENABLED() check would instead make the function silently ignore a request for an uncached region there.
--- arch/arm/lib/cache-cp15.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm/lib/cache-cp15.c +2 -0
@@ -325,9 +325,11 @@ int dcache_status(void) return (get_cr() & CR_C) != 0; } +#ifdef CONFIG_SYS_ARM_MMU void mmu_set_region_dcache_behaviour (phys_addr_t start,size_t size,enum dcache_option option) {mmu_set_region_dcache_behaviour_phys (start, start, size, option); } +#endif /* CONFIG_SYS_ARM_MMU */ #endif
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 2/5 arm: Keep -ffunction-sections -fdata-sections with LTO on ARM64
In reply to Josef Schlehoferarch/arm/config.mk is meant to leave out -ffunction-sections and -fdata-sections for LTO builds. Once LTO_ENABLE is set in time, and with --gc-sections kept, that makes U-Boot proper smaller on 32-bit ARM but larger on almost every ARM64 board, most of all on i.MX8M, where the 16 KiB alignment of early_tlb then pads .data.
Leave the flags out only for 32-bit LTO builds. This changes no image on main.
--- arch/arm/config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/config.mk +1 -1
@@ -17,7 +17,7 @@ ifneq ($(LTO_ENABLE)$(CONFIG_USE_PRIVATE_LIBGCC),yy) LDFLAGS_FINAL += --gc-sections endif -ifneq ($(LTO_ENABLE),y) +ifneq ($(LTO_ENABLE).$(CONFIG_ARM64),y.) PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections endif
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 3/5 arm: Keep the section flags with LTO and SKIP_RELOCATE_CODE
In reply to Josef SchlehoferWith SKIP_RELOCATE_CODE, relocate.S relocates a literal only when its value lies between __data_start and __data_end. Without -fdata-sections, GCC can reach .data objects through a section anchor that lies past the end of their block. If that block is the last one in .data, the anchor lies above __data_end, so a literal that holds it keeps its link address, and stores through it go to the read-only alias of the data. The .data of r8a78000_ironhide_cm33, the only board with SKIP_RELOCATE_CODE, does not trigger this today.
Keep -ffunction-sections -fdata-sections with LTO when SKIP_RELOCATE_CODE is set. This changes no image on main.
--- arch/arm/config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/config.mk +1 -1
@@ -17,7 +17,7 @@ ifneq ($(LTO_ENABLE)$(CONFIG_USE_PRIVATE_LIBGCC),yy) LDFLAGS_FINAL += --gc-sections endif -ifneq ($(LTO_ENABLE).$(CONFIG_ARM64),y.) +ifneq ($(LTO_ENABLE).$(CONFIG_ARM64)$(CONFIG_SKIP_RELOCATE_CODE),y.) PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections endif
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 4/5 arm: Keep --gc-sections with LTO
In reply to Josef SchlehoferCommit 34ccadcd3334 ("ARM: don't use --gc-sections with LTO when using private libgcc") left out --gc-sections for LTO builds that use the private libgcc. Since commit a55014d09b29 ("Makefile: Allow LTO to be disabled for a build"), the condition tests LTO_ENABLE, which the top Makefile sets only after it has read arch/arm/config.mk, so U-Boot proper has been linked with --gc-sections ever since.
Once the next patch sets LTO_ENABLE in time, the condition would take effect again. U-Boot proper would then keep the sections that nothing uses, such as the .discard.addressable section of keep-syms-lto.o. Keep --gc-sections for all builds. This changes no image on main.
--- arch/arm/config.mk | 2 -- 1 file changed, 2 deletions(-)
arch/arm/config.mk +0 -2
@@ -13,9 +13,7 @@ CFLAGS_NON_EFI := -fno-pic $(FIXED_REG) -ffunction-sections -fdata-sections \ -fstack-protector-strong CFLAGS_EFI := -fpic -fshort-wchar -ifneq ($(LTO_ENABLE)$(CONFIG_USE_PRIVATE_LIBGCC),yy) LDFLAGS_FINAL += --gc-sections -endif ifneq ($(LTO_ENABLE).$(CONFIG_ARM64)$(CONFIG_SKIP_RELOCATE_CODE),y.) PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v2 5/5 Makefile: Set LTO_ENABLE before including config.mk
In reply to Josef SchlehoferCommit a55014d09b29 ("Makefile: Allow LTO to be disabled for a build") made the condition in arch/arm/config.mk that leaves out -ffunction-sections -fdata-sections with LTO test LTO_ENABLE. The top Makefile, however, sets LTO_ENABLE only after it has included config.mk, which reads arch/$(ARCH)/config.mk, so for U-Boot proper the condition has seen an empty LTO_ENABLE since then. The xPL builds are not affected, as scripts/Makefile.xpl adds these flags for them anyway.
Set LTO_ENABLE before config.mk is included. After the previous patches, this leaves the flags out only for U-Boot proper of the 32-bit LTO boards without SKIP_RELOCATE_CODE.
--- Makefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
Makefile +7 -7
@@ -819,6 +819,13 @@ u-boot.cfg spl/u-boot.cfg tpl/u-boot.cfg vpl/u-boot.cfg: -include include/autoconf.mk -include include/autoconf.mk.dep +export LTO_ENABLE + +# This is y if LTO is enabled for this build. See NO_LTO=1 to disable LTO +ifeq ($(NO_LTO),) +LTO_ENABLE=$(if $(CONFIG_LTO),y) +endif + # We want to include arch/$(ARCH)/config.mk only when include/config/auto.conf # is up-to-date. When we switch to a different board configuration, old CONFIG # macros are still remaining in include/config/auto.conf. Without the following
@@ -858,13 +865,6 @@ export CFLAGS_EFI # Compiler flags to add when building EFI app export CFLAGS_NON_EFI # Compiler flags to remove when building EFI app export EFI_TARGET # binutils target if EFI is natively supported -export LTO_ENABLE - -# This is y if LTO is enabled for this build. See NO_LTO=1 to disable LTO -ifeq ($(NO_LTO),) -LTO_ENABLE=$(if $(CONFIG_LTO),y) -endif - # If board code explicitly specified LDSCRIPT or CONFIG_SYS_LDSCRIPT, use # that (or fail if absent). Otherwise, search for a linker script in a # standard location.
Signature · 1 line
2.54.0 (Apple Git-157)
Quoted 12 lines · Josef Schlehofer
As requested in the mpc85xx LTO review [1], this sets
LTO_ENABLEbeforearch/arm/config.mkis read, so that 32-bit ARM LTO builds of U-Boot proper leave out -ffunction-sections -fdata-sections.v1 broke the K3 R5 link [2]. Patch 1 fixes the dangling reference in
arch/arm/lib/cache-cp15.cthat the missing -ffunction-sections exposed. Patch 2 keeps both flags on ARM64, where U-Boot proper would otherwise grow on 104 of 105 boards, by up to 30240 bytes on i.MX8M. Patch 3 keeps them withSKIP_RELOCATE_CODE, whose data relocation does not expect section anchors. Patch 4 keeps --gc-sections, without which 32-bit U-Boot proper would be 2080 to 10832 bytes larger.
Quoted 1 line
[...]
Applied to u-boot/next, thanks!
[1/5] arm: cache-cp15: Do not reference the MMU helper without an MMU
commit: da83c61a114901d2b64935d3e05323b2cf271637
[2/5] arm: Keep -ffunction-sections -fdata-sections with LTO on ARM64
commit: 7a3fc96a8ba005b52381dada33c8d40ed0edbe5e
[3/5] arm: Keep the section flags with LTO and SKIP_RELOCATE_CODE
commit: f0a83474bb63c5394922e46d1e245193e47dbd5a
[4/5] arm: Keep --gc-sections with LTO
commit: 2aa7d3d7b5aa3afa74e66c01cc7c76f538729c84
[5/5] Makefile: Set LTO_ENABLE before including config.mk
commit: 5f3bcac3f7f94e447191bd3945b9a7ea964d336aSignature · 1 line
Tom