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