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_OBJS) and 7# $(KBUILD_VMLINUX_LIBS). Most are built-in.a files from top-level directories 8# in the kernel tree, others are specified in arch/$(ARCH)/Makefile. 9# $(KBUILD_VMLINUX_LIBS) are archives which are linked conditionally 10# (not within --whole-archive), and do not require symbol indexes added. 11# 12# vmlinux 13# ^ 14# | 15# +--< $(KBUILD_VMLINUX_OBJS) 16# | +--< init/built-in.a drivers/built-in.a mm/built-in.a + more 17# | 18# +--< $(KBUILD_VMLINUX_LIBS) 19# | +--< lib/lib.a + more 20# | 21# +-< ${kallsymso} (see description in KALLSYMS section) 22# 23# vmlinux version (uname -v) cannot be updated during normal 24# descending-into-subdirs phase since we do not yet know if we need to 25# update vmlinux. 26# Therefore this step is delayed until just before final link of vmlinux. 27# 28# System.map is generated to document addresses of all kernel symbols 29 30# Error out on error 31set -e 32 33LD="$1" 34KBUILD_LDFLAGS="$2" 35LDFLAGS_vmlinux="$3" 36 37# Nice output in kbuild format 38# Will be supressed by "make -s" 39info() 40{ 41 if [ "${quiet}" != "silent_" ]; then 42 printf " %-7s %s\n" "${1}" "${2}" 43 fi 44} 45 46# Generate a linker script to ensure correct ordering of initcalls. 47gen_initcalls() 48{ 49 info GEN .tmp_initcalls.lds 50 51 ${PYTHON3} ${srctree}/scripts/jobserver-exec \ 52 ${PERL} ${srctree}/scripts/generate_initcall_order.pl \ 53 ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS} \ 54 > .tmp_initcalls.lds 55} 56 57# If CONFIG_LTO_CLANG is selected, collect generated symbol versions into 58# .tmp_symversions.lds 59gen_symversions() 60{ 61 info GEN .tmp_symversions.lds 62 rm -f .tmp_symversions.lds 63 64 for o in ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}; do 65 if [ -f ${o}.symversions ]; then 66 cat ${o}.symversions >> .tmp_symversions.lds 67 fi 68 done 69} 70 71# Link of vmlinux.o used for section mismatch analysis 72# ${1} output file 73modpost_link() 74{ 75 local objects 76 local lds="" 77 78 objects="--whole-archive \ 79 ${KBUILD_VMLINUX_OBJS} \ 80 --no-whole-archive \ 81 --start-group \ 82 ${KBUILD_VMLINUX_LIBS} \ 83 --end-group" 84 85 if [ -n "${CONFIG_LTO_CLANG}" ]; then 86 gen_initcalls 87 lds="-T .tmp_initcalls.lds" 88 89 if [ -n "${CONFIG_MODVERSIONS}" ]; then 90 gen_symversions 91 lds="${lds} -T .tmp_symversions.lds" 92 fi 93 94 # This might take a while, so indicate that we're doing 95 # an LTO link 96 info LTO ${1} 97 else 98 info LD ${1} 99 fi 100 101 ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${lds} ${objects} 102} 103 104objtool_link() 105{ 106 local objtoolopt; 107 108 if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then 109 objtoolopt="check" 110 if [ -n "${CONFIG_CPU_UNRET_ENTRY}" ]; then 111 objtoolopt="${objtoolopt} --unret" 112 fi 113 if [ -z "${CONFIG_FRAME_POINTER}" ]; then 114 objtoolopt="${objtoolopt} --no-fp" 115 fi 116 if [ -n "${CONFIG_GCOV_KERNEL}" ]; then 117 objtoolopt="${objtoolopt} --no-unreachable" 118 fi 119 if [ -n "${CONFIG_RETPOLINE}" ]; then 120 objtoolopt="${objtoolopt} --retpoline" 121 fi 122 if [ -n "${CONFIG_X86_SMAP}" ]; then 123 objtoolopt="${objtoolopt} --uaccess" 124 fi 125 if [ -n "${CONFIG_SLS}" ]; then 126 objtoolopt="${objtoolopt} --sls" 127 fi 128 info OBJTOOL ${1} 129 tools/objtool/objtool ${objtoolopt} ${1} 130 fi 131} 132 133# Link of vmlinux 134# ${1} - output file 135# ${2}, ${3}, ... - optional extra .o files 136vmlinux_link() 137{ 138 local lds="${objtree}/${KBUILD_LDS}" 139 local output=${1} 140 local objects 141 local strip_debug 142 143 info LD ${output} 144 145 # skip output file argument 146 shift 147 148 # The kallsyms linking does not need debug symbols included. 149 if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then 150 strip_debug=-Wl,--strip-debug 151 fi 152 153 if [ "${SRCARCH}" != "um" ]; then 154 if [ -n "${CONFIG_LTO_CLANG}" ]; then 155 # Use vmlinux.o instead of performing the slow LTO 156 # link again. 157 objects="--whole-archive \ 158 vmlinux.o \ 159 --no-whole-archive \ 160 ${@}" 161 else 162 objects="--whole-archive \ 163 ${KBUILD_VMLINUX_OBJS} \ 164 --no-whole-archive \ 165 --start-group \ 166 ${KBUILD_VMLINUX_LIBS} \ 167 --end-group \ 168 ${@}" 169 fi 170 171 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \ 172 ${strip_debug#-Wl,} \ 173 -o ${output} \ 174 -T ${lds} ${objects} 175 else 176 objects="-Wl,--whole-archive \ 177 ${KBUILD_VMLINUX_OBJS} \ 178 -Wl,--no-whole-archive \ 179 -Wl,--start-group \ 180 ${KBUILD_VMLINUX_LIBS} \ 181 -Wl,--end-group \ 182 ${@}" 183 184 ${CC} ${CFLAGS_vmlinux} \ 185 ${strip_debug} \ 186 -o ${output} \ 187 -Wl,-T,${lds} \ 188 ${objects} \ 189 -lutil -lrt -lpthread 190 rm -f linux 191 fi 192} 193 194# generate .BTF typeinfo from DWARF debuginfo 195# ${1} - vmlinux image 196# ${2} - file to dump raw BTF data into 197gen_btf() 198{ 199 local pahole_ver 200 201 if ! [ -x "$(command -v ${PAHOLE})" ]; then 202 echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available" 203 return 1 204 fi 205 206 pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/') 207 if [ "${pahole_ver}" -lt "116" ]; then 208 echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16" 209 return 1 210 fi 211 212 vmlinux_link ${1} 213 214 info "BTF" ${2} 215 LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1} 216 217 # Create ${2} which contains just .BTF section but no symbols. Add 218 # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all 219 # deletes all symbols including __start_BTF and __stop_BTF, which will 220 # be redefined in the linker script. Add 2>/dev/null to suppress GNU 221 # objcopy warnings: "empty loadable segment detected at ..." 222 ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ 223 --strip-all ${1} ${2} 2>/dev/null 224 # Change e_type to ET_REL so that it can be used to link final vmlinux. 225 # GNU ld 2.35+ and lld do not allow an ET_EXEC input. 226 if [ -n "${CONFIG_CPU_BIG_ENDIAN}" ]; then 227 et_rel='\0\1' 228 else 229 et_rel='\1\0' 230 fi 231 printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none 232} 233 234# Create ${2} .S file with all symbols from the ${1} object file 235kallsyms() 236{ 237 local kallsymopt; 238 239 if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then 240 kallsymopt="${kallsymopt} --all-symbols" 241 fi 242 243 if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then 244 kallsymopt="${kallsymopt} --absolute-percpu" 245 fi 246 247 if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then 248 kallsymopt="${kallsymopt} --base-relative" 249 fi 250 251 info KSYMS ${2} 252 ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2} 253} 254 255# Perform one step in kallsyms generation, including temporary linking of 256# vmlinux. 257kallsyms_step() 258{ 259 kallsymso_prev=${kallsymso} 260 kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1} 261 kallsymso=${kallsyms_vmlinux}.o 262 kallsyms_S=${kallsyms_vmlinux}.S 263 264 vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o} 265 kallsyms ${kallsyms_vmlinux} ${kallsyms_S} 266 267 info AS ${kallsyms_S} 268 ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \ 269 ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ 270 -c -o ${kallsymso} ${kallsyms_S} 271} 272 273# Create map file with all symbols from ${1} 274# See mksymap for additional details 275mksysmap() 276{ 277 ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2} 278} 279 280sorttable() 281{ 282 ${objtree}/scripts/sorttable ${1} 283} 284 285# Delete output files in case of error 286cleanup() 287{ 288 rm -f .btf.* 289 rm -f .tmp_System.map 290 rm -f .tmp_initcalls.lds 291 rm -f .tmp_symversions.lds 292 rm -f .tmp_vmlinux* 293 rm -f System.map 294 rm -f vmlinux 295 rm -f vmlinux.o 296} 297 298on_exit() 299{ 300 if [ $? -ne 0 ]; then 301 cleanup 302 fi 303} 304trap on_exit EXIT 305 306on_signals() 307{ 308 exit 1 309} 310trap on_signals HUP INT QUIT TERM 311 312# Use "make V=1" to debug this script 313case "${KBUILD_VERBOSE}" in 314*1*) 315 set -x 316 ;; 317esac 318 319if [ "$1" = "clean" ]; then 320 cleanup 321 exit 0 322fi 323 324# We need access to CONFIG_ symbols 325. include/config/auto.conf 326 327# Update version 328info GEN .version 329if [ -r .version ]; then 330 VERSION=$(expr 0$(cat .version) + 1) 331 echo $VERSION > .version 332else 333 rm -f .version 334 echo 1 > .version 335fi; 336 337# final build of init/ 338${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1 339 340#link vmlinux.o 341modpost_link vmlinux.o 342objtool_link vmlinux.o 343 344# modpost vmlinux.o to check for section mismatches 345${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1 346 347info MODINFO modules.builtin.modinfo 348${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo 349info GEN modules.builtin 350# The second line aids cases where multiple modules share the same object. 351tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' | 352 tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin 353 354btf_vmlinux_bin_o="" 355if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then 356 btf_vmlinux_bin_o=.btf.vmlinux.bin.o 357 if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then 358 echo >&2 "Failed to generate BTF for vmlinux" 359 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF" 360 exit 1 361 fi 362fi 363 364kallsymso="" 365kallsymso_prev="" 366kallsyms_vmlinux="" 367if [ -n "${CONFIG_KALLSYMS}" ]; then 368 369 # kallsyms support 370 # Generate section listing all symbols and add it into vmlinux 371 # It's a three step process: 372 # 1) Link .tmp_vmlinux1 so it has all symbols and sections, 373 # but __kallsyms is empty. 374 # Running kallsyms on that gives us .tmp_kallsyms1.o with 375 # the right size 376 # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of 377 # the right size, but due to the added section, some 378 # addresses have shifted. 379 # From here, we generate a correct .tmp_kallsyms2.o 380 # 3) That link may have expanded the kernel image enough that 381 # more linker branch stubs / trampolines had to be added, which 382 # introduces new names, which further expands kallsyms. Do another 383 # pass if that is the case. In theory it's possible this results 384 # in even more stubs, but unlikely. 385 # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around 386 # other bugs. 387 # 4) The correct ${kallsymso} is linked into the final vmlinux. 388 # 389 # a) Verify that the System.map from vmlinux matches the map from 390 # ${kallsymso}. 391 392 kallsyms_step 1 393 kallsyms_step 2 394 395 # step 3 396 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev}) 397 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso}) 398 399 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then 400 kallsyms_step 3 401 fi 402fi 403 404vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o} 405 406# fill in BTF IDs 407if [ -n "${CONFIG_DEBUG_INFO_BTF}" -a -n "${CONFIG_BPF}" ]; then 408 info BTFIDS vmlinux 409 ${RESOLVE_BTFIDS} vmlinux 410fi 411 412if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then 413 info SORTTAB vmlinux 414 if ! sorttable vmlinux; then 415 echo >&2 Failed to sort kernel tables 416 exit 1 417 fi 418fi 419 420info SYSMAP System.map 421mksysmap vmlinux System.map 422 423# step a (see comment above) 424if [ -n "${CONFIG_KALLSYMS}" ]; then 425 mksysmap ${kallsyms_vmlinux} .tmp_System.map 426 427 if ! cmp -s System.map .tmp_System.map; then 428 echo >&2 Inconsistent kallsyms data 429 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround 430 exit 1 431 fi 432fi 433