From b56f407614b58ab139bef3612195fbdf07670436 Mon Sep 17 00:00:00 2001 From: Arthur Heymans Date: Wed, 21 Aug 2024 10:12:16 +0200 Subject: [PATCH] Add initial experimental LTO support This will not succeed in compiling on all target and compiler combinations but at least gets the ball rolling. The change is not invasive. Some notes: - GCC has issues with LTO on ARM - Clang uses LLD automatically on some arch - Clang with LTO fails on x86 as it forwards the linking to GCC for some reason - SMM building succeeds but the binary is empty Change-Id: Ieb9204777fd349542744a8946e2207731c37969c Signed-off-by: Arthur Heymans Reviewed-on: https://review.coreboot.org/c/coreboot/+/84003 Tested-by: build bot (Jenkins) Reviewed-by: Lean Sheng Tan Reviewed-by: Nico Huber --- Makefile.mk | 18 +++++++++++++++--- src/Kconfig | 7 +++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Makefile.mk b/Makefile.mk index 9efef49ba6..e7a9ab1bb8 100644 --- a/Makefile.mk +++ b/Makefile.mk @@ -373,7 +373,7 @@ endef cbfs-files-processor-struct= \ $(eval $(2): $(1) $(obj)/build.h $(obj)/fmap_config.h $(KCONFIG_AUTOHEADER); \ printf " CC+STRIP $(1)\n"; \ - $(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) --param asan-globals=0 $$(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \ + $(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) -fno-lto --param asan-globals=0 $$(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \ $(OBJCOPY_ramstage) -O binary --only-section='.*data*' --only-section='.*bss*' \ --set-section-flags .*bss*=alloc,contents,load $(2).tmp $(2); \ rm -f $(2).tmp) \ @@ -547,6 +547,14 @@ CFLAGS_common += -Wno-array-compare endif endif +ifeq ($(CONFIG_LTO),y) +CFLAGS_common += -flto +# Clang can not deal with GCC lto objects +ifeq ($(CONFIG_COMPILER_GCC),y) +ADAFLAGS_common += -flto +endif +endif + ADAFLAGS_common += -gnatp ADAFLAGS_common += -Wuninitialized ADAFLAGS_common += -Wall @@ -1316,11 +1324,15 @@ endif # CONFIG_CBFS_VERIFICATION define link_stage # $1 stage name - +ifeq ($(CONFIG_LTO),y) +$$(objcbfs)/$(1).debug: $$$$($(1)-libs) $$$$($(1)-objs) + @printf " LINK $$(subst $$(obj)/,,$$(@))\n" + $$(CC_$(1)) $$(CPPFLAGS_$(1)) $$(CFLAGS_$(1)) $$(LDFLAGS_$(1):%=-Wl,%) -o $$@ -L$$(obj) $$(COMPILER_RT_FLAGS_$(1):%=-Wl,%) -Wl,--whole-archive -Wl,--start-group $$(filter-out %.ld,$$($(1)-objs)) $$($(1)-libs) -Wl,--no-whole-archive $$(COMPILER_RT_$(1)) -Wl,--end-group -T $(call src-to-obj,$(1),$(CONFIG_MEMLAYOUT_LD_FILE)) +else $$(objcbfs)/$(1).debug: $$$$($(1)-libs) $$$$($(1)-objs) @printf " LINK $$(subst $$(obj)/,,$$(@))\n" $$(LD_$(1)) $$(LDFLAGS_$(1)) -o $$@ -L$$(obj) $$(COMPILER_RT_FLAGS_$(1)) --whole-archive --start-group $$(filter-out %.ld,$$($(1)-objs)) $$($(1)-libs) --no-whole-archive $$(COMPILER_RT_$(1)) --end-group -T $(call src-to-obj,$(1),$(CONFIG_MEMLAYOUT_LD_FILE)) - +endif endef ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y) diff --git a/src/Kconfig b/src/Kconfig index b6dc67f00a..55fad952bb 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -116,6 +116,13 @@ config CCACHE For details see https://ccache.samba.org. +config LTO + bool "Use link time optimization (LTO) (experimental)" + default n + help + Compile with link time optimization. This can often decrease the + final binary size, but may increase compilation time. + config IWYU bool "Test platform with include-what-you-use" help