1# SPDX-License-Identifier: GPL-2.0 2# 3# The stub may be linked into the kernel proper or into a separate boot binary, 4# but in either case, it executes before the kernel does (with MMU disabled) so 5# things like ftrace and stack-protector are likely to cause trouble if left 6# enabled, even if doing so doesn't break the build. 7# 8 9cflags-y := $(KBUILD_CFLAGS) 10 11# x86 links the stub into the decompressor rather than the kernel proper, so 12# drop the kernel's code model, calling convention, and stack alignment, and 13# the mitigations that rely on thunks and patch sites only the kernel has. 14cflags-$(CONFIG_X86) := $(filter-out -mcmodel=kernel \ 15 -mregparm=3 -freg-struct-return \ 16 -mpreferred-stack-boundary=% \ 17 -mstack-alignment=% \ 18 $(RETPOLINE_CFLAGS) $(RETHUNK_CFLAGS) \ 19 $(PADDING_CFLAGS), $(cflags-y)) 20cflags-$(CONFIG_X86_32) += -march=i386 21cflags-$(CONFIG_X86_64) += -mcmodel=small 22cflags-$(CONFIG_X86) += -fPIC 23# Nothing enables IBT while the stub runs, so ENDBR would only take space. 24cflags-$(CONFIG_X86) += $(call cc-option,-fcf-protection=none) 25 26cflags-$(CONFIG_ARM64) += -fpie -ffixed-x18 -fno-unwind-tables \ 27 -fno-asynchronous-unwind-tables 28cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \ 29 -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \ 30 -DEFI_HAVE_STRCMP -fno-builtin -fpic \ 31 $(call cc-option,-mno-single-pic-base) 32cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax 33cflags-$(CONFIG_LOONGARCH) += -fpie 34 35cflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt 36 37KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \ 38 -Os -DDISABLE_BRANCH_PROFILING \ 39 -include $(srctree)/include/linux/hidden.h \ 40 -D__NO_FORTIFY \ 41 -ffreestanding \ 42 -fno-stack-protector \ 43 $(DISABLE_KSTACK_ERASE) \ 44 $(CONFIG_CC_OPT_NO_ADDRSIG) \ 45 -D__DISABLE_EXPORTS 46 47# 48# struct randomization only makes sense for Linux internal types, which the EFI 49# stub code never touches, so let's turn off struct randomization for the stub 50# altogether 51# 52KBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS)) 53 54# remove SCS flags from all objects in this directory 55KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS)) 56# disable CFI 57KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) 58# disable LTO 59KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS)) 60 61# The .data section would be renamed to .data.efistub, therefore, remove 62# `-fdata-sections` flag from KBUILD_CFLAGS_KERNEL 63KBUILD_CFLAGS_KERNEL := $(filter-out -fdata-sections, $(KBUILD_CFLAGS_KERNEL)) 64 65KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ 66 67lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ 68 file.o mem.o random.o randomalloc.o pci.o \ 69 skip_spaces.o lib-cmdline.o lib-ctype.o \ 70 alignedmem.o printk.o vsprintf.o bli.o \ 71 lib-ucs2_string.o 72 73# include the stub's libfdt dependencies from lib/ when needed 74libfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \ 75 fdt_empty_tree.c fdt_sw.c 76 77lib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \ 78 $(patsubst %.c,lib-%.o,$(libfdt-deps)) 79 80$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE 81 $(call if_changed_rule,cc_o_c) 82 83lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ 84 primary_display.o efi-stub-entry.o 85 86lib-$(CONFIG_ARM) += arm32-stub.o 87lib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o 88lib-$(CONFIG_X86) += x86-stub.o smbios.o 89lib-$(CONFIG_X86_64) += x86-5lvl.o 90lib-$(CONFIG_RISCV) += kaslr.o riscv.o riscv-stub.o 91lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o 92 93CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) 94 95zboot-obj-y := zboot-decompress-gzip.o 96CFLAGS_zboot-decompress-gzip.o += -I$(srctree)/lib/zlib_inflate 97zboot-obj-$(CONFIG_KERNEL_ZSTD) := zboot-decompress-zstd.o lib-xxhash.o 98CFLAGS_zboot-decompress-zstd.o += -I$(srctree)/lib/zstd 99 100zboot-riscv-obj-$(CONFIG_32BIT) := lib-ashldi3.o lib-lshrdi3.o 101zboot-obj-$(CONFIG_RISCV) += lib-clz_ctz.o $(zboot-riscv-obj-y) 102zboot-loongarch-obj-$(CONFIG_32BIT) := lib-ashldi3.o lib-lshrdi3.o 103zboot-obj-$(CONFIG_LOONGARCH) += lib-clz_ctz.o $(zboot-loongarch-obj-y) 104lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) 105 106lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o 107 108targets := $(lib-y) 109lib-y := $(patsubst %.o,%.stub.o,$(lib-y)) 110 111# Even when -mbranch-protection=none is set, Clang will generate a 112# .note.gnu.property for code-less object files (like lib/ctype.c), 113# so work around this by explicitly removing the unwanted section. 114# https://llvm.org/pr46480 115STUBCOPY_FLAGS-y += --remove-section=.note.gnu.property 116 117STUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32 118STUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64 119 120# 121# ARM discards the .data section because it disallows r/w data in the 122# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub, 123# which are preserved explicitly by the decompressor linker script. 124# 125STUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub \ 126 --rename-section .bss=.bss.efistub,load,alloc 127STUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS 128 129# 130# arm64 puts the stub in the kernel proper, which will unnecessarily retain all 131# code indefinitely unless it is annotated as __init/__initdata/__initconst etc. 132# So let's apply the __init annotations at the section level, by prefixing 133# the section names directly. This will ensure that even all the inline string 134# literals are covered. 135# The fact that the stub and the kernel proper are essentially the same binary 136# also means that we need to be extra careful to make sure that the stub does 137# not rely on any absolute symbol references, considering that the virtual 138# kernel mapping that the linker uses is not active yet when the stub is 139# executing. So build all C dependencies of the EFI stub into libstub, and do 140# a verification pass to see if any absolute relocations exist in any of the 141# object files. 142# 143STUBCOPY_FLAGS-$(CONFIG_ARM64) += --prefix-alloc-sections=.init \ 144 --prefix-symbols=__efistub_ 145STUBCOPY_RELOC-$(CONFIG_ARM64) := R_AARCH64_ABS 146 147# For RISC-V, we don't need anything special other than arm64. Keep all the 148# symbols in .init section and make sure that no absolute symbols references 149# exist. 150STUBCOPY_FLAGS-$(CONFIG_RISCV) += --prefix-alloc-sections=.init \ 151 --prefix-symbols=__efistub_ 152STUBCOPY_RELOC-$(CONFIG_RISCV) := -E R_RISCV_HI20\|R_RISCV_$(BITS)\|R_RISCV_RELAX 153 154# For LoongArch, keep all the symbols in .init section and make sure that no 155# absolute symbols references exist. 156STUBCOPY_FLAGS-$(CONFIG_LOONGARCH) += --prefix-alloc-sections=.init \ 157 --prefix-symbols=__efistub_ 158STUBCOPY_RELOC-$(CONFIG_LOONGARCH) := R_LARCH_MARK_LA 159 160$(obj)/%.stub.o: $(obj)/%.o FORCE 161 $(call if_changed,stubcopy) 162 163# 164# Strip debug sections and some other sections that may legally contain 165# absolute relocations, so that we can inspect the remaining sections for 166# such relocations. If none are found, regenerate the output object, but 167# this time, use objcopy and leave all sections in place. 168# 169quiet_cmd_stubcopy = STUBCPY $@ 170 cmd_stubcopy = \ 171 $(STRIP) --strip-debug -o $@ $<; \ 172 if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then \ 173 echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \ 174 /bin/false; \ 175 fi; \ 176 $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@ 177