Makefile.modpost 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. # Stage 2 is handled by this file and does the following
  13. # 1) Find all modules listed in modules.order
  14. # 2) modpost is then used to
  15. # 3) create one <module>.mod.c file per module
  16. # 4) create one Module.symvers file with CRC for all exported symbols
  17. # Step 3 is used to place certain information in the module's ELF
  18. # section, including information such as:
  19. # Version magic (see include/linux/vermagic.h for full details)
  20. # - Kernel release
  21. # - SMP is CONFIG_SMP
  22. # - PREEMPT is CONFIG_PREEMPT[_RT]
  23. # - GCC Version
  24. # Module info
  25. # - Module version (MODULE_VERSION)
  26. # - Module alias'es (MODULE_ALIAS)
  27. # - Module license (MODULE_LICENSE)
  28. # - See include/linux/module.h for more details
  29. # Step 4 is solely used to allow module versioning in external modules,
  30. # where the CRC of each module is retrieved from the Module.symvers file.
  31. PHONY := __modpost
  32. __modpost:
  33. include include/config/auto.conf
  34. include $(srctree)/scripts/Kbuild.include
  35. mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/)
  36. modpost-args = \
  37. $(if $(CONFIG_MODVERSIONS),-m) \
  38. $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \
  39. $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \
  40. $(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
  41. $(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N) \
  42. -o $@
  43. # 'make -i -k' ignores compile errors, and builds as many modules as possible.
  44. ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
  45. modpost-args += -n
  46. endif
  47. ifeq ($(CONFIG_MODULE_SCMVERSION),y)
  48. ifeq ($(KBUILD_EXTMOD),)
  49. module_srcpath := $(srctree)
  50. else
  51. # Get the external module's source path. KBUILD_EXTMOD could either be an
  52. # absolute path or relative path from $(srctree). This makes sure that we
  53. # aren't using a relative path from a separate working directory (O= or
  54. # KBUILD_OUTPUT) since that may not be the actual module's SCM project path. So
  55. # check the path relative to $(srctree) first.
  56. ifneq ($(realpath $(srctree)/$(KBUILD_EXTMOD) 2>/dev/null),)
  57. module_srcpath := $(srctree)/$(KBUILD_EXTMOD)
  58. else
  59. module_srcpath := $(KBUILD_EXTMOD)
  60. endif
  61. endif
  62. # Get the SCM version of the module. Sed verifies setlocalversion returns
  63. # a proper revision based on the SCM type, e.g. git, mercurial, or svn.
  64. # Note: relative M= paths are not supported when building the kernel out of the
  65. # srctree since setlocalversion won't be able to find the module srctree.
  66. module_scmversion := $(shell $(srctree)/scripts/setlocalversion $(module_srcpath) | \
  67. sed -n 's/.*-\(\(g\|hg\)[a-fA-F0-9]\+\(-dirty\)\?\|svn[0-9]\+\).*/\1/p')
  68. ifneq ($(module_scmversion),)
  69. modpost-args += -v $(module_scmversion)
  70. endif
  71. endif
  72. ifeq ($(KBUILD_EXTMOD),)
  73. # Generate the list of in-tree objects in vmlinux
  74. # ---------------------------------------------------------------------------
  75. # This is used to retrieve symbol versions generated by genksyms.
  76. ifdef CONFIG_MODVERSIONS
  77. ifndef KBUILD_MIXED_TREE
  78. vmlinux.symvers Module.symvers: .vmlinux.objs
  79. endif
  80. endif
  81. # Ignore libgcc.a
  82. # Some architectures do '$(CC) --print-libgcc-file-name' to borrow libgcc.a
  83. # from the toolchain, but there is no EXPORT_SYMBOL in it.
  84. quiet_cmd_vmlinux_objs = GEN $@
  85. cmd_vmlinux_objs = \
  86. for f in $(real-prereqs); do \
  87. case $${f} in \
  88. *libgcc.a) ;; \
  89. *) $(AR) t $${f} ;; \
  90. esac \
  91. done > $@
  92. quiet_cmd_vmlinux_symvers = GEN $@
  93. cmd_vmlinux_symvers = grep "\<vmlinux\s\+EXPORT" $< > $@
  94. quiet_cmd_cat_symvers = GEN $@
  95. cmd_cat_symvers = cat $(real-prereqs) > $@
  96. ifndef KBUILD_MIXED_TREE
  97. targets += .vmlinux.objs
  98. .vmlinux.objs: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE
  99. $(call if_changed,vmlinux_objs)
  100. endif
  101. vmlinux.o-if-present := $(wildcard vmlinux.o)
  102. output-symdump := vmlinux.symvers
  103. ifdef KBUILD_MODULES
  104. output-symdump := $(if $(vmlinux.o-if-present), Module.symvers, modules-only.symvers)
  105. missing-input := $(filter-out $(vmlinux.o-if-present),vmlinux.o)
  106. targets += vmlinux.symvers
  107. vmlinux.symvers: Module.symvers FORCE
  108. $(call if_changed,vmlinux_symvers)
  109. __modpost: $(if $(vmlinux.o-if-present),vmlinux.symvers,)
  110. endif
  111. # Overwrite values for mixed building (overwritting makes merges easier)
  112. ifdef KBUILD_MIXED_TREE
  113. targets += Module.symvers
  114. Module.symvers: $(mixed-build-prefix)vmlinux.symvers modules-only.symvers FORCE
  115. $(call if_changed,cat_symvers)
  116. __modpost: Module.symvers
  117. vmlinux.o-if-present :=
  118. vmlinux.symvers-if-present := $(wildcard $(mixed-build-prefix)vmlinux.symvers)
  119. modpost-args += $(addprefix -i ,$(vmlinux.symvers-if-present))
  120. output-symdump := modules-only.symvers
  121. missing-input := $(filter-out $(vmlinux.symvers-if-present),$(mixed-build-prefix)vmlinux.symvers)
  122. endif
  123. else
  124. # set src + obj - they may be used in the modules's Makefile
  125. obj := $(KBUILD_EXTMOD)
  126. src := $(obj)
  127. # Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
  128. include $(or $(wildcard $(src)/Kbuild), $(src)/Makefile)
  129. module.symvers-if-present := $(wildcard Module.symvers)
  130. output-symdump := $(KBUILD_EXTMOD)/Module.symvers
  131. missing-input := $(filter-out $(module.symvers-if-present), Module.symvers)
  132. modpost-args += -e $(addprefix -i ,$(module.symvers-if-present) $(KBUILD_EXTRA_SYMBOLS))
  133. endif # ($(KBUILD_EXTMOD),)
  134. ifneq ($(KBUILD_MODPOST_WARN)$(missing-input),)
  135. modpost-args += -w
  136. endif
  137. modorder-if-needed := $(if $(KBUILD_MODULES), $(MODORDER))
  138. MODPOST = scripts/mod/modpost
  139. # Read out modules.order to pass in modpost.
  140. # Otherwise, allmodconfig would fail with "Argument list too long".
  141. quiet_cmd_modpost = MODPOST $@
  142. cmd_modpost = \
  143. $(if $(missing-input), \
  144. echo >&2 "WARNING: $(missing-input) is missing."; \
  145. echo >&2 " Modules may not have dependencies or modversions."; \
  146. echo >&2 " You may get many unresolved symbol warnings.";) \
  147. sed 's/ko$$/o/' $(or $(modorder-if-needed), /dev/null) | $(MODPOST) $(modpost-args) -T - $(vmlinux.o-if-present)
  148. targets += $(output-symdump)
  149. $(output-symdump): $(modorder-if-needed) $(vmlinux.o-if-present) $(module.symvers-if-present) $(MODPOST) FORCE
  150. $(call if_changed,modpost)
  151. __modpost: $(output-symdump)
  152. PHONY += FORCE
  153. FORCE:
  154. existing-targets := $(wildcard $(sort $(targets)))
  155. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  156. .PHONY: $(PHONY)