]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - scripts/link-vmlinux.sh
kbuild: add support for Clang LTO
[mirror_ubuntu-jammy-kernel.git] / scripts / link-vmlinux.sh
CommitLineData
1f2bfbd0 1#!/bin/sh
b2441318 2# SPDX-License-Identifier: GPL-2.0
1f2bfbd0
SR
3#
4# link vmlinux
5#
d151e971
MY
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.
1f2bfbd0
SR
11#
12# vmlinux
13# ^
14# |
d151e971
MY
15# +--< $(KBUILD_VMLINUX_OBJS)
16# | +--< init/built-in.a drivers/built-in.a mm/built-in.a + more
1f2bfbd0 17# |
3a166fc2
NP
18# +--< $(KBUILD_VMLINUX_LIBS)
19# | +--< lib/lib.a + more
20# |
1f2bfbd0
SR
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
3ec8a5b3
MY
33LD="$1"
34KBUILD_LDFLAGS="$2"
35LDFLAGS_vmlinux="$3"
36
1f2bfbd0
SR
37# Nice output in kbuild format
38# Will be supressed by "make -s"
39info()
40{
41 if [ "${quiet}" != "silent_" ]; then
e83b9f55 42 printf " %-7s %s\n" "${1}" "${2}"
1f2bfbd0
SR
43 fi
44}
45
46# Link of vmlinux.o used for section mismatch analysis
47# ${1} output file
48modpost_link()
49{
a5967db9
SR
50 local objects
51
6358d6e8 52 objects="--whole-archive \
d151e971 53 ${KBUILD_VMLINUX_OBJS} \
6358d6e8
NP
54 --no-whole-archive \
55 --start-group \
56 ${KBUILD_VMLINUX_LIBS} \
57 --end-group"
58
dc5723b0
ST
59 if [ -n "${CONFIG_LTO_CLANG}" ]; then
60 # This might take a while, so indicate that we're doing
61 # an LTO link
62 info LTO ${1}
63 else
64 info LD ${1}
65 fi
66
d503ac53 67 ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
1f2bfbd0
SR
68}
69
6804c1af
PZ
70objtool_link()
71{
72 local objtoolopt;
73
74 if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then
75 objtoolopt="check"
76 if [ -z "${CONFIG_FRAME_POINTER}" ]; then
77 objtoolopt="${objtoolopt} --no-fp"
78 fi
79 if [ -n "${CONFIG_GCOV_KERNEL}" ]; then
80 objtoolopt="${objtoolopt} --no-unreachable"
81 fi
82 if [ -n "${CONFIG_RETPOLINE}" ]; then
83 objtoolopt="${objtoolopt} --retpoline"
84 fi
85 if [ -n "${CONFIG_X86_SMAP}" ]; then
86 objtoolopt="${objtoolopt} --uaccess"
87 fi
88 info OBJTOOL ${1}
89 tools/objtool/objtool ${objtoolopt} ${1}
90 fi
91}
92
1f2bfbd0 93# Link of vmlinux
341dfcf8 94# ${1} - output file
618916a4 95# ${2}, ${3}, ... - optional extra .o files
1f2bfbd0
SR
96vmlinux_link()
97{
98 local lds="${objtree}/${KBUILD_LDS}"
618916a4 99 local output=${1}
a5967db9 100 local objects
af73d78b 101 local strip_debug
1f2bfbd0 102
d7b0827f
LT
103 info LD ${output}
104
618916a4
AN
105 # skip output file argument
106 shift
107
af73d78b
KC
108 # The kallsyms linking does not need debug symbols included.
109 if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
110 strip_debug=-Wl,--strip-debug
111 fi
112
1f2bfbd0 113 if [ "${SRCARCH}" != "um" ]; then
dc5723b0
ST
114 if [ -n "${CONFIG_LTO_CLANG}" ]; then
115 # Use vmlinux.o instead of performing the slow LTO
116 # link again.
117 objects="--whole-archive \
118 vmlinux.o \
119 --no-whole-archive \
120 ${@}"
121 else
122 objects="--whole-archive \
123 ${KBUILD_VMLINUX_OBJS} \
124 --no-whole-archive \
125 --start-group \
126 ${KBUILD_VMLINUX_LIBS} \
127 --end-group \
128 ${@}"
129 fi
a5967db9 130
618916a4 131 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
af73d78b 132 ${strip_debug#-Wl,} \
618916a4 133 -o ${output} \
a5967db9 134 -T ${lds} ${objects}
1f2bfbd0 135 else
6358d6e8 136 objects="-Wl,--whole-archive \
d151e971 137 ${KBUILD_VMLINUX_OBJS} \
6358d6e8
NP
138 -Wl,--no-whole-archive \
139 -Wl,--start-group \
140 ${KBUILD_VMLINUX_LIBS} \
141 -Wl,--end-group \
618916a4 142 ${@}"
a5967db9 143
618916a4 144 ${CC} ${CFLAGS_vmlinux} \
af73d78b 145 ${strip_debug} \
618916a4 146 -o ${output} \
6358d6e8
NP
147 -Wl,-T,${lds} \
148 ${objects} \
a5967db9 149 -lutil -lrt -lpthread
1f2bfbd0
SR
150 rm -f linux
151 fi
152}
153
e83b9f55 154# generate .BTF typeinfo from DWARF debuginfo
341dfcf8
AN
155# ${1} - vmlinux image
156# ${2} - file to dump raw BTF data into
e83b9f55
AN
157gen_btf()
158{
341dfcf8 159 local pahole_ver
e83b9f55 160
581b31c3 161 if ! [ -x "$(command -v ${PAHOLE})" ]; then
2a67a6cc 162 echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
341dfcf8 163 return 1
581b31c3
AN
164 fi
165
e83b9f55 166 pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
21a85bd6
LB
167 if [ "${pahole_ver}" -lt "116" ]; then
168 echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
341dfcf8 169 return 1
e83b9f55
AN
170 fi
171
341dfcf8 172 vmlinux_link ${1}
af73d78b
KC
173
174 info "BTF" ${2}
e83b9f55 175 LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
341dfcf8 176
90ceddcb
FS
177 # Create ${2} which contains just .BTF section but no symbols. Add
178 # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
179 # deletes all symbols including __start_BTF and __stop_BTF, which will
180 # be redefined in the linker script. Add 2>/dev/null to suppress GNU
181 # objcopy warnings: "empty loadable segment detected at ..."
182 ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
183 --strip-all ${1} ${2} 2>/dev/null
184 # Change e_type to ET_REL so that it can be used to link final vmlinux.
185 # Unlike GNU ld, lld does not allow an ET_EXEC input.
186 printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
e83b9f55 187}
1f2bfbd0 188
08beb669 189# Create ${2} .S file with all symbols from the ${1} object file
1f2bfbd0
SR
190kallsyms()
191{
1f2bfbd0
SR
192 local kallsymopt;
193
194 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
6895f97e 195 kallsymopt="${kallsymopt} --all-symbols"
1f2bfbd0
SR
196 fi
197
4d5d5664 198 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
c6bda7c9
RR
199 kallsymopt="${kallsymopt} --absolute-percpu"
200 fi
201
2213e9a6
AB
202 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
203 kallsymopt="${kallsymopt} --base-relative"
204 fi
205
08beb669
MY
206 info KSYMS ${2}
207 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2}
1f2bfbd0
SR
208}
209
8959e392
KC
210# Perform one step in kallsyms generation, including temporary linking of
211# vmlinux.
212kallsyms_step()
213{
214 kallsymso_prev=${kallsymso}
af73d78b
KC
215 kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
216 kallsymso=${kallsyms_vmlinux}.o
08beb669 217 kallsyms_S=${kallsyms_vmlinux}.S
8959e392 218
d7b0827f 219 vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
08beb669
MY
220 kallsyms ${kallsyms_vmlinux} ${kallsyms_S}
221
222 info AS ${kallsyms_S}
223 ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
224 ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
225 -c -o ${kallsymso} ${kallsyms_S}
8959e392
KC
226}
227
1f2bfbd0
SR
228# Create map file with all symbols from ${1}
229# See mksymap for additional details
230mksysmap()
231{
232 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
233}
234
10916706 235sorttable()
1347a2ce 236{
10916706 237 ${objtree}/scripts/sorttable ${1}
1347a2ce
LT
238}
239
1f2bfbd0 240# Delete output files in case of error
1f2bfbd0
SR
241cleanup()
242{
341dfcf8 243 rm -f .btf.*
1f2bfbd0 244 rm -f .tmp_System.map
1f2bfbd0
SR
245 rm -f .tmp_vmlinux*
246 rm -f System.map
247 rm -f vmlinux
248 rm -f vmlinux.o
249}
250
ab160dbb
SB
251on_exit()
252{
253 if [ $? -ne 0 ]; then
254 cleanup
255 fi
256}
257trap on_exit EXIT
258
259on_signals()
260{
261 exit 1
262}
263trap on_signals HUP INT QUIT TERM
264
1f2bfbd0
SR
265# Use "make V=1" to debug this script
266case "${KBUILD_VERBOSE}" in
267*1*)
268 set -x
269 ;;
270esac
271
272if [ "$1" = "clean" ]; then
273 cleanup
274 exit 0
275fi
276
277# We need access to CONFIG_ symbols
94cf8acc 278. include/config/auto.conf
1f2bfbd0 279
1f2bfbd0
SR
280# Update version
281info GEN .version
278ae604
MY
282if [ -r .version ]; then
283 VERSION=$(expr 0$(cat .version) + 1)
284 echo $VERSION > .version
1f2bfbd0 285else
278ae604
MY
286 rm -f .version
287 echo 1 > .version
1f2bfbd0
SR
288fi;
289
290# final build of init/
083bc0e1 291${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
1f2bfbd0 292
abac4c89 293#link vmlinux.o
abac4c89 294modpost_link vmlinux.o
6804c1af 295objtool_link vmlinux.o
abac4c89
NP
296
297# modpost vmlinux.o to check for section mismatches
a721588d 298${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
abac4c89 299
898490c0
AG
300info MODINFO modules.builtin.modinfo
301${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
8b41fc44
MY
302info GEN modules.builtin
303# The second line aids cases where multiple modules share the same object.
304tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' |
305 tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin
898490c0 306
7fd78568 307btf_vmlinux_bin_o=""
341dfcf8 308if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
af73d78b
KC
309 btf_vmlinux_bin_o=.btf.vmlinux.bin.o
310 if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
da5fb182
SF
311 echo >&2 "Failed to generate BTF for vmlinux"
312 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
313 exit 1
341dfcf8
AN
314 fi
315fi
316
1f2bfbd0 317kallsymso=""
8959e392 318kallsymso_prev=""
1f2bfbd0
SR
319kallsyms_vmlinux=""
320if [ -n "${CONFIG_KALLSYMS}" ]; then
321
322 # kallsyms support
323 # Generate section listing all symbols and add it into vmlinux
324 # It's a three step process:
325 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
326 # but __kallsyms is empty.
327 # Running kallsyms on that gives us .tmp_kallsyms1.o with
328 # the right size
329 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
330 # the right size, but due to the added section, some
331 # addresses have shifted.
332 # From here, we generate a correct .tmp_kallsyms2.o
7e2b37c9
NP
333 # 3) That link may have expanded the kernel image enough that
334 # more linker branch stubs / trampolines had to be added, which
335 # introduces new names, which further expands kallsyms. Do another
336 # pass if that is the case. In theory it's possible this results
337 # in even more stubs, but unlikely.
338 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
339 # other bugs.
340 # 4) The correct ${kallsymso} is linked into the final vmlinux.
1f2bfbd0
SR
341 #
342 # a) Verify that the System.map from vmlinux matches the map from
343 # ${kallsymso}.
344
8959e392
KC
345 kallsyms_step 1
346 kallsyms_step 2
1f2bfbd0 347
7e2b37c9 348 # step 3
8959e392
KC
349 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
350 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
7e2b37c9
NP
351
352 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
8959e392 353 kallsyms_step 3
1f2bfbd0
SR
354 fi
355fi
356
d7b0827f 357vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
e83b9f55 358
c9a0f3b8 359# fill in BTF IDs
017dab34
JO
360if [ -n "${CONFIG_DEBUG_INFO_BTF}" -a -n "${CONFIG_BPF}" ]; then
361 info BTFIDS vmlinux
362 ${RESOLVE_BTFIDS} vmlinux
c9a0f3b8
JO
363fi
364
10916706
SZ
365if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then
366 info SORTTAB vmlinux
f14bf6a3
SZ
367 if ! sorttable vmlinux; then
368 echo >&2 Failed to sort kernel tables
369 exit 1
370 fi
1347a2ce
LT
371fi
372
1f2bfbd0
SR
373info SYSMAP System.map
374mksysmap vmlinux System.map
375
376# step a (see comment above)
377if [ -n "${CONFIG_KALLSYMS}" ]; then
378 mksysmap ${kallsyms_vmlinux} .tmp_System.map
379
380 if ! cmp -s System.map .tmp_System.map; then
5369f550 381 echo >&2 Inconsistent kallsyms data
367e43c5 382 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
1f2bfbd0
SR
383 exit 1
384 fi
385fi