Merge tag 'linux-kselftest-4.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / scripts / link-vmlinux.sh
blobe6818b8e7141e6b9e021d8e781cb94b09293ef1a
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
4 # link vmlinux
6 # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
7 # $(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.
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 # |
23 # +--< $(KBUILD_VMLINUX_LIBS)
24 # | +--< lib/lib.a + more
25 # |
26 # +-< ${kallsymso} (see description in KALLSYMS section)
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.
33 # System.map is generated to document addresses of all kernel symbols
35 # Error out on error
36 set -e
38 # Nice output in kbuild format
39 # Will be supressed by "make -s"
40 info()
42 if [ "${quiet}" != "silent_" ]; then
43 printf " %-7s %s\n" ${1} ${2}
47 # 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.
52 # Traditional incremental style of link does not require this step
54 # built-in.o output file
56 archive_builtin()
58 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
59 info AR built-in.o
60 rm -f built-in.o;
61 ${AR} rcsTP${KBUILD_ARFLAGS} built-in.o \
62 ${KBUILD_VMLINUX_INIT} \
63 ${KBUILD_VMLINUX_MAIN}
67 # Link of vmlinux.o used for section mismatch analysis
68 # ${1} output file
69 modpost_link()
71 local objects
73 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
74 objects="--whole-archive \
75 built-in.o \
76 --no-whole-archive \
77 --start-group \
78 ${KBUILD_VMLINUX_LIBS} \
79 --end-group"
80 else
81 objects="${KBUILD_VMLINUX_INIT} \
82 --start-group \
83 ${KBUILD_VMLINUX_MAIN} \
84 ${KBUILD_VMLINUX_LIBS} \
85 --end-group"
87 ${LD} ${LDFLAGS} -r -o ${1} ${objects}
90 # Link of vmlinux
91 # ${1} - optional extra .o files
92 # ${2} - output file
93 vmlinux_link()
95 local lds="${objtree}/${KBUILD_LDS}"
96 local objects
98 if [ "${SRCARCH}" != "um" ]; then
99 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
100 objects="--whole-archive \
101 built-in.o \
102 --no-whole-archive \
103 --start-group \
104 ${KBUILD_VMLINUX_LIBS} \
105 --end-group \
106 ${1}"
107 else
108 objects="${KBUILD_VMLINUX_INIT} \
109 --start-group \
110 ${KBUILD_VMLINUX_MAIN} \
111 ${KBUILD_VMLINUX_LIBS} \
112 --end-group \
113 ${1}"
116 ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
117 -T ${lds} ${objects}
118 else
119 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
120 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}"
127 else
128 objects="${KBUILD_VMLINUX_INIT} \
129 -Wl,--start-group \
130 ${KBUILD_VMLINUX_MAIN} \
131 ${KBUILD_VMLINUX_LIBS} \
132 -Wl,--end-group \
133 ${1}"
136 ${CC} ${CFLAGS_vmlinux} -o ${2} \
137 -Wl,-T,${lds} \
138 ${objects} \
139 -lutil -lrt -lpthread
140 rm -f linux
145 # Create ${2} .o file with all symbols from the ${1} object file
146 kallsyms()
148 info KSYM ${2}
149 local kallsymopt;
151 if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
152 kallsymopt="${kallsymopt} --symbol-prefix=_"
155 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
156 kallsymopt="${kallsymopt} --all-symbols"
159 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
160 kallsymopt="${kallsymopt} --absolute-percpu"
163 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
164 kallsymopt="${kallsymopt} --base-relative"
167 local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
168 ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
170 local afile="`basename ${2} .o`.S"
172 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
173 ${CC} ${aflags} -c -o ${2} ${afile}
176 # Create map file with all symbols from ${1}
177 # See mksymap for additional details
178 mksysmap()
180 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
183 sortextable()
185 ${objtree}/scripts/sortextable ${1}
188 # Delete output files in case of error
189 cleanup()
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*
196 rm -f built-in.o
197 rm -f System.map
198 rm -f vmlinux
199 rm -f vmlinux.o
202 on_exit()
204 if [ $? -ne 0 ]; then
205 cleanup
208 trap on_exit EXIT
210 on_signals()
212 exit 1
214 trap on_signals HUP INT QUIT TERM
218 # Use "make V=1" to debug this script
219 case "${KBUILD_VERBOSE}" in
220 *1*)
221 set -x
223 esac
225 if [ "$1" = "clean" ]; then
226 cleanup
227 exit 0
230 # We need access to CONFIG_ symbols
231 case "${KCONFIG_CONFIG}" in
232 */*)
233 . "${KCONFIG_CONFIG}"
236 # Force using a file from the current directory
237 . "./${KCONFIG_CONFIG}"
238 esac
240 # Update version
241 info GEN .version
242 if [ ! -r .version ]; then
243 rm -f .version;
244 echo 1 >.version;
245 else
246 mv .version .old_version;
247 expr 0$(cat .old_version) + 1 >.version;
250 # final build of init/
251 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
253 archive_builtin
255 #link vmlinux.o
256 info LD vmlinux.o
257 modpost_link vmlinux.o
259 # modpost vmlinux.o to check for section mismatches
260 ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
262 kallsymso=""
263 kallsyms_vmlinux=""
264 if [ -n "${CONFIG_KALLSYMS}" ]; then
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
277 # 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.
286 # a) Verify that the System.map from vmlinux matches the map from
287 # ${kallsymso}.
289 kallsymso=.tmp_kallsyms2.o
290 kallsyms_vmlinux=.tmp_vmlinux2
292 # step 1
293 vmlinux_link "" .tmp_vmlinux1
294 kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
296 # step 2
297 vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
298 kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
300 # step 3
301 size1=$(stat -c "%s" .tmp_kallsyms1.o)
302 size2=$(stat -c "%s" .tmp_kallsyms2.o)
304 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
305 kallsymso=.tmp_kallsyms3.o
306 kallsyms_vmlinux=.tmp_vmlinux3
308 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
310 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
314 info LD vmlinux
315 vmlinux_link "${kallsymso}" vmlinux
317 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
318 info SORTEX vmlinux
319 sortextable vmlinux
322 info SYSMAP System.map
323 mksysmap vmlinux System.map
325 # step a (see comment above)
326 if [ -n "${CONFIG_KALLSYMS}" ]; then
327 mksysmap ${kallsyms_vmlinux} .tmp_System.map
329 if ! cmp -s System.map .tmp_System.map; then
330 echo >&2 Inconsistent kallsyms data
331 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
332 exit 1
336 # We made a new kernel - delete old version file
337 rm -f .old_version