1# SPDX-License-Identifier: GPL-2.0 2# =========================================================================== 3# Module versions 4# =========================================================================== 5# 6# Stage one of module building created the following: 7# a) The individual .o files used for the module 8# b) A <module>.o file which is the .o files above linked together 9# c) A <module>.mod file, listing the name of the preliminary <module>.o file, 10# plus all .o files 11# d) modules.order, which lists all the modules 12 13# Stage 2 is handled by this file and does the following 14# 1) Find all modules listed in modules.order 15# 2) modpost is then used to 16# 3) create one <module>.mod.c file per module 17# 4) create one Module.symvers file with CRC for all exported symbols 18 19# Step 3 is used to place certain information in the module's ELF 20# section, including information such as: 21# Version magic (see include/linux/vermagic.h for full details) 22# - Kernel release 23# - SMP is CONFIG_SMP 24# - PREEMPT is CONFIG_PREEMPT[_RT] 25# - GCC Version 26# Module info 27# - Module version (MODULE_VERSION) 28# - Module alias'es (MODULE_ALIAS) 29# - Module license (MODULE_LICENSE) 30# - See include/linux/module.h for more details 31 32# Step 4 is solely used to allow module versioning in external modules, 33# where the CRC of each module is retrieved from the Module.symvers file. 34 35PHONY := __modpost 36__modpost: 37 38include include/config/auto.conf 39include $(srctree)/scripts/Kbuild.include 40 41mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/) 42 43MODPOST = scripts/mod/modpost 44 45modpost-args = \ 46 $(if $(CONFIG_MODULES),-M) \ 47 $(if $(CONFIG_MODVERSIONS),-m) \ 48 $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \ 49 $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ 50 $(if $(KBUILD_MODPOST_WARN),-w) \ 51 $(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \ 52 $(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N) \ 53 $(if $(findstring 1, $(KBUILD_EXTRA_WARN)),-W) \ 54 -o $@ 55 56modpost-deps := $(MODPOST) 57 58# 'make -i -k' ignores compile errors, and builds as many modules as possible. 59ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),) 60modpost-args += -n 61endif 62 63# Read out modules.order to pass in modpost. 64# Otherwise, allmodconfig would fail with "Argument list too long". 65ifdef KBUILD_MODULES 66modpost-args += -T $(MODORDER) 67modpost-deps += $(MODORDER) 68endif 69 70ifeq ($(CONFIG_MODULE_SCMVERSION),y) 71ifeq ($(KBUILD_EXTMOD),) 72module_srcpath := $(srctree) 73else 74# Get the external module's source path. KBUILD_EXTMOD could either be an 75# absolute path or relative path from $(srctree). This makes sure that we 76# aren't using a relative path from a separate working directory (O= or 77# KBUILD_OUTPUT) since that may not be the actual module's SCM project path. So 78# check the path relative to $(srctree) first. 79ifneq ($(realpath $(srctree)/$(KBUILD_EXTMOD) 2>/dev/null),) 80 module_srcpath := $(srctree)/$(KBUILD_EXTMOD) 81else 82 module_srcpath := $(KBUILD_EXTMOD) 83endif 84endif 85 86# Get the SCM version of the module. Sed verifies setlocalversion returns 87# a proper revision based on the SCM type, e.g. git, mercurial, or svn. 88# Note: relative M= paths are not supported when building the kernel out of the 89# srctree since setlocalversion won't be able to find the module srctree. 90module_scmversion := $(shell $(srctree)/scripts/setlocalversion $(module_srcpath) | \ 91 sed -n 's/.*-\(\(g\|hg\)[a-fA-F0-9]\+\(-dirty\)\?\|svn[0-9]\+\).*/\1/p') 92ifneq ($(module_scmversion),) 93modpost-args += -v $(module_scmversion) 94endif 95endif 96 97ifeq ($(KBUILD_EXTMOD),) 98 99# Generate the list of in-tree objects in vmlinux 100# --------------------------------------------------------------------------- 101 102# This is used to retrieve symbol versions generated by genksyms. 103ifdef CONFIG_MODVERSIONS 104ifndef KBUILD_MIXED_TREE 105vmlinux.symvers Module.symvers: .vmlinux.objs 106endif 107endif 108 109# Ignore libgcc.a 110# Some architectures do '$(CC) --print-libgcc-file-name' to borrow libgcc.a 111# from the toolchain, but there is no EXPORT_SYMBOL in it. 112 113quiet_cmd_vmlinux_objs = GEN $@ 114 cmd_vmlinux_objs = \ 115 for f in $(real-prereqs); do \ 116 case $${f} in \ 117 *libgcc.a) ;; \ 118 *) $(AR) t $${f} ;; \ 119 esac \ 120 done > $@ 121 122quiet_cmd_vmlinux_symvers = GEN $@ 123 cmd_vmlinux_symvers = grep "\<vmlinux\s\+EXPORT" $< > $@ 124 125quiet_cmd_cat_symvers = GEN $@ 126 cmd_cat_symvers = cat $(real-prereqs) > $@ 127 128ifndef KBUILD_MIXED_TREE 129targets += .vmlinux.objs 130.vmlinux.objs: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE 131 $(call if_changed,vmlinux_objs) 132endif 133 134ifdef CONFIG_TRIM_UNUSED_KSYMS 135ksym-wl := $(CONFIG_UNUSED_KSYMS_WHITELIST) 136ksym-wl := $(if $(filter-out /%, $(ksym-wl)),$(if $(wildcard $(ksym-wl)),,$(srctree)/))$(ksym-wl) 137modpost-args += -t $(addprefix -u , $(ksym-wl)) 138modpost-deps += $(ksym-wl) 139endif 140 141ifeq ($(wildcard vmlinux.o),) 142missing-input := vmlinux.o 143output-symdump := modules-only.symvers 144else 145modpost-args += vmlinux.o 146modpost-deps += vmlinux.o 147output-symdump := $(if $(KBUILD_MODULES), Module.symvers, vmlinux.symvers) 148endif 149 150ifdef KBUILD_MODULES 151targets += vmlinux.symvers 152vmlinux.symvers: Module.symvers FORCE 153 $(call if_changed,vmlinux_symvers) 154 155__modpost: $(if $(wildcard vmlinux.o),vmlinux.symvers,) 156endif 157 158# Overwrite values for mixed building (overwritting makes merges easier) 159ifdef KBUILD_MIXED_TREE 160targets += Module.symvers 161Module.symvers: $(mixed-build-prefix)vmlinux.symvers modules-only.symvers FORCE 162 $(call if_changed,cat_symvers) 163 164__modpost: Module.symvers 165 166ifeq ($(wildcard $(mixed-build-prefix)vmlinux.symvers),) 167missing-input := $(mixed-build-prefix)vmlinux.symvers 168else 169missing-input := 170modpost-args += $(addprefix -i ,$(wildcard $(mixed-build-prefix)vmlinux.symvers)) 171endif 172# Note: we don't want to include the vmlinux symbols here because we want to be 173# sure we only use the vmlinux symbols from the GKI kernel. 174output-symdump := modules-only.symvers 175endif 176 177else 178 179# set src + obj - they may be used in the modules's Makefile 180obj := $(KBUILD_EXTMOD) 181src := $(obj) 182 183# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS 184include $(kbuild-file) 185 186output-symdump := $(KBUILD_EXTMOD)/Module.symvers 187 188ifeq ($(wildcard Module.symvers),) 189missing-input := Module.symvers 190else 191modpost-args += -i Module.symvers 192modpost-deps += Module.symvers 193endif 194 195modpost-args += -e $(addprefix -i , $(KBUILD_EXTRA_SYMBOLS)) 196 197endif # ($(KBUILD_EXTMOD),) 198 199quiet_cmd_modpost = MODPOST $@ 200 cmd_modpost = \ 201 $(if $(missing-input), \ 202 echo >&2 "WARNING: $(missing-input) is missing."; \ 203 echo >&2 " Modules may not have dependencies or modversions."; \ 204 echo >&2 " You may get many unresolved symbol errors."; \ 205 echo >&2 " You can set KBUILD_MODPOST_WARN=1 to turn errors into warning"; \ 206 echo >&2 " if you want to proceed at your own risk.";) \ 207 $(MODPOST) $(modpost-args) 208 209targets += $(output-symdump) 210$(output-symdump): $(modpost-deps) FORCE 211 $(call if_changed,modpost) 212 213__modpost: $(output-symdump) 214PHONY += FORCE 215FORCE: 216 217existing-targets := $(wildcard $(sort $(targets))) 218 219-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 220 221.PHONY: $(PHONY) 222