Blame view

scripts/link-vmlinux.sh 7.76 KB
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
1
  #!/bin/sh
b24413180   Greg Kroah-Hartman   License cleanup: ...
2
  # SPDX-License-Identifier: GPL-2.0
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
3
4
5
6
  #
  # link vmlinux
  #
  # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
3a166fc2d   Nicholas Piggin   kbuild: handle li...
7
8
9
10
11
12
  # $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.o files
  # from top-level directories in the kernel tree, others are specified in
  # arch/$(ARCH)/Makefile. Ordering when linking is important, and
  # $(KBUILD_VMLINUX_INIT) must be first. $(KBUILD_VMLINUX_LIBS) are archives
  # which are linked conditionally (not within --whole-archive), and do not
  # require symbol indexes added.
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
13
14
15
16
17
18
19
20
21
22
  #
  # vmlinux
  #   ^
  #   |
  #   +-< $(KBUILD_VMLINUX_INIT)
  #   |   +--< init/version.o + more
  #   |
  #   +--< $(KBUILD_VMLINUX_MAIN)
  #   |    +--< drivers/built-in.o mm/built-in.o + more
  #   |
3a166fc2d   Nicholas Piggin   kbuild: handle li...
23
24
25
  #   +--< $(KBUILD_VMLINUX_LIBS)
  #   |    +--< lib/lib.a + more
  #   |
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #   +-< ${kallsymso} (see description in KALLSYMS section)
  #
  # vmlinux version (uname -v) cannot be updated during normal
  # descending-into-subdirs phase since we do not yet know if we need to
  # update vmlinux.
  # Therefore this step is delayed until just before final link of vmlinux.
  #
  # System.map is generated to document addresses of all kernel symbols
  
  # Error out on error
  set -e
  
  # Nice output in kbuild format
  # Will be supressed by "make -s"
  info()
  {
  	if [ "${quiet}" != "silent_" ]; then
  		printf "  %-7s %s
  " ${1} ${2}
  	fi
  }
3a166fc2d   Nicholas Piggin   kbuild: handle li...
47
48
49
50
  # Thin archive build here makes a final archive with symbol table and indexes
  # from vmlinux objects INIT and MAIN, which can be used as input to linker.
  # KBUILD_VMLINUX_LIBS archives should already have symbol table and indexes
  # added.
a5967db9a   Stephen Rothwell   kbuild: allow arc...
51
52
53
54
55
56
57
58
59
60
  #
  # Traditional incremental style of link does not require this step
  #
  # built-in.o output file
  #
  archive_builtin()
  {
  	if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  		info AR built-in.o
  		rm -f built-in.o;
9a6cfca4f   Nicholas Piggin   kbuild: thin arch...
61
  		${AR} rcsTP${KBUILD_ARFLAGS} built-in.o			\
a5967db9a   Stephen Rothwell   kbuild: allow arc...
62
63
64
65
  					${KBUILD_VMLINUX_INIT}		\
  					${KBUILD_VMLINUX_MAIN}
  	fi
  }
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
66
67
68
69
  # Link of vmlinux.o used for section mismatch analysis
  # ${1} output file
  modpost_link()
  {
a5967db9a   Stephen Rothwell   kbuild: allow arc...
70
71
72
  	local objects
  
  	if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
3a166fc2d   Nicholas Piggin   kbuild: handle li...
73
74
75
76
77
78
  		objects="--whole-archive				\
  			built-in.o					\
  			--no-whole-archive				\
  			--start-group					\
  			${KBUILD_VMLINUX_LIBS}				\
  			--end-group"
a5967db9a   Stephen Rothwell   kbuild: allow arc...
79
80
81
82
  	else
  		objects="${KBUILD_VMLINUX_INIT}				\
  			--start-group					\
  			${KBUILD_VMLINUX_MAIN}				\
3a166fc2d   Nicholas Piggin   kbuild: handle li...
83
  			${KBUILD_VMLINUX_LIBS}				\
a5967db9a   Stephen Rothwell   kbuild: allow arc...
84
85
86
  			--end-group"
  	fi
  	${LD} ${LDFLAGS} -r -o ${1} ${objects}
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
87
88
89
90
91
92
93
94
  }
  
  # Link of vmlinux
  # ${1} - optional extra .o files
  # ${2} - output file
  vmlinux_link()
  {
  	local lds="${objtree}/${KBUILD_LDS}"
a5967db9a   Stephen Rothwell   kbuild: allow arc...
95
  	local objects
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
96
97
  
  	if [ "${SRCARCH}" != "um" ]; then
a5967db9a   Stephen Rothwell   kbuild: allow arc...
98
  		if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
3a166fc2d   Nicholas Piggin   kbuild: handle li...
99
100
101
102
103
104
105
  			objects="--whole-archive			\
  				built-in.o				\
  				--no-whole-archive			\
  				--start-group				\
  				${KBUILD_VMLINUX_LIBS}			\
  				--end-group				\
  				${1}"
a5967db9a   Stephen Rothwell   kbuild: allow arc...
106
107
108
109
  		else
  			objects="${KBUILD_VMLINUX_INIT}			\
  				--start-group				\
  				${KBUILD_VMLINUX_MAIN}			\
3a166fc2d   Nicholas Piggin   kbuild: handle li...
110
  				${KBUILD_VMLINUX_LIBS}			\
a5967db9a   Stephen Rothwell   kbuild: allow arc...
111
112
113
114
115
116
  				--end-group				\
  				${1}"
  		fi
  
  		${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}		\
  			-T ${lds} ${objects}
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
117
  	else
a5967db9a   Stephen Rothwell   kbuild: allow arc...
118
  		if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
3a166fc2d   Nicholas Piggin   kbuild: handle li...
119
120
121
122
123
124
125
  			objects="-Wl,--whole-archive			\
  				built-in.o				\
  				-Wl,--no-whole-archive			\
  				-Wl,--start-group			\
  				${KBUILD_VMLINUX_LIBS}			\
  				-Wl,--end-group				\
  				${1}"
a5967db9a   Stephen Rothwell   kbuild: allow arc...
126
127
128
129
  		else
  			objects="${KBUILD_VMLINUX_INIT}			\
  				-Wl,--start-group			\
  				${KBUILD_VMLINUX_MAIN}			\
3a166fc2d   Nicholas Piggin   kbuild: handle li...
130
  				${KBUILD_VMLINUX_LIBS}			\
a5967db9a   Stephen Rothwell   kbuild: allow arc...
131
132
133
134
135
136
137
138
  				-Wl,--end-group				\
  				${1}"
  		fi
  
  		${CC} ${CFLAGS_vmlinux} -o ${2}				\
  			-Wl,-T,${lds}					\
  			${objects}					\
  			-lutil -lrt -lpthread
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
139
140
141
142
143
144
145
146
147
148
  		rm -f linux
  	fi
  }
  
  
  # Create ${2} .o file with all symbols from the ${1} object file
  kallsyms()
  {
  	info KSYM ${2}
  	local kallsymopt;
b92021b09   Rusty Russell   CONFIG_SYMBOL_PRE...
149
150
  	if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
  		kallsymopt="${kallsymopt} --symbol-prefix=_"
6895f97e1   James Hogan   kbuild: add symbo...
151
  	fi
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
152
  	if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
6895f97e1   James Hogan   kbuild: add symbo...
153
  		kallsymopt="${kallsymopt} --all-symbols"
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
154
  	fi
4d5d5664c   Ard Biesheuvel   x86: kallsyms: di...
155
  	if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
c6bda7c98   Rusty Russell   kallsyms: fix per...
156
157
  		kallsymopt="${kallsymopt} --absolute-percpu"
  	fi
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
158
159
160
  	if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  		kallsymopt="${kallsymopt} --base-relative"
  	fi
00e6c28c6   Sam Ravnborg   kbuild: fix ia64 ...
161
162
  	local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL}               \
  		      ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
163

a04393420   Ard Biesheuvel   scripts/link-vmli...
164
165
166
167
  	local afile="`basename ${2} .o`.S"
  
  	${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
  	${CC} ${aflags} -c -o ${2} ${afile}
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
168
169
170
171
172
173
174
175
  }
  
  # Create map file with all symbols from ${1}
  # See mksymap for additional details
  mksysmap()
  {
  	${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  }
1347a2ceb   Linus Torvalds   Merge branch 'kbu...
176
177
178
179
  sortextable()
  {
  	${objtree}/scripts/sortextable ${1}
  }
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
180
  # Delete output files in case of error
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
181
182
183
184
185
186
187
  cleanup()
  {
  	rm -f .old_version
  	rm -f .tmp_System.map
  	rm -f .tmp_kallsyms*
  	rm -f .tmp_version
  	rm -f .tmp_vmlinux*
a5967db9a   Stephen Rothwell   kbuild: allow arc...
188
  	rm -f built-in.o
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
189
190
191
192
  	rm -f System.map
  	rm -f vmlinux
  	rm -f vmlinux.o
  }
ab160dbbc   Sylvain BERTRAND   scripts: fix link...
193
194
195
196
197
198
199
200
201
202
203
204
205
  on_exit()
  {
  	if [ $? -ne 0 ]; then
  		cleanup
  	fi
  }
  trap on_exit EXIT
  
  on_signals()
  {
  	exit 1
  }
  trap on_signals HUP INT QUIT TERM
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  #
  #
  # Use "make V=1" to debug this script
  case "${KBUILD_VERBOSE}" in
  *1*)
  	set -x
  	;;
  esac
  
  if [ "$1" = "clean" ]; then
  	cleanup
  	exit 0
  fi
  
  # We need access to CONFIG_ symbols
423a8155f   Michal Marek   kbuild: Fix readi...
221
222
223
224
225
226
227
228
  case "${KCONFIG_CONFIG}" in
  */*)
  	. "${KCONFIG_CONFIG}"
  	;;
  *)
  	# Force using a file from the current directory
  	. "./${KCONFIG_CONFIG}"
  esac
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
229

1f2bfbd00   Sam Ravnborg   kbuild: link of v...
230
231
232
233
234
235
236
237
238
239
240
  # Update version
  info GEN .version
  if [ ! -r .version ]; then
  	rm -f .version;
  	echo 1 >.version;
  else
  	mv .version .old_version;
  	expr 0$(cat .old_version) + 1 >.version;
  fi;
  
  # final build of init/
6b90bd4ba   Emese Revfy   GCC plugin infras...
241
  ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
242

abac4c897   Nicholas Piggin   kbuild: minor imp...
243
244
245
246
247
248
249
250
  archive_builtin
  
  #link vmlinux.o
  info LD vmlinux.o
  modpost_link vmlinux.o
  
  # modpost vmlinux.o to check for section mismatches
  ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  kallsymso=""
  kallsyms_vmlinux=""
  if [ -n "${CONFIG_KALLSYMS}" ]; then
  
  	# kallsyms support
  	# Generate section listing all symbols and add it into vmlinux
  	# It's a three step process:
  	# 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
  	#     but __kallsyms is empty.
  	#     Running kallsyms on that gives us .tmp_kallsyms1.o with
  	#     the right size
  	# 2)  Link .tmp_vmlinux2 so it now has a __kallsyms section of
  	#     the right size, but due to the added section, some
  	#     addresses have shifted.
  	#     From here, we generate a correct .tmp_kallsyms2.o
7e2b37c97   Nicholas Piggin   kbuild: kallsyms ...
266
267
268
269
270
271
272
273
  	# 3)  That link may have expanded the kernel image enough that
  	#     more linker branch stubs / trampolines had to be added, which
  	#     introduces new names, which further expands kallsyms. Do another
  	#     pass if that is the case. In theory it's possible this results
  	#     in even more stubs, but unlikely.
  	#     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  	#     other bugs.
  	# 4)  The correct ${kallsymso} is linked into the final vmlinux.
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	#
  	# a)  Verify that the System.map from vmlinux matches the map from
  	#     ${kallsymso}.
  
  	kallsymso=.tmp_kallsyms2.o
  	kallsyms_vmlinux=.tmp_vmlinux2
  
  	# step 1
  	vmlinux_link "" .tmp_vmlinux1
  	kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  
  	# step 2
  	vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  	kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
7e2b37c97   Nicholas Piggin   kbuild: kallsyms ...
288
289
290
291
292
  	# step 3
  	size1=$(stat -c "%s" .tmp_kallsyms1.o)
  	size2=$(stat -c "%s" .tmp_kallsyms2.o)
  
  	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
293
294
295
296
297
298
299
300
301
302
303
  		kallsymso=.tmp_kallsyms3.o
  		kallsyms_vmlinux=.tmp_vmlinux3
  
  		vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  
  		kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  	fi
  fi
  
  info LD vmlinux
  vmlinux_link "${kallsymso}" vmlinux
1347a2ceb   Linus Torvalds   Merge branch 'kbu...
304
305
306
307
  if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  	info SORTEX vmlinux
  	sortextable vmlinux
  fi
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
308
309
310
311
312
313
314
315
  info SYSMAP System.map
  mksysmap vmlinux System.map
  
  # step a (see comment above)
  if [ -n "${CONFIG_KALLSYMS}" ]; then
  	mksysmap ${kallsyms_vmlinux} .tmp_System.map
  
  	if ! cmp -s System.map .tmp_System.map; then
5369f5502   Michal Marek   kbuild: Print err...
316
  		echo >&2 Inconsistent kallsyms data
367e43c50   Michal Marek   link-vmlinux.sh: ...
317
  		echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
1f2bfbd00   Sam Ravnborg   kbuild: link of v...
318
319
320
321
322
323
  		exit 1
  	fi
  fi
  
  # We made a new kernel - delete old version file
  rm -f .old_version