xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 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 | # 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. |
| 51 | # |
| 52 | # Traditional incremental style of link does not require this step |
| 53 | # |
| 54 | # built-in.a output file |
| 55 | # |
| 56 | archive_builtin() |
| 57 | { |
| 58 | info AR built-in.a |
| 59 | rm -f built-in.a; |
| 60 | ${AR} rcsTP${KBUILD_ARFLAGS} built-in.a \ |
| 61 | ${KBUILD_VMLINUX_INIT} \ |
| 62 | ${KBUILD_VMLINUX_MAIN} |
| 63 | |
| 64 | # rebuild with llvm-ar to update the symbol table |
| 65 | if [ -n "${CONFIG_LTO_CLANG}" ]; then |
| 66 | mv -f built-in.a built-in.a.tmp |
| 67 | ${LLVM_AR} rcsT${KBUILD_ARFLAGS} built-in.a $(${AR} t built-in.a.tmp) |
| 68 | rm -f built-in.a.tmp |
| 69 | fi |
| 70 | } |
| 71 | |
| 72 | # If CONFIG_LTO_CLANG is selected, generate a linker script to ensure correct |
| 73 | # ordering of initcalls, and with CONFIG_MODVERSIONS also enabled, collect the |
| 74 | # previously generated symbol versions into the same script. |
| 75 | lto_lds() |
| 76 | { |
| 77 | if [ -z "${CONFIG_LTO_CLANG}" ]; then |
| 78 | return |
| 79 | fi |
| 80 | |
| 81 | ${srctree}/scripts/generate_initcall_order.pl \ |
| 82 | built-in.a ${KBUILD_VMLINUX_LIBS} \ |
| 83 | > .tmp_lto.lds |
| 84 | |
| 85 | if [ -n "${CONFIG_MODVERSIONS}" ]; then |
| 86 | for a in built-in.a ${KBUILD_VMLINUX_LIBS}; do |
| 87 | for o in $(${AR} t $a); do |
| 88 | if [ -f ${o}.symversions ]; then |
| 89 | cat ${o}.symversions >> .tmp_lto.lds |
| 90 | fi |
| 91 | done |
| 92 | done |
| 93 | fi |
| 94 | |
| 95 | echo "-T .tmp_lto.lds" |
| 96 | } |
| 97 | |
| 98 | # Link of vmlinux.o used for section mismatch analysis |
| 99 | # ${1} output file |
| 100 | modpost_link() |
| 101 | { |
| 102 | local objects |
| 103 | |
| 104 | objects="--whole-archive \ |
| 105 | built-in.a \ |
| 106 | --no-whole-archive \ |
| 107 | --start-group \ |
| 108 | ${KBUILD_VMLINUX_LIBS} \ |
| 109 | --end-group" |
| 110 | |
| 111 | if [ -n "${CONFIG_LTO_CLANG}" ]; then |
| 112 | # This might take a while, so indicate that we're doing |
| 113 | # an LTO link |
| 114 | info LTO vmlinux.o |
| 115 | fi |
| 116 | |
| 117 | ${LD} ${KBUILD_LDFLAGS} -r -o ${1} $(lto_lds) ${objects} |
| 118 | } |
| 119 | |
| 120 | # If CONFIG_LTO_CLANG is selected, we postpone running recordmcount until |
| 121 | # we have compiled LLVM IR to an object file. |
| 122 | recordmcount() |
| 123 | { |
| 124 | if [ -z "${CONFIG_LTO_CLANG}" ]; then |
| 125 | return |
| 126 | fi |
| 127 | |
| 128 | if [ -n "${CONFIG_FTRACE_MCOUNT_RECORD}" ]; then |
| 129 | scripts/recordmcount ${RECORDMCOUNT_FLAGS} $* |
| 130 | fi |
| 131 | } |
| 132 | |
| 133 | # Link of vmlinux |
| 134 | # ${1} - optional extra .o files |
| 135 | # ${2} - output file |
| 136 | vmlinux_link() |
| 137 | { |
| 138 | local lds="${objtree}/${KBUILD_LDS}" |
| 139 | local objects |
| 140 | |
| 141 | if [ "${SRCARCH}" != "um" ]; then |
| 142 | if [ -z "${CONFIG_LTO_CLANG}" ]; then |
| 143 | objects="--whole-archive \ |
| 144 | built-in.a \ |
| 145 | --no-whole-archive \ |
| 146 | --start-group \ |
| 147 | ${KBUILD_VMLINUX_LIBS} \ |
| 148 | --end-group \ |
| 149 | ${1}" |
| 150 | else |
| 151 | objects="--start-group \ |
| 152 | vmlinux.o \ |
| 153 | --end-group \ |
| 154 | ${1}" |
| 155 | fi |
| 156 | |
| 157 | ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \ |
| 158 | -T ${lds} ${objects} |
| 159 | else |
| 160 | objects="-Wl,--whole-archive \ |
| 161 | built-in.a \ |
| 162 | -Wl,--no-whole-archive \ |
| 163 | -Wl,--start-group \ |
| 164 | ${KBUILD_VMLINUX_LIBS} \ |
| 165 | -Wl,--end-group \ |
| 166 | ${1}" |
| 167 | |
| 168 | ${CC} ${CFLAGS_vmlinux} -o ${2} \ |
| 169 | -Wl,-T,${lds} \ |
| 170 | ${objects} \ |
| 171 | -lutil -lrt -lpthread |
| 172 | rm -f linux |
| 173 | fi |
| 174 | } |
| 175 | |
| 176 | # Create ${2} .o file with all symbols from the ${1} object file |
| 177 | kallsyms() |
| 178 | { |
| 179 | info KSYM ${2} |
| 180 | local kallsymopt; |
| 181 | |
| 182 | if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then |
| 183 | kallsymopt="${kallsymopt} --all-symbols" |
| 184 | fi |
| 185 | |
| 186 | if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then |
| 187 | kallsymopt="${kallsymopt} --absolute-percpu" |
| 188 | fi |
| 189 | |
| 190 | if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then |
| 191 | kallsymopt="${kallsymopt} --base-relative" |
| 192 | fi |
| 193 | |
| 194 | if [ -n "${CONFIG_KALLSYMS_UNCOMPRESSED}" ]; then |
| 195 | kallsymopt="${kallsymopt} --uncompressed" |
| 196 | fi |
| 197 | |
| 198 | local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ |
| 199 | ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}" |
| 200 | |
| 201 | local afile="`basename ${2} .o`.S" |
| 202 | |
| 203 | ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile} |
| 204 | ${CC} ${aflags} -c -o ${2} ${afile} |
| 205 | } |
| 206 | |
| 207 | # Create map file with all symbols from ${1} |
| 208 | # See mksymap for additional details |
| 209 | mksysmap() |
| 210 | { |
| 211 | ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2} |
| 212 | } |
| 213 | |
| 214 | sortextable() |
| 215 | { |
| 216 | ${objtree}/scripts/sortextable ${1} |
| 217 | } |
| 218 | |
| 219 | # Delete output files in case of error |
| 220 | cleanup() |
| 221 | { |
| 222 | rm -f .tmp_System.map |
| 223 | rm -f .tmp_kallsyms* |
| 224 | rm -f .tmp_lto.lds |
| 225 | rm -f .tmp_vmlinux* |
| 226 | rm -f built-in.a |
| 227 | rm -f System.map |
| 228 | rm -f vmlinux |
| 229 | rm -f vmlinux.o |
| 230 | } |
| 231 | |
| 232 | on_exit() |
| 233 | { |
| 234 | if [ $? -ne 0 ]; then |
| 235 | cleanup |
| 236 | fi |
| 237 | } |
| 238 | trap on_exit EXIT |
| 239 | |
| 240 | on_signals() |
| 241 | { |
| 242 | exit 1 |
| 243 | } |
| 244 | trap on_signals HUP INT QUIT TERM |
| 245 | |
| 246 | # |
| 247 | # |
| 248 | # Use "make V=1" to debug this script |
| 249 | case "${KBUILD_VERBOSE}" in |
| 250 | *1*) |
| 251 | set -x |
| 252 | ;; |
| 253 | esac |
| 254 | |
| 255 | if [ "$1" = "clean" ]; then |
| 256 | cleanup |
| 257 | exit 0 |
| 258 | fi |
| 259 | |
| 260 | # We need access to CONFIG_ symbols |
| 261 | case "${KCONFIG_CONFIG}" in |
| 262 | */*) |
| 263 | . "${KCONFIG_CONFIG}" |
| 264 | ;; |
| 265 | *) |
| 266 | # Force using a file from the current directory |
| 267 | . "./${KCONFIG_CONFIG}" |
| 268 | esac |
| 269 | |
| 270 | # Update version |
| 271 | info GEN .version |
| 272 | if [ -r .version ]; then |
| 273 | VERSION=$(expr 0$(cat .version) + 1) |
| 274 | echo $VERSION > .version |
| 275 | else |
| 276 | rm -f .version |
| 277 | echo 1 > .version |
| 278 | fi; |
| 279 | |
| 280 | # final build of init/ |
| 281 | ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init |
| 282 | |
| 283 | archive_builtin |
| 284 | |
| 285 | #link vmlinux.o |
| 286 | modpost_link vmlinux.o |
| 287 | |
| 288 | # modpost vmlinux.o to check for section mismatches |
| 289 | ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o |
| 290 | |
| 291 | if [ -n "${CONFIG_LTO_CLANG}" ]; then |
| 292 | # Call recordmcount if needed |
| 293 | recordmcount vmlinux.o |
| 294 | fi |
| 295 | |
| 296 | kallsymso="" |
| 297 | kallsyms_vmlinux="" |
| 298 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 299 | |
| 300 | # kallsyms support |
| 301 | # Generate section listing all symbols and add it into vmlinux |
| 302 | # It's a three step process: |
| 303 | # 1) Link .tmp_vmlinux1 so it has all symbols and sections, |
| 304 | # but __kallsyms is empty. |
| 305 | # Running kallsyms on that gives us .tmp_kallsyms1.o with |
| 306 | # the right size |
| 307 | # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of |
| 308 | # the right size, but due to the added section, some |
| 309 | # addresses have shifted. |
| 310 | # From here, we generate a correct .tmp_kallsyms2.o |
| 311 | # 3) That link may have expanded the kernel image enough that |
| 312 | # more linker branch stubs / trampolines had to be added, which |
| 313 | # introduces new names, which further expands kallsyms. Do another |
| 314 | # pass if that is the case. In theory it's possible this results |
| 315 | # in even more stubs, but unlikely. |
| 316 | # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around |
| 317 | # other bugs. |
| 318 | # 4) The correct ${kallsymso} is linked into the final vmlinux. |
| 319 | # |
| 320 | # a) Verify that the System.map from vmlinux matches the map from |
| 321 | # ${kallsymso}. |
| 322 | |
| 323 | kallsymso=.tmp_kallsyms2.o |
| 324 | kallsyms_vmlinux=.tmp_vmlinux2 |
| 325 | |
| 326 | # step 1 |
| 327 | vmlinux_link "" .tmp_vmlinux1 |
| 328 | kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o |
| 329 | |
| 330 | # step 2 |
| 331 | vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2 |
| 332 | kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o |
| 333 | |
| 334 | # step 3 |
| 335 | size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o) |
| 336 | size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o) |
| 337 | |
| 338 | if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then |
| 339 | kallsymso=.tmp_kallsyms3.o |
| 340 | kallsyms_vmlinux=.tmp_vmlinux3 |
| 341 | |
| 342 | vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3 |
| 343 | |
| 344 | kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o |
| 345 | fi |
| 346 | fi |
| 347 | |
| 348 | info LD vmlinux |
| 349 | vmlinux_link "${kallsymso}" vmlinux |
| 350 | |
| 351 | if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then |
| 352 | info SORTEX vmlinux |
| 353 | sortextable vmlinux |
| 354 | fi |
| 355 | |
| 356 | info SYSMAP System.map |
| 357 | mksysmap vmlinux System.map |
| 358 | |
| 359 | # step a (see comment above) |
| 360 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 361 | mksysmap ${kallsyms_vmlinux} .tmp_System.map |
| 362 | |
| 363 | if ! cmp -s System.map .tmp_System.map; then |
| 364 | echo >&2 Inconsistent kallsyms data |
| 365 | echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround |
| 366 | exit 1 |
| 367 | fi |
| 368 | fi |