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.
25 #include <asm/unistd.h>
26 #include <linux/err.h>
27 #include <linux/kernel.h>
28 #include <linux/bpf.h>
29 #include <linux/btf.h>
30 #include <linux/filter.h>
31 #include <linux/list.h>
32 #include <linux/limits.h>
33 #include <linux/perf_event.h>
34 #include <linux/ring_buffer.h>
36 #include <sys/types.h>
38 #include <tools/libc_compat.h>
45 #include "str_error.h"
46 #include "libbpf_util.h"
47 #include "libbpf_internal.h"
54 #define BPF_FS_MAGIC 0xcafe4a11
57 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
58 * compilation if user enables corresponding warning. Disable it explicitly.
60 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
62 #define __printf(a, b) __attribute__((format(printf, a, b)))
64 static int __base_pr(enum libbpf_print_level level, const char *format,
67 if (level == LIBBPF_DEBUG)
70 return vfprintf(stderr, format, args);
73 static libbpf_print_fn_t __libbpf_pr = __base_pr;
75 void libbpf_set_print(libbpf_print_fn_t fn)
81 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
88 va_start(args, format);
89 __libbpf_pr(level, format, args);
93 #define STRERR_BUFSIZE 128
95 #define CHECK_ERR(action, err, out) do { \
102 /* Copied from tools/perf/util/util.h */
104 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
108 # define zclose(fd) ({ \
111 ___err = close((fd)); \
116 #ifdef HAVE_LIBELF_MMAP_SUPPORT
117 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
119 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
122 static inline __u64 ptr_to_u64(const void *ptr)
124 return (__u64) (unsigned long) ptr;
127 struct bpf_capabilities {
128 /* v4.14: kernel support for program & map names. */
130 /* v5.2: kernel support for global data sections. */
132 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
134 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
139 * bpf_prog should be a better name but it has been used in
143 /* Index in elf obj file, for relocation use. */
148 /* section_name with / replaced by _; makes recursive pinning
149 * in bpf_object__pin_programs easier
152 struct bpf_insn *insns;
153 size_t insns_cnt, main_prog_cnt;
154 enum bpf_prog_type type;
175 bpf_program_prep_t preprocessor;
177 struct bpf_object *obj;
179 bpf_program_clear_priv_t clear_priv;
181 enum bpf_attach_type expected_attach_type;
184 __u32 func_info_rec_size;
187 struct bpf_capabilities *caps;
190 __u32 line_info_rec_size;
194 enum libbpf_map_type {
201 static const char * const libbpf_type_to_btf_name[] = {
202 [LIBBPF_MAP_DATA] = ".data",
203 [LIBBPF_MAP_BSS] = ".bss",
204 [LIBBPF_MAP_RODATA] = ".rodata",
213 struct bpf_map_def def;
214 __u32 btf_key_type_id;
215 __u32 btf_value_type_id;
217 bpf_map_clear_priv_t clear_priv;
218 enum libbpf_map_type libbpf_type;
226 static LIST_HEAD(bpf_objects_list);
229 char name[BPF_OBJ_NAME_LEN];
233 struct bpf_program *programs;
235 struct bpf_map *maps;
237 struct bpf_secdata sections;
240 bool has_pseudo_calls;
243 * Information when doing elf related work. Only valid if fd
269 * All loaded bpf_object is linked in a list, which is
270 * hidden to caller. bpf_objects__<func> handlers deal with
273 struct list_head list;
276 struct btf_ext *btf_ext;
279 bpf_object_clear_priv_t clear_priv;
281 struct bpf_capabilities caps;
285 #define obj_elf_valid(o) ((o)->efile.elf)
287 void bpf_program__unload(struct bpf_program *prog)
295 * If the object is opened but the program was never loaded,
296 * it is possible that prog->instances.nr == -1.
298 if (prog->instances.nr > 0) {
299 for (i = 0; i < prog->instances.nr; i++)
300 zclose(prog->instances.fds[i]);
301 } else if (prog->instances.nr != -1) {
302 pr_warning("Internal error: instances.nr is %d\n",
306 prog->instances.nr = -1;
307 zfree(&prog->instances.fds);
309 zclose(prog->btf_fd);
310 zfree(&prog->func_info);
311 zfree(&prog->line_info);
314 static void bpf_program__exit(struct bpf_program *prog)
319 if (prog->clear_priv)
320 prog->clear_priv(prog, prog->priv);
323 prog->clear_priv = NULL;
325 bpf_program__unload(prog);
327 zfree(&prog->section_name);
328 zfree(&prog->pin_name);
330 zfree(&prog->reloc_desc);
337 static char *__bpf_program__pin_name(struct bpf_program *prog)
341 name = p = strdup(prog->section_name);
342 while ((p = strchr(p, '/')))
349 bpf_program__init(void *data, size_t size, char *section_name, int idx,
350 struct bpf_program *prog)
352 if (size < sizeof(struct bpf_insn)) {
353 pr_warning("corrupted section '%s'\n", section_name);
357 memset(prog, 0, sizeof(*prog));
359 prog->section_name = strdup(section_name);
360 if (!prog->section_name) {
361 pr_warning("failed to alloc name for prog under section(%d) %s\n",
366 prog->pin_name = __bpf_program__pin_name(prog);
367 if (!prog->pin_name) {
368 pr_warning("failed to alloc pin name for prog under section(%d) %s\n",
373 prog->insns = malloc(size);
375 pr_warning("failed to alloc insns for prog under section %s\n",
379 prog->insns_cnt = size / sizeof(struct bpf_insn);
380 memcpy(prog->insns, data,
381 prog->insns_cnt * sizeof(struct bpf_insn));
383 prog->instances.fds = NULL;
384 prog->instances.nr = -1;
385 prog->type = BPF_PROG_TYPE_UNSPEC;
390 bpf_program__exit(prog);
395 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
396 char *section_name, int idx)
398 struct bpf_program prog, *progs;
401 err = bpf_program__init(data, size, section_name, idx, &prog);
405 prog.caps = &obj->caps;
406 progs = obj->programs;
407 nr_progs = obj->nr_programs;
409 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
412 * In this case the original obj->programs
413 * is still valid, so don't need special treat for
414 * bpf_close_object().
416 pr_warning("failed to alloc a new program under section '%s'\n",
418 bpf_program__exit(&prog);
422 pr_debug("found program %s\n", prog.section_name);
423 obj->programs = progs;
424 obj->nr_programs = nr_progs + 1;
426 progs[nr_progs] = prog;
431 bpf_object__init_prog_names(struct bpf_object *obj)
433 Elf_Data *symbols = obj->efile.symbols;
434 struct bpf_program *prog;
437 for (pi = 0; pi < obj->nr_programs; pi++) {
438 const char *name = NULL;
440 prog = &obj->programs[pi];
442 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
446 if (!gelf_getsym(symbols, si, &sym))
448 if (sym.st_shndx != prog->idx)
450 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
453 name = elf_strptr(obj->efile.elf,
454 obj->efile.strtabidx,
457 pr_warning("failed to get sym name string for prog %s\n",
459 return -LIBBPF_ERRNO__LIBELF;
463 if (!name && prog->idx == obj->efile.text_shndx)
467 pr_warning("failed to find sym for prog %s\n",
472 prog->name = strdup(name);
474 pr_warning("failed to allocate memory for prog sym %s\n",
483 static struct bpf_object *bpf_object__new(const char *path,
487 struct bpf_object *obj;
490 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
492 pr_warning("alloc memory failed for %s\n", path);
493 return ERR_PTR(-ENOMEM);
496 strcpy(obj->path, path);
497 /* Using basename() GNU version which doesn't modify arg. */
498 strncpy(obj->name, basename((void *)path),
499 sizeof(obj->name) - 1);
500 end = strchr(obj->name, '.');
506 * Caller of this function should also calls
507 * bpf_object__elf_finish() after data collection to return
508 * obj_buf to user. If not, we should duplicate the buffer to
509 * avoid user freeing them before elf finish.
511 obj->efile.obj_buf = obj_buf;
512 obj->efile.obj_buf_sz = obj_buf_sz;
513 obj->efile.maps_shndx = -1;
514 obj->efile.data_shndx = -1;
515 obj->efile.rodata_shndx = -1;
516 obj->efile.bss_shndx = -1;
520 INIT_LIST_HEAD(&obj->list);
521 list_add(&obj->list, &bpf_objects_list);
525 static void bpf_object__elf_finish(struct bpf_object *obj)
527 if (!obj_elf_valid(obj))
530 if (obj->efile.elf) {
531 elf_end(obj->efile.elf);
532 obj->efile.elf = NULL;
534 obj->efile.symbols = NULL;
535 obj->efile.data = NULL;
536 obj->efile.rodata = NULL;
537 obj->efile.bss = NULL;
539 zfree(&obj->efile.reloc);
540 obj->efile.nr_reloc = 0;
541 zclose(obj->efile.fd);
542 obj->efile.obj_buf = NULL;
543 obj->efile.obj_buf_sz = 0;
546 static int bpf_object__elf_init(struct bpf_object *obj)
551 if (obj_elf_valid(obj)) {
552 pr_warning("elf init: internal error\n");
553 return -LIBBPF_ERRNO__LIBELF;
556 if (obj->efile.obj_buf_sz > 0) {
558 * obj_buf should have been validated by
559 * bpf_object__open_buffer().
561 obj->efile.elf = elf_memory(obj->efile.obj_buf,
562 obj->efile.obj_buf_sz);
564 obj->efile.fd = open(obj->path, O_RDONLY);
565 if (obj->efile.fd < 0) {
566 char errmsg[STRERR_BUFSIZE];
567 char *cp = libbpf_strerror_r(errno, errmsg,
570 pr_warning("failed to open %s: %s\n", obj->path, cp);
574 obj->efile.elf = elf_begin(obj->efile.fd,
575 LIBBPF_ELF_C_READ_MMAP,
579 if (!obj->efile.elf) {
580 pr_warning("failed to open %s as ELF file\n",
582 err = -LIBBPF_ERRNO__LIBELF;
586 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
587 pr_warning("failed to get EHDR from %s\n",
589 err = -LIBBPF_ERRNO__FORMAT;
592 ep = &obj->efile.ehdr;
594 /* Old LLVM set e_machine to EM_NONE */
595 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
596 pr_warning("%s is not an eBPF object file\n",
598 err = -LIBBPF_ERRNO__FORMAT;
604 bpf_object__elf_finish(obj);
609 bpf_object__check_endianness(struct bpf_object *obj)
611 static unsigned int const endian = 1;
613 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
615 /* We are big endian, BPF obj is little endian. */
616 if (*(unsigned char const *)&endian != 1)
621 /* We are little endian, BPF obj is big endian. */
622 if (*(unsigned char const *)&endian != 0)
626 return -LIBBPF_ERRNO__ENDIAN;
632 pr_warning("Error: endianness mismatch.\n");
633 return -LIBBPF_ERRNO__ENDIAN;
637 bpf_object__init_license(struct bpf_object *obj,
638 void *data, size_t size)
640 memcpy(obj->license, data,
641 min(size, sizeof(obj->license) - 1));
642 pr_debug("license of %s is %s\n", obj->path, obj->license);
647 bpf_object__init_kversion(struct bpf_object *obj,
648 void *data, size_t size)
652 if (size != sizeof(kver)) {
653 pr_warning("invalid kver section in %s\n", obj->path);
654 return -LIBBPF_ERRNO__FORMAT;
656 memcpy(&kver, data, sizeof(kver));
657 obj->kern_version = kver;
658 pr_debug("kernel version of %s is %x\n", obj->path,
663 static int compare_bpf_map(const void *_a, const void *_b)
665 const struct bpf_map *a = _a;
666 const struct bpf_map *b = _b;
668 return a->offset - b->offset;
671 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
673 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
674 type == BPF_MAP_TYPE_HASH_OF_MAPS)
679 static int bpf_object_search_section_size(const struct bpf_object *obj,
680 const char *name, size_t *d_size)
682 const GElf_Ehdr *ep = &obj->efile.ehdr;
683 Elf *elf = obj->efile.elf;
687 while ((scn = elf_nextscn(elf, scn)) != NULL) {
688 const char *sec_name;
693 if (gelf_getshdr(scn, &sh) != &sh) {
694 pr_warning("failed to get section(%d) header from %s\n",
699 sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
701 pr_warning("failed to get section(%d) name from %s\n",
706 if (strcmp(name, sec_name))
709 data = elf_getdata(scn, 0);
711 pr_warning("failed to get section(%d) data from %s(%s)\n",
712 idx, name, obj->path);
716 *d_size = data->d_size;
723 int bpf_object__section_size(const struct bpf_object *obj, const char *name,
732 } else if (!strcmp(name, ".data")) {
734 *size = obj->efile.data->d_size;
735 } else if (!strcmp(name, ".bss")) {
737 *size = obj->efile.bss->d_size;
738 } else if (!strcmp(name, ".rodata")) {
739 if (obj->efile.rodata)
740 *size = obj->efile.rodata->d_size;
742 ret = bpf_object_search_section_size(obj, name, &d_size);
747 return *size ? 0 : ret;
750 int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
753 Elf_Data *symbols = obj->efile.symbols;
760 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
763 if (!gelf_getsym(symbols, si, &sym))
765 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
766 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
769 sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
772 pr_warning("failed to get sym name string for var %s\n",
776 if (strcmp(name, sname) == 0) {
785 static bool bpf_object__has_maps(const struct bpf_object *obj)
787 return obj->efile.maps_shndx >= 0 ||
788 obj->efile.data_shndx >= 0 ||
789 obj->efile.rodata_shndx >= 0 ||
790 obj->efile.bss_shndx >= 0;
794 bpf_object__init_internal_map(struct bpf_object *obj, struct bpf_map *map,
795 enum libbpf_map_type type, Elf_Data *data,
798 struct bpf_map_def *def = &map->def;
799 char map_name[BPF_OBJ_NAME_LEN];
801 map->libbpf_type = type;
802 map->offset = ~(typeof(map->offset))0;
803 snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name,
804 libbpf_type_to_btf_name[type]);
805 map->name = strdup(map_name);
807 pr_warning("failed to alloc map name\n");
811 def->type = BPF_MAP_TYPE_ARRAY;
812 def->key_size = sizeof(int);
813 def->value_size = data->d_size;
814 def->max_entries = 1;
815 def->map_flags = type == LIBBPF_MAP_RODATA ?
816 BPF_F_RDONLY_PROG : 0;
818 *data_buff = malloc(data->d_size);
821 pr_warning("failed to alloc map content buffer\n");
824 memcpy(*data_buff, data->d_buf, data->d_size);
827 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
832 bpf_object__init_maps(struct bpf_object *obj, int flags)
834 int i, map_idx, map_def_sz = 0, nr_syms, nr_maps = 0, nr_maps_glob = 0;
835 bool strict = !(flags & MAPS_RELAX_COMPAT);
836 Elf_Data *symbols = obj->efile.symbols;
837 Elf_Data *data = NULL;
842 nr_syms = symbols->d_size / sizeof(GElf_Sym);
844 if (obj->efile.maps_shndx >= 0) {
845 Elf_Scn *scn = elf_getscn(obj->efile.elf,
846 obj->efile.maps_shndx);
849 data = elf_getdata(scn, NULL);
851 pr_warning("failed to get Elf_Data from map section %d\n",
852 obj->efile.maps_shndx);
858 * Count number of maps. Each map has a name.
859 * Array of maps is not supported: only the first element is
862 * TODO: Detect array of map and report error.
864 if (obj->caps.global_data) {
865 if (obj->efile.data_shndx >= 0)
867 if (obj->efile.rodata_shndx >= 0)
869 if (obj->efile.bss_shndx >= 0)
873 for (i = 0; data && i < nr_syms; i++) {
876 if (!gelf_getsym(symbols, i, &sym))
878 if (sym.st_shndx != obj->efile.maps_shndx)
883 if (!nr_maps && !nr_maps_glob)
886 /* Assume equally sized map definitions */
888 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
889 nr_maps, data->d_size);
891 map_def_sz = data->d_size / nr_maps;
892 if (!data->d_size || (data->d_size % nr_maps) != 0) {
893 pr_warning("unable to determine map definition size "
894 "section %s, %d maps in %zd bytes\n",
895 obj->path, nr_maps, data->d_size);
900 nr_maps += nr_maps_glob;
901 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
903 pr_warning("alloc maps for object failed\n");
906 obj->nr_maps = nr_maps;
908 for (i = 0; i < nr_maps; i++) {
910 * fill all fd with -1 so won't close incorrect
911 * fd (fd=0 is stdin) when failure (zclose won't close
914 obj->maps[i].fd = -1;
915 obj->maps[i].inner_map_fd = -1;
919 * Fill obj->maps using data in "maps" section.
921 for (i = 0, map_idx = 0; data && i < nr_syms; i++) {
923 const char *map_name;
924 struct bpf_map_def *def;
926 if (!gelf_getsym(symbols, i, &sym))
928 if (sym.st_shndx != obj->efile.maps_shndx)
931 map_name = elf_strptr(obj->efile.elf,
932 obj->efile.strtabidx,
935 obj->maps[map_idx].libbpf_type = LIBBPF_MAP_UNSPEC;
936 obj->maps[map_idx].offset = sym.st_value;
937 if (sym.st_value + map_def_sz > data->d_size) {
938 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
939 obj->path, map_name);
943 obj->maps[map_idx].name = strdup(map_name);
944 if (!obj->maps[map_idx].name) {
945 pr_warning("failed to alloc map name\n");
948 pr_debug("map %d is \"%s\"\n", map_idx,
949 obj->maps[map_idx].name);
950 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
952 * If the definition of the map in the object file fits in
953 * bpf_map_def, copy it. Any extra fields in our version
954 * of bpf_map_def will default to zero as a result of the
957 if (map_def_sz <= sizeof(struct bpf_map_def)) {
958 memcpy(&obj->maps[map_idx].def, def, map_def_sz);
961 * Here the map structure being read is bigger than what
962 * we expect, truncate if the excess bits are all zero.
963 * If they are not zero, reject this map as
967 for (b = ((char *)def) + sizeof(struct bpf_map_def);
968 b < ((char *)def) + map_def_sz; b++) {
970 pr_warning("maps section in %s: \"%s\" "
971 "has unrecognized, non-zero "
973 obj->path, map_name);
978 memcpy(&obj->maps[map_idx].def, def,
979 sizeof(struct bpf_map_def));
984 if (!obj->caps.global_data)
988 * Populate rest of obj->maps with libbpf internal maps.
990 if (obj->efile.data_shndx >= 0)
991 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
994 &obj->sections.data);
995 if (!ret && obj->efile.rodata_shndx >= 0)
996 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
999 &obj->sections.rodata);
1000 if (!ret && obj->efile.bss_shndx >= 0)
1001 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
1003 obj->efile.bss, NULL);
1006 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
1011 static bool section_have_execinstr(struct bpf_object *obj, int idx)
1016 scn = elf_getscn(obj->efile.elf, idx);
1020 if (gelf_getshdr(scn, &sh) != &sh)
1023 if (sh.sh_flags & SHF_EXECINSTR)
1029 static void bpf_object__sanitize_btf(struct bpf_object *obj)
1031 bool has_datasec = obj->caps.btf_datasec;
1032 bool has_func = obj->caps.btf_func;
1033 struct btf *btf = obj->btf;
1038 if (!obj->btf || (has_func && has_datasec))
1041 for (i = 1; i <= btf__get_nr_types(btf); i++) {
1042 t = (struct btf_type *)btf__type_by_id(btf, i);
1043 kind = BTF_INFO_KIND(t->info);
1045 if (!has_datasec && kind == BTF_KIND_VAR) {
1046 /* replace VAR with INT */
1047 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1048 t->size = sizeof(int);
1049 *(int *)(t+1) = BTF_INT_ENC(0, 0, 32);
1050 } else if (!has_datasec && kind == BTF_KIND_DATASEC) {
1051 /* replace DATASEC with STRUCT */
1052 struct btf_var_secinfo *v = (void *)(t + 1);
1053 struct btf_member *m = (void *)(t + 1);
1054 struct btf_type *vt;
1057 name = (char *)btf__name_by_offset(btf, t->name_off);
1064 vlen = BTF_INFO_VLEN(t->info);
1065 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
1066 for (j = 0; j < vlen; j++, v++, m++) {
1067 /* order of field assignments is important */
1068 m->offset = v->offset * 8;
1070 /* preserve variable name as member name */
1071 vt = (void *)btf__type_by_id(btf, v->type);
1072 m->name_off = vt->name_off;
1074 } else if (!has_func && kind == BTF_KIND_FUNC_PROTO) {
1075 /* replace FUNC_PROTO with ENUM */
1076 vlen = BTF_INFO_VLEN(t->info);
1077 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
1078 t->size = sizeof(__u32); /* kernel enforced */
1079 } else if (!has_func && kind == BTF_KIND_FUNC) {
1080 /* replace FUNC with TYPEDEF */
1081 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
1086 static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
1091 if (!obj->caps.btf_func) {
1092 btf_ext__free(obj->btf_ext);
1093 obj->btf_ext = NULL;
1097 static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
1099 Elf *elf = obj->efile.elf;
1100 GElf_Ehdr *ep = &obj->efile.ehdr;
1101 Elf_Data *btf_ext_data = NULL;
1102 Elf_Data *btf_data = NULL;
1103 Elf_Scn *scn = NULL;
1104 int idx = 0, err = 0;
1106 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1107 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
1108 pr_warning("failed to get e_shstrndx from %s\n",
1110 return -LIBBPF_ERRNO__FORMAT;
1113 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1119 if (gelf_getshdr(scn, &sh) != &sh) {
1120 pr_warning("failed to get section(%d) header from %s\n",
1122 err = -LIBBPF_ERRNO__FORMAT;
1126 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1128 pr_warning("failed to get section(%d) name from %s\n",
1130 err = -LIBBPF_ERRNO__FORMAT;
1134 data = elf_getdata(scn, 0);
1136 pr_warning("failed to get section(%d) data from %s(%s)\n",
1137 idx, name, obj->path);
1138 err = -LIBBPF_ERRNO__FORMAT;
1141 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
1142 idx, name, (unsigned long)data->d_size,
1143 (int)sh.sh_link, (unsigned long)sh.sh_flags,
1146 if (strcmp(name, "license") == 0) {
1147 err = bpf_object__init_license(obj,
1150 } else if (strcmp(name, "version") == 0) {
1151 err = bpf_object__init_kversion(obj,
1154 } else if (strcmp(name, "maps") == 0) {
1155 obj->efile.maps_shndx = idx;
1156 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
1158 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
1159 btf_ext_data = data;
1160 } else if (sh.sh_type == SHT_SYMTAB) {
1161 if (obj->efile.symbols) {
1162 pr_warning("bpf: multiple SYMTAB in %s\n",
1164 err = -LIBBPF_ERRNO__FORMAT;
1166 obj->efile.symbols = data;
1167 obj->efile.strtabidx = sh.sh_link;
1169 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
1170 if (sh.sh_flags & SHF_EXECINSTR) {
1171 if (strcmp(name, ".text") == 0)
1172 obj->efile.text_shndx = idx;
1173 err = bpf_object__add_program(obj, data->d_buf,
1174 data->d_size, name, idx);
1176 char errmsg[STRERR_BUFSIZE];
1177 char *cp = libbpf_strerror_r(-err, errmsg,
1180 pr_warning("failed to alloc program %s (%s): %s",
1181 name, obj->path, cp);
1183 } else if (strcmp(name, ".data") == 0) {
1184 obj->efile.data = data;
1185 obj->efile.data_shndx = idx;
1186 } else if (strcmp(name, ".rodata") == 0) {
1187 obj->efile.rodata = data;
1188 obj->efile.rodata_shndx = idx;
1190 pr_debug("skip section(%d) %s\n", idx, name);
1192 } else if (sh.sh_type == SHT_REL) {
1193 void *reloc = obj->efile.reloc;
1194 int nr_reloc = obj->efile.nr_reloc + 1;
1195 int sec = sh.sh_info; /* points to other section */
1197 /* Only do relo for section with exec instructions */
1198 if (!section_have_execinstr(obj, sec)) {
1199 pr_debug("skip relo %s(%d) for section(%d)\n",
1204 reloc = reallocarray(reloc, nr_reloc,
1205 sizeof(*obj->efile.reloc));
1207 pr_warning("realloc failed\n");
1210 int n = nr_reloc - 1;
1212 obj->efile.reloc = reloc;
1213 obj->efile.nr_reloc = nr_reloc;
1215 obj->efile.reloc[n].shdr = sh;
1216 obj->efile.reloc[n].data = data;
1218 } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
1219 obj->efile.bss = data;
1220 obj->efile.bss_shndx = idx;
1222 pr_debug("skip section(%d) %s\n", idx, name);
1228 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
1229 pr_warning("Corrupted ELF file: index of strtab invalid\n");
1230 return LIBBPF_ERRNO__FORMAT;
1233 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
1234 if (IS_ERR(obj->btf)) {
1235 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1236 BTF_ELF_SEC, PTR_ERR(obj->btf));
1239 err = btf__finalize_data(obj, obj->btf);
1241 bpf_object__sanitize_btf(obj);
1242 err = btf__load(obj->btf);
1245 pr_warning("Error finalizing and loading %s into kernel: %d. Ignored and continue.\n",
1247 btf__free(obj->btf);
1255 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
1256 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
1258 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
1259 btf_ext_data->d_size);
1260 if (IS_ERR(obj->btf_ext)) {
1261 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1263 PTR_ERR(obj->btf_ext));
1264 obj->btf_ext = NULL;
1266 bpf_object__sanitize_btf_ext(obj);
1270 if (bpf_object__has_maps(obj)) {
1271 err = bpf_object__init_maps(obj, flags);
1275 err = bpf_object__init_prog_names(obj);
1280 static struct bpf_program *
1281 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
1283 struct bpf_program *prog;
1286 for (i = 0; i < obj->nr_programs; i++) {
1287 prog = &obj->programs[i];
1288 if (prog->idx == idx)
1294 struct bpf_program *
1295 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title)
1297 struct bpf_program *pos;
1299 bpf_object__for_each_program(pos, obj) {
1300 if (pos->section_name && !strcmp(pos->section_name, title))
1306 static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
1309 return shndx == obj->efile.data_shndx ||
1310 shndx == obj->efile.bss_shndx ||
1311 shndx == obj->efile.rodata_shndx;
1314 static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
1317 return shndx == obj->efile.maps_shndx;
1320 static bool bpf_object__relo_in_known_section(const struct bpf_object *obj,
1323 return shndx == obj->efile.text_shndx ||
1324 bpf_object__shndx_is_maps(obj, shndx) ||
1325 bpf_object__shndx_is_data(obj, shndx);
1328 static enum libbpf_map_type
1329 bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
1331 if (shndx == obj->efile.data_shndx)
1332 return LIBBPF_MAP_DATA;
1333 else if (shndx == obj->efile.bss_shndx)
1334 return LIBBPF_MAP_BSS;
1335 else if (shndx == obj->efile.rodata_shndx)
1336 return LIBBPF_MAP_RODATA;
1338 return LIBBPF_MAP_UNSPEC;
1342 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
1343 Elf_Data *data, struct bpf_object *obj)
1345 Elf_Data *symbols = obj->efile.symbols;
1346 struct bpf_map *maps = obj->maps;
1347 size_t nr_maps = obj->nr_maps;
1350 pr_debug("collecting relocating info for: '%s'\n",
1351 prog->section_name);
1352 nrels = shdr->sh_size / shdr->sh_entsize;
1354 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
1355 if (!prog->reloc_desc) {
1356 pr_warning("failed to alloc memory in relocation\n");
1359 prog->nr_reloc = nrels;
1361 for (i = 0; i < nrels; i++) {
1364 unsigned int insn_idx;
1365 unsigned int shdr_idx;
1366 struct bpf_insn *insns = prog->insns;
1367 enum libbpf_map_type type;
1371 if (!gelf_getrel(data, i, &rel)) {
1372 pr_warning("relocation: failed to get %d reloc\n", i);
1373 return -LIBBPF_ERRNO__FORMAT;
1376 if (!gelf_getsym(symbols,
1377 GELF_R_SYM(rel.r_info),
1379 pr_warning("relocation: symbol %"PRIx64" not found\n",
1380 GELF_R_SYM(rel.r_info));
1381 return -LIBBPF_ERRNO__FORMAT;
1384 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1385 sym.st_name) ? : "<?>";
1387 pr_debug("relo for %lld value %lld name %d (\'%s\')\n",
1388 (long long) (rel.r_info >> 32),
1389 (long long) sym.st_value, sym.st_name, name);
1391 shdr_idx = sym.st_shndx;
1392 if (!bpf_object__relo_in_known_section(obj, shdr_idx)) {
1393 pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n",
1394 prog->section_name, shdr_idx);
1395 return -LIBBPF_ERRNO__RELOC;
1398 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
1399 pr_debug("relocation: insn_idx=%u\n", insn_idx);
1401 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
1402 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
1403 pr_warning("incorrect bpf_call opcode\n");
1404 return -LIBBPF_ERRNO__RELOC;
1406 prog->reloc_desc[i].type = RELO_CALL;
1407 prog->reloc_desc[i].insn_idx = insn_idx;
1408 prog->reloc_desc[i].text_off = sym.st_value;
1409 obj->has_pseudo_calls = true;
1413 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
1414 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
1415 insn_idx, insns[insn_idx].code);
1416 return -LIBBPF_ERRNO__RELOC;
1419 if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
1420 bpf_object__shndx_is_data(obj, shdr_idx)) {
1421 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
1422 if (type != LIBBPF_MAP_UNSPEC) {
1423 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
1424 pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1425 name, insn_idx, insns[insn_idx].code);
1426 return -LIBBPF_ERRNO__RELOC;
1428 if (!obj->caps.global_data) {
1429 pr_warning("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
1431 return -LIBBPF_ERRNO__RELOC;
1435 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
1436 if (maps[map_idx].libbpf_type != type)
1438 if (type != LIBBPF_MAP_UNSPEC ||
1439 (type == LIBBPF_MAP_UNSPEC &&
1440 maps[map_idx].offset == sym.st_value)) {
1441 pr_debug("relocation: find map %zd (%s) for insn %u\n",
1442 map_idx, maps[map_idx].name, insn_idx);
1447 if (map_idx >= nr_maps) {
1448 pr_warning("bpf relocation: map_idx %d large than %d\n",
1449 (int)map_idx, (int)nr_maps - 1);
1450 return -LIBBPF_ERRNO__RELOC;
1453 prog->reloc_desc[i].type = type != LIBBPF_MAP_UNSPEC ?
1454 RELO_DATA : RELO_LD64;
1455 prog->reloc_desc[i].insn_idx = insn_idx;
1456 prog->reloc_desc[i].map_idx = map_idx;
1462 static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
1464 struct bpf_map_def *def = &map->def;
1465 __u32 key_type_id = 0, value_type_id = 0;
1468 if (!bpf_map__is_internal(map)) {
1469 ret = btf__get_map_kv_tids(btf, map->name, def->key_size,
1470 def->value_size, &key_type_id,
1474 * LLVM annotates global data differently in BTF, that is,
1475 * only as '.data', '.bss' or '.rodata'.
1477 ret = btf__find_by_name(btf,
1478 libbpf_type_to_btf_name[map->libbpf_type]);
1483 map->btf_key_type_id = key_type_id;
1484 map->btf_value_type_id = bpf_map__is_internal(map) ?
1485 ret : value_type_id;
1489 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1491 struct bpf_map_info info = {};
1492 __u32 len = sizeof(info);
1496 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1500 new_name = strdup(info.name);
1504 new_fd = open("/", O_RDONLY | O_CLOEXEC);
1506 goto err_free_new_name;
1508 new_fd = dup3(fd, new_fd, O_CLOEXEC);
1510 goto err_close_new_fd;
1512 err = zclose(map->fd);
1514 goto err_close_new_fd;
1518 map->name = new_name;
1519 map->def.type = info.type;
1520 map->def.key_size = info.key_size;
1521 map->def.value_size = info.value_size;
1522 map->def.max_entries = info.max_entries;
1523 map->def.map_flags = info.map_flags;
1524 map->btf_key_type_id = info.btf_key_type_id;
1525 map->btf_value_type_id = info.btf_value_type_id;
1536 int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
1538 if (!map || !max_entries)
1541 /* If map already created, its attributes can't be changed. */
1545 map->def.max_entries = max_entries;
1551 bpf_object__probe_name(struct bpf_object *obj)
1553 struct bpf_load_program_attr attr;
1554 char *cp, errmsg[STRERR_BUFSIZE];
1555 struct bpf_insn insns[] = {
1556 BPF_MOV64_IMM(BPF_REG_0, 0),
1561 /* make sure basic loading works */
1563 memset(&attr, 0, sizeof(attr));
1564 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1566 attr.insns_cnt = ARRAY_SIZE(insns);
1567 attr.license = "GPL";
1569 ret = bpf_load_program_xattr(&attr, NULL, 0);
1571 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1572 pr_warning("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
1573 __func__, cp, errno);
1578 /* now try the same program, but with the name */
1581 ret = bpf_load_program_xattr(&attr, NULL, 0);
1591 bpf_object__probe_global_data(struct bpf_object *obj)
1593 struct bpf_load_program_attr prg_attr;
1594 struct bpf_create_map_attr map_attr;
1595 char *cp, errmsg[STRERR_BUFSIZE];
1596 struct bpf_insn insns[] = {
1597 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
1598 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
1599 BPF_MOV64_IMM(BPF_REG_0, 0),
1604 memset(&map_attr, 0, sizeof(map_attr));
1605 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
1606 map_attr.key_size = sizeof(int);
1607 map_attr.value_size = 32;
1608 map_attr.max_entries = 1;
1610 map = bpf_create_map_xattr(&map_attr);
1612 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1613 pr_warning("Error in %s():%s(%d). Couldn't create simple array map.\n",
1614 __func__, cp, errno);
1620 memset(&prg_attr, 0, sizeof(prg_attr));
1621 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1622 prg_attr.insns = insns;
1623 prg_attr.insns_cnt = ARRAY_SIZE(insns);
1624 prg_attr.license = "GPL";
1626 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
1628 obj->caps.global_data = 1;
1636 static int bpf_object__probe_btf_func(struct bpf_object *obj)
1638 const char strs[] = "\0int\0x\0a";
1639 /* void x(int a) {} */
1642 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1643 /* FUNC_PROTO */ /* [2] */
1644 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
1645 BTF_PARAM_ENC(7, 1),
1646 /* FUNC x */ /* [3] */
1647 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
1651 res = libbpf__probe_raw_btf((char *)types, sizeof(types),
1652 strs, sizeof(strs));
1656 obj->caps.btf_func = 1;
1660 static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
1662 const char strs[] = "\0x\0.data";
1666 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1667 /* VAR x */ /* [2] */
1668 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
1670 /* DATASEC val */ /* [3] */
1671 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
1672 BTF_VAR_SECINFO_ENC(2, 0, 4),
1676 res = libbpf__probe_raw_btf((char *)types, sizeof(types),
1677 strs, sizeof(strs));
1681 obj->caps.btf_datasec = 1;
1686 bpf_object__probe_caps(struct bpf_object *obj)
1688 int (*probe_fn[])(struct bpf_object *obj) = {
1689 bpf_object__probe_name,
1690 bpf_object__probe_global_data,
1691 bpf_object__probe_btf_func,
1692 bpf_object__probe_btf_datasec,
1696 for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
1697 ret = probe_fn[i](obj);
1699 pr_debug("Probe #%d failed with %d.\n", i, ret);
1706 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
1708 char *cp, errmsg[STRERR_BUFSIZE];
1712 /* Nothing to do here since kernel already zero-initializes .bss map. */
1713 if (map->libbpf_type == LIBBPF_MAP_BSS)
1716 data = map->libbpf_type == LIBBPF_MAP_DATA ?
1717 obj->sections.data : obj->sections.rodata;
1719 err = bpf_map_update_elem(map->fd, &zero, data, 0);
1720 /* Freeze .rodata map as read-only from syscall side. */
1721 if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) {
1722 err = bpf_map_freeze(map->fd);
1724 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1725 pr_warning("Error freezing map(%s) as read-only: %s\n",
1734 bpf_object__create_maps(struct bpf_object *obj)
1736 struct bpf_create_map_attr create_attr = {};
1740 for (i = 0; i < obj->nr_maps; i++) {
1741 struct bpf_map *map = &obj->maps[i];
1742 struct bpf_map_def *def = &map->def;
1743 char *cp, errmsg[STRERR_BUFSIZE];
1744 int *pfd = &map->fd;
1747 pr_debug("skip map create (preset) %s: fd=%d\n",
1748 map->name, map->fd);
1753 create_attr.name = map->name;
1754 create_attr.map_ifindex = map->map_ifindex;
1755 create_attr.map_type = def->type;
1756 create_attr.map_flags = def->map_flags;
1757 create_attr.key_size = def->key_size;
1758 create_attr.value_size = def->value_size;
1759 create_attr.max_entries = def->max_entries;
1760 create_attr.btf_fd = 0;
1761 create_attr.btf_key_type_id = 0;
1762 create_attr.btf_value_type_id = 0;
1763 if (bpf_map_type__is_map_in_map(def->type) &&
1764 map->inner_map_fd >= 0)
1765 create_attr.inner_map_fd = map->inner_map_fd;
1767 if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1768 create_attr.btf_fd = btf__fd(obj->btf);
1769 create_attr.btf_key_type_id = map->btf_key_type_id;
1770 create_attr.btf_value_type_id = map->btf_value_type_id;
1773 *pfd = bpf_create_map_xattr(&create_attr);
1774 if (*pfd < 0 && create_attr.btf_key_type_id) {
1775 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1776 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1777 map->name, cp, errno);
1778 create_attr.btf_fd = 0;
1779 create_attr.btf_key_type_id = 0;
1780 create_attr.btf_value_type_id = 0;
1781 map->btf_key_type_id = 0;
1782 map->btf_value_type_id = 0;
1783 *pfd = bpf_create_map_xattr(&create_attr);
1791 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1792 pr_warning("failed to create map (name: '%s'): %s\n",
1794 for (j = 0; j < i; j++)
1795 zclose(obj->maps[j].fd);
1799 if (bpf_map__is_internal(map)) {
1800 err = bpf_object__populate_internal_map(obj, map);
1807 pr_debug("create map %s: fd=%d\n", map->name, *pfd);
1814 check_btf_ext_reloc_err(struct bpf_program *prog, int err,
1815 void *btf_prog_info, const char *info_name)
1817 if (err != -ENOENT) {
1818 pr_warning("Error in loading %s for sec %s.\n",
1819 info_name, prog->section_name);
1823 /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
1825 if (btf_prog_info) {
1827 * Some info has already been found but has problem
1828 * in the last btf_ext reloc. Must have to error
1831 pr_warning("Error in relocating %s for sec %s.\n",
1832 info_name, prog->section_name);
1837 * Have problem loading the very first info. Ignore
1840 pr_warning("Cannot find %s for main program sec %s. Ignore all %s.\n",
1841 info_name, prog->section_name, info_name);
1846 bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
1847 const char *section_name, __u32 insn_offset)
1851 if (!insn_offset || prog->func_info) {
1853 * !insn_offset => main program
1855 * For sub prog, the main program's func_info has to
1856 * be loaded first (i.e. prog->func_info != NULL)
1858 err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
1859 section_name, insn_offset,
1861 &prog->func_info_cnt);
1863 return check_btf_ext_reloc_err(prog, err,
1867 prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
1870 if (!insn_offset || prog->line_info) {
1871 err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
1872 section_name, insn_offset,
1874 &prog->line_info_cnt);
1876 return check_btf_ext_reloc_err(prog, err,
1880 prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
1884 prog->btf_fd = btf__fd(obj->btf);
1890 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1891 struct reloc_desc *relo)
1893 struct bpf_insn *insn, *new_insn;
1894 struct bpf_program *text;
1898 if (relo->type != RELO_CALL)
1899 return -LIBBPF_ERRNO__RELOC;
1901 if (prog->idx == obj->efile.text_shndx) {
1902 pr_warning("relo in .text insn %d into off %d\n",
1903 relo->insn_idx, relo->text_off);
1904 return -LIBBPF_ERRNO__RELOC;
1907 if (prog->main_prog_cnt == 0) {
1908 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1910 pr_warning("no .text section found yet relo into text exist\n");
1911 return -LIBBPF_ERRNO__RELOC;
1913 new_cnt = prog->insns_cnt + text->insns_cnt;
1914 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
1916 pr_warning("oom in prog realloc\n");
1921 err = bpf_program_reloc_btf_ext(prog, obj,
1928 memcpy(new_insn + prog->insns_cnt, text->insns,
1929 text->insns_cnt * sizeof(*insn));
1930 prog->insns = new_insn;
1931 prog->main_prog_cnt = prog->insns_cnt;
1932 prog->insns_cnt = new_cnt;
1933 pr_debug("added %zd insn from %s to prog %s\n",
1934 text->insns_cnt, text->section_name,
1935 prog->section_name);
1937 insn = &prog->insns[relo->insn_idx];
1938 insn->imm += prog->main_prog_cnt - relo->insn_idx;
1943 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
1951 err = bpf_program_reloc_btf_ext(prog, obj,
1952 prog->section_name, 0);
1957 if (!prog->reloc_desc)
1960 for (i = 0; i < prog->nr_reloc; i++) {
1961 if (prog->reloc_desc[i].type == RELO_LD64 ||
1962 prog->reloc_desc[i].type == RELO_DATA) {
1963 bool relo_data = prog->reloc_desc[i].type == RELO_DATA;
1964 struct bpf_insn *insns = prog->insns;
1965 int insn_idx, map_idx;
1967 insn_idx = prog->reloc_desc[i].insn_idx;
1968 map_idx = prog->reloc_desc[i].map_idx;
1970 if (insn_idx + 1 >= (int)prog->insns_cnt) {
1971 pr_warning("relocation out of range: '%s'\n",
1972 prog->section_name);
1973 return -LIBBPF_ERRNO__RELOC;
1977 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1979 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE;
1980 insns[insn_idx + 1].imm = insns[insn_idx].imm;
1982 insns[insn_idx].imm = obj->maps[map_idx].fd;
1983 } else if (prog->reloc_desc[i].type == RELO_CALL) {
1984 err = bpf_program__reloc_text(prog, obj,
1985 &prog->reloc_desc[i]);
1991 zfree(&prog->reloc_desc);
1998 bpf_object__relocate(struct bpf_object *obj)
2000 struct bpf_program *prog;
2004 for (i = 0; i < obj->nr_programs; i++) {
2005 prog = &obj->programs[i];
2007 err = bpf_program__relocate(prog, obj);
2009 pr_warning("failed to relocate '%s'\n",
2010 prog->section_name);
2017 static int bpf_object__collect_reloc(struct bpf_object *obj)
2021 if (!obj_elf_valid(obj)) {
2022 pr_warning("Internal error: elf object is closed\n");
2023 return -LIBBPF_ERRNO__INTERNAL;
2026 for (i = 0; i < obj->efile.nr_reloc; i++) {
2027 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
2028 Elf_Data *data = obj->efile.reloc[i].data;
2029 int idx = shdr->sh_info;
2030 struct bpf_program *prog;
2032 if (shdr->sh_type != SHT_REL) {
2033 pr_warning("internal error at %d\n", __LINE__);
2034 return -LIBBPF_ERRNO__INTERNAL;
2037 prog = bpf_object__find_prog_by_idx(obj, idx);
2039 pr_warning("relocation failed: no section(%d)\n", idx);
2040 return -LIBBPF_ERRNO__RELOC;
2043 err = bpf_program__collect_reloc(prog,
2053 load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
2054 char *license, __u32 kern_version, int *pfd)
2056 struct bpf_load_program_attr load_attr;
2057 char *cp, errmsg[STRERR_BUFSIZE];
2058 int log_buf_size = BPF_LOG_BUF_SIZE;
2062 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
2063 load_attr.prog_type = prog->type;
2064 load_attr.expected_attach_type = prog->expected_attach_type;
2065 if (prog->caps->name)
2066 load_attr.name = prog->name;
2067 load_attr.insns = insns;
2068 load_attr.insns_cnt = insns_cnt;
2069 load_attr.license = license;
2070 load_attr.kern_version = kern_version;
2071 load_attr.prog_ifindex = prog->prog_ifindex;
2072 load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0;
2073 load_attr.func_info = prog->func_info;
2074 load_attr.func_info_rec_size = prog->func_info_rec_size;
2075 load_attr.func_info_cnt = prog->func_info_cnt;
2076 load_attr.line_info = prog->line_info;
2077 load_attr.line_info_rec_size = prog->line_info_rec_size;
2078 load_attr.line_info_cnt = prog->line_info_cnt;
2079 load_attr.log_level = prog->log_level;
2080 if (!load_attr.insns || !load_attr.insns_cnt)
2084 log_buf = malloc(log_buf_size);
2086 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
2088 ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
2091 if (load_attr.log_level)
2092 pr_debug("verifier log:\n%s", log_buf);
2098 if (errno == ENOSPC) {
2103 ret = -LIBBPF_ERRNO__LOAD;
2104 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2105 pr_warning("load bpf program failed: %s\n", cp);
2107 if (log_buf && log_buf[0] != '\0') {
2108 ret = -LIBBPF_ERRNO__VERIFY;
2109 pr_warning("-- BEGIN DUMP LOG ---\n");
2110 pr_warning("\n%s\n", log_buf);
2111 pr_warning("-- END LOG --\n");
2112 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
2113 pr_warning("Program too large (%zu insns), at most %d insns\n",
2114 load_attr.insns_cnt, BPF_MAXINSNS);
2115 ret = -LIBBPF_ERRNO__PROG2BIG;
2117 /* Wrong program type? */
2118 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
2121 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
2122 load_attr.expected_attach_type = 0;
2123 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
2126 ret = -LIBBPF_ERRNO__PROGTYPE;
2132 ret = -LIBBPF_ERRNO__KVER;
2141 bpf_program__load(struct bpf_program *prog,
2142 char *license, __u32 kern_version)
2146 if (prog->instances.nr < 0 || !prog->instances.fds) {
2147 if (prog->preprocessor) {
2148 pr_warning("Internal error: can't load program '%s'\n",
2149 prog->section_name);
2150 return -LIBBPF_ERRNO__INTERNAL;
2153 prog->instances.fds = malloc(sizeof(int));
2154 if (!prog->instances.fds) {
2155 pr_warning("Not enough memory for BPF fds\n");
2158 prog->instances.nr = 1;
2159 prog->instances.fds[0] = -1;
2162 if (!prog->preprocessor) {
2163 if (prog->instances.nr != 1) {
2164 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
2165 prog->section_name, prog->instances.nr);
2167 err = load_program(prog, prog->insns, prog->insns_cnt,
2168 license, kern_version, &fd);
2170 prog->instances.fds[0] = fd;
2174 for (i = 0; i < prog->instances.nr; i++) {
2175 struct bpf_prog_prep_result result;
2176 bpf_program_prep_t preprocessor = prog->preprocessor;
2178 memset(&result, 0, sizeof(result));
2179 err = preprocessor(prog, i, prog->insns,
2180 prog->insns_cnt, &result);
2182 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
2183 i, prog->section_name);
2187 if (!result.new_insn_ptr || !result.new_insn_cnt) {
2188 pr_debug("Skip loading the %dth instance of program '%s'\n",
2189 i, prog->section_name);
2190 prog->instances.fds[i] = -1;
2196 err = load_program(prog, result.new_insn_ptr,
2197 result.new_insn_cnt,
2198 license, kern_version, &fd);
2201 pr_warning("Loading the %dth instance of program '%s' failed\n",
2202 i, prog->section_name);
2208 prog->instances.fds[i] = fd;
2212 pr_warning("failed to load program '%s'\n",
2213 prog->section_name);
2214 zfree(&prog->insns);
2215 prog->insns_cnt = 0;
2219 static bool bpf_program__is_function_storage(struct bpf_program *prog,
2220 struct bpf_object *obj)
2222 return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
2226 bpf_object__load_progs(struct bpf_object *obj)
2231 for (i = 0; i < obj->nr_programs; i++) {
2232 if (bpf_program__is_function_storage(&obj->programs[i], obj))
2234 err = bpf_program__load(&obj->programs[i],
2243 static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
2246 case BPF_PROG_TYPE_SOCKET_FILTER:
2247 case BPF_PROG_TYPE_SCHED_CLS:
2248 case BPF_PROG_TYPE_SCHED_ACT:
2249 case BPF_PROG_TYPE_XDP:
2250 case BPF_PROG_TYPE_CGROUP_SKB:
2251 case BPF_PROG_TYPE_CGROUP_SOCK:
2252 case BPF_PROG_TYPE_LWT_IN:
2253 case BPF_PROG_TYPE_LWT_OUT:
2254 case BPF_PROG_TYPE_LWT_XMIT:
2255 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2256 case BPF_PROG_TYPE_SOCK_OPS:
2257 case BPF_PROG_TYPE_SK_SKB:
2258 case BPF_PROG_TYPE_CGROUP_DEVICE:
2259 case BPF_PROG_TYPE_SK_MSG:
2260 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2261 case BPF_PROG_TYPE_LIRC_MODE2:
2262 case BPF_PROG_TYPE_SK_REUSEPORT:
2263 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2264 case BPF_PROG_TYPE_UNSPEC:
2265 case BPF_PROG_TYPE_TRACEPOINT:
2266 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2267 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2268 case BPF_PROG_TYPE_PERF_EVENT:
2269 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2271 case BPF_PROG_TYPE_KPROBE:
2277 static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
2279 if (needs_kver && obj->kern_version == 0) {
2280 pr_warning("%s doesn't provide kernel version\n",
2282 return -LIBBPF_ERRNO__KVERSION;
2287 static struct bpf_object *
2288 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
2289 bool needs_kver, int flags)
2291 struct bpf_object *obj;
2294 if (elf_version(EV_CURRENT) == EV_NONE) {
2295 pr_warning("failed to init libelf for %s\n", path);
2296 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
2299 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
2303 CHECK_ERR(bpf_object__elf_init(obj), err, out);
2304 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
2305 CHECK_ERR(bpf_object__probe_caps(obj), err, out);
2306 CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
2307 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
2308 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
2310 bpf_object__elf_finish(obj);
2313 bpf_object__close(obj);
2314 return ERR_PTR(err);
2317 struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
2320 /* param validation */
2324 pr_debug("loading %s\n", attr->file);
2326 return __bpf_object__open(attr->file, NULL, 0,
2327 bpf_prog_type__needs_kver(attr->prog_type),
2331 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
2333 return __bpf_object__open_xattr(attr, 0);
2336 struct bpf_object *bpf_object__open(const char *path)
2338 struct bpf_object_open_attr attr = {
2340 .prog_type = BPF_PROG_TYPE_UNSPEC,
2343 return bpf_object__open_xattr(&attr);
2346 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
2352 /* param validation */
2353 if (!obj_buf || obj_buf_sz <= 0)
2357 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
2358 (unsigned long)obj_buf,
2359 (unsigned long)obj_buf_sz);
2360 tmp_name[sizeof(tmp_name) - 1] = '\0';
2363 pr_debug("loading object '%s' from buffer\n",
2366 return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
2369 int bpf_object__unload(struct bpf_object *obj)
2376 for (i = 0; i < obj->nr_maps; i++)
2377 zclose(obj->maps[i].fd);
2379 for (i = 0; i < obj->nr_programs; i++)
2380 bpf_program__unload(&obj->programs[i]);
2385 int bpf_object__load(struct bpf_object *obj)
2393 pr_warning("object should not be loaded twice\n");
2399 CHECK_ERR(bpf_object__create_maps(obj), err, out);
2400 CHECK_ERR(bpf_object__relocate(obj), err, out);
2401 CHECK_ERR(bpf_object__load_progs(obj), err, out);
2405 bpf_object__unload(obj);
2406 pr_warning("failed to load object '%s'\n", obj->path);
2410 static int check_path(const char *path)
2412 char *cp, errmsg[STRERR_BUFSIZE];
2413 struct statfs st_fs;
2420 dname = strdup(path);
2424 dir = dirname(dname);
2425 if (statfs(dir, &st_fs)) {
2426 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2427 pr_warning("failed to statfs %s: %s\n", dir, cp);
2432 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
2433 pr_warning("specified path %s is not on BPF FS\n", path);
2440 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
2443 char *cp, errmsg[STRERR_BUFSIZE];
2446 err = check_path(path);
2451 pr_warning("invalid program pointer\n");
2455 if (instance < 0 || instance >= prog->instances.nr) {
2456 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2457 instance, prog->section_name, prog->instances.nr);
2461 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
2462 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2463 pr_warning("failed to pin program: %s\n", cp);
2466 pr_debug("pinned program '%s'\n", path);
2471 int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
2476 err = check_path(path);
2481 pr_warning("invalid program pointer\n");
2485 if (instance < 0 || instance >= prog->instances.nr) {
2486 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2487 instance, prog->section_name, prog->instances.nr);
2494 pr_debug("unpinned program '%s'\n", path);
2499 static int make_dir(const char *path)
2501 char *cp, errmsg[STRERR_BUFSIZE];
2504 if (mkdir(path, 0700) && errno != EEXIST)
2508 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
2509 pr_warning("failed to mkdir %s: %s\n", path, cp);
2514 int bpf_program__pin(struct bpf_program *prog, const char *path)
2518 err = check_path(path);
2523 pr_warning("invalid program pointer\n");
2527 if (prog->instances.nr <= 0) {
2528 pr_warning("no instances of prog %s to pin\n",
2529 prog->section_name);
2533 if (prog->instances.nr == 1) {
2534 /* don't create subdirs when pinning single instance */
2535 return bpf_program__pin_instance(prog, path, 0);
2538 err = make_dir(path);
2542 for (i = 0; i < prog->instances.nr; i++) {
2546 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
2550 } else if (len >= PATH_MAX) {
2551 err = -ENAMETOOLONG;
2555 err = bpf_program__pin_instance(prog, buf, i);
2563 for (i = i - 1; i >= 0; i--) {
2567 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
2570 else if (len >= PATH_MAX)
2573 bpf_program__unpin_instance(prog, buf, i);
2581 int bpf_program__unpin(struct bpf_program *prog, const char *path)
2585 err = check_path(path);
2590 pr_warning("invalid program pointer\n");
2594 if (prog->instances.nr <= 0) {
2595 pr_warning("no instances of prog %s to pin\n",
2596 prog->section_name);
2600 if (prog->instances.nr == 1) {
2601 /* don't create subdirs when pinning single instance */
2602 return bpf_program__unpin_instance(prog, path, 0);
2605 for (i = 0; i < prog->instances.nr; i++) {
2609 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
2612 else if (len >= PATH_MAX)
2613 return -ENAMETOOLONG;
2615 err = bpf_program__unpin_instance(prog, buf, i);
2627 int bpf_map__pin(struct bpf_map *map, const char *path)
2629 char *cp, errmsg[STRERR_BUFSIZE];
2632 err = check_path(path);
2637 pr_warning("invalid map pointer\n");
2641 if (bpf_obj_pin(map->fd, path)) {
2642 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2643 pr_warning("failed to pin map: %s\n", cp);
2647 pr_debug("pinned map '%s'\n", path);
2652 int bpf_map__unpin(struct bpf_map *map, const char *path)
2656 err = check_path(path);
2661 pr_warning("invalid map pointer\n");
2668 pr_debug("unpinned map '%s'\n", path);
2673 int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
2675 struct bpf_map *map;
2682 pr_warning("object not yet loaded; load it first\n");
2686 err = make_dir(path);
2690 bpf_object__for_each_map(map, obj) {
2694 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2695 bpf_map__name(map));
2698 goto err_unpin_maps;
2699 } else if (len >= PATH_MAX) {
2700 err = -ENAMETOOLONG;
2701 goto err_unpin_maps;
2704 err = bpf_map__pin(map, buf);
2706 goto err_unpin_maps;
2712 while ((map = bpf_map__prev(map, obj))) {
2716 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2717 bpf_map__name(map));
2720 else if (len >= PATH_MAX)
2723 bpf_map__unpin(map, buf);
2729 int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
2731 struct bpf_map *map;
2737 bpf_object__for_each_map(map, obj) {
2741 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2742 bpf_map__name(map));
2745 else if (len >= PATH_MAX)
2746 return -ENAMETOOLONG;
2748 err = bpf_map__unpin(map, buf);
2756 int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
2758 struct bpf_program *prog;
2765 pr_warning("object not yet loaded; load it first\n");
2769 err = make_dir(path);
2773 bpf_object__for_each_program(prog, obj) {
2777 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2781 goto err_unpin_programs;
2782 } else if (len >= PATH_MAX) {
2783 err = -ENAMETOOLONG;
2784 goto err_unpin_programs;
2787 err = bpf_program__pin(prog, buf);
2789 goto err_unpin_programs;
2795 while ((prog = bpf_program__prev(prog, obj))) {
2799 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2803 else if (len >= PATH_MAX)
2806 bpf_program__unpin(prog, buf);
2812 int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
2814 struct bpf_program *prog;
2820 bpf_object__for_each_program(prog, obj) {
2824 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2828 else if (len >= PATH_MAX)
2829 return -ENAMETOOLONG;
2831 err = bpf_program__unpin(prog, buf);
2839 int bpf_object__pin(struct bpf_object *obj, const char *path)
2843 err = bpf_object__pin_maps(obj, path);
2847 err = bpf_object__pin_programs(obj, path);
2849 bpf_object__unpin_maps(obj, path);
2856 void bpf_object__close(struct bpf_object *obj)
2863 if (obj->clear_priv)
2864 obj->clear_priv(obj, obj->priv);
2866 bpf_object__elf_finish(obj);
2867 bpf_object__unload(obj);
2868 btf__free(obj->btf);
2869 btf_ext__free(obj->btf_ext);
2871 for (i = 0; i < obj->nr_maps; i++) {
2872 zfree(&obj->maps[i].name);
2873 if (obj->maps[i].clear_priv)
2874 obj->maps[i].clear_priv(&obj->maps[i],
2876 obj->maps[i].priv = NULL;
2877 obj->maps[i].clear_priv = NULL;
2880 zfree(&obj->sections.rodata);
2881 zfree(&obj->sections.data);
2885 if (obj->programs && obj->nr_programs) {
2886 for (i = 0; i < obj->nr_programs; i++)
2887 bpf_program__exit(&obj->programs[i]);
2889 zfree(&obj->programs);
2891 list_del(&obj->list);
2896 bpf_object__next(struct bpf_object *prev)
2898 struct bpf_object *next;
2901 next = list_first_entry(&bpf_objects_list,
2905 next = list_next_entry(prev, list);
2907 /* Empty list is noticed here so don't need checking on entry. */
2908 if (&next->list == &bpf_objects_list)
2914 const char *bpf_object__name(struct bpf_object *obj)
2916 return obj ? obj->path : ERR_PTR(-EINVAL);
2919 unsigned int bpf_object__kversion(struct bpf_object *obj)
2921 return obj ? obj->kern_version : 0;
2924 struct btf *bpf_object__btf(struct bpf_object *obj)
2926 return obj ? obj->btf : NULL;
2929 int bpf_object__btf_fd(const struct bpf_object *obj)
2931 return obj->btf ? btf__fd(obj->btf) : -1;
2934 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
2935 bpf_object_clear_priv_t clear_priv)
2937 if (obj->priv && obj->clear_priv)
2938 obj->clear_priv(obj, obj->priv);
2941 obj->clear_priv = clear_priv;
2945 void *bpf_object__priv(struct bpf_object *obj)
2947 return obj ? obj->priv : ERR_PTR(-EINVAL);
2950 static struct bpf_program *
2951 __bpf_program__iter(struct bpf_program *p, struct bpf_object *obj, bool forward)
2953 size_t nr_programs = obj->nr_programs;
2960 /* Iter from the beginning */
2961 return forward ? &obj->programs[0] :
2962 &obj->programs[nr_programs - 1];
2964 if (p->obj != obj) {
2965 pr_warning("error: program handler doesn't match object\n");
2969 idx = (p - obj->programs) + (forward ? 1 : -1);
2970 if (idx >= obj->nr_programs || idx < 0)
2972 return &obj->programs[idx];
2975 struct bpf_program *
2976 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
2978 struct bpf_program *prog = prev;
2981 prog = __bpf_program__iter(prog, obj, true);
2982 } while (prog && bpf_program__is_function_storage(prog, obj));
2987 struct bpf_program *
2988 bpf_program__prev(struct bpf_program *next, struct bpf_object *obj)
2990 struct bpf_program *prog = next;
2993 prog = __bpf_program__iter(prog, obj, false);
2994 } while (prog && bpf_program__is_function_storage(prog, obj));
2999 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
3000 bpf_program_clear_priv_t clear_priv)
3002 if (prog->priv && prog->clear_priv)
3003 prog->clear_priv(prog, prog->priv);
3006 prog->clear_priv = clear_priv;
3010 void *bpf_program__priv(struct bpf_program *prog)
3012 return prog ? prog->priv : ERR_PTR(-EINVAL);
3015 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
3017 prog->prog_ifindex = ifindex;
3020 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
3024 title = prog->section_name;
3026 title = strdup(title);
3028 pr_warning("failed to strdup program title\n");
3029 return ERR_PTR(-ENOMEM);
3036 int bpf_program__fd(struct bpf_program *prog)
3038 return bpf_program__nth_fd(prog, 0);
3041 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
3042 bpf_program_prep_t prep)
3046 if (nr_instances <= 0 || !prep)
3049 if (prog->instances.nr > 0 || prog->instances.fds) {
3050 pr_warning("Can't set pre-processor after loading\n");
3054 instances_fds = malloc(sizeof(int) * nr_instances);
3055 if (!instances_fds) {
3056 pr_warning("alloc memory failed for fds\n");
3060 /* fill all fd with -1 */
3061 memset(instances_fds, -1, sizeof(int) * nr_instances);
3063 prog->instances.nr = nr_instances;
3064 prog->instances.fds = instances_fds;
3065 prog->preprocessor = prep;
3069 int bpf_program__nth_fd(struct bpf_program *prog, int n)
3076 if (n >= prog->instances.nr || n < 0) {
3077 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
3078 n, prog->section_name, prog->instances.nr);
3082 fd = prog->instances.fds[n];
3084 pr_warning("%dth instance of program '%s' is invalid\n",
3085 n, prog->section_name);
3092 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
3097 static bool bpf_program__is_type(struct bpf_program *prog,
3098 enum bpf_prog_type type)
3100 return prog ? (prog->type == type) : false;
3103 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
3104 int bpf_program__set_##NAME(struct bpf_program *prog) \
3108 bpf_program__set_type(prog, TYPE); \
3112 bool bpf_program__is_##NAME(struct bpf_program *prog) \
3114 return bpf_program__is_type(prog, TYPE); \
3117 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
3118 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
3119 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
3120 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
3121 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
3122 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
3123 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
3124 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
3126 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
3127 enum bpf_attach_type type)
3129 prog->expected_attach_type = type;
3132 #define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, atype) \
3133 { string, sizeof(string) - 1, ptype, eatype, is_attachable, atype }
3135 /* Programs that can NOT be attached. */
3136 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0)
3138 /* Programs that can be attached. */
3139 #define BPF_APROG_SEC(string, ptype, atype) \
3140 BPF_PROG_SEC_IMPL(string, ptype, 0, 1, atype)
3142 /* Programs that must specify expected attach type at load time. */
3143 #define BPF_EAPROG_SEC(string, ptype, eatype) \
3144 BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, eatype)
3146 /* Programs that can be attached but attach type can't be identified by section
3147 * name. Kept for backward compatibility.
3149 #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
3151 static const struct {
3154 enum bpf_prog_type prog_type;
3155 enum bpf_attach_type expected_attach_type;
3157 enum bpf_attach_type attach_type;
3158 } section_names[] = {
3159 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
3160 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
3161 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
3162 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
3163 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
3164 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
3165 BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
3166 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
3167 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
3168 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
3169 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
3170 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
3171 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
3172 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
3173 BPF_CGROUP_INET_INGRESS),
3174 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
3175 BPF_CGROUP_INET_EGRESS),
3176 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
3177 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
3178 BPF_CGROUP_INET_SOCK_CREATE),
3179 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
3180 BPF_CGROUP_INET4_POST_BIND),
3181 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
3182 BPF_CGROUP_INET6_POST_BIND),
3183 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
3185 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
3186 BPF_CGROUP_SOCK_OPS),
3187 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
3188 BPF_SK_SKB_STREAM_PARSER),
3189 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
3190 BPF_SK_SKB_STREAM_VERDICT),
3191 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
3192 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
3193 BPF_SK_MSG_VERDICT),
3194 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
3196 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
3197 BPF_FLOW_DISSECTOR),
3198 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3199 BPF_CGROUP_INET4_BIND),
3200 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3201 BPF_CGROUP_INET6_BIND),
3202 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3203 BPF_CGROUP_INET4_CONNECT),
3204 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3205 BPF_CGROUP_INET6_CONNECT),
3206 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3207 BPF_CGROUP_UDP4_SENDMSG),
3208 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3209 BPF_CGROUP_UDP6_SENDMSG),
3210 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
3214 #undef BPF_PROG_SEC_IMPL
3216 #undef BPF_APROG_SEC
3217 #undef BPF_EAPROG_SEC
3218 #undef BPF_APROG_COMPAT
3220 #define MAX_TYPE_NAME_SIZE 32
3222 static char *libbpf_get_type_names(bool attach_type)
3224 int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
3232 /* Forge string buf with all available names */
3233 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3234 if (attach_type && !section_names[i].is_attachable)
3237 if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
3242 strcat(buf, section_names[i].sec);
3248 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
3249 enum bpf_attach_type *expected_attach_type)
3257 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3258 if (strncmp(name, section_names[i].sec, section_names[i].len))
3260 *prog_type = section_names[i].prog_type;
3261 *expected_attach_type = section_names[i].expected_attach_type;
3264 pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
3265 type_names = libbpf_get_type_names(false);
3266 if (type_names != NULL) {
3267 pr_info("supported section(type) names are:%s\n", type_names);
3274 int libbpf_attach_type_by_name(const char *name,
3275 enum bpf_attach_type *attach_type)
3283 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3284 if (strncmp(name, section_names[i].sec, section_names[i].len))
3286 if (!section_names[i].is_attachable)
3288 *attach_type = section_names[i].attach_type;
3291 pr_warning("failed to guess attach type based on ELF section name '%s'\n", name);
3292 type_names = libbpf_get_type_names(true);
3293 if (type_names != NULL) {
3294 pr_info("attachable section(type) names are:%s\n", type_names);
3302 bpf_program__identify_section(struct bpf_program *prog,
3303 enum bpf_prog_type *prog_type,
3304 enum bpf_attach_type *expected_attach_type)
3306 return libbpf_prog_type_by_name(prog->section_name, prog_type,
3307 expected_attach_type);
3310 int bpf_map__fd(struct bpf_map *map)
3312 return map ? map->fd : -EINVAL;
3315 const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
3317 return map ? &map->def : ERR_PTR(-EINVAL);
3320 const char *bpf_map__name(struct bpf_map *map)
3322 return map ? map->name : NULL;
3325 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
3327 return map ? map->btf_key_type_id : 0;
3330 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
3332 return map ? map->btf_value_type_id : 0;
3335 int bpf_map__set_priv(struct bpf_map *map, void *priv,
3336 bpf_map_clear_priv_t clear_priv)
3342 if (map->clear_priv)
3343 map->clear_priv(map, map->priv);
3347 map->clear_priv = clear_priv;
3351 void *bpf_map__priv(struct bpf_map *map)
3353 return map ? map->priv : ERR_PTR(-EINVAL);
3356 bool bpf_map__is_offload_neutral(struct bpf_map *map)
3358 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
3361 bool bpf_map__is_internal(struct bpf_map *map)
3363 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
3366 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
3368 map->map_ifindex = ifindex;
3371 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
3373 if (!bpf_map_type__is_map_in_map(map->def.type)) {
3374 pr_warning("error: unsupported map type\n");
3377 if (map->inner_map_fd != -1) {
3378 pr_warning("error: inner_map_fd already specified\n");
3381 map->inner_map_fd = fd;
3385 static struct bpf_map *
3386 __bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i)
3389 struct bpf_map *s, *e;
3391 if (!obj || !obj->maps)
3395 e = obj->maps + obj->nr_maps;
3397 if ((m < s) || (m >= e)) {
3398 pr_warning("error in %s: map handler doesn't belong to object\n",
3403 idx = (m - obj->maps) + i;
3404 if (idx >= obj->nr_maps || idx < 0)
3406 return &obj->maps[idx];
3410 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
3415 return __bpf_map__iter(prev, obj, 1);
3419 bpf_map__prev(struct bpf_map *next, struct bpf_object *obj)
3424 return obj->maps + obj->nr_maps - 1;
3427 return __bpf_map__iter(next, obj, -1);
3431 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
3433 struct bpf_map *pos;
3435 bpf_object__for_each_map(pos, obj) {
3436 if (pos->name && !strcmp(pos->name, name))
3443 bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name)
3445 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
3449 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
3453 for (i = 0; i < obj->nr_maps; i++) {
3454 if (obj->maps[i].offset == offset)
3455 return &obj->maps[i];
3457 return ERR_PTR(-ENOENT);
3460 long libbpf_get_error(const void *ptr)
3463 return PTR_ERR(ptr);
3467 int bpf_prog_load(const char *file, enum bpf_prog_type type,
3468 struct bpf_object **pobj, int *prog_fd)
3470 struct bpf_prog_load_attr attr;
3472 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
3474 attr.prog_type = type;
3475 attr.expected_attach_type = 0;
3477 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
3480 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
3481 struct bpf_object **pobj, int *prog_fd)
3483 struct bpf_object_open_attr open_attr = {
3485 .prog_type = attr->prog_type,
3487 struct bpf_program *prog, *first_prog = NULL;
3488 enum bpf_attach_type expected_attach_type;
3489 enum bpf_prog_type prog_type;
3490 struct bpf_object *obj;
3491 struct bpf_map *map;
3499 obj = bpf_object__open_xattr(&open_attr);
3500 if (IS_ERR_OR_NULL(obj))
3503 bpf_object__for_each_program(prog, obj) {
3505 * If type is not specified, try to guess it based on
3508 prog_type = attr->prog_type;
3509 prog->prog_ifindex = attr->ifindex;
3510 expected_attach_type = attr->expected_attach_type;
3511 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
3512 err = bpf_program__identify_section(prog, &prog_type,
3513 &expected_attach_type);
3515 bpf_object__close(obj);
3520 bpf_program__set_type(prog, prog_type);
3521 bpf_program__set_expected_attach_type(prog,
3522 expected_attach_type);
3524 prog->log_level = attr->log_level;
3529 bpf_object__for_each_map(map, obj) {
3530 if (!bpf_map__is_offload_neutral(map))
3531 map->map_ifindex = attr->ifindex;
3535 pr_warning("object file doesn't contain bpf program\n");
3536 bpf_object__close(obj);
3540 err = bpf_object__load(obj);
3542 bpf_object__close(obj);
3547 *prog_fd = bpf_program__fd(first_prog);
3551 enum bpf_perf_event_ret
3552 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
3553 void **copy_mem, size_t *copy_size,
3554 bpf_perf_event_print_t fn, void *private_data)
3556 struct perf_event_mmap_page *header = mmap_mem;
3557 __u64 data_head = ring_buffer_read_head(header);
3558 __u64 data_tail = header->data_tail;
3559 void *base = ((__u8 *)header) + page_size;
3560 int ret = LIBBPF_PERF_EVENT_CONT;
3561 struct perf_event_header *ehdr;
3564 while (data_head != data_tail) {
3565 ehdr = base + (data_tail & (mmap_size - 1));
3566 ehdr_size = ehdr->size;
3568 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
3569 void *copy_start = ehdr;
3570 size_t len_first = base + mmap_size - copy_start;
3571 size_t len_secnd = ehdr_size - len_first;
3573 if (*copy_size < ehdr_size) {
3575 *copy_mem = malloc(ehdr_size);
3578 ret = LIBBPF_PERF_EVENT_ERROR;
3581 *copy_size = ehdr_size;
3584 memcpy(*copy_mem, copy_start, len_first);
3585 memcpy(*copy_mem + len_first, base, len_secnd);
3589 ret = fn(ehdr, private_data);
3590 data_tail += ehdr_size;
3591 if (ret != LIBBPF_PERF_EVENT_CONT)
3595 ring_buffer_write_tail(header, data_tail);
3599 struct bpf_prog_info_array_desc {
3600 int array_offset; /* e.g. offset of jited_prog_insns */
3601 int count_offset; /* e.g. offset of jited_prog_len */
3602 int size_offset; /* > 0: offset of rec size,
3603 * < 0: fix size of -size_offset
3607 static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
3608 [BPF_PROG_INFO_JITED_INSNS] = {
3609 offsetof(struct bpf_prog_info, jited_prog_insns),
3610 offsetof(struct bpf_prog_info, jited_prog_len),
3613 [BPF_PROG_INFO_XLATED_INSNS] = {
3614 offsetof(struct bpf_prog_info, xlated_prog_insns),
3615 offsetof(struct bpf_prog_info, xlated_prog_len),
3618 [BPF_PROG_INFO_MAP_IDS] = {
3619 offsetof(struct bpf_prog_info, map_ids),
3620 offsetof(struct bpf_prog_info, nr_map_ids),
3621 -(int)sizeof(__u32),
3623 [BPF_PROG_INFO_JITED_KSYMS] = {
3624 offsetof(struct bpf_prog_info, jited_ksyms),
3625 offsetof(struct bpf_prog_info, nr_jited_ksyms),
3626 -(int)sizeof(__u64),
3628 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
3629 offsetof(struct bpf_prog_info, jited_func_lens),
3630 offsetof(struct bpf_prog_info, nr_jited_func_lens),
3631 -(int)sizeof(__u32),
3633 [BPF_PROG_INFO_FUNC_INFO] = {
3634 offsetof(struct bpf_prog_info, func_info),
3635 offsetof(struct bpf_prog_info, nr_func_info),
3636 offsetof(struct bpf_prog_info, func_info_rec_size),
3638 [BPF_PROG_INFO_LINE_INFO] = {
3639 offsetof(struct bpf_prog_info, line_info),
3640 offsetof(struct bpf_prog_info, nr_line_info),
3641 offsetof(struct bpf_prog_info, line_info_rec_size),
3643 [BPF_PROG_INFO_JITED_LINE_INFO] = {
3644 offsetof(struct bpf_prog_info, jited_line_info),
3645 offsetof(struct bpf_prog_info, nr_jited_line_info),
3646 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
3648 [BPF_PROG_INFO_PROG_TAGS] = {
3649 offsetof(struct bpf_prog_info, prog_tags),
3650 offsetof(struct bpf_prog_info, nr_prog_tags),
3651 -(int)sizeof(__u8) * BPF_TAG_SIZE,
3656 static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, int offset)
3658 __u32 *array = (__u32 *)info;
3661 return array[offset / sizeof(__u32)];
3662 return -(int)offset;
3665 static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, int offset)
3667 __u64 *array = (__u64 *)info;
3670 return array[offset / sizeof(__u64)];
3671 return -(int)offset;
3674 static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
3677 __u32 *array = (__u32 *)info;
3680 array[offset / sizeof(__u32)] = val;
3683 static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
3686 __u64 *array = (__u64 *)info;
3689 array[offset / sizeof(__u64)] = val;
3692 struct bpf_prog_info_linear *
3693 bpf_program__get_prog_info_linear(int fd, __u64 arrays)
3695 struct bpf_prog_info_linear *info_linear;
3696 struct bpf_prog_info info = {};
3697 __u32 info_len = sizeof(info);
3702 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
3703 return ERR_PTR(-EINVAL);
3705 /* step 1: get array dimensions */
3706 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
3708 pr_debug("can't get prog info: %s", strerror(errno));
3709 return ERR_PTR(-EFAULT);
3712 /* step 2: calculate total size of all arrays */
3713 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3714 bool include_array = (arrays & (1UL << i)) > 0;
3715 struct bpf_prog_info_array_desc *desc;
3718 desc = bpf_prog_info_array_desc + i;
3720 /* kernel is too old to support this field */
3721 if (info_len < desc->array_offset + sizeof(__u32) ||
3722 info_len < desc->count_offset + sizeof(__u32) ||
3723 (desc->size_offset > 0 && info_len < desc->size_offset))
3724 include_array = false;
3726 if (!include_array) {
3727 arrays &= ~(1UL << i); /* clear the bit */
3731 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3732 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3734 data_len += count * size;
3737 /* step 3: allocate continuous memory */
3738 data_len = roundup(data_len, sizeof(__u64));
3739 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
3741 return ERR_PTR(-ENOMEM);
3743 /* step 4: fill data to info_linear->info */
3744 info_linear->arrays = arrays;
3745 memset(&info_linear->info, 0, sizeof(info));
3746 ptr = info_linear->data;
3748 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3749 struct bpf_prog_info_array_desc *desc;
3752 if ((arrays & (1UL << i)) == 0)
3755 desc = bpf_prog_info_array_desc + i;
3756 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3757 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3758 bpf_prog_info_set_offset_u32(&info_linear->info,
3759 desc->count_offset, count);
3760 bpf_prog_info_set_offset_u32(&info_linear->info,
3761 desc->size_offset, size);
3762 bpf_prog_info_set_offset_u64(&info_linear->info,
3765 ptr += count * size;
3768 /* step 5: call syscall again to get required arrays */
3769 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
3771 pr_debug("can't get prog info: %s", strerror(errno));
3773 return ERR_PTR(-EFAULT);
3776 /* step 6: verify the data */
3777 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3778 struct bpf_prog_info_array_desc *desc;
3781 if ((arrays & (1UL << i)) == 0)
3784 desc = bpf_prog_info_array_desc + i;
3785 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3786 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
3787 desc->count_offset);
3789 pr_warning("%s: mismatch in element count\n", __func__);
3791 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3792 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
3795 pr_warning("%s: mismatch in rec size\n", __func__);
3798 /* step 7: update info_len and data_len */
3799 info_linear->info_len = sizeof(struct bpf_prog_info);
3800 info_linear->data_len = data_len;
3805 void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
3809 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3810 struct bpf_prog_info_array_desc *desc;
3813 if ((info_linear->arrays & (1UL << i)) == 0)
3816 desc = bpf_prog_info_array_desc + i;
3817 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
3818 desc->array_offset);
3819 offs = addr - ptr_to_u64(info_linear->data);
3820 bpf_prog_info_set_offset_u64(&info_linear->info,
3821 desc->array_offset, offs);
3825 void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
3829 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3830 struct bpf_prog_info_array_desc *desc;
3833 if ((info_linear->arrays & (1UL << i)) == 0)
3836 desc = bpf_prog_info_array_desc + i;
3837 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
3838 desc->array_offset);
3839 addr = offs + ptr_to_u64(info_linear->data);
3840 bpf_prog_info_set_offset_u64(&info_linear->info,
3841 desc->array_offset, addr);