blob: d16dc7c0b5d132f78816161e2293915a358a6003 [file] [log] [blame]
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +02001#!/bin/sh
2#
3# link vmlinux
4#
5# vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
6# $(KBUILD_VMLINUX_MAIN). Most are built-in.o files from top-level directories
7# in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
8# Ordering when linking is important, and $(KBUILD_VMLINUX_INIT) must be first.
9#
10# vmlinux
11# ^
12# |
13# +-< $(KBUILD_VMLINUX_INIT)
14# | +--< init/version.o + more
15# |
16# +--< $(KBUILD_VMLINUX_MAIN)
17# | +--< drivers/built-in.o mm/built-in.o + 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
31# Nice output in kbuild format
32# Will be supressed by "make -s"
33info()
34{
35 if [ "${quiet}" != "silent_" ]; then
36 printf " %-7s %s\n" ${1} ${2}
37 fi
38}
39
Stephen Rothwella5967db2016-08-24 22:29:19 +100040# Thin archive build here makes a final archive with
41# symbol table and indexes from vmlinux objects, which can be
42# used as input to linker.
43#
44# Traditional incremental style of link does not require this step
45#
46# built-in.o output file
47#
48archive_builtin()
49{
50 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
51 info AR built-in.o
52 rm -f built-in.o;
53 ${AR} rcsT${KBUILD_ARFLAGS} built-in.o \
54 ${KBUILD_VMLINUX_INIT} \
55 ${KBUILD_VMLINUX_MAIN}
Sami Tolvanen475bdd72017-11-28 08:48:49 -080056
57 if [ -n "${CONFIG_LTO_CLANG}" ]; then
58 mv -f built-in.o built-in.o.tmp
59 ${LLVM_AR} rcsT${KBUILD_ARFLAGS} built-in.o $(${AR} t built-in.o.tmp)
60 rm -f built-in.o.tmp
61 fi
Stephen Rothwella5967db2016-08-24 22:29:19 +100062 fi
63}
64
Sami Tolvanen475bdd72017-11-28 08:48:49 -080065# If CONFIG_LTO_CLANG is selected, collect generated symbol versions into
66# .tmp_symversions
67modversions()
68{
69 if [ -z "${CONFIG_LTO_CLANG}" ]; then
70 return
71 fi
72
73 if [ -z "${CONFIG_MODVERSIONS}" ]; then
74 return
75 fi
76
77 rm -f .tmp_symversions
78
79 for a in built-in.o ${KBUILD_VMLINUX_LIBS}; do
80 for o in $(${AR} t $a); do
81 if [ -f ${o}.symversions ]; then
82 cat ${o}.symversions >> .tmp_symversions
83 fi
84 done
85 done
86
87 echo "-T .tmp_symversions"
88}
89
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +020090# Link of vmlinux.o used for section mismatch analysis
91# ${1} output file
92modpost_link()
93{
Stephen Rothwella5967db2016-08-24 22:29:19 +100094 local objects
95
96 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
97 objects="--whole-archive built-in.o"
98 else
99 objects="${KBUILD_VMLINUX_INIT} \
100 --start-group \
101 ${KBUILD_VMLINUX_MAIN} \
102 --end-group"
103 fi
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800104
105 if [ -n "${CONFIG_LTO_CLANG}" ]; then
106 # This might take a while, so indicate that we're doing
107 # an LTO link
108 info LTO vmlinux.o
109 else
110 info LD vmlinux.o
111 fi
112
113 ${LD} ${LDFLAGS} -r -o ${1} $(modversions) ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200114}
115
116# Link of vmlinux
117# ${1} - optional extra .o files
118# ${2} - output file
119vmlinux_link()
120{
121 local lds="${objtree}/${KBUILD_LDS}"
Stephen Rothwella5967db2016-08-24 22:29:19 +1000122 local objects
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200123
124 if [ "${SRCARCH}" != "um" ]; then
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800125 local ld=${LD}
126 local ldflags="${LDFLAGS} ${LDFLAGS_vmlinux}"
127
128 if [ -n "${LDFINAL_vmlinux}" ]; then
129 ld=${LDFINAL_vmlinux}
130 ldflags="${LDFLAGS_FINAL_vmlinux} ${LDFLAGS_vmlinux}"
131 fi
132
133 if [[ -n "${CONFIG_THIN_ARCHIVES}" && -z "${CONFIG_LTO_CLANG}" ]]; then
Stephen Rothwella5967db2016-08-24 22:29:19 +1000134 objects="--whole-archive built-in.o ${1}"
135 else
136 objects="${KBUILD_VMLINUX_INIT} \
137 --start-group \
138 ${KBUILD_VMLINUX_MAIN} \
139 --end-group \
140 ${1}"
141 fi
142
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800143 ${ld} ${ldflags} -o ${2} -T ${lds} ${objects}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200144 else
Stephen Rothwella5967db2016-08-24 22:29:19 +1000145 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
146 objects="-Wl,--whole-archive built-in.o ${1}"
147 else
148 objects="${KBUILD_VMLINUX_INIT} \
149 -Wl,--start-group \
150 ${KBUILD_VMLINUX_MAIN} \
151 -Wl,--end-group \
152 ${1}"
153 fi
154
155 ${CC} ${CFLAGS_vmlinux} -o ${2} \
156 -Wl,-T,${lds} \
157 ${objects} \
158 -lutil -lrt -lpthread
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200159 rm -f linux
160 fi
161}
162
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200163# Create ${2} .o file with all symbols from the ${1} object file
164kallsyms()
165{
166 info KSYM ${2}
167 local kallsymopt;
168
Rusty Russellb92021b2013-03-15 15:04:17 +1030169 if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
170 kallsymopt="${kallsymopt} --symbol-prefix=_"
James Hogan6895f972012-09-06 22:11:25 +0100171 fi
172
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200173 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
James Hogan6895f972012-09-06 22:11:25 +0100174 kallsymopt="${kallsymopt} --all-symbols"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200175 fi
176
Ard Biesheuvel4d5d5662016-03-15 14:58:12 -0700177 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030178 kallsymopt="${kallsymopt} --absolute-percpu"
179 fi
180
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700181 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
182 kallsymopt="${kallsymopt} --base-relative"
183 fi
184
Sam Ravnborg00e6c28c2012-05-08 19:53:46 +0200185 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
186 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200187
Ard Biesheuvela0439342016-02-05 11:25:05 +0100188 local afile="`basename ${2} .o`.S"
189
190 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
191 ${CC} ${aflags} -c -o ${2} ${afile}
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200192}
193
194# Create map file with all symbols from ${1}
195# See mksymap for additional details
196mksysmap()
197{
198 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
199}
200
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700201sortextable()
202{
203 ${objtree}/scripts/sortextable ${1}
204}
205
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200206# Delete output files in case of error
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200207cleanup()
208{
209 rm -f .old_version
210 rm -f .tmp_System.map
211 rm -f .tmp_kallsyms*
212 rm -f .tmp_version
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800213 rm -f .tmp_symversions
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200214 rm -f .tmp_vmlinux*
Stephen Rothwella5967db2016-08-24 22:29:19 +1000215 rm -f built-in.o
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200216 rm -f System.map
217 rm -f vmlinux
218 rm -f vmlinux.o
219}
220
Sylvain BERTRANDab160db2015-05-07 00:36:04 +0000221on_exit()
222{
223 if [ $? -ne 0 ]; then
224 cleanup
225 fi
226}
227trap on_exit EXIT
228
229on_signals()
230{
231 exit 1
232}
233trap on_signals HUP INT QUIT TERM
234
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200235#
236#
237# Use "make V=1" to debug this script
238case "${KBUILD_VERBOSE}" in
239*1*)
240 set -x
241 ;;
242esac
243
244if [ "$1" = "clean" ]; then
245 cleanup
246 exit 0
247fi
248
249# We need access to CONFIG_ symbols
Michal Marek423a8152013-02-25 13:47:53 +0100250case "${KCONFIG_CONFIG}" in
251*/*)
252 . "${KCONFIG_CONFIG}"
253 ;;
254*)
255 # Force using a file from the current directory
256 . "./${KCONFIG_CONFIG}"
257esac
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200258
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200259# Update version
260info GEN .version
261if [ ! -r .version ]; then
262 rm -f .version;
263 echo 1 >.version;
264else
265 mv .version .old_version;
266 expr 0$(cat .old_version) + 1 >.version;
267fi;
268
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800269archive_builtin
270
271#link vmlinux.o
272modpost_link vmlinux.o
273
274# modpost vmlinux.o to check for section mismatches
275${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
276
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200277# final build of init/
Emese Revfy6b90bd42016-05-24 00:09:38 +0200278${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200279
Sami Tolvanen475bdd72017-11-28 08:48:49 -0800280if [ -n "${CONFIG_LTO_CLANG}" ]; then
281 # Re-use vmlinux.o, so we can avoid the slow LTO link step in
282 # vmlinux_link
283 KBUILD_VMLINUX_INIT=
284 KBUILD_VMLINUX_MAIN=vmlinux.o
285fi
286
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200287kallsymso=""
288kallsyms_vmlinux=""
289if [ -n "${CONFIG_KALLSYMS}" ]; then
290
291 # kallsyms support
292 # Generate section listing all symbols and add it into vmlinux
293 # It's a three step process:
294 # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
295 # but __kallsyms is empty.
296 # Running kallsyms on that gives us .tmp_kallsyms1.o with
297 # the right size
298 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
299 # the right size, but due to the added section, some
300 # addresses have shifted.
301 # From here, we generate a correct .tmp_kallsyms2.o
302 # 2a) We may use an extra pass as this has been necessary to
303 # woraround some alignment related bugs.
304 # KALLSYMS_EXTRA_PASS=1 is used to trigger this.
305 # 3) The correct ${kallsymso} is linked into the final vmlinux.
306 #
307 # a) Verify that the System.map from vmlinux matches the map from
308 # ${kallsymso}.
309
310 kallsymso=.tmp_kallsyms2.o
311 kallsyms_vmlinux=.tmp_vmlinux2
312
313 # step 1
314 vmlinux_link "" .tmp_vmlinux1
315 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
316
317 # step 2
318 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
319 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
320
321 # step 2a
322 if [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
323 kallsymso=.tmp_kallsyms3.o
324 kallsyms_vmlinux=.tmp_vmlinux3
325
326 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
327
328 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
329 fi
330fi
331
332info LD vmlinux
333vmlinux_link "${kallsymso}" vmlinux
334
Linus Torvalds1347a2ce2012-05-28 10:32:28 -0700335if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
336 info SORTEX vmlinux
337 sortextable vmlinux
338fi
339
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200340info SYSMAP System.map
341mksysmap vmlinux System.map
342
343# step a (see comment above)
344if [ -n "${CONFIG_KALLSYMS}" ]; then
345 mksysmap ${kallsyms_vmlinux} .tmp_System.map
346
347 if ! cmp -s System.map .tmp_System.map; then
Michal Marek5369f552012-07-07 23:04:40 +0200348 echo >&2 Inconsistent kallsyms data
Michal Marek367e43c2012-08-10 11:55:11 +0200349 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
Sam Ravnborg1f2bfbd2012-05-05 10:18:41 +0200350 exit 1
351 fi
352fi
353
354# We made a new kernel - delete old version file
355rm -f .old_version