Makefile.modfinal 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. # ===========================================================================
  3. # Module final link
  4. # ===========================================================================
  5. PHONY := __modfinal
  6. __modfinal:
  7. include $(objtree)/include/config/auto.conf
  8. include $(srctree)/scripts/Kbuild.include
  9. # for c_flags and objtool_args
  10. include $(srctree)/scripts/Makefile.lib
  11. # find all modules listed in modules.order
  12. modules := $(sort $(shell cat $(MODORDER)))
  13. __modfinal: $(modules)
  14. @:
  15. # modname and part-of-module are set to make c_flags define proper module flags
  16. modname = $(notdir $(@:.mod.o=))
  17. part-of-module = y
  18. quiet_cmd_cc_o_c = CC [M] $@
  19. cmd_cc_o_c = \
  20. $(CC) $(filter-out $(CC_FLAGS_CFI) $(CC_FLAGS_FTRACE), \
  21. $(c_flags)) -c -o $@ $<
  22. %.mod.o: %.mod.c FORCE
  23. $(call if_changed_dep,cc_o_c)
  24. ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
  25. ifdef CONFIG_LTO_CLANG
  26. # With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
  27. # avoid a second slow LTO link
  28. prelink-ext := .lto
  29. # ELF processing was skipped earlier because we didn't have native code,
  30. # so let's now process the prelinked binary before we link the module.
  31. ifdef CONFIG_STACK_VALIDATION
  32. ifneq ($(SKIP_STACK_VALIDATION),1)
  33. cmd_ld_ko_o += \
  34. $(objtree)/tools/objtool/objtool $(objtool_args) \
  35. $(@:.ko=$(prelink-ext).o);
  36. endif # SKIP_STACK_VALIDATION
  37. endif # CONFIG_STACK_VALIDATION
  38. endif # CONFIG_LTO_CLANG
  39. quiet_cmd_ld_ko_o = LD [M] $@
  40. cmd_ld_ko_o += \
  41. $(LD) -r $(KBUILD_LDFLAGS) \
  42. $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
  43. -T scripts/module.lds -o $@ $(filter %.o, $^); \
  44. $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
  45. ifdef CONFIG_CFI_CLANG
  46. # LLVM can drops jump table symbols from the final binary. Add them
  47. # back to make stack traces and other symbol output readable.
  48. cmd_ld_ko_o += ; \
  49. $(srctree)/scripts/generate_cfi_kallsyms.pl --module \
  50. $@ > $(@:.ko=.lds); \
  51. if [ -s $(@:.ko=.lds) ]; then \
  52. $(LD) -r $(KBUILD_LDFLAGS) \
  53. $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
  54. -T $(@:.ko=.lds) \
  55. -o $(@:.ko=.tmp.ko) $@; \
  56. mv -f $(@:.ko=.tmp.ko) $@; \
  57. else \
  58. rm -f $(@:.ko=.lds); \
  59. fi
  60. endif
  61. $(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds FORCE
  62. +$(call if_changed,ld_ko_o)
  63. targets += $(modules) $(modules:.ko=.mod.o)
  64. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  65. # ---------------------------------------------------------------------------
  66. PHONY += FORCE
  67. FORCE:
  68. # Read all saved command lines and dependencies for the $(targets) we
  69. # may be building above, using $(if_changed{,_dep}). As an
  70. # optimization, we don't need to read them if the target does not
  71. # exist, we will rebuild anyway in that case.
  72. existing-targets := $(wildcard $(sort $(targets)))
  73. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  74. .PHONY: $(PHONY)