xref: /openbmc/linux/arch/x86/boot/compressed/Makefile (revision 1958b5fc)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2db569afaSSam Ravnborg#
3db569afaSSam Ravnborg# linux/arch/x86/boot/compressed/Makefile
4db569afaSSam Ravnborg#
5db569afaSSam Ravnborg# create a compressed vmlinux image from the original vmlinux
6db569afaSSam Ravnborg#
7fb7183efSKees Cook# vmlinuz is:
8fb7183efSKees Cook#	decompression code (*.o)
9fb7183efSKees Cook#	asm globals (piggy.S), including:
10fb7183efSKees Cook#		vmlinux.bin.(gz|bz2|lzma|...)
11fb7183efSKees Cook#
12fb7183efSKees Cook# vmlinux.bin is:
13fb7183efSKees Cook#	vmlinux stripped of debugging and comments
14fb7183efSKees Cook# vmlinux.bin.all is:
15fb7183efSKees Cook#	vmlinux.bin + vmlinux.relocs
16fb7183efSKees Cook# vmlinux.bin.(gz|bz2|lzma|...) is:
17fb7183efSKees Cook#	(see scripts/Makefile.lib size_append)
18fb7183efSKees Cook#	compressed vmlinux.bin.all + u32 size of vmlinux.bin.all
19db569afaSSam Ravnborg
20ef7f0d6aSAndrey RyabininKASAN_SANITIZE			:= n
21c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD	:= y
22ef7f0d6aSAndrey Ryabinin
235c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
245c9a8750SDmitry VyukovKCOV_INSTRUMENT		:= n
255c9a8750SDmitry Vyukov
26f9b493acSKyungsik Leetargets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \
27f9b493acSKyungsik Lee	vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4
28db569afaSSam Ravnborg
29846221cfSPaul BolleKBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ -O2
306d92bc9dSH.J. LuKBUILD_CFLAGS += -fno-strict-aliasing $(call cc-option, -fPIE, -fPIC)
3195a38f34SH. Peter AnvinKBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING
3217a2a9b5SH. Peter Anvincflags-$(CONFIG_X86_32) := -march=i386
33db569afaSSam Ravnborgcflags-$(CONFIG_X86_64) := -mcmodel=small
34db569afaSSam RavnborgKBUILD_CFLAGS += $(cflags-y)
358b3b005dSH. Peter AnvinKBUILD_CFLAGS += -mno-mmx -mno-sse
36db569afaSSam RavnborgKBUILD_CFLAGS += $(call cc-option,-ffreestanding)
37db569afaSSam RavnborgKBUILD_CFLAGS += $(call cc-option,-fno-stack-protector)
3820c6c189SMatthias KaehlckeKBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
39db569afaSSam Ravnborg
40db569afaSSam RavnborgKBUILD_AFLAGS  := $(KBUILD_CFLAGS) -D__ASSEMBLY__
417bf99fb6SPeter OberparleiterGCOV_PROFILE := n
42c6d30853SAndrey RyabininUBSAN_SANITIZE :=n
43db569afaSSam Ravnborg
44db569afaSSam RavnborgLDFLAGS := -m elf_$(UTS_MACHINE)
45a980ce35SH.J. Lu# Compressed kernel should be built as PIE since it may be loaded at any
46a980ce35SH.J. Lu# address by the bootloader.
476d92bc9dSH.J. Luifeq ($(CONFIG_X86_32),y)
486d92bc9dSH.J. LuLDFLAGS += $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker)
496d92bc9dSH.J. Luelse
506d92bc9dSH.J. Lu# To build 64-bit compressed kernel as PIE, we disable relocation
516d92bc9dSH.J. Lu# overflow check to avoid relocation overflow error with a new linker
526d92bc9dSH.J. Lu# command-line option, -z noreloc-overflow.
536d92bc9dSH.J. LuLDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
546d92bc9dSH.J. Lu	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
556d92bc9dSH.J. Luendif
56db569afaSSam RavnborgLDFLAGS_vmlinux := -T
57db569afaSSam Ravnborg
5802a884c0SH. Peter Anvinhostprogs-y	:= mkpiggy
5912871c56SMatt FlemingHOST_EXTRACFLAGS += -I$(srctree)/tools/include
6002a884c0SH. Peter Anvin
614abf061bSYinghai Lused-voffset := -e 's/^\([0-9a-fA-F]*\) [ABCDGRSTVW] \(_text\|__bss_start\|_end\)$$/\#define VO_\2 _AC(0x\1,UL)/p'
6267b66625SYinghai Lu
6367b66625SYinghai Luquiet_cmd_voffset = VOFFSET $@
6467b66625SYinghai Lu      cmd_voffset = $(NM) $< | sed -n $(sed-voffset) > $@
6567b66625SYinghai Lu
6667b66625SYinghai Lutargets += ../voffset.h
6767b66625SYinghai Lu
6867b66625SYinghai Lu$(obj)/../voffset.h: vmlinux FORCE
6967b66625SYinghai Lu	$(call if_changed,voffset)
7067b66625SYinghai Lu
7167b66625SYinghai Lu$(obj)/misc.o: $(obj)/../voffset.h
7267b66625SYinghai Lu
739a1cb471SJosh Triplettvmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \
74dc425a6eSKees Cook	$(obj)/string.o $(obj)/cmdline.o $(obj)/error.o \
759e6abd2aSJosh Triplett	$(obj)/piggy.o $(obj)/cpuflags.o
769e6abd2aSJosh Triplett
773afed06aSJosh Triplettvmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o
789b238748SKees Cookvmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o
793a94707dSKees Cookifdef CONFIG_X86_64
803a94707dSKees Cook	vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/pagetable.o
811958b5fcSTom Lendacky	vmlinux-objs-y += $(obj)/mem_encrypt.o
823a94707dSKees Cookendif
83291f3632SMatt Fleming
849dead5bbSMatthew Garrett$(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
859dead5bbSMatthew Garrett
86243b6754SArd Biesheuvelvmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
87243b6754SArd Biesheuvel	$(objtree)/drivers/firmware/efi/libstub/lib.a
8896738c69SMatt Flemingvmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
89291f3632SMatt Fleming
9098f78525SKees Cook# The compressed kernel is built with -fPIC/-fPIE so that a boot loader
9198f78525SKees Cook# can place it anywhere in memory and it will still run. However, since
9298f78525SKees Cook# it is executed as-is without any ELF relocation processing performed
9398f78525SKees Cook# (and has already had all relocation sections stripped from the binary),
9498f78525SKees Cook# none of the code can use data relocations (e.g. static assignments of
9598f78525SKees Cook# pointer values), since they will be meaningless at runtime. This check
9698f78525SKees Cook# will refuse to link the vmlinux if any of these relocations are found.
9798f78525SKees Cookquiet_cmd_check_data_rel = DATAREL $@
9898f78525SKees Cookdefine cmd_check_data_rel
9998f78525SKees Cook	for obj in $(filter %.o,$^); do \
10037805787SRob Landley		${CROSS_COMPILE}readelf -S $$obj | grep -qF .rel.local && { \
10198f78525SKees Cook			echo "error: $$obj has data relocations!" >&2; \
10298f78525SKees Cook			exit 1; \
10398f78525SKees Cook		} || true; \
10498f78525SKees Cook	done
10598f78525SKees Cookendef
10698f78525SKees Cook
1079a1cb471SJosh Triplett$(obj)/vmlinux: $(vmlinux-objs-y) FORCE
10898f78525SKees Cook	$(call if_changed,check_data_rel)
109db569afaSSam Ravnborg	$(call if_changed,ld)
110db569afaSSam Ravnborg
111099e1377SIan CampbellOBJCOPYFLAGS_vmlinux.bin :=  -R .comment -S
112db569afaSSam Ravnborg$(obj)/vmlinux.bin: vmlinux FORCE
113db569afaSSam Ravnborg	$(call if_changed,objcopy)
114db569afaSSam Ravnborg
1159a1cb471SJosh Tripletttargets += $(patsubst $(obj)/%,%,$(vmlinux-objs-y)) vmlinux.bin.all vmlinux.relocs
116db569afaSSam Ravnborg
1176520fe55SH. Peter AnvinCMD_RELOCS = arch/x86/tools/relocs
118db569afaSSam Ravnborgquiet_cmd_relocs = RELOCS  $@
1196520fe55SH. Peter Anvin      cmd_relocs = $(CMD_RELOCS) $< > $@;$(CMD_RELOCS) --abs-relocs $<
1206520fe55SH. Peter Anvin$(obj)/vmlinux.relocs: vmlinux FORCE
121db569afaSSam Ravnborg	$(call if_changed,relocs)
122db569afaSSam Ravnborg
123db569afaSSam Ravnborgvmlinux.bin.all-y := $(obj)/vmlinux.bin
1245f11e020SH. Peter Anvinvmlinux.bin.all-$(CONFIG_X86_NEED_RELOCS) += $(obj)/vmlinux.relocs
125db569afaSSam Ravnborg
1265f11e020SH. Peter Anvin$(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
127db569afaSSam Ravnborg	$(call if_changed,gzip)
1285f11e020SH. Peter Anvin$(obj)/vmlinux.bin.bz2: $(vmlinux.bin.all-y) FORCE
129ae03c499SAlain Knaff	$(call if_changed,bzip2)
1305f11e020SH. Peter Anvin$(obj)/vmlinux.bin.lzma: $(vmlinux.bin.all-y) FORCE
131ae03c499SAlain Knaff	$(call if_changed,lzma)
13230314804SLasse Collin$(obj)/vmlinux.bin.xz: $(vmlinux.bin.all-y) FORCE
13330314804SLasse Collin	$(call if_changed,xzkern)
13413510997SAlbin Tonnerre$(obj)/vmlinux.bin.lzo: $(vmlinux.bin.all-y) FORCE
13513510997SAlbin Tonnerre	$(call if_changed,lzo)
136f9b493acSKyungsik Lee$(obj)/vmlinux.bin.lz4: $(vmlinux.bin.all-y) FORCE
137f9b493acSKyungsik Lee	$(call if_changed,lz4)
138db569afaSSam Ravnborg
139283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_GZIP)	:= gz
140283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_BZIP2)	:= bz2
141283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_LZMA)	:= lzma
14230314804SLasse Collinsuffix-$(CONFIG_KERNEL_XZ)	:= xz
14313510997SAlbin Tonnerresuffix-$(CONFIG_KERNEL_LZO) 	:= lzo
144f9b493acSKyungsik Leesuffix-$(CONFIG_KERNEL_LZ4) 	:= lz4
145ae03c499SAlain Knaff
14602a884c0SH. Peter Anvinquiet_cmd_mkpiggy = MKPIGGY $@
1474d2d5424SYinghai Lu      cmd_mkpiggy = $(obj)/mkpiggy $< > $@ || ( rm -f $@ ; false )
14802a884c0SH. Peter Anvin
14902a884c0SH. Peter Anvintargets += piggy.S
15002a884c0SH. Peter Anvin$(obj)/piggy.S: $(obj)/vmlinux.bin.$(suffix-y) $(obj)/mkpiggy FORCE
15102a884c0SH. Peter Anvin	$(call if_changed,mkpiggy)
152