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