blob: e6818b8e7141e6b9e021d8e781cb94b09293ef1a [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 Piggin3a166fc2017-06-20 01:52:05 +10007# $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.o files
8# 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)
21# | +--< drivers/built-in.o mm/built-in.o + more
22# |
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#
54# built-in.o output file
55#
56archive_builtin()
57{
58 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
59 info AR built-in.o
60 rm -f built-in.o;
Nicholas Piggin9a6cfca2017-06-09 15:24:14 +100061 ${AR} rcsTP${KBUILD_ARFLAGS} built-in.o \
Stephen Rothwella5967db2016-08-24 22:29:19 +100062 ${KBUILD_VMLINUX_INIT} \
63 ${KBUILD_VMLINUX_MAIN}
64 fi
65}
66
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020067# Link of vmlinux.o used for section mismatch analysis
68# ${1} output file
69modpost_link()
70{
Stephen Rothwella5967db2016-08-24 22:29:19 +100071 local objects
72
73 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
Nicholas Piggin3a166fc2017-06-20 01:52:05 +100074 objects="--whole-archive \
75 built-in.o \
76 --no-whole-archive \
77 --start-group \
78 ${KBUILD_VMLINUX_LIBS} \
79 --end-group"
Stephen Rothwella5967db2016-08-24 22:29:19 +100080 else
81 objects="${KBUILD_VMLINUX_INIT} \
82 --start-group \
83 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin3a166fc2017-06-20 01:52:05 +100084 ${KBUILD_VMLINUX_LIBS} \
Stephen Rothwella5967db2016-08-24 22:29:19 +100085 --end-group"
86 fi
87 ${LD} ${LDFLAGS} -r -o ${1} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020088}
89
90# Link of vmlinux
91# ${1} - optional extra .o files
92# ${2} - output file
93vmlinux_link()
94{
95 local lds="${objtree}/${KBUILD_LDS}"
Stephen Rothwella5967db2016-08-24 22:29:19 +100096 local objects
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020097
98 if [ "${SRCARCH}" != "um" ]; then
Stephen Rothwella5967db2016-08-24 22:29:19 +100099 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
Nicholas Piggin3a166fc2017-06-20 01:52:05 +1000100 objects="--whole-archive \
101 built-in.o \
102 --no-whole-archive \
103 --start-group \
104 ${KBUILD_VMLINUX_LIBS} \
105 --end-group \
106 ${1}"
Stephen Rothwella5967db2016-08-24 22:29:19 +1000107 else
108 objects="${KBUILD_VMLINUX_INIT} \
109 --start-group \
110 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin3a166fc2017-06-20 01:52:05 +1000111 ${KBUILD_VMLINUX_LIBS} \
Stephen Rothwella5967db2016-08-24 22:29:19 +1000112 --end-group \
113 ${1}"
114 fi
115
116 ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
117 -T ${lds} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200118 else
Stephen Rothwella5967db2016-08-24 22:29:19 +1000119 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
Nicholas Piggin3a166fc2017-06-20 01:52:05 +1000120 objects="-Wl,--whole-archive \
121 built-in.o \
122 -Wl,--no-whole-archive \
123 -Wl,--start-group \
124 ${KBUILD_VMLINUX_LIBS} \
125 -Wl,--end-group \
126 ${1}"
Stephen Rothwella5967db2016-08-24 22:29:19 +1000127 else
128 objects="${KBUILD_VMLINUX_INIT} \
129 -Wl,--start-group \
130 ${KBUILD_VMLINUX_MAIN} \
Nicholas Piggin3a166fc2017-06-20 01:52:05 +1000131 ${KBUILD_VMLINUX_LIBS} \
Stephen Rothwella5967db2016-08-24 22:29:19 +1000132 -Wl,--end-group \
133 ${1}"
134 fi
135
136 ${CC} ${CFLAGS_vmlinux} -o ${2} \
137 -Wl,-T,${lds} \
138 ${objects} \
139 -lutil -lrt -lpthread
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200140 rm -f linux
141 fi
142}
143
144
145# Create ${2} .o file with all symbols from the ${1} object file
146kallsyms()
147{
148 info KSYM ${2}
149 local kallsymopt;
150
Rusty Russellb92021b2013-03-15 15:04:17 +1030151 if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
152 kallsymopt="${kallsymopt} --symbol-prefix=_"
James Hogan6895f972012-09-06 22:11:25 +0100153 fi
154
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200155 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
James Hogan6895f972012-09-06 22:11:25 +0100156 kallsymopt="${kallsymopt} --all-symbols"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200157 fi
158
Ard Biesheuvel4d5d5662016-03-15 14:58:12 -0700159 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030160 kallsymopt="${kallsymopt} --absolute-percpu"
161 fi
162
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700163 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
164 kallsymopt="${kallsymopt} --base-relative"
165 fi
166
Sam Ravnborg00e6c28c2012-05-08 19:53:46 +0200167 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
168 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200169
Ard Biesheuvela0439342016-02-05 11:25:05 +0100170 local afile="`basename ${2} .o`.S"
171
172 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
173 ${CC} ${aflags} -c -o ${2} ${afile}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200174}
175
176# Create map file with all symbols from ${1}
177# See mksymap for additional details
178mksysmap()
179{
180 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
181}
182
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700183sortextable()
184{
185 ${objtree}/scripts/sortextable ${1}
186}
187
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200188# Delete output files in case of error
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200189cleanup()
190{
191 rm -f .old_version
192 rm -f .tmp_System.map
193 rm -f .tmp_kallsyms*
194 rm -f .tmp_version
195 rm -f .tmp_vmlinux*
Stephen Rothwella5967db2016-08-24 22:29:19 +1000196 rm -f built-in.o
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200197 rm -f System.map
198 rm -f vmlinux
199 rm -f vmlinux.o
200}
201
Sylvain BERTRANDab160db2015-05-07 00:36:04 +0000202on_exit()
203{
204 if [ $? -ne 0 ]; then
205 cleanup
206 fi
207}
208trap on_exit EXIT
209
210on_signals()
211{
212 exit 1
213}
214trap on_signals HUP INT QUIT TERM
215
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200216#
217#
218# Use "make V=1" to debug this script
219case "${KBUILD_VERBOSE}" in
220*1*)
221 set -x
222 ;;
223esac
224
225if [ "$1" = "clean" ]; then
226 cleanup
227 exit 0
228fi
229
230# We need access to CONFIG_ symbols
Michal Marek423a8152013-02-25 13:47:53 +0100231case "${KCONFIG_CONFIG}" in
232*/*)
233 . "${KCONFIG_CONFIG}"
234 ;;
235*)
236 # Force using a file from the current directory
237 . "./${KCONFIG_CONFIG}"
238esac
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200239
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200240# Update version
241info GEN .version
242if [ ! -r .version ]; then
243 rm -f .version;
244 echo 1 >.version;
245else
246 mv .version .old_version;
247 expr 0$(cat .old_version) + 1 >.version;
248fi;
249
250# final build of init/
Emese Revfy6b90bd42016-05-24 00:09:38 +0200251${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200252
Nicholas Pigginabac4c82016-11-24 03:41:43 +1100253archive_builtin
254
255#link vmlinux.o
256info LD vmlinux.o
257modpost_link vmlinux.o
258
259# modpost vmlinux.o to check for section mismatches
260${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
261
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200262kallsymso=""
263kallsyms_vmlinux=""
264if [ -n "${CONFIG_KALLSYMS}" ]; then
265
266 # kallsyms support
267 # Generate section listing all symbols and add it into vmlinux
268 # It's a three step process:
269 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
270 # but __kallsyms is empty.
271 # Running kallsyms on that gives us .tmp_kallsyms1.o with
272 # the right size
273 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
274 # the right size, but due to the added section, some
275 # addresses have shifted.
276 # From here, we generate a correct .tmp_kallsyms2.o
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100277 # 3) That link may have expanded the kernel image enough that
278 # more linker branch stubs / trampolines had to be added, which
279 # introduces new names, which further expands kallsyms. Do another
280 # pass if that is the case. In theory it's possible this results
281 # in even more stubs, but unlikely.
282 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
283 # other bugs.
284 # 4) The correct ${kallsymso} is linked into the final vmlinux.
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200285 #
286 # a) Verify that the System.map from vmlinux matches the map from
287 # ${kallsymso}.
288
289 kallsymso=.tmp_kallsyms2.o
290 kallsyms_vmlinux=.tmp_vmlinux2
291
292 # step 1
293 vmlinux_link "" .tmp_vmlinux1
294 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
295
296 # step 2
297 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
298 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
299
Nicholas Piggin7e2b37c2016-11-24 03:41:37 +1100300 # step 3
301 size1=$(stat -c "%s" .tmp_kallsyms1.o)
302 size2=$(stat -c "%s" .tmp_kallsyms2.o)
303
304 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200305 kallsymso=.tmp_kallsyms3.o
306 kallsyms_vmlinux=.tmp_vmlinux3
307
308 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
309
310 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
311 fi
312fi
313
314info LD vmlinux
315vmlinux_link "${kallsymso}" vmlinux
316
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700317if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
318 info SORTEX vmlinux
319 sortextable vmlinux
320fi
321
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200322info SYSMAP System.map
323mksysmap vmlinux System.map
324
325# step a (see comment above)
326if [ -n "${CONFIG_KALLSYMS}" ]; then
327 mksysmap ${kallsyms_vmlinux} .tmp_System.map
328
329 if ! cmp -s System.map .tmp_System.map; then
Michal Marek5369f552012-07-07 23:04:40 +0200330 echo >&2 Inconsistent kallsyms data
Michal Marek367e43c2012-08-10 11:55:11 +0200331 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200332 exit 1
333 fi
334fi
335
336# We made a new kernel - delete old version file
337rm -f .old_version