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 int bpf_object__elf_collect(struct bpf_object
*obj
)
492 Elf
*elf
= obj
->efile
.elf
;
493 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
495 int idx
= 0, err
= 0;
497 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
498 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
499 pr_warning("failed to get e_shstrndx from %s\n",
501 return -LIBBPF_ERRNO__FORMAT
;
504 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
510 if (gelf_getshdr(scn
, &sh
) != &sh
) {
511 pr_warning("failed to get section header from %s\n",
513 err
= -LIBBPF_ERRNO__FORMAT
;
517 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
519 pr_warning("failed to get section name from %s\n",
521 err
= -LIBBPF_ERRNO__FORMAT
;
525 data
= elf_getdata(scn
, 0);
527 pr_warning("failed to get section data from %s(%s)\n",
529 err
= -LIBBPF_ERRNO__FORMAT
;
532 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
533 name
, (unsigned long)data
->d_size
,
534 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
537 if (strcmp(name
, "license") == 0)
538 err
= bpf_object__init_license(obj
,
541 else if (strcmp(name
, "version") == 0)
542 err
= bpf_object__init_kversion(obj
,
545 else if (strcmp(name
, "maps") == 0)
546 err
= bpf_object__init_maps(obj
, data
->d_buf
,
548 else if (sh
.sh_type
== SHT_SYMTAB
) {
549 if (obj
->efile
.symbols
) {
550 pr_warning("bpf: multiple SYMTAB in %s\n",
552 err
= -LIBBPF_ERRNO__FORMAT
;
554 obj
->efile
.symbols
= data
;
555 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
556 (sh
.sh_flags
& SHF_EXECINSTR
) &&
557 (data
->d_size
> 0)) {
558 err
= bpf_object__add_program(obj
, data
->d_buf
,
559 data
->d_size
, name
, idx
);
561 char errmsg
[STRERR_BUFSIZE
];
563 strerror_r(-err
, errmsg
, sizeof(errmsg
));
564 pr_warning("failed to alloc program %s (%s): %s",
565 name
, obj
->path
, errmsg
);
567 } else if (sh
.sh_type
== SHT_REL
) {
568 void *reloc
= obj
->efile
.reloc
;
569 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
571 reloc
= realloc(reloc
,
572 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
574 pr_warning("realloc failed\n");
577 int n
= nr_reloc
- 1;
579 obj
->efile
.reloc
= reloc
;
580 obj
->efile
.nr_reloc
= nr_reloc
;
582 obj
->efile
.reloc
[n
].shdr
= sh
;
583 obj
->efile
.reloc
[n
].data
= data
;
593 static struct bpf_program
*
594 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
596 struct bpf_program
*prog
;
599 for (i
= 0; i
< obj
->nr_programs
; i
++) {
600 prog
= &obj
->programs
[i
];
601 if (prog
->idx
== idx
)
608 bpf_program__collect_reloc(struct bpf_program
*prog
,
609 size_t nr_maps
, GElf_Shdr
*shdr
,
610 Elf_Data
*data
, Elf_Data
*symbols
)
614 pr_debug("collecting relocating info for: '%s'\n",
616 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
618 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
619 if (!prog
->reloc_desc
) {
620 pr_warning("failed to alloc memory in relocation\n");
623 prog
->nr_reloc
= nrels
;
625 for (i
= 0; i
< nrels
; i
++) {
628 unsigned int insn_idx
;
629 struct bpf_insn
*insns
= prog
->insns
;
632 if (!gelf_getrel(data
, i
, &rel
)) {
633 pr_warning("relocation: failed to get %d reloc\n", i
);
634 return -LIBBPF_ERRNO__FORMAT
;
637 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
638 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
640 if (!gelf_getsym(symbols
,
641 GELF_R_SYM(rel
.r_info
),
643 pr_warning("relocation: symbol %"PRIx64
" not found\n",
644 GELF_R_SYM(rel
.r_info
));
645 return -LIBBPF_ERRNO__FORMAT
;
648 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
649 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
650 insn_idx
, insns
[insn_idx
].code
);
651 return -LIBBPF_ERRNO__RELOC
;
654 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
655 if (map_idx
>= nr_maps
) {
656 pr_warning("bpf relocation: map_idx %d large than %d\n",
657 (int)map_idx
, (int)nr_maps
- 1);
658 return -LIBBPF_ERRNO__RELOC
;
661 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
662 prog
->reloc_desc
[i
].map_idx
= map_idx
;
668 bpf_object__create_maps(struct bpf_object
*obj
)
674 nr_maps
= obj
->maps_buf_sz
/ sizeof(struct bpf_map_def
);
675 if (!obj
->maps_buf
|| !nr_maps
) {
676 pr_debug("don't need create maps for %s\n",
681 obj
->map_fds
= malloc(sizeof(int) * nr_maps
);
683 pr_warning("realloc perf_bpf_map_fds failed\n");
686 obj
->nr_map_fds
= nr_maps
;
688 /* fill all fd with -1 */
689 memset(obj
->map_fds
, -1, sizeof(int) * nr_maps
);
692 for (i
= 0; i
< nr_maps
; i
++) {
693 struct bpf_map_def def
;
695 def
= *(struct bpf_map_def
*)(obj
->maps_buf
+
696 i
* sizeof(struct bpf_map_def
));
698 *pfd
= bpf_create_map(def
.type
,
706 pr_warning("failed to create map: %s\n",
708 for (j
= 0; j
< i
; j
++)
709 zclose(obj
->map_fds
[j
]);
711 zfree(&obj
->map_fds
);
714 pr_debug("create map: fd=%d\n", *pfd
);
718 zfree(&obj
->maps_buf
);
719 obj
->maps_buf_sz
= 0;
724 bpf_program__relocate(struct bpf_program
*prog
, int *map_fds
)
728 if (!prog
|| !prog
->reloc_desc
)
731 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
732 int insn_idx
, map_idx
;
733 struct bpf_insn
*insns
= prog
->insns
;
735 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
736 map_idx
= prog
->reloc_desc
[i
].map_idx
;
738 if (insn_idx
>= (int)prog
->insns_cnt
) {
739 pr_warning("relocation out of range: '%s'\n",
741 return -LIBBPF_ERRNO__RELOC
;
743 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
744 insns
[insn_idx
].imm
= map_fds
[map_idx
];
747 zfree(&prog
->reloc_desc
);
754 bpf_object__relocate(struct bpf_object
*obj
)
756 struct bpf_program
*prog
;
760 for (i
= 0; i
< obj
->nr_programs
; i
++) {
761 prog
= &obj
->programs
[i
];
763 err
= bpf_program__relocate(prog
, obj
->map_fds
);
765 pr_warning("failed to relocate '%s'\n",
773 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
777 if (!obj_elf_valid(obj
)) {
778 pr_warning("Internal error: elf object is closed\n");
779 return -LIBBPF_ERRNO__INTERNAL
;
782 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
783 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
784 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
785 int idx
= shdr
->sh_info
;
786 struct bpf_program
*prog
;
787 size_t nr_maps
= obj
->maps_buf_sz
/
788 sizeof(struct bpf_map_def
);
790 if (shdr
->sh_type
!= SHT_REL
) {
791 pr_warning("internal error at %d\n", __LINE__
);
792 return -LIBBPF_ERRNO__INTERNAL
;
795 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
797 pr_warning("relocation failed: no %d section\n",
799 return -LIBBPF_ERRNO__RELOC
;
802 err
= bpf_program__collect_reloc(prog
, nr_maps
,
812 load_program(struct bpf_insn
*insns
, int insns_cnt
,
813 char *license
, u32 kern_version
, int *pfd
)
818 if (!insns
|| !insns_cnt
)
821 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
823 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
825 ret
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
826 insns_cnt
, license
, kern_version
,
827 log_buf
, BPF_LOG_BUF_SIZE
);
835 ret
= -LIBBPF_ERRNO__LOAD
;
836 pr_warning("load bpf program failed: %s\n", strerror(errno
));
838 if (log_buf
&& log_buf
[0] != '\0') {
839 ret
= -LIBBPF_ERRNO__VERIFY
;
840 pr_warning("-- BEGIN DUMP LOG ---\n");
841 pr_warning("\n%s\n", log_buf
);
842 pr_warning("-- END LOG --\n");
844 if (insns_cnt
>= BPF_MAXINSNS
) {
845 pr_warning("Program too large (%d insns), at most %d insns\n",
846 insns_cnt
, BPF_MAXINSNS
);
847 ret
= -LIBBPF_ERRNO__PROG2BIG
;
848 } else if (log_buf
) {
849 pr_warning("log buffer is empty\n");
850 ret
= -LIBBPF_ERRNO__KVER
;
860 bpf_program__load(struct bpf_program
*prog
,
861 char *license
, u32 kern_version
)
865 err
= load_program(prog
->insns
, prog
->insns_cnt
,
866 license
, kern_version
, &fd
);
871 pr_warning("failed to load program '%s'\n",
879 bpf_object__load_progs(struct bpf_object
*obj
)
884 for (i
= 0; i
< obj
->nr_programs
; i
++) {
885 err
= bpf_program__load(&obj
->programs
[i
],
894 static int bpf_object__validate(struct bpf_object
*obj
)
896 if (obj
->kern_version
== 0) {
897 pr_warning("%s doesn't provide kernel version\n",
899 return -LIBBPF_ERRNO__KVERSION
;
904 static struct bpf_object
*
905 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
907 struct bpf_object
*obj
;
910 if (elf_version(EV_CURRENT
) == EV_NONE
) {
911 pr_warning("failed to init libelf for %s\n", path
);
912 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
915 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
919 CHECK_ERR(bpf_object__elf_init(obj
), err
, out
);
920 CHECK_ERR(bpf_object__check_endianness(obj
), err
, out
);
921 CHECK_ERR(bpf_object__elf_collect(obj
), err
, out
);
922 CHECK_ERR(bpf_object__collect_reloc(obj
), err
, out
);
923 CHECK_ERR(bpf_object__validate(obj
), err
, out
);
925 bpf_object__elf_finish(obj
);
928 bpf_object__close(obj
);
932 struct bpf_object
*bpf_object__open(const char *path
)
934 /* param validation */
938 pr_debug("loading %s\n", path
);
940 return __bpf_object__open(path
, NULL
, 0);
943 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
949 /* param validation */
950 if (!obj_buf
|| obj_buf_sz
<= 0)
954 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
955 (unsigned long)obj_buf
,
956 (unsigned long)obj_buf_sz
);
957 tmp_name
[sizeof(tmp_name
) - 1] = '\0';
960 pr_debug("loading object '%s' from buffer\n",
963 return __bpf_object__open(name
, obj_buf
, obj_buf_sz
);
966 int bpf_object__unload(struct bpf_object
*obj
)
973 for (i
= 0; i
< obj
->nr_map_fds
; i
++)
974 zclose(obj
->map_fds
[i
]);
975 zfree(&obj
->map_fds
);
978 for (i
= 0; i
< obj
->nr_programs
; i
++)
979 bpf_program__unload(&obj
->programs
[i
]);
984 int bpf_object__load(struct bpf_object
*obj
)
992 pr_warning("object should not be loaded twice\n");
998 CHECK_ERR(bpf_object__create_maps(obj
), err
, out
);
999 CHECK_ERR(bpf_object__relocate(obj
), err
, out
);
1000 CHECK_ERR(bpf_object__load_progs(obj
), err
, out
);
1004 bpf_object__unload(obj
);
1005 pr_warning("failed to load object '%s'\n", obj
->path
);
1009 void bpf_object__close(struct bpf_object
*obj
)
1016 bpf_object__elf_finish(obj
);
1017 bpf_object__unload(obj
);
1019 zfree(&obj
->maps_buf
);
1021 if (obj
->programs
&& obj
->nr_programs
) {
1022 for (i
= 0; i
< obj
->nr_programs
; i
++)
1023 bpf_program__exit(&obj
->programs
[i
]);
1025 zfree(&obj
->programs
);
1027 list_del(&obj
->list
);
1032 bpf_object__next(struct bpf_object
*prev
)
1034 struct bpf_object
*next
;
1037 next
= list_first_entry(&bpf_objects_list
,
1041 next
= list_next_entry(prev
, list
);
1043 /* Empty list is noticed here so don't need checking on entry. */
1044 if (&next
->list
== &bpf_objects_list
)
1051 bpf_object__get_name(struct bpf_object
*obj
)
1054 return ERR_PTR(-EINVAL
);
1059 bpf_object__get_kversion(struct bpf_object
*obj
)
1063 return obj
->kern_version
;
1066 struct bpf_program
*
1067 bpf_program__next(struct bpf_program
*prev
, struct bpf_object
*obj
)
1075 return &obj
->programs
[0];
1077 if (prev
->obj
!= obj
) {
1078 pr_warning("error: program handler doesn't match object\n");
1082 idx
= (prev
- obj
->programs
) + 1;
1083 if (idx
>= obj
->nr_programs
)
1085 return &obj
->programs
[idx
];
1088 int bpf_program__set_private(struct bpf_program
*prog
,
1090 bpf_program_clear_priv_t clear_priv
)
1092 if (prog
->priv
&& prog
->clear_priv
)
1093 prog
->clear_priv(prog
, prog
->priv
);
1096 prog
->clear_priv
= clear_priv
;
1100 int bpf_program__get_private(struct bpf_program
*prog
, void **ppriv
)
1102 *ppriv
= prog
->priv
;
1106 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
)
1110 title
= prog
->section_name
;
1112 title
= strdup(title
);
1114 pr_warning("failed to strdup program title\n");
1115 return ERR_PTR(-ENOMEM
);
1122 int bpf_program__fd(struct bpf_program
*prog
)