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