1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * Common eBPF ELF object loading operations.
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
9 * Copyright (C) 2017 Nicira, Inc.
10 * Copyright (C) 2019 Isovalent, Inc.
28 #include <asm/unistd.h>
29 #include <linux/err.h>
30 #include <linux/kernel.h>
31 #include <linux/bpf.h>
32 #include <linux/btf.h>
33 #include <linux/filter.h>
34 #include <linux/list.h>
35 #include <linux/limits.h>
36 #include <linux/perf_event.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/version.h>
39 #include <sys/epoll.h>
40 #include <sys/ioctl.h>
43 #include <sys/types.h>
45 #include <sys/utsname.h>
46 #include <sys/resource.h>
54 #include "str_error.h"
55 #include "libbpf_internal.h"
63 #define BPF_FS_MAGIC 0xcafe4a11
66 #define BPF_INSN_SZ (sizeof(struct bpf_insn))
68 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
69 * compilation if user enables corresponding warning. Disable it explicitly.
71 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
73 #define __printf(a, b) __attribute__((format(printf, a, b)))
75 static struct bpf_map
*bpf_object__add_map(struct bpf_object
*obj
);
76 static const struct btf_type
*
77 skip_mods_and_typedefs(const struct btf
*btf
, __u32 id
, __u32
*res_id
);
79 static int __base_pr(enum libbpf_print_level level
, const char *format
,
82 if (level
== LIBBPF_DEBUG
)
85 return vfprintf(stderr
, format
, args
);
88 static libbpf_print_fn_t __libbpf_pr
= __base_pr
;
90 libbpf_print_fn_t
libbpf_set_print(libbpf_print_fn_t fn
)
92 libbpf_print_fn_t old_print_fn
= __libbpf_pr
;
99 void libbpf_print(enum libbpf_print_level level
, const char *format
, ...)
106 va_start(args
, format
);
107 __libbpf_pr(level
, format
, args
);
111 static void pr_perm_msg(int err
)
116 if (err
!= -EPERM
|| geteuid() != 0)
119 err
= getrlimit(RLIMIT_MEMLOCK
, &limit
);
123 if (limit
.rlim_cur
== RLIM_INFINITY
)
126 if (limit
.rlim_cur
< 1024)
127 snprintf(buf
, sizeof(buf
), "%zu bytes", (size_t)limit
.rlim_cur
);
128 else if (limit
.rlim_cur
< 1024*1024)
129 snprintf(buf
, sizeof(buf
), "%.1f KiB", (double)limit
.rlim_cur
/ 1024);
131 snprintf(buf
, sizeof(buf
), "%.1f MiB", (double)limit
.rlim_cur
/ (1024*1024));
133 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
137 #define STRERR_BUFSIZE 128
139 /* Copied from tools/perf/util/util.h */
141 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
145 # define zclose(fd) ({ \
148 ___err = close((fd)); \
153 static inline __u64
ptr_to_u64(const void *ptr
)
155 return (__u64
) (unsigned long) ptr
;
158 enum kern_feature_id
{
159 /* v4.14: kernel support for program & map names. */
161 /* v5.2: kernel support for global data sections. */
165 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
167 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
169 /* BTF_FUNC_GLOBAL is supported */
170 FEAT_BTF_GLOBAL_FUNC
,
171 /* BPF_F_MMAPABLE is supported for arrays */
173 /* kernel support for expected_attach_type in BPF_PROG_LOAD */
174 FEAT_EXP_ATTACH_TYPE
,
175 /* bpf_probe_read_{kernel,user}[_str] helpers */
176 FEAT_PROBE_READ_KERN
,
177 /* BPF_PROG_BIND_MAP is supported */
179 /* Kernel support for module BTFs */
184 static bool kernel_supports(enum kern_feature_id feat_id
);
194 enum reloc_type type
;
203 typedef struct bpf_link
*(*attach_fn_t
)(const struct bpf_sec_def
*sec
,
204 struct bpf_program
*prog
);
209 enum bpf_prog_type prog_type
;
210 enum bpf_attach_type expected_attach_type
;
211 bool is_exp_attach_type_optional
;
215 attach_fn_t attach_fn
;
219 * bpf_prog should be a better name but it has been used in
223 const struct bpf_sec_def
*sec_def
;
226 /* this program's instruction offset (in number of instructions)
227 * within its containing ELF section
230 /* number of original instructions in ELF section belonging to this
231 * program, not taking into account subprogram instructions possible
232 * appended later during relocation
235 /* Offset (in number of instructions) of the start of instruction
236 * belonging to this BPF program within its containing main BPF
237 * program. For the entry-point (main) BPF program, this is always
238 * zero. For a sub-program, this gets reset before each of main BPF
239 * programs are processed and relocated and is used to determined
240 * whether sub-program was already appended to the main program, and
241 * if yes, at which instruction offset.
246 /* sec_name with / replaced by _; makes recursive pinning
247 * in bpf_object__pin_programs easier
251 /* instructions that belong to BPF program; insns[0] is located at
252 * sec_insn_off instruction within its ELF section in ELF file, so
253 * when mapping ELF file instruction index to the local instruction,
254 * one needs to subtract sec_insn_off; and vice versa.
256 struct bpf_insn
*insns
;
257 /* actual number of instruction in this BPF program's image; for
258 * entry-point BPF programs this includes the size of main program
259 * itself plus all the used sub-programs, appended at the end
263 struct reloc_desc
*reloc_desc
;
271 bpf_program_prep_t preprocessor
;
273 struct bpf_object
*obj
;
275 bpf_program_clear_priv_t clear_priv
;
278 enum bpf_prog_type type
;
279 enum bpf_attach_type expected_attach_type
;
281 __u32 attach_btf_obj_fd
;
283 __u32 attach_prog_fd
;
285 __u32 func_info_rec_size
;
289 __u32 line_info_rec_size
;
294 struct bpf_struct_ops
{
296 const struct btf_type
*type
;
297 struct bpf_program
**progs
;
298 __u32
*kern_func_off
;
299 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
301 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
302 * btf_vmlinux's format.
303 * struct bpf_struct_ops_tcp_congestion_ops {
304 * [... some other kernel fields ...]
305 * struct tcp_congestion_ops data;
307 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
308 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
315 #define DATA_SEC ".data"
316 #define BSS_SEC ".bss"
317 #define RODATA_SEC ".rodata"
318 #define KCONFIG_SEC ".kconfig"
319 #define KSYMS_SEC ".ksyms"
320 #define STRUCT_OPS_SEC ".struct_ops"
322 enum libbpf_map_type
{
330 static const char * const libbpf_type_to_btf_name
[] = {
331 [LIBBPF_MAP_DATA
] = DATA_SEC
,
332 [LIBBPF_MAP_BSS
] = BSS_SEC
,
333 [LIBBPF_MAP_RODATA
] = RODATA_SEC
,
334 [LIBBPF_MAP_KCONFIG
] = KCONFIG_SEC
,
344 struct bpf_map_def def
;
347 __u32 btf_key_type_id
;
348 __u32 btf_value_type_id
;
349 __u32 btf_vmlinux_value_type_id
;
351 bpf_map_clear_priv_t clear_priv
;
352 enum libbpf_map_type libbpf_type
;
354 struct bpf_struct_ops
*st_ops
;
355 struct bpf_map
*inner_map
;
379 enum extern_type type
;
395 unsigned long long addr
;
397 /* target btf_id of the corresponding kernel var. */
400 /* local btf_id of the ksym extern's type. */
406 static LIST_HEAD(bpf_objects_list
);
416 char name
[BPF_OBJ_NAME_LEN
];
420 struct bpf_program
*programs
;
422 struct bpf_map
*maps
;
427 struct extern_desc
*externs
;
436 * Information when doing elf related work. Only valid if fd
449 Elf_Data
*st_ops_data
;
450 size_t shstrndx
; /* section index for section name strings */
459 __u32 btf_maps_sec_btf_id
;
468 * All loaded bpf_object is linked in a list, which is
469 * hidden to caller. bpf_objects__<func> handlers deal with
472 struct list_head list
;
475 struct btf_ext
*btf_ext
;
477 /* Parse and load BTF vmlinux if any of the programs in the object need
480 struct btf
*btf_vmlinux
;
481 /* vmlinux BTF override for CO-RE relocations */
482 struct btf
*btf_vmlinux_override
;
483 /* Lazily initialized kernel module BTFs */
484 struct module_btf
*btf_modules
;
485 bool btf_modules_loaded
;
486 size_t btf_module_cnt
;
487 size_t btf_module_cap
;
490 bpf_object_clear_priv_t clear_priv
;
494 #define obj_elf_valid(o) ((o)->efile.elf)
496 static const char *elf_sym_str(const struct bpf_object
*obj
, size_t off
);
497 static const char *elf_sec_str(const struct bpf_object
*obj
, size_t off
);
498 static Elf_Scn
*elf_sec_by_idx(const struct bpf_object
*obj
, size_t idx
);
499 static Elf_Scn
*elf_sec_by_name(const struct bpf_object
*obj
, const char *name
);
500 static int elf_sec_hdr(const struct bpf_object
*obj
, Elf_Scn
*scn
, GElf_Shdr
*hdr
);
501 static const char *elf_sec_name(const struct bpf_object
*obj
, Elf_Scn
*scn
);
502 static Elf_Data
*elf_sec_data(const struct bpf_object
*obj
, Elf_Scn
*scn
);
503 static int elf_sym_by_sec_off(const struct bpf_object
*obj
, size_t sec_idx
,
504 size_t off
, __u32 sym_type
, GElf_Sym
*sym
);
506 void bpf_program__unload(struct bpf_program
*prog
)
514 * If the object is opened but the program was never loaded,
515 * it is possible that prog->instances.nr == -1.
517 if (prog
->instances
.nr
> 0) {
518 for (i
= 0; i
< prog
->instances
.nr
; i
++)
519 zclose(prog
->instances
.fds
[i
]);
520 } else if (prog
->instances
.nr
!= -1) {
521 pr_warn("Internal error: instances.nr is %d\n",
525 prog
->instances
.nr
= -1;
526 zfree(&prog
->instances
.fds
);
528 zfree(&prog
->func_info
);
529 zfree(&prog
->line_info
);
532 static void bpf_program__exit(struct bpf_program
*prog
)
537 if (prog
->clear_priv
)
538 prog
->clear_priv(prog
, prog
->priv
);
541 prog
->clear_priv
= NULL
;
543 bpf_program__unload(prog
);
545 zfree(&prog
->sec_name
);
546 zfree(&prog
->pin_name
);
548 zfree(&prog
->reloc_desc
);
555 static char *__bpf_program__pin_name(struct bpf_program
*prog
)
559 name
= p
= strdup(prog
->sec_name
);
560 while ((p
= strchr(p
, '/')))
566 static bool insn_is_subprog_call(const struct bpf_insn
*insn
)
568 return BPF_CLASS(insn
->code
) == BPF_JMP
&&
569 BPF_OP(insn
->code
) == BPF_CALL
&&
570 BPF_SRC(insn
->code
) == BPF_K
&&
571 insn
->src_reg
== BPF_PSEUDO_CALL
&&
572 insn
->dst_reg
== 0 &&
577 bpf_object__init_prog(struct bpf_object
*obj
, struct bpf_program
*prog
,
578 const char *name
, size_t sec_idx
, const char *sec_name
,
579 size_t sec_off
, void *insn_data
, size_t insn_data_sz
)
581 if (insn_data_sz
== 0 || insn_data_sz
% BPF_INSN_SZ
|| sec_off
% BPF_INSN_SZ
) {
582 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
583 sec_name
, name
, sec_off
, insn_data_sz
);
587 memset(prog
, 0, sizeof(*prog
));
590 prog
->sec_idx
= sec_idx
;
591 prog
->sec_insn_off
= sec_off
/ BPF_INSN_SZ
;
592 prog
->sec_insn_cnt
= insn_data_sz
/ BPF_INSN_SZ
;
593 /* insns_cnt can later be increased by appending used subprograms */
594 prog
->insns_cnt
= prog
->sec_insn_cnt
;
596 prog
->type
= BPF_PROG_TYPE_UNSPEC
;
599 prog
->instances
.fds
= NULL
;
600 prog
->instances
.nr
= -1;
602 prog
->sec_name
= strdup(sec_name
);
606 prog
->name
= strdup(name
);
610 prog
->pin_name
= __bpf_program__pin_name(prog
);
614 prog
->insns
= malloc(insn_data_sz
);
617 memcpy(prog
->insns
, insn_data
, insn_data_sz
);
621 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name
, name
);
622 bpf_program__exit(prog
);
627 bpf_object__add_programs(struct bpf_object
*obj
, Elf_Data
*sec_data
,
628 const char *sec_name
, int sec_idx
)
630 struct bpf_program
*prog
, *progs
;
631 void *data
= sec_data
->d_buf
;
632 size_t sec_sz
= sec_data
->d_size
, sec_off
, prog_sz
;
637 progs
= obj
->programs
;
638 nr_progs
= obj
->nr_programs
;
641 while (sec_off
< sec_sz
) {
642 if (elf_sym_by_sec_off(obj
, sec_idx
, sec_off
, STT_FUNC
, &sym
)) {
643 pr_warn("sec '%s': failed to find program symbol at offset %zu\n",
645 return -LIBBPF_ERRNO__FORMAT
;
648 prog_sz
= sym
.st_size
;
650 name
= elf_sym_str(obj
, sym
.st_name
);
652 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
654 return -LIBBPF_ERRNO__FORMAT
;
657 if (sec_off
+ prog_sz
> sec_sz
) {
658 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
660 return -LIBBPF_ERRNO__FORMAT
;
663 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
664 sec_name
, name
, sec_off
/ BPF_INSN_SZ
, sec_off
, prog_sz
/ BPF_INSN_SZ
, prog_sz
);
666 progs
= libbpf_reallocarray(progs
, nr_progs
+ 1, sizeof(*progs
));
669 * In this case the original obj->programs
670 * is still valid, so don't need special treat for
671 * bpf_close_object().
673 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
677 obj
->programs
= progs
;
679 prog
= &progs
[nr_progs
];
681 err
= bpf_object__init_prog(obj
, prog
, name
, sec_idx
, sec_name
,
682 sec_off
, data
+ sec_off
, prog_sz
);
687 obj
->nr_programs
= nr_progs
;
695 static __u32
get_kernel_version(void)
697 __u32 major
, minor
, patch
;
701 if (sscanf(info
.release
, "%u.%u.%u", &major
, &minor
, &patch
) != 3)
703 return KERNEL_VERSION(major
, minor
, patch
);
706 static const struct btf_member
*
707 find_member_by_offset(const struct btf_type
*t
, __u32 bit_offset
)
709 struct btf_member
*m
;
712 for (i
= 0, m
= btf_members(t
); i
< btf_vlen(t
); i
++, m
++) {
713 if (btf_member_bit_offset(t
, i
) == bit_offset
)
720 static const struct btf_member
*
721 find_member_by_name(const struct btf
*btf
, const struct btf_type
*t
,
724 struct btf_member
*m
;
727 for (i
= 0, m
= btf_members(t
); i
< btf_vlen(t
); i
++, m
++) {
728 if (!strcmp(btf__name_by_offset(btf
, m
->name_off
), name
))
735 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
736 static int find_btf_by_prefix_kind(const struct btf
*btf
, const char *prefix
,
737 const char *name
, __u32 kind
);
740 find_struct_ops_kern_types(const struct btf
*btf
, const char *tname
,
741 const struct btf_type
**type
, __u32
*type_id
,
742 const struct btf_type
**vtype
, __u32
*vtype_id
,
743 const struct btf_member
**data_member
)
745 const struct btf_type
*kern_type
, *kern_vtype
;
746 const struct btf_member
*kern_data_member
;
747 __s32 kern_vtype_id
, kern_type_id
;
750 kern_type_id
= btf__find_by_name_kind(btf
, tname
, BTF_KIND_STRUCT
);
751 if (kern_type_id
< 0) {
752 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
756 kern_type
= btf__type_by_id(btf
, kern_type_id
);
758 /* Find the corresponding "map_value" type that will be used
759 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
760 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
763 kern_vtype_id
= find_btf_by_prefix_kind(btf
, STRUCT_OPS_VALUE_PREFIX
,
764 tname
, BTF_KIND_STRUCT
);
765 if (kern_vtype_id
< 0) {
766 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
767 STRUCT_OPS_VALUE_PREFIX
, tname
);
768 return kern_vtype_id
;
770 kern_vtype
= btf__type_by_id(btf
, kern_vtype_id
);
772 /* Find "struct tcp_congestion_ops" from
773 * struct bpf_struct_ops_tcp_congestion_ops {
775 * struct tcp_congestion_ops data;
778 kern_data_member
= btf_members(kern_vtype
);
779 for (i
= 0; i
< btf_vlen(kern_vtype
); i
++, kern_data_member
++) {
780 if (kern_data_member
->type
== kern_type_id
)
783 if (i
== btf_vlen(kern_vtype
)) {
784 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
785 tname
, STRUCT_OPS_VALUE_PREFIX
, tname
);
790 *type_id
= kern_type_id
;
792 *vtype_id
= kern_vtype_id
;
793 *data_member
= kern_data_member
;
798 static bool bpf_map__is_struct_ops(const struct bpf_map
*map
)
800 return map
->def
.type
== BPF_MAP_TYPE_STRUCT_OPS
;
803 /* Init the map's fields that depend on kern_btf */
804 static int bpf_map__init_kern_struct_ops(struct bpf_map
*map
,
805 const struct btf
*btf
,
806 const struct btf
*kern_btf
)
808 const struct btf_member
*member
, *kern_member
, *kern_data_member
;
809 const struct btf_type
*type
, *kern_type
, *kern_vtype
;
810 __u32 i
, kern_type_id
, kern_vtype_id
, kern_data_off
;
811 struct bpf_struct_ops
*st_ops
;
812 void *data
, *kern_data
;
816 st_ops
= map
->st_ops
;
818 tname
= st_ops
->tname
;
819 err
= find_struct_ops_kern_types(kern_btf
, tname
,
820 &kern_type
, &kern_type_id
,
821 &kern_vtype
, &kern_vtype_id
,
826 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
827 map
->name
, st_ops
->type_id
, kern_type_id
, kern_vtype_id
);
829 map
->def
.value_size
= kern_vtype
->size
;
830 map
->btf_vmlinux_value_type_id
= kern_vtype_id
;
832 st_ops
->kern_vdata
= calloc(1, kern_vtype
->size
);
833 if (!st_ops
->kern_vdata
)
837 kern_data_off
= kern_data_member
->offset
/ 8;
838 kern_data
= st_ops
->kern_vdata
+ kern_data_off
;
840 member
= btf_members(type
);
841 for (i
= 0; i
< btf_vlen(type
); i
++, member
++) {
842 const struct btf_type
*mtype
, *kern_mtype
;
843 __u32 mtype_id
, kern_mtype_id
;
844 void *mdata
, *kern_mdata
;
845 __s64 msize
, kern_msize
;
846 __u32 moff
, kern_moff
;
847 __u32 kern_member_idx
;
850 mname
= btf__name_by_offset(btf
, member
->name_off
);
851 kern_member
= find_member_by_name(kern_btf
, kern_type
, mname
);
853 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
858 kern_member_idx
= kern_member
- btf_members(kern_type
);
859 if (btf_member_bitfield_size(type
, i
) ||
860 btf_member_bitfield_size(kern_type
, kern_member_idx
)) {
861 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
866 moff
= member
->offset
/ 8;
867 kern_moff
= kern_member
->offset
/ 8;
870 kern_mdata
= kern_data
+ kern_moff
;
872 mtype
= skip_mods_and_typedefs(btf
, member
->type
, &mtype_id
);
873 kern_mtype
= skip_mods_and_typedefs(kern_btf
, kern_member
->type
,
875 if (BTF_INFO_KIND(mtype
->info
) !=
876 BTF_INFO_KIND(kern_mtype
->info
)) {
877 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
878 map
->name
, mname
, BTF_INFO_KIND(mtype
->info
),
879 BTF_INFO_KIND(kern_mtype
->info
));
883 if (btf_is_ptr(mtype
)) {
884 struct bpf_program
*prog
;
886 mtype
= skip_mods_and_typedefs(btf
, mtype
->type
, &mtype_id
);
887 kern_mtype
= skip_mods_and_typedefs(kern_btf
,
890 if (!btf_is_func_proto(mtype
) ||
891 !btf_is_func_proto(kern_mtype
)) {
892 pr_warn("struct_ops init_kern %s: non func ptr %s is not supported\n",
897 prog
= st_ops
->progs
[i
];
899 pr_debug("struct_ops init_kern %s: func ptr %s is not set\n",
904 prog
->attach_btf_id
= kern_type_id
;
905 prog
->expected_attach_type
= kern_member_idx
;
907 st_ops
->kern_func_off
[i
] = kern_data_off
+ kern_moff
;
909 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
910 map
->name
, mname
, prog
->name
, moff
,
916 msize
= btf__resolve_size(btf
, mtype_id
);
917 kern_msize
= btf__resolve_size(kern_btf
, kern_mtype_id
);
918 if (msize
< 0 || kern_msize
< 0 || msize
!= kern_msize
) {
919 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
920 map
->name
, mname
, (ssize_t
)msize
,
921 (ssize_t
)kern_msize
);
925 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
926 map
->name
, mname
, (unsigned int)msize
,
928 memcpy(kern_mdata
, mdata
, msize
);
934 static int bpf_object__init_kern_struct_ops_maps(struct bpf_object
*obj
)
940 for (i
= 0; i
< obj
->nr_maps
; i
++) {
943 if (!bpf_map__is_struct_ops(map
))
946 err
= bpf_map__init_kern_struct_ops(map
, obj
->btf
,
955 static int bpf_object__init_struct_ops_maps(struct bpf_object
*obj
)
957 const struct btf_type
*type
, *datasec
;
958 const struct btf_var_secinfo
*vsi
;
959 struct bpf_struct_ops
*st_ops
;
960 const char *tname
, *var_name
;
961 __s32 type_id
, datasec_id
;
962 const struct btf
*btf
;
966 if (obj
->efile
.st_ops_shndx
== -1)
970 datasec_id
= btf__find_by_name_kind(btf
, STRUCT_OPS_SEC
,
972 if (datasec_id
< 0) {
973 pr_warn("struct_ops init: DATASEC %s not found\n",
978 datasec
= btf__type_by_id(btf
, datasec_id
);
979 vsi
= btf_var_secinfos(datasec
);
980 for (i
= 0; i
< btf_vlen(datasec
); i
++, vsi
++) {
981 type
= btf__type_by_id(obj
->btf
, vsi
->type
);
982 var_name
= btf__name_by_offset(obj
->btf
, type
->name_off
);
984 type_id
= btf__resolve_type(obj
->btf
, vsi
->type
);
986 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
987 vsi
->type
, STRUCT_OPS_SEC
);
991 type
= btf__type_by_id(obj
->btf
, type_id
);
992 tname
= btf__name_by_offset(obj
->btf
, type
->name_off
);
994 pr_warn("struct_ops init: anonymous type is not supported\n");
997 if (!btf_is_struct(type
)) {
998 pr_warn("struct_ops init: %s is not a struct\n", tname
);
1002 map
= bpf_object__add_map(obj
);
1004 return PTR_ERR(map
);
1006 map
->sec_idx
= obj
->efile
.st_ops_shndx
;
1007 map
->sec_offset
= vsi
->offset
;
1008 map
->name
= strdup(var_name
);
1012 map
->def
.type
= BPF_MAP_TYPE_STRUCT_OPS
;
1013 map
->def
.key_size
= sizeof(int);
1014 map
->def
.value_size
= type
->size
;
1015 map
->def
.max_entries
= 1;
1017 map
->st_ops
= calloc(1, sizeof(*map
->st_ops
));
1020 st_ops
= map
->st_ops
;
1021 st_ops
->data
= malloc(type
->size
);
1022 st_ops
->progs
= calloc(btf_vlen(type
), sizeof(*st_ops
->progs
));
1023 st_ops
->kern_func_off
= malloc(btf_vlen(type
) *
1024 sizeof(*st_ops
->kern_func_off
));
1025 if (!st_ops
->data
|| !st_ops
->progs
|| !st_ops
->kern_func_off
)
1028 if (vsi
->offset
+ type
->size
> obj
->efile
.st_ops_data
->d_size
) {
1029 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
1030 var_name
, STRUCT_OPS_SEC
);
1034 memcpy(st_ops
->data
,
1035 obj
->efile
.st_ops_data
->d_buf
+ vsi
->offset
,
1037 st_ops
->tname
= tname
;
1038 st_ops
->type
= type
;
1039 st_ops
->type_id
= type_id
;
1041 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1042 tname
, type_id
, var_name
, vsi
->offset
);
1048 static struct bpf_object
*bpf_object__new(const char *path
,
1049 const void *obj_buf
,
1051 const char *obj_name
)
1053 struct bpf_object
*obj
;
1056 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
1058 pr_warn("alloc memory failed for %s\n", path
);
1059 return ERR_PTR(-ENOMEM
);
1062 strcpy(obj
->path
, path
);
1064 strncpy(obj
->name
, obj_name
, sizeof(obj
->name
) - 1);
1065 obj
->name
[sizeof(obj
->name
) - 1] = 0;
1067 /* Using basename() GNU version which doesn't modify arg. */
1068 strncpy(obj
->name
, basename((void *)path
),
1069 sizeof(obj
->name
) - 1);
1070 end
= strchr(obj
->name
, '.');
1077 * Caller of this function should also call
1078 * bpf_object__elf_finish() after data collection to return
1079 * obj_buf to user. If not, we should duplicate the buffer to
1080 * avoid user freeing them before elf finish.
1082 obj
->efile
.obj_buf
= obj_buf
;
1083 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
1084 obj
->efile
.maps_shndx
= -1;
1085 obj
->efile
.btf_maps_shndx
= -1;
1086 obj
->efile
.data_shndx
= -1;
1087 obj
->efile
.rodata_shndx
= -1;
1088 obj
->efile
.bss_shndx
= -1;
1089 obj
->efile
.st_ops_shndx
= -1;
1090 obj
->kconfig_map_idx
= -1;
1091 obj
->rodata_map_idx
= -1;
1093 obj
->kern_version
= get_kernel_version();
1094 obj
->loaded
= false;
1096 INIT_LIST_HEAD(&obj
->list
);
1097 list_add(&obj
->list
, &bpf_objects_list
);
1101 static void bpf_object__elf_finish(struct bpf_object
*obj
)
1103 if (!obj_elf_valid(obj
))
1106 if (obj
->efile
.elf
) {
1107 elf_end(obj
->efile
.elf
);
1108 obj
->efile
.elf
= NULL
;
1110 obj
->efile
.symbols
= NULL
;
1111 obj
->efile
.data
= NULL
;
1112 obj
->efile
.rodata
= NULL
;
1113 obj
->efile
.bss
= NULL
;
1114 obj
->efile
.st_ops_data
= NULL
;
1116 zfree(&obj
->efile
.reloc_sects
);
1117 obj
->efile
.nr_reloc_sects
= 0;
1118 zclose(obj
->efile
.fd
);
1119 obj
->efile
.obj_buf
= NULL
;
1120 obj
->efile
.obj_buf_sz
= 0;
1123 /* if libelf is old and doesn't support mmap(), fall back to read() */
1124 #ifndef ELF_C_READ_MMAP
1125 #define ELF_C_READ_MMAP ELF_C_READ
1128 static int bpf_object__elf_init(struct bpf_object
*obj
)
1133 if (obj_elf_valid(obj
)) {
1134 pr_warn("elf: init internal error\n");
1135 return -LIBBPF_ERRNO__LIBELF
;
1138 if (obj
->efile
.obj_buf_sz
> 0) {
1140 * obj_buf should have been validated by
1141 * bpf_object__open_buffer().
1143 obj
->efile
.elf
= elf_memory((char *)obj
->efile
.obj_buf
,
1144 obj
->efile
.obj_buf_sz
);
1146 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
1147 if (obj
->efile
.fd
< 0) {
1148 char errmsg
[STRERR_BUFSIZE
], *cp
;
1151 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
1152 pr_warn("elf: failed to open %s: %s\n", obj
->path
, cp
);
1156 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
, ELF_C_READ_MMAP
, NULL
);
1159 if (!obj
->efile
.elf
) {
1160 pr_warn("elf: failed to open %s as ELF file: %s\n", obj
->path
, elf_errmsg(-1));
1161 err
= -LIBBPF_ERRNO__LIBELF
;
1165 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
1166 pr_warn("elf: failed to get ELF header from %s: %s\n", obj
->path
, elf_errmsg(-1));
1167 err
= -LIBBPF_ERRNO__FORMAT
;
1170 ep
= &obj
->efile
.ehdr
;
1172 if (elf_getshdrstrndx(obj
->efile
.elf
, &obj
->efile
.shstrndx
)) {
1173 pr_warn("elf: failed to get section names section index for %s: %s\n",
1174 obj
->path
, elf_errmsg(-1));
1175 err
= -LIBBPF_ERRNO__FORMAT
;
1179 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1180 if (!elf_rawdata(elf_getscn(obj
->efile
.elf
, obj
->efile
.shstrndx
), NULL
)) {
1181 pr_warn("elf: failed to get section names strings from %s: %s\n",
1182 obj
->path
, elf_errmsg(-1));
1183 return -LIBBPF_ERRNO__FORMAT
;
1186 /* Old LLVM set e_machine to EM_NONE */
1187 if (ep
->e_type
!= ET_REL
||
1188 (ep
->e_machine
&& ep
->e_machine
!= EM_BPF
)) {
1189 pr_warn("elf: %s is not a valid eBPF object file\n", obj
->path
);
1190 err
= -LIBBPF_ERRNO__FORMAT
;
1196 bpf_object__elf_finish(obj
);
1200 static int bpf_object__check_endianness(struct bpf_object
*obj
)
1202 #if __BYTE_ORDER == __LITTLE_ENDIAN
1203 if (obj
->efile
.ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
)
1205 #elif __BYTE_ORDER == __BIG_ENDIAN
1206 if (obj
->efile
.ehdr
.e_ident
[EI_DATA
] == ELFDATA2MSB
)
1209 # error "Unrecognized __BYTE_ORDER__"
1211 pr_warn("elf: endianness mismatch in %s.\n", obj
->path
);
1212 return -LIBBPF_ERRNO__ENDIAN
;
1216 bpf_object__init_license(struct bpf_object
*obj
, void *data
, size_t size
)
1218 memcpy(obj
->license
, data
, min(size
, sizeof(obj
->license
) - 1));
1219 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
1224 bpf_object__init_kversion(struct bpf_object
*obj
, void *data
, size_t size
)
1228 if (size
!= sizeof(kver
)) {
1229 pr_warn("invalid kver section in %s\n", obj
->path
);
1230 return -LIBBPF_ERRNO__FORMAT
;
1232 memcpy(&kver
, data
, sizeof(kver
));
1233 obj
->kern_version
= kver
;
1234 pr_debug("kernel version of %s is %x\n", obj
->path
, obj
->kern_version
);
1238 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type
)
1240 if (type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
||
1241 type
== BPF_MAP_TYPE_HASH_OF_MAPS
)
1246 int bpf_object__section_size(const struct bpf_object
*obj
, const char *name
,
1254 } else if (!strcmp(name
, DATA_SEC
)) {
1255 if (obj
->efile
.data
)
1256 *size
= obj
->efile
.data
->d_size
;
1257 } else if (!strcmp(name
, BSS_SEC
)) {
1259 *size
= obj
->efile
.bss
->d_size
;
1260 } else if (!strcmp(name
, RODATA_SEC
)) {
1261 if (obj
->efile
.rodata
)
1262 *size
= obj
->efile
.rodata
->d_size
;
1263 } else if (!strcmp(name
, STRUCT_OPS_SEC
)) {
1264 if (obj
->efile
.st_ops_data
)
1265 *size
= obj
->efile
.st_ops_data
->d_size
;
1267 Elf_Scn
*scn
= elf_sec_by_name(obj
, name
);
1268 Elf_Data
*data
= elf_sec_data(obj
, scn
);
1271 ret
= 0; /* found it */
1272 *size
= data
->d_size
;
1276 return *size
? 0 : ret
;
1279 int bpf_object__variable_offset(const struct bpf_object
*obj
, const char *name
,
1282 Elf_Data
*symbols
= obj
->efile
.symbols
;
1289 for (si
= 0; si
< symbols
->d_size
/ sizeof(GElf_Sym
); si
++) {
1292 if (!gelf_getsym(symbols
, si
, &sym
))
1294 if (GELF_ST_BIND(sym
.st_info
) != STB_GLOBAL
||
1295 GELF_ST_TYPE(sym
.st_info
) != STT_OBJECT
)
1298 sname
= elf_sym_str(obj
, sym
.st_name
);
1300 pr_warn("failed to get sym name string for var %s\n",
1304 if (strcmp(name
, sname
) == 0) {
1305 *off
= sym
.st_value
;
1313 static struct bpf_map
*bpf_object__add_map(struct bpf_object
*obj
)
1315 struct bpf_map
*new_maps
;
1319 if (obj
->nr_maps
< obj
->maps_cap
)
1320 return &obj
->maps
[obj
->nr_maps
++];
1322 new_cap
= max((size_t)4, obj
->maps_cap
* 3 / 2);
1323 new_maps
= libbpf_reallocarray(obj
->maps
, new_cap
, sizeof(*obj
->maps
));
1325 pr_warn("alloc maps for object failed\n");
1326 return ERR_PTR(-ENOMEM
);
1329 obj
->maps_cap
= new_cap
;
1330 obj
->maps
= new_maps
;
1332 /* zero out new maps */
1333 memset(obj
->maps
+ obj
->nr_maps
, 0,
1334 (obj
->maps_cap
- obj
->nr_maps
) * sizeof(*obj
->maps
));
1336 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
1337 * when failure (zclose won't close negative fd)).
1339 for (i
= obj
->nr_maps
; i
< obj
->maps_cap
; i
++) {
1340 obj
->maps
[i
].fd
= -1;
1341 obj
->maps
[i
].inner_map_fd
= -1;
1344 return &obj
->maps
[obj
->nr_maps
++];
1347 static size_t bpf_map_mmap_sz(const struct bpf_map
*map
)
1349 long page_sz
= sysconf(_SC_PAGE_SIZE
);
1352 map_sz
= (size_t)roundup(map
->def
.value_size
, 8) * map
->def
.max_entries
;
1353 map_sz
= roundup(map_sz
, page_sz
);
1357 static char *internal_map_name(struct bpf_object
*obj
,
1358 enum libbpf_map_type type
)
1360 char map_name
[BPF_OBJ_NAME_LEN
], *p
;
1361 const char *sfx
= libbpf_type_to_btf_name
[type
];
1362 int sfx_len
= max((size_t)7, strlen(sfx
));
1363 int pfx_len
= min((size_t)BPF_OBJ_NAME_LEN
- sfx_len
- 1,
1366 snprintf(map_name
, sizeof(map_name
), "%.*s%.*s", pfx_len
, obj
->name
,
1367 sfx_len
, libbpf_type_to_btf_name
[type
]);
1369 /* sanitise map name to characters allowed by kernel */
1370 for (p
= map_name
; *p
&& p
< map_name
+ sizeof(map_name
); p
++)
1371 if (!isalnum(*p
) && *p
!= '_' && *p
!= '.')
1374 return strdup(map_name
);
1378 bpf_object__init_internal_map(struct bpf_object
*obj
, enum libbpf_map_type type
,
1379 int sec_idx
, void *data
, size_t data_sz
)
1381 struct bpf_map_def
*def
;
1382 struct bpf_map
*map
;
1385 map
= bpf_object__add_map(obj
);
1387 return PTR_ERR(map
);
1389 map
->libbpf_type
= type
;
1390 map
->sec_idx
= sec_idx
;
1391 map
->sec_offset
= 0;
1392 map
->name
= internal_map_name(obj
, type
);
1394 pr_warn("failed to alloc map name\n");
1399 def
->type
= BPF_MAP_TYPE_ARRAY
;
1400 def
->key_size
= sizeof(int);
1401 def
->value_size
= data_sz
;
1402 def
->max_entries
= 1;
1403 def
->map_flags
= type
== LIBBPF_MAP_RODATA
|| type
== LIBBPF_MAP_KCONFIG
1404 ? BPF_F_RDONLY_PROG
: 0;
1405 def
->map_flags
|= BPF_F_MMAPABLE
;
1407 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1408 map
->name
, map
->sec_idx
, map
->sec_offset
, def
->map_flags
);
1410 map
->mmaped
= mmap(NULL
, bpf_map_mmap_sz(map
), PROT_READ
| PROT_WRITE
,
1411 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1412 if (map
->mmaped
== MAP_FAILED
) {
1415 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1422 memcpy(map
->mmaped
, data
, data_sz
);
1424 pr_debug("map %td is \"%s\"\n", map
- obj
->maps
, map
->name
);
1428 static int bpf_object__init_global_data_maps(struct bpf_object
*obj
)
1433 * Populate obj->maps with libbpf internal maps.
1435 if (obj
->efile
.data_shndx
>= 0) {
1436 err
= bpf_object__init_internal_map(obj
, LIBBPF_MAP_DATA
,
1437 obj
->efile
.data_shndx
,
1438 obj
->efile
.data
->d_buf
,
1439 obj
->efile
.data
->d_size
);
1443 if (obj
->efile
.rodata_shndx
>= 0) {
1444 err
= bpf_object__init_internal_map(obj
, LIBBPF_MAP_RODATA
,
1445 obj
->efile
.rodata_shndx
,
1446 obj
->efile
.rodata
->d_buf
,
1447 obj
->efile
.rodata
->d_size
);
1451 obj
->rodata_map_idx
= obj
->nr_maps
- 1;
1453 if (obj
->efile
.bss_shndx
>= 0) {
1454 err
= bpf_object__init_internal_map(obj
, LIBBPF_MAP_BSS
,
1455 obj
->efile
.bss_shndx
,
1457 obj
->efile
.bss
->d_size
);
1465 static struct extern_desc
*find_extern_by_name(const struct bpf_object
*obj
,
1470 for (i
= 0; i
< obj
->nr_extern
; i
++) {
1471 if (strcmp(obj
->externs
[i
].name
, name
) == 0)
1472 return &obj
->externs
[i
];
1477 static int set_kcfg_value_tri(struct extern_desc
*ext
, void *ext_val
,
1480 switch (ext
->kcfg
.type
) {
1483 pr_warn("extern (kcfg) %s=%c should be tristate or char\n",
1487 *(bool *)ext_val
= value
== 'y' ? true : false;
1491 *(enum libbpf_tristate
*)ext_val
= TRI_YES
;
1492 else if (value
== 'm')
1493 *(enum libbpf_tristate
*)ext_val
= TRI_MODULE
;
1494 else /* value == 'n' */
1495 *(enum libbpf_tristate
*)ext_val
= TRI_NO
;
1498 *(char *)ext_val
= value
;
1504 pr_warn("extern (kcfg) %s=%c should be bool, tristate, or char\n",
1512 static int set_kcfg_value_str(struct extern_desc
*ext
, char *ext_val
,
1517 if (ext
->kcfg
.type
!= KCFG_CHAR_ARR
) {
1518 pr_warn("extern (kcfg) %s=%s should be char array\n", ext
->name
, value
);
1522 len
= strlen(value
);
1523 if (value
[len
- 1] != '"') {
1524 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
1531 if (len
>= ext
->kcfg
.sz
) {
1532 pr_warn("extern (kcfg) '%s': long string config %s of (%zu bytes) truncated to %d bytes\n",
1533 ext
->name
, value
, len
, ext
->kcfg
.sz
- 1);
1534 len
= ext
->kcfg
.sz
- 1;
1536 memcpy(ext_val
, value
+ 1, len
);
1537 ext_val
[len
] = '\0';
1542 static int parse_u64(const char *value
, __u64
*res
)
1548 *res
= strtoull(value
, &value_end
, 0);
1551 pr_warn("failed to parse '%s' as integer: %d\n", value
, err
);
1555 pr_warn("failed to parse '%s' as integer completely\n", value
);
1561 static bool is_kcfg_value_in_range(const struct extern_desc
*ext
, __u64 v
)
1563 int bit_sz
= ext
->kcfg
.sz
* 8;
1565 if (ext
->kcfg
.sz
== 8)
1568 /* Validate that value stored in u64 fits in integer of `ext->sz`
1569 * bytes size without any loss of information. If the target integer
1570 * is signed, we rely on the following limits of integer type of
1571 * Y bits and subsequent transformation:
1573 * -2^(Y-1) <= X <= 2^(Y-1) - 1
1574 * 0 <= X + 2^(Y-1) <= 2^Y - 1
1575 * 0 <= X + 2^(Y-1) < 2^Y
1577 * For unsigned target integer, check that all the (64 - Y) bits are
1580 if (ext
->kcfg
.is_signed
)
1581 return v
+ (1ULL << (bit_sz
- 1)) < (1ULL << bit_sz
);
1583 return (v
>> bit_sz
) == 0;
1586 static int set_kcfg_value_num(struct extern_desc
*ext
, void *ext_val
,
1589 if (ext
->kcfg
.type
!= KCFG_INT
&& ext
->kcfg
.type
!= KCFG_CHAR
) {
1590 pr_warn("extern (kcfg) %s=%llu should be integer\n",
1591 ext
->name
, (unsigned long long)value
);
1594 if (!is_kcfg_value_in_range(ext
, value
)) {
1595 pr_warn("extern (kcfg) %s=%llu value doesn't fit in %d bytes\n",
1596 ext
->name
, (unsigned long long)value
, ext
->kcfg
.sz
);
1599 switch (ext
->kcfg
.sz
) {
1600 case 1: *(__u8
*)ext_val
= value
; break;
1601 case 2: *(__u16
*)ext_val
= value
; break;
1602 case 4: *(__u32
*)ext_val
= value
; break;
1603 case 8: *(__u64
*)ext_val
= value
; break;
1611 static int bpf_object__process_kconfig_line(struct bpf_object
*obj
,
1612 char *buf
, void *data
)
1614 struct extern_desc
*ext
;
1620 if (strncmp(buf
, "CONFIG_", 7))
1623 sep
= strchr(buf
, '=');
1625 pr_warn("failed to parse '%s': no separator\n", buf
);
1629 /* Trim ending '\n' */
1631 if (buf
[len
- 1] == '\n')
1632 buf
[len
- 1] = '\0';
1633 /* Split on '=' and ensure that a value is present. */
1637 pr_warn("failed to parse '%s': no value\n", buf
);
1641 ext
= find_extern_by_name(obj
, buf
);
1642 if (!ext
|| ext
->is_set
)
1645 ext_val
= data
+ ext
->kcfg
.data_off
;
1649 case 'y': case 'n': case 'm':
1650 err
= set_kcfg_value_tri(ext
, ext_val
, *value
);
1653 err
= set_kcfg_value_str(ext
, ext_val
, value
);
1656 /* assume integer */
1657 err
= parse_u64(value
, &num
);
1659 pr_warn("extern (kcfg) %s=%s should be integer\n",
1663 err
= set_kcfg_value_num(ext
, ext_val
, num
);
1668 pr_debug("extern (kcfg) %s=%s\n", ext
->name
, value
);
1672 static int bpf_object__read_kconfig_file(struct bpf_object
*obj
, void *data
)
1680 len
= snprintf(buf
, PATH_MAX
, "/boot/config-%s", uts
.release
);
1683 else if (len
>= PATH_MAX
)
1684 return -ENAMETOOLONG
;
1686 /* gzopen also accepts uncompressed files. */
1687 file
= gzopen(buf
, "r");
1689 file
= gzopen("/proc/config.gz", "r");
1692 pr_warn("failed to open system Kconfig\n");
1696 while (gzgets(file
, buf
, sizeof(buf
))) {
1697 err
= bpf_object__process_kconfig_line(obj
, buf
, data
);
1699 pr_warn("error parsing system Kconfig line '%s': %d\n",
1710 static int bpf_object__read_kconfig_mem(struct bpf_object
*obj
,
1711 const char *config
, void *data
)
1717 file
= fmemopen((void *)config
, strlen(config
), "r");
1720 pr_warn("failed to open in-memory Kconfig: %d\n", err
);
1724 while (fgets(buf
, sizeof(buf
), file
)) {
1725 err
= bpf_object__process_kconfig_line(obj
, buf
, data
);
1727 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
1737 static int bpf_object__init_kconfig_map(struct bpf_object
*obj
)
1739 struct extern_desc
*last_ext
= NULL
, *ext
;
1743 for (i
= 0; i
< obj
->nr_extern
; i
++) {
1744 ext
= &obj
->externs
[i
];
1745 if (ext
->type
== EXT_KCFG
)
1752 map_sz
= last_ext
->kcfg
.data_off
+ last_ext
->kcfg
.sz
;
1753 err
= bpf_object__init_internal_map(obj
, LIBBPF_MAP_KCONFIG
,
1754 obj
->efile
.symbols_shndx
,
1759 obj
->kconfig_map_idx
= obj
->nr_maps
- 1;
1764 static int bpf_object__init_user_maps(struct bpf_object
*obj
, bool strict
)
1766 Elf_Data
*symbols
= obj
->efile
.symbols
;
1767 int i
, map_def_sz
= 0, nr_maps
= 0, nr_syms
;
1768 Elf_Data
*data
= NULL
;
1771 if (obj
->efile
.maps_shndx
< 0)
1778 scn
= elf_sec_by_idx(obj
, obj
->efile
.maps_shndx
);
1779 data
= elf_sec_data(obj
, scn
);
1780 if (!scn
|| !data
) {
1781 pr_warn("elf: failed to get legacy map definitions for %s\n",
1787 * Count number of maps. Each map has a name.
1788 * Array of maps is not supported: only the first element is
1791 * TODO: Detect array of map and report error.
1793 nr_syms
= symbols
->d_size
/ sizeof(GElf_Sym
);
1794 for (i
= 0; i
< nr_syms
; i
++) {
1797 if (!gelf_getsym(symbols
, i
, &sym
))
1799 if (sym
.st_shndx
!= obj
->efile
.maps_shndx
)
1803 /* Assume equally sized map definitions */
1804 pr_debug("elf: found %d legacy map definitions (%zd bytes) in %s\n",
1805 nr_maps
, data
->d_size
, obj
->path
);
1807 if (!data
->d_size
|| nr_maps
== 0 || (data
->d_size
% nr_maps
) != 0) {
1808 pr_warn("elf: unable to determine legacy map definition size in %s\n",
1812 map_def_sz
= data
->d_size
/ nr_maps
;
1814 /* Fill obj->maps using data in "maps" section. */
1815 for (i
= 0; i
< nr_syms
; i
++) {
1817 const char *map_name
;
1818 struct bpf_map_def
*def
;
1819 struct bpf_map
*map
;
1821 if (!gelf_getsym(symbols
, i
, &sym
))
1823 if (sym
.st_shndx
!= obj
->efile
.maps_shndx
)
1826 map
= bpf_object__add_map(obj
);
1828 return PTR_ERR(map
);
1830 map_name
= elf_sym_str(obj
, sym
.st_name
);
1832 pr_warn("failed to get map #%d name sym string for obj %s\n",
1834 return -LIBBPF_ERRNO__FORMAT
;
1837 map
->libbpf_type
= LIBBPF_MAP_UNSPEC
;
1838 map
->sec_idx
= sym
.st_shndx
;
1839 map
->sec_offset
= sym
.st_value
;
1840 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
1841 map_name
, map
->sec_idx
, map
->sec_offset
);
1842 if (sym
.st_value
+ map_def_sz
> data
->d_size
) {
1843 pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
1844 obj
->path
, map_name
);
1848 map
->name
= strdup(map_name
);
1850 pr_warn("failed to alloc map name\n");
1853 pr_debug("map %d is \"%s\"\n", i
, map
->name
);
1854 def
= (struct bpf_map_def
*)(data
->d_buf
+ sym
.st_value
);
1856 * If the definition of the map in the object file fits in
1857 * bpf_map_def, copy it. Any extra fields in our version
1858 * of bpf_map_def will default to zero as a result of the
1861 if (map_def_sz
<= sizeof(struct bpf_map_def
)) {
1862 memcpy(&map
->def
, def
, map_def_sz
);
1865 * Here the map structure being read is bigger than what
1866 * we expect, truncate if the excess bits are all zero.
1867 * If they are not zero, reject this map as
1872 for (b
= ((char *)def
) + sizeof(struct bpf_map_def
);
1873 b
< ((char *)def
) + map_def_sz
; b
++) {
1875 pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n",
1876 obj
->path
, map_name
);
1881 memcpy(&map
->def
, def
, sizeof(struct bpf_map_def
));
1887 static const struct btf_type
*
1888 skip_mods_and_typedefs(const struct btf
*btf
, __u32 id
, __u32
*res_id
)
1890 const struct btf_type
*t
= btf__type_by_id(btf
, id
);
1895 while (btf_is_mod(t
) || btf_is_typedef(t
)) {
1898 t
= btf__type_by_id(btf
, t
->type
);
1904 static const struct btf_type
*
1905 resolve_func_ptr(const struct btf
*btf
, __u32 id
, __u32
*res_id
)
1907 const struct btf_type
*t
;
1909 t
= skip_mods_and_typedefs(btf
, id
, NULL
);
1913 t
= skip_mods_and_typedefs(btf
, t
->type
, res_id
);
1915 return btf_is_func_proto(t
) ? t
: NULL
;
1918 static const char *btf_kind_str(const struct btf_type
*t
)
1920 switch (btf_kind(t
)) {
1921 case BTF_KIND_UNKN
: return "void";
1922 case BTF_KIND_INT
: return "int";
1923 case BTF_KIND_PTR
: return "ptr";
1924 case BTF_KIND_ARRAY
: return "array";
1925 case BTF_KIND_STRUCT
: return "struct";
1926 case BTF_KIND_UNION
: return "union";
1927 case BTF_KIND_ENUM
: return "enum";
1928 case BTF_KIND_FWD
: return "fwd";
1929 case BTF_KIND_TYPEDEF
: return "typedef";
1930 case BTF_KIND_VOLATILE
: return "volatile";
1931 case BTF_KIND_CONST
: return "const";
1932 case BTF_KIND_RESTRICT
: return "restrict";
1933 case BTF_KIND_FUNC
: return "func";
1934 case BTF_KIND_FUNC_PROTO
: return "func_proto";
1935 case BTF_KIND_VAR
: return "var";
1936 case BTF_KIND_DATASEC
: return "datasec";
1937 default: return "unknown";
1942 * Fetch integer attribute of BTF map definition. Such attributes are
1943 * represented using a pointer to an array, in which dimensionality of array
1944 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1945 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1946 * type definition, while using only sizeof(void *) space in ELF data section.
1948 static bool get_map_field_int(const char *map_name
, const struct btf
*btf
,
1949 const struct btf_member
*m
, __u32
*res
)
1951 const struct btf_type
*t
= skip_mods_and_typedefs(btf
, m
->type
, NULL
);
1952 const char *name
= btf__name_by_offset(btf
, m
->name_off
);
1953 const struct btf_array
*arr_info
;
1954 const struct btf_type
*arr_t
;
1956 if (!btf_is_ptr(t
)) {
1957 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
1958 map_name
, name
, btf_kind_str(t
));
1962 arr_t
= btf__type_by_id(btf
, t
->type
);
1964 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
1965 map_name
, name
, t
->type
);
1968 if (!btf_is_array(arr_t
)) {
1969 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
1970 map_name
, name
, btf_kind_str(arr_t
));
1973 arr_info
= btf_array(arr_t
);
1974 *res
= arr_info
->nelems
;
1978 static int build_map_pin_path(struct bpf_map
*map
, const char *path
)
1984 path
= "/sys/fs/bpf";
1986 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
, bpf_map__name(map
));
1989 else if (len
>= PATH_MAX
)
1990 return -ENAMETOOLONG
;
1992 return bpf_map__set_pin_path(map
, buf
);
1996 static int parse_btf_map_def(struct bpf_object
*obj
,
1997 struct bpf_map
*map
,
1998 const struct btf_type
*def
,
1999 bool strict
, bool is_inner
,
2000 const char *pin_root_path
)
2002 const struct btf_type
*t
;
2003 const struct btf_member
*m
;
2006 vlen
= btf_vlen(def
);
2007 m
= btf_members(def
);
2008 for (i
= 0; i
< vlen
; i
++, m
++) {
2009 const char *name
= btf__name_by_offset(obj
->btf
, m
->name_off
);
2012 pr_warn("map '%s': invalid field #%d.\n", map
->name
, i
);
2015 if (strcmp(name
, "type") == 0) {
2016 if (!get_map_field_int(map
->name
, obj
->btf
, m
,
2019 pr_debug("map '%s': found type = %u.\n",
2020 map
->name
, map
->def
.type
);
2021 } else if (strcmp(name
, "max_entries") == 0) {
2022 if (!get_map_field_int(map
->name
, obj
->btf
, m
,
2023 &map
->def
.max_entries
))
2025 pr_debug("map '%s': found max_entries = %u.\n",
2026 map
->name
, map
->def
.max_entries
);
2027 } else if (strcmp(name
, "map_flags") == 0) {
2028 if (!get_map_field_int(map
->name
, obj
->btf
, m
,
2029 &map
->def
.map_flags
))
2031 pr_debug("map '%s': found map_flags = %u.\n",
2032 map
->name
, map
->def
.map_flags
);
2033 } else if (strcmp(name
, "numa_node") == 0) {
2034 if (!get_map_field_int(map
->name
, obj
->btf
, m
, &map
->numa_node
))
2036 pr_debug("map '%s': found numa_node = %u.\n", map
->name
, map
->numa_node
);
2037 } else if (strcmp(name
, "key_size") == 0) {
2040 if (!get_map_field_int(map
->name
, obj
->btf
, m
, &sz
))
2042 pr_debug("map '%s': found key_size = %u.\n",
2044 if (map
->def
.key_size
&& map
->def
.key_size
!= sz
) {
2045 pr_warn("map '%s': conflicting key size %u != %u.\n",
2046 map
->name
, map
->def
.key_size
, sz
);
2049 map
->def
.key_size
= sz
;
2050 } else if (strcmp(name
, "key") == 0) {
2053 t
= btf__type_by_id(obj
->btf
, m
->type
);
2055 pr_warn("map '%s': key type [%d] not found.\n",
2056 map
->name
, m
->type
);
2059 if (!btf_is_ptr(t
)) {
2060 pr_warn("map '%s': key spec is not PTR: %s.\n",
2061 map
->name
, btf_kind_str(t
));
2064 sz
= btf__resolve_size(obj
->btf
, t
->type
);
2066 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2067 map
->name
, t
->type
, (ssize_t
)sz
);
2070 pr_debug("map '%s': found key [%u], sz = %zd.\n",
2071 map
->name
, t
->type
, (ssize_t
)sz
);
2072 if (map
->def
.key_size
&& map
->def
.key_size
!= sz
) {
2073 pr_warn("map '%s': conflicting key size %u != %zd.\n",
2074 map
->name
, map
->def
.key_size
, (ssize_t
)sz
);
2077 map
->def
.key_size
= sz
;
2078 map
->btf_key_type_id
= t
->type
;
2079 } else if (strcmp(name
, "value_size") == 0) {
2082 if (!get_map_field_int(map
->name
, obj
->btf
, m
, &sz
))
2084 pr_debug("map '%s': found value_size = %u.\n",
2086 if (map
->def
.value_size
&& map
->def
.value_size
!= sz
) {
2087 pr_warn("map '%s': conflicting value size %u != %u.\n",
2088 map
->name
, map
->def
.value_size
, sz
);
2091 map
->def
.value_size
= sz
;
2092 } else if (strcmp(name
, "value") == 0) {
2095 t
= btf__type_by_id(obj
->btf
, m
->type
);
2097 pr_warn("map '%s': value type [%d] not found.\n",
2098 map
->name
, m
->type
);
2101 if (!btf_is_ptr(t
)) {
2102 pr_warn("map '%s': value spec is not PTR: %s.\n",
2103 map
->name
, btf_kind_str(t
));
2106 sz
= btf__resolve_size(obj
->btf
, t
->type
);
2108 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2109 map
->name
, t
->type
, (ssize_t
)sz
);
2112 pr_debug("map '%s': found value [%u], sz = %zd.\n",
2113 map
->name
, t
->type
, (ssize_t
)sz
);
2114 if (map
->def
.value_size
&& map
->def
.value_size
!= sz
) {
2115 pr_warn("map '%s': conflicting value size %u != %zd.\n",
2116 map
->name
, map
->def
.value_size
, (ssize_t
)sz
);
2119 map
->def
.value_size
= sz
;
2120 map
->btf_value_type_id
= t
->type
;
2122 else if (strcmp(name
, "values") == 0) {
2126 pr_warn("map '%s': multi-level inner maps not supported.\n",
2130 if (i
!= vlen
- 1) {
2131 pr_warn("map '%s': '%s' member should be last.\n",
2135 if (!bpf_map_type__is_map_in_map(map
->def
.type
)) {
2136 pr_warn("map '%s': should be map-in-map.\n",
2140 if (map
->def
.value_size
&& map
->def
.value_size
!= 4) {
2141 pr_warn("map '%s': conflicting value size %u != 4.\n",
2142 map
->name
, map
->def
.value_size
);
2145 map
->def
.value_size
= 4;
2146 t
= btf__type_by_id(obj
->btf
, m
->type
);
2148 pr_warn("map '%s': map-in-map inner type [%d] not found.\n",
2149 map
->name
, m
->type
);
2152 if (!btf_is_array(t
) || btf_array(t
)->nelems
) {
2153 pr_warn("map '%s': map-in-map inner spec is not a zero-sized array.\n",
2157 t
= skip_mods_and_typedefs(obj
->btf
, btf_array(t
)->type
,
2159 if (!btf_is_ptr(t
)) {
2160 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2161 map
->name
, btf_kind_str(t
));
2164 t
= skip_mods_and_typedefs(obj
->btf
, t
->type
, NULL
);
2165 if (!btf_is_struct(t
)) {
2166 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2167 map
->name
, btf_kind_str(t
));
2171 map
->inner_map
= calloc(1, sizeof(*map
->inner_map
));
2172 if (!map
->inner_map
)
2174 map
->inner_map
->sec_idx
= obj
->efile
.btf_maps_shndx
;
2175 map
->inner_map
->name
= malloc(strlen(map
->name
) +
2176 sizeof(".inner") + 1);
2177 if (!map
->inner_map
->name
)
2179 sprintf(map
->inner_map
->name
, "%s.inner", map
->name
);
2181 err
= parse_btf_map_def(obj
, map
->inner_map
, t
, strict
,
2182 true /* is_inner */, NULL
);
2185 } else if (strcmp(name
, "pinning") == 0) {
2190 pr_debug("map '%s': inner def can't be pinned.\n",
2194 if (!get_map_field_int(map
->name
, obj
->btf
, m
, &val
))
2196 pr_debug("map '%s': found pinning = %u.\n",
2199 if (val
!= LIBBPF_PIN_NONE
&&
2200 val
!= LIBBPF_PIN_BY_NAME
) {
2201 pr_warn("map '%s': invalid pinning value %u.\n",
2205 if (val
== LIBBPF_PIN_BY_NAME
) {
2206 err
= build_map_pin_path(map
, pin_root_path
);
2208 pr_warn("map '%s': couldn't build pin path.\n",
2215 pr_warn("map '%s': unknown field '%s'.\n",
2219 pr_debug("map '%s': ignoring unknown field '%s'.\n",
2224 if (map
->def
.type
== BPF_MAP_TYPE_UNSPEC
) {
2225 pr_warn("map '%s': map type isn't specified.\n", map
->name
);
2232 static int bpf_object__init_user_btf_map(struct bpf_object
*obj
,
2233 const struct btf_type
*sec
,
2234 int var_idx
, int sec_idx
,
2235 const Elf_Data
*data
, bool strict
,
2236 const char *pin_root_path
)
2238 const struct btf_type
*var
, *def
;
2239 const struct btf_var_secinfo
*vi
;
2240 const struct btf_var
*var_extra
;
2241 const char *map_name
;
2242 struct bpf_map
*map
;
2244 vi
= btf_var_secinfos(sec
) + var_idx
;
2245 var
= btf__type_by_id(obj
->btf
, vi
->type
);
2246 var_extra
= btf_var(var
);
2247 map_name
= btf__name_by_offset(obj
->btf
, var
->name_off
);
2249 if (map_name
== NULL
|| map_name
[0] == '\0') {
2250 pr_warn("map #%d: empty name.\n", var_idx
);
2253 if ((__u64
)vi
->offset
+ vi
->size
> data
->d_size
) {
2254 pr_warn("map '%s' BTF data is corrupted.\n", map_name
);
2257 if (!btf_is_var(var
)) {
2258 pr_warn("map '%s': unexpected var kind %s.\n",
2259 map_name
, btf_kind_str(var
));
2262 if (var_extra
->linkage
!= BTF_VAR_GLOBAL_ALLOCATED
&&
2263 var_extra
->linkage
!= BTF_VAR_STATIC
) {
2264 pr_warn("map '%s': unsupported var linkage %u.\n",
2265 map_name
, var_extra
->linkage
);
2269 def
= skip_mods_and_typedefs(obj
->btf
, var
->type
, NULL
);
2270 if (!btf_is_struct(def
)) {
2271 pr_warn("map '%s': unexpected def kind %s.\n",
2272 map_name
, btf_kind_str(var
));
2275 if (def
->size
> vi
->size
) {
2276 pr_warn("map '%s': invalid def size.\n", map_name
);
2280 map
= bpf_object__add_map(obj
);
2282 return PTR_ERR(map
);
2283 map
->name
= strdup(map_name
);
2285 pr_warn("map '%s': failed to alloc map name.\n", map_name
);
2288 map
->libbpf_type
= LIBBPF_MAP_UNSPEC
;
2289 map
->def
.type
= BPF_MAP_TYPE_UNSPEC
;
2290 map
->sec_idx
= sec_idx
;
2291 map
->sec_offset
= vi
->offset
;
2292 map
->btf_var_idx
= var_idx
;
2293 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2294 map_name
, map
->sec_idx
, map
->sec_offset
);
2296 return parse_btf_map_def(obj
, map
, def
, strict
, false, pin_root_path
);
2299 static int bpf_object__init_user_btf_maps(struct bpf_object
*obj
, bool strict
,
2300 const char *pin_root_path
)
2302 const struct btf_type
*sec
= NULL
;
2303 int nr_types
, i
, vlen
, err
;
2304 const struct btf_type
*t
;
2309 if (obj
->efile
.btf_maps_shndx
< 0)
2312 scn
= elf_sec_by_idx(obj
, obj
->efile
.btf_maps_shndx
);
2313 data
= elf_sec_data(obj
, scn
);
2314 if (!scn
|| !data
) {
2315 pr_warn("elf: failed to get %s map definitions for %s\n",
2316 MAPS_ELF_SEC
, obj
->path
);
2320 nr_types
= btf__get_nr_types(obj
->btf
);
2321 for (i
= 1; i
<= nr_types
; i
++) {
2322 t
= btf__type_by_id(obj
->btf
, i
);
2323 if (!btf_is_datasec(t
))
2325 name
= btf__name_by_offset(obj
->btf
, t
->name_off
);
2326 if (strcmp(name
, MAPS_ELF_SEC
) == 0) {
2328 obj
->efile
.btf_maps_sec_btf_id
= i
;
2334 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC
);
2338 vlen
= btf_vlen(sec
);
2339 for (i
= 0; i
< vlen
; i
++) {
2340 err
= bpf_object__init_user_btf_map(obj
, sec
, i
,
2341 obj
->efile
.btf_maps_shndx
,
2351 static int bpf_object__init_maps(struct bpf_object
*obj
,
2352 const struct bpf_object_open_opts
*opts
)
2354 const char *pin_root_path
;
2358 strict
= !OPTS_GET(opts
, relaxed_maps
, false);
2359 pin_root_path
= OPTS_GET(opts
, pin_root_path
, NULL
);
2361 err
= bpf_object__init_user_maps(obj
, strict
);
2362 err
= err
?: bpf_object__init_user_btf_maps(obj
, strict
, pin_root_path
);
2363 err
= err
?: bpf_object__init_global_data_maps(obj
);
2364 err
= err
?: bpf_object__init_kconfig_map(obj
);
2365 err
= err
?: bpf_object__init_struct_ops_maps(obj
);
2372 static bool section_have_execinstr(struct bpf_object
*obj
, int idx
)
2376 if (elf_sec_hdr(obj
, elf_sec_by_idx(obj
, idx
), &sh
))
2379 return sh
.sh_flags
& SHF_EXECINSTR
;
2382 static bool btf_needs_sanitization(struct bpf_object
*obj
)
2384 bool has_func_global
= kernel_supports(FEAT_BTF_GLOBAL_FUNC
);
2385 bool has_datasec
= kernel_supports(FEAT_BTF_DATASEC
);
2386 bool has_func
= kernel_supports(FEAT_BTF_FUNC
);
2388 return !has_func
|| !has_datasec
|| !has_func_global
;
2391 static void bpf_object__sanitize_btf(struct bpf_object
*obj
, struct btf
*btf
)
2393 bool has_func_global
= kernel_supports(FEAT_BTF_GLOBAL_FUNC
);
2394 bool has_datasec
= kernel_supports(FEAT_BTF_DATASEC
);
2395 bool has_func
= kernel_supports(FEAT_BTF_FUNC
);
2399 for (i
= 1; i
<= btf__get_nr_types(btf
); i
++) {
2400 t
= (struct btf_type
*)btf__type_by_id(btf
, i
);
2402 if (!has_datasec
&& btf_is_var(t
)) {
2403 /* replace VAR with INT */
2404 t
->info
= BTF_INFO_ENC(BTF_KIND_INT
, 0, 0);
2406 * using size = 1 is the safest choice, 4 will be too
2407 * big and cause kernel BTF validation failure if
2408 * original variable took less than 4 bytes
2411 *(int *)(t
+ 1) = BTF_INT_ENC(0, 0, 8);
2412 } else if (!has_datasec
&& btf_is_datasec(t
)) {
2413 /* replace DATASEC with STRUCT */
2414 const struct btf_var_secinfo
*v
= btf_var_secinfos(t
);
2415 struct btf_member
*m
= btf_members(t
);
2416 struct btf_type
*vt
;
2419 name
= (char *)btf__name_by_offset(btf
, t
->name_off
);
2427 t
->info
= BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, vlen
);
2428 for (j
= 0; j
< vlen
; j
++, v
++, m
++) {
2429 /* order of field assignments is important */
2430 m
->offset
= v
->offset
* 8;
2432 /* preserve variable name as member name */
2433 vt
= (void *)btf__type_by_id(btf
, v
->type
);
2434 m
->name_off
= vt
->name_off
;
2436 } else if (!has_func
&& btf_is_func_proto(t
)) {
2437 /* replace FUNC_PROTO with ENUM */
2439 t
->info
= BTF_INFO_ENC(BTF_KIND_ENUM
, 0, vlen
);
2440 t
->size
= sizeof(__u32
); /* kernel enforced */
2441 } else if (!has_func
&& btf_is_func(t
)) {
2442 /* replace FUNC with TYPEDEF */
2443 t
->info
= BTF_INFO_ENC(BTF_KIND_TYPEDEF
, 0, 0);
2444 } else if (!has_func_global
&& btf_is_func(t
)) {
2445 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2446 t
->info
= BTF_INFO_ENC(BTF_KIND_FUNC
, 0, 0);
2451 static bool libbpf_needs_btf(const struct bpf_object
*obj
)
2453 return obj
->efile
.btf_maps_shndx
>= 0 ||
2454 obj
->efile
.st_ops_shndx
>= 0 ||
2458 static bool kernel_needs_btf(const struct bpf_object
*obj
)
2460 return obj
->efile
.st_ops_shndx
>= 0;
2463 static int bpf_object__init_btf(struct bpf_object
*obj
,
2465 Elf_Data
*btf_ext_data
)
2470 obj
->btf
= btf__new(btf_data
->d_buf
, btf_data
->d_size
);
2471 if (IS_ERR(obj
->btf
)) {
2472 err
= PTR_ERR(obj
->btf
);
2474 pr_warn("Error loading ELF section %s: %d.\n",
2478 /* enforce 8-byte pointers for BPF-targeted BTFs */
2479 btf__set_pointer_size(obj
->btf
, 8);
2484 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2485 BTF_EXT_ELF_SEC
, BTF_ELF_SEC
);
2488 obj
->btf_ext
= btf_ext__new(btf_ext_data
->d_buf
,
2489 btf_ext_data
->d_size
);
2490 if (IS_ERR(obj
->btf_ext
)) {
2491 pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n",
2492 BTF_EXT_ELF_SEC
, PTR_ERR(obj
->btf_ext
));
2493 obj
->btf_ext
= NULL
;
2498 if (err
&& libbpf_needs_btf(obj
)) {
2499 pr_warn("BTF is required, but is missing or corrupted.\n");
2505 static int bpf_object__finalize_btf(struct bpf_object
*obj
)
2512 err
= btf__finalize_data(obj
, obj
->btf
);
2514 pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC
, err
);
2521 static bool prog_needs_vmlinux_btf(struct bpf_program
*prog
)
2523 if (prog
->type
== BPF_PROG_TYPE_STRUCT_OPS
||
2524 prog
->type
== BPF_PROG_TYPE_LSM
)
2527 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
2528 * also need vmlinux BTF
2530 if (prog
->type
== BPF_PROG_TYPE_TRACING
&& !prog
->attach_prog_fd
)
2536 static bool obj_needs_vmlinux_btf(const struct bpf_object
*obj
)
2538 struct bpf_program
*prog
;
2541 /* CO-RE relocations need kernel BTF */
2542 if (obj
->btf_ext
&& obj
->btf_ext
->core_relo_info
.len
)
2545 /* Support for typed ksyms needs kernel BTF */
2546 for (i
= 0; i
< obj
->nr_extern
; i
++) {
2547 const struct extern_desc
*ext
;
2549 ext
= &obj
->externs
[i
];
2550 if (ext
->type
== EXT_KSYM
&& ext
->ksym
.type_id
)
2554 bpf_object__for_each_program(prog
, obj
) {
2557 if (prog_needs_vmlinux_btf(prog
))
2564 static int bpf_object__load_vmlinux_btf(struct bpf_object
*obj
, bool force
)
2568 /* btf_vmlinux could be loaded earlier */
2569 if (obj
->btf_vmlinux
)
2572 if (!force
&& !obj_needs_vmlinux_btf(obj
))
2575 obj
->btf_vmlinux
= libbpf_find_kernel_btf();
2576 if (IS_ERR(obj
->btf_vmlinux
)) {
2577 err
= PTR_ERR(obj
->btf_vmlinux
);
2578 pr_warn("Error loading vmlinux BTF: %d\n", err
);
2579 obj
->btf_vmlinux
= NULL
;
2585 static int bpf_object__sanitize_and_load_btf(struct bpf_object
*obj
)
2587 struct btf
*kern_btf
= obj
->btf
;
2588 bool btf_mandatory
, sanitize
;
2594 if (!kernel_supports(FEAT_BTF
)) {
2595 if (kernel_needs_btf(obj
)) {
2599 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
2603 sanitize
= btf_needs_sanitization(obj
);
2605 const void *raw_data
;
2608 /* clone BTF to sanitize a copy and leave the original intact */
2609 raw_data
= btf__get_raw_data(obj
->btf
, &sz
);
2610 kern_btf
= btf__new(raw_data
, sz
);
2611 if (IS_ERR(kern_btf
))
2612 return PTR_ERR(kern_btf
);
2614 /* enforce 8-byte pointers for BPF-targeted BTFs */
2615 btf__set_pointer_size(obj
->btf
, 8);
2616 bpf_object__sanitize_btf(obj
, kern_btf
);
2619 err
= btf__load(kern_btf
);
2622 /* move fd to libbpf's BTF */
2623 btf__set_fd(obj
->btf
, btf__fd(kern_btf
));
2624 btf__set_fd(kern_btf
, -1);
2626 btf__free(kern_btf
);
2630 btf_mandatory
= kernel_needs_btf(obj
);
2631 pr_warn("Error loading .BTF into kernel: %d. %s\n", err
,
2632 btf_mandatory
? "BTF is mandatory, can't proceed."
2633 : "BTF is optional, ignoring.");
2640 static const char *elf_sym_str(const struct bpf_object
*obj
, size_t off
)
2644 name
= elf_strptr(obj
->efile
.elf
, obj
->efile
.strtabidx
, off
);
2646 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
2647 off
, obj
->path
, elf_errmsg(-1));
2654 static const char *elf_sec_str(const struct bpf_object
*obj
, size_t off
)
2658 name
= elf_strptr(obj
->efile
.elf
, obj
->efile
.shstrndx
, off
);
2660 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
2661 off
, obj
->path
, elf_errmsg(-1));
2668 static Elf_Scn
*elf_sec_by_idx(const struct bpf_object
*obj
, size_t idx
)
2672 scn
= elf_getscn(obj
->efile
.elf
, idx
);
2674 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
2675 idx
, obj
->path
, elf_errmsg(-1));
2681 static Elf_Scn
*elf_sec_by_name(const struct bpf_object
*obj
, const char *name
)
2683 Elf_Scn
*scn
= NULL
;
2684 Elf
*elf
= obj
->efile
.elf
;
2685 const char *sec_name
;
2687 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
2688 sec_name
= elf_sec_name(obj
, scn
);
2692 if (strcmp(sec_name
, name
) != 0)
2700 static int elf_sec_hdr(const struct bpf_object
*obj
, Elf_Scn
*scn
, GElf_Shdr
*hdr
)
2705 if (gelf_getshdr(scn
, hdr
) != hdr
) {
2706 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
2707 elf_ndxscn(scn
), obj
->path
, elf_errmsg(-1));
2714 static const char *elf_sec_name(const struct bpf_object
*obj
, Elf_Scn
*scn
)
2722 if (elf_sec_hdr(obj
, scn
, &sh
))
2725 name
= elf_sec_str(obj
, sh
.sh_name
);
2727 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
2728 elf_ndxscn(scn
), obj
->path
, elf_errmsg(-1));
2735 static Elf_Data
*elf_sec_data(const struct bpf_object
*obj
, Elf_Scn
*scn
)
2742 data
= elf_getdata(scn
, 0);
2744 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
2745 elf_ndxscn(scn
), elf_sec_name(obj
, scn
) ?: "<?>",
2746 obj
->path
, elf_errmsg(-1));
2753 static int elf_sym_by_sec_off(const struct bpf_object
*obj
, size_t sec_idx
,
2754 size_t off
, __u32 sym_type
, GElf_Sym
*sym
)
2756 Elf_Data
*symbols
= obj
->efile
.symbols
;
2757 size_t n
= symbols
->d_size
/ sizeof(GElf_Sym
);
2760 for (i
= 0; i
< n
; i
++) {
2761 if (!gelf_getsym(symbols
, i
, sym
))
2763 if (sym
->st_shndx
!= sec_idx
|| sym
->st_value
!= off
)
2765 if (GELF_ST_TYPE(sym
->st_info
) != sym_type
)
2773 static bool is_sec_name_dwarf(const char *name
)
2775 /* approximation, but the actual list is too long */
2776 return strncmp(name
, ".debug_", sizeof(".debug_") - 1) == 0;
2779 static bool ignore_elf_section(GElf_Shdr
*hdr
, const char *name
)
2781 /* no special handling of .strtab */
2782 if (hdr
->sh_type
== SHT_STRTAB
)
2785 /* ignore .llvm_addrsig section as well */
2786 if (hdr
->sh_type
== 0x6FFF4C03 /* SHT_LLVM_ADDRSIG */)
2789 /* no subprograms will lead to an empty .text section, ignore it */
2790 if (hdr
->sh_type
== SHT_PROGBITS
&& hdr
->sh_size
== 0 &&
2791 strcmp(name
, ".text") == 0)
2794 /* DWARF sections */
2795 if (is_sec_name_dwarf(name
))
2798 if (strncmp(name
, ".rel", sizeof(".rel") - 1) == 0) {
2799 name
+= sizeof(".rel") - 1;
2800 /* DWARF section relocations */
2801 if (is_sec_name_dwarf(name
))
2804 /* .BTF and .BTF.ext don't need relocations */
2805 if (strcmp(name
, BTF_ELF_SEC
) == 0 ||
2806 strcmp(name
, BTF_EXT_ELF_SEC
) == 0)
2813 static int cmp_progs(const void *_a
, const void *_b
)
2815 const struct bpf_program
*a
= _a
;
2816 const struct bpf_program
*b
= _b
;
2818 if (a
->sec_idx
!= b
->sec_idx
)
2819 return a
->sec_idx
< b
->sec_idx
? -1 : 1;
2821 /* sec_insn_off can't be the same within the section */
2822 return a
->sec_insn_off
< b
->sec_insn_off
? -1 : 1;
2825 static int bpf_object__elf_collect(struct bpf_object
*obj
)
2827 Elf
*elf
= obj
->efile
.elf
;
2828 Elf_Data
*btf_ext_data
= NULL
;
2829 Elf_Data
*btf_data
= NULL
;
2830 int idx
= 0, err
= 0;
2836 /* a bunch of ELF parsing functionality depends on processing symbols,
2837 * so do the first pass and find the symbol table
2840 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
2841 if (elf_sec_hdr(obj
, scn
, &sh
))
2842 return -LIBBPF_ERRNO__FORMAT
;
2844 if (sh
.sh_type
== SHT_SYMTAB
) {
2845 if (obj
->efile
.symbols
) {
2846 pr_warn("elf: multiple symbol tables in %s\n", obj
->path
);
2847 return -LIBBPF_ERRNO__FORMAT
;
2850 data
= elf_sec_data(obj
, scn
);
2852 return -LIBBPF_ERRNO__FORMAT
;
2854 obj
->efile
.symbols
= data
;
2855 obj
->efile
.symbols_shndx
= elf_ndxscn(scn
);
2856 obj
->efile
.strtabidx
= sh
.sh_link
;
2861 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
2864 if (elf_sec_hdr(obj
, scn
, &sh
))
2865 return -LIBBPF_ERRNO__FORMAT
;
2867 name
= elf_sec_str(obj
, sh
.sh_name
);
2869 return -LIBBPF_ERRNO__FORMAT
;
2871 if (ignore_elf_section(&sh
, name
))
2874 data
= elf_sec_data(obj
, scn
);
2876 return -LIBBPF_ERRNO__FORMAT
;
2878 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
2879 idx
, name
, (unsigned long)data
->d_size
,
2880 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
2883 if (strcmp(name
, "license") == 0) {
2884 err
= bpf_object__init_license(obj
, data
->d_buf
, data
->d_size
);
2887 } else if (strcmp(name
, "version") == 0) {
2888 err
= bpf_object__init_kversion(obj
, data
->d_buf
, data
->d_size
);
2891 } else if (strcmp(name
, "maps") == 0) {
2892 obj
->efile
.maps_shndx
= idx
;
2893 } else if (strcmp(name
, MAPS_ELF_SEC
) == 0) {
2894 obj
->efile
.btf_maps_shndx
= idx
;
2895 } else if (strcmp(name
, BTF_ELF_SEC
) == 0) {
2897 } else if (strcmp(name
, BTF_EXT_ELF_SEC
) == 0) {
2898 btf_ext_data
= data
;
2899 } else if (sh
.sh_type
== SHT_SYMTAB
) {
2900 /* already processed during the first pass above */
2901 } else if (sh
.sh_type
== SHT_PROGBITS
&& data
->d_size
> 0) {
2902 if (sh
.sh_flags
& SHF_EXECINSTR
) {
2903 if (strcmp(name
, ".text") == 0)
2904 obj
->efile
.text_shndx
= idx
;
2905 err
= bpf_object__add_programs(obj
, data
, name
, idx
);
2908 } else if (strcmp(name
, DATA_SEC
) == 0) {
2909 obj
->efile
.data
= data
;
2910 obj
->efile
.data_shndx
= idx
;
2911 } else if (strcmp(name
, RODATA_SEC
) == 0) {
2912 obj
->efile
.rodata
= data
;
2913 obj
->efile
.rodata_shndx
= idx
;
2914 } else if (strcmp(name
, STRUCT_OPS_SEC
) == 0) {
2915 obj
->efile
.st_ops_data
= data
;
2916 obj
->efile
.st_ops_shndx
= idx
;
2918 pr_info("elf: skipping unrecognized data section(%d) %s\n",
2921 } else if (sh
.sh_type
== SHT_REL
) {
2922 int nr_sects
= obj
->efile
.nr_reloc_sects
;
2923 void *sects
= obj
->efile
.reloc_sects
;
2924 int sec
= sh
.sh_info
; /* points to other section */
2926 /* Only do relo for section with exec instructions */
2927 if (!section_have_execinstr(obj
, sec
) &&
2928 strcmp(name
, ".rel" STRUCT_OPS_SEC
) &&
2929 strcmp(name
, ".rel" MAPS_ELF_SEC
)) {
2930 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
2932 elf_sec_name(obj
, elf_sec_by_idx(obj
, sec
)) ?: "<?>");
2936 sects
= libbpf_reallocarray(sects
, nr_sects
+ 1,
2937 sizeof(*obj
->efile
.reloc_sects
));
2941 obj
->efile
.reloc_sects
= sects
;
2942 obj
->efile
.nr_reloc_sects
++;
2944 obj
->efile
.reloc_sects
[nr_sects
].shdr
= sh
;
2945 obj
->efile
.reloc_sects
[nr_sects
].data
= data
;
2946 } else if (sh
.sh_type
== SHT_NOBITS
&& strcmp(name
, BSS_SEC
) == 0) {
2947 obj
->efile
.bss
= data
;
2948 obj
->efile
.bss_shndx
= idx
;
2950 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx
, name
,
2951 (size_t)sh
.sh_size
);
2955 if (!obj
->efile
.strtabidx
|| obj
->efile
.strtabidx
> idx
) {
2956 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj
->path
);
2957 return -LIBBPF_ERRNO__FORMAT
;
2960 /* sort BPF programs by section name and in-section instruction offset
2961 * for faster search */
2962 qsort(obj
->programs
, obj
->nr_programs
, sizeof(*obj
->programs
), cmp_progs
);
2964 return bpf_object__init_btf(obj
, btf_data
, btf_ext_data
);
2967 static bool sym_is_extern(const GElf_Sym
*sym
)
2969 int bind
= GELF_ST_BIND(sym
->st_info
);
2970 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
2971 return sym
->st_shndx
== SHN_UNDEF
&&
2972 (bind
== STB_GLOBAL
|| bind
== STB_WEAK
) &&
2973 GELF_ST_TYPE(sym
->st_info
) == STT_NOTYPE
;
2976 static int find_extern_btf_id(const struct btf
*btf
, const char *ext_name
)
2978 const struct btf_type
*t
;
2979 const char *var_name
;
2985 n
= btf__get_nr_types(btf
);
2986 for (i
= 1; i
<= n
; i
++) {
2987 t
= btf__type_by_id(btf
, i
);
2992 var_name
= btf__name_by_offset(btf
, t
->name_off
);
2993 if (strcmp(var_name
, ext_name
))
2996 if (btf_var(t
)->linkage
!= BTF_VAR_GLOBAL_EXTERN
)
3005 static int find_extern_sec_btf_id(struct btf
*btf
, int ext_btf_id
) {
3006 const struct btf_var_secinfo
*vs
;
3007 const struct btf_type
*t
;
3013 n
= btf__get_nr_types(btf
);
3014 for (i
= 1; i
<= n
; i
++) {
3015 t
= btf__type_by_id(btf
, i
);
3017 if (!btf_is_datasec(t
))
3020 vs
= btf_var_secinfos(t
);
3021 for (j
= 0; j
< btf_vlen(t
); j
++, vs
++) {
3022 if (vs
->type
== ext_btf_id
)
3030 static enum kcfg_type
find_kcfg_type(const struct btf
*btf
, int id
,
3033 const struct btf_type
*t
;
3036 t
= skip_mods_and_typedefs(btf
, id
, NULL
);
3037 name
= btf__name_by_offset(btf
, t
->name_off
);
3041 switch (btf_kind(t
)) {
3042 case BTF_KIND_INT
: {
3043 int enc
= btf_int_encoding(t
);
3045 if (enc
& BTF_INT_BOOL
)
3046 return t
->size
== 1 ? KCFG_BOOL
: KCFG_UNKNOWN
;
3048 *is_signed
= enc
& BTF_INT_SIGNED
;
3051 if (t
->size
< 1 || t
->size
> 8 || (t
->size
& (t
->size
- 1)))
3052 return KCFG_UNKNOWN
;
3057 return KCFG_UNKNOWN
;
3058 if (strcmp(name
, "libbpf_tristate"))
3059 return KCFG_UNKNOWN
;
3060 return KCFG_TRISTATE
;
3061 case BTF_KIND_ARRAY
:
3062 if (btf_array(t
)->nelems
== 0)
3063 return KCFG_UNKNOWN
;
3064 if (find_kcfg_type(btf
, btf_array(t
)->type
, NULL
) != KCFG_CHAR
)
3065 return KCFG_UNKNOWN
;
3066 return KCFG_CHAR_ARR
;
3068 return KCFG_UNKNOWN
;
3072 static int cmp_externs(const void *_a
, const void *_b
)
3074 const struct extern_desc
*a
= _a
;
3075 const struct extern_desc
*b
= _b
;
3077 if (a
->type
!= b
->type
)
3078 return a
->type
< b
->type
? -1 : 1;
3080 if (a
->type
== EXT_KCFG
) {
3081 /* descending order by alignment requirements */
3082 if (a
->kcfg
.align
!= b
->kcfg
.align
)
3083 return a
->kcfg
.align
> b
->kcfg
.align
? -1 : 1;
3084 /* ascending order by size, within same alignment class */
3085 if (a
->kcfg
.sz
!= b
->kcfg
.sz
)
3086 return a
->kcfg
.sz
< b
->kcfg
.sz
? -1 : 1;
3089 /* resolve ties by name */
3090 return strcmp(a
->name
, b
->name
);
3093 static int find_int_btf_id(const struct btf
*btf
)
3095 const struct btf_type
*t
;
3098 n
= btf__get_nr_types(btf
);
3099 for (i
= 1; i
<= n
; i
++) {
3100 t
= btf__type_by_id(btf
, i
);
3102 if (btf_is_int(t
) && btf_int_bits(t
) == 32)
3109 static int bpf_object__collect_externs(struct bpf_object
*obj
)
3111 struct btf_type
*sec
, *kcfg_sec
= NULL
, *ksym_sec
= NULL
;
3112 const struct btf_type
*t
;
3113 struct extern_desc
*ext
;
3115 const char *ext_name
, *sec_name
;
3119 if (!obj
->efile
.symbols
)
3122 scn
= elf_sec_by_idx(obj
, obj
->efile
.symbols_shndx
);
3123 if (elf_sec_hdr(obj
, scn
, &sh
))
3124 return -LIBBPF_ERRNO__FORMAT
;
3126 n
= sh
.sh_size
/ sh
.sh_entsize
;
3127 pr_debug("looking for externs among %d symbols...\n", n
);
3129 for (i
= 0; i
< n
; i
++) {
3132 if (!gelf_getsym(obj
->efile
.symbols
, i
, &sym
))
3133 return -LIBBPF_ERRNO__FORMAT
;
3134 if (!sym_is_extern(&sym
))
3136 ext_name
= elf_sym_str(obj
, sym
.st_name
);
3137 if (!ext_name
|| !ext_name
[0])
3141 ext
= libbpf_reallocarray(ext
, obj
->nr_extern
+ 1, sizeof(*ext
));
3145 ext
= &ext
[obj
->nr_extern
];
3146 memset(ext
, 0, sizeof(*ext
));
3149 ext
->btf_id
= find_extern_btf_id(obj
->btf
, ext_name
);
3150 if (ext
->btf_id
<= 0) {
3151 pr_warn("failed to find BTF for extern '%s': %d\n",
3152 ext_name
, ext
->btf_id
);
3155 t
= btf__type_by_id(obj
->btf
, ext
->btf_id
);
3156 ext
->name
= btf__name_by_offset(obj
->btf
, t
->name_off
);
3158 ext
->is_weak
= GELF_ST_BIND(sym
.st_info
) == STB_WEAK
;
3160 ext
->sec_btf_id
= find_extern_sec_btf_id(obj
->btf
, ext
->btf_id
);
3161 if (ext
->sec_btf_id
<= 0) {
3162 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
3163 ext_name
, ext
->btf_id
, ext
->sec_btf_id
);
3164 return ext
->sec_btf_id
;
3166 sec
= (void *)btf__type_by_id(obj
->btf
, ext
->sec_btf_id
);
3167 sec_name
= btf__name_by_offset(obj
->btf
, sec
->name_off
);
3169 if (strcmp(sec_name
, KCONFIG_SEC
) == 0) {
3171 ext
->type
= EXT_KCFG
;
3172 ext
->kcfg
.sz
= btf__resolve_size(obj
->btf
, t
->type
);
3173 if (ext
->kcfg
.sz
<= 0) {
3174 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
3175 ext_name
, ext
->kcfg
.sz
);
3176 return ext
->kcfg
.sz
;
3178 ext
->kcfg
.align
= btf__align_of(obj
->btf
, t
->type
);
3179 if (ext
->kcfg
.align
<= 0) {
3180 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
3181 ext_name
, ext
->kcfg
.align
);
3184 ext
->kcfg
.type
= find_kcfg_type(obj
->btf
, t
->type
,
3185 &ext
->kcfg
.is_signed
);
3186 if (ext
->kcfg
.type
== KCFG_UNKNOWN
) {
3187 pr_warn("extern (kcfg) '%s' type is unsupported\n", ext_name
);
3190 } else if (strcmp(sec_name
, KSYMS_SEC
) == 0) {
3192 ext
->type
= EXT_KSYM
;
3193 skip_mods_and_typedefs(obj
->btf
, t
->type
,
3194 &ext
->ksym
.type_id
);
3196 pr_warn("unrecognized extern section '%s'\n", sec_name
);
3200 pr_debug("collected %d externs total\n", obj
->nr_extern
);
3202 if (!obj
->nr_extern
)
3205 /* sort externs by type, for kcfg ones also by (align, size, name) */
3206 qsort(obj
->externs
, obj
->nr_extern
, sizeof(*ext
), cmp_externs
);
3208 /* for .ksyms section, we need to turn all externs into allocated
3209 * variables in BTF to pass kernel verification; we do this by
3210 * pretending that each extern is a 8-byte variable
3213 /* find existing 4-byte integer type in BTF to use for fake
3214 * extern variables in DATASEC
3216 int int_btf_id
= find_int_btf_id(obj
->btf
);
3218 for (i
= 0; i
< obj
->nr_extern
; i
++) {
3219 ext
= &obj
->externs
[i
];
3220 if (ext
->type
!= EXT_KSYM
)
3222 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
3223 i
, ext
->sym_idx
, ext
->name
);
3228 for (i
= 0, off
= 0; i
< n
; i
++, off
+= sizeof(int)) {
3229 struct btf_var_secinfo
*vs
= btf_var_secinfos(sec
) + i
;
3230 struct btf_type
*vt
;
3232 vt
= (void *)btf__type_by_id(obj
->btf
, vs
->type
);
3233 ext_name
= btf__name_by_offset(obj
->btf
, vt
->name_off
);
3234 ext
= find_extern_by_name(obj
, ext_name
);
3236 pr_warn("failed to find extern definition for BTF var '%s'\n",
3240 btf_var(vt
)->linkage
= BTF_VAR_GLOBAL_ALLOCATED
;
3241 vt
->type
= int_btf_id
;
3243 vs
->size
= sizeof(int);
3250 /* for kcfg externs calculate their offsets within a .kconfig map */
3252 for (i
= 0; i
< obj
->nr_extern
; i
++) {
3253 ext
= &obj
->externs
[i
];
3254 if (ext
->type
!= EXT_KCFG
)
3257 ext
->kcfg
.data_off
= roundup(off
, ext
->kcfg
.align
);
3258 off
= ext
->kcfg
.data_off
+ ext
->kcfg
.sz
;
3259 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
3260 i
, ext
->sym_idx
, ext
->kcfg
.data_off
, ext
->name
);
3264 for (i
= 0; i
< n
; i
++) {
3265 struct btf_var_secinfo
*vs
= btf_var_secinfos(sec
) + i
;
3267 t
= btf__type_by_id(obj
->btf
, vs
->type
);
3268 ext_name
= btf__name_by_offset(obj
->btf
, t
->name_off
);
3269 ext
= find_extern_by_name(obj
, ext_name
);
3271 pr_warn("failed to find extern definition for BTF var '%s'\n",
3275 btf_var(t
)->linkage
= BTF_VAR_GLOBAL_ALLOCATED
;
3276 vs
->offset
= ext
->kcfg
.data_off
;
3282 struct bpf_program
*
3283 bpf_object__find_program_by_title(const struct bpf_object
*obj
,
3286 struct bpf_program
*pos
;
3288 bpf_object__for_each_program(pos
, obj
) {
3289 if (pos
->sec_name
&& !strcmp(pos
->sec_name
, title
))
3295 static bool prog_is_subprog(const struct bpf_object
*obj
,
3296 const struct bpf_program
*prog
)
3298 /* For legacy reasons, libbpf supports an entry-point BPF programs
3299 * without SEC() attribute, i.e., those in the .text section. But if
3300 * there are 2 or more such programs in the .text section, they all
3301 * must be subprograms called from entry-point BPF programs in
3302 * designated SEC()'tions, otherwise there is no way to distinguish
3303 * which of those programs should be loaded vs which are a subprogram.
3304 * Similarly, if there is a function/program in .text and at least one
3305 * other BPF program with custom SEC() attribute, then we just assume
3306 * .text programs are subprograms (even if they are not called from
3307 * other programs), because libbpf never explicitly supported mixing
3308 * SEC()-designated BPF programs and .text entry-point BPF programs.
3310 return prog
->sec_idx
== obj
->efile
.text_shndx
&& obj
->nr_programs
> 1;
3313 struct bpf_program
*
3314 bpf_object__find_program_by_name(const struct bpf_object
*obj
,
3317 struct bpf_program
*prog
;
3319 bpf_object__for_each_program(prog
, obj
) {
3320 if (prog_is_subprog(obj
, prog
))
3322 if (!strcmp(prog
->name
, name
))
3328 static bool bpf_object__shndx_is_data(const struct bpf_object
*obj
,
3331 return shndx
== obj
->efile
.data_shndx
||
3332 shndx
== obj
->efile
.bss_shndx
||
3333 shndx
== obj
->efile
.rodata_shndx
;
3336 static bool bpf_object__shndx_is_maps(const struct bpf_object
*obj
,
3339 return shndx
== obj
->efile
.maps_shndx
||
3340 shndx
== obj
->efile
.btf_maps_shndx
;
3343 static enum libbpf_map_type
3344 bpf_object__section_to_libbpf_map_type(const struct bpf_object
*obj
, int shndx
)
3346 if (shndx
== obj
->efile
.data_shndx
)
3347 return LIBBPF_MAP_DATA
;
3348 else if (shndx
== obj
->efile
.bss_shndx
)
3349 return LIBBPF_MAP_BSS
;
3350 else if (shndx
== obj
->efile
.rodata_shndx
)
3351 return LIBBPF_MAP_RODATA
;
3352 else if (shndx
== obj
->efile
.symbols_shndx
)
3353 return LIBBPF_MAP_KCONFIG
;
3355 return LIBBPF_MAP_UNSPEC
;
3358 static int bpf_program__record_reloc(struct bpf_program
*prog
,
3359 struct reloc_desc
*reloc_desc
,
3360 __u32 insn_idx
, const char *sym_name
,
3361 const GElf_Sym
*sym
, const GElf_Rel
*rel
)
3363 struct bpf_insn
*insn
= &prog
->insns
[insn_idx
];
3364 size_t map_idx
, nr_maps
= prog
->obj
->nr_maps
;
3365 struct bpf_object
*obj
= prog
->obj
;
3366 __u32 shdr_idx
= sym
->st_shndx
;
3367 enum libbpf_map_type type
;
3368 const char *sym_sec_name
;
3369 struct bpf_map
*map
;
3371 reloc_desc
->processed
= false;
3373 /* sub-program call relocation */
3374 if (insn
->code
== (BPF_JMP
| BPF_CALL
)) {
3375 if (insn
->src_reg
!= BPF_PSEUDO_CALL
) {
3376 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog
->name
);
3377 return -LIBBPF_ERRNO__RELOC
;
3379 /* text_shndx can be 0, if no default "main" program exists */
3380 if (!shdr_idx
|| shdr_idx
!= obj
->efile
.text_shndx
) {
3381 sym_sec_name
= elf_sec_name(obj
, elf_sec_by_idx(obj
, shdr_idx
));
3382 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
3383 prog
->name
, sym_name
, sym_sec_name
);
3384 return -LIBBPF_ERRNO__RELOC
;
3386 if (sym
->st_value
% BPF_INSN_SZ
) {
3387 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
3388 prog
->name
, sym_name
, (size_t)sym
->st_value
);
3389 return -LIBBPF_ERRNO__RELOC
;
3391 reloc_desc
->type
= RELO_CALL
;
3392 reloc_desc
->insn_idx
= insn_idx
;
3393 reloc_desc
->sym_off
= sym
->st_value
;
3397 if (insn
->code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
3398 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
3399 prog
->name
, sym_name
, insn_idx
, insn
->code
);
3400 return -LIBBPF_ERRNO__RELOC
;
3403 if (sym_is_extern(sym
)) {
3404 int sym_idx
= GELF_R_SYM(rel
->r_info
);
3405 int i
, n
= obj
->nr_extern
;
3406 struct extern_desc
*ext
;
3408 for (i
= 0; i
< n
; i
++) {
3409 ext
= &obj
->externs
[i
];
3410 if (ext
->sym_idx
== sym_idx
)
3414 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
3415 prog
->name
, sym_name
, sym_idx
);
3416 return -LIBBPF_ERRNO__RELOC
;
3418 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
3419 prog
->name
, i
, ext
->name
, ext
->sym_idx
, insn_idx
);
3420 reloc_desc
->type
= RELO_EXTERN
;
3421 reloc_desc
->insn_idx
= insn_idx
;
3422 reloc_desc
->sym_off
= i
; /* sym_off stores extern index */
3426 if (!shdr_idx
|| shdr_idx
>= SHN_LORESERVE
) {
3427 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
3428 prog
->name
, sym_name
, shdr_idx
);
3429 return -LIBBPF_ERRNO__RELOC
;
3432 type
= bpf_object__section_to_libbpf_map_type(obj
, shdr_idx
);
3433 sym_sec_name
= elf_sec_name(obj
, elf_sec_by_idx(obj
, shdr_idx
));
3435 /* generic map reference relocation */
3436 if (type
== LIBBPF_MAP_UNSPEC
) {
3437 if (!bpf_object__shndx_is_maps(obj
, shdr_idx
)) {
3438 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
3439 prog
->name
, sym_name
, sym_sec_name
);
3440 return -LIBBPF_ERRNO__RELOC
;
3442 for (map_idx
= 0; map_idx
< nr_maps
; map_idx
++) {
3443 map
= &obj
->maps
[map_idx
];
3444 if (map
->libbpf_type
!= type
||
3445 map
->sec_idx
!= sym
->st_shndx
||
3446 map
->sec_offset
!= sym
->st_value
)
3448 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
3449 prog
->name
, map_idx
, map
->name
, map
->sec_idx
,
3450 map
->sec_offset
, insn_idx
);
3453 if (map_idx
>= nr_maps
) {
3454 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
3455 prog
->name
, sym_sec_name
, (size_t)sym
->st_value
);
3456 return -LIBBPF_ERRNO__RELOC
;
3458 reloc_desc
->type
= RELO_LD64
;
3459 reloc_desc
->insn_idx
= insn_idx
;
3460 reloc_desc
->map_idx
= map_idx
;
3461 reloc_desc
->sym_off
= 0; /* sym->st_value determines map_idx */
3465 /* global data map relocation */
3466 if (!bpf_object__shndx_is_data(obj
, shdr_idx
)) {
3467 pr_warn("prog '%s': bad data relo against section '%s'\n",
3468 prog
->name
, sym_sec_name
);
3469 return -LIBBPF_ERRNO__RELOC
;
3471 for (map_idx
= 0; map_idx
< nr_maps
; map_idx
++) {
3472 map
= &obj
->maps
[map_idx
];
3473 if (map
->libbpf_type
!= type
)
3475 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
3476 prog
->name
, map_idx
, map
->name
, map
->sec_idx
,
3477 map
->sec_offset
, insn_idx
);
3480 if (map_idx
>= nr_maps
) {
3481 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
3482 prog
->name
, sym_sec_name
);
3483 return -LIBBPF_ERRNO__RELOC
;
3486 reloc_desc
->type
= RELO_DATA
;
3487 reloc_desc
->insn_idx
= insn_idx
;
3488 reloc_desc
->map_idx
= map_idx
;
3489 reloc_desc
->sym_off
= sym
->st_value
;
3493 static bool prog_contains_insn(const struct bpf_program
*prog
, size_t insn_idx
)
3495 return insn_idx
>= prog
->sec_insn_off
&&
3496 insn_idx
< prog
->sec_insn_off
+ prog
->sec_insn_cnt
;
3499 static struct bpf_program
*find_prog_by_sec_insn(const struct bpf_object
*obj
,
3500 size_t sec_idx
, size_t insn_idx
)
3502 int l
= 0, r
= obj
->nr_programs
- 1, m
;
3503 struct bpf_program
*prog
;
3506 m
= l
+ (r
- l
+ 1) / 2;
3507 prog
= &obj
->programs
[m
];
3509 if (prog
->sec_idx
< sec_idx
||
3510 (prog
->sec_idx
== sec_idx
&& prog
->sec_insn_off
<= insn_idx
))
3515 /* matching program could be at index l, but it still might be the
3516 * wrong one, so we need to double check conditions for the last time
3518 prog
= &obj
->programs
[l
];
3519 if (prog
->sec_idx
== sec_idx
&& prog_contains_insn(prog
, insn_idx
))
3525 bpf_object__collect_prog_relos(struct bpf_object
*obj
, GElf_Shdr
*shdr
, Elf_Data
*data
)
3527 Elf_Data
*symbols
= obj
->efile
.symbols
;
3528 const char *relo_sec_name
, *sec_name
;
3529 size_t sec_idx
= shdr
->sh_info
;
3530 struct bpf_program
*prog
;
3531 struct reloc_desc
*relos
;
3533 const char *sym_name
;
3538 relo_sec_name
= elf_sec_str(obj
, shdr
->sh_name
);
3539 sec_name
= elf_sec_name(obj
, elf_sec_by_idx(obj
, sec_idx
));
3540 if (!relo_sec_name
|| !sec_name
)
3543 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
3544 relo_sec_name
, sec_idx
, sec_name
);
3545 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
3547 for (i
= 0; i
< nrels
; i
++) {
3548 if (!gelf_getrel(data
, i
, &rel
)) {
3549 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name
, i
);
3550 return -LIBBPF_ERRNO__FORMAT
;
3552 if (!gelf_getsym(symbols
, GELF_R_SYM(rel
.r_info
), &sym
)) {
3553 pr_warn("sec '%s': symbol 0x%zx not found for relo #%d\n",
3554 relo_sec_name
, (size_t)GELF_R_SYM(rel
.r_info
), i
);
3555 return -LIBBPF_ERRNO__FORMAT
;
3557 if (rel
.r_offset
% BPF_INSN_SZ
) {
3558 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
3559 relo_sec_name
, (size_t)GELF_R_SYM(rel
.r_info
), i
);
3560 return -LIBBPF_ERRNO__FORMAT
;
3563 insn_idx
= rel
.r_offset
/ BPF_INSN_SZ
;
3564 /* relocations against static functions are recorded as
3565 * relocations against the section that contains a function;
3566 * in such case, symbol will be STT_SECTION and sym.st_name
3567 * will point to empty string (0), so fetch section name
3570 if (GELF_ST_TYPE(sym
.st_info
) == STT_SECTION
&& sym
.st_name
== 0)
3571 sym_name
= elf_sec_name(obj
, elf_sec_by_idx(obj
, sym
.st_shndx
));
3573 sym_name
= elf_sym_str(obj
, sym
.st_name
);
3574 sym_name
= sym_name
?: "<?";
3576 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
3577 relo_sec_name
, i
, insn_idx
, sym_name
);
3579 prog
= find_prog_by_sec_insn(obj
, sec_idx
, insn_idx
);
3581 pr_warn("sec '%s': relo #%d: program not found in section '%s' for insn #%u\n",
3582 relo_sec_name
, i
, sec_name
, insn_idx
);
3583 return -LIBBPF_ERRNO__RELOC
;
3586 relos
= libbpf_reallocarray(prog
->reloc_desc
,
3587 prog
->nr_reloc
+ 1, sizeof(*relos
));
3590 prog
->reloc_desc
= relos
;
3592 /* adjust insn_idx to local BPF program frame of reference */
3593 insn_idx
-= prog
->sec_insn_off
;
3594 err
= bpf_program__record_reloc(prog
, &relos
[prog
->nr_reloc
],
3595 insn_idx
, sym_name
, &sym
, &rel
);
3604 static int bpf_map_find_btf_info(struct bpf_object
*obj
, struct bpf_map
*map
)
3606 struct bpf_map_def
*def
= &map
->def
;
3607 __u32 key_type_id
= 0, value_type_id
= 0;
3610 /* if it's BTF-defined map, we don't need to search for type IDs.
3611 * For struct_ops map, it does not need btf_key_type_id and
3612 * btf_value_type_id.
3614 if (map
->sec_idx
== obj
->efile
.btf_maps_shndx
||
3615 bpf_map__is_struct_ops(map
))
3618 if (!bpf_map__is_internal(map
)) {
3619 ret
= btf__get_map_kv_tids(obj
->btf
, map
->name
, def
->key_size
,
3620 def
->value_size
, &key_type_id
,
3624 * LLVM annotates global data differently in BTF, that is,
3625 * only as '.data', '.bss' or '.rodata'.
3627 ret
= btf__find_by_name(obj
->btf
,
3628 libbpf_type_to_btf_name
[map
->libbpf_type
]);
3633 map
->btf_key_type_id
= key_type_id
;
3634 map
->btf_value_type_id
= bpf_map__is_internal(map
) ?
3635 ret
: value_type_id
;
3639 int bpf_map__reuse_fd(struct bpf_map
*map
, int fd
)
3641 struct bpf_map_info info
= {};
3642 __u32 len
= sizeof(info
);
3646 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
3650 new_name
= strdup(info
.name
);
3654 new_fd
= open("/", O_RDONLY
| O_CLOEXEC
);
3657 goto err_free_new_name
;
3660 new_fd
= dup3(fd
, new_fd
, O_CLOEXEC
);
3663 goto err_close_new_fd
;
3666 err
= zclose(map
->fd
);
3669 goto err_close_new_fd
;
3674 map
->name
= new_name
;
3675 map
->def
.type
= info
.type
;
3676 map
->def
.key_size
= info
.key_size
;
3677 map
->def
.value_size
= info
.value_size
;
3678 map
->def
.max_entries
= info
.max_entries
;
3679 map
->def
.map_flags
= info
.map_flags
;
3680 map
->btf_key_type_id
= info
.btf_key_type_id
;
3681 map
->btf_value_type_id
= info
.btf_value_type_id
;
3693 __u32
bpf_map__max_entries(const struct bpf_map
*map
)
3695 return map
->def
.max_entries
;
3698 int bpf_map__set_max_entries(struct bpf_map
*map
, __u32 max_entries
)
3702 map
->def
.max_entries
= max_entries
;
3706 int bpf_map__resize(struct bpf_map
*map
, __u32 max_entries
)
3708 if (!map
|| !max_entries
)
3711 return bpf_map__set_max_entries(map
, max_entries
);
3715 bpf_object__probe_loading(struct bpf_object
*obj
)
3717 struct bpf_load_program_attr attr
;
3718 char *cp
, errmsg
[STRERR_BUFSIZE
];
3719 struct bpf_insn insns
[] = {
3720 BPF_MOV64_IMM(BPF_REG_0
, 0),
3725 /* make sure basic loading works */
3727 memset(&attr
, 0, sizeof(attr
));
3728 attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
3730 attr
.insns_cnt
= ARRAY_SIZE(insns
);
3731 attr
.license
= "GPL";
3733 ret
= bpf_load_program_xattr(&attr
, NULL
, 0);
3736 cp
= libbpf_strerror_r(ret
, errmsg
, sizeof(errmsg
));
3737 pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
3738 "program. Make sure your kernel supports BPF "
3739 "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
3740 "set to big enough value.\n", __func__
, cp
, ret
);
3748 static int probe_fd(int fd
)
3755 static int probe_kern_prog_name(void)
3757 struct bpf_load_program_attr attr
;
3758 struct bpf_insn insns
[] = {
3759 BPF_MOV64_IMM(BPF_REG_0
, 0),
3764 /* make sure loading with name works */
3766 memset(&attr
, 0, sizeof(attr
));
3767 attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
3769 attr
.insns_cnt
= ARRAY_SIZE(insns
);
3770 attr
.license
= "GPL";
3772 ret
= bpf_load_program_xattr(&attr
, NULL
, 0);
3773 return probe_fd(ret
);
3776 static int probe_kern_global_data(void)
3778 struct bpf_load_program_attr prg_attr
;
3779 struct bpf_create_map_attr map_attr
;
3780 char *cp
, errmsg
[STRERR_BUFSIZE
];
3781 struct bpf_insn insns
[] = {
3782 BPF_LD_MAP_VALUE(BPF_REG_1
, 0, 16),
3783 BPF_ST_MEM(BPF_DW
, BPF_REG_1
, 0, 42),
3784 BPF_MOV64_IMM(BPF_REG_0
, 0),
3789 memset(&map_attr
, 0, sizeof(map_attr
));
3790 map_attr
.map_type
= BPF_MAP_TYPE_ARRAY
;
3791 map_attr
.key_size
= sizeof(int);
3792 map_attr
.value_size
= 32;
3793 map_attr
.max_entries
= 1;
3795 map
= bpf_create_map_xattr(&map_attr
);
3798 cp
= libbpf_strerror_r(ret
, errmsg
, sizeof(errmsg
));
3799 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3800 __func__
, cp
, -ret
);
3806 memset(&prg_attr
, 0, sizeof(prg_attr
));
3807 prg_attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
3808 prg_attr
.insns
= insns
;
3809 prg_attr
.insns_cnt
= ARRAY_SIZE(insns
);
3810 prg_attr
.license
= "GPL";
3812 ret
= bpf_load_program_xattr(&prg_attr
, NULL
, 0);
3814 return probe_fd(ret
);
3817 static int probe_kern_btf(void)
3819 static const char strs
[] = "\0int";
3822 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED
, 0, 32, 4),
3825 return probe_fd(libbpf__load_raw_btf((char *)types
, sizeof(types
),
3826 strs
, sizeof(strs
)));
3829 static int probe_kern_btf_func(void)
3831 static const char strs
[] = "\0int\0x\0a";
3832 /* void x(int a) {} */
3835 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
3836 /* FUNC_PROTO */ /* [2] */
3837 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO
, 0, 1), 0),
3838 BTF_PARAM_ENC(7, 1),
3839 /* FUNC x */ /* [3] */
3840 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC
, 0, 0), 2),
3843 return probe_fd(libbpf__load_raw_btf((char *)types
, sizeof(types
),
3844 strs
, sizeof(strs
)));
3847 static int probe_kern_btf_func_global(void)
3849 static const char strs
[] = "\0int\0x\0a";
3850 /* static void x(int a) {} */
3853 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
3854 /* FUNC_PROTO */ /* [2] */
3855 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO
, 0, 1), 0),
3856 BTF_PARAM_ENC(7, 1),
3857 /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
3858 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC
, 0, BTF_FUNC_GLOBAL
), 2),
3861 return probe_fd(libbpf__load_raw_btf((char *)types
, sizeof(types
),
3862 strs
, sizeof(strs
)));
3865 static int probe_kern_btf_datasec(void)
3867 static const char strs
[] = "\0x\0.data";
3871 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
3872 /* VAR x */ /* [2] */
3873 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR
, 0, 0), 1),
3875 /* DATASEC val */ /* [3] */
3876 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC
, 0, 1), 4),
3877 BTF_VAR_SECINFO_ENC(2, 0, 4),
3880 return probe_fd(libbpf__load_raw_btf((char *)types
, sizeof(types
),
3881 strs
, sizeof(strs
)));
3884 static int probe_kern_array_mmap(void)
3886 struct bpf_create_map_attr attr
= {
3887 .map_type
= BPF_MAP_TYPE_ARRAY
,
3888 .map_flags
= BPF_F_MMAPABLE
,
3889 .key_size
= sizeof(int),
3890 .value_size
= sizeof(int),
3894 return probe_fd(bpf_create_map_xattr(&attr
));
3897 static int probe_kern_exp_attach_type(void)
3899 struct bpf_load_program_attr attr
;
3900 struct bpf_insn insns
[] = {
3901 BPF_MOV64_IMM(BPF_REG_0
, 0),
3905 memset(&attr
, 0, sizeof(attr
));
3906 /* use any valid combination of program type and (optional)
3907 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
3908 * to see if kernel supports expected_attach_type field for
3909 * BPF_PROG_LOAD command
3911 attr
.prog_type
= BPF_PROG_TYPE_CGROUP_SOCK
;
3912 attr
.expected_attach_type
= BPF_CGROUP_INET_SOCK_CREATE
;
3914 attr
.insns_cnt
= ARRAY_SIZE(insns
);
3915 attr
.license
= "GPL";
3917 return probe_fd(bpf_load_program_xattr(&attr
, NULL
, 0));
3920 static int probe_kern_probe_read_kernel(void)
3922 struct bpf_load_program_attr attr
;
3923 struct bpf_insn insns
[] = {
3924 BPF_MOV64_REG(BPF_REG_1
, BPF_REG_10
), /* r1 = r10 (fp) */
3925 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_1
, -8), /* r1 += -8 */
3926 BPF_MOV64_IMM(BPF_REG_2
, 8), /* r2 = 8 */
3927 BPF_MOV64_IMM(BPF_REG_3
, 0), /* r3 = 0 */
3928 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_probe_read_kernel
),
3932 memset(&attr
, 0, sizeof(attr
));
3933 attr
.prog_type
= BPF_PROG_TYPE_KPROBE
;
3935 attr
.insns_cnt
= ARRAY_SIZE(insns
);
3936 attr
.license
= "GPL";
3938 return probe_fd(bpf_load_program_xattr(&attr
, NULL
, 0));
3941 static int probe_prog_bind_map(void)
3943 struct bpf_load_program_attr prg_attr
;
3944 struct bpf_create_map_attr map_attr
;
3945 char *cp
, errmsg
[STRERR_BUFSIZE
];
3946 struct bpf_insn insns
[] = {
3947 BPF_MOV64_IMM(BPF_REG_0
, 0),
3952 memset(&map_attr
, 0, sizeof(map_attr
));
3953 map_attr
.map_type
= BPF_MAP_TYPE_ARRAY
;
3954 map_attr
.key_size
= sizeof(int);
3955 map_attr
.value_size
= 32;
3956 map_attr
.max_entries
= 1;
3958 map
= bpf_create_map_xattr(&map_attr
);
3961 cp
= libbpf_strerror_r(ret
, errmsg
, sizeof(errmsg
));
3962 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3963 __func__
, cp
, -ret
);
3967 memset(&prg_attr
, 0, sizeof(prg_attr
));
3968 prg_attr
.prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
3969 prg_attr
.insns
= insns
;
3970 prg_attr
.insns_cnt
= ARRAY_SIZE(insns
);
3971 prg_attr
.license
= "GPL";
3973 prog
= bpf_load_program_xattr(&prg_attr
, NULL
, 0);
3979 ret
= bpf_prog_bind_map(prog
, map
, NULL
);
3987 static int probe_module_btf(void)
3989 static const char strs
[] = "\0int";
3992 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED
, 0, 32, 4),
3994 struct bpf_btf_info info
;
3995 __u32 len
= sizeof(info
);
3999 fd
= libbpf__load_raw_btf((char *)types
, sizeof(types
), strs
, sizeof(strs
));
4001 return 0; /* BTF not supported at all */
4003 memset(&info
, 0, sizeof(info
));
4004 info
.name
= ptr_to_u64(name
);
4005 info
.name_len
= sizeof(name
);
4007 /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
4008 * kernel's module BTF support coincides with support for
4009 * name/name_len fields in struct bpf_btf_info.
4011 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
4016 enum kern_feature_result
{
4022 typedef int (*feature_probe_fn
)(void);
4024 static struct kern_feature_desc
{
4026 feature_probe_fn probe
;
4027 enum kern_feature_result res
;
4028 } feature_probes
[__FEAT_CNT
] = {
4029 [FEAT_PROG_NAME
] = {
4030 "BPF program name", probe_kern_prog_name
,
4032 [FEAT_GLOBAL_DATA
] = {
4033 "global variables", probe_kern_global_data
,
4036 "minimal BTF", probe_kern_btf
,
4039 "BTF functions", probe_kern_btf_func
,
4041 [FEAT_BTF_GLOBAL_FUNC
] = {
4042 "BTF global function", probe_kern_btf_func_global
,
4044 [FEAT_BTF_DATASEC
] = {
4045 "BTF data section and variable", probe_kern_btf_datasec
,
4047 [FEAT_ARRAY_MMAP
] = {
4048 "ARRAY map mmap()", probe_kern_array_mmap
,
4050 [FEAT_EXP_ATTACH_TYPE
] = {
4051 "BPF_PROG_LOAD expected_attach_type attribute",
4052 probe_kern_exp_attach_type
,
4054 [FEAT_PROBE_READ_KERN
] = {
4055 "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel
,
4057 [FEAT_PROG_BIND_MAP
] = {
4058 "BPF_PROG_BIND_MAP support", probe_prog_bind_map
,
4060 [FEAT_MODULE_BTF
] = {
4061 "module BTF support", probe_module_btf
,
4065 static bool kernel_supports(enum kern_feature_id feat_id
)
4067 struct kern_feature_desc
*feat
= &feature_probes
[feat_id
];
4070 if (READ_ONCE(feat
->res
) == FEAT_UNKNOWN
) {
4071 ret
= feat
->probe();
4073 WRITE_ONCE(feat
->res
, FEAT_SUPPORTED
);
4074 } else if (ret
== 0) {
4075 WRITE_ONCE(feat
->res
, FEAT_MISSING
);
4077 pr_warn("Detection of kernel %s support failed: %d\n", feat
->desc
, ret
);
4078 WRITE_ONCE(feat
->res
, FEAT_MISSING
);
4082 return READ_ONCE(feat
->res
) == FEAT_SUPPORTED
;
4085 static bool map_is_reuse_compat(const struct bpf_map
*map
, int map_fd
)
4087 struct bpf_map_info map_info
= {};
4088 char msg
[STRERR_BUFSIZE
];
4091 map_info_len
= sizeof(map_info
);
4093 if (bpf_obj_get_info_by_fd(map_fd
, &map_info
, &map_info_len
)) {
4094 pr_warn("failed to get map info for map FD %d: %s\n",
4095 map_fd
, libbpf_strerror_r(errno
, msg
, sizeof(msg
)));
4099 return (map_info
.type
== map
->def
.type
&&
4100 map_info
.key_size
== map
->def
.key_size
&&
4101 map_info
.value_size
== map
->def
.value_size
&&
4102 map_info
.max_entries
== map
->def
.max_entries
&&
4103 map_info
.map_flags
== map
->def
.map_flags
);
4107 bpf_object__reuse_map(struct bpf_map
*map
)
4109 char *cp
, errmsg
[STRERR_BUFSIZE
];
4112 pin_fd
= bpf_obj_get(map
->pin_path
);
4115 if (err
== -ENOENT
) {
4116 pr_debug("found no pinned map to reuse at '%s'\n",
4121 cp
= libbpf_strerror_r(-err
, errmsg
, sizeof(errmsg
));
4122 pr_warn("couldn't retrieve pinned map '%s': %s\n",
4127 if (!map_is_reuse_compat(map
, pin_fd
)) {
4128 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
4134 err
= bpf_map__reuse_fd(map
, pin_fd
);
4140 pr_debug("reused pinned map at '%s'\n", map
->pin_path
);
4146 bpf_object__populate_internal_map(struct bpf_object
*obj
, struct bpf_map
*map
)
4148 enum libbpf_map_type map_type
= map
->libbpf_type
;
4149 char *cp
, errmsg
[STRERR_BUFSIZE
];
4152 err
= bpf_map_update_elem(map
->fd
, &zero
, map
->mmaped
, 0);
4155 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
4156 pr_warn("Error setting initial map(%s) contents: %s\n",
4161 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
4162 if (map_type
== LIBBPF_MAP_RODATA
|| map_type
== LIBBPF_MAP_KCONFIG
) {
4163 err
= bpf_map_freeze(map
->fd
);
4166 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
4167 pr_warn("Error freezing map(%s) as read-only: %s\n",
4175 static void bpf_map__destroy(struct bpf_map
*map
);
4177 static int bpf_object__create_map(struct bpf_object
*obj
, struct bpf_map
*map
)
4179 struct bpf_create_map_attr create_attr
;
4180 struct bpf_map_def
*def
= &map
->def
;
4182 memset(&create_attr
, 0, sizeof(create_attr
));
4184 if (kernel_supports(FEAT_PROG_NAME
))
4185 create_attr
.name
= map
->name
;
4186 create_attr
.map_ifindex
= map
->map_ifindex
;
4187 create_attr
.map_type
= def
->type
;
4188 create_attr
.map_flags
= def
->map_flags
;
4189 create_attr
.key_size
= def
->key_size
;
4190 create_attr
.value_size
= def
->value_size
;
4191 create_attr
.numa_node
= map
->numa_node
;
4193 if (def
->type
== BPF_MAP_TYPE_PERF_EVENT_ARRAY
&& !def
->max_entries
) {
4196 nr_cpus
= libbpf_num_possible_cpus();
4198 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
4199 map
->name
, nr_cpus
);
4202 pr_debug("map '%s': setting size to %d\n", map
->name
, nr_cpus
);
4203 create_attr
.max_entries
= nr_cpus
;
4205 create_attr
.max_entries
= def
->max_entries
;
4208 if (bpf_map__is_struct_ops(map
))
4209 create_attr
.btf_vmlinux_value_type_id
=
4210 map
->btf_vmlinux_value_type_id
;
4212 create_attr
.btf_fd
= 0;
4213 create_attr
.btf_key_type_id
= 0;
4214 create_attr
.btf_value_type_id
= 0;
4215 if (obj
->btf
&& btf__fd(obj
->btf
) >= 0 && !bpf_map_find_btf_info(obj
, map
)) {
4216 create_attr
.btf_fd
= btf__fd(obj
->btf
);
4217 create_attr
.btf_key_type_id
= map
->btf_key_type_id
;
4218 create_attr
.btf_value_type_id
= map
->btf_value_type_id
;
4221 if (bpf_map_type__is_map_in_map(def
->type
)) {
4222 if (map
->inner_map
) {
4225 err
= bpf_object__create_map(obj
, map
->inner_map
);
4227 pr_warn("map '%s': failed to create inner map: %d\n",
4231 map
->inner_map_fd
= bpf_map__fd(map
->inner_map
);
4233 if (map
->inner_map_fd
>= 0)
4234 create_attr
.inner_map_fd
= map
->inner_map_fd
;
4237 map
->fd
= bpf_create_map_xattr(&create_attr
);
4238 if (map
->fd
< 0 && (create_attr
.btf_key_type_id
||
4239 create_attr
.btf_value_type_id
)) {
4240 char *cp
, errmsg
[STRERR_BUFSIZE
];
4243 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
4244 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
4245 map
->name
, cp
, err
);
4246 create_attr
.btf_fd
= 0;
4247 create_attr
.btf_key_type_id
= 0;
4248 create_attr
.btf_value_type_id
= 0;
4249 map
->btf_key_type_id
= 0;
4250 map
->btf_value_type_id
= 0;
4251 map
->fd
= bpf_create_map_xattr(&create_attr
);
4257 if (bpf_map_type__is_map_in_map(def
->type
) && map
->inner_map
) {
4258 bpf_map__destroy(map
->inner_map
);
4259 zfree(&map
->inner_map
);
4265 static int init_map_slots(struct bpf_map
*map
)
4267 const struct bpf_map
*targ_map
;
4271 for (i
= 0; i
< map
->init_slots_sz
; i
++) {
4272 if (!map
->init_slots
[i
])
4275 targ_map
= map
->init_slots
[i
];
4276 fd
= bpf_map__fd(targ_map
);
4277 err
= bpf_map_update_elem(map
->fd
, &i
, &fd
, 0);
4280 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
4281 map
->name
, i
, targ_map
->name
,
4285 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
4286 map
->name
, i
, targ_map
->name
, fd
);
4289 zfree(&map
->init_slots
);
4290 map
->init_slots_sz
= 0;
4296 bpf_object__create_maps(struct bpf_object
*obj
)
4298 struct bpf_map
*map
;
4299 char *cp
, errmsg
[STRERR_BUFSIZE
];
4303 for (i
= 0; i
< obj
->nr_maps
; i
++) {
4304 map
= &obj
->maps
[i
];
4306 if (map
->pin_path
) {
4307 err
= bpf_object__reuse_map(map
);
4309 pr_warn("map '%s': error reusing pinned map\n",
4316 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
4317 map
->name
, map
->fd
);
4319 err
= bpf_object__create_map(obj
, map
);
4323 pr_debug("map '%s': created successfully, fd=%d\n",
4324 map
->name
, map
->fd
);
4326 if (bpf_map__is_internal(map
)) {
4327 err
= bpf_object__populate_internal_map(obj
, map
);
4334 if (map
->init_slots_sz
) {
4335 err
= init_map_slots(map
);
4343 if (map
->pin_path
&& !map
->pinned
) {
4344 err
= bpf_map__pin(map
, NULL
);
4346 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
4347 map
->name
, map
->pin_path
, err
);
4357 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
4358 pr_warn("map '%s': failed to create: %s(%d)\n", map
->name
, cp
, err
);
4360 for (j
= 0; j
< i
; j
++)
4361 zclose(obj
->maps
[j
].fd
);
4365 #define BPF_CORE_SPEC_MAX_LEN 64
4367 /* represents BPF CO-RE field or array element accessor */
4368 struct bpf_core_accessor
{
4369 __u32 type_id
; /* struct/union type or array element type */
4370 __u32 idx
; /* field index or array index */
4371 const char *name
; /* field name or NULL for array accessor */
4374 struct bpf_core_spec
{
4375 const struct btf
*btf
;
4376 /* high-level spec: named fields and array indices only */
4377 struct bpf_core_accessor spec
[BPF_CORE_SPEC_MAX_LEN
];
4378 /* original unresolved (no skip_mods_or_typedefs) root type ID */
4380 /* CO-RE relocation kind */
4381 enum bpf_core_relo_kind relo_kind
;
4382 /* high-level spec length */
4384 /* raw, low-level spec: 1-to-1 with accessor spec string */
4385 int raw_spec
[BPF_CORE_SPEC_MAX_LEN
];
4386 /* raw spec length */
4388 /* field bit offset represented by spec */
4392 static bool str_is_empty(const char *s
)
4397 static bool is_flex_arr(const struct btf
*btf
,
4398 const struct bpf_core_accessor
*acc
,
4399 const struct btf_array
*arr
)
4401 const struct btf_type
*t
;
4403 /* not a flexible array, if not inside a struct or has non-zero size */
4404 if (!acc
->name
|| arr
->nelems
> 0)
4407 /* has to be the last member of enclosing struct */
4408 t
= btf__type_by_id(btf
, acc
->type_id
);
4409 return acc
->idx
== btf_vlen(t
) - 1;
4412 static const char *core_relo_kind_str(enum bpf_core_relo_kind kind
)
4415 case BPF_FIELD_BYTE_OFFSET
: return "byte_off";
4416 case BPF_FIELD_BYTE_SIZE
: return "byte_sz";
4417 case BPF_FIELD_EXISTS
: return "field_exists";
4418 case BPF_FIELD_SIGNED
: return "signed";
4419 case BPF_FIELD_LSHIFT_U64
: return "lshift_u64";
4420 case BPF_FIELD_RSHIFT_U64
: return "rshift_u64";
4421 case BPF_TYPE_ID_LOCAL
: return "local_type_id";
4422 case BPF_TYPE_ID_TARGET
: return "target_type_id";
4423 case BPF_TYPE_EXISTS
: return "type_exists";
4424 case BPF_TYPE_SIZE
: return "type_size";
4425 case BPF_ENUMVAL_EXISTS
: return "enumval_exists";
4426 case BPF_ENUMVAL_VALUE
: return "enumval_value";
4427 default: return "unknown";
4431 static bool core_relo_is_field_based(enum bpf_core_relo_kind kind
)
4434 case BPF_FIELD_BYTE_OFFSET
:
4435 case BPF_FIELD_BYTE_SIZE
:
4436 case BPF_FIELD_EXISTS
:
4437 case BPF_FIELD_SIGNED
:
4438 case BPF_FIELD_LSHIFT_U64
:
4439 case BPF_FIELD_RSHIFT_U64
:
4446 static bool core_relo_is_type_based(enum bpf_core_relo_kind kind
)
4449 case BPF_TYPE_ID_LOCAL
:
4450 case BPF_TYPE_ID_TARGET
:
4451 case BPF_TYPE_EXISTS
:
4459 static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind
)
4462 case BPF_ENUMVAL_EXISTS
:
4463 case BPF_ENUMVAL_VALUE
:
4471 * Turn bpf_core_relo into a low- and high-level spec representation,
4472 * validating correctness along the way, as well as calculating resulting
4473 * field bit offset, specified by accessor string. Low-level spec captures
4474 * every single level of nestedness, including traversing anonymous
4475 * struct/union members. High-level one only captures semantically meaningful
4476 * "turning points": named fields and array indicies.
4477 * E.g., for this case:
4480 * int __unimportant;
4488 * struct sample *s = ...;
4490 * int x = &s->a[3]; // access string = '0:1:2:3'
4492 * Low-level spec has 1:1 mapping with each element of access string (it's
4493 * just a parsed access string representation): [0, 1, 2, 3].
4495 * High-level spec will capture only 3 points:
4496 * - intial zero-index access by pointer (&s->... is the same as &s[0]...);
4497 * - field 'a' access (corresponds to '2' in low-level spec);
4498 * - array element #3 access (corresponds to '3' in low-level spec).
4500 * Type-based relocations (TYPE_EXISTS/TYPE_SIZE,
4501 * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
4502 * spec and raw_spec are kept empty.
4504 * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
4505 * string to specify enumerator's value index that need to be relocated.
4507 static int bpf_core_parse_spec(const struct btf
*btf
,
4509 const char *spec_str
,
4510 enum bpf_core_relo_kind relo_kind
,
4511 struct bpf_core_spec
*spec
)
4513 int access_idx
, parsed_len
, i
;
4514 struct bpf_core_accessor
*acc
;
4515 const struct btf_type
*t
;
4520 if (str_is_empty(spec_str
) || *spec_str
== ':')
4523 memset(spec
, 0, sizeof(*spec
));
4525 spec
->root_type_id
= type_id
;
4526 spec
->relo_kind
= relo_kind
;
4528 /* type-based relocations don't have a field access string */
4529 if (core_relo_is_type_based(relo_kind
)) {
4530 if (strcmp(spec_str
, "0"))
4535 /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
4537 if (*spec_str
== ':')
4539 if (sscanf(spec_str
, "%d%n", &access_idx
, &parsed_len
) != 1)
4541 if (spec
->raw_len
== BPF_CORE_SPEC_MAX_LEN
)
4543 spec_str
+= parsed_len
;
4544 spec
->raw_spec
[spec
->raw_len
++] = access_idx
;
4547 if (spec
->raw_len
== 0)
4550 t
= skip_mods_and_typedefs(btf
, type_id
, &id
);
4554 access_idx
= spec
->raw_spec
[0];
4555 acc
= &spec
->spec
[0];
4557 acc
->idx
= access_idx
;
4560 if (core_relo_is_enumval_based(relo_kind
)) {
4561 if (!btf_is_enum(t
) || spec
->raw_len
> 1 || access_idx
>= btf_vlen(t
))
4564 /* record enumerator name in a first accessor */
4565 acc
->name
= btf__name_by_offset(btf
, btf_enum(t
)[access_idx
].name_off
);
4569 if (!core_relo_is_field_based(relo_kind
))
4572 sz
= btf__resolve_size(btf
, id
);
4575 spec
->bit_offset
= access_idx
* sz
* 8;
4577 for (i
= 1; i
< spec
->raw_len
; i
++) {
4578 t
= skip_mods_and_typedefs(btf
, id
, &id
);
4582 access_idx
= spec
->raw_spec
[i
];
4583 acc
= &spec
->spec
[spec
->len
];
4585 if (btf_is_composite(t
)) {
4586 const struct btf_member
*m
;
4589 if (access_idx
>= btf_vlen(t
))
4592 bit_offset
= btf_member_bit_offset(t
, access_idx
);
4593 spec
->bit_offset
+= bit_offset
;
4595 m
= btf_members(t
) + access_idx
;
4597 name
= btf__name_by_offset(btf
, m
->name_off
);
4598 if (str_is_empty(name
))
4602 acc
->idx
= access_idx
;
4608 } else if (btf_is_array(t
)) {
4609 const struct btf_array
*a
= btf_array(t
);
4612 t
= skip_mods_and_typedefs(btf
, a
->type
, &id
);
4616 flex
= is_flex_arr(btf
, acc
- 1, a
);
4617 if (!flex
&& access_idx
>= a
->nelems
)
4620 spec
->spec
[spec
->len
].type_id
= id
;
4621 spec
->spec
[spec
->len
].idx
= access_idx
;
4624 sz
= btf__resolve_size(btf
, id
);
4627 spec
->bit_offset
+= access_idx
* sz
* 8;
4629 pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
4630 type_id
, spec_str
, i
, id
, btf_kind_str(t
));
4638 static bool bpf_core_is_flavor_sep(const char *s
)
4640 /* check X___Y name pattern, where X and Y are not underscores */
4641 return s
[0] != '_' && /* X */
4642 s
[1] == '_' && s
[2] == '_' && s
[3] == '_' && /* ___ */
4643 s
[4] != '_'; /* Y */
4646 /* Given 'some_struct_name___with_flavor' return the length of a name prefix
4647 * before last triple underscore. Struct name part after last triple
4648 * underscore is ignored by BPF CO-RE relocation during relocation matching.
4650 static size_t bpf_core_essential_name_len(const char *name
)
4652 size_t n
= strlen(name
);
4655 for (i
= n
- 5; i
>= 0; i
--) {
4656 if (bpf_core_is_flavor_sep(name
+ i
))
4664 const struct btf
*btf
;
4665 const struct btf_type
*t
;
4670 /* dynamically sized list of type IDs and its associated struct btf */
4671 struct core_cand_list
{
4672 struct core_cand
*cands
;
4676 static void bpf_core_free_cands(struct core_cand_list
*cands
)
4682 static int bpf_core_add_cands(struct core_cand
*local_cand
,
4683 size_t local_essent_len
,
4684 const struct btf
*targ_btf
,
4685 const char *targ_btf_name
,
4687 struct core_cand_list
*cands
)
4689 struct core_cand
*new_cands
, *cand
;
4690 const struct btf_type
*t
;
4691 const char *targ_name
;
4692 size_t targ_essent_len
;
4695 n
= btf__get_nr_types(targ_btf
);
4696 for (i
= targ_start_id
; i
<= n
; i
++) {
4697 t
= btf__type_by_id(targ_btf
, i
);
4698 if (btf_kind(t
) != btf_kind(local_cand
->t
))
4701 targ_name
= btf__name_by_offset(targ_btf
, t
->name_off
);
4702 if (str_is_empty(targ_name
))
4705 targ_essent_len
= bpf_core_essential_name_len(targ_name
);
4706 if (targ_essent_len
!= local_essent_len
)
4709 if (strncmp(local_cand
->name
, targ_name
, local_essent_len
) != 0)
4712 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
4713 local_cand
->id
, btf_kind_str(local_cand
->t
),
4714 local_cand
->name
, i
, btf_kind_str(t
), targ_name
,
4716 new_cands
= libbpf_reallocarray(cands
->cands
, cands
->len
+ 1,
4717 sizeof(*cands
->cands
));
4721 cand
= &new_cands
[cands
->len
];
4722 cand
->btf
= targ_btf
;
4724 cand
->name
= targ_name
;
4727 cands
->cands
= new_cands
;
4733 static int load_module_btfs(struct bpf_object
*obj
)
4735 struct bpf_btf_info info
;
4736 struct module_btf
*mod_btf
;
4742 if (obj
->btf_modules_loaded
)
4745 /* don't do this again, even if we find no module BTFs */
4746 obj
->btf_modules_loaded
= true;
4748 /* kernel too old to support module BTFs */
4749 if (!kernel_supports(FEAT_MODULE_BTF
))
4753 err
= bpf_btf_get_next_id(id
, &id
);
4754 if (err
&& errno
== ENOENT
)
4758 pr_warn("failed to iterate BTF objects: %d\n", err
);
4762 fd
= bpf_btf_get_fd_by_id(id
);
4764 if (errno
== ENOENT
)
4765 continue; /* expected race: BTF was unloaded */
4767 pr_warn("failed to get BTF object #%d FD: %d\n", id
, err
);
4772 memset(&info
, 0, sizeof(info
));
4773 info
.name
= ptr_to_u64(name
);
4774 info
.name_len
= sizeof(name
);
4776 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
4779 pr_warn("failed to get BTF object #%d info: %d\n", id
, err
);
4783 /* ignore non-module BTFs */
4784 if (!info
.kernel_btf
|| strcmp(name
, "vmlinux") == 0) {
4789 btf
= btf_get_from_fd(fd
, obj
->btf_vmlinux
);
4791 pr_warn("failed to load module [%s]'s BTF object #%d: %ld\n",
4792 name
, id
, PTR_ERR(btf
));
4797 err
= btf_ensure_mem((void **)&obj
->btf_modules
, &obj
->btf_module_cap
,
4798 sizeof(*obj
->btf_modules
), obj
->btf_module_cnt
+ 1);
4802 mod_btf
= &obj
->btf_modules
[obj
->btf_module_cnt
++];
4807 mod_btf
->name
= strdup(name
);
4808 if (!mod_btf
->name
) {
4822 static struct core_cand_list
*
4823 bpf_core_find_cands(struct bpf_object
*obj
, const struct btf
*local_btf
, __u32 local_type_id
)
4825 struct core_cand local_cand
= {};
4826 struct core_cand_list
*cands
;
4827 const struct btf
*main_btf
;
4828 size_t local_essent_len
;
4831 local_cand
.btf
= local_btf
;
4832 local_cand
.t
= btf__type_by_id(local_btf
, local_type_id
);
4834 return ERR_PTR(-EINVAL
);
4836 local_cand
.name
= btf__name_by_offset(local_btf
, local_cand
.t
->name_off
);
4837 if (str_is_empty(local_cand
.name
))
4838 return ERR_PTR(-EINVAL
);
4839 local_essent_len
= bpf_core_essential_name_len(local_cand
.name
);
4841 cands
= calloc(1, sizeof(*cands
));
4843 return ERR_PTR(-ENOMEM
);
4845 /* Attempt to find target candidates in vmlinux BTF first */
4846 main_btf
= obj
->btf_vmlinux_override
?: obj
->btf_vmlinux
;
4847 err
= bpf_core_add_cands(&local_cand
, local_essent_len
, main_btf
, "vmlinux", 1, cands
);
4851 /* if vmlinux BTF has any candidate, don't got for module BTFs */
4855 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
4856 if (obj
->btf_vmlinux_override
)
4859 /* now look through module BTFs, trying to still find candidates */
4860 err
= load_module_btfs(obj
);
4864 for (i
= 0; i
< obj
->btf_module_cnt
; i
++) {
4865 err
= bpf_core_add_cands(&local_cand
, local_essent_len
,
4866 obj
->btf_modules
[i
].btf
,
4867 obj
->btf_modules
[i
].name
,
4868 btf__get_nr_types(obj
->btf_vmlinux
) + 1,
4876 bpf_core_free_cands(cands
);
4877 return ERR_PTR(err
);
4880 /* Check two types for compatibility for the purpose of field access
4881 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
4882 * are relocating semantically compatible entities:
4883 * - any two STRUCTs/UNIONs are compatible and can be mixed;
4884 * - any two FWDs are compatible, if their names match (modulo flavor suffix);
4885 * - any two PTRs are always compatible;
4886 * - for ENUMs, names should be the same (ignoring flavor suffix) or at
4887 * least one of enums should be anonymous;
4888 * - for ENUMs, check sizes, names are ignored;
4889 * - for INT, size and signedness are ignored;
4890 * - for ARRAY, dimensionality is ignored, element types are checked for
4891 * compatibility recursively;
4892 * - everything else shouldn't be ever a target of relocation.
4893 * These rules are not set in stone and probably will be adjusted as we get
4894 * more experience with using BPF CO-RE relocations.
4896 static int bpf_core_fields_are_compat(const struct btf
*local_btf
,
4898 const struct btf
*targ_btf
,
4901 const struct btf_type
*local_type
, *targ_type
;
4904 local_type
= skip_mods_and_typedefs(local_btf
, local_id
, &local_id
);
4905 targ_type
= skip_mods_and_typedefs(targ_btf
, targ_id
, &targ_id
);
4906 if (!local_type
|| !targ_type
)
4909 if (btf_is_composite(local_type
) && btf_is_composite(targ_type
))
4911 if (btf_kind(local_type
) != btf_kind(targ_type
))
4914 switch (btf_kind(local_type
)) {
4918 case BTF_KIND_ENUM
: {
4919 const char *local_name
, *targ_name
;
4920 size_t local_len
, targ_len
;
4922 local_name
= btf__name_by_offset(local_btf
,
4923 local_type
->name_off
);
4924 targ_name
= btf__name_by_offset(targ_btf
, targ_type
->name_off
);
4925 local_len
= bpf_core_essential_name_len(local_name
);
4926 targ_len
= bpf_core_essential_name_len(targ_name
);
4927 /* one of them is anonymous or both w/ same flavor-less names */
4928 return local_len
== 0 || targ_len
== 0 ||
4929 (local_len
== targ_len
&&
4930 strncmp(local_name
, targ_name
, local_len
) == 0);
4933 /* just reject deprecated bitfield-like integers; all other
4934 * integers are by default compatible between each other
4936 return btf_int_offset(local_type
) == 0 &&
4937 btf_int_offset(targ_type
) == 0;
4938 case BTF_KIND_ARRAY
:
4939 local_id
= btf_array(local_type
)->type
;
4940 targ_id
= btf_array(targ_type
)->type
;
4943 pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
4944 btf_kind(local_type
), local_id
, targ_id
);
4950 * Given single high-level named field accessor in local type, find
4951 * corresponding high-level accessor for a target type. Along the way,
4952 * maintain low-level spec for target as well. Also keep updating target
4955 * Searching is performed through recursive exhaustive enumeration of all
4956 * fields of a struct/union. If there are any anonymous (embedded)
4957 * structs/unions, they are recursively searched as well. If field with
4958 * desired name is found, check compatibility between local and target types,
4959 * before returning result.
4961 * 1 is returned, if field is found.
4962 * 0 is returned if no compatible field is found.
4963 * <0 is returned on error.
4965 static int bpf_core_match_member(const struct btf
*local_btf
,
4966 const struct bpf_core_accessor
*local_acc
,
4967 const struct btf
*targ_btf
,
4969 struct bpf_core_spec
*spec
,
4970 __u32
*next_targ_id
)
4972 const struct btf_type
*local_type
, *targ_type
;
4973 const struct btf_member
*local_member
, *m
;
4974 const char *local_name
, *targ_name
;
4978 targ_type
= skip_mods_and_typedefs(targ_btf
, targ_id
, &targ_id
);
4981 if (!btf_is_composite(targ_type
))
4984 local_id
= local_acc
->type_id
;
4985 local_type
= btf__type_by_id(local_btf
, local_id
);
4986 local_member
= btf_members(local_type
) + local_acc
->idx
;
4987 local_name
= btf__name_by_offset(local_btf
, local_member
->name_off
);
4989 n
= btf_vlen(targ_type
);
4990 m
= btf_members(targ_type
);
4991 for (i
= 0; i
< n
; i
++, m
++) {
4994 bit_offset
= btf_member_bit_offset(targ_type
, i
);
4996 /* too deep struct/union/array nesting */
4997 if (spec
->raw_len
== BPF_CORE_SPEC_MAX_LEN
)
5000 /* speculate this member will be the good one */
5001 spec
->bit_offset
+= bit_offset
;
5002 spec
->raw_spec
[spec
->raw_len
++] = i
;
5004 targ_name
= btf__name_by_offset(targ_btf
, m
->name_off
);
5005 if (str_is_empty(targ_name
)) {
5006 /* embedded struct/union, we need to go deeper */
5007 found
= bpf_core_match_member(local_btf
, local_acc
,
5009 spec
, next_targ_id
);
5010 if (found
) /* either found or error */
5012 } else if (strcmp(local_name
, targ_name
) == 0) {
5013 /* matching named field */
5014 struct bpf_core_accessor
*targ_acc
;
5016 targ_acc
= &spec
->spec
[spec
->len
++];
5017 targ_acc
->type_id
= targ_id
;
5019 targ_acc
->name
= targ_name
;
5021 *next_targ_id
= m
->type
;
5022 found
= bpf_core_fields_are_compat(local_btf
,
5026 spec
->len
--; /* pop accessor */
5029 /* member turned out not to be what we looked for */
5030 spec
->bit_offset
-= bit_offset
;
5037 /* Check local and target types for compatibility. This check is used for
5038 * type-based CO-RE relocations and follow slightly different rules than
5039 * field-based relocations. This function assumes that root types were already
5040 * checked for name match. Beyond that initial root-level name check, names
5041 * are completely ignored. Compatibility rules are as follows:
5042 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5043 * kind should match for local and target types (i.e., STRUCT is not
5044 * compatible with UNION);
5045 * - for ENUMs, the size is ignored;
5046 * - for INT, size and signedness are ignored;
5047 * - for ARRAY, dimensionality is ignored, element types are checked for
5048 * compatibility recursively;
5049 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5050 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5051 * - FUNC_PROTOs are compatible if they have compatible signature: same
5052 * number of input args and compatible return and argument types.
5053 * These rules are not set in stone and probably will be adjusted as we get
5054 * more experience with using BPF CO-RE relocations.
5056 static int bpf_core_types_are_compat(const struct btf
*local_btf
, __u32 local_id
,
5057 const struct btf
*targ_btf
, __u32 targ_id
)
5059 const struct btf_type
*local_type
, *targ_type
;
5060 int depth
= 32; /* max recursion depth */
5062 /* caller made sure that names match (ignoring flavor suffix) */
5063 local_type
= btf__type_by_id(local_btf
, local_id
);
5064 targ_type
= btf__type_by_id(targ_btf
, targ_id
);
5065 if (btf_kind(local_type
) != btf_kind(targ_type
))
5073 local_type
= skip_mods_and_typedefs(local_btf
, local_id
, &local_id
);
5074 targ_type
= skip_mods_and_typedefs(targ_btf
, targ_id
, &targ_id
);
5075 if (!local_type
|| !targ_type
)
5078 if (btf_kind(local_type
) != btf_kind(targ_type
))
5081 switch (btf_kind(local_type
)) {
5083 case BTF_KIND_STRUCT
:
5084 case BTF_KIND_UNION
:
5089 /* just reject deprecated bitfield-like integers; all other
5090 * integers are by default compatible between each other
5092 return btf_int_offset(local_type
) == 0 && btf_int_offset(targ_type
) == 0;
5094 local_id
= local_type
->type
;
5095 targ_id
= targ_type
->type
;
5097 case BTF_KIND_ARRAY
:
5098 local_id
= btf_array(local_type
)->type
;
5099 targ_id
= btf_array(targ_type
)->type
;
5101 case BTF_KIND_FUNC_PROTO
: {
5102 struct btf_param
*local_p
= btf_params(local_type
);
5103 struct btf_param
*targ_p
= btf_params(targ_type
);
5104 __u16 local_vlen
= btf_vlen(local_type
);
5105 __u16 targ_vlen
= btf_vlen(targ_type
);
5108 if (local_vlen
!= targ_vlen
)
5111 for (i
= 0; i
< local_vlen
; i
++, local_p
++, targ_p
++) {
5112 skip_mods_and_typedefs(local_btf
, local_p
->type
, &local_id
);
5113 skip_mods_and_typedefs(targ_btf
, targ_p
->type
, &targ_id
);
5114 err
= bpf_core_types_are_compat(local_btf
, local_id
, targ_btf
, targ_id
);
5119 /* tail recurse for return type check */
5120 skip_mods_and_typedefs(local_btf
, local_type
->type
, &local_id
);
5121 skip_mods_and_typedefs(targ_btf
, targ_type
->type
, &targ_id
);
5125 pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
5126 btf_kind_str(local_type
), local_id
, targ_id
);
5132 * Try to match local spec to a target type and, if successful, produce full
5133 * target spec (high-level, low-level + bit offset).
5135 static int bpf_core_spec_match(struct bpf_core_spec
*local_spec
,
5136 const struct btf
*targ_btf
, __u32 targ_id
,
5137 struct bpf_core_spec
*targ_spec
)
5139 const struct btf_type
*targ_type
;
5140 const struct bpf_core_accessor
*local_acc
;
5141 struct bpf_core_accessor
*targ_acc
;
5144 memset(targ_spec
, 0, sizeof(*targ_spec
));
5145 targ_spec
->btf
= targ_btf
;
5146 targ_spec
->root_type_id
= targ_id
;
5147 targ_spec
->relo_kind
= local_spec
->relo_kind
;
5149 if (core_relo_is_type_based(local_spec
->relo_kind
)) {
5150 return bpf_core_types_are_compat(local_spec
->btf
,
5151 local_spec
->root_type_id
,
5155 local_acc
= &local_spec
->spec
[0];
5156 targ_acc
= &targ_spec
->spec
[0];
5158 if (core_relo_is_enumval_based(local_spec
->relo_kind
)) {
5159 size_t local_essent_len
, targ_essent_len
;
5160 const struct btf_enum
*e
;
5161 const char *targ_name
;
5163 /* has to resolve to an enum */
5164 targ_type
= skip_mods_and_typedefs(targ_spec
->btf
, targ_id
, &targ_id
);
5165 if (!btf_is_enum(targ_type
))
5168 local_essent_len
= bpf_core_essential_name_len(local_acc
->name
);
5170 for (i
= 0, e
= btf_enum(targ_type
); i
< btf_vlen(targ_type
); i
++, e
++) {
5171 targ_name
= btf__name_by_offset(targ_spec
->btf
, e
->name_off
);
5172 targ_essent_len
= bpf_core_essential_name_len(targ_name
);
5173 if (targ_essent_len
!= local_essent_len
)
5175 if (strncmp(local_acc
->name
, targ_name
, local_essent_len
) == 0) {
5176 targ_acc
->type_id
= targ_id
;
5178 targ_acc
->name
= targ_name
;
5180 targ_spec
->raw_spec
[targ_spec
->raw_len
] = targ_acc
->idx
;
5181 targ_spec
->raw_len
++;
5188 if (!core_relo_is_field_based(local_spec
->relo_kind
))
5191 for (i
= 0; i
< local_spec
->len
; i
++, local_acc
++, targ_acc
++) {
5192 targ_type
= skip_mods_and_typedefs(targ_spec
->btf
, targ_id
,
5197 if (local_acc
->name
) {
5198 matched
= bpf_core_match_member(local_spec
->btf
,
5201 targ_spec
, &targ_id
);
5205 /* for i=0, targ_id is already treated as array element
5206 * type (because it's the original struct), for others
5207 * we should find array element type first
5210 const struct btf_array
*a
;
5213 if (!btf_is_array(targ_type
))
5216 a
= btf_array(targ_type
);
5217 flex
= is_flex_arr(targ_btf
, targ_acc
- 1, a
);
5218 if (!flex
&& local_acc
->idx
>= a
->nelems
)
5220 if (!skip_mods_and_typedefs(targ_btf
, a
->type
,
5225 /* too deep struct/union/array nesting */
5226 if (targ_spec
->raw_len
== BPF_CORE_SPEC_MAX_LEN
)
5229 targ_acc
->type_id
= targ_id
;
5230 targ_acc
->idx
= local_acc
->idx
;
5231 targ_acc
->name
= NULL
;
5233 targ_spec
->raw_spec
[targ_spec
->raw_len
] = targ_acc
->idx
;
5234 targ_spec
->raw_len
++;
5236 sz
= btf__resolve_size(targ_btf
, targ_id
);
5239 targ_spec
->bit_offset
+= local_acc
->idx
* sz
* 8;
5246 static int bpf_core_calc_field_relo(const struct bpf_program
*prog
,
5247 const struct bpf_core_relo
*relo
,
5248 const struct bpf_core_spec
*spec
,
5249 __u32
*val
, __u32
*field_sz
, __u32
*type_id
,
5252 const struct bpf_core_accessor
*acc
;
5253 const struct btf_type
*t
;
5254 __u32 byte_off
, byte_sz
, bit_off
, bit_sz
, field_type_id
;
5255 const struct btf_member
*m
;
5256 const struct btf_type
*mt
;
5262 if (relo
->kind
== BPF_FIELD_EXISTS
) {
5263 *val
= spec
? 1 : 0;
5268 return -EUCLEAN
; /* request instruction poisoning */
5270 acc
= &spec
->spec
[spec
->len
- 1];
5271 t
= btf__type_by_id(spec
->btf
, acc
->type_id
);
5273 /* a[n] accessor needs special handling */
5275 if (relo
->kind
== BPF_FIELD_BYTE_OFFSET
) {
5276 *val
= spec
->bit_offset
/ 8;
5277 /* remember field size for load/store mem size */
5278 sz
= btf__resolve_size(spec
->btf
, acc
->type_id
);
5282 *type_id
= acc
->type_id
;
5283 } else if (relo
->kind
== BPF_FIELD_BYTE_SIZE
) {
5284 sz
= btf__resolve_size(spec
->btf
, acc
->type_id
);
5289 pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
5290 prog
->name
, relo
->kind
, relo
->insn_off
/ 8);
5298 m
= btf_members(t
) + acc
->idx
;
5299 mt
= skip_mods_and_typedefs(spec
->btf
, m
->type
, &field_type_id
);
5300 bit_off
= spec
->bit_offset
;
5301 bit_sz
= btf_member_bitfield_size(t
, acc
->idx
);
5303 bitfield
= bit_sz
> 0;
5306 byte_off
= bit_off
/ 8 / byte_sz
* byte_sz
;
5307 /* figure out smallest int size necessary for bitfield load */
5308 while (bit_off
+ bit_sz
- byte_off
* 8 > byte_sz
* 8) {
5310 /* bitfield can't be read with 64-bit read */
5311 pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
5312 prog
->name
, relo
->kind
, relo
->insn_off
/ 8);
5316 byte_off
= bit_off
/ 8 / byte_sz
* byte_sz
;
5319 sz
= btf__resolve_size(spec
->btf
, field_type_id
);
5323 byte_off
= spec
->bit_offset
/ 8;
5324 bit_sz
= byte_sz
* 8;
5327 /* for bitfields, all the relocatable aspects are ambiguous and we
5328 * might disagree with compiler, so turn off validation of expected
5329 * value, except for signedness
5332 *validate
= !bitfield
;
5334 switch (relo
->kind
) {
5335 case BPF_FIELD_BYTE_OFFSET
:
5338 *field_sz
= byte_sz
;
5339 *type_id
= field_type_id
;
5342 case BPF_FIELD_BYTE_SIZE
:
5345 case BPF_FIELD_SIGNED
:
5346 /* enums will be assumed unsigned */
5347 *val
= btf_is_enum(mt
) ||
5348 (btf_int_encoding(mt
) & BTF_INT_SIGNED
);
5350 *validate
= true; /* signedness is never ambiguous */
5352 case BPF_FIELD_LSHIFT_U64
:
5353 #if __BYTE_ORDER == __LITTLE_ENDIAN
5354 *val
= 64 - (bit_off
+ bit_sz
- byte_off
* 8);
5356 *val
= (8 - byte_sz
) * 8 + (bit_off
- byte_off
* 8);
5359 case BPF_FIELD_RSHIFT_U64
:
5362 *validate
= true; /* right shift is never ambiguous */
5364 case BPF_FIELD_EXISTS
:
5372 static int bpf_core_calc_type_relo(const struct bpf_core_relo
*relo
,
5373 const struct bpf_core_spec
*spec
,
5378 /* type-based relos return zero when target type is not found */
5384 switch (relo
->kind
) {
5385 case BPF_TYPE_ID_TARGET
:
5386 *val
= spec
->root_type_id
;
5388 case BPF_TYPE_EXISTS
:
5392 sz
= btf__resolve_size(spec
->btf
, spec
->root_type_id
);
5397 case BPF_TYPE_ID_LOCAL
:
5398 /* BPF_TYPE_ID_LOCAL is handled specially and shouldn't get here */
5406 static int bpf_core_calc_enumval_relo(const struct bpf_core_relo
*relo
,
5407 const struct bpf_core_spec
*spec
,
5410 const struct btf_type
*t
;
5411 const struct btf_enum
*e
;
5413 switch (relo
->kind
) {
5414 case BPF_ENUMVAL_EXISTS
:
5415 *val
= spec
? 1 : 0;
5417 case BPF_ENUMVAL_VALUE
:
5419 return -EUCLEAN
; /* request instruction poisoning */
5420 t
= btf__type_by_id(spec
->btf
, spec
->spec
[0].type_id
);
5421 e
= btf_enum(t
) + spec
->spec
[0].idx
;
5431 struct bpf_core_relo_res
5433 /* expected value in the instruction, unless validate == false */
5435 /* new value that needs to be patched up to */
5437 /* relocation unsuccessful, poison instruction, but don't fail load */
5439 /* some relocations can't be validated against orig_val */
5441 /* for field byte offset relocations or the forms:
5442 * *(T *)(rX + <off>) = rY
5443 * rX = *(T *)(rY + <off>),
5444 * we remember original and resolved field size to adjust direct
5445 * memory loads of pointers and integers; this is necessary for 32-bit
5446 * host kernel architectures, but also allows to automatically
5447 * relocate fields that were resized from, e.g., u32 to u64, etc.
5449 bool fail_memsz_adjust
;
5456 /* Calculate original and target relocation values, given local and target
5457 * specs and relocation kind. These values are calculated for each candidate.
5458 * If there are multiple candidates, resulting values should all be consistent
5459 * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
5460 * If instruction has to be poisoned, *poison will be set to true.
5462 static int bpf_core_calc_relo(const struct bpf_program
*prog
,
5463 const struct bpf_core_relo
*relo
,
5465 const struct bpf_core_spec
*local_spec
,
5466 const struct bpf_core_spec
*targ_spec
,
5467 struct bpf_core_relo_res
*res
)
5469 int err
= -EOPNOTSUPP
;
5473 res
->poison
= false;
5474 res
->validate
= true;
5475 res
->fail_memsz_adjust
= false;
5476 res
->orig_sz
= res
->new_sz
= 0;
5477 res
->orig_type_id
= res
->new_type_id
= 0;
5479 if (core_relo_is_field_based(relo
->kind
)) {
5480 err
= bpf_core_calc_field_relo(prog
, relo
, local_spec
,
5481 &res
->orig_val
, &res
->orig_sz
,
5482 &res
->orig_type_id
, &res
->validate
);
5483 err
= err
?: bpf_core_calc_field_relo(prog
, relo
, targ_spec
,
5484 &res
->new_val
, &res
->new_sz
,
5485 &res
->new_type_id
, NULL
);
5488 /* Validate if it's safe to adjust load/store memory size.
5489 * Adjustments are performed only if original and new memory
5492 res
->fail_memsz_adjust
= false;
5493 if (res
->orig_sz
!= res
->new_sz
) {
5494 const struct btf_type
*orig_t
, *new_t
;
5496 orig_t
= btf__type_by_id(local_spec
->btf
, res
->orig_type_id
);
5497 new_t
= btf__type_by_id(targ_spec
->btf
, res
->new_type_id
);
5499 /* There are two use cases in which it's safe to
5500 * adjust load/store's mem size:
5501 * - reading a 32-bit kernel pointer, while on BPF
5502 * size pointers are always 64-bit; in this case
5503 * it's safe to "downsize" instruction size due to
5504 * pointer being treated as unsigned integer with
5505 * zero-extended upper 32-bits;
5506 * - reading unsigned integers, again due to
5507 * zero-extension is preserving the value correctly.
5509 * In all other cases it's incorrect to attempt to
5510 * load/store field because read value will be
5511 * incorrect, so we poison relocated instruction.
5513 if (btf_is_ptr(orig_t
) && btf_is_ptr(new_t
))
5515 if (btf_is_int(orig_t
) && btf_is_int(new_t
) &&
5516 btf_int_encoding(orig_t
) != BTF_INT_SIGNED
&&
5517 btf_int_encoding(new_t
) != BTF_INT_SIGNED
)
5520 /* mark as invalid mem size adjustment, but this will
5521 * only be checked for LDX/STX/ST insns
5523 res
->fail_memsz_adjust
= true;
5525 } else if (core_relo_is_type_based(relo
->kind
)) {
5526 err
= bpf_core_calc_type_relo(relo
, local_spec
, &res
->orig_val
);
5527 err
= err
?: bpf_core_calc_type_relo(relo
, targ_spec
, &res
->new_val
);
5528 } else if (core_relo_is_enumval_based(relo
->kind
)) {
5529 err
= bpf_core_calc_enumval_relo(relo
, local_spec
, &res
->orig_val
);
5530 err
= err
?: bpf_core_calc_enumval_relo(relo
, targ_spec
, &res
->new_val
);
5534 if (err
== -EUCLEAN
) {
5535 /* EUCLEAN is used to signal instruction poisoning request */
5538 } else if (err
== -EOPNOTSUPP
) {
5539 /* EOPNOTSUPP means unknown/unsupported relocation */
5540 pr_warn("prog '%s': relo #%d: unrecognized CO-RE relocation %s (%d) at insn #%d\n",
5541 prog
->name
, relo_idx
, core_relo_kind_str(relo
->kind
),
5542 relo
->kind
, relo
->insn_off
/ 8);
5549 * Turn instruction for which CO_RE relocation failed into invalid one with
5550 * distinct signature.
5552 static void bpf_core_poison_insn(struct bpf_program
*prog
, int relo_idx
,
5553 int insn_idx
, struct bpf_insn
*insn
)
5555 pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
5556 prog
->name
, relo_idx
, insn_idx
);
5557 insn
->code
= BPF_JMP
| BPF_CALL
;
5561 /* if this instruction is reachable (not a dead code),
5562 * verifier will complain with the following message:
5563 * invalid func unknown#195896080
5565 insn
->imm
= 195896080; /* => 0xbad2310 => "bad relo" */
5568 static bool is_ldimm64(struct bpf_insn
*insn
)
5570 return insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
);
5573 static int insn_bpf_size_to_bytes(struct bpf_insn
*insn
)
5575 switch (BPF_SIZE(insn
->code
)) {
5576 case BPF_DW
: return 8;
5577 case BPF_W
: return 4;
5578 case BPF_H
: return 2;
5579 case BPF_B
: return 1;
5584 static int insn_bytes_to_bpf_size(__u32 sz
)
5587 case 8: return BPF_DW
;
5588 case 4: return BPF_W
;
5589 case 2: return BPF_H
;
5590 case 1: return BPF_B
;
5596 * Patch relocatable BPF instruction.
5598 * Patched value is determined by relocation kind and target specification.
5599 * For existence relocations target spec will be NULL if field/type is not found.
5600 * Expected insn->imm value is determined using relocation kind and local
5601 * spec, and is checked before patching instruction. If actual insn->imm value
5602 * is wrong, bail out with error.
5604 * Currently supported classes of BPF instruction are:
5605 * 1. rX = <imm> (assignment with immediate operand);
5606 * 2. rX += <imm> (arithmetic operations with immediate operand);
5607 * 3. rX = <imm64> (load with 64-bit immediate value);
5608 * 4. rX = *(T *)(rY + <off>), where T is one of {u8, u16, u32, u64};
5609 * 5. *(T *)(rX + <off>) = rY, where T is one of {u8, u16, u32, u64};
5610 * 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}.
5612 static int bpf_core_patch_insn(struct bpf_program
*prog
,
5613 const struct bpf_core_relo
*relo
,
5615 const struct bpf_core_relo_res
*res
)
5617 __u32 orig_val
, new_val
;
5618 struct bpf_insn
*insn
;
5622 if (relo
->insn_off
% BPF_INSN_SZ
)
5624 insn_idx
= relo
->insn_off
/ BPF_INSN_SZ
;
5625 /* adjust insn_idx from section frame of reference to the local
5626 * program's frame of reference; (sub-)program code is not yet
5627 * relocated, so it's enough to just subtract in-section offset
5629 insn_idx
= insn_idx
- prog
->sec_insn_off
;
5630 insn
= &prog
->insns
[insn_idx
];
5631 class = BPF_CLASS(insn
->code
);
5635 /* poison second part of ldimm64 to avoid confusing error from
5636 * verifier about "unknown opcode 00"
5638 if (is_ldimm64(insn
))
5639 bpf_core_poison_insn(prog
, relo_idx
, insn_idx
+ 1, insn
+ 1);
5640 bpf_core_poison_insn(prog
, relo_idx
, insn_idx
, insn
);
5644 orig_val
= res
->orig_val
;
5645 new_val
= res
->new_val
;
5650 if (BPF_SRC(insn
->code
) != BPF_K
)
5652 if (res
->validate
&& insn
->imm
!= orig_val
) {
5653 pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n",
5654 prog
->name
, relo_idx
,
5655 insn_idx
, insn
->imm
, orig_val
, new_val
);
5658 orig_val
= insn
->imm
;
5659 insn
->imm
= new_val
;
5660 pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n",
5661 prog
->name
, relo_idx
, insn_idx
,
5667 if (res
->validate
&& insn
->off
!= orig_val
) {
5668 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %u -> %u\n",
5669 prog
->name
, relo_idx
, insn_idx
, insn
->off
, orig_val
, new_val
);
5672 if (new_val
> SHRT_MAX
) {
5673 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n",
5674 prog
->name
, relo_idx
, insn_idx
, new_val
);
5677 if (res
->fail_memsz_adjust
) {
5678 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) accesses field incorrectly. "
5679 "Make sure you are accessing pointers, unsigned integers, or fields of matching type and size.\n",
5680 prog
->name
, relo_idx
, insn_idx
);
5684 orig_val
= insn
->off
;
5685 insn
->off
= new_val
;
5686 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n",
5687 prog
->name
, relo_idx
, insn_idx
, orig_val
, new_val
);
5689 if (res
->new_sz
!= res
->orig_sz
) {
5690 int insn_bytes_sz
, insn_bpf_sz
;
5692 insn_bytes_sz
= insn_bpf_size_to_bytes(insn
);
5693 if (insn_bytes_sz
!= res
->orig_sz
) {
5694 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) unexpected mem size: got %d, exp %u\n",
5695 prog
->name
, relo_idx
, insn_idx
, insn_bytes_sz
, res
->orig_sz
);
5699 insn_bpf_sz
= insn_bytes_to_bpf_size(res
->new_sz
);
5700 if (insn_bpf_sz
< 0) {
5701 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) invalid new mem size: %u\n",
5702 prog
->name
, relo_idx
, insn_idx
, res
->new_sz
);
5706 insn
->code
= BPF_MODE(insn
->code
) | insn_bpf_sz
| BPF_CLASS(insn
->code
);
5707 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) mem_sz %u -> %u\n",
5708 prog
->name
, relo_idx
, insn_idx
, res
->orig_sz
, res
->new_sz
);
5714 if (!is_ldimm64(insn
) ||
5715 insn
[0].src_reg
!= 0 || insn
[0].off
!= 0 ||
5716 insn_idx
+ 1 >= prog
->insns_cnt
||
5717 insn
[1].code
!= 0 || insn
[1].dst_reg
!= 0 ||
5718 insn
[1].src_reg
!= 0 || insn
[1].off
!= 0) {
5719 pr_warn("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n",
5720 prog
->name
, relo_idx
, insn_idx
);
5724 imm
= insn
[0].imm
+ ((__u64
)insn
[1].imm
<< 32);
5725 if (res
->validate
&& imm
!= orig_val
) {
5726 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %u -> %u\n",
5727 prog
->name
, relo_idx
,
5728 insn_idx
, (unsigned long long)imm
,
5733 insn
[0].imm
= new_val
;
5734 insn
[1].imm
= 0; /* currently only 32-bit values are supported */
5735 pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %u\n",
5736 prog
->name
, relo_idx
, insn_idx
,
5737 (unsigned long long)imm
, new_val
);
5741 pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:0x%x, src:0x%x, dst:0x%x, off:0x%x, imm:0x%x\n",
5742 prog
->name
, relo_idx
, insn_idx
, insn
->code
,
5743 insn
->src_reg
, insn
->dst_reg
, insn
->off
, insn
->imm
);
5750 /* Output spec definition in the format:
5751 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
5752 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
5754 static void bpf_core_dump_spec(int level
, const struct bpf_core_spec
*spec
)
5756 const struct btf_type
*t
;
5757 const struct btf_enum
*e
;
5762 type_id
= spec
->root_type_id
;
5763 t
= btf__type_by_id(spec
->btf
, type_id
);
5764 s
= btf__name_by_offset(spec
->btf
, t
->name_off
);
5766 libbpf_print(level
, "[%u] %s %s", type_id
, btf_kind_str(t
), str_is_empty(s
) ? "<anon>" : s
);
5768 if (core_relo_is_type_based(spec
->relo_kind
))
5771 if (core_relo_is_enumval_based(spec
->relo_kind
)) {
5772 t
= skip_mods_and_typedefs(spec
->btf
, type_id
, NULL
);
5773 e
= btf_enum(t
) + spec
->raw_spec
[0];
5774 s
= btf__name_by_offset(spec
->btf
, e
->name_off
);
5776 libbpf_print(level
, "::%s = %u", s
, e
->val
);
5780 if (core_relo_is_field_based(spec
->relo_kind
)) {
5781 for (i
= 0; i
< spec
->len
; i
++) {
5782 if (spec
->spec
[i
].name
)
5783 libbpf_print(level
, ".%s", spec
->spec
[i
].name
);
5784 else if (i
> 0 || spec
->spec
[i
].idx
> 0)
5785 libbpf_print(level
, "[%u]", spec
->spec
[i
].idx
);
5788 libbpf_print(level
, " (");
5789 for (i
= 0; i
< spec
->raw_len
; i
++)
5790 libbpf_print(level
, "%s%d", i
== 0 ? "" : ":", spec
->raw_spec
[i
]);
5792 if (spec
->bit_offset
% 8)
5793 libbpf_print(level
, " @ offset %u.%u)",
5794 spec
->bit_offset
/ 8, spec
->bit_offset
% 8);
5796 libbpf_print(level
, " @ offset %u)", spec
->bit_offset
/ 8);
5801 static size_t bpf_core_hash_fn(const void *key
, void *ctx
)
5806 static bool bpf_core_equal_fn(const void *k1
, const void *k2
, void *ctx
)
5811 static void *u32_as_hash_key(__u32 x
)
5813 return (void *)(uintptr_t)x
;
5817 * CO-RE relocate single instruction.
5819 * The outline and important points of the algorithm:
5820 * 1. For given local type, find corresponding candidate target types.
5821 * Candidate type is a type with the same "essential" name, ignoring
5822 * everything after last triple underscore (___). E.g., `sample`,
5823 * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
5824 * for each other. Names with triple underscore are referred to as
5825 * "flavors" and are useful, among other things, to allow to
5826 * specify/support incompatible variations of the same kernel struct, which
5827 * might differ between different kernel versions and/or build
5830 * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
5831 * converter, when deduplicated BTF of a kernel still contains more than
5832 * one different types with the same name. In that case, ___2, ___3, etc
5833 * are appended starting from second name conflict. But start flavors are
5834 * also useful to be defined "locally", in BPF program, to extract same
5835 * data from incompatible changes between different kernel
5836 * versions/configurations. For instance, to handle field renames between
5837 * kernel versions, one can use two flavors of the struct name with the
5838 * same common name and use conditional relocations to extract that field,
5839 * depending on target kernel version.
5840 * 2. For each candidate type, try to match local specification to this
5841 * candidate target type. Matching involves finding corresponding
5842 * high-level spec accessors, meaning that all named fields should match,
5843 * as well as all array accesses should be within the actual bounds. Also,
5844 * types should be compatible (see bpf_core_fields_are_compat for details).
5845 * 3. It is supported and expected that there might be multiple flavors
5846 * matching the spec. As long as all the specs resolve to the same set of
5847 * offsets across all candidates, there is no error. If there is any
5848 * ambiguity, CO-RE relocation will fail. This is necessary to accomodate
5849 * imprefection of BTF deduplication, which can cause slight duplication of
5850 * the same BTF type, if some directly or indirectly referenced (by
5851 * pointer) type gets resolved to different actual types in different
5852 * object files. If such situation occurs, deduplicated BTF will end up
5853 * with two (or more) structurally identical types, which differ only in
5854 * types they refer to through pointer. This should be OK in most cases and
5856 * 4. Candidate types search is performed by linearly scanning through all
5857 * types in target BTF. It is anticipated that this is overall more
5858 * efficient memory-wise and not significantly worse (if not better)
5859 * CPU-wise compared to prebuilding a map from all local type names to
5860 * a list of candidate type names. It's also sped up by caching resolved
5861 * list of matching candidates per each local "root" type ID, that has at
5862 * least one bpf_core_relo associated with it. This list is shared
5863 * between multiple relocations for the same type ID and is updated as some
5864 * of the candidates are pruned due to structural incompatibility.
5866 static int bpf_core_apply_relo(struct bpf_program
*prog
,
5867 const struct bpf_core_relo
*relo
,
5869 const struct btf
*local_btf
,
5870 struct hashmap
*cand_cache
)
5872 struct bpf_core_spec local_spec
, cand_spec
, targ_spec
= {};
5873 const void *type_key
= u32_as_hash_key(relo
->type_id
);
5874 struct bpf_core_relo_res cand_res
, targ_res
;
5875 const struct btf_type
*local_type
;
5876 const char *local_name
;
5877 struct core_cand_list
*cands
= NULL
;
5879 const char *spec_str
;
5882 local_id
= relo
->type_id
;
5883 local_type
= btf__type_by_id(local_btf
, local_id
);
5887 local_name
= btf__name_by_offset(local_btf
, local_type
->name_off
);
5891 spec_str
= btf__name_by_offset(local_btf
, relo
->access_str_off
);
5892 if (str_is_empty(spec_str
))
5895 err
= bpf_core_parse_spec(local_btf
, local_id
, spec_str
, relo
->kind
, &local_spec
);
5897 pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
5898 prog
->name
, relo_idx
, local_id
, btf_kind_str(local_type
),
5899 str_is_empty(local_name
) ? "<anon>" : local_name
,
5904 pr_debug("prog '%s': relo #%d: kind <%s> (%d), spec is ", prog
->name
,
5905 relo_idx
, core_relo_kind_str(relo
->kind
), relo
->kind
);
5906 bpf_core_dump_spec(LIBBPF_DEBUG
, &local_spec
);
5907 libbpf_print(LIBBPF_DEBUG
, "\n");
5909 /* TYPE_ID_LOCAL relo is special and doesn't need candidate search */
5910 if (relo
->kind
== BPF_TYPE_ID_LOCAL
) {
5911 targ_res
.validate
= true;
5912 targ_res
.poison
= false;
5913 targ_res
.orig_val
= local_spec
.root_type_id
;
5914 targ_res
.new_val
= local_spec
.root_type_id
;
5918 /* libbpf doesn't support candidate search for anonymous types */
5919 if (str_is_empty(spec_str
)) {
5920 pr_warn("prog '%s': relo #%d: <%s> (%d) relocation doesn't support anonymous types\n",
5921 prog
->name
, relo_idx
, core_relo_kind_str(relo
->kind
), relo
->kind
);
5925 if (!hashmap__find(cand_cache
, type_key
, (void **)&cands
)) {
5926 cands
= bpf_core_find_cands(prog
->obj
, local_btf
, local_id
);
5927 if (IS_ERR(cands
)) {
5928 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5929 prog
->name
, relo_idx
, local_id
, btf_kind_str(local_type
),
5930 local_name
, PTR_ERR(cands
));
5931 return PTR_ERR(cands
);
5933 err
= hashmap__set(cand_cache
, type_key
, cands
, NULL
, NULL
);
5935 bpf_core_free_cands(cands
);
5940 for (i
= 0, j
= 0; i
< cands
->len
; i
++) {
5941 err
= bpf_core_spec_match(&local_spec
, cands
->cands
[i
].btf
,
5942 cands
->cands
[i
].id
, &cand_spec
);
5944 pr_warn("prog '%s': relo #%d: error matching candidate #%d ",
5945 prog
->name
, relo_idx
, i
);
5946 bpf_core_dump_spec(LIBBPF_WARN
, &cand_spec
);
5947 libbpf_print(LIBBPF_WARN
, ": %d\n", err
);
5951 pr_debug("prog '%s': relo #%d: %s candidate #%d ", prog
->name
,
5952 relo_idx
, err
== 0 ? "non-matching" : "matching", i
);
5953 bpf_core_dump_spec(LIBBPF_DEBUG
, &cand_spec
);
5954 libbpf_print(LIBBPF_DEBUG
, "\n");
5959 err
= bpf_core_calc_relo(prog
, relo
, relo_idx
, &local_spec
, &cand_spec
, &cand_res
);
5964 targ_res
= cand_res
;
5965 targ_spec
= cand_spec
;
5966 } else if (cand_spec
.bit_offset
!= targ_spec
.bit_offset
) {
5967 /* if there are many field relo candidates, they
5968 * should all resolve to the same bit offset
5970 pr_warn("prog '%s': relo #%d: field offset ambiguity: %u != %u\n",
5971 prog
->name
, relo_idx
, cand_spec
.bit_offset
,
5972 targ_spec
.bit_offset
);
5974 } else if (cand_res
.poison
!= targ_res
.poison
|| cand_res
.new_val
!= targ_res
.new_val
) {
5975 /* all candidates should result in the same relocation
5976 * decision and value, otherwise it's dangerous to
5977 * proceed due to ambiguity
5979 pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %u != %s %u\n",
5980 prog
->name
, relo_idx
,
5981 cand_res
.poison
? "failure" : "success", cand_res
.new_val
,
5982 targ_res
.poison
? "failure" : "success", targ_res
.new_val
);
5986 cands
->cands
[j
++] = cands
->cands
[i
];
5990 * For BPF_FIELD_EXISTS relo or when used BPF program has field
5991 * existence checks or kernel version/config checks, it's expected
5992 * that we might not find any candidates. In this case, if field
5993 * wasn't found in any candidate, the list of candidates shouldn't
5994 * change at all, we'll just handle relocating appropriately,
5995 * depending on relo's kind.
6001 * If no candidates were found, it might be both a programmer error,
6002 * as well as expected case, depending whether instruction w/
6003 * relocation is guarded in some way that makes it unreachable (dead
6004 * code) if relocation can't be resolved. This is handled in
6005 * bpf_core_patch_insn() uniformly by replacing that instruction with
6006 * BPF helper call insn (using invalid helper ID). If that instruction
6007 * is indeed unreachable, then it will be ignored and eliminated by
6008 * verifier. If it was an error, then verifier will complain and point
6009 * to a specific instruction number in its log.
6012 pr_debug("prog '%s': relo #%d: no matching targets found\n",
6013 prog
->name
, relo_idx
);
6015 /* calculate single target relo result explicitly */
6016 err
= bpf_core_calc_relo(prog
, relo
, relo_idx
, &local_spec
, NULL
, &targ_res
);
6022 /* bpf_core_patch_insn() should know how to handle missing targ_spec */
6023 err
= bpf_core_patch_insn(prog
, relo
, relo_idx
, &targ_res
);
6025 pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
6026 prog
->name
, relo_idx
, relo
->insn_off
, err
);
6034 bpf_object__relocate_core(struct bpf_object
*obj
, const char *targ_btf_path
)
6036 const struct btf_ext_info_sec
*sec
;
6037 const struct bpf_core_relo
*rec
;
6038 const struct btf_ext_info
*seg
;
6039 struct hashmap_entry
*entry
;
6040 struct hashmap
*cand_cache
= NULL
;
6041 struct bpf_program
*prog
;
6042 const char *sec_name
;
6043 int i
, err
= 0, insn_idx
, sec_idx
;
6045 if (obj
->btf_ext
->core_relo_info
.len
== 0)
6048 if (targ_btf_path
) {
6049 obj
->btf_vmlinux_override
= btf__parse(targ_btf_path
, NULL
);
6050 if (IS_ERR_OR_NULL(obj
->btf_vmlinux_override
)) {
6051 err
= PTR_ERR(obj
->btf_vmlinux_override
);
6052 pr_warn("failed to parse target BTF: %d\n", err
);
6057 cand_cache
= hashmap__new(bpf_core_hash_fn
, bpf_core_equal_fn
, NULL
);
6058 if (IS_ERR(cand_cache
)) {
6059 err
= PTR_ERR(cand_cache
);
6063 seg
= &obj
->btf_ext
->core_relo_info
;
6064 for_each_btf_ext_sec(seg
, sec
) {
6065 sec_name
= btf__name_by_offset(obj
->btf
, sec
->sec_name_off
);
6066 if (str_is_empty(sec_name
)) {
6070 /* bpf_object's ELF is gone by now so it's not easy to find
6071 * section index by section name, but we can find *any*
6072 * bpf_program within desired section name and use it's
6073 * prog->sec_idx to do a proper search by section index and
6074 * instruction offset
6077 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6078 prog
= &obj
->programs
[i
];
6079 if (strcmp(prog
->sec_name
, sec_name
) == 0)
6083 pr_warn("sec '%s': failed to find a BPF program\n", sec_name
);
6086 sec_idx
= prog
->sec_idx
;
6088 pr_debug("sec '%s': found %d CO-RE relocations\n",
6089 sec_name
, sec
->num_info
);
6091 for_each_btf_ext_rec(seg
, sec
, i
, rec
) {
6092 insn_idx
= rec
->insn_off
/ BPF_INSN_SZ
;
6093 prog
= find_prog_by_sec_insn(obj
, sec_idx
, insn_idx
);
6095 pr_warn("sec '%s': failed to find program at insn #%d for CO-RE offset relocation #%d\n",
6096 sec_name
, insn_idx
, i
);
6100 /* no need to apply CO-RE relocation if the program is
6101 * not going to be loaded
6106 err
= bpf_core_apply_relo(prog
, rec
, i
, obj
->btf
, cand_cache
);
6108 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
6109 prog
->name
, i
, err
);
6116 /* obj->btf_vmlinux and module BTFs are freed after object load */
6117 btf__free(obj
->btf_vmlinux_override
);
6118 obj
->btf_vmlinux_override
= NULL
;
6120 if (!IS_ERR_OR_NULL(cand_cache
)) {
6121 hashmap__for_each_entry(cand_cache
, entry
, i
) {
6122 bpf_core_free_cands(entry
->value
);
6124 hashmap__free(cand_cache
);
6129 /* Relocate data references within program code:
6131 * - global variable references;
6132 * - extern references.
6135 bpf_object__relocate_data(struct bpf_object
*obj
, struct bpf_program
*prog
)
6139 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
6140 struct reloc_desc
*relo
= &prog
->reloc_desc
[i
];
6141 struct bpf_insn
*insn
= &prog
->insns
[relo
->insn_idx
];
6142 struct extern_desc
*ext
;
6144 switch (relo
->type
) {
6146 insn
[0].src_reg
= BPF_PSEUDO_MAP_FD
;
6147 insn
[0].imm
= obj
->maps
[relo
->map_idx
].fd
;
6148 relo
->processed
= true;
6151 insn
[0].src_reg
= BPF_PSEUDO_MAP_VALUE
;
6152 insn
[1].imm
= insn
[0].imm
+ relo
->sym_off
;
6153 insn
[0].imm
= obj
->maps
[relo
->map_idx
].fd
;
6154 relo
->processed
= true;
6157 ext
= &obj
->externs
[relo
->sym_off
];
6158 if (ext
->type
== EXT_KCFG
) {
6159 insn
[0].src_reg
= BPF_PSEUDO_MAP_VALUE
;
6160 insn
[0].imm
= obj
->maps
[obj
->kconfig_map_idx
].fd
;
6161 insn
[1].imm
= ext
->kcfg
.data_off
;
6162 } else /* EXT_KSYM */ {
6163 if (ext
->ksym
.type_id
) { /* typed ksyms */
6164 insn
[0].src_reg
= BPF_PSEUDO_BTF_ID
;
6165 insn
[0].imm
= ext
->ksym
.vmlinux_btf_id
;
6166 } else { /* typeless ksyms */
6167 insn
[0].imm
= (__u32
)ext
->ksym
.addr
;
6168 insn
[1].imm
= ext
->ksym
.addr
>> 32;
6171 relo
->processed
= true;
6174 /* will be handled as a follow up pass */
6177 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6178 prog
->name
, i
, relo
->type
);
6186 static int adjust_prog_btf_ext_info(const struct bpf_object
*obj
,
6187 const struct bpf_program
*prog
,
6188 const struct btf_ext_info
*ext_info
,
6189 void **prog_info
, __u32
*prog_rec_cnt
,
6192 void *copy_start
= NULL
, *copy_end
= NULL
;
6193 void *rec
, *rec_end
, *new_prog_info
;
6194 const struct btf_ext_info_sec
*sec
;
6195 size_t old_sz
, new_sz
;
6196 const char *sec_name
;
6199 for_each_btf_ext_sec(ext_info
, sec
) {
6200 sec_name
= btf__name_by_offset(obj
->btf
, sec
->sec_name_off
);
6203 if (strcmp(sec_name
, prog
->sec_name
) != 0)
6206 for_each_btf_ext_rec(ext_info
, sec
, i
, rec
) {
6207 __u32 insn_off
= *(__u32
*)rec
/ BPF_INSN_SZ
;
6209 if (insn_off
< prog
->sec_insn_off
)
6211 if (insn_off
>= prog
->sec_insn_off
+ prog
->sec_insn_cnt
)
6216 copy_end
= rec
+ ext_info
->rec_size
;
6222 /* append func/line info of a given (sub-)program to the main
6223 * program func/line info
6225 old_sz
= (size_t)(*prog_rec_cnt
) * ext_info
->rec_size
;
6226 new_sz
= old_sz
+ (copy_end
- copy_start
);
6227 new_prog_info
= realloc(*prog_info
, new_sz
);
6230 *prog_info
= new_prog_info
;
6231 *prog_rec_cnt
= new_sz
/ ext_info
->rec_size
;
6232 memcpy(new_prog_info
+ old_sz
, copy_start
, copy_end
- copy_start
);
6234 /* Kernel instruction offsets are in units of 8-byte
6235 * instructions, while .BTF.ext instruction offsets generated
6236 * by Clang are in units of bytes. So convert Clang offsets
6237 * into kernel offsets and adjust offset according to program
6238 * relocated position.
6240 off_adj
= prog
->sub_insn_off
- prog
->sec_insn_off
;
6241 rec
= new_prog_info
+ old_sz
;
6242 rec_end
= new_prog_info
+ new_sz
;
6243 for (; rec
< rec_end
; rec
+= ext_info
->rec_size
) {
6244 __u32
*insn_off
= rec
;
6246 *insn_off
= *insn_off
/ BPF_INSN_SZ
+ off_adj
;
6248 *prog_rec_sz
= ext_info
->rec_size
;
6256 reloc_prog_func_and_line_info(const struct bpf_object
*obj
,
6257 struct bpf_program
*main_prog
,
6258 const struct bpf_program
*prog
)
6262 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6263 * supprot func/line info
6265 if (!obj
->btf_ext
|| !kernel_supports(FEAT_BTF_FUNC
))
6268 /* only attempt func info relocation if main program's func_info
6269 * relocation was successful
6271 if (main_prog
!= prog
&& !main_prog
->func_info
)
6274 err
= adjust_prog_btf_ext_info(obj
, prog
, &obj
->btf_ext
->func_info
,
6275 &main_prog
->func_info
,
6276 &main_prog
->func_info_cnt
,
6277 &main_prog
->func_info_rec_size
);
6279 if (err
!= -ENOENT
) {
6280 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
6284 if (main_prog
->func_info
) {
6286 * Some info has already been found but has problem
6287 * in the last btf_ext reloc. Must have to error out.
6289 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog
->name
);
6292 /* Have problem loading the very first info. Ignore the rest. */
6293 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6298 /* don't relocate line info if main program's relocation failed */
6299 if (main_prog
!= prog
&& !main_prog
->line_info
)
6302 err
= adjust_prog_btf_ext_info(obj
, prog
, &obj
->btf_ext
->line_info
,
6303 &main_prog
->line_info
,
6304 &main_prog
->line_info_cnt
,
6305 &main_prog
->line_info_rec_size
);
6307 if (err
!= -ENOENT
) {
6308 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
6312 if (main_prog
->line_info
) {
6314 * Some info has already been found but has problem
6315 * in the last btf_ext reloc. Must have to error out.
6317 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog
->name
);
6320 /* Have problem loading the very first info. Ignore the rest. */
6321 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6327 static int cmp_relo_by_insn_idx(const void *key
, const void *elem
)
6329 size_t insn_idx
= *(const size_t *)key
;
6330 const struct reloc_desc
*relo
= elem
;
6332 if (insn_idx
== relo
->insn_idx
)
6334 return insn_idx
< relo
->insn_idx
? -1 : 1;
6337 static struct reloc_desc
*find_prog_insn_relo(const struct bpf_program
*prog
, size_t insn_idx
)
6339 return bsearch(&insn_idx
, prog
->reloc_desc
, prog
->nr_reloc
,
6340 sizeof(*prog
->reloc_desc
), cmp_relo_by_insn_idx
);
6344 bpf_object__reloc_code(struct bpf_object
*obj
, struct bpf_program
*main_prog
,
6345 struct bpf_program
*prog
)
6347 size_t sub_insn_idx
, insn_idx
, new_cnt
;
6348 struct bpf_program
*subprog
;
6349 struct bpf_insn
*insns
, *insn
;
6350 struct reloc_desc
*relo
;
6353 err
= reloc_prog_func_and_line_info(obj
, main_prog
, prog
);
6357 for (insn_idx
= 0; insn_idx
< prog
->sec_insn_cnt
; insn_idx
++) {
6358 insn
= &main_prog
->insns
[prog
->sub_insn_off
+ insn_idx
];
6359 if (!insn_is_subprog_call(insn
))
6362 relo
= find_prog_insn_relo(prog
, insn_idx
);
6363 if (relo
&& relo
->type
!= RELO_CALL
) {
6364 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6365 prog
->name
, insn_idx
, relo
->type
);
6366 return -LIBBPF_ERRNO__RELOC
;
6369 /* sub-program instruction index is a combination of
6370 * an offset of a symbol pointed to by relocation and
6371 * call instruction's imm field; for global functions,
6372 * call always has imm = -1, but for static functions
6373 * relocation is against STT_SECTION and insn->imm
6374 * points to a start of a static function
6376 sub_insn_idx
= relo
->sym_off
/ BPF_INSN_SZ
+ insn
->imm
+ 1;
6378 /* if subprogram call is to a static function within
6379 * the same ELF section, there won't be any relocation
6380 * emitted, but it also means there is no additional
6381 * offset necessary, insns->imm is relative to
6382 * instruction's original position within the section
6384 sub_insn_idx
= prog
->sec_insn_off
+ insn_idx
+ insn
->imm
+ 1;
6387 /* we enforce that sub-programs should be in .text section */
6388 subprog
= find_prog_by_sec_insn(obj
, obj
->efile
.text_shndx
, sub_insn_idx
);
6390 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6392 return -LIBBPF_ERRNO__RELOC
;
6395 /* if it's the first call instruction calling into this
6396 * subprogram (meaning this subprog hasn't been processed
6397 * yet) within the context of current main program:
6398 * - append it at the end of main program's instructions blog;
6399 * - process is recursively, while current program is put on hold;
6400 * - if that subprogram calls some other not yet processes
6401 * subprogram, same thing will happen recursively until
6402 * there are no more unprocesses subprograms left to append
6405 if (subprog
->sub_insn_off
== 0) {
6406 subprog
->sub_insn_off
= main_prog
->insns_cnt
;
6408 new_cnt
= main_prog
->insns_cnt
+ subprog
->insns_cnt
;
6409 insns
= libbpf_reallocarray(main_prog
->insns
, new_cnt
, sizeof(*insns
));
6411 pr_warn("prog '%s': failed to realloc prog code\n", main_prog
->name
);
6414 main_prog
->insns
= insns
;
6415 main_prog
->insns_cnt
= new_cnt
;
6417 memcpy(main_prog
->insns
+ subprog
->sub_insn_off
, subprog
->insns
,
6418 subprog
->insns_cnt
* sizeof(*insns
));
6420 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6421 main_prog
->name
, subprog
->insns_cnt
, subprog
->name
);
6423 err
= bpf_object__reloc_code(obj
, main_prog
, subprog
);
6428 /* main_prog->insns memory could have been re-allocated, so
6429 * calculate pointer again
6431 insn
= &main_prog
->insns
[prog
->sub_insn_off
+ insn_idx
];
6432 /* calculate correct instruction position within current main
6433 * prog; each main prog can have a different set of
6434 * subprograms appended (potentially in different order as
6435 * well), so position of any subprog can be different for
6436 * different main programs */
6437 insn
->imm
= subprog
->sub_insn_off
- (prog
->sub_insn_off
+ insn_idx
) - 1;
6440 relo
->processed
= true;
6442 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6443 prog
->name
, insn_idx
, insn
->imm
, subprog
->name
, subprog
->sub_insn_off
);
6450 * Relocate sub-program calls.
6452 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6453 * main prog) is processed separately. For each subprog (non-entry functions,
6454 * that can be called from either entry progs or other subprogs) gets their
6455 * sub_insn_off reset to zero. This serves as indicator that this subprogram
6456 * hasn't been yet appended and relocated within current main prog. Once its
6457 * relocated, sub_insn_off will point at the position within current main prog
6458 * where given subprog was appended. This will further be used to relocate all
6459 * the call instructions jumping into this subprog.
6461 * We start with main program and process all call instructions. If the call
6462 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6463 * is zero), subprog instructions are appended at the end of main program's
6464 * instruction array. Then main program is "put on hold" while we recursively
6465 * process newly appended subprogram. If that subprogram calls into another
6466 * subprogram that hasn't been appended, new subprogram is appended again to
6467 * the *main* prog's instructions (subprog's instructions are always left
6468 * untouched, as they need to be in unmodified state for subsequent main progs
6469 * and subprog instructions are always sent only as part of a main prog) and
6470 * the process continues recursively. Once all the subprogs called from a main
6471 * prog or any of its subprogs are appended (and relocated), all their
6472 * positions within finalized instructions array are known, so it's easy to
6473 * rewrite call instructions with correct relative offsets, corresponding to
6474 * desired target subprog.
6476 * Its important to realize that some subprogs might not be called from some
6477 * main prog and any of its called/used subprogs. Those will keep their
6478 * subprog->sub_insn_off as zero at all times and won't be appended to current
6479 * main prog and won't be relocated within the context of current main prog.
6480 * They might still be used from other main progs later.
6482 * Visually this process can be shown as below. Suppose we have two main
6483 * programs mainA and mainB and BPF object contains three subprogs: subA,
6484 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6485 * subC both call subB:
6487 * +--------+ +-------+
6489 * +--+---+ +--+-+-+ +---+--+
6490 * | subA | | subB | | subC |
6491 * +--+---+ +------+ +---+--+
6494 * +---+-------+ +------+----+
6495 * | mainA | | mainB |
6496 * +-----------+ +-----------+
6498 * We'll start relocating mainA, will find subA, append it and start
6499 * processing sub A recursively:
6501 * +-----------+------+
6503 * +-----------+------+
6505 * At this point we notice that subB is used from subA, so we append it and
6506 * relocate (there are no further subcalls from subB):
6508 * +-----------+------+------+
6509 * | mainA | subA | subB |
6510 * +-----------+------+------+
6512 * At this point, we relocate subA calls, then go one level up and finish with
6513 * relocatin mainA calls. mainA is done.
6515 * For mainB process is similar but results in different order. We start with
6516 * mainB and skip subA and subB, as mainB never calls them (at least
6517 * directly), but we see subC is needed, so we append and start processing it:
6519 * +-----------+------+
6521 * +-----------+------+
6522 * Now we see subC needs subB, so we go back to it, append and relocate it:
6524 * +-----------+------+------+
6525 * | mainB | subC | subB |
6526 * +-----------+------+------+
6528 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6531 bpf_object__relocate_calls(struct bpf_object
*obj
, struct bpf_program
*prog
)
6533 struct bpf_program
*subprog
;
6536 /* mark all subprogs as not relocated (yet) within the context of
6537 * current main program
6539 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6540 subprog
= &obj
->programs
[i
];
6541 if (!prog_is_subprog(obj
, subprog
))
6544 subprog
->sub_insn_off
= 0;
6545 for (j
= 0; j
< subprog
->nr_reloc
; j
++)
6546 if (subprog
->reloc_desc
[j
].type
== RELO_CALL
)
6547 subprog
->reloc_desc
[j
].processed
= false;
6550 err
= bpf_object__reloc_code(obj
, prog
, prog
);
6559 bpf_object__relocate(struct bpf_object
*obj
, const char *targ_btf_path
)
6561 struct bpf_program
*prog
;
6566 err
= bpf_object__relocate_core(obj
, targ_btf_path
);
6568 pr_warn("failed to perform CO-RE relocations: %d\n",
6573 /* relocate data references first for all programs and sub-programs,
6574 * as they don't change relative to code locations, so subsequent
6575 * subprogram processing won't need to re-calculate any of them
6577 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6578 prog
= &obj
->programs
[i
];
6579 err
= bpf_object__relocate_data(obj
, prog
);
6581 pr_warn("prog '%s': failed to relocate data references: %d\n",
6586 /* now relocate subprogram calls and append used subprograms to main
6587 * programs; each copy of subprogram code needs to be relocated
6588 * differently for each main program, because its code location might
6591 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6592 prog
= &obj
->programs
[i
];
6593 /* sub-program's sub-calls are relocated within the context of
6594 * its main program only
6596 if (prog_is_subprog(obj
, prog
))
6599 err
= bpf_object__relocate_calls(obj
, prog
);
6601 pr_warn("prog '%s': failed to relocate calls: %d\n",
6606 /* free up relocation descriptors */
6607 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6608 prog
= &obj
->programs
[i
];
6609 zfree(&prog
->reloc_desc
);
6615 static int bpf_object__collect_st_ops_relos(struct bpf_object
*obj
,
6616 GElf_Shdr
*shdr
, Elf_Data
*data
);
6618 static int bpf_object__collect_map_relos(struct bpf_object
*obj
,
6619 GElf_Shdr
*shdr
, Elf_Data
*data
)
6621 const int bpf_ptr_sz
= 8, host_ptr_sz
= sizeof(void *);
6622 int i
, j
, nrels
, new_sz
;
6623 const struct btf_var_secinfo
*vi
= NULL
;
6624 const struct btf_type
*sec
, *var
, *def
;
6625 struct bpf_map
*map
= NULL
, *targ_map
;
6626 const struct btf_member
*member
;
6627 const char *name
, *mname
;
6634 if (!obj
->efile
.btf_maps_sec_btf_id
|| !obj
->btf
)
6636 sec
= btf__type_by_id(obj
->btf
, obj
->efile
.btf_maps_sec_btf_id
);
6640 symbols
= obj
->efile
.symbols
;
6641 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
6642 for (i
= 0; i
< nrels
; i
++) {
6643 if (!gelf_getrel(data
, i
, &rel
)) {
6644 pr_warn(".maps relo #%d: failed to get ELF relo\n", i
);
6645 return -LIBBPF_ERRNO__FORMAT
;
6647 if (!gelf_getsym(symbols
, GELF_R_SYM(rel
.r_info
), &sym
)) {
6648 pr_warn(".maps relo #%d: symbol %zx not found\n",
6649 i
, (size_t)GELF_R_SYM(rel
.r_info
));
6650 return -LIBBPF_ERRNO__FORMAT
;
6652 name
= elf_sym_str(obj
, sym
.st_name
) ?: "<?>";
6653 if (sym
.st_shndx
!= obj
->efile
.btf_maps_shndx
) {
6654 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
6656 return -LIBBPF_ERRNO__RELOC
;
6659 pr_debug(".maps relo #%d: for %zd value %zd rel.r_offset %zu name %d ('%s')\n",
6660 i
, (ssize_t
)(rel
.r_info
>> 32), (size_t)sym
.st_value
,
6661 (size_t)rel
.r_offset
, sym
.st_name
, name
);
6663 for (j
= 0; j
< obj
->nr_maps
; j
++) {
6664 map
= &obj
->maps
[j
];
6665 if (map
->sec_idx
!= obj
->efile
.btf_maps_shndx
)
6668 vi
= btf_var_secinfos(sec
) + map
->btf_var_idx
;
6669 if (vi
->offset
<= rel
.r_offset
&&
6670 rel
.r_offset
+ bpf_ptr_sz
<= vi
->offset
+ vi
->size
)
6673 if (j
== obj
->nr_maps
) {
6674 pr_warn(".maps relo #%d: cannot find map '%s' at rel.r_offset %zu\n",
6675 i
, name
, (size_t)rel
.r_offset
);
6679 if (!bpf_map_type__is_map_in_map(map
->def
.type
))
6681 if (map
->def
.type
== BPF_MAP_TYPE_HASH_OF_MAPS
&&
6682 map
->def
.key_size
!= sizeof(int)) {
6683 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
6684 i
, map
->name
, sizeof(int));
6688 targ_map
= bpf_object__find_map_by_name(obj
, name
);
6692 var
= btf__type_by_id(obj
->btf
, vi
->type
);
6693 def
= skip_mods_and_typedefs(obj
->btf
, var
->type
, NULL
);
6694 if (btf_vlen(def
) == 0)
6696 member
= btf_members(def
) + btf_vlen(def
) - 1;
6697 mname
= btf__name_by_offset(obj
->btf
, member
->name_off
);
6698 if (strcmp(mname
, "values"))
6701 moff
= btf_member_bit_offset(def
, btf_vlen(def
) - 1) / 8;
6702 if (rel
.r_offset
- vi
->offset
< moff
)
6705 moff
= rel
.r_offset
- vi
->offset
- moff
;
6706 /* here we use BPF pointer size, which is always 64 bit, as we
6707 * are parsing ELF that was built for BPF target
6709 if (moff
% bpf_ptr_sz
)
6712 if (moff
>= map
->init_slots_sz
) {
6714 tmp
= libbpf_reallocarray(map
->init_slots
, new_sz
, host_ptr_sz
);
6717 map
->init_slots
= tmp
;
6718 memset(map
->init_slots
+ map
->init_slots_sz
, 0,
6719 (new_sz
- map
->init_slots_sz
) * host_ptr_sz
);
6720 map
->init_slots_sz
= new_sz
;
6722 map
->init_slots
[moff
] = targ_map
;
6724 pr_debug(".maps relo #%d: map '%s' slot [%d] points to map '%s'\n",
6725 i
, map
->name
, moff
, name
);
6731 static int cmp_relocs(const void *_a
, const void *_b
)
6733 const struct reloc_desc
*a
= _a
;
6734 const struct reloc_desc
*b
= _b
;
6736 if (a
->insn_idx
!= b
->insn_idx
)
6737 return a
->insn_idx
< b
->insn_idx
? -1 : 1;
6739 /* no two relocations should have the same insn_idx, but ... */
6740 if (a
->type
!= b
->type
)
6741 return a
->type
< b
->type
? -1 : 1;
6746 static int bpf_object__collect_relos(struct bpf_object
*obj
)
6750 for (i
= 0; i
< obj
->efile
.nr_reloc_sects
; i
++) {
6751 GElf_Shdr
*shdr
= &obj
->efile
.reloc_sects
[i
].shdr
;
6752 Elf_Data
*data
= obj
->efile
.reloc_sects
[i
].data
;
6753 int idx
= shdr
->sh_info
;
6755 if (shdr
->sh_type
!= SHT_REL
) {
6756 pr_warn("internal error at %d\n", __LINE__
);
6757 return -LIBBPF_ERRNO__INTERNAL
;
6760 if (idx
== obj
->efile
.st_ops_shndx
)
6761 err
= bpf_object__collect_st_ops_relos(obj
, shdr
, data
);
6762 else if (idx
== obj
->efile
.btf_maps_shndx
)
6763 err
= bpf_object__collect_map_relos(obj
, shdr
, data
);
6765 err
= bpf_object__collect_prog_relos(obj
, shdr
, data
);
6770 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6771 struct bpf_program
*p
= &obj
->programs
[i
];
6776 qsort(p
->reloc_desc
, p
->nr_reloc
, sizeof(*p
->reloc_desc
), cmp_relocs
);
6781 static bool insn_is_helper_call(struct bpf_insn
*insn
, enum bpf_func_id
*func_id
)
6783 if (BPF_CLASS(insn
->code
) == BPF_JMP
&&
6784 BPF_OP(insn
->code
) == BPF_CALL
&&
6785 BPF_SRC(insn
->code
) == BPF_K
&&
6786 insn
->src_reg
== 0 &&
6787 insn
->dst_reg
== 0) {
6788 *func_id
= insn
->imm
;
6794 static int bpf_object__sanitize_prog(struct bpf_object
* obj
, struct bpf_program
*prog
)
6796 struct bpf_insn
*insn
= prog
->insns
;
6797 enum bpf_func_id func_id
;
6800 for (i
= 0; i
< prog
->insns_cnt
; i
++, insn
++) {
6801 if (!insn_is_helper_call(insn
, &func_id
))
6804 /* on kernels that don't yet support
6805 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
6806 * to bpf_probe_read() which works well for old kernels
6809 case BPF_FUNC_probe_read_kernel
:
6810 case BPF_FUNC_probe_read_user
:
6811 if (!kernel_supports(FEAT_PROBE_READ_KERN
))
6812 insn
->imm
= BPF_FUNC_probe_read
;
6814 case BPF_FUNC_probe_read_kernel_str
:
6815 case BPF_FUNC_probe_read_user_str
:
6816 if (!kernel_supports(FEAT_PROBE_READ_KERN
))
6817 insn
->imm
= BPF_FUNC_probe_read_str
;
6827 load_program(struct bpf_program
*prog
, struct bpf_insn
*insns
, int insns_cnt
,
6828 char *license
, __u32 kern_version
, int *pfd
)
6830 struct bpf_prog_load_params load_attr
= {};
6831 char *cp
, errmsg
[STRERR_BUFSIZE
];
6832 size_t log_buf_size
= 0;
6833 char *log_buf
= NULL
;
6836 if (prog
->type
== BPF_PROG_TYPE_UNSPEC
) {
6838 * The program type must be set. Most likely we couldn't find a proper
6839 * section definition at load time, and thus we didn't infer the type.
6841 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
6842 prog
->name
, prog
->sec_name
);
6846 if (!insns
|| !insns_cnt
)
6849 load_attr
.prog_type
= prog
->type
;
6850 /* old kernels might not support specifying expected_attach_type */
6851 if (!kernel_supports(FEAT_EXP_ATTACH_TYPE
) && prog
->sec_def
&&
6852 prog
->sec_def
->is_exp_attach_type_optional
)
6853 load_attr
.expected_attach_type
= 0;
6855 load_attr
.expected_attach_type
= prog
->expected_attach_type
;
6856 if (kernel_supports(FEAT_PROG_NAME
))
6857 load_attr
.name
= prog
->name
;
6858 load_attr
.insns
= insns
;
6859 load_attr
.insn_cnt
= insns_cnt
;
6860 load_attr
.license
= license
;
6861 load_attr
.attach_btf_id
= prog
->attach_btf_id
;
6862 if (prog
->attach_prog_fd
)
6863 load_attr
.attach_prog_fd
= prog
->attach_prog_fd
;
6865 load_attr
.attach_btf_obj_fd
= prog
->attach_btf_obj_fd
;
6866 load_attr
.attach_btf_id
= prog
->attach_btf_id
;
6867 load_attr
.kern_version
= kern_version
;
6868 load_attr
.prog_ifindex
= prog
->prog_ifindex
;
6870 /* specify func_info/line_info only if kernel supports them */
6871 btf_fd
= bpf_object__btf_fd(prog
->obj
);
6872 if (btf_fd
>= 0 && kernel_supports(FEAT_BTF_FUNC
)) {
6873 load_attr
.prog_btf_fd
= btf_fd
;
6874 load_attr
.func_info
= prog
->func_info
;
6875 load_attr
.func_info_rec_size
= prog
->func_info_rec_size
;
6876 load_attr
.func_info_cnt
= prog
->func_info_cnt
;
6877 load_attr
.line_info
= prog
->line_info
;
6878 load_attr
.line_info_rec_size
= prog
->line_info_rec_size
;
6879 load_attr
.line_info_cnt
= prog
->line_info_cnt
;
6881 load_attr
.log_level
= prog
->log_level
;
6882 load_attr
.prog_flags
= prog
->prog_flags
;
6886 log_buf
= malloc(log_buf_size
);
6893 load_attr
.log_buf
= log_buf
;
6894 load_attr
.log_buf_sz
= log_buf_size
;
6895 ret
= libbpf__bpf_prog_load(&load_attr
);
6898 if (log_buf
&& load_attr
.log_level
)
6899 pr_debug("verifier log:\n%s", log_buf
);
6901 if (prog
->obj
->rodata_map_idx
>= 0 &&
6902 kernel_supports(FEAT_PROG_BIND_MAP
)) {
6903 struct bpf_map
*rodata_map
=
6904 &prog
->obj
->maps
[prog
->obj
->rodata_map_idx
];
6906 if (bpf_prog_bind_map(ret
, bpf_map__fd(rodata_map
), NULL
)) {
6907 cp
= libbpf_strerror_r(errno
, errmsg
, sizeof(errmsg
));
6908 pr_warn("prog '%s': failed to bind .rodata map: %s\n",
6910 /* Don't fail hard if can't bind rodata. */
6919 if (!log_buf
|| errno
== ENOSPC
) {
6920 log_buf_size
= max((size_t)BPF_LOG_BUF_SIZE
,
6926 ret
= errno
? -errno
: -LIBBPF_ERRNO__LOAD
;
6927 cp
= libbpf_strerror_r(errno
, errmsg
, sizeof(errmsg
));
6928 pr_warn("load bpf program failed: %s\n", cp
);
6931 if (log_buf
&& log_buf
[0] != '\0') {
6932 ret
= -LIBBPF_ERRNO__VERIFY
;
6933 pr_warn("-- BEGIN DUMP LOG ---\n");
6934 pr_warn("\n%s\n", log_buf
);
6935 pr_warn("-- END LOG --\n");
6936 } else if (load_attr
.insn_cnt
>= BPF_MAXINSNS
) {
6937 pr_warn("Program too large (%zu insns), at most %d insns\n",
6938 load_attr
.insn_cnt
, BPF_MAXINSNS
);
6939 ret
= -LIBBPF_ERRNO__PROG2BIG
;
6940 } else if (load_attr
.prog_type
!= BPF_PROG_TYPE_KPROBE
) {
6941 /* Wrong program type? */
6944 load_attr
.prog_type
= BPF_PROG_TYPE_KPROBE
;
6945 load_attr
.expected_attach_type
= 0;
6946 load_attr
.log_buf
= NULL
;
6947 load_attr
.log_buf_sz
= 0;
6948 fd
= libbpf__bpf_prog_load(&load_attr
);
6951 ret
= -LIBBPF_ERRNO__PROGTYPE
;
6961 static int libbpf_find_attach_btf_id(struct bpf_program
*prog
, int *btf_obj_fd
, int *btf_type_id
);
6963 int bpf_program__load(struct bpf_program
*prog
, char *license
, __u32 kern_ver
)
6967 if (prog
->obj
->loaded
) {
6968 pr_warn("prog '%s': can't load after object was loaded\n", prog
->name
);
6972 if ((prog
->type
== BPF_PROG_TYPE_TRACING
||
6973 prog
->type
== BPF_PROG_TYPE_LSM
||
6974 prog
->type
== BPF_PROG_TYPE_EXT
) && !prog
->attach_btf_id
) {
6975 int btf_obj_fd
= 0, btf_type_id
= 0;
6977 err
= libbpf_find_attach_btf_id(prog
, &btf_obj_fd
, &btf_type_id
);
6981 prog
->attach_btf_obj_fd
= btf_obj_fd
;
6982 prog
->attach_btf_id
= btf_type_id
;
6985 if (prog
->instances
.nr
< 0 || !prog
->instances
.fds
) {
6986 if (prog
->preprocessor
) {
6987 pr_warn("Internal error: can't load program '%s'\n",
6989 return -LIBBPF_ERRNO__INTERNAL
;
6992 prog
->instances
.fds
= malloc(sizeof(int));
6993 if (!prog
->instances
.fds
) {
6994 pr_warn("Not enough memory for BPF fds\n");
6997 prog
->instances
.nr
= 1;
6998 prog
->instances
.fds
[0] = -1;
7001 if (!prog
->preprocessor
) {
7002 if (prog
->instances
.nr
!= 1) {
7003 pr_warn("prog '%s': inconsistent nr(%d) != 1\n",
7004 prog
->name
, prog
->instances
.nr
);
7006 err
= load_program(prog
, prog
->insns
, prog
->insns_cnt
,
7007 license
, kern_ver
, &fd
);
7009 prog
->instances
.fds
[0] = fd
;
7013 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
7014 struct bpf_prog_prep_result result
;
7015 bpf_program_prep_t preprocessor
= prog
->preprocessor
;
7017 memset(&result
, 0, sizeof(result
));
7018 err
= preprocessor(prog
, i
, prog
->insns
,
7019 prog
->insns_cnt
, &result
);
7021 pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
7026 if (!result
.new_insn_ptr
|| !result
.new_insn_cnt
) {
7027 pr_debug("Skip loading the %dth instance of program '%s'\n",
7029 prog
->instances
.fds
[i
] = -1;
7035 err
= load_program(prog
, result
.new_insn_ptr
,
7036 result
.new_insn_cnt
, license
, kern_ver
, &fd
);
7038 pr_warn("Loading the %dth instance of program '%s' failed\n",
7045 prog
->instances
.fds
[i
] = fd
;
7049 pr_warn("failed to load program '%s'\n", prog
->name
);
7050 zfree(&prog
->insns
);
7051 prog
->insns_cnt
= 0;
7056 bpf_object__load_progs(struct bpf_object
*obj
, int log_level
)
7058 struct bpf_program
*prog
;
7062 for (i
= 0; i
< obj
->nr_programs
; i
++) {
7063 prog
= &obj
->programs
[i
];
7064 err
= bpf_object__sanitize_prog(obj
, prog
);
7069 for (i
= 0; i
< obj
->nr_programs
; i
++) {
7070 prog
= &obj
->programs
[i
];
7071 if (prog_is_subprog(obj
, prog
))
7074 pr_debug("prog '%s': skipped loading\n", prog
->name
);
7077 prog
->log_level
|= log_level
;
7078 err
= bpf_program__load(prog
, obj
->license
, obj
->kern_version
);
7085 static const struct bpf_sec_def
*find_sec_def(const char *sec_name
);
7087 static struct bpf_object
*
7088 __bpf_object__open(const char *path
, const void *obj_buf
, size_t obj_buf_sz
,
7089 const struct bpf_object_open_opts
*opts
)
7091 const char *obj_name
, *kconfig
;
7092 struct bpf_program
*prog
;
7093 struct bpf_object
*obj
;
7097 if (elf_version(EV_CURRENT
) == EV_NONE
) {
7098 pr_warn("failed to init libelf for %s\n",
7099 path
? : "(mem buf)");
7100 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
7103 if (!OPTS_VALID(opts
, bpf_object_open_opts
))
7104 return ERR_PTR(-EINVAL
);
7106 obj_name
= OPTS_GET(opts
, object_name
, NULL
);
7109 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
7110 (unsigned long)obj_buf
,
7111 (unsigned long)obj_buf_sz
);
7112 obj_name
= tmp_name
;
7115 pr_debug("loading object '%s' from buffer\n", obj_name
);
7118 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
, obj_name
);
7122 kconfig
= OPTS_GET(opts
, kconfig
, NULL
);
7124 obj
->kconfig
= strdup(kconfig
);
7126 return ERR_PTR(-ENOMEM
);
7129 err
= bpf_object__elf_init(obj
);
7130 err
= err
? : bpf_object__check_endianness(obj
);
7131 err
= err
? : bpf_object__elf_collect(obj
);
7132 err
= err
? : bpf_object__collect_externs(obj
);
7133 err
= err
? : bpf_object__finalize_btf(obj
);
7134 err
= err
? : bpf_object__init_maps(obj
, opts
);
7135 err
= err
? : bpf_object__collect_relos(obj
);
7138 bpf_object__elf_finish(obj
);
7140 bpf_object__for_each_program(prog
, obj
) {
7141 prog
->sec_def
= find_sec_def(prog
->sec_name
);
7142 if (!prog
->sec_def
) {
7143 /* couldn't guess, but user might manually specify */
7144 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7145 prog
->name
, prog
->sec_name
);
7149 if (prog
->sec_def
->is_sleepable
)
7150 prog
->prog_flags
|= BPF_F_SLEEPABLE
;
7151 bpf_program__set_type(prog
, prog
->sec_def
->prog_type
);
7152 bpf_program__set_expected_attach_type(prog
,
7153 prog
->sec_def
->expected_attach_type
);
7155 if (prog
->sec_def
->prog_type
== BPF_PROG_TYPE_TRACING
||
7156 prog
->sec_def
->prog_type
== BPF_PROG_TYPE_EXT
)
7157 prog
->attach_prog_fd
= OPTS_GET(opts
, attach_prog_fd
, 0);
7162 bpf_object__close(obj
);
7163 return ERR_PTR(err
);
7166 static struct bpf_object
*
7167 __bpf_object__open_xattr(struct bpf_object_open_attr
*attr
, int flags
)
7169 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
7170 .relaxed_maps
= flags
& MAPS_RELAX_COMPAT
,
7173 /* param validation */
7177 pr_debug("loading %s\n", attr
->file
);
7178 return __bpf_object__open(attr
->file
, NULL
, 0, &opts
);
7181 struct bpf_object
*bpf_object__open_xattr(struct bpf_object_open_attr
*attr
)
7183 return __bpf_object__open_xattr(attr
, 0);
7186 struct bpf_object
*bpf_object__open(const char *path
)
7188 struct bpf_object_open_attr attr
= {
7190 .prog_type
= BPF_PROG_TYPE_UNSPEC
,
7193 return bpf_object__open_xattr(&attr
);
7197 bpf_object__open_file(const char *path
, const struct bpf_object_open_opts
*opts
)
7200 return ERR_PTR(-EINVAL
);
7202 pr_debug("loading %s\n", path
);
7204 return __bpf_object__open(path
, NULL
, 0, opts
);
7208 bpf_object__open_mem(const void *obj_buf
, size_t obj_buf_sz
,
7209 const struct bpf_object_open_opts
*opts
)
7211 if (!obj_buf
|| obj_buf_sz
== 0)
7212 return ERR_PTR(-EINVAL
);
7214 return __bpf_object__open(NULL
, obj_buf
, obj_buf_sz
, opts
);
7218 bpf_object__open_buffer(const void *obj_buf
, size_t obj_buf_sz
,
7221 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
7222 .object_name
= name
,
7223 /* wrong default, but backwards-compatible */
7224 .relaxed_maps
= true,
7227 /* returning NULL is wrong, but backwards-compatible */
7228 if (!obj_buf
|| obj_buf_sz
== 0)
7231 return bpf_object__open_mem(obj_buf
, obj_buf_sz
, &opts
);
7234 int bpf_object__unload(struct bpf_object
*obj
)
7241 for (i
= 0; i
< obj
->nr_maps
; i
++) {
7242 zclose(obj
->maps
[i
].fd
);
7243 if (obj
->maps
[i
].st_ops
)
7244 zfree(&obj
->maps
[i
].st_ops
->kern_vdata
);
7247 for (i
= 0; i
< obj
->nr_programs
; i
++)
7248 bpf_program__unload(&obj
->programs
[i
]);
7253 static int bpf_object__sanitize_maps(struct bpf_object
*obj
)
7257 bpf_object__for_each_map(m
, obj
) {
7258 if (!bpf_map__is_internal(m
))
7260 if (!kernel_supports(FEAT_GLOBAL_DATA
)) {
7261 pr_warn("kernel doesn't support global data\n");
7264 if (!kernel_supports(FEAT_ARRAY_MMAP
))
7265 m
->def
.map_flags
^= BPF_F_MMAPABLE
;
7271 static int bpf_object__read_kallsyms_file(struct bpf_object
*obj
)
7273 char sym_type
, sym_name
[500];
7274 unsigned long long sym_addr
;
7275 struct extern_desc
*ext
;
7279 f
= fopen("/proc/kallsyms", "r");
7282 pr_warn("failed to open /proc/kallsyms: %d\n", err
);
7287 ret
= fscanf(f
, "%llx %c %499s%*[^\n]\n",
7288 &sym_addr
, &sym_type
, sym_name
);
7289 if (ret
== EOF
&& feof(f
))
7292 pr_warn("failed to read kallsyms entry: %d\n", ret
);
7297 ext
= find_extern_by_name(obj
, sym_name
);
7298 if (!ext
|| ext
->type
!= EXT_KSYM
)
7301 if (ext
->is_set
&& ext
->ksym
.addr
!= sym_addr
) {
7302 pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n",
7303 sym_name
, ext
->ksym
.addr
, sym_addr
);
7309 ext
->ksym
.addr
= sym_addr
;
7310 pr_debug("extern (ksym) %s=0x%llx\n", sym_name
, sym_addr
);
7319 static int bpf_object__resolve_ksyms_btf_id(struct bpf_object
*obj
)
7321 struct extern_desc
*ext
;
7324 for (i
= 0; i
< obj
->nr_extern
; i
++) {
7325 const struct btf_type
*targ_var
, *targ_type
;
7326 __u32 targ_type_id
, local_type_id
;
7327 const char *targ_var_name
;
7330 ext
= &obj
->externs
[i
];
7331 if (ext
->type
!= EXT_KSYM
|| !ext
->ksym
.type_id
)
7334 id
= btf__find_by_name_kind(obj
->btf_vmlinux
, ext
->name
,
7337 pr_warn("extern (ksym) '%s': failed to find BTF ID in vmlinux BTF.\n",
7342 /* find local type_id */
7343 local_type_id
= ext
->ksym
.type_id
;
7345 /* find target type_id */
7346 targ_var
= btf__type_by_id(obj
->btf_vmlinux
, id
);
7347 targ_var_name
= btf__name_by_offset(obj
->btf_vmlinux
,
7348 targ_var
->name_off
);
7349 targ_type
= skip_mods_and_typedefs(obj
->btf_vmlinux
,
7353 ret
= bpf_core_types_are_compat(obj
->btf
, local_type_id
,
7354 obj
->btf_vmlinux
, targ_type_id
);
7356 const struct btf_type
*local_type
;
7357 const char *targ_name
, *local_name
;
7359 local_type
= btf__type_by_id(obj
->btf
, local_type_id
);
7360 local_name
= btf__name_by_offset(obj
->btf
,
7361 local_type
->name_off
);
7362 targ_name
= btf__name_by_offset(obj
->btf_vmlinux
,
7363 targ_type
->name_off
);
7365 pr_warn("extern (ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
7366 ext
->name
, local_type_id
,
7367 btf_kind_str(local_type
), local_name
, targ_type_id
,
7368 btf_kind_str(targ_type
), targ_name
);
7373 ext
->ksym
.vmlinux_btf_id
= id
;
7374 pr_debug("extern (ksym) '%s': resolved to [%d] %s %s\n",
7375 ext
->name
, id
, btf_kind_str(targ_var
), targ_var_name
);
7380 static int bpf_object__resolve_externs(struct bpf_object
*obj
,
7381 const char *extra_kconfig
)
7383 bool need_config
= false, need_kallsyms
= false;
7384 bool need_vmlinux_btf
= false;
7385 struct extern_desc
*ext
;
7386 void *kcfg_data
= NULL
;
7389 if (obj
->nr_extern
== 0)
7392 if (obj
->kconfig_map_idx
>= 0)
7393 kcfg_data
= obj
->maps
[obj
->kconfig_map_idx
].mmaped
;
7395 for (i
= 0; i
< obj
->nr_extern
; i
++) {
7396 ext
= &obj
->externs
[i
];
7398 if (ext
->type
== EXT_KCFG
&&
7399 strcmp(ext
->name
, "LINUX_KERNEL_VERSION") == 0) {
7400 void *ext_val
= kcfg_data
+ ext
->kcfg
.data_off
;
7401 __u32 kver
= get_kernel_version();
7404 pr_warn("failed to get kernel version\n");
7407 err
= set_kcfg_value_num(ext
, ext_val
, kver
);
7410 pr_debug("extern (kcfg) %s=0x%x\n", ext
->name
, kver
);
7411 } else if (ext
->type
== EXT_KCFG
&&
7412 strncmp(ext
->name
, "CONFIG_", 7) == 0) {
7414 } else if (ext
->type
== EXT_KSYM
) {
7415 if (ext
->ksym
.type_id
)
7416 need_vmlinux_btf
= true;
7418 need_kallsyms
= true;
7420 pr_warn("unrecognized extern '%s'\n", ext
->name
);
7424 if (need_config
&& extra_kconfig
) {
7425 err
= bpf_object__read_kconfig_mem(obj
, extra_kconfig
, kcfg_data
);
7428 need_config
= false;
7429 for (i
= 0; i
< obj
->nr_extern
; i
++) {
7430 ext
= &obj
->externs
[i
];
7431 if (ext
->type
== EXT_KCFG
&& !ext
->is_set
) {
7438 err
= bpf_object__read_kconfig_file(obj
, kcfg_data
);
7442 if (need_kallsyms
) {
7443 err
= bpf_object__read_kallsyms_file(obj
);
7447 if (need_vmlinux_btf
) {
7448 err
= bpf_object__resolve_ksyms_btf_id(obj
);
7452 for (i
= 0; i
< obj
->nr_extern
; i
++) {
7453 ext
= &obj
->externs
[i
];
7455 if (!ext
->is_set
&& !ext
->is_weak
) {
7456 pr_warn("extern %s (strong) not resolved\n", ext
->name
);
7458 } else if (!ext
->is_set
) {
7459 pr_debug("extern %s (weak) not resolved, defaulting to zero\n",
7467 int bpf_object__load_xattr(struct bpf_object_load_attr
*attr
)
7469 struct bpf_object
*obj
;
7479 pr_warn("object '%s': load can't be attempted twice\n", obj
->name
);
7483 err
= bpf_object__probe_loading(obj
);
7484 err
= err
? : bpf_object__load_vmlinux_btf(obj
, false);
7485 err
= err
? : bpf_object__resolve_externs(obj
, obj
->kconfig
);
7486 err
= err
? : bpf_object__sanitize_and_load_btf(obj
);
7487 err
= err
? : bpf_object__sanitize_maps(obj
);
7488 err
= err
? : bpf_object__init_kern_struct_ops_maps(obj
);
7489 err
= err
? : bpf_object__create_maps(obj
);
7490 err
= err
? : bpf_object__relocate(obj
, attr
->target_btf_path
);
7491 err
= err
? : bpf_object__load_progs(obj
, attr
->log_level
);
7493 /* clean up module BTFs */
7494 for (i
= 0; i
< obj
->btf_module_cnt
; i
++) {
7495 close(obj
->btf_modules
[i
].fd
);
7496 btf__free(obj
->btf_modules
[i
].btf
);
7497 free(obj
->btf_modules
[i
].name
);
7499 free(obj
->btf_modules
);
7501 /* clean up vmlinux BTF */
7502 btf__free(obj
->btf_vmlinux
);
7503 obj
->btf_vmlinux
= NULL
;
7505 obj
->loaded
= true; /* doesn't matter if successfully or not */
7512 /* unpin any maps that were auto-pinned during load */
7513 for (i
= 0; i
< obj
->nr_maps
; i
++)
7514 if (obj
->maps
[i
].pinned
&& !obj
->maps
[i
].reused
)
7515 bpf_map__unpin(&obj
->maps
[i
], NULL
);
7517 bpf_object__unload(obj
);
7518 pr_warn("failed to load object '%s'\n", obj
->path
);
7522 int bpf_object__load(struct bpf_object
*obj
)
7524 struct bpf_object_load_attr attr
= {
7528 return bpf_object__load_xattr(&attr
);
7531 static int make_parent_dir(const char *path
)
7533 char *cp
, errmsg
[STRERR_BUFSIZE
];
7537 dname
= strdup(path
);
7541 dir
= dirname(dname
);
7542 if (mkdir(dir
, 0700) && errno
!= EEXIST
)
7547 cp
= libbpf_strerror_r(-err
, errmsg
, sizeof(errmsg
));
7548 pr_warn("failed to mkdir %s: %s\n", path
, cp
);
7553 static int check_path(const char *path
)
7555 char *cp
, errmsg
[STRERR_BUFSIZE
];
7556 struct statfs st_fs
;
7563 dname
= strdup(path
);
7567 dir
= dirname(dname
);
7568 if (statfs(dir
, &st_fs
)) {
7569 cp
= libbpf_strerror_r(errno
, errmsg
, sizeof(errmsg
));
7570 pr_warn("failed to statfs %s: %s\n", dir
, cp
);
7575 if (!err
&& st_fs
.f_type
!= BPF_FS_MAGIC
) {
7576 pr_warn("specified path %s is not on BPF FS\n", path
);
7583 int bpf_program__pin_instance(struct bpf_program
*prog
, const char *path
,
7586 char *cp
, errmsg
[STRERR_BUFSIZE
];
7589 err
= make_parent_dir(path
);
7593 err
= check_path(path
);
7598 pr_warn("invalid program pointer\n");
7602 if (instance
< 0 || instance
>= prog
->instances
.nr
) {
7603 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
7604 instance
, prog
->name
, prog
->instances
.nr
);
7608 if (bpf_obj_pin(prog
->instances
.fds
[instance
], path
)) {
7610 cp
= libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
));
7611 pr_warn("failed to pin program: %s\n", cp
);
7614 pr_debug("pinned program '%s'\n", path
);
7619 int bpf_program__unpin_instance(struct bpf_program
*prog
, const char *path
,
7624 err
= check_path(path
);
7629 pr_warn("invalid program pointer\n");
7633 if (instance
< 0 || instance
>= prog
->instances
.nr
) {
7634 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
7635 instance
, prog
->name
, prog
->instances
.nr
);
7642 pr_debug("unpinned program '%s'\n", path
);
7647 int bpf_program__pin(struct bpf_program
*prog
, const char *path
)
7651 err
= make_parent_dir(path
);
7655 err
= check_path(path
);
7660 pr_warn("invalid program pointer\n");
7664 if (prog
->instances
.nr
<= 0) {
7665 pr_warn("no instances of prog %s to pin\n", prog
->name
);
7669 if (prog
->instances
.nr
== 1) {
7670 /* don't create subdirs when pinning single instance */
7671 return bpf_program__pin_instance(prog
, path
, 0);
7674 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
7678 len
= snprintf(buf
, PATH_MAX
, "%s/%d", path
, i
);
7682 } else if (len
>= PATH_MAX
) {
7683 err
= -ENAMETOOLONG
;
7687 err
= bpf_program__pin_instance(prog
, buf
, i
);
7695 for (i
= i
- 1; i
>= 0; i
--) {
7699 len
= snprintf(buf
, PATH_MAX
, "%s/%d", path
, i
);
7702 else if (len
>= PATH_MAX
)
7705 bpf_program__unpin_instance(prog
, buf
, i
);
7713 int bpf_program__unpin(struct bpf_program
*prog
, const char *path
)
7717 err
= check_path(path
);
7722 pr_warn("invalid program pointer\n");
7726 if (prog
->instances
.nr
<= 0) {
7727 pr_warn("no instances of prog %s to pin\n", prog
->name
);
7731 if (prog
->instances
.nr
== 1) {
7732 /* don't create subdirs when pinning single instance */
7733 return bpf_program__unpin_instance(prog
, path
, 0);
7736 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
7740 len
= snprintf(buf
, PATH_MAX
, "%s/%d", path
, i
);
7743 else if (len
>= PATH_MAX
)
7744 return -ENAMETOOLONG
;
7746 err
= bpf_program__unpin_instance(prog
, buf
, i
);
7758 int bpf_map__pin(struct bpf_map
*map
, const char *path
)
7760 char *cp
, errmsg
[STRERR_BUFSIZE
];
7764 pr_warn("invalid map pointer\n");
7768 if (map
->pin_path
) {
7769 if (path
&& strcmp(path
, map
->pin_path
)) {
7770 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7771 bpf_map__name(map
), map
->pin_path
, path
);
7773 } else if (map
->pinned
) {
7774 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
7775 bpf_map__name(map
), map
->pin_path
);
7780 pr_warn("missing a path to pin map '%s' at\n",
7781 bpf_map__name(map
));
7783 } else if (map
->pinned
) {
7784 pr_warn("map '%s' already pinned\n", bpf_map__name(map
));
7788 map
->pin_path
= strdup(path
);
7789 if (!map
->pin_path
) {
7795 err
= make_parent_dir(map
->pin_path
);
7799 err
= check_path(map
->pin_path
);
7803 if (bpf_obj_pin(map
->fd
, map
->pin_path
)) {
7809 pr_debug("pinned map '%s'\n", map
->pin_path
);
7814 cp
= libbpf_strerror_r(-err
, errmsg
, sizeof(errmsg
));
7815 pr_warn("failed to pin map: %s\n", cp
);
7819 int bpf_map__unpin(struct bpf_map
*map
, const char *path
)
7824 pr_warn("invalid map pointer\n");
7828 if (map
->pin_path
) {
7829 if (path
&& strcmp(path
, map
->pin_path
)) {
7830 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7831 bpf_map__name(map
), map
->pin_path
, path
);
7834 path
= map
->pin_path
;
7836 pr_warn("no path to unpin map '%s' from\n",
7837 bpf_map__name(map
));
7841 err
= check_path(path
);
7849 map
->pinned
= false;
7850 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map
), path
);
7855 int bpf_map__set_pin_path(struct bpf_map
*map
, const char *path
)
7865 free(map
->pin_path
);
7866 map
->pin_path
= new;
7870 const char *bpf_map__get_pin_path(const struct bpf_map
*map
)
7872 return map
->pin_path
;
7875 bool bpf_map__is_pinned(const struct bpf_map
*map
)
7880 static void sanitize_pin_path(char *s
)
7882 /* bpffs disallows periods in path names */
7890 int bpf_object__pin_maps(struct bpf_object
*obj
, const char *path
)
7892 struct bpf_map
*map
;
7899 pr_warn("object not yet loaded; load it first\n");
7903 bpf_object__for_each_map(map
, obj
) {
7904 char *pin_path
= NULL
;
7910 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
7911 bpf_map__name(map
));
7914 goto err_unpin_maps
;
7915 } else if (len
>= PATH_MAX
) {
7916 err
= -ENAMETOOLONG
;
7917 goto err_unpin_maps
;
7919 sanitize_pin_path(buf
);
7921 } else if (!map
->pin_path
) {
7925 err
= bpf_map__pin(map
, pin_path
);
7927 goto err_unpin_maps
;
7933 while ((map
= bpf_map__prev(map
, obj
))) {
7937 bpf_map__unpin(map
, NULL
);
7943 int bpf_object__unpin_maps(struct bpf_object
*obj
, const char *path
)
7945 struct bpf_map
*map
;
7951 bpf_object__for_each_map(map
, obj
) {
7952 char *pin_path
= NULL
;
7958 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
7959 bpf_map__name(map
));
7962 else if (len
>= PATH_MAX
)
7963 return -ENAMETOOLONG
;
7964 sanitize_pin_path(buf
);
7966 } else if (!map
->pin_path
) {
7970 err
= bpf_map__unpin(map
, pin_path
);
7978 int bpf_object__pin_programs(struct bpf_object
*obj
, const char *path
)
7980 struct bpf_program
*prog
;
7987 pr_warn("object not yet loaded; load it first\n");
7991 bpf_object__for_each_program(prog
, obj
) {
7995 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
7999 goto err_unpin_programs
;
8000 } else if (len
>= PATH_MAX
) {
8001 err
= -ENAMETOOLONG
;
8002 goto err_unpin_programs
;
8005 err
= bpf_program__pin(prog
, buf
);
8007 goto err_unpin_programs
;
8013 while ((prog
= bpf_program__prev(prog
, obj
))) {
8017 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
8021 else if (len
>= PATH_MAX
)
8024 bpf_program__unpin(prog
, buf
);
8030 int bpf_object__unpin_programs(struct bpf_object
*obj
, const char *path
)
8032 struct bpf_program
*prog
;
8038 bpf_object__for_each_program(prog
, obj
) {
8042 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
8046 else if (len
>= PATH_MAX
)
8047 return -ENAMETOOLONG
;
8049 err
= bpf_program__unpin(prog
, buf
);
8057 int bpf_object__pin(struct bpf_object
*obj
, const char *path
)
8061 err
= bpf_object__pin_maps(obj
, path
);
8065 err
= bpf_object__pin_programs(obj
, path
);
8067 bpf_object__unpin_maps(obj
, path
);
8074 static void bpf_map__destroy(struct bpf_map
*map
)
8076 if (map
->clear_priv
)
8077 map
->clear_priv(map
, map
->priv
);
8079 map
->clear_priv
= NULL
;
8081 if (map
->inner_map
) {
8082 bpf_map__destroy(map
->inner_map
);
8083 zfree(&map
->inner_map
);
8086 zfree(&map
->init_slots
);
8087 map
->init_slots_sz
= 0;
8090 munmap(map
->mmaped
, bpf_map_mmap_sz(map
));
8095 zfree(&map
->st_ops
->data
);
8096 zfree(&map
->st_ops
->progs
);
8097 zfree(&map
->st_ops
->kern_func_off
);
8098 zfree(&map
->st_ops
);
8102 zfree(&map
->pin_path
);
8108 void bpf_object__close(struct bpf_object
*obj
)
8112 if (IS_ERR_OR_NULL(obj
))
8115 if (obj
->clear_priv
)
8116 obj
->clear_priv(obj
, obj
->priv
);
8118 bpf_object__elf_finish(obj
);
8119 bpf_object__unload(obj
);
8120 btf__free(obj
->btf
);
8121 btf_ext__free(obj
->btf_ext
);
8123 for (i
= 0; i
< obj
->nr_maps
; i
++)
8124 bpf_map__destroy(&obj
->maps
[i
]);
8126 zfree(&obj
->kconfig
);
8127 zfree(&obj
->externs
);
8133 if (obj
->programs
&& obj
->nr_programs
) {
8134 for (i
= 0; i
< obj
->nr_programs
; i
++)
8135 bpf_program__exit(&obj
->programs
[i
]);
8137 zfree(&obj
->programs
);
8139 list_del(&obj
->list
);
8144 bpf_object__next(struct bpf_object
*prev
)
8146 struct bpf_object
*next
;
8149 next
= list_first_entry(&bpf_objects_list
,
8153 next
= list_next_entry(prev
, list
);
8155 /* Empty list is noticed here so don't need checking on entry. */
8156 if (&next
->list
== &bpf_objects_list
)
8162 const char *bpf_object__name(const struct bpf_object
*obj
)
8164 return obj
? obj
->name
: ERR_PTR(-EINVAL
);
8167 unsigned int bpf_object__kversion(const struct bpf_object
*obj
)
8169 return obj
? obj
->kern_version
: 0;
8172 struct btf
*bpf_object__btf(const struct bpf_object
*obj
)
8174 return obj
? obj
->btf
: NULL
;
8177 int bpf_object__btf_fd(const struct bpf_object
*obj
)
8179 return obj
->btf
? btf__fd(obj
->btf
) : -1;
8182 int bpf_object__set_priv(struct bpf_object
*obj
, void *priv
,
8183 bpf_object_clear_priv_t clear_priv
)
8185 if (obj
->priv
&& obj
->clear_priv
)
8186 obj
->clear_priv(obj
, obj
->priv
);
8189 obj
->clear_priv
= clear_priv
;
8193 void *bpf_object__priv(const struct bpf_object
*obj
)
8195 return obj
? obj
->priv
: ERR_PTR(-EINVAL
);
8198 static struct bpf_program
*
8199 __bpf_program__iter(const struct bpf_program
*p
, const struct bpf_object
*obj
,
8202 size_t nr_programs
= obj
->nr_programs
;
8209 /* Iter from the beginning */
8210 return forward
? &obj
->programs
[0] :
8211 &obj
->programs
[nr_programs
- 1];
8213 if (p
->obj
!= obj
) {
8214 pr_warn("error: program handler doesn't match object\n");
8218 idx
= (p
- obj
->programs
) + (forward
? 1 : -1);
8219 if (idx
>= obj
->nr_programs
|| idx
< 0)
8221 return &obj
->programs
[idx
];
8224 struct bpf_program
*
8225 bpf_program__next(struct bpf_program
*prev
, const struct bpf_object
*obj
)
8227 struct bpf_program
*prog
= prev
;
8230 prog
= __bpf_program__iter(prog
, obj
, true);
8231 } while (prog
&& prog_is_subprog(obj
, prog
));
8236 struct bpf_program
*
8237 bpf_program__prev(struct bpf_program
*next
, const struct bpf_object
*obj
)
8239 struct bpf_program
*prog
= next
;
8242 prog
= __bpf_program__iter(prog
, obj
, false);
8243 } while (prog
&& prog_is_subprog(obj
, prog
));
8248 int bpf_program__set_priv(struct bpf_program
*prog
, void *priv
,
8249 bpf_program_clear_priv_t clear_priv
)
8251 if (prog
->priv
&& prog
->clear_priv
)
8252 prog
->clear_priv(prog
, prog
->priv
);
8255 prog
->clear_priv
= clear_priv
;
8259 void *bpf_program__priv(const struct bpf_program
*prog
)
8261 return prog
? prog
->priv
: ERR_PTR(-EINVAL
);
8264 void bpf_program__set_ifindex(struct bpf_program
*prog
, __u32 ifindex
)
8266 prog
->prog_ifindex
= ifindex
;
8269 const char *bpf_program__name(const struct bpf_program
*prog
)
8274 const char *bpf_program__section_name(const struct bpf_program
*prog
)
8276 return prog
->sec_name
;
8279 const char *bpf_program__title(const struct bpf_program
*prog
, bool needs_copy
)
8283 title
= prog
->sec_name
;
8285 title
= strdup(title
);
8287 pr_warn("failed to strdup program title\n");
8288 return ERR_PTR(-ENOMEM
);
8295 bool bpf_program__autoload(const struct bpf_program
*prog
)
8300 int bpf_program__set_autoload(struct bpf_program
*prog
, bool autoload
)
8302 if (prog
->obj
->loaded
)
8305 prog
->load
= autoload
;
8309 int bpf_program__fd(const struct bpf_program
*prog
)
8311 return bpf_program__nth_fd(prog
, 0);
8314 size_t bpf_program__size(const struct bpf_program
*prog
)
8316 return prog
->insns_cnt
* BPF_INSN_SZ
;
8319 int bpf_program__set_prep(struct bpf_program
*prog
, int nr_instances
,
8320 bpf_program_prep_t prep
)
8324 if (nr_instances
<= 0 || !prep
)
8327 if (prog
->instances
.nr
> 0 || prog
->instances
.fds
) {
8328 pr_warn("Can't set pre-processor after loading\n");
8332 instances_fds
= malloc(sizeof(int) * nr_instances
);
8333 if (!instances_fds
) {
8334 pr_warn("alloc memory failed for fds\n");
8338 /* fill all fd with -1 */
8339 memset(instances_fds
, -1, sizeof(int) * nr_instances
);
8341 prog
->instances
.nr
= nr_instances
;
8342 prog
->instances
.fds
= instances_fds
;
8343 prog
->preprocessor
= prep
;
8347 int bpf_program__nth_fd(const struct bpf_program
*prog
, int n
)
8354 if (n
>= prog
->instances
.nr
|| n
< 0) {
8355 pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
8356 n
, prog
->name
, prog
->instances
.nr
);
8360 fd
= prog
->instances
.fds
[n
];
8362 pr_warn("%dth instance of program '%s' is invalid\n",
8370 enum bpf_prog_type
bpf_program__get_type(struct bpf_program
*prog
)
8375 void bpf_program__set_type(struct bpf_program
*prog
, enum bpf_prog_type type
)
8380 static bool bpf_program__is_type(const struct bpf_program
*prog
,
8381 enum bpf_prog_type type
)
8383 return prog
? (prog
->type
== type
) : false;
8386 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
8387 int bpf_program__set_##NAME(struct bpf_program *prog) \
8391 bpf_program__set_type(prog, TYPE); \
8395 bool bpf_program__is_##NAME(const struct bpf_program *prog) \
8397 return bpf_program__is_type(prog, TYPE); \
8400 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
8401 BPF_PROG_TYPE_FNS(lsm
, BPF_PROG_TYPE_LSM
);
8402 BPF_PROG_TYPE_FNS(kprobe
, BPF_PROG_TYPE_KPROBE
);
8403 BPF_PROG_TYPE_FNS(sched_cls
, BPF_PROG_TYPE_SCHED_CLS
);
8404 BPF_PROG_TYPE_FNS(sched_act
, BPF_PROG_TYPE_SCHED_ACT
);
8405 BPF_PROG_TYPE_FNS(tracepoint
, BPF_PROG_TYPE_TRACEPOINT
);
8406 BPF_PROG_TYPE_FNS(raw_tracepoint
, BPF_PROG_TYPE_RAW_TRACEPOINT
);
8407 BPF_PROG_TYPE_FNS(xdp
, BPF_PROG_TYPE_XDP
);
8408 BPF_PROG_TYPE_FNS(perf_event
, BPF_PROG_TYPE_PERF_EVENT
);
8409 BPF_PROG_TYPE_FNS(tracing
, BPF_PROG_TYPE_TRACING
);
8410 BPF_PROG_TYPE_FNS(struct_ops
, BPF_PROG_TYPE_STRUCT_OPS
);
8411 BPF_PROG_TYPE_FNS(extension
, BPF_PROG_TYPE_EXT
);
8412 BPF_PROG_TYPE_FNS(sk_lookup
, BPF_PROG_TYPE_SK_LOOKUP
);
8414 enum bpf_attach_type
8415 bpf_program__get_expected_attach_type(struct bpf_program
*prog
)
8417 return prog
->expected_attach_type
;
8420 void bpf_program__set_expected_attach_type(struct bpf_program
*prog
,
8421 enum bpf_attach_type type
)
8423 prog
->expected_attach_type
= type
;
8426 #define BPF_PROG_SEC_IMPL(string, ptype, eatype, eatype_optional, \
8427 attachable, attach_btf) \
8430 .len = sizeof(string) - 1, \
8431 .prog_type = ptype, \
8432 .expected_attach_type = eatype, \
8433 .is_exp_attach_type_optional = eatype_optional, \
8434 .is_attachable = attachable, \
8435 .is_attach_btf = attach_btf, \
8438 /* Programs that can NOT be attached. */
8439 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0)
8441 /* Programs that can be attached. */
8442 #define BPF_APROG_SEC(string, ptype, atype) \
8443 BPF_PROG_SEC_IMPL(string, ptype, atype, true, 1, 0)
8445 /* Programs that must specify expected attach type at load time. */
8446 #define BPF_EAPROG_SEC(string, ptype, eatype) \
8447 BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 1, 0)
8449 /* Programs that use BTF to identify attach point */
8450 #define BPF_PROG_BTF(string, ptype, eatype) \
8451 BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 0, 1)
8453 /* Programs that can be attached but attach type can't be identified by section
8454 * name. Kept for backward compatibility.
8456 #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
8458 #define SEC_DEF(sec_pfx, ptype, ...) { \
8460 .len = sizeof(sec_pfx) - 1, \
8461 .prog_type = BPF_PROG_TYPE_##ptype, \
8465 static struct bpf_link
*attach_kprobe(const struct bpf_sec_def
*sec
,
8466 struct bpf_program
*prog
);
8467 static struct bpf_link
*attach_tp(const struct bpf_sec_def
*sec
,
8468 struct bpf_program
*prog
);
8469 static struct bpf_link
*attach_raw_tp(const struct bpf_sec_def
*sec
,
8470 struct bpf_program
*prog
);
8471 static struct bpf_link
*attach_trace(const struct bpf_sec_def
*sec
,
8472 struct bpf_program
*prog
);
8473 static struct bpf_link
*attach_lsm(const struct bpf_sec_def
*sec
,
8474 struct bpf_program
*prog
);
8475 static struct bpf_link
*attach_iter(const struct bpf_sec_def
*sec
,
8476 struct bpf_program
*prog
);
8478 static const struct bpf_sec_def section_defs
[] = {
8479 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER
),
8480 BPF_PROG_SEC("sk_reuseport", BPF_PROG_TYPE_SK_REUSEPORT
),
8481 SEC_DEF("kprobe/", KPROBE
,
8482 .attach_fn
= attach_kprobe
),
8483 BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE
),
8484 SEC_DEF("kretprobe/", KPROBE
,
8485 .attach_fn
= attach_kprobe
),
8486 BPF_PROG_SEC("uretprobe/", BPF_PROG_TYPE_KPROBE
),
8487 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS
),
8488 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT
),
8489 SEC_DEF("tracepoint/", TRACEPOINT
,
8490 .attach_fn
= attach_tp
),
8491 SEC_DEF("tp/", TRACEPOINT
,
8492 .attach_fn
= attach_tp
),
8493 SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT
,
8494 .attach_fn
= attach_raw_tp
),
8495 SEC_DEF("raw_tp/", RAW_TRACEPOINT
,
8496 .attach_fn
= attach_raw_tp
),
8497 SEC_DEF("tp_btf/", TRACING
,
8498 .expected_attach_type
= BPF_TRACE_RAW_TP
,
8499 .is_attach_btf
= true,
8500 .attach_fn
= attach_trace
),
8501 SEC_DEF("fentry/", TRACING
,
8502 .expected_attach_type
= BPF_TRACE_FENTRY
,
8503 .is_attach_btf
= true,
8504 .attach_fn
= attach_trace
),
8505 SEC_DEF("fmod_ret/", TRACING
,
8506 .expected_attach_type
= BPF_MODIFY_RETURN
,
8507 .is_attach_btf
= true,
8508 .attach_fn
= attach_trace
),
8509 SEC_DEF("fexit/", TRACING
,
8510 .expected_attach_type
= BPF_TRACE_FEXIT
,
8511 .is_attach_btf
= true,
8512 .attach_fn
= attach_trace
),
8513 SEC_DEF("fentry.s/", TRACING
,
8514 .expected_attach_type
= BPF_TRACE_FENTRY
,
8515 .is_attach_btf
= true,
8516 .is_sleepable
= true,
8517 .attach_fn
= attach_trace
),
8518 SEC_DEF("fmod_ret.s/", TRACING
,
8519 .expected_attach_type
= BPF_MODIFY_RETURN
,
8520 .is_attach_btf
= true,
8521 .is_sleepable
= true,
8522 .attach_fn
= attach_trace
),
8523 SEC_DEF("fexit.s/", TRACING
,
8524 .expected_attach_type
= BPF_TRACE_FEXIT
,
8525 .is_attach_btf
= true,
8526 .is_sleepable
= true,
8527 .attach_fn
= attach_trace
),
8528 SEC_DEF("freplace/", EXT
,
8529 .is_attach_btf
= true,
8530 .attach_fn
= attach_trace
),
8531 SEC_DEF("lsm/", LSM
,
8532 .is_attach_btf
= true,
8533 .expected_attach_type
= BPF_LSM_MAC
,
8534 .attach_fn
= attach_lsm
),
8535 SEC_DEF("lsm.s/", LSM
,
8536 .is_attach_btf
= true,
8537 .is_sleepable
= true,
8538 .expected_attach_type
= BPF_LSM_MAC
,
8539 .attach_fn
= attach_lsm
),
8540 SEC_DEF("iter/", TRACING
,
8541 .expected_attach_type
= BPF_TRACE_ITER
,
8542 .is_attach_btf
= true,
8543 .attach_fn
= attach_iter
),
8544 BPF_EAPROG_SEC("xdp_devmap/", BPF_PROG_TYPE_XDP
,
8546 BPF_EAPROG_SEC("xdp_cpumap/", BPF_PROG_TYPE_XDP
,
8548 BPF_APROG_SEC("xdp", BPF_PROG_TYPE_XDP
,
8550 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT
),
8551 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN
),
8552 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT
),
8553 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT
),
8554 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL
),
8555 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB
,
8556 BPF_CGROUP_INET_INGRESS
),
8557 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB
,
8558 BPF_CGROUP_INET_EGRESS
),
8559 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB
),
8560 BPF_EAPROG_SEC("cgroup/sock_create", BPF_PROG_TYPE_CGROUP_SOCK
,
8561 BPF_CGROUP_INET_SOCK_CREATE
),
8562 BPF_EAPROG_SEC("cgroup/sock_release", BPF_PROG_TYPE_CGROUP_SOCK
,
8563 BPF_CGROUP_INET_SOCK_RELEASE
),
8564 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK
,
8565 BPF_CGROUP_INET_SOCK_CREATE
),
8566 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK
,
8567 BPF_CGROUP_INET4_POST_BIND
),
8568 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK
,
8569 BPF_CGROUP_INET6_POST_BIND
),
8570 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE
,
8572 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS
,
8573 BPF_CGROUP_SOCK_OPS
),
8574 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB
,
8575 BPF_SK_SKB_STREAM_PARSER
),
8576 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB
,
8577 BPF_SK_SKB_STREAM_VERDICT
),
8578 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB
),
8579 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG
,
8580 BPF_SK_MSG_VERDICT
),
8581 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2
,
8583 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR
,
8584 BPF_FLOW_DISSECTOR
),
8585 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8586 BPF_CGROUP_INET4_BIND
),
8587 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8588 BPF_CGROUP_INET6_BIND
),
8589 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8590 BPF_CGROUP_INET4_CONNECT
),
8591 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8592 BPF_CGROUP_INET6_CONNECT
),
8593 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8594 BPF_CGROUP_UDP4_SENDMSG
),
8595 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8596 BPF_CGROUP_UDP6_SENDMSG
),
8597 BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8598 BPF_CGROUP_UDP4_RECVMSG
),
8599 BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8600 BPF_CGROUP_UDP6_RECVMSG
),
8601 BPF_EAPROG_SEC("cgroup/getpeername4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8602 BPF_CGROUP_INET4_GETPEERNAME
),
8603 BPF_EAPROG_SEC("cgroup/getpeername6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8604 BPF_CGROUP_INET6_GETPEERNAME
),
8605 BPF_EAPROG_SEC("cgroup/getsockname4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8606 BPF_CGROUP_INET4_GETSOCKNAME
),
8607 BPF_EAPROG_SEC("cgroup/getsockname6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR
,
8608 BPF_CGROUP_INET6_GETSOCKNAME
),
8609 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL
,
8611 BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT
,
8612 BPF_CGROUP_GETSOCKOPT
),
8613 BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT
,
8614 BPF_CGROUP_SETSOCKOPT
),
8615 BPF_PROG_SEC("struct_ops", BPF_PROG_TYPE_STRUCT_OPS
),
8616 BPF_EAPROG_SEC("sk_lookup/", BPF_PROG_TYPE_SK_LOOKUP
,
8620 #undef BPF_PROG_SEC_IMPL
8622 #undef BPF_APROG_SEC
8623 #undef BPF_EAPROG_SEC
8624 #undef BPF_APROG_COMPAT
8627 #define MAX_TYPE_NAME_SIZE 32
8629 static const struct bpf_sec_def
*find_sec_def(const char *sec_name
)
8631 int i
, n
= ARRAY_SIZE(section_defs
);
8633 for (i
= 0; i
< n
; i
++) {
8634 if (strncmp(sec_name
,
8635 section_defs
[i
].sec
, section_defs
[i
].len
))
8637 return §ion_defs
[i
];
8642 static char *libbpf_get_type_names(bool attach_type
)
8644 int i
, len
= ARRAY_SIZE(section_defs
) * MAX_TYPE_NAME_SIZE
;
8652 /* Forge string buf with all available names */
8653 for (i
= 0; i
< ARRAY_SIZE(section_defs
); i
++) {
8654 if (attach_type
&& !section_defs
[i
].is_attachable
)
8657 if (strlen(buf
) + strlen(section_defs
[i
].sec
) + 2 > len
) {
8662 strcat(buf
, section_defs
[i
].sec
);
8668 int libbpf_prog_type_by_name(const char *name
, enum bpf_prog_type
*prog_type
,
8669 enum bpf_attach_type
*expected_attach_type
)
8671 const struct bpf_sec_def
*sec_def
;
8677 sec_def
= find_sec_def(name
);
8679 *prog_type
= sec_def
->prog_type
;
8680 *expected_attach_type
= sec_def
->expected_attach_type
;
8684 pr_debug("failed to guess program type from ELF section '%s'\n", name
);
8685 type_names
= libbpf_get_type_names(false);
8686 if (type_names
!= NULL
) {
8687 pr_debug("supported section(type) names are:%s\n", type_names
);
8694 static struct bpf_map
*find_struct_ops_map_by_offset(struct bpf_object
*obj
,
8697 struct bpf_map
*map
;
8700 for (i
= 0; i
< obj
->nr_maps
; i
++) {
8701 map
= &obj
->maps
[i
];
8702 if (!bpf_map__is_struct_ops(map
))
8704 if (map
->sec_offset
<= offset
&&
8705 offset
- map
->sec_offset
< map
->def
.value_size
)
8712 /* Collect the reloc from ELF and populate the st_ops->progs[] */
8713 static int bpf_object__collect_st_ops_relos(struct bpf_object
*obj
,
8714 GElf_Shdr
*shdr
, Elf_Data
*data
)
8716 const struct btf_member
*member
;
8717 struct bpf_struct_ops
*st_ops
;
8718 struct bpf_program
*prog
;
8719 unsigned int shdr_idx
;
8720 const struct btf
*btf
;
8721 struct bpf_map
*map
;
8723 unsigned int moff
, insn_idx
;
8730 symbols
= obj
->efile
.symbols
;
8732 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
8733 for (i
= 0; i
< nrels
; i
++) {
8734 if (!gelf_getrel(data
, i
, &rel
)) {
8735 pr_warn("struct_ops reloc: failed to get %d reloc\n", i
);
8736 return -LIBBPF_ERRNO__FORMAT
;
8739 if (!gelf_getsym(symbols
, GELF_R_SYM(rel
.r_info
), &sym
)) {
8740 pr_warn("struct_ops reloc: symbol %zx not found\n",
8741 (size_t)GELF_R_SYM(rel
.r_info
));
8742 return -LIBBPF_ERRNO__FORMAT
;
8745 name
= elf_sym_str(obj
, sym
.st_name
) ?: "<?>";
8746 map
= find_struct_ops_map_by_offset(obj
, rel
.r_offset
);
8748 pr_warn("struct_ops reloc: cannot find map at rel.r_offset %zu\n",
8749 (size_t)rel
.r_offset
);
8753 moff
= rel
.r_offset
- map
->sec_offset
;
8754 shdr_idx
= sym
.st_shndx
;
8755 st_ops
= map
->st_ops
;
8756 pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel.r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
8758 (long long)(rel
.r_info
>> 32),
8759 (long long)sym
.st_value
,
8760 shdr_idx
, (size_t)rel
.r_offset
,
8761 map
->sec_offset
, sym
.st_name
, name
);
8763 if (shdr_idx
>= SHN_LORESERVE
) {
8764 pr_warn("struct_ops reloc %s: rel.r_offset %zu shdr_idx %u unsupported non-static function\n",
8765 map
->name
, (size_t)rel
.r_offset
, shdr_idx
);
8766 return -LIBBPF_ERRNO__RELOC
;
8768 if (sym
.st_value
% BPF_INSN_SZ
) {
8769 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
8770 map
->name
, (unsigned long long)sym
.st_value
);
8771 return -LIBBPF_ERRNO__FORMAT
;
8773 insn_idx
= sym
.st_value
/ BPF_INSN_SZ
;
8775 member
= find_member_by_offset(st_ops
->type
, moff
* 8);
8777 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
8781 member_idx
= member
- btf_members(st_ops
->type
);
8782 name
= btf__name_by_offset(btf
, member
->name_off
);
8784 if (!resolve_func_ptr(btf
, member
->type
, NULL
)) {
8785 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
8790 prog
= find_prog_by_sec_insn(obj
, shdr_idx
, insn_idx
);
8792 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
8793 map
->name
, shdr_idx
, name
);
8797 if (prog
->type
== BPF_PROG_TYPE_UNSPEC
) {
8798 const struct bpf_sec_def
*sec_def
;
8800 sec_def
= find_sec_def(prog
->sec_name
);
8802 sec_def
->prog_type
!= BPF_PROG_TYPE_STRUCT_OPS
) {
8804 prog
->type
= sec_def
->prog_type
;
8808 prog
->type
= BPF_PROG_TYPE_STRUCT_OPS
;
8809 prog
->attach_btf_id
= st_ops
->type_id
;
8810 prog
->expected_attach_type
= member_idx
;
8811 } else if (prog
->type
!= BPF_PROG_TYPE_STRUCT_OPS
||
8812 prog
->attach_btf_id
!= st_ops
->type_id
||
8813 prog
->expected_attach_type
!= member_idx
) {
8816 st_ops
->progs
[member_idx
] = prog
;
8822 pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n",
8823 map
->name
, prog
->name
, prog
->sec_name
, prog
->type
,
8824 prog
->attach_btf_id
, prog
->expected_attach_type
, name
);
8828 #define BTF_TRACE_PREFIX "btf_trace_"
8829 #define BTF_LSM_PREFIX "bpf_lsm_"
8830 #define BTF_ITER_PREFIX "bpf_iter_"
8831 #define BTF_MAX_NAME_SIZE 128
8833 static int find_btf_by_prefix_kind(const struct btf
*btf
, const char *prefix
,
8834 const char *name
, __u32 kind
)
8836 char btf_type_name
[BTF_MAX_NAME_SIZE
];
8839 ret
= snprintf(btf_type_name
, sizeof(btf_type_name
),
8840 "%s%s", prefix
, name
);
8841 /* snprintf returns the number of characters written excluding the
8842 * the terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
8843 * indicates truncation.
8845 if (ret
< 0 || ret
>= sizeof(btf_type_name
))
8846 return -ENAMETOOLONG
;
8847 return btf__find_by_name_kind(btf
, btf_type_name
, kind
);
8850 static inline int find_attach_btf_id(struct btf
*btf
, const char *name
,
8851 enum bpf_attach_type attach_type
)
8855 if (attach_type
== BPF_TRACE_RAW_TP
)
8856 err
= find_btf_by_prefix_kind(btf
, BTF_TRACE_PREFIX
, name
,
8858 else if (attach_type
== BPF_LSM_MAC
)
8859 err
= find_btf_by_prefix_kind(btf
, BTF_LSM_PREFIX
, name
,
8861 else if (attach_type
== BPF_TRACE_ITER
)
8862 err
= find_btf_by_prefix_kind(btf
, BTF_ITER_PREFIX
, name
,
8865 err
= btf__find_by_name_kind(btf
, name
, BTF_KIND_FUNC
);
8870 int libbpf_find_vmlinux_btf_id(const char *name
,
8871 enum bpf_attach_type attach_type
)
8876 btf
= libbpf_find_kernel_btf();
8878 pr_warn("vmlinux BTF is not found\n");
8882 err
= find_attach_btf_id(btf
, name
, attach_type
);
8884 pr_warn("%s is not found in vmlinux BTF\n", name
);
8890 static int libbpf_find_prog_btf_id(const char *name
, __u32 attach_prog_fd
)
8892 struct bpf_prog_info_linear
*info_linear
;
8893 struct bpf_prog_info
*info
;
8894 struct btf
*btf
= NULL
;
8897 info_linear
= bpf_program__get_prog_info_linear(attach_prog_fd
, 0);
8898 if (IS_ERR_OR_NULL(info_linear
)) {
8899 pr_warn("failed get_prog_info_linear for FD %d\n",
8903 info
= &info_linear
->info
;
8904 if (!info
->btf_id
) {
8905 pr_warn("The target program doesn't have BTF\n");
8908 if (btf__get_from_id(info
->btf_id
, &btf
)) {
8909 pr_warn("Failed to get BTF of the program\n");
8912 err
= btf__find_by_name_kind(btf
, name
, BTF_KIND_FUNC
);
8915 pr_warn("%s is not found in prog's BTF\n", name
);
8923 static int find_kernel_btf_id(struct bpf_object
*obj
, const char *attach_name
,
8924 enum bpf_attach_type attach_type
,
8925 int *btf_obj_fd
, int *btf_type_id
)
8929 ret
= find_attach_btf_id(obj
->btf_vmlinux
, attach_name
, attach_type
);
8931 *btf_obj_fd
= 0; /* vmlinux BTF */
8938 ret
= load_module_btfs(obj
);
8942 for (i
= 0; i
< obj
->btf_module_cnt
; i
++) {
8943 const struct module_btf
*mod
= &obj
->btf_modules
[i
];
8945 ret
= find_attach_btf_id(mod
->btf
, attach_name
, attach_type
);
8947 *btf_obj_fd
= mod
->fd
;
8960 static int libbpf_find_attach_btf_id(struct bpf_program
*prog
, int *btf_obj_fd
, int *btf_type_id
)
8962 enum bpf_attach_type attach_type
= prog
->expected_attach_type
;
8963 __u32 attach_prog_fd
= prog
->attach_prog_fd
;
8964 const char *name
= prog
->sec_name
, *attach_name
;
8965 const struct bpf_sec_def
*sec
= NULL
;
8971 for (i
= 0; i
< ARRAY_SIZE(section_defs
); i
++) {
8972 if (!section_defs
[i
].is_attach_btf
)
8974 if (strncmp(name
, section_defs
[i
].sec
, section_defs
[i
].len
))
8977 sec
= §ion_defs
[i
];
8982 pr_warn("failed to identify BTF ID based on ELF section name '%s'\n", name
);
8985 attach_name
= name
+ sec
->len
;
8987 /* BPF program's BTF ID */
8988 if (attach_prog_fd
) {
8989 err
= libbpf_find_prog_btf_id(attach_name
, attach_prog_fd
);
8991 pr_warn("failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
8992 attach_prog_fd
, attach_name
, err
);
9000 /* kernel/module BTF ID */
9001 err
= find_kernel_btf_id(prog
->obj
, attach_name
, attach_type
, btf_obj_fd
, btf_type_id
);
9003 pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name
, err
);
9009 int libbpf_attach_type_by_name(const char *name
,
9010 enum bpf_attach_type
*attach_type
)
9018 for (i
= 0; i
< ARRAY_SIZE(section_defs
); i
++) {
9019 if (strncmp(name
, section_defs
[i
].sec
, section_defs
[i
].len
))
9021 if (!section_defs
[i
].is_attachable
)
9023 *attach_type
= section_defs
[i
].expected_attach_type
;
9026 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name
);
9027 type_names
= libbpf_get_type_names(true);
9028 if (type_names
!= NULL
) {
9029 pr_debug("attachable section(type) names are:%s\n", type_names
);
9036 int bpf_map__fd(const struct bpf_map
*map
)
9038 return map
? map
->fd
: -EINVAL
;
9041 const struct bpf_map_def
*bpf_map__def(const struct bpf_map
*map
)
9043 return map
? &map
->def
: ERR_PTR(-EINVAL
);
9046 const char *bpf_map__name(const struct bpf_map
*map
)
9048 return map
? map
->name
: NULL
;
9051 enum bpf_map_type
bpf_map__type(const struct bpf_map
*map
)
9053 return map
->def
.type
;
9056 int bpf_map__set_type(struct bpf_map
*map
, enum bpf_map_type type
)
9060 map
->def
.type
= type
;
9064 __u32
bpf_map__map_flags(const struct bpf_map
*map
)
9066 return map
->def
.map_flags
;
9069 int bpf_map__set_map_flags(struct bpf_map
*map
, __u32 flags
)
9073 map
->def
.map_flags
= flags
;
9077 __u32
bpf_map__numa_node(const struct bpf_map
*map
)
9079 return map
->numa_node
;
9082 int bpf_map__set_numa_node(struct bpf_map
*map
, __u32 numa_node
)
9086 map
->numa_node
= numa_node
;
9090 __u32
bpf_map__key_size(const struct bpf_map
*map
)
9092 return map
->def
.key_size
;
9095 int bpf_map__set_key_size(struct bpf_map
*map
, __u32 size
)
9099 map
->def
.key_size
= size
;
9103 __u32
bpf_map__value_size(const struct bpf_map
*map
)
9105 return map
->def
.value_size
;
9108 int bpf_map__set_value_size(struct bpf_map
*map
, __u32 size
)
9112 map
->def
.value_size
= size
;
9116 __u32
bpf_map__btf_key_type_id(const struct bpf_map
*map
)
9118 return map
? map
->btf_key_type_id
: 0;
9121 __u32
bpf_map__btf_value_type_id(const struct bpf_map
*map
)
9123 return map
? map
->btf_value_type_id
: 0;
9126 int bpf_map__set_priv(struct bpf_map
*map
, void *priv
,
9127 bpf_map_clear_priv_t clear_priv
)
9133 if (map
->clear_priv
)
9134 map
->clear_priv(map
, map
->priv
);
9138 map
->clear_priv
= clear_priv
;
9142 void *bpf_map__priv(const struct bpf_map
*map
)
9144 return map
? map
->priv
: ERR_PTR(-EINVAL
);
9147 int bpf_map__set_initial_value(struct bpf_map
*map
,
9148 const void *data
, size_t size
)
9150 if (!map
->mmaped
|| map
->libbpf_type
== LIBBPF_MAP_KCONFIG
||
9151 size
!= map
->def
.value_size
|| map
->fd
>= 0)
9154 memcpy(map
->mmaped
, data
, size
);
9158 bool bpf_map__is_offload_neutral(const struct bpf_map
*map
)
9160 return map
->def
.type
== BPF_MAP_TYPE_PERF_EVENT_ARRAY
;
9163 bool bpf_map__is_internal(const struct bpf_map
*map
)
9165 return map
->libbpf_type
!= LIBBPF_MAP_UNSPEC
;
9168 __u32
bpf_map__ifindex(const struct bpf_map
*map
)
9170 return map
->map_ifindex
;
9173 int bpf_map__set_ifindex(struct bpf_map
*map
, __u32 ifindex
)
9177 map
->map_ifindex
= ifindex
;
9181 int bpf_map__set_inner_map_fd(struct bpf_map
*map
, int fd
)
9183 if (!bpf_map_type__is_map_in_map(map
->def
.type
)) {
9184 pr_warn("error: unsupported map type\n");
9187 if (map
->inner_map_fd
!= -1) {
9188 pr_warn("error: inner_map_fd already specified\n");
9191 map
->inner_map_fd
= fd
;
9195 static struct bpf_map
*
9196 __bpf_map__iter(const struct bpf_map
*m
, const struct bpf_object
*obj
, int i
)
9199 struct bpf_map
*s
, *e
;
9201 if (!obj
|| !obj
->maps
)
9205 e
= obj
->maps
+ obj
->nr_maps
;
9207 if ((m
< s
) || (m
>= e
)) {
9208 pr_warn("error in %s: map handler doesn't belong to object\n",
9213 idx
= (m
- obj
->maps
) + i
;
9214 if (idx
>= obj
->nr_maps
|| idx
< 0)
9216 return &obj
->maps
[idx
];
9220 bpf_map__next(const struct bpf_map
*prev
, const struct bpf_object
*obj
)
9225 return __bpf_map__iter(prev
, obj
, 1);
9229 bpf_map__prev(const struct bpf_map
*next
, const struct bpf_object
*obj
)
9234 return obj
->maps
+ obj
->nr_maps
- 1;
9237 return __bpf_map__iter(next
, obj
, -1);
9241 bpf_object__find_map_by_name(const struct bpf_object
*obj
, const char *name
)
9243 struct bpf_map
*pos
;
9245 bpf_object__for_each_map(pos
, obj
) {
9246 if (pos
->name
&& !strcmp(pos
->name
, name
))
9253 bpf_object__find_map_fd_by_name(const struct bpf_object
*obj
, const char *name
)
9255 return bpf_map__fd(bpf_object__find_map_by_name(obj
, name
));
9259 bpf_object__find_map_by_offset(struct bpf_object
*obj
, size_t offset
)
9261 return ERR_PTR(-ENOTSUP
);
9264 long libbpf_get_error(const void *ptr
)
9266 return PTR_ERR_OR_ZERO(ptr
);
9269 int bpf_prog_load(const char *file
, enum bpf_prog_type type
,
9270 struct bpf_object
**pobj
, int *prog_fd
)
9272 struct bpf_prog_load_attr attr
;
9274 memset(&attr
, 0, sizeof(struct bpf_prog_load_attr
));
9276 attr
.prog_type
= type
;
9277 attr
.expected_attach_type
= 0;
9279 return bpf_prog_load_xattr(&attr
, pobj
, prog_fd
);
9282 int bpf_prog_load_xattr(const struct bpf_prog_load_attr
*attr
,
9283 struct bpf_object
**pobj
, int *prog_fd
)
9285 struct bpf_object_open_attr open_attr
= {};
9286 struct bpf_program
*prog
, *first_prog
= NULL
;
9287 struct bpf_object
*obj
;
9288 struct bpf_map
*map
;
9296 open_attr
.file
= attr
->file
;
9297 open_attr
.prog_type
= attr
->prog_type
;
9299 obj
= bpf_object__open_xattr(&open_attr
);
9300 if (IS_ERR_OR_NULL(obj
))
9303 bpf_object__for_each_program(prog
, obj
) {
9304 enum bpf_attach_type attach_type
= attr
->expected_attach_type
;
9306 * to preserve backwards compatibility, bpf_prog_load treats
9307 * attr->prog_type, if specified, as an override to whatever
9308 * bpf_object__open guessed
9310 if (attr
->prog_type
!= BPF_PROG_TYPE_UNSPEC
) {
9311 bpf_program__set_type(prog
, attr
->prog_type
);
9312 bpf_program__set_expected_attach_type(prog
,
9315 if (bpf_program__get_type(prog
) == BPF_PROG_TYPE_UNSPEC
) {
9317 * we haven't guessed from section name and user
9318 * didn't provide a fallback type, too bad...
9320 bpf_object__close(obj
);
9324 prog
->prog_ifindex
= attr
->ifindex
;
9325 prog
->log_level
= attr
->log_level
;
9326 prog
->prog_flags
|= attr
->prog_flags
;
9331 bpf_object__for_each_map(map
, obj
) {
9332 if (!bpf_map__is_offload_neutral(map
))
9333 map
->map_ifindex
= attr
->ifindex
;
9337 pr_warn("object file doesn't contain bpf program\n");
9338 bpf_object__close(obj
);
9342 err
= bpf_object__load(obj
);
9344 bpf_object__close(obj
);
9349 *prog_fd
= bpf_program__fd(first_prog
);
9354 int (*detach
)(struct bpf_link
*link
);
9355 int (*destroy
)(struct bpf_link
*link
);
9356 char *pin_path
; /* NULL, if not pinned */
9357 int fd
; /* hook FD, -1 if not applicable */
9361 /* Replace link's underlying BPF program with the new one */
9362 int bpf_link__update_program(struct bpf_link
*link
, struct bpf_program
*prog
)
9364 return bpf_link_update(bpf_link__fd(link
), bpf_program__fd(prog
), NULL
);
9367 /* Release "ownership" of underlying BPF resource (typically, BPF program
9368 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
9369 * link, when destructed through bpf_link__destroy() call won't attempt to
9370 * detach/unregisted that BPF resource. This is useful in situations where,
9371 * say, attached BPF program has to outlive userspace program that attached it
9372 * in the system. Depending on type of BPF program, though, there might be
9373 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
9374 * exit of userspace program doesn't trigger automatic detachment and clean up
9375 * inside the kernel.
9377 void bpf_link__disconnect(struct bpf_link
*link
)
9379 link
->disconnected
= true;
9382 int bpf_link__destroy(struct bpf_link
*link
)
9386 if (IS_ERR_OR_NULL(link
))
9389 if (!link
->disconnected
&& link
->detach
)
9390 err
= link
->detach(link
);
9392 link
->destroy(link
);
9394 free(link
->pin_path
);
9400 int bpf_link__fd(const struct bpf_link
*link
)
9405 const char *bpf_link__pin_path(const struct bpf_link
*link
)
9407 return link
->pin_path
;
9410 static int bpf_link__detach_fd(struct bpf_link
*link
)
9412 return close(link
->fd
);
9415 struct bpf_link
*bpf_link__open(const char *path
)
9417 struct bpf_link
*link
;
9420 fd
= bpf_obj_get(path
);
9423 pr_warn("failed to open link at %s: %d\n", path
, fd
);
9427 link
= calloc(1, sizeof(*link
));
9430 return ERR_PTR(-ENOMEM
);
9432 link
->detach
= &bpf_link__detach_fd
;
9435 link
->pin_path
= strdup(path
);
9436 if (!link
->pin_path
) {
9437 bpf_link__destroy(link
);
9438 return ERR_PTR(-ENOMEM
);
9444 int bpf_link__detach(struct bpf_link
*link
)
9446 return bpf_link_detach(link
->fd
) ? -errno
: 0;
9449 int bpf_link__pin(struct bpf_link
*link
, const char *path
)
9455 err
= make_parent_dir(path
);
9458 err
= check_path(path
);
9462 link
->pin_path
= strdup(path
);
9463 if (!link
->pin_path
)
9466 if (bpf_obj_pin(link
->fd
, link
->pin_path
)) {
9468 zfree(&link
->pin_path
);
9472 pr_debug("link fd=%d: pinned at %s\n", link
->fd
, link
->pin_path
);
9476 int bpf_link__unpin(struct bpf_link
*link
)
9480 if (!link
->pin_path
)
9483 err
= unlink(link
->pin_path
);
9487 pr_debug("link fd=%d: unpinned from %s\n", link
->fd
, link
->pin_path
);
9488 zfree(&link
->pin_path
);
9492 static int bpf_link__detach_perf_event(struct bpf_link
*link
)
9496 err
= ioctl(link
->fd
, PERF_EVENT_IOC_DISABLE
, 0);
9504 struct bpf_link
*bpf_program__attach_perf_event(struct bpf_program
*prog
,
9507 char errmsg
[STRERR_BUFSIZE
];
9508 struct bpf_link
*link
;
9512 pr_warn("prog '%s': invalid perf event FD %d\n",
9514 return ERR_PTR(-EINVAL
);
9516 prog_fd
= bpf_program__fd(prog
);
9518 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
9520 return ERR_PTR(-EINVAL
);
9523 link
= calloc(1, sizeof(*link
));
9525 return ERR_PTR(-ENOMEM
);
9526 link
->detach
= &bpf_link__detach_perf_event
;
9529 if (ioctl(pfd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
) < 0) {
9532 pr_warn("prog '%s': failed to attach to pfd %d: %s\n",
9533 prog
->name
, pfd
, libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9535 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
9537 return ERR_PTR(err
);
9539 if (ioctl(pfd
, PERF_EVENT_IOC_ENABLE
, 0) < 0) {
9542 pr_warn("prog '%s': failed to enable pfd %d: %s\n",
9543 prog
->name
, pfd
, libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9544 return ERR_PTR(err
);
9550 * this function is expected to parse integer in the range of [0, 2^31-1] from
9551 * given file using scanf format string fmt. If actual parsed value is
9552 * negative, the result might be indistinguishable from error
9554 static int parse_uint_from_file(const char *file
, const char *fmt
)
9556 char buf
[STRERR_BUFSIZE
];
9560 f
= fopen(file
, "r");
9563 pr_debug("failed to open '%s': %s\n", file
,
9564 libbpf_strerror_r(err
, buf
, sizeof(buf
)));
9567 err
= fscanf(f
, fmt
, &ret
);
9569 err
= err
== EOF
? -EIO
: -errno
;
9570 pr_debug("failed to parse '%s': %s\n", file
,
9571 libbpf_strerror_r(err
, buf
, sizeof(buf
)));
9579 static int determine_kprobe_perf_type(void)
9581 const char *file
= "/sys/bus/event_source/devices/kprobe/type";
9583 return parse_uint_from_file(file
, "%d\n");
9586 static int determine_uprobe_perf_type(void)
9588 const char *file
= "/sys/bus/event_source/devices/uprobe/type";
9590 return parse_uint_from_file(file
, "%d\n");
9593 static int determine_kprobe_retprobe_bit(void)
9595 const char *file
= "/sys/bus/event_source/devices/kprobe/format/retprobe";
9597 return parse_uint_from_file(file
, "config:%d\n");
9600 static int determine_uprobe_retprobe_bit(void)
9602 const char *file
= "/sys/bus/event_source/devices/uprobe/format/retprobe";
9604 return parse_uint_from_file(file
, "config:%d\n");
9607 static int perf_event_open_probe(bool uprobe
, bool retprobe
, const char *name
,
9608 uint64_t offset
, int pid
)
9610 struct perf_event_attr attr
= {};
9611 char errmsg
[STRERR_BUFSIZE
];
9614 type
= uprobe
? determine_uprobe_perf_type()
9615 : determine_kprobe_perf_type();
9617 pr_warn("failed to determine %s perf type: %s\n",
9618 uprobe
? "uprobe" : "kprobe",
9619 libbpf_strerror_r(type
, errmsg
, sizeof(errmsg
)));
9623 int bit
= uprobe
? determine_uprobe_retprobe_bit()
9624 : determine_kprobe_retprobe_bit();
9627 pr_warn("failed to determine %s retprobe bit: %s\n",
9628 uprobe
? "uprobe" : "kprobe",
9629 libbpf_strerror_r(bit
, errmsg
, sizeof(errmsg
)));
9632 attr
.config
|= 1 << bit
;
9634 attr
.size
= sizeof(attr
);
9636 attr
.config1
= ptr_to_u64(name
); /* kprobe_func or uprobe_path */
9637 attr
.config2
= offset
; /* kprobe_addr or probe_offset */
9639 /* pid filter is meaningful only for uprobes */
9640 pfd
= syscall(__NR_perf_event_open
, &attr
,
9641 pid
< 0 ? -1 : pid
/* pid */,
9642 pid
== -1 ? 0 : -1 /* cpu */,
9643 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC
);
9646 pr_warn("%s perf_event_open() failed: %s\n",
9647 uprobe
? "uprobe" : "kprobe",
9648 libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9654 struct bpf_link
*bpf_program__attach_kprobe(struct bpf_program
*prog
,
9656 const char *func_name
)
9658 char errmsg
[STRERR_BUFSIZE
];
9659 struct bpf_link
*link
;
9662 pfd
= perf_event_open_probe(false /* uprobe */, retprobe
, func_name
,
9663 0 /* offset */, -1 /* pid */);
9665 pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
9666 prog
->name
, retprobe
? "kretprobe" : "kprobe", func_name
,
9667 libbpf_strerror_r(pfd
, errmsg
, sizeof(errmsg
)));
9668 return ERR_PTR(pfd
);
9670 link
= bpf_program__attach_perf_event(prog
, pfd
);
9673 err
= PTR_ERR(link
);
9674 pr_warn("prog '%s': failed to attach to %s '%s': %s\n",
9675 prog
->name
, retprobe
? "kretprobe" : "kprobe", func_name
,
9676 libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9682 static struct bpf_link
*attach_kprobe(const struct bpf_sec_def
*sec
,
9683 struct bpf_program
*prog
)
9685 const char *func_name
;
9688 func_name
= prog
->sec_name
+ sec
->len
;
9689 retprobe
= strcmp(sec
->sec
, "kretprobe/") == 0;
9691 return bpf_program__attach_kprobe(prog
, retprobe
, func_name
);
9694 struct bpf_link
*bpf_program__attach_uprobe(struct bpf_program
*prog
,
9695 bool retprobe
, pid_t pid
,
9696 const char *binary_path
,
9699 char errmsg
[STRERR_BUFSIZE
];
9700 struct bpf_link
*link
;
9703 pfd
= perf_event_open_probe(true /* uprobe */, retprobe
,
9704 binary_path
, func_offset
, pid
);
9706 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
9707 prog
->name
, retprobe
? "uretprobe" : "uprobe",
9708 binary_path
, func_offset
,
9709 libbpf_strerror_r(pfd
, errmsg
, sizeof(errmsg
)));
9710 return ERR_PTR(pfd
);
9712 link
= bpf_program__attach_perf_event(prog
, pfd
);
9715 err
= PTR_ERR(link
);
9716 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
9717 prog
->name
, retprobe
? "uretprobe" : "uprobe",
9718 binary_path
, func_offset
,
9719 libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9725 static int determine_tracepoint_id(const char *tp_category
,
9726 const char *tp_name
)
9728 char file
[PATH_MAX
];
9731 ret
= snprintf(file
, sizeof(file
),
9732 "/sys/kernel/debug/tracing/events/%s/%s/id",
9733 tp_category
, tp_name
);
9736 if (ret
>= sizeof(file
)) {
9737 pr_debug("tracepoint %s/%s path is too long\n",
9738 tp_category
, tp_name
);
9741 return parse_uint_from_file(file
, "%d\n");
9744 static int perf_event_open_tracepoint(const char *tp_category
,
9745 const char *tp_name
)
9747 struct perf_event_attr attr
= {};
9748 char errmsg
[STRERR_BUFSIZE
];
9749 int tp_id
, pfd
, err
;
9751 tp_id
= determine_tracepoint_id(tp_category
, tp_name
);
9753 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
9754 tp_category
, tp_name
,
9755 libbpf_strerror_r(tp_id
, errmsg
, sizeof(errmsg
)));
9759 attr
.type
= PERF_TYPE_TRACEPOINT
;
9760 attr
.size
= sizeof(attr
);
9761 attr
.config
= tp_id
;
9763 pfd
= syscall(__NR_perf_event_open
, &attr
, -1 /* pid */, 0 /* cpu */,
9764 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC
);
9767 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
9768 tp_category
, tp_name
,
9769 libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9775 struct bpf_link
*bpf_program__attach_tracepoint(struct bpf_program
*prog
,
9776 const char *tp_category
,
9777 const char *tp_name
)
9779 char errmsg
[STRERR_BUFSIZE
];
9780 struct bpf_link
*link
;
9783 pfd
= perf_event_open_tracepoint(tp_category
, tp_name
);
9785 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
9786 prog
->name
, tp_category
, tp_name
,
9787 libbpf_strerror_r(pfd
, errmsg
, sizeof(errmsg
)));
9788 return ERR_PTR(pfd
);
9790 link
= bpf_program__attach_perf_event(prog
, pfd
);
9793 err
= PTR_ERR(link
);
9794 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
9795 prog
->name
, tp_category
, tp_name
,
9796 libbpf_strerror_r(err
, errmsg
, sizeof(errmsg
)));
9802 static struct bpf_link
*attach_tp(const struct bpf_sec_def
*sec
,
9803 struct bpf_program
*prog
)
9805 char *sec_name
, *tp_cat
, *tp_name
;
9806 struct bpf_link
*link
;
9808 sec_name
= strdup(prog
->sec_name
);
9810 return ERR_PTR(-ENOMEM
);
9812 /* extract "tp/<category>/<name>" */
9813 tp_cat
= sec_name
+ sec
->len
;
9814 tp_name
= strchr(tp_cat
, '/');
9816 link
= ERR_PTR(-EINVAL
);
9822 link
= bpf_program__attach_tracepoint(prog
, tp_cat
, tp_name
);
9828 struct bpf_link
*bpf_program__attach_raw_tracepoint(struct bpf_program
*prog
,
9829 const char *tp_name
)
9831 char errmsg
[STRERR_BUFSIZE
];
9832 struct bpf_link
*link
;
9835 prog_fd
= bpf_program__fd(prog
);
9837 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
9838 return ERR_PTR(-EINVAL
);
9841 link
= calloc(1, sizeof(*link
));
9843 return ERR_PTR(-ENOMEM
);
9844 link
->detach
= &bpf_link__detach_fd
;
9846 pfd
= bpf_raw_tracepoint_open(tp_name
, prog_fd
);
9850 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
9851 prog
->name
, tp_name
, libbpf_strerror_r(pfd
, errmsg
, sizeof(errmsg
)));
9852 return ERR_PTR(pfd
);
9858 static struct bpf_link
*attach_raw_tp(const struct bpf_sec_def
*sec
,
9859 struct bpf_program
*prog
)
9861 const char *tp_name
= prog
->sec_name
+ sec
->len
;
9863 return bpf_program__attach_raw_tracepoint(prog
, tp_name
);
9866 /* Common logic for all BPF program types that attach to a btf_id */
9867 static struct bpf_link
*bpf_program__attach_btf_id(struct bpf_program
*prog
)
9869 char errmsg
[STRERR_BUFSIZE
];
9870 struct bpf_link
*link
;
9873 prog_fd
= bpf_program__fd(prog
);
9875 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
9876 return ERR_PTR(-EINVAL
);
9879 link
= calloc(1, sizeof(*link
));
9881 return ERR_PTR(-ENOMEM
);
9882 link
->detach
= &bpf_link__detach_fd
;
9884 pfd
= bpf_raw_tracepoint_open(NULL
, prog_fd
);
9888 pr_warn("prog '%s': failed to attach: %s\n",
9889 prog
->name
, libbpf_strerror_r(pfd
, errmsg
, sizeof(errmsg
)));
9890 return ERR_PTR(pfd
);
9893 return (struct bpf_link
*)link
;
9896 struct bpf_link
*bpf_program__attach_trace(struct bpf_program
*prog
)
9898 return bpf_program__attach_btf_id(prog
);
9901 struct bpf_link
*bpf_program__attach_lsm(struct bpf_program
*prog
)
9903 return bpf_program__attach_btf_id(prog
);
9906 static struct bpf_link
*attach_trace(const struct bpf_sec_def
*sec
,
9907 struct bpf_program
*prog
)
9909 return bpf_program__attach_trace(prog
);
9912 static struct bpf_link
*attach_lsm(const struct bpf_sec_def
*sec
,
9913 struct bpf_program
*prog
)
9915 return bpf_program__attach_lsm(prog
);
9918 static struct bpf_link
*attach_iter(const struct bpf_sec_def
*sec
,
9919 struct bpf_program
*prog
)
9921 return bpf_program__attach_iter(prog
, NULL
);
9924 static struct bpf_link
*
9925 bpf_program__attach_fd(struct bpf_program
*prog
, int target_fd
, int btf_id
,
9926 const char *target_name
)
9928 DECLARE_LIBBPF_OPTS(bpf_link_create_opts
, opts
,
9929 .target_btf_id
= btf_id
);
9930 enum bpf_attach_type attach_type
;
9931 char errmsg
[STRERR_BUFSIZE
];
9932 struct bpf_link
*link
;
9933 int prog_fd
, link_fd
;
9935 prog_fd
= bpf_program__fd(prog
);
9937 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
9938 return ERR_PTR(-EINVAL
);
9941 link
= calloc(1, sizeof(*link
));
9943 return ERR_PTR(-ENOMEM
);
9944 link
->detach
= &bpf_link__detach_fd
;
9946 attach_type
= bpf_program__get_expected_attach_type(prog
);
9947 link_fd
= bpf_link_create(prog_fd
, target_fd
, attach_type
, &opts
);
9951 pr_warn("prog '%s': failed to attach to %s: %s\n",
9952 prog
->name
, target_name
,
9953 libbpf_strerror_r(link_fd
, errmsg
, sizeof(errmsg
)));
9954 return ERR_PTR(link_fd
);
9961 bpf_program__attach_cgroup(struct bpf_program
*prog
, int cgroup_fd
)
9963 return bpf_program__attach_fd(prog
, cgroup_fd
, 0, "cgroup");
9967 bpf_program__attach_netns(struct bpf_program
*prog
, int netns_fd
)
9969 return bpf_program__attach_fd(prog
, netns_fd
, 0, "netns");
9972 struct bpf_link
*bpf_program__attach_xdp(struct bpf_program
*prog
, int ifindex
)
9974 /* target_fd/target_ifindex use the same field in LINK_CREATE */
9975 return bpf_program__attach_fd(prog
, ifindex
, 0, "xdp");
9978 struct bpf_link
*bpf_program__attach_freplace(struct bpf_program
*prog
,
9980 const char *attach_func_name
)
9984 if (!!target_fd
!= !!attach_func_name
) {
9985 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
9987 return ERR_PTR(-EINVAL
);
9990 if (prog
->type
!= BPF_PROG_TYPE_EXT
) {
9991 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
9993 return ERR_PTR(-EINVAL
);
9997 btf_id
= libbpf_find_prog_btf_id(attach_func_name
, target_fd
);
9999 return ERR_PTR(btf_id
);
10001 return bpf_program__attach_fd(prog
, target_fd
, btf_id
, "freplace");
10003 /* no target, so use raw_tracepoint_open for compatibility
10006 return bpf_program__attach_trace(prog
);
10011 bpf_program__attach_iter(struct bpf_program
*prog
,
10012 const struct bpf_iter_attach_opts
*opts
)
10014 DECLARE_LIBBPF_OPTS(bpf_link_create_opts
, link_create_opts
);
10015 char errmsg
[STRERR_BUFSIZE
];
10016 struct bpf_link
*link
;
10017 int prog_fd
, link_fd
;
10018 __u32 target_fd
= 0;
10020 if (!OPTS_VALID(opts
, bpf_iter_attach_opts
))
10021 return ERR_PTR(-EINVAL
);
10023 link_create_opts
.iter_info
= OPTS_GET(opts
, link_info
, (void *)0);
10024 link_create_opts
.iter_info_len
= OPTS_GET(opts
, link_info_len
, 0);
10026 prog_fd
= bpf_program__fd(prog
);
10028 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
10029 return ERR_PTR(-EINVAL
);
10032 link
= calloc(1, sizeof(*link
));
10034 return ERR_PTR(-ENOMEM
);
10035 link
->detach
= &bpf_link__detach_fd
;
10037 link_fd
= bpf_link_create(prog_fd
, target_fd
, BPF_TRACE_ITER
,
10038 &link_create_opts
);
10042 pr_warn("prog '%s': failed to attach to iterator: %s\n",
10043 prog
->name
, libbpf_strerror_r(link_fd
, errmsg
, sizeof(errmsg
)));
10044 return ERR_PTR(link_fd
);
10046 link
->fd
= link_fd
;
10050 struct bpf_link
*bpf_program__attach(struct bpf_program
*prog
)
10052 const struct bpf_sec_def
*sec_def
;
10054 sec_def
= find_sec_def(prog
->sec_name
);
10055 if (!sec_def
|| !sec_def
->attach_fn
)
10056 return ERR_PTR(-ESRCH
);
10058 return sec_def
->attach_fn(sec_def
, prog
);
10061 static int bpf_link__detach_struct_ops(struct bpf_link
*link
)
10065 if (bpf_map_delete_elem(link
->fd
, &zero
))
10071 struct bpf_link
*bpf_map__attach_struct_ops(struct bpf_map
*map
)
10073 struct bpf_struct_ops
*st_ops
;
10074 struct bpf_link
*link
;
10078 if (!bpf_map__is_struct_ops(map
) || map
->fd
== -1)
10079 return ERR_PTR(-EINVAL
);
10081 link
= calloc(1, sizeof(*link
));
10083 return ERR_PTR(-EINVAL
);
10085 st_ops
= map
->st_ops
;
10086 for (i
= 0; i
< btf_vlen(st_ops
->type
); i
++) {
10087 struct bpf_program
*prog
= st_ops
->progs
[i
];
10094 prog_fd
= bpf_program__fd(prog
);
10095 kern_data
= st_ops
->kern_vdata
+ st_ops
->kern_func_off
[i
];
10096 *(unsigned long *)kern_data
= prog_fd
;
10099 err
= bpf_map_update_elem(map
->fd
, &zero
, st_ops
->kern_vdata
, 0);
10103 return ERR_PTR(err
);
10106 link
->detach
= bpf_link__detach_struct_ops
;
10107 link
->fd
= map
->fd
;
10112 enum bpf_perf_event_ret
10113 bpf_perf_event_read_simple(void *mmap_mem
, size_t mmap_size
, size_t page_size
,
10114 void **copy_mem
, size_t *copy_size
,
10115 bpf_perf_event_print_t fn
, void *private_data
)
10117 struct perf_event_mmap_page
*header
= mmap_mem
;
10118 __u64 data_head
= ring_buffer_read_head(header
);
10119 __u64 data_tail
= header
->data_tail
;
10120 void *base
= ((__u8
*)header
) + page_size
;
10121 int ret
= LIBBPF_PERF_EVENT_CONT
;
10122 struct perf_event_header
*ehdr
;
10125 while (data_head
!= data_tail
) {
10126 ehdr
= base
+ (data_tail
& (mmap_size
- 1));
10127 ehdr_size
= ehdr
->size
;
10129 if (((void *)ehdr
) + ehdr_size
> base
+ mmap_size
) {
10130 void *copy_start
= ehdr
;
10131 size_t len_first
= base
+ mmap_size
- copy_start
;
10132 size_t len_secnd
= ehdr_size
- len_first
;
10134 if (*copy_size
< ehdr_size
) {
10136 *copy_mem
= malloc(ehdr_size
);
10139 ret
= LIBBPF_PERF_EVENT_ERROR
;
10142 *copy_size
= ehdr_size
;
10145 memcpy(*copy_mem
, copy_start
, len_first
);
10146 memcpy(*copy_mem
+ len_first
, base
, len_secnd
);
10150 ret
= fn(ehdr
, private_data
);
10151 data_tail
+= ehdr_size
;
10152 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
10156 ring_buffer_write_tail(header
, data_tail
);
10160 struct perf_buffer
;
10162 struct perf_buffer_params
{
10163 struct perf_event_attr
*attr
;
10164 /* if event_cb is specified, it takes precendence */
10165 perf_buffer_event_fn event_cb
;
10166 /* sample_cb and lost_cb are higher-level common-case callbacks */
10167 perf_buffer_sample_fn sample_cb
;
10168 perf_buffer_lost_fn lost_cb
;
10175 struct perf_cpu_buf
{
10176 struct perf_buffer
*pb
;
10177 void *base
; /* mmap()'ed memory */
10178 void *buf
; /* for reconstructing segmented data */
10185 struct perf_buffer
{
10186 perf_buffer_event_fn event_cb
;
10187 perf_buffer_sample_fn sample_cb
;
10188 perf_buffer_lost_fn lost_cb
;
10189 void *ctx
; /* passed into callbacks */
10193 struct perf_cpu_buf
**cpu_bufs
;
10194 struct epoll_event
*events
;
10195 int cpu_cnt
; /* number of allocated CPU buffers */
10196 int epoll_fd
; /* perf event FD */
10197 int map_fd
; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
10200 static void perf_buffer__free_cpu_buf(struct perf_buffer
*pb
,
10201 struct perf_cpu_buf
*cpu_buf
)
10205 if (cpu_buf
->base
&&
10206 munmap(cpu_buf
->base
, pb
->mmap_size
+ pb
->page_size
))
10207 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf
->cpu
);
10208 if (cpu_buf
->fd
>= 0) {
10209 ioctl(cpu_buf
->fd
, PERF_EVENT_IOC_DISABLE
, 0);
10210 close(cpu_buf
->fd
);
10212 free(cpu_buf
->buf
);
10216 void perf_buffer__free(struct perf_buffer
*pb
)
10220 if (IS_ERR_OR_NULL(pb
))
10222 if (pb
->cpu_bufs
) {
10223 for (i
= 0; i
< pb
->cpu_cnt
; i
++) {
10224 struct perf_cpu_buf
*cpu_buf
= pb
->cpu_bufs
[i
];
10229 bpf_map_delete_elem(pb
->map_fd
, &cpu_buf
->map_key
);
10230 perf_buffer__free_cpu_buf(pb
, cpu_buf
);
10232 free(pb
->cpu_bufs
);
10234 if (pb
->epoll_fd
>= 0)
10235 close(pb
->epoll_fd
);
10240 static struct perf_cpu_buf
*
10241 perf_buffer__open_cpu_buf(struct perf_buffer
*pb
, struct perf_event_attr
*attr
,
10242 int cpu
, int map_key
)
10244 struct perf_cpu_buf
*cpu_buf
;
10245 char msg
[STRERR_BUFSIZE
];
10248 cpu_buf
= calloc(1, sizeof(*cpu_buf
));
10250 return ERR_PTR(-ENOMEM
);
10253 cpu_buf
->cpu
= cpu
;
10254 cpu_buf
->map_key
= map_key
;
10256 cpu_buf
->fd
= syscall(__NR_perf_event_open
, attr
, -1 /* pid */, cpu
,
10257 -1, PERF_FLAG_FD_CLOEXEC
);
10258 if (cpu_buf
->fd
< 0) {
10260 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
10261 cpu
, libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10265 cpu_buf
->base
= mmap(NULL
, pb
->mmap_size
+ pb
->page_size
,
10266 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
10268 if (cpu_buf
->base
== MAP_FAILED
) {
10269 cpu_buf
->base
= NULL
;
10271 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
10272 cpu
, libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10276 if (ioctl(cpu_buf
->fd
, PERF_EVENT_IOC_ENABLE
, 0) < 0) {
10278 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
10279 cpu
, libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10286 perf_buffer__free_cpu_buf(pb
, cpu_buf
);
10287 return (struct perf_cpu_buf
*)ERR_PTR(err
);
10290 static struct perf_buffer
*__perf_buffer__new(int map_fd
, size_t page_cnt
,
10291 struct perf_buffer_params
*p
);
10293 struct perf_buffer
*perf_buffer__new(int map_fd
, size_t page_cnt
,
10294 const struct perf_buffer_opts
*opts
)
10296 struct perf_buffer_params p
= {};
10297 struct perf_event_attr attr
= { 0, };
10299 attr
.config
= PERF_COUNT_SW_BPF_OUTPUT
;
10300 attr
.type
= PERF_TYPE_SOFTWARE
;
10301 attr
.sample_type
= PERF_SAMPLE_RAW
;
10302 attr
.sample_period
= 1;
10303 attr
.wakeup_events
= 1;
10306 p
.sample_cb
= opts
? opts
->sample_cb
: NULL
;
10307 p
.lost_cb
= opts
? opts
->lost_cb
: NULL
;
10308 p
.ctx
= opts
? opts
->ctx
: NULL
;
10310 return __perf_buffer__new(map_fd
, page_cnt
, &p
);
10313 struct perf_buffer
*
10314 perf_buffer__new_raw(int map_fd
, size_t page_cnt
,
10315 const struct perf_buffer_raw_opts
*opts
)
10317 struct perf_buffer_params p
= {};
10319 p
.attr
= opts
->attr
;
10320 p
.event_cb
= opts
->event_cb
;
10322 p
.cpu_cnt
= opts
->cpu_cnt
;
10323 p
.cpus
= opts
->cpus
;
10324 p
.map_keys
= opts
->map_keys
;
10326 return __perf_buffer__new(map_fd
, page_cnt
, &p
);
10329 static struct perf_buffer
*__perf_buffer__new(int map_fd
, size_t page_cnt
,
10330 struct perf_buffer_params
*p
)
10332 const char *online_cpus_file
= "/sys/devices/system/cpu/online";
10333 struct bpf_map_info map
;
10334 char msg
[STRERR_BUFSIZE
];
10335 struct perf_buffer
*pb
;
10336 bool *online
= NULL
;
10337 __u32 map_info_len
;
10340 if (page_cnt
& (page_cnt
- 1)) {
10341 pr_warn("page count should be power of two, but is %zu\n",
10343 return ERR_PTR(-EINVAL
);
10346 /* best-effort sanity checks */
10347 memset(&map
, 0, sizeof(map
));
10348 map_info_len
= sizeof(map
);
10349 err
= bpf_obj_get_info_by_fd(map_fd
, &map
, &map_info_len
);
10352 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
10353 * -EBADFD, -EFAULT, or -E2BIG on real error
10355 if (err
!= -EINVAL
) {
10356 pr_warn("failed to get map info for map FD %d: %s\n",
10357 map_fd
, libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10358 return ERR_PTR(err
);
10360 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
10363 if (map
.type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
) {
10364 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
10366 return ERR_PTR(-EINVAL
);
10370 pb
= calloc(1, sizeof(*pb
));
10372 return ERR_PTR(-ENOMEM
);
10374 pb
->event_cb
= p
->event_cb
;
10375 pb
->sample_cb
= p
->sample_cb
;
10376 pb
->lost_cb
= p
->lost_cb
;
10379 pb
->page_size
= getpagesize();
10380 pb
->mmap_size
= pb
->page_size
* page_cnt
;
10381 pb
->map_fd
= map_fd
;
10383 pb
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
10384 if (pb
->epoll_fd
< 0) {
10386 pr_warn("failed to create epoll instance: %s\n",
10387 libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10391 if (p
->cpu_cnt
> 0) {
10392 pb
->cpu_cnt
= p
->cpu_cnt
;
10394 pb
->cpu_cnt
= libbpf_num_possible_cpus();
10395 if (pb
->cpu_cnt
< 0) {
10399 if (map
.max_entries
&& map
.max_entries
< pb
->cpu_cnt
)
10400 pb
->cpu_cnt
= map
.max_entries
;
10403 pb
->events
= calloc(pb
->cpu_cnt
, sizeof(*pb
->events
));
10406 pr_warn("failed to allocate events: out of memory\n");
10409 pb
->cpu_bufs
= calloc(pb
->cpu_cnt
, sizeof(*pb
->cpu_bufs
));
10410 if (!pb
->cpu_bufs
) {
10412 pr_warn("failed to allocate buffers: out of memory\n");
10416 err
= parse_cpu_mask_file(online_cpus_file
, &online
, &n
);
10418 pr_warn("failed to get online CPU mask: %d\n", err
);
10422 for (i
= 0, j
= 0; i
< pb
->cpu_cnt
; i
++) {
10423 struct perf_cpu_buf
*cpu_buf
;
10426 cpu
= p
->cpu_cnt
> 0 ? p
->cpus
[i
] : i
;
10427 map_key
= p
->cpu_cnt
> 0 ? p
->map_keys
[i
] : i
;
10429 /* in case user didn't explicitly requested particular CPUs to
10430 * be attached to, skip offline/not present CPUs
10432 if (p
->cpu_cnt
<= 0 && (cpu
>= n
|| !online
[cpu
]))
10435 cpu_buf
= perf_buffer__open_cpu_buf(pb
, p
->attr
, cpu
, map_key
);
10436 if (IS_ERR(cpu_buf
)) {
10437 err
= PTR_ERR(cpu_buf
);
10441 pb
->cpu_bufs
[j
] = cpu_buf
;
10443 err
= bpf_map_update_elem(pb
->map_fd
, &map_key
,
10447 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
10448 cpu
, map_key
, cpu_buf
->fd
,
10449 libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10453 pb
->events
[j
].events
= EPOLLIN
;
10454 pb
->events
[j
].data
.ptr
= cpu_buf
;
10455 if (epoll_ctl(pb
->epoll_fd
, EPOLL_CTL_ADD
, cpu_buf
->fd
,
10456 &pb
->events
[j
]) < 0) {
10458 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
10460 libbpf_strerror_r(err
, msg
, sizeof(msg
)));
10473 perf_buffer__free(pb
);
10474 return ERR_PTR(err
);
10477 struct perf_sample_raw
{
10478 struct perf_event_header header
;
10483 struct perf_sample_lost
{
10484 struct perf_event_header header
;
10487 uint64_t sample_id
;
10490 static enum bpf_perf_event_ret
10491 perf_buffer__process_record(struct perf_event_header
*e
, void *ctx
)
10493 struct perf_cpu_buf
*cpu_buf
= ctx
;
10494 struct perf_buffer
*pb
= cpu_buf
->pb
;
10497 /* user wants full control over parsing perf event */
10499 return pb
->event_cb(pb
->ctx
, cpu_buf
->cpu
, e
);
10502 case PERF_RECORD_SAMPLE
: {
10503 struct perf_sample_raw
*s
= data
;
10506 pb
->sample_cb(pb
->ctx
, cpu_buf
->cpu
, s
->data
, s
->size
);
10509 case PERF_RECORD_LOST
: {
10510 struct perf_sample_lost
*s
= data
;
10513 pb
->lost_cb(pb
->ctx
, cpu_buf
->cpu
, s
->lost
);
10517 pr_warn("unknown perf sample type %d\n", e
->type
);
10518 return LIBBPF_PERF_EVENT_ERROR
;
10520 return LIBBPF_PERF_EVENT_CONT
;
10523 static int perf_buffer__process_records(struct perf_buffer
*pb
,
10524 struct perf_cpu_buf
*cpu_buf
)
10526 enum bpf_perf_event_ret ret
;
10528 ret
= bpf_perf_event_read_simple(cpu_buf
->base
, pb
->mmap_size
,
10529 pb
->page_size
, &cpu_buf
->buf
,
10530 &cpu_buf
->buf_size
,
10531 perf_buffer__process_record
, cpu_buf
);
10532 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
10537 int perf_buffer__epoll_fd(const struct perf_buffer
*pb
)
10539 return pb
->epoll_fd
;
10542 int perf_buffer__poll(struct perf_buffer
*pb
, int timeout_ms
)
10546 cnt
= epoll_wait(pb
->epoll_fd
, pb
->events
, pb
->cpu_cnt
, timeout_ms
);
10547 for (i
= 0; i
< cnt
; i
++) {
10548 struct perf_cpu_buf
*cpu_buf
= pb
->events
[i
].data
.ptr
;
10550 err
= perf_buffer__process_records(pb
, cpu_buf
);
10552 pr_warn("error while processing records: %d\n", err
);
10556 return cnt
< 0 ? -errno
: cnt
;
10559 /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
10562 size_t perf_buffer__buffer_cnt(const struct perf_buffer
*pb
)
10564 return pb
->cpu_cnt
;
10568 * Return perf_event FD of a ring buffer in *buf_idx* slot of
10569 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
10570 * select()/poll()/epoll() Linux syscalls.
10572 int perf_buffer__buffer_fd(const struct perf_buffer
*pb
, size_t buf_idx
)
10574 struct perf_cpu_buf
*cpu_buf
;
10576 if (buf_idx
>= pb
->cpu_cnt
)
10579 cpu_buf
= pb
->cpu_bufs
[buf_idx
];
10583 return cpu_buf
->fd
;
10587 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
10588 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
10589 * consume, do nothing and return success.
10594 int perf_buffer__consume_buffer(struct perf_buffer
*pb
, size_t buf_idx
)
10596 struct perf_cpu_buf
*cpu_buf
;
10598 if (buf_idx
>= pb
->cpu_cnt
)
10601 cpu_buf
= pb
->cpu_bufs
[buf_idx
];
10605 return perf_buffer__process_records(pb
, cpu_buf
);
10608 int perf_buffer__consume(struct perf_buffer
*pb
)
10612 for (i
= 0; i
< pb
->cpu_cnt
; i
++) {
10613 struct perf_cpu_buf
*cpu_buf
= pb
->cpu_bufs
[i
];
10618 err
= perf_buffer__process_records(pb
, cpu_buf
);
10620 pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i
, err
);
10627 struct bpf_prog_info_array_desc
{
10628 int array_offset
; /* e.g. offset of jited_prog_insns */
10629 int count_offset
; /* e.g. offset of jited_prog_len */
10630 int size_offset
; /* > 0: offset of rec size,
10631 * < 0: fix size of -size_offset
10635 static struct bpf_prog_info_array_desc bpf_prog_info_array_desc
[] = {
10636 [BPF_PROG_INFO_JITED_INSNS
] = {
10637 offsetof(struct bpf_prog_info
, jited_prog_insns
),
10638 offsetof(struct bpf_prog_info
, jited_prog_len
),
10641 [BPF_PROG_INFO_XLATED_INSNS
] = {
10642 offsetof(struct bpf_prog_info
, xlated_prog_insns
),
10643 offsetof(struct bpf_prog_info
, xlated_prog_len
),
10646 [BPF_PROG_INFO_MAP_IDS
] = {
10647 offsetof(struct bpf_prog_info
, map_ids
),
10648 offsetof(struct bpf_prog_info
, nr_map_ids
),
10649 -(int)sizeof(__u32
),
10651 [BPF_PROG_INFO_JITED_KSYMS
] = {
10652 offsetof(struct bpf_prog_info
, jited_ksyms
),
10653 offsetof(struct bpf_prog_info
, nr_jited_ksyms
),
10654 -(int)sizeof(__u64
),
10656 [BPF_PROG_INFO_JITED_FUNC_LENS
] = {
10657 offsetof(struct bpf_prog_info
, jited_func_lens
),
10658 offsetof(struct bpf_prog_info
, nr_jited_func_lens
),
10659 -(int)sizeof(__u32
),
10661 [BPF_PROG_INFO_FUNC_INFO
] = {
10662 offsetof(struct bpf_prog_info
, func_info
),
10663 offsetof(struct bpf_prog_info
, nr_func_info
),
10664 offsetof(struct bpf_prog_info
, func_info_rec_size
),
10666 [BPF_PROG_INFO_LINE_INFO
] = {
10667 offsetof(struct bpf_prog_info
, line_info
),
10668 offsetof(struct bpf_prog_info
, nr_line_info
),
10669 offsetof(struct bpf_prog_info
, line_info_rec_size
),
10671 [BPF_PROG_INFO_JITED_LINE_INFO
] = {
10672 offsetof(struct bpf_prog_info
, jited_line_info
),
10673 offsetof(struct bpf_prog_info
, nr_jited_line_info
),
10674 offsetof(struct bpf_prog_info
, jited_line_info_rec_size
),
10676 [BPF_PROG_INFO_PROG_TAGS
] = {
10677 offsetof(struct bpf_prog_info
, prog_tags
),
10678 offsetof(struct bpf_prog_info
, nr_prog_tags
),
10679 -(int)sizeof(__u8
) * BPF_TAG_SIZE
,
10684 static __u32
bpf_prog_info_read_offset_u32(struct bpf_prog_info
*info
,
10687 __u32
*array
= (__u32
*)info
;
10690 return array
[offset
/ sizeof(__u32
)];
10691 return -(int)offset
;
10694 static __u64
bpf_prog_info_read_offset_u64(struct bpf_prog_info
*info
,
10697 __u64
*array
= (__u64
*)info
;
10700 return array
[offset
/ sizeof(__u64
)];
10701 return -(int)offset
;
10704 static void bpf_prog_info_set_offset_u32(struct bpf_prog_info
*info
, int offset
,
10707 __u32
*array
= (__u32
*)info
;
10710 array
[offset
/ sizeof(__u32
)] = val
;
10713 static void bpf_prog_info_set_offset_u64(struct bpf_prog_info
*info
, int offset
,
10716 __u64
*array
= (__u64
*)info
;
10719 array
[offset
/ sizeof(__u64
)] = val
;
10722 struct bpf_prog_info_linear
*
10723 bpf_program__get_prog_info_linear(int fd
, __u64 arrays
)
10725 struct bpf_prog_info_linear
*info_linear
;
10726 struct bpf_prog_info info
= {};
10727 __u32 info_len
= sizeof(info
);
10728 __u32 data_len
= 0;
10732 if (arrays
>> BPF_PROG_INFO_LAST_ARRAY
)
10733 return ERR_PTR(-EINVAL
);
10735 /* step 1: get array dimensions */
10736 err
= bpf_obj_get_info_by_fd(fd
, &info
, &info_len
);
10738 pr_debug("can't get prog info: %s", strerror(errno
));
10739 return ERR_PTR(-EFAULT
);
10742 /* step 2: calculate total size of all arrays */
10743 for (i
= BPF_PROG_INFO_FIRST_ARRAY
; i
< BPF_PROG_INFO_LAST_ARRAY
; ++i
) {
10744 bool include_array
= (arrays
& (1UL << i
)) > 0;
10745 struct bpf_prog_info_array_desc
*desc
;
10748 desc
= bpf_prog_info_array_desc
+ i
;
10750 /* kernel is too old to support this field */
10751 if (info_len
< desc
->array_offset
+ sizeof(__u32
) ||
10752 info_len
< desc
->count_offset
+ sizeof(__u32
) ||
10753 (desc
->size_offset
> 0 && info_len
< desc
->size_offset
))
10754 include_array
= false;
10756 if (!include_array
) {
10757 arrays
&= ~(1UL << i
); /* clear the bit */
10761 count
= bpf_prog_info_read_offset_u32(&info
, desc
->count_offset
);
10762 size
= bpf_prog_info_read_offset_u32(&info
, desc
->size_offset
);
10764 data_len
+= count
* size
;
10767 /* step 3: allocate continuous memory */
10768 data_len
= roundup(data_len
, sizeof(__u64
));
10769 info_linear
= malloc(sizeof(struct bpf_prog_info_linear
) + data_len
);
10771 return ERR_PTR(-ENOMEM
);
10773 /* step 4: fill data to info_linear->info */
10774 info_linear
->arrays
= arrays
;
10775 memset(&info_linear
->info
, 0, sizeof(info
));
10776 ptr
= info_linear
->data
;
10778 for (i
= BPF_PROG_INFO_FIRST_ARRAY
; i
< BPF_PROG_INFO_LAST_ARRAY
; ++i
) {
10779 struct bpf_prog_info_array_desc
*desc
;
10782 if ((arrays
& (1UL << i
)) == 0)
10785 desc
= bpf_prog_info_array_desc
+ i
;
10786 count
= bpf_prog_info_read_offset_u32(&info
, desc
->count_offset
);
10787 size
= bpf_prog_info_read_offset_u32(&info
, desc
->size_offset
);
10788 bpf_prog_info_set_offset_u32(&info_linear
->info
,
10789 desc
->count_offset
, count
);
10790 bpf_prog_info_set_offset_u32(&info_linear
->info
,
10791 desc
->size_offset
, size
);
10792 bpf_prog_info_set_offset_u64(&info_linear
->info
,
10793 desc
->array_offset
,
10795 ptr
+= count
* size
;
10798 /* step 5: call syscall again to get required arrays */
10799 err
= bpf_obj_get_info_by_fd(fd
, &info_linear
->info
, &info_len
);
10801 pr_debug("can't get prog info: %s", strerror(errno
));
10803 return ERR_PTR(-EFAULT
);
10806 /* step 6: verify the data */
10807 for (i
= BPF_PROG_INFO_FIRST_ARRAY
; i
< BPF_PROG_INFO_LAST_ARRAY
; ++i
) {
10808 struct bpf_prog_info_array_desc
*desc
;
10811 if ((arrays
& (1UL << i
)) == 0)
10814 desc
= bpf_prog_info_array_desc
+ i
;
10815 v1
= bpf_prog_info_read_offset_u32(&info
, desc
->count_offset
);
10816 v2
= bpf_prog_info_read_offset_u32(&info_linear
->info
,
10817 desc
->count_offset
);
10819 pr_warn("%s: mismatch in element count\n", __func__
);
10821 v1
= bpf_prog_info_read_offset_u32(&info
, desc
->size_offset
);
10822 v2
= bpf_prog_info_read_offset_u32(&info_linear
->info
,
10823 desc
->size_offset
);
10825 pr_warn("%s: mismatch in rec size\n", __func__
);
10828 /* step 7: update info_len and data_len */
10829 info_linear
->info_len
= sizeof(struct bpf_prog_info
);
10830 info_linear
->data_len
= data_len
;
10832 return info_linear
;
10835 void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear
*info_linear
)
10839 for (i
= BPF_PROG_INFO_FIRST_ARRAY
; i
< BPF_PROG_INFO_LAST_ARRAY
; ++i
) {
10840 struct bpf_prog_info_array_desc
*desc
;
10843 if ((info_linear
->arrays
& (1UL << i
)) == 0)
10846 desc
= bpf_prog_info_array_desc
+ i
;
10847 addr
= bpf_prog_info_read_offset_u64(&info_linear
->info
,
10848 desc
->array_offset
);
10849 offs
= addr
- ptr_to_u64(info_linear
->data
);
10850 bpf_prog_info_set_offset_u64(&info_linear
->info
,
10851 desc
->array_offset
, offs
);
10855 void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear
*info_linear
)
10859 for (i
= BPF_PROG_INFO_FIRST_ARRAY
; i
< BPF_PROG_INFO_LAST_ARRAY
; ++i
) {
10860 struct bpf_prog_info_array_desc
*desc
;
10863 if ((info_linear
->arrays
& (1UL << i
)) == 0)
10866 desc
= bpf_prog_info_array_desc
+ i
;
10867 offs
= bpf_prog_info_read_offset_u64(&info_linear
->info
,
10868 desc
->array_offset
);
10869 addr
= offs
+ ptr_to_u64(info_linear
->data
);
10870 bpf_prog_info_set_offset_u64(&info_linear
->info
,
10871 desc
->array_offset
, addr
);
10875 int bpf_program__set_attach_target(struct bpf_program
*prog
,
10876 int attach_prog_fd
,
10877 const char *attach_func_name
)
10879 int btf_obj_fd
= 0, btf_id
= 0, err
;
10881 if (!prog
|| attach_prog_fd
< 0 || !attach_func_name
)
10884 if (prog
->obj
->loaded
)
10887 if (attach_prog_fd
) {
10888 btf_id
= libbpf_find_prog_btf_id(attach_func_name
,
10893 /* load btf_vmlinux, if not yet */
10894 err
= bpf_object__load_vmlinux_btf(prog
->obj
, true);
10897 err
= find_kernel_btf_id(prog
->obj
, attach_func_name
,
10898 prog
->expected_attach_type
,
10899 &btf_obj_fd
, &btf_id
);
10904 prog
->attach_btf_id
= btf_id
;
10905 prog
->attach_btf_obj_fd
= btf_obj_fd
;
10906 prog
->attach_prog_fd
= attach_prog_fd
;
10910 int parse_cpu_mask_str(const char *s
, bool **mask
, int *mask_sz
)
10912 int err
= 0, n
, len
, start
, end
= -1;
10918 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
10920 if (*s
== ',' || *s
== '\n') {
10924 n
= sscanf(s
, "%d%n-%d%n", &start
, &len
, &end
, &len
);
10925 if (n
<= 0 || n
> 2) {
10926 pr_warn("Failed to get CPU range %s: %d\n", s
, n
);
10929 } else if (n
== 1) {
10932 if (start
< 0 || start
> end
) {
10933 pr_warn("Invalid CPU range [%d,%d] in %s\n",
10938 tmp
= realloc(*mask
, end
+ 1);
10944 memset(tmp
+ *mask_sz
, 0, start
- *mask_sz
);
10945 memset(tmp
+ start
, 1, end
- start
+ 1);
10946 *mask_sz
= end
+ 1;
10950 pr_warn("Empty CPU range\n");
10960 int parse_cpu_mask_file(const char *fcpu
, bool **mask
, int *mask_sz
)
10962 int fd
, err
= 0, len
;
10965 fd
= open(fcpu
, O_RDONLY
);
10968 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu
, err
);
10971 len
= read(fd
, buf
, sizeof(buf
));
10974 err
= len
? -errno
: -EINVAL
;
10975 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu
, err
);
10978 if (len
>= sizeof(buf
)) {
10979 pr_warn("CPU mask is too big in file %s\n", fcpu
);
10984 return parse_cpu_mask_str(buf
, mask
, mask_sz
);
10987 int libbpf_num_possible_cpus(void)
10989 static const char *fcpu
= "/sys/devices/system/cpu/possible";
10991 int err
, n
, i
, tmp_cpus
;
10994 tmp_cpus
= READ_ONCE(cpus
);
10998 err
= parse_cpu_mask_file(fcpu
, &mask
, &n
);
11003 for (i
= 0; i
< n
; i
++) {
11009 WRITE_ONCE(cpus
, tmp_cpus
);
11013 int bpf_object__open_skeleton(struct bpf_object_skeleton
*s
,
11014 const struct bpf_object_open_opts
*opts
)
11016 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, skel_opts
,
11017 .object_name
= s
->name
,
11019 struct bpf_object
*obj
;
11022 /* Attempt to preserve opts->object_name, unless overriden by user
11023 * explicitly. Overwriting object name for skeletons is discouraged,
11024 * as it breaks global data maps, because they contain object name
11025 * prefix as their own map name prefix. When skeleton is generated,
11026 * bpftool is making an assumption that this name will stay the same.
11029 memcpy(&skel_opts
, opts
, sizeof(*opts
));
11030 if (!opts
->object_name
)
11031 skel_opts
.object_name
= s
->name
;
11034 obj
= bpf_object__open_mem(s
->data
, s
->data_sz
, &skel_opts
);
11036 pr_warn("failed to initialize skeleton BPF object '%s': %ld\n",
11037 s
->name
, PTR_ERR(obj
));
11038 return PTR_ERR(obj
);
11043 for (i
= 0; i
< s
->map_cnt
; i
++) {
11044 struct bpf_map
**map
= s
->maps
[i
].map
;
11045 const char *name
= s
->maps
[i
].name
;
11046 void **mmaped
= s
->maps
[i
].mmaped
;
11048 *map
= bpf_object__find_map_by_name(obj
, name
);
11050 pr_warn("failed to find skeleton map '%s'\n", name
);
11054 /* externs shouldn't be pre-setup from user code */
11055 if (mmaped
&& (*map
)->libbpf_type
!= LIBBPF_MAP_KCONFIG
)
11056 *mmaped
= (*map
)->mmaped
;
11059 for (i
= 0; i
< s
->prog_cnt
; i
++) {
11060 struct bpf_program
**prog
= s
->progs
[i
].prog
;
11061 const char *name
= s
->progs
[i
].name
;
11063 *prog
= bpf_object__find_program_by_name(obj
, name
);
11065 pr_warn("failed to find skeleton program '%s'\n", name
);
11073 int bpf_object__load_skeleton(struct bpf_object_skeleton
*s
)
11077 err
= bpf_object__load(*s
->obj
);
11079 pr_warn("failed to load BPF skeleton '%s': %d\n", s
->name
, err
);
11083 for (i
= 0; i
< s
->map_cnt
; i
++) {
11084 struct bpf_map
*map
= *s
->maps
[i
].map
;
11085 size_t mmap_sz
= bpf_map_mmap_sz(map
);
11086 int prot
, map_fd
= bpf_map__fd(map
);
11087 void **mmaped
= s
->maps
[i
].mmaped
;
11092 if (!(map
->def
.map_flags
& BPF_F_MMAPABLE
)) {
11097 if (map
->def
.map_flags
& BPF_F_RDONLY_PROG
)
11100 prot
= PROT_READ
| PROT_WRITE
;
11102 /* Remap anonymous mmap()-ed "map initialization image" as
11103 * a BPF map-backed mmap()-ed memory, but preserving the same
11104 * memory address. This will cause kernel to change process'
11105 * page table to point to a different piece of kernel memory,
11106 * but from userspace point of view memory address (and its
11107 * contents, being identical at this point) will stay the
11108 * same. This mapping will be released by bpf_object__close()
11109 * as per normal clean up procedure, so we don't need to worry
11110 * about it from skeleton's clean up perspective.
11112 *mmaped
= mmap(map
->mmaped
, mmap_sz
, prot
,
11113 MAP_SHARED
| MAP_FIXED
, map_fd
, 0);
11114 if (*mmaped
== MAP_FAILED
) {
11117 pr_warn("failed to re-mmap() map '%s': %d\n",
11118 bpf_map__name(map
), err
);
11126 int bpf_object__attach_skeleton(struct bpf_object_skeleton
*s
)
11130 for (i
= 0; i
< s
->prog_cnt
; i
++) {
11131 struct bpf_program
*prog
= *s
->progs
[i
].prog
;
11132 struct bpf_link
**link
= s
->progs
[i
].link
;
11133 const struct bpf_sec_def
*sec_def
;
11138 sec_def
= find_sec_def(prog
->sec_name
);
11139 if (!sec_def
|| !sec_def
->attach_fn
)
11142 *link
= sec_def
->attach_fn(sec_def
, prog
);
11143 if (IS_ERR(*link
)) {
11144 pr_warn("failed to auto-attach program '%s': %ld\n",
11145 bpf_program__name(prog
), PTR_ERR(*link
));
11146 return PTR_ERR(*link
);
11153 void bpf_object__detach_skeleton(struct bpf_object_skeleton
*s
)
11157 for (i
= 0; i
< s
->prog_cnt
; i
++) {
11158 struct bpf_link
**link
= s
->progs
[i
].link
;
11160 bpf_link__destroy(*link
);
11165 void bpf_object__destroy_skeleton(struct bpf_object_skeleton
*s
)
11168 bpf_object__detach_skeleton(s
);
11170 bpf_object__close(*s
->obj
);