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, listing the name of the preliminary <module>.o file,
10*4882a593Smuzhiyun#    plus all .o files
11*4882a593Smuzhiyun# d) modules.order, which lists all the modules
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun# Stage 2 is handled by this file and does the following
14*4882a593Smuzhiyun# 1) Find all modules listed in modules.order
15*4882a593Smuzhiyun# 2) modpost is then used to
16*4882a593Smuzhiyun# 3)  create one <module>.mod.c file pr. module
17*4882a593Smuzhiyun# 4)  create one Module.symvers file with CRC for all exported symbols
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# Step 3 is used to place certain information in the module's ELF
20*4882a593Smuzhiyun# section, including information such as:
21*4882a593Smuzhiyun#   Version magic (see include/linux/vermagic.h for full details)
22*4882a593Smuzhiyun#     - Kernel release
23*4882a593Smuzhiyun#     - SMP is CONFIG_SMP
24*4882a593Smuzhiyun#     - PREEMPT is CONFIG_PREEMPT[_RT]
25*4882a593Smuzhiyun#     - GCC Version
26*4882a593Smuzhiyun#   Module info
27*4882a593Smuzhiyun#     - Module version (MODULE_VERSION)
28*4882a593Smuzhiyun#     - Module alias'es (MODULE_ALIAS)
29*4882a593Smuzhiyun#     - Module license (MODULE_LICENSE)
30*4882a593Smuzhiyun#     - See include/linux/module.h for more details
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun# Step 4 is solely used to allow module versioning in external modules,
33*4882a593Smuzhiyun# where the CRC of each module is retrieved from the Module.symvers file.
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined
36*4882a593Smuzhiyun# symbols in the final module linking stage
37*4882a593Smuzhiyun# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
38*4882a593Smuzhiyun# This is solely useful to speed up test compiles
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunPHONY := __modpost
41*4882a593Smuzhiyun__modpost:
42*4882a593Smuzhiyun
43*4882a593Smuzhiyuninclude include/config/auto.conf
44*4882a593Smuzhiyuninclude scripts/Kbuild.include
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun# for ld_flags
47*4882a593Smuzhiyuninclude scripts/Makefile.lib
48*4882a593Smuzhiyun
49*4882a593Smuzhiyunmixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/)
50*4882a593Smuzhiyun
51*4882a593SmuzhiyunMODPOST = scripts/mod/modpost								\
52*4882a593Smuzhiyun	$(if $(CONFIG_MODVERSIONS),-m)							\
53*4882a593Smuzhiyun	$(if $(CONFIG_MODULE_SRCVERSION_ALL),-a)					\
54*4882a593Smuzhiyun	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)					\
55*4882a593Smuzhiyun	$(if $(KBUILD_MODPOST_WARN),-w) \
56*4882a593Smuzhiyun	-o $@
57*4882a593Smuzhiyun
58*4882a593Smuzhiyunifdef MODPOST_VMLINUX
59*4882a593Smuzhiyun
60*4882a593Smuzhiyunquiet_cmd_modpost = MODPOST $@
61*4882a593Smuzhiyun      cmd_modpost = $(MODPOST) $<
62*4882a593Smuzhiyun
63*4882a593Smuzhiyunvmlinux.symvers: vmlinux.o
64*4882a593Smuzhiyun	$(call cmd,modpost)
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun__modpost: vmlinux.symvers
67*4882a593Smuzhiyun
68*4882a593Smuzhiyunelse
69*4882a593Smuzhiyun
70*4882a593Smuzhiyunifeq ($(KBUILD_EXTMOD),)
71*4882a593Smuzhiyun
72*4882a593Smuzhiyuninput-symdump := $(mixed-build-prefix)vmlinux.symvers
73*4882a593Smuzhiyunoutput-symdump := modules-only.symvers
74*4882a593Smuzhiyunmodule_srcpath := $(srctree)
75*4882a593Smuzhiyun
76*4882a593Smuzhiyunquiet_cmd_cat = GEN     $@
77*4882a593Smuzhiyun      cmd_cat = cat $(real-prereqs) > $@
78*4882a593Smuzhiyun
79*4882a593Smuzhiyunifneq ($(wildcard $(mixed-build-prefix)vmlinux.symvers),)
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun__modpost: Module.symvers
82*4882a593SmuzhiyunModule.symvers: $(mixed-build-prefix)vmlinux.symvers modules-only.symvers FORCE
83*4882a593Smuzhiyun	$(call if_changed,cat)
84*4882a593Smuzhiyun
85*4882a593Smuzhiyuntargets += Module.symvers
86*4882a593Smuzhiyun
87*4882a593Smuzhiyunendif
88*4882a593Smuzhiyun
89*4882a593Smuzhiyunelse
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun# set src + obj - they may be used in the modules's Makefile
92*4882a593Smuzhiyunobj := $(KBUILD_EXTMOD)
93*4882a593Smuzhiyunsrc := $(obj)
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
96*4882a593Smuzhiyuninclude $(if $(wildcard $(src)/Kbuild), $(src)/Kbuild, $(src)/Makefile)
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun# modpost option for external modules
99*4882a593SmuzhiyunMODPOST += -e
100*4882a593Smuzhiyun
101*4882a593Smuzhiyuninput-symdump := Module.symvers $(KBUILD_EXTRA_SYMBOLS)
102*4882a593Smuzhiyunoutput-symdump := $(KBUILD_EXTMOD)/Module.symvers
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun# Get the external module's source path. KBUILD_EXTMOD could either be an
105*4882a593Smuzhiyun# absolute path or relative path from $(srctree). This makes sure that we
106*4882a593Smuzhiyun# aren't using a relative path from a separate working directory (O= or
107*4882a593Smuzhiyun# KBUILD_OUTPUT) since that may not be the actual module's SCM project path. So
108*4882a593Smuzhiyun# check the path relative to $(srctree) first.
109*4882a593Smuzhiyunifneq ($(realpath $(srctree)/$(KBUILD_EXTMOD) 2>/dev/null),)
110*4882a593Smuzhiyun	module_srcpath := $(srctree)/$(KBUILD_EXTMOD)
111*4882a593Smuzhiyunelse
112*4882a593Smuzhiyun	module_srcpath := $(KBUILD_EXTMOD)
113*4882a593Smuzhiyunendif
114*4882a593Smuzhiyun
115*4882a593Smuzhiyunendif
116*4882a593Smuzhiyun
117*4882a593Smuzhiyunifeq ($(CONFIG_MODULE_SCMVERSION),y)
118*4882a593Smuzhiyun# Get the SCM version of the module. `sed` verifies setlocalversion returns
119*4882a593Smuzhiyun# a proper revision based on the SCM type, e.g. git, mercurial, or svn.
120*4882a593Smuzhiyunmodule_scmversion := $(shell $(srctree)/scripts/setlocalversion $(module_srcpath) | \
121*4882a593Smuzhiyun	sed -n 's/.*-\(\(g\|hg\)[a-fA-F0-9]\+\(-dirty\)\?\|svn[0-9]\+\).*/\1/p')
122*4882a593Smuzhiyunifneq ($(module_scmversion),)
123*4882a593SmuzhiyunMODPOST += -v $(module_scmversion)
124*4882a593Smuzhiyunendif
125*4882a593Smuzhiyunendif
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun# modpost options for modules (both in-kernel and external)
128*4882a593SmuzhiyunMODPOST += \
129*4882a593Smuzhiyun	$(addprefix -i ,$(wildcard $(input-symdump))) \
130*4882a593Smuzhiyun	$(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
131*4882a593Smuzhiyun	$(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N)
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun# 'make -i -k' ignores compile errors, and builds as many modules as possible.
134*4882a593Smuzhiyunifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
135*4882a593SmuzhiyunMODPOST += -n
136*4882a593Smuzhiyunendif
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun# Clear VPATH to not search for *.symvers in $(srctree). Check only $(objtree).
139*4882a593SmuzhiyunVPATH :=
140*4882a593Smuzhiyun$(input-symdump):
141*4882a593Smuzhiyun	@echo >&2 'WARNING: Symbol version dump "$@" is missing.'
142*4882a593Smuzhiyun	@echo >&2 '         Modules may not have dependencies or modversions.'
143*4882a593Smuzhiyun
144*4882a593Smuzhiyunifdef CONFIG_LTO_CLANG
145*4882a593Smuzhiyun# With CONFIG_LTO_CLANG, .o files might be LLVM bitcode, so we need to run
146*4882a593Smuzhiyun# LTO to compile them into native code before running modpost
147*4882a593Smuzhiyunprelink-ext := .lto
148*4882a593Smuzhiyun
149*4882a593Smuzhiyunquiet_cmd_cc_lto_link_modules = LTO [M] $@
150*4882a593Smuzhiyuncmd_cc_lto_link_modules =						\
151*4882a593Smuzhiyun	$(LD) $(ld_flags) -r -o $@					\
152*4882a593Smuzhiyun		$(shell [ -s $(@:.lto.o=.o.symversions) ] &&		\
153*4882a593Smuzhiyun			echo -T $(@:.lto.o=.o.symversions))		\
154*4882a593Smuzhiyun		--whole-archive $^
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun%.lto.o: %.o
157*4882a593Smuzhiyun	$(call if_changed,cc_lto_link_modules)
158*4882a593Smuzhiyunendif
159*4882a593Smuzhiyun
160*4882a593Smuzhiyunmodules := $(sort $(shell cat $(MODORDER)))
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun# Read out modules.order to pass in modpost.
163*4882a593Smuzhiyun# Otherwise, allmodconfig would fail with "Argument list too long".
164*4882a593Smuzhiyunquiet_cmd_modpost = MODPOST $@
165*4882a593Smuzhiyun      cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
168*4882a593Smuzhiyun	$(call if_changed,modpost)
169*4882a593Smuzhiyun
170*4882a593Smuzhiyuntargets += $(output-symdump)
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun__modpost: $(output-symdump)
173*4882a593Smuzhiyunifneq ($(KBUILD_MODPOST_NOFINAL),1)
174*4882a593Smuzhiyun	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
175*4882a593Smuzhiyunendif
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunPHONY += FORCE
178*4882a593SmuzhiyunFORCE:
179*4882a593Smuzhiyun
180*4882a593Smuzhiyunexisting-targets := $(wildcard $(sort $(targets)))
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
183*4882a593Smuzhiyun
184*4882a593Smuzhiyunendif
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun.PHONY: $(PHONY)
187