xref: /openbmc/linux/drivers/firmware/efi/libstub/Makefile (revision 43b1df0e013c25abb536699f46d0e9f291b586a0)
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
9f4f75ad5SArd Biesheuvelcflags-$(CONFIG_X86_64)		:= -mcmodel=small
10846221cfSPaul Bollecflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ -O2 \
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) \
16003602adSArvind Sankar				   -fno-asynchronous-unwind-tables
17f4f75ad5SArd Biesheuvel
18ce279d37SLaura Abbott# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
19ce279d37SLaura Abbott# disable the stackleak plugin
20e1a7eafbSTorsten Duwecflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
21e1a7eafbSTorsten Duwe				   -fpie $(DISABLE_STACKLEAK_PLUGIN)
22e1a7eafbSTorsten Duwecflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
2341f1c484SAlistair Strachan				   -fno-builtin -fpic \
2441f1c484SAlistair Strachan				   $(call cc-option,-mno-single-pic-base)
25f4f75ad5SArd Biesheuvel
262e0eb483SAtish Patracflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt
27e8f3010fSArd Biesheuvel
28b523e185SArd BiesheuvelKBUILD_CFLAGS			:= $(cflags-y) -DDISABLE_BRANCH_PROFILING \
296f05106eSArd Biesheuvel				   -include $(srctree)/drivers/firmware/efi/libstub/hidden.h \
303e2c044aSKees Cook				   -D__NO_FORTIFY \
31f4f75ad5SArd Biesheuvel				   $(call cc-option,-ffreestanding) \
320b3e3366SLaura Abbott				   $(call cc-option,-fno-stack-protector) \
33f922c4abSArd Biesheuvel				   -D__DISABLE_EXPORTS
34f4f75ad5SArd Biesheuvel
35f4f75ad5SArd BiesheuvelGCOV_PROFILE			:= n
360b24beccSAndrey RyabininKASAN_SANITIZE			:= n
37c6d30853SAndrey RyabininUBSAN_SANITIZE			:= n
38c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD	:= y
39f4f75ad5SArd Biesheuvel
405c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
415c9a8750SDmitry VyukovKCOV_INSTRUMENT			:= n
425c9a8750SDmitry Vyukov
430d959814SDominik Brodowskilib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
4491d150c0SArd Biesheuvel				   file.o mem.o random.o randomalloc.o pci.o \
45*43b1df0eSArd Biesheuvel				   skip_spaces.o lib-cmdline.o lib-ctype.o \
46*43b1df0eSArd Biesheuvel				   alignedmem.o
47e8f3010fSArd Biesheuvel
48e8f3010fSArd Biesheuvel# include the stub's generic dependencies from lib/ when building for ARM/arm64
492e0eb483SAtish Patraefi-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
50e8f3010fSArd Biesheuvel
51e8f3010fSArd Biesheuvel$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
52e8f3010fSArd Biesheuvel	$(call if_changed_rule,cc_o_c)
53e8f3010fSArd Biesheuvel
542e0eb483SAtish Patralib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o fdt.o string.o \
552e0eb483SAtish Patra				   $(patsubst %.c,lib-%.o,$(efi-deps-y))
56f4f75ad5SArd Biesheuvel
5781a0bc39SRoy Franzlib-$(CONFIG_ARM)		+= arm32-stub.o
58a6a14469SArd Biesheuvellib-$(CONFIG_ARM64)		+= arm64-stub.o
59c2d0b470SArd Biesheuvellib-$(CONFIG_X86)		+= x86-stub.o
6041cd96faSArd BiesheuvelCFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
61bf457786SArd BiesheuvelCFLAGS_arm64-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
62bf457786SArd Biesheuvel
63ddeeefe2SArd Biesheuvel#
64ddeeefe2SArd Biesheuvel# arm64 puts the stub in the kernel proper, which will unnecessarily retain all
65ddeeefe2SArd Biesheuvel# code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
66ddeeefe2SArd Biesheuvel# So let's apply the __init annotations at the section level, by prefixing
67ddeeefe2SArd Biesheuvel# the section names directly. This will ensure that even all the inline string
68ddeeefe2SArd Biesheuvel# literals are covered.
69e8f3010fSArd Biesheuvel# The fact that the stub and the kernel proper are essentially the same binary
70e8f3010fSArd Biesheuvel# also means that we need to be extra careful to make sure that the stub does
71e8f3010fSArd Biesheuvel# not rely on any absolute symbol references, considering that the virtual
72e8f3010fSArd Biesheuvel# kernel mapping that the linker uses is not active yet when the stub is
73e8f3010fSArd Biesheuvel# executing. So build all C dependencies of the EFI stub into libstub, and do
74e8f3010fSArd Biesheuvel# a verification pass to see if any absolute relocations exist in any of the
75e8f3010fSArd Biesheuvel# object files.
76ddeeefe2SArd Biesheuvel#
772e0eb483SAtish Patraextra-$(CONFIG_EFI_GENERIC_STUB) := $(lib-y)
782e0eb483SAtish Patralib-$(CONFIG_EFI_GENERIC_STUB)	:= $(patsubst %.o,%.stub.o,$(lib-y))
79ddeeefe2SArd Biesheuvel
80e8f3010fSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
81e8f3010fSArd Biesheuvel				   --prefix-symbols=__efistub_
82e8f3010fSArd BiesheuvelSTUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
83e8f3010fSArd Biesheuvel
84e8f3010fSArd Biesheuvel$(obj)/%.stub.o: $(obj)/%.o FORCE
85e8f3010fSArd Biesheuvel	$(call if_changed,stubcopy)
86e8f3010fSArd Biesheuvel
87696204faSArd Biesheuvel#
88696204faSArd Biesheuvel# Strip debug sections and some other sections that may legally contain
89696204faSArd Biesheuvel# absolute relocations, so that we can inspect the remaining sections for
90696204faSArd Biesheuvel# such relocations. If none are found, regenerate the output object, but
91696204faSArd Biesheuvel# this time, use objcopy and leave all sections in place.
92696204faSArd Biesheuvel#
93e8f3010fSArd Biesheuvelquiet_cmd_stubcopy = STUBCPY $@
94e8d368adSMasahiro Yamada      cmd_stubcopy =							\
9502562d0cSArd Biesheuvel	$(STRIP) --strip-debug -o $@ $<;				\
96e8d368adSMasahiro Yamada	if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then		\
97e8d368adSMasahiro Yamada		echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \
98e8d368adSMasahiro Yamada		/bin/false;						\
99e8d368adSMasahiro Yamada	fi;								\
100e8d368adSMasahiro Yamada	$(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@
10181a0bc39SRoy Franz
10281a0bc39SRoy Franz#
10381a0bc39SRoy Franz# ARM discards the .data section because it disallows r/w data in the
10481a0bc39SRoy Franz# decompressor. So move our .data to .data.efistub, which is preserved
10581a0bc39SRoy Franz# explicitly by the decompressor linker script.
10681a0bc39SRoy Franz#
107696204faSArd BiesheuvelSTUBCOPY_FLAGS-$(CONFIG_ARM)	+= --rename-section .data=.data.efistub
10881a0bc39SRoy FranzSTUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
109