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
207 * All loaded bpf_object is linked in a list, which is
208 * hidden to caller. bpf_objects__<func> handlers deal with
211 struct list_head list
;
214 #define obj_elf_valid(o) ((o)->efile.elf)
216 static void bpf_program__unload(struct bpf_program
*prog
)
224 * If the object is opened but the program was never loaded,
225 * it is possible that prog->instances.nr == -1.
227 if (prog
->instances
.nr
> 0) {
228 for (i
= 0; i
< prog
->instances
.nr
; i
++)
229 zclose(prog
->instances
.fds
[i
]);
230 } else if (prog
->instances
.nr
!= -1) {
231 pr_warning("Internal error: instances.nr is %d\n",
235 prog
->instances
.nr
= -1;
236 zfree(&prog
->instances
.fds
);
239 static void bpf_program__exit(struct bpf_program
*prog
)
244 if (prog
->clear_priv
)
245 prog
->clear_priv(prog
, prog
->priv
);
248 prog
->clear_priv
= NULL
;
250 bpf_program__unload(prog
);
251 zfree(&prog
->section_name
);
253 zfree(&prog
->reloc_desc
);
261 bpf_program__init(void *data
, size_t size
, char *name
, int idx
,
262 struct bpf_program
*prog
)
264 if (size
< sizeof(struct bpf_insn
)) {
265 pr_warning("corrupted section '%s'\n", name
);
269 bzero(prog
, sizeof(*prog
));
271 prog
->section_name
= strdup(name
);
272 if (!prog
->section_name
) {
273 pr_warning("failed to alloc name for prog %s\n",
278 prog
->insns
= malloc(size
);
280 pr_warning("failed to alloc insns for %s\n", name
);
283 prog
->insns_cnt
= size
/ sizeof(struct bpf_insn
);
284 memcpy(prog
->insns
, data
,
285 prog
->insns_cnt
* sizeof(struct bpf_insn
));
287 prog
->instances
.fds
= NULL
;
288 prog
->instances
.nr
= -1;
292 bpf_program__exit(prog
);
297 bpf_object__add_program(struct bpf_object
*obj
, void *data
, size_t size
,
300 struct bpf_program prog
, *progs
;
303 err
= bpf_program__init(data
, size
, name
, idx
, &prog
);
307 progs
= obj
->programs
;
308 nr_progs
= obj
->nr_programs
;
310 progs
= realloc(progs
, sizeof(progs
[0]) * (nr_progs
+ 1));
313 * In this case the original obj->programs
314 * is still valid, so don't need special treat for
315 * bpf_close_object().
317 pr_warning("failed to alloc a new program '%s'\n",
319 bpf_program__exit(&prog
);
323 pr_debug("found program %s\n", prog
.section_name
);
324 obj
->programs
= progs
;
325 obj
->nr_programs
= nr_progs
+ 1;
327 progs
[nr_progs
] = prog
;
331 static struct bpf_object
*bpf_object__new(const char *path
,
335 struct bpf_object
*obj
;
337 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
339 pr_warning("alloc memory failed for %s\n", path
);
340 return ERR_PTR(-ENOMEM
);
343 strcpy(obj
->path
, path
);
347 * Caller of this function should also calls
348 * bpf_object__elf_finish() after data collection to return
349 * obj_buf to user. If not, we should duplicate the buffer to
350 * avoid user freeing them before elf finish.
352 obj
->efile
.obj_buf
= obj_buf
;
353 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
354 obj
->efile
.maps_shndx
= -1;
358 INIT_LIST_HEAD(&obj
->list
);
359 list_add(&obj
->list
, &bpf_objects_list
);
363 static void bpf_object__elf_finish(struct bpf_object
*obj
)
365 if (!obj_elf_valid(obj
))
368 if (obj
->efile
.elf
) {
369 elf_end(obj
->efile
.elf
);
370 obj
->efile
.elf
= NULL
;
372 obj
->efile
.symbols
= NULL
;
374 zfree(&obj
->efile
.reloc
);
375 obj
->efile
.nr_reloc
= 0;
376 zclose(obj
->efile
.fd
);
377 obj
->efile
.obj_buf
= NULL
;
378 obj
->efile
.obj_buf_sz
= 0;
381 static int bpf_object__elf_init(struct bpf_object
*obj
)
386 if (obj_elf_valid(obj
)) {
387 pr_warning("elf init: internal error\n");
388 return -LIBBPF_ERRNO__LIBELF
;
391 if (obj
->efile
.obj_buf_sz
> 0) {
393 * obj_buf should have been validated by
394 * bpf_object__open_buffer().
396 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
397 obj
->efile
.obj_buf_sz
);
399 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
400 if (obj
->efile
.fd
< 0) {
401 pr_warning("failed to open %s: %s\n", obj
->path
,
406 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
407 LIBBPF_ELF_C_READ_MMAP
,
411 if (!obj
->efile
.elf
) {
412 pr_warning("failed to open %s as ELF file\n",
414 err
= -LIBBPF_ERRNO__LIBELF
;
418 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
419 pr_warning("failed to get EHDR from %s\n",
421 err
= -LIBBPF_ERRNO__FORMAT
;
424 ep
= &obj
->efile
.ehdr
;
426 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
!= 0)) {
427 pr_warning("%s is not an eBPF object file\n",
429 err
= -LIBBPF_ERRNO__FORMAT
;
435 bpf_object__elf_finish(obj
);
440 bpf_object__check_endianness(struct bpf_object
*obj
)
442 static unsigned int const endian
= 1;
444 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
446 /* We are big endian, BPF obj is little endian. */
447 if (*(unsigned char const *)&endian
!= 1)
452 /* We are little endian, BPF obj is big endian. */
453 if (*(unsigned char const *)&endian
!= 0)
457 return -LIBBPF_ERRNO__ENDIAN
;
463 pr_warning("Error: endianness mismatch.\n");
464 return -LIBBPF_ERRNO__ENDIAN
;
468 bpf_object__init_license(struct bpf_object
*obj
,
469 void *data
, size_t size
)
471 memcpy(obj
->license
, data
,
472 min(size
, sizeof(obj
->license
) - 1));
473 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
478 bpf_object__init_kversion(struct bpf_object
*obj
,
479 void *data
, size_t size
)
483 if (size
!= sizeof(kver
)) {
484 pr_warning("invalid kver section in %s\n", obj
->path
);
485 return -LIBBPF_ERRNO__FORMAT
;
487 memcpy(&kver
, data
, sizeof(kver
));
488 obj
->kern_version
= kver
;
489 pr_debug("kernel version of %s is %x\n", obj
->path
,
495 bpf_object__init_maps(struct bpf_object
*obj
, void *data
,
501 nr_maps
= size
/ sizeof(struct bpf_map_def
);
502 if (!data
|| !nr_maps
) {
503 pr_debug("%s doesn't need map definition\n",
508 pr_debug("maps in %s: %zd bytes\n", obj
->path
, size
);
510 obj
->maps
= calloc(nr_maps
, sizeof(obj
->maps
[0]));
512 pr_warning("alloc maps for object failed\n");
515 obj
->nr_maps
= nr_maps
;
517 for (i
= 0; i
< nr_maps
; i
++) {
518 struct bpf_map_def
*def
= &obj
->maps
[i
].def
;
521 * fill all fd with -1 so won't close incorrect
522 * fd (fd=0 is stdin) when failure (zclose won't close
525 obj
->maps
[i
].fd
= -1;
527 /* Save map definition into obj->maps */
528 *def
= ((struct bpf_map_def
*)data
)[i
];
534 bpf_object__init_maps_name(struct bpf_object
*obj
)
537 Elf_Data
*symbols
= obj
->efile
.symbols
;
539 if (!symbols
|| obj
->efile
.maps_shndx
< 0)
542 for (i
= 0; i
< symbols
->d_size
/ sizeof(GElf_Sym
); i
++) {
545 const char *map_name
;
547 if (!gelf_getsym(symbols
, i
, &sym
))
549 if (sym
.st_shndx
!= obj
->efile
.maps_shndx
)
552 map_name
= elf_strptr(obj
->efile
.elf
,
553 obj
->efile
.strtabidx
,
555 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
556 if (map_idx
>= obj
->nr_maps
) {
557 pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
558 map_name
, map_idx
, obj
->nr_maps
);
561 obj
->maps
[map_idx
].name
= strdup(map_name
);
562 if (!obj
->maps
[map_idx
].name
) {
563 pr_warning("failed to alloc map name\n");
566 pr_debug("map %zu is \"%s\"\n", map_idx
,
567 obj
->maps
[map_idx
].name
);
572 static int bpf_object__elf_collect(struct bpf_object
*obj
)
574 Elf
*elf
= obj
->efile
.elf
;
575 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
577 int idx
= 0, err
= 0;
579 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
580 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
581 pr_warning("failed to get e_shstrndx from %s\n",
583 return -LIBBPF_ERRNO__FORMAT
;
586 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
592 if (gelf_getshdr(scn
, &sh
) != &sh
) {
593 pr_warning("failed to get section header from %s\n",
595 err
= -LIBBPF_ERRNO__FORMAT
;
599 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
601 pr_warning("failed to get section name from %s\n",
603 err
= -LIBBPF_ERRNO__FORMAT
;
607 data
= elf_getdata(scn
, 0);
609 pr_warning("failed to get section data from %s(%s)\n",
611 err
= -LIBBPF_ERRNO__FORMAT
;
614 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
615 name
, (unsigned long)data
->d_size
,
616 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
619 if (strcmp(name
, "license") == 0)
620 err
= bpf_object__init_license(obj
,
623 else if (strcmp(name
, "version") == 0)
624 err
= bpf_object__init_kversion(obj
,
627 else if (strcmp(name
, "maps") == 0) {
628 err
= bpf_object__init_maps(obj
, data
->d_buf
,
630 obj
->efile
.maps_shndx
= idx
;
631 } else if (sh
.sh_type
== SHT_SYMTAB
) {
632 if (obj
->efile
.symbols
) {
633 pr_warning("bpf: multiple SYMTAB in %s\n",
635 err
= -LIBBPF_ERRNO__FORMAT
;
637 obj
->efile
.symbols
= data
;
638 obj
->efile
.strtabidx
= sh
.sh_link
;
640 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
641 (sh
.sh_flags
& SHF_EXECINSTR
) &&
642 (data
->d_size
> 0)) {
643 err
= bpf_object__add_program(obj
, data
->d_buf
,
644 data
->d_size
, name
, idx
);
646 char errmsg
[STRERR_BUFSIZE
];
648 strerror_r(-err
, errmsg
, sizeof(errmsg
));
649 pr_warning("failed to alloc program %s (%s): %s",
650 name
, obj
->path
, errmsg
);
652 } else if (sh
.sh_type
== SHT_REL
) {
653 void *reloc
= obj
->efile
.reloc
;
654 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
656 reloc
= realloc(reloc
,
657 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
659 pr_warning("realloc failed\n");
662 int n
= nr_reloc
- 1;
664 obj
->efile
.reloc
= reloc
;
665 obj
->efile
.nr_reloc
= nr_reloc
;
667 obj
->efile
.reloc
[n
].shdr
= sh
;
668 obj
->efile
.reloc
[n
].data
= data
;
675 if (!obj
->efile
.strtabidx
|| obj
->efile
.strtabidx
>= idx
) {
676 pr_warning("Corrupted ELF file: index of strtab invalid\n");
677 return LIBBPF_ERRNO__FORMAT
;
679 if (obj
->efile
.maps_shndx
>= 0)
680 err
= bpf_object__init_maps_name(obj
);
685 static struct bpf_program
*
686 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
688 struct bpf_program
*prog
;
691 for (i
= 0; i
< obj
->nr_programs
; i
++) {
692 prog
= &obj
->programs
[i
];
693 if (prog
->idx
== idx
)
700 bpf_program__collect_reloc(struct bpf_program
*prog
,
701 size_t nr_maps
, GElf_Shdr
*shdr
,
702 Elf_Data
*data
, Elf_Data
*symbols
,
707 pr_debug("collecting relocating info for: '%s'\n",
709 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
711 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
712 if (!prog
->reloc_desc
) {
713 pr_warning("failed to alloc memory in relocation\n");
716 prog
->nr_reloc
= nrels
;
718 for (i
= 0; i
< nrels
; i
++) {
721 unsigned int insn_idx
;
722 struct bpf_insn
*insns
= prog
->insns
;
725 if (!gelf_getrel(data
, i
, &rel
)) {
726 pr_warning("relocation: failed to get %d reloc\n", i
);
727 return -LIBBPF_ERRNO__FORMAT
;
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 (sym
.st_shndx
!= maps_shndx
) {
739 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
740 prog
->section_name
, sym
.st_shndx
);
741 return -LIBBPF_ERRNO__RELOC
;
744 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
745 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
747 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
748 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
749 insn_idx
, insns
[insn_idx
].code
);
750 return -LIBBPF_ERRNO__RELOC
;
753 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
754 if (map_idx
>= nr_maps
) {
755 pr_warning("bpf relocation: map_idx %d large than %d\n",
756 (int)map_idx
, (int)nr_maps
- 1);
757 return -LIBBPF_ERRNO__RELOC
;
760 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
761 prog
->reloc_desc
[i
].map_idx
= map_idx
;
767 bpf_object__create_maps(struct bpf_object
*obj
)
771 for (i
= 0; i
< obj
->nr_maps
; i
++) {
772 struct bpf_map_def
*def
= &obj
->maps
[i
].def
;
773 int *pfd
= &obj
->maps
[i
].fd
;
775 *pfd
= bpf_create_map(def
->type
,
783 pr_warning("failed to create map: %s\n",
785 for (j
= 0; j
< i
; j
++)
786 zclose(obj
->maps
[j
].fd
);
789 pr_debug("create map: fd=%d\n", *pfd
);
796 bpf_program__relocate(struct bpf_program
*prog
, struct bpf_object
*obj
)
800 if (!prog
|| !prog
->reloc_desc
)
803 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
804 int insn_idx
, map_idx
;
805 struct bpf_insn
*insns
= prog
->insns
;
807 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
808 map_idx
= prog
->reloc_desc
[i
].map_idx
;
810 if (insn_idx
>= (int)prog
->insns_cnt
) {
811 pr_warning("relocation out of range: '%s'\n",
813 return -LIBBPF_ERRNO__RELOC
;
815 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
816 insns
[insn_idx
].imm
= obj
->maps
[map_idx
].fd
;
819 zfree(&prog
->reloc_desc
);
826 bpf_object__relocate(struct bpf_object
*obj
)
828 struct bpf_program
*prog
;
832 for (i
= 0; i
< obj
->nr_programs
; i
++) {
833 prog
= &obj
->programs
[i
];
835 err
= bpf_program__relocate(prog
, obj
);
837 pr_warning("failed to relocate '%s'\n",
845 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
849 if (!obj_elf_valid(obj
)) {
850 pr_warning("Internal error: elf object is closed\n");
851 return -LIBBPF_ERRNO__INTERNAL
;
854 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
855 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
856 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
857 int idx
= shdr
->sh_info
;
858 struct bpf_program
*prog
;
859 size_t nr_maps
= obj
->nr_maps
;
861 if (shdr
->sh_type
!= SHT_REL
) {
862 pr_warning("internal error at %d\n", __LINE__
);
863 return -LIBBPF_ERRNO__INTERNAL
;
866 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
868 pr_warning("relocation failed: no %d section\n",
870 return -LIBBPF_ERRNO__RELOC
;
873 err
= bpf_program__collect_reloc(prog
, nr_maps
,
876 obj
->efile
.maps_shndx
);
884 load_program(struct bpf_insn
*insns
, int insns_cnt
,
885 char *license
, u32 kern_version
, int *pfd
)
890 if (!insns
|| !insns_cnt
)
893 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
895 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
897 ret
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
898 insns_cnt
, license
, kern_version
,
899 log_buf
, BPF_LOG_BUF_SIZE
);
907 ret
= -LIBBPF_ERRNO__LOAD
;
908 pr_warning("load bpf program failed: %s\n", strerror(errno
));
910 if (log_buf
&& log_buf
[0] != '\0') {
911 ret
= -LIBBPF_ERRNO__VERIFY
;
912 pr_warning("-- BEGIN DUMP LOG ---\n");
913 pr_warning("\n%s\n", log_buf
);
914 pr_warning("-- END LOG --\n");
916 if (insns_cnt
>= BPF_MAXINSNS
) {
917 pr_warning("Program too large (%d insns), at most %d insns\n",
918 insns_cnt
, BPF_MAXINSNS
);
919 ret
= -LIBBPF_ERRNO__PROG2BIG
;
920 } else if (log_buf
) {
921 pr_warning("log buffer is empty\n");
922 ret
= -LIBBPF_ERRNO__KVER
;
932 bpf_program__load(struct bpf_program
*prog
,
933 char *license
, u32 kern_version
)
937 if (prog
->instances
.nr
< 0 || !prog
->instances
.fds
) {
938 if (prog
->preprocessor
) {
939 pr_warning("Internal error: can't load program '%s'\n",
941 return -LIBBPF_ERRNO__INTERNAL
;
944 prog
->instances
.fds
= malloc(sizeof(int));
945 if (!prog
->instances
.fds
) {
946 pr_warning("Not enough memory for BPF fds\n");
949 prog
->instances
.nr
= 1;
950 prog
->instances
.fds
[0] = -1;
953 if (!prog
->preprocessor
) {
954 if (prog
->instances
.nr
!= 1) {
955 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
956 prog
->section_name
, prog
->instances
.nr
);
958 err
= load_program(prog
->insns
, prog
->insns_cnt
,
959 license
, kern_version
, &fd
);
961 prog
->instances
.fds
[0] = fd
;
965 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
966 struct bpf_prog_prep_result result
;
967 bpf_program_prep_t preprocessor
= prog
->preprocessor
;
969 bzero(&result
, sizeof(result
));
970 err
= preprocessor(prog
, i
, prog
->insns
,
971 prog
->insns_cnt
, &result
);
973 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
974 i
, prog
->section_name
);
978 if (!result
.new_insn_ptr
|| !result
.new_insn_cnt
) {
979 pr_debug("Skip loading the %dth instance of program '%s'\n",
980 i
, prog
->section_name
);
981 prog
->instances
.fds
[i
] = -1;
987 err
= load_program(result
.new_insn_ptr
,
989 license
, kern_version
, &fd
);
992 pr_warning("Loading the %dth instance of program '%s' failed\n",
993 i
, prog
->section_name
);
999 prog
->instances
.fds
[i
] = fd
;
1003 pr_warning("failed to load program '%s'\n",
1004 prog
->section_name
);
1005 zfree(&prog
->insns
);
1006 prog
->insns_cnt
= 0;
1011 bpf_object__load_progs(struct bpf_object
*obj
)
1016 for (i
= 0; i
< obj
->nr_programs
; i
++) {
1017 err
= bpf_program__load(&obj
->programs
[i
],
1026 static int bpf_object__validate(struct bpf_object
*obj
)
1028 if (obj
->kern_version
== 0) {
1029 pr_warning("%s doesn't provide kernel version\n",
1031 return -LIBBPF_ERRNO__KVERSION
;
1036 static struct bpf_object
*
1037 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
1039 struct bpf_object
*obj
;
1042 if (elf_version(EV_CURRENT
) == EV_NONE
) {
1043 pr_warning("failed to init libelf for %s\n", path
);
1044 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
1047 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
1051 CHECK_ERR(bpf_object__elf_init(obj
), err
, out
);
1052 CHECK_ERR(bpf_object__check_endianness(obj
), err
, out
);
1053 CHECK_ERR(bpf_object__elf_collect(obj
), err
, out
);
1054 CHECK_ERR(bpf_object__collect_reloc(obj
), err
, out
);
1055 CHECK_ERR(bpf_object__validate(obj
), err
, out
);
1057 bpf_object__elf_finish(obj
);
1060 bpf_object__close(obj
);
1061 return ERR_PTR(err
);
1064 struct bpf_object
*bpf_object__open(const char *path
)
1066 /* param validation */
1070 pr_debug("loading %s\n", path
);
1072 return __bpf_object__open(path
, NULL
, 0);
1075 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
1081 /* param validation */
1082 if (!obj_buf
|| obj_buf_sz
<= 0)
1086 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
1087 (unsigned long)obj_buf
,
1088 (unsigned long)obj_buf_sz
);
1089 tmp_name
[sizeof(tmp_name
) - 1] = '\0';
1092 pr_debug("loading object '%s' from buffer\n",
1095 return __bpf_object__open(name
, obj_buf
, obj_buf_sz
);
1098 int bpf_object__unload(struct bpf_object
*obj
)
1105 for (i
= 0; i
< obj
->nr_maps
; i
++)
1106 zclose(obj
->maps
[i
].fd
);
1108 for (i
= 0; i
< obj
->nr_programs
; i
++)
1109 bpf_program__unload(&obj
->programs
[i
]);
1114 int bpf_object__load(struct bpf_object
*obj
)
1122 pr_warning("object should not be loaded twice\n");
1128 CHECK_ERR(bpf_object__create_maps(obj
), err
, out
);
1129 CHECK_ERR(bpf_object__relocate(obj
), err
, out
);
1130 CHECK_ERR(bpf_object__load_progs(obj
), err
, out
);
1134 bpf_object__unload(obj
);
1135 pr_warning("failed to load object '%s'\n", obj
->path
);
1139 void bpf_object__close(struct bpf_object
*obj
)
1146 bpf_object__elf_finish(obj
);
1147 bpf_object__unload(obj
);
1149 for (i
= 0; i
< obj
->nr_maps
; i
++) {
1150 zfree(&obj
->maps
[i
].name
);
1151 if (obj
->maps
[i
].clear_priv
)
1152 obj
->maps
[i
].clear_priv(&obj
->maps
[i
],
1154 obj
->maps
[i
].priv
= NULL
;
1155 obj
->maps
[i
].clear_priv
= NULL
;
1160 if (obj
->programs
&& obj
->nr_programs
) {
1161 for (i
= 0; i
< obj
->nr_programs
; i
++)
1162 bpf_program__exit(&obj
->programs
[i
]);
1164 zfree(&obj
->programs
);
1166 list_del(&obj
->list
);
1171 bpf_object__next(struct bpf_object
*prev
)
1173 struct bpf_object
*next
;
1176 next
= list_first_entry(&bpf_objects_list
,
1180 next
= list_next_entry(prev
, list
);
1182 /* Empty list is noticed here so don't need checking on entry. */
1183 if (&next
->list
== &bpf_objects_list
)
1190 bpf_object__get_name(struct bpf_object
*obj
)
1193 return ERR_PTR(-EINVAL
);
1198 bpf_object__get_kversion(struct bpf_object
*obj
)
1202 return obj
->kern_version
;
1205 struct bpf_program
*
1206 bpf_program__next(struct bpf_program
*prev
, struct bpf_object
*obj
)
1214 return &obj
->programs
[0];
1216 if (prev
->obj
!= obj
) {
1217 pr_warning("error: program handler doesn't match object\n");
1221 idx
= (prev
- obj
->programs
) + 1;
1222 if (idx
>= obj
->nr_programs
)
1224 return &obj
->programs
[idx
];
1227 int bpf_program__set_private(struct bpf_program
*prog
,
1229 bpf_program_clear_priv_t clear_priv
)
1231 if (prog
->priv
&& prog
->clear_priv
)
1232 prog
->clear_priv(prog
, prog
->priv
);
1235 prog
->clear_priv
= clear_priv
;
1239 int bpf_program__get_private(struct bpf_program
*prog
, void **ppriv
)
1241 *ppriv
= prog
->priv
;
1245 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
)
1249 title
= prog
->section_name
;
1251 title
= strdup(title
);
1253 pr_warning("failed to strdup program title\n");
1254 return ERR_PTR(-ENOMEM
);
1261 int bpf_program__fd(struct bpf_program
*prog
)
1263 return bpf_program__nth_fd(prog
, 0);
1266 int bpf_program__set_prep(struct bpf_program
*prog
, int nr_instances
,
1267 bpf_program_prep_t prep
)
1271 if (nr_instances
<= 0 || !prep
)
1274 if (prog
->instances
.nr
> 0 || prog
->instances
.fds
) {
1275 pr_warning("Can't set pre-processor after loading\n");
1279 instances_fds
= malloc(sizeof(int) * nr_instances
);
1280 if (!instances_fds
) {
1281 pr_warning("alloc memory failed for fds\n");
1285 /* fill all fd with -1 */
1286 memset(instances_fds
, -1, sizeof(int) * nr_instances
);
1288 prog
->instances
.nr
= nr_instances
;
1289 prog
->instances
.fds
= instances_fds
;
1290 prog
->preprocessor
= prep
;
1294 int bpf_program__nth_fd(struct bpf_program
*prog
, int n
)
1298 if (n
>= prog
->instances
.nr
|| n
< 0) {
1299 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1300 n
, prog
->section_name
, prog
->instances
.nr
);
1304 fd
= prog
->instances
.fds
[n
];
1306 pr_warning("%dth instance of program '%s' is invalid\n",
1307 n
, prog
->section_name
);
1314 int bpf_map__get_fd(struct bpf_map
*map
)
1322 int bpf_map__get_def(struct bpf_map
*map
, struct bpf_map_def
*pdef
)
1331 const char *bpf_map__get_name(struct bpf_map
*map
)
1338 int bpf_map__set_private(struct bpf_map
*map
, void *priv
,
1339 bpf_map_clear_priv_t clear_priv
)
1345 if (map
->clear_priv
)
1346 map
->clear_priv(map
, map
->priv
);
1350 map
->clear_priv
= clear_priv
;
1354 int bpf_map__get_private(struct bpf_map
*map
, void **ppriv
)
1365 bpf_map__next(struct bpf_map
*prev
, struct bpf_object
*obj
)
1368 struct bpf_map
*s
, *e
;
1370 if (!obj
|| !obj
->maps
)
1374 e
= obj
->maps
+ obj
->nr_maps
;
1379 if ((prev
< s
) || (prev
>= e
)) {
1380 pr_warning("error in %s: map handler doesn't belong to object\n",
1385 idx
= (prev
- obj
->maps
) + 1;
1386 if (idx
>= obj
->nr_maps
)
1388 return &obj
->maps
[idx
];
1392 bpf_object__get_map_by_name(struct bpf_object
*obj
, const char *name
)
1394 struct bpf_map
*pos
;
1396 bpf_map__for_each(pos
, obj
) {
1397 if (pos
->name
&& !strcmp(pos
->name
, name
))