162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci# ===========================================================================
362306a36Sopenharmony_ci# Module versions
462306a36Sopenharmony_ci# ===========================================================================
562306a36Sopenharmony_ci#
662306a36Sopenharmony_ci# Stage one of module building created the following:
762306a36Sopenharmony_ci# a) The individual .o files used for the module
862306a36Sopenharmony_ci# b) A <module>.o file which is the .o files above linked together
962306a36Sopenharmony_ci# c) A <module>.mod file, listing the name of the preliminary <module>.o file,
1062306a36Sopenharmony_ci#    plus all .o files
1162306a36Sopenharmony_ci# d) modules.order, which lists all the modules
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci# Stage 2 is handled by this file and does the following
1462306a36Sopenharmony_ci# 1) Find all modules listed in modules.order
1562306a36Sopenharmony_ci# 2) modpost is then used to
1662306a36Sopenharmony_ci# 3)  create one <module>.mod.c file per module
1762306a36Sopenharmony_ci# 4)  create one Module.symvers file with CRC for all exported symbols
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci# Step 3 is used to place certain information in the module's ELF
2062306a36Sopenharmony_ci# section, including information such as:
2162306a36Sopenharmony_ci#   Version magic (see include/linux/vermagic.h for full details)
2262306a36Sopenharmony_ci#     - Kernel release
2362306a36Sopenharmony_ci#     - SMP is CONFIG_SMP
2462306a36Sopenharmony_ci#     - PREEMPT is CONFIG_PREEMPT[_RT]
2562306a36Sopenharmony_ci#     - GCC Version
2662306a36Sopenharmony_ci#   Module info
2762306a36Sopenharmony_ci#     - Module version (MODULE_VERSION)
2862306a36Sopenharmony_ci#     - Module alias'es (MODULE_ALIAS)
2962306a36Sopenharmony_ci#     - Module license (MODULE_LICENSE)
3062306a36Sopenharmony_ci#     - See include/linux/module.h for more details
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci# Step 4 is solely used to allow module versioning in external modules,
3362306a36Sopenharmony_ci# where the CRC of each module is retrieved from the Module.symvers file.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciPHONY := __modpost
3662306a36Sopenharmony_ci__modpost:
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciinclude include/config/auto.conf
3962306a36Sopenharmony_ciinclude $(srctree)/scripts/Kbuild.include
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciMODPOST = scripts/mod/modpost
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cimodpost-args =										\
4462306a36Sopenharmony_ci	$(if $(CONFIG_MODULES),-M)							\
4562306a36Sopenharmony_ci	$(if $(CONFIG_MODVERSIONS),-m)							\
4662306a36Sopenharmony_ci	$(if $(CONFIG_MODULE_SRCVERSION_ALL),-a)					\
4762306a36Sopenharmony_ci	$(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E)					\
4862306a36Sopenharmony_ci	$(if $(KBUILD_MODPOST_WARN),-w)							\
4962306a36Sopenharmony_ci	$(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS))					\
5062306a36Sopenharmony_ci	$(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N)	\
5162306a36Sopenharmony_ci	$(if $(findstring 1, $(KBUILD_EXTRA_WARN)),-W)					\
5262306a36Sopenharmony_ci	-o $@
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cimodpost-deps := $(MODPOST)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci# 'make -i -k' ignores compile errors, and builds as many modules as possible.
5762306a36Sopenharmony_ciifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
5862306a36Sopenharmony_cimodpost-args += -n
5962306a36Sopenharmony_ciendif
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci# Read out modules.order to pass in modpost.
6262306a36Sopenharmony_ci# Otherwise, allmodconfig would fail with "Argument list too long".
6362306a36Sopenharmony_ciifdef KBUILD_MODULES
6462306a36Sopenharmony_cimodpost-args += -T $(MODORDER)
6562306a36Sopenharmony_cimodpost-deps += $(MODORDER)
6662306a36Sopenharmony_ciendif
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci# Generate the list of in-tree objects in vmlinux
7162306a36Sopenharmony_ci# ---------------------------------------------------------------------------
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci# This is used to retrieve symbol versions generated by genksyms.
7462306a36Sopenharmony_ciifdef CONFIG_MODVERSIONS
7562306a36Sopenharmony_civmlinux.symvers Module.symvers: .vmlinux.objs
7662306a36Sopenharmony_ciendif
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci# Ignore libgcc.a
7962306a36Sopenharmony_ci# Some architectures do '$(CC) --print-libgcc-file-name' to borrow libgcc.a
8062306a36Sopenharmony_ci# from the toolchain, but there is no EXPORT_SYMBOL in it.
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ciquiet_cmd_vmlinux_objs = GEN     $@
8362306a36Sopenharmony_ci      cmd_vmlinux_objs =		\
8462306a36Sopenharmony_ci	for f in $(real-prereqs); do	\
8562306a36Sopenharmony_ci		case $${f} in		\
8662306a36Sopenharmony_ci		*libgcc.a) ;;		\
8762306a36Sopenharmony_ci		*) $(AR) t $${f} ;;	\
8862306a36Sopenharmony_ci		esac			\
8962306a36Sopenharmony_ci	done > $@
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_citargets += .vmlinux.objs
9262306a36Sopenharmony_ci.vmlinux.objs: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE
9362306a36Sopenharmony_ci	$(call if_changed,vmlinux_objs)
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciifdef CONFIG_TRIM_UNUSED_KSYMS
9662306a36Sopenharmony_ciksym-wl := $(CONFIG_UNUSED_KSYMS_WHITELIST)
9762306a36Sopenharmony_ciksym-wl := $(if $(filter-out /%, $(ksym-wl)),$(srctree)/)$(ksym-wl)
9862306a36Sopenharmony_cimodpost-args += -t $(addprefix -u , $(ksym-wl))
9962306a36Sopenharmony_cimodpost-deps += $(ksym-wl)
10062306a36Sopenharmony_ciendif
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciifeq ($(wildcard vmlinux.o),)
10362306a36Sopenharmony_cimissing-input := vmlinux.o
10462306a36Sopenharmony_cioutput-symdump := modules-only.symvers
10562306a36Sopenharmony_cielse
10662306a36Sopenharmony_cimodpost-args += vmlinux.o
10762306a36Sopenharmony_cimodpost-deps += vmlinux.o
10862306a36Sopenharmony_cioutput-symdump := $(if $(KBUILD_MODULES), Module.symvers, vmlinux.symvers)
10962306a36Sopenharmony_ciendif
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cielse
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci# set src + obj - they may be used in the modules's Makefile
11462306a36Sopenharmony_ciobj := $(KBUILD_EXTMOD)
11562306a36Sopenharmony_cisrc := $(obj)
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
11862306a36Sopenharmony_ciinclude $(kbuild-file)
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cioutput-symdump := $(KBUILD_EXTMOD)/Module.symvers
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciifeq ($(wildcard Module.symvers),)
12362306a36Sopenharmony_cimissing-input := Module.symvers
12462306a36Sopenharmony_cielse
12562306a36Sopenharmony_cimodpost-args += -i Module.symvers
12662306a36Sopenharmony_cimodpost-deps += Module.symvers
12762306a36Sopenharmony_ciendif
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cimodpost-args += -e $(addprefix -i , $(KBUILD_EXTRA_SYMBOLS))
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciendif # ($(KBUILD_EXTMOD),)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciquiet_cmd_modpost = MODPOST $@
13462306a36Sopenharmony_ci      cmd_modpost = \
13562306a36Sopenharmony_ci	$(if $(missing-input), \
13662306a36Sopenharmony_ci		echo >&2 "WARNING: $(missing-input) is missing."; \
13762306a36Sopenharmony_ci		echo >&2 "         Modules may not have dependencies or modversions."; \
13862306a36Sopenharmony_ci		echo >&2 "         You may get many unresolved symbol errors."; \
13962306a36Sopenharmony_ci		echo >&2 "         You can set KBUILD_MODPOST_WARN=1 to turn errors into warning"; \
14062306a36Sopenharmony_ci		echo >&2 "         if you want to proceed at your own risk.";) \
14162306a36Sopenharmony_ci	$(MODPOST) $(modpost-args)
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_citargets += $(output-symdump)
14462306a36Sopenharmony_ci$(output-symdump): $(modpost-deps) FORCE
14562306a36Sopenharmony_ci	$(call if_changed,modpost)
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci__modpost: $(output-symdump)
14862306a36Sopenharmony_ciPHONY += FORCE
14962306a36Sopenharmony_ciFORCE:
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciexisting-targets := $(wildcard $(sort $(targets)))
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci.PHONY: $(PHONY)
156