Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
kbuild: Support explicitly selecting LD linker
Browse files Browse the repository at this point in the history
https://lld.llvm.org/

Enable CONFIG_LD_(BFD|GOLD|LLD) to force compilation with desired LD
linker. This enables experimental support for LLVM's LLD linker
which has been shown to result in faster link time and potentially
reduce kernel image size when used in conjunction with Clang LTO.
GNU gold will remain as the preferred linker due to it's universal
compatibility.

Co-authored-by: Danny Lin <danny@kdrag0n.dev>
[@0ctobot: This is an adaptation of the following commits:
kdrag0n/proton_bluecross@0b08017,
kdrag0n/proton_bluecross@aba5259,
kdrag0n/proton_bluecross@70c8925]
Co-authored-by: Adam W. Willis <return.of.octobot@gmail.com>
  • Loading branch information
kdrag0n and 0ctobot committed Jul 8, 2019
1 parent c22c1cc commit 7911ae8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
LDGOLD = $(CROSS_COMPILE)ld.gold
LDLLD = ld.lld
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
Expand Down Expand Up @@ -638,11 +639,19 @@ export CFLAGS_GCOV CFLAGS_KCOV

# Make toolchain changes before including arch/$(SRCARCH)/Makefile to ensure
# ar/cc/ld-* macros return correct values.
ifdef CONFIG_LTO_CLANG
# use GNU gold with LLVMgold for LTO linking, and LD for vmlinux_link
ifdef CONFIG_LD_GOLD
LDFINAL_vmlinux := $(LD)
LD := $(LDGOLD)
endif
ifdef CONFIG_LD_LLD
LD := $(LDLLD)
endif

ifdef CONFIG_LTO_CLANG
# use GNU gold and LD for vmlinux_link, or LLD for LTO linking
ifeq ($(ld-name),gold)
LDFLAGS += -plugin LLVMgold.so
endif
LDFLAGS += -plugin-opt=-function-sections
LDFLAGS += -plugin-opt=-data-sections
# use llvm-ar for building symbol tables from IR files, and llvm-dis instead
Expand Down Expand Up @@ -691,6 +700,10 @@ ifdef CONFIG_LTO
lto-flags := $(lto-clang-flags)
KBUILD_CFLAGS += $(lto-flags)

ifeq ($(ld-name),lld)
LDFLAGS += --lto-O3
endif

DISABLE_LTO := $(DISABLE_LTO_CLANG)
export DISABLE_LTO

Expand Down Expand Up @@ -1186,8 +1199,10 @@ ifdef CONFIG_LTO_CLANG
ifneq ($(call clang-ifversion, -ge, 0500, y), y)
@echo Cannot use CONFIG_LTO_CLANG: requires clang 5.0 or later >&2 && exit 1
endif
ifneq ($(call gold-ifversion, -ge, 112000000, y), y)
@echo Cannot use CONFIG_LTO_CLANG: requires GNU gold 1.12 or later >&2 && exit 1
ifneq ($(ld-name), lld)
ifneq ($(call gold-ifversion, -ge, 112000000, y), y)
@echo Cannot use CONFIG_LTO_CLANG: requires LLD or GNU gold 1.12 or later >&2 && exit 1
endif
endif
endif
# Make sure compiler supports LTO flags
Expand Down
29 changes: 29 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,34 @@ config THIN_ARCHIVES
Select this if the architecture wants to use thin archives
instead of ld -r to create the built-in.o files.

choice
prompt "Kernel linker"
default LD_BFD if !LTO_CLANG
default LD_GOLD if LTO_CLANG
help
Select the desired kernel linker from a supported list.

config LD_BFD
bool "bfd"
depends on !LTO_CLANG
help
This option selects the default ld.bfd linker, which is part of
GNU binutils.

config LD_GOLD
bool "gold"
help
This option selects the ld.gold linker, which is a part of GNU
binutils and required when compiling with Clang LTO.

config LD_LLD
bool "lld"
help
This option selects ld.lld as a drop-in replacement for ld.gold
and may optionally be used when compiling with Clang LTO.

endchoice

config LTO
def_bool n

Expand Down Expand Up @@ -527,6 +555,7 @@ config LTO_CLANG
select LTO
select THIN_ARCHIVES
select LD_DEAD_CODE_DATA_ELIMINATION
select LD_GOLD
help
This option enables clang's Link Time Optimization (LTO), which allows
the compiler to optimize the kernel globally at link time. If you
Expand Down
1 change: 1 addition & 0 deletions init/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,7 @@ config LD_DEAD_CODE_DATA_ELIMINATION
bool "Dead code and data elimination (EXPERIMENTAL)"
depends on HAVE_LD_DEAD_CODE_DATA_ELIMINATION
depends on EXPERT
select LD_GOLD
help
Enable this if you want to do dead code and data elimination with
the linker by compiling with -ffunction-sections -fdata-sections,
Expand Down
4 changes: 2 additions & 2 deletions scripts/Kbuild.include
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))

# ld-name
# Expands to either bfd or gold
ld-name = $(shell $(LD) -v 2>&1 | grep -q "GNU gold" && echo gold || echo bfd)
# Expands to bfd, gold, or lld
ld-name = $(shell if $(LD) -v 2>&1 | grep -q "GNU gold"; then echo gold; elif $(LD) -v 2>&1 | grep -q "LLD"; then echo lld; else echo bfd; fi)

# ld-version
# Note this is mainly for HJ Lu's 3 number binutil versions
Expand Down

0 comments on commit 7911ae8

Please sign in to comment.