xref: /openbmc/linux/arch/x86/boot/compressed/Makefile (revision 09e43968)
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
2040d04110SMarco Elver# Sanitizer runtimes are unavailable and cannot be linked for early boot code.
21ef7f0d6aSAndrey RyabininKASAN_SANITIZE			:= n
2240d04110SMarco ElverKCSAN_SANITIZE			:= n
23c0dd6716SJosh PoimboeufOBJECT_FILES_NON_STANDARD	:= y
24ef7f0d6aSAndrey Ryabinin
255c9a8750SDmitry Vyukov# Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
265c9a8750SDmitry VyukovKCOV_INSTRUMENT		:= n
275c9a8750SDmitry Vyukov
28f9b493acSKyungsik Leetargets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma \
29fb46d057SNick Terrell	vmlinux.bin.xz vmlinux.bin.lzo vmlinux.bin.lz4 vmlinux.bin.zst
30db569afaSSam Ravnborg
31d6289f36SCao jinKBUILD_CFLAGS := -m$(BITS) -O2
326d92bc9dSH.J. LuKBUILD_CFLAGS += -fno-strict-aliasing $(call cc-option, -fPIE, -fPIC)
3395a38f34SH. Peter AnvinKBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING
3417a2a9b5SH. Peter Anvincflags-$(CONFIG_X86_32) := -march=i386
35db569afaSSam Ravnborgcflags-$(CONFIG_X86_64) := -mcmodel=small
36db569afaSSam RavnborgKBUILD_CFLAGS += $(cflags-y)
378b3b005dSH. Peter AnvinKBUILD_CFLAGS += -mno-mmx -mno-sse
38685969e0SMasahiro YamadaKBUILD_CFLAGS += -ffreestanding
39893ab004SMasahiro YamadaKBUILD_CFLAGS += -fno-stack-protector
4020c6c189SMatthias KaehlckeKBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
416c3b56b1SMatthias KaehlckeKBUILD_CFLAGS += $(call cc-disable-warning, gnu)
42dca5203eSNathan ChancellorKBUILD_CFLAGS += -Wno-pointer-sign
439e2276faSBruce AshfieldKBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
44003602adSArvind SankarKBUILD_CFLAGS += -fno-asynchronous-unwind-tables
45fb46d057SNick TerrellKBUILD_CFLAGS += -D__DISABLE_EXPORTS
4609e43968SArvind Sankar# Disable relocation relaxation in case the link is not PIE.
4709e43968SArvind SankarKBUILD_CFLAGS += $(call as-option,-Wa$(comma)-mrelax-relocations=no)
48db569afaSSam Ravnborg
49db569afaSSam RavnborgKBUILD_AFLAGS  := $(KBUILD_CFLAGS) -D__ASSEMBLY__
507bf99fb6SPeter OberparleiterGCOV_PROFILE := n
51c6d30853SAndrey RyabininUBSAN_SANITIZE :=n
52db569afaSSam Ravnborg
53d503ac53SMasahiro YamadaKBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
54a980ce35SH.J. Lu# Compressed kernel should be built as PIE since it may be loaded at any
55a980ce35SH.J. Lu# address by the bootloader.
566d92bc9dSH.J. Luifeq ($(CONFIG_X86_32),y)
57d503ac53SMasahiro YamadaKBUILD_LDFLAGS += $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker)
586d92bc9dSH.J. Luelse
596d92bc9dSH.J. Lu# To build 64-bit compressed kernel as PIE, we disable relocation
606d92bc9dSH.J. Lu# overflow check to avoid relocation overflow error with a new linker
616d92bc9dSH.J. Lu# command-line option, -z noreloc-overflow.
62d503ac53SMasahiro YamadaKBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
636d92bc9dSH.J. Lu	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
646d92bc9dSH.J. Luendif
65db569afaSSam RavnborgLDFLAGS_vmlinux := -T
66db569afaSSam Ravnborg
675f2fb52fSMasahiro Yamadahostprogs	:= mkpiggy
6812871c56SMatt FlemingHOST_EXTRACFLAGS += -I$(srctree)/tools/include
6902a884c0SH. Peter Anvin
704abf061bSYinghai Lused-voffset := -e 's/^\([0-9a-fA-F]*\) [ABCDGRSTVW] \(_text\|__bss_start\|_end\)$$/\#define VO_\2 _AC(0x\1,UL)/p'
7167b66625SYinghai Lu
7267b66625SYinghai Luquiet_cmd_voffset = VOFFSET $@
7367b66625SYinghai Lu      cmd_voffset = $(NM) $< | sed -n $(sed-voffset) > $@
7467b66625SYinghai Lu
7567b66625SYinghai Lutargets += ../voffset.h
7667b66625SYinghai Lu
7767b66625SYinghai Lu$(obj)/../voffset.h: vmlinux FORCE
7867b66625SYinghai Lu	$(call if_changed,voffset)
7967b66625SYinghai Lu
8067b66625SYinghai Lu$(obj)/misc.o: $(obj)/../voffset.h
8167b66625SYinghai Lu
822c33c27fSDaniel Kipervmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/kernel_info.o $(obj)/head_$(BITS).o \
832c33c27fSDaniel Kiper	$(obj)/misc.o $(obj)/string.o $(obj)/cmdline.o $(obj)/error.o \
849e6abd2aSJosh Triplett	$(obj)/piggy.o $(obj)/cpuflags.o
859e6abd2aSJosh Triplett
863afed06aSJosh Triplettvmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o
879b238748SKees Cookvmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o
883a94707dSKees Cookifdef CONFIG_X86_64
897cc4eb1bSKirill A. Shutemov	vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr_64.o
901958b5fcSTom Lendacky	vmlinux-objs-y += $(obj)/mem_encrypt.o
9108529078SKirill A. Shutemov	vmlinux-objs-y += $(obj)/pgtable_64.o
923a94707dSKees Cookendif
93291f3632SMatt Fleming
943a63f70bSChao Fanvmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
953a63f70bSChao Fan
9696738c69SMatt Flemingvmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
97da05b143SArvind Sankarefi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a
98291f3632SMatt Fleming
9998f78525SKees Cook# The compressed kernel is built with -fPIC/-fPIE so that a boot loader
10098f78525SKees Cook# can place it anywhere in memory and it will still run. However, since
10198f78525SKees Cook# it is executed as-is without any ELF relocation processing performed
10298f78525SKees Cook# (and has already had all relocation sections stripped from the binary),
10398f78525SKees Cook# none of the code can use data relocations (e.g. static assignments of
10498f78525SKees Cook# pointer values), since they will be meaningless at runtime. This check
10598f78525SKees Cook# will refuse to link the vmlinux if any of these relocations are found.
10698f78525SKees Cookquiet_cmd_check_data_rel = DATAREL $@
10798f78525SKees Cookdefine cmd_check_data_rel
10898f78525SKees Cook	for obj in $(filter %.o,$^); do \
109eefb8c12SDmitry Golovin		$(READELF) -S $$obj | grep -qF .rel.local && { \
11098f78525SKees Cook			echo "error: $$obj has data relocations!" >&2; \
11198f78525SKees Cook			exit 1; \
11298f78525SKees Cook		} || true; \
11398f78525SKees Cook	done
11498f78525SKees Cookendef
11598f78525SKees Cook
11692a47286SKees Cook# We need to run two commands under "if_changed", so merge them into a
11792a47286SKees Cook# single invocation.
11892a47286SKees Cookquiet_cmd_check-and-link-vmlinux = LD      $@
11992a47286SKees Cook      cmd_check-and-link-vmlinux = $(cmd_check_data_rel); $(cmd_ld)
12092a47286SKees Cook
121da05b143SArvind Sankar$(obj)/vmlinux: $(vmlinux-objs-y) $(efi-obj-y) FORCE
12292a47286SKees Cook	$(call if_changed,check-and-link-vmlinux)
123db569afaSSam Ravnborg
124099e1377SIan CampbellOBJCOPYFLAGS_vmlinux.bin :=  -R .comment -S
125db569afaSSam Ravnborg$(obj)/vmlinux.bin: vmlinux FORCE
126db569afaSSam Ravnborg	$(call if_changed,objcopy)
127db569afaSSam Ravnborg
1289a1cb471SJosh Tripletttargets += $(patsubst $(obj)/%,%,$(vmlinux-objs-y)) vmlinux.bin.all vmlinux.relocs
129db569afaSSam Ravnborg
1306520fe55SH. Peter AnvinCMD_RELOCS = arch/x86/tools/relocs
131db569afaSSam Ravnborgquiet_cmd_relocs = RELOCS  $@
1326520fe55SH. Peter Anvin      cmd_relocs = $(CMD_RELOCS) $< > $@;$(CMD_RELOCS) --abs-relocs $<
1336520fe55SH. Peter Anvin$(obj)/vmlinux.relocs: vmlinux FORCE
134db569afaSSam Ravnborg	$(call if_changed,relocs)
135db569afaSSam Ravnborg
136db569afaSSam Ravnborgvmlinux.bin.all-y := $(obj)/vmlinux.bin
1375f11e020SH. Peter Anvinvmlinux.bin.all-$(CONFIG_X86_NEED_RELOCS) += $(obj)/vmlinux.relocs
138db569afaSSam Ravnborg
1395f11e020SH. Peter Anvin$(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
140db569afaSSam Ravnborg	$(call if_changed,gzip)
1415f11e020SH. Peter Anvin$(obj)/vmlinux.bin.bz2: $(vmlinux.bin.all-y) FORCE
142ae03c499SAlain Knaff	$(call if_changed,bzip2)
1435f11e020SH. Peter Anvin$(obj)/vmlinux.bin.lzma: $(vmlinux.bin.all-y) FORCE
144ae03c499SAlain Knaff	$(call if_changed,lzma)
14530314804SLasse Collin$(obj)/vmlinux.bin.xz: $(vmlinux.bin.all-y) FORCE
14630314804SLasse Collin	$(call if_changed,xzkern)
14713510997SAlbin Tonnerre$(obj)/vmlinux.bin.lzo: $(vmlinux.bin.all-y) FORCE
14813510997SAlbin Tonnerre	$(call if_changed,lzo)
149f9b493acSKyungsik Lee$(obj)/vmlinux.bin.lz4: $(vmlinux.bin.all-y) FORCE
150f9b493acSKyungsik Lee	$(call if_changed,lz4)
151fb46d057SNick Terrell$(obj)/vmlinux.bin.zst: $(vmlinux.bin.all-y) FORCE
152fb46d057SNick Terrell	$(call if_changed,zstd22)
153db569afaSSam Ravnborg
154283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_GZIP)	:= gz
155283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_BZIP2)	:= bz2
156283ab1c0SH. Peter Anvinsuffix-$(CONFIG_KERNEL_LZMA)	:= lzma
15730314804SLasse Collinsuffix-$(CONFIG_KERNEL_XZ)	:= xz
15813510997SAlbin Tonnerresuffix-$(CONFIG_KERNEL_LZO) 	:= lzo
159f9b493acSKyungsik Leesuffix-$(CONFIG_KERNEL_LZ4) 	:= lz4
160fb46d057SNick Terrellsuffix-$(CONFIG_KERNEL_ZSTD)	:= zst
161ae03c499SAlain Knaff
16202a884c0SH. Peter Anvinquiet_cmd_mkpiggy = MKPIGGY $@
163172caf19SMasahiro Yamada      cmd_mkpiggy = $(obj)/mkpiggy $< > $@
16402a884c0SH. Peter Anvin
16502a884c0SH. Peter Anvintargets += piggy.S
16602a884c0SH. Peter Anvin$(obj)/piggy.S: $(obj)/vmlinux.bin.$(suffix-y) $(obj)/mkpiggy FORCE
16702a884c0SH. Peter Anvin	$(call if_changed,mkpiggy)
168