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