Makefile.vmlinux 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. PHONY := __default
  3. __default: vmlinux
  4. include include/config/auto.conf
  5. include $(srctree)/scripts/Kbuild.include
  6. include $(srctree)/scripts/Makefile.lib
  7. targets :=
  8. %.o: %.c FORCE
  9. $(call if_changed_rule,cc_o_c)
  10. %.o: %.S FORCE
  11. $(call if_changed_rule,as_o_S)
  12. # Built-in dtb
  13. # ---------------------------------------------------------------------------
  14. quiet_cmd_wrap_dtbs = WRAP $@
  15. cmd_wrap_dtbs = { \
  16. echo '\#include <asm-generic/vmlinux.lds.h>'; \
  17. echo '.section .dtb.init.rodata,"a"'; \
  18. while read dtb; do \
  19. symbase=__dtb_$$(basename -s .dtb "$${dtb}" | tr - _); \
  20. echo '.balign STRUCT_ALIGNMENT'; \
  21. echo ".global $${symbase}_begin"; \
  22. echo "$${symbase}_begin:"; \
  23. echo '.incbin "'$$dtb'" '; \
  24. echo ".global $${symbase}_end"; \
  25. echo "$${symbase}_end:"; \
  26. done < $<; \
  27. } > $@
  28. .builtin-dtbs.S: .builtin-dtbs-list FORCE
  29. $(call if_changed,wrap_dtbs)
  30. quiet_cmd_gen_dtbs_list = GEN $@
  31. cmd_gen_dtbs_list = \
  32. $(if $(CONFIG_BUILTIN_DTB_NAME), echo "arch/$(SRCARCH)/boot/dts/$(CONFIG_BUILTIN_DTB_NAME).dtb",:) > $@
  33. .builtin-dtbs-list: arch/$(SRCARCH)/boot/dts/dtbs-list FORCE
  34. $(call if_changed,$(if $(CONFIG_BUILTIN_DTB_ALL),copy,gen_dtbs_list))
  35. targets += .builtin-dtbs-list
  36. ifdef CONFIG_GENERIC_BUILTIN_DTB
  37. targets += .builtin-dtbs.S .builtin-dtbs.o
  38. vmlinux.unstripped: .builtin-dtbs.o
  39. endif
  40. # vmlinux.unstripped
  41. # ---------------------------------------------------------------------------
  42. ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX
  43. vmlinux.unstripped: arch/$(SRCARCH)/tools/vmlinux.arch.o
  44. arch/$(SRCARCH)/tools/vmlinux.arch.o: vmlinux.o FORCE
  45. $(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@
  46. endif
  47. ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
  48. # Final link of vmlinux with optional arch pass after final link
  49. cmd_link_vmlinux = \
  50. $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@"; \
  51. $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
  52. targets += vmlinux.unstripped .vmlinux.export.o
  53. vmlinux.unstripped: scripts/link-vmlinux.sh vmlinux.o .vmlinux.export.o $(KBUILD_LDS) FORCE
  54. +$(call if_changed_dep,link_vmlinux)
  55. ifdef CONFIG_DEBUG_INFO_BTF
  56. vmlinux.unstripped: $(RESOLVE_BTFIDS) $(srctree)/scripts/gen-btf.sh
  57. endif
  58. ifdef CONFIG_BUILDTIME_TABLE_SORT
  59. vmlinux.unstripped: scripts/sorttable
  60. endif
  61. # vmlinux
  62. # ---------------------------------------------------------------------------
  63. remove-section-y := .modinfo
  64. remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) += '.rel*' '!.rel*.dyn'
  65. # for compatibility with binutils < 2.32
  66. # https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=c12d9fa2afe7abcbe407a00e15719e1a1350c2a7
  67. remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) += '.rel.*'
  68. remove-symbols := -w --strip-unneeded-symbol='__mod_device_table__*'
  69. # To avoid warnings: "empty loadable segment detected at ..." from GNU objcopy,
  70. # it is necessary to remove the PT_LOAD flag from the segment.
  71. quiet_cmd_strip_relocs = OBJCOPY $@
  72. cmd_strip_relocs = $(OBJCOPY) $(patsubst %,--set-section-flags %=noload,$(remove-section-y)) $< $@; \
  73. $(OBJCOPY) $(addprefix --remove-section=,$(remove-section-y)) $(remove-symbols) $@
  74. targets += vmlinux
  75. vmlinux: vmlinux.unstripped FORCE
  76. $(call if_changed,strip_relocs)
  77. # modules.builtin.modinfo
  78. # ---------------------------------------------------------------------------
  79. # .modinfo in vmlinux.unstripped is aligned to 8 bytes for compatibility with
  80. # tools that expect vmlinux to have sufficiently aligned sections but the
  81. # additional bytes used for padding .modinfo to satisfy this requirement break
  82. # certain versions of kmod with
  83. #
  84. # depmod: ERROR: kmod_builtin_iter_next: unexpected string without modname prefix
  85. #
  86. # Strip the trailing padding bytes after extracting .modinfo to comply with
  87. # what kmod expects to parse.
  88. quiet_cmd_modules_builtin_modinfo = GEN $@
  89. cmd_modules_builtin_modinfo = $(cmd_objcopy); \
  90. sed -i 's/\x00\+$$/\x00/g' $@; \
  91. chmod -x $@
  92. OBJCOPYFLAGS_modules.builtin.modinfo := -j .modinfo -O binary
  93. targets += modules.builtin.modinfo
  94. modules.builtin.modinfo: vmlinux.unstripped FORCE
  95. $(call if_changed,modules_builtin_modinfo)
  96. # modules.builtin
  97. # ---------------------------------------------------------------------------
  98. __default: modules.builtin
  99. # The second line aids cases where multiple modules share the same object.
  100. quiet_cmd_modules_builtin = GEN $@
  101. cmd_modules_builtin = \
  102. tr '\0' '\n' < $< | \
  103. sed -n 's/^[[:alnum:]:_]*\.file=//p' | \
  104. tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$$/.ko/' > $@
  105. targets += modules.builtin
  106. modules.builtin: modules.builtin.modinfo FORCE
  107. $(call if_changed,modules_builtin)
  108. # modules.builtin.ranges
  109. # ---------------------------------------------------------------------------
  110. ifdef CONFIG_BUILTIN_MODULE_RANGES
  111. __default: modules.builtin.ranges
  112. quiet_cmd_modules_builtin_ranges = GEN $@
  113. cmd_modules_builtin_ranges = gawk -f $(real-prereqs) > $@
  114. targets += modules.builtin.ranges
  115. modules.builtin.ranges: $(srctree)/scripts/generate_builtin_ranges.awk \
  116. modules.builtin vmlinux.map vmlinux.o.map FORCE
  117. $(call if_changed,modules_builtin_ranges)
  118. vmlinux.map: vmlinux.unstripped
  119. @:
  120. endif
  121. # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
  122. # ---------------------------------------------------------------------------
  123. PHONY += FORCE
  124. FORCE:
  125. # Read all saved command lines and dependencies for the $(targets) we
  126. # may be building above, using $(if_changed{,_dep}). As an
  127. # optimization, we don't need to read them if the target does not
  128. # exist, we will rebuild anyway in that case.
  129. existing-targets := $(wildcard $(sort $(targets)))
  130. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  131. .PHONY: $(PHONY)