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#
8f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_32)		:= -march=i386
9769e0fe1SArd Biesheuvelcflags-$(CONFIG_X86_64)		:= -mcmodel=small
10bbf8e8b0SArvind Sankarcflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
11f4f75ad5SArd Biesheuvel				   -fPIC -fno-strict-aliasing -mno-red-zone \
123db5e0baSNathan Chancellor				   -mno-mmx -mno-sse -fshort-wchar \
133db5e0baSNathan Chancellor				   -Wno-pointer-sign \
143db5e0baSNathan Chancellor				   $(call cc-disable-warning, address-of-packed-member) \
15003602adSArvind Sankar				   $(call cc-disable-warning, gnu) \
1658d746c1SNathan Chancellor				   -fno-asynchronous-unwind-tables \
1758d746c1SNathan Chancellor				   $(CLANG_FLAGS)
18f4f75ad5SArd Biesheuvel
19ce279d37SLaura Abbott# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
20ce279d37SLaura Abbott# disable the stackleak plugin
21e1a7eafbSTorsten Duwecflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
22e2179a09SKees Cook				   -fpie $(DISABLE_STACKLEAK_PLUGIN) \
23e2179a09SKees Cook				   $(call cc-option,-mbranch-protection=none)
24e1a7eafbSTorsten Duwecflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
2541f1c484SAlistair Strachan				   -fno-builtin -fpic \
2641f1c484SAlistair Strachan				   $(call cc-option,-mno-single-pic-base)
27d7071743SAtish Patracflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
28d7071743SAtish Patra				   -fpic
29ead384d9SHuacai Chencflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
30ead384d9SHuacai Chen				   -fpie
31f4f75ad5SArd Biesheuvel
322e0eb483SAtish Patracflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt
33e8f3010fSArd Biesheuvel
34bbf8e8b0SArvind SankarKBUILD_CFLAGS			:= $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
35e544ea57SArd Biesheuvel				   -include $(srctree)/include/linux/hidden.h \
363e2c044aSKees Cook				   -D__NO_FORTIFY \
37685969e0SMasahiro Yamada				   -ffreestanding \
38893ab004SMasahiro Yamada				   -fno-stack-protector \
39f77767edSArd Biesheuvel				   $(call cc-option,-fno-addrsig) \
40f922c4abSArd Biesheuvel				   -D__DISABLE_EXPORTS
41f4f75ad5SArd Biesheuvel
42cc49c71dSSami Tolvanen# remove SCS flags from all objects in this directory
43cc49c71dSSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
446e20f185SSami Tolvanen# disable LTO
456e20f185SSami TolvanenKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
46cc49c71dSSami Tolvanen
47f4f75ad5SArd BiesheuvelGCOV_PROFILE			:= n
480ebba714SMarco Elver# Sanitizer runtimes are unavailable and cannot be linked here.
490b24beccSAndrey RyabininKASAN_SANITIZE			:= n
500ebba714SMarco ElverKCSAN_SANITIZE			:= n
51c6d30853SAndrey RyabininUBSAN_SANITIZE			:= n
52c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD	:= y
53f4f75ad5SArd Biesheuvel
545c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
555c9a8750SDmitry VyukovKCOV_INSTRUMENT			:= n
565c9a8750SDmitry Vyukov
570d959814SDominik Brodowskilib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
5891d150c0SArd Biesheuvel				   file.o mem.o random.o randomalloc.o pci.o \
5943b1df0eSArd Biesheuvel				   skip_spaces.o lib-cmdline.o lib-ctype.o \
602c7d1e30SArvind Sankar				   alignedmem.o relocate.o vsprintf.o
61e8f3010fSArd Biesheuvel
62e8f3010fSArd Biesheuvel# include the stub's generic dependencies from lib/ when building for ARM/arm64
632e0eb483SAtish Patraefi-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
64e8f3010fSArd Biesheuvel
65e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
66e8f3010fSArd Biesheuvel	$(call if_changed_rule,cc_o_c)
67e8f3010fSArd Biesheuvel
68*c82ceb44SArd Biesheuvellib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o fdt.o string.o intrinsics.o \
692e0eb483SAtish Patra				   $(patsubst %.c,lib-%.o,$(efi-deps-y))
70f4f75ad5SArd Biesheuvel
7181a0bc39SRoy Franzlib-$(CONFIG_ARM)		+= arm32-stub.o
72a6a14469SArd Biesheuvellib-$(CONFIG_ARM64)		+= arm64-stub.o
73c2d0b470SArd Biesheuvellib-$(CONFIG_X86)		+= x86-stub.o
74d7071743SAtish Patralib-$(CONFIG_RISCV)		+= riscv-stub.o
75ead384d9SHuacai Chenlib-$(CONFIG_LOONGARCH)		+= loongarch-stub.o
76ead384d9SHuacai Chen
7741cd96faSArd BiesheuvelCFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
78bf457786SArd Biesheuvel
79e2179a09SKees Cook# Even when -mbranch-protection=none is set, Clang will generate a
80e2179a09SKees Cook# .note.gnu.property for code-less object files (like lib/ctype.c),
81e2179a09SKees Cook# so work around this by explicitly removing the unwanted section.
82e2179a09SKees Cook# https://bugs.llvm.org/show_bug.cgi?id=46480
83e2179a09SKees CookSTUBCOPY_FLAGS-y		+= --remove-section=.note.gnu.property
84e2179a09SKees Cook
85ddeeefe2SArd Biesheuvel#
8626a92425SArvind Sankar# For x86, bootloaders like systemd-boot or grub-efi do not zero-initialize the
8726a92425SArvind Sankar# .bss section, so the .bss section of the EFI stub needs to be included in the
8826a92425SArvind Sankar# .data section of the compressed kernel to ensure initialization. Rename the
8926a92425SArvind Sankar# .bss section here so it's easy to pick out in the linker script.
9026a92425SArvind Sankar#
9126a92425SArvind SankarSTUBCOPY_FLAGS-$(CONFIG_X86)	+= --rename-section .bss=.bss.efistub,load,alloc
9226a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_32)	:= R_386_32
9326a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_X86_64)	:= R_X86_64_64
9426a92425SArvind Sankar
9526a92425SArvind Sankar#
9626a92425SArvind Sankar# ARM discards the .data section because it disallows r/w data in the
9726a92425SArvind Sankar# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub,
9826a92425SArvind Sankar# which are preserved explicitly by the decompressor linker script.
9926a92425SArvind Sankar#
10026a92425SArvind SankarSTUBCOPY_FLAGS-$(CONFIG_ARM)	+= --rename-section .data=.data.efistub	\
10126a92425SArvind Sankar				   --rename-section .bss=.bss.efistub,load,alloc
10226a92425SArvind SankarSTUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
10326a92425SArvind Sankar
10426a92425SArvind Sankar#
105ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all
106ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
107ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing
108ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string
109ddeeefe2SArd Biesheuvel# literals are covered.
110e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary
111e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does
112e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual
113e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is
114e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do
115e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the
116e8f3010fSArd Biesheuvel# object files.
117ddeeefe2SArd Biesheuvel#
11826a92425SArvind Sankarextra-y				:= $(lib-y)
11926a92425SArvind Sankarlib-y				:= $(patsubst %.o,%.stub.o,$(lib-y))
120ddeeefe2SArd Biesheuvel
121e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
122e8f3010fSArd Biesheuvel				   --prefix-symbols=__efistub_
123e8f3010fSArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
124e8f3010fSArd Biesheuvel
125d7071743SAtish Patra# For RISC-V, we don't need anything special other than arm64. Keep all the
126d7071743SAtish Patra# symbols in .init section and make sure that no absolute symbols references
127d7071743SAtish Patra# doesn't exist.
128d7071743SAtish PatraSTUBCOPY_FLAGS-$(CONFIG_RISCV)	+= --prefix-alloc-sections=.init \
129d7071743SAtish Patra				   --prefix-symbols=__efistub_
130d7071743SAtish PatraSTUBCOPY_RELOC-$(CONFIG_RISCV)	:= R_RISCV_HI20
131d7071743SAtish Patra
132ead384d9SHuacai Chen# For LoongArch, keep all the symbols in .init section and make sure that no
133ead384d9SHuacai Chen# absolute symbols references exist.
134ead384d9SHuacai ChenSTUBCOPY_FLAGS-$(CONFIG_LOONGARCH)	+= --prefix-alloc-sections=.init \
135ead384d9SHuacai Chen					   --prefix-symbols=__efistub_
136ead384d9SHuacai ChenSTUBCOPY_RELOC-$(CONFIG_LOONGARCH)	:= R_LARCH_MARK_LA
137ead384d9SHuacai Chen
138e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE
139e8f3010fSArd Biesheuvel	$(call if_changed,stubcopy)
140e8f3010fSArd Biesheuvel
141696204faSArd Biesheuvel#
142696204faSArd Biesheuvel# Strip debug sections and some other sections that may legally contain
143696204faSArd Biesheuvel# absolute relocations, so that we can inspect the remaining sections for
144696204faSArd Biesheuvel# such relocations. If none are found, regenerate the output object, but
145696204faSArd Biesheuvel# this time, use objcopy and leave all sections in place.
146696204faSArd Biesheuvel#
147e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@
148e8d368adSMasahiro Yamada      cmd_stubcopy =							\
14902562d0cSArd Biesheuvel	$(STRIP) --strip-debug -o $@ $<;				\
150e8d368adSMasahiro Yamada	if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then		\
151e8d368adSMasahiro Yamada		echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \
152e8d368adSMasahiro Yamada		/bin/false;						\
153e8d368adSMasahiro Yamada	fi;								\
154e8d368adSMasahiro Yamada	$(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@
155