kbuild: link-vmlinux.sh: simplify .version increment
[linux-2.6-block.git] / scripts / link-vmlinux.sh
1 #!/bin/sh
2 #
3 # link vmlinux
4 #
5 # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
6 # $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.o files
7 # from top-level directories in the kernel tree, others are specified in
8 # arch/$(ARCH)/Makefile. Ordering when linking is important, and
9 # $(KBUILD_VMLINUX_INIT) must be first. $(KBUILD_VMLINUX_LIBS) are archives
10 # which are linked conditionally (not within --whole-archive), and do not
11 # require symbol indexes added.
12 #
13 # vmlinux
14 #   ^
15 #   |
16 #   +-< $(KBUILD_VMLINUX_INIT)
17 #   |   +--< init/version.o + more
18 #   |
19 #   +--< $(KBUILD_VMLINUX_MAIN)
20 #   |    +--< drivers/built-in.o mm/built-in.o + more
21 #   |
22 #   +--< $(KBUILD_VMLINUX_LIBS)
23 #   |    +--< lib/lib.a + more
24 #   |
25 #   +-< ${kallsymso} (see description in KALLSYMS section)
26 #
27 # vmlinux version (uname -v) cannot be updated during normal
28 # descending-into-subdirs phase since we do not yet know if we need to
29 # update vmlinux.
30 # Therefore this step is delayed until just before final link of vmlinux.
31 #
32 # System.map is generated to document addresses of all kernel symbols
33
34 # Error out on error
35 set -e
36
37 # Nice output in kbuild format
38 # Will be supressed by "make -s"
39 info()
40 {
41         if [ "${quiet}" != "silent_" ]; then
42                 printf "  %-7s %s\n" ${1} ${2}
43         fi
44 }
45
46 # Thin archive build here makes a final archive with symbol table and indexes
47 # from vmlinux objects INIT and MAIN, which can be used as input to linker.
48 # KBUILD_VMLINUX_LIBS archives should already have symbol table and indexes
49 # added.
50 #
51 # Traditional incremental style of link does not require this step
52 #
53 # built-in.o output file
54 #
55 archive_builtin()
56 {
57         if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
58                 info AR built-in.o
59                 rm -f built-in.o;
60                 ${AR} rcsTP${KBUILD_ARFLAGS} built-in.o                 \
61                                         ${KBUILD_VMLINUX_INIT}          \
62                                         ${KBUILD_VMLINUX_MAIN}
63         fi
64 }
65
66 # Link of vmlinux.o used for section mismatch analysis
67 # ${1} output file
68 modpost_link()
69 {
70         local objects
71
72         if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
73                 objects="--whole-archive                                \
74                         built-in.o                                      \
75                         --no-whole-archive                              \
76                         --start-group                                   \
77                         ${KBUILD_VMLINUX_LIBS}                          \
78                         --end-group"
79         else
80                 objects="${KBUILD_VMLINUX_INIT}                         \
81                         --start-group                                   \
82                         ${KBUILD_VMLINUX_MAIN}                          \
83                         ${KBUILD_VMLINUX_LIBS}                          \
84                         --end-group"
85         fi
86         ${LD} ${LDFLAGS} -r -o ${1} ${objects}
87 }
88
89 # Link of vmlinux
90 # ${1} - optional extra .o files
91 # ${2} - output file
92 vmlinux_link()
93 {
94         local lds="${objtree}/${KBUILD_LDS}"
95         local objects
96
97         if [ "${SRCARCH}" != "um" ]; then
98                 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
99                         objects="--whole-archive                        \
100                                 built-in.o                              \
101                                 --no-whole-archive                      \
102                                 --start-group                           \
103                                 ${KBUILD_VMLINUX_LIBS}                  \
104                                 --end-group                             \
105                                 ${1}"
106                 else
107                         objects="${KBUILD_VMLINUX_INIT}                 \
108                                 --start-group                           \
109                                 ${KBUILD_VMLINUX_MAIN}                  \
110                                 ${KBUILD_VMLINUX_LIBS}                  \
111                                 --end-group                             \
112                                 ${1}"
113                 fi
114
115                 ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}             \
116                         -T ${lds} ${objects}
117         else
118                 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
119                         objects="-Wl,--whole-archive                    \
120                                 built-in.o                              \
121                                 -Wl,--no-whole-archive                  \
122                                 -Wl,--start-group                       \
123                                 ${KBUILD_VMLINUX_LIBS}                  \
124                                 -Wl,--end-group                         \
125                                 ${1}"
126                 else
127                         objects="${KBUILD_VMLINUX_INIT}                 \
128                                 -Wl,--start-group                       \
129                                 ${KBUILD_VMLINUX_MAIN}                  \
130                                 ${KBUILD_VMLINUX_LIBS}                  \
131                                 -Wl,--end-group                         \
132                                 ${1}"
133                 fi
134
135                 ${CC} ${CFLAGS_vmlinux} -o ${2}                         \
136                         -Wl,-T,${lds}                                   \
137                         ${objects}                                      \
138                         -lutil -lrt -lpthread
139                 rm -f linux
140         fi
141 }
142
143
144 # Create ${2} .o file with all symbols from the ${1} object file
145 kallsyms()
146 {
147         info KSYM ${2}
148         local kallsymopt;
149
150         if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
151                 kallsymopt="${kallsymopt} --symbol-prefix=_"
152         fi
153
154         if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
155                 kallsymopt="${kallsymopt} --all-symbols"
156         fi
157
158         if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
159                 kallsymopt="${kallsymopt} --absolute-percpu"
160         fi
161
162         if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
163                 kallsymopt="${kallsymopt} --base-relative"
164         fi
165
166         local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL}               \
167                       ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
168
169         local afile="`basename ${2} .o`.S"
170
171         ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
172         ${CC} ${aflags} -c -o ${2} ${afile}
173 }
174
175 # Create map file with all symbols from ${1}
176 # See mksymap for additional details
177 mksysmap()
178 {
179         ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
180 }
181
182 sortextable()
183 {
184         ${objtree}/scripts/sortextable ${1}
185 }
186
187 # Delete output files in case of error
188 cleanup()
189 {
190         rm -f .tmp_System.map
191         rm -f .tmp_kallsyms*
192         rm -f .tmp_vmlinux*
193         rm -f built-in.o
194         rm -f System.map
195         rm -f vmlinux
196         rm -f vmlinux.o
197 }
198
199 on_exit()
200 {
201         if [ $? -ne 0 ]; then
202                 cleanup
203         fi
204 }
205 trap on_exit EXIT
206
207 on_signals()
208 {
209         exit 1
210 }
211 trap on_signals HUP INT QUIT TERM
212
213 #
214 #
215 # Use "make V=1" to debug this script
216 case "${KBUILD_VERBOSE}" in
217 *1*)
218         set -x
219         ;;
220 esac
221
222 if [ "$1" = "clean" ]; then
223         cleanup
224         exit 0
225 fi
226
227 # We need access to CONFIG_ symbols
228 case "${KCONFIG_CONFIG}" in
229 */*)
230         . "${KCONFIG_CONFIG}"
231         ;;
232 *)
233         # Force using a file from the current directory
234         . "./${KCONFIG_CONFIG}"
235 esac
236
237 # Update version
238 info GEN .version
239 if [ -r .version ]; then
240         VERSION=$(expr 0$(cat .version) + 1)
241         echo $VERSION > .version
242 else
243         rm -f .version
244         echo 1 > .version
245 fi;
246
247 # final build of init/
248 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
249
250 archive_builtin
251
252 #link vmlinux.o
253 info LD vmlinux.o
254 modpost_link vmlinux.o
255
256 # modpost vmlinux.o to check for section mismatches
257 ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
258
259 kallsymso=""
260 kallsyms_vmlinux=""
261 if [ -n "${CONFIG_KALLSYMS}" ]; then
262
263         # kallsyms support
264         # Generate section listing all symbols and add it into vmlinux
265         # It's a three step process:
266         # 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
267         #     but __kallsyms is empty.
268         #     Running kallsyms on that gives us .tmp_kallsyms1.o with
269         #     the right size
270         # 2)  Link .tmp_vmlinux2 so it now has a __kallsyms section of
271         #     the right size, but due to the added section, some
272         #     addresses have shifted.
273         #     From here, we generate a correct .tmp_kallsyms2.o
274         # 3)  That link may have expanded the kernel image enough that
275         #     more linker branch stubs / trampolines had to be added, which
276         #     introduces new names, which further expands kallsyms. Do another
277         #     pass if that is the case. In theory it's possible this results
278         #     in even more stubs, but unlikely.
279         #     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
280         #     other bugs.
281         # 4)  The correct ${kallsymso} is linked into the final vmlinux.
282         #
283         # a)  Verify that the System.map from vmlinux matches the map from
284         #     ${kallsymso}.
285
286         kallsymso=.tmp_kallsyms2.o
287         kallsyms_vmlinux=.tmp_vmlinux2
288
289         # step 1
290         vmlinux_link "" .tmp_vmlinux1
291         kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
292
293         # step 2
294         vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
295         kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
296
297         # step 3
298         size1=$(stat -c "%s" .tmp_kallsyms1.o)
299         size2=$(stat -c "%s" .tmp_kallsyms2.o)
300
301         if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
302                 kallsymso=.tmp_kallsyms3.o
303                 kallsyms_vmlinux=.tmp_vmlinux3
304
305                 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
306
307                 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
308         fi
309 fi
310
311 info LD vmlinux
312 vmlinux_link "${kallsymso}" vmlinux
313
314 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
315         info SORTEX vmlinux
316         sortextable vmlinux
317 fi
318
319 info SYSMAP System.map
320 mksysmap vmlinux System.map
321
322 # step a (see comment above)
323 if [ -n "${CONFIG_KALLSYMS}" ]; then
324         mksysmap ${kallsyms_vmlinux} .tmp_System.map
325
326         if ! cmp -s System.map .tmp_System.map; then
327                 echo >&2 Inconsistent kallsyms data
328                 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
329                 exit 1
330         fi
331 fi