1# SPDX-License-Identifier: GPL-2.0
2
3purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string.o
4
5targets += $(purgatory-y)
6PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y))
7
8$(obj)/string.o: $(srctree)/arch/x86/boot/compressed/string.c FORCE
9	$(call if_changed_rule,cc_o_c)
10
11$(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
12	$(call if_changed_rule,cc_o_c)
13
14CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
15
16# When profile-guided optimization is enabled, llvm emits two different
17# overlapping text sections, which is not supported by kexec. Remove profile
18# optimization flags.
19KBUILD_CFLAGS := $(filter-out -fprofile-sample-use=% -fprofile-use=%,$(KBUILD_CFLAGS))
20
21# When LTO is enabled, llvm emits many text sections, which is not supported
22# by kexec. Remove -flto=* flags.
23KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS))
24
25# When linking purgatory.ro with -r unresolved symbols are not checked,
26# also link a purgatory.chk binary without -r to check for unresolved symbols.
27PURGATORY_LDFLAGS := -e purgatory_start -z nodefaultlib
28LDFLAGS_purgatory.ro := -r $(PURGATORY_LDFLAGS)
29LDFLAGS_purgatory.chk := $(PURGATORY_LDFLAGS)
30targets += purgatory.ro purgatory.chk
31
32# These are adjustments to the compiler flags used for objects that
33# make up the standalone purgatory.ro
34
35PURGATORY_CFLAGS_REMOVE := -mcmodel=kernel
36PURGATORY_CFLAGS := -mcmodel=small -ffreestanding -fno-zero-initialized-in-bss -g0
37PURGATORY_CFLAGS += -fpic -fvisibility=hidden
38PURGATORY_CFLAGS += $(DISABLE_STACKLEAK_PLUGIN) -DDISABLE_BRANCH_PROFILING
39PURGATORY_CFLAGS += -fno-stack-protector
40
41# Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
42# in turn leaves some undefined symbols like __fentry__ in purgatory and not
43# sure how to relocate those.
44ifdef CONFIG_FUNCTION_TRACER
45PURGATORY_CFLAGS_REMOVE		+= $(CC_FLAGS_FTRACE)
46endif
47
48ifdef CONFIG_STACKPROTECTOR
49PURGATORY_CFLAGS_REMOVE		+= -fstack-protector
50endif
51
52ifdef CONFIG_STACKPROTECTOR_STRONG
53PURGATORY_CFLAGS_REMOVE		+= -fstack-protector-strong
54endif
55
56ifdef CONFIG_MITIGATION_RETPOLINE
57PURGATORY_CFLAGS_REMOVE		+= $(RETPOLINE_CFLAGS)
58endif
59
60ifdef CONFIG_CFI_CLANG
61PURGATORY_CFLAGS_REMOVE		+= $(CC_FLAGS_CFI)
62endif
63
64CFLAGS_REMOVE_purgatory.o	+= $(PURGATORY_CFLAGS_REMOVE)
65CFLAGS_purgatory.o		+= $(PURGATORY_CFLAGS)
66
67CFLAGS_REMOVE_sha256.o		+= $(PURGATORY_CFLAGS_REMOVE)
68CFLAGS_sha256.o			+= $(PURGATORY_CFLAGS)
69
70CFLAGS_REMOVE_string.o		+= $(PURGATORY_CFLAGS_REMOVE)
71CFLAGS_string.o			+= $(PURGATORY_CFLAGS)
72
73asflags-remove-y		+= $(foreach x, -g -gdwarf-4 -gdwarf-5, $(x) -Wa,$(x))
74
75$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE
76		$(call if_changed,ld)
77
78$(obj)/purgatory.chk: $(obj)/purgatory.ro FORCE
79		$(call if_changed,ld)
80
81$(obj)/kexec-purgatory.o: $(obj)/purgatory.ro $(obj)/purgatory.chk
82
83obj-y += kexec-purgatory.o
84