18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci# ===========================================================================
38c2ecf20Sopenharmony_ci# Module versions
48c2ecf20Sopenharmony_ci# ===========================================================================
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Stage one of module building created the following:
78c2ecf20Sopenharmony_ci# a) The individual .o files used for the module
88c2ecf20Sopenharmony_ci# b) A <module>.o file which is the .o files above linked together
98c2ecf20Sopenharmony_ci# c) A <module>.mod file, listing the name of the preliminary <module>.o file,
108c2ecf20Sopenharmony_ci#    plus all .o files
118c2ecf20Sopenharmony_ci# d) modules.order, which lists all the modules
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci# Stage 2 is handled by this file and does the following
148c2ecf20Sopenharmony_ci# 1) Find all modules listed in modules.order
158c2ecf20Sopenharmony_ci# 2) modpost is then used to
168c2ecf20Sopenharmony_ci# 3)  create one <module>.mod.c file pr. module
178c2ecf20Sopenharmony_ci# 4)  create one Module.symvers file with CRC for all exported symbols
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci# Step 3 is used to place certain information in the module's ELF
208c2ecf20Sopenharmony_ci# section, including information such as:
218c2ecf20Sopenharmony_ci#   Version magic (see include/linux/vermagic.h for full details)
228c2ecf20Sopenharmony_ci#     - Kernel release
238c2ecf20Sopenharmony_ci#     - SMP is CONFIG_SMP
248c2ecf20Sopenharmony_ci#     - PREEMPT is CONFIG_PREEMPT[_RT]
258c2ecf20Sopenharmony_ci#     - GCC Version
268c2ecf20Sopenharmony_ci#   Module info
278c2ecf20Sopenharmony_ci#     - Module version (MODULE_VERSION)
288c2ecf20Sopenharmony_ci#     - Module alias'es (MODULE_ALIAS)
298c2ecf20Sopenharmony_ci#     - Module license (MODULE_LICENSE)
308c2ecf20Sopenharmony_ci#     - See include/linux/module.h for more details
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci# Step 4 is solely used to allow module versioning in external modules,
338c2ecf20Sopenharmony_ci# where the CRC of each module is retrieved from the Module.symvers file.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined
368c2ecf20Sopenharmony_ci# symbols in the final module linking stage
378c2ecf20Sopenharmony_ci# KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
388c2ecf20Sopenharmony_ci# This is solely useful to speed up test compiles
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciPHONY := __modpost
418c2ecf20Sopenharmony_ci__modpost:
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciinclude include/config/auto.conf
448c2ecf20Sopenharmony_ciinclude scripts/Kbuild.include
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci# for ld_flags
478c2ecf20Sopenharmony_ciinclude scripts/Makefile.lib
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciMODPOST = scripts/mod/modpost								\
508c2ecf20Sopenharmony_ci	$(if $(CONFIG_MODVERSIONS),-m)							\
518c2ecf20Sopenharmony_ci	$(if $(CONFIG_MODULE_SRCVERSION_ALL),-a)					\
528c2ecf20Sopenharmony_ci	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)					\
538c2ecf20Sopenharmony_ci	$(if $(KBUILD_MODPOST_WARN),-w) \
548c2ecf20Sopenharmony_ci	-o $@
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciifdef MODPOST_VMLINUX
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciquiet_cmd_modpost = MODPOST $@
598c2ecf20Sopenharmony_ci      cmd_modpost = $(MODPOST) $<
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_civmlinux.symvers: vmlinux.o
628c2ecf20Sopenharmony_ci	$(call cmd,modpost)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci__modpost: vmlinux.symvers
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cielse
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciinput-symdump := vmlinux.symvers
718c2ecf20Sopenharmony_cioutput-symdump := modules-only.symvers
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciquiet_cmd_cat = GEN     $@
748c2ecf20Sopenharmony_ci      cmd_cat = cat $(real-prereqs) > $@
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciifneq ($(wildcard vmlinux.symvers),)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci__modpost: Module.symvers
798c2ecf20Sopenharmony_ciModule.symvers: vmlinux.symvers modules-only.symvers FORCE
808c2ecf20Sopenharmony_ci	$(call if_changed,cat)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_citargets += Module.symvers
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciendif
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cielse
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci# set src + obj - they may be used in the modules's Makefile
898c2ecf20Sopenharmony_ciobj := $(KBUILD_EXTMOD)
908c2ecf20Sopenharmony_cisrc := $(obj)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
938c2ecf20Sopenharmony_ciinclude $(if $(wildcard $(src)/Kbuild), $(src)/Kbuild, $(src)/Makefile)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci# modpost option for external modules
968c2ecf20Sopenharmony_ciMODPOST += -e
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciinput-symdump := Module.symvers $(KBUILD_EXTRA_SYMBOLS)
998c2ecf20Sopenharmony_cioutput-symdump := $(KBUILD_EXTMOD)/Module.symvers
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciendif
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci# modpost options for modules (both in-kernel and external)
1048c2ecf20Sopenharmony_ciMODPOST += \
1058c2ecf20Sopenharmony_ci	$(addprefix -i ,$(wildcard $(input-symdump))) \
1068c2ecf20Sopenharmony_ci	$(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
1078c2ecf20Sopenharmony_ci	$(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci# 'make -i -k' ignores compile errors, and builds as many modules as possible.
1108c2ecf20Sopenharmony_ciifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
1118c2ecf20Sopenharmony_ciMODPOST += -n
1128c2ecf20Sopenharmony_ciendif
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci# Clear VPATH to not search for *.symvers in $(srctree). Check only $(objtree).
1158c2ecf20Sopenharmony_ciVPATH :=
1168c2ecf20Sopenharmony_ci$(input-symdump):
1178c2ecf20Sopenharmony_ci	@echo >&2 'WARNING: Symbol version dump "$@" is missing.'
1188c2ecf20Sopenharmony_ci	@echo >&2 '         Modules may not have dependencies or modversions.'
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciifdef CONFIG_LTO_CLANG
1218c2ecf20Sopenharmony_ci# With CONFIG_LTO_CLANG, .o files might be LLVM bitcode, so we need to run
1228c2ecf20Sopenharmony_ci# LTO to compile them into native code before running modpost
1238c2ecf20Sopenharmony_ciprelink-ext := .lto
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ciquiet_cmd_cc_lto_link_modules = LTO [M] $@
1268c2ecf20Sopenharmony_cicmd_cc_lto_link_modules =						\
1278c2ecf20Sopenharmony_ci	$(LD) $(ld_flags) -r -o $@					\
1288c2ecf20Sopenharmony_ci		$(shell [ -s $(@:.lto.o=.o.symversions) ] &&		\
1298c2ecf20Sopenharmony_ci			echo -T $(@:.lto.o=.o.symversions))		\
1308c2ecf20Sopenharmony_ci		--whole-archive $^
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci%.lto.o: %.o
1338c2ecf20Sopenharmony_ci	$(call if_changed,cc_lto_link_modules)
1348c2ecf20Sopenharmony_ciendif
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cimodules := $(sort $(shell cat $(MODORDER)))
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci# Read out modules.order to pass in modpost.
1398c2ecf20Sopenharmony_ci# Otherwise, allmodconfig would fail with "Argument list too long".
1408c2ecf20Sopenharmony_ciquiet_cmd_modpost = MODPOST $@
1418c2ecf20Sopenharmony_ci      cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
1448c2ecf20Sopenharmony_ci	$(call if_changed,modpost)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_citargets += $(output-symdump)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci__modpost: $(output-symdump)
1498c2ecf20Sopenharmony_ciifneq ($(KBUILD_MODPOST_NOFINAL),1)
1508c2ecf20Sopenharmony_ci	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
1518c2ecf20Sopenharmony_ciendif
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciPHONY += FORCE
1548c2ecf20Sopenharmony_ciFORCE:
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciexisting-targets := $(wildcard $(sort $(targets)))
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ciendif
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci.PHONY: $(PHONY)
163