1 # SPDX-License-Identifier: GPL-2.0
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.
5 modname-multi = $(sort $(foreach m,$(multi-obj-ym),\
6 $(if $(filter $*.o, $(call suffix-search, $m, .o, -objs -y -m)),$(m:.o=))))
8 __modname = $(or $(modname-multi),$(basetarget))
10 modname = $(subst $(space),:,$(__modname))
11 modfile = $(addprefix $(obj)/,$(__modname))
13 # target with $(obj)/ and its suffix stripped
14 target-stem = $(basename $(patsubst $(obj)/%,%,$@))
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)
19 name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
20 name-fix = $(call stringify,$(call name-fix-token,$1))
21 basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
22 modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
23 -D__KBUILD_MODNAME=$(call name-fix-token,$(modname))
24 modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
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)
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
45 ifeq ($(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))), \
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)
55 ifeq ($(CONFIG_KASAN),y)
56 ifneq ($(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)), \
66 ifeq ($(CONFIG_KMSAN),y)
67 _c_flags += $(if $(patsubst n%,, \
68 $(KMSAN_SANITIZE_$(target-stem).o)$(KMSAN_SANITIZE)$(is-kernel-object)), \
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)
75 ifeq ($(CONFIG_UBSAN),y)
76 _c_flags += $(if $(patsubst n%,, \
77 $(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_SANITIZE)$(is-kernel-object)), \
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))
84 ifeq ($(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))), \
88 _rust_flags += $(if $(patsubst n%,, \
89 $(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
94 # Enable KCSAN flags except some files or directories we don't want to check
95 # (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
97 ifeq ($(CONFIG_KCSAN),y)
98 _c_flags += $(if $(patsubst n%,, \
99 $(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
101 # Some uninstrumented files provide implied barriers required to avoid false
102 # positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
103 _c_flags += $(if $(patsubst n%,, \
104 $(KCSAN_INSTRUMENT_BARRIERS_$(target-stem).o)$(KCSAN_INSTRUMENT_BARRIERS)n), \
105 -D__KCSAN_INSTRUMENT_BARRIERS__)
109 # Enable context analysis flags only where explicitly opted in.
110 # (depends on variables CONTEXT_ANALYSIS_obj.o, CONTEXT_ANALYSIS)
112 ifeq ($(CONFIG_WARN_CONTEXT_ANALYSIS),y)
113 _c_flags += $(if $(patsubst n%,, \
114 $(CONTEXT_ANALYSIS_$(target-stem).o)$(CONTEXT_ANALYSIS)$(if $(is-kernel-object),$(CONFIG_WARN_CONTEXT_ANALYSIS_ALL))), \
115 $(CFLAGS_CONTEXT_ANALYSIS))
119 # Enable AutoFDO build flags except some files or directories we don't want to
120 # enable (depends on variables AUTOFDO_PROFILE_obj.o and AUTOFDO_PROFILE).
122 ifeq ($(CONFIG_AUTOFDO_CLANG),y)
123 _c_flags += $(if $(patsubst n%,, \
124 $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
125 $(CFLAGS_AUTOFDO_CLANG))
129 # Enable Propeller build flags except some files or directories we don't want to
130 # enable (depends on variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE).
132 ifdef CONFIG_PROPELLER_CLANG
133 _c_flags += $(if $(patsubst n%,, \
134 $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(PROPELLER_PROFILE))$(is-kernel-object), \
135 $(CFLAGS_PROPELLER_CLANG))
138 # $(src) for including checkin headers from generated source files
139 # $(obj) for including generated headers from checkin source files
140 ifdef building_out_of_srctree
141 _c_flags += $(addprefix -I, $(src) $(obj))
142 _a_flags += $(addprefix -I, $(src) $(obj))
143 _cpp_flags += $(addprefix -I, $(src) $(obj))
146 # If $(is-kernel-object) is 'y', this object will be linked to vmlinux or modules
147 is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
149 part-of-builtin = $(if $(filter $(basename $@).o, $(real-obj-y) $(lib-y)),y)
150 part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
151 quiet_modtag = $(if $(part-of-module),[M], )
154 $(if $(part-of-module), \
155 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
156 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
158 modkern_rustflags = \
159 $(if $(part-of-module), \
160 $(KBUILD_RUSTFLAGS_MODULE) $(RUSTFLAGS_MODULE), \
161 $(KBUILD_RUSTFLAGS_KERNEL) $(RUSTFLAGS_KERNEL))
163 modkern_aflags = $(if $(part-of-module), \
164 $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE), \
165 $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL) $(modfile_flags))
167 c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
168 -include $(srctree)/include/linux/compiler_types.h \
169 $(_c_flags) $(modkern_cflags) \
170 $(basename_flags) $(modname_flags)
172 rust_flags = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg
174 a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
175 $(_a_flags) $(modkern_aflags) $(modname_flags)
177 cpp_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
180 ld_flags = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
184 objtool := $(objtree)/tools/objtool/objtool
186 objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK) += --hacks=jump_label
187 objtool-args-$(CONFIG_HAVE_NOINSTR_HACK) += --hacks=noinstr
188 objtool-args-$(CONFIG_MITIGATION_CALL_DEPTH_TRACKING) += --hacks=skylake
189 objtool-args-$(CONFIG_X86_KERNEL_IBT) += --ibt
190 objtool-args-$(CONFIG_FINEIBT) += --cfi
191 objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL) += --mcount
192 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
193 objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT) += --mnop
195 objtool-args-$(CONFIG_UNWINDER_ORC) += --orc
196 objtool-args-$(CONFIG_MITIGATION_RETPOLINE) += --retpoline
197 objtool-args-$(CONFIG_MITIGATION_RETHUNK) += --rethunk
198 objtool-args-$(CONFIG_MITIGATION_SLS) += --sls
199 objtool-args-$(CONFIG_STACK_VALIDATION) += --stackval
200 objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE) += --static-call
201 objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION) += --uaccess
202 objtool-args-$(or $(CONFIG_GCOV_KERNEL),$(CONFIG_KCOV)) += --no-unreachable
203 objtool-args-$(CONFIG_PREFIX_SYMBOLS) += --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
204 objtool-args-$(CONFIG_OBJTOOL_WERROR) += --werror
206 objtool-args = $(objtool-args-y) \
207 $(if $(delay-objtool), --link) \
208 $(if $(part-of-module), --module)
210 delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT),$(CONFIG_KLP_BUILD))
212 cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
213 cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
217 endif # CONFIG_OBJTOOL
219 # Useful for describing the dependency of composite objects
221 # $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
225 $(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
228 # Remove ".." and "." from a path, without using "realpath"
230 # $(call normalize_path,path/to/../file)
231 define normalize_path
232 $(strip $(eval elements :=) \
233 $(foreach elem,$(subst /, ,$1), \
234 $(if $(filter-out .,$(elem)), \
235 $(if $(filter ..,$(elem)), \
236 $(eval elements := $(wordlist 2,$(words $(elements)),x $(elements))), \
237 $(eval elements := $(elements) $(elem))))) \
238 $(subst $(space),/,$(elements)))
242 # ===========================================================================
243 # These are shared by some Makefile.* files.
245 ifdef CONFIG_LTO_CLANG
246 # Run $(LD) here to convert LLVM IR to ELF in the following cases:
247 # - when this object needs objtool processing, as objtool cannot process LLVM IR
248 # - when this is a single-object module, as modpost cannot process LLVM IR
249 cmd_ld_single = $(if $(objtool-enabled)$(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
252 quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
253 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
258 $(call cmd_and_fixdep,cc_o_c)
261 $(call cmd,gen_objtooldep)
262 $(call cmd,gen_symversions_c)
263 $(call cmd,record_mcount)
264 $(call cmd,warn_shared_object)
267 quiet_cmd_as_o_S = AS $(quiet_modtag) $@
268 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
271 $(call cmd_and_fixdep,as_o_S)
272 $(call cmd,gen_objtooldep)
273 $(call cmd,gen_symversions_S)
274 $(call cmd,warn_shared_object)
278 # ===========================================================================
279 # 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
280 # the copy would be read-only as well, leading to an error when executing the
281 # rule next time. Use 'cat' instead in order to generate a writable file.
282 quiet_cmd_copy = COPY $@
283 cmd_copy = cat $< > $@
285 $(obj)/%: $(src)/%_shipped
289 # ===========================================================================
290 quiet_cmd_touch = TOUCH $(call normalize_path,$@)
293 # Commands useful for building a boot image
294 # ===========================================================================
298 # target: source(s) FORCE
299 # $(if_changed,ld/objcopy/gzip)
301 # and add target to 'targets' so that we know we have to
302 # read in the saved command line
305 # ---------------------------------------------------------------------------
308 cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
311 # ---------------------------------------------------------------------------
314 cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
317 # ---------------------------------------------------------------------------
319 quiet_cmd_objcopy = OBJCOPY $@
320 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
323 # ---------------------------------------------------------------------------
325 quiet_cmd_gzip = GZIP $@
326 cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
329 # ---------------------------------------------------------------------------
331 # Bzip2 and LZMA do not include size in file... so we have to fake that;
332 # append the size as a 32-bit littleendian number as gzip does.
333 size_append = printf $(shell \
335 for F in $(real-prereqs); do \
336 fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \
337 dec_size=$$(expr $$dec_size + $$fsize); \
339 printf "%08x\n" $$dec_size | \
340 sed 's/\(..\)/\1 /g' | { \
341 read ch0 ch1 ch2 ch3; \
342 for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \
343 printf '%s%03o' '\\' $$((0x$$ch)); \
348 quiet_cmd_file_size = GEN $@
349 cmd_file_size = $(size_append) > $@
351 quiet_cmd_bzip2 = BZIP2 $@
352 cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
354 quiet_cmd_bzip2_with_size = BZIP2 $@
355 cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
358 # ---------------------------------------------------------------------------
360 quiet_cmd_lzma = LZMA $@
361 cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
363 quiet_cmd_lzma_with_size = LZMA $@
364 cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
366 quiet_cmd_lzo = LZO $@
367 cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
369 quiet_cmd_lzo_with_size = LZO $@
370 cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
372 quiet_cmd_lz4 = LZ4 $@
373 cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -9 - - > $@
375 quiet_cmd_lz4_with_size = LZ4 $@
376 cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
377 $(size_append); } > $@
380 # ---------------------------------------------------------------------------
382 MKIMAGE := $(srctree)/scripts/mkuboot.sh
384 # SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
385 # the number of overrides in arch makefiles
386 UIMAGE_ARCH ?= $(SRCARCH)
387 UIMAGE_COMPRESSION ?= $(or $(2),none)
389 UIMAGE_TYPE ?= kernel
390 UIMAGE_LOADADDR ?= arch_must_set_this
391 UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
392 UIMAGE_NAME ?= Linux-$(KERNELRELEASE)
394 quiet_cmd_uimage = UIMAGE $@
395 cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
396 -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
398 -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
399 -n '$(UIMAGE_NAME)' -d $< $@
401 # Flat Image Tree (FIT)
402 # This allows for packaging of a kernel and all devicetrees files, using
404 # ---------------------------------------------------------------------------
406 MAKE_FIT := $(srctree)/scripts/make_fit.py
408 # Use this to override the compression algorithm
409 FIT_COMPRESSION ?= gzip
411 quiet_cmd_fit = FIT $@
412 cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
413 --name '$(UIMAGE_NAME)' $(FIT_EXTRA_ARGS) \
414 $(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
415 $(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
416 --compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
419 # ---------------------------------------------------------------------------
420 # Use xzkern or xzkern_with_size to compress the kernel image and xzmisc to
421 # compress other things.
423 # xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
424 # of the kernel decompressor. A BCJ filter is used if it is available for
425 # the target architecture.
427 # xzkern_with_size also appends uncompressed size of the data using
428 # size_append. The .xz format has the size information available at the end
429 # of the file too, but it's in more complex format and it's good to avoid
430 # changing the part of the boot code that reads the uncompressed size.
431 # Note that the bytes added by size_append will make the xz tool think that
432 # the file is corrupt. This is expected.
434 # xzmisc doesn't use size_append, so it can be used to create normal .xz
435 # files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
436 # big dictionary would increase the memory usage too much in the multi-call
437 # decompression mode. A BCJ filter isn't used either.
438 quiet_cmd_xzkern = XZKERN $@
439 cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
441 quiet_cmd_xzkern_with_size = XZKERN $@
442 cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
443 $(size_append); } > $@
445 quiet_cmd_xzmisc = XZMISC $@
446 cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
449 # ---------------------------------------------------------------------------
450 # Appends the uncompressed size of the data using size_append. The .zst
451 # format has the size information available at the beginning of the file too,
452 # but it's in a more complex format and it's good to avoid changing the part
453 # of the boot code that reads the uncompressed size.
455 # Note that the bytes added by size_append will make the zstd tool think that
456 # the file is corrupt. This is expected.
458 # zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
459 # 128 MB. zstd22 is used for kernel compression because it is decompressed in a
460 # single pass, so zstd doesn't need to allocate a window buffer. When streaming
461 # decompression is used, like initramfs decompression, zstd22 should likely not
462 # be used because it would require zstd to allocate a 128 MB buffer.
464 quiet_cmd_zstd = ZSTD $@
465 cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
467 quiet_cmd_zstd22 = ZSTD22 $@
468 cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
470 quiet_cmd_zstd22_with_size = ZSTD22 $@
471 cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
474 # ---------------------------------------------------------------------------
476 # Default sed regexp - multiline due to syntax constraints
478 # Use [:space:] because LLVM's integrated assembler inserts <tab> around
479 # the .ascii directive whereas GCC keeps the <space> as-is.
481 's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
482 /^->/{s:->#\(.*\):/* \1 */:; \
483 s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
487 # Use filechk to avoid rebuilds when a header changes, but the resulting file
489 define filechk_offsets
493 echo " * DO NOT MODIFY."; \
495 echo " * This file was generated by Kbuild"; \
498 sed -ne $(sed-offsets) < $<; \