xref: /openbmc/linux/scripts/link-vmlinux.sh (revision 6355592e)
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
33# Nice output in kbuild format
34# Will be supressed by "make -s"
35info()
36{
37	if [ "${quiet}" != "silent_" ]; then
38		printf "  %-7s %s\n" "${1}" "${2}"
39	fi
40}
41
42# Link of vmlinux.o used for section mismatch analysis
43# ${1} output file
44modpost_link()
45{
46	local objects
47
48	objects="--whole-archive				\
49		${KBUILD_VMLINUX_OBJS}				\
50		--no-whole-archive				\
51		--start-group					\
52		${KBUILD_VMLINUX_LIBS}				\
53		--end-group"
54
55	${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
56}
57
58# Link of vmlinux
59# ${1} - output file
60# ${2}, ${3}, ... - optional extra .o files
61vmlinux_link()
62{
63	local lds="${objtree}/${KBUILD_LDS}"
64	local output=${1}
65	local objects
66
67	# skip output file argument
68	shift
69
70	if [ "${SRCARCH}" != "um" ]; then
71		objects="--whole-archive			\
72			${KBUILD_VMLINUX_OBJS}			\
73			--no-whole-archive			\
74			--start-group				\
75			${KBUILD_VMLINUX_LIBS}			\
76			--end-group				\
77			${@}"
78
79		${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}	\
80			-o ${output}				\
81			-T ${lds} ${objects}
82	else
83		objects="-Wl,--whole-archive			\
84			${KBUILD_VMLINUX_OBJS}			\
85			-Wl,--no-whole-archive			\
86			-Wl,--start-group			\
87			${KBUILD_VMLINUX_LIBS}			\
88			-Wl,--end-group				\
89			${@}"
90
91		${CC} ${CFLAGS_vmlinux}				\
92			-o ${output}				\
93			-Wl,-T,${lds}				\
94			${objects}				\
95			-lutil -lrt -lpthread
96		rm -f linux
97	fi
98}
99
100# generate .BTF typeinfo from DWARF debuginfo
101# ${1} - vmlinux image
102# ${2} - file to dump raw BTF data into
103gen_btf()
104{
105	local pahole_ver
106	local bin_arch
107
108	if ! [ -x "$(command -v ${PAHOLE})" ]; then
109		info "BTF" "${1}: pahole (${PAHOLE}) is not available"
110		return 1
111	fi
112
113	pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
114	if [ "${pahole_ver}" -lt "113" ]; then
115		info "BTF" "${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13"
116		return 1
117	fi
118
119	info "BTF" ${2}
120	vmlinux_link ${1}
121	LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
122
123	# dump .BTF section into raw binary file to link with final vmlinux
124	bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \
125		cut -d, -f1 | cut -d' ' -f2)
126	bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \
127		awk '{print $4}')
128	${OBJCOPY} --dump-section .BTF=.btf.vmlinux.bin ${1} 2>/dev/null
129	${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \
130		--rename-section .data=.BTF .btf.vmlinux.bin ${2}
131}
132
133# Create ${2} .o file with all symbols from the ${1} object file
134kallsyms()
135{
136	info KSYM ${2}
137	local kallsymopt;
138
139	if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
140		kallsymopt="${kallsymopt} --all-symbols"
141	fi
142
143	if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
144		kallsymopt="${kallsymopt} --absolute-percpu"
145	fi
146
147	if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
148		kallsymopt="${kallsymopt} --base-relative"
149	fi
150
151	local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL}               \
152		      ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
153
154	local afile="`basename ${2} .o`.S"
155
156	${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
157	${CC} ${aflags} -c -o ${2} ${afile}
158}
159
160# Create map file with all symbols from ${1}
161# See mksymap for additional details
162mksysmap()
163{
164	${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
165}
166
167sortextable()
168{
169	${objtree}/scripts/sortextable ${1}
170}
171
172# Delete output files in case of error
173cleanup()
174{
175	rm -f .btf.*
176	rm -f .tmp_System.map
177	rm -f .tmp_kallsyms*
178	rm -f .tmp_vmlinux*
179	rm -f System.map
180	rm -f vmlinux
181	rm -f vmlinux.o
182}
183
184on_exit()
185{
186	if [ $? -ne 0 ]; then
187		cleanup
188	fi
189}
190trap on_exit EXIT
191
192on_signals()
193{
194	exit 1
195}
196trap on_signals HUP INT QUIT TERM
197
198#
199#
200# Use "make V=1" to debug this script
201case "${KBUILD_VERBOSE}" in
202*1*)
203	set -x
204	;;
205esac
206
207if [ "$1" = "clean" ]; then
208	cleanup
209	exit 0
210fi
211
212# We need access to CONFIG_ symbols
213. include/config/auto.conf
214
215# Update version
216info GEN .version
217if [ -r .version ]; then
218	VERSION=$(expr 0$(cat .version) + 1)
219	echo $VERSION > .version
220else
221	rm -f .version
222	echo 1 > .version
223fi;
224
225# final build of init/
226${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
227
228#link vmlinux.o
229info LD vmlinux.o
230modpost_link vmlinux.o
231
232# modpost vmlinux.o to check for section mismatches
233${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
234
235info MODINFO modules.builtin.modinfo
236${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
237
238btf_vmlinux_bin_o=""
239if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
240	if gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then
241		btf_vmlinux_bin_o=.btf.vmlinux.bin.o
242	fi
243fi
244
245kallsymso=""
246kallsyms_vmlinux=""
247if [ -n "${CONFIG_KALLSYMS}" ]; then
248
249	# kallsyms support
250	# Generate section listing all symbols and add it into vmlinux
251	# It's a three step process:
252	# 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
253	#     but __kallsyms is empty.
254	#     Running kallsyms on that gives us .tmp_kallsyms1.o with
255	#     the right size
256	# 2)  Link .tmp_vmlinux2 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_kallsyms2.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	kallsymso=.tmp_kallsyms2.o
273	kallsyms_vmlinux=.tmp_vmlinux2
274
275	# step 1
276	vmlinux_link .tmp_vmlinux1 ${btf_vmlinux_bin_o}
277	kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
278
279	# step 2
280	vmlinux_link .tmp_vmlinux2 .tmp_kallsyms1.o ${btf_vmlinux_bin_o}
281	kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
282
283	# step 3
284	size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
285	size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
286
287	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
288		kallsymso=.tmp_kallsyms3.o
289		kallsyms_vmlinux=.tmp_vmlinux3
290
291		vmlinux_link .tmp_vmlinux3 .tmp_kallsyms2.o ${btf_vmlinux_bin_o}
292		kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
293	fi
294fi
295
296info LD vmlinux
297vmlinux_link vmlinux "${kallsymso}" "${btf_vmlinux_bin_o}"
298
299if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
300	info SORTEX vmlinux
301	sortextable vmlinux
302fi
303
304info SYSMAP System.map
305mksysmap vmlinux System.map
306
307# step a (see comment above)
308if [ -n "${CONFIG_KALLSYMS}" ]; then
309	mksysmap ${kallsyms_vmlinux} .tmp_System.map
310
311	if ! cmp -s System.map .tmp_System.map; 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