bpf: Support pre-2.25-binutils objcopy for vmlinux BTF
authorStanislav Fomichev <sdf@google.com>
Wed, 27 Nov 2019 16:14:10 +0000 (08:14 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 27 Nov 2019 19:11:42 +0000 (11:11 -0800)
If vmlinux BTF generation fails, but CONFIG_DEBUG_INFO_BTF is set,
.BTF section of vmlinux is empty and kernel will prohibit
BPF loading and return "in-kernel BTF is malformed".

--dump-section argument to binutils' objcopy was added in version 2.25.
When using pre-2.25 binutils, BTF generation silently fails. Convert
to --only-section which is present on pre-2.25 binutils.

Documentation/process/changes.rst states that binutils 2.21+
is supported, not sure those standards apply to BPF subsystem.

v2:
* exit and print an error if gen_btf fails (John Fastabend)

v3:
* resend with Andrii's Acked-by/Tested-by tags

Fixes: 341dfcf8d78ea ("btf: expose BTF info through sysfs")
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20191127161410.57327-1-sdf@google.com
scripts/link-vmlinux.sh

index 0649537..2998ddb 100755 (executable)
@@ -127,7 +127,8 @@ gen_btf()
                cut -d, -f1 | cut -d' ' -f2)
        bin_format=$(LANG=C ${OBJDUMP} -f ${1} | grep 'file format' | \
                awk '{print $4}')
-       ${OBJCOPY} --dump-section .BTF=.btf.vmlinux.bin ${1} 2>/dev/null
+       ${OBJCOPY} --set-section-flags .BTF=alloc -O binary \
+               --only-section=.BTF ${1} .btf.vmlinux.bin 2>/dev/null
        ${OBJCOPY} -I binary -O ${bin_format} -B ${bin_arch} \
                --rename-section .data=.BTF .btf.vmlinux.bin ${2}
 }
@@ -253,6 +254,10 @@ btf_vmlinux_bin_o=""
 if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
        if gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then
                btf_vmlinux_bin_o=.btf.vmlinux.bin.o
+       else
+               echo >&2 "Failed to generate BTF for vmlinux"
+               echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
+               exit 1
        fi
 fi