libbpf parses all of /proc/kallsyms to resolve typeless ksyms. Use KALLSYMS_QUERY_BY_NAME instead: exact names, plus ".llvm." prefix matches. Ambiguous matches at different addresses fail the load. A missing strong extern still fails and a missing weak extern is 0, without a text scan. On any other error (e.g. -ENOTTY on older kernels, or -EPERM), fall back to the text parser. Signed-off-by: Samuel Wu --- tools/lib/bpf/libbpf.c | 69 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 61a388868373..ea131f47b625 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -9427,8 +9428,72 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type, return 0; } -static int bpf_object__read_kallsyms_file(struct bpf_object *obj) +static int query_ksym_by_name(int fd, struct extern_desc *ext, bool lto_prefix) { + struct kallsyms_query_sym sym = {}; + struct kallsyms_query_by_name q = { + .size = sizeof(q), + .query_flags = lto_prefix ? KALLSYMS_QUERY_NAME_PREFIX : 0, + .query_name = ptr_to_u64(ext->name), + .sym = ptr_to_u64(&sym), + .sym_len = sizeof(sym), + }; + char prefix_buf[512]; + int n; + + if (lto_prefix) { + n = snprintf(prefix_buf, sizeof(prefix_buf), "%s.llvm.", ext->name); + if (n < 0 || n >= sizeof(prefix_buf)) + return 0; + q.query_name = ptr_to_u64(prefix_buf); + } + + while (!ioctl(fd, KALLSYMS_QUERY_BY_NAME, &q)) { + if (!ext->is_set) { + ext->is_set = true; + ext->ksym.addr = sym.sym_addr; + pr_debug("extern (ksym) '%s': set to 0x%llx\n", ext->name, ext->ksym.addr); + } else if (sym.sym_addr != ext->ksym.addr) { + pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n", + ext->name, ext->ksym.addr, (unsigned long long)sym.sym_addr); + return -ENOTUNIQ; + } + q.pos++; + } + + return errno == ENOENT ? 0 : -errno; +} + +static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj) +{ + struct extern_desc *ext; + int err = 0; + int fd; + int i; + + fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC); + if (fd < 0) { + err = -errno; + pr_warn("failed to open /proc/kallsyms: %s\n", errstr(err)); + return err; + } + + for (i = 0; i < obj->nr_extern && !err; i++) { + ext = &obj->externs[i]; + if (ext->type != EXT_KSYM || ext->ksym.type_id) + continue; + + /* Check exact name and then LTO .llvm. prefix */ + err = query_ksym_by_name(fd, ext, false); + err = err ?: query_ksym_by_name(fd, ext, true /* lto_prefix */); + } + close(fd); + if (!err || err == -ENOTUNIQ) + return err; + + /* Other errors (e.g. -ENOTTY, -EPERM): parse the text file */ + pr_debug("KALLSYMS_QUERY_BY_NAME failed (%s), falling back to /proc/kallsyms\n", + errstr(err)); return libbpf_kallsyms_parse(kallsyms_cb, obj); } @@ -9705,7 +9770,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj, return -EINVAL; } if (need_kallsyms) { - err = bpf_object__read_kallsyms_file(obj); + err = bpf_object__resolve_typeless_ksyms(obj); if (err) return -EINVAL; } -- 2.56.1.911.g883d8bcb20-goog