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# 80d60ffeeSArd Biesheuvel 90d60ffeeSArd Biesheuvel# non-x86 reuses KBUILD_CFLAGS, x86 does not 100d60ffeeSArd Biesheuvelcflags-y := $(KBUILD_CFLAGS) 110d60ffeeSArd Biesheuvel 12f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_32) := -march=i386 13769e0fe1SArd Biesheuvelcflags-$(CONFIG_X86_64) := -mcmodel=small 14bbf8e8b0SArvind Sankarcflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ 15f4f75ad5SArd Biesheuvel -fPIC -fno-strict-aliasing -mno-red-zone \ 163db5e0baSNathan Chancellor -mno-mmx -mno-sse -fshort-wchar \ 173db5e0baSNathan Chancellor -Wno-pointer-sign \ 183db5e0baSNathan Chancellor $(call cc-disable-warning, address-of-packed-member) \ 19003602adSArvind Sankar $(call cc-disable-warning, gnu) \ 2058d746c1SNathan Chancellor -fno-asynchronous-unwind-tables \ 2158d746c1SNathan Chancellor $(CLANG_FLAGS) 22f4f75ad5SArd Biesheuvel 23ce279d37SLaura Abbott# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly 24ce279d37SLaura Abbott# disable the stackleak plugin 250d60ffeeSArd Biesheuvelcflags-$(CONFIG_ARM64) += -fpie $(DISABLE_STACKLEAK_PLUGIN) \ 26e2179a09SKees Cook $(call cc-option,-mbranch-protection=none) 272e6fa86fSArd Biesheuvelcflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \ 282e6fa86fSArd Biesheuvel -fno-builtin -fpic \ 2941f1c484SAlistair Strachan $(call cc-option,-mno-single-pic-base) 300d60ffeeSArd Biesheuvelcflags-$(CONFIG_RISCV) += -fpic 310d60ffeeSArd Biesheuvelcflags-$(CONFIG_LOONGARCH) += -fpie 32f4f75ad5SArd Biesheuvel 3340cd01a9SArd Biesheuvelcflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt 34e8f3010fSArd Biesheuvel 350d60ffeeSArd BiesheuvelKBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \ 360d60ffeeSArd Biesheuvel -Os -DDISABLE_BRANCH_PROFILING \ 37e544ea57SArd Biesheuvel -include $(srctree)/include/linux/hidden.h \ 383e2c044aSKees Cook -D__NO_FORTIFY \ 39685969e0SMasahiro Yamada -ffreestanding \ 40893ab004SMasahiro Yamada -fno-stack-protector \ 41f77767edSArd Biesheuvel $(call cc-option,-fno-addrsig) \ 42f922c4abSArd Biesheuvel -D__DISABLE_EXPORTS 43f4f75ad5SArd Biesheuvel 441a388792SArd Biesheuvel# 451a388792SArd Biesheuvel# struct randomization only makes sense for Linux internal types, which the EFI 461a388792SArd Biesheuvel# stub code never touches, so let's turn off struct randomization for the stub 471a388792SArd Biesheuvel# altogether 481a388792SArd Biesheuvel# 491a388792SArd BiesheuvelKBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS)) 501a388792SArd Biesheuvel 51cc49c71dSSami Tolvanen# remove SCS flags from all objects in this directory 52cc49c71dSSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) 53f143ff39SSami Tolvanen# disable CFI 54f143ff39SSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) 556e20f185SSami Tolvanen# disable LTO 566e20f185SSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) 57cc49c71dSSami Tolvanen 58f4f75ad5SArd BiesheuvelGCOV_PROFILE := n 590ebba714SMarco Elver# Sanitizer runtimes are unavailable and cannot be linked here. 600b24beccSAndrey RyabininKASAN_SANITIZE := n 610ebba714SMarco ElverKCSAN_SANITIZE := n 6279dbd006SAlexander PotapenkoKMSAN_SANITIZE := n 63c6d30853SAndrey RyabininUBSAN_SANITIZE := n 64c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD := y 65f4f75ad5SArd Biesheuvel 665c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. 675c9a8750SDmitry VyukovKCOV_INSTRUMENT := n 685c9a8750SDmitry Vyukov 690d959814SDominik Brodowskilib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ 7091d150c0SArd Biesheuvel file.o mem.o random.o randomalloc.o pci.o \ 7143b1df0eSArd Biesheuvel skip_spaces.o lib-cmdline.o lib-ctype.o \ 722e6fa86fSArd Biesheuvel alignedmem.o relocate.o printk.o vsprintf.o 73e8f3010fSArd Biesheuvel 7440cd01a9SArd Biesheuvel# include the stub's libfdt dependencies from lib/ when needed 7540cd01a9SArd Biesheuvellibfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \ 7640cd01a9SArd Biesheuvel fdt_empty_tree.c fdt_sw.c 7740cd01a9SArd Biesheuvel 7840cd01a9SArd Biesheuvellib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \ 7940cd01a9SArd Biesheuvel $(patsubst %.c,lib-%.o,$(libfdt-deps)) 80e8f3010fSArd Biesheuvel 81e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE 82e8f3010fSArd Biesheuvel $(call if_changed_rule,cc_o_c) 83e8f3010fSArd Biesheuvel 84*732ea9dbSArd Biesheuvellib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ 85*732ea9dbSArd Biesheuvel screen_info.o 86f4f75ad5SArd Biesheuvel 8781a0bc39SRoy Franzlib-$(CONFIG_ARM) += arm32-stub.o 884ef80609SArd Biesheuvellib-$(CONFIG_ARM64) += arm64-stub.o arm64-entry.o 89c2d0b470SArd Biesheuvellib-$(CONFIG_X86) += x86-stub.o 90d7071743SAtish Patralib-$(CONFIG_RISCV) += riscv-stub.o 91ead384d9SHuacai Chenlib-$(CONFIG_LOONGARCH) += loongarch-stub.o 92ead384d9SHuacai Chen 9341cd96faSArd BiesheuvelCFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 94bf457786SArd Biesheuvel 95a0509109SArd Biesheuvelzboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o 96a0509109SArd Biesheuvellib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) 97a0509109SArd Biesheuvel 98a0509109SArd Biesheuvelextra-y := $(lib-y) 99a0509109SArd Biesheuvellib-y := $(patsubst %.o,%.stub.o,$(lib-y)) 100a0509109SArd Biesheuvel 101e2179a09SKees Cook# Even when -mbranch-protection=none is set, Clang will generate a 102e2179a09SKees Cook# .note.gnu.property for code-less object files (like lib/ctype.c), 103e2179a09SKees Cook# so work around this by explicitly removing the unwanted section. 104e2179a09SKees Cook# https://bugs.llvm.org/show_bug.cgi?id=46480 105e2179a09SKees CookSTUBCOPY_FLAGS-y += --remove-section=.note.gnu.property 106e2179a09SKees Cook 107ddeeefe2SArd Biesheuvel# 10826a92425SArvind Sankar# For x86, bootloaders like systemd-boot or grub-efi do not zero-initialize the 10926a92425SArvind Sankar# .bss section, so the .bss section of the EFI stub needs to be included in the 11026a92425SArvind Sankar# .data section of the compressed kernel to ensure initialization. Rename the 11126a92425SArvind Sankar# .bss section here so it's easy to pick out in the linker script. 11226a92425SArvind Sankar# 11326a92425SArvind SankarSTUBCOPY_FLAGS-$(CONFIG_X86) += --rename-section .bss=.bss.efistub,load,alloc 11426a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32 11526a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64 11626a92425SArvind Sankar 11726a92425SArvind Sankar# 11826a92425SArvind Sankar# ARM discards the .data section because it disallows r/w data in the 11926a92425SArvind Sankar# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub, 12026a92425SArvind Sankar# which are preserved explicitly by the decompressor linker script. 12126a92425SArvind Sankar# 12226a92425SArvind SankarSTUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub \ 12326a92425SArvind Sankar --rename-section .bss=.bss.efistub,load,alloc 12426a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS 12526a92425SArvind Sankar 12626a92425SArvind Sankar# 127ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all 128ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc. 129ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing 130ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string 131ddeeefe2SArd Biesheuvel# literals are covered. 132e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary 133e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does 134e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual 135e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is 136e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do 137e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the 138e8f3010fSArd Biesheuvel# object files. 139ddeeefe2SArd Biesheuvel# 140e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \ 141e8f3010fSArd Biesheuvel --prefix-symbols=__efistub_ 1424ef80609SArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS64 143e8f3010fSArd Biesheuvel 144d7071743SAtish Patra# For RISC-V, we don't need anything special other than arm64. Keep all the 145d7071743SAtish Patra# symbols in .init section and make sure that no absolute symbols references 146d7071743SAtish Patra# doesn't exist. 147d7071743SAtish PatraSTUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \ 148d7071743SAtish Patra --prefix-symbols=__efistub_ 149d7071743SAtish PatraSTUBCOPY_RELOC-$(CONFIG_RISCV) := R_RISCV_HI20 150d7071743SAtish Patra 151ead384d9SHuacai Chen# For LoongArch, keep all the symbols in .init section and make sure that no 152ead384d9SHuacai Chen# absolute symbols references exist. 153ead384d9SHuacai ChenSTUBCOPY_FLAGS-$(CONFIG_LOONGARCH) += --prefix-alloc-sections=.init \ 154ead384d9SHuacai Chen --prefix-symbols=__efistub_ 155ead384d9SHuacai ChenSTUBCOPY_RELOC-$(CONFIG_LOONGARCH) := R_LARCH_MARK_LA 156ead384d9SHuacai Chen 157e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE 158e8f3010fSArd Biesheuvel $(call if_changed,stubcopy) 159e8f3010fSArd Biesheuvel 160696204faSArd Biesheuvel# 161696204faSArd Biesheuvel# Strip debug sections and some other sections that may legally contain 162696204faSArd Biesheuvel# absolute relocations, so that we can inspect the remaining sections for 163696204faSArd Biesheuvel# such relocations. If none are found, regenerate the output object, but 164696204faSArd Biesheuvel# this time, use objcopy and leave all sections in place. 165696204faSArd Biesheuvel# 166e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@ 167e8d368adSMasahiro Yamada cmd_stubcopy = \ 16802562d0cSArd Biesheuvel $(STRIP) --strip-debug -o $@ $<; \ 169e8d368adSMasahiro Yamada if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then \ 170e8d368adSMasahiro Yamada echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \ 171e8d368adSMasahiro Yamada /bin/false; \ 172e8d368adSMasahiro Yamada fi; \ 173e8d368adSMasahiro Yamada $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@ 174