Makefile.modpost 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # ===========================================================================
  2. # Module versions
  3. # ===========================================================================
  4. #
  5. # Stage one of module building created the following:
  6. # a) The individual .o files used for the module
  7. # b) A <module>.o file which is the .o files above linked together
  8. # c) A <module>.mod file in $(MODVERDIR)/, listing the name of the
  9. # the preliminary <module>.o file, plus all .o files
  10. # Stage 2 is handled by this file and does the following
  11. # 1) Find all modules from the files listed in $(MODVERDIR)/
  12. # 2) modpost is then used to
  13. # 3) create one <module>.mod.c file pr. module
  14. # 4) create one Module.symvers file with CRC for all exported symbols
  15. # 5) compile all <module>.mod.c files
  16. # 6) final link of the module to a <module.ko> (or <module.unsigned>) file
  17. # 7) signs the modules to a <module.ko> file
  18. # Step 3 is used to place certain information in the module's ELF
  19. # section, including information such as:
  20. # Version magic (see include/linux/vermagic.h for full details)
  21. # - Kernel release
  22. # - SMP is CONFIG_SMP
  23. # - PREEMPT is CONFIG_PREEMPT
  24. # - GCC Version
  25. # Module info
  26. # - Module version (MODULE_VERSION)
  27. # - Module alias'es (MODULE_ALIAS)
  28. # - Module license (MODULE_LICENSE)
  29. # - See include/linux/module.h for more details
  30. # Step 4 is solely used to allow module versioning in external modules,
  31. # where the CRC of each module is retrieved from the Module.symvers file.
  32. # Step 7 is dependent on CONFIG_MODULE_SIG being enabled.
  33. # KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined
  34. # symbols in the final module linking stage
  35. # KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules.
  36. # This is solely useful to speed up test compiles
  37. PHONY := _modpost
  38. _modpost: __modpost
  39. include include/config/auto.conf
  40. include scripts/Kbuild.include
  41. # When building external modules load the Kbuild file to retrieve EXTRA_SYMBOLS info
  42. ifneq ($(KBUILD_EXTMOD),)
  43. # set src + obj - they may be used when building the .mod.c file
  44. obj := $(KBUILD_EXTMOD)
  45. src := $(obj)
  46. # Include the module's Makefile to find KBUILD_EXTRA_SYMBOLS
  47. include $(if $(wildcard $(KBUILD_EXTMOD)/Kbuild), \
  48. $(KBUILD_EXTMOD)/Kbuild, $(KBUILD_EXTMOD)/Makefile)
  49. endif
  50. include scripts/Makefile.lib
  51. kernelsymfile := $(objtree)/Module.symvers
  52. modulesymfile := $(firstword $(KBUILD_EXTMOD))/Module.symvers
  53. # Step 1), find all modules listed in $(MODVERDIR)/
  54. __modules := $(sort $(shell grep -h '\.ko$$' /dev/null $(wildcard $(MODVERDIR)/*.mod)))
  55. modules := $(patsubst %.o,%.ko, $(wildcard $(__modules:.ko=.o)))
  56. # Stop after building .o files if NOFINAL is set. Makes compile tests quicker
  57. _modpost: $(if $(KBUILD_MODPOST_NOFINAL), $(modules:.ko:.o),$(modules))
  58. ifneq ($(KBUILD_BUILDHOST),$(ARCH))
  59. cross_build := 1
  60. endif
  61. # Step 2), invoke modpost
  62. # Includes step 3,4
  63. modpost = scripts/mod/modpost \
  64. $(if $(CONFIG_MODVERSIONS),-m) \
  65. $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a,) \
  66. $(if $(KBUILD_EXTMOD),-i,-o) $(kernelsymfile) \
  67. $(if $(KBUILD_EXTMOD),-I $(modulesymfile)) \
  68. $(if $(KBUILD_EXTRA_SYMBOLS), $(patsubst %, -e %,$(KBUILD_EXTRA_SYMBOLS))) \
  69. $(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \
  70. $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S) \
  71. $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \
  72. $(if $(cross_build),-c)
  73. quiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules
  74. cmd_modpost = $(modpost) -s
  75. PHONY += __modpost
  76. __modpost: $(modules:.ko=.o) FORCE
  77. $(call cmd,modpost) $(wildcard vmlinux) $(filter-out FORCE,$^)
  78. quiet_cmd_kernel-mod = MODPOST $@
  79. cmd_kernel-mod = $(modpost) $@
  80. vmlinux.o: FORCE
  81. $(call cmd,kernel-mod)
  82. # Declare generated files as targets for modpost
  83. $(symverfile): __modpost ;
  84. $(modules:.ko=.mod.c): __modpost ;
  85. # Step 5), compile all *.mod.c files
  86. # modname is set to make c_flags define KBUILD_MODNAME
  87. modname = $(notdir $(@:.mod.o=))
  88. quiet_cmd_cc_o_c = CC $@
  89. cmd_cc_o_c = $(CC) $(c_flags) $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE) \
  90. -c -o $@ $<
  91. $(modules:.ko=.mod.o): %.mod.o: %.mod.c FORCE
  92. $(call if_changed_dep,cc_o_c)
  93. targets += $(modules:.ko=.mod.o)
  94. # Step 6), final link of the modules
  95. ifneq ($(CONFIG_MODULE_SIG),y)
  96. quiet_cmd_ld_ko_o = LD [M] $@
  97. cmd_ld_ko_o = $(LD) -r $(LDFLAGS) \
  98. $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
  99. -o $@ $(filter-out FORCE,$^)
  100. $(modules): %.ko :%.o %.mod.o FORCE
  101. $(call if_changed,ld_ko_o)
  102. targets += $(modules)
  103. else
  104. quiet_cmd_ld_ko_unsigned_o = LD [M] $@
  105. cmd_ld_ko_unsigned_o = \
  106. $(LD) -r $(LDFLAGS) \
  107. $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
  108. -o $@ $(filter-out FORCE,$^) \
  109. $(if $(AFTER_LINK),; $(AFTER_LINK))
  110. $(modules:.ko=.ko.unsigned): %.ko.unsigned :%.o %.mod.o FORCE
  111. $(call if_changed,ld_ko_unsigned_o)
  112. targets += $(modules:.ko=.ko.unsigned)
  113. # Step 7), sign the modules
  114. MODSECKEY = ./signing_key.priv
  115. MODPUBKEY = ./signing_key.x509
  116. ifeq ($(wildcard $(MODSECKEY))+$(wildcard $(MODPUBKEY)),$(MODSECKEY)+$(MODPUBKEY))
  117. ifeq ($(KBUILD_SRC),)
  118. # no O= is being used
  119. SCRIPTS_DIR := scripts
  120. else
  121. SCRIPTS_DIR := $(KBUILD_SRC)/scripts
  122. endif
  123. SIGN_MODULES := 1
  124. else
  125. SIGN_MODULES := 0
  126. endif
  127. # only sign if it's an in-tree module
  128. ifneq ($(KBUILD_EXTMOD),)
  129. SIGN_MODULES := 0
  130. endif
  131. # We strip the module as best we can - note that using both strip and eu-strip
  132. # results in a smaller module than using either alone.
  133. EU_STRIP = $(shell which eu-strip || echo true)
  134. quiet_cmd_sign_ko_stripped_ko_unsigned = STRIP [M] $@
  135. cmd_sign_ko_stripped_ko_unsigned = \
  136. cp $< $@ && \
  137. strip -x -g $@ && \
  138. $(EU_STRIP) $@
  139. ifeq ($(SIGN_MODULES),1)
  140. quiet_cmd_genkeyid = GENKEYID $@
  141. cmd_genkeyid = \
  142. perl $(SCRIPTS_DIR)/x509keyid $< $<.signer $<.keyid
  143. %.signer %.keyid: %
  144. $(call if_changed,genkeyid)
  145. KEYRING_DEP := $(MODSECKEY) $(MODPUBKEY) $(MODPUBKEY).signer $(MODPUBKEY).keyid
  146. quiet_cmd_sign_ko_ko_stripped = SIGN [M] $@
  147. cmd_sign_ko_ko_stripped = \
  148. sh $(SCRIPTS_DIR)/sign-file $(MODSECKEY) $(MODPUBKEY) $< $@
  149. else
  150. KEYRING_DEP :=
  151. quiet_cmd_sign_ko_ko_unsigned = NO SIGN [M] $@
  152. cmd_sign_ko_ko_unsigned = \
  153. cp $< $@
  154. endif
  155. $(modules): %.ko :%.ko.stripped $(KEYRING_DEP) FORCE
  156. $(call if_changed,sign_ko_ko_stripped)
  157. $(patsubst %.ko,%.ko.stripped,$(modules)): %.ko.stripped :%.ko.unsigned FORCE
  158. $(call if_changed,sign_ko_stripped_ko_unsigned)
  159. targets += $(modules)
  160. endif
  161. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  162. # ---------------------------------------------------------------------------
  163. PHONY += FORCE
  164. FORCE:
  165. # Read all saved command lines and dependencies for the $(targets) we
  166. # may be building above, using $(if_changed{,_dep}). As an
  167. # optimization, we don't need to read them if the target does not
  168. # exist, we will rebuild anyway in that case.
  169. targets := $(wildcard $(sort $(targets)))
  170. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  171. ifneq ($(cmd_files),)
  172. include $(cmd_files)
  173. endif
  174. # Declare the contents of the .PHONY variable as phony. We keep that
  175. # information in a variable se we can use it in if_changed and friends.
  176. .PHONY: $(PHONY)