1 # SPDX-License-Identifier: GPL-2.0-only
6 include include/config/auto.conf
7 include $(srctree)/scripts/Kbuild.include
10 include $(srctree)/scripts/Makefile.lib
12 # Generate a linker script to ensure correct ordering of initcalls for Clang LTO
13 # ---------------------------------------------------------------------------
15 quiet_cmd_gen_initcalls_lds = GEN $@
16 cmd_gen_initcalls_lds = \
17 $(PYTHON3) $(srctree)/scripts/jobserver-exec \
18 $(PERL) $(real-prereqs) > $@
20 .tmp_initcalls.lds: $(srctree)/scripts/generate_initcall_order.pl \
21 $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE
22 $(call if_changed,gen_initcalls_lds)
24 targets := .tmp_initcalls.lds
26 ifdef CONFIG_LTO_CLANG
27 initcalls-lds := .tmp_initcalls.lds
30 # objtool for vmlinux.o
31 # ---------------------------------------------------------------------------
33 # For LTO and IBT, objtool doesn't run on individual translation units.
34 # Run everything on vmlinux instead.
36 objtool-enabled := $(or $(delay-objtool),$(CONFIG_NOINSTR_VALIDATION))
38 # Reuse objtool_args defined in scripts/Makefile.lib if LTO or IBT is enabled.
40 # Add some more flags as needed.
41 # --no-unreachable and --link might be added twice, but it is fine.
43 # Expand objtool_args to a simple variable to avoid circular reference.
46 $(if $(delay-objtool),$(objtool_args)) \
47 $(if $(CONFIG_NOINSTR_VALIDATION), --noinstr $(if $(CONFIG_CPU_UNRET_ENTRY), --unret)) \
48 $(if $(CONFIG_GCOV_KERNEL), --no-unreachable) \
51 # Link of vmlinux.o used for section mismatch analysis
52 # ---------------------------------------------------------------------------
54 quiet_cmd_ld_vmlinux.o = LD $@
56 $(LD) ${KBUILD_LDFLAGS} -r -o $@ \
57 $(addprefix -T , $(initcalls-lds)) \
58 --whole-archive $(KBUILD_VMLINUX_OBJS) --no-whole-archive \
59 --start-group $(KBUILD_VMLINUX_LIBS) --end-group \
62 define rule_ld_vmlinux.o
63 $(call cmd_and_savecmd,ld_vmlinux.o)
64 $(call cmd,gen_objtooldep)
67 vmlinux.o: $(initcalls-lds) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE
68 $(call if_changed_rule,ld_vmlinux.o)
72 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
73 # ---------------------------------------------------------------------------
78 # Read all saved command lines and dependencies for the $(targets) we
79 # may be building above, using $(if_changed{,_dep}). As an
80 # optimization, we don't need to read them if the target does not
81 # exist, we will rebuild anyway in that case.
83 existing-targets := $(wildcard $(sort $(targets)))
85 -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)