• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# link vmlinux
5#
6# vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_OBJS) and
7# $(KBUILD_VMLINUX_LIBS). Most are built-in.a files from top-level directories
8# in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
9# $(KBUILD_VMLINUX_LIBS) are archives which are linked conditionally
10# (not within --whole-archive), and do not require symbol indexes added.
11#
12# vmlinux
13#   ^
14#   |
15#   +--< $(KBUILD_VMLINUX_OBJS)
16#   |    +--< init/built-in.a drivers/built-in.a mm/built-in.a + more
17#   |
18#   +--< $(KBUILD_VMLINUX_LIBS)
19#   |    +--< lib/lib.a + more
20#   |
21#   +-< ${kallsymso} (see description in KALLSYMS section)
22#
23# vmlinux version (uname -v) cannot be updated during normal
24# descending-into-subdirs phase since we do not yet know if we need to
25# update vmlinux.
26# Therefore this step is delayed until just before final link of vmlinux.
27#
28# System.map is generated to document addresses of all kernel symbols
29
30# Error out on error
31set -e
32
33LD="$1"
34KBUILD_LDFLAGS="$2"
35LDFLAGS_vmlinux="$3"
36
37# Nice output in kbuild format
38# Will be supressed by "make -s"
39info()
40{
41	if [ "${quiet}" != "silent_" ]; then
42		printf "  %-7s %s\n" "${1}" "${2}"
43	fi
44}
45
46# Generate a linker script to ensure correct ordering of initcalls.
47gen_initcalls()
48{
49	info GEN .tmp_initcalls.lds
50
51	${PYTHON3} ${srctree}/scripts/jobserver-exec		\
52	${PERL} ${srctree}/scripts/generate_initcall_order.pl	\
53		${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}	\
54		> .tmp_initcalls.lds
55}
56
57# If CONFIG_LTO_CLANG is selected, collect generated symbol versions into
58# .tmp_symversions.lds
59gen_symversions()
60{
61	info GEN .tmp_symversions.lds
62	rm -f .tmp_symversions.lds
63
64	for o in ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}; do
65		if [ -f ${o}.symversions ]; then
66			cat ${o}.symversions >> .tmp_symversions.lds
67		fi
68	done
69}
70
71# Link of vmlinux.o used for section mismatch analysis
72# ${1} output file
73modpost_link()
74{
75	local objects
76	local lds=""
77
78	objects="--whole-archive				\
79		${KBUILD_VMLINUX_OBJS}				\
80		--no-whole-archive				\
81		--start-group					\
82		${KBUILD_VMLINUX_LIBS}				\
83		--end-group"
84
85	if [ -n "${CONFIG_LTO_CLANG}" ]; then
86		gen_initcalls
87		lds="-T .tmp_initcalls.lds"
88
89		if [ -n "${CONFIG_MODVERSIONS}" ]; then
90			gen_symversions
91			lds="${lds} -T .tmp_symversions.lds"
92		fi
93
94		# This might take a while, so indicate that we're doing
95		# an LTO link
96		info LTO ${1}
97	else
98		info LD ${1}
99	fi
100
101	${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${lds} ${objects}
102}
103
104objtool_link()
105{
106	local objtoolopt;
107
108	if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then
109		objtoolopt="check"
110		if [ -n "${CONFIG_CPU_UNRET_ENTRY}" ]; then
111			objtoolopt="${objtoolopt} --unret"
112		fi
113		if [ -z "${CONFIG_FRAME_POINTER}" ]; then
114			objtoolopt="${objtoolopt} --no-fp"
115		fi
116		if [ -n "${CONFIG_GCOV_KERNEL}" ]; then
117			objtoolopt="${objtoolopt} --no-unreachable"
118		fi
119		if [ -n "${CONFIG_RETPOLINE}" ]; then
120			objtoolopt="${objtoolopt} --retpoline"
121		fi
122		if [ -n "${CONFIG_X86_SMAP}" ]; then
123			objtoolopt="${objtoolopt} --uaccess"
124		fi
125		if [ -n "${CONFIG_SLS}" ]; then
126			objtoolopt="${objtoolopt} --sls"
127		fi
128		info OBJTOOL ${1}
129		tools/objtool/objtool ${objtoolopt} ${1}
130	fi
131}
132
133# Link of vmlinux
134# ${1} - output file
135# ${2}, ${3}, ... - optional extra .o files
136vmlinux_link()
137{
138	local lds="${objtree}/${KBUILD_LDS}"
139	local output=${1}
140	local objects
141	local strip_debug
142
143	info LD ${output}
144
145	# skip output file argument
146	shift
147
148	# The kallsyms linking does not need debug symbols included.
149	if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
150		strip_debug=-Wl,--strip-debug
151	fi
152
153	if [ "${SRCARCH}" != "um" ]; then
154		if [ -n "${CONFIG_LTO_CLANG}" ]; then
155			# Use vmlinux.o instead of performing the slow LTO
156			# link again.
157			objects="--whole-archive		\
158				vmlinux.o 			\
159				--no-whole-archive		\
160				${@}"
161		else
162			objects="--whole-archive		\
163				${KBUILD_VMLINUX_OBJS}		\
164				--no-whole-archive		\
165				--start-group			\
166				${KBUILD_VMLINUX_LIBS}		\
167				--end-group			\
168				${@}"
169		fi
170
171		${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}	\
172			${strip_debug#-Wl,}			\
173			-o ${output}				\
174			-T ${lds} ${objects}
175	else
176		objects="-Wl,--whole-archive			\
177			${KBUILD_VMLINUX_OBJS}			\
178			-Wl,--no-whole-archive			\
179			-Wl,--start-group			\
180			${KBUILD_VMLINUX_LIBS}			\
181			-Wl,--end-group				\
182			${@}"
183
184		${CC} ${CFLAGS_vmlinux}				\
185			${strip_debug}				\
186			-o ${output}				\
187			-Wl,-T,${lds}				\
188			${objects}				\
189			-lutil -lrt -lpthread
190		rm -f linux
191	fi
192}
193
194# generate .BTF typeinfo from DWARF debuginfo
195# ${1} - vmlinux image
196# ${2} - file to dump raw BTF data into
197gen_btf()
198{
199	local pahole_ver
200
201	if ! [ -x "$(command -v ${PAHOLE})" ]; then
202		echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
203		return 1
204	fi
205
206	pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
207	if [ "${pahole_ver}" -lt "116" ]; then
208		echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
209		return 1
210	fi
211
212	vmlinux_link ${1}
213
214	info "BTF" ${2}
215	LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
216
217	# Create ${2} which contains just .BTF section but no symbols. Add
218	# SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
219	# deletes all symbols including __start_BTF and __stop_BTF, which will
220	# be redefined in the linker script. Add 2>/dev/null to suppress GNU
221	# objcopy warnings: "empty loadable segment detected at ..."
222	${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
223		--strip-all ${1} ${2} 2>/dev/null
224	# Change e_type to ET_REL so that it can be used to link final vmlinux.
225	# Unlike GNU ld, lld does not allow an ET_EXEC input.
226	printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
227}
228
229# Create ${2} .S file with all symbols from the ${1} object file
230kallsyms()
231{
232	local kallsymopt;
233
234	if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
235		kallsymopt="${kallsymopt} --all-symbols"
236	fi
237
238	if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
239		kallsymopt="${kallsymopt} --absolute-percpu"
240	fi
241
242	if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
243		kallsymopt="${kallsymopt} --base-relative"
244	fi
245
246	info KSYMS ${2}
247	${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2}
248}
249
250# Perform one step in kallsyms generation, including temporary linking of
251# vmlinux.
252kallsyms_step()
253{
254	kallsymso_prev=${kallsymso}
255	kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
256	kallsymso=${kallsyms_vmlinux}.o
257	kallsyms_S=${kallsyms_vmlinux}.S
258
259	vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
260	kallsyms ${kallsyms_vmlinux} ${kallsyms_S}
261
262	info AS ${kallsyms_S}
263	${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
264	      ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
265	      -c -o ${kallsymso} ${kallsyms_S}
266}
267
268# Create map file with all symbols from ${1}
269# See mksymap for additional details
270mksysmap()
271{
272	${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
273}
274
275sorttable()
276{
277	${objtree}/scripts/sorttable ${1}
278}
279
280# Delete output files in case of error
281cleanup()
282{
283	rm -f .btf.*
284	rm -f .tmp_System.map
285	rm -f .tmp_initcalls.lds
286	rm -f .tmp_symversions.lds
287	rm -f .tmp_vmlinux*
288	rm -f System.map
289	rm -f vmlinux
290	rm -f vmlinux.o
291}
292
293on_exit()
294{
295	if [ $? -ne 0 ]; then
296		cleanup
297	fi
298}
299trap on_exit EXIT
300
301on_signals()
302{
303	exit 1
304}
305trap on_signals HUP INT QUIT TERM
306
307# Use "make V=1" to debug this script
308case "${KBUILD_VERBOSE}" in
309*1*)
310	set -x
311	;;
312esac
313
314if [ "$1" = "clean" ]; then
315	cleanup
316	exit 0
317fi
318
319# We need access to CONFIG_ symbols
320. include/config/auto.conf
321
322# Update version
323info GEN .version
324if [ -r .version ]; then
325	VERSION=$(expr 0$(cat .version) + 1)
326	echo $VERSION > .version
327else
328	rm -f .version
329	echo 1 > .version
330fi;
331
332# final build of init/
333${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
334
335#link vmlinux.o
336modpost_link vmlinux.o
337objtool_link vmlinux.o
338
339# modpost vmlinux.o to check for section mismatches
340${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
341
342info MODINFO modules.builtin.modinfo
343${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
344info GEN modules.builtin
345# The second line aids cases where multiple modules share the same object.
346tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' |
347	tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin
348
349btf_vmlinux_bin_o=""
350if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
351	btf_vmlinux_bin_o=.btf.vmlinux.bin.o
352	if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
353		echo >&2 "Failed to generate BTF for vmlinux"
354		echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
355		exit 1
356	fi
357fi
358
359kallsymso=""
360kallsymso_prev=""
361kallsyms_vmlinux=""
362if [ -n "${CONFIG_KALLSYMS}" ]; then
363
364	# kallsyms support
365	# Generate section listing all symbols and add it into vmlinux
366	# It's a three step process:
367	# 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
368	#     but __kallsyms is empty.
369	#     Running kallsyms on that gives us .tmp_kallsyms1.o with
370	#     the right size
371	# 2)  Link .tmp_vmlinux2 so it now has a __kallsyms section of
372	#     the right size, but due to the added section, some
373	#     addresses have shifted.
374	#     From here, we generate a correct .tmp_kallsyms2.o
375	# 3)  That link may have expanded the kernel image enough that
376	#     more linker branch stubs / trampolines had to be added, which
377	#     introduces new names, which further expands kallsyms. Do another
378	#     pass if that is the case. In theory it's possible this results
379	#     in even more stubs, but unlikely.
380	#     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
381	#     other bugs.
382	# 4)  The correct ${kallsymso} is linked into the final vmlinux.
383	#
384	# a)  Verify that the System.map from vmlinux matches the map from
385	#     ${kallsymso}.
386
387	kallsyms_step 1
388	kallsyms_step 2
389
390	# step 3
391	size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
392	size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
393
394	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
395		kallsyms_step 3
396	fi
397fi
398
399vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
400
401# fill in BTF IDs
402if [ -n "${CONFIG_DEBUG_INFO_BTF}" -a -n "${CONFIG_BPF}" ]; then
403	info BTFIDS vmlinux
404	${RESOLVE_BTFIDS} vmlinux
405fi
406
407if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then
408	info SORTTAB vmlinux
409	if ! sorttable vmlinux; then
410		echo >&2 Failed to sort kernel tables
411		exit 1
412	fi
413fi
414
415info SYSMAP System.map
416mksysmap vmlinux System.map
417
418# step a (see comment above)
419if [ -n "${CONFIG_KALLSYMS}" ]; then
420	mksysmap ${kallsyms_vmlinux} .tmp_System.map
421
422	if ! cmp -s System.map .tmp_System.map; then
423		echo >&2 Inconsistent kallsyms data
424		echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
425		exit 1
426	fi
427fi
428