blob: 4788def71358371b67fa64a22a14d43ff9894300 [file] [log] [blame]
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +02001#!/bin/sh
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +02003#
4# link vmlinux
5#
6# vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
Nicholas Pigginf49821e2018-02-11 00:25:04 +10007# $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.a files
Nicholas Piggin3a166fc2017-06-20 01:52:05 +10008# from top-level directories in the kernel tree, others are specified in
9# arch/$(ARCH)/Makefile. Ordering when linking is important, and
10# $(KBUILD_VMLINUX_INIT) must be first. $(KBUILD_VMLINUX_LIBS) are archives
11# which are linked conditionally (not within --whole-archive), and do not
12# require symbol indexes added.
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020013#
14# vmlinux
15# ^
16# |
17# +-< $(KBUILD_VMLINUX_INIT)
18# | +--< init/version.o + more
19# |
20# +--< $(KBUILD_VMLINUX_MAIN)
Nicholas Pigginf49821e2018-02-11 00:25:04 +100021# | +--< drivers/built-in.a mm/built-in.a + more
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020022# |
Nicholas Piggin3a166fc2017-06-20 01:52:05 +100023# +--< $(KBUILD_VMLINUX_LIBS)
24# | +--< lib/lib.a + more
25# |
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020026# +-< ${kallsymso} (see description in KALLSYMS section)
27#
28# vmlinux version (uname -v) cannot be updated during normal
29# descending-into-subdirs phase since we do not yet know if we need to
30# update vmlinux.
31# Therefore this step is delayed until just before final link of vmlinux.
32#
33# System.map is generated to document addresses of all kernel symbols
34
35# Error out on error
36set -e
37
38# Nice output in kbuild format
39# Will be supressed by "make -s"
40info()
41{
42 if [ "${quiet}" != "silent_" ]; then
43 printf " %-7s %s\n" ${1} ${2}
44 fi
45}
46
47# Link of vmlinux.o used for section mismatch analysis
48# ${1} output file
49modpost_link()
50{
Stephen Rothwella5967db2016-08-24 22:29:19 +100051 local objects
52
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100053 objects="--whole-archive \
Masahiro Yamadadee94952019-01-17 09:10:03 +090054 ${KBUILD_VMLINUX_INIT} \
55 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100056 --no-whole-archive \
57 --start-group \
58 ${KBUILD_VMLINUX_LIBS} \
59 --end-group"
60
Masahiro Yamadad503ac52018-08-24 08:20:39 +090061 ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020062}
63
64# Link of vmlinux
65# ${1} - optional extra .o files
66# ${2} - output file
67vmlinux_link()
68{
69 local lds="${objtree}/${KBUILD_LDS}"
Stephen Rothwella5967db2016-08-24 22:29:19 +100070 local objects
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020071
72 if [ "${SRCARCH}" != "um" ]; then
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100073 objects="--whole-archive \
Masahiro Yamadadee94952019-01-17 09:10:03 +090074 ${KBUILD_VMLINUX_INIT} \
75 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100076 --no-whole-archive \
77 --start-group \
78 ${KBUILD_VMLINUX_LIBS} \
79 --end-group \
80 ${1}"
Stephen Rothwella5967db2016-08-24 22:29:19 +100081
Masahiro Yamadad503ac52018-08-24 08:20:39 +090082 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
Stephen Rothwella5967db2016-08-24 22:29:19 +100083 -T ${lds} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020084 else
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100085 objects="-Wl,--whole-archive \
Masahiro Yamadadee94952019-01-17 09:10:03 +090086 ${KBUILD_VMLINUX_INIT} \
87 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100088 -Wl,--no-whole-archive \
89 -Wl,--start-group \
90 ${KBUILD_VMLINUX_LIBS} \
91 -Wl,--end-group \
92 ${1}"
Stephen Rothwella5967db2016-08-24 22:29:19 +100093
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100094 ${CC} ${CFLAGS_vmlinux} -o ${2} \
95 -Wl,-T,${lds} \
96 ${objects} \
Stephen Rothwella5967db2016-08-24 22:29:19 +100097 -lutil -lrt -lpthread
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020098 rm -f linux
99 fi
100}
101
102
103# Create ${2} .o file with all symbols from the ${1} object file
104kallsyms()
105{
106 info KSYM ${2}
107 local kallsymopt;
108
109 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
James Hogan6895f972012-09-06 22:11:25 +0100110 kallsymopt="${kallsymopt} --all-symbols"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200111 fi
112
Ard Biesheuvel4d5d5662016-03-15 14:58:12 -0700113 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030114 kallsymopt="${kallsymopt} --absolute-percpu"
115 fi
116
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700117 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
118 kallsymopt="${kallsymopt} --base-relative"
119 fi
120
Sam Ravnborg00e6c28c2012-05-08 19:53:46 +0200121 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
122 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200123
Ard Biesheuvela0439342016-02-05 11:25:05 +0100124 local afile="`basename ${2} .o`.S"
125
126 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
127 ${CC} ${aflags} -c -o ${2} ${afile}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200128}
129
130# Create map file with all symbols from ${1}
131# See mksymap for additional details
132mksysmap()
133{
134 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
135}
136
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700137sortextable()
138{
139 ${objtree}/scripts/sortextable ${1}
140}
141
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200142# Delete output files in case of error
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200143cleanup()
144{
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200145 rm -f .tmp_System.map
146 rm -f .tmp_kallsyms*
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200147 rm -f .tmp_vmlinux*
148 rm -f System.map
149 rm -f vmlinux
150 rm -f vmlinux.o
151}
152
Sylvain BERTRANDab160db2015-05-07 00:36:04 +0000153on_exit()
154{
155 if [ $? -ne 0 ]; then
156 cleanup
157 fi
158}
159trap on_exit EXIT
160
161on_signals()
162{
163 exit 1
164}
165trap on_signals HUP INT QUIT TERM
166
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200167#
168#
169# Use "make V=1" to debug this script
170case "${KBUILD_VERBOSE}" in
171*1*)
172 set -x
173 ;;
174esac
175
176if [ "$1" = "clean" ]; then
177 cleanup
178 exit 0
179fi
180
181# We need access to CONFIG_ symbols
Michal Marek423a8152013-02-25 13:47:53 +0100182case "${KCONFIG_CONFIG}" in
183*/*)
184 . "${KCONFIG_CONFIG}"
185 ;;
186*)
187 # Force using a file from the current directory
188 . "./${KCONFIG_CONFIG}"
189esac
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200190
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200191# Update version
192info GEN .version
Masahiro Yamada278ae602017-09-22 14:31:13 +0900193if [ -r .version ]; then
194 VERSION=$(expr 0$(cat .version) + 1)
195 echo $VERSION > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200196else
Masahiro Yamada278ae602017-09-22 14:31:13 +0900197 rm -f .version
198 echo 1 > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200199fi;
200
201# final build of init/
Cao jina7b151f2018-02-21 12:25:07 +0800202${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200203
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100204#link vmlinux.o
205info LD vmlinux.o
206modpost_link vmlinux.o
207
208# modpost vmlinux.o to check for section mismatches
209${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
210
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200211kallsymso=""
212kallsyms_vmlinux=""
213if [ -n "${CONFIG_KALLSYMS}" ]; then
214
215 # kallsyms support
216 # Generate section listing all symbols and add it into vmlinux
217 # It's a three step process:
218 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
219 # but __kallsyms is empty.
220 # Running kallsyms on that gives us .tmp_kallsyms1.o with
221 # the right size
222 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
223 # the right size, but due to the added section, some
224 # addresses have shifted.
225 # From here, we generate a correct .tmp_kallsyms2.o
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100226 # 3) That link may have expanded the kernel image enough that
227 # more linker branch stubs / trampolines had to be added, which
228 # introduces new names, which further expands kallsyms. Do another
229 # pass if that is the case. In theory it's possible this results
230 # in even more stubs, but unlikely.
231 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
232 # other bugs.
233 # 4) The correct ${kallsymso} is linked into the final vmlinux.
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200234 #
235 # a) Verify that the System.map from vmlinux matches the map from
236 # ${kallsymso}.
237
238 kallsymso=.tmp_kallsyms2.o
239 kallsyms_vmlinux=.tmp_vmlinux2
240
241 # step 1
242 vmlinux_link "" .tmp_vmlinux1
243 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
244
245 # step 2
246 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
247 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
248
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100249 # step 3
Michael Forneya670b0b2018-03-18 17:54:02 -0700250 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
251 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100252
253 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200254 kallsymso=.tmp_kallsyms3.o
255 kallsyms_vmlinux=.tmp_vmlinux3
256
257 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
258
259 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
260 fi
261fi
262
263info LD vmlinux
264vmlinux_link "${kallsymso}" vmlinux
265
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700266if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
267 info SORTEX vmlinux
268 sortextable vmlinux
269fi
270
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200271info SYSMAP System.map
272mksysmap vmlinux System.map
273
274# step a (see comment above)
275if [ -n "${CONFIG_KALLSYMS}" ]; then
276 mksysmap ${kallsyms_vmlinux} .tmp_System.map
277
278 if ! cmp -s System.map .tmp_System.map; then
Michal Marek5369f552012-07-07 23:04:40 +0200279 echo >&2 Inconsistent kallsyms data
Michal Marek367e43c2012-08-10 11:55:11 +0200280 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200281 exit 1
282 fi
283fi