Skip to content

Commit af73d78

Browse files
keesborkmann
authored andcommitted
kbuild: Remove debug info from kallsyms linking
When CONFIG_DEBUG_INFO is enabled, the two kallsyms linking steps spend time collecting and writing the dwarf sections to the temporary output files. kallsyms does not need this information, and leaving it off halves their linking time. This is especially noticeable without CONFIG_DEBUG_INFO_REDUCED. The BTF linking stage, however, does still need those details. Refactor the BTF and kallsyms generation stages slightly for more regularized temporary names. Skip debug during kallsyms links. Additionally move "info BTF" to the correct place since commit 8959e39 ("kbuild: Parameterize kallsyms generation and correct reporting"), which added "info LD ..." to vmlinux_link calls. For a full debug info build with BTF, my link time goes from 1m06s to 0m54s, saving about 12 seconds, or 18%. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/202003031814.4AEA3351@keescook
1 parent 0641cad commit af73d78

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

‎scripts/link-vmlinux.sh‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ vmlinux_link()
6363
local lds="${objtree}/${KBUILD_LDS}"
6464
local output=${1}
6565
local objects
66+
local strip_debug
6667

6768
info LD ${output}
6869

6970
# skip output file argument
7071
shift
7172

73+
# The kallsyms linking does not need debug symbols included.
74+
if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
75+
strip_debug=-Wl,--strip-debug
76+
fi
77+
7278
if [ "${SRCARCH}" != "um" ]; then
7379
objects="--whole-archive \
7480
${KBUILD_VMLINUX_OBJS} \
@@ -79,6 +85,7 @@ vmlinux_link()
7985
${@}"
8086

8187
${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
88+
${strip_debug#-Wl,} \
8289
-o ${output} \
8390
-T ${lds} ${objects}
8491
else
@@ -91,6 +98,7 @@ vmlinux_link()
9198
${@}"
9299

93100
${CC} ${CFLAGS_vmlinux} \
101+
${strip_debug} \
94102
-o ${output} \
95103
-Wl,-T,${lds} \
96104
${objects} \
@@ -106,6 +114,8 @@ gen_btf()
106114
{
107115
local pahole_ver
108116
local bin_arch
117+
local bin_format
118+
local bin_file
109119

110120
if ! [ -x "$(command -v ${PAHOLE})" ]; then
111121
echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@@ -118,20 +128,22 @@ gen_btf()
118128
return 1
119129
fi
120130

121-
info "BTF" ${2}
122131
vmlinux_link ${1}
132+
133+
info "BTF" ${2}
123134
LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
124135

125136
# dump .BTF section into raw binary file to link with final vmlinux
126137
bin_arch=$(LANG=C ${OBJDUMP} -f ${1} | grep architecture | \
127138
cut -d, -f1 | cut -d' ' -f2)
128139
bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \
129140
awk '{print $4}')
141+
bin_file=.btf.vmlinux.bin
130142
${OBJCOPY} --change-section-address .BTF=0 \
131143
--set-section-flags .BTF=alloc -O binary \
132-
--only-section=.BTF ${1} .btf.vmlinux.bin
144+
--only-section=.BTF ${1} $bin_file
133145
${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \
134-
--rename-section .data=.BTF .btf.vmlinux.bin ${2}
146+
--rename-section .data=.BTF $bin_file ${2}
135147
}
136148

137149
# Create ${2} .o file with all symbols from the ${1} object file
@@ -166,8 +178,8 @@ kallsyms()
166178
kallsyms_step()
167179
{
168180
kallsymso_prev=${kallsymso}
169-
kallsymso=.tmp_kallsyms${1}.o
170-
kallsyms_vmlinux=.tmp_vmlinux${1}
181+
kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
182+
kallsymso=${kallsyms_vmlinux}.o
171183

172184
vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
173185
kallsyms ${kallsyms_vmlinux} ${kallsymso}
@@ -190,7 +202,6 @@ cleanup()
190202
{
191203
rm -f .btf.*
192204
rm -f .tmp_System.map
193-
rm -f .tmp_kallsyms*
194205
rm -f .tmp_vmlinux*
195206
rm -f System.map
196207
rm -f vmlinux
@@ -257,9 +268,8 @@ tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' |
257268

258269
btf_vmlinux_bin_o=""
259270
if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
260-
if gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then
261-
btf_vmlinux_bin_o=.btf.vmlinux.bin.o
262-
else
271+
btf_vmlinux_bin_o=.btf.vmlinux.bin.o
272+
if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
263273
echo >&2 "Failed to generate BTF for vmlinux"
264274
echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
265275
exit 1

0 commit comments

Comments
 (0)