blob: 559a531e09f26c0a53baa7a1feb9fee705b06a76 [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
Masahiro Yamada390b1152019-03-08 14:49:10 +0900284. include/config/auto.conf
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200285
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200286# Update version
287info GEN .version
Masahiro Yamada278ae602017-09-22 14:31:13 +0900288if [ -r .version ]; then
289 VERSION=$(expr 0$(cat .version) + 1)
290 echo $VERSION > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200291else
Masahiro Yamada278ae602017-09-22 14:31:13 +0900292 rm -f .version
293 echo 1 > .version
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200294fi;
295
296# final build of init/
Cao jina7b151f2018-02-21 12:25:07 +0800297${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200298
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100299archive_builtin
300
301#link vmlinux.o
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100302modpost_link vmlinux.o
303
304# modpost vmlinux.o to check for section mismatches
305${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
306
Sami Tolvanenc274f832019-05-08 13:22:12 -0700307if [ -n "${CONFIG_LTO_CLANG}" ]; then
308 # Call recordmcount if needed
309 recordmcount vmlinux.o
310fi
311
Preeti Nagarf097e722019-04-30 15:07:27 +0530312# Generate RTIC MP placeholder compile unit of the correct size
313# and add it to the list of link objects
314# this needs to be done before generating kallsyms
315if [ ! -z ${RTIC_MPGEN+x} ]; then
316 rtic_mp vmlinux.o rtic_mp.o .tmp_rtic_mp_sz1 .tmp_rtic_mp1.c
317 KBUILD_VMLINUX_LIBS+=" "
318 KBUILD_VMLINUX_LIBS+=$RTIC_MP_O
319fi
320
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200321kallsymso=""
322kallsyms_vmlinux=""
323if [ -n "${CONFIG_KALLSYMS}" ]; then
324
325 # kallsyms support
326 # Generate section listing all symbols and add it into vmlinux
327 # It's a three step process:
328 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
329 # but __kallsyms is empty.
330 # Running kallsyms on that gives us .tmp_kallsyms1.o with
331 # the right size
332 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
333 # the right size, but due to the added section, some
334 # addresses have shifted.
335 # From here, we generate a correct .tmp_kallsyms2.o
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100336 # 3) That link may have expanded the kernel image enough that
337 # more linker branch stubs / trampolines had to be added, which
338 # introduces new names, which further expands kallsyms. Do another
339 # pass if that is the case. In theory it's possible this results
340 # in even more stubs, but unlikely.
341 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
342 # other bugs.
343 # 4) The correct ${kallsymso} is linked into the final vmlinux.
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200344 #
345 # a) Verify that the System.map from vmlinux matches the map from
346 # ${kallsymso}.
347
348 kallsymso=.tmp_kallsyms2.o
349 kallsyms_vmlinux=.tmp_vmlinux2
350
351 # step 1
352 vmlinux_link "" .tmp_vmlinux1
353 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
354
355 # step 2
356 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
357 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
358
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100359 # step 3
Michael Forneya670b0b2018-03-18 17:54:02 -0700360 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
361 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100362
363 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200364 kallsymso=.tmp_kallsyms3.o
365 kallsyms_vmlinux=.tmp_vmlinux3
366
367 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
368
369 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
370 fi
371fi
372
Preeti Nagarf097e722019-04-30 15:07:27 +0530373# Update RTIC MP object by replacing the place holder
374# with actual MP data of the same size
375# Also double check that object size did not change
376# Note: Check initilally if RTIC_MP_O is not empty or uninitialized,
377# as incase RTIC_MPGEN is set and failure occurs in RTIC_MP_O
378# generation, below check for comparing object sizes fails
379# due to an empty RTIC_MP_O object.
380if [ ! -z ${RTIC_MP_O} ]; then
381 rtic_mp "${kallsyms_vmlinux}" rtic_mp.o .tmp_rtic_mp_sz2 \
382 .tmp_rtic_mp2.c
383 if ! cmp -s .tmp_rtic_mp_sz1 .tmp_rtic_mp_sz2; then
384 echo >&2 'ERROR: RTIC MP object files size mismatch'
385 exit 1
386 fi
387fi
388
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200389info LD vmlinux
390vmlinux_link "${kallsymso}" vmlinux
391
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700392if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
393 info SORTEX vmlinux
394 sortextable vmlinux
395fi
396
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200397info SYSMAP System.map
398mksysmap vmlinux System.map
399
400# step a (see comment above)
401if [ -n "${CONFIG_KALLSYMS}" ]; then
402 mksysmap ${kallsyms_vmlinux} .tmp_System.map
403
404 if ! cmp -s System.map .tmp_System.map; then
Michal Marek5369f552012-07-07 23:04:40 +0200405 echo >&2 Inconsistent kallsyms data
Michal Marek367e43c2012-08-10 11:55:11 +0200406 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200407 exit 1
408 fi
409fi
James Shao6fe790c2019-08-16 16:04:16 -0700410
411# Starting Android Q, the DTB's are part of dtb.img and not part
412# of the kernel image. RTIC DTS relies on the kernel environment
413# and could not build outside of the kernel. Generate RTIC DTS after
414# successful kernel build if MPGen is enabled. The DTB will be
415# generated with dtb.img in kernel_definitions.mk.
416if [ ! -z ${RTIC_MPGEN+x} ]; then
417 ${RTIC_MPGEN} --objcopy="${OBJCOPY}" --objdump="${OBJDUMP}" \
418 --binpath="" --vmlinux="vmlinux" --config=${KCONFIG_CONFIG} \
Preeti Nagarc34b1ec2019-10-01 12:44:46 +0530419 --cc="${CC} ${KBUILD_AFLAGS}" --dts=rtic_mp.dts \
420 || echo RTIC MP DTS generation has failed
421 # Echo statement above prints the error message in case above
422 # RTIC MP DTS generation command fails and it ensures rtic mp
423 # failure does not cause kernel compilation to fail.
James Shao6fe790c2019-08-16 16:04:16 -0700424fi