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) \ 268358098bSArd Biesheuvel -fno-unwind-tables -fno-asynchronous-unwind-tables 272e6fa86fSArd Biesheuvelcflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \ 28da8dd0c7SArd Biesheuvel -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \ 29da8dd0c7SArd Biesheuvel -DEFI_HAVE_STRCMP -fno-builtin -fpic \ 3041f1c484SAlistair Strachan $(call cc-option,-mno-single-pic-base) 31*b5db73fbSJisheng Zhangcflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax \ 32*b5db73fbSJisheng Zhang $(DISABLE_STACKLEAK_PLUGIN) 330d60ffeeSArd Biesheuvelcflags-$(CONFIG_LOONGARCH) += -fpie 34f4f75ad5SArd Biesheuvel 3540cd01a9SArd Biesheuvelcflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt 36e8f3010fSArd Biesheuvel 370d60ffeeSArd BiesheuvelKBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \ 380d60ffeeSArd Biesheuvel -Os -DDISABLE_BRANCH_PROFILING \ 39e544ea57SArd Biesheuvel -include $(srctree)/include/linux/hidden.h \ 403e2c044aSKees Cook -D__NO_FORTIFY \ 41685969e0SMasahiro Yamada -ffreestanding \ 42893ab004SMasahiro Yamada -fno-stack-protector \ 43f77767edSArd Biesheuvel $(call cc-option,-fno-addrsig) \ 44f922c4abSArd Biesheuvel -D__DISABLE_EXPORTS 45f4f75ad5SArd Biesheuvel 461a388792SArd Biesheuvel# 471a388792SArd Biesheuvel# struct randomization only makes sense for Linux internal types, which the EFI 481a388792SArd Biesheuvel# stub code never touches, so let's turn off struct randomization for the stub 491a388792SArd Biesheuvel# altogether 501a388792SArd Biesheuvel# 511a388792SArd BiesheuvelKBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS)) 521a388792SArd Biesheuvel 53cc49c71dSSami Tolvanen# remove SCS flags from all objects in this directory 54cc49c71dSSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) 55f143ff39SSami Tolvanen# disable CFI 56f143ff39SSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) 576e20f185SSami Tolvanen# disable LTO 586e20f185SSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) 59cc49c71dSSami Tolvanen 600d959814SDominik Brodowskilib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ 6191d150c0SArd Biesheuvel file.o mem.o random.o randomalloc.o pci.o \ 6243b1df0eSArd Biesheuvel skip_spaces.o lib-cmdline.o lib-ctype.o \ 632e6fa86fSArd Biesheuvel alignedmem.o relocate.o printk.o vsprintf.o 64e8f3010fSArd Biesheuvel 6540cd01a9SArd Biesheuvel# include the stub's libfdt dependencies from lib/ when needed 6640cd01a9SArd Biesheuvellibfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \ 6740cd01a9SArd Biesheuvel fdt_empty_tree.c fdt_sw.c 6840cd01a9SArd Biesheuvel 6940cd01a9SArd Biesheuvellib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \ 7040cd01a9SArd Biesheuvel $(patsubst %.c,lib-%.o,$(libfdt-deps)) 71e8f3010fSArd Biesheuvel 72e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE 73e8f3010fSArd Biesheuvel $(call if_changed_rule,cc_o_c) 74e8f3010fSArd Biesheuvel 75732ea9dbSArd Biesheuvellib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ 7642c8ea3dSArd Biesheuvel screen_info.o efi-stub-entry.o 77f4f75ad5SArd Biesheuvel 7881a0bc39SRoy Franzlib-$(CONFIG_ARM) += arm32-stub.o 796b56beb5SAlexandre Ghitilib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o 80cd619387SArd Biesheuvellib-$(CONFIG_X86) += x86-stub.o smbios.o 81cb1c9e02SArd Biesheuvellib-$(CONFIG_X86_64) += x86-5lvl.o 82b7ac4b8eSAlexandre Ghitilib-$(CONFIG_RISCV) += kaslr.o riscv.o riscv-stub.o 83d729b554SArd Biesheuvellib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o 84ead384d9SHuacai Chen 8541cd96faSArd BiesheuvelCFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 86bf457786SArd Biesheuvel 87a0509109SArd Biesheuvelzboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o 88a0509109SArd Biesheuvellib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) 89a0509109SArd Biesheuvel 90745e3ed8SKirill A. Shutemovlib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o 91745e3ed8SKirill A. Shutemov 92a0509109SArd Biesheuvelextra-y := $(lib-y) 93a0509109SArd Biesheuvellib-y := $(patsubst %.o,%.stub.o,$(lib-y)) 94a0509109SArd Biesheuvel 95e2179a09SKees Cook# Even when -mbranch-protection=none is set, Clang will generate a 96e2179a09SKees Cook# .note.gnu.property for code-less object files (like lib/ctype.c), 97e2179a09SKees Cook# so work around this by explicitly removing the unwanted section. 982947a456SNathan Chancellor# https://llvm.org/pr46480 99e2179a09SKees CookSTUBCOPY_FLAGS-y += --remove-section=.note.gnu.property 100e2179a09SKees Cook 10126a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32 10226a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64 10326a92425SArvind Sankar 10426a92425SArvind Sankar# 10526a92425SArvind Sankar# ARM discards the .data section because it disallows r/w data in the 10626a92425SArvind Sankar# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub, 10726a92425SArvind Sankar# which are preserved explicitly by the decompressor linker script. 10826a92425SArvind Sankar# 10926a92425SArvind SankarSTUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub \ 11026a92425SArvind Sankar --rename-section .bss=.bss.efistub,load,alloc 11126a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS 11226a92425SArvind Sankar 11326a92425SArvind Sankar# 114ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all 115ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc. 116ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing 117ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string 118ddeeefe2SArd Biesheuvel# literals are covered. 119e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary 120e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does 121e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual 122e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is 123e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do 124e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the 125e8f3010fSArd Biesheuvel# object files. 126ddeeefe2SArd Biesheuvel# 127e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \ 128e8f3010fSArd Biesheuvel --prefix-symbols=__efistub_ 12961786170SArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS 130e8f3010fSArd Biesheuvel 131d7071743SAtish Patra# For RISC-V, we don't need anything special other than arm64. Keep all the 132d7071743SAtish Patra# symbols in .init section and make sure that no absolute symbols references 133f6e6e95cSXiao Wang# exist. 134d7071743SAtish PatraSTUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \ 135d7071743SAtish Patra --prefix-symbols=__efistub_ 136d2baf8ccSArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_RISCV) := -E R_RISCV_HI20\|R_RISCV_$(BITS)\|R_RISCV_RELAX 137d7071743SAtish Patra 138ead384d9SHuacai Chen# For LoongArch, keep all the symbols in .init section and make sure that no 139ead384d9SHuacai Chen# absolute symbols references exist. 140ead384d9SHuacai ChenSTUBCOPY_FLAGS-$(CONFIG_LOONGARCH) += --prefix-alloc-sections=.init \ 141ead384d9SHuacai Chen --prefix-symbols=__efistub_ 142ead384d9SHuacai ChenSTUBCOPY_RELOC-$(CONFIG_LOONGARCH) := R_LARCH_MARK_LA 143ead384d9SHuacai Chen 144e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE 145e8f3010fSArd Biesheuvel $(call if_changed,stubcopy) 146e8f3010fSArd Biesheuvel 147696204faSArd Biesheuvel# 148696204faSArd Biesheuvel# Strip debug sections and some other sections that may legally contain 149696204faSArd Biesheuvel# absolute relocations, so that we can inspect the remaining sections for 150696204faSArd Biesheuvel# such relocations. If none are found, regenerate the output object, but 151696204faSArd Biesheuvel# this time, use objcopy and leave all sections in place. 152696204faSArd Biesheuvel# 153e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@ 154e8d368adSMasahiro Yamada cmd_stubcopy = \ 15502562d0cSArd Biesheuvel $(STRIP) --strip-debug -o $@ $<; \ 156e8d368adSMasahiro Yamada if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then \ 157e8d368adSMasahiro Yamada echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \ 158e8d368adSMasahiro Yamada /bin/false; \ 159e8d368adSMasahiro Yamada fi; \ 160e8d368adSMasahiro Yamada $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@ 161