b.liu | e958203 | 2025-04-17 19:18:16 +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_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 |
| 31 | set -e |
| 32 | |
| 33 | LD="$1" |
| 34 | KBUILD_LDFLAGS="$2" |
| 35 | LDFLAGS_vmlinux="$3" |
| 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 | |
| 46 | # If CONFIG_LTO_CLANG is selected, generate a linker script to ensure correct |
| 47 | # ordering of initcalls, and with CONFIG_MODVERSIONS also enabled, collect the |
| 48 | # previously generated symbol versions into the same script. |
| 49 | lto_lds() |
| 50 | { |
| 51 | if [ -z "${CONFIG_LTO_CLANG}" ]; then |
| 52 | return |
| 53 | fi |
| 54 | |
| 55 | ${srctree}/scripts/generate_initcall_order.pl \ |
| 56 | ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS} \ |
| 57 | > .tmp_lto.lds |
| 58 | |
| 59 | if [ -n "${CONFIG_MODVERSIONS}" ]; then |
| 60 | for a in ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}; do |
| 61 | for o in $(${AR} t $a 2>/dev/null); do |
| 62 | if [ -f ${o}.symversions ]; then |
| 63 | cat ${o}.symversions >> .tmp_lto.lds |
| 64 | fi |
| 65 | done |
| 66 | done |
| 67 | fi |
| 68 | |
| 69 | echo "-T .tmp_lto.lds" |
| 70 | } |
| 71 | |
| 72 | # Link of vmlinux.o used for section mismatch analysis |
| 73 | # ${1} output file |
| 74 | modpost_link() |
| 75 | { |
| 76 | local objects |
| 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 | # This might take a while, so indicate that we're doing |
| 87 | # an LTO link |
| 88 | info LTO ${1} |
| 89 | else |
| 90 | info LD ${1} |
| 91 | fi |
| 92 | |
| 93 | ${LD} ${KBUILD_LDFLAGS} -r -o ${1} $(lto_lds) ${objects} |
| 94 | } |
| 95 | |
| 96 | # If CONFIG_LTO_CLANG is selected, we postpone running recordmcount until |
| 97 | # we have compiled LLVM IR to an object file. |
| 98 | recordmcount() |
| 99 | { |
| 100 | if [ -z "${CONFIG_LTO_CLANG}" ]; then |
| 101 | return |
| 102 | fi |
| 103 | |
| 104 | if [ -n "${CONFIG_FTRACE_MCOUNT_RECORD}" ]; then |
| 105 | scripts/recordmcount ${RECORDMCOUNT_FLAGS} $* |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | # Link of vmlinux |
| 110 | # ${1} - output file |
| 111 | # ${2}, ${3}, ... - optional extra .o files |
| 112 | vmlinux_link() |
| 113 | { |
| 114 | local lds="${objtree}/${KBUILD_LDS}" |
| 115 | local output=${1} |
| 116 | local objects |
| 117 | local strip_debug |
| 118 | |
| 119 | info LD ${output} |
| 120 | |
| 121 | # skip output file argument |
| 122 | shift |
| 123 | |
| 124 | # The kallsyms linking does not need debug symbols included. |
| 125 | if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then |
| 126 | strip_debug=-Wl,--strip-debug |
| 127 | fi |
| 128 | |
| 129 | if [ "${SRCARCH}" != "um" ]; then |
| 130 | if [ -n "${CONFIG_LTO_CLANG}" ]; then |
| 131 | # Use vmlinux.o instead of performing the slow LTO |
| 132 | # link again. |
| 133 | objects="--whole-archive \ |
| 134 | vmlinux.o \ |
| 135 | --no-whole-archive \ |
| 136 | ${@}" |
| 137 | else |
| 138 | objects="--whole-archive \ |
| 139 | ${KBUILD_VMLINUX_OBJS} \ |
| 140 | --no-whole-archive \ |
| 141 | --start-group \ |
| 142 | ${KBUILD_VMLINUX_LIBS} \ |
| 143 | --end-group \ |
| 144 | ${@}" |
| 145 | fi |
| 146 | |
| 147 | ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \ |
| 148 | ${strip_debug#-Wl,} \ |
| 149 | -o ${output} \ |
| 150 | -T ${lds} ${objects} |
| 151 | else |
| 152 | objects="-Wl,--whole-archive \ |
| 153 | ${KBUILD_VMLINUX_OBJS} \ |
| 154 | -Wl,--no-whole-archive \ |
| 155 | -Wl,--start-group \ |
| 156 | ${KBUILD_VMLINUX_LIBS} \ |
| 157 | -Wl,--end-group \ |
| 158 | ${@}" |
| 159 | |
| 160 | ${CC} ${CFLAGS_vmlinux} \ |
| 161 | ${strip_debug} \ |
| 162 | -o ${output} \ |
| 163 | -Wl,-T,${lds} \ |
| 164 | ${objects} \ |
| 165 | -lutil -lrt -lpthread |
| 166 | rm -f linux |
| 167 | fi |
| 168 | } |
| 169 | |
| 170 | # generate .BTF typeinfo from DWARF debuginfo |
| 171 | # ${1} - vmlinux image |
| 172 | # ${2} - file to dump raw BTF data into |
| 173 | gen_btf() |
| 174 | { |
| 175 | local pahole_ver |
| 176 | |
| 177 | if ! [ -x "$(command -v ${PAHOLE})" ]; then |
| 178 | echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available" |
| 179 | return 1 |
| 180 | fi |
| 181 | |
| 182 | pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/') |
| 183 | if [ "${pahole_ver}" -lt "113" ]; then |
| 184 | echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.13" |
| 185 | return 1 |
| 186 | fi |
| 187 | |
| 188 | vmlinux_link ${1} |
| 189 | |
| 190 | info "BTF" ${2} |
| 191 | LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1} |
| 192 | |
| 193 | # Create ${2} which contains just .BTF section but no symbols. Add |
| 194 | # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all |
| 195 | # deletes all symbols including __start_BTF and __stop_BTF, which will |
| 196 | # be redefined in the linker script. Add 2>/dev/null to suppress GNU |
| 197 | # objcopy warnings: "empty loadable segment detected at ..." |
| 198 | ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \ |
| 199 | --strip-all ${1} ${2} 2>/dev/null |
| 200 | # Change e_type to ET_REL so that it can be used to link final vmlinux. |
| 201 | # GNU ld 2.35+ and lld do not allow an ET_EXEC input. |
| 202 | if [ -n "${CONFIG_CPU_BIG_ENDIAN}" ]; then |
| 203 | et_rel='\0\1' |
| 204 | else |
| 205 | et_rel='\1\0' |
| 206 | fi |
| 207 | printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none |
| 208 | } |
| 209 | |
| 210 | # Create ${2} .o file with all symbols from the ${1} object file |
| 211 | kallsyms() |
| 212 | { |
| 213 | info KSYM ${2} |
| 214 | local kallsymopt; |
| 215 | |
| 216 | if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then |
| 217 | kallsymopt="${kallsymopt} --all-symbols" |
| 218 | fi |
| 219 | |
| 220 | if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then |
| 221 | kallsymopt="${kallsymopt} --absolute-percpu" |
| 222 | fi |
| 223 | |
| 224 | if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then |
| 225 | kallsymopt="${kallsymopt} --base-relative" |
| 226 | fi |
| 227 | |
| 228 | if [ -n "${CONFIG_KALLSYMS_UNCOMPRESSED}" ]; then |
| 229 | kallsymopt="${kallsymopt} --uncompressed" |
| 230 | fi |
| 231 | |
| 232 | local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ |
| 233 | ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}" |
| 234 | |
| 235 | local afile="`basename ${2} .o`.S" |
| 236 | |
| 237 | ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile} |
| 238 | ${CC} ${aflags} -c -o ${2} ${afile} |
| 239 | } |
| 240 | |
| 241 | # Perform one step in kallsyms generation, including temporary linking of |
| 242 | # vmlinux. |
| 243 | kallsyms_step() |
| 244 | { |
| 245 | kallsymso_prev=${kallsymso} |
| 246 | kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1} |
| 247 | kallsymso=${kallsyms_vmlinux}.o |
| 248 | |
| 249 | vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o} |
| 250 | kallsyms ${kallsyms_vmlinux} ${kallsymso} |
| 251 | } |
| 252 | |
| 253 | # Create map file with all symbols from ${1} |
| 254 | # See mksymap for additional details |
| 255 | mksysmap() |
| 256 | { |
| 257 | ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2} |
| 258 | } |
| 259 | |
| 260 | sortextable() |
| 261 | { |
| 262 | ${objtree}/scripts/sortextable ${1} |
| 263 | } |
| 264 | |
| 265 | # Delete output files in case of error |
| 266 | cleanup() |
| 267 | { |
| 268 | rm -f .btf.* |
| 269 | rm -f .tmp_System.map |
| 270 | rm -f .tmp_lto.lds |
| 271 | rm -f .tmp_vmlinux* |
| 272 | rm -f System.map |
| 273 | rm -f vmlinux |
| 274 | rm -f vmlinux.o |
| 275 | } |
| 276 | |
| 277 | on_exit() |
| 278 | { |
| 279 | if [ $? -ne 0 ]; then |
| 280 | cleanup |
| 281 | fi |
| 282 | } |
| 283 | trap on_exit EXIT |
| 284 | |
| 285 | on_signals() |
| 286 | { |
| 287 | exit 1 |
| 288 | } |
| 289 | trap on_signals HUP INT QUIT TERM |
| 290 | |
| 291 | # |
| 292 | # |
| 293 | # Use "make V=1" to debug this script |
| 294 | case "${KBUILD_VERBOSE}" in |
| 295 | *1*) |
| 296 | set -x |
| 297 | ;; |
| 298 | esac |
| 299 | |
| 300 | if [ "$1" = "clean" ]; then |
| 301 | cleanup |
| 302 | exit 0 |
| 303 | fi |
| 304 | |
| 305 | # We need access to CONFIG_ symbols |
| 306 | . include/config/auto.conf |
| 307 | |
| 308 | # Update version |
| 309 | info GEN .version |
| 310 | if [ -r .version ]; then |
| 311 | VERSION=$(expr 0$(cat .version) + 1) |
| 312 | echo $VERSION > .version |
| 313 | else |
| 314 | rm -f .version |
| 315 | echo 1 > .version |
| 316 | fi; |
| 317 | |
| 318 | # final build of init/ |
| 319 | ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init |
| 320 | |
| 321 | #link vmlinux.o |
| 322 | modpost_link vmlinux.o |
| 323 | |
| 324 | # modpost vmlinux.o to check for section mismatches |
| 325 | ${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1 |
| 326 | |
| 327 | if [ -n "${CONFIG_LTO_CLANG}" ]; then |
| 328 | # Call recordmcount if needed |
| 329 | recordmcount vmlinux.o |
| 330 | fi |
| 331 | |
| 332 | info MODINFO modules.builtin.modinfo |
| 333 | ${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo |
| 334 | |
| 335 | btf_vmlinux_bin_o="" |
| 336 | if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then |
| 337 | btf_vmlinux_bin_o=.btf.vmlinux.bin.o |
| 338 | if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then |
| 339 | echo >&2 "Failed to generate BTF for vmlinux" |
| 340 | echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF" |
| 341 | exit 1 |
| 342 | fi |
| 343 | fi |
| 344 | |
| 345 | kallsymso="" |
| 346 | kallsymso_prev="" |
| 347 | kallsyms_vmlinux="" |
| 348 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 349 | |
| 350 | # kallsyms support |
| 351 | # Generate section listing all symbols and add it into vmlinux |
| 352 | # It's a three step process: |
| 353 | # 1) Link .tmp_vmlinux1 so it has all symbols and sections, |
| 354 | # but __kallsyms is empty. |
| 355 | # Running kallsyms on that gives us .tmp_kallsyms1.o with |
| 356 | # the right size |
| 357 | # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of |
| 358 | # the right size, but due to the added section, some |
| 359 | # addresses have shifted. |
| 360 | # From here, we generate a correct .tmp_kallsyms2.o |
| 361 | # 3) That link may have expanded the kernel image enough that |
| 362 | # more linker branch stubs / trampolines had to be added, which |
| 363 | # introduces new names, which further expands kallsyms. Do another |
| 364 | # pass if that is the case. In theory it's possible this results |
| 365 | # in even more stubs, but unlikely. |
| 366 | # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around |
| 367 | # other bugs. |
| 368 | # 4) The correct ${kallsymso} is linked into the final vmlinux. |
| 369 | # |
| 370 | # a) Verify that the System.map from vmlinux matches the map from |
| 371 | # ${kallsymso}. |
| 372 | |
| 373 | kallsyms_step 1 |
| 374 | kallsyms_step 2 |
| 375 | |
| 376 | # step 3 |
| 377 | size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev}) |
| 378 | size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso}) |
| 379 | |
| 380 | if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then |
| 381 | kallsyms_step 3 |
| 382 | fi |
| 383 | fi |
| 384 | |
| 385 | vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o} |
| 386 | |
| 387 | if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then |
| 388 | info SORTEX vmlinux |
| 389 | sortextable vmlinux |
| 390 | fi |
| 391 | |
| 392 | info SYSMAP System.map |
| 393 | mksysmap vmlinux System.map |
| 394 | |
| 395 | # step a (see comment above) |
| 396 | if [ -n "${CONFIG_KALLSYMS}" ]; then |
| 397 | mksysmap ${kallsyms_vmlinux} .tmp_System.map |
| 398 | |
| 399 | if ! cmp -s System.map .tmp_System.map; then |
| 400 | echo >&2 Inconsistent kallsyms data |
| 401 | echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround |
| 402 | exit 1 |
| 403 | fi |
| 404 | fi |