summaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2021-05-24 23:12:31 +0300
committerTom Rini <trini@konsulko.com>2021-05-24 23:12:31 +0300
commit27c6d9663c3f42059514e47c17652304a1cfcfc9 (patch)
tree15e7a1deb449053a3ffca9b708813cce2da366ec /Makefile
parenteb53b943be2949ca111140a8e05532cd74cda058 (diff)
parent2fc62f2991744dfeec65f8619092c359d8ecbcb0 (diff)
downloadu-boot-27c6d9663c3f42059514e47c17652304a1cfcfc9.tar.xz
Merge branch '2021-05-24-add-lto-support'
- Add LTO (link time optimization) support to the build system and enable it on a few boards. This is an alternative to using -ffunction-sections/-fdata-sections and --gc-sections at link time to remove unused code. This can result in notable savings, but needs testing on each platform before use as it can expose problems by optimizing away various functionally necessary calls.
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile81
1 files changed, 74 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 9615f8b3a0..a038c6d6b6 100644
--- a/Makefile
+++ b/Makefile
@@ -676,6 +676,31 @@ else
KBUILD_CFLAGS += -O2
endif
+LTO_CFLAGS :=
+LTO_FINAL_LDFLAGS :=
+export LTO_CFLAGS LTO_FINAL_LDFLAGS
+ifdef CONFIG_LTO
+ ifeq ($(cc-name),clang)
+ LTO_CFLAGS += -flto
+ LTO_FINAL_LDFLAGS += -flto
+
+ AR = $(shell $(CC) -print-prog-name=llvm-ar)
+ NM = $(shell $(CC) -print-prog-name=llvm-nm)
+ else
+ NPROC := $(shell nproc 2>/dev/null || echo 1)
+ LTO_CFLAGS += -flto=$(NPROC)
+ LTO_FINAL_LDFLAGS += -fuse-linker-plugin -flto=$(NPROC)
+
+ # use plugin aware tools
+ AR = $(CROSS_COMPILE)gcc-ar
+ NM = $(CROSS_COMPILE)gcc-nm
+ endif
+
+ CFLAGS_NON_EFI += $(LTO_CFLAGS)
+
+ KBUILD_CFLAGS += $(LTO_CFLAGS)
+endif
+
ifeq ($(CONFIG_STACKPROTECTOR),y)
KBUILD_CFLAGS += $(call cc-option,-fstack-protector-strong)
CFLAGS_EFI += $(call cc-option,-fno-stack-protector)
@@ -972,6 +997,8 @@ LDFLAGS_u-boot += $(LDFLAGS_FINAL)
# Avoid 'Not enough room for program headers' error on binutils 2.28 onwards.
LDFLAGS_u-boot += $(call ld-option, --no-dynamic-linker)
+LDFLAGS_u-boot += --build-id=none
+
ifeq ($(CONFIG_ARC)$(CONFIG_NIOS2)$(CONFIG_X86)$(CONFIG_XTENSA),)
LDFLAGS_u-boot += -Ttext $(CONFIG_SYS_TEXT_BASE)
endif
@@ -1708,14 +1735,54 @@ u-boot-swap.bin: u-boot.bin FORCE
ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink)
+# Generate linker list symbols references to force compiler to not optimize
+# them away when compiling with LTO
+ifdef CONFIG_LTO
+u-boot-keep-syms-lto := keep-syms-lto.o
+u-boot-keep-syms-lto_c := $(patsubst %.o,%.c,$(u-boot-keep-syms-lto))
+
+quiet_cmd_keep_syms_lto = KSL $@
+ cmd_keep_syms_lto = \
+ NM=$(NM) $(srctree)/scripts/gen_ll_addressable_symbols.sh $^ >$@
+
+quiet_cmd_keep_syms_lto_cc = KSLCC $@
+ cmd_keep_syms_lto_cc = \
+ $(CC) $(filter-out $(LTO_CFLAGS),$(c_flags)) -c -o $@ $<
+
+$(u-boot-keep-syms-lto_c): $(u-boot-main)
+ $(call if_changed,keep_syms_lto)
+$(u-boot-keep-syms-lto): $(u-boot-keep-syms-lto_c)
+ $(call if_changed,keep_syms_lto_cc)
+else
+u-boot-keep-syms-lto :=
+endif
+
# Rule to link u-boot
# May be overridden by arch/$(ARCH)/config.mk
+ifdef CONFIG_LTO
+quiet_cmd_u-boot__ ?= LTO $@
+ cmd_u-boot__ ?= \
+ $(CC) -nostdlib -nostartfiles \
+ $(LTO_FINAL_LDFLAGS) $(c_flags) \
+ $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o $@ \
+ -T u-boot.lds $(u-boot-init) \
+ -Wl,--whole-archive \
+ $(u-boot-main) \
+ $(u-boot-keep-syms-lto) \
+ $(PLATFORM_LIBS) \
+ -Wl,--no-whole-archive \
+ -Wl,-Map,u-boot.map; \
+ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
+else
quiet_cmd_u-boot__ ?= LD $@
- cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
- -T u-boot.lds $(u-boot-init) \
- --start-group $(u-boot-main) --end-group \
- $(PLATFORM_LIBS) -Map u-boot.map; \
- $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
+ cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
+ -T u-boot.lds $(u-boot-init) \
+ --whole-archive \
+ $(u-boot-main) \
+ --no-whole-archive \
+ $(PLATFORM_LIBS) -Map u-boot.map; \
+ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
+endif
quiet_cmd_smap = GEN common/system_map.o
cmd_smap = \
@@ -1724,7 +1791,7 @@ cmd_smap = \
$(CC) $(c_flags) -DSYSTEM_MAP="\"$${smap}\"" \
-c $(srctree)/common/system_map.c -o common/system_map.o
-u-boot: $(u-boot-init) $(u-boot-main) u-boot.lds FORCE
+u-boot: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) u-boot.lds FORCE
+$(call if_changed,u-boot__)
ifeq ($(CONFIG_KALLSYMS),y)
$(call cmd,smap)
@@ -2007,7 +2074,7 @@ CLEAN_FILES += include/bmp_logo.h include/bmp_logo_data.h tools/version.h \
boot* u-boot* MLO* SPL System.map fit-dtb.blob* \
u-boot-ivt.img.log u-boot-dtb.imx.log SPL.log u-boot.imx.log \
lpc32xx-* bl31.c bl31.elf bl31_*.bin image.map tispl.bin* \
- idbloader.img flash.bin flash.log defconfig
+ idbloader.img flash.bin flash.log defconfig keep-syms-lto.c
# Directories & files removed with 'make mrproper'
MRPROPER_DIRS += include/config include/generated spl tpl \