When obj->gen_loader is set, gen_loader resolves typeless ksyms on the target kernel at load time via bpf_kallsyms_lookup_name() rather than using addresses resolved on the build host. Typed ksyms and kfuncs already short-circuit on obj->gen_loader in bpf_object__resolve_ksyms_btf_id(), avoiding host vmlinux BTF lookups, but bpf_object__resolve_externs() still sets need_kallsyms = true for typeless ksyms and calls bpf_object__read_kallsyms_file(). As a result, offline loader generation (e.g. bpftool gen skeleton -L) opens the build host's /proc/kallsyms, which: - fails in sandboxed or cross-compilation environments without /proc/kallsyms access, - fails with -ESRCH for strong typeless ksyms that exist on the target kernel but not on the build host, and - leaks the build host's symbol addresses into the initial ld_imm64 immediates embedded in the generated loader data blob (breaking build reproducibility), even though emit_relo_ksym_typeless() overwrites them at load time. Short-circuit all EXT_KSYM entries in bpf_object__resolve_externs() when obj->gen_loader is set by setting ext->is_set = true (ext is already zeroed by bpf_object__collect_externs()), and drop the now redundant obj->gen_loader check from bpf_object__resolve_ksyms_btf_id(). This skips both vmlinux BTF and /proc/kallsyms lookups on the build host. Also fix emit_relo_ksym_typeless() to only ignore -ENOENT from bpf_kallsyms_lookup_name() when relo->is_weak is set. Previously, -ENOENT was ignored unconditionally, which silently resolved missing strong typeless ksyms to 0 on the target kernel (previously masked on single-host builds by the host /proc/kallsyms check). Enforcing -ENOENT for non-weak symbols ensures missing strong typeless ksyms fail at load time on the target kernel now that host /proc/kallsyms is no longer consulted. Suggested-by: Alexei Starovoitov Signed-off-by: ThiƩbaud Weksteen --- tools/lib/bpf/gen_loader.c | 6 ++++-- tools/lib/bpf/libbpf.c | 11 ++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c index 749a86c43e7f..3376770e00f4 100644 --- a/tools/lib/bpf/gen_loader.c +++ b/tools/lib/bpf/gen_loader.c @@ -817,8 +817,10 @@ static void emit_relo_ksym_typeless(struct bpf_gen *gen, /* skip typeless ksym_desc in fd closing loop in cleanup_relos */ kdesc->typeless = true; emit_bpf_kallsyms_lookup_name(gen, relo); - emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, -ENOENT, 1)); - emit(gen, BPF_MOV64_IMM(BPF_REG_7, 0)); + if (relo->is_weak) { + emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_7, -ENOENT, 1)); + emit(gen, BPF_MOV64_IMM(BPF_REG_7, 0)); + } emit_check_err(gen); /* store lower half of addr into insn[insn_idx].imm */ emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_8, BPF_REG_9, offsetof(struct bpf_insn, imm))); diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e66e848eb463..69f11d7d9a95 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9663,12 +9663,6 @@ static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) if (ext->type != EXT_KSYM || !ext->ksym.type_id) continue; - if (obj->gen_loader) { - ext->is_set = true; - ext->ksym.kernel_btf_obj_fd = 0; - ext->ksym.kernel_btf_id = 0; - continue; - } t = btf__type_by_id(obj->btf, ext->btf_id); if (btf_is_var(t)) err = bpf_object__resolve_ksym_var_btf_id(obj, ext); @@ -9699,7 +9693,10 @@ static int bpf_object__resolve_externs(struct bpf_object *obj, ext = &obj->externs[i]; if (ext->type == EXT_KSYM) { - if (ext->ksym.type_id) + if (obj->gen_loader) + /* the loader prog resolves it */ + ext->is_set = true; + else if (ext->ksym.type_id) need_vmlinux_btf = true; else need_kallsyms = true; -- 2.56.1.987.g2ec0ab4260-goog