xref: /linux-6.15/scripts/link-vmlinux.sh (revision b1a9a5e0)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# link vmlinux
5#
6# vmlinux is linked from the objects in vmlinux.a and $(KBUILD_VMLINUX_LIBS).
7# vmlinux.a contains objects that are linked unconditionally.
8# $(KBUILD_VMLINUX_LIBS) are archives which are linked conditionally
9# (not within --whole-archive), and do not require symbol indexes added.
10#
11# vmlinux
12#   ^
13#   |
14#   +--< vmlinux.a
15#   |
16#   +--< $(KBUILD_VMLINUX_LIBS)
17#   |    +--< lib/lib.a + more
18#   |
19#   +-< ${kallsymso} (see description in KALLSYMS section)
20#
21# vmlinux version (uname -v) cannot be updated during normal
22# descending-into-subdirs phase since we do not yet know if we need to
23# update vmlinux.
24# Therefore this step is delayed until just before final link of vmlinux.
25#
26# System.map is generated to document addresses of all kernel symbols
27
28# Error out on error
29set -e
30
31LD="$1"
32KBUILD_LDFLAGS="$2"
33LDFLAGS_vmlinux="$3"
34
35is_enabled() {
36	grep -q "^$1=y" include/config/auto.conf
37}
38
39# Nice output in kbuild format
40# Will be supressed by "make -s"
41info()
42{
43	printf "  %-7s %s\n" "${1}" "${2}"
44}
45
46# Link of vmlinux
47# ${1} - output file
48vmlinux_link()
49{
50	local output=${1}
51	local objs
52	local libs
53	local ld
54	local ldflags
55	local ldlibs
56
57	info LD ${output}
58
59	# skip output file argument
60	shift
61
62	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
63		# Use vmlinux.o instead of performing the slow LTO link again.
64		objs=vmlinux.o
65		libs=
66	else
67		objs=vmlinux.a
68		libs="${KBUILD_VMLINUX_LIBS}"
69	fi
70
71	if is_enabled CONFIG_MODULES; then
72		objs="${objs} .vmlinux.export.o"
73	fi
74
75	objs="${objs} init/version-timestamp.o"
76
77	if [ "${SRCARCH}" = "um" ]; then
78		wl=-Wl,
79		ld="${CC}"
80		ldflags="${CFLAGS_vmlinux}"
81		ldlibs="-lutil -lrt -lpthread"
82	else
83		wl=
84		ld="${LD}"
85		ldflags="${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}"
86		ldlibs=
87	fi
88
89	ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
90
91	# The kallsyms linking does not need debug symbols included.
92	if [ -n "${strip_debug}" ] ; then
93		ldflags="${ldflags} ${wl}--strip-debug"
94	fi
95
96	if is_enabled CONFIG_VMLINUX_MAP; then
97		ldflags="${ldflags} ${wl}-Map=${output}.map"
98	fi
99
100	${ld} ${ldflags} -o ${output}					\
101		${wl}--whole-archive ${objs} ${wl}--no-whole-archive	\
102		${wl}--start-group ${libs} ${wl}--end-group		\
103		${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs}
104}
105
106# generate .BTF typeinfo from DWARF debuginfo
107# ${1} - vmlinux image
108gen_btf()
109{
110	local pahole_ver
111	local btf_data=${1}.btf.o
112
113	if ! [ -x "$(command -v ${PAHOLE})" ]; then
114		echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
115		return 1
116	fi
117
118	pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
119	if [ "${pahole_ver}" -lt "116" ]; then
120		echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
121		return 1
122	fi
123
124	info BTF "${btf_data}"
125	LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
126
127	# Create ${btf_data} which contains just .BTF section but no symbols. Add
128	# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
129	# deletes all symbols including __start_BTF and __stop_BTF, which will
130	# be redefined in the linker script. Add 2>/dev/null to suppress GNU
131	# objcopy warnings: "empty loadable segment detected at ..."
132	${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
133		--strip-all ${1} "${btf_data}" 2>/dev/null
134	# Change e_type to ET_REL so that it can be used to link final vmlinux.
135	# GNU ld 2.35+ and lld do not allow an ET_EXEC input.
136	if is_enabled CONFIG_CPU_BIG_ENDIAN; then
137		et_rel='\0\1'
138	else
139		et_rel='\1\0'
140	fi
141	printf "${et_rel}" | dd of="${btf_data}" conv=notrunc bs=1 seek=16 status=none
142
143	btf_vmlinux_bin_o=${btf_data}
144}
145
146# Create ${2}.o file with all symbols from the ${1} object file
147kallsyms()
148{
149	local kallsymopt;
150
151	if is_enabled CONFIG_KALLSYMS_ALL; then
152		kallsymopt="${kallsymopt} --all-symbols"
153	fi
154
155	if is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU; then
156		kallsymopt="${kallsymopt} --absolute-percpu"
157	fi
158
159	if is_enabled CONFIG_KALLSYMS_BASE_RELATIVE; then
160		kallsymopt="${kallsymopt} --base-relative"
161	fi
162
163	if is_enabled CONFIG_LTO_CLANG; then
164		kallsymopt="${kallsymopt} --lto-clang"
165	fi
166
167	info KSYMS "${2}.S"
168	scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
169
170	info AS "${2}.o"
171	${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
172	      ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} -c -o "${2}.o" "${2}.S"
173
174	kallsymso=${2}.o
175}
176
177# Perform kallsyms for the given temporary vmlinux.
178sysmap_and_kallsyms()
179{
180	mksysmap "${1}" "${1}.syms"
181	kallsyms "${1}.syms" "${1}.kallsyms"
182
183	kallsyms_sysmap=${1}.syms
184}
185
186# Create map file with all symbols from ${1}
187# See mksymap for additional details
188mksysmap()
189{
190	info NM ${2}
191	${NM} -n "${1}" | sed -f "${srctree}/scripts/mksysmap" > "${2}"
192}
193
194sorttable()
195{
196	${objtree}/scripts/sorttable ${1}
197}
198
199cleanup()
200{
201	rm -f .btf.*
202	rm -f System.map
203	rm -f vmlinux
204	rm -f vmlinux.map
205}
206
207# Use "make V=1" to debug this script
208case "${KBUILD_VERBOSE}" in
209*1*)
210	set -x
211	;;
212esac
213
214if [ "$1" = "clean" ]; then
215	cleanup
216	exit 0
217fi
218
219${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
220
221btf_vmlinux_bin_o=
222kallsymso=
223strip_debug=
224
225if is_enabled CONFIG_KALLSYMS; then
226	kallsyms /dev/null .tmp_vmlinux0.kallsyms
227fi
228
229if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then
230
231	# The kallsyms linking does not need debug symbols, but the BTF does.
232	if ! is_enabled CONFIG_DEBUG_INFO_BTF; then
233		strip_debug=1
234	fi
235
236	vmlinux_link .tmp_vmlinux1
237fi
238
239if is_enabled CONFIG_DEBUG_INFO_BTF; then
240	if ! gen_btf .tmp_vmlinux1; then
241		echo >&2 "Failed to generate BTF for vmlinux"
242		echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
243		exit 1
244	fi
245fi
246
247if is_enabled CONFIG_KALLSYMS; then
248
249	# kallsyms support
250	# Generate section listing all symbols and add it into vmlinux
251	# It's a four step process:
252	# 0)  Generate a dummy __kallsyms with empty symbol list.
253	# 1)  Link .tmp_vmlinux.kallsyms1 so it has all symbols and sections,
254	#     with a dummy __kallsyms.
255	#     Running kallsyms on that gives us .tmp_kallsyms1.o with
256	#     the right size
257	# 2)  Link .tmp_vmlinux.kallsyms2 so it now has a __kallsyms section of
258	#     the right size, but due to the added section, some
259	#     addresses have shifted.
260	#     From here, we generate a correct .tmp_vmlinux.kallsyms2.o
261	# 3)  That link may have expanded the kernel image enough that
262	#     more linker branch stubs / trampolines had to be added, which
263	#     introduces new names, which further expands kallsyms. Do another
264	#     pass if that is the case. In theory it's possible this results
265	#     in even more stubs, but unlikely.
266	#     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
267	#     other bugs.
268	# 4)  The correct ${kallsymso} is linked into the final vmlinux.
269	#
270	# a)  Verify that the System.map from vmlinux matches the map from
271	#     ${kallsymso}.
272
273	# The kallsyms linking does not need debug symbols included.
274	strip_debug=1
275
276	sysmap_and_kallsyms .tmp_vmlinux1
277	size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
278
279	vmlinux_link .tmp_vmlinux2
280	sysmap_and_kallsyms .tmp_vmlinux2
281	size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
282
283	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
284		vmlinux_link .tmp_vmlinux3
285		sysmap_and_kallsyms .tmp_vmlinux3
286	fi
287fi
288
289strip_debug=
290
291vmlinux_link vmlinux
292
293# fill in BTF IDs
294if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then
295	info BTFIDS vmlinux
296	${RESOLVE_BTFIDS} vmlinux
297fi
298
299mksysmap vmlinux System.map
300
301if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
302	info SORTTAB vmlinux
303	if ! sorttable vmlinux; then
304		echo >&2 Failed to sort kernel tables
305		exit 1
306	fi
307fi
308
309# step a (see comment above)
310if is_enabled CONFIG_KALLSYMS; then
311	if ! cmp -s System.map "${kallsyms_sysmap}"; then
312		echo >&2 Inconsistent kallsyms data
313		echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround'
314		exit 1
315	fi
316fi
317
318# For fixdep
319echo "vmlinux: $0" > .vmlinux.d
320