1# SPDX-License-Identifier: GPL-2.0-only 2# =========================================================================== 3# Module final link 4# =========================================================================== 5 6PHONY := __modfinal 7__modfinal: 8 9include $(objtree)/include/config/auto.conf 10include $(srctree)/scripts/Kbuild.include 11 12# for c_flags and objtool_args 13include $(srctree)/scripts/Makefile.lib 14 15# find all modules listed in modules.order 16modules := $(sort $(shell cat $(MODORDER))) 17 18__modfinal: $(modules) 19 @: 20 21# modname and part-of-module are set to make c_flags define proper module flags 22modname = $(notdir $(@:.mod.o=)) 23part-of-module = y 24 25quiet_cmd_cc_o_c = CC [M] $@ 26 cmd_cc_o_c = \ 27 $(CC) $(filter-out $(CC_FLAGS_CFI) $(CC_FLAGS_FTRACE), \ 28 $(c_flags)) -c -o $@ $< 29 30%.mod.o: %.mod.c FORCE 31 $(call if_changed_dep,cc_o_c) 32 33ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) 34 35ifdef CONFIG_LTO_CLANG 36# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to 37# avoid a second slow LTO link 38prelink-ext := .lto 39 40# ELF processing was skipped earlier because we didn't have native code, 41# so let's now process the prelinked binary before we link the module. 42 43ifdef CONFIG_STACK_VALIDATION 44ifneq ($(SKIP_STACK_VALIDATION),1) 45cmd_ld_ko_o += \ 46 $(objtree)/tools/objtool/objtool $(objtool_args) \ 47 $(@:.ko=$(prelink-ext).o); 48 49endif # SKIP_STACK_VALIDATION 50endif # CONFIG_STACK_VALIDATION 51 52endif # CONFIG_LTO_CLANG 53 54quiet_cmd_ld_ko_o = LD [M] $@ 55 cmd_ld_ko_o += \ 56 $(LD) -r $(KBUILD_LDFLAGS) \ 57 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 58 -T scripts/module.lds -o $@ $(filter %.o, $^); \ 59 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) 60 61ifdef CONFIG_CFI_CLANG 62# LLVM can drops jump table symbols from the final binary. Add them 63# back to make stack traces and other symbol output readable. 64cmd_ld_ko_o += ; \ 65 $(srctree)/scripts/generate_cfi_kallsyms.pl --module \ 66 $@ > $(@:.ko=.lds); \ 67 if [ -s $(@:.ko=.lds) ]; then \ 68 $(LD) -r $(KBUILD_LDFLAGS) \ 69 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 70 -T $(@:.ko=.lds) \ 71 -o $(@:.ko=.tmp.ko) $@; \ 72 mv -f $(@:.ko=.tmp.ko) $@; \ 73 else \ 74 rm -f $(@:.ko=.lds); \ 75 fi 76endif 77 78$(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds FORCE 79 +$(call if_changed,ld_ko_o) 80 81targets += $(modules) $(modules:.ko=.mod.o) 82 83# Add FORCE to the prequisites of a target to force it to be always rebuilt. 84# --------------------------------------------------------------------------- 85 86PHONY += FORCE 87FORCE: 88 89# Read all saved command lines and dependencies for the $(targets) we 90# may be building above, using $(if_changed{,_dep}). As an 91# optimization, we don't need to read them if the target does not 92# exist, we will rebuild anyway in that case. 93 94existing-targets := $(wildcard $(sort $(targets))) 95 96-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 97 98.PHONY: $(PHONY) 99