Makefile.modpost 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 pr. 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. # KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined
  32. # symbols in the final module linking stage
  33. # KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
  34. # This is solely useful to speed up test compiles
  35. PHONY := __modpost
  36. __modpost:
  37. include include/config/auto.conf
  38. include scripts/Kbuild.include
  39. # for ld_flags
  40. include scripts/Makefile.lib
  41. mixed-build-prefix = $(if $(KBUILD_MIXED_TREE),$(KBUILD_MIXED_TREE)/)
  42. MODPOST = scripts/mod/modpost \
  43. $(if $(CONFIG_MODVERSIONS),-m) \
  44. $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \
  45. $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \
  46. $(if $(KBUILD_MODPOST_WARN),-w) \
  47. -o $@
  48. ifdef MODPOST_VMLINUX
  49. quiet_cmd_modpost = MODPOST $@
  50. cmd_modpost = $(MODPOST) $<
  51. vmlinux.symvers: vmlinux.o
  52. $(call cmd,modpost)
  53. __modpost: vmlinux.symvers
  54. else
  55. ifeq ($(KBUILD_EXTMOD),)
  56. input-symdump := $(mixed-build-prefix)vmlinux.symvers
  57. output-symdump := modules-only.symvers
  58. module_srcpath := $(srctree)
  59. quiet_cmd_cat = GEN $@
  60. cmd_cat = cat $(real-prereqs) > $@
  61. ifneq ($(wildcard $(mixed-build-prefix)vmlinux.symvers),)
  62. __modpost: Module.symvers
  63. Module.symvers: $(mixed-build-prefix)vmlinux.symvers modules-only.symvers FORCE
  64. $(call if_changed,cat)
  65. targets += Module.symvers
  66. endif
  67. else
  68. # set src + obj - they may be used in the modules's Makefile
  69. obj := $(KBUILD_EXTMOD)
  70. src := $(obj)
  71. # Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
  72. include $(if $(wildcard $(KBUILD_EXTMOD)/Kbuild), \
  73. $(KBUILD_EXTMOD)/Kbuild, $(KBUILD_EXTMOD)/Makefile)
  74. # modpost option for external modules
  75. MODPOST += -e
  76. input-symdump := Module.symvers $(KBUILD_EXTRA_SYMBOLS)
  77. output-symdump := $(KBUILD_EXTMOD)/Module.symvers
  78. # Get the external module's source path. KBUILD_EXTMOD could either be an
  79. # absolute path or relative path from $(srctree). This makes sure that we
  80. # aren't using a relative path from a separate working directory (O= or
  81. # KBUILD_OUTPUT) since that may not be the actual module's SCM project path. So
  82. # check the path relative to $(srctree) first.
  83. ifneq ($(realpath $(srctree)/$(KBUILD_EXTMOD) 2>/dev/null),)
  84. module_srcpath := $(srctree)/$(KBUILD_EXTMOD)
  85. else
  86. module_srcpath := $(KBUILD_EXTMOD)
  87. endif
  88. endif
  89. ifeq ($(CONFIG_MODULE_SCMVERSION),y)
  90. # Get the SCM version of the module. `sed` verifies setlocalversion returns
  91. # a proper revision based on the SCM type, e.g. git, mercurial, or svn.
  92. module_scmversion := $(shell $(srctree)/scripts/setlocalversion $(module_srcpath) | \
  93. sed -n 's/.*-\(\(g\|hg\)[a-fA-F0-9]\+\(-dirty\)\?\|svn[0-9]\+\).*/\1/p')
  94. ifneq ($(module_scmversion),)
  95. MODPOST += -v $(module_scmversion)
  96. endif
  97. endif
  98. # modpost options for modules (both in-kernel and external)
  99. MODPOST += \
  100. $(addprefix -i ,$(wildcard $(input-symdump))) \
  101. $(if $(KBUILD_NSDEPS),-d $(MODULES_NSDEPS)) \
  102. $(if $(CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS)$(KBUILD_NSDEPS),-N)
  103. # 'make -i -k' ignores compile errors, and builds as many modules as possible.
  104. ifneq ($(findstring i,$(filter-out --%,$(MAKEFLAGS))),)
  105. MODPOST += -n
  106. endif
  107. # Clear VPATH to not search for *.symvers in $(srctree). Check only $(objtree).
  108. VPATH :=
  109. $(input-symdump):
  110. @echo >&2 'WARNING: Symbol version dump "$@" is missing.'
  111. @echo >&2 ' Modules may not have dependencies or modversions.'
  112. ifdef CONFIG_LTO_CLANG
  113. # With CONFIG_LTO_CLANG, .o files might be LLVM bitcode, so we need to run
  114. # LTO to compile them into native code before running modpost
  115. prelink-ext := .lto
  116. quiet_cmd_cc_lto_link_modules = LTO [M] $@
  117. cmd_cc_lto_link_modules = \
  118. $(LD) $(ld_flags) -r -o $@ \
  119. $(shell [ -s $(@:.lto.o=.o.symversions) ] && \
  120. echo -T $(@:.lto.o=.o.symversions)) \
  121. --whole-archive $^
  122. %.lto.o: %.o
  123. $(call if_changed,cc_lto_link_modules)
  124. endif
  125. modules := $(sort $(shell cat $(MODORDER)))
  126. # Read out modules.order to pass in modpost.
  127. # Otherwise, allmodconfig would fail with "Argument list too long".
  128. quiet_cmd_modpost = MODPOST $@
  129. cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
  130. $(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
  131. $(call if_changed,modpost)
  132. targets += $(output-symdump)
  133. __modpost: $(output-symdump)
  134. ifneq ($(KBUILD_MODPOST_NOFINAL),1)
  135. $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modfinal
  136. endif
  137. PHONY += FORCE
  138. FORCE:
  139. existing-targets := $(wildcard $(sort $(targets)))
  140. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  141. endif
  142. .PHONY: $(PHONY)