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. */
398 int kernel_btf_obj_fd;
401 /* local btf_id of the ksym extern's type. */
407 static LIST_HEAD(bpf_objects_list);
417 char name[BPF_OBJ_NAME_LEN];
421 struct bpf_program *programs;
423 struct bpf_map *maps;
428 struct extern_desc *externs;
437 * Information when doing elf related work. Only valid if fd
450 Elf_Data *st_ops_data;
451 size_t shstrndx; /* section index for section name strings */
460 __u32 btf_maps_sec_btf_id;
469 * All loaded bpf_object is linked in a list, which is
470 * hidden to caller. bpf_objects__<func> handlers deal with
473 struct list_head list;
476 struct btf_ext *btf_ext;
478 /* Parse and load BTF vmlinux if any of the programs in the object need
481 struct btf *btf_vmlinux;
482 /* vmlinux BTF override for CO-RE relocations */
483 struct btf *btf_vmlinux_override;
484 /* Lazily initialized kernel module BTFs */
485 struct module_btf *btf_modules;
486 bool btf_modules_loaded;
487 size_t btf_module_cnt;
488 size_t btf_module_cap;
491 bpf_object_clear_priv_t clear_priv;
495 #define obj_elf_valid(o) ((o)->efile.elf)
497 static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
498 static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
499 static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
500 static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
501 static int elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn, GElf_Shdr *hdr);
502 static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
503 static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
504 static int elf_sym_by_sec_off(const struct bpf_object *obj, size_t sec_idx,
505 size_t off, __u32 sym_type, GElf_Sym *sym);
507 void bpf_program__unload(struct bpf_program *prog)
515 * If the object is opened but the program was never loaded,
516 * it is possible that prog->instances.nr == -1.
518 if (prog->instances.nr > 0) {
519 for (i = 0; i < prog->instances.nr; i++)
520 zclose(prog->instances.fds[i]);
521 } else if (prog->instances.nr != -1) {
522 pr_warn("Internal error: instances.nr is %d\n",
526 prog->instances.nr = -1;
527 zfree(&prog->instances.fds);
529 zfree(&prog->func_info);
530 zfree(&prog->line_info);
533 static void bpf_program__exit(struct bpf_program *prog)
538 if (prog->clear_priv)
539 prog->clear_priv(prog, prog->priv);
542 prog->clear_priv = NULL;
544 bpf_program__unload(prog);
546 zfree(&prog->sec_name);
547 zfree(&prog->pin_name);
549 zfree(&prog->reloc_desc);
556 static char *__bpf_program__pin_name(struct bpf_program *prog)
560 name = p = strdup(prog->sec_name);
561 while ((p = strchr(p, '/')))
567 static bool insn_is_subprog_call(const struct bpf_insn *insn)
569 return BPF_CLASS(insn->code) == BPF_JMP &&
570 BPF_OP(insn->code) == BPF_CALL &&
571 BPF_SRC(insn->code) == BPF_K &&
572 insn->src_reg == BPF_PSEUDO_CALL &&
573 insn->dst_reg == 0 &&
578 bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
579 const char *name, size_t sec_idx, const char *sec_name,
580 size_t sec_off, void *insn_data, size_t insn_data_sz)
582 if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
583 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
584 sec_name, name, sec_off, insn_data_sz);
588 memset(prog, 0, sizeof(*prog));
591 prog->sec_idx = sec_idx;
592 prog->sec_insn_off = sec_off / BPF_INSN_SZ;
593 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
594 /* insns_cnt can later be increased by appending used subprograms */
595 prog->insns_cnt = prog->sec_insn_cnt;
597 prog->type = BPF_PROG_TYPE_UNSPEC;
600 prog->instances.fds = NULL;
601 prog->instances.nr = -1;
603 prog->sec_name = strdup(sec_name);
607 prog->name = strdup(name);
611 prog->pin_name = __bpf_program__pin_name(prog);
615 prog->insns = malloc(insn_data_sz);
618 memcpy(prog->insns, insn_data, insn_data_sz);
622 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
623 bpf_program__exit(prog);
628 bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
629 const char *sec_name, int sec_idx)
631 struct bpf_program *prog, *progs;
632 void *data = sec_data->d_buf;
633 size_t sec_sz = sec_data->d_size, sec_off, prog_sz;
638 progs = obj->programs;
639 nr_progs = obj->nr_programs;
642 while (sec_off < sec_sz) {
643 if (elf_sym_by_sec_off(obj, sec_idx, sec_off, STT_FUNC, &sym)) {
644 pr_warn("sec '%s': failed to find program symbol at offset %zu\n",
646 return -LIBBPF_ERRNO__FORMAT;
649 prog_sz = sym.st_size;
651 name = elf_sym_str(obj, sym.st_name);
653 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
655 return -LIBBPF_ERRNO__FORMAT;
658 if (sec_off + prog_sz > sec_sz) {
659 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
661 return -LIBBPF_ERRNO__FORMAT;
664 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
665 sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
667 progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
670 * In this case the original obj->programs
671 * is still valid, so don't need special treat for
672 * bpf_close_object().
674 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
678 obj->programs = progs;
680 prog = &progs[nr_progs];
682 err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
683 sec_off, data + sec_off, prog_sz);
688 obj->nr_programs = nr_progs;
696 static __u32 get_kernel_version(void)
698 __u32 major, minor, patch;
702 if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
704 return KERNEL_VERSION(major, minor, patch);
707 static const struct btf_member *
708 find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
710 struct btf_member *m;
713 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
714 if (btf_member_bit_offset(t, i) == bit_offset)
721 static const struct btf_member *
722 find_member_by_name(const struct btf *btf, const struct btf_type *t,
725 struct btf_member *m;
728 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
729 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
736 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
737 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
738 const char *name, __u32 kind);
741 find_struct_ops_kern_types(const struct btf *btf, const char *tname,
742 const struct btf_type **type, __u32 *type_id,
743 const struct btf_type **vtype, __u32 *vtype_id,
744 const struct btf_member **data_member)
746 const struct btf_type *kern_type, *kern_vtype;
747 const struct btf_member *kern_data_member;
748 __s32 kern_vtype_id, kern_type_id;
751 kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
752 if (kern_type_id < 0) {
753 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
757 kern_type = btf__type_by_id(btf, kern_type_id);
759 /* Find the corresponding "map_value" type that will be used
760 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
761 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
764 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
765 tname, BTF_KIND_STRUCT);
766 if (kern_vtype_id < 0) {
767 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
768 STRUCT_OPS_VALUE_PREFIX, tname);
769 return kern_vtype_id;
771 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
773 /* Find "struct tcp_congestion_ops" from
774 * struct bpf_struct_ops_tcp_congestion_ops {
776 * struct tcp_congestion_ops data;
779 kern_data_member = btf_members(kern_vtype);
780 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
781 if (kern_data_member->type == kern_type_id)
784 if (i == btf_vlen(kern_vtype)) {
785 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
786 tname, STRUCT_OPS_VALUE_PREFIX, tname);
791 *type_id = kern_type_id;
793 *vtype_id = kern_vtype_id;
794 *data_member = kern_data_member;
799 static bool bpf_map__is_struct_ops(const struct bpf_map *map)
801 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
804 /* Init the map's fields that depend on kern_btf */
805 static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
806 const struct btf *btf,
807 const struct btf *kern_btf)
809 const struct btf_member *member, *kern_member, *kern_data_member;
810 const struct btf_type *type, *kern_type, *kern_vtype;
811 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
812 struct bpf_struct_ops *st_ops;
813 void *data, *kern_data;
817 st_ops = map->st_ops;
819 tname = st_ops->tname;
820 err = find_struct_ops_kern_types(kern_btf, tname,
821 &kern_type, &kern_type_id,
822 &kern_vtype, &kern_vtype_id,
827 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
828 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
830 map->def.value_size = kern_vtype->size;
831 map->btf_vmlinux_value_type_id = kern_vtype_id;
833 st_ops->kern_vdata = calloc(1, kern_vtype->size);
834 if (!st_ops->kern_vdata)
838 kern_data_off = kern_data_member->offset / 8;
839 kern_data = st_ops->kern_vdata + kern_data_off;
841 member = btf_members(type);
842 for (i = 0; i < btf_vlen(type); i++, member++) {
843 const struct btf_type *mtype, *kern_mtype;
844 __u32 mtype_id, kern_mtype_id;
845 void *mdata, *kern_mdata;
846 __s64 msize, kern_msize;
847 __u32 moff, kern_moff;
848 __u32 kern_member_idx;
851 mname = btf__name_by_offset(btf, member->name_off);
852 kern_member = find_member_by_name(kern_btf, kern_type, mname);
854 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
859 kern_member_idx = kern_member - btf_members(kern_type);
860 if (btf_member_bitfield_size(type, i) ||
861 btf_member_bitfield_size(kern_type, kern_member_idx)) {
862 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
867 moff = member->offset / 8;
868 kern_moff = kern_member->offset / 8;
871 kern_mdata = kern_data + kern_moff;
873 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
874 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
876 if (BTF_INFO_KIND(mtype->info) !=
877 BTF_INFO_KIND(kern_mtype->info)) {
878 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
879 map->name, mname, BTF_INFO_KIND(mtype->info),
880 BTF_INFO_KIND(kern_mtype->info));
884 if (btf_is_ptr(mtype)) {
885 struct bpf_program *prog;
887 prog = st_ops->progs[i];
891 kern_mtype = skip_mods_and_typedefs(kern_btf,
895 /* mtype->type must be a func_proto which was
896 * guaranteed in bpf_object__collect_st_ops_relos(),
897 * so only check kern_mtype for func_proto here.
899 if (!btf_is_func_proto(kern_mtype)) {
900 pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
905 prog->attach_btf_id = kern_type_id;
906 prog->expected_attach_type = kern_member_idx;
908 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
910 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
911 map->name, mname, prog->name, moff,
917 msize = btf__resolve_size(btf, mtype_id);
918 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
919 if (msize < 0 || kern_msize < 0 || msize != kern_msize) {
920 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
921 map->name, mname, (ssize_t)msize,
922 (ssize_t)kern_msize);
926 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
927 map->name, mname, (unsigned int)msize,
929 memcpy(kern_mdata, mdata, msize);
935 static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
941 for (i = 0; i < obj->nr_maps; i++) {
944 if (!bpf_map__is_struct_ops(map))
947 err = bpf_map__init_kern_struct_ops(map, obj->btf,
956 static int bpf_object__init_struct_ops_maps(struct bpf_object *obj)
958 const struct btf_type *type, *datasec;
959 const struct btf_var_secinfo *vsi;
960 struct bpf_struct_ops *st_ops;
961 const char *tname, *var_name;
962 __s32 type_id, datasec_id;
963 const struct btf *btf;
967 if (obj->efile.st_ops_shndx == -1)
971 datasec_id = btf__find_by_name_kind(btf, STRUCT_OPS_SEC,
973 if (datasec_id < 0) {
974 pr_warn("struct_ops init: DATASEC %s not found\n",
979 datasec = btf__type_by_id(btf, datasec_id);
980 vsi = btf_var_secinfos(datasec);
981 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
982 type = btf__type_by_id(obj->btf, vsi->type);
983 var_name = btf__name_by_offset(obj->btf, type->name_off);
985 type_id = btf__resolve_type(obj->btf, vsi->type);
987 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
988 vsi->type, STRUCT_OPS_SEC);
992 type = btf__type_by_id(obj->btf, type_id);
993 tname = btf__name_by_offset(obj->btf, type->name_off);
995 pr_warn("struct_ops init: anonymous type is not supported\n");
998 if (!btf_is_struct(type)) {
999 pr_warn("struct_ops init: %s is not a struct\n", tname);
1003 map = bpf_object__add_map(obj);
1005 return PTR_ERR(map);
1007 map->sec_idx = obj->efile.st_ops_shndx;
1008 map->sec_offset = vsi->offset;
1009 map->name = strdup(var_name);
1013 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1014 map->def.key_size = sizeof(int);
1015 map->def.value_size = type->size;
1016 map->def.max_entries = 1;
1018 map->st_ops = calloc(1, sizeof(*map->st_ops));
1021 st_ops = map->st_ops;
1022 st_ops->data = malloc(type->size);
1023 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1024 st_ops->kern_func_off = malloc(btf_vlen(type) *
1025 sizeof(*st_ops->kern_func_off));
1026 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1029 if (vsi->offset + type->size > obj->efile.st_ops_data->d_size) {
1030 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
1031 var_name, STRUCT_OPS_SEC);
1035 memcpy(st_ops->data,
1036 obj->efile.st_ops_data->d_buf + vsi->offset,
1038 st_ops->tname = tname;
1039 st_ops->type = type;
1040 st_ops->type_id = type_id;
1042 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1043 tname, type_id, var_name, vsi->offset);
1049 static struct bpf_object *bpf_object__new(const char *path,
1050 const void *obj_buf,
1052 const char *obj_name)
1054 struct bpf_object *obj;
1057 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1059 pr_warn("alloc memory failed for %s\n", path);
1060 return ERR_PTR(-ENOMEM);
1063 strcpy(obj->path, path);
1065 strncpy(obj->name, obj_name, sizeof(obj->name) - 1);
1066 obj->name[sizeof(obj->name) - 1] = 0;
1068 /* Using basename() GNU version which doesn't modify arg. */
1069 strncpy(obj->name, basename((void *)path),
1070 sizeof(obj->name) - 1);
1071 end = strchr(obj->name, '.');
1078 * Caller of this function should also call
1079 * bpf_object__elf_finish() after data collection to return
1080 * obj_buf to user. If not, we should duplicate the buffer to
1081 * avoid user freeing them before elf finish.
1083 obj->efile.obj_buf = obj_buf;
1084 obj->efile.obj_buf_sz = obj_buf_sz;
1085 obj->efile.maps_shndx = -1;
1086 obj->efile.btf_maps_shndx = -1;
1087 obj->efile.data_shndx = -1;
1088 obj->efile.rodata_shndx = -1;
1089 obj->efile.bss_shndx = -1;
1090 obj->efile.st_ops_shndx = -1;
1091 obj->kconfig_map_idx = -1;
1092 obj->rodata_map_idx = -1;
1094 obj->kern_version = get_kernel_version();
1095 obj->loaded = false;
1097 INIT_LIST_HEAD(&obj->list);
1098 list_add(&obj->list, &bpf_objects_list);
1102 static void bpf_object__elf_finish(struct bpf_object *obj)
1104 if (!obj_elf_valid(obj))
1107 if (obj->efile.elf) {
1108 elf_end(obj->efile.elf);
1109 obj->efile.elf = NULL;
1111 obj->efile.symbols = NULL;
1112 obj->efile.data = NULL;
1113 obj->efile.rodata = NULL;
1114 obj->efile.bss = NULL;
1115 obj->efile.st_ops_data = NULL;
1117 zfree(&obj->efile.reloc_sects);
1118 obj->efile.nr_reloc_sects = 0;
1119 zclose(obj->efile.fd);
1120 obj->efile.obj_buf = NULL;
1121 obj->efile.obj_buf_sz = 0;
1124 /* if libelf is old and doesn't support mmap(), fall back to read() */
1125 #ifndef ELF_C_READ_MMAP
1126 #define ELF_C_READ_MMAP ELF_C_READ
1129 static int bpf_object__elf_init(struct bpf_object *obj)
1134 if (obj_elf_valid(obj)) {
1135 pr_warn("elf: init internal error\n");
1136 return -LIBBPF_ERRNO__LIBELF;
1139 if (obj->efile.obj_buf_sz > 0) {
1141 * obj_buf should have been validated by
1142 * bpf_object__open_buffer().
1144 obj->efile.elf = elf_memory((char *)obj->efile.obj_buf,
1145 obj->efile.obj_buf_sz);
1147 obj->efile.fd = open(obj->path, O_RDONLY);
1148 if (obj->efile.fd < 0) {
1149 char errmsg[STRERR_BUFSIZE], *cp;
1152 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
1153 pr_warn("elf: failed to open %s: %s\n", obj->path, cp);
1157 obj->efile.elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1160 if (!obj->efile.elf) {
1161 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
1162 err = -LIBBPF_ERRNO__LIBELF;
1166 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
1167 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
1168 err = -LIBBPF_ERRNO__FORMAT;
1171 ep = &obj->efile.ehdr;
1173 if (elf_getshdrstrndx(obj->efile.elf, &obj->efile.shstrndx)) {
1174 pr_warn("elf: failed to get section names section index for %s: %s\n",
1175 obj->path, elf_errmsg(-1));
1176 err = -LIBBPF_ERRNO__FORMAT;
1180 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1181 if (!elf_rawdata(elf_getscn(obj->efile.elf, obj->efile.shstrndx), NULL)) {
1182 pr_warn("elf: failed to get section names strings from %s: %s\n",
1183 obj->path, elf_errmsg(-1));
1184 return -LIBBPF_ERRNO__FORMAT;
1187 /* Old LLVM set e_machine to EM_NONE */
1188 if (ep->e_type != ET_REL ||
1189 (ep->e_machine && ep->e_machine != EM_BPF)) {
1190 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
1191 err = -LIBBPF_ERRNO__FORMAT;
1197 bpf_object__elf_finish(obj);
1201 static int bpf_object__check_endianness(struct bpf_object *obj)
1203 #if __BYTE_ORDER == __LITTLE_ENDIAN
1204 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1206 #elif __BYTE_ORDER == __BIG_ENDIAN
1207 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
1210 # error "Unrecognized __BYTE_ORDER__"
1212 pr_warn("elf: endianness mismatch in %s.\n", obj->path);
1213 return -LIBBPF_ERRNO__ENDIAN;
1217 bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
1219 memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
1220 pr_debug("license of %s is %s\n", obj->path, obj->license);
1225 bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1229 if (size != sizeof(kver)) {
1230 pr_warn("invalid kver section in %s\n", obj->path);
1231 return -LIBBPF_ERRNO__FORMAT;
1233 memcpy(&kver, data, sizeof(kver));
1234 obj->kern_version = kver;
1235 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1239 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1241 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1242 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1247 int bpf_object__section_size(const struct bpf_object *obj, const char *name,
1255 } else if (!strcmp(name, DATA_SEC)) {
1256 if (obj->efile.data)
1257 *size = obj->efile.data->d_size;
1258 } else if (!strcmp(name, BSS_SEC)) {
1260 *size = obj->efile.bss->d_size;
1261 } else if (!strcmp(name, RODATA_SEC)) {
1262 if (obj->efile.rodata)
1263 *size = obj->efile.rodata->d_size;
1264 } else if (!strcmp(name, STRUCT_OPS_SEC)) {
1265 if (obj->efile.st_ops_data)
1266 *size = obj->efile.st_ops_data->d_size;
1268 Elf_Scn *scn = elf_sec_by_name(obj, name);
1269 Elf_Data *data = elf_sec_data(obj, scn);
1272 ret = 0; /* found it */
1273 *size = data->d_size;
1277 return *size ? 0 : ret;
1280 int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
1283 Elf_Data *symbols = obj->efile.symbols;
1290 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
1293 if (!gelf_getsym(symbols, si, &sym))
1295 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
1296 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
1299 sname = elf_sym_str(obj, sym.st_name);
1301 pr_warn("failed to get sym name string for var %s\n",
1305 if (strcmp(name, sname) == 0) {
1306 *off = sym.st_value;
1314 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
1316 struct bpf_map *new_maps;
1320 if (obj->nr_maps < obj->maps_cap)
1321 return &obj->maps[obj->nr_maps++];
1323 new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
1324 new_maps = libbpf_reallocarray(obj->maps, new_cap, sizeof(*obj->maps));
1326 pr_warn("alloc maps for object failed\n");
1327 return ERR_PTR(-ENOMEM);
1330 obj->maps_cap = new_cap;
1331 obj->maps = new_maps;
1333 /* zero out new maps */
1334 memset(obj->maps + obj->nr_maps, 0,
1335 (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
1337 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
1338 * when failure (zclose won't close negative fd)).
1340 for (i = obj->nr_maps; i < obj->maps_cap; i++) {
1341 obj->maps[i].fd = -1;
1342 obj->maps[i].inner_map_fd = -1;
1345 return &obj->maps[obj->nr_maps++];
1348 static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1350 long page_sz = sysconf(_SC_PAGE_SIZE);
1353 map_sz = (size_t)roundup(map->def.value_size, 8) * map->def.max_entries;
1354 map_sz = roundup(map_sz, page_sz);
1358 static char *internal_map_name(struct bpf_object *obj,
1359 enum libbpf_map_type type)
1361 char map_name[BPF_OBJ_NAME_LEN], *p;
1362 const char *sfx = libbpf_type_to_btf_name[type];
1363 int sfx_len = max((size_t)7, strlen(sfx));
1364 int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1,
1367 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
1368 sfx_len, libbpf_type_to_btf_name[type]);
1370 /* sanitise map name to characters allowed by kernel */
1371 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1372 if (!isalnum(*p) && *p != '_' && *p != '.')
1375 return strdup(map_name);
1379 bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
1380 int sec_idx, void *data, size_t data_sz)
1382 struct bpf_map_def *def;
1383 struct bpf_map *map;
1386 map = bpf_object__add_map(obj);
1388 return PTR_ERR(map);
1390 map->libbpf_type = type;
1391 map->sec_idx = sec_idx;
1392 map->sec_offset = 0;
1393 map->name = internal_map_name(obj, type);
1395 pr_warn("failed to alloc map name\n");
1400 def->type = BPF_MAP_TYPE_ARRAY;
1401 def->key_size = sizeof(int);
1402 def->value_size = data_sz;
1403 def->max_entries = 1;
1404 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
1405 ? BPF_F_RDONLY_PROG : 0;
1406 def->map_flags |= BPF_F_MMAPABLE;
1408 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1409 map->name, map->sec_idx, map->sec_offset, def->map_flags);
1411 map->mmaped = mmap(NULL, bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
1412 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1413 if (map->mmaped == MAP_FAILED) {
1416 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1423 memcpy(map->mmaped, data, data_sz);
1425 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
1429 static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1434 * Populate obj->maps with libbpf internal maps.
1436 if (obj->efile.data_shndx >= 0) {
1437 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
1438 obj->efile.data_shndx,
1439 obj->efile.data->d_buf,
1440 obj->efile.data->d_size);
1444 if (obj->efile.rodata_shndx >= 0) {
1445 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
1446 obj->efile.rodata_shndx,
1447 obj->efile.rodata->d_buf,
1448 obj->efile.rodata->d_size);
1452 obj->rodata_map_idx = obj->nr_maps - 1;
1454 if (obj->efile.bss_shndx >= 0) {
1455 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
1456 obj->efile.bss_shndx,
1458 obj->efile.bss->d_size);
1466 static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1471 for (i = 0; i < obj->nr_extern; i++) {
1472 if (strcmp(obj->externs[i].name, name) == 0)
1473 return &obj->externs[i];
1478 static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1481 switch (ext->kcfg.type) {
1484 pr_warn("extern (kcfg) %s=%c should be tristate or char\n",
1488 *(bool *)ext_val = value == 'y' ? true : false;
1492 *(enum libbpf_tristate *)ext_val = TRI_YES;
1493 else if (value == 'm')
1494 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
1495 else /* value == 'n' */
1496 *(enum libbpf_tristate *)ext_val = TRI_NO;
1499 *(char *)ext_val = value;
1505 pr_warn("extern (kcfg) %s=%c should be bool, tristate, or char\n",
1513 static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
1518 if (ext->kcfg.type != KCFG_CHAR_ARR) {
1519 pr_warn("extern (kcfg) %s=%s should be char array\n", ext->name, value);
1523 len = strlen(value);
1524 if (value[len - 1] != '"') {
1525 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
1532 if (len >= ext->kcfg.sz) {
1533 pr_warn("extern (kcfg) '%s': long string config %s of (%zu bytes) truncated to %d bytes\n",
1534 ext->name, value, len, ext->kcfg.sz - 1);
1535 len = ext->kcfg.sz - 1;
1537 memcpy(ext_val, value + 1, len);
1538 ext_val[len] = '\0';
1543 static int parse_u64(const char *value, __u64 *res)
1549 *res = strtoull(value, &value_end, 0);
1552 pr_warn("failed to parse '%s' as integer: %d\n", value, err);
1556 pr_warn("failed to parse '%s' as integer completely\n", value);
1562 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
1564 int bit_sz = ext->kcfg.sz * 8;
1566 if (ext->kcfg.sz == 8)
1569 /* Validate that value stored in u64 fits in integer of `ext->sz`
1570 * bytes size without any loss of information. If the target integer
1571 * is signed, we rely on the following limits of integer type of
1572 * Y bits and subsequent transformation:
1574 * -2^(Y-1) <= X <= 2^(Y-1) - 1
1575 * 0 <= X + 2^(Y-1) <= 2^Y - 1
1576 * 0 <= X + 2^(Y-1) < 2^Y
1578 * For unsigned target integer, check that all the (64 - Y) bits are
1581 if (ext->kcfg.is_signed)
1582 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
1584 return (v >> bit_sz) == 0;
1587 static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
1590 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
1591 pr_warn("extern (kcfg) %s=%llu should be integer\n",
1592 ext->name, (unsigned long long)value);
1595 if (!is_kcfg_value_in_range(ext, value)) {
1596 pr_warn("extern (kcfg) %s=%llu value doesn't fit in %d bytes\n",
1597 ext->name, (unsigned long long)value, ext->kcfg.sz);
1600 switch (ext->kcfg.sz) {
1601 case 1: *(__u8 *)ext_val = value; break;
1602 case 2: *(__u16 *)ext_val = value; break;
1603 case 4: *(__u32 *)ext_val = value; break;
1604 case 8: *(__u64 *)ext_val = value; break;
1612 static int bpf_object__process_kconfig_line(struct bpf_object *obj,
1613 char *buf, void *data)
1615 struct extern_desc *ext;
1621 if (strncmp(buf, "CONFIG_", 7))
1624 sep = strchr(buf, '=');
1626 pr_warn("failed to parse '%s': no separator\n", buf);
1630 /* Trim ending '\n' */
1632 if (buf[len - 1] == '\n')
1633 buf[len - 1] = '\0';
1634 /* Split on '=' and ensure that a value is present. */
1638 pr_warn("failed to parse '%s': no value\n", buf);
1642 ext = find_extern_by_name(obj, buf);
1643 if (!ext || ext->is_set)
1646 ext_val = data + ext->kcfg.data_off;
1650 case 'y': case 'n': case 'm':
1651 err = set_kcfg_value_tri(ext, ext_val, *value);
1654 err = set_kcfg_value_str(ext, ext_val, value);
1657 /* assume integer */
1658 err = parse_u64(value, &num);
1660 pr_warn("extern (kcfg) %s=%s should be integer\n",
1664 err = set_kcfg_value_num(ext, ext_val, num);
1669 pr_debug("extern (kcfg) %s=%s\n", ext->name, value);
1673 static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
1681 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
1684 else if (len >= PATH_MAX)
1685 return -ENAMETOOLONG;
1687 /* gzopen also accepts uncompressed files. */
1688 file = gzopen(buf, "r");
1690 file = gzopen("/proc/config.gz", "r");
1693 pr_warn("failed to open system Kconfig\n");
1697 while (gzgets(file, buf, sizeof(buf))) {
1698 err = bpf_object__process_kconfig_line(obj, buf, data);
1700 pr_warn("error parsing system Kconfig line '%s': %d\n",
1711 static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
1712 const char *config, void *data)
1718 file = fmemopen((void *)config, strlen(config), "r");
1721 pr_warn("failed to open in-memory Kconfig: %d\n", err);
1725 while (fgets(buf, sizeof(buf), file)) {
1726 err = bpf_object__process_kconfig_line(obj, buf, data);
1728 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
1738 static int bpf_object__init_kconfig_map(struct bpf_object *obj)
1740 struct extern_desc *last_ext = NULL, *ext;
1744 for (i = 0; i < obj->nr_extern; i++) {
1745 ext = &obj->externs[i];
1746 if (ext->type == EXT_KCFG)
1753 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
1754 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
1755 obj->efile.symbols_shndx,
1760 obj->kconfig_map_idx = obj->nr_maps - 1;
1765 static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
1767 Elf_Data *symbols = obj->efile.symbols;
1768 int i, map_def_sz = 0, nr_maps = 0, nr_syms;
1769 Elf_Data *data = NULL;
1772 if (obj->efile.maps_shndx < 0)
1779 scn = elf_sec_by_idx(obj, obj->efile.maps_shndx);
1780 data = elf_sec_data(obj, scn);
1781 if (!scn || !data) {
1782 pr_warn("elf: failed to get legacy map definitions for %s\n",
1788 * Count number of maps. Each map has a name.
1789 * Array of maps is not supported: only the first element is
1792 * TODO: Detect array of map and report error.
1794 nr_syms = symbols->d_size / sizeof(GElf_Sym);
1795 for (i = 0; i < nr_syms; i++) {
1798 if (!gelf_getsym(symbols, i, &sym))
1800 if (sym.st_shndx != obj->efile.maps_shndx)
1804 /* Assume equally sized map definitions */
1805 pr_debug("elf: found %d legacy map definitions (%zd bytes) in %s\n",
1806 nr_maps, data->d_size, obj->path);
1808 if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) {
1809 pr_warn("elf: unable to determine legacy map definition size in %s\n",
1813 map_def_sz = data->d_size / nr_maps;
1815 /* Fill obj->maps using data in "maps" section. */
1816 for (i = 0; i < nr_syms; i++) {
1818 const char *map_name;
1819 struct bpf_map_def *def;
1820 struct bpf_map *map;
1822 if (!gelf_getsym(symbols, i, &sym))
1824 if (sym.st_shndx != obj->efile.maps_shndx)
1827 map = bpf_object__add_map(obj);
1829 return PTR_ERR(map);
1831 map_name = elf_sym_str(obj, sym.st_name);
1833 pr_warn("failed to get map #%d name sym string for obj %s\n",
1835 return -LIBBPF_ERRNO__FORMAT;
1838 map->libbpf_type = LIBBPF_MAP_UNSPEC;
1839 map->sec_idx = sym.st_shndx;
1840 map->sec_offset = sym.st_value;
1841 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
1842 map_name, map->sec_idx, map->sec_offset);
1843 if (sym.st_value + map_def_sz > data->d_size) {
1844 pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
1845 obj->path, map_name);
1849 map->name = strdup(map_name);
1851 pr_warn("failed to alloc map name\n");
1854 pr_debug("map %d is \"%s\"\n", i, map->name);
1855 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
1857 * If the definition of the map in the object file fits in
1858 * bpf_map_def, copy it. Any extra fields in our version
1859 * of bpf_map_def will default to zero as a result of the
1862 if (map_def_sz <= sizeof(struct bpf_map_def)) {
1863 memcpy(&map->def, def, map_def_sz);
1866 * Here the map structure being read is bigger than what
1867 * we expect, truncate if the excess bits are all zero.
1868 * If they are not zero, reject this map as
1873 for (b = ((char *)def) + sizeof(struct bpf_map_def);
1874 b < ((char *)def) + map_def_sz; b++) {
1876 pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n",
1877 obj->path, map_name);
1882 memcpy(&map->def, def, sizeof(struct bpf_map_def));
1888 static const struct btf_type *
1889 skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
1891 const struct btf_type *t = btf__type_by_id(btf, id);
1896 while (btf_is_mod(t) || btf_is_typedef(t)) {
1899 t = btf__type_by_id(btf, t->type);
1905 static const struct btf_type *
1906 resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
1908 const struct btf_type *t;
1910 t = skip_mods_and_typedefs(btf, id, NULL);
1914 t = skip_mods_and_typedefs(btf, t->type, res_id);
1916 return btf_is_func_proto(t) ? t : NULL;
1919 static const char *btf_kind_str(const struct btf_type *t)
1921 switch (btf_kind(t)) {
1922 case BTF_KIND_UNKN: return "void";
1923 case BTF_KIND_INT: return "int";
1924 case BTF_KIND_PTR: return "ptr";
1925 case BTF_KIND_ARRAY: return "array";
1926 case BTF_KIND_STRUCT: return "struct";
1927 case BTF_KIND_UNION: return "union";
1928 case BTF_KIND_ENUM: return "enum";
1929 case BTF_KIND_FWD: return "fwd";
1930 case BTF_KIND_TYPEDEF: return "typedef";
1931 case BTF_KIND_VOLATILE: return "volatile";
1932 case BTF_KIND_CONST: return "const";
1933 case BTF_KIND_RESTRICT: return "restrict";
1934 case BTF_KIND_FUNC: return "func";
1935 case BTF_KIND_FUNC_PROTO: return "func_proto";
1936 case BTF_KIND_VAR: return "var";
1937 case BTF_KIND_DATASEC: return "datasec";
1938 default: return "unknown";
1943 * Fetch integer attribute of BTF map definition. Such attributes are
1944 * represented using a pointer to an array, in which dimensionality of array
1945 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1946 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1947 * type definition, while using only sizeof(void *) space in ELF data section.
1949 static bool get_map_field_int(const char *map_name, const struct btf *btf,
1950 const struct btf_member *m, __u32 *res)
1952 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
1953 const char *name = btf__name_by_offset(btf, m->name_off);
1954 const struct btf_array *arr_info;
1955 const struct btf_type *arr_t;
1957 if (!btf_is_ptr(t)) {
1958 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
1959 map_name, name, btf_kind_str(t));
1963 arr_t = btf__type_by_id(btf, t->type);
1965 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
1966 map_name, name, t->type);
1969 if (!btf_is_array(arr_t)) {
1970 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
1971 map_name, name, btf_kind_str(arr_t));
1974 arr_info = btf_array(arr_t);
1975 *res = arr_info->nelems;
1979 static int build_map_pin_path(struct bpf_map *map, const char *path)
1985 path = "/sys/fs/bpf";
1987 len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
1990 else if (len >= PATH_MAX)
1991 return -ENAMETOOLONG;
1993 return bpf_map__set_pin_path(map, buf);
1997 static int parse_btf_map_def(struct bpf_object *obj,
1998 struct bpf_map *map,
1999 const struct btf_type *def,
2000 bool strict, bool is_inner,
2001 const char *pin_root_path)
2003 const struct btf_type *t;
2004 const struct btf_member *m;
2007 vlen = btf_vlen(def);
2008 m = btf_members(def);
2009 for (i = 0; i < vlen; i++, m++) {
2010 const char *name = btf__name_by_offset(obj->btf, m->name_off);
2013 pr_warn("map '%s': invalid field #%d.\n", map->name, i);
2016 if (strcmp(name, "type") == 0) {
2017 if (!get_map_field_int(map->name, obj->btf, m,
2020 pr_debug("map '%s': found type = %u.\n",
2021 map->name, map->def.type);
2022 } else if (strcmp(name, "max_entries") == 0) {
2023 if (!get_map_field_int(map->name, obj->btf, m,
2024 &map->def.max_entries))
2026 pr_debug("map '%s': found max_entries = %u.\n",
2027 map->name, map->def.max_entries);
2028 } else if (strcmp(name, "map_flags") == 0) {
2029 if (!get_map_field_int(map->name, obj->btf, m,
2030 &map->def.map_flags))
2032 pr_debug("map '%s': found map_flags = %u.\n",
2033 map->name, map->def.map_flags);
2034 } else if (strcmp(name, "numa_node") == 0) {
2035 if (!get_map_field_int(map->name, obj->btf, m, &map->numa_node))
2037 pr_debug("map '%s': found numa_node = %u.\n", map->name, map->numa_node);
2038 } else if (strcmp(name, "key_size") == 0) {
2041 if (!get_map_field_int(map->name, obj->btf, m, &sz))
2043 pr_debug("map '%s': found key_size = %u.\n",
2045 if (map->def.key_size && map->def.key_size != sz) {
2046 pr_warn("map '%s': conflicting key size %u != %u.\n",
2047 map->name, map->def.key_size, sz);
2050 map->def.key_size = sz;
2051 } else if (strcmp(name, "key") == 0) {
2054 t = btf__type_by_id(obj->btf, m->type);
2056 pr_warn("map '%s': key type [%d] not found.\n",
2057 map->name, m->type);
2060 if (!btf_is_ptr(t)) {
2061 pr_warn("map '%s': key spec is not PTR: %s.\n",
2062 map->name, btf_kind_str(t));
2065 sz = btf__resolve_size(obj->btf, t->type);
2067 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2068 map->name, t->type, (ssize_t)sz);
2071 pr_debug("map '%s': found key [%u], sz = %zd.\n",
2072 map->name, t->type, (ssize_t)sz);
2073 if (map->def.key_size && map->def.key_size != sz) {
2074 pr_warn("map '%s': conflicting key size %u != %zd.\n",
2075 map->name, map->def.key_size, (ssize_t)sz);
2078 map->def.key_size = sz;
2079 map->btf_key_type_id = t->type;
2080 } else if (strcmp(name, "value_size") == 0) {
2083 if (!get_map_field_int(map->name, obj->btf, m, &sz))
2085 pr_debug("map '%s': found value_size = %u.\n",
2087 if (map->def.value_size && map->def.value_size != sz) {
2088 pr_warn("map '%s': conflicting value size %u != %u.\n",
2089 map->name, map->def.value_size, sz);
2092 map->def.value_size = sz;
2093 } else if (strcmp(name, "value") == 0) {
2096 t = btf__type_by_id(obj->btf, m->type);
2098 pr_warn("map '%s': value type [%d] not found.\n",
2099 map->name, m->type);
2102 if (!btf_is_ptr(t)) {
2103 pr_warn("map '%s': value spec is not PTR: %s.\n",
2104 map->name, btf_kind_str(t));
2107 sz = btf__resolve_size(obj->btf, t->type);
2109 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2110 map->name, t->type, (ssize_t)sz);
2113 pr_debug("map '%s': found value [%u], sz = %zd.\n",
2114 map->name, t->type, (ssize_t)sz);
2115 if (map->def.value_size && map->def.value_size != sz) {
2116 pr_warn("map '%s': conflicting value size %u != %zd.\n",
2117 map->name, map->def.value_size, (ssize_t)sz);
2120 map->def.value_size = sz;
2121 map->btf_value_type_id = t->type;
2123 else if (strcmp(name, "values") == 0) {
2127 pr_warn("map '%s': multi-level inner maps not supported.\n",
2131 if (i != vlen - 1) {
2132 pr_warn("map '%s': '%s' member should be last.\n",
2136 if (!bpf_map_type__is_map_in_map(map->def.type)) {
2137 pr_warn("map '%s': should be map-in-map.\n",
2141 if (map->def.value_size && map->def.value_size != 4) {
2142 pr_warn("map '%s': conflicting value size %u != 4.\n",
2143 map->name, map->def.value_size);
2146 map->def.value_size = 4;
2147 t = btf__type_by_id(obj->btf, m->type);
2149 pr_warn("map '%s': map-in-map inner type [%d] not found.\n",
2150 map->name, m->type);
2153 if (!btf_is_array(t) || btf_array(t)->nelems) {
2154 pr_warn("map '%s': map-in-map inner spec is not a zero-sized array.\n",
2158 t = skip_mods_and_typedefs(obj->btf, btf_array(t)->type,
2160 if (!btf_is_ptr(t)) {
2161 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2162 map->name, btf_kind_str(t));
2165 t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
2166 if (!btf_is_struct(t)) {
2167 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2168 map->name, btf_kind_str(t));
2172 map->inner_map = calloc(1, sizeof(*map->inner_map));
2173 if (!map->inner_map)
2175 map->inner_map->sec_idx = obj->efile.btf_maps_shndx;
2176 map->inner_map->name = malloc(strlen(map->name) +
2177 sizeof(".inner") + 1);
2178 if (!map->inner_map->name)
2180 sprintf(map->inner_map->name, "%s.inner", map->name);
2182 err = parse_btf_map_def(obj, map->inner_map, t, strict,
2183 true /* is_inner */, NULL);
2186 } else if (strcmp(name, "pinning") == 0) {
2191 pr_debug("map '%s': inner def can't be pinned.\n",
2195 if (!get_map_field_int(map->name, obj->btf, m, &val))
2197 pr_debug("map '%s': found pinning = %u.\n",
2200 if (val != LIBBPF_PIN_NONE &&
2201 val != LIBBPF_PIN_BY_NAME) {
2202 pr_warn("map '%s': invalid pinning value %u.\n",
2206 if (val == LIBBPF_PIN_BY_NAME) {
2207 err = build_map_pin_path(map, pin_root_path);
2209 pr_warn("map '%s': couldn't build pin path.\n",
2216 pr_warn("map '%s': unknown field '%s'.\n",
2220 pr_debug("map '%s': ignoring unknown field '%s'.\n",
2225 if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
2226 pr_warn("map '%s': map type isn't specified.\n", map->name);
2233 static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2234 const struct btf_type *sec,
2235 int var_idx, int sec_idx,
2236 const Elf_Data *data, bool strict,
2237 const char *pin_root_path)
2239 const struct btf_type *var, *def;
2240 const struct btf_var_secinfo *vi;
2241 const struct btf_var *var_extra;
2242 const char *map_name;
2243 struct bpf_map *map;
2245 vi = btf_var_secinfos(sec) + var_idx;
2246 var = btf__type_by_id(obj->btf, vi->type);
2247 var_extra = btf_var(var);
2248 map_name = btf__name_by_offset(obj->btf, var->name_off);
2250 if (map_name == NULL || map_name[0] == '\0') {
2251 pr_warn("map #%d: empty name.\n", var_idx);
2254 if ((__u64)vi->offset + vi->size > data->d_size) {
2255 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2258 if (!btf_is_var(var)) {
2259 pr_warn("map '%s': unexpected var kind %s.\n",
2260 map_name, btf_kind_str(var));
2263 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2264 var_extra->linkage != BTF_VAR_STATIC) {
2265 pr_warn("map '%s': unsupported var linkage %u.\n",
2266 map_name, var_extra->linkage);
2270 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2271 if (!btf_is_struct(def)) {
2272 pr_warn("map '%s': unexpected def kind %s.\n",
2273 map_name, btf_kind_str(var));
2276 if (def->size > vi->size) {
2277 pr_warn("map '%s': invalid def size.\n", map_name);
2281 map = bpf_object__add_map(obj);
2283 return PTR_ERR(map);
2284 map->name = strdup(map_name);
2286 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2289 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2290 map->def.type = BPF_MAP_TYPE_UNSPEC;
2291 map->sec_idx = sec_idx;
2292 map->sec_offset = vi->offset;
2293 map->btf_var_idx = var_idx;
2294 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2295 map_name, map->sec_idx, map->sec_offset);
2297 return parse_btf_map_def(obj, map, def, strict, false, pin_root_path);
2300 static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2301 const char *pin_root_path)
2303 const struct btf_type *sec = NULL;
2304 int nr_types, i, vlen, err;
2305 const struct btf_type *t;
2310 if (obj->efile.btf_maps_shndx < 0)
2313 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2314 data = elf_sec_data(obj, scn);
2315 if (!scn || !data) {
2316 pr_warn("elf: failed to get %s map definitions for %s\n",
2317 MAPS_ELF_SEC, obj->path);
2321 nr_types = btf__get_nr_types(obj->btf);
2322 for (i = 1; i <= nr_types; i++) {
2323 t = btf__type_by_id(obj->btf, i);
2324 if (!btf_is_datasec(t))
2326 name = btf__name_by_offset(obj->btf, t->name_off);
2327 if (strcmp(name, MAPS_ELF_SEC) == 0) {
2329 obj->efile.btf_maps_sec_btf_id = i;
2335 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
2339 vlen = btf_vlen(sec);
2340 for (i = 0; i < vlen; i++) {
2341 err = bpf_object__init_user_btf_map(obj, sec, i,
2342 obj->efile.btf_maps_shndx,
2352 static int bpf_object__init_maps(struct bpf_object *obj,
2353 const struct bpf_object_open_opts *opts)
2355 const char *pin_root_path;
2359 strict = !OPTS_GET(opts, relaxed_maps, false);
2360 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
2362 err = bpf_object__init_user_maps(obj, strict);
2363 err = err ?: bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
2364 err = err ?: bpf_object__init_global_data_maps(obj);
2365 err = err ?: bpf_object__init_kconfig_map(obj);
2366 err = err ?: bpf_object__init_struct_ops_maps(obj);
2373 static bool section_have_execinstr(struct bpf_object *obj, int idx)
2377 if (elf_sec_hdr(obj, elf_sec_by_idx(obj, idx), &sh))
2380 return sh.sh_flags & SHF_EXECINSTR;
2383 static bool btf_needs_sanitization(struct bpf_object *obj)
2385 bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
2386 bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
2387 bool has_func = kernel_supports(FEAT_BTF_FUNC);
2389 return !has_func || !has_datasec || !has_func_global;
2392 static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
2394 bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
2395 bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
2396 bool has_func = kernel_supports(FEAT_BTF_FUNC);
2400 for (i = 1; i <= btf__get_nr_types(btf); i++) {
2401 t = (struct btf_type *)btf__type_by_id(btf, i);
2403 if (!has_datasec && btf_is_var(t)) {
2404 /* replace VAR with INT */
2405 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
2407 * using size = 1 is the safest choice, 4 will be too
2408 * big and cause kernel BTF validation failure if
2409 * original variable took less than 4 bytes
2412 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
2413 } else if (!has_datasec && btf_is_datasec(t)) {
2414 /* replace DATASEC with STRUCT */
2415 const struct btf_var_secinfo *v = btf_var_secinfos(t);
2416 struct btf_member *m = btf_members(t);
2417 struct btf_type *vt;
2420 name = (char *)btf__name_by_offset(btf, t->name_off);
2428 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
2429 for (j = 0; j < vlen; j++, v++, m++) {
2430 /* order of field assignments is important */
2431 m->offset = v->offset * 8;
2433 /* preserve variable name as member name */
2434 vt = (void *)btf__type_by_id(btf, v->type);
2435 m->name_off = vt->name_off;
2437 } else if (!has_func && btf_is_func_proto(t)) {
2438 /* replace FUNC_PROTO with ENUM */
2440 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
2441 t->size = sizeof(__u32); /* kernel enforced */
2442 } else if (!has_func && btf_is_func(t)) {
2443 /* replace FUNC with TYPEDEF */
2444 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
2445 } else if (!has_func_global && btf_is_func(t)) {
2446 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2447 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
2452 static bool libbpf_needs_btf(const struct bpf_object *obj)
2454 return obj->efile.btf_maps_shndx >= 0 ||
2455 obj->efile.st_ops_shndx >= 0 ||
2459 static bool kernel_needs_btf(const struct bpf_object *obj)
2461 return obj->efile.st_ops_shndx >= 0;
2464 static int bpf_object__init_btf(struct bpf_object *obj,
2466 Elf_Data *btf_ext_data)
2471 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
2472 if (IS_ERR(obj->btf)) {
2473 err = PTR_ERR(obj->btf);
2475 pr_warn("Error loading ELF section %s: %d.\n",
2479 /* enforce 8-byte pointers for BPF-targeted BTFs */
2480 btf__set_pointer_size(obj->btf, 8);
2485 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2486 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
2489 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
2490 btf_ext_data->d_size);
2491 if (IS_ERR(obj->btf_ext)) {
2492 pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n",
2493 BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
2494 obj->btf_ext = NULL;
2499 if (err && libbpf_needs_btf(obj)) {
2500 pr_warn("BTF is required, but is missing or corrupted.\n");
2506 static int bpf_object__finalize_btf(struct bpf_object *obj)
2513 err = btf__finalize_data(obj, obj->btf);
2515 pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err);
2522 static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
2524 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
2525 prog->type == BPF_PROG_TYPE_LSM)
2528 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
2529 * also need vmlinux BTF
2531 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
2537 static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
2539 struct bpf_program *prog;
2542 /* CO-RE relocations need kernel BTF */
2543 if (obj->btf_ext && obj->btf_ext->core_relo_info.len)
2546 /* Support for typed ksyms needs kernel BTF */
2547 for (i = 0; i < obj->nr_extern; i++) {
2548 const struct extern_desc *ext;
2550 ext = &obj->externs[i];
2551 if (ext->type == EXT_KSYM && ext->ksym.type_id)
2555 bpf_object__for_each_program(prog, obj) {
2558 if (prog_needs_vmlinux_btf(prog))
2565 static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
2569 /* btf_vmlinux could be loaded earlier */
2570 if (obj->btf_vmlinux)
2573 if (!force && !obj_needs_vmlinux_btf(obj))
2576 obj->btf_vmlinux = libbpf_find_kernel_btf();
2577 if (IS_ERR(obj->btf_vmlinux)) {
2578 err = PTR_ERR(obj->btf_vmlinux);
2579 pr_warn("Error loading vmlinux BTF: %d\n", err);
2580 obj->btf_vmlinux = NULL;
2586 static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
2588 struct btf *kern_btf = obj->btf;
2589 bool btf_mandatory, sanitize;
2595 if (!kernel_supports(FEAT_BTF)) {
2596 if (kernel_needs_btf(obj)) {
2600 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
2604 sanitize = btf_needs_sanitization(obj);
2606 const void *raw_data;
2609 /* clone BTF to sanitize a copy and leave the original intact */
2610 raw_data = btf__get_raw_data(obj->btf, &sz);
2611 kern_btf = btf__new(raw_data, sz);
2612 if (IS_ERR(kern_btf))
2613 return PTR_ERR(kern_btf);
2615 /* enforce 8-byte pointers for BPF-targeted BTFs */
2616 btf__set_pointer_size(obj->btf, 8);
2617 bpf_object__sanitize_btf(obj, kern_btf);
2620 err = btf__load(kern_btf);
2623 /* move fd to libbpf's BTF */
2624 btf__set_fd(obj->btf, btf__fd(kern_btf));
2625 btf__set_fd(kern_btf, -1);
2627 btf__free(kern_btf);
2631 btf_mandatory = kernel_needs_btf(obj);
2632 pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
2633 btf_mandatory ? "BTF is mandatory, can't proceed."
2634 : "BTF is optional, ignoring.");
2641 static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
2645 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
2647 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
2648 off, obj->path, elf_errmsg(-1));
2655 static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
2659 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
2661 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
2662 off, obj->path, elf_errmsg(-1));
2669 static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
2673 scn = elf_getscn(obj->efile.elf, idx);
2675 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
2676 idx, obj->path, elf_errmsg(-1));
2682 static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
2684 Elf_Scn *scn = NULL;
2685 Elf *elf = obj->efile.elf;
2686 const char *sec_name;
2688 while ((scn = elf_nextscn(elf, scn)) != NULL) {
2689 sec_name = elf_sec_name(obj, scn);
2693 if (strcmp(sec_name, name) != 0)
2701 static int elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn, GElf_Shdr *hdr)
2706 if (gelf_getshdr(scn, hdr) != hdr) {
2707 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
2708 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
2715 static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
2723 if (elf_sec_hdr(obj, scn, &sh))
2726 name = elf_sec_str(obj, sh.sh_name);
2728 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
2729 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
2736 static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
2743 data = elf_getdata(scn, 0);
2745 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
2746 elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
2747 obj->path, elf_errmsg(-1));
2754 static int elf_sym_by_sec_off(const struct bpf_object *obj, size_t sec_idx,
2755 size_t off, __u32 sym_type, GElf_Sym *sym)
2757 Elf_Data *symbols = obj->efile.symbols;
2758 size_t n = symbols->d_size / sizeof(GElf_Sym);
2761 for (i = 0; i < n; i++) {
2762 if (!gelf_getsym(symbols, i, sym))
2764 if (sym->st_shndx != sec_idx || sym->st_value != off)
2766 if (GELF_ST_TYPE(sym->st_info) != sym_type)
2774 static bool is_sec_name_dwarf(const char *name)
2776 /* approximation, but the actual list is too long */
2777 return strncmp(name, ".debug_", sizeof(".debug_") - 1) == 0;
2780 static bool ignore_elf_section(GElf_Shdr *hdr, const char *name)
2782 /* no special handling of .strtab */
2783 if (hdr->sh_type == SHT_STRTAB)
2786 /* ignore .llvm_addrsig section as well */
2787 if (hdr->sh_type == 0x6FFF4C03 /* SHT_LLVM_ADDRSIG */)
2790 /* no subprograms will lead to an empty .text section, ignore it */
2791 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
2792 strcmp(name, ".text") == 0)
2795 /* DWARF sections */
2796 if (is_sec_name_dwarf(name))
2799 if (strncmp(name, ".rel", sizeof(".rel") - 1) == 0) {
2800 name += sizeof(".rel") - 1;
2801 /* DWARF section relocations */
2802 if (is_sec_name_dwarf(name))
2805 /* .BTF and .BTF.ext don't need relocations */
2806 if (strcmp(name, BTF_ELF_SEC) == 0 ||
2807 strcmp(name, BTF_EXT_ELF_SEC) == 0)
2814 static int cmp_progs(const void *_a, const void *_b)
2816 const struct bpf_program *a = _a;
2817 const struct bpf_program *b = _b;
2819 if (a->sec_idx != b->sec_idx)
2820 return a->sec_idx < b->sec_idx ? -1 : 1;
2822 /* sec_insn_off can't be the same within the section */
2823 return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
2826 static int bpf_object__elf_collect(struct bpf_object *obj)
2828 Elf *elf = obj->efile.elf;
2829 Elf_Data *btf_ext_data = NULL;
2830 Elf_Data *btf_data = NULL;
2831 int idx = 0, err = 0;
2837 /* a bunch of ELF parsing functionality depends on processing symbols,
2838 * so do the first pass and find the symbol table
2841 while ((scn = elf_nextscn(elf, scn)) != NULL) {
2842 if (elf_sec_hdr(obj, scn, &sh))
2843 return -LIBBPF_ERRNO__FORMAT;
2845 if (sh.sh_type == SHT_SYMTAB) {
2846 if (obj->efile.symbols) {
2847 pr_warn("elf: multiple symbol tables in %s\n", obj->path);
2848 return -LIBBPF_ERRNO__FORMAT;
2851 data = elf_sec_data(obj, scn);
2853 return -LIBBPF_ERRNO__FORMAT;
2855 obj->efile.symbols = data;
2856 obj->efile.symbols_shndx = elf_ndxscn(scn);
2857 obj->efile.strtabidx = sh.sh_link;
2862 while ((scn = elf_nextscn(elf, scn)) != NULL) {
2865 if (elf_sec_hdr(obj, scn, &sh))
2866 return -LIBBPF_ERRNO__FORMAT;
2868 name = elf_sec_str(obj, sh.sh_name);
2870 return -LIBBPF_ERRNO__FORMAT;
2872 if (ignore_elf_section(&sh, name))
2875 data = elf_sec_data(obj, scn);
2877 return -LIBBPF_ERRNO__FORMAT;
2879 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
2880 idx, name, (unsigned long)data->d_size,
2881 (int)sh.sh_link, (unsigned long)sh.sh_flags,
2884 if (strcmp(name, "license") == 0) {
2885 err = bpf_object__init_license(obj, data->d_buf, data->d_size);
2888 } else if (strcmp(name, "version") == 0) {
2889 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
2892 } else if (strcmp(name, "maps") == 0) {
2893 obj->efile.maps_shndx = idx;
2894 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
2895 obj->efile.btf_maps_shndx = idx;
2896 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
2898 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
2899 btf_ext_data = data;
2900 } else if (sh.sh_type == SHT_SYMTAB) {
2901 /* already processed during the first pass above */
2902 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
2903 if (sh.sh_flags & SHF_EXECINSTR) {
2904 if (strcmp(name, ".text") == 0)
2905 obj->efile.text_shndx = idx;
2906 err = bpf_object__add_programs(obj, data, name, idx);
2909 } else if (strcmp(name, DATA_SEC) == 0) {
2910 obj->efile.data = data;
2911 obj->efile.data_shndx = idx;
2912 } else if (strcmp(name, RODATA_SEC) == 0) {
2913 obj->efile.rodata = data;
2914 obj->efile.rodata_shndx = idx;
2915 } else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
2916 obj->efile.st_ops_data = data;
2917 obj->efile.st_ops_shndx = idx;
2919 pr_info("elf: skipping unrecognized data section(%d) %s\n",
2922 } else if (sh.sh_type == SHT_REL) {
2923 int nr_sects = obj->efile.nr_reloc_sects;
2924 void *sects = obj->efile.reloc_sects;
2925 int sec = sh.sh_info; /* points to other section */
2927 /* Only do relo for section with exec instructions */
2928 if (!section_have_execinstr(obj, sec) &&
2929 strcmp(name, ".rel" STRUCT_OPS_SEC) &&
2930 strcmp(name, ".rel" MAPS_ELF_SEC)) {
2931 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
2933 elf_sec_name(obj, elf_sec_by_idx(obj, sec)) ?: "<?>");
2937 sects = libbpf_reallocarray(sects, nr_sects + 1,
2938 sizeof(*obj->efile.reloc_sects));
2942 obj->efile.reloc_sects = sects;
2943 obj->efile.nr_reloc_sects++;
2945 obj->efile.reloc_sects[nr_sects].shdr = sh;
2946 obj->efile.reloc_sects[nr_sects].data = data;
2947 } else if (sh.sh_type == SHT_NOBITS && strcmp(name, BSS_SEC) == 0) {
2948 obj->efile.bss = data;
2949 obj->efile.bss_shndx = idx;
2951 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
2952 (size_t)sh.sh_size);
2956 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
2957 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
2958 return -LIBBPF_ERRNO__FORMAT;
2961 /* sort BPF programs by section name and in-section instruction offset
2962 * for faster search */
2963 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
2965 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
2968 static bool sym_is_extern(const GElf_Sym *sym)
2970 int bind = GELF_ST_BIND(sym->st_info);
2971 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
2972 return sym->st_shndx == SHN_UNDEF &&
2973 (bind == STB_GLOBAL || bind == STB_WEAK) &&
2974 GELF_ST_TYPE(sym->st_info) == STT_NOTYPE;
2977 static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
2979 const struct btf_type *t;
2980 const char *var_name;
2986 n = btf__get_nr_types(btf);
2987 for (i = 1; i <= n; i++) {
2988 t = btf__type_by_id(btf, i);
2993 var_name = btf__name_by_offset(btf, t->name_off);
2994 if (strcmp(var_name, ext_name))
2997 if (btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
3006 static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
3007 const struct btf_var_secinfo *vs;
3008 const struct btf_type *t;
3014 n = btf__get_nr_types(btf);
3015 for (i = 1; i <= n; i++) {
3016 t = btf__type_by_id(btf, i);
3018 if (!btf_is_datasec(t))
3021 vs = btf_var_secinfos(t);
3022 for (j = 0; j < btf_vlen(t); j++, vs++) {
3023 if (vs->type == ext_btf_id)
3031 static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
3034 const struct btf_type *t;
3037 t = skip_mods_and_typedefs(btf, id, NULL);
3038 name = btf__name_by_offset(btf, t->name_off);
3042 switch (btf_kind(t)) {
3043 case BTF_KIND_INT: {
3044 int enc = btf_int_encoding(t);
3046 if (enc & BTF_INT_BOOL)
3047 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
3049 *is_signed = enc & BTF_INT_SIGNED;
3052 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
3053 return KCFG_UNKNOWN;
3058 return KCFG_UNKNOWN;
3059 if (strcmp(name, "libbpf_tristate"))
3060 return KCFG_UNKNOWN;
3061 return KCFG_TRISTATE;
3062 case BTF_KIND_ARRAY:
3063 if (btf_array(t)->nelems == 0)
3064 return KCFG_UNKNOWN;
3065 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
3066 return KCFG_UNKNOWN;
3067 return KCFG_CHAR_ARR;
3069 return KCFG_UNKNOWN;
3073 static int cmp_externs(const void *_a, const void *_b)
3075 const struct extern_desc *a = _a;
3076 const struct extern_desc *b = _b;
3078 if (a->type != b->type)
3079 return a->type < b->type ? -1 : 1;
3081 if (a->type == EXT_KCFG) {
3082 /* descending order by alignment requirements */
3083 if (a->kcfg.align != b->kcfg.align)
3084 return a->kcfg.align > b->kcfg.align ? -1 : 1;
3085 /* ascending order by size, within same alignment class */
3086 if (a->kcfg.sz != b->kcfg.sz)
3087 return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
3090 /* resolve ties by name */
3091 return strcmp(a->name, b->name);
3094 static int find_int_btf_id(const struct btf *btf)
3096 const struct btf_type *t;
3099 n = btf__get_nr_types(btf);
3100 for (i = 1; i <= n; i++) {
3101 t = btf__type_by_id(btf, i);
3103 if (btf_is_int(t) && btf_int_bits(t) == 32)
3110 static int bpf_object__collect_externs(struct bpf_object *obj)
3112 struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
3113 const struct btf_type *t;
3114 struct extern_desc *ext;
3116 const char *ext_name, *sec_name;
3120 if (!obj->efile.symbols)
3123 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
3124 if (elf_sec_hdr(obj, scn, &sh))
3125 return -LIBBPF_ERRNO__FORMAT;
3127 n = sh.sh_size / sh.sh_entsize;
3128 pr_debug("looking for externs among %d symbols...\n", n);
3130 for (i = 0; i < n; i++) {
3133 if (!gelf_getsym(obj->efile.symbols, i, &sym))
3134 return -LIBBPF_ERRNO__FORMAT;
3135 if (!sym_is_extern(&sym))
3137 ext_name = elf_sym_str(obj, sym.st_name);
3138 if (!ext_name || !ext_name[0])
3142 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
3146 ext = &ext[obj->nr_extern];
3147 memset(ext, 0, sizeof(*ext));
3150 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
3151 if (ext->btf_id <= 0) {
3152 pr_warn("failed to find BTF for extern '%s': %d\n",
3153 ext_name, ext->btf_id);
3156 t = btf__type_by_id(obj->btf, ext->btf_id);
3157 ext->name = btf__name_by_offset(obj->btf, t->name_off);
3159 ext->is_weak = GELF_ST_BIND(sym.st_info) == STB_WEAK;
3161 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
3162 if (ext->sec_btf_id <= 0) {
3163 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
3164 ext_name, ext->btf_id, ext->sec_btf_id);
3165 return ext->sec_btf_id;
3167 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
3168 sec_name = btf__name_by_offset(obj->btf, sec->name_off);
3170 if (strcmp(sec_name, KCONFIG_SEC) == 0) {
3172 ext->type = EXT_KCFG;
3173 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
3174 if (ext->kcfg.sz <= 0) {
3175 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
3176 ext_name, ext->kcfg.sz);
3177 return ext->kcfg.sz;
3179 ext->kcfg.align = btf__align_of(obj->btf, t->type);
3180 if (ext->kcfg.align <= 0) {
3181 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
3182 ext_name, ext->kcfg.align);
3185 ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
3186 &ext->kcfg.is_signed);
3187 if (ext->kcfg.type == KCFG_UNKNOWN) {
3188 pr_warn("extern (kcfg) '%s' type is unsupported\n", ext_name);
3191 } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
3193 ext->type = EXT_KSYM;
3194 skip_mods_and_typedefs(obj->btf, t->type,
3195 &ext->ksym.type_id);
3197 pr_warn("unrecognized extern section '%s'\n", sec_name);
3201 pr_debug("collected %d externs total\n", obj->nr_extern);
3203 if (!obj->nr_extern)
3206 /* sort externs by type, for kcfg ones also by (align, size, name) */
3207 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
3209 /* for .ksyms section, we need to turn all externs into allocated
3210 * variables in BTF to pass kernel verification; we do this by
3211 * pretending that each extern is a 8-byte variable
3214 /* find existing 4-byte integer type in BTF to use for fake
3215 * extern variables in DATASEC
3217 int int_btf_id = find_int_btf_id(obj->btf);
3219 for (i = 0; i < obj->nr_extern; i++) {
3220 ext = &obj->externs[i];
3221 if (ext->type != EXT_KSYM)
3223 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
3224 i, ext->sym_idx, ext->name);
3229 for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
3230 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3231 struct btf_type *vt;
3233 vt = (void *)btf__type_by_id(obj->btf, vs->type);
3234 ext_name = btf__name_by_offset(obj->btf, vt->name_off);
3235 ext = find_extern_by_name(obj, ext_name);
3237 pr_warn("failed to find extern definition for BTF var '%s'\n",
3241 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3242 vt->type = int_btf_id;
3244 vs->size = sizeof(int);
3251 /* for kcfg externs calculate their offsets within a .kconfig map */
3253 for (i = 0; i < obj->nr_extern; i++) {
3254 ext = &obj->externs[i];
3255 if (ext->type != EXT_KCFG)
3258 ext->kcfg.data_off = roundup(off, ext->kcfg.align);
3259 off = ext->kcfg.data_off + ext->kcfg.sz;
3260 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
3261 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
3265 for (i = 0; i < n; i++) {
3266 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3268 t = btf__type_by_id(obj->btf, vs->type);
3269 ext_name = btf__name_by_offset(obj->btf, t->name_off);
3270 ext = find_extern_by_name(obj, ext_name);
3272 pr_warn("failed to find extern definition for BTF var '%s'\n",
3276 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3277 vs->offset = ext->kcfg.data_off;
3283 struct bpf_program *
3284 bpf_object__find_program_by_title(const struct bpf_object *obj,
3287 struct bpf_program *pos;
3289 bpf_object__for_each_program(pos, obj) {
3290 if (pos->sec_name && !strcmp(pos->sec_name, title))
3296 static bool prog_is_subprog(const struct bpf_object *obj,
3297 const struct bpf_program *prog)
3299 /* For legacy reasons, libbpf supports an entry-point BPF programs
3300 * without SEC() attribute, i.e., those in the .text section. But if
3301 * there are 2 or more such programs in the .text section, they all
3302 * must be subprograms called from entry-point BPF programs in
3303 * designated SEC()'tions, otherwise there is no way to distinguish
3304 * which of those programs should be loaded vs which are a subprogram.
3305 * Similarly, if there is a function/program in .text and at least one
3306 * other BPF program with custom SEC() attribute, then we just assume
3307 * .text programs are subprograms (even if they are not called from
3308 * other programs), because libbpf never explicitly supported mixing
3309 * SEC()-designated BPF programs and .text entry-point BPF programs.
3311 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
3314 struct bpf_program *
3315 bpf_object__find_program_by_name(const struct bpf_object *obj,
3318 struct bpf_program *prog;
3320 bpf_object__for_each_program(prog, obj) {
3321 if (prog_is_subprog(obj, prog))
3323 if (!strcmp(prog->name, name))
3329 static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
3332 return shndx == obj->efile.data_shndx ||
3333 shndx == obj->efile.bss_shndx ||
3334 shndx == obj->efile.rodata_shndx;
3337 static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
3340 return shndx == obj->efile.maps_shndx ||
3341 shndx == obj->efile.btf_maps_shndx;
3344 static enum libbpf_map_type
3345 bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
3347 if (shndx == obj->efile.data_shndx)
3348 return LIBBPF_MAP_DATA;
3349 else if (shndx == obj->efile.bss_shndx)
3350 return LIBBPF_MAP_BSS;
3351 else if (shndx == obj->efile.rodata_shndx)
3352 return LIBBPF_MAP_RODATA;
3353 else if (shndx == obj->efile.symbols_shndx)
3354 return LIBBPF_MAP_KCONFIG;
3356 return LIBBPF_MAP_UNSPEC;
3359 static int bpf_program__record_reloc(struct bpf_program *prog,
3360 struct reloc_desc *reloc_desc,
3361 __u32 insn_idx, const char *sym_name,
3362 const GElf_Sym *sym, const GElf_Rel *rel)
3364 struct bpf_insn *insn = &prog->insns[insn_idx];
3365 size_t map_idx, nr_maps = prog->obj->nr_maps;
3366 struct bpf_object *obj = prog->obj;
3367 __u32 shdr_idx = sym->st_shndx;
3368 enum libbpf_map_type type;
3369 const char *sym_sec_name;
3370 struct bpf_map *map;
3372 reloc_desc->processed = false;
3374 /* sub-program call relocation */
3375 if (insn->code == (BPF_JMP | BPF_CALL)) {
3376 if (insn->src_reg != BPF_PSEUDO_CALL) {
3377 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
3378 return -LIBBPF_ERRNO__RELOC;
3380 /* text_shndx can be 0, if no default "main" program exists */
3381 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
3382 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
3383 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
3384 prog->name, sym_name, sym_sec_name);
3385 return -LIBBPF_ERRNO__RELOC;
3387 if (sym->st_value % BPF_INSN_SZ) {
3388 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
3389 prog->name, sym_name, (size_t)sym->st_value);
3390 return -LIBBPF_ERRNO__RELOC;
3392 reloc_desc->type = RELO_CALL;
3393 reloc_desc->insn_idx = insn_idx;
3394 reloc_desc->sym_off = sym->st_value;
3398 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
3399 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
3400 prog->name, sym_name, insn_idx, insn->code);
3401 return -LIBBPF_ERRNO__RELOC;
3404 if (sym_is_extern(sym)) {
3405 int sym_idx = GELF_R_SYM(rel->r_info);
3406 int i, n = obj->nr_extern;
3407 struct extern_desc *ext;
3409 for (i = 0; i < n; i++) {
3410 ext = &obj->externs[i];
3411 if (ext->sym_idx == sym_idx)
3415 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
3416 prog->name, sym_name, sym_idx);
3417 return -LIBBPF_ERRNO__RELOC;
3419 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
3420 prog->name, i, ext->name, ext->sym_idx, insn_idx);
3421 reloc_desc->type = RELO_EXTERN;
3422 reloc_desc->insn_idx = insn_idx;
3423 reloc_desc->sym_off = i; /* sym_off stores extern index */
3427 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
3428 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
3429 prog->name, sym_name, shdr_idx);
3430 return -LIBBPF_ERRNO__RELOC;
3433 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
3434 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
3436 /* generic map reference relocation */
3437 if (type == LIBBPF_MAP_UNSPEC) {
3438 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
3439 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
3440 prog->name, sym_name, sym_sec_name);
3441 return -LIBBPF_ERRNO__RELOC;
3443 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3444 map = &obj->maps[map_idx];
3445 if (map->libbpf_type != type ||
3446 map->sec_idx != sym->st_shndx ||
3447 map->sec_offset != sym->st_value)
3449 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
3450 prog->name, map_idx, map->name, map->sec_idx,
3451 map->sec_offset, insn_idx);
3454 if (map_idx >= nr_maps) {
3455 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
3456 prog->name, sym_sec_name, (size_t)sym->st_value);
3457 return -LIBBPF_ERRNO__RELOC;
3459 reloc_desc->type = RELO_LD64;
3460 reloc_desc->insn_idx = insn_idx;
3461 reloc_desc->map_idx = map_idx;
3462 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
3466 /* global data map relocation */
3467 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
3468 pr_warn("prog '%s': bad data relo against section '%s'\n",
3469 prog->name, sym_sec_name);
3470 return -LIBBPF_ERRNO__RELOC;
3472 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3473 map = &obj->maps[map_idx];
3474 if (map->libbpf_type != type)
3476 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
3477 prog->name, map_idx, map->name, map->sec_idx,
3478 map->sec_offset, insn_idx);
3481 if (map_idx >= nr_maps) {
3482 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
3483 prog->name, sym_sec_name);
3484 return -LIBBPF_ERRNO__RELOC;
3487 reloc_desc->type = RELO_DATA;
3488 reloc_desc->insn_idx = insn_idx;
3489 reloc_desc->map_idx = map_idx;
3490 reloc_desc->sym_off = sym->st_value;
3494 static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
3496 return insn_idx >= prog->sec_insn_off &&
3497 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
3500 static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
3501 size_t sec_idx, size_t insn_idx)
3503 int l = 0, r = obj->nr_programs - 1, m;
3504 struct bpf_program *prog;
3507 m = l + (r - l + 1) / 2;
3508 prog = &obj->programs[m];
3510 if (prog->sec_idx < sec_idx ||
3511 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
3516 /* matching program could be at index l, but it still might be the
3517 * wrong one, so we need to double check conditions for the last time
3519 prog = &obj->programs[l];
3520 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
3526 bpf_object__collect_prog_relos(struct bpf_object *obj, GElf_Shdr *shdr, Elf_Data *data)
3528 Elf_Data *symbols = obj->efile.symbols;
3529 const char *relo_sec_name, *sec_name;
3530 size_t sec_idx = shdr->sh_info;
3531 struct bpf_program *prog;
3532 struct reloc_desc *relos;
3534 const char *sym_name;
3539 relo_sec_name = elf_sec_str(obj, shdr->sh_name);
3540 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
3541 if (!relo_sec_name || !sec_name)
3544 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
3545 relo_sec_name, sec_idx, sec_name);
3546 nrels = shdr->sh_size / shdr->sh_entsize;
3548 for (i = 0; i < nrels; i++) {
3549 if (!gelf_getrel(data, i, &rel)) {
3550 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
3551 return -LIBBPF_ERRNO__FORMAT;
3553 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
3554 pr_warn("sec '%s': symbol 0x%zx not found for relo #%d\n",
3555 relo_sec_name, (size_t)GELF_R_SYM(rel.r_info), i);
3556 return -LIBBPF_ERRNO__FORMAT;
3558 if (rel.r_offset % BPF_INSN_SZ) {
3559 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
3560 relo_sec_name, (size_t)GELF_R_SYM(rel.r_info), i);
3561 return -LIBBPF_ERRNO__FORMAT;
3564 insn_idx = rel.r_offset / BPF_INSN_SZ;
3565 /* relocations against static functions are recorded as
3566 * relocations against the section that contains a function;
3567 * in such case, symbol will be STT_SECTION and sym.st_name
3568 * will point to empty string (0), so fetch section name
3571 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && sym.st_name == 0)
3572 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym.st_shndx));
3574 sym_name = elf_sym_str(obj, sym.st_name);
3575 sym_name = sym_name ?: "<?";
3577 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
3578 relo_sec_name, i, insn_idx, sym_name);
3580 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
3582 pr_warn("sec '%s': relo #%d: program not found in section '%s' for insn #%u\n",
3583 relo_sec_name, i, sec_name, insn_idx);
3584 return -LIBBPF_ERRNO__RELOC;
3587 relos = libbpf_reallocarray(prog->reloc_desc,
3588 prog->nr_reloc + 1, sizeof(*relos));
3591 prog->reloc_desc = relos;
3593 /* adjust insn_idx to local BPF program frame of reference */
3594 insn_idx -= prog->sec_insn_off;
3595 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
3596 insn_idx, sym_name, &sym, &rel);
3605 static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
3607 struct bpf_map_def *def = &map->def;
3608 __u32 key_type_id = 0, value_type_id = 0;
3611 /* if it's BTF-defined map, we don't need to search for type IDs.
3612 * For struct_ops map, it does not need btf_key_type_id and
3613 * btf_value_type_id.
3615 if (map->sec_idx == obj->efile.btf_maps_shndx ||
3616 bpf_map__is_struct_ops(map))
3619 if (!bpf_map__is_internal(map)) {
3620 ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
3621 def->value_size, &key_type_id,
3625 * LLVM annotates global data differently in BTF, that is,
3626 * only as '.data', '.bss' or '.rodata'.
3628 ret = btf__find_by_name(obj->btf,
3629 libbpf_type_to_btf_name[map->libbpf_type]);
3634 map->btf_key_type_id = key_type_id;
3635 map->btf_value_type_id = bpf_map__is_internal(map) ?
3636 ret : value_type_id;
3640 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
3642 struct bpf_map_info info = {};
3643 __u32 len = sizeof(info);
3647 err = bpf_obj_get_info_by_fd(fd, &info, &len);
3651 new_name = strdup(info.name);
3655 new_fd = open("/", O_RDONLY | O_CLOEXEC);
3658 goto err_free_new_name;
3661 new_fd = dup3(fd, new_fd, O_CLOEXEC);
3664 goto err_close_new_fd;
3667 err = zclose(map->fd);
3670 goto err_close_new_fd;
3675 map->name = new_name;
3676 map->def.type = info.type;
3677 map->def.key_size = info.key_size;
3678 map->def.value_size = info.value_size;
3679 map->def.max_entries = info.max_entries;
3680 map->def.map_flags = info.map_flags;
3681 map->btf_key_type_id = info.btf_key_type_id;
3682 map->btf_value_type_id = info.btf_value_type_id;
3694 __u32 bpf_map__max_entries(const struct bpf_map *map)
3696 return map->def.max_entries;
3699 int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
3703 map->def.max_entries = max_entries;
3707 int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
3709 if (!map || !max_entries)
3712 return bpf_map__set_max_entries(map, max_entries);
3716 bpf_object__probe_loading(struct bpf_object *obj)
3718 struct bpf_load_program_attr attr;
3719 char *cp, errmsg[STRERR_BUFSIZE];
3720 struct bpf_insn insns[] = {
3721 BPF_MOV64_IMM(BPF_REG_0, 0),
3726 /* make sure basic loading works */
3728 memset(&attr, 0, sizeof(attr));
3729 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3731 attr.insns_cnt = ARRAY_SIZE(insns);
3732 attr.license = "GPL";
3734 ret = bpf_load_program_xattr(&attr, NULL, 0);
3737 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
3738 pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
3739 "program. Make sure your kernel supports BPF "
3740 "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
3741 "set to big enough value.\n", __func__, cp, ret);
3749 static int probe_fd(int fd)
3756 static int probe_kern_prog_name(void)
3758 struct bpf_load_program_attr attr;
3759 struct bpf_insn insns[] = {
3760 BPF_MOV64_IMM(BPF_REG_0, 0),
3765 /* make sure loading with name works */
3767 memset(&attr, 0, sizeof(attr));
3768 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3770 attr.insns_cnt = ARRAY_SIZE(insns);
3771 attr.license = "GPL";
3773 ret = bpf_load_program_xattr(&attr, NULL, 0);
3774 return probe_fd(ret);
3777 static int probe_kern_global_data(void)
3779 struct bpf_load_program_attr prg_attr;
3780 struct bpf_create_map_attr map_attr;
3781 char *cp, errmsg[STRERR_BUFSIZE];
3782 struct bpf_insn insns[] = {
3783 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
3784 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
3785 BPF_MOV64_IMM(BPF_REG_0, 0),
3790 memset(&map_attr, 0, sizeof(map_attr));
3791 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
3792 map_attr.key_size = sizeof(int);
3793 map_attr.value_size = 32;
3794 map_attr.max_entries = 1;
3796 map = bpf_create_map_xattr(&map_attr);
3799 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
3800 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3801 __func__, cp, -ret);
3807 memset(&prg_attr, 0, sizeof(prg_attr));
3808 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3809 prg_attr.insns = insns;
3810 prg_attr.insns_cnt = ARRAY_SIZE(insns);
3811 prg_attr.license = "GPL";
3813 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
3815 return probe_fd(ret);
3818 static int probe_kern_btf(void)
3820 static const char strs[] = "\0int";
3823 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
3826 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3827 strs, sizeof(strs)));
3830 static int probe_kern_btf_func(void)
3832 static const char strs[] = "\0int\0x\0a";
3833 /* void x(int a) {} */
3836 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3837 /* FUNC_PROTO */ /* [2] */
3838 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3839 BTF_PARAM_ENC(7, 1),
3840 /* FUNC x */ /* [3] */
3841 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
3844 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3845 strs, sizeof(strs)));
3848 static int probe_kern_btf_func_global(void)
3850 static const char strs[] = "\0int\0x\0a";
3851 /* static void x(int a) {} */
3854 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3855 /* FUNC_PROTO */ /* [2] */
3856 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3857 BTF_PARAM_ENC(7, 1),
3858 /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
3859 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
3862 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3863 strs, sizeof(strs)));
3866 static int probe_kern_btf_datasec(void)
3868 static const char strs[] = "\0x\0.data";
3872 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3873 /* VAR x */ /* [2] */
3874 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3876 /* DATASEC val */ /* [3] */
3877 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3878 BTF_VAR_SECINFO_ENC(2, 0, 4),
3881 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3882 strs, sizeof(strs)));
3885 static int probe_kern_array_mmap(void)
3887 struct bpf_create_map_attr attr = {
3888 .map_type = BPF_MAP_TYPE_ARRAY,
3889 .map_flags = BPF_F_MMAPABLE,
3890 .key_size = sizeof(int),
3891 .value_size = sizeof(int),
3895 return probe_fd(bpf_create_map_xattr(&attr));
3898 static int probe_kern_exp_attach_type(void)
3900 struct bpf_load_program_attr attr;
3901 struct bpf_insn insns[] = {
3902 BPF_MOV64_IMM(BPF_REG_0, 0),
3906 memset(&attr, 0, sizeof(attr));
3907 /* use any valid combination of program type and (optional)
3908 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
3909 * to see if kernel supports expected_attach_type field for
3910 * BPF_PROG_LOAD command
3912 attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
3913 attr.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE;
3915 attr.insns_cnt = ARRAY_SIZE(insns);
3916 attr.license = "GPL";
3918 return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
3921 static int probe_kern_probe_read_kernel(void)
3923 struct bpf_load_program_attr attr;
3924 struct bpf_insn insns[] = {
3925 BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
3926 BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
3927 BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */
3928 BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */
3929 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
3933 memset(&attr, 0, sizeof(attr));
3934 attr.prog_type = BPF_PROG_TYPE_KPROBE;
3936 attr.insns_cnt = ARRAY_SIZE(insns);
3937 attr.license = "GPL";
3939 return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
3942 static int probe_prog_bind_map(void)
3944 struct bpf_load_program_attr prg_attr;
3945 struct bpf_create_map_attr map_attr;
3946 char *cp, errmsg[STRERR_BUFSIZE];
3947 struct bpf_insn insns[] = {
3948 BPF_MOV64_IMM(BPF_REG_0, 0),
3953 memset(&map_attr, 0, sizeof(map_attr));
3954 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
3955 map_attr.key_size = sizeof(int);
3956 map_attr.value_size = 32;
3957 map_attr.max_entries = 1;
3959 map = bpf_create_map_xattr(&map_attr);
3962 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
3963 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3964 __func__, cp, -ret);
3968 memset(&prg_attr, 0, sizeof(prg_attr));
3969 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3970 prg_attr.insns = insns;
3971 prg_attr.insns_cnt = ARRAY_SIZE(insns);
3972 prg_attr.license = "GPL";
3974 prog = bpf_load_program_xattr(&prg_attr, NULL, 0);
3980 ret = bpf_prog_bind_map(prog, map, NULL);
3988 static int probe_module_btf(void)
3990 static const char strs[] = "\0int";
3993 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
3995 struct bpf_btf_info info;
3996 __u32 len = sizeof(info);
4000 fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs));
4002 return 0; /* BTF not supported at all */
4004 memset(&info, 0, sizeof(info));
4005 info.name = ptr_to_u64(name);
4006 info.name_len = sizeof(name);
4008 /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
4009 * kernel's module BTF support coincides with support for
4010 * name/name_len fields in struct bpf_btf_info.
4012 err = bpf_obj_get_info_by_fd(fd, &info, &len);
4017 enum kern_feature_result {
4023 typedef int (*feature_probe_fn)(void);
4025 static struct kern_feature_desc {
4027 feature_probe_fn probe;
4028 enum kern_feature_result res;
4029 } feature_probes[__FEAT_CNT] = {
4030 [FEAT_PROG_NAME] = {
4031 "BPF program name", probe_kern_prog_name,
4033 [FEAT_GLOBAL_DATA] = {
4034 "global variables", probe_kern_global_data,
4037 "minimal BTF", probe_kern_btf,
4040 "BTF functions", probe_kern_btf_func,
4042 [FEAT_BTF_GLOBAL_FUNC] = {
4043 "BTF global function", probe_kern_btf_func_global,
4045 [FEAT_BTF_DATASEC] = {
4046 "BTF data section and variable", probe_kern_btf_datasec,
4048 [FEAT_ARRAY_MMAP] = {
4049 "ARRAY map mmap()", probe_kern_array_mmap,
4051 [FEAT_EXP_ATTACH_TYPE] = {
4052 "BPF_PROG_LOAD expected_attach_type attribute",
4053 probe_kern_exp_attach_type,
4055 [FEAT_PROBE_READ_KERN] = {
4056 "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
4058 [FEAT_PROG_BIND_MAP] = {
4059 "BPF_PROG_BIND_MAP support", probe_prog_bind_map,
4061 [FEAT_MODULE_BTF] = {
4062 "module BTF support", probe_module_btf,
4066 static bool kernel_supports(enum kern_feature_id feat_id)
4068 struct kern_feature_desc *feat = &feature_probes[feat_id];
4071 if (READ_ONCE(feat->res) == FEAT_UNKNOWN) {
4072 ret = feat->probe();
4074 WRITE_ONCE(feat->res, FEAT_SUPPORTED);
4075 } else if (ret == 0) {
4076 WRITE_ONCE(feat->res, FEAT_MISSING);
4078 pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
4079 WRITE_ONCE(feat->res, FEAT_MISSING);
4083 return READ_ONCE(feat->res) == FEAT_SUPPORTED;
4086 static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
4088 struct bpf_map_info map_info = {};
4089 char msg[STRERR_BUFSIZE];
4092 map_info_len = sizeof(map_info);
4094 if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) {
4095 pr_warn("failed to get map info for map FD %d: %s\n",
4096 map_fd, libbpf_strerror_r(errno, msg, sizeof(msg)));
4100 return (map_info.type == map->def.type &&
4101 map_info.key_size == map->def.key_size &&
4102 map_info.value_size == map->def.value_size &&
4103 map_info.max_entries == map->def.max_entries &&
4104 map_info.map_flags == map->def.map_flags);
4108 bpf_object__reuse_map(struct bpf_map *map)
4110 char *cp, errmsg[STRERR_BUFSIZE];
4113 pin_fd = bpf_obj_get(map->pin_path);
4116 if (err == -ENOENT) {
4117 pr_debug("found no pinned map to reuse at '%s'\n",
4122 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
4123 pr_warn("couldn't retrieve pinned map '%s': %s\n",
4128 if (!map_is_reuse_compat(map, pin_fd)) {
4129 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
4135 err = bpf_map__reuse_fd(map, pin_fd);
4141 pr_debug("reused pinned map at '%s'\n", map->pin_path);
4147 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
4149 enum libbpf_map_type map_type = map->libbpf_type;
4150 char *cp, errmsg[STRERR_BUFSIZE];
4153 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
4156 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4157 pr_warn("Error setting initial map(%s) contents: %s\n",
4162 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
4163 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
4164 err = bpf_map_freeze(map->fd);
4167 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4168 pr_warn("Error freezing map(%s) as read-only: %s\n",
4176 static void bpf_map__destroy(struct bpf_map *map);
4178 static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
4180 struct bpf_create_map_attr create_attr;
4181 struct bpf_map_def *def = &map->def;
4183 memset(&create_attr, 0, sizeof(create_attr));
4185 if (kernel_supports(FEAT_PROG_NAME))
4186 create_attr.name = map->name;
4187 create_attr.map_ifindex = map->map_ifindex;
4188 create_attr.map_type = def->type;
4189 create_attr.map_flags = def->map_flags;
4190 create_attr.key_size = def->key_size;
4191 create_attr.value_size = def->value_size;
4192 create_attr.numa_node = map->numa_node;
4194 if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !def->max_entries) {
4197 nr_cpus = libbpf_num_possible_cpus();
4199 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
4200 map->name, nr_cpus);
4203 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
4204 create_attr.max_entries = nr_cpus;
4206 create_attr.max_entries = def->max_entries;
4209 if (bpf_map__is_struct_ops(map))
4210 create_attr.btf_vmlinux_value_type_id =
4211 map->btf_vmlinux_value_type_id;
4213 create_attr.btf_fd = 0;
4214 create_attr.btf_key_type_id = 0;
4215 create_attr.btf_value_type_id = 0;
4216 if (obj->btf && btf__fd(obj->btf) >= 0 && !bpf_map_find_btf_info(obj, map)) {
4217 create_attr.btf_fd = btf__fd(obj->btf);
4218 create_attr.btf_key_type_id = map->btf_key_type_id;
4219 create_attr.btf_value_type_id = map->btf_value_type_id;
4222 if (bpf_map_type__is_map_in_map(def->type)) {
4223 if (map->inner_map) {
4226 err = bpf_object__create_map(obj, map->inner_map);
4228 pr_warn("map '%s': failed to create inner map: %d\n",
4232 map->inner_map_fd = bpf_map__fd(map->inner_map);
4234 if (map->inner_map_fd >= 0)
4235 create_attr.inner_map_fd = map->inner_map_fd;
4238 map->fd = bpf_create_map_xattr(&create_attr);
4239 if (map->fd < 0 && (create_attr.btf_key_type_id ||
4240 create_attr.btf_value_type_id)) {
4241 char *cp, errmsg[STRERR_BUFSIZE];
4244 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4245 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
4246 map->name, cp, err);
4247 create_attr.btf_fd = 0;
4248 create_attr.btf_key_type_id = 0;
4249 create_attr.btf_value_type_id = 0;
4250 map->btf_key_type_id = 0;
4251 map->btf_value_type_id = 0;
4252 map->fd = bpf_create_map_xattr(&create_attr);
4258 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
4259 bpf_map__destroy(map->inner_map);
4260 zfree(&map->inner_map);
4266 static int init_map_slots(struct bpf_map *map)
4268 const struct bpf_map *targ_map;
4272 for (i = 0; i < map->init_slots_sz; i++) {
4273 if (!map->init_slots[i])
4276 targ_map = map->init_slots[i];
4277 fd = bpf_map__fd(targ_map);
4278 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
4281 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
4282 map->name, i, targ_map->name,
4286 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
4287 map->name, i, targ_map->name, fd);
4290 zfree(&map->init_slots);
4291 map->init_slots_sz = 0;
4297 bpf_object__create_maps(struct bpf_object *obj)
4299 struct bpf_map *map;
4300 char *cp, errmsg[STRERR_BUFSIZE];
4304 for (i = 0; i < obj->nr_maps; i++) {
4305 map = &obj->maps[i];
4307 if (map->pin_path) {
4308 err = bpf_object__reuse_map(map);
4310 pr_warn("map '%s': error reusing pinned map\n",
4317 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
4318 map->name, map->fd);
4320 err = bpf_object__create_map(obj, map);
4324 pr_debug("map '%s': created successfully, fd=%d\n",
4325 map->name, map->fd);
4327 if (bpf_map__is_internal(map)) {
4328 err = bpf_object__populate_internal_map(obj, map);
4335 if (map->init_slots_sz) {
4336 err = init_map_slots(map);
4344 if (map->pin_path && !map->pinned) {
4345 err = bpf_map__pin(map, NULL);
4347 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
4348 map->name, map->pin_path, err);
4358 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4359 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
4361 for (j = 0; j < i; j++)
4362 zclose(obj->maps[j].fd);
4366 #define BPF_CORE_SPEC_MAX_LEN 64
4368 /* represents BPF CO-RE field or array element accessor */
4369 struct bpf_core_accessor {
4370 __u32 type_id; /* struct/union type or array element type */
4371 __u32 idx; /* field index or array index */
4372 const char *name; /* field name or NULL for array accessor */
4375 struct bpf_core_spec {
4376 const struct btf *btf;
4377 /* high-level spec: named fields and array indices only */
4378 struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
4379 /* original unresolved (no skip_mods_or_typedefs) root type ID */
4381 /* CO-RE relocation kind */
4382 enum bpf_core_relo_kind relo_kind;
4383 /* high-level spec length */
4385 /* raw, low-level spec: 1-to-1 with accessor spec string */
4386 int raw_spec[BPF_CORE_SPEC_MAX_LEN];
4387 /* raw spec length */
4389 /* field bit offset represented by spec */
4393 static bool str_is_empty(const char *s)
4398 static bool is_flex_arr(const struct btf *btf,
4399 const struct bpf_core_accessor *acc,
4400 const struct btf_array *arr)
4402 const struct btf_type *t;
4404 /* not a flexible array, if not inside a struct or has non-zero size */
4405 if (!acc->name || arr->nelems > 0)
4408 /* has to be the last member of enclosing struct */
4409 t = btf__type_by_id(btf, acc->type_id);
4410 return acc->idx == btf_vlen(t) - 1;
4413 static const char *core_relo_kind_str(enum bpf_core_relo_kind kind)
4416 case BPF_FIELD_BYTE_OFFSET: return "byte_off";
4417 case BPF_FIELD_BYTE_SIZE: return "byte_sz";
4418 case BPF_FIELD_EXISTS: return "field_exists";
4419 case BPF_FIELD_SIGNED: return "signed";
4420 case BPF_FIELD_LSHIFT_U64: return "lshift_u64";
4421 case BPF_FIELD_RSHIFT_U64: return "rshift_u64";
4422 case BPF_TYPE_ID_LOCAL: return "local_type_id";
4423 case BPF_TYPE_ID_TARGET: return "target_type_id";
4424 case BPF_TYPE_EXISTS: return "type_exists";
4425 case BPF_TYPE_SIZE: return "type_size";
4426 case BPF_ENUMVAL_EXISTS: return "enumval_exists";
4427 case BPF_ENUMVAL_VALUE: return "enumval_value";
4428 default: return "unknown";
4432 static bool core_relo_is_field_based(enum bpf_core_relo_kind kind)
4435 case BPF_FIELD_BYTE_OFFSET:
4436 case BPF_FIELD_BYTE_SIZE:
4437 case BPF_FIELD_EXISTS:
4438 case BPF_FIELD_SIGNED:
4439 case BPF_FIELD_LSHIFT_U64:
4440 case BPF_FIELD_RSHIFT_U64:
4447 static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
4450 case BPF_TYPE_ID_LOCAL:
4451 case BPF_TYPE_ID_TARGET:
4452 case BPF_TYPE_EXISTS:
4460 static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind)
4463 case BPF_ENUMVAL_EXISTS:
4464 case BPF_ENUMVAL_VALUE:
4472 * Turn bpf_core_relo into a low- and high-level spec representation,
4473 * validating correctness along the way, as well as calculating resulting
4474 * field bit offset, specified by accessor string. Low-level spec captures
4475 * every single level of nestedness, including traversing anonymous
4476 * struct/union members. High-level one only captures semantically meaningful
4477 * "turning points": named fields and array indicies.
4478 * E.g., for this case:
4481 * int __unimportant;
4489 * struct sample *s = ...;
4491 * int x = &s->a[3]; // access string = '0:1:2:3'
4493 * Low-level spec has 1:1 mapping with each element of access string (it's
4494 * just a parsed access string representation): [0, 1, 2, 3].
4496 * High-level spec will capture only 3 points:
4497 * - intial zero-index access by pointer (&s->... is the same as &s[0]...);
4498 * - field 'a' access (corresponds to '2' in low-level spec);
4499 * - array element #3 access (corresponds to '3' in low-level spec).
4501 * Type-based relocations (TYPE_EXISTS/TYPE_SIZE,
4502 * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
4503 * spec and raw_spec are kept empty.
4505 * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
4506 * string to specify enumerator's value index that need to be relocated.
4508 static int bpf_core_parse_spec(const struct btf *btf,
4510 const char *spec_str,
4511 enum bpf_core_relo_kind relo_kind,
4512 struct bpf_core_spec *spec)
4514 int access_idx, parsed_len, i;
4515 struct bpf_core_accessor *acc;
4516 const struct btf_type *t;
4521 if (str_is_empty(spec_str) || *spec_str == ':')
4524 memset(spec, 0, sizeof(*spec));
4526 spec->root_type_id = type_id;
4527 spec->relo_kind = relo_kind;
4529 /* type-based relocations don't have a field access string */
4530 if (core_relo_is_type_based(relo_kind)) {
4531 if (strcmp(spec_str, "0"))
4536 /* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
4538 if (*spec_str == ':')
4540 if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
4542 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4544 spec_str += parsed_len;
4545 spec->raw_spec[spec->raw_len++] = access_idx;
4548 if (spec->raw_len == 0)
4551 t = skip_mods_and_typedefs(btf, type_id, &id);
4555 access_idx = spec->raw_spec[0];
4556 acc = &spec->spec[0];
4558 acc->idx = access_idx;
4561 if (core_relo_is_enumval_based(relo_kind)) {
4562 if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
4565 /* record enumerator name in a first accessor */
4566 acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
4570 if (!core_relo_is_field_based(relo_kind))
4573 sz = btf__resolve_size(btf, id);
4576 spec->bit_offset = access_idx * sz * 8;
4578 for (i = 1; i < spec->raw_len; i++) {
4579 t = skip_mods_and_typedefs(btf, id, &id);
4583 access_idx = spec->raw_spec[i];
4584 acc = &spec->spec[spec->len];
4586 if (btf_is_composite(t)) {
4587 const struct btf_member *m;
4590 if (access_idx >= btf_vlen(t))
4593 bit_offset = btf_member_bit_offset(t, access_idx);
4594 spec->bit_offset += bit_offset;
4596 m = btf_members(t) + access_idx;
4598 name = btf__name_by_offset(btf, m->name_off);
4599 if (str_is_empty(name))
4603 acc->idx = access_idx;
4609 } else if (btf_is_array(t)) {
4610 const struct btf_array *a = btf_array(t);
4613 t = skip_mods_and_typedefs(btf, a->type, &id);
4617 flex = is_flex_arr(btf, acc - 1, a);
4618 if (!flex && access_idx >= a->nelems)
4621 spec->spec[spec->len].type_id = id;
4622 spec->spec[spec->len].idx = access_idx;
4625 sz = btf__resolve_size(btf, id);
4628 spec->bit_offset += access_idx * sz * 8;
4630 pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
4631 type_id, spec_str, i, id, btf_kind_str(t));
4639 static bool bpf_core_is_flavor_sep(const char *s)
4641 /* check X___Y name pattern, where X and Y are not underscores */
4642 return s[0] != '_' && /* X */
4643 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
4644 s[4] != '_'; /* Y */
4647 /* Given 'some_struct_name___with_flavor' return the length of a name prefix
4648 * before last triple underscore. Struct name part after last triple
4649 * underscore is ignored by BPF CO-RE relocation during relocation matching.
4651 static size_t bpf_core_essential_name_len(const char *name)
4653 size_t n = strlen(name);
4656 for (i = n - 5; i >= 0; i--) {
4657 if (bpf_core_is_flavor_sep(name + i))
4665 const struct btf *btf;
4666 const struct btf_type *t;
4671 /* dynamically sized list of type IDs and its associated struct btf */
4672 struct core_cand_list {
4673 struct core_cand *cands;
4677 static void bpf_core_free_cands(struct core_cand_list *cands)
4683 static int bpf_core_add_cands(struct core_cand *local_cand,
4684 size_t local_essent_len,
4685 const struct btf *targ_btf,
4686 const char *targ_btf_name,
4688 struct core_cand_list *cands)
4690 struct core_cand *new_cands, *cand;
4691 const struct btf_type *t;
4692 const char *targ_name;
4693 size_t targ_essent_len;
4696 n = btf__get_nr_types(targ_btf);
4697 for (i = targ_start_id; i <= n; i++) {
4698 t = btf__type_by_id(targ_btf, i);
4699 if (btf_kind(t) != btf_kind(local_cand->t))
4702 targ_name = btf__name_by_offset(targ_btf, t->name_off);
4703 if (str_is_empty(targ_name))
4706 targ_essent_len = bpf_core_essential_name_len(targ_name);
4707 if (targ_essent_len != local_essent_len)
4710 if (strncmp(local_cand->name, targ_name, local_essent_len) != 0)
4713 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
4714 local_cand->id, btf_kind_str(local_cand->t),
4715 local_cand->name, i, btf_kind_str(t), targ_name,
4717 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
4718 sizeof(*cands->cands));
4722 cand = &new_cands[cands->len];
4723 cand->btf = targ_btf;
4725 cand->name = targ_name;
4728 cands->cands = new_cands;
4734 static int load_module_btfs(struct bpf_object *obj)
4736 struct bpf_btf_info info;
4737 struct module_btf *mod_btf;
4743 if (obj->btf_modules_loaded)
4746 /* don't do this again, even if we find no module BTFs */
4747 obj->btf_modules_loaded = true;
4749 /* kernel too old to support module BTFs */
4750 if (!kernel_supports(FEAT_MODULE_BTF))
4754 err = bpf_btf_get_next_id(id, &id);
4755 if (err && errno == ENOENT)
4759 pr_warn("failed to iterate BTF objects: %d\n", err);
4763 fd = bpf_btf_get_fd_by_id(id);
4765 if (errno == ENOENT)
4766 continue; /* expected race: BTF was unloaded */
4768 pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
4773 memset(&info, 0, sizeof(info));
4774 info.name = ptr_to_u64(name);
4775 info.name_len = sizeof(name);
4777 err = bpf_obj_get_info_by_fd(fd, &info, &len);
4780 pr_warn("failed to get BTF object #%d info: %d\n", id, err);
4784 /* ignore non-module BTFs */
4785 if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
4790 btf = btf_get_from_fd(fd, obj->btf_vmlinux);
4792 pr_warn("failed to load module [%s]'s BTF object #%d: %ld\n",
4793 name, id, PTR_ERR(btf));
4798 err = btf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
4799 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
4803 mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
4808 mod_btf->name = strdup(name);
4809 if (!mod_btf->name) {
4823 static struct core_cand_list *
4824 bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
4826 struct core_cand local_cand = {};
4827 struct core_cand_list *cands;
4828 const struct btf *main_btf;
4829 size_t local_essent_len;
4832 local_cand.btf = local_btf;
4833 local_cand.t = btf__type_by_id(local_btf, local_type_id);
4835 return ERR_PTR(-EINVAL);
4837 local_cand.name = btf__name_by_offset(local_btf, local_cand.t->name_off);
4838 if (str_is_empty(local_cand.name))
4839 return ERR_PTR(-EINVAL);
4840 local_essent_len = bpf_core_essential_name_len(local_cand.name);
4842 cands = calloc(1, sizeof(*cands));
4844 return ERR_PTR(-ENOMEM);
4846 /* Attempt to find target candidates in vmlinux BTF first */
4847 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
4848 err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
4852 /* if vmlinux BTF has any candidate, don't got for module BTFs */
4856 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
4857 if (obj->btf_vmlinux_override)
4860 /* now look through module BTFs, trying to still find candidates */
4861 err = load_module_btfs(obj);
4865 for (i = 0; i < obj->btf_module_cnt; i++) {
4866 err = bpf_core_add_cands(&local_cand, local_essent_len,
4867 obj->btf_modules[i].btf,
4868 obj->btf_modules[i].name,
4869 btf__get_nr_types(obj->btf_vmlinux) + 1,
4877 bpf_core_free_cands(cands);
4878 return ERR_PTR(err);
4881 /* Check two types for compatibility for the purpose of field access
4882 * relocation. const/volatile/restrict and typedefs are skipped to ensure we
4883 * are relocating semantically compatible entities:
4884 * - any two STRUCTs/UNIONs are compatible and can be mixed;
4885 * - any two FWDs are compatible, if their names match (modulo flavor suffix);
4886 * - any two PTRs are always compatible;
4887 * - for ENUMs, names should be the same (ignoring flavor suffix) or at
4888 * least one of enums should be anonymous;
4889 * - for ENUMs, check sizes, names are ignored;
4890 * - for INT, size and signedness are ignored;
4891 * - for ARRAY, dimensionality is ignored, element types are checked for
4892 * compatibility recursively;
4893 * - everything else shouldn't be ever a target of relocation.
4894 * These rules are not set in stone and probably will be adjusted as we get
4895 * more experience with using BPF CO-RE relocations.
4897 static int bpf_core_fields_are_compat(const struct btf *local_btf,
4899 const struct btf *targ_btf,
4902 const struct btf_type *local_type, *targ_type;
4905 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
4906 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
4907 if (!local_type || !targ_type)
4910 if (btf_is_composite(local_type) && btf_is_composite(targ_type))
4912 if (btf_kind(local_type) != btf_kind(targ_type))
4915 switch (btf_kind(local_type)) {
4919 case BTF_KIND_ENUM: {
4920 const char *local_name, *targ_name;
4921 size_t local_len, targ_len;
4923 local_name = btf__name_by_offset(local_btf,
4924 local_type->name_off);
4925 targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
4926 local_len = bpf_core_essential_name_len(local_name);
4927 targ_len = bpf_core_essential_name_len(targ_name);
4928 /* one of them is anonymous or both w/ same flavor-less names */
4929 return local_len == 0 || targ_len == 0 ||
4930 (local_len == targ_len &&
4931 strncmp(local_name, targ_name, local_len) == 0);
4934 /* just reject deprecated bitfield-like integers; all other
4935 * integers are by default compatible between each other
4937 return btf_int_offset(local_type) == 0 &&
4938 btf_int_offset(targ_type) == 0;
4939 case BTF_KIND_ARRAY:
4940 local_id = btf_array(local_type)->type;
4941 targ_id = btf_array(targ_type)->type;
4944 pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
4945 btf_kind(local_type), local_id, targ_id);
4951 * Given single high-level named field accessor in local type, find
4952 * corresponding high-level accessor for a target type. Along the way,
4953 * maintain low-level spec for target as well. Also keep updating target
4956 * Searching is performed through recursive exhaustive enumeration of all
4957 * fields of a struct/union. If there are any anonymous (embedded)
4958 * structs/unions, they are recursively searched as well. If field with
4959 * desired name is found, check compatibility between local and target types,
4960 * before returning result.
4962 * 1 is returned, if field is found.
4963 * 0 is returned if no compatible field is found.
4964 * <0 is returned on error.
4966 static int bpf_core_match_member(const struct btf *local_btf,
4967 const struct bpf_core_accessor *local_acc,
4968 const struct btf *targ_btf,
4970 struct bpf_core_spec *spec,
4971 __u32 *next_targ_id)
4973 const struct btf_type *local_type, *targ_type;
4974 const struct btf_member *local_member, *m;
4975 const char *local_name, *targ_name;
4979 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
4982 if (!btf_is_composite(targ_type))
4985 local_id = local_acc->type_id;
4986 local_type = btf__type_by_id(local_btf, local_id);
4987 local_member = btf_members(local_type) + local_acc->idx;
4988 local_name = btf__name_by_offset(local_btf, local_member->name_off);
4990 n = btf_vlen(targ_type);
4991 m = btf_members(targ_type);
4992 for (i = 0; i < n; i++, m++) {
4995 bit_offset = btf_member_bit_offset(targ_type, i);
4997 /* too deep struct/union/array nesting */
4998 if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
5001 /* speculate this member will be the good one */
5002 spec->bit_offset += bit_offset;
5003 spec->raw_spec[spec->raw_len++] = i;
5005 targ_name = btf__name_by_offset(targ_btf, m->name_off);
5006 if (str_is_empty(targ_name)) {
5007 /* embedded struct/union, we need to go deeper */
5008 found = bpf_core_match_member(local_btf, local_acc,
5010 spec, next_targ_id);
5011 if (found) /* either found or error */
5013 } else if (strcmp(local_name, targ_name) == 0) {
5014 /* matching named field */
5015 struct bpf_core_accessor *targ_acc;
5017 targ_acc = &spec->spec[spec->len++];
5018 targ_acc->type_id = targ_id;
5020 targ_acc->name = targ_name;
5022 *next_targ_id = m->type;
5023 found = bpf_core_fields_are_compat(local_btf,
5027 spec->len--; /* pop accessor */
5030 /* member turned out not to be what we looked for */
5031 spec->bit_offset -= bit_offset;
5038 /* Check local and target types for compatibility. This check is used for
5039 * type-based CO-RE relocations and follow slightly different rules than
5040 * field-based relocations. This function assumes that root types were already
5041 * checked for name match. Beyond that initial root-level name check, names
5042 * are completely ignored. Compatibility rules are as follows:
5043 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5044 * kind should match for local and target types (i.e., STRUCT is not
5045 * compatible with UNION);
5046 * - for ENUMs, the size is ignored;
5047 * - for INT, size and signedness are ignored;
5048 * - for ARRAY, dimensionality is ignored, element types are checked for
5049 * compatibility recursively;
5050 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5051 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5052 * - FUNC_PROTOs are compatible if they have compatible signature: same
5053 * number of input args and compatible return and argument types.
5054 * These rules are not set in stone and probably will be adjusted as we get
5055 * more experience with using BPF CO-RE relocations.
5057 static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5058 const struct btf *targ_btf, __u32 targ_id)
5060 const struct btf_type *local_type, *targ_type;
5061 int depth = 32; /* max recursion depth */
5063 /* caller made sure that names match (ignoring flavor suffix) */
5064 local_type = btf__type_by_id(local_btf, local_id);
5065 targ_type = btf__type_by_id(targ_btf, targ_id);
5066 if (btf_kind(local_type) != btf_kind(targ_type))
5074 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
5075 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
5076 if (!local_type || !targ_type)
5079 if (btf_kind(local_type) != btf_kind(targ_type))
5082 switch (btf_kind(local_type)) {
5084 case BTF_KIND_STRUCT:
5085 case BTF_KIND_UNION:
5090 /* just reject deprecated bitfield-like integers; all other
5091 * integers are by default compatible between each other
5093 return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
5095 local_id = local_type->type;
5096 targ_id = targ_type->type;
5098 case BTF_KIND_ARRAY:
5099 local_id = btf_array(local_type)->type;
5100 targ_id = btf_array(targ_type)->type;
5102 case BTF_KIND_FUNC_PROTO: {
5103 struct btf_param *local_p = btf_params(local_type);
5104 struct btf_param *targ_p = btf_params(targ_type);
5105 __u16 local_vlen = btf_vlen(local_type);
5106 __u16 targ_vlen = btf_vlen(targ_type);
5109 if (local_vlen != targ_vlen)
5112 for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
5113 skip_mods_and_typedefs(local_btf, local_p->type, &local_id);
5114 skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id);
5115 err = bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id);
5120 /* tail recurse for return type check */
5121 skip_mods_and_typedefs(local_btf, local_type->type, &local_id);
5122 skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
5126 pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
5127 btf_kind_str(local_type), local_id, targ_id);
5133 * Try to match local spec to a target type and, if successful, produce full
5134 * target spec (high-level, low-level + bit offset).
5136 static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
5137 const struct btf *targ_btf, __u32 targ_id,
5138 struct bpf_core_spec *targ_spec)
5140 const struct btf_type *targ_type;
5141 const struct bpf_core_accessor *local_acc;
5142 struct bpf_core_accessor *targ_acc;
5145 memset(targ_spec, 0, sizeof(*targ_spec));
5146 targ_spec->btf = targ_btf;
5147 targ_spec->root_type_id = targ_id;
5148 targ_spec->relo_kind = local_spec->relo_kind;
5150 if (core_relo_is_type_based(local_spec->relo_kind)) {
5151 return bpf_core_types_are_compat(local_spec->btf,
5152 local_spec->root_type_id,
5156 local_acc = &local_spec->spec[0];
5157 targ_acc = &targ_spec->spec[0];
5159 if (core_relo_is_enumval_based(local_spec->relo_kind)) {
5160 size_t local_essent_len, targ_essent_len;
5161 const struct btf_enum *e;
5162 const char *targ_name;
5164 /* has to resolve to an enum */
5165 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
5166 if (!btf_is_enum(targ_type))
5169 local_essent_len = bpf_core_essential_name_len(local_acc->name);
5171 for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
5172 targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
5173 targ_essent_len = bpf_core_essential_name_len(targ_name);
5174 if (targ_essent_len != local_essent_len)
5176 if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
5177 targ_acc->type_id = targ_id;
5179 targ_acc->name = targ_name;
5181 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
5182 targ_spec->raw_len++;
5189 if (!core_relo_is_field_based(local_spec->relo_kind))
5192 for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
5193 targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
5198 if (local_acc->name) {
5199 matched = bpf_core_match_member(local_spec->btf,
5202 targ_spec, &targ_id);
5206 /* for i=0, targ_id is already treated as array element
5207 * type (because it's the original struct), for others
5208 * we should find array element type first
5211 const struct btf_array *a;
5214 if (!btf_is_array(targ_type))
5217 a = btf_array(targ_type);
5218 flex = is_flex_arr(targ_btf, targ_acc - 1, a);
5219 if (!flex && local_acc->idx >= a->nelems)
5221 if (!skip_mods_and_typedefs(targ_btf, a->type,
5226 /* too deep struct/union/array nesting */
5227 if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
5230 targ_acc->type_id = targ_id;
5231 targ_acc->idx = local_acc->idx;
5232 targ_acc->name = NULL;
5234 targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
5235 targ_spec->raw_len++;
5237 sz = btf__resolve_size(targ_btf, targ_id);
5240 targ_spec->bit_offset += local_acc->idx * sz * 8;
5247 static int bpf_core_calc_field_relo(const struct bpf_program *prog,
5248 const struct bpf_core_relo *relo,
5249 const struct bpf_core_spec *spec,
5250 __u32 *val, __u32 *field_sz, __u32 *type_id,
5253 const struct bpf_core_accessor *acc;
5254 const struct btf_type *t;
5255 __u32 byte_off, byte_sz, bit_off, bit_sz, field_type_id;
5256 const struct btf_member *m;
5257 const struct btf_type *mt;
5263 if (relo->kind == BPF_FIELD_EXISTS) {
5264 *val = spec ? 1 : 0;
5269 return -EUCLEAN; /* request instruction poisoning */
5271 acc = &spec->spec[spec->len - 1];
5272 t = btf__type_by_id(spec->btf, acc->type_id);
5274 /* a[n] accessor needs special handling */
5276 if (relo->kind == BPF_FIELD_BYTE_OFFSET) {
5277 *val = spec->bit_offset / 8;
5278 /* remember field size for load/store mem size */
5279 sz = btf__resolve_size(spec->btf, acc->type_id);
5283 *type_id = acc->type_id;
5284 } else if (relo->kind == BPF_FIELD_BYTE_SIZE) {
5285 sz = btf__resolve_size(spec->btf, acc->type_id);
5290 pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
5291 prog->name, relo->kind, relo->insn_off / 8);
5299 m = btf_members(t) + acc->idx;
5300 mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
5301 bit_off = spec->bit_offset;
5302 bit_sz = btf_member_bitfield_size(t, acc->idx);
5304 bitfield = bit_sz > 0;
5307 byte_off = bit_off / 8 / byte_sz * byte_sz;
5308 /* figure out smallest int size necessary for bitfield load */
5309 while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
5311 /* bitfield can't be read with 64-bit read */
5312 pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
5313 prog->name, relo->kind, relo->insn_off / 8);
5317 byte_off = bit_off / 8 / byte_sz * byte_sz;
5320 sz = btf__resolve_size(spec->btf, field_type_id);
5324 byte_off = spec->bit_offset / 8;
5325 bit_sz = byte_sz * 8;
5328 /* for bitfields, all the relocatable aspects are ambiguous and we
5329 * might disagree with compiler, so turn off validation of expected
5330 * value, except for signedness
5333 *validate = !bitfield;
5335 switch (relo->kind) {
5336 case BPF_FIELD_BYTE_OFFSET:
5339 *field_sz = byte_sz;
5340 *type_id = field_type_id;
5343 case BPF_FIELD_BYTE_SIZE:
5346 case BPF_FIELD_SIGNED:
5347 /* enums will be assumed unsigned */
5348 *val = btf_is_enum(mt) ||
5349 (btf_int_encoding(mt) & BTF_INT_SIGNED);
5351 *validate = true; /* signedness is never ambiguous */
5353 case BPF_FIELD_LSHIFT_U64:
5354 #if __BYTE_ORDER == __LITTLE_ENDIAN
5355 *val = 64 - (bit_off + bit_sz - byte_off * 8);
5357 *val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8);
5360 case BPF_FIELD_RSHIFT_U64:
5363 *validate = true; /* right shift is never ambiguous */
5365 case BPF_FIELD_EXISTS:
5373 static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
5374 const struct bpf_core_spec *spec,
5379 /* type-based relos return zero when target type is not found */
5385 switch (relo->kind) {
5386 case BPF_TYPE_ID_TARGET:
5387 *val = spec->root_type_id;
5389 case BPF_TYPE_EXISTS:
5393 sz = btf__resolve_size(spec->btf, spec->root_type_id);
5398 case BPF_TYPE_ID_LOCAL:
5399 /* BPF_TYPE_ID_LOCAL is handled specially and shouldn't get here */
5407 static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
5408 const struct bpf_core_spec *spec,
5411 const struct btf_type *t;
5412 const struct btf_enum *e;
5414 switch (relo->kind) {
5415 case BPF_ENUMVAL_EXISTS:
5416 *val = spec ? 1 : 0;
5418 case BPF_ENUMVAL_VALUE:
5420 return -EUCLEAN; /* request instruction poisoning */
5421 t = btf__type_by_id(spec->btf, spec->spec[0].type_id);
5422 e = btf_enum(t) + spec->spec[0].idx;
5432 struct bpf_core_relo_res
5434 /* expected value in the instruction, unless validate == false */
5436 /* new value that needs to be patched up to */
5438 /* relocation unsuccessful, poison instruction, but don't fail load */
5440 /* some relocations can't be validated against orig_val */
5442 /* for field byte offset relocations or the forms:
5443 * *(T *)(rX + <off>) = rY
5444 * rX = *(T *)(rY + <off>),
5445 * we remember original and resolved field size to adjust direct
5446 * memory loads of pointers and integers; this is necessary for 32-bit
5447 * host kernel architectures, but also allows to automatically
5448 * relocate fields that were resized from, e.g., u32 to u64, etc.
5450 bool fail_memsz_adjust;
5457 /* Calculate original and target relocation values, given local and target
5458 * specs and relocation kind. These values are calculated for each candidate.
5459 * If there are multiple candidates, resulting values should all be consistent
5460 * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
5461 * If instruction has to be poisoned, *poison will be set to true.
5463 static int bpf_core_calc_relo(const struct bpf_program *prog,
5464 const struct bpf_core_relo *relo,
5466 const struct bpf_core_spec *local_spec,
5467 const struct bpf_core_spec *targ_spec,
5468 struct bpf_core_relo_res *res)
5470 int err = -EOPNOTSUPP;
5474 res->poison = false;
5475 res->validate = true;
5476 res->fail_memsz_adjust = false;
5477 res->orig_sz = res->new_sz = 0;
5478 res->orig_type_id = res->new_type_id = 0;
5480 if (core_relo_is_field_based(relo->kind)) {
5481 err = bpf_core_calc_field_relo(prog, relo, local_spec,
5482 &res->orig_val, &res->orig_sz,
5483 &res->orig_type_id, &res->validate);
5484 err = err ?: bpf_core_calc_field_relo(prog, relo, targ_spec,
5485 &res->new_val, &res->new_sz,
5486 &res->new_type_id, NULL);
5489 /* Validate if it's safe to adjust load/store memory size.
5490 * Adjustments are performed only if original and new memory
5493 res->fail_memsz_adjust = false;
5494 if (res->orig_sz != res->new_sz) {
5495 const struct btf_type *orig_t, *new_t;
5497 orig_t = btf__type_by_id(local_spec->btf, res->orig_type_id);
5498 new_t = btf__type_by_id(targ_spec->btf, res->new_type_id);
5500 /* There are two use cases in which it's safe to
5501 * adjust load/store's mem size:
5502 * - reading a 32-bit kernel pointer, while on BPF
5503 * size pointers are always 64-bit; in this case
5504 * it's safe to "downsize" instruction size due to
5505 * pointer being treated as unsigned integer with
5506 * zero-extended upper 32-bits;
5507 * - reading unsigned integers, again due to
5508 * zero-extension is preserving the value correctly.
5510 * In all other cases it's incorrect to attempt to
5511 * load/store field because read value will be
5512 * incorrect, so we poison relocated instruction.
5514 if (btf_is_ptr(orig_t) && btf_is_ptr(new_t))
5516 if (btf_is_int(orig_t) && btf_is_int(new_t) &&
5517 btf_int_encoding(orig_t) != BTF_INT_SIGNED &&
5518 btf_int_encoding(new_t) != BTF_INT_SIGNED)
5521 /* mark as invalid mem size adjustment, but this will
5522 * only be checked for LDX/STX/ST insns
5524 res->fail_memsz_adjust = true;
5526 } else if (core_relo_is_type_based(relo->kind)) {
5527 err = bpf_core_calc_type_relo(relo, local_spec, &res->orig_val);
5528 err = err ?: bpf_core_calc_type_relo(relo, targ_spec, &res->new_val);
5529 } else if (core_relo_is_enumval_based(relo->kind)) {
5530 err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
5531 err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
5535 if (err == -EUCLEAN) {
5536 /* EUCLEAN is used to signal instruction poisoning request */
5539 } else if (err == -EOPNOTSUPP) {
5540 /* EOPNOTSUPP means unknown/unsupported relocation */
5541 pr_warn("prog '%s': relo #%d: unrecognized CO-RE relocation %s (%d) at insn #%d\n",
5542 prog->name, relo_idx, core_relo_kind_str(relo->kind),
5543 relo->kind, relo->insn_off / 8);
5550 * Turn instruction for which CO_RE relocation failed into invalid one with
5551 * distinct signature.
5553 static void bpf_core_poison_insn(struct bpf_program *prog, int relo_idx,
5554 int insn_idx, struct bpf_insn *insn)
5556 pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
5557 prog->name, relo_idx, insn_idx);
5558 insn->code = BPF_JMP | BPF_CALL;
5562 /* if this instruction is reachable (not a dead code),
5563 * verifier will complain with the following message:
5564 * invalid func unknown#195896080
5566 insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */
5569 static bool is_ldimm64(struct bpf_insn *insn)
5571 return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
5574 static int insn_bpf_size_to_bytes(struct bpf_insn *insn)
5576 switch (BPF_SIZE(insn->code)) {
5577 case BPF_DW: return 8;
5578 case BPF_W: return 4;
5579 case BPF_H: return 2;
5580 case BPF_B: return 1;
5585 static int insn_bytes_to_bpf_size(__u32 sz)
5588 case 8: return BPF_DW;
5589 case 4: return BPF_W;
5590 case 2: return BPF_H;
5591 case 1: return BPF_B;
5597 * Patch relocatable BPF instruction.
5599 * Patched value is determined by relocation kind and target specification.
5600 * For existence relocations target spec will be NULL if field/type is not found.
5601 * Expected insn->imm value is determined using relocation kind and local
5602 * spec, and is checked before patching instruction. If actual insn->imm value
5603 * is wrong, bail out with error.
5605 * Currently supported classes of BPF instruction are:
5606 * 1. rX = <imm> (assignment with immediate operand);
5607 * 2. rX += <imm> (arithmetic operations with immediate operand);
5608 * 3. rX = <imm64> (load with 64-bit immediate value);
5609 * 4. rX = *(T *)(rY + <off>), where T is one of {u8, u16, u32, u64};
5610 * 5. *(T *)(rX + <off>) = rY, where T is one of {u8, u16, u32, u64};
5611 * 6. *(T *)(rX + <off>) = <imm>, where T is one of {u8, u16, u32, u64}.
5613 static int bpf_core_patch_insn(struct bpf_program *prog,
5614 const struct bpf_core_relo *relo,
5616 const struct bpf_core_relo_res *res)
5618 __u32 orig_val, new_val;
5619 struct bpf_insn *insn;
5623 if (relo->insn_off % BPF_INSN_SZ)
5625 insn_idx = relo->insn_off / BPF_INSN_SZ;
5626 /* adjust insn_idx from section frame of reference to the local
5627 * program's frame of reference; (sub-)program code is not yet
5628 * relocated, so it's enough to just subtract in-section offset
5630 insn_idx = insn_idx - prog->sec_insn_off;
5631 insn = &prog->insns[insn_idx];
5632 class = BPF_CLASS(insn->code);
5636 /* poison second part of ldimm64 to avoid confusing error from
5637 * verifier about "unknown opcode 00"
5639 if (is_ldimm64(insn))
5640 bpf_core_poison_insn(prog, relo_idx, insn_idx + 1, insn + 1);
5641 bpf_core_poison_insn(prog, relo_idx, insn_idx, insn);
5645 orig_val = res->orig_val;
5646 new_val = res->new_val;
5651 if (BPF_SRC(insn->code) != BPF_K)
5653 if (res->validate && insn->imm != orig_val) {
5654 pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n",
5655 prog->name, relo_idx,
5656 insn_idx, insn->imm, orig_val, new_val);
5659 orig_val = insn->imm;
5660 insn->imm = new_val;
5661 pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n",
5662 prog->name, relo_idx, insn_idx,
5668 if (res->validate && insn->off != orig_val) {
5669 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %u -> %u\n",
5670 prog->name, relo_idx, insn_idx, insn->off, orig_val, new_val);
5673 if (new_val > SHRT_MAX) {
5674 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n",
5675 prog->name, relo_idx, insn_idx, new_val);
5678 if (res->fail_memsz_adjust) {
5679 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) accesses field incorrectly. "
5680 "Make sure you are accessing pointers, unsigned integers, or fields of matching type and size.\n",
5681 prog->name, relo_idx, insn_idx);
5685 orig_val = insn->off;
5686 insn->off = new_val;
5687 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n",
5688 prog->name, relo_idx, insn_idx, orig_val, new_val);
5690 if (res->new_sz != res->orig_sz) {
5691 int insn_bytes_sz, insn_bpf_sz;
5693 insn_bytes_sz = insn_bpf_size_to_bytes(insn);
5694 if (insn_bytes_sz != res->orig_sz) {
5695 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) unexpected mem size: got %d, exp %u\n",
5696 prog->name, relo_idx, insn_idx, insn_bytes_sz, res->orig_sz);
5700 insn_bpf_sz = insn_bytes_to_bpf_size(res->new_sz);
5701 if (insn_bpf_sz < 0) {
5702 pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) invalid new mem size: %u\n",
5703 prog->name, relo_idx, insn_idx, res->new_sz);
5707 insn->code = BPF_MODE(insn->code) | insn_bpf_sz | BPF_CLASS(insn->code);
5708 pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) mem_sz %u -> %u\n",
5709 prog->name, relo_idx, insn_idx, res->orig_sz, res->new_sz);
5715 if (!is_ldimm64(insn) ||
5716 insn[0].src_reg != 0 || insn[0].off != 0 ||
5717 insn_idx + 1 >= prog->insns_cnt ||
5718 insn[1].code != 0 || insn[1].dst_reg != 0 ||
5719 insn[1].src_reg != 0 || insn[1].off != 0) {
5720 pr_warn("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n",
5721 prog->name, relo_idx, insn_idx);
5725 imm = insn[0].imm + ((__u64)insn[1].imm << 32);
5726 if (res->validate && imm != orig_val) {
5727 pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %u -> %u\n",
5728 prog->name, relo_idx,
5729 insn_idx, (unsigned long long)imm,
5734 insn[0].imm = new_val;
5735 insn[1].imm = 0; /* currently only 32-bit values are supported */
5736 pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %u\n",
5737 prog->name, relo_idx, insn_idx,
5738 (unsigned long long)imm, new_val);
5742 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",
5743 prog->name, relo_idx, insn_idx, insn->code,
5744 insn->src_reg, insn->dst_reg, insn->off, insn->imm);
5751 /* Output spec definition in the format:
5752 * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
5753 * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
5755 static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
5757 const struct btf_type *t;
5758 const struct btf_enum *e;
5763 type_id = spec->root_type_id;
5764 t = btf__type_by_id(spec->btf, type_id);
5765 s = btf__name_by_offset(spec->btf, t->name_off);
5767 libbpf_print(level, "[%u] %s %s", type_id, btf_kind_str(t), str_is_empty(s) ? "<anon>" : s);
5769 if (core_relo_is_type_based(spec->relo_kind))
5772 if (core_relo_is_enumval_based(spec->relo_kind)) {
5773 t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
5774 e = btf_enum(t) + spec->raw_spec[0];
5775 s = btf__name_by_offset(spec->btf, e->name_off);
5777 libbpf_print(level, "::%s = %u", s, e->val);
5781 if (core_relo_is_field_based(spec->relo_kind)) {
5782 for (i = 0; i < spec->len; i++) {
5783 if (spec->spec[i].name)
5784 libbpf_print(level, ".%s", spec->spec[i].name);
5785 else if (i > 0 || spec->spec[i].idx > 0)
5786 libbpf_print(level, "[%u]", spec->spec[i].idx);
5789 libbpf_print(level, " (");
5790 for (i = 0; i < spec->raw_len; i++)
5791 libbpf_print(level, "%s%d", i == 0 ? "" : ":", spec->raw_spec[i]);
5793 if (spec->bit_offset % 8)
5794 libbpf_print(level, " @ offset %u.%u)",
5795 spec->bit_offset / 8, spec->bit_offset % 8);
5797 libbpf_print(level, " @ offset %u)", spec->bit_offset / 8);
5802 static size_t bpf_core_hash_fn(const void *key, void *ctx)
5807 static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
5812 static void *u32_as_hash_key(__u32 x)
5814 return (void *)(uintptr_t)x;
5818 * CO-RE relocate single instruction.
5820 * The outline and important points of the algorithm:
5821 * 1. For given local type, find corresponding candidate target types.
5822 * Candidate type is a type with the same "essential" name, ignoring
5823 * everything after last triple underscore (___). E.g., `sample`,
5824 * `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
5825 * for each other. Names with triple underscore are referred to as
5826 * "flavors" and are useful, among other things, to allow to
5827 * specify/support incompatible variations of the same kernel struct, which
5828 * might differ between different kernel versions and/or build
5831 * N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
5832 * converter, when deduplicated BTF of a kernel still contains more than
5833 * one different types with the same name. In that case, ___2, ___3, etc
5834 * are appended starting from second name conflict. But start flavors are
5835 * also useful to be defined "locally", in BPF program, to extract same
5836 * data from incompatible changes between different kernel
5837 * versions/configurations. For instance, to handle field renames between
5838 * kernel versions, one can use two flavors of the struct name with the
5839 * same common name and use conditional relocations to extract that field,
5840 * depending on target kernel version.
5841 * 2. For each candidate type, try to match local specification to this
5842 * candidate target type. Matching involves finding corresponding
5843 * high-level spec accessors, meaning that all named fields should match,
5844 * as well as all array accesses should be within the actual bounds. Also,
5845 * types should be compatible (see bpf_core_fields_are_compat for details).
5846 * 3. It is supported and expected that there might be multiple flavors
5847 * matching the spec. As long as all the specs resolve to the same set of
5848 * offsets across all candidates, there is no error. If there is any
5849 * ambiguity, CO-RE relocation will fail. This is necessary to accomodate
5850 * imprefection of BTF deduplication, which can cause slight duplication of
5851 * the same BTF type, if some directly or indirectly referenced (by
5852 * pointer) type gets resolved to different actual types in different
5853 * object files. If such situation occurs, deduplicated BTF will end up
5854 * with two (or more) structurally identical types, which differ only in
5855 * types they refer to through pointer. This should be OK in most cases and
5857 * 4. Candidate types search is performed by linearly scanning through all
5858 * types in target BTF. It is anticipated that this is overall more
5859 * efficient memory-wise and not significantly worse (if not better)
5860 * CPU-wise compared to prebuilding a map from all local type names to
5861 * a list of candidate type names. It's also sped up by caching resolved
5862 * list of matching candidates per each local "root" type ID, that has at
5863 * least one bpf_core_relo associated with it. This list is shared
5864 * between multiple relocations for the same type ID and is updated as some
5865 * of the candidates are pruned due to structural incompatibility.
5867 static int bpf_core_apply_relo(struct bpf_program *prog,
5868 const struct bpf_core_relo *relo,
5870 const struct btf *local_btf,
5871 struct hashmap *cand_cache)
5873 struct bpf_core_spec local_spec, cand_spec, targ_spec = {};
5874 const void *type_key = u32_as_hash_key(relo->type_id);
5875 struct bpf_core_relo_res cand_res, targ_res;
5876 const struct btf_type *local_type;
5877 const char *local_name;
5878 struct core_cand_list *cands = NULL;
5880 const char *spec_str;
5883 local_id = relo->type_id;
5884 local_type = btf__type_by_id(local_btf, local_id);
5888 local_name = btf__name_by_offset(local_btf, local_type->name_off);
5892 spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
5893 if (str_is_empty(spec_str))
5896 err = bpf_core_parse_spec(local_btf, local_id, spec_str, relo->kind, &local_spec);
5898 pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
5899 prog->name, relo_idx, local_id, btf_kind_str(local_type),
5900 str_is_empty(local_name) ? "<anon>" : local_name,
5905 pr_debug("prog '%s': relo #%d: kind <%s> (%d), spec is ", prog->name,
5906 relo_idx, core_relo_kind_str(relo->kind), relo->kind);
5907 bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec);
5908 libbpf_print(LIBBPF_DEBUG, "\n");
5910 /* TYPE_ID_LOCAL relo is special and doesn't need candidate search */
5911 if (relo->kind == BPF_TYPE_ID_LOCAL) {
5912 targ_res.validate = true;
5913 targ_res.poison = false;
5914 targ_res.orig_val = local_spec.root_type_id;
5915 targ_res.new_val = local_spec.root_type_id;
5919 /* libbpf doesn't support candidate search for anonymous types */
5920 if (str_is_empty(spec_str)) {
5921 pr_warn("prog '%s': relo #%d: <%s> (%d) relocation doesn't support anonymous types\n",
5922 prog->name, relo_idx, core_relo_kind_str(relo->kind), relo->kind);
5926 if (!hashmap__find(cand_cache, type_key, (void **)&cands)) {
5927 cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5928 if (IS_ERR(cands)) {
5929 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5930 prog->name, relo_idx, local_id, btf_kind_str(local_type),
5931 local_name, PTR_ERR(cands));
5932 return PTR_ERR(cands);
5934 err = hashmap__set(cand_cache, type_key, cands, NULL, NULL);
5936 bpf_core_free_cands(cands);
5941 for (i = 0, j = 0; i < cands->len; i++) {
5942 err = bpf_core_spec_match(&local_spec, cands->cands[i].btf,
5943 cands->cands[i].id, &cand_spec);
5945 pr_warn("prog '%s': relo #%d: error matching candidate #%d ",
5946 prog->name, relo_idx, i);
5947 bpf_core_dump_spec(LIBBPF_WARN, &cand_spec);
5948 libbpf_print(LIBBPF_WARN, ": %d\n", err);
5952 pr_debug("prog '%s': relo #%d: %s candidate #%d ", prog->name,
5953 relo_idx, err == 0 ? "non-matching" : "matching", i);
5954 bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec);
5955 libbpf_print(LIBBPF_DEBUG, "\n");
5960 err = bpf_core_calc_relo(prog, relo, relo_idx, &local_spec, &cand_spec, &cand_res);
5965 targ_res = cand_res;
5966 targ_spec = cand_spec;
5967 } else if (cand_spec.bit_offset != targ_spec.bit_offset) {
5968 /* if there are many field relo candidates, they
5969 * should all resolve to the same bit offset
5971 pr_warn("prog '%s': relo #%d: field offset ambiguity: %u != %u\n",
5972 prog->name, relo_idx, cand_spec.bit_offset,
5973 targ_spec.bit_offset);
5975 } else if (cand_res.poison != targ_res.poison || cand_res.new_val != targ_res.new_val) {
5976 /* all candidates should result in the same relocation
5977 * decision and value, otherwise it's dangerous to
5978 * proceed due to ambiguity
5980 pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %u != %s %u\n",
5981 prog->name, relo_idx,
5982 cand_res.poison ? "failure" : "success", cand_res.new_val,
5983 targ_res.poison ? "failure" : "success", targ_res.new_val);
5987 cands->cands[j++] = cands->cands[i];
5991 * For BPF_FIELD_EXISTS relo or when used BPF program has field
5992 * existence checks or kernel version/config checks, it's expected
5993 * that we might not find any candidates. In this case, if field
5994 * wasn't found in any candidate, the list of candidates shouldn't
5995 * change at all, we'll just handle relocating appropriately,
5996 * depending on relo's kind.
6002 * If no candidates were found, it might be both a programmer error,
6003 * as well as expected case, depending whether instruction w/
6004 * relocation is guarded in some way that makes it unreachable (dead
6005 * code) if relocation can't be resolved. This is handled in
6006 * bpf_core_patch_insn() uniformly by replacing that instruction with
6007 * BPF helper call insn (using invalid helper ID). If that instruction
6008 * is indeed unreachable, then it will be ignored and eliminated by
6009 * verifier. If it was an error, then verifier will complain and point
6010 * to a specific instruction number in its log.
6013 pr_debug("prog '%s': relo #%d: no matching targets found\n",
6014 prog->name, relo_idx);
6016 /* calculate single target relo result explicitly */
6017 err = bpf_core_calc_relo(prog, relo, relo_idx, &local_spec, NULL, &targ_res);
6023 /* bpf_core_patch_insn() should know how to handle missing targ_spec */
6024 err = bpf_core_patch_insn(prog, relo, relo_idx, &targ_res);
6026 pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
6027 prog->name, relo_idx, relo->insn_off, err);
6035 bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
6037 const struct btf_ext_info_sec *sec;
6038 const struct bpf_core_relo *rec;
6039 const struct btf_ext_info *seg;
6040 struct hashmap_entry *entry;
6041 struct hashmap *cand_cache = NULL;
6042 struct bpf_program *prog;
6043 const char *sec_name;
6044 int i, err = 0, insn_idx, sec_idx;
6046 if (obj->btf_ext->core_relo_info.len == 0)
6049 if (targ_btf_path) {
6050 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
6051 if (IS_ERR_OR_NULL(obj->btf_vmlinux_override)) {
6052 err = PTR_ERR(obj->btf_vmlinux_override);
6053 pr_warn("failed to parse target BTF: %d\n", err);
6058 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
6059 if (IS_ERR(cand_cache)) {
6060 err = PTR_ERR(cand_cache);
6064 seg = &obj->btf_ext->core_relo_info;
6065 for_each_btf_ext_sec(seg, sec) {
6066 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
6067 if (str_is_empty(sec_name)) {
6071 /* bpf_object's ELF is gone by now so it's not easy to find
6072 * section index by section name, but we can find *any*
6073 * bpf_program within desired section name and use it's
6074 * prog->sec_idx to do a proper search by section index and
6075 * instruction offset
6078 for (i = 0; i < obj->nr_programs; i++) {
6079 prog = &obj->programs[i];
6080 if (strcmp(prog->sec_name, sec_name) == 0)
6084 pr_warn("sec '%s': failed to find a BPF program\n", sec_name);
6087 sec_idx = prog->sec_idx;
6089 pr_debug("sec '%s': found %d CO-RE relocations\n",
6090 sec_name, sec->num_info);
6092 for_each_btf_ext_rec(seg, sec, i, rec) {
6093 insn_idx = rec->insn_off / BPF_INSN_SZ;
6094 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
6096 pr_warn("sec '%s': failed to find program at insn #%d for CO-RE offset relocation #%d\n",
6097 sec_name, insn_idx, i);
6101 /* no need to apply CO-RE relocation if the program is
6102 * not going to be loaded
6107 err = bpf_core_apply_relo(prog, rec, i, obj->btf, cand_cache);
6109 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
6110 prog->name, i, err);
6117 /* obj->btf_vmlinux and module BTFs are freed after object load */
6118 btf__free(obj->btf_vmlinux_override);
6119 obj->btf_vmlinux_override = NULL;
6121 if (!IS_ERR_OR_NULL(cand_cache)) {
6122 hashmap__for_each_entry(cand_cache, entry, i) {
6123 bpf_core_free_cands(entry->value);
6125 hashmap__free(cand_cache);
6130 /* Relocate data references within program code:
6132 * - global variable references;
6133 * - extern references.
6136 bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
6140 for (i = 0; i < prog->nr_reloc; i++) {
6141 struct reloc_desc *relo = &prog->reloc_desc[i];
6142 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6143 struct extern_desc *ext;
6145 switch (relo->type) {
6147 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
6148 insn[0].imm = obj->maps[relo->map_idx].fd;
6149 relo->processed = true;
6152 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6153 insn[1].imm = insn[0].imm + relo->sym_off;
6154 insn[0].imm = obj->maps[relo->map_idx].fd;
6155 relo->processed = true;
6158 ext = &obj->externs[relo->sym_off];
6159 if (ext->type == EXT_KCFG) {
6160 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6161 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
6162 insn[1].imm = ext->kcfg.data_off;
6163 } else /* EXT_KSYM */ {
6164 if (ext->ksym.type_id) { /* typed ksyms */
6165 insn[0].src_reg = BPF_PSEUDO_BTF_ID;
6166 insn[0].imm = ext->ksym.kernel_btf_id;
6167 insn[1].imm = ext->ksym.kernel_btf_obj_fd;
6168 } else { /* typeless ksyms */
6169 insn[0].imm = (__u32)ext->ksym.addr;
6170 insn[1].imm = ext->ksym.addr >> 32;
6173 relo->processed = true;
6176 /* will be handled as a follow up pass */
6179 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6180 prog->name, i, relo->type);
6188 static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
6189 const struct bpf_program *prog,
6190 const struct btf_ext_info *ext_info,
6191 void **prog_info, __u32 *prog_rec_cnt,
6194 void *copy_start = NULL, *copy_end = NULL;
6195 void *rec, *rec_end, *new_prog_info;
6196 const struct btf_ext_info_sec *sec;
6197 size_t old_sz, new_sz;
6198 const char *sec_name;
6201 for_each_btf_ext_sec(ext_info, sec) {
6202 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
6205 if (strcmp(sec_name, prog->sec_name) != 0)
6208 for_each_btf_ext_rec(ext_info, sec, i, rec) {
6209 __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
6211 if (insn_off < prog->sec_insn_off)
6213 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
6218 copy_end = rec + ext_info->rec_size;
6224 /* append func/line info of a given (sub-)program to the main
6225 * program func/line info
6227 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
6228 new_sz = old_sz + (copy_end - copy_start);
6229 new_prog_info = realloc(*prog_info, new_sz);
6232 *prog_info = new_prog_info;
6233 *prog_rec_cnt = new_sz / ext_info->rec_size;
6234 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
6236 /* Kernel instruction offsets are in units of 8-byte
6237 * instructions, while .BTF.ext instruction offsets generated
6238 * by Clang are in units of bytes. So convert Clang offsets
6239 * into kernel offsets and adjust offset according to program
6240 * relocated position.
6242 off_adj = prog->sub_insn_off - prog->sec_insn_off;
6243 rec = new_prog_info + old_sz;
6244 rec_end = new_prog_info + new_sz;
6245 for (; rec < rec_end; rec += ext_info->rec_size) {
6246 __u32 *insn_off = rec;
6248 *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
6250 *prog_rec_sz = ext_info->rec_size;
6258 reloc_prog_func_and_line_info(const struct bpf_object *obj,
6259 struct bpf_program *main_prog,
6260 const struct bpf_program *prog)
6264 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6265 * supprot func/line info
6267 if (!obj->btf_ext || !kernel_supports(FEAT_BTF_FUNC))
6270 /* only attempt func info relocation if main program's func_info
6271 * relocation was successful
6273 if (main_prog != prog && !main_prog->func_info)
6276 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
6277 &main_prog->func_info,
6278 &main_prog->func_info_cnt,
6279 &main_prog->func_info_rec_size);
6281 if (err != -ENOENT) {
6282 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
6286 if (main_prog->func_info) {
6288 * Some info has already been found but has problem
6289 * in the last btf_ext reloc. Must have to error out.
6291 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
6294 /* Have problem loading the very first info. Ignore the rest. */
6295 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6300 /* don't relocate line info if main program's relocation failed */
6301 if (main_prog != prog && !main_prog->line_info)
6304 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
6305 &main_prog->line_info,
6306 &main_prog->line_info_cnt,
6307 &main_prog->line_info_rec_size);
6309 if (err != -ENOENT) {
6310 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
6314 if (main_prog->line_info) {
6316 * Some info has already been found but has problem
6317 * in the last btf_ext reloc. Must have to error out.
6319 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
6322 /* Have problem loading the very first info. Ignore the rest. */
6323 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6329 static int cmp_relo_by_insn_idx(const void *key, const void *elem)
6331 size_t insn_idx = *(const size_t *)key;
6332 const struct reloc_desc *relo = elem;
6334 if (insn_idx == relo->insn_idx)
6336 return insn_idx < relo->insn_idx ? -1 : 1;
6339 static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
6341 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
6342 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
6346 bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
6347 struct bpf_program *prog)
6349 size_t sub_insn_idx, insn_idx, new_cnt;
6350 struct bpf_program *subprog;
6351 struct bpf_insn *insns, *insn;
6352 struct reloc_desc *relo;
6355 err = reloc_prog_func_and_line_info(obj, main_prog, prog);
6359 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
6360 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6361 if (!insn_is_subprog_call(insn))
6364 relo = find_prog_insn_relo(prog, insn_idx);
6365 if (relo && relo->type != RELO_CALL) {
6366 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6367 prog->name, insn_idx, relo->type);
6368 return -LIBBPF_ERRNO__RELOC;
6371 /* sub-program instruction index is a combination of
6372 * an offset of a symbol pointed to by relocation and
6373 * call instruction's imm field; for global functions,
6374 * call always has imm = -1, but for static functions
6375 * relocation is against STT_SECTION and insn->imm
6376 * points to a start of a static function
6378 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
6380 /* if subprogram call is to a static function within
6381 * the same ELF section, there won't be any relocation
6382 * emitted, but it also means there is no additional
6383 * offset necessary, insns->imm is relative to
6384 * instruction's original position within the section
6386 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
6389 /* we enforce that sub-programs should be in .text section */
6390 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
6392 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6394 return -LIBBPF_ERRNO__RELOC;
6397 /* if it's the first call instruction calling into this
6398 * subprogram (meaning this subprog hasn't been processed
6399 * yet) within the context of current main program:
6400 * - append it at the end of main program's instructions blog;
6401 * - process is recursively, while current program is put on hold;
6402 * - if that subprogram calls some other not yet processes
6403 * subprogram, same thing will happen recursively until
6404 * there are no more unprocesses subprograms left to append
6407 if (subprog->sub_insn_off == 0) {
6408 subprog->sub_insn_off = main_prog->insns_cnt;
6410 new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
6411 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
6413 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
6416 main_prog->insns = insns;
6417 main_prog->insns_cnt = new_cnt;
6419 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
6420 subprog->insns_cnt * sizeof(*insns));
6422 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6423 main_prog->name, subprog->insns_cnt, subprog->name);
6425 err = bpf_object__reloc_code(obj, main_prog, subprog);
6430 /* main_prog->insns memory could have been re-allocated, so
6431 * calculate pointer again
6433 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6434 /* calculate correct instruction position within current main
6435 * prog; each main prog can have a different set of
6436 * subprograms appended (potentially in different order as
6437 * well), so position of any subprog can be different for
6438 * different main programs */
6439 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
6442 relo->processed = true;
6444 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6445 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
6452 * Relocate sub-program calls.
6454 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6455 * main prog) is processed separately. For each subprog (non-entry functions,
6456 * that can be called from either entry progs or other subprogs) gets their
6457 * sub_insn_off reset to zero. This serves as indicator that this subprogram
6458 * hasn't been yet appended and relocated within current main prog. Once its
6459 * relocated, sub_insn_off will point at the position within current main prog
6460 * where given subprog was appended. This will further be used to relocate all
6461 * the call instructions jumping into this subprog.
6463 * We start with main program and process all call instructions. If the call
6464 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6465 * is zero), subprog instructions are appended at the end of main program's
6466 * instruction array. Then main program is "put on hold" while we recursively
6467 * process newly appended subprogram. If that subprogram calls into another
6468 * subprogram that hasn't been appended, new subprogram is appended again to
6469 * the *main* prog's instructions (subprog's instructions are always left
6470 * untouched, as they need to be in unmodified state for subsequent main progs
6471 * and subprog instructions are always sent only as part of a main prog) and
6472 * the process continues recursively. Once all the subprogs called from a main
6473 * prog or any of its subprogs are appended (and relocated), all their
6474 * positions within finalized instructions array are known, so it's easy to
6475 * rewrite call instructions with correct relative offsets, corresponding to
6476 * desired target subprog.
6478 * Its important to realize that some subprogs might not be called from some
6479 * main prog and any of its called/used subprogs. Those will keep their
6480 * subprog->sub_insn_off as zero at all times and won't be appended to current
6481 * main prog and won't be relocated within the context of current main prog.
6482 * They might still be used from other main progs later.
6484 * Visually this process can be shown as below. Suppose we have two main
6485 * programs mainA and mainB and BPF object contains three subprogs: subA,
6486 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6487 * subC both call subB:
6489 * +--------+ +-------+
6491 * +--+---+ +--+-+-+ +---+--+
6492 * | subA | | subB | | subC |
6493 * +--+---+ +------+ +---+--+
6496 * +---+-------+ +------+----+
6497 * | mainA | | mainB |
6498 * +-----------+ +-----------+
6500 * We'll start relocating mainA, will find subA, append it and start
6501 * processing sub A recursively:
6503 * +-----------+------+
6505 * +-----------+------+
6507 * At this point we notice that subB is used from subA, so we append it and
6508 * relocate (there are no further subcalls from subB):
6510 * +-----------+------+------+
6511 * | mainA | subA | subB |
6512 * +-----------+------+------+
6514 * At this point, we relocate subA calls, then go one level up and finish with
6515 * relocatin mainA calls. mainA is done.
6517 * For mainB process is similar but results in different order. We start with
6518 * mainB and skip subA and subB, as mainB never calls them (at least
6519 * directly), but we see subC is needed, so we append and start processing it:
6521 * +-----------+------+
6523 * +-----------+------+
6524 * Now we see subC needs subB, so we go back to it, append and relocate it:
6526 * +-----------+------+------+
6527 * | mainB | subC | subB |
6528 * +-----------+------+------+
6530 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6533 bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6535 struct bpf_program *subprog;
6538 /* mark all subprogs as not relocated (yet) within the context of
6539 * current main program
6541 for (i = 0; i < obj->nr_programs; i++) {
6542 subprog = &obj->programs[i];
6543 if (!prog_is_subprog(obj, subprog))
6546 subprog->sub_insn_off = 0;
6547 for (j = 0; j < subprog->nr_reloc; j++)
6548 if (subprog->reloc_desc[j].type == RELO_CALL)
6549 subprog->reloc_desc[j].processed = false;
6552 err = bpf_object__reloc_code(obj, prog, prog);
6561 bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
6563 struct bpf_program *prog;
6568 err = bpf_object__relocate_core(obj, targ_btf_path);
6570 pr_warn("failed to perform CO-RE relocations: %d\n",
6575 /* relocate data references first for all programs and sub-programs,
6576 * as they don't change relative to code locations, so subsequent
6577 * subprogram processing won't need to re-calculate any of them
6579 for (i = 0; i < obj->nr_programs; i++) {
6580 prog = &obj->programs[i];
6581 err = bpf_object__relocate_data(obj, prog);
6583 pr_warn("prog '%s': failed to relocate data references: %d\n",
6588 /* now relocate subprogram calls and append used subprograms to main
6589 * programs; each copy of subprogram code needs to be relocated
6590 * differently for each main program, because its code location might
6593 for (i = 0; i < obj->nr_programs; i++) {
6594 prog = &obj->programs[i];
6595 /* sub-program's sub-calls are relocated within the context of
6596 * its main program only
6598 if (prog_is_subprog(obj, prog))
6601 err = bpf_object__relocate_calls(obj, prog);
6603 pr_warn("prog '%s': failed to relocate calls: %d\n",
6608 /* free up relocation descriptors */
6609 for (i = 0; i < obj->nr_programs; i++) {
6610 prog = &obj->programs[i];
6611 zfree(&prog->reloc_desc);
6617 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
6618 GElf_Shdr *shdr, Elf_Data *data);
6620 static int bpf_object__collect_map_relos(struct bpf_object *obj,
6621 GElf_Shdr *shdr, Elf_Data *data)
6623 const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
6624 int i, j, nrels, new_sz;
6625 const struct btf_var_secinfo *vi = NULL;
6626 const struct btf_type *sec, *var, *def;
6627 struct bpf_map *map = NULL, *targ_map;
6628 const struct btf_member *member;
6629 const char *name, *mname;
6636 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
6638 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
6642 symbols = obj->efile.symbols;
6643 nrels = shdr->sh_size / shdr->sh_entsize;
6644 for (i = 0; i < nrels; i++) {
6645 if (!gelf_getrel(data, i, &rel)) {
6646 pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
6647 return -LIBBPF_ERRNO__FORMAT;
6649 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
6650 pr_warn(".maps relo #%d: symbol %zx not found\n",
6651 i, (size_t)GELF_R_SYM(rel.r_info));
6652 return -LIBBPF_ERRNO__FORMAT;
6654 name = elf_sym_str(obj, sym.st_name) ?: "<?>";
6655 if (sym.st_shndx != obj->efile.btf_maps_shndx) {
6656 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
6658 return -LIBBPF_ERRNO__RELOC;
6661 pr_debug(".maps relo #%d: for %zd value %zd rel.r_offset %zu name %d ('%s')\n",
6662 i, (ssize_t)(rel.r_info >> 32), (size_t)sym.st_value,
6663 (size_t)rel.r_offset, sym.st_name, name);
6665 for (j = 0; j < obj->nr_maps; j++) {
6666 map = &obj->maps[j];
6667 if (map->sec_idx != obj->efile.btf_maps_shndx)
6670 vi = btf_var_secinfos(sec) + map->btf_var_idx;
6671 if (vi->offset <= rel.r_offset &&
6672 rel.r_offset + bpf_ptr_sz <= vi->offset + vi->size)
6675 if (j == obj->nr_maps) {
6676 pr_warn(".maps relo #%d: cannot find map '%s' at rel.r_offset %zu\n",
6677 i, name, (size_t)rel.r_offset);
6681 if (!bpf_map_type__is_map_in_map(map->def.type))
6683 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
6684 map->def.key_size != sizeof(int)) {
6685 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
6686 i, map->name, sizeof(int));
6690 targ_map = bpf_object__find_map_by_name(obj, name);
6694 var = btf__type_by_id(obj->btf, vi->type);
6695 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
6696 if (btf_vlen(def) == 0)
6698 member = btf_members(def) + btf_vlen(def) - 1;
6699 mname = btf__name_by_offset(obj->btf, member->name_off);
6700 if (strcmp(mname, "values"))
6703 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
6704 if (rel.r_offset - vi->offset < moff)
6707 moff = rel.r_offset - vi->offset - moff;
6708 /* here we use BPF pointer size, which is always 64 bit, as we
6709 * are parsing ELF that was built for BPF target
6711 if (moff % bpf_ptr_sz)
6714 if (moff >= map->init_slots_sz) {
6716 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
6719 map->init_slots = tmp;
6720 memset(map->init_slots + map->init_slots_sz, 0,
6721 (new_sz - map->init_slots_sz) * host_ptr_sz);
6722 map->init_slots_sz = new_sz;
6724 map->init_slots[moff] = targ_map;
6726 pr_debug(".maps relo #%d: map '%s' slot [%d] points to map '%s'\n",
6727 i, map->name, moff, name);
6733 static int cmp_relocs(const void *_a, const void *_b)
6735 const struct reloc_desc *a = _a;
6736 const struct reloc_desc *b = _b;
6738 if (a->insn_idx != b->insn_idx)
6739 return a->insn_idx < b->insn_idx ? -1 : 1;
6741 /* no two relocations should have the same insn_idx, but ... */
6742 if (a->type != b->type)
6743 return a->type < b->type ? -1 : 1;
6748 static int bpf_object__collect_relos(struct bpf_object *obj)
6752 for (i = 0; i < obj->efile.nr_reloc_sects; i++) {
6753 GElf_Shdr *shdr = &obj->efile.reloc_sects[i].shdr;
6754 Elf_Data *data = obj->efile.reloc_sects[i].data;
6755 int idx = shdr->sh_info;
6757 if (shdr->sh_type != SHT_REL) {
6758 pr_warn("internal error at %d\n", __LINE__);
6759 return -LIBBPF_ERRNO__INTERNAL;
6762 if (idx == obj->efile.st_ops_shndx)
6763 err = bpf_object__collect_st_ops_relos(obj, shdr, data);
6764 else if (idx == obj->efile.btf_maps_shndx)
6765 err = bpf_object__collect_map_relos(obj, shdr, data);
6767 err = bpf_object__collect_prog_relos(obj, shdr, data);
6772 for (i = 0; i < obj->nr_programs; i++) {
6773 struct bpf_program *p = &obj->programs[i];
6778 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6783 static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
6785 if (BPF_CLASS(insn->code) == BPF_JMP &&
6786 BPF_OP(insn->code) == BPF_CALL &&
6787 BPF_SRC(insn->code) == BPF_K &&
6788 insn->src_reg == 0 &&
6789 insn->dst_reg == 0) {
6790 *func_id = insn->imm;
6796 static int bpf_object__sanitize_prog(struct bpf_object* obj, struct bpf_program *prog)
6798 struct bpf_insn *insn = prog->insns;
6799 enum bpf_func_id func_id;
6802 for (i = 0; i < prog->insns_cnt; i++, insn++) {
6803 if (!insn_is_helper_call(insn, &func_id))
6806 /* on kernels that don't yet support
6807 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
6808 * to bpf_probe_read() which works well for old kernels
6811 case BPF_FUNC_probe_read_kernel:
6812 case BPF_FUNC_probe_read_user:
6813 if (!kernel_supports(FEAT_PROBE_READ_KERN))
6814 insn->imm = BPF_FUNC_probe_read;
6816 case BPF_FUNC_probe_read_kernel_str:
6817 case BPF_FUNC_probe_read_user_str:
6818 if (!kernel_supports(FEAT_PROBE_READ_KERN))
6819 insn->imm = BPF_FUNC_probe_read_str;
6829 load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
6830 char *license, __u32 kern_version, int *pfd)
6832 struct bpf_prog_load_params load_attr = {};
6833 char *cp, errmsg[STRERR_BUFSIZE];
6834 size_t log_buf_size = 0;
6835 char *log_buf = NULL;
6838 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
6840 * The program type must be set. Most likely we couldn't find a proper
6841 * section definition at load time, and thus we didn't infer the type.
6843 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
6844 prog->name, prog->sec_name);
6848 if (!insns || !insns_cnt)
6851 load_attr.prog_type = prog->type;
6852 /* old kernels might not support specifying expected_attach_type */
6853 if (!kernel_supports(FEAT_EXP_ATTACH_TYPE) && prog->sec_def &&
6854 prog->sec_def->is_exp_attach_type_optional)
6855 load_attr.expected_attach_type = 0;
6857 load_attr.expected_attach_type = prog->expected_attach_type;
6858 if (kernel_supports(FEAT_PROG_NAME))
6859 load_attr.name = prog->name;
6860 load_attr.insns = insns;
6861 load_attr.insn_cnt = insns_cnt;
6862 load_attr.license = license;
6863 load_attr.attach_btf_id = prog->attach_btf_id;
6864 if (prog->attach_prog_fd)
6865 load_attr.attach_prog_fd = prog->attach_prog_fd;
6867 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
6868 load_attr.attach_btf_id = prog->attach_btf_id;
6869 load_attr.kern_version = kern_version;
6870 load_attr.prog_ifindex = prog->prog_ifindex;
6872 /* specify func_info/line_info only if kernel supports them */
6873 btf_fd = bpf_object__btf_fd(prog->obj);
6874 if (btf_fd >= 0 && kernel_supports(FEAT_BTF_FUNC)) {
6875 load_attr.prog_btf_fd = btf_fd;
6876 load_attr.func_info = prog->func_info;
6877 load_attr.func_info_rec_size = prog->func_info_rec_size;
6878 load_attr.func_info_cnt = prog->func_info_cnt;
6879 load_attr.line_info = prog->line_info;
6880 load_attr.line_info_rec_size = prog->line_info_rec_size;
6881 load_attr.line_info_cnt = prog->line_info_cnt;
6883 load_attr.log_level = prog->log_level;
6884 load_attr.prog_flags = prog->prog_flags;
6888 log_buf = malloc(log_buf_size);
6895 load_attr.log_buf = log_buf;
6896 load_attr.log_buf_sz = log_buf_size;
6897 ret = libbpf__bpf_prog_load(&load_attr);
6900 if (log_buf && load_attr.log_level)
6901 pr_debug("verifier log:\n%s", log_buf);
6903 if (prog->obj->rodata_map_idx >= 0 &&
6904 kernel_supports(FEAT_PROG_BIND_MAP)) {
6905 struct bpf_map *rodata_map =
6906 &prog->obj->maps[prog->obj->rodata_map_idx];
6908 if (bpf_prog_bind_map(ret, bpf_map__fd(rodata_map), NULL)) {
6909 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
6910 pr_warn("prog '%s': failed to bind .rodata map: %s\n",
6912 /* Don't fail hard if can't bind rodata. */
6921 if (!log_buf || errno == ENOSPC) {
6922 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE,
6928 ret = errno ? -errno : -LIBBPF_ERRNO__LOAD;
6929 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
6930 pr_warn("load bpf program failed: %s\n", cp);
6933 if (log_buf && log_buf[0] != '\0') {
6934 ret = -LIBBPF_ERRNO__VERIFY;
6935 pr_warn("-- BEGIN DUMP LOG ---\n");
6936 pr_warn("\n%s\n", log_buf);
6937 pr_warn("-- END LOG --\n");
6938 } else if (load_attr.insn_cnt >= BPF_MAXINSNS) {
6939 pr_warn("Program too large (%zu insns), at most %d insns\n",
6940 load_attr.insn_cnt, BPF_MAXINSNS);
6941 ret = -LIBBPF_ERRNO__PROG2BIG;
6942 } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
6943 /* Wrong program type? */
6946 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
6947 load_attr.expected_attach_type = 0;
6948 load_attr.log_buf = NULL;
6949 load_attr.log_buf_sz = 0;
6950 fd = libbpf__bpf_prog_load(&load_attr);
6953 ret = -LIBBPF_ERRNO__PROGTYPE;
6963 static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, int *btf_type_id);
6965 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
6969 if (prog->obj->loaded) {
6970 pr_warn("prog '%s': can't load after object was loaded\n", prog->name);
6974 if ((prog->type == BPF_PROG_TYPE_TRACING ||
6975 prog->type == BPF_PROG_TYPE_LSM ||
6976 prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
6977 int btf_obj_fd = 0, btf_type_id = 0;
6979 err = libbpf_find_attach_btf_id(prog, &btf_obj_fd, &btf_type_id);
6983 prog->attach_btf_obj_fd = btf_obj_fd;
6984 prog->attach_btf_id = btf_type_id;
6987 if (prog->instances.nr < 0 || !prog->instances.fds) {
6988 if (prog->preprocessor) {
6989 pr_warn("Internal error: can't load program '%s'\n",
6991 return -LIBBPF_ERRNO__INTERNAL;
6994 prog->instances.fds = malloc(sizeof(int));
6995 if (!prog->instances.fds) {
6996 pr_warn("Not enough memory for BPF fds\n");
6999 prog->instances.nr = 1;
7000 prog->instances.fds[0] = -1;
7003 if (!prog->preprocessor) {
7004 if (prog->instances.nr != 1) {
7005 pr_warn("prog '%s': inconsistent nr(%d) != 1\n",
7006 prog->name, prog->instances.nr);
7008 err = load_program(prog, prog->insns, prog->insns_cnt,
7009 license, kern_ver, &fd);
7011 prog->instances.fds[0] = fd;
7015 for (i = 0; i < prog->instances.nr; i++) {
7016 struct bpf_prog_prep_result result;
7017 bpf_program_prep_t preprocessor = prog->preprocessor;
7019 memset(&result, 0, sizeof(result));
7020 err = preprocessor(prog, i, prog->insns,
7021 prog->insns_cnt, &result);
7023 pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
7028 if (!result.new_insn_ptr || !result.new_insn_cnt) {
7029 pr_debug("Skip loading the %dth instance of program '%s'\n",
7031 prog->instances.fds[i] = -1;
7037 err = load_program(prog, result.new_insn_ptr,
7038 result.new_insn_cnt, license, kern_ver, &fd);
7040 pr_warn("Loading the %dth instance of program '%s' failed\n",
7047 prog->instances.fds[i] = fd;
7051 pr_warn("failed to load program '%s'\n", prog->name);
7052 zfree(&prog->insns);
7053 prog->insns_cnt = 0;
7058 bpf_object__load_progs(struct bpf_object *obj, int log_level)
7060 struct bpf_program *prog;
7064 for (i = 0; i < obj->nr_programs; i++) {
7065 prog = &obj->programs[i];
7066 err = bpf_object__sanitize_prog(obj, prog);
7071 for (i = 0; i < obj->nr_programs; i++) {
7072 prog = &obj->programs[i];
7073 if (prog_is_subprog(obj, prog))
7076 pr_debug("prog '%s': skipped loading\n", prog->name);
7079 prog->log_level |= log_level;
7080 err = bpf_program__load(prog, obj->license, obj->kern_version);
7087 static const struct bpf_sec_def *find_sec_def(const char *sec_name);
7089 static struct bpf_object *
7090 __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
7091 const struct bpf_object_open_opts *opts)
7093 const char *obj_name, *kconfig;
7094 struct bpf_program *prog;
7095 struct bpf_object *obj;
7099 if (elf_version(EV_CURRENT) == EV_NONE) {
7100 pr_warn("failed to init libelf for %s\n",
7101 path ? : "(mem buf)");
7102 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
7105 if (!OPTS_VALID(opts, bpf_object_open_opts))
7106 return ERR_PTR(-EINVAL);
7108 obj_name = OPTS_GET(opts, object_name, NULL);
7111 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
7112 (unsigned long)obj_buf,
7113 (unsigned long)obj_buf_sz);
7114 obj_name = tmp_name;
7117 pr_debug("loading object '%s' from buffer\n", obj_name);
7120 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
7124 kconfig = OPTS_GET(opts, kconfig, NULL);
7126 obj->kconfig = strdup(kconfig);
7128 return ERR_PTR(-ENOMEM);
7131 err = bpf_object__elf_init(obj);
7132 err = err ? : bpf_object__check_endianness(obj);
7133 err = err ? : bpf_object__elf_collect(obj);
7134 err = err ? : bpf_object__collect_externs(obj);
7135 err = err ? : bpf_object__finalize_btf(obj);
7136 err = err ? : bpf_object__init_maps(obj, opts);
7137 err = err ? : bpf_object__collect_relos(obj);
7140 bpf_object__elf_finish(obj);
7142 bpf_object__for_each_program(prog, obj) {
7143 prog->sec_def = find_sec_def(prog->sec_name);
7144 if (!prog->sec_def) {
7145 /* couldn't guess, but user might manually specify */
7146 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7147 prog->name, prog->sec_name);
7151 if (prog->sec_def->is_sleepable)
7152 prog->prog_flags |= BPF_F_SLEEPABLE;
7153 bpf_program__set_type(prog, prog->sec_def->prog_type);
7154 bpf_program__set_expected_attach_type(prog,
7155 prog->sec_def->expected_attach_type);
7157 if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING ||
7158 prog->sec_def->prog_type == BPF_PROG_TYPE_EXT)
7159 prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
7164 bpf_object__close(obj);
7165 return ERR_PTR(err);
7168 static struct bpf_object *
7169 __bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags)
7171 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
7172 .relaxed_maps = flags & MAPS_RELAX_COMPAT,
7175 /* param validation */
7179 pr_debug("loading %s\n", attr->file);
7180 return __bpf_object__open(attr->file, NULL, 0, &opts);
7183 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
7185 return __bpf_object__open_xattr(attr, 0);
7188 struct bpf_object *bpf_object__open(const char *path)
7190 struct bpf_object_open_attr attr = {
7192 .prog_type = BPF_PROG_TYPE_UNSPEC,
7195 return bpf_object__open_xattr(&attr);
7199 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
7202 return ERR_PTR(-EINVAL);
7204 pr_debug("loading %s\n", path);
7206 return __bpf_object__open(path, NULL, 0, opts);
7210 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
7211 const struct bpf_object_open_opts *opts)
7213 if (!obj_buf || obj_buf_sz == 0)
7214 return ERR_PTR(-EINVAL);
7216 return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts);
7220 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
7223 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
7224 .object_name = name,
7225 /* wrong default, but backwards-compatible */
7226 .relaxed_maps = true,
7229 /* returning NULL is wrong, but backwards-compatible */
7230 if (!obj_buf || obj_buf_sz == 0)
7233 return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
7236 int bpf_object__unload(struct bpf_object *obj)
7243 for (i = 0; i < obj->nr_maps; i++) {
7244 zclose(obj->maps[i].fd);
7245 if (obj->maps[i].st_ops)
7246 zfree(&obj->maps[i].st_ops->kern_vdata);
7249 for (i = 0; i < obj->nr_programs; i++)
7250 bpf_program__unload(&obj->programs[i]);
7255 static int bpf_object__sanitize_maps(struct bpf_object *obj)
7259 bpf_object__for_each_map(m, obj) {
7260 if (!bpf_map__is_internal(m))
7262 if (!kernel_supports(FEAT_GLOBAL_DATA)) {
7263 pr_warn("kernel doesn't support global data\n");
7266 if (!kernel_supports(FEAT_ARRAY_MMAP))
7267 m->def.map_flags ^= BPF_F_MMAPABLE;
7273 static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
7275 char sym_type, sym_name[500];
7276 unsigned long long sym_addr;
7277 struct extern_desc *ext;
7281 f = fopen("/proc/kallsyms", "r");
7284 pr_warn("failed to open /proc/kallsyms: %d\n", err);
7289 ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
7290 &sym_addr, &sym_type, sym_name);
7291 if (ret == EOF && feof(f))
7294 pr_warn("failed to read kallsyms entry: %d\n", ret);
7299 ext = find_extern_by_name(obj, sym_name);
7300 if (!ext || ext->type != EXT_KSYM)
7303 if (ext->is_set && ext->ksym.addr != sym_addr) {
7304 pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n",
7305 sym_name, ext->ksym.addr, sym_addr);
7311 ext->ksym.addr = sym_addr;
7312 pr_debug("extern (ksym) %s=0x%llx\n", sym_name, sym_addr);
7321 static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
7323 struct extern_desc *ext;
7325 int i, j, id, btf_fd, err;
7327 for (i = 0; i < obj->nr_extern; i++) {
7328 const struct btf_type *targ_var, *targ_type;
7329 __u32 targ_type_id, local_type_id;
7330 const char *targ_var_name;
7333 ext = &obj->externs[i];
7334 if (ext->type != EXT_KSYM || !ext->ksym.type_id)
7337 btf = obj->btf_vmlinux;
7339 id = btf__find_by_name_kind(btf, ext->name, BTF_KIND_VAR);
7340 if (id == -ENOENT) {
7341 err = load_module_btfs(obj);
7345 for (j = 0; j < obj->btf_module_cnt; j++) {
7346 btf = obj->btf_modules[j].btf;
7347 /* we assume module BTF FD is always >0 */
7348 btf_fd = obj->btf_modules[j].fd;
7349 id = btf__find_by_name_kind(btf, ext->name, BTF_KIND_VAR);
7355 pr_warn("extern (ksym) '%s': failed to find BTF ID in kernel BTF(s).\n",
7360 /* find local type_id */
7361 local_type_id = ext->ksym.type_id;
7363 /* find target type_id */
7364 targ_var = btf__type_by_id(btf, id);
7365 targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
7366 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
7368 ret = bpf_core_types_are_compat(obj->btf, local_type_id,
7371 const struct btf_type *local_type;
7372 const char *targ_name, *local_name;
7374 local_type = btf__type_by_id(obj->btf, local_type_id);
7375 local_name = btf__name_by_offset(obj->btf, local_type->name_off);
7376 targ_name = btf__name_by_offset(btf, targ_type->name_off);
7378 pr_warn("extern (ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
7379 ext->name, local_type_id,
7380 btf_kind_str(local_type), local_name, targ_type_id,
7381 btf_kind_str(targ_type), targ_name);
7386 ext->ksym.kernel_btf_obj_fd = btf_fd;
7387 ext->ksym.kernel_btf_id = id;
7388 pr_debug("extern (ksym) '%s': resolved to [%d] %s %s\n",
7389 ext->name, id, btf_kind_str(targ_var), targ_var_name);
7394 static int bpf_object__resolve_externs(struct bpf_object *obj,
7395 const char *extra_kconfig)
7397 bool need_config = false, need_kallsyms = false;
7398 bool need_vmlinux_btf = false;
7399 struct extern_desc *ext;
7400 void *kcfg_data = NULL;
7403 if (obj->nr_extern == 0)
7406 if (obj->kconfig_map_idx >= 0)
7407 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
7409 for (i = 0; i < obj->nr_extern; i++) {
7410 ext = &obj->externs[i];
7412 if (ext->type == EXT_KCFG &&
7413 strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
7414 void *ext_val = kcfg_data + ext->kcfg.data_off;
7415 __u32 kver = get_kernel_version();
7418 pr_warn("failed to get kernel version\n");
7421 err = set_kcfg_value_num(ext, ext_val, kver);
7424 pr_debug("extern (kcfg) %s=0x%x\n", ext->name, kver);
7425 } else if (ext->type == EXT_KCFG &&
7426 strncmp(ext->name, "CONFIG_", 7) == 0) {
7428 } else if (ext->type == EXT_KSYM) {
7429 if (ext->ksym.type_id)
7430 need_vmlinux_btf = true;
7432 need_kallsyms = true;
7434 pr_warn("unrecognized extern '%s'\n", ext->name);
7438 if (need_config && extra_kconfig) {
7439 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
7442 need_config = false;
7443 for (i = 0; i < obj->nr_extern; i++) {
7444 ext = &obj->externs[i];
7445 if (ext->type == EXT_KCFG && !ext->is_set) {
7452 err = bpf_object__read_kconfig_file(obj, kcfg_data);
7456 if (need_kallsyms) {
7457 err = bpf_object__read_kallsyms_file(obj);
7461 if (need_vmlinux_btf) {
7462 err = bpf_object__resolve_ksyms_btf_id(obj);
7466 for (i = 0; i < obj->nr_extern; i++) {
7467 ext = &obj->externs[i];
7469 if (!ext->is_set && !ext->is_weak) {
7470 pr_warn("extern %s (strong) not resolved\n", ext->name);
7472 } else if (!ext->is_set) {
7473 pr_debug("extern %s (weak) not resolved, defaulting to zero\n",
7481 int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
7483 struct bpf_object *obj;
7493 pr_warn("object '%s': load can't be attempted twice\n", obj->name);
7497 err = bpf_object__probe_loading(obj);
7498 err = err ? : bpf_object__load_vmlinux_btf(obj, false);
7499 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
7500 err = err ? : bpf_object__sanitize_and_load_btf(obj);
7501 err = err ? : bpf_object__sanitize_maps(obj);
7502 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
7503 err = err ? : bpf_object__create_maps(obj);
7504 err = err ? : bpf_object__relocate(obj, attr->target_btf_path);
7505 err = err ? : bpf_object__load_progs(obj, attr->log_level);
7507 /* clean up module BTFs */
7508 for (i = 0; i < obj->btf_module_cnt; i++) {
7509 close(obj->btf_modules[i].fd);
7510 btf__free(obj->btf_modules[i].btf);
7511 free(obj->btf_modules[i].name);
7513 free(obj->btf_modules);
7515 /* clean up vmlinux BTF */
7516 btf__free(obj->btf_vmlinux);
7517 obj->btf_vmlinux = NULL;
7519 obj->loaded = true; /* doesn't matter if successfully or not */
7526 /* unpin any maps that were auto-pinned during load */
7527 for (i = 0; i < obj->nr_maps; i++)
7528 if (obj->maps[i].pinned && !obj->maps[i].reused)
7529 bpf_map__unpin(&obj->maps[i], NULL);
7531 bpf_object__unload(obj);
7532 pr_warn("failed to load object '%s'\n", obj->path);
7536 int bpf_object__load(struct bpf_object *obj)
7538 struct bpf_object_load_attr attr = {
7542 return bpf_object__load_xattr(&attr);
7545 static int make_parent_dir(const char *path)
7547 char *cp, errmsg[STRERR_BUFSIZE];
7551 dname = strdup(path);
7555 dir = dirname(dname);
7556 if (mkdir(dir, 0700) && errno != EEXIST)
7561 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
7562 pr_warn("failed to mkdir %s: %s\n", path, cp);
7567 static int check_path(const char *path)
7569 char *cp, errmsg[STRERR_BUFSIZE];
7570 struct statfs st_fs;
7577 dname = strdup(path);
7581 dir = dirname(dname);
7582 if (statfs(dir, &st_fs)) {
7583 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
7584 pr_warn("failed to statfs %s: %s\n", dir, cp);
7589 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
7590 pr_warn("specified path %s is not on BPF FS\n", path);
7597 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
7600 char *cp, errmsg[STRERR_BUFSIZE];
7603 err = make_parent_dir(path);
7607 err = check_path(path);
7612 pr_warn("invalid program pointer\n");
7616 if (instance < 0 || instance >= prog->instances.nr) {
7617 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
7618 instance, prog->name, prog->instances.nr);
7622 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
7624 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
7625 pr_warn("failed to pin program: %s\n", cp);
7628 pr_debug("pinned program '%s'\n", path);
7633 int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
7638 err = check_path(path);
7643 pr_warn("invalid program pointer\n");
7647 if (instance < 0 || instance >= prog->instances.nr) {
7648 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
7649 instance, prog->name, prog->instances.nr);
7656 pr_debug("unpinned program '%s'\n", path);
7661 int bpf_program__pin(struct bpf_program *prog, const char *path)
7665 err = make_parent_dir(path);
7669 err = check_path(path);
7674 pr_warn("invalid program pointer\n");
7678 if (prog->instances.nr <= 0) {
7679 pr_warn("no instances of prog %s to pin\n", prog->name);
7683 if (prog->instances.nr == 1) {
7684 /* don't create subdirs when pinning single instance */
7685 return bpf_program__pin_instance(prog, path, 0);
7688 for (i = 0; i < prog->instances.nr; i++) {
7692 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7696 } else if (len >= PATH_MAX) {
7697 err = -ENAMETOOLONG;
7701 err = bpf_program__pin_instance(prog, buf, i);
7709 for (i = i - 1; i >= 0; i--) {
7713 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7716 else if (len >= PATH_MAX)
7719 bpf_program__unpin_instance(prog, buf, i);
7727 int bpf_program__unpin(struct bpf_program *prog, const char *path)
7731 err = check_path(path);
7736 pr_warn("invalid program pointer\n");
7740 if (prog->instances.nr <= 0) {
7741 pr_warn("no instances of prog %s to pin\n", prog->name);
7745 if (prog->instances.nr == 1) {
7746 /* don't create subdirs when pinning single instance */
7747 return bpf_program__unpin_instance(prog, path, 0);
7750 for (i = 0; i < prog->instances.nr; i++) {
7754 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7757 else if (len >= PATH_MAX)
7758 return -ENAMETOOLONG;
7760 err = bpf_program__unpin_instance(prog, buf, i);
7772 int bpf_map__pin(struct bpf_map *map, const char *path)
7774 char *cp, errmsg[STRERR_BUFSIZE];
7778 pr_warn("invalid map pointer\n");
7782 if (map->pin_path) {
7783 if (path && strcmp(path, map->pin_path)) {
7784 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7785 bpf_map__name(map), map->pin_path, path);
7787 } else if (map->pinned) {
7788 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
7789 bpf_map__name(map), map->pin_path);
7794 pr_warn("missing a path to pin map '%s' at\n",
7795 bpf_map__name(map));
7797 } else if (map->pinned) {
7798 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
7802 map->pin_path = strdup(path);
7803 if (!map->pin_path) {
7809 err = make_parent_dir(map->pin_path);
7813 err = check_path(map->pin_path);
7817 if (bpf_obj_pin(map->fd, map->pin_path)) {
7823 pr_debug("pinned map '%s'\n", map->pin_path);
7828 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
7829 pr_warn("failed to pin map: %s\n", cp);
7833 int bpf_map__unpin(struct bpf_map *map, const char *path)
7838 pr_warn("invalid map pointer\n");
7842 if (map->pin_path) {
7843 if (path && strcmp(path, map->pin_path)) {
7844 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7845 bpf_map__name(map), map->pin_path, path);
7848 path = map->pin_path;
7850 pr_warn("no path to unpin map '%s' from\n",
7851 bpf_map__name(map));
7855 err = check_path(path);
7863 map->pinned = false;
7864 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
7869 int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
7879 free(map->pin_path);
7880 map->pin_path = new;
7884 const char *bpf_map__get_pin_path(const struct bpf_map *map)
7886 return map->pin_path;
7889 bool bpf_map__is_pinned(const struct bpf_map *map)
7894 static void sanitize_pin_path(char *s)
7896 /* bpffs disallows periods in path names */
7904 int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
7906 struct bpf_map *map;
7913 pr_warn("object not yet loaded; load it first\n");
7917 bpf_object__for_each_map(map, obj) {
7918 char *pin_path = NULL;
7924 len = snprintf(buf, PATH_MAX, "%s/%s", path,
7925 bpf_map__name(map));
7928 goto err_unpin_maps;
7929 } else if (len >= PATH_MAX) {
7930 err = -ENAMETOOLONG;
7931 goto err_unpin_maps;
7933 sanitize_pin_path(buf);
7935 } else if (!map->pin_path) {
7939 err = bpf_map__pin(map, pin_path);
7941 goto err_unpin_maps;
7947 while ((map = bpf_map__prev(map, obj))) {
7951 bpf_map__unpin(map, NULL);
7957 int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
7959 struct bpf_map *map;
7965 bpf_object__for_each_map(map, obj) {
7966 char *pin_path = NULL;
7972 len = snprintf(buf, PATH_MAX, "%s/%s", path,
7973 bpf_map__name(map));
7976 else if (len >= PATH_MAX)
7977 return -ENAMETOOLONG;
7978 sanitize_pin_path(buf);
7980 } else if (!map->pin_path) {
7984 err = bpf_map__unpin(map, pin_path);
7992 int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
7994 struct bpf_program *prog;
8001 pr_warn("object not yet loaded; load it first\n");
8005 bpf_object__for_each_program(prog, obj) {
8009 len = snprintf(buf, PATH_MAX, "%s/%s", path,
8013 goto err_unpin_programs;
8014 } else if (len >= PATH_MAX) {
8015 err = -ENAMETOOLONG;
8016 goto err_unpin_programs;
8019 err = bpf_program__pin(prog, buf);
8021 goto err_unpin_programs;
8027 while ((prog = bpf_program__prev(prog, obj))) {
8031 len = snprintf(buf, PATH_MAX, "%s/%s", path,
8035 else if (len >= PATH_MAX)
8038 bpf_program__unpin(prog, buf);
8044 int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
8046 struct bpf_program *prog;
8052 bpf_object__for_each_program(prog, obj) {
8056 len = snprintf(buf, PATH_MAX, "%s/%s", path,
8060 else if (len >= PATH_MAX)
8061 return -ENAMETOOLONG;
8063 err = bpf_program__unpin(prog, buf);
8071 int bpf_object__pin(struct bpf_object *obj, const char *path)
8075 err = bpf_object__pin_maps(obj, path);
8079 err = bpf_object__pin_programs(obj, path);
8081 bpf_object__unpin_maps(obj, path);
8088 static void bpf_map__destroy(struct bpf_map *map)
8090 if (map->clear_priv)
8091 map->clear_priv(map, map->priv);
8093 map->clear_priv = NULL;
8095 if (map->inner_map) {
8096 bpf_map__destroy(map->inner_map);
8097 zfree(&map->inner_map);
8100 zfree(&map->init_slots);
8101 map->init_slots_sz = 0;
8104 munmap(map->mmaped, bpf_map_mmap_sz(map));
8109 zfree(&map->st_ops->data);
8110 zfree(&map->st_ops->progs);
8111 zfree(&map->st_ops->kern_func_off);
8112 zfree(&map->st_ops);
8116 zfree(&map->pin_path);
8122 void bpf_object__close(struct bpf_object *obj)
8126 if (IS_ERR_OR_NULL(obj))
8129 if (obj->clear_priv)
8130 obj->clear_priv(obj, obj->priv);
8132 bpf_object__elf_finish(obj);
8133 bpf_object__unload(obj);
8134 btf__free(obj->btf);
8135 btf_ext__free(obj->btf_ext);
8137 for (i = 0; i < obj->nr_maps; i++)
8138 bpf_map__destroy(&obj->maps[i]);
8140 zfree(&obj->kconfig);
8141 zfree(&obj->externs);
8147 if (obj->programs && obj->nr_programs) {
8148 for (i = 0; i < obj->nr_programs; i++)
8149 bpf_program__exit(&obj->programs[i]);
8151 zfree(&obj->programs);
8153 list_del(&obj->list);
8158 bpf_object__next(struct bpf_object *prev)
8160 struct bpf_object *next;
8163 next = list_first_entry(&bpf_objects_list,
8167 next = list_next_entry(prev, list);
8169 /* Empty list is noticed here so don't need checking on entry. */
8170 if (&next->list == &bpf_objects_list)
8176 const char *bpf_object__name(const struct bpf_object *obj)
8178 return obj ? obj->name : ERR_PTR(-EINVAL);
8181 unsigned int bpf_object__kversion(const struct bpf_object *obj)
8183 return obj ? obj->kern_version : 0;
8186 struct btf *bpf_object__btf(const struct bpf_object *obj)
8188 return obj ? obj->btf : NULL;
8191 int bpf_object__btf_fd(const struct bpf_object *obj)
8193 return obj->btf ? btf__fd(obj->btf) : -1;
8196 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
8197 bpf_object_clear_priv_t clear_priv)
8199 if (obj->priv && obj->clear_priv)
8200 obj->clear_priv(obj, obj->priv);
8203 obj->clear_priv = clear_priv;
8207 void *bpf_object__priv(const struct bpf_object *obj)
8209 return obj ? obj->priv : ERR_PTR(-EINVAL);
8212 static struct bpf_program *
8213 __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
8216 size_t nr_programs = obj->nr_programs;
8223 /* Iter from the beginning */
8224 return forward ? &obj->programs[0] :
8225 &obj->programs[nr_programs - 1];
8227 if (p->obj != obj) {
8228 pr_warn("error: program handler doesn't match object\n");
8232 idx = (p - obj->programs) + (forward ? 1 : -1);
8233 if (idx >= obj->nr_programs || idx < 0)
8235 return &obj->programs[idx];
8238 struct bpf_program *
8239 bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
8241 struct bpf_program *prog = prev;
8244 prog = __bpf_program__iter(prog, obj, true);
8245 } while (prog && prog_is_subprog(obj, prog));
8250 struct bpf_program *
8251 bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
8253 struct bpf_program *prog = next;
8256 prog = __bpf_program__iter(prog, obj, false);
8257 } while (prog && prog_is_subprog(obj, prog));
8262 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
8263 bpf_program_clear_priv_t clear_priv)
8265 if (prog->priv && prog->clear_priv)
8266 prog->clear_priv(prog, prog->priv);
8269 prog->clear_priv = clear_priv;
8273 void *bpf_program__priv(const struct bpf_program *prog)
8275 return prog ? prog->priv : ERR_PTR(-EINVAL);
8278 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
8280 prog->prog_ifindex = ifindex;
8283 const char *bpf_program__name(const struct bpf_program *prog)
8288 const char *bpf_program__section_name(const struct bpf_program *prog)
8290 return prog->sec_name;
8293 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
8297 title = prog->sec_name;
8299 title = strdup(title);
8301 pr_warn("failed to strdup program title\n");
8302 return ERR_PTR(-ENOMEM);
8309 bool bpf_program__autoload(const struct bpf_program *prog)
8314 int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
8316 if (prog->obj->loaded)
8319 prog->load = autoload;
8323 int bpf_program__fd(const struct bpf_program *prog)
8325 return bpf_program__nth_fd(prog, 0);
8328 size_t bpf_program__size(const struct bpf_program *prog)
8330 return prog->insns_cnt * BPF_INSN_SZ;
8333 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
8334 bpf_program_prep_t prep)
8338 if (nr_instances <= 0 || !prep)
8341 if (prog->instances.nr > 0 || prog->instances.fds) {
8342 pr_warn("Can't set pre-processor after loading\n");
8346 instances_fds = malloc(sizeof(int) * nr_instances);
8347 if (!instances_fds) {
8348 pr_warn("alloc memory failed for fds\n");
8352 /* fill all fd with -1 */
8353 memset(instances_fds, -1, sizeof(int) * nr_instances);
8355 prog->instances.nr = nr_instances;
8356 prog->instances.fds = instances_fds;
8357 prog->preprocessor = prep;
8361 int bpf_program__nth_fd(const struct bpf_program *prog, int n)
8368 if (n >= prog->instances.nr || n < 0) {
8369 pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
8370 n, prog->name, prog->instances.nr);
8374 fd = prog->instances.fds[n];
8376 pr_warn("%dth instance of program '%s' is invalid\n",
8384 enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog)
8389 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
8394 static bool bpf_program__is_type(const struct bpf_program *prog,
8395 enum bpf_prog_type type)
8397 return prog ? (prog->type == type) : false;
8400 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
8401 int bpf_program__set_##NAME(struct bpf_program *prog) \
8405 bpf_program__set_type(prog, TYPE); \
8409 bool bpf_program__is_##NAME(const struct bpf_program *prog) \
8411 return bpf_program__is_type(prog, TYPE); \
8414 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
8415 BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
8416 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
8417 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
8418 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
8419 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
8420 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
8421 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
8422 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
8423 BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING);
8424 BPF_PROG_TYPE_FNS(struct_ops, BPF_PROG_TYPE_STRUCT_OPS);
8425 BPF_PROG_TYPE_FNS(extension, BPF_PROG_TYPE_EXT);
8426 BPF_PROG_TYPE_FNS(sk_lookup, BPF_PROG_TYPE_SK_LOOKUP);
8428 enum bpf_attach_type
8429 bpf_program__get_expected_attach_type(struct bpf_program *prog)
8431 return prog->expected_attach_type;
8434 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
8435 enum bpf_attach_type type)
8437 prog->expected_attach_type = type;
8440 #define BPF_PROG_SEC_IMPL(string, ptype, eatype, eatype_optional, \
8441 attachable, attach_btf) \
8444 .len = sizeof(string) - 1, \
8445 .prog_type = ptype, \
8446 .expected_attach_type = eatype, \
8447 .is_exp_attach_type_optional = eatype_optional, \
8448 .is_attachable = attachable, \
8449 .is_attach_btf = attach_btf, \
8452 /* Programs that can NOT be attached. */
8453 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0)
8455 /* Programs that can be attached. */
8456 #define BPF_APROG_SEC(string, ptype, atype) \
8457 BPF_PROG_SEC_IMPL(string, ptype, atype, true, 1, 0)
8459 /* Programs that must specify expected attach type at load time. */
8460 #define BPF_EAPROG_SEC(string, ptype, eatype) \
8461 BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 1, 0)
8463 /* Programs that use BTF to identify attach point */
8464 #define BPF_PROG_BTF(string, ptype, eatype) \
8465 BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 0, 1)
8467 /* Programs that can be attached but attach type can't be identified by section
8468 * name. Kept for backward compatibility.
8470 #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
8472 #define SEC_DEF(sec_pfx, ptype, ...) { \
8474 .len = sizeof(sec_pfx) - 1, \
8475 .prog_type = BPF_PROG_TYPE_##ptype, \
8479 static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
8480 struct bpf_program *prog);
8481 static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
8482 struct bpf_program *prog);
8483 static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
8484 struct bpf_program *prog);
8485 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
8486 struct bpf_program *prog);
8487 static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
8488 struct bpf_program *prog);
8489 static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
8490 struct bpf_program *prog);
8492 static const struct bpf_sec_def section_defs[] = {
8493 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
8494 BPF_PROG_SEC("sk_reuseport", BPF_PROG_TYPE_SK_REUSEPORT),
8495 SEC_DEF("kprobe/", KPROBE,
8496 .attach_fn = attach_kprobe),
8497 BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE),
8498 SEC_DEF("kretprobe/", KPROBE,
8499 .attach_fn = attach_kprobe),
8500 BPF_PROG_SEC("uretprobe/", BPF_PROG_TYPE_KPROBE),
8501 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
8502 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
8503 SEC_DEF("tracepoint/", TRACEPOINT,
8504 .attach_fn = attach_tp),
8505 SEC_DEF("tp/", TRACEPOINT,
8506 .attach_fn = attach_tp),
8507 SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT,
8508 .attach_fn = attach_raw_tp),
8509 SEC_DEF("raw_tp/", RAW_TRACEPOINT,
8510 .attach_fn = attach_raw_tp),
8511 SEC_DEF("tp_btf/", TRACING,
8512 .expected_attach_type = BPF_TRACE_RAW_TP,
8513 .is_attach_btf = true,
8514 .attach_fn = attach_trace),
8515 SEC_DEF("fentry/", TRACING,
8516 .expected_attach_type = BPF_TRACE_FENTRY,
8517 .is_attach_btf = true,
8518 .attach_fn = attach_trace),
8519 SEC_DEF("fmod_ret/", TRACING,
8520 .expected_attach_type = BPF_MODIFY_RETURN,
8521 .is_attach_btf = true,
8522 .attach_fn = attach_trace),
8523 SEC_DEF("fexit/", TRACING,
8524 .expected_attach_type = BPF_TRACE_FEXIT,
8525 .is_attach_btf = true,
8526 .attach_fn = attach_trace),
8527 SEC_DEF("fentry.s/", TRACING,
8528 .expected_attach_type = BPF_TRACE_FENTRY,
8529 .is_attach_btf = true,
8530 .is_sleepable = true,
8531 .attach_fn = attach_trace),
8532 SEC_DEF("fmod_ret.s/", TRACING,
8533 .expected_attach_type = BPF_MODIFY_RETURN,
8534 .is_attach_btf = true,
8535 .is_sleepable = true,
8536 .attach_fn = attach_trace),
8537 SEC_DEF("fexit.s/", TRACING,
8538 .expected_attach_type = BPF_TRACE_FEXIT,
8539 .is_attach_btf = true,
8540 .is_sleepable = true,
8541 .attach_fn = attach_trace),
8542 SEC_DEF("freplace/", EXT,
8543 .is_attach_btf = true,
8544 .attach_fn = attach_trace),
8545 SEC_DEF("lsm/", LSM,
8546 .is_attach_btf = true,
8547 .expected_attach_type = BPF_LSM_MAC,
8548 .attach_fn = attach_lsm),
8549 SEC_DEF("lsm.s/", LSM,
8550 .is_attach_btf = true,
8551 .is_sleepable = true,
8552 .expected_attach_type = BPF_LSM_MAC,
8553 .attach_fn = attach_lsm),
8554 SEC_DEF("iter/", TRACING,
8555 .expected_attach_type = BPF_TRACE_ITER,
8556 .is_attach_btf = true,
8557 .attach_fn = attach_iter),
8558 BPF_EAPROG_SEC("xdp_devmap/", BPF_PROG_TYPE_XDP,
8560 BPF_EAPROG_SEC("xdp_cpumap/", BPF_PROG_TYPE_XDP,
8562 BPF_APROG_SEC("xdp", BPF_PROG_TYPE_XDP,
8564 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
8565 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
8566 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
8567 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
8568 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
8569 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
8570 BPF_CGROUP_INET_INGRESS),
8571 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
8572 BPF_CGROUP_INET_EGRESS),
8573 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
8574 BPF_EAPROG_SEC("cgroup/sock_create", BPF_PROG_TYPE_CGROUP_SOCK,
8575 BPF_CGROUP_INET_SOCK_CREATE),
8576 BPF_EAPROG_SEC("cgroup/sock_release", BPF_PROG_TYPE_CGROUP_SOCK,
8577 BPF_CGROUP_INET_SOCK_RELEASE),
8578 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
8579 BPF_CGROUP_INET_SOCK_CREATE),
8580 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
8581 BPF_CGROUP_INET4_POST_BIND),
8582 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
8583 BPF_CGROUP_INET6_POST_BIND),
8584 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
8586 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
8587 BPF_CGROUP_SOCK_OPS),
8588 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
8589 BPF_SK_SKB_STREAM_PARSER),
8590 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
8591 BPF_SK_SKB_STREAM_VERDICT),
8592 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
8593 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
8594 BPF_SK_MSG_VERDICT),
8595 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
8597 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
8598 BPF_FLOW_DISSECTOR),
8599 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8600 BPF_CGROUP_INET4_BIND),
8601 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8602 BPF_CGROUP_INET6_BIND),
8603 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8604 BPF_CGROUP_INET4_CONNECT),
8605 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8606 BPF_CGROUP_INET6_CONNECT),
8607 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8608 BPF_CGROUP_UDP4_SENDMSG),
8609 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8610 BPF_CGROUP_UDP6_SENDMSG),
8611 BPF_EAPROG_SEC("cgroup/recvmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8612 BPF_CGROUP_UDP4_RECVMSG),
8613 BPF_EAPROG_SEC("cgroup/recvmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8614 BPF_CGROUP_UDP6_RECVMSG),
8615 BPF_EAPROG_SEC("cgroup/getpeername4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8616 BPF_CGROUP_INET4_GETPEERNAME),
8617 BPF_EAPROG_SEC("cgroup/getpeername6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8618 BPF_CGROUP_INET6_GETPEERNAME),
8619 BPF_EAPROG_SEC("cgroup/getsockname4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8620 BPF_CGROUP_INET4_GETSOCKNAME),
8621 BPF_EAPROG_SEC("cgroup/getsockname6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
8622 BPF_CGROUP_INET6_GETSOCKNAME),
8623 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
8625 BPF_EAPROG_SEC("cgroup/getsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
8626 BPF_CGROUP_GETSOCKOPT),
8627 BPF_EAPROG_SEC("cgroup/setsockopt", BPF_PROG_TYPE_CGROUP_SOCKOPT,
8628 BPF_CGROUP_SETSOCKOPT),
8629 BPF_PROG_SEC("struct_ops", BPF_PROG_TYPE_STRUCT_OPS),
8630 BPF_EAPROG_SEC("sk_lookup/", BPF_PROG_TYPE_SK_LOOKUP,
8634 #undef BPF_PROG_SEC_IMPL
8636 #undef BPF_APROG_SEC
8637 #undef BPF_EAPROG_SEC
8638 #undef BPF_APROG_COMPAT
8641 #define MAX_TYPE_NAME_SIZE 32
8643 static const struct bpf_sec_def *find_sec_def(const char *sec_name)
8645 int i, n = ARRAY_SIZE(section_defs);
8647 for (i = 0; i < n; i++) {
8648 if (strncmp(sec_name,
8649 section_defs[i].sec, section_defs[i].len))
8651 return §ion_defs[i];
8656 static char *libbpf_get_type_names(bool attach_type)
8658 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
8666 /* Forge string buf with all available names */
8667 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
8668 if (attach_type && !section_defs[i].is_attachable)
8671 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
8676 strcat(buf, section_defs[i].sec);
8682 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
8683 enum bpf_attach_type *expected_attach_type)
8685 const struct bpf_sec_def *sec_def;
8691 sec_def = find_sec_def(name);
8693 *prog_type = sec_def->prog_type;
8694 *expected_attach_type = sec_def->expected_attach_type;
8698 pr_debug("failed to guess program type from ELF section '%s'\n", name);
8699 type_names = libbpf_get_type_names(false);
8700 if (type_names != NULL) {
8701 pr_debug("supported section(type) names are:%s\n", type_names);
8708 static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
8711 struct bpf_map *map;
8714 for (i = 0; i < obj->nr_maps; i++) {
8715 map = &obj->maps[i];
8716 if (!bpf_map__is_struct_ops(map))
8718 if (map->sec_offset <= offset &&
8719 offset - map->sec_offset < map->def.value_size)
8726 /* Collect the reloc from ELF and populate the st_ops->progs[] */
8727 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
8728 GElf_Shdr *shdr, Elf_Data *data)
8730 const struct btf_member *member;
8731 struct bpf_struct_ops *st_ops;
8732 struct bpf_program *prog;
8733 unsigned int shdr_idx;
8734 const struct btf *btf;
8735 struct bpf_map *map;
8737 unsigned int moff, insn_idx;
8744 symbols = obj->efile.symbols;
8746 nrels = shdr->sh_size / shdr->sh_entsize;
8747 for (i = 0; i < nrels; i++) {
8748 if (!gelf_getrel(data, i, &rel)) {
8749 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
8750 return -LIBBPF_ERRNO__FORMAT;
8753 if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
8754 pr_warn("struct_ops reloc: symbol %zx not found\n",
8755 (size_t)GELF_R_SYM(rel.r_info));
8756 return -LIBBPF_ERRNO__FORMAT;
8759 name = elf_sym_str(obj, sym.st_name) ?: "<?>";
8760 map = find_struct_ops_map_by_offset(obj, rel.r_offset);
8762 pr_warn("struct_ops reloc: cannot find map at rel.r_offset %zu\n",
8763 (size_t)rel.r_offset);
8767 moff = rel.r_offset - map->sec_offset;
8768 shdr_idx = sym.st_shndx;
8769 st_ops = map->st_ops;
8770 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",
8772 (long long)(rel.r_info >> 32),
8773 (long long)sym.st_value,
8774 shdr_idx, (size_t)rel.r_offset,
8775 map->sec_offset, sym.st_name, name);
8777 if (shdr_idx >= SHN_LORESERVE) {
8778 pr_warn("struct_ops reloc %s: rel.r_offset %zu shdr_idx %u unsupported non-static function\n",
8779 map->name, (size_t)rel.r_offset, shdr_idx);
8780 return -LIBBPF_ERRNO__RELOC;
8782 if (sym.st_value % BPF_INSN_SZ) {
8783 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
8784 map->name, (unsigned long long)sym.st_value);
8785 return -LIBBPF_ERRNO__FORMAT;
8787 insn_idx = sym.st_value / BPF_INSN_SZ;
8789 member = find_member_by_offset(st_ops->type, moff * 8);
8791 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
8795 member_idx = member - btf_members(st_ops->type);
8796 name = btf__name_by_offset(btf, member->name_off);
8798 if (!resolve_func_ptr(btf, member->type, NULL)) {
8799 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
8804 prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
8806 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
8807 map->name, shdr_idx, name);
8811 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
8812 const struct bpf_sec_def *sec_def;
8814 sec_def = find_sec_def(prog->sec_name);
8816 sec_def->prog_type != BPF_PROG_TYPE_STRUCT_OPS) {
8818 prog->type = sec_def->prog_type;
8822 prog->type = BPF_PROG_TYPE_STRUCT_OPS;
8823 prog->attach_btf_id = st_ops->type_id;
8824 prog->expected_attach_type = member_idx;
8825 } else if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
8826 prog->attach_btf_id != st_ops->type_id ||
8827 prog->expected_attach_type != member_idx) {
8830 st_ops->progs[member_idx] = prog;
8836 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",
8837 map->name, prog->name, prog->sec_name, prog->type,
8838 prog->attach_btf_id, prog->expected_attach_type, name);
8842 #define BTF_TRACE_PREFIX "btf_trace_"
8843 #define BTF_LSM_PREFIX "bpf_lsm_"
8844 #define BTF_ITER_PREFIX "bpf_iter_"
8845 #define BTF_MAX_NAME_SIZE 128
8847 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
8848 const char *name, __u32 kind)
8850 char btf_type_name[BTF_MAX_NAME_SIZE];
8853 ret = snprintf(btf_type_name, sizeof(btf_type_name),
8854 "%s%s", prefix, name);
8855 /* snprintf returns the number of characters written excluding the
8856 * the terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
8857 * indicates truncation.
8859 if (ret < 0 || ret >= sizeof(btf_type_name))
8860 return -ENAMETOOLONG;
8861 return btf__find_by_name_kind(btf, btf_type_name, kind);
8864 static inline int find_attach_btf_id(struct btf *btf, const char *name,
8865 enum bpf_attach_type attach_type)
8869 if (attach_type == BPF_TRACE_RAW_TP)
8870 err = find_btf_by_prefix_kind(btf, BTF_TRACE_PREFIX, name,
8872 else if (attach_type == BPF_LSM_MAC)
8873 err = find_btf_by_prefix_kind(btf, BTF_LSM_PREFIX, name,
8875 else if (attach_type == BPF_TRACE_ITER)
8876 err = find_btf_by_prefix_kind(btf, BTF_ITER_PREFIX, name,
8879 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
8884 int libbpf_find_vmlinux_btf_id(const char *name,
8885 enum bpf_attach_type attach_type)
8890 btf = libbpf_find_kernel_btf();
8892 pr_warn("vmlinux BTF is not found\n");
8896 err = find_attach_btf_id(btf, name, attach_type);
8898 pr_warn("%s is not found in vmlinux BTF\n", name);
8904 static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
8906 struct bpf_prog_info_linear *info_linear;
8907 struct bpf_prog_info *info;
8908 struct btf *btf = NULL;
8911 info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0);
8912 if (IS_ERR_OR_NULL(info_linear)) {
8913 pr_warn("failed get_prog_info_linear for FD %d\n",
8917 info = &info_linear->info;
8918 if (!info->btf_id) {
8919 pr_warn("The target program doesn't have BTF\n");
8922 if (btf__get_from_id(info->btf_id, &btf)) {
8923 pr_warn("Failed to get BTF of the program\n");
8926 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
8929 pr_warn("%s is not found in prog's BTF\n", name);
8937 static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
8938 enum bpf_attach_type attach_type,
8939 int *btf_obj_fd, int *btf_type_id)
8943 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type);
8945 *btf_obj_fd = 0; /* vmlinux BTF */
8952 ret = load_module_btfs(obj);
8956 for (i = 0; i < obj->btf_module_cnt; i++) {
8957 const struct module_btf *mod = &obj->btf_modules[i];
8959 ret = find_attach_btf_id(mod->btf, attach_name, attach_type);
8961 *btf_obj_fd = mod->fd;
8974 static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, int *btf_type_id)
8976 enum bpf_attach_type attach_type = prog->expected_attach_type;
8977 __u32 attach_prog_fd = prog->attach_prog_fd;
8978 const char *name = prog->sec_name, *attach_name;
8979 const struct bpf_sec_def *sec = NULL;
8985 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
8986 if (!section_defs[i].is_attach_btf)
8988 if (strncmp(name, section_defs[i].sec, section_defs[i].len))
8991 sec = §ion_defs[i];
8996 pr_warn("failed to identify BTF ID based on ELF section name '%s'\n", name);
8999 attach_name = name + sec->len;
9001 /* BPF program's BTF ID */
9002 if (attach_prog_fd) {
9003 err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
9005 pr_warn("failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
9006 attach_prog_fd, attach_name, err);
9014 /* kernel/module BTF ID */
9015 err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id);
9017 pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name, err);
9023 int libbpf_attach_type_by_name(const char *name,
9024 enum bpf_attach_type *attach_type)
9032 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
9033 if (strncmp(name, section_defs[i].sec, section_defs[i].len))
9035 if (!section_defs[i].is_attachable)
9037 *attach_type = section_defs[i].expected_attach_type;
9040 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
9041 type_names = libbpf_get_type_names(true);
9042 if (type_names != NULL) {
9043 pr_debug("attachable section(type) names are:%s\n", type_names);
9050 int bpf_map__fd(const struct bpf_map *map)
9052 return map ? map->fd : -EINVAL;
9055 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
9057 return map ? &map->def : ERR_PTR(-EINVAL);
9060 const char *bpf_map__name(const struct bpf_map *map)
9062 return map ? map->name : NULL;
9065 enum bpf_map_type bpf_map__type(const struct bpf_map *map)
9067 return map->def.type;
9070 int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
9074 map->def.type = type;
9078 __u32 bpf_map__map_flags(const struct bpf_map *map)
9080 return map->def.map_flags;
9083 int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
9087 map->def.map_flags = flags;
9091 __u32 bpf_map__numa_node(const struct bpf_map *map)
9093 return map->numa_node;
9096 int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
9100 map->numa_node = numa_node;
9104 __u32 bpf_map__key_size(const struct bpf_map *map)
9106 return map->def.key_size;
9109 int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
9113 map->def.key_size = size;
9117 __u32 bpf_map__value_size(const struct bpf_map *map)
9119 return map->def.value_size;
9122 int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
9126 map->def.value_size = size;
9130 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
9132 return map ? map->btf_key_type_id : 0;
9135 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
9137 return map ? map->btf_value_type_id : 0;
9140 int bpf_map__set_priv(struct bpf_map *map, void *priv,
9141 bpf_map_clear_priv_t clear_priv)
9147 if (map->clear_priv)
9148 map->clear_priv(map, map->priv);
9152 map->clear_priv = clear_priv;
9156 void *bpf_map__priv(const struct bpf_map *map)
9158 return map ? map->priv : ERR_PTR(-EINVAL);
9161 int bpf_map__set_initial_value(struct bpf_map *map,
9162 const void *data, size_t size)
9164 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
9165 size != map->def.value_size || map->fd >= 0)
9168 memcpy(map->mmaped, data, size);
9172 bool bpf_map__is_offload_neutral(const struct bpf_map *map)
9174 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
9177 bool bpf_map__is_internal(const struct bpf_map *map)
9179 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
9182 __u32 bpf_map__ifindex(const struct bpf_map *map)
9184 return map->map_ifindex;
9187 int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
9191 map->map_ifindex = ifindex;
9195 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
9197 if (!bpf_map_type__is_map_in_map(map->def.type)) {
9198 pr_warn("error: unsupported map type\n");
9201 if (map->inner_map_fd != -1) {
9202 pr_warn("error: inner_map_fd already specified\n");
9205 map->inner_map_fd = fd;
9209 static struct bpf_map *
9210 __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
9213 struct bpf_map *s, *e;
9215 if (!obj || !obj->maps)
9219 e = obj->maps + obj->nr_maps;
9221 if ((m < s) || (m >= e)) {
9222 pr_warn("error in %s: map handler doesn't belong to object\n",
9227 idx = (m - obj->maps) + i;
9228 if (idx >= obj->nr_maps || idx < 0)
9230 return &obj->maps[idx];
9234 bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
9239 return __bpf_map__iter(prev, obj, 1);
9243 bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
9248 return obj->maps + obj->nr_maps - 1;
9251 return __bpf_map__iter(next, obj, -1);
9255 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
9257 struct bpf_map *pos;
9259 bpf_object__for_each_map(pos, obj) {
9260 if (pos->name && !strcmp(pos->name, name))
9267 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
9269 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
9273 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
9275 return ERR_PTR(-ENOTSUP);
9278 long libbpf_get_error(const void *ptr)
9280 return PTR_ERR_OR_ZERO(ptr);
9283 int bpf_prog_load(const char *file, enum bpf_prog_type type,
9284 struct bpf_object **pobj, int *prog_fd)
9286 struct bpf_prog_load_attr attr;
9288 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
9290 attr.prog_type = type;
9291 attr.expected_attach_type = 0;
9293 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
9296 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
9297 struct bpf_object **pobj, int *prog_fd)
9299 struct bpf_object_open_attr open_attr = {};
9300 struct bpf_program *prog, *first_prog = NULL;
9301 struct bpf_object *obj;
9302 struct bpf_map *map;
9310 open_attr.file = attr->file;
9311 open_attr.prog_type = attr->prog_type;
9313 obj = bpf_object__open_xattr(&open_attr);
9314 if (IS_ERR_OR_NULL(obj))
9317 bpf_object__for_each_program(prog, obj) {
9318 enum bpf_attach_type attach_type = attr->expected_attach_type;
9320 * to preserve backwards compatibility, bpf_prog_load treats
9321 * attr->prog_type, if specified, as an override to whatever
9322 * bpf_object__open guessed
9324 if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) {
9325 bpf_program__set_type(prog, attr->prog_type);
9326 bpf_program__set_expected_attach_type(prog,
9329 if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) {
9331 * we haven't guessed from section name and user
9332 * didn't provide a fallback type, too bad...
9334 bpf_object__close(obj);
9338 prog->prog_ifindex = attr->ifindex;
9339 prog->log_level = attr->log_level;
9340 prog->prog_flags |= attr->prog_flags;
9345 bpf_object__for_each_map(map, obj) {
9346 if (!bpf_map__is_offload_neutral(map))
9347 map->map_ifindex = attr->ifindex;
9351 pr_warn("object file doesn't contain bpf program\n");
9352 bpf_object__close(obj);
9356 err = bpf_object__load(obj);
9358 bpf_object__close(obj);
9363 *prog_fd = bpf_program__fd(first_prog);
9368 int (*detach)(struct bpf_link *link);
9369 int (*destroy)(struct bpf_link *link);
9370 char *pin_path; /* NULL, if not pinned */
9371 int fd; /* hook FD, -1 if not applicable */
9375 /* Replace link's underlying BPF program with the new one */
9376 int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
9378 return bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
9381 /* Release "ownership" of underlying BPF resource (typically, BPF program
9382 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
9383 * link, when destructed through bpf_link__destroy() call won't attempt to
9384 * detach/unregisted that BPF resource. This is useful in situations where,
9385 * say, attached BPF program has to outlive userspace program that attached it
9386 * in the system. Depending on type of BPF program, though, there might be
9387 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
9388 * exit of userspace program doesn't trigger automatic detachment and clean up
9389 * inside the kernel.
9391 void bpf_link__disconnect(struct bpf_link *link)
9393 link->disconnected = true;
9396 int bpf_link__destroy(struct bpf_link *link)
9400 if (IS_ERR_OR_NULL(link))
9403 if (!link->disconnected && link->detach)
9404 err = link->detach(link);
9406 link->destroy(link);
9408 free(link->pin_path);
9414 int bpf_link__fd(const struct bpf_link *link)
9419 const char *bpf_link__pin_path(const struct bpf_link *link)
9421 return link->pin_path;
9424 static int bpf_link__detach_fd(struct bpf_link *link)
9426 return close(link->fd);
9429 struct bpf_link *bpf_link__open(const char *path)
9431 struct bpf_link *link;
9434 fd = bpf_obj_get(path);
9437 pr_warn("failed to open link at %s: %d\n", path, fd);
9441 link = calloc(1, sizeof(*link));
9444 return ERR_PTR(-ENOMEM);
9446 link->detach = &bpf_link__detach_fd;
9449 link->pin_path = strdup(path);
9450 if (!link->pin_path) {
9451 bpf_link__destroy(link);
9452 return ERR_PTR(-ENOMEM);
9458 int bpf_link__detach(struct bpf_link *link)
9460 return bpf_link_detach(link->fd) ? -errno : 0;
9463 int bpf_link__pin(struct bpf_link *link, const char *path)
9469 err = make_parent_dir(path);
9472 err = check_path(path);
9476 link->pin_path = strdup(path);
9477 if (!link->pin_path)
9480 if (bpf_obj_pin(link->fd, link->pin_path)) {
9482 zfree(&link->pin_path);
9486 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
9490 int bpf_link__unpin(struct bpf_link *link)
9494 if (!link->pin_path)
9497 err = unlink(link->pin_path);
9501 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
9502 zfree(&link->pin_path);
9506 static int bpf_link__detach_perf_event(struct bpf_link *link)
9510 err = ioctl(link->fd, PERF_EVENT_IOC_DISABLE, 0);
9518 struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
9521 char errmsg[STRERR_BUFSIZE];
9522 struct bpf_link *link;
9526 pr_warn("prog '%s': invalid perf event FD %d\n",
9528 return ERR_PTR(-EINVAL);
9530 prog_fd = bpf_program__fd(prog);
9532 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
9534 return ERR_PTR(-EINVAL);
9537 link = calloc(1, sizeof(*link));
9539 return ERR_PTR(-ENOMEM);
9540 link->detach = &bpf_link__detach_perf_event;
9543 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
9546 pr_warn("prog '%s': failed to attach to pfd %d: %s\n",
9547 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9549 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
9551 return ERR_PTR(err);
9553 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
9556 pr_warn("prog '%s': failed to enable pfd %d: %s\n",
9557 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9558 return ERR_PTR(err);
9564 * this function is expected to parse integer in the range of [0, 2^31-1] from
9565 * given file using scanf format string fmt. If actual parsed value is
9566 * negative, the result might be indistinguishable from error
9568 static int parse_uint_from_file(const char *file, const char *fmt)
9570 char buf[STRERR_BUFSIZE];
9574 f = fopen(file, "r");
9577 pr_debug("failed to open '%s': %s\n", file,
9578 libbpf_strerror_r(err, buf, sizeof(buf)));
9581 err = fscanf(f, fmt, &ret);
9583 err = err == EOF ? -EIO : -errno;
9584 pr_debug("failed to parse '%s': %s\n", file,
9585 libbpf_strerror_r(err, buf, sizeof(buf)));
9593 static int determine_kprobe_perf_type(void)
9595 const char *file = "/sys/bus/event_source/devices/kprobe/type";
9597 return parse_uint_from_file(file, "%d\n");
9600 static int determine_uprobe_perf_type(void)
9602 const char *file = "/sys/bus/event_source/devices/uprobe/type";
9604 return parse_uint_from_file(file, "%d\n");
9607 static int determine_kprobe_retprobe_bit(void)
9609 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
9611 return parse_uint_from_file(file, "config:%d\n");
9614 static int determine_uprobe_retprobe_bit(void)
9616 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
9618 return parse_uint_from_file(file, "config:%d\n");
9621 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
9622 uint64_t offset, int pid)
9624 struct perf_event_attr attr = {};
9625 char errmsg[STRERR_BUFSIZE];
9628 type = uprobe ? determine_uprobe_perf_type()
9629 : determine_kprobe_perf_type();
9631 pr_warn("failed to determine %s perf type: %s\n",
9632 uprobe ? "uprobe" : "kprobe",
9633 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
9637 int bit = uprobe ? determine_uprobe_retprobe_bit()
9638 : determine_kprobe_retprobe_bit();
9641 pr_warn("failed to determine %s retprobe bit: %s\n",
9642 uprobe ? "uprobe" : "kprobe",
9643 libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
9646 attr.config |= 1 << bit;
9648 attr.size = sizeof(attr);
9650 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
9651 attr.config2 = offset; /* kprobe_addr or probe_offset */
9653 /* pid filter is meaningful only for uprobes */
9654 pfd = syscall(__NR_perf_event_open, &attr,
9655 pid < 0 ? -1 : pid /* pid */,
9656 pid == -1 ? 0 : -1 /* cpu */,
9657 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9660 pr_warn("%s perf_event_open() failed: %s\n",
9661 uprobe ? "uprobe" : "kprobe",
9662 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9668 struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
9670 const char *func_name)
9672 char errmsg[STRERR_BUFSIZE];
9673 struct bpf_link *link;
9676 pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
9677 0 /* offset */, -1 /* pid */);
9679 pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
9680 prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
9681 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
9682 return ERR_PTR(pfd);
9684 link = bpf_program__attach_perf_event(prog, pfd);
9687 err = PTR_ERR(link);
9688 pr_warn("prog '%s': failed to attach to %s '%s': %s\n",
9689 prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
9690 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9696 static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
9697 struct bpf_program *prog)
9699 const char *func_name;
9702 func_name = prog->sec_name + sec->len;
9703 retprobe = strcmp(sec->sec, "kretprobe/") == 0;
9705 return bpf_program__attach_kprobe(prog, retprobe, func_name);
9708 struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
9709 bool retprobe, pid_t pid,
9710 const char *binary_path,
9713 char errmsg[STRERR_BUFSIZE];
9714 struct bpf_link *link;
9717 pfd = perf_event_open_probe(true /* uprobe */, retprobe,
9718 binary_path, func_offset, pid);
9720 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
9721 prog->name, retprobe ? "uretprobe" : "uprobe",
9722 binary_path, func_offset,
9723 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
9724 return ERR_PTR(pfd);
9726 link = bpf_program__attach_perf_event(prog, pfd);
9729 err = PTR_ERR(link);
9730 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
9731 prog->name, retprobe ? "uretprobe" : "uprobe",
9732 binary_path, func_offset,
9733 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9739 static int determine_tracepoint_id(const char *tp_category,
9740 const char *tp_name)
9742 char file[PATH_MAX];
9745 ret = snprintf(file, sizeof(file),
9746 "/sys/kernel/debug/tracing/events/%s/%s/id",
9747 tp_category, tp_name);
9750 if (ret >= sizeof(file)) {
9751 pr_debug("tracepoint %s/%s path is too long\n",
9752 tp_category, tp_name);
9755 return parse_uint_from_file(file, "%d\n");
9758 static int perf_event_open_tracepoint(const char *tp_category,
9759 const char *tp_name)
9761 struct perf_event_attr attr = {};
9762 char errmsg[STRERR_BUFSIZE];
9763 int tp_id, pfd, err;
9765 tp_id = determine_tracepoint_id(tp_category, tp_name);
9767 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
9768 tp_category, tp_name,
9769 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
9773 attr.type = PERF_TYPE_TRACEPOINT;
9774 attr.size = sizeof(attr);
9775 attr.config = tp_id;
9777 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
9778 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9781 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
9782 tp_category, tp_name,
9783 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9789 struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
9790 const char *tp_category,
9791 const char *tp_name)
9793 char errmsg[STRERR_BUFSIZE];
9794 struct bpf_link *link;
9797 pfd = perf_event_open_tracepoint(tp_category, tp_name);
9799 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
9800 prog->name, tp_category, tp_name,
9801 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
9802 return ERR_PTR(pfd);
9804 link = bpf_program__attach_perf_event(prog, pfd);
9807 err = PTR_ERR(link);
9808 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
9809 prog->name, tp_category, tp_name,
9810 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9816 static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
9817 struct bpf_program *prog)
9819 char *sec_name, *tp_cat, *tp_name;
9820 struct bpf_link *link;
9822 sec_name = strdup(prog->sec_name);
9824 return ERR_PTR(-ENOMEM);
9826 /* extract "tp/<category>/<name>" */
9827 tp_cat = sec_name + sec->len;
9828 tp_name = strchr(tp_cat, '/');
9830 link = ERR_PTR(-EINVAL);
9836 link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
9842 struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
9843 const char *tp_name)
9845 char errmsg[STRERR_BUFSIZE];
9846 struct bpf_link *link;
9849 prog_fd = bpf_program__fd(prog);
9851 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
9852 return ERR_PTR(-EINVAL);
9855 link = calloc(1, sizeof(*link));
9857 return ERR_PTR(-ENOMEM);
9858 link->detach = &bpf_link__detach_fd;
9860 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
9864 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
9865 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
9866 return ERR_PTR(pfd);
9872 static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
9873 struct bpf_program *prog)
9875 const char *tp_name = prog->sec_name + sec->len;
9877 return bpf_program__attach_raw_tracepoint(prog, tp_name);
9880 /* Common logic for all BPF program types that attach to a btf_id */
9881 static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
9883 char errmsg[STRERR_BUFSIZE];
9884 struct bpf_link *link;
9887 prog_fd = bpf_program__fd(prog);
9889 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
9890 return ERR_PTR(-EINVAL);
9893 link = calloc(1, sizeof(*link));
9895 return ERR_PTR(-ENOMEM);
9896 link->detach = &bpf_link__detach_fd;
9898 pfd = bpf_raw_tracepoint_open(NULL, prog_fd);
9902 pr_warn("prog '%s': failed to attach: %s\n",
9903 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
9904 return ERR_PTR(pfd);
9907 return (struct bpf_link *)link;
9910 struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)
9912 return bpf_program__attach_btf_id(prog);
9915 struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
9917 return bpf_program__attach_btf_id(prog);
9920 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
9921 struct bpf_program *prog)
9923 return bpf_program__attach_trace(prog);
9926 static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
9927 struct bpf_program *prog)
9929 return bpf_program__attach_lsm(prog);
9932 static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
9933 struct bpf_program *prog)
9935 return bpf_program__attach_iter(prog, NULL);
9938 static struct bpf_link *
9939 bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
9940 const char *target_name)
9942 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
9943 .target_btf_id = btf_id);
9944 enum bpf_attach_type attach_type;
9945 char errmsg[STRERR_BUFSIZE];
9946 struct bpf_link *link;
9947 int prog_fd, link_fd;
9949 prog_fd = bpf_program__fd(prog);
9951 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
9952 return ERR_PTR(-EINVAL);
9955 link = calloc(1, sizeof(*link));
9957 return ERR_PTR(-ENOMEM);
9958 link->detach = &bpf_link__detach_fd;
9960 attach_type = bpf_program__get_expected_attach_type(prog);
9961 link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
9965 pr_warn("prog '%s': failed to attach to %s: %s\n",
9966 prog->name, target_name,
9967 libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
9968 return ERR_PTR(link_fd);
9975 bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
9977 return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
9981 bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
9983 return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
9986 struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
9988 /* target_fd/target_ifindex use the same field in LINK_CREATE */
9989 return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
9992 struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
9994 const char *attach_func_name)
9998 if (!!target_fd != !!attach_func_name) {
9999 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
10001 return ERR_PTR(-EINVAL);
10004 if (prog->type != BPF_PROG_TYPE_EXT) {
10005 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
10007 return ERR_PTR(-EINVAL);
10011 btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
10013 return ERR_PTR(btf_id);
10015 return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
10017 /* no target, so use raw_tracepoint_open for compatibility
10020 return bpf_program__attach_trace(prog);
10025 bpf_program__attach_iter(struct bpf_program *prog,
10026 const struct bpf_iter_attach_opts *opts)
10028 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
10029 char errmsg[STRERR_BUFSIZE];
10030 struct bpf_link *link;
10031 int prog_fd, link_fd;
10032 __u32 target_fd = 0;
10034 if (!OPTS_VALID(opts, bpf_iter_attach_opts))
10035 return ERR_PTR(-EINVAL);
10037 link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
10038 link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
10040 prog_fd = bpf_program__fd(prog);
10042 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
10043 return ERR_PTR(-EINVAL);
10046 link = calloc(1, sizeof(*link));
10048 return ERR_PTR(-ENOMEM);
10049 link->detach = &bpf_link__detach_fd;
10051 link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
10052 &link_create_opts);
10056 pr_warn("prog '%s': failed to attach to iterator: %s\n",
10057 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
10058 return ERR_PTR(link_fd);
10060 link->fd = link_fd;
10064 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
10066 const struct bpf_sec_def *sec_def;
10068 sec_def = find_sec_def(prog->sec_name);
10069 if (!sec_def || !sec_def->attach_fn)
10070 return ERR_PTR(-ESRCH);
10072 return sec_def->attach_fn(sec_def, prog);
10075 static int bpf_link__detach_struct_ops(struct bpf_link *link)
10079 if (bpf_map_delete_elem(link->fd, &zero))
10085 struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map)
10087 struct bpf_struct_ops *st_ops;
10088 struct bpf_link *link;
10092 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
10093 return ERR_PTR(-EINVAL);
10095 link = calloc(1, sizeof(*link));
10097 return ERR_PTR(-EINVAL);
10099 st_ops = map->st_ops;
10100 for (i = 0; i < btf_vlen(st_ops->type); i++) {
10101 struct bpf_program *prog = st_ops->progs[i];
10108 prog_fd = bpf_program__fd(prog);
10109 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
10110 *(unsigned long *)kern_data = prog_fd;
10113 err = bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0);
10117 return ERR_PTR(err);
10120 link->detach = bpf_link__detach_struct_ops;
10121 link->fd = map->fd;
10126 enum bpf_perf_event_ret
10127 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
10128 void **copy_mem, size_t *copy_size,
10129 bpf_perf_event_print_t fn, void *private_data)
10131 struct perf_event_mmap_page *header = mmap_mem;
10132 __u64 data_head = ring_buffer_read_head(header);
10133 __u64 data_tail = header->data_tail;
10134 void *base = ((__u8 *)header) + page_size;
10135 int ret = LIBBPF_PERF_EVENT_CONT;
10136 struct perf_event_header *ehdr;
10139 while (data_head != data_tail) {
10140 ehdr = base + (data_tail & (mmap_size - 1));
10141 ehdr_size = ehdr->size;
10143 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
10144 void *copy_start = ehdr;
10145 size_t len_first = base + mmap_size - copy_start;
10146 size_t len_secnd = ehdr_size - len_first;
10148 if (*copy_size < ehdr_size) {
10150 *copy_mem = malloc(ehdr_size);
10153 ret = LIBBPF_PERF_EVENT_ERROR;
10156 *copy_size = ehdr_size;
10159 memcpy(*copy_mem, copy_start, len_first);
10160 memcpy(*copy_mem + len_first, base, len_secnd);
10164 ret = fn(ehdr, private_data);
10165 data_tail += ehdr_size;
10166 if (ret != LIBBPF_PERF_EVENT_CONT)
10170 ring_buffer_write_tail(header, data_tail);
10174 struct perf_buffer;
10176 struct perf_buffer_params {
10177 struct perf_event_attr *attr;
10178 /* if event_cb is specified, it takes precendence */
10179 perf_buffer_event_fn event_cb;
10180 /* sample_cb and lost_cb are higher-level common-case callbacks */
10181 perf_buffer_sample_fn sample_cb;
10182 perf_buffer_lost_fn lost_cb;
10189 struct perf_cpu_buf {
10190 struct perf_buffer *pb;
10191 void *base; /* mmap()'ed memory */
10192 void *buf; /* for reconstructing segmented data */
10199 struct perf_buffer {
10200 perf_buffer_event_fn event_cb;
10201 perf_buffer_sample_fn sample_cb;
10202 perf_buffer_lost_fn lost_cb;
10203 void *ctx; /* passed into callbacks */
10207 struct perf_cpu_buf **cpu_bufs;
10208 struct epoll_event *events;
10209 int cpu_cnt; /* number of allocated CPU buffers */
10210 int epoll_fd; /* perf event FD */
10211 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
10214 static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
10215 struct perf_cpu_buf *cpu_buf)
10219 if (cpu_buf->base &&
10220 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
10221 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
10222 if (cpu_buf->fd >= 0) {
10223 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
10224 close(cpu_buf->fd);
10226 free(cpu_buf->buf);
10230 void perf_buffer__free(struct perf_buffer *pb)
10234 if (IS_ERR_OR_NULL(pb))
10236 if (pb->cpu_bufs) {
10237 for (i = 0; i < pb->cpu_cnt; i++) {
10238 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
10243 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
10244 perf_buffer__free_cpu_buf(pb, cpu_buf);
10246 free(pb->cpu_bufs);
10248 if (pb->epoll_fd >= 0)
10249 close(pb->epoll_fd);
10254 static struct perf_cpu_buf *
10255 perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
10256 int cpu, int map_key)
10258 struct perf_cpu_buf *cpu_buf;
10259 char msg[STRERR_BUFSIZE];
10262 cpu_buf = calloc(1, sizeof(*cpu_buf));
10264 return ERR_PTR(-ENOMEM);
10267 cpu_buf->cpu = cpu;
10268 cpu_buf->map_key = map_key;
10270 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
10271 -1, PERF_FLAG_FD_CLOEXEC);
10272 if (cpu_buf->fd < 0) {
10274 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
10275 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
10279 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
10280 PROT_READ | PROT_WRITE, MAP_SHARED,
10282 if (cpu_buf->base == MAP_FAILED) {
10283 cpu_buf->base = NULL;
10285 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
10286 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
10290 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10292 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
10293 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
10300 perf_buffer__free_cpu_buf(pb, cpu_buf);
10301 return (struct perf_cpu_buf *)ERR_PTR(err);
10304 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
10305 struct perf_buffer_params *p);
10307 struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
10308 const struct perf_buffer_opts *opts)
10310 struct perf_buffer_params p = {};
10311 struct perf_event_attr attr = { 0, };
10313 attr.config = PERF_COUNT_SW_BPF_OUTPUT;
10314 attr.type = PERF_TYPE_SOFTWARE;
10315 attr.sample_type = PERF_SAMPLE_RAW;
10316 attr.sample_period = 1;
10317 attr.wakeup_events = 1;
10320 p.sample_cb = opts ? opts->sample_cb : NULL;
10321 p.lost_cb = opts ? opts->lost_cb : NULL;
10322 p.ctx = opts ? opts->ctx : NULL;
10324 return __perf_buffer__new(map_fd, page_cnt, &p);
10327 struct perf_buffer *
10328 perf_buffer__new_raw(int map_fd, size_t page_cnt,
10329 const struct perf_buffer_raw_opts *opts)
10331 struct perf_buffer_params p = {};
10333 p.attr = opts->attr;
10334 p.event_cb = opts->event_cb;
10336 p.cpu_cnt = opts->cpu_cnt;
10337 p.cpus = opts->cpus;
10338 p.map_keys = opts->map_keys;
10340 return __perf_buffer__new(map_fd, page_cnt, &p);
10343 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
10344 struct perf_buffer_params *p)
10346 const char *online_cpus_file = "/sys/devices/system/cpu/online";
10347 struct bpf_map_info map;
10348 char msg[STRERR_BUFSIZE];
10349 struct perf_buffer *pb;
10350 bool *online = NULL;
10351 __u32 map_info_len;
10354 if (page_cnt & (page_cnt - 1)) {
10355 pr_warn("page count should be power of two, but is %zu\n",
10357 return ERR_PTR(-EINVAL);
10360 /* best-effort sanity checks */
10361 memset(&map, 0, sizeof(map));
10362 map_info_len = sizeof(map);
10363 err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
10366 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
10367 * -EBADFD, -EFAULT, or -E2BIG on real error
10369 if (err != -EINVAL) {
10370 pr_warn("failed to get map info for map FD %d: %s\n",
10371 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
10372 return ERR_PTR(err);
10374 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
10377 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
10378 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
10380 return ERR_PTR(-EINVAL);
10384 pb = calloc(1, sizeof(*pb));
10386 return ERR_PTR(-ENOMEM);
10388 pb->event_cb = p->event_cb;
10389 pb->sample_cb = p->sample_cb;
10390 pb->lost_cb = p->lost_cb;
10393 pb->page_size = getpagesize();
10394 pb->mmap_size = pb->page_size * page_cnt;
10395 pb->map_fd = map_fd;
10397 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
10398 if (pb->epoll_fd < 0) {
10400 pr_warn("failed to create epoll instance: %s\n",
10401 libbpf_strerror_r(err, msg, sizeof(msg)));
10405 if (p->cpu_cnt > 0) {
10406 pb->cpu_cnt = p->cpu_cnt;
10408 pb->cpu_cnt = libbpf_num_possible_cpus();
10409 if (pb->cpu_cnt < 0) {
10413 if (map.max_entries && map.max_entries < pb->cpu_cnt)
10414 pb->cpu_cnt = map.max_entries;
10417 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
10420 pr_warn("failed to allocate events: out of memory\n");
10423 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
10424 if (!pb->cpu_bufs) {
10426 pr_warn("failed to allocate buffers: out of memory\n");
10430 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
10432 pr_warn("failed to get online CPU mask: %d\n", err);
10436 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
10437 struct perf_cpu_buf *cpu_buf;
10440 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
10441 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
10443 /* in case user didn't explicitly requested particular CPUs to
10444 * be attached to, skip offline/not present CPUs
10446 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
10449 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
10450 if (IS_ERR(cpu_buf)) {
10451 err = PTR_ERR(cpu_buf);
10455 pb->cpu_bufs[j] = cpu_buf;
10457 err = bpf_map_update_elem(pb->map_fd, &map_key,
10461 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
10462 cpu, map_key, cpu_buf->fd,
10463 libbpf_strerror_r(err, msg, sizeof(msg)));
10467 pb->events[j].events = EPOLLIN;
10468 pb->events[j].data.ptr = cpu_buf;
10469 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
10470 &pb->events[j]) < 0) {
10472 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
10474 libbpf_strerror_r(err, msg, sizeof(msg)));
10487 perf_buffer__free(pb);
10488 return ERR_PTR(err);
10491 struct perf_sample_raw {
10492 struct perf_event_header header;
10497 struct perf_sample_lost {
10498 struct perf_event_header header;
10501 uint64_t sample_id;
10504 static enum bpf_perf_event_ret
10505 perf_buffer__process_record(struct perf_event_header *e, void *ctx)
10507 struct perf_cpu_buf *cpu_buf = ctx;
10508 struct perf_buffer *pb = cpu_buf->pb;
10511 /* user wants full control over parsing perf event */
10513 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
10516 case PERF_RECORD_SAMPLE: {
10517 struct perf_sample_raw *s = data;
10520 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
10523 case PERF_RECORD_LOST: {
10524 struct perf_sample_lost *s = data;
10527 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
10531 pr_warn("unknown perf sample type %d\n", e->type);
10532 return LIBBPF_PERF_EVENT_ERROR;
10534 return LIBBPF_PERF_EVENT_CONT;
10537 static int perf_buffer__process_records(struct perf_buffer *pb,
10538 struct perf_cpu_buf *cpu_buf)
10540 enum bpf_perf_event_ret ret;
10542 ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
10543 pb->page_size, &cpu_buf->buf,
10544 &cpu_buf->buf_size,
10545 perf_buffer__process_record, cpu_buf);
10546 if (ret != LIBBPF_PERF_EVENT_CONT)
10551 int perf_buffer__epoll_fd(const struct perf_buffer *pb)
10553 return pb->epoll_fd;
10556 int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
10560 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
10561 for (i = 0; i < cnt; i++) {
10562 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
10564 err = perf_buffer__process_records(pb, cpu_buf);
10566 pr_warn("error while processing records: %d\n", err);
10570 return cnt < 0 ? -errno : cnt;
10573 /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
10576 size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
10578 return pb->cpu_cnt;
10582 * Return perf_event FD of a ring buffer in *buf_idx* slot of
10583 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
10584 * select()/poll()/epoll() Linux syscalls.
10586 int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
10588 struct perf_cpu_buf *cpu_buf;
10590 if (buf_idx >= pb->cpu_cnt)
10593 cpu_buf = pb->cpu_bufs[buf_idx];
10597 return cpu_buf->fd;
10601 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
10602 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
10603 * consume, do nothing and return success.
10608 int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
10610 struct perf_cpu_buf *cpu_buf;
10612 if (buf_idx >= pb->cpu_cnt)
10615 cpu_buf = pb->cpu_bufs[buf_idx];
10619 return perf_buffer__process_records(pb, cpu_buf);
10622 int perf_buffer__consume(struct perf_buffer *pb)
10626 for (i = 0; i < pb->cpu_cnt; i++) {
10627 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
10632 err = perf_buffer__process_records(pb, cpu_buf);
10634 pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err);
10641 struct bpf_prog_info_array_desc {
10642 int array_offset; /* e.g. offset of jited_prog_insns */
10643 int count_offset; /* e.g. offset of jited_prog_len */
10644 int size_offset; /* > 0: offset of rec size,
10645 * < 0: fix size of -size_offset
10649 static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
10650 [BPF_PROG_INFO_JITED_INSNS] = {
10651 offsetof(struct bpf_prog_info, jited_prog_insns),
10652 offsetof(struct bpf_prog_info, jited_prog_len),
10655 [BPF_PROG_INFO_XLATED_INSNS] = {
10656 offsetof(struct bpf_prog_info, xlated_prog_insns),
10657 offsetof(struct bpf_prog_info, xlated_prog_len),
10660 [BPF_PROG_INFO_MAP_IDS] = {
10661 offsetof(struct bpf_prog_info, map_ids),
10662 offsetof(struct bpf_prog_info, nr_map_ids),
10663 -(int)sizeof(__u32),
10665 [BPF_PROG_INFO_JITED_KSYMS] = {
10666 offsetof(struct bpf_prog_info, jited_ksyms),
10667 offsetof(struct bpf_prog_info, nr_jited_ksyms),
10668 -(int)sizeof(__u64),
10670 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
10671 offsetof(struct bpf_prog_info, jited_func_lens),
10672 offsetof(struct bpf_prog_info, nr_jited_func_lens),
10673 -(int)sizeof(__u32),
10675 [BPF_PROG_INFO_FUNC_INFO] = {
10676 offsetof(struct bpf_prog_info, func_info),
10677 offsetof(struct bpf_prog_info, nr_func_info),
10678 offsetof(struct bpf_prog_info, func_info_rec_size),
10680 [BPF_PROG_INFO_LINE_INFO] = {
10681 offsetof(struct bpf_prog_info, line_info),
10682 offsetof(struct bpf_prog_info, nr_line_info),
10683 offsetof(struct bpf_prog_info, line_info_rec_size),
10685 [BPF_PROG_INFO_JITED_LINE_INFO] = {
10686 offsetof(struct bpf_prog_info, jited_line_info),
10687 offsetof(struct bpf_prog_info, nr_jited_line_info),
10688 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
10690 [BPF_PROG_INFO_PROG_TAGS] = {
10691 offsetof(struct bpf_prog_info, prog_tags),
10692 offsetof(struct bpf_prog_info, nr_prog_tags),
10693 -(int)sizeof(__u8) * BPF_TAG_SIZE,
10698 static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info,
10701 __u32 *array = (__u32 *)info;
10704 return array[offset / sizeof(__u32)];
10705 return -(int)offset;
10708 static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info,
10711 __u64 *array = (__u64 *)info;
10714 return array[offset / sizeof(__u64)];
10715 return -(int)offset;
10718 static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
10721 __u32 *array = (__u32 *)info;
10724 array[offset / sizeof(__u32)] = val;
10727 static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
10730 __u64 *array = (__u64 *)info;
10733 array[offset / sizeof(__u64)] = val;
10736 struct bpf_prog_info_linear *
10737 bpf_program__get_prog_info_linear(int fd, __u64 arrays)
10739 struct bpf_prog_info_linear *info_linear;
10740 struct bpf_prog_info info = {};
10741 __u32 info_len = sizeof(info);
10742 __u32 data_len = 0;
10746 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
10747 return ERR_PTR(-EINVAL);
10749 /* step 1: get array dimensions */
10750 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
10752 pr_debug("can't get prog info: %s", strerror(errno));
10753 return ERR_PTR(-EFAULT);
10756 /* step 2: calculate total size of all arrays */
10757 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
10758 bool include_array = (arrays & (1UL << i)) > 0;
10759 struct bpf_prog_info_array_desc *desc;
10762 desc = bpf_prog_info_array_desc + i;
10764 /* kernel is too old to support this field */
10765 if (info_len < desc->array_offset + sizeof(__u32) ||
10766 info_len < desc->count_offset + sizeof(__u32) ||
10767 (desc->size_offset > 0 && info_len < desc->size_offset))
10768 include_array = false;
10770 if (!include_array) {
10771 arrays &= ~(1UL << i); /* clear the bit */
10775 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
10776 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
10778 data_len += count * size;
10781 /* step 3: allocate continuous memory */
10782 data_len = roundup(data_len, sizeof(__u64));
10783 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
10785 return ERR_PTR(-ENOMEM);
10787 /* step 4: fill data to info_linear->info */
10788 info_linear->arrays = arrays;
10789 memset(&info_linear->info, 0, sizeof(info));
10790 ptr = info_linear->data;
10792 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
10793 struct bpf_prog_info_array_desc *desc;
10796 if ((arrays & (1UL << i)) == 0)
10799 desc = bpf_prog_info_array_desc + i;
10800 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
10801 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
10802 bpf_prog_info_set_offset_u32(&info_linear->info,
10803 desc->count_offset, count);
10804 bpf_prog_info_set_offset_u32(&info_linear->info,
10805 desc->size_offset, size);
10806 bpf_prog_info_set_offset_u64(&info_linear->info,
10807 desc->array_offset,
10809 ptr += count * size;
10812 /* step 5: call syscall again to get required arrays */
10813 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
10815 pr_debug("can't get prog info: %s", strerror(errno));
10817 return ERR_PTR(-EFAULT);
10820 /* step 6: verify the data */
10821 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
10822 struct bpf_prog_info_array_desc *desc;
10825 if ((arrays & (1UL << i)) == 0)
10828 desc = bpf_prog_info_array_desc + i;
10829 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
10830 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
10831 desc->count_offset);
10833 pr_warn("%s: mismatch in element count\n", __func__);
10835 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
10836 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
10837 desc->size_offset);
10839 pr_warn("%s: mismatch in rec size\n", __func__);
10842 /* step 7: update info_len and data_len */
10843 info_linear->info_len = sizeof(struct bpf_prog_info);
10844 info_linear->data_len = data_len;
10846 return info_linear;
10849 void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
10853 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
10854 struct bpf_prog_info_array_desc *desc;
10857 if ((info_linear->arrays & (1UL << i)) == 0)
10860 desc = bpf_prog_info_array_desc + i;
10861 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
10862 desc->array_offset);
10863 offs = addr - ptr_to_u64(info_linear->data);
10864 bpf_prog_info_set_offset_u64(&info_linear->info,
10865 desc->array_offset, offs);
10869 void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
10873 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
10874 struct bpf_prog_info_array_desc *desc;
10877 if ((info_linear->arrays & (1UL << i)) == 0)
10880 desc = bpf_prog_info_array_desc + i;
10881 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
10882 desc->array_offset);
10883 addr = offs + ptr_to_u64(info_linear->data);
10884 bpf_prog_info_set_offset_u64(&info_linear->info,
10885 desc->array_offset, addr);
10889 int bpf_program__set_attach_target(struct bpf_program *prog,
10890 int attach_prog_fd,
10891 const char *attach_func_name)
10893 int btf_obj_fd = 0, btf_id = 0, err;
10895 if (!prog || attach_prog_fd < 0 || !attach_func_name)
10898 if (prog->obj->loaded)
10901 if (attach_prog_fd) {
10902 btf_id = libbpf_find_prog_btf_id(attach_func_name,
10907 /* load btf_vmlinux, if not yet */
10908 err = bpf_object__load_vmlinux_btf(prog->obj, true);
10911 err = find_kernel_btf_id(prog->obj, attach_func_name,
10912 prog->expected_attach_type,
10913 &btf_obj_fd, &btf_id);
10918 prog->attach_btf_id = btf_id;
10919 prog->attach_btf_obj_fd = btf_obj_fd;
10920 prog->attach_prog_fd = attach_prog_fd;
10924 int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
10926 int err = 0, n, len, start, end = -1;
10932 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
10934 if (*s == ',' || *s == '\n') {
10938 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
10939 if (n <= 0 || n > 2) {
10940 pr_warn("Failed to get CPU range %s: %d\n", s, n);
10943 } else if (n == 1) {
10946 if (start < 0 || start > end) {
10947 pr_warn("Invalid CPU range [%d,%d] in %s\n",
10952 tmp = realloc(*mask, end + 1);
10958 memset(tmp + *mask_sz, 0, start - *mask_sz);
10959 memset(tmp + start, 1, end - start + 1);
10960 *mask_sz = end + 1;
10964 pr_warn("Empty CPU range\n");
10974 int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
10976 int fd, err = 0, len;
10979 fd = open(fcpu, O_RDONLY);
10982 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
10985 len = read(fd, buf, sizeof(buf));
10988 err = len ? -errno : -EINVAL;
10989 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
10992 if (len >= sizeof(buf)) {
10993 pr_warn("CPU mask is too big in file %s\n", fcpu);
10998 return parse_cpu_mask_str(buf, mask, mask_sz);
11001 int libbpf_num_possible_cpus(void)
11003 static const char *fcpu = "/sys/devices/system/cpu/possible";
11005 int err, n, i, tmp_cpus;
11008 tmp_cpus = READ_ONCE(cpus);
11012 err = parse_cpu_mask_file(fcpu, &mask, &n);
11017 for (i = 0; i < n; i++) {
11023 WRITE_ONCE(cpus, tmp_cpus);
11027 int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
11028 const struct bpf_object_open_opts *opts)
11030 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
11031 .object_name = s->name,
11033 struct bpf_object *obj;
11036 /* Attempt to preserve opts->object_name, unless overriden by user
11037 * explicitly. Overwriting object name for skeletons is discouraged,
11038 * as it breaks global data maps, because they contain object name
11039 * prefix as their own map name prefix. When skeleton is generated,
11040 * bpftool is making an assumption that this name will stay the same.
11043 memcpy(&skel_opts, opts, sizeof(*opts));
11044 if (!opts->object_name)
11045 skel_opts.object_name = s->name;
11048 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
11050 pr_warn("failed to initialize skeleton BPF object '%s': %ld\n",
11051 s->name, PTR_ERR(obj));
11052 return PTR_ERR(obj);
11057 for (i = 0; i < s->map_cnt; i++) {
11058 struct bpf_map **map = s->maps[i].map;
11059 const char *name = s->maps[i].name;
11060 void **mmaped = s->maps[i].mmaped;
11062 *map = bpf_object__find_map_by_name(obj, name);
11064 pr_warn("failed to find skeleton map '%s'\n", name);
11068 /* externs shouldn't be pre-setup from user code */
11069 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
11070 *mmaped = (*map)->mmaped;
11073 for (i = 0; i < s->prog_cnt; i++) {
11074 struct bpf_program **prog = s->progs[i].prog;
11075 const char *name = s->progs[i].name;
11077 *prog = bpf_object__find_program_by_name(obj, name);
11079 pr_warn("failed to find skeleton program '%s'\n", name);
11087 int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
11091 err = bpf_object__load(*s->obj);
11093 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
11097 for (i = 0; i < s->map_cnt; i++) {
11098 struct bpf_map *map = *s->maps[i].map;
11099 size_t mmap_sz = bpf_map_mmap_sz(map);
11100 int prot, map_fd = bpf_map__fd(map);
11101 void **mmaped = s->maps[i].mmaped;
11106 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
11111 if (map->def.map_flags & BPF_F_RDONLY_PROG)
11114 prot = PROT_READ | PROT_WRITE;
11116 /* Remap anonymous mmap()-ed "map initialization image" as
11117 * a BPF map-backed mmap()-ed memory, but preserving the same
11118 * memory address. This will cause kernel to change process'
11119 * page table to point to a different piece of kernel memory,
11120 * but from userspace point of view memory address (and its
11121 * contents, being identical at this point) will stay the
11122 * same. This mapping will be released by bpf_object__close()
11123 * as per normal clean up procedure, so we don't need to worry
11124 * about it from skeleton's clean up perspective.
11126 *mmaped = mmap(map->mmaped, mmap_sz, prot,
11127 MAP_SHARED | MAP_FIXED, map_fd, 0);
11128 if (*mmaped == MAP_FAILED) {
11131 pr_warn("failed to re-mmap() map '%s': %d\n",
11132 bpf_map__name(map), err);
11140 int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
11144 for (i = 0; i < s->prog_cnt; i++) {
11145 struct bpf_program *prog = *s->progs[i].prog;
11146 struct bpf_link **link = s->progs[i].link;
11147 const struct bpf_sec_def *sec_def;
11152 sec_def = find_sec_def(prog->sec_name);
11153 if (!sec_def || !sec_def->attach_fn)
11156 *link = sec_def->attach_fn(sec_def, prog);
11157 if (IS_ERR(*link)) {
11158 pr_warn("failed to auto-attach program '%s': %ld\n",
11159 bpf_program__name(prog), PTR_ERR(*link));
11160 return PTR_ERR(*link);
11167 void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
11171 for (i = 0; i < s->prog_cnt; i++) {
11172 struct bpf_link **link = s->progs[i].link;
11174 bpf_link__destroy(*link);
11179 void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
11182 bpf_object__detach_skeleton(s);
11184 bpf_object__close(*s->obj);