Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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# Create ${2}.o file with all symbols from the ${1} object file
110kallsyms()
111{
112 local kallsymopt;
113
114 if is_enabled CONFIG_KALLSYMS_ALL; then
115 kallsymopt="${kallsymopt} --all-symbols"
116 fi
117
118 info KSYMS "${2}.S"
119 scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
120
121 info AS "${2}.o"
122 ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
123 ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} -c -o "${2}.o" "${2}.S"
124
125 kallsymso=${2}.o
126}
127
128# Perform kallsyms for the given temporary vmlinux.
129sysmap_and_kallsyms()
130{
131 mksysmap "${1}" "${1}.syms"
132 kallsyms "${1}.syms" "${1}.kallsyms"
133
134 kallsyms_sysmap=${1}.syms
135}
136
137# Create map file with all symbols from ${1}
138# See mksymap for additional details
139mksysmap()
140{
141 info NM ${2}
142 ${NM} -n "${1}" | sed -f "${srctree}/scripts/mksysmap" > "${2}"
143}
144
145sorttable()
146{
147 ${NM} -S ${1} > .tmp_vmlinux.nm-sort
148 ${objtree}/scripts/sorttable -s .tmp_vmlinux.nm-sort ${1}
149}
150
151cleanup()
152{
153 rm -f .btf.*
154 rm -f .tmp_vmlinux.nm-sort
155 rm -f System.map
156 rm -f vmlinux
157 rm -f vmlinux.map
158}
159
160# Use "make V=1" to debug this script
161case "${KBUILD_VERBOSE}" in
162*1*)
163 set -x
164 ;;
165esac
166
167if [ "$1" = "clean" ]; then
168 cleanup
169 exit 0
170fi
171
172${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
173
174arch_vmlinux_o=
175if is_enabled CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX; then
176 arch_vmlinux_o=arch/${SRCARCH}/tools/vmlinux.arch.o
177fi
178
179btf_vmlinux_bin_o=
180btfids_vmlinux=
181kallsymso=
182strip_debug=
183generate_map=
184
185# Use "make UT=1" to trigger warnings on unused tracepoints
186case "${WARN_ON_UNUSED_TRACEPOINTS}" in
187*1*)
188 ${objtree}/scripts/tracepoint-update vmlinux.o
189 ;;
190esac
191
192if is_enabled CONFIG_KALLSYMS; then
193 true > .tmp_vmlinux0.syms
194 kallsyms .tmp_vmlinux0.syms .tmp_vmlinux0.kallsyms
195fi
196
197if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then
198
199 # The kallsyms linking does not need debug symbols, but the BTF does.
200 if ! is_enabled CONFIG_DEBUG_INFO_BTF; then
201 strip_debug=1
202 fi
203
204 vmlinux_link .tmp_vmlinux1
205fi
206
207if is_enabled CONFIG_DEBUG_INFO_BTF; then
208 info BTF .tmp_vmlinux1
209 if ! ${CONFIG_SHELL} ${srctree}/scripts/gen-btf.sh .tmp_vmlinux1; then
210 echo >&2 "Failed to generate BTF for vmlinux"
211 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
212 exit 1
213 fi
214 btf_vmlinux_bin_o=.tmp_vmlinux1.btf.o
215 btfids_vmlinux=.tmp_vmlinux1.BTF_ids
216fi
217
218if is_enabled CONFIG_KALLSYMS; then
219
220 # kallsyms support
221 # Generate section listing all symbols and add it into vmlinux
222 # It's a four step process:
223 # 0) Generate a dummy __kallsyms with empty symbol list.
224 # 1) Link .tmp_vmlinux1.kallsyms so it has all symbols and sections,
225 # with a dummy __kallsyms.
226 # Running kallsyms on that gives us .tmp_vmlinux1.kallsyms.o with
227 # the right size
228 # 2) Link .tmp_vmlinux2.kallsyms so it now has a __kallsyms section of
229 # the right size, but due to the added section, some
230 # addresses have shifted.
231 # From here, we generate a correct .tmp_vmlinux2.kallsyms.o
232 # 3) That link may have expanded the kernel image enough that
233 # more linker branch stubs / trampolines had to be added, which
234 # introduces new names, which further expands kallsyms. Do another
235 # pass if that is the case. In theory it's possible this results
236 # in even more stubs, but unlikely.
237 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
238 # other bugs.
239 # 4) The correct ${kallsymso} is linked into the final vmlinux.
240 #
241 # a) Verify that the System.map from vmlinux matches the map from
242 # ${kallsymso}.
243
244 # The kallsyms linking does not need debug symbols included.
245 strip_debug=1
246
247 sysmap_and_kallsyms .tmp_vmlinux1
248 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
249
250 vmlinux_link .tmp_vmlinux2
251 sysmap_and_kallsyms .tmp_vmlinux2
252 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
253
254 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
255 vmlinux_link .tmp_vmlinux3
256 sysmap_and_kallsyms .tmp_vmlinux3
257 fi
258fi
259
260strip_debug=
261
262if is_enabled CONFIG_VMLINUX_MAP; then
263 generate_map=1
264fi
265
266vmlinux_link "${VMLINUX}"
267
268if is_enabled CONFIG_DEBUG_INFO_BTF; then
269 info BTFIDS ${VMLINUX}
270 ${RESOLVE_BTFIDS} --patch_btfids ${btfids_vmlinux} ${VMLINUX}
271fi
272
273mksysmap "${VMLINUX}" System.map
274
275if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
276 info SORTTAB "${VMLINUX}"
277 if ! sorttable "${VMLINUX}"; then
278 echo >&2 Failed to sort kernel tables
279 exit 1
280 fi
281fi
282
283# step a (see comment above)
284if is_enabled CONFIG_KALLSYMS; then
285 if ! cmp -s System.map "${kallsyms_sysmap}"; then
286 echo >&2 Inconsistent kallsyms data
287 echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround'
288 exit 1
289 fi
290fi
291
292# For fixdep
293echo "${VMLINUX}: $0" > ".${VMLINUX}.d"