xref: /OK3568_Linux_fs/kernel/drivers/firmware/efi/libstub/Makefile (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# The stub may be linked into the kernel proper or into a separate boot binary,
4*4882a593Smuzhiyun# but in either case, it executes before the kernel does (with MMU disabled) so
5*4882a593Smuzhiyun# things like ftrace and stack-protector are likely to cause trouble if left
6*4882a593Smuzhiyun# enabled, even if doing so doesn't break the build.
7*4882a593Smuzhiyun#
8*4882a593Smuzhiyuncflags-$(CONFIG_X86_32)		:= -march=i386
9*4882a593Smuzhiyuncflags-$(CONFIG_X86_64)		:= -mcmodel=small
10*4882a593Smuzhiyuncflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
11*4882a593Smuzhiyun				   -fPIC -fno-strict-aliasing -mno-red-zone \
12*4882a593Smuzhiyun				   -mno-mmx -mno-sse -fshort-wchar \
13*4882a593Smuzhiyun				   -Wno-pointer-sign \
14*4882a593Smuzhiyun				   $(call cc-disable-warning, address-of-packed-member) \
15*4882a593Smuzhiyun				   $(call cc-disable-warning, gnu) \
16*4882a593Smuzhiyun				   -fno-asynchronous-unwind-tables \
17*4882a593Smuzhiyun				   $(CLANG_FLAGS)
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
20*4882a593Smuzhiyun# disable the stackleak plugin
21*4882a593Smuzhiyuncflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
22*4882a593Smuzhiyun				   -fpie $(DISABLE_STACKLEAK_PLUGIN) \
23*4882a593Smuzhiyun				   $(call cc-option,-mbranch-protection=none)
24*4882a593Smuzhiyuncflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
25*4882a593Smuzhiyun				   -fno-builtin -fpic \
26*4882a593Smuzhiyun				   $(call cc-option,-mno-single-pic-base)
27*4882a593Smuzhiyuncflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
28*4882a593Smuzhiyun				   -fpic
29*4882a593Smuzhiyun
30*4882a593Smuzhiyuncflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunKBUILD_CFLAGS			:= $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
33*4882a593Smuzhiyun				   -include $(srctree)/include/linux/hidden.h \
34*4882a593Smuzhiyun				   -D__NO_FORTIFY \
35*4882a593Smuzhiyun				   -ffreestanding \
36*4882a593Smuzhiyun				   -fno-stack-protector \
37*4882a593Smuzhiyun				   $(call cc-option,-fno-addrsig) \
38*4882a593Smuzhiyun				   -D__DISABLE_EXPORTS
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun#
41*4882a593Smuzhiyun# struct randomization only makes sense for Linux internal types, which the EFI
42*4882a593Smuzhiyun# stub code never touches, so let's turn off struct randomization for the stub
43*4882a593Smuzhiyun# altogether
44*4882a593Smuzhiyun#
45*4882a593SmuzhiyunKBUILD_CFLAGS := $(filter-out $(RANDSTRUCT_CFLAGS), $(KBUILD_CFLAGS))
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun# remove SCS flags from all objects in this directory
48*4882a593SmuzhiyunKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
49*4882a593Smuzhiyun# disable LTO
50*4882a593SmuzhiyunKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunGCOV_PROFILE			:= n
53*4882a593Smuzhiyun# Sanitizer runtimes are unavailable and cannot be linked here.
54*4882a593SmuzhiyunKASAN_SANITIZE			:= n
55*4882a593SmuzhiyunKCSAN_SANITIZE			:= n
56*4882a593SmuzhiyunUBSAN_SANITIZE			:= n
57*4882a593SmuzhiyunOBJECT_FILES_NON_STANDARD	:= y
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
60*4882a593SmuzhiyunKCOV_INSTRUMENT			:= n
61*4882a593Smuzhiyun
62*4882a593Smuzhiyunlib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
63*4882a593Smuzhiyun				   file.o mem.o random.o randomalloc.o pci.o \
64*4882a593Smuzhiyun				   skip_spaces.o lib-cmdline.o lib-ctype.o \
65*4882a593Smuzhiyun				   alignedmem.o relocate.o vsprintf.o
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun# include the stub's generic dependencies from lib/ when building for ARM/arm64
68*4882a593Smuzhiyunefi-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
71*4882a593Smuzhiyun	$(call if_changed_rule,cc_o_c)
72*4882a593Smuzhiyun
73*4882a593Smuzhiyunlib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o fdt.o string.o \
74*4882a593Smuzhiyun				   $(patsubst %.c,lib-%.o,$(efi-deps-y))
75*4882a593Smuzhiyun
76*4882a593Smuzhiyunlib-$(CONFIG_ARM)		+= arm32-stub.o
77*4882a593Smuzhiyunlib-$(CONFIG_ARM64)		+= arm64-stub.o
78*4882a593Smuzhiyunlib-$(CONFIG_X86)		+= x86-stub.o
79*4882a593Smuzhiyunlib-$(CONFIG_RISCV)		+= riscv-stub.o
80*4882a593SmuzhiyunCFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun# Even when -mbranch-protection=none is set, Clang will generate a
83*4882a593Smuzhiyun# .note.gnu.property for code-less object files (like lib/ctype.c),
84*4882a593Smuzhiyun# so work around this by explicitly removing the unwanted section.
85*4882a593Smuzhiyun# https://bugs.llvm.org/show_bug.cgi?id=46480
86*4882a593SmuzhiyunSTUBCOPY_FLAGS-y		+= --remove-section=.note.gnu.property
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun#
89*4882a593Smuzhiyun# For x86, bootloaders like systemd-boot or grub-efi do not zero-initialize the
90*4882a593Smuzhiyun# .bss section, so the .bss section of the EFI stub needs to be included in the
91*4882a593Smuzhiyun# .data section of the compressed kernel to ensure initialization. Rename the
92*4882a593Smuzhiyun# .bss section here so it's easy to pick out in the linker script.
93*4882a593Smuzhiyun#
94*4882a593SmuzhiyunSTUBCOPY_FLAGS-$(CONFIG_X86)	+= --rename-section .bss=.bss.efistub,load,alloc
95*4882a593SmuzhiyunSTUBCOPY_RELOC-$(CONFIG_X86_32)	:= R_386_32
96*4882a593SmuzhiyunSTUBCOPY_RELOC-$(CONFIG_X86_64)	:= R_X86_64_64
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun#
99*4882a593Smuzhiyun# ARM discards the .data section because it disallows r/w data in the
100*4882a593Smuzhiyun# decompressor. So move our .data to .data.efistub and .bss to .bss.efistub,
101*4882a593Smuzhiyun# which are preserved explicitly by the decompressor linker script.
102*4882a593Smuzhiyun#
103*4882a593SmuzhiyunSTUBCOPY_FLAGS-$(CONFIG_ARM)	+= --rename-section .data=.data.efistub	\
104*4882a593Smuzhiyun				   --rename-section .bss=.bss.efistub,load,alloc
105*4882a593SmuzhiyunSTUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun#
108*4882a593Smuzhiyun# arm64 puts the stub in the kernel proper, which will unnecessarily retain all
109*4882a593Smuzhiyun# code indefinitely unless it is annotated as __init/__initdata/__initconst etc.
110*4882a593Smuzhiyun# So let's apply the __init annotations at the section level, by prefixing
111*4882a593Smuzhiyun# the section names directly. This will ensure that even all the inline string
112*4882a593Smuzhiyun# literals are covered.
113*4882a593Smuzhiyun# The fact that the stub and the kernel proper are essentially the same binary
114*4882a593Smuzhiyun# also means that we need to be extra careful to make sure that the stub does
115*4882a593Smuzhiyun# not rely on any absolute symbol references, considering that the virtual
116*4882a593Smuzhiyun# kernel mapping that the linker uses is not active yet when the stub is
117*4882a593Smuzhiyun# executing. So build all C dependencies of the EFI stub into libstub, and do
118*4882a593Smuzhiyun# a verification pass to see if any absolute relocations exist in any of the
119*4882a593Smuzhiyun# object files.
120*4882a593Smuzhiyun#
121*4882a593Smuzhiyunextra-y				:= $(lib-y)
122*4882a593Smuzhiyunlib-y				:= $(patsubst %.o,%.stub.o,$(lib-y))
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunSTUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
125*4882a593Smuzhiyun				   --prefix-symbols=__efistub_
126*4882a593SmuzhiyunSTUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun# For RISC-V, we don't need anything special other than arm64. Keep all the
129*4882a593Smuzhiyun# symbols in .init section and make sure that no absolute symbols references
130*4882a593Smuzhiyun# doesn't exist.
131*4882a593SmuzhiyunSTUBCOPY_FLAGS-$(CONFIG_RISCV)	+= --prefix-alloc-sections=.init \
132*4882a593Smuzhiyun				   --prefix-symbols=__efistub_
133*4882a593SmuzhiyunSTUBCOPY_RELOC-$(CONFIG_RISCV)	:= R_RISCV_HI20
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun$(obj)/%.stub.o: $(obj)/%.o FORCE
136*4882a593Smuzhiyun	$(call if_changed,stubcopy)
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun#
139*4882a593Smuzhiyun# Strip debug sections and some other sections that may legally contain
140*4882a593Smuzhiyun# absolute relocations, so that we can inspect the remaining sections for
141*4882a593Smuzhiyun# such relocations. If none are found, regenerate the output object, but
142*4882a593Smuzhiyun# this time, use objcopy and leave all sections in place.
143*4882a593Smuzhiyun#
144*4882a593Smuzhiyunquiet_cmd_stubcopy = STUBCPY $@
145*4882a593Smuzhiyun      cmd_stubcopy =							\
146*4882a593Smuzhiyun	$(STRIP) --strip-debug -o $@ $<;				\
147*4882a593Smuzhiyun	if $(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y); then		\
148*4882a593Smuzhiyun		echo "$@: absolute symbol references not allowed in the EFI stub" >&2; \
149*4882a593Smuzhiyun		/bin/false;						\
150*4882a593Smuzhiyun	fi;								\
151*4882a593Smuzhiyun	$(OBJCOPY) $(STUBCOPY_FLAGS-y) $< $@
152