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