libbpf: Support raw BTF placed in the default search path
authorTao Chen <chentao.kernel@linux.alibaba.com>
Mon, 12 Sep 2022 16:43:00 +0000 (00:43 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 22 Sep 2022 00:26:16 +0000 (17:26 -0700)
Currently, the default vmlinux files at '/boot/vmlinux-*',
'/lib/modules/*/vmlinux-*' etc. are parsed with 'btf__parse_elf()' to
extract BTF. It is possible that these files are actually raw BTF files
similar to /sys/kernel/btf/vmlinux. So parse these files with
'btf__parse' which tries both raw format and ELF format.

This might be useful in some scenarios where users put their custom BTF
into known locations and don't want to specify btf_custom_path option.

Signed-off-by: Tao Chen <chentao.kernel@linux.alibaba.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/3f59fb5a345d2e4f10e16fe9e35fbc4c03ecaa3e.1662999860.git.chentao.kernel@linux.alibaba.com
tools/lib/bpf/btf.c

index 3611315..b4d9a96 100644 (file)
@@ -4642,20 +4642,17 @@ static int btf_dedup_remap_types(struct btf_dedup *d)
  */
 struct btf *btf__load_vmlinux_btf(void)
 {
-       struct {
-               const char *path_fmt;
-               bool raw_btf;
-       } locations[] = {
+       const char *locations[] = {
                /* try canonical vmlinux BTF through sysfs first */
-               { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
-               /* fall back to trying to find vmlinux ELF on disk otherwise */
-               { "/boot/vmlinux-%1$s" },
-               { "/lib/modules/%1$s/vmlinux-%1$s" },
-               { "/lib/modules/%1$s/build/vmlinux" },
-               { "/usr/lib/modules/%1$s/kernel/vmlinux" },
-               { "/usr/lib/debug/boot/vmlinux-%1$s" },
-               { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
-               { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
+               "/sys/kernel/btf/vmlinux",
+               /* fall back to trying to find vmlinux on disk otherwise */
+               "/boot/vmlinux-%1$s",
+               "/lib/modules/%1$s/vmlinux-%1$s",
+               "/lib/modules/%1$s/build/vmlinux",
+               "/usr/lib/modules/%1$s/kernel/vmlinux",
+               "/usr/lib/debug/boot/vmlinux-%1$s",
+               "/usr/lib/debug/boot/vmlinux-%1$s.debug",
+               "/usr/lib/debug/lib/modules/%1$s/vmlinux",
        };
        char path[PATH_MAX + 1];
        struct utsname buf;
@@ -4665,15 +4662,12 @@ struct btf *btf__load_vmlinux_btf(void)
        uname(&buf);
 
        for (i = 0; i < ARRAY_SIZE(locations); i++) {
-               snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
+               snprintf(path, PATH_MAX, locations[i], buf.release);
 
                if (access(path, R_OK))
                        continue;
 
-               if (locations[i].raw_btf)
-                       btf = btf__parse_raw(path);
-               else
-                       btf = btf__parse_elf(path, NULL);
+               btf = btf__parse(path, NULL);
                err = libbpf_get_error(btf);
                pr_debug("loading kernel BTF '%s': %d\n", path, err);
                if (err)