xref: /linux/scripts/Makefile.vmlinux (revision 3e86e4d74c0490e5fc5a7f8de8f29e7579c9ffe5)
1# SPDX-License-Identifier: GPL-2.0-only
2
3PHONY := __default
4__default: vmlinux
5
6include include/config/auto.conf
7include $(srctree)/scripts/Kbuild.include
8include $(srctree)/scripts/Makefile.lib
9
10targets :=
11
12%.o: %.c FORCE
13	$(call if_changed_rule,cc_o_c)
14
15%.o: %.S FORCE
16	$(call if_changed_rule,as_o_S)
17
18# Built-in dtb
19# ---------------------------------------------------------------------------
20
21quiet_cmd_wrap_dtbs = WRAP    $@
22      cmd_wrap_dtbs = {							\
23	echo '\#include <asm-generic/vmlinux.lds.h>';			\
24	echo '.section .dtb.init.rodata,"a"';				\
25	while read dtb; do						\
26		symbase=__dtb_$$(basename -s .dtb "$${dtb}" | tr - _);	\
27		echo '.balign STRUCT_ALIGNMENT';			\
28		echo ".global $${symbase}_begin";			\
29		echo "$${symbase}_begin:";				\
30		echo '.incbin "'$$dtb'" ';				\
31		echo ".global $${symbase}_end";				\
32		echo "$${symbase}_end:";				\
33	done < $<;							\
34	} > $@
35
36.builtin-dtbs.S: .builtin-dtbs-list FORCE
37	$(call if_changed,wrap_dtbs)
38
39quiet_cmd_gen_dtbs_list = GEN     $@
40      cmd_gen_dtbs_list = \
41	$(if $(CONFIG_BUILTIN_DTB_NAME), echo "arch/$(SRCARCH)/boot/dts/$(CONFIG_BUILTIN_DTB_NAME).dtb",:) > $@
42
43.builtin-dtbs-list: arch/$(SRCARCH)/boot/dts/dtbs-list FORCE
44	$(call if_changed,$(if $(CONFIG_BUILTIN_DTB_ALL),copy,gen_dtbs_list))
45
46targets += .builtin-dtbs-list
47
48ifdef CONFIG_GENERIC_BUILTIN_DTB
49targets += .builtin-dtbs.S .builtin-dtbs.o
50vmlinux.unstripped: .builtin-dtbs.o
51endif
52
53# vmlinux.unstripped
54# ---------------------------------------------------------------------------
55
56ifdef CONFIG_MODULES
57targets += .vmlinux.export.o
58vmlinux.unstripped: .vmlinux.export.o
59endif
60
61ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX
62vmlinux.unstripped: arch/$(SRCARCH)/tools/vmlinux.arch.o
63
64arch/$(SRCARCH)/tools/vmlinux.arch.o: vmlinux.o FORCE
65	$(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@
66endif
67
68ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
69
70# Final link of vmlinux with optional arch pass after final link
71cmd_link_vmlinux =							\
72	$< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@";	\
73	$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
74
75targets += vmlinux.unstripped
76vmlinux.unstripped: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) FORCE
77	+$(call if_changed_dep,link_vmlinux)
78ifdef CONFIG_DEBUG_INFO_BTF
79vmlinux.unstripped: $(RESOLVE_BTFIDS)
80endif
81
82ifdef CONFIG_BUILDTIME_TABLE_SORT
83vmlinux.unstripped: scripts/sorttable
84endif
85
86# vmlinux
87# ---------------------------------------------------------------------------
88
89remove-section-y                                   := .modinfo
90remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) += '.rel*'
91
92# To avoid warnings: "empty loadable segment detected at ..." from GNU objcopy,
93# it is necessary to remove the PT_LOAD flag from the segment.
94quiet_cmd_strip_relocs = OBJCOPY $@
95      cmd_strip_relocs = $(OBJCOPY) $(patsubst %,--set-section-flags %=noload,$(remove-section-y)) $< $@; \
96                         $(OBJCOPY) $(addprefix --remove-section=,$(remove-section-y)) $@
97
98targets += vmlinux
99vmlinux: vmlinux.unstripped FORCE
100	$(call if_changed,strip_relocs)
101
102# modules.builtin.ranges
103# ---------------------------------------------------------------------------
104ifdef CONFIG_BUILTIN_MODULE_RANGES
105__default: modules.builtin.ranges
106
107quiet_cmd_modules_builtin_ranges = GEN     $@
108      cmd_modules_builtin_ranges = gawk -f $(real-prereqs) > $@
109
110targets += modules.builtin.ranges
111modules.builtin.ranges: $(srctree)/scripts/generate_builtin_ranges.awk \
112			modules.builtin vmlinux.map vmlinux.o.map FORCE
113	$(call if_changed,modules_builtin_ranges)
114
115vmlinux.map: vmlinux.unstripped
116	@:
117
118endif
119
120# Add FORCE to the prerequisites of a target to force it to be always rebuilt.
121# ---------------------------------------------------------------------------
122
123PHONY += FORCE
124FORCE:
125
126# Read all saved command lines and dependencies for the $(targets) we
127# may be building above, using $(if_changed{,_dep}). As an
128# optimization, we don't need to read them if the target does not
129# exist, we will rebuild anyway in that case.
130
131existing-targets := $(wildcard $(sort $(targets)))
132
133-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
134
135.PHONY: $(PHONY)
136