2 * Common eBPF ELF object loading operations.
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
17 #include <asm/unistd.h>
18 #include <linux/kernel.h>
19 #include <linux/bpf.h>
20 #include <linux/list.h>
27 #define __printf(a, b) __attribute__((format(printf, a, b)))
30 static int __base_pr(const char *format
, ...)
35 va_start(args
, format
);
36 err
= vfprintf(stderr
, format
, args
);
41 static __printf(1, 2) libbpf_print_fn_t __pr_warning
= __base_pr
;
42 static __printf(1, 2) libbpf_print_fn_t __pr_info
= __base_pr
;
43 static __printf(1, 2) libbpf_print_fn_t __pr_debug
;
45 #define __pr(func, fmt, ...) \
48 (func)("libbpf: " fmt, ##__VA_ARGS__); \
51 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
52 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
53 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
55 void libbpf_set_print(libbpf_print_fn_t warn
,
56 libbpf_print_fn_t info
,
57 libbpf_print_fn_t debug
)
64 #define STRERR_BUFSIZE 128
66 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
67 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
68 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
70 static const char *libbpf_strerror_table
[NR_ERRNO
] = {
71 [ERRCODE_OFFSET(LIBELF
)] = "Something wrong in libelf",
72 [ERRCODE_OFFSET(FORMAT
)] = "BPF object format invalid",
73 [ERRCODE_OFFSET(KVERSION
)] = "'version' section incorrect or lost",
74 [ERRCODE_OFFSET(ENDIAN
)] = "Endian missmatch",
75 [ERRCODE_OFFSET(INTERNAL
)] = "Internal error in libbpf",
76 [ERRCODE_OFFSET(RELOC
)] = "Relocation failed",
77 [ERRCODE_OFFSET(VERIFY
)] = "Kernel verifier blocks program loading",
78 [ERRCODE_OFFSET(PROG2BIG
)] = "Program too big",
79 [ERRCODE_OFFSET(KVER
)] = "Incorrect kernel version",
82 int libbpf_strerror(int err
, char *buf
, size_t size
)
87 err
= err
> 0 ? err
: -err
;
89 if (err
< __LIBBPF_ERRNO__START
) {
92 ret
= strerror_r(err
, buf
, size
);
97 if (err
< __LIBBPF_ERRNO__END
) {
100 msg
= libbpf_strerror_table
[ERRNO_OFFSET(err
)];
101 snprintf(buf
, size
, "%s", msg
);
102 buf
[size
- 1] = '\0';
106 snprintf(buf
, size
, "Unknown libbpf error %d", err
);
107 buf
[size
- 1] = '\0';
111 #define CHECK_ERR(action, err, out) do { \
118 /* Copied from tools/perf/util/util.h */
120 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
124 # define zclose(fd) ({ \
127 ___err = close((fd)); \
132 #ifdef HAVE_LIBELF_MMAP_SUPPORT
133 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
135 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
139 * bpf_prog should be a better name but it has been used in
143 /* Index in elf obj file, for relocation use. */
146 struct bpf_insn
*insns
;
159 bpf_program_prep_t preprocessor
;
161 struct bpf_object
*obj
;
163 bpf_program_clear_priv_t clear_priv
;
169 struct bpf_map_def def
;
171 bpf_map_clear_priv_t clear_priv
;
174 static LIST_HEAD(bpf_objects_list
);
180 struct bpf_program
*programs
;
182 struct bpf_map
*maps
;
188 * Information when doing elf related work. Only valid if fd
206 * All loaded bpf_object is linked in a list, which is
207 * hidden to caller. bpf_objects__<func> handlers deal with
210 struct list_head list
;
213 #define obj_elf_valid(o) ((o)->efile.elf)
215 static void bpf_program__unload(struct bpf_program
*prog
)
223 * If the object is opened but the program was never loaded,
224 * it is possible that prog->instances.nr == -1.
226 if (prog
->instances
.nr
> 0) {
227 for (i
= 0; i
< prog
->instances
.nr
; i
++)
228 zclose(prog
->instances
.fds
[i
]);
229 } else if (prog
->instances
.nr
!= -1) {
230 pr_warning("Internal error: instances.nr is %d\n",
234 prog
->instances
.nr
= -1;
235 zfree(&prog
->instances
.fds
);
238 static void bpf_program__exit(struct bpf_program
*prog
)
243 if (prog
->clear_priv
)
244 prog
->clear_priv(prog
, prog
->priv
);
247 prog
->clear_priv
= NULL
;
249 bpf_program__unload(prog
);
250 zfree(&prog
->section_name
);
252 zfree(&prog
->reloc_desc
);
260 bpf_program__init(void *data
, size_t size
, char *name
, int idx
,
261 struct bpf_program
*prog
)
263 if (size
< sizeof(struct bpf_insn
)) {
264 pr_warning("corrupted section '%s'\n", name
);
268 bzero(prog
, sizeof(*prog
));
270 prog
->section_name
= strdup(name
);
271 if (!prog
->section_name
) {
272 pr_warning("failed to alloc name for prog %s\n",
277 prog
->insns
= malloc(size
);
279 pr_warning("failed to alloc insns for %s\n", name
);
282 prog
->insns_cnt
= size
/ sizeof(struct bpf_insn
);
283 memcpy(prog
->insns
, data
,
284 prog
->insns_cnt
* sizeof(struct bpf_insn
));
286 prog
->instances
.fds
= NULL
;
287 prog
->instances
.nr
= -1;
291 bpf_program__exit(prog
);
296 bpf_object__add_program(struct bpf_object
*obj
, void *data
, size_t size
,
299 struct bpf_program prog
, *progs
;
302 err
= bpf_program__init(data
, size
, name
, idx
, &prog
);
306 progs
= obj
->programs
;
307 nr_progs
= obj
->nr_programs
;
309 progs
= realloc(progs
, sizeof(progs
[0]) * (nr_progs
+ 1));
312 * In this case the original obj->programs
313 * is still valid, so don't need special treat for
314 * bpf_close_object().
316 pr_warning("failed to alloc a new program '%s'\n",
318 bpf_program__exit(&prog
);
322 pr_debug("found program %s\n", prog
.section_name
);
323 obj
->programs
= progs
;
324 obj
->nr_programs
= nr_progs
+ 1;
326 progs
[nr_progs
] = prog
;
330 static struct bpf_object
*bpf_object__new(const char *path
,
334 struct bpf_object
*obj
;
336 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
338 pr_warning("alloc memory failed for %s\n", path
);
339 return ERR_PTR(-ENOMEM
);
342 strcpy(obj
->path
, path
);
346 * Caller of this function should also calls
347 * bpf_object__elf_finish() after data collection to return
348 * obj_buf to user. If not, we should duplicate the buffer to
349 * avoid user freeing them before elf finish.
351 obj
->efile
.obj_buf
= obj_buf
;
352 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
356 INIT_LIST_HEAD(&obj
->list
);
357 list_add(&obj
->list
, &bpf_objects_list
);
361 static void bpf_object__elf_finish(struct bpf_object
*obj
)
363 if (!obj_elf_valid(obj
))
366 if (obj
->efile
.elf
) {
367 elf_end(obj
->efile
.elf
);
368 obj
->efile
.elf
= NULL
;
370 obj
->efile
.symbols
= NULL
;
372 zfree(&obj
->efile
.reloc
);
373 obj
->efile
.nr_reloc
= 0;
374 zclose(obj
->efile
.fd
);
375 obj
->efile
.obj_buf
= NULL
;
376 obj
->efile
.obj_buf_sz
= 0;
379 static int bpf_object__elf_init(struct bpf_object
*obj
)
384 if (obj_elf_valid(obj
)) {
385 pr_warning("elf init: internal error\n");
386 return -LIBBPF_ERRNO__LIBELF
;
389 if (obj
->efile
.obj_buf_sz
> 0) {
391 * obj_buf should have been validated by
392 * bpf_object__open_buffer().
394 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
395 obj
->efile
.obj_buf_sz
);
397 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
398 if (obj
->efile
.fd
< 0) {
399 pr_warning("failed to open %s: %s\n", obj
->path
,
404 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
405 LIBBPF_ELF_C_READ_MMAP
,
409 if (!obj
->efile
.elf
) {
410 pr_warning("failed to open %s as ELF file\n",
412 err
= -LIBBPF_ERRNO__LIBELF
;
416 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
417 pr_warning("failed to get EHDR from %s\n",
419 err
= -LIBBPF_ERRNO__FORMAT
;
422 ep
= &obj
->efile
.ehdr
;
424 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
!= 0)) {
425 pr_warning("%s is not an eBPF object file\n",
427 err
= -LIBBPF_ERRNO__FORMAT
;
433 bpf_object__elf_finish(obj
);
438 bpf_object__check_endianness(struct bpf_object
*obj
)
440 static unsigned int const endian
= 1;
442 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
444 /* We are big endian, BPF obj is little endian. */
445 if (*(unsigned char const *)&endian
!= 1)
450 /* We are little endian, BPF obj is big endian. */
451 if (*(unsigned char const *)&endian
!= 0)
455 return -LIBBPF_ERRNO__ENDIAN
;
461 pr_warning("Error: endianness mismatch.\n");
462 return -LIBBPF_ERRNO__ENDIAN
;
466 bpf_object__init_license(struct bpf_object
*obj
,
467 void *data
, size_t size
)
469 memcpy(obj
->license
, data
,
470 min(size
, sizeof(obj
->license
) - 1));
471 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
476 bpf_object__init_kversion(struct bpf_object
*obj
,
477 void *data
, size_t size
)
481 if (size
!= sizeof(kver
)) {
482 pr_warning("invalid kver section in %s\n", obj
->path
);
483 return -LIBBPF_ERRNO__FORMAT
;
485 memcpy(&kver
, data
, sizeof(kver
));
486 obj
->kern_version
= kver
;
487 pr_debug("kernel version of %s is %x\n", obj
->path
,
493 bpf_object__init_maps(struct bpf_object
*obj
, void *data
,
499 nr_maps
= size
/ sizeof(struct bpf_map_def
);
500 if (!data
|| !nr_maps
) {
501 pr_debug("%s doesn't need map definition\n",
506 pr_debug("maps in %s: %zd bytes\n", obj
->path
, size
);
508 obj
->maps
= calloc(nr_maps
, sizeof(obj
->maps
[0]));
510 pr_warning("alloc maps for object failed\n");
513 obj
->nr_maps
= nr_maps
;
515 for (i
= 0; i
< nr_maps
; i
++) {
516 struct bpf_map_def
*def
= &obj
->maps
[i
].def
;
519 * fill all fd with -1 so won't close incorrect
520 * fd (fd=0 is stdin) when failure (zclose won't close
523 obj
->maps
[i
].fd
= -1;
525 /* Save map definition into obj->maps */
526 *def
= ((struct bpf_map_def
*)data
)[i
];
532 bpf_object__init_maps_name(struct bpf_object
*obj
, int maps_shndx
)
535 Elf_Data
*symbols
= obj
->efile
.symbols
;
537 if (!symbols
|| maps_shndx
< 0)
540 for (i
= 0; i
< symbols
->d_size
/ sizeof(GElf_Sym
); i
++) {
543 const char *map_name
;
545 if (!gelf_getsym(symbols
, i
, &sym
))
547 if (sym
.st_shndx
!= maps_shndx
)
550 map_name
= elf_strptr(obj
->efile
.elf
,
551 obj
->efile
.strtabidx
,
553 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
554 if (map_idx
>= obj
->nr_maps
) {
555 pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
556 map_name
, map_idx
, obj
->nr_maps
);
559 obj
->maps
[map_idx
].name
= strdup(map_name
);
560 if (!obj
->maps
[map_idx
].name
) {
561 pr_warning("failed to alloc map name\n");
564 pr_debug("map %zu is \"%s\"\n", map_idx
,
565 obj
->maps
[map_idx
].name
);
570 static int bpf_object__elf_collect(struct bpf_object
*obj
)
572 Elf
*elf
= obj
->efile
.elf
;
573 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
575 int idx
= 0, err
= 0, maps_shndx
= -1;
577 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
578 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
579 pr_warning("failed to get e_shstrndx from %s\n",
581 return -LIBBPF_ERRNO__FORMAT
;
584 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
590 if (gelf_getshdr(scn
, &sh
) != &sh
) {
591 pr_warning("failed to get section header from %s\n",
593 err
= -LIBBPF_ERRNO__FORMAT
;
597 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
599 pr_warning("failed to get section name from %s\n",
601 err
= -LIBBPF_ERRNO__FORMAT
;
605 data
= elf_getdata(scn
, 0);
607 pr_warning("failed to get section data from %s(%s)\n",
609 err
= -LIBBPF_ERRNO__FORMAT
;
612 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
613 name
, (unsigned long)data
->d_size
,
614 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
617 if (strcmp(name
, "license") == 0)
618 err
= bpf_object__init_license(obj
,
621 else if (strcmp(name
, "version") == 0)
622 err
= bpf_object__init_kversion(obj
,
625 else if (strcmp(name
, "maps") == 0) {
626 err
= bpf_object__init_maps(obj
, data
->d_buf
,
629 } else if (sh
.sh_type
== SHT_SYMTAB
) {
630 if (obj
->efile
.symbols
) {
631 pr_warning("bpf: multiple SYMTAB in %s\n",
633 err
= -LIBBPF_ERRNO__FORMAT
;
635 obj
->efile
.symbols
= data
;
636 obj
->efile
.strtabidx
= sh
.sh_link
;
638 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
639 (sh
.sh_flags
& SHF_EXECINSTR
) &&
640 (data
->d_size
> 0)) {
641 err
= bpf_object__add_program(obj
, data
->d_buf
,
642 data
->d_size
, name
, idx
);
644 char errmsg
[STRERR_BUFSIZE
];
646 strerror_r(-err
, errmsg
, sizeof(errmsg
));
647 pr_warning("failed to alloc program %s (%s): %s",
648 name
, obj
->path
, errmsg
);
650 } else if (sh
.sh_type
== SHT_REL
) {
651 void *reloc
= obj
->efile
.reloc
;
652 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
654 reloc
= realloc(reloc
,
655 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
657 pr_warning("realloc failed\n");
660 int n
= nr_reloc
- 1;
662 obj
->efile
.reloc
= reloc
;
663 obj
->efile
.nr_reloc
= nr_reloc
;
665 obj
->efile
.reloc
[n
].shdr
= sh
;
666 obj
->efile
.reloc
[n
].data
= data
;
673 if (!obj
->efile
.strtabidx
|| obj
->efile
.strtabidx
>= idx
) {
674 pr_warning("Corrupted ELF file: index of strtab invalid\n");
675 return LIBBPF_ERRNO__FORMAT
;
678 err
= bpf_object__init_maps_name(obj
, maps_shndx
);
683 static struct bpf_program
*
684 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
686 struct bpf_program
*prog
;
689 for (i
= 0; i
< obj
->nr_programs
; i
++) {
690 prog
= &obj
->programs
[i
];
691 if (prog
->idx
== idx
)
698 bpf_program__collect_reloc(struct bpf_program
*prog
,
699 size_t nr_maps
, GElf_Shdr
*shdr
,
700 Elf_Data
*data
, Elf_Data
*symbols
)
704 pr_debug("collecting relocating info for: '%s'\n",
706 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
708 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
709 if (!prog
->reloc_desc
) {
710 pr_warning("failed to alloc memory in relocation\n");
713 prog
->nr_reloc
= nrels
;
715 for (i
= 0; i
< nrels
; i
++) {
718 unsigned int insn_idx
;
719 struct bpf_insn
*insns
= prog
->insns
;
722 if (!gelf_getrel(data
, i
, &rel
)) {
723 pr_warning("relocation: failed to get %d reloc\n", i
);
724 return -LIBBPF_ERRNO__FORMAT
;
727 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
728 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
730 if (!gelf_getsym(symbols
,
731 GELF_R_SYM(rel
.r_info
),
733 pr_warning("relocation: symbol %"PRIx64
" not found\n",
734 GELF_R_SYM(rel
.r_info
));
735 return -LIBBPF_ERRNO__FORMAT
;
738 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
739 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
740 insn_idx
, insns
[insn_idx
].code
);
741 return -LIBBPF_ERRNO__RELOC
;
744 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
745 if (map_idx
>= nr_maps
) {
746 pr_warning("bpf relocation: map_idx %d large than %d\n",
747 (int)map_idx
, (int)nr_maps
- 1);
748 return -LIBBPF_ERRNO__RELOC
;
751 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
752 prog
->reloc_desc
[i
].map_idx
= map_idx
;
758 bpf_object__create_maps(struct bpf_object
*obj
)
762 for (i
= 0; i
< obj
->nr_maps
; i
++) {
763 struct bpf_map_def
*def
= &obj
->maps
[i
].def
;
764 int *pfd
= &obj
->maps
[i
].fd
;
766 *pfd
= bpf_create_map(def
->type
,
774 pr_warning("failed to create map: %s\n",
776 for (j
= 0; j
< i
; j
++)
777 zclose(obj
->maps
[j
].fd
);
780 pr_debug("create map: fd=%d\n", *pfd
);
787 bpf_program__relocate(struct bpf_program
*prog
, struct bpf_object
*obj
)
791 if (!prog
|| !prog
->reloc_desc
)
794 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
795 int insn_idx
, map_idx
;
796 struct bpf_insn
*insns
= prog
->insns
;
798 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
799 map_idx
= prog
->reloc_desc
[i
].map_idx
;
801 if (insn_idx
>= (int)prog
->insns_cnt
) {
802 pr_warning("relocation out of range: '%s'\n",
804 return -LIBBPF_ERRNO__RELOC
;
806 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
807 insns
[insn_idx
].imm
= obj
->maps
[map_idx
].fd
;
810 zfree(&prog
->reloc_desc
);
817 bpf_object__relocate(struct bpf_object
*obj
)
819 struct bpf_program
*prog
;
823 for (i
= 0; i
< obj
->nr_programs
; i
++) {
824 prog
= &obj
->programs
[i
];
826 err
= bpf_program__relocate(prog
, obj
);
828 pr_warning("failed to relocate '%s'\n",
836 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
840 if (!obj_elf_valid(obj
)) {
841 pr_warning("Internal error: elf object is closed\n");
842 return -LIBBPF_ERRNO__INTERNAL
;
845 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
846 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
847 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
848 int idx
= shdr
->sh_info
;
849 struct bpf_program
*prog
;
850 size_t nr_maps
= obj
->nr_maps
;
852 if (shdr
->sh_type
!= SHT_REL
) {
853 pr_warning("internal error at %d\n", __LINE__
);
854 return -LIBBPF_ERRNO__INTERNAL
;
857 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
859 pr_warning("relocation failed: no %d section\n",
861 return -LIBBPF_ERRNO__RELOC
;
864 err
= bpf_program__collect_reloc(prog
, nr_maps
,
874 load_program(struct bpf_insn
*insns
, int insns_cnt
,
875 char *license
, u32 kern_version
, int *pfd
)
880 if (!insns
|| !insns_cnt
)
883 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
885 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
887 ret
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
888 insns_cnt
, license
, kern_version
,
889 log_buf
, BPF_LOG_BUF_SIZE
);
897 ret
= -LIBBPF_ERRNO__LOAD
;
898 pr_warning("load bpf program failed: %s\n", strerror(errno
));
900 if (log_buf
&& log_buf
[0] != '\0') {
901 ret
= -LIBBPF_ERRNO__VERIFY
;
902 pr_warning("-- BEGIN DUMP LOG ---\n");
903 pr_warning("\n%s\n", log_buf
);
904 pr_warning("-- END LOG --\n");
906 if (insns_cnt
>= BPF_MAXINSNS
) {
907 pr_warning("Program too large (%d insns), at most %d insns\n",
908 insns_cnt
, BPF_MAXINSNS
);
909 ret
= -LIBBPF_ERRNO__PROG2BIG
;
910 } else if (log_buf
) {
911 pr_warning("log buffer is empty\n");
912 ret
= -LIBBPF_ERRNO__KVER
;
922 bpf_program__load(struct bpf_program
*prog
,
923 char *license
, u32 kern_version
)
927 if (prog
->instances
.nr
< 0 || !prog
->instances
.fds
) {
928 if (prog
->preprocessor
) {
929 pr_warning("Internal error: can't load program '%s'\n",
931 return -LIBBPF_ERRNO__INTERNAL
;
934 prog
->instances
.fds
= malloc(sizeof(int));
935 if (!prog
->instances
.fds
) {
936 pr_warning("Not enough memory for BPF fds\n");
939 prog
->instances
.nr
= 1;
940 prog
->instances
.fds
[0] = -1;
943 if (!prog
->preprocessor
) {
944 if (prog
->instances
.nr
!= 1) {
945 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
946 prog
->section_name
, prog
->instances
.nr
);
948 err
= load_program(prog
->insns
, prog
->insns_cnt
,
949 license
, kern_version
, &fd
);
951 prog
->instances
.fds
[0] = fd
;
955 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
956 struct bpf_prog_prep_result result
;
957 bpf_program_prep_t preprocessor
= prog
->preprocessor
;
959 bzero(&result
, sizeof(result
));
960 err
= preprocessor(prog
, i
, prog
->insns
,
961 prog
->insns_cnt
, &result
);
963 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
964 i
, prog
->section_name
);
968 if (!result
.new_insn_ptr
|| !result
.new_insn_cnt
) {
969 pr_debug("Skip loading the %dth instance of program '%s'\n",
970 i
, prog
->section_name
);
971 prog
->instances
.fds
[i
] = -1;
977 err
= load_program(result
.new_insn_ptr
,
979 license
, kern_version
, &fd
);
982 pr_warning("Loading the %dth instance of program '%s' failed\n",
983 i
, prog
->section_name
);
989 prog
->instances
.fds
[i
] = fd
;
993 pr_warning("failed to load program '%s'\n",
1001 bpf_object__load_progs(struct bpf_object
*obj
)
1006 for (i
= 0; i
< obj
->nr_programs
; i
++) {
1007 err
= bpf_program__load(&obj
->programs
[i
],
1016 static int bpf_object__validate(struct bpf_object
*obj
)
1018 if (obj
->kern_version
== 0) {
1019 pr_warning("%s doesn't provide kernel version\n",
1021 return -LIBBPF_ERRNO__KVERSION
;
1026 static struct bpf_object
*
1027 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
1029 struct bpf_object
*obj
;
1032 if (elf_version(EV_CURRENT
) == EV_NONE
) {
1033 pr_warning("failed to init libelf for %s\n", path
);
1034 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
1037 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
1041 CHECK_ERR(bpf_object__elf_init(obj
), err
, out
);
1042 CHECK_ERR(bpf_object__check_endianness(obj
), err
, out
);
1043 CHECK_ERR(bpf_object__elf_collect(obj
), err
, out
);
1044 CHECK_ERR(bpf_object__collect_reloc(obj
), err
, out
);
1045 CHECK_ERR(bpf_object__validate(obj
), err
, out
);
1047 bpf_object__elf_finish(obj
);
1050 bpf_object__close(obj
);
1051 return ERR_PTR(err
);
1054 struct bpf_object
*bpf_object__open(const char *path
)
1056 /* param validation */
1060 pr_debug("loading %s\n", path
);
1062 return __bpf_object__open(path
, NULL
, 0);
1065 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
1071 /* param validation */
1072 if (!obj_buf
|| obj_buf_sz
<= 0)
1076 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
1077 (unsigned long)obj_buf
,
1078 (unsigned long)obj_buf_sz
);
1079 tmp_name
[sizeof(tmp_name
) - 1] = '\0';
1082 pr_debug("loading object '%s' from buffer\n",
1085 return __bpf_object__open(name
, obj_buf
, obj_buf_sz
);
1088 int bpf_object__unload(struct bpf_object
*obj
)
1095 for (i
= 0; i
< obj
->nr_maps
; i
++)
1096 zclose(obj
->maps
[i
].fd
);
1098 for (i
= 0; i
< obj
->nr_programs
; i
++)
1099 bpf_program__unload(&obj
->programs
[i
]);
1104 int bpf_object__load(struct bpf_object
*obj
)
1112 pr_warning("object should not be loaded twice\n");
1118 CHECK_ERR(bpf_object__create_maps(obj
), err
, out
);
1119 CHECK_ERR(bpf_object__relocate(obj
), err
, out
);
1120 CHECK_ERR(bpf_object__load_progs(obj
), err
, out
);
1124 bpf_object__unload(obj
);
1125 pr_warning("failed to load object '%s'\n", obj
->path
);
1129 void bpf_object__close(struct bpf_object
*obj
)
1136 bpf_object__elf_finish(obj
);
1137 bpf_object__unload(obj
);
1139 for (i
= 0; i
< obj
->nr_maps
; i
++) {
1140 zfree(&obj
->maps
[i
].name
);
1141 if (obj
->maps
[i
].clear_priv
)
1142 obj
->maps
[i
].clear_priv(&obj
->maps
[i
],
1144 obj
->maps
[i
].priv
= NULL
;
1145 obj
->maps
[i
].clear_priv
= NULL
;
1150 if (obj
->programs
&& obj
->nr_programs
) {
1151 for (i
= 0; i
< obj
->nr_programs
; i
++)
1152 bpf_program__exit(&obj
->programs
[i
]);
1154 zfree(&obj
->programs
);
1156 list_del(&obj
->list
);
1161 bpf_object__next(struct bpf_object
*prev
)
1163 struct bpf_object
*next
;
1166 next
= list_first_entry(&bpf_objects_list
,
1170 next
= list_next_entry(prev
, list
);
1172 /* Empty list is noticed here so don't need checking on entry. */
1173 if (&next
->list
== &bpf_objects_list
)
1180 bpf_object__get_name(struct bpf_object
*obj
)
1183 return ERR_PTR(-EINVAL
);
1188 bpf_object__get_kversion(struct bpf_object
*obj
)
1192 return obj
->kern_version
;
1195 struct bpf_program
*
1196 bpf_program__next(struct bpf_program
*prev
, struct bpf_object
*obj
)
1204 return &obj
->programs
[0];
1206 if (prev
->obj
!= obj
) {
1207 pr_warning("error: program handler doesn't match object\n");
1211 idx
= (prev
- obj
->programs
) + 1;
1212 if (idx
>= obj
->nr_programs
)
1214 return &obj
->programs
[idx
];
1217 int bpf_program__set_private(struct bpf_program
*prog
,
1219 bpf_program_clear_priv_t clear_priv
)
1221 if (prog
->priv
&& prog
->clear_priv
)
1222 prog
->clear_priv(prog
, prog
->priv
);
1225 prog
->clear_priv
= clear_priv
;
1229 int bpf_program__get_private(struct bpf_program
*prog
, void **ppriv
)
1231 *ppriv
= prog
->priv
;
1235 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
)
1239 title
= prog
->section_name
;
1241 title
= strdup(title
);
1243 pr_warning("failed to strdup program title\n");
1244 return ERR_PTR(-ENOMEM
);
1251 int bpf_program__fd(struct bpf_program
*prog
)
1253 return bpf_program__nth_fd(prog
, 0);
1256 int bpf_program__set_prep(struct bpf_program
*prog
, int nr_instances
,
1257 bpf_program_prep_t prep
)
1261 if (nr_instances
<= 0 || !prep
)
1264 if (prog
->instances
.nr
> 0 || prog
->instances
.fds
) {
1265 pr_warning("Can't set pre-processor after loading\n");
1269 instances_fds
= malloc(sizeof(int) * nr_instances
);
1270 if (!instances_fds
) {
1271 pr_warning("alloc memory failed for fds\n");
1275 /* fill all fd with -1 */
1276 memset(instances_fds
, -1, sizeof(int) * nr_instances
);
1278 prog
->instances
.nr
= nr_instances
;
1279 prog
->instances
.fds
= instances_fds
;
1280 prog
->preprocessor
= prep
;
1284 int bpf_program__nth_fd(struct bpf_program
*prog
, int n
)
1288 if (n
>= prog
->instances
.nr
|| n
< 0) {
1289 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1290 n
, prog
->section_name
, prog
->instances
.nr
);
1294 fd
= prog
->instances
.fds
[n
];
1296 pr_warning("%dth instance of program '%s' is invalid\n",
1297 n
, prog
->section_name
);
1304 int bpf_map__get_fd(struct bpf_map
*map
)
1312 int bpf_map__get_def(struct bpf_map
*map
, struct bpf_map_def
*pdef
)
1321 const char *bpf_map__get_name(struct bpf_map
*map
)
1328 int bpf_map__set_private(struct bpf_map
*map
, void *priv
,
1329 bpf_map_clear_priv_t clear_priv
)
1335 if (map
->clear_priv
)
1336 map
->clear_priv(map
, map
->priv
);
1340 map
->clear_priv
= clear_priv
;
1344 int bpf_map__get_private(struct bpf_map
*map
, void **ppriv
)
1355 bpf_map__next(struct bpf_map
*prev
, struct bpf_object
*obj
)
1358 struct bpf_map
*s
, *e
;
1360 if (!obj
|| !obj
->maps
)
1364 e
= obj
->maps
+ obj
->nr_maps
;
1369 if ((prev
< s
) || (prev
>= e
)) {
1370 pr_warning("error in %s: map handler doesn't belong to object\n",
1375 idx
= (prev
- obj
->maps
) + 1;
1376 if (idx
>= obj
->nr_maps
)
1378 return &obj
->maps
[idx
];
1382 bpf_object__get_map_by_name(struct bpf_object
*obj
, const char *name
)
1384 struct bpf_map
*pos
;
1386 bpf_map__for_each(pos
, obj
) {
1387 if (pos
->name
&& !strcmp(pos
->name
, name
))