kbuild: merge temporary vmlinux for BTF and kallsyms

CONFIG_DEBUG_INFO_BTF=y requires one additional link step.
(.tmp_vmlinux.btf)

CONFIG_KALLSYMS=y requires two additional link steps.
(.tmp_vmlinux.kallsyms1 and .tmp_vmlinux.kallsyms2)

Enabling both requires three additional link steps.

When CONFIG_DEBUG_INFO_BTF=y and CONFIG_KALLSYMS=y, the current build
process is as follows:

    KSYMS   .tmp_vmlinux.kallsyms0.S
    AS      .tmp_vmlinux.kallsyms0.o
    LD      .tmp_vmlinux.btf             # temporary vmlinux for BTF
    BTF     .btf.vmlinux.bin.o
    LD      .tmp_vmlinux.kallsyms1       # temporary vmlinux for kallsyms step 1
    NM      .tmp_vmlinux.kallsyms1.syms
    KSYMS   .tmp_vmlinux.kallsyms1.S
    AS      .tmp_vmlinux.kallsyms1.o
    LD      .tmp_vmlinux.kallsyms2       # temporary vmlinux for kallsyms step 2
    NM      .tmp_vmlinux.kallsyms2.syms
    KSYMS   .tmp_vmlinux.kallsyms2.S
    AS      .tmp_vmlinux.kallsyms2.o
    LD      vmlinux                      # final vmlinux

This is redundant because the BTF generation and the kallsyms step 1 can
be performed against the same temporary vmlinux.

When both CONFIG_DEBUG_INFO_BTF and CONFIG_KALLSYMS are enabled, we can
reduce the number of link steps by one.

This commit changes the build process as follows:

    KSYMS   .tmp_vmlinux0.kallsyms.S
    AS      .tmp_vmlinux0.kallsyms.o
    LD      .tmp_vmlinux1                # temporary vmlinux for BTF and kallsyms step 1
    BTF     .tmp_vmlinux1.btf.o
    NM      .tmp_vmlinux1.syms
    KSYMS   .tmp_vmlinux1.kallsyms.S
    AS      .tmp_vmlinux1.kallsyms.o
    LD      .tmp_vmlinux2                # temporary vmlinux for kallsyms step 2
    NM      .tmp_vmlinux2.syms
    KSYMS   .tmp_vmlinux2.kallsyms.S
    AS      .tmp_vmlinux2.kallsyms.o
    LD      vmlinux                      # final vmlinux

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
Masahiro Yamada 2024-06-10 20:25:18 +09:00
parent c442db3f49
commit b1a9a5e047

View File

@ -105,11 +105,10 @@ vmlinux_link()
# generate .BTF typeinfo from DWARF debuginfo # generate .BTF typeinfo from DWARF debuginfo
# ${1} - vmlinux image # ${1} - vmlinux image
# ${2} - file to dump raw BTF data into
gen_btf() gen_btf()
{ {
local pahole_ver local pahole_ver
local btf_data=${2} local btf_data=${1}.btf.o
if ! [ -x "$(command -v ${PAHOLE})" ]; then if ! [ -x "$(command -v ${PAHOLE})" ]; then
echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available" echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@ -122,8 +121,6 @@ gen_btf()
return 1 return 1
fi fi
vmlinux_link ${1}
info BTF "${btf_data}" info BTF "${btf_data}"
LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1} LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
@ -177,15 +174,13 @@ kallsyms()
kallsymso=${2}.o kallsymso=${2}.o
} }
# Perform one step in kallsyms generation, including temporary linking of # Perform kallsyms for the given temporary vmlinux.
# vmlinux. sysmap_and_kallsyms()
kallsyms_step()
{ {
kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1} mksysmap "${1}" "${1}.syms"
kallsyms "${1}.syms" "${1}.kallsyms"
vmlinux_link "${kallsyms_vmlinux}" kallsyms_sysmap=${1}.syms
mksysmap "${kallsyms_vmlinux}" "${kallsyms_vmlinux}.syms"
kallsyms "${kallsyms_vmlinux}.syms" "${kallsyms_vmlinux}"
} }
# Create map file with all symbols from ${1} # Create map file with all symbols from ${1}
@ -228,11 +223,21 @@ kallsymso=
strip_debug= strip_debug=
if is_enabled CONFIG_KALLSYMS; then if is_enabled CONFIG_KALLSYMS; then
kallsyms /dev/null .tmp_vmlinux.kallsyms0 kallsyms /dev/null .tmp_vmlinux0.kallsyms
fi
if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then
# The kallsyms linking does not need debug symbols, but the BTF does.
if ! is_enabled CONFIG_DEBUG_INFO_BTF; then
strip_debug=1
fi
vmlinux_link .tmp_vmlinux1
fi fi
if is_enabled CONFIG_DEBUG_INFO_BTF; then if is_enabled CONFIG_DEBUG_INFO_BTF; then
if ! gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then if ! gen_btf .tmp_vmlinux1; then
echo >&2 "Failed to generate BTF for vmlinux" echo >&2 "Failed to generate BTF for vmlinux"
echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF" echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
exit 1 exit 1
@ -268,14 +273,16 @@ if is_enabled CONFIG_KALLSYMS; then
# The kallsyms linking does not need debug symbols included. # The kallsyms linking does not need debug symbols included.
strip_debug=1 strip_debug=1
kallsyms_step 1 sysmap_and_kallsyms .tmp_vmlinux1
size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso}) size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
kallsyms_step 2 vmlinux_link .tmp_vmlinux2
sysmap_and_kallsyms .tmp_vmlinux2
size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso}) size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
kallsyms_step 3 vmlinux_link .tmp_vmlinux3
sysmap_and_kallsyms .tmp_vmlinux3
fi fi
fi fi
@ -301,7 +308,7 @@ fi
# step a (see comment above) # step a (see comment above)
if is_enabled CONFIG_KALLSYMS; then if is_enabled CONFIG_KALLSYMS; then
if ! cmp -s System.map ${kallsyms_vmlinux}.syms; then if ! cmp -s System.map "${kallsyms_sysmap}"; then
echo >&2 Inconsistent kallsyms data echo >&2 Inconsistent kallsyms data
echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround' echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround'
exit 1 exit 1