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

From: Samuel Wu

Date: Wed Nov 25 2026 - 19:53:34 EST


On Wed, Nov 25, 2026 at 12:25 PM Andrii Nakryiko
<andrii.nakryiko@xxxxxxxxx> wrote:
>
> 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

Ack, will drop entire #ifndef block
[...]

> > +
> > + 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

Ack
[...]

> > + /* 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 */);

Ack- will update in v4. Thanks for the thoughtful review!