Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 1 | #!/bin/sh |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 3 | # |
| 4 | # link vmlinux |
| 5 | # |
| 6 | # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 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. |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 13 | # |
| 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 | # | |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 23 | # +--< $(KBUILD_VMLINUX_LIBS) |
| 24 | # | +--< lib/lib.a + more |
| 25 | # | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 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 | |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 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. |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 51 | # |
| 52 | # Traditional incremental style of link does not require this step |
| 53 | # |
| 54 | # built-in.o output file |
| 55 | # |
| 56 | archive_builtin() |
| 57 | { |
| 58 | if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then |
| 59 | info AR built-in.o |
| 60 | rm -f built-in.o; |
Nicholas Piggin | 9a6cfca | 2017-06-09 15:24:14 +1000 | [diff] [blame] | 61 | ${AR} rcsTP${KBUILD_ARFLAGS} built-in.o \ |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 62 | ${KBUILD_VMLINUX_INIT} \ |
| 63 | ${KBUILD_VMLINUX_MAIN} |
| 64 | fi |
| 65 | } |
| 66 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 67 | # Link of vmlinux.o used for section mismatch analysis |
| 68 | # ${1} output file |
| 69 | modpost_link() |
| 70 | { |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 71 | local objects |
| 72 | |
| 73 | if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 74 | objects="--whole-archive \ |
| 75 | built-in.o \ |
| 76 | --no-whole-archive \ |
| 77 | --start-group \ |
| 78 | ${KBUILD_VMLINUX_LIBS} \ |
| 79 | --end-group" |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 80 | else |
| 81 | objects="${KBUILD_VMLINUX_INIT} \ |
| 82 | --start-group \ |
| 83 | ${KBUILD_VMLINUX_MAIN} \ |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 84 | ${KBUILD_VMLINUX_LIBS} \ |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 85 | --end-group" |
| 86 | fi |
| 87 | ${LD} ${LDFLAGS} -r -o ${1} ${objects} |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | # Link of vmlinux |
| 91 | # ${1} - optional extra .o files |
| 92 | # ${2} - output file |
| 93 | vmlinux_link() |
| 94 | { |
| 95 | local lds="${objtree}/${KBUILD_LDS}" |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 96 | local objects |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 97 | |
| 98 | if [ "${SRCARCH}" != "um" ]; then |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 99 | if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 100 | objects="--whole-archive \ |
| 101 | built-in.o \ |
| 102 | --no-whole-archive \ |
| 103 | --start-group \ |
| 104 | ${KBUILD_VMLINUX_LIBS} \ |
| 105 | --end-group \ |
| 106 | ${1}" |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 107 | else |
| 108 | objects="${KBUILD_VMLINUX_INIT} \ |
| 109 | --start-group \ |
| 110 | ${KBUILD_VMLINUX_MAIN} \ |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 111 | ${KBUILD_VMLINUX_LIBS} \ |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 112 | --end-group \ |
| 113 | ${1}" |
| 114 | fi |
| 115 | |
| 116 | ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \ |
| 117 | -T ${lds} ${objects} |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 118 | else |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 119 | if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 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}" |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 127 | else |
| 128 | objects="${KBUILD_VMLINUX_INIT} \ |
| 129 | -Wl,--start-group \ |
| 130 | ${KBUILD_VMLINUX_MAIN} \ |
Nicholas Piggin | 3a166fc | 2017-06-20 01:52:05 +1000 | [diff] [blame] | 131 | ${KBUILD_VMLINUX_LIBS} \ |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 132 | -Wl,--end-group \ |
| 133 | ${1}" |
| 134 | fi |
| 135 | |
| 136 | ${CC} ${CFLAGS_vmlinux} -o ${2} \ |
| 137 | -Wl,-T,${lds} \ |
| 138 | ${objects} \ |
| 139 | -lutil -lrt -lpthread |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 140 | rm -f linux |
| 141 | fi |
| 142 | } |
| 143 | |
| 144 | |
| 145 | # Create ${2} .o file with all symbols from the ${1} object file |
| 146 | kallsyms() |
| 147 | { |
| 148 | info KSYM ${2} |
| 149 | local kallsymopt; |
| 150 | |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 151 | if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then |
| 152 | kallsymopt="${kallsymopt} --symbol-prefix=_" |
James Hogan | 6895f97 | 2012-09-06 22:11:25 +0100 | [diff] [blame] | 153 | fi |
| 154 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 155 | if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then |
James Hogan | 6895f97 | 2012-09-06 22:11:25 +0100 | [diff] [blame] | 156 | kallsymopt="${kallsymopt} --all-symbols" |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 157 | fi |
| 158 | |
Ard Biesheuvel | 4d5d566 | 2016-03-15 14:58:12 -0700 | [diff] [blame] | 159 | if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then |
Rusty Russell | c6bda7c | 2014-03-17 14:05:46 +1030 | [diff] [blame] | 160 | kallsymopt="${kallsymopt} --absolute-percpu" |
| 161 | fi |
| 162 | |
Ard Biesheuvel | 2213e9a | 2016-03-15 14:58:19 -0700 | [diff] [blame] | 163 | if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then |
| 164 | kallsymopt="${kallsymopt} --base-relative" |
| 165 | fi |
| 166 | |
Sam Ravnborg | 00e6c28c | 2012-05-08 19:53:46 +0200 | [diff] [blame] | 167 | local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ |
| 168 | ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}" |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 169 | |
Ard Biesheuvel | a043934 | 2016-02-05 11:25:05 +0100 | [diff] [blame] | 170 | local afile="`basename ${2} .o`.S" |
| 171 | |
| 172 | ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile} |
| 173 | ${CC} ${aflags} -c -o ${2} ${afile} |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | # Create map file with all symbols from ${1} |
| 177 | # See mksymap for additional details |
| 178 | mksysmap() |
| 179 | { |
| 180 | ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2} |
| 181 | } |
| 182 | |
Linus Torvalds | 1347a2ce | 2012-05-28 10:32:28 -0700 | [diff] [blame] | 183 | sortextable() |
| 184 | { |
| 185 | ${objtree}/scripts/sortextable ${1} |
| 186 | } |
| 187 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 188 | # Delete output files in case of error |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 189 | cleanup() |
| 190 | { |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 191 | rm -f .tmp_System.map |
| 192 | rm -f .tmp_kallsyms* |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 193 | rm -f .tmp_vmlinux* |
Stephen Rothwell | a5967db | 2016-08-24 22:29:19 +1000 | [diff] [blame] | 194 | rm -f built-in.o |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 195 | rm -f System.map |
| 196 | rm -f vmlinux |
| 197 | rm -f vmlinux.o |
| 198 | } |
| 199 | |
Sylvain BERTRAND | ab160db | 2015-05-07 00:36:04 +0000 | [diff] [blame] | 200 | on_exit() |
| 201 | { |
| 202 | if [ $? -ne 0 ]; then |
| 203 | cleanup |
| 204 | fi |
| 205 | } |
| 206 | trap on_exit EXIT |
| 207 | |
| 208 | on_signals() |
| 209 | { |
| 210 | exit 1 |
| 211 | } |
| 212 | trap on_signals HUP INT QUIT TERM |
| 213 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 214 | # |
| 215 | # |
| 216 | # Use "make V=1" to debug this script |
| 217 | case "${KBUILD_VERBOSE}" in |
| 218 | *1*) |
| 219 | set -x |
| 220 | ;; |
| 221 | esac |
| 222 | |
| 223 | if [ "$1" = "clean" ]; then |
| 224 | cleanup |
| 225 | exit 0 |
| 226 | fi |
| 227 | |
| 228 | # We need access to CONFIG_ symbols |
Michal Marek | 423a815 | 2013-02-25 13:47:53 +0100 | [diff] [blame] | 229 | case "${KCONFIG_CONFIG}" in |
| 230 | */*) |
| 231 | . "${KCONFIG_CONFIG}" |
| 232 | ;; |
| 233 | *) |
| 234 | # Force using a file from the current directory |
| 235 | . "./${KCONFIG_CONFIG}" |
| 236 | esac |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 237 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 238 | # Update version |
| 239 | info GEN .version |
Masahiro Yamada | 278ae60 | 2017-09-22 14:31:13 +0900 | [diff] [blame] | 240 | if [ -r .version ]; then |
| 241 | VERSION=$(expr 0$(cat .version) + 1) |
| 242 | echo $VERSION > .version |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 243 | else |
Masahiro Yamada | 278ae60 | 2017-09-22 14:31:13 +0900 | [diff] [blame] | 244 | rm -f .version |
| 245 | echo 1 > .version |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 246 | fi; |
| 247 | |
| 248 | # final build of init/ |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 249 | ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}" |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 250 | |
Nicholas Piggin | abac4c8 | 2016-11-24 03:41:43 +1100 | [diff] [blame] | 251 | archive_builtin |
| 252 | |
| 253 | #link vmlinux.o |
| 254 | info LD vmlinux.o |
| 255 | modpost_link vmlinux.o |
| 256 | |
| 257 | # modpost vmlinux.o to check for section mismatches |
| 258 | ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o |
| 259 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 260 | kallsymso="" |
| 261 | kallsyms_vmlinux="" |
| 262 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 263 | |
| 264 | # kallsyms support |
| 265 | # Generate section listing all symbols and add it into vmlinux |
| 266 | # It's a three step process: |
| 267 | # 1) Link .tmp_vmlinux1 so it has all symbols and sections, |
| 268 | # but __kallsyms is empty. |
| 269 | # Running kallsyms on that gives us .tmp_kallsyms1.o with |
| 270 | # the right size |
| 271 | # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of |
| 272 | # the right size, but due to the added section, some |
| 273 | # addresses have shifted. |
| 274 | # From here, we generate a correct .tmp_kallsyms2.o |
Nicholas Piggin | 7e2b37c | 2016-11-24 03:41:37 +1100 | [diff] [blame] | 275 | # 3) That link may have expanded the kernel image enough that |
| 276 | # more linker branch stubs / trampolines had to be added, which |
| 277 | # introduces new names, which further expands kallsyms. Do another |
| 278 | # pass if that is the case. In theory it's possible this results |
| 279 | # in even more stubs, but unlikely. |
| 280 | # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around |
| 281 | # other bugs. |
| 282 | # 4) The correct ${kallsymso} is linked into the final vmlinux. |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 283 | # |
| 284 | # a) Verify that the System.map from vmlinux matches the map from |
| 285 | # ${kallsymso}. |
| 286 | |
| 287 | kallsymso=.tmp_kallsyms2.o |
| 288 | kallsyms_vmlinux=.tmp_vmlinux2 |
| 289 | |
| 290 | # step 1 |
| 291 | vmlinux_link "" .tmp_vmlinux1 |
| 292 | kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o |
| 293 | |
| 294 | # step 2 |
| 295 | vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2 |
| 296 | kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o |
| 297 | |
Nicholas Piggin | 7e2b37c | 2016-11-24 03:41:37 +1100 | [diff] [blame] | 298 | # step 3 |
| 299 | size1=$(stat -c "%s" .tmp_kallsyms1.o) |
| 300 | size2=$(stat -c "%s" .tmp_kallsyms2.o) |
| 301 | |
| 302 | if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 303 | kallsymso=.tmp_kallsyms3.o |
| 304 | kallsyms_vmlinux=.tmp_vmlinux3 |
| 305 | |
| 306 | vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3 |
| 307 | |
| 308 | kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o |
| 309 | fi |
| 310 | fi |
| 311 | |
| 312 | info LD vmlinux |
| 313 | vmlinux_link "${kallsymso}" vmlinux |
| 314 | |
Linus Torvalds | 1347a2ce | 2012-05-28 10:32:28 -0700 | [diff] [blame] | 315 | if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then |
| 316 | info SORTEX vmlinux |
| 317 | sortextable vmlinux |
| 318 | fi |
| 319 | |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 320 | info SYSMAP System.map |
| 321 | mksysmap vmlinux System.map |
| 322 | |
| 323 | # step a (see comment above) |
| 324 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 325 | mksysmap ${kallsyms_vmlinux} .tmp_System.map |
| 326 | |
| 327 | if ! cmp -s System.map .tmp_System.map; then |
Michal Marek | 5369f55 | 2012-07-07 23:04:40 +0200 | [diff] [blame] | 328 | echo >&2 Inconsistent kallsyms data |
Michal Marek | 367e43c | 2012-08-10 11:55:11 +0200 | [diff] [blame] | 329 | echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround |
Sam Ravnborg | 1f2bfbd | 2012-05-05 10:18:41 +0200 | [diff] [blame] | 330 | exit 1 |
| 331 | fi |
| 332 | fi |