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.
7 * Copyright (C) 2017 Nicira, Inc.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License (not later!)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, see <http://www.gnu.org/licenses>
32 #include <asm/unistd.h>
33 #include <linux/err.h>
34 #include <linux/kernel.h>
35 #include <linux/bpf.h>
36 #include <linux/list.h>
37 #include <linux/limits.h>
39 #include <sys/types.h>
52 #define BPF_FS_MAGIC 0xcafe4a11
55 #define __printf(a, b) __attribute__((format(printf, a, b)))
58 static int __base_pr(const char *format
, ...)
63 va_start(args
, format
);
64 err
= vfprintf(stderr
, format
, args
);
69 static __printf(1, 2) libbpf_print_fn_t __pr_warning
= __base_pr
;
70 static __printf(1, 2) libbpf_print_fn_t __pr_info
= __base_pr
;
71 static __printf(1, 2) libbpf_print_fn_t __pr_debug
;
73 #define __pr(func, fmt, ...) \
76 (func)("libbpf: " fmt, ##__VA_ARGS__); \
79 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
80 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
81 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
83 void libbpf_set_print(libbpf_print_fn_t warn
,
84 libbpf_print_fn_t info
,
85 libbpf_print_fn_t debug
)
92 #define STRERR_BUFSIZE 128
94 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
95 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
96 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
98 static const char *libbpf_strerror_table
[NR_ERRNO
] = {
99 [ERRCODE_OFFSET(LIBELF
)] = "Something wrong in libelf",
100 [ERRCODE_OFFSET(FORMAT
)] = "BPF object format invalid",
101 [ERRCODE_OFFSET(KVERSION
)] = "'version' section incorrect or lost",
102 [ERRCODE_OFFSET(ENDIAN
)] = "Endian mismatch",
103 [ERRCODE_OFFSET(INTERNAL
)] = "Internal error in libbpf",
104 [ERRCODE_OFFSET(RELOC
)] = "Relocation failed",
105 [ERRCODE_OFFSET(VERIFY
)] = "Kernel verifier blocks program loading",
106 [ERRCODE_OFFSET(PROG2BIG
)] = "Program too big",
107 [ERRCODE_OFFSET(KVER
)] = "Incorrect kernel version",
108 [ERRCODE_OFFSET(PROGTYPE
)] = "Kernel doesn't support this program type",
111 int libbpf_strerror(int err
, char *buf
, size_t size
)
116 err
= err
> 0 ? err
: -err
;
118 if (err
< __LIBBPF_ERRNO__START
) {
121 ret
= strerror_r(err
, buf
, size
);
122 buf
[size
- 1] = '\0';
126 if (err
< __LIBBPF_ERRNO__END
) {
129 msg
= libbpf_strerror_table
[ERRNO_OFFSET(err
)];
130 snprintf(buf
, size
, "%s", msg
);
131 buf
[size
- 1] = '\0';
135 snprintf(buf
, size
, "Unknown libbpf error %d", err
);
136 buf
[size
- 1] = '\0';
140 #define CHECK_ERR(action, err, out) do { \
147 /* Copied from tools/perf/util/util.h */
149 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
153 # define zclose(fd) ({ \
156 ___err = close((fd)); \
161 #ifdef HAVE_LIBELF_MMAP_SUPPORT
162 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
164 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
168 * bpf_prog should be a better name but it has been used in
172 /* Index in elf obj file, for relocation use. */
176 struct bpf_insn
*insns
;
178 enum bpf_prog_type type
;
190 bpf_program_prep_t preprocessor
;
192 struct bpf_object
*obj
;
194 bpf_program_clear_priv_t clear_priv
;
201 struct bpf_map_def def
;
203 bpf_map_clear_priv_t clear_priv
;
206 static LIST_HEAD(bpf_objects_list
);
212 struct bpf_program
*programs
;
214 struct bpf_map
*maps
;
220 * Information when doing elf related work. Only valid if fd
239 * All loaded bpf_object is linked in a list, which is
240 * hidden to caller. bpf_objects__<func> handlers deal with
243 struct list_head list
;
246 bpf_object_clear_priv_t clear_priv
;
250 #define obj_elf_valid(o) ((o)->efile.elf)
252 static void bpf_program__unload(struct bpf_program
*prog
)
260 * If the object is opened but the program was never loaded,
261 * it is possible that prog->instances.nr == -1.
263 if (prog
->instances
.nr
> 0) {
264 for (i
= 0; i
< prog
->instances
.nr
; i
++)
265 zclose(prog
->instances
.fds
[i
]);
266 } else if (prog
->instances
.nr
!= -1) {
267 pr_warning("Internal error: instances.nr is %d\n",
271 prog
->instances
.nr
= -1;
272 zfree(&prog
->instances
.fds
);
275 static void bpf_program__exit(struct bpf_program
*prog
)
280 if (prog
->clear_priv
)
281 prog
->clear_priv(prog
, prog
->priv
);
284 prog
->clear_priv
= NULL
;
286 bpf_program__unload(prog
);
288 zfree(&prog
->section_name
);
290 zfree(&prog
->reloc_desc
);
298 bpf_program__init(void *data
, size_t size
, char *section_name
, int idx
,
299 struct bpf_program
*prog
)
301 if (size
< sizeof(struct bpf_insn
)) {
302 pr_warning("corrupted section '%s'\n", section_name
);
306 bzero(prog
, sizeof(*prog
));
308 prog
->section_name
= strdup(section_name
);
309 if (!prog
->section_name
) {
310 pr_warning("failed to alloc name for prog under section %s\n",
315 prog
->insns
= malloc(size
);
317 pr_warning("failed to alloc insns for prog under section %s\n",
321 prog
->insns_cnt
= size
/ sizeof(struct bpf_insn
);
322 memcpy(prog
->insns
, data
,
323 prog
->insns_cnt
* sizeof(struct bpf_insn
));
325 prog
->instances
.fds
= NULL
;
326 prog
->instances
.nr
= -1;
327 prog
->type
= BPF_PROG_TYPE_KPROBE
;
331 bpf_program__exit(prog
);
336 bpf_object__add_program(struct bpf_object
*obj
, void *data
, size_t size
,
337 char *section_name
, int idx
)
339 struct bpf_program prog
, *progs
;
342 err
= bpf_program__init(data
, size
, section_name
, idx
, &prog
);
346 progs
= obj
->programs
;
347 nr_progs
= obj
->nr_programs
;
349 progs
= realloc(progs
, sizeof(progs
[0]) * (nr_progs
+ 1));
352 * In this case the original obj->programs
353 * is still valid, so don't need special treat for
354 * bpf_close_object().
356 pr_warning("failed to alloc a new program under section '%s'\n",
358 bpf_program__exit(&prog
);
362 pr_debug("found program %s\n", prog
.section_name
);
363 obj
->programs
= progs
;
364 obj
->nr_programs
= nr_progs
+ 1;
366 progs
[nr_progs
] = prog
;
371 bpf_object__init_prog_names(struct bpf_object
*obj
)
373 Elf_Data
*symbols
= obj
->efile
.symbols
;
374 struct bpf_program
*prog
;
377 for (pi
= 0; pi
< obj
->nr_programs
; pi
++) {
380 prog
= &obj
->programs
[pi
];
382 for (si
= 0; si
< symbols
->d_size
/ sizeof(GElf_Sym
) && !name
;
386 if (!gelf_getsym(symbols
, si
, &sym
))
388 if (sym
.st_shndx
!= prog
->idx
)
390 if (GELF_ST_BIND(sym
.st_info
) != STB_GLOBAL
)
393 name
= elf_strptr(obj
->efile
.elf
,
394 obj
->efile
.strtabidx
,
397 pr_warning("failed to get sym name string for prog %s\n",
399 return -LIBBPF_ERRNO__LIBELF
;
404 pr_warning("failed to find sym for prog %s\n",
409 prog
->name
= strdup(name
);
411 pr_warning("failed to allocate memory for prog sym %s\n",
420 static struct bpf_object
*bpf_object__new(const char *path
,
424 struct bpf_object
*obj
;
426 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
428 pr_warning("alloc memory failed for %s\n", path
);
429 return ERR_PTR(-ENOMEM
);
432 strcpy(obj
->path
, path
);
436 * Caller of this function should also calls
437 * bpf_object__elf_finish() after data collection to return
438 * obj_buf to user. If not, we should duplicate the buffer to
439 * avoid user freeing them before elf finish.
441 obj
->efile
.obj_buf
= obj_buf
;
442 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
443 obj
->efile
.maps_shndx
= -1;
447 INIT_LIST_HEAD(&obj
->list
);
448 list_add(&obj
->list
, &bpf_objects_list
);
452 static void bpf_object__elf_finish(struct bpf_object
*obj
)
454 if (!obj_elf_valid(obj
))
457 if (obj
->efile
.elf
) {
458 elf_end(obj
->efile
.elf
);
459 obj
->efile
.elf
= NULL
;
461 obj
->efile
.symbols
= NULL
;
463 zfree(&obj
->efile
.reloc
);
464 obj
->efile
.nr_reloc
= 0;
465 zclose(obj
->efile
.fd
);
466 obj
->efile
.obj_buf
= NULL
;
467 obj
->efile
.obj_buf_sz
= 0;
470 static int bpf_object__elf_init(struct bpf_object
*obj
)
475 if (obj_elf_valid(obj
)) {
476 pr_warning("elf init: internal error\n");
477 return -LIBBPF_ERRNO__LIBELF
;
480 if (obj
->efile
.obj_buf_sz
> 0) {
482 * obj_buf should have been validated by
483 * bpf_object__open_buffer().
485 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
486 obj
->efile
.obj_buf_sz
);
488 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
489 if (obj
->efile
.fd
< 0) {
490 pr_warning("failed to open %s: %s\n", obj
->path
,
495 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
496 LIBBPF_ELF_C_READ_MMAP
,
500 if (!obj
->efile
.elf
) {
501 pr_warning("failed to open %s as ELF file\n",
503 err
= -LIBBPF_ERRNO__LIBELF
;
507 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
508 pr_warning("failed to get EHDR from %s\n",
510 err
= -LIBBPF_ERRNO__FORMAT
;
513 ep
= &obj
->efile
.ehdr
;
515 /* Old LLVM set e_machine to EM_NONE */
516 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
&& (ep
->e_machine
!= EM_BPF
))) {
517 pr_warning("%s is not an eBPF object file\n",
519 err
= -LIBBPF_ERRNO__FORMAT
;
525 bpf_object__elf_finish(obj
);
530 bpf_object__check_endianness(struct bpf_object
*obj
)
532 static unsigned int const endian
= 1;
534 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
536 /* We are big endian, BPF obj is little endian. */
537 if (*(unsigned char const *)&endian
!= 1)
542 /* We are little endian, BPF obj is big endian. */
543 if (*(unsigned char const *)&endian
!= 0)
547 return -LIBBPF_ERRNO__ENDIAN
;
553 pr_warning("Error: endianness mismatch.\n");
554 return -LIBBPF_ERRNO__ENDIAN
;
558 bpf_object__init_license(struct bpf_object
*obj
,
559 void *data
, size_t size
)
561 memcpy(obj
->license
, data
,
562 min(size
, sizeof(obj
->license
) - 1));
563 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
568 bpf_object__init_kversion(struct bpf_object
*obj
,
569 void *data
, size_t size
)
573 if (size
!= sizeof(kver
)) {
574 pr_warning("invalid kver section in %s\n", obj
->path
);
575 return -LIBBPF_ERRNO__FORMAT
;
577 memcpy(&kver
, data
, sizeof(kver
));
578 obj
->kern_version
= kver
;
579 pr_debug("kernel version of %s is %x\n", obj
->path
,
584 static int compare_bpf_map(const void *_a
, const void *_b
)
586 const struct bpf_map
*a
= _a
;
587 const struct bpf_map
*b
= _b
;
589 return a
->offset
- b
->offset
;
593 bpf_object__init_maps(struct bpf_object
*obj
)
595 int i
, map_idx
, map_def_sz
, nr_maps
= 0;
598 Elf_Data
*symbols
= obj
->efile
.symbols
;
600 if (obj
->efile
.maps_shndx
< 0)
605 scn
= elf_getscn(obj
->efile
.elf
, obj
->efile
.maps_shndx
);
607 data
= elf_getdata(scn
, NULL
);
609 pr_warning("failed to get Elf_Data from map section %d\n",
610 obj
->efile
.maps_shndx
);
615 * Count number of maps. Each map has a name.
616 * Array of maps is not supported: only the first element is
619 * TODO: Detect array of map and report error.
621 for (i
= 0; i
< symbols
->d_size
/ sizeof(GElf_Sym
); i
++) {
624 if (!gelf_getsym(symbols
, i
, &sym
))
626 if (sym
.st_shndx
!= obj
->efile
.maps_shndx
)
631 /* Alloc obj->maps and fill nr_maps. */
632 pr_debug("maps in %s: %d maps in %zd bytes\n", obj
->path
,
633 nr_maps
, data
->d_size
);
638 /* Assume equally sized map definitions */
639 map_def_sz
= data
->d_size
/ nr_maps
;
640 if (!data
->d_size
|| (data
->d_size
% nr_maps
) != 0) {
641 pr_warning("unable to determine map definition size "
642 "section %s, %d maps in %zd bytes\n",
643 obj
->path
, nr_maps
, data
->d_size
);
647 obj
->maps
= calloc(nr_maps
, sizeof(obj
->maps
[0]));
649 pr_warning("alloc maps for object failed\n");
652 obj
->nr_maps
= nr_maps
;
655 * fill all fd with -1 so won't close incorrect
656 * fd (fd=0 is stdin) when failure (zclose won't close
659 for (i
= 0; i
< nr_maps
; i
++)
660 obj
->maps
[i
].fd
= -1;
663 * Fill obj->maps using data in "maps" section.
665 for (i
= 0, map_idx
= 0; i
< symbols
->d_size
/ sizeof(GElf_Sym
); i
++) {
667 const char *map_name
;
668 struct bpf_map_def
*def
;
670 if (!gelf_getsym(symbols
, i
, &sym
))
672 if (sym
.st_shndx
!= obj
->efile
.maps_shndx
)
675 map_name
= elf_strptr(obj
->efile
.elf
,
676 obj
->efile
.strtabidx
,
678 obj
->maps
[map_idx
].offset
= sym
.st_value
;
679 if (sym
.st_value
+ map_def_sz
> data
->d_size
) {
680 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
681 obj
->path
, map_name
);
685 obj
->maps
[map_idx
].name
= strdup(map_name
);
686 if (!obj
->maps
[map_idx
].name
) {
687 pr_warning("failed to alloc map name\n");
690 pr_debug("map %d is \"%s\"\n", map_idx
,
691 obj
->maps
[map_idx
].name
);
692 def
= (struct bpf_map_def
*)(data
->d_buf
+ sym
.st_value
);
694 * If the definition of the map in the object file fits in
695 * bpf_map_def, copy it. Any extra fields in our version
696 * of bpf_map_def will default to zero as a result of the
699 if (map_def_sz
<= sizeof(struct bpf_map_def
)) {
700 memcpy(&obj
->maps
[map_idx
].def
, def
, map_def_sz
);
703 * Here the map structure being read is bigger than what
704 * we expect, truncate if the excess bits are all zero.
705 * If they are not zero, reject this map as
709 for (b
= ((char *)def
) + sizeof(struct bpf_map_def
);
710 b
< ((char *)def
) + map_def_sz
; b
++) {
712 pr_warning("maps section in %s: \"%s\" "
713 "has unrecognized, non-zero "
715 obj
->path
, map_name
);
719 memcpy(&obj
->maps
[map_idx
].def
, def
,
720 sizeof(struct bpf_map_def
));
725 qsort(obj
->maps
, obj
->nr_maps
, sizeof(obj
->maps
[0]), compare_bpf_map
);
729 static int bpf_object__elf_collect(struct bpf_object
*obj
)
731 Elf
*elf
= obj
->efile
.elf
;
732 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
734 int idx
= 0, err
= 0;
736 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
737 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
738 pr_warning("failed to get e_shstrndx from %s\n",
740 return -LIBBPF_ERRNO__FORMAT
;
743 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
749 if (gelf_getshdr(scn
, &sh
) != &sh
) {
750 pr_warning("failed to get section header from %s\n",
752 err
= -LIBBPF_ERRNO__FORMAT
;
756 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
758 pr_warning("failed to get section name from %s\n",
760 err
= -LIBBPF_ERRNO__FORMAT
;
764 data
= elf_getdata(scn
, 0);
766 pr_warning("failed to get section data from %s(%s)\n",
768 err
= -LIBBPF_ERRNO__FORMAT
;
771 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
772 name
, (unsigned long)data
->d_size
,
773 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
776 if (strcmp(name
, "license") == 0)
777 err
= bpf_object__init_license(obj
,
780 else if (strcmp(name
, "version") == 0)
781 err
= bpf_object__init_kversion(obj
,
784 else if (strcmp(name
, "maps") == 0)
785 obj
->efile
.maps_shndx
= idx
;
786 else if (sh
.sh_type
== SHT_SYMTAB
) {
787 if (obj
->efile
.symbols
) {
788 pr_warning("bpf: multiple SYMTAB in %s\n",
790 err
= -LIBBPF_ERRNO__FORMAT
;
792 obj
->efile
.symbols
= data
;
793 obj
->efile
.strtabidx
= sh
.sh_link
;
795 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
796 (sh
.sh_flags
& SHF_EXECINSTR
) &&
797 (data
->d_size
> 0)) {
798 err
= bpf_object__add_program(obj
, data
->d_buf
,
799 data
->d_size
, name
, idx
);
801 char errmsg
[STRERR_BUFSIZE
];
803 strerror_r(-err
, errmsg
, sizeof(errmsg
));
804 pr_warning("failed to alloc program %s (%s): %s",
805 name
, obj
->path
, errmsg
);
807 } else if (sh
.sh_type
== SHT_REL
) {
808 void *reloc
= obj
->efile
.reloc
;
809 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
811 reloc
= realloc(reloc
,
812 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
814 pr_warning("realloc failed\n");
817 int n
= nr_reloc
- 1;
819 obj
->efile
.reloc
= reloc
;
820 obj
->efile
.nr_reloc
= nr_reloc
;
822 obj
->efile
.reloc
[n
].shdr
= sh
;
823 obj
->efile
.reloc
[n
].data
= data
;
830 if (!obj
->efile
.strtabidx
|| obj
->efile
.strtabidx
>= idx
) {
831 pr_warning("Corrupted ELF file: index of strtab invalid\n");
832 return LIBBPF_ERRNO__FORMAT
;
834 if (obj
->efile
.maps_shndx
>= 0) {
835 err
= bpf_object__init_maps(obj
);
839 err
= bpf_object__init_prog_names(obj
);
844 static struct bpf_program
*
845 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
847 struct bpf_program
*prog
;
850 for (i
= 0; i
< obj
->nr_programs
; i
++) {
851 prog
= &obj
->programs
[i
];
852 if (prog
->idx
== idx
)
859 bpf_program__collect_reloc(struct bpf_program
*prog
,
860 size_t nr_maps
, GElf_Shdr
*shdr
,
861 Elf_Data
*data
, Elf_Data
*symbols
,
862 int maps_shndx
, struct bpf_map
*maps
)
866 pr_debug("collecting relocating info for: '%s'\n",
868 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
870 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
871 if (!prog
->reloc_desc
) {
872 pr_warning("failed to alloc memory in relocation\n");
875 prog
->nr_reloc
= nrels
;
877 for (i
= 0; i
< nrels
; i
++) {
880 unsigned int insn_idx
;
881 struct bpf_insn
*insns
= prog
->insns
;
884 if (!gelf_getrel(data
, i
, &rel
)) {
885 pr_warning("relocation: failed to get %d reloc\n", i
);
886 return -LIBBPF_ERRNO__FORMAT
;
889 if (!gelf_getsym(symbols
,
890 GELF_R_SYM(rel
.r_info
),
892 pr_warning("relocation: symbol %"PRIx64
" not found\n",
893 GELF_R_SYM(rel
.r_info
));
894 return -LIBBPF_ERRNO__FORMAT
;
897 if (sym
.st_shndx
!= maps_shndx
) {
898 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
899 prog
->section_name
, sym
.st_shndx
);
900 return -LIBBPF_ERRNO__RELOC
;
903 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
904 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
906 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
907 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
908 insn_idx
, insns
[insn_idx
].code
);
909 return -LIBBPF_ERRNO__RELOC
;
912 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
913 for (map_idx
= 0; map_idx
< nr_maps
; map_idx
++) {
914 if (maps
[map_idx
].offset
== sym
.st_value
) {
915 pr_debug("relocation: find map %zd (%s) for insn %u\n",
916 map_idx
, maps
[map_idx
].name
, insn_idx
);
921 if (map_idx
>= nr_maps
) {
922 pr_warning("bpf relocation: map_idx %d large than %d\n",
923 (int)map_idx
, (int)nr_maps
- 1);
924 return -LIBBPF_ERRNO__RELOC
;
927 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
928 prog
->reloc_desc
[i
].map_idx
= map_idx
;
934 bpf_object__create_maps(struct bpf_object
*obj
)
938 for (i
= 0; i
< obj
->nr_maps
; i
++) {
939 struct bpf_map_def
*def
= &obj
->maps
[i
].def
;
940 int *pfd
= &obj
->maps
[i
].fd
;
942 *pfd
= bpf_create_map_name(def
->type
,
952 pr_warning("failed to create map (name: '%s'): %s\n",
955 for (j
= 0; j
< i
; j
++)
956 zclose(obj
->maps
[j
].fd
);
959 pr_debug("create map %s: fd=%d\n", obj
->maps
[i
].name
, *pfd
);
966 bpf_program__relocate(struct bpf_program
*prog
, struct bpf_object
*obj
)
970 if (!prog
|| !prog
->reloc_desc
)
973 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
974 int insn_idx
, map_idx
;
975 struct bpf_insn
*insns
= prog
->insns
;
977 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
978 map_idx
= prog
->reloc_desc
[i
].map_idx
;
980 if (insn_idx
>= (int)prog
->insns_cnt
) {
981 pr_warning("relocation out of range: '%s'\n",
983 return -LIBBPF_ERRNO__RELOC
;
985 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
986 insns
[insn_idx
].imm
= obj
->maps
[map_idx
].fd
;
989 zfree(&prog
->reloc_desc
);
996 bpf_object__relocate(struct bpf_object
*obj
)
998 struct bpf_program
*prog
;
1002 for (i
= 0; i
< obj
->nr_programs
; i
++) {
1003 prog
= &obj
->programs
[i
];
1005 err
= bpf_program__relocate(prog
, obj
);
1007 pr_warning("failed to relocate '%s'\n",
1008 prog
->section_name
);
1015 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
1019 if (!obj_elf_valid(obj
)) {
1020 pr_warning("Internal error: elf object is closed\n");
1021 return -LIBBPF_ERRNO__INTERNAL
;
1024 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
1025 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
1026 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
1027 int idx
= shdr
->sh_info
;
1028 struct bpf_program
*prog
;
1029 size_t nr_maps
= obj
->nr_maps
;
1031 if (shdr
->sh_type
!= SHT_REL
) {
1032 pr_warning("internal error at %d\n", __LINE__
);
1033 return -LIBBPF_ERRNO__INTERNAL
;
1036 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
1038 pr_warning("relocation failed: no %d section\n",
1040 return -LIBBPF_ERRNO__RELOC
;
1043 err
= bpf_program__collect_reloc(prog
, nr_maps
,
1046 obj
->efile
.maps_shndx
,
1055 load_program(enum bpf_prog_type type
, const char *name
, struct bpf_insn
*insns
,
1056 int insns_cnt
, char *license
, u32 kern_version
, int *pfd
)
1061 if (!insns
|| !insns_cnt
)
1064 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
1066 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1068 ret
= bpf_load_program_name(type
, name
, insns
, insns_cnt
, license
,
1069 kern_version
, log_buf
, BPF_LOG_BUF_SIZE
);
1077 ret
= -LIBBPF_ERRNO__LOAD
;
1078 pr_warning("load bpf program failed: %s\n", strerror(errno
));
1080 if (log_buf
&& log_buf
[0] != '\0') {
1081 ret
= -LIBBPF_ERRNO__VERIFY
;
1082 pr_warning("-- BEGIN DUMP LOG ---\n");
1083 pr_warning("\n%s\n", log_buf
);
1084 pr_warning("-- END LOG --\n");
1085 } else if (insns_cnt
>= BPF_MAXINSNS
) {
1086 pr_warning("Program too large (%d insns), at most %d insns\n",
1087 insns_cnt
, BPF_MAXINSNS
);
1088 ret
= -LIBBPF_ERRNO__PROG2BIG
;
1090 /* Wrong program type? */
1091 if (type
!= BPF_PROG_TYPE_KPROBE
) {
1094 fd
= bpf_load_program_name(BPF_PROG_TYPE_KPROBE
, name
,
1095 insns
, insns_cnt
, license
,
1096 kern_version
, NULL
, 0);
1099 ret
= -LIBBPF_ERRNO__PROGTYPE
;
1105 ret
= -LIBBPF_ERRNO__KVER
;
1114 bpf_program__load(struct bpf_program
*prog
,
1115 char *license
, u32 kern_version
)
1119 if (prog
->instances
.nr
< 0 || !prog
->instances
.fds
) {
1120 if (prog
->preprocessor
) {
1121 pr_warning("Internal error: can't load program '%s'\n",
1122 prog
->section_name
);
1123 return -LIBBPF_ERRNO__INTERNAL
;
1126 prog
->instances
.fds
= malloc(sizeof(int));
1127 if (!prog
->instances
.fds
) {
1128 pr_warning("Not enough memory for BPF fds\n");
1131 prog
->instances
.nr
= 1;
1132 prog
->instances
.fds
[0] = -1;
1135 if (!prog
->preprocessor
) {
1136 if (prog
->instances
.nr
!= 1) {
1137 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1138 prog
->section_name
, prog
->instances
.nr
);
1140 err
= load_program(prog
->type
, prog
->name
, prog
->insns
,
1141 prog
->insns_cnt
, license
, kern_version
, &fd
);
1143 prog
->instances
.fds
[0] = fd
;
1147 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
1148 struct bpf_prog_prep_result result
;
1149 bpf_program_prep_t preprocessor
= prog
->preprocessor
;
1151 bzero(&result
, sizeof(result
));
1152 err
= preprocessor(prog
, i
, prog
->insns
,
1153 prog
->insns_cnt
, &result
);
1155 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1156 i
, prog
->section_name
);
1160 if (!result
.new_insn_ptr
|| !result
.new_insn_cnt
) {
1161 pr_debug("Skip loading the %dth instance of program '%s'\n",
1162 i
, prog
->section_name
);
1163 prog
->instances
.fds
[i
] = -1;
1169 err
= load_program(prog
->type
, prog
->name
,
1170 result
.new_insn_ptr
,
1171 result
.new_insn_cnt
,
1172 license
, kern_version
, &fd
);
1175 pr_warning("Loading the %dth instance of program '%s' failed\n",
1176 i
, prog
->section_name
);
1182 prog
->instances
.fds
[i
] = fd
;
1186 pr_warning("failed to load program '%s'\n",
1187 prog
->section_name
);
1188 zfree(&prog
->insns
);
1189 prog
->insns_cnt
= 0;
1194 bpf_object__load_progs(struct bpf_object
*obj
)
1199 for (i
= 0; i
< obj
->nr_programs
; i
++) {
1200 err
= bpf_program__load(&obj
->programs
[i
],
1209 static int bpf_object__validate(struct bpf_object
*obj
)
1211 if (obj
->kern_version
== 0) {
1212 pr_warning("%s doesn't provide kernel version\n",
1214 return -LIBBPF_ERRNO__KVERSION
;
1219 static struct bpf_object
*
1220 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
1222 struct bpf_object
*obj
;
1225 if (elf_version(EV_CURRENT
) == EV_NONE
) {
1226 pr_warning("failed to init libelf for %s\n", path
);
1227 return ERR_PTR(-LIBBPF_ERRNO__LIBELF
);
1230 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
1234 CHECK_ERR(bpf_object__elf_init(obj
), err
, out
);
1235 CHECK_ERR(bpf_object__check_endianness(obj
), err
, out
);
1236 CHECK_ERR(bpf_object__elf_collect(obj
), err
, out
);
1237 CHECK_ERR(bpf_object__collect_reloc(obj
), err
, out
);
1238 CHECK_ERR(bpf_object__validate(obj
), err
, out
);
1240 bpf_object__elf_finish(obj
);
1243 bpf_object__close(obj
);
1244 return ERR_PTR(err
);
1247 struct bpf_object
*bpf_object__open(const char *path
)
1249 /* param validation */
1253 pr_debug("loading %s\n", path
);
1255 return __bpf_object__open(path
, NULL
, 0);
1258 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
1264 /* param validation */
1265 if (!obj_buf
|| obj_buf_sz
<= 0)
1269 snprintf(tmp_name
, sizeof(tmp_name
), "%lx-%lx",
1270 (unsigned long)obj_buf
,
1271 (unsigned long)obj_buf_sz
);
1272 tmp_name
[sizeof(tmp_name
) - 1] = '\0';
1275 pr_debug("loading object '%s' from buffer\n",
1278 return __bpf_object__open(name
, obj_buf
, obj_buf_sz
);
1281 int bpf_object__unload(struct bpf_object
*obj
)
1288 for (i
= 0; i
< obj
->nr_maps
; i
++)
1289 zclose(obj
->maps
[i
].fd
);
1291 for (i
= 0; i
< obj
->nr_programs
; i
++)
1292 bpf_program__unload(&obj
->programs
[i
]);
1297 int bpf_object__load(struct bpf_object
*obj
)
1305 pr_warning("object should not be loaded twice\n");
1311 CHECK_ERR(bpf_object__create_maps(obj
), err
, out
);
1312 CHECK_ERR(bpf_object__relocate(obj
), err
, out
);
1313 CHECK_ERR(bpf_object__load_progs(obj
), err
, out
);
1317 bpf_object__unload(obj
);
1318 pr_warning("failed to load object '%s'\n", obj
->path
);
1322 static int check_path(const char *path
)
1324 struct statfs st_fs
;
1331 dname
= strdup(path
);
1335 dir
= dirname(dname
);
1336 if (statfs(dir
, &st_fs
)) {
1337 pr_warning("failed to statfs %s: %s\n", dir
, strerror(errno
));
1342 if (!err
&& st_fs
.f_type
!= BPF_FS_MAGIC
) {
1343 pr_warning("specified path %s is not on BPF FS\n", path
);
1350 int bpf_program__pin_instance(struct bpf_program
*prog
, const char *path
,
1355 err
= check_path(path
);
1360 pr_warning("invalid program pointer\n");
1364 if (instance
< 0 || instance
>= prog
->instances
.nr
) {
1365 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1366 instance
, prog
->section_name
, prog
->instances
.nr
);
1370 if (bpf_obj_pin(prog
->instances
.fds
[instance
], path
)) {
1371 pr_warning("failed to pin program: %s\n", strerror(errno
));
1374 pr_debug("pinned program '%s'\n", path
);
1379 static int make_dir(const char *path
)
1383 if (mkdir(path
, 0700) && errno
!= EEXIST
)
1387 pr_warning("failed to mkdir %s: %s\n", path
, strerror(-err
));
1391 int bpf_program__pin(struct bpf_program
*prog
, const char *path
)
1395 err
= check_path(path
);
1400 pr_warning("invalid program pointer\n");
1404 if (prog
->instances
.nr
<= 0) {
1405 pr_warning("no instances of prog %s to pin\n",
1406 prog
->section_name
);
1410 err
= make_dir(path
);
1414 for (i
= 0; i
< prog
->instances
.nr
; i
++) {
1418 len
= snprintf(buf
, PATH_MAX
, "%s/%d", path
, i
);
1421 else if (len
>= PATH_MAX
)
1422 return -ENAMETOOLONG
;
1424 err
= bpf_program__pin_instance(prog
, buf
, i
);
1432 int bpf_map__pin(struct bpf_map
*map
, const char *path
)
1436 err
= check_path(path
);
1441 pr_warning("invalid map pointer\n");
1445 if (bpf_obj_pin(map
->fd
, path
)) {
1446 pr_warning("failed to pin map: %s\n", strerror(errno
));
1450 pr_debug("pinned map '%s'\n", path
);
1454 int bpf_object__pin(struct bpf_object
*obj
, const char *path
)
1456 struct bpf_program
*prog
;
1457 struct bpf_map
*map
;
1464 pr_warning("object not yet loaded; load it first\n");
1468 err
= make_dir(path
);
1472 bpf_map__for_each(map
, obj
) {
1476 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
1477 bpf_map__name(map
));
1480 else if (len
>= PATH_MAX
)
1481 return -ENAMETOOLONG
;
1483 err
= bpf_map__pin(map
, buf
);
1488 bpf_object__for_each_program(prog
, obj
) {
1492 len
= snprintf(buf
, PATH_MAX
, "%s/%s", path
,
1493 prog
->section_name
);
1496 else if (len
>= PATH_MAX
)
1497 return -ENAMETOOLONG
;
1499 err
= bpf_program__pin(prog
, buf
);
1507 void bpf_object__close(struct bpf_object
*obj
)
1514 if (obj
->clear_priv
)
1515 obj
->clear_priv(obj
, obj
->priv
);
1517 bpf_object__elf_finish(obj
);
1518 bpf_object__unload(obj
);
1520 for (i
= 0; i
< obj
->nr_maps
; i
++) {
1521 zfree(&obj
->maps
[i
].name
);
1522 if (obj
->maps
[i
].clear_priv
)
1523 obj
->maps
[i
].clear_priv(&obj
->maps
[i
],
1525 obj
->maps
[i
].priv
= NULL
;
1526 obj
->maps
[i
].clear_priv
= NULL
;
1531 if (obj
->programs
&& obj
->nr_programs
) {
1532 for (i
= 0; i
< obj
->nr_programs
; i
++)
1533 bpf_program__exit(&obj
->programs
[i
]);
1535 zfree(&obj
->programs
);
1537 list_del(&obj
->list
);
1542 bpf_object__next(struct bpf_object
*prev
)
1544 struct bpf_object
*next
;
1547 next
= list_first_entry(&bpf_objects_list
,
1551 next
= list_next_entry(prev
, list
);
1553 /* Empty list is noticed here so don't need checking on entry. */
1554 if (&next
->list
== &bpf_objects_list
)
1560 const char *bpf_object__name(struct bpf_object
*obj
)
1562 return obj
? obj
->path
: ERR_PTR(-EINVAL
);
1565 unsigned int bpf_object__kversion(struct bpf_object
*obj
)
1567 return obj
? obj
->kern_version
: 0;
1570 int bpf_object__set_priv(struct bpf_object
*obj
, void *priv
,
1571 bpf_object_clear_priv_t clear_priv
)
1573 if (obj
->priv
&& obj
->clear_priv
)
1574 obj
->clear_priv(obj
, obj
->priv
);
1577 obj
->clear_priv
= clear_priv
;
1581 void *bpf_object__priv(struct bpf_object
*obj
)
1583 return obj
? obj
->priv
: ERR_PTR(-EINVAL
);
1586 struct bpf_program
*
1587 bpf_program__next(struct bpf_program
*prev
, struct bpf_object
*obj
)
1595 return &obj
->programs
[0];
1597 if (prev
->obj
!= obj
) {
1598 pr_warning("error: program handler doesn't match object\n");
1602 idx
= (prev
- obj
->programs
) + 1;
1603 if (idx
>= obj
->nr_programs
)
1605 return &obj
->programs
[idx
];
1608 int bpf_program__set_priv(struct bpf_program
*prog
, void *priv
,
1609 bpf_program_clear_priv_t clear_priv
)
1611 if (prog
->priv
&& prog
->clear_priv
)
1612 prog
->clear_priv(prog
, prog
->priv
);
1615 prog
->clear_priv
= clear_priv
;
1619 void *bpf_program__priv(struct bpf_program
*prog
)
1621 return prog
? prog
->priv
: ERR_PTR(-EINVAL
);
1624 const char *bpf_program__title(struct bpf_program
*prog
, bool needs_copy
)
1628 title
= prog
->section_name
;
1630 title
= strdup(title
);
1632 pr_warning("failed to strdup program title\n");
1633 return ERR_PTR(-ENOMEM
);
1640 int bpf_program__fd(struct bpf_program
*prog
)
1642 return bpf_program__nth_fd(prog
, 0);
1645 int bpf_program__set_prep(struct bpf_program
*prog
, int nr_instances
,
1646 bpf_program_prep_t prep
)
1650 if (nr_instances
<= 0 || !prep
)
1653 if (prog
->instances
.nr
> 0 || prog
->instances
.fds
) {
1654 pr_warning("Can't set pre-processor after loading\n");
1658 instances_fds
= malloc(sizeof(int) * nr_instances
);
1659 if (!instances_fds
) {
1660 pr_warning("alloc memory failed for fds\n");
1664 /* fill all fd with -1 */
1665 memset(instances_fds
, -1, sizeof(int) * nr_instances
);
1667 prog
->instances
.nr
= nr_instances
;
1668 prog
->instances
.fds
= instances_fds
;
1669 prog
->preprocessor
= prep
;
1673 int bpf_program__nth_fd(struct bpf_program
*prog
, int n
)
1677 if (n
>= prog
->instances
.nr
|| n
< 0) {
1678 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1679 n
, prog
->section_name
, prog
->instances
.nr
);
1683 fd
= prog
->instances
.fds
[n
];
1685 pr_warning("%dth instance of program '%s' is invalid\n",
1686 n
, prog
->section_name
);
1693 void bpf_program__set_type(struct bpf_program
*prog
, enum bpf_prog_type type
)
1698 static bool bpf_program__is_type(struct bpf_program
*prog
,
1699 enum bpf_prog_type type
)
1701 return prog
? (prog
->type
== type
) : false;
1704 #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1705 int bpf_program__set_##NAME(struct bpf_program *prog) \
1709 bpf_program__set_type(prog, TYPE); \
1713 bool bpf_program__is_##NAME(struct bpf_program *prog) \
1715 return bpf_program__is_type(prog, TYPE); \
1718 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1719 BPF_PROG_TYPE_FNS(kprobe
, BPF_PROG_TYPE_KPROBE
);
1720 BPF_PROG_TYPE_FNS(sched_cls
, BPF_PROG_TYPE_SCHED_CLS
);
1721 BPF_PROG_TYPE_FNS(sched_act
, BPF_PROG_TYPE_SCHED_ACT
);
1722 BPF_PROG_TYPE_FNS(tracepoint
, BPF_PROG_TYPE_TRACEPOINT
);
1723 BPF_PROG_TYPE_FNS(xdp
, BPF_PROG_TYPE_XDP
);
1724 BPF_PROG_TYPE_FNS(perf_event
, BPF_PROG_TYPE_PERF_EVENT
);
1726 int bpf_map__fd(struct bpf_map
*map
)
1728 return map
? map
->fd
: -EINVAL
;
1731 const struct bpf_map_def
*bpf_map__def(struct bpf_map
*map
)
1733 return map
? &map
->def
: ERR_PTR(-EINVAL
);
1736 const char *bpf_map__name(struct bpf_map
*map
)
1738 return map
? map
->name
: NULL
;
1741 int bpf_map__set_priv(struct bpf_map
*map
, void *priv
,
1742 bpf_map_clear_priv_t clear_priv
)
1748 if (map
->clear_priv
)
1749 map
->clear_priv(map
, map
->priv
);
1753 map
->clear_priv
= clear_priv
;
1757 void *bpf_map__priv(struct bpf_map
*map
)
1759 return map
? map
->priv
: ERR_PTR(-EINVAL
);
1763 bpf_map__next(struct bpf_map
*prev
, struct bpf_object
*obj
)
1766 struct bpf_map
*s
, *e
;
1768 if (!obj
|| !obj
->maps
)
1772 e
= obj
->maps
+ obj
->nr_maps
;
1777 if ((prev
< s
) || (prev
>= e
)) {
1778 pr_warning("error in %s: map handler doesn't belong to object\n",
1783 idx
= (prev
- obj
->maps
) + 1;
1784 if (idx
>= obj
->nr_maps
)
1786 return &obj
->maps
[idx
];
1790 bpf_object__find_map_by_name(struct bpf_object
*obj
, const char *name
)
1792 struct bpf_map
*pos
;
1794 bpf_map__for_each(pos
, obj
) {
1795 if (pos
->name
&& !strcmp(pos
->name
, name
))
1802 bpf_object__find_map_by_offset(struct bpf_object
*obj
, size_t offset
)
1806 for (i
= 0; i
< obj
->nr_maps
; i
++) {
1807 if (obj
->maps
[i
].offset
== offset
)
1808 return &obj
->maps
[i
];
1810 return ERR_PTR(-ENOENT
);
1813 long libbpf_get_error(const void *ptr
)
1816 return PTR_ERR(ptr
);
1820 int bpf_prog_load(const char *file
, enum bpf_prog_type type
,
1821 struct bpf_object
**pobj
, int *prog_fd
)
1823 struct bpf_program
*prog
;
1824 struct bpf_object
*obj
;
1827 obj
= bpf_object__open(file
);
1831 prog
= bpf_program__next(NULL
, obj
);
1833 bpf_object__close(obj
);
1837 bpf_program__set_type(prog
, type
);
1838 err
= bpf_object__load(obj
);
1840 bpf_object__close(obj
);
1845 *prog_fd
= bpf_program__fd(prog
);