1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun# =========================================================================== 3*4882a593Smuzhiyun# Module versions 4*4882a593Smuzhiyun# =========================================================================== 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# Stage one of module building created the following: 7*4882a593Smuzhiyun# a) The individual .o files used for the module 8*4882a593Smuzhiyun# b) A <module>.o file which is the .o files above linked together 9*4882a593Smuzhiyun# c) A <module>.mod file in $(MODVERDIR)/, listing the name of the 10*4882a593Smuzhiyun# the preliminary <module>.o file, plus all .o files 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun# Stage 2 is handled by this file and does the following 13*4882a593Smuzhiyun# 1) Find all modules from the files listed in $(MODVERDIR)/ 14*4882a593Smuzhiyun# 2) modpost is then used to 15*4882a593Smuzhiyun# 3) create one <module>.mod.c file pr. module 16*4882a593Smuzhiyun# 4) create one Module.symvers file with CRC for all exported symbols 17*4882a593Smuzhiyun# 5) compile all <module>.mod.c files 18*4882a593Smuzhiyun# 6) final link of the module to a <module.ko> file 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun# Step 3 is used to place certain information in the module's ELF 21*4882a593Smuzhiyun# section, including information such as: 22*4882a593Smuzhiyun# Version magic (see include/linux/vermagic.h for full details) 23*4882a593Smuzhiyun# - Kernel release 24*4882a593Smuzhiyun# - SMP is CONFIG_SMP 25*4882a593Smuzhiyun# - PREEMPT is CONFIG_PREEMPT 26*4882a593Smuzhiyun# - GCC Version 27*4882a593Smuzhiyun# Module info 28*4882a593Smuzhiyun# - Module version (MODULE_VERSION) 29*4882a593Smuzhiyun# - Module alias'es (MODULE_ALIAS) 30*4882a593Smuzhiyun# - Module license (MODULE_LICENSE) 31*4882a593Smuzhiyun# - See include/linux/module.h for more details 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun# Step 4 is solely used to allow module versioning in external modules, 34*4882a593Smuzhiyun# where the CRC of each module is retrieved from the Module.symvers file. 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined 37*4882a593Smuzhiyun# symbols in the final module linking stage 38*4882a593Smuzhiyun# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules. 39*4882a593Smuzhiyun# This is solely useful to speed up test compiles 40*4882a593SmuzhiyunPHONY := _modpost 41*4882a593Smuzhiyun_modpost: __modpost 42*4882a593Smuzhiyun 43*4882a593Smuzhiyuninclude include/config/auto.conf 44*4882a593Smuzhiyuninclude scripts/Kbuild.include 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun# When building external modules load the Kbuild file to retrieve EXTRA_SYMBOLS info 47*4882a593Smuzhiyunifneq ($(KBUILD_EXTMOD),) 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun# set src + obj - they may be used when building the .mod.c file 50*4882a593Smuzhiyunobj := $(KBUILD_EXTMOD) 51*4882a593Smuzhiyunsrc := $(obj) 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS 54*4882a593Smuzhiyuninclude $(if $(wildcard $(KBUILD_EXTMOD)/Kbuild), \ 55*4882a593Smuzhiyun $(KBUILD_EXTMOD)/Kbuild, $(KBUILD_EXTMOD)/Makefile) 56*4882a593Smuzhiyunendif 57*4882a593Smuzhiyun 58*4882a593Smuzhiyuninclude scripts/Makefile.lib 59*4882a593Smuzhiyun 60*4882a593Smuzhiyunkernelsymfile := $(objtree)/Module.symvers 61*4882a593Smuzhiyunmodulesymfile := $(firstword $(KBUILD_EXTMOD))/Module.symvers 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun# Step 1), find all modules listed in $(MODVERDIR)/ 64*4882a593SmuzhiyunMODLISTCMD := find $(MODVERDIR) -name '*.mod' | xargs -r grep -h '\.ko$$' | sort -u 65*4882a593Smuzhiyun__modules := $(shell $(MODLISTCMD)) 66*4882a593Smuzhiyunmodules := $(patsubst %.o,%.ko, $(wildcard $(__modules:.ko=.o))) 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun# Stop after building .o files if NOFINAL is set. Makes compile tests quicker 69*4882a593Smuzhiyun_modpost: $(if $(KBUILD_MODPOST_NOFINAL), $(modules:.ko:.o),$(modules)) 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun# Step 2), invoke modpost 72*4882a593Smuzhiyun# Includes step 3,4 73*4882a593Smuzhiyunmodpost = scripts/mod/modpost \ 74*4882a593Smuzhiyun $(if $(CONFIG_MODVERSIONS),-m) \ 75*4882a593Smuzhiyun $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a,) \ 76*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-i,-o) $(kernelsymfile) \ 77*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-I $(modulesymfile)) \ 78*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS))) \ 79*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \ 80*4882a593Smuzhiyun $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S) \ 81*4882a593Smuzhiyun $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ 82*4882a593Smuzhiyun $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunMODPOST_OPT=$(subst -i,-n,$(filter -i,$(MAKEFLAGS))) 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun# If CONFIG_LTO_CLANG is enabled, .o files are either LLVM IR, or empty, so we 87*4882a593Smuzhiyun# need to link them into actual objects before passing them to modpost 88*4882a593Smuzhiyunmodpost-ext = $(if $(CONFIG_LTO_CLANG),.lto,) 89*4882a593Smuzhiyun 90*4882a593Smuzhiyunifdef CONFIG_LTO_CLANG 91*4882a593Smuzhiyunquiet_cmd_cc_lto_link_modules = LD [M] $@ 92*4882a593Smuzhiyuncmd_cc_lto_link_modules = \ 93*4882a593Smuzhiyun $(LD) $(ld_flags) -r -o $(@) \ 94*4882a593Smuzhiyun $(shell [ -s $(@:$(modpost-ext).o=.o.symversions) ] && \ 95*4882a593Smuzhiyun echo -T $(@:$(modpost-ext).o=.o.symversions)) \ 96*4882a593Smuzhiyun --whole-archive $(filter-out FORCE,$^) 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun$(modules:.ko=$(modpost-ext).o): %$(modpost-ext).o: %.o FORCE 99*4882a593Smuzhiyun $(call if_changed,cc_lto_link_modules) 100*4882a593Smuzhiyunendif 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun# We can go over command line length here, so be careful. 103*4882a593Smuzhiyunquiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules 104*4882a593Smuzhiyun cmd_modpost = $(MODLISTCMD) | sed 's/\.ko$$/$(modpost-ext)\.o/' | $(modpost) $(MODPOST_OPT) -s -T - 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunPHONY += __modpost 107*4882a593Smuzhiyun__modpost: $(modules:.ko=$(modpost-ext).o) FORCE 108*4882a593Smuzhiyun $(call cmd,modpost) $(wildcard vmlinux) 109*4882a593Smuzhiyun 110*4882a593Smuzhiyunquiet_cmd_kernel-mod = MODPOST $@ 111*4882a593Smuzhiyun cmd_kernel-mod = $(modpost) $@ 112*4882a593Smuzhiyun 113*4882a593Smuzhiyunvmlinux.o: FORCE 114*4882a593Smuzhiyun $(call cmd,kernel-mod) 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun# Declare generated files as targets for modpost 117*4882a593Smuzhiyun$(modules:.ko=.mod.c): __modpost ; 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun# Step 5), compile all *.mod.c files 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun# modname is set to make c_flags define KBUILD_MODNAME 122*4882a593Smuzhiyunmodname = $(notdir $(@:.mod.o=)) 123*4882a593Smuzhiyun 124*4882a593Smuzhiyunquiet_cmd_cc_o_c = CC $@ 125*4882a593Smuzhiyun cmd_cc_o_c = $(CC) $(c_flags) $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE) \ 126*4882a593Smuzhiyun -c -o $@ $< 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun$(modules:.ko=.mod.o): %.mod.o: %.mod.c FORCE 129*4882a593Smuzhiyun $(call if_changed_dep,cc_o_c) 130*4882a593Smuzhiyun 131*4882a593Smuzhiyuntargets += $(modules:.ko=.mod.o) 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun# Step 6), final link of the modules with optional arch pass after final link 136*4882a593Smuzhiyunquiet_cmd_ld_ko_o = LD [M] $@ 137*4882a593Smuzhiyun 138*4882a593Smuzhiyunifdef CONFIG_LTO_CLANG 139*4882a593Smuzhiyun cmd_ld_ko_o = \ 140*4882a593Smuzhiyun $(LD) -r $(LDFLAGS) \ 141*4882a593Smuzhiyun $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 142*4882a593Smuzhiyun $(shell [ -s $(@:.ko=.o.symversions) ] && \ 143*4882a593Smuzhiyun echo -T $(@:.ko=.o.symversions)) \ 144*4882a593Smuzhiyun -o $@ --whole-archive \ 145*4882a593Smuzhiyun $(filter-out FORCE,$(^:$(modpost-ext).o=.o)) 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun ifdef CONFIG_FTRACE_MCOUNT_RECORD 148*4882a593Smuzhiyun cmd_ld_ko_o += ; $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) $@ 149*4882a593Smuzhiyun endif 150*4882a593Smuzhiyunelse 151*4882a593Smuzhiyun cmd_ld_ko_o = \ 152*4882a593Smuzhiyun $(LD) -r $(KBUILD_LDFLAGS) \ 153*4882a593Smuzhiyun $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ 154*4882a593Smuzhiyun -o $@ $(filter-out FORCE,$^) ; \ 155*4882a593Smuzhiyun $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) 156*4882a593Smuzhiyunendif 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun$(modules): %.ko: %$(modpost-ext).o %.mod.o FORCE 159*4882a593Smuzhiyun +$(call if_changed,ld_ko_o) 160*4882a593Smuzhiyun 161*4882a593Smuzhiyuntargets += $(modules) 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun# Add FORCE to the prequisites of a target to force it to be always rebuilt. 165*4882a593Smuzhiyun# --------------------------------------------------------------------------- 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunPHONY += FORCE 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunFORCE: 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun# Read all saved command lines and dependencies for the $(targets) we 172*4882a593Smuzhiyun# may be building above, using $(if_changed{,_dep}). As an 173*4882a593Smuzhiyun# optimization, we don't need to read them if the target does not 174*4882a593Smuzhiyun# exist, we will rebuild anyway in that case. 175*4882a593Smuzhiyun 176*4882a593Smuzhiyuncmd_files := $(wildcard $(foreach f,$(sort $(targets)),$(dir $(f)).$(notdir $(f)).cmd)) 177*4882a593Smuzhiyun 178*4882a593Smuzhiyunifneq ($(cmd_files),) 179*4882a593Smuzhiyun include $(cmd_files) 180*4882a593Smuzhiyunendif 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun.PHONY: $(PHONY) 183