]> asedeno.scripts.mit.edu Git - linux.git/blob - scripts/link-vmlinux.sh
kbuild: remove top-level built-in.a
[linux.git] / scripts / link-vmlinux.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # link vmlinux
5 #
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.a 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.
13 #
14 # vmlinux
15 #   ^
16 #   |
17 #   +-< $(KBUILD_VMLINUX_INIT)
18 #   |   +--< init/version.o + more
19 #   |
20 #   +--< $(KBUILD_VMLINUX_MAIN)
21 #   |    +--< drivers/built-in.a mm/built-in.a + more
22 #   |
23 #   +--< $(KBUILD_VMLINUX_LIBS)
24 #   |    +--< lib/lib.a + more
25 #   |
26 #   +-< ${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
36 set -e
37
38 # Nice output in kbuild format
39 # Will be supressed by "make -s"
40 info()
41 {
42         if [ "${quiet}" != "silent_" ]; then
43                 printf "  %-7s %s\n" ${1} ${2}
44         fi
45 }
46
47 # Link of vmlinux.o used for section mismatch analysis
48 # ${1} output file
49 modpost_link()
50 {
51         local objects
52
53         objects="--whole-archive                                \
54                 ${KBUILD_VMLINUX_INIT}                          \
55                 ${KBUILD_VMLINUX_MAIN}                          \
56                 --no-whole-archive                              \
57                 --start-group                                   \
58                 ${KBUILD_VMLINUX_LIBS}                          \
59                 --end-group"
60
61         ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
62 }
63
64 # Link of vmlinux
65 # ${1} - optional extra .o files
66 # ${2} - output file
67 vmlinux_link()
68 {
69         local lds="${objtree}/${KBUILD_LDS}"
70         local objects
71
72         if [ "${SRCARCH}" != "um" ]; then
73                 objects="--whole-archive                        \
74                         ${KBUILD_VMLINUX_INIT}                  \
75                         ${KBUILD_VMLINUX_MAIN}                  \
76                         --no-whole-archive                      \
77                         --start-group                           \
78                         ${KBUILD_VMLINUX_LIBS}                  \
79                         --end-group                             \
80                         ${1}"
81
82                 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}      \
83                         -T ${lds} ${objects}
84         else
85                 objects="-Wl,--whole-archive                    \
86                         ${KBUILD_VMLINUX_INIT}                  \
87                         ${KBUILD_VMLINUX_MAIN}                  \
88                         -Wl,--no-whole-archive                  \
89                         -Wl,--start-group                       \
90                         ${KBUILD_VMLINUX_LIBS}                  \
91                         -Wl,--end-group                         \
92                         ${1}"
93
94                 ${CC} ${CFLAGS_vmlinux} -o ${2}                 \
95                         -Wl,-T,${lds}                           \
96                         ${objects}                              \
97                         -lutil -lrt -lpthread
98                 rm -f linux
99         fi
100 }
101
102
103 # Create ${2} .o file with all symbols from the ${1} object file
104 kallsyms()
105 {
106         info KSYM ${2}
107         local kallsymopt;
108
109         if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
110                 kallsymopt="${kallsymopt} --all-symbols"
111         fi
112
113         if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
114                 kallsymopt="${kallsymopt} --absolute-percpu"
115         fi
116
117         if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
118                 kallsymopt="${kallsymopt} --base-relative"
119         fi
120
121         local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL}               \
122                       ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
123
124         local afile="`basename ${2} .o`.S"
125
126         ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
127         ${CC} ${aflags} -c -o ${2} ${afile}
128 }
129
130 # Create map file with all symbols from ${1}
131 # See mksymap for additional details
132 mksysmap()
133 {
134         ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
135 }
136
137 sortextable()
138 {
139         ${objtree}/scripts/sortextable ${1}
140 }
141
142 # Delete output files in case of error
143 cleanup()
144 {
145         rm -f .tmp_System.map
146         rm -f .tmp_kallsyms*
147         rm -f .tmp_vmlinux*
148         rm -f System.map
149         rm -f vmlinux
150         rm -f vmlinux.o
151 }
152
153 on_exit()
154 {
155         if [ $? -ne 0 ]; then
156                 cleanup
157         fi
158 }
159 trap on_exit EXIT
160
161 on_signals()
162 {
163         exit 1
164 }
165 trap on_signals HUP INT QUIT TERM
166
167 #
168 #
169 # Use "make V=1" to debug this script
170 case "${KBUILD_VERBOSE}" in
171 *1*)
172         set -x
173         ;;
174 esac
175
176 if [ "$1" = "clean" ]; then
177         cleanup
178         exit 0
179 fi
180
181 # We need access to CONFIG_ symbols
182 case "${KCONFIG_CONFIG}" in
183 */*)
184         . "${KCONFIG_CONFIG}"
185         ;;
186 *)
187         # Force using a file from the current directory
188         . "./${KCONFIG_CONFIG}"
189 esac
190
191 # Update version
192 info GEN .version
193 if [ -r .version ]; then
194         VERSION=$(expr 0$(cat .version) + 1)
195         echo $VERSION > .version
196 else
197         rm -f .version
198         echo 1 > .version
199 fi;
200
201 # final build of init/
202 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
203
204 #link vmlinux.o
205 info LD vmlinux.o
206 modpost_link vmlinux.o
207
208 # modpost vmlinux.o to check for section mismatches
209 ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
210
211 kallsymso=""
212 kallsyms_vmlinux=""
213 if [ -n "${CONFIG_KALLSYMS}" ]; then
214
215         # kallsyms support
216         # Generate section listing all symbols and add it into vmlinux
217         # It's a three step process:
218         # 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
219         #     but __kallsyms is empty.
220         #     Running kallsyms on that gives us .tmp_kallsyms1.o with
221         #     the right size
222         # 2)  Link .tmp_vmlinux2 so it now has a __kallsyms section of
223         #     the right size, but due to the added section, some
224         #     addresses have shifted.
225         #     From here, we generate a correct .tmp_kallsyms2.o
226         # 3)  That link may have expanded the kernel image enough that
227         #     more linker branch stubs / trampolines had to be added, which
228         #     introduces new names, which further expands kallsyms. Do another
229         #     pass if that is the case. In theory it's possible this results
230         #     in even more stubs, but unlikely.
231         #     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
232         #     other bugs.
233         # 4)  The correct ${kallsymso} is linked into the final vmlinux.
234         #
235         # a)  Verify that the System.map from vmlinux matches the map from
236         #     ${kallsymso}.
237
238         kallsymso=.tmp_kallsyms2.o
239         kallsyms_vmlinux=.tmp_vmlinux2
240
241         # step 1
242         vmlinux_link "" .tmp_vmlinux1
243         kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
244
245         # step 2
246         vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
247         kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
248
249         # step 3
250         size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
251         size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
252
253         if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
254                 kallsymso=.tmp_kallsyms3.o
255                 kallsyms_vmlinux=.tmp_vmlinux3
256
257                 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
258
259                 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
260         fi
261 fi
262
263 info LD vmlinux
264 vmlinux_link "${kallsymso}" vmlinux
265
266 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
267         info SORTEX vmlinux
268         sortextable vmlinux
269 fi
270
271 info SYSMAP System.map
272 mksysmap vmlinux System.map
273
274 # step a (see comment above)
275 if [ -n "${CONFIG_KALLSYMS}" ]; then
276         mksysmap ${kallsyms_vmlinux} .tmp_System.map
277
278         if ! cmp -s System.map .tmp_System.map; then
279                 echo >&2 Inconsistent kallsyms data
280                 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
281                 exit 1
282         fi
283 fi