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 $(objtree)/include/config/auto.conf 39include $(srctree)/scripts/Kbuild.include 40 41mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/) 42 43MODPOST = $(objtree)/scripts/mod/modpost 44 45modpost-args = \ 46 $(if $(CONFIG_MODULES),-M) \ 47 $(if $(CONFIG_MODVERSIONS),-m) \ 48 $(if $(CONFIG_BASIC_MODVERSIONS),-b) \ 49 $(if $(CONFIG_EXTENDED_MODVERSIONS),-x) \ 50 $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \ 51 $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ 52 $(if $(KBUILD_MODPOST_WARN),-w) \ 53 $(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \ 54 $(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N) \ 55 $(if $(findstring 1, $(KBUILD_EXTRA_WARN)),-W) \ 56 -o $@ 57 58modpost-deps := $(MODPOST) 59 60# 'make -i -k' ignores compile errors, and builds as many modules as possible. 61ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),) 62modpost-args += -n 63endif 64 65# Read out modules.order to pass in modpost. 66# Otherwise, allmodconfig would fail with "Argument list too long". 67ifdef KBUILD_MODULES 68modpost-args += -T $(MODORDER) 69modpost-deps += $(MODORDER) 70endif 71 72ifeq ($(CONFIG_MODULE_SCMVERSION),y) 73ifeq ($(KBUILD_EXTMOD),) 74module_srcpath := $(srctree) 75else 76# Get the external module's source path. KBUILD_EXTMOD could either be an 77# absolute path or relative path from $(srctree). This makes sure that we 78# aren't using a relative path from a separate working directory (O= or 79# KBUILD_OUTPUT) since that may not be the actual module's SCM project path. So 80# check the path relative to $(srctree) first. 81ifneq ($(realpath $(srctree)/$(KBUILD_EXTMOD) 2>/dev/null),) 82 module_srcpath := $(srctree)/$(KBUILD_EXTMOD) 83else 84 module_srcpath := $(KBUILD_EXTMOD) 85endif 86endif 87 88# Get the SCM version of the module. Sed verifies setlocalversion returns 89# a proper revision based on the SCM type, e.g. git, mercurial, or svn. 90# Note: relative M= paths are not supported when building the kernel out of the 91# srctree since setlocalversion won't be able to find the module srctree. 92module_scmversion := $(shell $(srctree)/scripts/setlocalversion $(module_srcpath) | \ 93 sed -n 's/.*-\(\(g\|hg\)[a-fA-F0-9]\+\(-dirty\)\?\|svn[0-9]\+\).*/\1/p') 94ifneq ($(module_scmversion),) 95modpost-args += -v $(module_scmversion) 96endif 97endif 98 99ifeq ($(KBUILD_EXTMOD),) 100 101# Generate the list of in-tree objects in vmlinux 102# --------------------------------------------------------------------------- 103 104# This is used to retrieve symbol versions generated by genksyms. 105ifdef CONFIG_MODVERSIONS 106ifndef KBUILD_MIXED_TREE 107vmlinux.symvers Module.symvers: .vmlinux.objs 108endif 109endif 110 111# Ignore libgcc.a 112# Some architectures do '$(CC) --print-libgcc-file-name' to borrow libgcc.a 113# from the toolchain, but there is no EXPORT_SYMBOL in it. 114 115quiet_cmd_vmlinux_objs = GEN $@ 116 cmd_vmlinux_objs = \ 117 for f in $(real-prereqs); do \ 118 case $${f} in \ 119 *libgcc.a) ;; \ 120 *) $(AR) t $${f} ;; \ 121 esac \ 122 done > $@ 123 124quiet_cmd_vmlinux_symvers = GEN $@ 125 cmd_vmlinux_symvers = grep "\<vmlinux\s\+EXPORT" $< > $@ 126 127quiet_cmd_cat_symvers = GEN $@ 128 cmd_cat_symvers = cat $(real-prereqs) > $@ 129 130ifndef KBUILD_MIXED_TREE 131targets += .vmlinux.objs 132.vmlinux.objs: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE 133 $(call if_changed,vmlinux_objs) 134endif 135 136ifdef CONFIG_TRIM_UNUSED_KSYMS 137ksym-wl := $(CONFIG_UNUSED_KSYMS_WHITELIST) 138ksym-wl := $(if $(filter-out /%, $(ksym-wl)),$(if $(wildcard $(ksym-wl)),,$(srctree)/))$(ksym-wl) 139modpost-args += -t $(addprefix -u , $(ksym-wl)) 140modpost-deps += $(ksym-wl) 141endif 142 143ifeq ($(wildcard vmlinux.o),) 144missing-input := vmlinux.o 145output-symdump := modules-only.symvers 146else 147modpost-args += vmlinux.o 148modpost-deps += vmlinux.o 149output-symdump := $(if $(KBUILD_MODULES), Module.symvers, vmlinux.symvers) 150endif 151 152ifdef KBUILD_MODULES 153targets += vmlinux.symvers 154vmlinux.symvers: Module.symvers FORCE 155 $(call if_changed,vmlinux_symvers) 156 157__modpost: $(if $(wildcard vmlinux.o),vmlinux.symvers,) 158endif 159 160# Overwrite values for mixed building (overwritting makes merges easier) 161ifdef KBUILD_MIXED_TREE 162targets += Module.symvers 163Module.symvers: $(mixed-build-prefix)vmlinux.symvers modules-only.symvers FORCE 164 $(call if_changed,cat_symvers) 165 166__modpost: Module.symvers 167 168ifeq ($(wildcard $(mixed-build-prefix)vmlinux.symvers),) 169missing-input := $(mixed-build-prefix)vmlinux.symvers 170else 171missing-input := 172modpost-args += $(addprefix -i ,$(wildcard $(mixed-build-prefix)vmlinux.symvers)) 173endif 174# Note: we don't want to include the vmlinux symbols here because we want to be 175# sure we only use the vmlinux symbols from the GKI kernel. 176output-symdump := modules-only.symvers 177endif 178 179else 180 181# set src + obj - they may be used in the modules's Makefile 182obj := $(KBUILD_EXTMOD) 183src := $(if $(VPATH),$(VPATH)/)$(obj) 184 185# Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS 186include $(kbuild-file) 187 188output-symdump := $(KBUILD_EXTMOD)/Module.symvers 189 190ifeq ($(wildcard $(objtree)/Module.symvers),) 191missing-input := $(objtree)/Module.symvers 192else 193modpost-args += -i $(objtree)/Module.symvers 194modpost-deps += $(objtree)/Module.symvers 195endif 196 197modpost-args += -e $(addprefix -i , $(KBUILD_EXTRA_SYMBOLS)) 198 199endif # ($(KBUILD_EXTMOD),) 200 201ifdef CONFIG_MODULE_SIG_PROTECT 202mod-protect-list := $(CONFIG_MODULE_SIG_PROTECT_LIST) 203mod-protect-list := $(if $(filter-out /%, $(mod-protect-list)),$(if $(wildcard $(mod-protect-list)),,$(srctree)/))$(mod-protect-list) 204modpost-args += $(addprefix -p , $(mod-protect-list)) 205modpost-deps += $(mod-protect-list) 206endif 207 208quiet_cmd_modpost = MODPOST $@ 209 cmd_modpost = \ 210 $(if $(missing-input), \ 211 echo >&2 "WARNING: $(missing-input) is missing."; \ 212 echo >&2 " Modules may not have dependencies or modversions."; \ 213 echo >&2 " You may get many unresolved symbol errors."; \ 214 echo >&2 " You can set KBUILD_MODPOST_WARN=1 to turn errors into warning"; \ 215 echo >&2 " if you want to proceed at your own risk.";) \ 216 $(MODPOST) $(modpost-args) 217 218targets += $(output-symdump) 219$(output-symdump): $(modpost-deps) FORCE 220 $(call if_changed,modpost) 221 222__modpost: $(output-symdump) 223PHONY += FORCE 224FORCE: 225 226existing-targets := $(wildcard $(sort $(targets))) 227 228-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 229 230.PHONY: $(PHONY) 231