18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci# 38c2ecf20Sopenharmony_ci# The stub may be linked into the kernel proper or into a separate boot binary, 48c2ecf20Sopenharmony_ci# but in either case, it executes before the kernel does (with MMU disabled) so 58c2ecf20Sopenharmony_ci# things like ftrace and stack-protector are likely to cause trouble if left 68c2ecf20Sopenharmony_ci# enabled, even if doing so doesn't break the build. 78c2ecf20Sopenharmony_ci# 88c2ecf20Sopenharmony_cicflags-$(CONFIG_X86_32) := -march=i386 98c2ecf20Sopenharmony_cicflags-$(CONFIG_X86_64) := -mcmodel=small 108c2ecf20Sopenharmony_cicflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ 118c2ecf20Sopenharmony_ci -fPIC -fno-strict-aliasing -mno-red-zone \ 128c2ecf20Sopenharmony_ci -mno-mmx -mno-sse -fshort-wchar \ 138c2ecf20Sopenharmony_ci -Wno-pointer-sign \ 148c2ecf20Sopenharmony_ci $(call cc-disable-warning, address-of-packed-member) \ 158c2ecf20Sopenharmony_ci $(call cc-disable-warning, gnu) \ 168c2ecf20Sopenharmony_ci -fno-asynchronous-unwind-tables \ 178c2ecf20Sopenharmony_ci $(CLANG_FLAGS) 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly 208c2ecf20Sopenharmony_ci# disable the stackleak plugin 218c2ecf20Sopenharmony_cicflags-$(CONFIG_ARM64) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ 228c2ecf20Sopenharmony_ci -fpie $(DISABLE_STACKLEAK_PLUGIN) \ 238c2ecf20Sopenharmony_ci $(call cc-option,-mbranch-protection=none) 248c2ecf20Sopenharmony_cicflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ 258c2ecf20Sopenharmony_ci -fno-builtin -fpic \ 268c2ecf20Sopenharmony_ci $(call cc-option,-mno-single-pic-base) 278c2ecf20Sopenharmony_cicflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ 288c2ecf20Sopenharmony_ci -fpic 298c2ecf20Sopenharmony_cicflags-$(CONFIG_LOONGARCH) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ 308c2ecf20Sopenharmony_ci -fpie 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cicflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciKBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ 358c2ecf20Sopenharmony_ci -include $(srctree)/include/linux/hidden.h \ 368c2ecf20Sopenharmony_ci -D__NO_FORTIFY \ 378c2ecf20Sopenharmony_ci -ffreestanding \ 388c2ecf20Sopenharmony_ci -fno-stack-protector \ 398c2ecf20Sopenharmony_ci $(call cc-option,-fno-addrsig) \ 408c2ecf20Sopenharmony_ci -D__DISABLE_EXPORTS 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci# 438c2ecf20Sopenharmony_ci# struct randomization only makes sense for Linux internal types, which the EFI 448c2ecf20Sopenharmony_ci# stub code never touches, so let's turn off struct randomization for the stub 458c2ecf20Sopenharmony_ci# altogether 468c2ecf20Sopenharmony_ci# 478c2ecf20Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS)) 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci# remove SCS flags from all objects in this directory 508c2ecf20Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) 518c2ecf20Sopenharmony_ci# disable LTO 528c2ecf20Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ciGCOV_PROFILE := n 558c2ecf20Sopenharmony_ci# Sanitizer runtimes are unavailable and cannot be linked here. 568c2ecf20Sopenharmony_ciKASAN_SANITIZE := n 578c2ecf20Sopenharmony_ciKCSAN_SANITIZE := n 588c2ecf20Sopenharmony_ciUBSAN_SANITIZE := n 598c2ecf20Sopenharmony_ciOBJECT_FILES_NON_STANDARD := y 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in. 628c2ecf20Sopenharmony_ciKCOV_INSTRUMENT := n 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cilib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ 658c2ecf20Sopenharmony_ci file.o mem.o random.o randomalloc.o pci.o \ 668c2ecf20Sopenharmony_ci skip_spaces.o lib-cmdline.o lib-ctype.o \ 678c2ecf20Sopenharmony_ci alignedmem.o relocate.o vsprintf.o 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci# include the stub's libfdt dependencies from lib/ when needed 708c2ecf20Sopenharmony_cilibfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \ 718c2ecf20Sopenharmony_ci fdt_empty_tree.c fdt_sw.c 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cilib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \ 748c2ecf20Sopenharmony_ci $(patsubst %.c,lib-%.o,$(libfdt-deps)) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE 778c2ecf20Sopenharmony_ci $(call if_changed_rule,cc_o_c) 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cilib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cilib-$(CONFIG_ARM) += arm32-stub.o 828c2ecf20Sopenharmony_cilib-$(CONFIG_ARM64) += arm64-stub.o 838c2ecf20Sopenharmony_cilib-$(CONFIG_X86) += x86-stub.o 848c2ecf20Sopenharmony_cilib-$(CONFIG_RISCV) += riscv-stub.o 858c2ecf20Sopenharmony_cilib-$(CONFIG_LOONGARCH) += loongarch-stub.o 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciCFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci# Even when -mbranch-protection=none is set, Clang will generate a 908c2ecf20Sopenharmony_ci# .note.gnu.property for code-less object files (like lib/ctype.c), 918c2ecf20Sopenharmony_ci# so work around this by explicitly removing the unwanted section. 928c2ecf20Sopenharmony_ci# https://bugs.llvm.org/show_bug.cgi?id=46480 938c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-y += --remove-section=.note.gnu.property 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci# 968c2ecf20Sopenharmony_ci# For x86, bootloaders like systemd-boot or grub-efi do not zero-initialize the 978c2ecf20Sopenharmony_ci# .bss section, so the .bss section of the EFI stub needs to be included in the 988c2ecf20Sopenharmony_ci# .data section of the compressed kernel to ensure initialization. Rename the 998c2ecf20Sopenharmony_ci# .bss section here so it's easy to pick out in the linker script. 1008c2ecf20Sopenharmony_ci# 1018c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_X86) += --rename-section .bss=.bss.efistub,load,alloc 1028c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32 1038c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci# 1068c2ecf20Sopenharmony_ci# ARM discards the .data section because it disallows r/w data in the 1078c2ecf20Sopenharmony_ci# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub, 1088c2ecf20Sopenharmony_ci# which are preserved explicitly by the decompressor linker script. 1098c2ecf20Sopenharmony_ci# 1108c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub \ 1118c2ecf20Sopenharmony_ci --rename-section .bss=.bss.efistub,load,alloc 1128c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci# 1168c2ecf20Sopenharmony_ci# arm64 puts the stub in the kernel proper, which will unnecessarily retain all 1178c2ecf20Sopenharmony_ci# code indefinitely unless it is annotated as __init/__initdata/__initconst etc. 1188c2ecf20Sopenharmony_ci# So let's apply the __init annotations at the section level, by prefixing 1198c2ecf20Sopenharmony_ci# the section names directly. This will ensure that even all the inline string 1208c2ecf20Sopenharmony_ci# literals are covered. 1218c2ecf20Sopenharmony_ci# The fact that the stub and the kernel proper are essentially the same binary 1228c2ecf20Sopenharmony_ci# also means that we need to be extra careful to make sure that the stub does 1238c2ecf20Sopenharmony_ci# not rely on any absolute symbol references, considering that the virtual 1248c2ecf20Sopenharmony_ci# kernel mapping that the linker uses is not active yet when the stub is 1258c2ecf20Sopenharmony_ci# executing. So build all C dependencies of the EFI stub into libstub, and do 1268c2ecf20Sopenharmony_ci# a verification pass to see if any absolute relocations exist in any of the 1278c2ecf20Sopenharmony_ci# object files. 1288c2ecf20Sopenharmony_ci# 1298c2ecf20Sopenharmony_ciextra-y := $(lib-y) 1308c2ecf20Sopenharmony_cilib-y := $(patsubst %.o,%.stub.o,$(lib-y)) 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \ 1338c2ecf20Sopenharmony_ci --prefix-symbols=__efistub_ 1348c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci# For RISC-V, we don't need anything special other than arm64. Keep all the 1378c2ecf20Sopenharmony_ci# symbols in .init section and make sure that no absolute symbols references 1388c2ecf20Sopenharmony_ci# doesn't exist. 1398c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \ 1408c2ecf20Sopenharmony_ci --prefix-symbols=__efistub_ 1418c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_RISCV) := R_RISCV_HI20 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci# For LoongArch, keep all the symbols in .init section and make sure that no 1448c2ecf20Sopenharmony_ci# absolute symbols references doesn't exist. 1458c2ecf20Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_LOONGARCH) += --prefix-alloc-sections=.init 1468c2ecf20Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_LOONGARCH) := R_LARCH_MARK_LA 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci$(obj)/%.stub.o: $(obj)/%.o FORCE 1498c2ecf20Sopenharmony_ci $(call if_changed,stubcopy) 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci# 1528c2ecf20Sopenharmony_ci# Strip debug sections and some other sections that may legally contain 1538c2ecf20Sopenharmony_ci# absolute relocations, so that we can inspect the remaining sections for 1548c2ecf20Sopenharmony_ci# such relocations. If none are found, regenerate the output object, but 1558c2ecf20Sopenharmony_ci# this time, use objcopy and leave all sections in place. 1568c2ecf20Sopenharmony_ci# 1578c2ecf20Sopenharmony_ciquiet_cmd_stubcopy = STUBCPY $@ 1588c2ecf20Sopenharmony_ci cmd_stubcopy = \ 1598c2ecf20Sopenharmony_ci $(STRIP) --strip-debug -o $@ $<; \ 1608c2ecf20Sopenharmony_ci if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then \ 1618c2ecf20Sopenharmony_ci echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \ 1628c2ecf20Sopenharmony_ci /bin/false; \ 1638c2ecf20Sopenharmony_ci fi; \ 1648c2ecf20Sopenharmony_ci $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@ 165