blob: 517c732bbe52b510dd155dbd6029f67f736678b7 [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
Nicholas Piggin3a166fc2017-06-20 01:52:05 +100047# Thin archive build here makes a final archive with symbol table and indexes
48# from vmlinux objects INIT and MAIN, which can be used as input to linker.
49# KBUILD_VMLINUX_LIBS archives should already have symbol table and indexes
50# added.
Stephen Rothwella5967db2016-08-24 22:29:19 +100051#
52# Traditional incremental style of link does not require this step
53#
Nicholas Pigginf49821e2018-02-11 00:25:04 +100054# built-in.a output file
Stephen Rothwella5967db2016-08-24 22:29:19 +100055#
56archive_builtin()
57{
Nicholas Pigginf49821e2018-02-11 00:25:04 +100058 info AR built-in.a
59 rm -f built-in.a;
60 ${AR} rcsTP${KBUILD_ARFLAGS} built-in.a \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +100061 ${KBUILD_VMLINUX_INIT} \
62 ${KBUILD_VMLINUX_MAIN}
Sami Tolvanen0e0752eb2017-11-28 08:48:49 -080063
64 # rebuild with llvm-ar to update the symbol table
65 if [ -n "${CONFIG_LTO_CLANG}" ]; then
66 mv -f built-in.a built-in.a.tmp
67 ${LLVM_AR} rcsT${KBUILD_ARFLAGS} built-in.a $(${AR} t built-in.a.tmp)
68 rm -f built-in.a.tmp
69 fi
70}
71
Sami Tolvanen93185a92017-12-08 15:49:24 -080072# If CONFIG_LTO_CLANG is selected, generate a linker script to ensure correct
73# ordering of initcalls, and with CONFIG_MODVERSIONS also enabled, collect the
74# previously generated symbol versions into the same script.
75lto_lds()
Sami Tolvanen0e0752eb2017-11-28 08:48:49 -080076{
77 if [ -z "${CONFIG_LTO_CLANG}" ]; then
78 return
79 fi
80
Sami Tolvanen93185a92017-12-08 15:49:24 -080081 ${srctree}/scripts/generate_initcall_order.pl \
82 built-in.a ${KBUILD_VMLINUX_LIBS} \
83 > .tmp_lto.lds
84
85 if [ -n "${CONFIG_MODVERSIONS}" ]; then
86 for a in built-in.a ${KBUILD_VMLINUX_LIBS}; do
87 for o in $(${AR} t $a); do
88 if [ -f ${o}.symversions ]; then
89 cat ${o}.symversions >> .tmp_lto.lds
90 fi
91 done
92 done
Sami Tolvanen0e0752eb2017-11-28 08:48:49 -080093 fi
94
Sami Tolvanen93185a92017-12-08 15:49:24 -080095 echo "-T .tmp_lto.lds"
Stephen Rothwella5967db2016-08-24 22:29:19 +100096}
97
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020098# Link of vmlinux.o used for section mismatch analysis
99# ${1} output file
100modpost_link()
101{
Stephen Rothwella5967db2016-08-24 22:29:19 +1000102 local objects
103
Nicholas Piggin6358d6e2018-02-11 00:25:03 +1000104 objects="--whole-archive \
Nicholas Pigginf49821e2018-02-11 00:25:04 +1000105 built-in.a \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +1000106 --no-whole-archive \
107 --start-group \
108 ${KBUILD_VMLINUX_LIBS} \
109 --end-group"
110
Sami Tolvanen0e0752eb2017-11-28 08:48:49 -0800111 if [ -n "${CONFIG_LTO_CLANG}" ]; then
112 # This might take a while, so indicate that we're doing
113 # an LTO link
114 info LTO vmlinux.o
115 fi
116
Sami Tolvanen93185a92017-12-08 15:49:24 -0800117 ${LD} ${KBUILD_LDFLAGS} -r -o ${1} $(lto_lds) ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200118}
119
Sami Tolvanenc274f832019-05-08 13:22:12 -0700120# If CONFIG_LTO_CLANG is selected, we postpone running recordmcount until
121# we have compiled LLVM IR to an object file.
122recordmcount()
123{
124 if [ -z "${CONFIG_LTO_CLANG}" ]; then
125 return
126 fi
127
128 if [ -n "${CONFIG_FTRACE_MCOUNT_RECORD}" ]; then
129 scripts/recordmcount ${RECORDMCOUNT_FLAGS} $*
130 fi
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200131}
132
133# Link of vmlinux
134# ${1} - optional extra .o files
135# ${2} - output file
136vmlinux_link()
137{
138 local lds="${objtree}/${KBUILD_LDS}"
Stephen Rothwella5967db2016-08-24 22:29:19 +1000139 local objects
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200140
141 if [ "${SRCARCH}" != "um" ]; then
Sami Tolvanen0e0752eb2017-11-28 08:48:49 -0800142 if [ -z "${CONFIG_LTO_CLANG}" ]; then
143 objects="--whole-archive \
144 built-in.a \
145 --no-whole-archive \
146 --start-group \
147 ${KBUILD_VMLINUX_LIBS} \
148 --end-group \
149 ${1}"
150 else
151 objects="--start-group \
152 vmlinux.o \
153 --end-group \
154 ${1}"
155 fi
Stephen Rothwella5967db2016-08-24 22:29:19 +1000156
Masahiro Yamadad503ac52018-08-24 08:20:39 +0900157 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
Stephen Rothwella5967db2016-08-24 22:29:19 +1000158 -T ${lds} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200159 else
Nicholas Piggin6358d6e2018-02-11 00:25:03 +1000160 objects="-Wl,--whole-archive \
Nicholas Pigginf49821e2018-02-11 00:25:04 +1000161 built-in.a \
Nicholas Piggin6358d6e2018-02-11 00:25:03 +1000162 -Wl,--no-whole-archive \
163 -Wl,--start-group \
164 ${KBUILD_VMLINUX_LIBS} \
165 -Wl,--end-group \
166 ${1}"
Stephen Rothwella5967db2016-08-24 22:29:19 +1000167
Nicholas Piggin6358d6e2018-02-11 00:25:03 +1000168 ${CC} ${CFLAGS_vmlinux} -o ${2} \
169 -Wl,-T,${lds} \
170 ${objects} \
Stephen Rothwella5967db2016-08-24 22:29:19 +1000171 -lutil -lrt -lpthread
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200172 rm -f linux
173 fi
174}
175
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200176# Create ${2} .o file with all symbols from the ${1} object file
177kallsyms()
178{
179 info KSYM ${2}
180 local kallsymopt;
181
182 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
James Hogan6895f972012-09-06 22:11:25 +0100183 kallsymopt="${kallsymopt} --all-symbols"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200184 fi
185
Ard Biesheuvel4d5d5662016-03-15 14:58:12 -0700186 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030187 kallsymopt="${kallsymopt} --absolute-percpu"
188 fi
189
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700190 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
191 kallsymopt="${kallsymopt} --base-relative"
192 fi
193
Sam Ravnborg00e6c28c2012-05-08 19:53:46 +0200194 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
195 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200196
Ard Biesheuvela0439342016-02-05 11:25:05 +0100197 local afile="`basename ${2} .o`.S"
198
199 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
200 ${CC} ${aflags} -c -o ${2} ${afile}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200201}
202
Preeti Nagarf097e722019-04-30 15:07:27 +0530203# Generates ${2} .o file with RTIC MP's from the ${1} object file (vmlinux)
204# ${3} the file name where the sizes of the RTIC MP structure are stored
205# just in case, save copy of the RTIC mp to ${4}
206# Note: RTIC_MPGEN has to be set if MPGen is available
207rtic_mp()
208{
209 # assume that RTIC_MP_O generation may fail
210 RTIC_MP_O=
211
212 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
213 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
214
215 ${RTIC_MPGEN} --objcopy="${OBJCOPY}" --objdump="${OBJDUMP}" \
216 --binpath='' --vmlinux=${1} --config=${KCONFIG_CONFIG} && \
217 cat rtic_mp.c | ${CC} ${aflags} -c -o ${2} -x c - && \
218 cp rtic_mp.c ${4} && \
219 ${NM} --print-size --size-sort ${2} > ${3} && \
220 RTIC_MP_O=${2} || echo RTIC MP generation has failed
221 # NM - save generated variable sizes for verification
222 # RTIC_MP_O is our retval - great success if set to generated .o file
223 # Echo statement above prints the error message in case any of the
224 # above RTIC MP generation commands fail and it ensures rtic mp failure
225 # does not cause kernel compilation to fail.
226}
227
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200228# 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
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700235sortextable()
236{
237 ${objtree}/scripts/sortextable ${1}
238}
239
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200240# Delete output files in case of error
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200241cleanup()
242{
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200243 rm -f .tmp_System.map
244 rm -f .tmp_kallsyms*
Sami Tolvanen93185a92017-12-08 15:49:24 -0800245 rm -f .tmp_lto.lds
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200246 rm -f .tmp_vmlinux*
Nicholas Pigginf49821e2018-02-11 00:25:04 +1000247 rm -f built-in.a
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200248 rm -f System.map
249 rm -f vmlinux
250 rm -f vmlinux.o
Preeti Nagarf097e722019-04-30 15:07:27 +0530251 rm -f .tmp_rtic_mp_sz*
252 rm -f rtic_mp.*
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200253}
254
Sylvain BERTRANDab160db2015-05-07 00:36:04 +0000255on_exit()
256{
257 if [ $? -ne 0 ]; then
258 cleanup
259 fi
260}
261trap on_exit EXIT
262
263on_signals()
264{
265 exit 1
266}
267trap on_signals HUP INT QUIT TERM
268
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200269#
270#
271# Use "make V=1" to debug this script
272case "${KBUILD_VERBOSE}" in
273*1*)
274 set -x
275 ;;
276esac
277
278if [ "$1" = "clean" ]; then
279 cleanup
280 exit 0
281fi
282
283# We need access to CONFIG_ symbols
Michal Marek423a8152013-02-25 13:47:53 +0100284case "${KCONFIG_CONFIG}" in
285*/*)
286 . "${KCONFIG_CONFIG}"
287 ;;
288*)
289 # Force using a file from the current directory
290 . "./${KCONFIG_CONFIG}"
291esac
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200292
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200293# Update version
294info GEN .version
Masahiro Yamada278ae602017-09-22 14:31:13 +0900295if [ -r .version ]; then
296 VERSION=$(expr 0$(cat .version) + 1)
297 echo $VERSION > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200298else
Masahiro Yamada278ae602017-09-22 14:31:13 +0900299 rm -f .version
300 echo 1 > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200301fi;
302
303# final build of init/
Cao jina7b151f2018-02-21 12:25:07 +0800304${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200305
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100306archive_builtin
307
308#link vmlinux.o
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100309modpost_link vmlinux.o
310
311# modpost vmlinux.o to check for section mismatches
312${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
313
Sami Tolvanenc274f832019-05-08 13:22:12 -0700314if [ -n "${CONFIG_LTO_CLANG}" ]; then
315 # Call recordmcount if needed
316 recordmcount vmlinux.o
317fi
318
Preeti Nagarf097e722019-04-30 15:07:27 +0530319# Generate RTIC MP placeholder compile unit of the correct size
320# and add it to the list of link objects
321# this needs to be done before generating kallsyms
322if [ ! -z ${RTIC_MPGEN+x} ]; then
323 rtic_mp vmlinux.o rtic_mp.o .tmp_rtic_mp_sz1 .tmp_rtic_mp1.c
324 KBUILD_VMLINUX_LIBS+=" "
325 KBUILD_VMLINUX_LIBS+=$RTIC_MP_O
326fi
327
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200328kallsymso=""
329kallsyms_vmlinux=""
330if [ -n "${CONFIG_KALLSYMS}" ]; then
331
332 # kallsyms support
333 # Generate section listing all symbols and add it into vmlinux
334 # It's a three step process:
335 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
336 # but __kallsyms is empty.
337 # Running kallsyms on that gives us .tmp_kallsyms1.o with
338 # the right size
339 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
340 # the right size, but due to the added section, some
341 # addresses have shifted.
342 # From here, we generate a correct .tmp_kallsyms2.o
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100343 # 3) That link may have expanded the kernel image enough that
344 # more linker branch stubs / trampolines had to be added, which
345 # introduces new names, which further expands kallsyms. Do another
346 # pass if that is the case. In theory it's possible this results
347 # in even more stubs, but unlikely.
348 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
349 # other bugs.
350 # 4) The correct ${kallsymso} is linked into the final vmlinux.
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200351 #
352 # a) Verify that the System.map from vmlinux matches the map from
353 # ${kallsymso}.
354
355 kallsymso=.tmp_kallsyms2.o
356 kallsyms_vmlinux=.tmp_vmlinux2
357
358 # step 1
359 vmlinux_link "" .tmp_vmlinux1
360 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
361
362 # step 2
363 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
364 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
365
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100366 # step 3
Michael Forneya670b0b2018-03-18 17:54:02 -0700367 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
368 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100369
370 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200371 kallsymso=.tmp_kallsyms3.o
372 kallsyms_vmlinux=.tmp_vmlinux3
373
374 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
375
376 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
377 fi
378fi
379
Preeti Nagarf097e722019-04-30 15:07:27 +0530380# Update RTIC MP object by replacing the place holder
381# with actual MP data of the same size
382# Also double check that object size did not change
383# Note: Check initilally if RTIC_MP_O is not empty or uninitialized,
384# as incase RTIC_MPGEN is set and failure occurs in RTIC_MP_O
385# generation, below check for comparing object sizes fails
386# due to an empty RTIC_MP_O object.
387if [ ! -z ${RTIC_MP_O} ]; then
388 rtic_mp "${kallsyms_vmlinux}" rtic_mp.o .tmp_rtic_mp_sz2 \
389 .tmp_rtic_mp2.c
390 if ! cmp -s .tmp_rtic_mp_sz1 .tmp_rtic_mp_sz2; then
391 echo >&2 'ERROR: RTIC MP object files size mismatch'
392 exit 1
393 fi
394fi
395
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200396info LD vmlinux
397vmlinux_link "${kallsymso}" vmlinux
398
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700399if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
400 info SORTEX vmlinux
401 sortextable vmlinux
402fi
403
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200404info SYSMAP System.map
405mksysmap vmlinux System.map
406
407# step a (see comment above)
408if [ -n "${CONFIG_KALLSYMS}" ]; then
409 mksysmap ${kallsyms_vmlinux} .tmp_System.map
410
411 if ! cmp -s System.map .tmp_System.map; then
Michal Marek5369f552012-07-07 23:04:40 +0200412 echo >&2 Inconsistent kallsyms data
Michal Marek367e43c2012-08-10 11:55:11 +0200413 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200414 exit 1
415 fi
416fi
James Shao6fe790c2019-08-16 16:04:16 -0700417
418# Starting Android Q, the DTB's are part of dtb.img and not part
419# of the kernel image. RTIC DTS relies on the kernel environment
420# and could not build outside of the kernel. Generate RTIC DTS after
421# successful kernel build if MPGen is enabled. The DTB will be
422# generated with dtb.img in kernel_definitions.mk.
423if [ ! -z ${RTIC_MPGEN+x} ]; then
424 ${RTIC_MPGEN} --objcopy="${OBJCOPY}" --objdump="${OBJDUMP}" \
425 --binpath="" --vmlinux="vmlinux" --config=${KCONFIG_CONFIG} \
Preeti Nagarc34b1ec2019-10-01 12:44:46 +0530426 --cc="${CC} ${KBUILD_AFLAGS}" --dts=rtic_mp.dts \
427 || echo RTIC MP DTS generation has failed
428 # Echo statement above prints the error message in case above
429 # RTIC MP DTS generation command fails and it ensures rtic mp
430 # failure does not cause kernel compilation to fail.
James Shao6fe790c2019-08-16 16:04:16 -0700431fi