[PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF

From: Geyslan Gregório via B4 Relay

Date: Sat Dec 05 2026 - 16:28:24 EST


From: Geyslan Gregório <gg@xxxxxxxxxxx>

An object that declares a typed ksym makes libbpf load vmlinux BTF, and
bpf_object__load() fails when the kernel was built without
CONFIG_DEBUG_INFO_BTF:

libbpf: failed to find valid kernel BTF
libbpf: Error loading vmlinux BTF: -ESRCH

It fails even when every typed ksym is weak and btf_custom_path serves
the CO-RE relocations. Weak ksyms are optional: when one is missing
from kernel BTF, libbpf leaves it unresolved, its address is 0, and a
call to an unresolved kfunc is poisoned and must be dead code. A kernel
without BTF has no kfunc and no typed variable to resolve to, so it is
the same case.

Have obj_needs_vmlinux_btf() tell whether vmlinux BTF is required, or
optional because weak typed ksyms are its only users. When it is
optional and libbpf finds no usable kernel BTF, go on without it.
bpf_object__resolve_externs() then skips the typed ksym lookup, and the
ksyms stay unresolved.

No usable kernel BTF is the -ESRCH of btf__load_vmlinux_btf(): nothing
in /sys/kernel/btf and no vmlinux image with BTF it can parse. Since
commit f2e4040c82d3 ("libbpf: Add some details for BTF parsing
failures") an error reading /sys/kernel/btf/vmlinux is returned as it
is, and it stays fatal here: the kernel has BTF, and its kfuncs must
not be dropped silently.

The same applies where the kernel has BTF and the process cannot see
/sys/kernel/btf, in a container without sysfs for example. Such an
object failed to load there, and now loads with its weak typed ksyms
unresolved. libbpf reports it at the info level.

An object that needs vmlinux BTF for another reason (a strong typed
ksym, CO-RE relocations without btf_custom_path, a BTF-typed program, a
struct_ops map) fails as before, and so do the callers that ask for
vmlinux BTF explicitly. The light skeleton loader is not changed.

Signed-off-by: Geyslan Gregório <gg@xxxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 50 +++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 11 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6fe0143ea959..5e2bb95bda95 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -3736,58 +3736,83 @@ static bool map_needs_vmlinux_btf(struct bpf_map *map)
return bpf_map__is_struct_ops(map);
}

-static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
+enum vmlinux_btf_need {
+ VMLINUX_BTF_NONE,
+ /* Only weak typed ksyms use it. Without kernel BTF they are left
+ * unresolved, as on a kernel with BTF that lacks them.
+ */
+ VMLINUX_BTF_OPTIONAL,
+ VMLINUX_BTF_REQUIRED,
+};
+
+static enum vmlinux_btf_need obj_needs_vmlinux_btf(const struct bpf_object *obj)
{
struct bpf_program *prog;
struct bpf_map *map;
+ bool weak_ksyms = false;
int i;

/* CO-RE relocations need kernel BTF, only when btf_custom_path
* is not specified
*/
if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
- return true;
+ return VMLINUX_BTF_REQUIRED;

- /* Support for typed ksyms needs kernel BTF */
+ /* Support for typed ksyms needs kernel BTF, except for weak ones */
for (i = 0; i < obj->nr_extern; i++) {
const struct extern_desc *ext;

ext = &obj->externs[i];
- if (ext->type == EXT_KSYM && ext->ksym.type_id)
- return true;
+ if (ext->type != EXT_KSYM || !ext->ksym.type_id)
+ continue;
+ if (!ext->is_weak)
+ return VMLINUX_BTF_REQUIRED;
+ weak_ksyms = true;
}

bpf_object__for_each_program(prog, obj) {
if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
continue;
if (prog_needs_vmlinux_btf(prog))
- return true;
+ return VMLINUX_BTF_REQUIRED;
}

bpf_object__for_each_map(map, obj) {
if (map_needs_vmlinux_btf(map))
- return true;
+ return VMLINUX_BTF_REQUIRED;
}

- return false;
+ return weak_ksyms ? VMLINUX_BTF_OPTIONAL : VMLINUX_BTF_NONE;
}

static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
{
+ enum vmlinux_btf_need need = VMLINUX_BTF_REQUIRED;
int err;

/* btf_vmlinux could be loaded earlier */
if (obj->btf_vmlinux || obj->gen_loader)
return 0;

- if (!force && !obj_needs_vmlinux_btf(obj))
+ if (!force)
+ need = obj_needs_vmlinux_btf(obj);
+ if (need == VMLINUX_BTF_NONE)
return 0;

obj->btf_vmlinux = btf__load_vmlinux_btf();
err = libbpf_get_error(obj->btf_vmlinux);
if (err) {
- pr_warn("Error loading vmlinux BTF: %s\n", errstr(err));
obj->btf_vmlinux = NULL;
+ /* -ESRCH means libbpf found no usable kernel BTF, neither in
+ * sysfs nor in a vmlinux image. Any other error comes from
+ * /sys/kernel/btf/vmlinux, which exists but could not be read:
+ * going on would silently drop kfuncs the kernel has.
+ */
+ if (need == VMLINUX_BTF_OPTIONAL && err == -ESRCH) {
+ pr_info("no usable kernel BTF, weak typed ksyms are left unresolved\n");
+ return 0;
+ }
+ pr_warn("Error loading vmlinux BTF: %s\n", errstr(err));
return err;
}
return 0;
@@ -10078,7 +10103,10 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
if (err)
return -EINVAL;
}
- if (need_vmlinux_btf) {
+ /* Without vmlinux BTF every typed ksym is weak, see
+ * obj_needs_vmlinux_btf(), and is left unresolved.
+ */
+ if (need_vmlinux_btf && obj->btf_vmlinux) {
err = bpf_object__resolve_ksyms_btf_id(obj);
if (err)
return -EINVAL;

--
2.56.1