Skip to content

Commit f2e6e3e

Browse files
twd2jmontleon
authored andcommitted
RISC-V: build: Allow LTO to be selected
Allow LTO to be selected for RISC-V, only when LLD >= 14, since there is an issue [1] in prior LLD versions that prevents LLD to generate proper machine code for RISC-V when writing `nop`s. To avoid boot failures in QEMU [2], '-mattr=+c' and '-mattr=+relax' need to be passed via '-mllvm' to ld.lld, as there appears to be an issue with LLVM's target-features and LTO [3], which can result in incorrect relocations to branch targets [4]. Once this is fixed in LLVM, it can be made conditional on affected ld.lld versions. Disable LTO for arch/riscv/kernel/pi, as llvm-objcopy expects an ELF object file when manipulating the files in that subfolder, rather than LLVM bitcode. [1] llvm/llvm-project#50505, resolved by LLVM commit e63455d5e0e5 ("[MC] Use local MCSubtargetInfo in writeNops") [2] ClangBuiltLinux/linux#1942 [3] llvm/llvm-project#59350 [4] llvm/llvm-project#65090 Tested-by: Wende Tan <[email protected]> Change-Id: Ib5a2cfe304cf10594e3f8cfec8d44794fd433882 Signed-off-by: Wende Tan <[email protected]> Co-developed-by: Nathan Chancellor <[email protected]> Signed-off-by: Nathan Chancellor <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 88e204d commit f2e6e3e

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

arch/riscv/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ config RISCV
4747
select ARCH_SUPPORTS_CFI_CLANG
4848
select ARCH_SUPPORTS_DEBUG_PAGEALLOC if MMU
4949
select ARCH_SUPPORTS_HUGETLBFS if MMU
50+
# LLD >= 14: https://github.com/llvm/llvm-project/issues/50505
51+
select ARCH_SUPPORTS_LTO_CLANG if LLD_VERSION >= 140000
52+
select ARCH_SUPPORTS_LTO_CLANG_THIN if LLD_VERSION >= 140000
5053
select ARCH_SUPPORTS_PAGE_TABLE_CHECK if MMU
5154
select ARCH_SUPPORTS_PER_VMA_LOCK if MMU
5255
select ARCH_SUPPORTS_SHADOW_CALL_STACK if HAVE_SHADOW_CALL_STACK

arch/riscv/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ ifndef CONFIG_AS_IS_LLVM
5151
KBUILD_CFLAGS += -Wa,-mno-relax
5252
KBUILD_AFLAGS += -Wa,-mno-relax
5353
endif
54+
# LLVM has an issue with target-features and LTO: https://github.com/llvm/llvm-project/issues/59350
55+
# Ensure it is aware of linker relaxation with LTO, otherwise relocations may
56+
# be incorrect: https://github.com/llvm/llvm-project/issues/65090
57+
else ifeq ($(CONFIG_LTO_CLANG),y)
58+
KBUILD_LDFLAGS += -mllvm -mattr=+c -mllvm -mattr=+relax
5459
endif
5560
endif
5661

arch/riscv/kernel/pi/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
99
-fno-asynchronous-unwind-tables -fno-unwind-tables \
1010
$(call cc-option,-fno-addrsig)
1111

12+
# Disable LTO
13+
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
14+
1215
KBUILD_CFLAGS += -mcmodel=medany
1316

1417
CFLAGS_cmdline_early.o += -D__NO_FORTIFY

0 commit comments

Comments
 (0)