Re: [PATCH bpf-next v3 8/9] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY_BY_NAME ioctl

From: Andrii Nakryiko

Date: Wed Nov 25 2026 - 15:26:27 EST


On Fri, Nov 13, 2026 at 2:14 PM Samuel Wu <wusamuel@xxxxxxxxxx> wrote:
>
> libbpf parses all of /proc/kallsyms to resolve typeless ksyms.
>
> Use KALLSYMS_QUERY_BY_NAME instead: exact names, plus "<name>.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 <wusamuel@xxxxxxxxxx>
> ---
> tools/lib/bpf/libbpf.c | 99 +++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 97 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 44134421bd7d..f3f3475a64d3 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -59,6 +59,34 @@
> #define BPF_FS_MAGIC 0xcafe4a11
> #endif
>
> +#ifndef KALLSYMS_QUERY_BY_NAME
> +/* 'f' is PROCFS_IOCTL_MAGIC */
> +#define KALLSYMS_QUERY_BY_NAME _IOC(_IOC_READ | _IOC_WRITE, 'f', 48, 0)
> +
> +enum kallsyms_query_flags {
> + KALLSYMS_QUERY_NAME_PREFIX = 0x01,
> +};
> +
> +struct kallsyms_query_sym {
> + __u64 sym_addr;
> + __u64 sym_size;
> + __u64 sym_name;
> + __u64 sym_name_len;
> + __u64 sym_module;
> + __u64 sym_module_len;
> +};
> +
> +struct kallsyms_query_by_name {
> + __u64 size;
> + __u64 query_flags;
> + __u64 query_name;
> + __u64 module_filter;
> + __u64 pos;
> + __u64 sym;
> + __u64 sym_struct_size;
> +};
> +#endif
> +

there is no need for this, libbpf in kernel can use kernels' most up
to date UAPI headers directly, and on Github we sync necessary UAPI
headers into include/ directory, so libbpf source code can be built
with latest definitions of kernel UAPI. So you can drop this part

> #define MAX_EVENT_NAME_LEN 64
>
> #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
> @@ -9421,8 +9449,75 @@ 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_struct_size = 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 -EADDRNOTAVAIL;

I think there is ENOTUNIQ which we can utilize here

> + }
> + 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.<hash> prefix */
> + err = query_ksym_by_name(fd, ext, false);
> + if (!err)
> + err = query_ksym_by_name(fd, ext, true);

err = err ?: query_ksym_by_name(fd, ext, true /* lto_prefix */);


> + }
> + close(fd);
> + if (!err || err == -EADDRNOTAVAIL)
> + 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);
> }
>
> @@ -9699,7 +9794,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.0.679.gc527c65306-goog
>