1f4f75ad5SArd Biesheuvel#
2f4f75ad5SArd Biesheuvel# The stub may be linked into the kernel proper or into a separate boot binary,
3f4f75ad5SArd Biesheuvel# but in either case, it executes before the kernel does (with MMU disabled) so
4f4f75ad5SArd Biesheuvel# things like ftrace and stack-protector are likely to cause trouble if left
5f4f75ad5SArd Biesheuvel# enabled, even if doing so doesn't break the build.
6f4f75ad5SArd Biesheuvel#
7f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_32)		:= -march=i386
8f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_64)		:= -mcmodel=small
9f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2 \
10f4f75ad5SArd Biesheuvel				   -fPIC -fno-strict-aliasing -mno-red-zone \
11f4f75ad5SArd Biesheuvel				   -mno-mmx -mno-sse -DDISABLE_BRANCH_PROFILING
12f4f75ad5SArd Biesheuvel
13f4f75ad5SArd Biesheuvelcflags-$(CONFIG_ARM64)		:= $(subst -pg,,$(KBUILD_CFLAGS))
14f4f75ad5SArd Biesheuvelcflags-$(CONFIG_ARM)		:= $(subst -pg,,$(KBUILD_CFLAGS)) \
15f4f75ad5SArd Biesheuvel				   -fno-builtin -fpic -mno-single-pic-base
16f4f75ad5SArd Biesheuvel
17e8f3010fSArd Biesheuvelcflags-$(CONFIG_EFI_ARMSTUB)	+= -I$(srctree)/scripts/dtc/libfdt
18e8f3010fSArd Biesheuvel
19f4f75ad5SArd BiesheuvelKBUILD_CFLAGS			:= $(cflags-y) \
20f4f75ad5SArd Biesheuvel				   $(call cc-option,-ffreestanding) \
21f4f75ad5SArd Biesheuvel				   $(call cc-option,-fno-stack-protector)
22f4f75ad5SArd Biesheuvel
23f4f75ad5SArd BiesheuvelGCOV_PROFILE			:= n
240b24beccSAndrey RyabininKASAN_SANITIZE			:= n
25f4f75ad5SArd Biesheuvel
26f4f75ad5SArd Biesheuvellib-y				:= efi-stub-helper.o
27e8f3010fSArd Biesheuvel
28e8f3010fSArd Biesheuvel# include the stub's generic dependencies from lib/ when building for ARM/arm64
29e8f3010fSArd Biesheuvelarm-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c sort.c
30e8f3010fSArd Biesheuvel
31e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
32e8f3010fSArd Biesheuvel	$(call if_changed_rule,cc_o_c)
33e8f3010fSArd Biesheuvel
34e8f3010fSArd Biesheuvellib-$(CONFIG_EFI_ARMSTUB)	+= arm-stub.o fdt.o string.o \
35e8f3010fSArd Biesheuvel				   $(patsubst %.c,lib-%.o,$(arm-deps))
36f4f75ad5SArd Biesheuvel
3781a0bc39SRoy Franzlib-$(CONFIG_ARM)		+= arm32-stub.o
38bf457786SArd Biesheuvellib-$(CONFIG_ARM64)		+= arm64-stub.o
39bf457786SArd BiesheuvelCFLAGS_arm64-stub.o 		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
40bf457786SArd Biesheuvel
41ddeeefe2SArd Biesheuvel#
42ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all
43ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
44ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing
45ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string
46ddeeefe2SArd Biesheuvel# literals are covered.
47e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary
48e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does
49e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual
50e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is
51e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do
52e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the
53e8f3010fSArd Biesheuvel# object files.
54ddeeefe2SArd Biesheuvel#
55e8f3010fSArd Biesheuvelextra-$(CONFIG_EFI_ARMSTUB)	:= $(lib-y)
56e8f3010fSArd Biesheuvellib-$(CONFIG_EFI_ARMSTUB)	:= $(patsubst %.o,%.stub.o,$(lib-y))
57ddeeefe2SArd Biesheuvel
58f8f8bdc4SArd BiesheuvelSTUBCOPY_FLAGS-y		:= -R .debug* -R *ksymtab* -R *kcrctab*
59e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
60e8f3010fSArd Biesheuvel				   --prefix-symbols=__efistub_
61e8f3010fSArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
62e8f3010fSArd Biesheuvel
63e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE
64e8f3010fSArd Biesheuvel	$(call if_changed,stubcopy)
65e8f3010fSArd Biesheuvel
66e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@
67e8f3010fSArd Biesheuvel      cmd_stubcopy = if $(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@; then	\
68e8f3010fSArd Biesheuvel		     $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y)	\
69e8f3010fSArd Biesheuvel		     && (echo >&2 "$@: absolute symbol references not allowed in the EFI stub"; \
70e8f3010fSArd Biesheuvel			 rm -f $@; /bin/false); else /bin/false; fi
7181a0bc39SRoy Franz
7281a0bc39SRoy Franz#
7381a0bc39SRoy Franz# ARM discards the .data section because it disallows r/w data in the
7481a0bc39SRoy Franz# decompressor. So move our .data to .data.efistub, which is preserved
7581a0bc39SRoy Franz# explicitly by the decompressor linker script.
7681a0bc39SRoy Franz#
7781a0bc39SRoy FranzSTUBCOPY_FLAGS-$(CONFIG_ARM)	+= --rename-section .data=.data.efistub
7881a0bc39SRoy FranzSTUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
79