162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#
362306a36Sopenharmony_ci# The stub may be linked into the kernel proper or into a separate boot binary,
462306a36Sopenharmony_ci# but in either case, it executes before the kernel does (with MMU disabled) so
562306a36Sopenharmony_ci# things like ftrace and stack-protector are likely to cause trouble if left
662306a36Sopenharmony_ci# enabled, even if doing so doesn't break the build.
762306a36Sopenharmony_ci#
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci# non-x86 reuses KBUILD_CFLAGS, x86 does not
1062306a36Sopenharmony_cicflags-y			:= $(KBUILD_CFLAGS)
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cicflags-$(CONFIG_X86_32)		:= -march=i386
1362306a36Sopenharmony_cicflags-$(CONFIG_X86_64)		:= -mcmodel=small
1462306a36Sopenharmony_cicflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
1562306a36Sopenharmony_ci				   -fPIC -fno-strict-aliasing -mno-red-zone \
1662306a36Sopenharmony_ci				   -mno-mmx -mno-sse -fshort-wchar \
1762306a36Sopenharmony_ci				   -Wno-pointer-sign \
1862306a36Sopenharmony_ci				   $(call cc-disable-warning, address-of-packed-member) \
1962306a36Sopenharmony_ci				   $(call cc-disable-warning, gnu) \
2062306a36Sopenharmony_ci				   -fno-asynchronous-unwind-tables \
2162306a36Sopenharmony_ci				   $(CLANG_FLAGS)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
2462306a36Sopenharmony_ci# disable the stackleak plugin
2562306a36Sopenharmony_cicflags-$(CONFIG_ARM64)		+= -fpie $(DISABLE_STACKLEAK_PLUGIN) \
2662306a36Sopenharmony_ci				   -fno-unwind-tables -fno-asynchronous-unwind-tables
2762306a36Sopenharmony_cicflags-$(CONFIG_ARM)		+= -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
2862306a36Sopenharmony_ci				   -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
2962306a36Sopenharmony_ci				   -DEFI_HAVE_STRCMP -fno-builtin -fpic \
3062306a36Sopenharmony_ci				   $(call cc-option,-mno-single-pic-base)
3162306a36Sopenharmony_cicflags-$(CONFIG_RISCV)		+= -fpic -mno-relax
3262306a36Sopenharmony_cicflags-$(CONFIG_LOONGARCH)	+= -fpie
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cicflags-$(CONFIG_EFI_PARAMS_FROM_FDT)	+= -I$(srctree)/scripts/dtc/libfdt
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciKBUILD_CFLAGS			:= $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
3762306a36Sopenharmony_ci				   -Os -DDISABLE_BRANCH_PROFILING \
3862306a36Sopenharmony_ci				   -include $(srctree)/include/linux/hidden.h \
3962306a36Sopenharmony_ci				   -D__NO_FORTIFY \
4062306a36Sopenharmony_ci				   -ffreestanding \
4162306a36Sopenharmony_ci				   -fno-stack-protector \
4262306a36Sopenharmony_ci				   $(call cc-option,-fno-addrsig) \
4362306a36Sopenharmony_ci				   -D__DISABLE_EXPORTS
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#
4662306a36Sopenharmony_ci# struct randomization only makes sense for Linux internal types, which the EFI
4762306a36Sopenharmony_ci# stub code never touches, so let's turn off struct randomization for the stub
4862306a36Sopenharmony_ci# altogether
4962306a36Sopenharmony_ci#
5062306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS))
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci# remove SCS flags from all objects in this directory
5362306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
5462306a36Sopenharmony_ci# disable CFI
5562306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_CFI), $(KBUILD_CFLAGS))
5662306a36Sopenharmony_ci# disable LTO
5762306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciGCOV_PROFILE			:= n
6062306a36Sopenharmony_ci# Sanitizer runtimes are unavailable and cannot be linked here.
6162306a36Sopenharmony_ciKASAN_SANITIZE			:= n
6262306a36Sopenharmony_ciKCSAN_SANITIZE			:= n
6362306a36Sopenharmony_ciKMSAN_SANITIZE			:= n
6462306a36Sopenharmony_ciUBSAN_SANITIZE			:= n
6562306a36Sopenharmony_ciOBJECT_FILES_NON_STANDARD	:= y
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
6862306a36Sopenharmony_ciKCOV_INSTRUMENT			:= n
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cilib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
7162306a36Sopenharmony_ci				   file.o mem.o random.o randomalloc.o pci.o \
7262306a36Sopenharmony_ci				   skip_spaces.o lib-cmdline.o lib-ctype.o \
7362306a36Sopenharmony_ci				   alignedmem.o relocate.o printk.o vsprintf.o
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci# include the stub's libfdt dependencies from lib/ when needed
7662306a36Sopenharmony_cilibfdt-deps			:= fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \
7762306a36Sopenharmony_ci				   fdt_empty_tree.c fdt_sw.c
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cilib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \
8062306a36Sopenharmony_ci				     $(patsubst %.c,lib-%.o,$(libfdt-deps))
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
8362306a36Sopenharmony_ci	$(call if_changed_rule,cc_o_c)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cilib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
8662306a36Sopenharmony_ci				   screen_info.o efi-stub-entry.o
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cilib-$(CONFIG_ARM)		+= arm32-stub.o
8962306a36Sopenharmony_cilib-$(CONFIG_ARM64)		+= kaslr.o arm64.o arm64-stub.o smbios.o
9062306a36Sopenharmony_cilib-$(CONFIG_X86)		+= x86-stub.o
9162306a36Sopenharmony_cilib-$(CONFIG_X86_64)		+= x86-5lvl.o
9262306a36Sopenharmony_cilib-$(CONFIG_RISCV)		+= kaslr.o riscv.o riscv-stub.o
9362306a36Sopenharmony_cilib-$(CONFIG_LOONGARCH)		+= loongarch.o loongarch-stub.o
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciCFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cizboot-obj-$(CONFIG_RISCV)	:= lib-clz_ctz.o lib-ashldi3.o
9862306a36Sopenharmony_cilib-$(CONFIG_EFI_ZBOOT)		+= zboot.o $(zboot-obj-y)
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cilib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciextra-y				:= $(lib-y)
10362306a36Sopenharmony_cilib-y				:= $(patsubst %.o,%.stub.o,$(lib-y))
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci# Even when -mbranch-protection=none is set, Clang will generate a
10662306a36Sopenharmony_ci# .note.gnu.property for code-less object files (like lib/ctype.c),
10762306a36Sopenharmony_ci# so work around this by explicitly removing the unwanted section.
10862306a36Sopenharmony_ci# https://bugs.llvm.org/show_bug.cgi?id=46480
10962306a36Sopenharmony_ciSTUBCOPY_FLAGS-y		+= --remove-section=.note.gnu.property
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_X86_32)	:= R_386_32
11262306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_X86_64)	:= R_X86_64_64
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#
11562306a36Sopenharmony_ci# ARM discards the .data section because it disallows r/w data in the
11662306a36Sopenharmony_ci# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub,
11762306a36Sopenharmony_ci# which are preserved explicitly by the decompressor linker script.
11862306a36Sopenharmony_ci#
11962306a36Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_ARM)	+= --rename-section .data=.data.efistub	\
12062306a36Sopenharmony_ci				   --rename-section .bss=.bss.efistub,load,alloc
12162306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#
12462306a36Sopenharmony_ci# arm64 puts the stub in the kernel proper, which will unnecessarily retain all
12562306a36Sopenharmony_ci# code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
12662306a36Sopenharmony_ci# So let's apply the __init annotations at the section level, by prefixing
12762306a36Sopenharmony_ci# the section names directly. This will ensure that even all the inline string
12862306a36Sopenharmony_ci# literals are covered.
12962306a36Sopenharmony_ci# The fact that the stub and the kernel proper are essentially the same binary
13062306a36Sopenharmony_ci# also means that we need to be extra careful to make sure that the stub does
13162306a36Sopenharmony_ci# not rely on any absolute symbol references, considering that the virtual
13262306a36Sopenharmony_ci# kernel mapping that the linker uses is not active yet when the stub is
13362306a36Sopenharmony_ci# executing. So build all C dependencies of the EFI stub into libstub, and do
13462306a36Sopenharmony_ci# a verification pass to see if any absolute relocations exist in any of the
13562306a36Sopenharmony_ci# object files.
13662306a36Sopenharmony_ci#
13762306a36Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
13862306a36Sopenharmony_ci				   --prefix-symbols=__efistub_
13962306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci# For RISC-V, we don't need anything special other than arm64. Keep all the
14262306a36Sopenharmony_ci# symbols in .init section and make sure that no absolute symbols references
14362306a36Sopenharmony_ci# exist.
14462306a36Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_RISCV)	+= --prefix-alloc-sections=.init \
14562306a36Sopenharmony_ci				   --prefix-symbols=__efistub_
14662306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_RISCV)	:= R_RISCV_HI20
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci# For LoongArch, keep all the symbols in .init section and make sure that no
14962306a36Sopenharmony_ci# absolute symbols references exist.
15062306a36Sopenharmony_ciSTUBCOPY_FLAGS-$(CONFIG_LOONGARCH)	+= --prefix-alloc-sections=.init \
15162306a36Sopenharmony_ci					   --prefix-symbols=__efistub_
15262306a36Sopenharmony_ciSTUBCOPY_RELOC-$(CONFIG_LOONGARCH)	:= R_LARCH_MARK_LA
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci$(obj)/%.stub.o: $(obj)/%.o FORCE
15562306a36Sopenharmony_ci	$(call if_changed,stubcopy)
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#
15862306a36Sopenharmony_ci# Strip debug sections and some other sections that may legally contain
15962306a36Sopenharmony_ci# absolute relocations, so that we can inspect the remaining sections for
16062306a36Sopenharmony_ci# such relocations. If none are found, regenerate the output object, but
16162306a36Sopenharmony_ci# this time, use objcopy and leave all sections in place.
16262306a36Sopenharmony_ci#
16362306a36Sopenharmony_ciquiet_cmd_stubcopy = STUBCPY $@
16462306a36Sopenharmony_ci      cmd_stubcopy =							\
16562306a36Sopenharmony_ci	$(STRIP) --strip-debug -o $@ $<;				\
16662306a36Sopenharmony_ci	if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then		\
16762306a36Sopenharmony_ci		echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \
16862306a36Sopenharmony_ci		/bin/false;						\
16962306a36Sopenharmony_ci	fi;								\
17062306a36Sopenharmony_ci	$(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@
171