xref: /linux/scripts/Makefile.lib (revision c50a04f8f45c7f13972f9097622d1d929033ea8c)
1# SPDX-License-Identifier: GPL-2.0
2
3# Finds the multi-part object the current object will be linked into.
4# If the object belongs to two or more multi-part objects, list them all.
5modname-multi = $(sort $(foreach m,$(multi-obj-ym),\
6		$(if $(filter $*.o, $(call suffix-search, $m, .o, -objs -y -m)),$(m:.o=))))
7
8__modname = $(or $(modname-multi),$(basetarget))
9
10modname = $(subst $(space),:,$(__modname))
11modfile = $(addprefix $(obj)/,$(__modname))
12
13# target with $(obj)/ and its suffix stripped
14target-stem = $(basename $(patsubst $(obj)/%,%,$@))
15
16# These flags are needed for modversions and compiling, so we define them here
17# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
18# end up in (or would, if it gets compiled in)
19name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
20name-fix = $(call stringify,$(call name-fix-token,$1))
21basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
22modname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
23		 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
24modfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
25
26_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
27                     $(filter-out $(ccflags-remove-y), \
28                         $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
29                     $(CFLAGS_$(target-stem).o))
30_rust_flags    = $(filter-out $(RUSTFLAGS_REMOVE_$(target-stem).o), \
31                     $(filter-out $(rustflags-remove-y), \
32                         $(KBUILD_RUSTFLAGS) $(rustflags-y)) \
33                     $(RUSTFLAGS_$(target-stem).o))
34_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
35                     $(filter-out $(asflags-remove-y), \
36                         $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
37                     $(AFLAGS_$(target-stem).o))
38_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
39
40#
41# Enable gcov profiling flags for a file, directory or for all files depending
42# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
43# (in this order)
44#
45ifeq ($(CONFIG_GCOV_KERNEL),y)
46_c_flags += $(if $(patsubst n%,, \
47		$(GCOV_PROFILE_$(target-stem).o)$(GCOV_PROFILE)$(if $(is-kernel-object),$(CONFIG_GCOV_PROFILE_ALL))), \
48		$(CFLAGS_GCOV))
49endif
50
51#
52# Enable address sanitizer flags for kernel except some files or directories
53# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
54#
55ifeq ($(CONFIG_KASAN),y)
56ifneq ($(CONFIG_KASAN_HW_TAGS),y)
57_c_flags += $(if $(patsubst n%,, \
58		$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
59		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
60_rust_flags += $(if $(patsubst n%,, \
61		$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
62		$(RUSTFLAGS_KASAN))
63endif
64endif
65
66ifeq ($(CONFIG_KMSAN),y)
67_c_flags += $(if $(patsubst n%,, \
68		$(KMSAN_SANITIZE_$(target-stem).o)$(KMSAN_SANITIZE)$(is-kernel-object)), \
69		$(CFLAGS_KMSAN))
70_c_flags += $(if $(patsubst n%,, \
71		$(KMSAN_ENABLE_CHECKS_$(target-stem).o)$(KMSAN_ENABLE_CHECKS)$(is-kernel-object)), \
72		, -mllvm -msan-disable-checks=1)
73endif
74
75ifeq ($(CONFIG_UBSAN),y)
76_c_flags += $(if $(patsubst n%,, \
77		$(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_SANITIZE)$(is-kernel-object)), \
78		$(CFLAGS_UBSAN))
79_c_flags += $(if $(patsubst n%,, \
80		$(UBSAN_INTEGER_WRAP_$(target-stem).o)$(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_INTEGER_WRAP)$(UBSAN_SANITIZE)$(is-kernel-object)), \
81		$(CFLAGS_UBSAN_INTEGER_WRAP))
82endif
83
84ifeq ($(CONFIG_KCOV),y)
85_c_flags += $(if $(patsubst n%,, \
86	$(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
87	$(CFLAGS_KCOV))
88endif
89
90#
91# Enable KCSAN flags except some files or directories we don't want to check
92# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
93#
94ifeq ($(CONFIG_KCSAN),y)
95_c_flags += $(if $(patsubst n%,, \
96	$(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
97	$(CFLAGS_KCSAN))
98# Some uninstrumented files provide implied barriers required to avoid false
99# positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
100_c_flags += $(if $(patsubst n%,, \
101	$(KCSAN_INSTRUMENT_BARRIERS_$(target-stem).o)$(KCSAN_INSTRUMENT_BARRIERS)n), \
102	-D__KCSAN_INSTRUMENT_BARRIERS__)
103endif
104
105#
106# Enable AutoFDO build flags except some files or directories we don't want to
107# enable (depends on variables AUTOFDO_PROFILE_obj.o and AUTOFDO_PROFILE).
108#
109ifeq ($(CONFIG_AUTOFDO_CLANG),y)
110_c_flags += $(if $(patsubst n%,, \
111	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
112	$(CFLAGS_AUTOFDO_CLANG))
113endif
114
115#
116# Enable Propeller build flags except some files or directories we don't want to
117# enable (depends on variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE).
118#
119ifdef CONFIG_PROPELLER_CLANG
120_c_flags += $(if $(patsubst n%,, \
121	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(PROPELLER_PROFILE))$(is-kernel-object), \
122	$(CFLAGS_PROPELLER_CLANG))
123endif
124
125# $(src) for including checkin headers from generated source files
126# $(obj) for including generated headers from checkin source files
127ifdef building_out_of_srctree
128_c_flags   += $(addprefix -I, $(src) $(obj))
129_a_flags   += $(addprefix -I, $(src) $(obj))
130_cpp_flags += $(addprefix -I, $(src) $(obj))
131endif
132
133# If $(is-kernel-object) is 'y', this object will be linked to vmlinux or modules
134is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
135
136part-of-builtin = $(if $(filter $(basename $@).o, $(real-obj-y) $(lib-y)),y)
137part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
138quiet_modtag = $(if $(part-of-module),[M],   )
139
140modkern_cflags =                                          \
141	$(if $(part-of-module),                           \
142		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
143		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
144
145modkern_rustflags =                                              \
146	$(if $(part-of-module),                                   \
147		$(KBUILD_RUSTFLAGS_MODULE) $(RUSTFLAGS_MODULE), \
148		$(KBUILD_RUSTFLAGS_KERNEL) $(RUSTFLAGS_KERNEL))
149
150modkern_aflags = $(if $(part-of-module),				\
151			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
152			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL) $(modfile_flags))
153
154c_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
155		 -include $(srctree)/include/linux/compiler_types.h       \
156		 $(_c_flags) $(modkern_cflags)                           \
157		 $(basename_flags) $(modname_flags)
158
159rust_flags     = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg
160
161a_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
162		 $(_a_flags) $(modkern_aflags) $(modname_flags)
163
164cpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
165		 $(_cpp_flags)
166
167ld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
168
169ifdef CONFIG_OBJTOOL
170
171objtool := $(objtree)/tools/objtool/objtool
172
173objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK)		+= --hacks=jump_label
174objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)		+= --hacks=noinstr
175objtool-args-$(CONFIG_MITIGATION_CALL_DEPTH_TRACKING)	+= --hacks=skylake
176objtool-args-$(CONFIG_X86_KERNEL_IBT)			+= --ibt
177objtool-args-$(CONFIG_FINEIBT)				+= --cfi
178objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)	+= --mcount
179ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
180objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT)		+= --mnop
181endif
182objtool-args-$(CONFIG_UNWINDER_ORC)			+= --orc
183objtool-args-$(CONFIG_MITIGATION_RETPOLINE)		+= --retpoline
184objtool-args-$(CONFIG_MITIGATION_RETHUNK)		+= --rethunk
185objtool-args-$(CONFIG_MITIGATION_SLS)			+= --sls
186objtool-args-$(CONFIG_STACK_VALIDATION)			+= --stackval
187objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE)		+= --static-call
188objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION)		+= --uaccess
189objtool-args-$(or $(CONFIG_GCOV_KERNEL),$(CONFIG_KCOV))	+= --no-unreachable
190objtool-args-$(CONFIG_PREFIX_SYMBOLS)			+= --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
191objtool-args-$(CONFIG_OBJTOOL_WERROR)			+= --Werror
192
193objtool-args = $(objtool-args-y)					\
194	$(if $(delay-objtool), --link)					\
195	$(if $(part-of-module), --module)
196
197delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
198
199cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
200cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
201
202objtool-enabled := y
203
204endif # CONFIG_OBJTOOL
205
206# Useful for describing the dependency of composite objects
207# Usage:
208#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
209define multi_depend
210$(foreach m, $1, \
211	$(eval $m: \
212	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
213endef
214
215# Build commands
216# ===========================================================================
217# These are shared by some Makefile.* files.
218
219ifdef CONFIG_LTO_CLANG
220# Run $(LD) here to convert LLVM IR to ELF in the following cases:
221#  - when this object needs objtool processing, as objtool cannot process LLVM IR
222#  - when this is a single-object module, as modpost cannot process LLVM IR
223cmd_ld_single = $(if $(objtool-enabled)$(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
224endif
225
226quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
227      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
228		$(cmd_ld_single) \
229		$(cmd_objtool)
230
231define rule_cc_o_c
232	$(call cmd_and_fixdep,cc_o_c)
233	$(call cmd,checksrc)
234	$(call cmd,checkdoc)
235	$(call cmd,gen_objtooldep)
236	$(call cmd,gen_symversions_c)
237	$(call cmd,record_mcount)
238	$(call cmd,warn_shared_object)
239endef
240
241quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
242      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
243
244define rule_as_o_S
245	$(call cmd_and_fixdep,as_o_S)
246	$(call cmd,gen_objtooldep)
247	$(call cmd,gen_symversions_S)
248	$(call cmd,warn_shared_object)
249endef
250
251# Copy a file
252# ===========================================================================
253# 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
254# the copy would be read-only as well, leading to an error when executing the
255# rule next time. Use 'cat' instead in order to generate a writable file.
256quiet_cmd_copy = COPY    $@
257      cmd_copy = cat $< > $@
258
259$(obj)/%: $(src)/%_shipped
260	$(call cmd,copy)
261
262# Commands useful for building a boot image
263# ===========================================================================
264#
265#	Use as following:
266#
267#	target: source(s) FORCE
268#		$(if_changed,ld/objcopy/gzip)
269#
270#	and add target to 'targets' so that we know we have to
271#	read in the saved command line
272
273# Linking
274# ---------------------------------------------------------------------------
275
276quiet_cmd_ld = LD      $@
277      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
278
279# Archive
280# ---------------------------------------------------------------------------
281
282quiet_cmd_ar = AR      $@
283      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
284
285# Objcopy
286# ---------------------------------------------------------------------------
287
288quiet_cmd_objcopy = OBJCOPY $@
289cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
290
291# Gzip
292# ---------------------------------------------------------------------------
293
294quiet_cmd_gzip = GZIP    $@
295      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
296
297# Bzip2
298# ---------------------------------------------------------------------------
299
300# Bzip2 and LZMA do not include size in file... so we have to fake that;
301# append the size as a 32-bit littleendian number as gzip does.
302size_append = printf $(shell						\
303dec_size=0;								\
304for F in $(real-prereqs); do					\
305	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
306	dec_size=$$(expr $$dec_size + $$fsize);				\
307done;									\
308printf "%08x\n" $$dec_size |						\
309	sed 's/\(..\)/\1 /g' | {					\
310		read ch0 ch1 ch2 ch3;					\
311		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
312			printf '%s%03o' '\\' $$((0x$$ch)); 		\
313		done;							\
314	}								\
315)
316
317quiet_cmd_file_size = GEN     $@
318      cmd_file_size = $(size_append) > $@
319
320quiet_cmd_bzip2 = BZIP2   $@
321      cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
322
323quiet_cmd_bzip2_with_size = BZIP2   $@
324      cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
325
326# Lzma
327# ---------------------------------------------------------------------------
328
329quiet_cmd_lzma = LZMA    $@
330      cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
331
332quiet_cmd_lzma_with_size = LZMA    $@
333      cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
334
335quiet_cmd_lzo = LZO     $@
336      cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
337
338quiet_cmd_lzo_with_size = LZO     $@
339      cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
340
341quiet_cmd_lz4 = LZ4     $@
342      cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -9 - - > $@
343
344quiet_cmd_lz4_with_size = LZ4     $@
345      cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
346                  $(size_append); } > $@
347
348# U-Boot mkimage
349# ---------------------------------------------------------------------------
350
351MKIMAGE := $(srctree)/scripts/mkuboot.sh
352
353# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
354# the number of overrides in arch makefiles
355UIMAGE_ARCH ?= $(SRCARCH)
356UIMAGE_COMPRESSION ?= $(or $(2),none)
357UIMAGE_OPTS-y ?=
358UIMAGE_TYPE ?= kernel
359UIMAGE_LOADADDR ?= arch_must_set_this
360UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
361UIMAGE_NAME ?= Linux-$(KERNELRELEASE)
362
363quiet_cmd_uimage = UIMAGE  $@
364      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
365			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
366			-T $(UIMAGE_TYPE) \
367			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
368			-n '$(UIMAGE_NAME)' -d $< $@
369
370# Flat Image Tree (FIT)
371# This allows for packaging of a kernel and all devicetrees files, using
372# compression.
373# ---------------------------------------------------------------------------
374
375MAKE_FIT := $(srctree)/scripts/make_fit.py
376
377# Use this to override the compression algorithm
378FIT_COMPRESSION ?= gzip
379
380quiet_cmd_fit = FIT     $@
381      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
382		--name '$(UIMAGE_NAME)' \
383		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
384		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
385		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
386
387# XZ
388# ---------------------------------------------------------------------------
389# Use xzkern or xzkern_with_size to compress the kernel image and xzmisc to
390# compress other things.
391#
392# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
393# of the kernel decompressor. A BCJ filter is used if it is available for
394# the target architecture.
395#
396# xzkern_with_size also appends uncompressed size of the data using
397# size_append. The .xz format has the size information available at the end
398# of the file too, but it's in more complex format and it's good to avoid
399# changing the part of the boot code that reads the uncompressed size.
400# Note that the bytes added by size_append will make the xz tool think that
401# the file is corrupt. This is expected.
402#
403# xzmisc doesn't use size_append, so it can be used to create normal .xz
404# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
405# big dictionary would increase the memory usage too much in the multi-call
406# decompression mode. A BCJ filter isn't used either.
407quiet_cmd_xzkern = XZKERN  $@
408      cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
409
410quiet_cmd_xzkern_with_size = XZKERN  $@
411      cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
412                     $(size_append); } > $@
413
414quiet_cmd_xzmisc = XZMISC  $@
415      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
416
417# ZSTD
418# ---------------------------------------------------------------------------
419# Appends the uncompressed size of the data using size_append. The .zst
420# format has the size information available at the beginning of the file too,
421# but it's in a more complex format and it's good to avoid changing the part
422# of the boot code that reads the uncompressed size.
423#
424# Note that the bytes added by size_append will make the zstd tool think that
425# the file is corrupt. This is expected.
426#
427# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
428# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
429# single pass, so zstd doesn't need to allocate a window buffer. When streaming
430# decompression is used, like initramfs decompression, zstd22 should likely not
431# be used because it would require zstd to allocate a 128 MB buffer.
432
433quiet_cmd_zstd = ZSTD    $@
434      cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
435
436quiet_cmd_zstd22 = ZSTD22  $@
437      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
438
439quiet_cmd_zstd22_with_size = ZSTD22  $@
440      cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
441
442# ASM offsets
443# ---------------------------------------------------------------------------
444
445# Default sed regexp - multiline due to syntax constraints
446#
447# Use [:space:] because LLVM's integrated assembler inserts <tab> around
448# the .ascii directive whereas GCC keeps the <space> as-is.
449define sed-offsets
450	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
451	/^->/{s:->#\(.*\):/* \1 */:; \
452	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
453	s:->::; p;}'
454endef
455
456# Use filechk to avoid rebuilds when a header changes, but the resulting file
457# does not
458define filechk_offsets
459	 echo "#ifndef $2"; \
460	 echo "#define $2"; \
461	 echo "/*"; \
462	 echo " * DO NOT MODIFY."; \
463	 echo " *"; \
464	 echo " * This file was generated by Kbuild"; \
465	 echo " */"; \
466	 echo ""; \
467	 sed -ne $(sed-offsets) < $<; \
468	 echo ""; \
469	 echo "#endif"
470endef
471