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>
26 #define __printf(a, b) __attribute__((format(printf, a, b)))
29 static int __base_pr(const char *format
, ...)
34 va_start(args
, format
);
35 err
= vfprintf(stderr
, format
, args
);
40 static __printf(1, 2) libbpf_print_fn_t __pr_warning
= __base_pr
;
41 static __printf(1, 2) libbpf_print_fn_t __pr_info
= __base_pr
;
42 static __printf(1, 2) libbpf_print_fn_t __pr_debug
;
44 #define __pr(func, fmt, ...) \
47 (func)("libbpf: " fmt, ##__VA_ARGS__); \
50 #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
51 #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
52 #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
54 void libbpf_set_print(libbpf_print_fn_t warn
,
55 libbpf_print_fn_t info
,
56 libbpf_print_fn_t debug
)
63 /* Copied from tools/perf/util/util.h */
65 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
69 # define zclose(fd) ({ \
72 ___err = close((fd)); \
77 #ifdef HAVE_LIBELF_MMAP_SUPPORT
78 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
80 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
84 * bpf_prog should be a better name but it has been used in
88 /* Index in elf obj file, for relocation use. */
91 struct bpf_insn
*insns
;
109 struct bpf_program
*programs
;
113 * This field is required because maps_buf will be freed and
114 * maps_buf_sz will be set to 0 after loaded.
120 * Information when doing elf related work. Only valid if fd
138 #define obj_elf_valid(o) ((o)->efile.elf)
140 static void bpf_program__unload(struct bpf_program
*prog
)
148 static void bpf_program__exit(struct bpf_program
*prog
)
153 bpf_program__unload(prog
);
154 zfree(&prog
->section_name
);
156 zfree(&prog
->reloc_desc
);
164 bpf_program__init(void *data
, size_t size
, char *name
, int idx
,
165 struct bpf_program
*prog
)
167 if (size
< sizeof(struct bpf_insn
)) {
168 pr_warning("corrupted section '%s'\n", name
);
172 bzero(prog
, sizeof(*prog
));
174 prog
->section_name
= strdup(name
);
175 if (!prog
->section_name
) {
176 pr_warning("failed to alloc name for prog %s\n",
181 prog
->insns
= malloc(size
);
183 pr_warning("failed to alloc insns for %s\n", name
);
186 prog
->insns_cnt
= size
/ sizeof(struct bpf_insn
);
187 memcpy(prog
->insns
, data
,
188 prog
->insns_cnt
* sizeof(struct bpf_insn
));
194 bpf_program__exit(prog
);
199 bpf_object__add_program(struct bpf_object
*obj
, void *data
, size_t size
,
202 struct bpf_program prog
, *progs
;
205 err
= bpf_program__init(data
, size
, name
, idx
, &prog
);
209 progs
= obj
->programs
;
210 nr_progs
= obj
->nr_programs
;
212 progs
= realloc(progs
, sizeof(progs
[0]) * (nr_progs
+ 1));
215 * In this case the original obj->programs
216 * is still valid, so don't need special treat for
217 * bpf_close_object().
219 pr_warning("failed to alloc a new program '%s'\n",
221 bpf_program__exit(&prog
);
225 pr_debug("found program %s\n", prog
.section_name
);
226 obj
->programs
= progs
;
227 obj
->nr_programs
= nr_progs
+ 1;
228 progs
[nr_progs
] = prog
;
232 static struct bpf_object
*bpf_object__new(const char *path
,
236 struct bpf_object
*obj
;
238 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
240 pr_warning("alloc memory failed for %s\n", path
);
244 strcpy(obj
->path
, path
);
248 * Caller of this function should also calls
249 * bpf_object__elf_finish() after data collection to return
250 * obj_buf to user. If not, we should duplicate the buffer to
251 * avoid user freeing them before elf finish.
253 obj
->efile
.obj_buf
= obj_buf
;
254 obj
->efile
.obj_buf_sz
= obj_buf_sz
;
260 static void bpf_object__elf_finish(struct bpf_object
*obj
)
262 if (!obj_elf_valid(obj
))
265 if (obj
->efile
.elf
) {
266 elf_end(obj
->efile
.elf
);
267 obj
->efile
.elf
= NULL
;
269 obj
->efile
.symbols
= NULL
;
271 zfree(&obj
->efile
.reloc
);
272 obj
->efile
.nr_reloc
= 0;
273 zclose(obj
->efile
.fd
);
274 obj
->efile
.obj_buf
= NULL
;
275 obj
->efile
.obj_buf_sz
= 0;
278 static int bpf_object__elf_init(struct bpf_object
*obj
)
283 if (obj_elf_valid(obj
)) {
284 pr_warning("elf init: internal error\n");
288 if (obj
->efile
.obj_buf_sz
> 0) {
290 * obj_buf should have been validated by
291 * bpf_object__open_buffer().
293 obj
->efile
.elf
= elf_memory(obj
->efile
.obj_buf
,
294 obj
->efile
.obj_buf_sz
);
296 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
);
297 if (obj
->efile
.fd
< 0) {
298 pr_warning("failed to open %s: %s\n", obj
->path
,
303 obj
->efile
.elf
= elf_begin(obj
->efile
.fd
,
304 LIBBPF_ELF_C_READ_MMAP
,
308 if (!obj
->efile
.elf
) {
309 pr_warning("failed to open %s as ELF file\n",
315 if (!gelf_getehdr(obj
->efile
.elf
, &obj
->efile
.ehdr
)) {
316 pr_warning("failed to get EHDR from %s\n",
321 ep
= &obj
->efile
.ehdr
;
323 if ((ep
->e_type
!= ET_REL
) || (ep
->e_machine
!= 0)) {
324 pr_warning("%s is not an eBPF object file\n",
332 bpf_object__elf_finish(obj
);
337 bpf_object__check_endianness(struct bpf_object
*obj
)
339 static unsigned int const endian
= 1;
341 switch (obj
->efile
.ehdr
.e_ident
[EI_DATA
]) {
343 /* We are big endian, BPF obj is little endian. */
344 if (*(unsigned char const *)&endian
!= 1)
349 /* We are little endian, BPF obj is big endian. */
350 if (*(unsigned char const *)&endian
!= 0)
360 pr_warning("Error: endianness mismatch.\n");
365 bpf_object__init_license(struct bpf_object
*obj
,
366 void *data
, size_t size
)
368 memcpy(obj
->license
, data
,
369 min(size
, sizeof(obj
->license
) - 1));
370 pr_debug("license of %s is %s\n", obj
->path
, obj
->license
);
375 bpf_object__init_kversion(struct bpf_object
*obj
,
376 void *data
, size_t size
)
380 if (size
!= sizeof(kver
)) {
381 pr_warning("invalid kver section in %s\n", obj
->path
);
384 memcpy(&kver
, data
, sizeof(kver
));
385 obj
->kern_version
= kver
;
386 pr_debug("kernel version of %s is %x\n", obj
->path
,
392 bpf_object__init_maps(struct bpf_object
*obj
, void *data
,
396 pr_debug("%s doesn't need map definition\n",
401 obj
->maps_buf
= malloc(size
);
402 if (!obj
->maps_buf
) {
403 pr_warning("malloc maps failed: %s\n", obj
->path
);
407 obj
->maps_buf_sz
= size
;
408 memcpy(obj
->maps_buf
, data
, size
);
409 pr_debug("maps in %s: %ld bytes\n", obj
->path
, (long)size
);
413 static int bpf_object__elf_collect(struct bpf_object
*obj
)
415 Elf
*elf
= obj
->efile
.elf
;
416 GElf_Ehdr
*ep
= &obj
->efile
.ehdr
;
418 int idx
= 0, err
= 0;
420 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
421 if (!elf_rawdata(elf_getscn(elf
, ep
->e_shstrndx
), NULL
)) {
422 pr_warning("failed to get e_shstrndx from %s\n",
427 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
433 if (gelf_getshdr(scn
, &sh
) != &sh
) {
434 pr_warning("failed to get section header from %s\n",
440 name
= elf_strptr(elf
, ep
->e_shstrndx
, sh
.sh_name
);
442 pr_warning("failed to get section name from %s\n",
448 data
= elf_getdata(scn
, 0);
450 pr_warning("failed to get section data from %s(%s)\n",
455 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
456 name
, (unsigned long)data
->d_size
,
457 (int)sh
.sh_link
, (unsigned long)sh
.sh_flags
,
460 if (strcmp(name
, "license") == 0)
461 err
= bpf_object__init_license(obj
,
464 else if (strcmp(name
, "version") == 0)
465 err
= bpf_object__init_kversion(obj
,
468 else if (strcmp(name
, "maps") == 0)
469 err
= bpf_object__init_maps(obj
, data
->d_buf
,
471 else if (sh
.sh_type
== SHT_SYMTAB
) {
472 if (obj
->efile
.symbols
) {
473 pr_warning("bpf: multiple SYMTAB in %s\n",
477 obj
->efile
.symbols
= data
;
478 } else if ((sh
.sh_type
== SHT_PROGBITS
) &&
479 (sh
.sh_flags
& SHF_EXECINSTR
) &&
480 (data
->d_size
> 0)) {
481 err
= bpf_object__add_program(obj
, data
->d_buf
,
482 data
->d_size
, name
, idx
);
485 strerror_r(-err
, errmsg
, sizeof(errmsg
));
486 pr_warning("failed to alloc program %s (%s): %s",
487 name
, obj
->path
, errmsg
);
489 } else if (sh
.sh_type
== SHT_REL
) {
490 void *reloc
= obj
->efile
.reloc
;
491 int nr_reloc
= obj
->efile
.nr_reloc
+ 1;
493 reloc
= realloc(reloc
,
494 sizeof(*obj
->efile
.reloc
) * nr_reloc
);
496 pr_warning("realloc failed\n");
499 int n
= nr_reloc
- 1;
501 obj
->efile
.reloc
= reloc
;
502 obj
->efile
.nr_reloc
= nr_reloc
;
504 obj
->efile
.reloc
[n
].shdr
= sh
;
505 obj
->efile
.reloc
[n
].data
= data
;
515 static struct bpf_program
*
516 bpf_object__find_prog_by_idx(struct bpf_object
*obj
, int idx
)
518 struct bpf_program
*prog
;
521 for (i
= 0; i
< obj
->nr_programs
; i
++) {
522 prog
= &obj
->programs
[i
];
523 if (prog
->idx
== idx
)
530 bpf_program__collect_reloc(struct bpf_program
*prog
,
531 size_t nr_maps
, GElf_Shdr
*shdr
,
532 Elf_Data
*data
, Elf_Data
*symbols
)
536 pr_debug("collecting relocating info for: '%s'\n",
538 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
540 prog
->reloc_desc
= malloc(sizeof(*prog
->reloc_desc
) * nrels
);
541 if (!prog
->reloc_desc
) {
542 pr_warning("failed to alloc memory in relocation\n");
545 prog
->nr_reloc
= nrels
;
547 for (i
= 0; i
< nrels
; i
++) {
550 unsigned int insn_idx
;
551 struct bpf_insn
*insns
= prog
->insns
;
554 if (!gelf_getrel(data
, i
, &rel
)) {
555 pr_warning("relocation: failed to get %d reloc\n", i
);
559 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
560 pr_debug("relocation: insn_idx=%u\n", insn_idx
);
562 if (!gelf_getsym(symbols
,
563 GELF_R_SYM(rel
.r_info
),
565 pr_warning("relocation: symbol %"PRIx64
" not found\n",
566 GELF_R_SYM(rel
.r_info
));
570 if (insns
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
571 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
572 insn_idx
, insns
[insn_idx
].code
);
576 map_idx
= sym
.st_value
/ sizeof(struct bpf_map_def
);
577 if (map_idx
>= nr_maps
) {
578 pr_warning("bpf relocation: map_idx %d large than %d\n",
579 (int)map_idx
, (int)nr_maps
- 1);
583 prog
->reloc_desc
[i
].insn_idx
= insn_idx
;
584 prog
->reloc_desc
[i
].map_idx
= map_idx
;
590 bpf_object__create_maps(struct bpf_object
*obj
)
596 nr_maps
= obj
->maps_buf_sz
/ sizeof(struct bpf_map_def
);
597 if (!obj
->maps_buf
|| !nr_maps
) {
598 pr_debug("don't need create maps for %s\n",
603 obj
->map_fds
= malloc(sizeof(int) * nr_maps
);
605 pr_warning("realloc perf_bpf_map_fds failed\n");
608 obj
->nr_map_fds
= nr_maps
;
610 /* fill all fd with -1 */
611 memset(obj
->map_fds
, -1, sizeof(int) * nr_maps
);
614 for (i
= 0; i
< nr_maps
; i
++) {
615 struct bpf_map_def def
;
617 def
= *(struct bpf_map_def
*)(obj
->maps_buf
+
618 i
* sizeof(struct bpf_map_def
));
620 *pfd
= bpf_create_map(def
.type
,
628 pr_warning("failed to create map: %s\n",
630 for (j
= 0; j
< i
; j
++)
631 zclose(obj
->map_fds
[j
]);
633 zfree(&obj
->map_fds
);
636 pr_debug("create map: fd=%d\n", *pfd
);
640 zfree(&obj
->maps_buf
);
641 obj
->maps_buf_sz
= 0;
646 bpf_program__relocate(struct bpf_program
*prog
, int *map_fds
)
650 if (!prog
|| !prog
->reloc_desc
)
653 for (i
= 0; i
< prog
->nr_reloc
; i
++) {
654 int insn_idx
, map_idx
;
655 struct bpf_insn
*insns
= prog
->insns
;
657 insn_idx
= prog
->reloc_desc
[i
].insn_idx
;
658 map_idx
= prog
->reloc_desc
[i
].map_idx
;
660 if (insn_idx
>= (int)prog
->insns_cnt
) {
661 pr_warning("relocation out of range: '%s'\n",
665 insns
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
666 insns
[insn_idx
].imm
= map_fds
[map_idx
];
669 zfree(&prog
->reloc_desc
);
676 bpf_object__relocate(struct bpf_object
*obj
)
678 struct bpf_program
*prog
;
682 for (i
= 0; i
< obj
->nr_programs
; i
++) {
683 prog
= &obj
->programs
[i
];
685 err
= bpf_program__relocate(prog
, obj
->map_fds
);
687 pr_warning("failed to relocate '%s'\n",
695 static int bpf_object__collect_reloc(struct bpf_object
*obj
)
699 if (!obj_elf_valid(obj
)) {
700 pr_warning("Internal error: elf object is closed\n");
704 for (i
= 0; i
< obj
->efile
.nr_reloc
; i
++) {
705 GElf_Shdr
*shdr
= &obj
->efile
.reloc
[i
].shdr
;
706 Elf_Data
*data
= obj
->efile
.reloc
[i
].data
;
707 int idx
= shdr
->sh_info
;
708 struct bpf_program
*prog
;
709 size_t nr_maps
= obj
->maps_buf_sz
/
710 sizeof(struct bpf_map_def
);
712 if (shdr
->sh_type
!= SHT_REL
) {
713 pr_warning("internal error at %d\n", __LINE__
);
717 prog
= bpf_object__find_prog_by_idx(obj
, idx
);
719 pr_warning("relocation failed: no %d section\n",
724 err
= bpf_program__collect_reloc(prog
, nr_maps
,
734 load_program(struct bpf_insn
*insns
, int insns_cnt
,
735 char *license
, u32 kern_version
, int *pfd
)
740 if (!insns
|| !insns_cnt
)
743 log_buf
= malloc(BPF_LOG_BUF_SIZE
);
745 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
747 ret
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
748 insns_cnt
, license
, kern_version
,
749 log_buf
, BPF_LOG_BUF_SIZE
);
758 pr_warning("load bpf program failed: %s\n", strerror(errno
));
761 pr_warning("-- BEGIN DUMP LOG ---\n");
762 pr_warning("\n%s\n", log_buf
);
763 pr_warning("-- END LOG --\n");
772 bpf_program__load(struct bpf_program
*prog
,
773 char *license
, u32 kern_version
)
777 err
= load_program(prog
->insns
, prog
->insns_cnt
,
778 license
, kern_version
, &fd
);
783 pr_warning("failed to load program '%s'\n",
791 bpf_object__load_progs(struct bpf_object
*obj
)
796 for (i
= 0; i
< obj
->nr_programs
; i
++) {
797 err
= bpf_program__load(&obj
->programs
[i
],
806 static int bpf_object__validate(struct bpf_object
*obj
)
808 if (obj
->kern_version
== 0) {
809 pr_warning("%s doesn't provide kernel version\n",
816 static struct bpf_object
*
817 __bpf_object__open(const char *path
, void *obj_buf
, size_t obj_buf_sz
)
819 struct bpf_object
*obj
;
821 if (elf_version(EV_CURRENT
) == EV_NONE
) {
822 pr_warning("failed to init libelf for %s\n", path
);
826 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
);
830 if (bpf_object__elf_init(obj
))
832 if (bpf_object__check_endianness(obj
))
834 if (bpf_object__elf_collect(obj
))
836 if (bpf_object__collect_reloc(obj
))
838 if (bpf_object__validate(obj
))
841 bpf_object__elf_finish(obj
);
844 bpf_object__close(obj
);
848 struct bpf_object
*bpf_object__open(const char *path
)
850 /* param validation */
854 pr_debug("loading %s\n", path
);
856 return __bpf_object__open(path
, NULL
, 0);
859 struct bpf_object
*bpf_object__open_buffer(void *obj_buf
,
862 /* param validation */
863 if (!obj_buf
|| obj_buf_sz
<= 0)
866 pr_debug("loading object from buffer\n");
868 return __bpf_object__open("[buffer]", obj_buf
, obj_buf_sz
);
871 int bpf_object__unload(struct bpf_object
*obj
)
878 for (i
= 0; i
< obj
->nr_map_fds
; i
++)
879 zclose(obj
->map_fds
[i
]);
880 zfree(&obj
->map_fds
);
883 for (i
= 0; i
< obj
->nr_programs
; i
++)
884 bpf_program__unload(&obj
->programs
[i
]);
889 int bpf_object__load(struct bpf_object
*obj
)
895 pr_warning("object should not be loaded twice\n");
900 if (bpf_object__create_maps(obj
))
902 if (bpf_object__relocate(obj
))
904 if (bpf_object__load_progs(obj
))
909 bpf_object__unload(obj
);
910 pr_warning("failed to load object '%s'\n", obj
->path
);
914 void bpf_object__close(struct bpf_object
*obj
)
921 bpf_object__elf_finish(obj
);
922 bpf_object__unload(obj
);
924 zfree(&obj
->maps_buf
);
926 if (obj
->programs
&& obj
->nr_programs
) {
927 for (i
= 0; i
< obj
->nr_programs
; i
++)
928 bpf_program__exit(&obj
->programs
[i
]);
930 zfree(&obj
->programs
);