1# SPDX-License-Identifier: GPL-2.0
2#
3# linux/arch/x86/boot/compressed/Makefile
4#
5# create a compressed vmlinux image from the original vmlinux
6#
7# vmlinuz is:
8#	decompression code (*.o)
9#	asm globals (piggy.S), including:
10#		vmlinux.bin.(gz|bz2|lzma|...)
11#
12# vmlinux.bin is:
13#	vmlinux stripped of debugging and comments
14# vmlinux.bin.all is:
15#	vmlinux.bin + vmlinux.relocs
16# vmlinux.bin.(gz|bz2|lzma|...) is:
17#	(see scripts/Makefile.lib size_append)
18#	compressed vmlinux.bin.all + u32 size of vmlinux.bin.all
19
20targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \
21	vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4 vmlinux.bin.zst
22
23# CLANG_FLAGS must come before any cc-disable-warning or cc-option calls in
24# case of cross compiling, as it has the '--target=' flag, which is needed to
25# avoid errors with '-march=i386', and future flags may depend on the target to
26# be valid.
27KBUILD_CFLAGS := -m$(BITS) -O2 $(CLANG_FLAGS)
28KBUILD_CFLAGS += -fno-strict-aliasing -fPIE
29KBUILD_CFLAGS += -Wundef
30KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING
31cflags-$(CONFIG_X86_32) := -march=i386
32cflags-$(CONFIG_X86_64) := -mcmodel=small -mno-red-zone
33KBUILD_CFLAGS += $(cflags-y)
34KBUILD_CFLAGS += -mno-mmx -mno-sse
35KBUILD_CFLAGS += -ffreestanding -fshort-wchar
36KBUILD_CFLAGS += -fno-stack-protector
37KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
38KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
39KBUILD_CFLAGS += -Wno-pointer-sign
40KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
41KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
42KBUILD_CFLAGS += -D__DISABLE_EXPORTS
43# Disable relocation relaxation in case the link is not PIE.
44KBUILD_CFLAGS += $(call cc-option,-Wa$(comma)-mrelax-relocations=no)
45KBUILD_CFLAGS += -include $(srctree)/include/linux/hidden.h
46
47# sev.c indirectly includes inat-table.h which is generated during
48# compilation and stored in $(objtree). Add the directory to the includes so
49# that the compiler finds it even with out-of-tree builds (make O=/some/path).
50CFLAGS_sev.o += -I$(objtree)/arch/x86/lib/
51
52KBUILD_AFLAGS  := $(KBUILD_CFLAGS) -D__ASSEMBLY__
53
54KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
55KBUILD_LDFLAGS += $(call ld-option,--no-ld-generated-unwind-info)
56# Compressed kernel should be built as PIE since it may be loaded at any
57# address by the bootloader.
58LDFLAGS_vmlinux := -pie $(call ld-option, --no-dynamic-linker)
59ifdef CONFIG_LD_ORPHAN_WARN
60LDFLAGS_vmlinux += --orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
61endif
62LDFLAGS_vmlinux += -z noexecstack
63ifeq ($(CONFIG_LD_IS_BFD),y)
64LDFLAGS_vmlinux += $(call ld-option,--no-warn-rwx-segments)
65endif
66ifeq ($(CONFIG_EFI_STUB),y)
67# ensure that the static EFI stub library will be pulled in, even if it is
68# never referenced explicitly from the startup code
69LDFLAGS_vmlinux += -u efi_pe_entry
70endif
71LDFLAGS_vmlinux += -T
72
73hostprogs	:= mkpiggy
74HOST_EXTRACFLAGS += -I$(srctree)/tools/include
75
76sed-voffset := -e 's/^\([0-9a-fA-F]*\) [ABCDGRSTVW] \(_text\|__start_rodata\|__bss_start\|_end\)$$/\#define VO_\2 _AC(0x\1,UL)/p'
77
78quiet_cmd_voffset = VOFFSET $@
79      cmd_voffset = $(NM) $< | sed -n $(sed-voffset) > $@
80
81targets += ../voffset.h
82
83$(obj)/../voffset.h: vmlinux FORCE
84	$(call if_changed,voffset)
85
86$(obj)/misc.o: $(obj)/../voffset.h
87
88vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/kernel_info.o $(obj)/head_$(BITS).o \
89	$(obj)/misc.o $(obj)/string.o $(obj)/cmdline.o $(obj)/error.o \
90	$(obj)/piggy.o $(obj)/cpuflags.o
91
92vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o
93vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o
94ifdef CONFIG_X86_64
95	vmlinux-objs-y += $(obj)/ident_map_64.o
96	vmlinux-objs-y += $(obj)/idt_64.o $(obj)/idt_handlers_64.o
97	vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/mem_encrypt.o
98	vmlinux-objs-y += $(obj)/pgtable_64.o
99	vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/sev.o
100endif
101
102vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
103vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o $(obj)/tdx-shared.o
104vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o
105
106vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
107vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
108vmlinux-libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
109
110$(obj)/vmlinux: $(vmlinux-objs-y) $(vmlinux-libs-y) FORCE
111	$(call if_changed,ld)
112
113OBJCOPYFLAGS_vmlinux.bin :=  -R .comment -S
114$(obj)/vmlinux.bin: vmlinux FORCE
115	$(call if_changed,objcopy)
116
117targets += $(patsubst $(obj)/%,%,$(vmlinux-objs-y)) vmlinux.bin.all vmlinux.relocs
118
119# vmlinux.relocs is created by the vmlinux postlink step.
120$(obj)/vmlinux.relocs: vmlinux
121	@true
122
123vmlinux.bin.all-y := $(obj)/vmlinux.bin
124vmlinux.bin.all-$(CONFIG_X86_NEED_RELOCS) += $(obj)/vmlinux.relocs
125
126$(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
127	$(call if_changed,gzip)
128$(obj)/vmlinux.bin.bz2: $(vmlinux.bin.all-y) FORCE
129	$(call if_changed,bzip2_with_size)
130$(obj)/vmlinux.bin.lzma: $(vmlinux.bin.all-y) FORCE
131	$(call if_changed,lzma_with_size)
132$(obj)/vmlinux.bin.xz: $(vmlinux.bin.all-y) FORCE
133	$(call if_changed,xzkern_with_size)
134$(obj)/vmlinux.bin.lzo: $(vmlinux.bin.all-y) FORCE
135	$(call if_changed,lzo_with_size)
136$(obj)/vmlinux.bin.lz4: $(vmlinux.bin.all-y) FORCE
137	$(call if_changed,lz4_with_size)
138$(obj)/vmlinux.bin.zst: $(vmlinux.bin.all-y) FORCE
139	$(call if_changed,zstd22_with_size)
140
141suffix-$(CONFIG_KERNEL_GZIP)	:= gz
142suffix-$(CONFIG_KERNEL_BZIP2)	:= bz2
143suffix-$(CONFIG_KERNEL_LZMA)	:= lzma
144suffix-$(CONFIG_KERNEL_XZ)	:= xz
145suffix-$(CONFIG_KERNEL_LZO) 	:= lzo
146suffix-$(CONFIG_KERNEL_LZ4) 	:= lz4
147suffix-$(CONFIG_KERNEL_ZSTD)	:= zst
148
149quiet_cmd_mkpiggy = MKPIGGY $@
150      cmd_mkpiggy = $(obj)/mkpiggy $< > $@
151
152targets += piggy.S
153$(obj)/piggy.S: $(obj)/vmlinux.bin.$(suffix-y) $(obj)/mkpiggy FORCE
154	$(call if_changed,mkpiggy)
155