Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Add getters for BTF.ext func and line info

Introducing new libbpf API getters for BTF.ext func and line info,
namely:
bpf_program__func_info
bpf_program__func_info_cnt
bpf_program__line_info
bpf_program__line_info_cnt

This change enables scenarios, when user needs to load bpf_program
directly using `bpf_prog_load`, instead of higher-level
`bpf_object__load`. Line and func info are required for checking BTF
info in verifier; verification may fail without these fields if, for
example, program calls `bpf_obj_new`.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250408234417.452565-2-mykyta.yatsenko5@gmail.com

authored by

Mykyta Yatsenko and committed by
Andrii Nakryiko
243d720e 37b1b3ed

+34
+24
tools/lib/bpf/libbpf.c
··· 9446 9446 return 0; 9447 9447 } 9448 9448 9449 + struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog) 9450 + { 9451 + if (prog->func_info_rec_size != sizeof(struct bpf_func_info)) 9452 + return libbpf_err_ptr(-EOPNOTSUPP); 9453 + return prog->func_info; 9454 + } 9455 + 9456 + __u32 bpf_program__func_info_cnt(const struct bpf_program *prog) 9457 + { 9458 + return prog->func_info_cnt; 9459 + } 9460 + 9461 + struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog) 9462 + { 9463 + if (prog->line_info_rec_size != sizeof(struct bpf_line_info)) 9464 + return libbpf_err_ptr(-EOPNOTSUPP); 9465 + return prog->line_info; 9466 + } 9467 + 9468 + __u32 bpf_program__line_info_cnt(const struct bpf_program *prog) 9469 + { 9470 + return prog->line_info_cnt; 9471 + } 9472 + 9449 9473 #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \ 9450 9474 .sec = (char *)sec_pfx, \ 9451 9475 .prog_type = BPF_PROG_TYPE_##ptype, \
+6
tools/lib/bpf/libbpf.h
··· 940 940 LIBBPF_API const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size); 941 941 LIBBPF_API int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size); 942 942 943 + LIBBPF_API struct bpf_func_info *bpf_program__func_info(const struct bpf_program *prog); 944 + LIBBPF_API __u32 bpf_program__func_info_cnt(const struct bpf_program *prog); 945 + 946 + LIBBPF_API struct bpf_line_info *bpf_program__line_info(const struct bpf_program *prog); 947 + LIBBPF_API __u32 bpf_program__line_info_cnt(const struct bpf_program *prog); 948 + 943 949 /** 944 950 * @brief **bpf_program__set_attach_target()** sets BTF-based attach target 945 951 * for supported BPF program types:
+4
tools/lib/bpf/libbpf.map
··· 437 437 bpf_linker__add_fd; 438 438 bpf_linker__new_fd; 439 439 bpf_object__prepare; 440 + bpf_program__func_info; 441 + bpf_program__func_info_cnt; 442 + bpf_program__line_info; 443 + bpf_program__line_info_cnt; 440 444 btf__add_decl_attr; 441 445 btf__add_type_attr; 442 446 } LIBBPF_1.5.0;