| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | |
| 3 | PHONY := __default |
| 4 | __default: vmlinux.o modules.builtin.modinfo modules.builtin |
| 5 | |
| 6 | include include/config/auto.conf |
| 7 | include $(srctree)/scripts/Kbuild.include |
| 8 | |
| 9 | # for objtool |
| 10 | include $(srctree)/scripts/Makefile.lib |
| 11 | |
| 12 | # Generate a linker script to ensure correct ordering of initcalls for Clang LTO |
| 13 | # --------------------------------------------------------------------------- |
| 14 | |
| 15 | quiet_cmd_gen_initcalls_lds = GEN $@ |
| 16 | cmd_gen_initcalls_lds = \ |
| 17 | $(PYTHON3) $(srctree)/scripts/jobserver-exec \ |
| 18 | $(PERL) $(real-prereqs) > $@ |
| 19 | |
| 20 | .tmp_initcalls.lds: $(srctree)/scripts/generate_initcall_order.pl \ |
| 21 | vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE |
| 22 | $(call if_changed,gen_initcalls_lds) |
| 23 | |
| 24 | targets := .tmp_initcalls.lds |
| 25 | |
| 26 | ifdef CONFIG_LTO_CLANG |
| 27 | initcalls-lds := .tmp_initcalls.lds |
| 28 | endif |
| 29 | |
| 30 | # objtool for vmlinux.o |
| 31 | # --------------------------------------------------------------------------- |
| 32 | # |
| 33 | # For delay-objtool (IBT or LTO), objtool doesn't run on individual translation |
| 34 | # units. Instead it runs on vmlinux.o. |
| 35 | # |
| 36 | # For !delay-objtool + CONFIG_NOINSTR_VALIDATION, it runs on both translation |
| 37 | # units and vmlinux.o, with the latter only used for noinstr/unret validation. |
| 38 | |
| 39 | objtool-enabled := $(or $(delay-objtool),$(CONFIG_NOINSTR_VALIDATION)) |
| 40 | |
| 41 | ifeq ($(delay-objtool),y) |
| 42 | vmlinux-objtool-args-y += $(objtool-args-y) |
| 43 | else |
| 44 | vmlinux-objtool-args-$(CONFIG_OBJTOOL_WERROR) += --Werror |
| 45 | endif |
| 46 | |
| 47 | vmlinux-objtool-args-$(CONFIG_NOINSTR_VALIDATION) += --noinstr \ |
| 48 | $(if $(or $(CONFIG_MITIGATION_UNRET_ENTRY),$(CONFIG_MITIGATION_SRSO)), --unret) |
| 49 | |
| 50 | objtool-args = $(vmlinux-objtool-args-y) --link |
| 51 | |
| 52 | # Link of vmlinux.o used for section mismatch analysis |
| 53 | # --------------------------------------------------------------------------- |
| 54 | |
| 55 | vmlinux-o-ld-args-$(CONFIG_BUILTIN_MODULE_RANGES) += [email protected] |
| 56 | |
| 57 | quiet_cmd_ld_vmlinux.o = LD $@ |
| 58 | cmd_ld_vmlinux.o = \ |
| 59 | $(LD) ${KBUILD_LDFLAGS} -r -o $@ \ |
| 60 | $(vmlinux-o-ld-args-y) \ |
| 61 | $(addprefix -T , $(initcalls-lds)) \ |
| 62 | --whole-archive vmlinux.a --no-whole-archive \ |
| 63 | --start-group $(KBUILD_VMLINUX_LIBS) --end-group \ |
| 64 | $(cmd_objtool) |
| 65 | |
| 66 | define rule_ld_vmlinux.o |
| 67 | $(call cmd_and_savecmd,ld_vmlinux.o) |
| 68 | $(call cmd,gen_objtooldep) |
| 69 | endef |
| 70 | |
| 71 | vmlinux.o: $(initcalls-lds) vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE |
| 72 | $(call if_changed_rule,ld_vmlinux.o) |
| 73 | |
| 74 | targets += vmlinux.o |
| 75 | |
| 76 | # modules.builtin.modinfo |
| 77 | # --------------------------------------------------------------------------- |
| 78 | |
| 79 | OBJCOPYFLAGS_modules.builtin.modinfo := -j .modinfo -O binary |
| 80 | |
| 81 | targets += modules.builtin.modinfo |
| 82 | modules.builtin.modinfo: vmlinux.o FORCE |
| 83 | $(call if_changed,objcopy) |
| 84 | |
| 85 | # modules.builtin |
| 86 | # --------------------------------------------------------------------------- |
| 87 | |
| 88 | # The second line aids cases where multiple modules share the same object. |
| 89 | |
| 90 | quiet_cmd_modules_builtin = GEN $@ |
| 91 | cmd_modules_builtin = \ |
| 92 | tr '\0' '\n' < $< | \ |
| 93 | sed -n 's/^[[:alnum:]:_]*\.file=//p' | \ |
| 94 | tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$$/.ko/' > $@ |
| 95 | |
| 96 | targets += modules.builtin |
| 97 | modules.builtin: modules.builtin.modinfo FORCE |
| 98 | $(call if_changed,modules_builtin) |
| 99 | |
| 100 | # Add FORCE to the prerequisites of a target to force it to be always rebuilt. |
| 101 | # --------------------------------------------------------------------------- |
| 102 | |
| 103 | PHONY += FORCE |
| 104 | FORCE: |
| 105 | |
| 106 | # Read all saved command lines and dependencies for the $(targets) we |
| 107 | # may be building above, using $(if_changed{,_dep}). As an |
| 108 | # optimization, we don't need to read them if the target does not |
| 109 | # exist, we will rebuild anyway in that case. |
| 110 | |
| 111 | existing-targets := $(wildcard $(sort $(targets))) |
| 112 | |
| 113 | -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) |
| 114 | |
| 115 | .PHONY: $(PHONY) |