From cc03e713d6f0e0cb8f782152ca8478b1bd3225a7 Mon Sep 17 00:00:00 2001 From: Sultan Alsawaf Date: Sat, 26 Dec 2020 19:58:22 -0800 Subject: [PATCH] lto: Add Link Time Optimization support for GCC This requires a modern version of GCC and various other patches in order to work. LTO results in a smaller kernel binary with better performance. Based off of work from Andi Kleen . Signed-off-by: Sultan Alsawaf --- Makefile | 20 ++++++++++++++++++-- init/Kconfig | 3 +++ scripts/Makefile.lib | 2 +- scripts/link-vmlinux.sh | 4 ++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index ce823734bea4..5962c09af907 100644 --- a/Makefile +++ b/Makefile @@ -380,8 +380,8 @@ LD = $(CROSS_COMPILE)ld CC = $(CROSS_COMPILE)gcc LDGOLD = $(CROSS_COMPILE)ld.gold CPP = $(CC) -E -AR = $(CROSS_COMPILE)ar -NM = $(CROSS_COMPILE)nm +AR ?= $(CROSS_COMPILE)ar +NM ?= $(CROSS_COMPILE)nm STRIP = $(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump @@ -684,6 +684,22 @@ LLVM_NM := llvm-nm export LLVM_AR LLVM_NM endif +ifdef CONFIG_LTO_GCC +LTO_CFLAGS := -flto -flto=jobserver -fno-fat-lto-objects \ + -fuse-linker-plugin -fwhole-program +KBUILD_CFLAGS += $(LTO_CFLAGS) +LTO_LDFLAGS := $(LTO_CFLAGS) -Wno-lto-type-mismatch -Wno-psabi \ + -Wno-stringop-overflow -flinker-output=nolto-rel +LDFINAL := $(CONFIG_SHELL) $(srctree)/scripts/gcc-ld $(LTO_LDFLAGS) +AR := $(CROSS_COMPILE)gcc-ar +NM := $(CROSS_COMPILE)gcc-nm +DISABLE_LTO := -fno-lto +export DISABLE_LTO LDFINAL +else +LDFINAL := $(LD) +export LDFINAL +endif + # The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default # values of the respective KBUILD_* variables ARCH_CPPFLAGS := diff --git a/init/Kconfig b/init/Kconfig index f38d241dc9d5..02487c86acd2 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1209,6 +1209,9 @@ config CC_OPTIMIZE_FOR_SIZE endchoice +config LTO_GCC + bool "Enable Link Time Optimization for GCC" + config SYSCTL bool diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 7058964f99f3..e4e1dc0b14ae 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -250,7 +250,7 @@ $(obj)/%: $(src)/%_shipped # --------------------------------------------------------------------------- quiet_cmd_ld = LD $@ -cmd_ld = $(LD) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \ +cmd_ld = $(LDFINAL) $(LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) \ $(filter-out FORCE,$^) -o $@ # Objcopy diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index c98e0393978c..83afdb0d697a 100644 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -125,7 +125,7 @@ modpost_link() info LD vmlinux.o fi - ${LD} ${LDFLAGS} -r -o ${1} $(lto_lds) ${objects} + ${LDFINAL} ${LDFLAGS} -r -o ${1} $(lto_lds) ${objects} } # If CONFIG_LTO_CLANG is selected, we postpone running recordmcount until @@ -150,7 +150,7 @@ vmlinux_link() local objects if [ "${SRCARCH}" != "um" ]; then - local ld=${LD} + local ld=${LDFINAL} local ldflags="${LDFLAGS} ${LDFLAGS_vmlinux}" if [ -n "${LDFINAL_vmlinux}" ]; then