1 // SPDX-License-Identifier: LGPL-2.1
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.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation;
14 * version 2.1 of the License (not later!)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, see <http://www.gnu.org/licenses>
34 #include <asm/unistd.h>
35 #include <linux/err.h>
36 #include <linux/kernel.h>
37 #include <linux/bpf.h>
38 #include <linux/list.h>
39 #include <linux/limits.h>
41 #include <sys/types.h>
54 #define BPF_FS_MAGIC 0xcafe4a11
57 #define __printf(a, b) __attribute__((format(printf, a, b)))
60 static int __base_pr(const char *format, ...)
65 va_start(args, format);
66 err = vfprintf(stderr, format, args);
71 static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
72 static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
73 static __printf(1, 2) libbpf_print_fn_t __pr_debug;
75 #define __pr(func, fmt, ...) \
78 (func)("libbpf: " fmt, ##__VA_ARGS__); \
81 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
82 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
83 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
85 void libbpf_set_print(libbpf_print_fn_t warn,
86 libbpf_print_fn_t info,
87 libbpf_print_fn_t debug)
94 #define STRERR_BUFSIZE 128
96 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
97 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
98 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
100 static const char *libbpf_strerror_table[NR_ERRNO] = {
101 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
102 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
103 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
104 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
105 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
106 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
107 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
108 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
109 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
110 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
111 [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
112 [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
115 int libbpf_strerror(int err, char *buf, size_t size)
120 err = err > 0 ? err : -err;
122 if (err < __LIBBPF_ERRNO__START) {
125 ret = strerror_r(err, buf, size);
126 buf[size - 1] = '\0';
130 if (err < __LIBBPF_ERRNO__END) {
133 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
134 snprintf(buf, size, "%s", msg);
135 buf[size - 1] = '\0';
139 snprintf(buf, size, "Unknown libbpf error %d", err);
140 buf[size - 1] = '\0';
144 #define CHECK_ERR(action, err, out) do { \
151 /* Copied from tools/perf/util/util.h */
153 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
157 # define zclose(fd) ({ \
160 ___err = close((fd)); \
165 #ifdef HAVE_LIBELF_MMAP_SUPPORT
166 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
168 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
172 * bpf_prog should be a better name but it has been used in
176 /* Index in elf obj file, for relocation use. */
180 struct bpf_insn *insns;
181 size_t insns_cnt, main_prog_cnt;
182 enum bpf_prog_type type;
201 bpf_program_prep_t preprocessor;
203 struct bpf_object *obj;
205 bpf_program_clear_priv_t clear_priv;
207 enum bpf_attach_type expected_attach_type;
214 struct bpf_map_def def;
216 bpf_map_clear_priv_t clear_priv;
219 static LIST_HEAD(bpf_objects_list);
225 struct bpf_program *programs;
227 struct bpf_map *maps;
233 * Information when doing elf related work. Only valid if fd
253 * All loaded bpf_object is linked in a list, which is
254 * hidden to caller. bpf_objects__<func> handlers deal with
257 struct list_head list;
260 bpf_object_clear_priv_t clear_priv;
264 #define obj_elf_valid(o) ((o)->efile.elf)
266 static void bpf_program__unload(struct bpf_program *prog)
274 * If the object is opened but the program was never loaded,
275 * it is possible that prog->instances.nr == -1.
277 if (prog->instances.nr > 0) {
278 for (i = 0; i < prog->instances.nr; i++)
279 zclose(prog->instances.fds[i]);
280 } else if (prog->instances.nr != -1) {
281 pr_warning("Internal error: instances.nr is %d\n",
285 prog->instances.nr = -1;
286 zfree(&prog->instances.fds);
289 static void bpf_program__exit(struct bpf_program *prog)
294 if (prog->clear_priv)
295 prog->clear_priv(prog, prog->priv);
298 prog->clear_priv = NULL;
300 bpf_program__unload(prog);
302 zfree(&prog->section_name);
304 zfree(&prog->reloc_desc);
312 bpf_program__init(void *data, size_t size, char *section_name, int idx,
313 struct bpf_program *prog)
315 if (size < sizeof(struct bpf_insn)) {
316 pr_warning("corrupted section '%s'\n", section_name);
320 bzero(prog, sizeof(*prog));
322 prog->section_name = strdup(section_name);
323 if (!prog->section_name) {
324 pr_warning("failed to alloc name for prog under section(%d) %s\n",
329 prog->insns = malloc(size);
331 pr_warning("failed to alloc insns for prog under section %s\n",
335 prog->insns_cnt = size / sizeof(struct bpf_insn);
336 memcpy(prog->insns, data,
337 prog->insns_cnt * sizeof(struct bpf_insn));
339 prog->instances.fds = NULL;
340 prog->instances.nr = -1;
341 prog->type = BPF_PROG_TYPE_KPROBE;
345 bpf_program__exit(prog);
350 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
351 char *section_name, int idx)
353 struct bpf_program prog, *progs;
356 err = bpf_program__init(data, size, section_name, idx, &prog);
360 progs = obj->programs;
361 nr_progs = obj->nr_programs;
363 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
366 * In this case the original obj->programs
367 * is still valid, so don't need special treat for
368 * bpf_close_object().
370 pr_warning("failed to alloc a new program under section '%s'\n",
372 bpf_program__exit(&prog);
376 pr_debug("found program %s\n", prog.section_name);
377 obj->programs = progs;
378 obj->nr_programs = nr_progs + 1;
380 progs[nr_progs] = prog;
385 bpf_object__init_prog_names(struct bpf_object *obj)
387 Elf_Data *symbols = obj->efile.symbols;
388 struct bpf_program *prog;
391 for (pi = 0; pi < obj->nr_programs; pi++) {
392 const char *name = NULL;
394 prog = &obj->programs[pi];
395 if (prog->idx == obj->efile.text_shndx) {
400 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
404 if (!gelf_getsym(symbols, si, &sym))
406 if (sym.st_shndx != prog->idx)
408 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
411 name = elf_strptr(obj->efile.elf,
412 obj->efile.strtabidx,
415 pr_warning("failed to get sym name string for prog %s\n",
417 return -LIBBPF_ERRNO__LIBELF;
422 pr_warning("failed to find sym for prog %s\n",
427 prog->name = strdup(name);
429 pr_warning("failed to allocate memory for prog sym %s\n",
438 static struct bpf_object *bpf_object__new(const char *path,
442 struct bpf_object *obj;
444 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
446 pr_warning("alloc memory failed for %s\n", path);
447 return ERR_PTR(-ENOMEM);
450 strcpy(obj->path, path);
454 * Caller of this function should also calls
455 * bpf_object__elf_finish() after data collection to return
456 * obj_buf to user. If not, we should duplicate the buffer to
457 * avoid user freeing them before elf finish.
459 obj->efile.obj_buf = obj_buf;
460 obj->efile.obj_buf_sz = obj_buf_sz;
461 obj->efile.maps_shndx = -1;
465 INIT_LIST_HEAD(&obj->list);
466 list_add(&obj->list, &bpf_objects_list);
470 static void bpf_object__elf_finish(struct bpf_object *obj)
472 if (!obj_elf_valid(obj))
475 if (obj->efile.elf) {
476 elf_end(obj->efile.elf);
477 obj->efile.elf = NULL;
479 obj->efile.symbols = NULL;
481 zfree(&obj->efile.reloc);
482 obj->efile.nr_reloc = 0;
483 zclose(obj->efile.fd);
484 obj->efile.obj_buf = NULL;
485 obj->efile.obj_buf_sz = 0;
488 static int bpf_object__elf_init(struct bpf_object *obj)
493 if (obj_elf_valid(obj)) {
494 pr_warning("elf init: internal error\n");
495 return -LIBBPF_ERRNO__LIBELF;
498 if (obj->efile.obj_buf_sz > 0) {
500 * obj_buf should have been validated by
501 * bpf_object__open_buffer().
503 obj->efile.elf = elf_memory(obj->efile.obj_buf,
504 obj->efile.obj_buf_sz);
506 obj->efile.fd = open(obj->path, O_RDONLY);
507 if (obj->efile.fd < 0) {
508 pr_warning("failed to open %s: %s\n", obj->path,
513 obj->efile.elf = elf_begin(obj->efile.fd,
514 LIBBPF_ELF_C_READ_MMAP,
518 if (!obj->efile.elf) {
519 pr_warning("failed to open %s as ELF file\n",
521 err = -LIBBPF_ERRNO__LIBELF;
525 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
526 pr_warning("failed to get EHDR from %s\n",
528 err = -LIBBPF_ERRNO__FORMAT;
531 ep = &obj->efile.ehdr;
533 /* Old LLVM set e_machine to EM_NONE */
534 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
535 pr_warning("%s is not an eBPF object file\n",
537 err = -LIBBPF_ERRNO__FORMAT;
543 bpf_object__elf_finish(obj);
548 bpf_object__check_endianness(struct bpf_object *obj)
550 static unsigned int const endian = 1;
552 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
554 /* We are big endian, BPF obj is little endian. */
555 if (*(unsigned char const *)&endian != 1)
560 /* We are little endian, BPF obj is big endian. */
561 if (*(unsigned char const *)&endian != 0)
565 return -LIBBPF_ERRNO__ENDIAN;
571 pr_warning("Error: endianness mismatch.\n");
572 return -LIBBPF_ERRNO__ENDIAN;
576 bpf_object__init_license(struct bpf_object *obj,
577 void *data, size_t size)
579 memcpy(obj->license, data,
580 min(size, sizeof(obj->license) - 1));
581 pr_debug("license of %s is %s\n", obj->path, obj->license);
586 bpf_object__init_kversion(struct bpf_object *obj,
587 void *data, size_t size)
591 if (size != sizeof(kver)) {
592 pr_warning("invalid kver section in %s\n", obj->path);
593 return -LIBBPF_ERRNO__FORMAT;
595 memcpy(&kver, data, sizeof(kver));
596 obj->kern_version = kver;
597 pr_debug("kernel version of %s is %x\n", obj->path,
602 static int compare_bpf_map(const void *_a, const void *_b)
604 const struct bpf_map *a = _a;
605 const struct bpf_map *b = _b;
607 return a->offset - b->offset;
611 bpf_object__init_maps(struct bpf_object *obj)
613 int i, map_idx, map_def_sz, nr_maps = 0;
616 Elf_Data *symbols = obj->efile.symbols;
618 if (obj->efile.maps_shndx < 0)
623 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
625 data = elf_getdata(scn, NULL);
627 pr_warning("failed to get Elf_Data from map section %d\n",
628 obj->efile.maps_shndx);
633 * Count number of maps. Each map has a name.
634 * Array of maps is not supported: only the first element is
637 * TODO: Detect array of map and report error.
639 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
642 if (!gelf_getsym(symbols, i, &sym))
644 if (sym.st_shndx != obj->efile.maps_shndx)
649 /* Alloc obj->maps and fill nr_maps. */
650 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
651 nr_maps, data->d_size);
656 /* Assume equally sized map definitions */
657 map_def_sz = data->d_size / nr_maps;
658 if (!data->d_size || (data->d_size % nr_maps) != 0) {
659 pr_warning("unable to determine map definition size "
660 "section %s, %d maps in %zd bytes\n",
661 obj->path, nr_maps, data->d_size);
665 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
667 pr_warning("alloc maps for object failed\n");
670 obj->nr_maps = nr_maps;
673 * fill all fd with -1 so won't close incorrect
674 * fd (fd=0 is stdin) when failure (zclose won't close
677 for (i = 0; i < nr_maps; i++)
678 obj->maps[i].fd = -1;
681 * Fill obj->maps using data in "maps" section.
683 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
685 const char *map_name;
686 struct bpf_map_def *def;
688 if (!gelf_getsym(symbols, i, &sym))
690 if (sym.st_shndx != obj->efile.maps_shndx)
693 map_name = elf_strptr(obj->efile.elf,
694 obj->efile.strtabidx,
696 obj->maps[map_idx].offset = sym.st_value;
697 if (sym.st_value + map_def_sz > data->d_size) {
698 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
699 obj->path, map_name);
703 obj->maps[map_idx].name = strdup(map_name);
704 if (!obj->maps[map_idx].name) {
705 pr_warning("failed to alloc map name\n");
708 pr_debug("map %d is \"%s\"\n", map_idx,
709 obj->maps[map_idx].name);
710 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
712 * If the definition of the map in the object file fits in
713 * bpf_map_def, copy it. Any extra fields in our version
714 * of bpf_map_def will default to zero as a result of the
717 if (map_def_sz <= sizeof(struct bpf_map_def)) {
718 memcpy(&obj->maps[map_idx].def, def, map_def_sz);
721 * Here the map structure being read is bigger than what
722 * we expect, truncate if the excess bits are all zero.
723 * If they are not zero, reject this map as
727 for (b = ((char *)def) + sizeof(struct bpf_map_def);
728 b < ((char *)def) + map_def_sz; b++) {
730 pr_warning("maps section in %s: \"%s\" "
731 "has unrecognized, non-zero "
733 obj->path, map_name);
737 memcpy(&obj->maps[map_idx].def, def,
738 sizeof(struct bpf_map_def));
743 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
747 static bool section_have_execinstr(struct bpf_object *obj, int idx)
752 scn = elf_getscn(obj->efile.elf, idx);
756 if (gelf_getshdr(scn, &sh) != &sh)
759 if (sh.sh_flags & SHF_EXECINSTR)
765 static int bpf_object__elf_collect(struct bpf_object *obj)
767 Elf *elf = obj->efile.elf;
768 GElf_Ehdr *ep = &obj->efile.ehdr;
770 int idx = 0, err = 0;
772 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
773 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
774 pr_warning("failed to get e_shstrndx from %s\n",
776 return -LIBBPF_ERRNO__FORMAT;
779 while ((scn = elf_nextscn(elf, scn)) != NULL) {
785 if (gelf_getshdr(scn, &sh) != &sh) {
786 pr_warning("failed to get section(%d) header from %s\n",
788 err = -LIBBPF_ERRNO__FORMAT;
792 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
794 pr_warning("failed to get section(%d) name from %s\n",
796 err = -LIBBPF_ERRNO__FORMAT;
800 data = elf_getdata(scn, 0);
802 pr_warning("failed to get section(%d) data from %s(%s)\n",
803 idx, name, obj->path);
804 err = -LIBBPF_ERRNO__FORMAT;
807 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
808 idx, name, (unsigned long)data->d_size,
809 (int)sh.sh_link, (unsigned long)sh.sh_flags,
812 if (strcmp(name, "license") == 0)
813 err = bpf_object__init_license(obj,
816 else if (strcmp(name, "version") == 0)
817 err = bpf_object__init_kversion(obj,
820 else if (strcmp(name, "maps") == 0)
821 obj->efile.maps_shndx = idx;
822 else if (sh.sh_type == SHT_SYMTAB) {
823 if (obj->efile.symbols) {
824 pr_warning("bpf: multiple SYMTAB in %s\n",
826 err = -LIBBPF_ERRNO__FORMAT;
828 obj->efile.symbols = data;
829 obj->efile.strtabidx = sh.sh_link;
831 } else if ((sh.sh_type == SHT_PROGBITS) &&
832 (sh.sh_flags & SHF_EXECINSTR) &&
833 (data->d_size > 0)) {
834 if (strcmp(name, ".text") == 0)
835 obj->efile.text_shndx = idx;
836 err = bpf_object__add_program(obj, data->d_buf,
837 data->d_size, name, idx);
839 char errmsg[STRERR_BUFSIZE];
841 strerror_r(-err, errmsg, sizeof(errmsg));
842 pr_warning("failed to alloc program %s (%s): %s",
843 name, obj->path, errmsg);
845 } else if (sh.sh_type == SHT_REL) {
846 void *reloc = obj->efile.reloc;
847 int nr_reloc = obj->efile.nr_reloc + 1;
848 int sec = sh.sh_info; /* points to other section */
850 /* Only do relo for section with exec instructions */
851 if (!section_have_execinstr(obj, sec)) {
852 pr_debug("skip relo %s(%d) for section(%d)\n",
857 reloc = realloc(reloc,
858 sizeof(*obj->efile.reloc) * nr_reloc);
860 pr_warning("realloc failed\n");
863 int n = nr_reloc - 1;
865 obj->efile.reloc = reloc;
866 obj->efile.nr_reloc = nr_reloc;
868 obj->efile.reloc[n].shdr = sh;
869 obj->efile.reloc[n].data = data;
872 pr_debug("skip section(%d) %s\n", idx, name);
878 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
879 pr_warning("Corrupted ELF file: index of strtab invalid\n");
880 return LIBBPF_ERRNO__FORMAT;
882 if (obj->efile.maps_shndx >= 0) {
883 err = bpf_object__init_maps(obj);
887 err = bpf_object__init_prog_names(obj);
892 static struct bpf_program *
893 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
895 struct bpf_program *prog;
898 for (i = 0; i < obj->nr_programs; i++) {
899 prog = &obj->programs[i];
900 if (prog->idx == idx)
907 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
908 Elf_Data *data, struct bpf_object *obj)
910 Elf_Data *symbols = obj->efile.symbols;
911 int text_shndx = obj->efile.text_shndx;
912 int maps_shndx = obj->efile.maps_shndx;
913 struct bpf_map *maps = obj->maps;
914 size_t nr_maps = obj->nr_maps;
917 pr_debug("collecting relocating info for: '%s'\n",
919 nrels = shdr->sh_size / shdr->sh_entsize;
921 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
922 if (!prog->reloc_desc) {
923 pr_warning("failed to alloc memory in relocation\n");
926 prog->nr_reloc = nrels;
928 for (i = 0; i < nrels; i++) {
931 unsigned int insn_idx;
932 struct bpf_insn *insns = prog->insns;
935 if (!gelf_getrel(data, i, &rel)) {
936 pr_warning("relocation: failed to get %d reloc\n", i);
937 return -LIBBPF_ERRNO__FORMAT;
940 if (!gelf_getsym(symbols,
941 GELF_R_SYM(rel.r_info),
943 pr_warning("relocation: symbol %"PRIx64" not found\n",
944 GELF_R_SYM(rel.r_info));
945 return -LIBBPF_ERRNO__FORMAT;
947 pr_debug("relo for %lld value %lld name %d\n",
948 (long long) (rel.r_info >> 32),
949 (long long) sym.st_value, sym.st_name);
951 if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
952 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
953 prog->section_name, sym.st_shndx);
954 return -LIBBPF_ERRNO__RELOC;
957 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
958 pr_debug("relocation: insn_idx=%u\n", insn_idx);
960 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
961 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
962 pr_warning("incorrect bpf_call opcode\n");
963 return -LIBBPF_ERRNO__RELOC;
965 prog->reloc_desc[i].type = RELO_CALL;
966 prog->reloc_desc[i].insn_idx = insn_idx;
967 prog->reloc_desc[i].text_off = sym.st_value;
971 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
972 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
973 insn_idx, insns[insn_idx].code);
974 return -LIBBPF_ERRNO__RELOC;
977 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
978 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
979 if (maps[map_idx].offset == sym.st_value) {
980 pr_debug("relocation: find map %zd (%s) for insn %u\n",
981 map_idx, maps[map_idx].name, insn_idx);
986 if (map_idx >= nr_maps) {
987 pr_warning("bpf relocation: map_idx %d large than %d\n",
988 (int)map_idx, (int)nr_maps - 1);
989 return -LIBBPF_ERRNO__RELOC;
992 prog->reloc_desc[i].type = RELO_LD64;
993 prog->reloc_desc[i].insn_idx = insn_idx;
994 prog->reloc_desc[i].map_idx = map_idx;
1000 bpf_object__create_maps(struct bpf_object *obj)
1004 for (i = 0; i < obj->nr_maps; i++) {
1005 struct bpf_map_def *def = &obj->maps[i].def;
1006 int *pfd = &obj->maps[i].fd;
1008 *pfd = bpf_create_map_name(def->type,
1018 pr_warning("failed to create map (name: '%s'): %s\n",
1021 for (j = 0; j < i; j++)
1022 zclose(obj->maps[j].fd);
1025 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
1032 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1033 struct reloc_desc *relo)
1035 struct bpf_insn *insn, *new_insn;
1036 struct bpf_program *text;
1039 if (relo->type != RELO_CALL)
1040 return -LIBBPF_ERRNO__RELOC;
1042 if (prog->idx == obj->efile.text_shndx) {
1043 pr_warning("relo in .text insn %d into off %d\n",
1044 relo->insn_idx, relo->text_off);
1045 return -LIBBPF_ERRNO__RELOC;
1048 if (prog->main_prog_cnt == 0) {
1049 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1051 pr_warning("no .text section found yet relo into text exist\n");
1052 return -LIBBPF_ERRNO__RELOC;
1054 new_cnt = prog->insns_cnt + text->insns_cnt;
1055 new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
1057 pr_warning("oom in prog realloc\n");
1060 memcpy(new_insn + prog->insns_cnt, text->insns,
1061 text->insns_cnt * sizeof(*insn));
1062 prog->insns = new_insn;
1063 prog->main_prog_cnt = prog->insns_cnt;
1064 prog->insns_cnt = new_cnt;
1065 pr_debug("added %zd insn from %s to prog %s\n",
1066 text->insns_cnt, text->section_name,
1067 prog->section_name);
1069 insn = &prog->insns[relo->insn_idx];
1070 insn->imm += prog->main_prog_cnt - relo->insn_idx;
1075 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
1079 if (!prog || !prog->reloc_desc)
1082 for (i = 0; i < prog->nr_reloc; i++) {
1083 if (prog->reloc_desc[i].type == RELO_LD64) {
1084 struct bpf_insn *insns = prog->insns;
1085 int insn_idx, map_idx;
1087 insn_idx = prog->reloc_desc[i].insn_idx;
1088 map_idx = prog->reloc_desc[i].map_idx;
1090 if (insn_idx >= (int)prog->insns_cnt) {
1091 pr_warning("relocation out of range: '%s'\n",
1092 prog->section_name);
1093 return -LIBBPF_ERRNO__RELOC;
1095 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1096 insns[insn_idx].imm = obj->maps[map_idx].fd;
1098 err = bpf_program__reloc_text(prog, obj,
1099 &prog->reloc_desc[i]);
1105 zfree(&prog->reloc_desc);
1112 bpf_object__relocate(struct bpf_object *obj)
1114 struct bpf_program *prog;
1118 for (i = 0; i < obj->nr_programs; i++) {
1119 prog = &obj->programs[i];
1121 err = bpf_program__relocate(prog, obj);
1123 pr_warning("failed to relocate '%s'\n",
1124 prog->section_name);
1131 static int bpf_object__collect_reloc(struct bpf_object *obj)
1135 if (!obj_elf_valid(obj)) {
1136 pr_warning("Internal error: elf object is closed\n");
1137 return -LIBBPF_ERRNO__INTERNAL;
1140 for (i = 0; i < obj->efile.nr_reloc; i++) {
1141 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1142 Elf_Data *data = obj->efile.reloc[i].data;
1143 int idx = shdr->sh_info;
1144 struct bpf_program *prog;
1146 if (shdr->sh_type != SHT_REL) {
1147 pr_warning("internal error at %d\n", __LINE__);
1148 return -LIBBPF_ERRNO__INTERNAL;
1151 prog = bpf_object__find_prog_by_idx(obj, idx);
1153 pr_warning("relocation failed: no section(%d)\n", idx);
1154 return -LIBBPF_ERRNO__RELOC;
1157 err = bpf_program__collect_reloc(prog,
1167 load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
1168 const char *name, struct bpf_insn *insns, int insns_cnt,
1169 char *license, u32 kern_version, int *pfd)
1171 struct bpf_load_program_attr load_attr;
1175 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
1176 load_attr.prog_type = type;
1177 load_attr.expected_attach_type = expected_attach_type;
1178 load_attr.name = name;
1179 load_attr.insns = insns;
1180 load_attr.insns_cnt = insns_cnt;
1181 load_attr.license = license;
1182 load_attr.kern_version = kern_version;
1184 if (!load_attr.insns || !load_attr.insns_cnt)
1187 log_buf = malloc(BPF_LOG_BUF_SIZE);
1189 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1191 ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
1199 ret = -LIBBPF_ERRNO__LOAD;
1200 pr_warning("load bpf program failed: %s\n", strerror(errno));
1202 if (log_buf && log_buf[0] != '\0') {
1203 ret = -LIBBPF_ERRNO__VERIFY;
1204 pr_warning("-- BEGIN DUMP LOG ---\n");
1205 pr_warning("\n%s\n", log_buf);
1206 pr_warning("-- END LOG --\n");
1207 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
1208 pr_warning("Program too large (%zu insns), at most %d insns\n",
1209 load_attr.insns_cnt, BPF_MAXINSNS);
1210 ret = -LIBBPF_ERRNO__PROG2BIG;
1212 /* Wrong program type? */
1213 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
1216 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
1217 load_attr.expected_attach_type = 0;
1218 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
1221 ret = -LIBBPF_ERRNO__PROGTYPE;
1227 ret = -LIBBPF_ERRNO__KVER;
1236 bpf_program__load(struct bpf_program *prog,
1237 char *license, u32 kern_version)
1241 if (prog->instances.nr < 0 || !prog->instances.fds) {
1242 if (prog->preprocessor) {
1243 pr_warning("Internal error: can't load program '%s'\n",
1244 prog->section_name);
1245 return -LIBBPF_ERRNO__INTERNAL;
1248 prog->instances.fds = malloc(sizeof(int));
1249 if (!prog->instances.fds) {
1250 pr_warning("Not enough memory for BPF fds\n");
1253 prog->instances.nr = 1;
1254 prog->instances.fds[0] = -1;
1257 if (!prog->preprocessor) {
1258 if (prog->instances.nr != 1) {
1259 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1260 prog->section_name, prog->instances.nr);
1262 err = load_program(prog->type, prog->expected_attach_type,
1263 prog->name, prog->insns, prog->insns_cnt,
1264 license, kern_version, &fd);
1266 prog->instances.fds[0] = fd;
1270 for (i = 0; i < prog->instances.nr; i++) {
1271 struct bpf_prog_prep_result result;
1272 bpf_program_prep_t preprocessor = prog->preprocessor;
1274 bzero(&result, sizeof(result));
1275 err = preprocessor(prog, i, prog->insns,
1276 prog->insns_cnt, &result);
1278 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1279 i, prog->section_name);
1283 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1284 pr_debug("Skip loading the %dth instance of program '%s'\n",
1285 i, prog->section_name);
1286 prog->instances.fds[i] = -1;
1292 err = load_program(prog->type, prog->expected_attach_type,
1293 prog->name, result.new_insn_ptr,
1294 result.new_insn_cnt,
1295 license, kern_version, &fd);
1298 pr_warning("Loading the %dth instance of program '%s' failed\n",
1299 i, prog->section_name);
1305 prog->instances.fds[i] = fd;
1309 pr_warning("failed to load program '%s'\n",
1310 prog->section_name);
1311 zfree(&prog->insns);
1312 prog->insns_cnt = 0;
1317 bpf_object__load_progs(struct bpf_object *obj)
1322 for (i = 0; i < obj->nr_programs; i++) {
1323 if (obj->programs[i].idx == obj->efile.text_shndx)
1325 err = bpf_program__load(&obj->programs[i],
1334 static int bpf_object__validate(struct bpf_object *obj)
1336 if (obj->kern_version == 0) {
1337 pr_warning("%s doesn't provide kernel version\n",
1339 return -LIBBPF_ERRNO__KVERSION;
1344 static struct bpf_object *
1345 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
1347 struct bpf_object *obj;
1350 if (elf_version(EV_CURRENT) == EV_NONE) {
1351 pr_warning("failed to init libelf for %s\n", path);
1352 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1355 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1359 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1360 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1361 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1362 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1363 CHECK_ERR(bpf_object__validate(obj), err, out);
1365 bpf_object__elf_finish(obj);
1368 bpf_object__close(obj);
1369 return ERR_PTR(err);
1372 struct bpf_object *bpf_object__open(const char *path)
1374 /* param validation */
1378 pr_debug("loading %s\n", path);
1380 return __bpf_object__open(path, NULL, 0);
1383 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1389 /* param validation */
1390 if (!obj_buf || obj_buf_sz <= 0)
1394 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1395 (unsigned long)obj_buf,
1396 (unsigned long)obj_buf_sz);
1397 tmp_name[sizeof(tmp_name) - 1] = '\0';
1400 pr_debug("loading object '%s' from buffer\n",
1403 return __bpf_object__open(name, obj_buf, obj_buf_sz);
1406 int bpf_object__unload(struct bpf_object *obj)
1413 for (i = 0; i < obj->nr_maps; i++)
1414 zclose(obj->maps[i].fd);
1416 for (i = 0; i < obj->nr_programs; i++)
1417 bpf_program__unload(&obj->programs[i]);
1422 int bpf_object__load(struct bpf_object *obj)
1430 pr_warning("object should not be loaded twice\n");
1436 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1437 CHECK_ERR(bpf_object__relocate(obj), err, out);
1438 CHECK_ERR(bpf_object__load_progs(obj), err, out);
1442 bpf_object__unload(obj);
1443 pr_warning("failed to load object '%s'\n", obj->path);
1447 static int check_path(const char *path)
1449 struct statfs st_fs;
1456 dname = strdup(path);
1460 dir = dirname(dname);
1461 if (statfs(dir, &st_fs)) {
1462 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1467 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1468 pr_warning("specified path %s is not on BPF FS\n", path);
1475 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1480 err = check_path(path);
1485 pr_warning("invalid program pointer\n");
1489 if (instance < 0 || instance >= prog->instances.nr) {
1490 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1491 instance, prog->section_name, prog->instances.nr);
1495 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1496 pr_warning("failed to pin program: %s\n", strerror(errno));
1499 pr_debug("pinned program '%s'\n", path);
1504 static int make_dir(const char *path)
1508 if (mkdir(path, 0700) && errno != EEXIST)
1512 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1516 int bpf_program__pin(struct bpf_program *prog, const char *path)
1520 err = check_path(path);
1525 pr_warning("invalid program pointer\n");
1529 if (prog->instances.nr <= 0) {
1530 pr_warning("no instances of prog %s to pin\n",
1531 prog->section_name);
1535 err = make_dir(path);
1539 for (i = 0; i < prog->instances.nr; i++) {
1543 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1546 else if (len >= PATH_MAX)
1547 return -ENAMETOOLONG;
1549 err = bpf_program__pin_instance(prog, buf, i);
1557 int bpf_map__pin(struct bpf_map *map, const char *path)
1561 err = check_path(path);
1566 pr_warning("invalid map pointer\n");
1570 if (bpf_obj_pin(map->fd, path)) {
1571 pr_warning("failed to pin map: %s\n", strerror(errno));
1575 pr_debug("pinned map '%s'\n", path);
1579 int bpf_object__pin(struct bpf_object *obj, const char *path)
1581 struct bpf_program *prog;
1582 struct bpf_map *map;
1589 pr_warning("object not yet loaded; load it first\n");
1593 err = make_dir(path);
1597 bpf_map__for_each(map, obj) {
1601 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1602 bpf_map__name(map));
1605 else if (len >= PATH_MAX)
1606 return -ENAMETOOLONG;
1608 err = bpf_map__pin(map, buf);
1613 bpf_object__for_each_program(prog, obj) {
1617 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1618 prog->section_name);
1621 else if (len >= PATH_MAX)
1622 return -ENAMETOOLONG;
1624 err = bpf_program__pin(prog, buf);
1632 void bpf_object__close(struct bpf_object *obj)
1639 if (obj->clear_priv)
1640 obj->clear_priv(obj, obj->priv);
1642 bpf_object__elf_finish(obj);
1643 bpf_object__unload(obj);
1645 for (i = 0; i < obj->nr_maps; i++) {
1646 zfree(&obj->maps[i].name);
1647 if (obj->maps[i].clear_priv)
1648 obj->maps[i].clear_priv(&obj->maps[i],
1650 obj->maps[i].priv = NULL;
1651 obj->maps[i].clear_priv = NULL;
1656 if (obj->programs && obj->nr_programs) {
1657 for (i = 0; i < obj->nr_programs; i++)
1658 bpf_program__exit(&obj->programs[i]);
1660 zfree(&obj->programs);
1662 list_del(&obj->list);
1667 bpf_object__next(struct bpf_object *prev)
1669 struct bpf_object *next;
1672 next = list_first_entry(&bpf_objects_list,
1676 next = list_next_entry(prev, list);
1678 /* Empty list is noticed here so don't need checking on entry. */
1679 if (&next->list == &bpf_objects_list)
1685 const char *bpf_object__name(struct bpf_object *obj)
1687 return obj ? obj->path : ERR_PTR(-EINVAL);
1690 unsigned int bpf_object__kversion(struct bpf_object *obj)
1692 return obj ? obj->kern_version : 0;
1695 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1696 bpf_object_clear_priv_t clear_priv)
1698 if (obj->priv && obj->clear_priv)
1699 obj->clear_priv(obj, obj->priv);
1702 obj->clear_priv = clear_priv;
1706 void *bpf_object__priv(struct bpf_object *obj)
1708 return obj ? obj->priv : ERR_PTR(-EINVAL);
1711 struct bpf_program *
1712 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1720 return &obj->programs[0];
1722 if (prev->obj != obj) {
1723 pr_warning("error: program handler doesn't match object\n");
1727 idx = (prev - obj->programs) + 1;
1728 if (idx >= obj->nr_programs)
1730 return &obj->programs[idx];
1733 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1734 bpf_program_clear_priv_t clear_priv)
1736 if (prog->priv && prog->clear_priv)
1737 prog->clear_priv(prog, prog->priv);
1740 prog->clear_priv = clear_priv;
1744 void *bpf_program__priv(struct bpf_program *prog)
1746 return prog ? prog->priv : ERR_PTR(-EINVAL);
1749 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1753 title = prog->section_name;
1755 title = strdup(title);
1757 pr_warning("failed to strdup program title\n");
1758 return ERR_PTR(-ENOMEM);
1765 int bpf_program__fd(struct bpf_program *prog)
1767 return bpf_program__nth_fd(prog, 0);
1770 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1771 bpf_program_prep_t prep)
1775 if (nr_instances <= 0 || !prep)
1778 if (prog->instances.nr > 0 || prog->instances.fds) {
1779 pr_warning("Can't set pre-processor after loading\n");
1783 instances_fds = malloc(sizeof(int) * nr_instances);
1784 if (!instances_fds) {
1785 pr_warning("alloc memory failed for fds\n");
1789 /* fill all fd with -1 */
1790 memset(instances_fds, -1, sizeof(int) * nr_instances);
1792 prog->instances.nr = nr_instances;
1793 prog->instances.fds = instances_fds;
1794 prog->preprocessor = prep;
1798 int bpf_program__nth_fd(struct bpf_program *prog, int n)
1802 if (n >= prog->instances.nr || n < 0) {
1803 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1804 n, prog->section_name, prog->instances.nr);
1808 fd = prog->instances.fds[n];
1810 pr_warning("%dth instance of program '%s' is invalid\n",
1811 n, prog->section_name);
1818 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
1823 static bool bpf_program__is_type(struct bpf_program *prog,
1824 enum bpf_prog_type type)
1826 return prog ? (prog->type == type) : false;
1829 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1830 int bpf_program__set_##NAME(struct bpf_program *prog) \
1834 bpf_program__set_type(prog, TYPE); \
1838 bool bpf_program__is_##NAME(struct bpf_program *prog) \
1840 return bpf_program__is_type(prog, TYPE); \
1843 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1844 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
1845 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1846 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
1847 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
1848 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1849 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
1851 static void bpf_program__set_expected_attach_type(struct bpf_program *prog,
1852 enum bpf_attach_type type)
1854 prog->expected_attach_type = type;
1857 #define BPF_PROG_SEC_FULL(string, ptype, atype) \
1858 { string, sizeof(string) - 1, ptype, atype }
1860 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
1862 #define BPF_SA_PROG_SEC(string, ptype) \
1863 BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
1865 static const struct {
1868 enum bpf_prog_type prog_type;
1869 enum bpf_attach_type expected_attach_type;
1870 } section_names[] = {
1871 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
1872 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
1873 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
1874 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
1875 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
1876 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
1877 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
1878 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
1879 BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
1880 BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
1881 BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
1882 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
1883 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
1884 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
1885 BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
1886 BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
1887 BPF_PROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG),
1888 BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND),
1889 BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND),
1890 BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
1891 BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
1895 #undef BPF_PROG_SEC_FULL
1896 #undef BPF_SA_PROG_SEC
1898 static int bpf_program__identify_section(struct bpf_program *prog)
1902 if (!prog->section_name)
1905 for (i = 0; i < ARRAY_SIZE(section_names); i++)
1906 if (strncmp(prog->section_name, section_names[i].sec,
1907 section_names[i].len) == 0)
1911 pr_warning("failed to guess program type based on section name %s\n",
1912 prog->section_name);
1917 int bpf_map__fd(struct bpf_map *map)
1919 return map ? map->fd : -EINVAL;
1922 const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
1924 return map ? &map->def : ERR_PTR(-EINVAL);
1927 const char *bpf_map__name(struct bpf_map *map)
1929 return map ? map->name : NULL;
1932 int bpf_map__set_priv(struct bpf_map *map, void *priv,
1933 bpf_map_clear_priv_t clear_priv)
1939 if (map->clear_priv)
1940 map->clear_priv(map, map->priv);
1944 map->clear_priv = clear_priv;
1948 void *bpf_map__priv(struct bpf_map *map)
1950 return map ? map->priv : ERR_PTR(-EINVAL);
1954 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1957 struct bpf_map *s, *e;
1959 if (!obj || !obj->maps)
1963 e = obj->maps + obj->nr_maps;
1968 if ((prev < s) || (prev >= e)) {
1969 pr_warning("error in %s: map handler doesn't belong to object\n",
1974 idx = (prev - obj->maps) + 1;
1975 if (idx >= obj->nr_maps)
1977 return &obj->maps[idx];
1981 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
1983 struct bpf_map *pos;
1985 bpf_map__for_each(pos, obj) {
1986 if (pos->name && !strcmp(pos->name, name))
1993 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1997 for (i = 0; i < obj->nr_maps; i++) {
1998 if (obj->maps[i].offset == offset)
1999 return &obj->maps[i];
2001 return ERR_PTR(-ENOENT);
2004 long libbpf_get_error(const void *ptr)
2007 return PTR_ERR(ptr);
2011 int bpf_prog_load(const char *file, enum bpf_prog_type type,
2012 struct bpf_object **pobj, int *prog_fd)
2014 struct bpf_prog_load_attr attr;
2016 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
2018 attr.prog_type = type;
2019 attr.expected_attach_type = 0;
2021 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
2024 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2025 struct bpf_object **pobj, int *prog_fd)
2027 struct bpf_program *prog, *first_prog = NULL;
2028 enum bpf_attach_type expected_attach_type;
2029 enum bpf_prog_type prog_type;
2030 struct bpf_object *obj;
2037 obj = bpf_object__open(attr->file);
2041 bpf_object__for_each_program(prog, obj) {
2043 * If type is not specified, try to guess it based on
2046 prog_type = attr->prog_type;
2047 expected_attach_type = attr->expected_attach_type;
2048 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
2049 section_idx = bpf_program__identify_section(prog);
2050 if (section_idx < 0) {
2051 bpf_object__close(obj);
2054 prog_type = section_names[section_idx].prog_type;
2055 expected_attach_type =
2056 section_names[section_idx].expected_attach_type;
2059 bpf_program__set_type(prog, prog_type);
2060 bpf_program__set_expected_attach_type(prog,
2061 expected_attach_type);
2063 if (prog->idx != obj->efile.text_shndx && !first_prog)
2068 pr_warning("object file doesn't contain bpf program\n");
2069 bpf_object__close(obj);
2073 err = bpf_object__load(obj);
2075 bpf_object__close(obj);
2080 *prog_fd = bpf_program__fd(first_prog);