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
;
157 struct bpf_object
*obj
;
159 bpf_program_clear_priv_t clear_priv
;
162 static LIST_HEAD(bpf_objects_list
);
170 struct bpf_program
*programs
;
174 * This field is required because maps_buf will be freed and
175 * maps_buf_sz will be set to 0 after loaded.
181 * Information when doing elf related work. Only valid if fd
198 * All loaded bpf_object is linked in a list, which is
199 * hidden to caller. bpf_objects__<func> handlers deal with
202 struct list_head list
;
205 #define obj_elf_valid(o) ((o)->efile.elf)
207 static void bpf_program__unload(struct bpf_program
*prog
)
215 static void bpf_program__exit(struct bpf_program
*prog
)
220 if (prog
->clear_priv
)
221 prog
->clear_priv(prog
, prog
->priv
);
224 prog
->clear_priv
= NULL
;
226 bpf_program__unload(prog
);
227 zfree(&prog
->section_name
);
229 zfree(&prog
->reloc_desc
);
237 bpf_program__init(void *data
, size_t size
, char *name
, int idx
,
238 struct bpf_program
*prog
)
240 if (size
< sizeof(struct bpf_insn
)) {
241 pr_warning("corrupted section '%s'\n", name
);
245 bzero(prog
, sizeof(*prog
));
247 prog
->section_name
= strdup(name
);
248 if (!prog
->section_name
) {
249 pr_warning("failed to alloc name for prog %s\n",
254 prog
->insns
= malloc(size
);
256 pr_warning("failed to alloc insns for %s\n", name
);
259 prog
->insns_cnt
= size
/ sizeof(struct bpf_insn
);
260 memcpy(prog
->insns
, data
,
261 prog
->insns_cnt
* sizeof(struct bpf_insn
));
267 bpf_program__exit(prog
);
272 bpf_object__add_program(struct bpf_object
*obj
, void *data
, size_t size
,
275 struct bpf_program prog
, *progs
;
278 err
= bpf_program__init(data
, size
, name
, idx
, &prog
);
282 progs
= obj
->programs
;
283 nr_progs
= obj
->nr_programs
;
285 progs
= realloc(progs
, sizeof(progs
[0]) * (nr_progs
+ 1));
288 * In this case the original obj->programs
289 * is still valid, so don't need special treat for
290 * bpf_close_object().
292 pr_warning("failed to alloc a new program '%s'\n",
294 bpf_program__exit(&prog
);
298 pr_debug("found program %s\n", prog
.section_name
);
299 obj
->programs
= progs
;
300 obj
->nr_programs
= nr_progs
+ 1;
302 progs
[nr_progs
] = prog
;
306 static struct bpf_object
*bpf_object__new(const char *path
,
310 struct bpf_object
*obj
;
312 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
314 pr_warning("alloc memory failed for %s\n", path
);
315 return ERR_PTR(-ENOMEM
);
318 strcpy(obj
->path
, path
);
322 * Caller of this function should also calls
323 * bpf_object__elf_finish() after data collection to return
324 * obj_buf to user. If not, we should duplicate the buffer to
325 * avoid user freeing them before elf finish.
327 obj
->efile
.obj_buf
= obj_buf
;
328 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
332 INIT_LIST_HEAD(&obj
->list
);
333 list_add(&obj
->list
, &bpf_objects_list
);
337 static void bpf_object__elf_finish(struct bpf_object
*obj
)
339 if (!obj_elf_valid(obj
))
342 if (obj
->efile
.elf
) {
343 elf_end(obj
->efile
.elf
);
344 obj
->efile
.elf
= NULL
;
346 obj
->efile
.symbols
= NULL
;
348 zfree(&obj
->efile
.reloc
);
349 obj
->efile
.nr_reloc
= 0;
350 zclose(obj
->efile
.fd
);
351 obj
->efile
.obj_buf
= NULL
;
352 obj
->efile
.obj_buf_sz
= 0;
355 static int bpf_object__elf_init(struct bpf_object
*obj
)
360 if (obj_elf_valid(obj
)) {
361 pr_warning("elf init: internal error\n");
362 return -LIBBPF_ERRNO__LIBELF
;
365 if (obj
->efile
.obj_buf_sz
> 0) {
367 * obj_buf should have been validated by
368 * bpf_object__open_buffer().
370 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
371 obj
->efile
.obj_buf_sz
);
373 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
374 if (obj
->efile
.fd
< 0) {
375 pr_warning("failed to open %s: %s\n", obj
->path
,
380 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
381 LIBBPF_ELF_C_READ_MMAP
,
385 if (!obj
->efile
.elf
) {
386 pr_warning("failed to open %s as ELF file\n",
388 err
= -LIBBPF_ERRNO__LIBELF
;
392 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
393 pr_warning("failed to get EHDR from %s\n",
395 err
= -LIBBPF_ERRNO__FORMAT
;
398 ep
= &obj
->efile
.ehdr
;
400 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
!= 0)) {
401 pr_warning("%s is not an eBPF object file\n",
403 err
= -LIBBPF_ERRNO__FORMAT
;
409 bpf_object__elf_finish(obj
);
414 bpf_object__check_endianness(struct bpf_object
*obj
)
416 static unsigned int const endian
= 1;
418 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
420 /* We are big endian, BPF obj is little endian. */
421 if (*(unsigned char const *)&endian
!= 1)
426 /* We are little endian, BPF obj is big endian. */
427 if (*(unsigned char const *)&endian
!= 0)
431 return -LIBBPF_ERRNO__ENDIAN
;
437 pr_warning("Error: endianness mismatch.\n");
438 return -LIBBPF_ERRNO__ENDIAN
;
442 bpf_object__init_license(struct bpf_object
*obj
,
443 void *data
, size_t size
)
445 memcpy(obj
->license
, data
,
446 min(size
, sizeof(obj
->license
) - 1));
447 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
452 bpf_object__init_kversion(struct bpf_object
*obj
,
453 void *data
, size_t size
)
457 if (size
!= sizeof(kver
)) {
458 pr_warning("invalid kver section in %s\n", obj
->path
);
459 return -LIBBPF_ERRNO__FORMAT
;
461 memcpy(&kver
, data
, sizeof(kver
));
462 obj
->kern_version
= kver
;
463 pr_debug("kernel version of %s is %x\n", obj
->path
,
469 bpf_object__init_maps(struct bpf_object
*obj
, void *data
,
473 pr_debug("%s doesn't need map definition\n",
478 obj
->maps_buf
= malloc(size
);
479 if (!obj
->maps_buf
) {
480 pr_warning("malloc maps failed: %s\n", obj
->path
);
484 obj
->maps_buf_sz
= size
;
485 memcpy(obj
->maps_buf
, data
, size
);
486 pr_debug("maps in %s: %ld bytes\n", obj
->path
, (long)size
);
490 static bool section_have_execinstr(struct bpf_object
*obj
, int idx
)
495 scn
= elf_getscn(obj
->efile
.elf
, idx
);
499 if (gelf_getshdr(scn
, &sh
) != &sh
)
502 if (sh
.sh_flags
& SHF_EXECINSTR
)
508 static int bpf_object__elf_collect(struct bpf_object
*obj
)
510 Elf
*elf
= obj
->efile
.elf
;
511 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
513 int idx
= 0, err
= 0;
515 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
516 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
517 pr_warning("failed to get e_shstrndx from %s\n",
519 return -LIBBPF_ERRNO__FORMAT
;
522 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
528 if (gelf_getshdr(scn
, &sh
) != &sh
) {
529 pr_warning("failed to get section header from %s\n",
531 err
= -LIBBPF_ERRNO__FORMAT
;
535 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
537 pr_warning("failed to get section name from %s\n",
539 err
= -LIBBPF_ERRNO__FORMAT
;
543 data
= elf_getdata(scn
, 0);
545 pr_warning("failed to get section data from %s(%s)\n",
547 err
= -LIBBPF_ERRNO__FORMAT
;
550 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
551 name
, (unsigned long)data
->d_size
,
552 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
555 if (strcmp(name
, "license") == 0)
556 err
= bpf_object__init_license(obj
,
559 else if (strcmp(name
, "version") == 0)
560 err
= bpf_object__init_kversion(obj
,
563 else if (strcmp(name
, "maps") == 0)
564 err
= bpf_object__init_maps(obj
, data
->d_buf
,
566 else if (sh
.sh_type
== SHT_SYMTAB
) {
567 if (obj
->efile
.symbols
) {
568 pr_warning("bpf: multiple SYMTAB in %s\n",
570 err
= -LIBBPF_ERRNO__FORMAT
;
572 obj
->efile
.symbols
= data
;
573 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
574 (sh
.sh_flags
& SHF_EXECINSTR
) &&
575 (data
->d_size
> 0)) {
576 err
= bpf_object__add_program(obj
, data
->d_buf
,
577 data
->d_size
, name
, idx
);
579 char errmsg
[STRERR_BUFSIZE
];
581 strerror_r(-err
, errmsg
, sizeof(errmsg
));
582 pr_warning("failed to alloc program %s (%s): %s",
583 name
, obj
->path
, errmsg
);
585 } else if (sh
.sh_type
== SHT_REL
) {
586 void *reloc
= obj
->efile
.reloc
;
587 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
588 int sec
= sh
.sh_info
; /* points to other section */
590 /* Only do relo for section with exec instructions */
591 if (!section_have_execinstr(obj
, sec
)) {
592 pr_debug("skip relo %s(%d) for section(%d)\n",
597 reloc
= realloc(reloc
,
598 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
600 pr_warning("realloc failed\n");
603 int n
= nr_reloc
- 1;
605 obj
->efile
.reloc
= reloc
;
606 obj
->efile
.nr_reloc
= nr_reloc
;
608 obj
->efile
.reloc
[n
].shdr
= sh
;
609 obj
->efile
.reloc
[n
].data
= data
;
619 static struct bpf_program
*
620 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
622 struct bpf_program
*prog
;
625 for (i
= 0; i
< obj
->nr_programs
; i
++) {
626 prog
= &obj
->programs
[i
];
627 if (prog
->idx
== idx
)
634 bpf_program__collect_reloc(struct bpf_program
*prog
,
635 size_t nr_maps
, GElf_Shdr
*shdr
,
636 Elf_Data
*data
, Elf_Data
*symbols
)
640 pr_debug("collecting relocating info for: '%s'\n",
642 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
644 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
645 if (!prog
->reloc_desc
) {
646 pr_warning("failed to alloc memory in relocation\n");
649 prog
->nr_reloc
= nrels
;
651 for (i
= 0; i
< nrels
; i
++) {
654 unsigned int insn_idx
;
655 struct bpf_insn
*insns
= prog
->insns
;
658 if (!gelf_getrel(data
, i
, &rel
)) {
659 pr_warning("relocation: failed to get %d reloc\n", i
);
660 return -LIBBPF_ERRNO__FORMAT
;
663 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
664 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
666 if (!gelf_getsym(symbols
,
667 GELF_R_SYM(rel
.r_info
),
669 pr_warning("relocation: symbol %"PRIx64
" not found\n",
670 GELF_R_SYM(rel
.r_info
));
671 return -LIBBPF_ERRNO__FORMAT
;
674 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
675 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
676 insn_idx
, insns
[insn_idx
].code
);
677 return -LIBBPF_ERRNO__RELOC
;
680 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
681 if (map_idx
>= nr_maps
) {
682 pr_warning("bpf relocation: map_idx %d large than %d\n",
683 (int)map_idx
, (int)nr_maps
- 1);
684 return -LIBBPF_ERRNO__RELOC
;
687 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
688 prog
->reloc_desc
[i
].map_idx
= map_idx
;
694 bpf_object__create_maps(struct bpf_object
*obj
)
700 nr_maps
= obj
->maps_buf_sz
/ sizeof(struct bpf_map_def
);
701 if (!obj
->maps_buf
|| !nr_maps
) {
702 pr_debug("don't need create maps for %s\n",
707 obj
->map_fds
= malloc(sizeof(int) * nr_maps
);
709 pr_warning("realloc perf_bpf_map_fds failed\n");
712 obj
->nr_map_fds
= nr_maps
;
714 /* fill all fd with -1 */
715 memset(obj
->map_fds
, -1, sizeof(int) * nr_maps
);
718 for (i
= 0; i
< nr_maps
; i
++) {
719 struct bpf_map_def def
;
721 def
= *(struct bpf_map_def
*)(obj
->maps_buf
+
722 i
* sizeof(struct bpf_map_def
));
724 *pfd
= bpf_create_map(def
.type
,
732 pr_warning("failed to create map: %s\n",
734 for (j
= 0; j
< i
; j
++)
735 zclose(obj
->map_fds
[j
]);
737 zfree(&obj
->map_fds
);
740 pr_debug("create map: fd=%d\n", *pfd
);
744 zfree(&obj
->maps_buf
);
745 obj
->maps_buf_sz
= 0;
750 bpf_program__relocate(struct bpf_program
*prog
, int *map_fds
)
754 if (!prog
|| !prog
->reloc_desc
)
757 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
758 int insn_idx
, map_idx
;
759 struct bpf_insn
*insns
= prog
->insns
;
761 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
762 map_idx
= prog
->reloc_desc
[i
].map_idx
;
764 if (insn_idx
>= (int)prog
->insns_cnt
) {
765 pr_warning("relocation out of range: '%s'\n",
767 return -LIBBPF_ERRNO__RELOC
;
769 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
770 insns
[insn_idx
].imm
= map_fds
[map_idx
];
773 zfree(&prog
->reloc_desc
);
780 bpf_object__relocate(struct bpf_object
*obj
)
782 struct bpf_program
*prog
;
786 for (i
= 0; i
< obj
->nr_programs
; i
++) {
787 prog
= &obj
->programs
[i
];
789 err
= bpf_program__relocate(prog
, obj
->map_fds
);
791 pr_warning("failed to relocate '%s'\n",
799 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
803 if (!obj_elf_valid(obj
)) {
804 pr_warning("Internal error: elf object is closed\n");
805 return -LIBBPF_ERRNO__INTERNAL
;
808 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
809 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
810 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
811 int idx
= shdr
->sh_info
;
812 struct bpf_program
*prog
;
813 size_t nr_maps
= obj
->maps_buf_sz
/
814 sizeof(struct bpf_map_def
);
816 if (shdr
->sh_type
!= SHT_REL
) {
817 pr_warning("internal error at %d\n", __LINE__
);
818 return -LIBBPF_ERRNO__INTERNAL
;
821 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
823 pr_warning("relocation failed: no %d section\n",
825 return -LIBBPF_ERRNO__RELOC
;
828 err
= bpf_program__collect_reloc(prog
, nr_maps
,
838 load_program(struct bpf_insn
*insns
, int insns_cnt
,
839 char *license
, u32 kern_version
, int *pfd
)
844 if (!insns
|| !insns_cnt
)
847 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
849 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
851 ret
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
852 insns_cnt
, license
, kern_version
,
853 log_buf
, BPF_LOG_BUF_SIZE
);
861 ret
= -LIBBPF_ERRNO__LOAD
;
862 pr_warning("load bpf program failed: %s\n", strerror(errno
));
864 if (log_buf
&& log_buf
[0] != '\0') {
865 ret
= -LIBBPF_ERRNO__VERIFY
;
866 pr_warning("-- BEGIN DUMP LOG ---\n");
867 pr_warning("\n%s\n", log_buf
);
868 pr_warning("-- END LOG --\n");
870 if (insns_cnt
>= BPF_MAXINSNS
) {
871 pr_warning("Program too large (%d insns), at most %d insns\n",
872 insns_cnt
, BPF_MAXINSNS
);
873 ret
= -LIBBPF_ERRNO__PROG2BIG
;
874 } else if (log_buf
) {
875 pr_warning("log buffer is empty\n");
876 ret
= -LIBBPF_ERRNO__KVER
;
886 bpf_program__load(struct bpf_program
*prog
,
887 char *license
, u32 kern_version
)
891 err
= load_program(prog
->insns
, prog
->insns_cnt
,
892 license
, kern_version
, &fd
);
897 pr_warning("failed to load program '%s'\n",
905 bpf_object__load_progs(struct bpf_object
*obj
)
910 for (i
= 0; i
< obj
->nr_programs
; i
++) {
911 err
= bpf_program__load(&obj
->programs
[i
],
920 static int bpf_object__validate(struct bpf_object
*obj
)
922 if (obj
->kern_version
== 0) {
923 pr_warning("%s doesn't provide kernel version\n",
925 return -LIBBPF_ERRNO__KVERSION
;
930 static struct bpf_object
*
931 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
933 struct bpf_object
*obj
;
936 if (elf_version(EV_CURRENT
) == EV_NONE
) {
937 pr_warning("failed to init libelf for %s\n", path
);
938 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
941 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
945 CHECK_ERR(bpf_object__elf_init(obj
), err
, out
);
946 CHECK_ERR(bpf_object__check_endianness(obj
), err
, out
);
947 CHECK_ERR(bpf_object__elf_collect(obj
), err
, out
);
948 CHECK_ERR(bpf_object__collect_reloc(obj
), err
, out
);
949 CHECK_ERR(bpf_object__validate(obj
), err
, out
);
951 bpf_object__elf_finish(obj
);
954 bpf_object__close(obj
);
958 struct bpf_object
*bpf_object__open(const char *path
)
960 /* param validation */
964 pr_debug("loading %s\n", path
);
966 return __bpf_object__open(path
, NULL
, 0);
969 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
975 /* param validation */
976 if (!obj_buf
|| obj_buf_sz
<= 0)
980 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
981 (unsigned long)obj_buf
,
982 (unsigned long)obj_buf_sz
);
983 tmp_name
[sizeof(tmp_name
) - 1] = '\0';
986 pr_debug("loading object '%s' from buffer\n",
989 return __bpf_object__open(name
, obj_buf
, obj_buf_sz
);
992 int bpf_object__unload(struct bpf_object
*obj
)
999 for (i
= 0; i
< obj
->nr_map_fds
; i
++)
1000 zclose(obj
->map_fds
[i
]);
1001 zfree(&obj
->map_fds
);
1002 obj
->nr_map_fds
= 0;
1004 for (i
= 0; i
< obj
->nr_programs
; i
++)
1005 bpf_program__unload(&obj
->programs
[i
]);
1010 int bpf_object__load(struct bpf_object
*obj
)
1018 pr_warning("object should not be loaded twice\n");
1024 CHECK_ERR(bpf_object__create_maps(obj
), err
, out
);
1025 CHECK_ERR(bpf_object__relocate(obj
), err
, out
);
1026 CHECK_ERR(bpf_object__load_progs(obj
), err
, out
);
1030 bpf_object__unload(obj
);
1031 pr_warning("failed to load object '%s'\n", obj
->path
);
1035 void bpf_object__close(struct bpf_object
*obj
)
1042 bpf_object__elf_finish(obj
);
1043 bpf_object__unload(obj
);
1045 zfree(&obj
->maps_buf
);
1047 if (obj
->programs
&& obj
->nr_programs
) {
1048 for (i
= 0; i
< obj
->nr_programs
; i
++)
1049 bpf_program__exit(&obj
->programs
[i
]);
1051 zfree(&obj
->programs
);
1053 list_del(&obj
->list
);
1058 bpf_object__next(struct bpf_object
*prev
)
1060 struct bpf_object
*next
;
1063 next
= list_first_entry(&bpf_objects_list
,
1067 next
= list_next_entry(prev
, list
);
1069 /* Empty list is noticed here so don't need checking on entry. */
1070 if (&next
->list
== &bpf_objects_list
)
1077 bpf_object__get_name(struct bpf_object
*obj
)
1080 return ERR_PTR(-EINVAL
);
1085 bpf_object__get_kversion(struct bpf_object
*obj
)
1089 return obj
->kern_version
;
1092 struct bpf_program
*
1093 bpf_program__next(struct bpf_program
*prev
, struct bpf_object
*obj
)
1101 return &obj
->programs
[0];
1103 if (prev
->obj
!= obj
) {
1104 pr_warning("error: program handler doesn't match object\n");
1108 idx
= (prev
- obj
->programs
) + 1;
1109 if (idx
>= obj
->nr_programs
)
1111 return &obj
->programs
[idx
];
1114 int bpf_program__set_private(struct bpf_program
*prog
,
1116 bpf_program_clear_priv_t clear_priv
)
1118 if (prog
->priv
&& prog
->clear_priv
)
1119 prog
->clear_priv(prog
, prog
->priv
);
1122 prog
->clear_priv
= clear_priv
;
1126 int bpf_program__get_private(struct bpf_program
*prog
, void **ppriv
)
1128 *ppriv
= prog
->priv
;
1132 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
)
1136 title
= prog
->section_name
;
1138 title
= strdup(title
);
1140 pr_warning("failed to strdup program title\n");
1141 return ERR_PTR(-ENOMEM
);
1148 int bpf_program__fd(struct bpf_program
*prog
)