1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 2f4f75ad5SArd Biesheuvel# 3f4f75ad5SArd Biesheuvel# The stub may be linked into the kernel proper or into a separate boot binary, 4f4f75ad5SArd Biesheuvel# but in either case, it executes before the kernel does (with MMU disabled) so 5f4f75ad5SArd Biesheuvel# things like ftrace and stack-protector are likely to cause trouble if left 6f4f75ad5SArd Biesheuvel# enabled, even if doing so doesn't break the build. 7f4f75ad5SArd Biesheuvel# 8f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_32) := -march=i386 9f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_64) := -mcmodel=small 10846221cfSPaul Bollecflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ -O2 \ 11f4f75ad5SArd Biesheuvel -fPIC -fno-strict-aliasing -mno-red-zone \ 1236b64976SArd Biesheuvel -mno-mmx -mno-sse -fshort-wchar 13f4f75ad5SArd Biesheuvel 14ce279d37SLaura Abbott# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly 15ce279d37SLaura Abbott# disable the stackleak plugin 16ce279d37SLaura Abbottcflags-$(CONFIG_ARM64) := $(subst -pg,,$(KBUILD_CFLAGS)) -fpie \ 17ce279d37SLaura Abbott $(DISABLE_STACKLEAK_PLUGIN) 18696204faSArd Biesheuvelcflags-$(CONFIG_ARM) := $(subst -pg,,$(KBUILD_CFLAGS)) \ 19*41f1c484SAlistair Strachan -fno-builtin -fpic \ 20*41f1c484SAlistair Strachan $(call cc-option,-mno-single-pic-base) 21f4f75ad5SArd Biesheuvel 22e8f3010fSArd Biesheuvelcflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt 23e8f3010fSArd Biesheuvel 24b523e185SArd BiesheuvelKBUILD_CFLAGS := $(cflags-y) -DDISABLE_BRANCH_PROFILING \ 253e2c044aSKees Cook -D__NO_FORTIFY \ 26f4f75ad5SArd Biesheuvel $(call cc-option,-ffreestanding) \ 270b3e3366SLaura Abbott $(call cc-option,-fno-stack-protector) \ 28f922c4abSArd Biesheuvel -D__DISABLE_EXPORTS 29f4f75ad5SArd Biesheuvel 30f4f75ad5SArd BiesheuvelGCOV_PROFILE := n 310b24beccSAndrey RyabininKASAN_SANITIZE := n 32c6d30853SAndrey RyabininUBSAN_SANITIZE := n 33c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD := y 34f4f75ad5SArd Biesheuvel 355c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. 365c9a8750SDmitry VyukovKCOV_INSTRUMENT := n 375c9a8750SDmitry Vyukov 3833b6d034SThiebaud Weksteenlib-y := efi-stub-helper.o gop.o secureboot.o tpm.o 39e8f3010fSArd Biesheuvel 40e8f3010fSArd Biesheuvel# include the stub's generic dependencies from lib/ when building for ARM/arm64 4129f9007bSArd Biesheuvelarm-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c 4229f9007bSArd Biesheuvelarm-deps-$(CONFIG_ARM64) += sort.c 43e8f3010fSArd Biesheuvel 44e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE 45e8f3010fSArd Biesheuvel $(call if_changed_rule,cc_o_c) 46e8f3010fSArd Biesheuvel 47a6a14469SArd Biesheuvellib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o string.o random.o \ 4829f9007bSArd Biesheuvel $(patsubst %.c,lib-%.o,$(arm-deps-y)) 49f4f75ad5SArd Biesheuvel 5081a0bc39SRoy Franzlib-$(CONFIG_ARM) += arm32-stub.o 51a6a14469SArd Biesheuvellib-$(CONFIG_ARM64) += arm64-stub.o 52bf457786SArd BiesheuvelCFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 53bf457786SArd Biesheuvel 54ddeeefe2SArd Biesheuvel# 55ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all 56ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc. 57ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing 58ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string 59ddeeefe2SArd Biesheuvel# literals are covered. 60e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary 61e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does 62e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual 63e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is 64e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do 65e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the 66e8f3010fSArd Biesheuvel# object files. 67ddeeefe2SArd Biesheuvel# 68e8f3010fSArd Biesheuvelextra-$(CONFIG_EFI_ARMSTUB) := $(lib-y) 69e8f3010fSArd Biesheuvellib-$(CONFIG_EFI_ARMSTUB) := $(patsubst %.o,%.stub.o,$(lib-y)) 70ddeeefe2SArd Biesheuvel 71696204faSArd BiesheuvelSTUBCOPY_RM-y := -R *ksymtab* -R *kcrctab* 72e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \ 73e8f3010fSArd Biesheuvel --prefix-symbols=__efistub_ 74e8f3010fSArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS 75e8f3010fSArd Biesheuvel 76e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE 77e8f3010fSArd Biesheuvel $(call if_changed,stubcopy) 78e8f3010fSArd Biesheuvel 79696204faSArd Biesheuvel# 80696204faSArd Biesheuvel# Strip debug sections and some other sections that may legally contain 81696204faSArd Biesheuvel# absolute relocations, so that we can inspect the remaining sections for 82696204faSArd Biesheuvel# such relocations. If none are found, regenerate the output object, but 83696204faSArd Biesheuvel# this time, use objcopy and leave all sections in place. 84696204faSArd Biesheuvel# 85e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@ 86696204faSArd Biesheuvel cmd_stubcopy = if $(STRIP) --strip-debug $(STUBCOPY_RM-y) -o $@ $<; \ 87696204faSArd Biesheuvel then if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); \ 88696204faSArd Biesheuvel then (echo >&2 "$@: absolute symbol references not allowed in the EFI stub"; \ 89696204faSArd Biesheuvel rm -f $@; /bin/false); \ 90696204faSArd Biesheuvel else $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@; fi \ 91696204faSArd Biesheuvel else /bin/false; fi 9281a0bc39SRoy Franz 9381a0bc39SRoy Franz# 9481a0bc39SRoy Franz# ARM discards the .data section because it disallows r/w data in the 9581a0bc39SRoy Franz# decompressor. So move our .data to .data.efistub, which is preserved 9681a0bc39SRoy Franz# explicitly by the decompressor linker script. 9781a0bc39SRoy Franz# 98696204faSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub 9981a0bc39SRoy FranzSTUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS 100