1 /*
2 * Common eBPF ELF object loading operations.
3 *
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 */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <asm/unistd.h>
18 #include <linux/kernel.h>
19 #include <linux/bpf.h>
20 #include <linux/list.h>
21 #include <libelf.h>
22 #include <gelf.h>
23
24 #include "libbpf.h"
25 #include "bpf.h"
26
27 #define __printf(a, b) __attribute__((format(printf, a, b)))
28
29 __printf(1, 2)
__base_pr(const char * format,...)30 static int __base_pr(const char *format, ...)
31 {
32 va_list args;
33 int err;
34
35 va_start(args, format);
36 err = vfprintf(stderr, format, args);
37 va_end(args);
38 return err;
39 }
40
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;
44
45 #define __pr(func, fmt, ...) \
46 do { \
47 if ((func)) \
48 (func)("libbpf: " fmt, ##__VA_ARGS__); \
49 } while (0)
50
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__)
54
libbpf_set_print(libbpf_print_fn_t warn,libbpf_print_fn_t info,libbpf_print_fn_t debug)55 void libbpf_set_print(libbpf_print_fn_t warn,
56 libbpf_print_fn_t info,
57 libbpf_print_fn_t debug)
58 {
59 __pr_warning = warn;
60 __pr_info = info;
61 __pr_debug = debug;
62 }
63
64 #define STRERR_BUFSIZE 128
65
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)
69
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",
80 };
81
libbpf_strerror(int err,char * buf,size_t size)82 int libbpf_strerror(int err, char *buf, size_t size)
83 {
84 if (!buf || !size)
85 return -1;
86
87 err = err > 0 ? err : -err;
88
89 if (err < __LIBBPF_ERRNO__START) {
90 int ret;
91
92 ret = strerror_r(err, buf, size);
93 buf[size - 1] = '\0';
94 return ret;
95 }
96
97 if (err < __LIBBPF_ERRNO__END) {
98 const char *msg;
99
100 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
101 snprintf(buf, size, "%s", msg);
102 buf[size - 1] = '\0';
103 return 0;
104 }
105
106 snprintf(buf, size, "Unknown libbpf error %d", err);
107 buf[size - 1] = '\0';
108 return -1;
109 }
110
111 #define CHECK_ERR(action, err, out) do { \
112 err = action; \
113 if (err) \
114 goto out; \
115 } while(0)
116
117
118 /* Copied from tools/perf/util/util.h */
119 #ifndef zfree
120 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
121 #endif
122
123 #ifndef zclose
124 # define zclose(fd) ({ \
125 int ___err = 0; \
126 if ((fd) >= 0) \
127 ___err = close((fd)); \
128 fd = -1; \
129 ___err; })
130 #endif
131
132 #ifdef HAVE_LIBELF_MMAP_SUPPORT
133 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
134 #else
135 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
136 #endif
137
138 /*
139 * bpf_prog should be a better name but it has been used in
140 * linux/filter.h.
141 */
142 struct bpf_program {
143 /* Index in elf obj file, for relocation use. */
144 int idx;
145 char *section_name;
146 struct bpf_insn *insns;
147 size_t insns_cnt;
148
149 struct {
150 int insn_idx;
151 int map_idx;
152 } *reloc_desc;
153 int nr_reloc;
154
155 int fd;
156
157 struct bpf_object *obj;
158 void *priv;
159 bpf_program_clear_priv_t clear_priv;
160 };
161
162 static LIST_HEAD(bpf_objects_list);
163
164 struct bpf_object {
165 char license[64];
166 u32 kern_version;
167 void *maps_buf;
168 size_t maps_buf_sz;
169
170 struct bpf_program *programs;
171 size_t nr_programs;
172 int *map_fds;
173 /*
174 * This field is required because maps_buf will be freed and
175 * maps_buf_sz will be set to 0 after loaded.
176 */
177 size_t nr_map_fds;
178 bool loaded;
179
180 /*
181 * Information when doing elf related work. Only valid if fd
182 * is valid.
183 */
184 struct {
185 int fd;
186 void *obj_buf;
187 size_t obj_buf_sz;
188 Elf *elf;
189 GElf_Ehdr ehdr;
190 Elf_Data *symbols;
191 struct {
192 GElf_Shdr shdr;
193 Elf_Data *data;
194 } *reloc;
195 int nr_reloc;
196 } efile;
197 /*
198 * All loaded bpf_object is linked in a list, which is
199 * hidden to caller. bpf_objects__<func> handlers deal with
200 * all objects.
201 */
202 struct list_head list;
203 char path[];
204 };
205 #define obj_elf_valid(o) ((o)->efile.elf)
206
bpf_program__unload(struct bpf_program * prog)207 static void bpf_program__unload(struct bpf_program *prog)
208 {
209 if (!prog)
210 return;
211
212 zclose(prog->fd);
213 }
214
bpf_program__exit(struct bpf_program * prog)215 static void bpf_program__exit(struct bpf_program *prog)
216 {
217 if (!prog)
218 return;
219
220 if (prog->clear_priv)
221 prog->clear_priv(prog, prog->priv);
222
223 prog->priv = NULL;
224 prog->clear_priv = NULL;
225
226 bpf_program__unload(prog);
227 zfree(&prog->section_name);
228 zfree(&prog->insns);
229 zfree(&prog->reloc_desc);
230
231 prog->nr_reloc = 0;
232 prog->insns_cnt = 0;
233 prog->idx = -1;
234 }
235
236 static int
bpf_program__init(void * data,size_t size,char * name,int idx,struct bpf_program * prog)237 bpf_program__init(void *data, size_t size, char *name, int idx,
238 struct bpf_program *prog)
239 {
240 if (size < sizeof(struct bpf_insn)) {
241 pr_warning("corrupted section '%s'\n", name);
242 return -EINVAL;
243 }
244
245 bzero(prog, sizeof(*prog));
246
247 prog->section_name = strdup(name);
248 if (!prog->section_name) {
249 pr_warning("failed to alloc name for prog %s\n",
250 name);
251 goto errout;
252 }
253
254 prog->insns = malloc(size);
255 if (!prog->insns) {
256 pr_warning("failed to alloc insns for %s\n", name);
257 goto errout;
258 }
259 prog->insns_cnt = size / sizeof(struct bpf_insn);
260 memcpy(prog->insns, data,
261 prog->insns_cnt * sizeof(struct bpf_insn));
262 prog->idx = idx;
263 prog->fd = -1;
264
265 return 0;
266 errout:
267 bpf_program__exit(prog);
268 return -ENOMEM;
269 }
270
271 static int
bpf_object__add_program(struct bpf_object * obj,void * data,size_t size,char * name,int idx)272 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
273 char *name, int idx)
274 {
275 struct bpf_program prog, *progs;
276 int nr_progs, err;
277
278 err = bpf_program__init(data, size, name, idx, &prog);
279 if (err)
280 return err;
281
282 progs = obj->programs;
283 nr_progs = obj->nr_programs;
284
285 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
286 if (!progs) {
287 /*
288 * In this case the original obj->programs
289 * is still valid, so don't need special treat for
290 * bpf_close_object().
291 */
292 pr_warning("failed to alloc a new program '%s'\n",
293 name);
294 bpf_program__exit(&prog);
295 return -ENOMEM;
296 }
297
298 pr_debug("found program %s\n", prog.section_name);
299 obj->programs = progs;
300 obj->nr_programs = nr_progs + 1;
301 prog.obj = obj;
302 progs[nr_progs] = prog;
303 return 0;
304 }
305
bpf_object__new(const char * path,void * obj_buf,size_t obj_buf_sz)306 static struct bpf_object *bpf_object__new(const char *path,
307 void *obj_buf,
308 size_t obj_buf_sz)
309 {
310 struct bpf_object *obj;
311
312 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
313 if (!obj) {
314 pr_warning("alloc memory failed for %s\n", path);
315 return ERR_PTR(-ENOMEM);
316 }
317
318 strcpy(obj->path, path);
319 obj->efile.fd = -1;
320
321 /*
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.
326 */
327 obj->efile.obj_buf = obj_buf;
328 obj->efile.obj_buf_sz = obj_buf_sz;
329
330 obj->loaded = false;
331
332 INIT_LIST_HEAD(&obj->list);
333 list_add(&obj->list, &bpf_objects_list);
334 return obj;
335 }
336
bpf_object__elf_finish(struct bpf_object * obj)337 static void bpf_object__elf_finish(struct bpf_object *obj)
338 {
339 if (!obj_elf_valid(obj))
340 return;
341
342 if (obj->efile.elf) {
343 elf_end(obj->efile.elf);
344 obj->efile.elf = NULL;
345 }
346 obj->efile.symbols = NULL;
347
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;
353 }
354
bpf_object__elf_init(struct bpf_object * obj)355 static int bpf_object__elf_init(struct bpf_object *obj)
356 {
357 int err = 0;
358 GElf_Ehdr *ep;
359
360 if (obj_elf_valid(obj)) {
361 pr_warning("elf init: internal error\n");
362 return -LIBBPF_ERRNO__LIBELF;
363 }
364
365 if (obj->efile.obj_buf_sz > 0) {
366 /*
367 * obj_buf should have been validated by
368 * bpf_object__open_buffer().
369 */
370 obj->efile.elf = elf_memory(obj->efile.obj_buf,
371 obj->efile.obj_buf_sz);
372 } else {
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,
376 strerror(errno));
377 return -errno;
378 }
379
380 obj->efile.elf = elf_begin(obj->efile.fd,
381 LIBBPF_ELF_C_READ_MMAP,
382 NULL);
383 }
384
385 if (!obj->efile.elf) {
386 pr_warning("failed to open %s as ELF file\n",
387 obj->path);
388 err = -LIBBPF_ERRNO__LIBELF;
389 goto errout;
390 }
391
392 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
393 pr_warning("failed to get EHDR from %s\n",
394 obj->path);
395 err = -LIBBPF_ERRNO__FORMAT;
396 goto errout;
397 }
398 ep = &obj->efile.ehdr;
399
400 if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
401 pr_warning("%s is not an eBPF object file\n",
402 obj->path);
403 err = -LIBBPF_ERRNO__FORMAT;
404 goto errout;
405 }
406
407 return 0;
408 errout:
409 bpf_object__elf_finish(obj);
410 return err;
411 }
412
413 static int
bpf_object__check_endianness(struct bpf_object * obj)414 bpf_object__check_endianness(struct bpf_object *obj)
415 {
416 static unsigned int const endian = 1;
417
418 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
419 case ELFDATA2LSB:
420 /* We are big endian, BPF obj is little endian. */
421 if (*(unsigned char const *)&endian != 1)
422 goto mismatch;
423 break;
424
425 case ELFDATA2MSB:
426 /* We are little endian, BPF obj is big endian. */
427 if (*(unsigned char const *)&endian != 0)
428 goto mismatch;
429 break;
430 default:
431 return -LIBBPF_ERRNO__ENDIAN;
432 }
433
434 return 0;
435
436 mismatch:
437 pr_warning("Error: endianness mismatch.\n");
438 return -LIBBPF_ERRNO__ENDIAN;
439 }
440
441 static int
bpf_object__init_license(struct bpf_object * obj,void * data,size_t size)442 bpf_object__init_license(struct bpf_object *obj,
443 void *data, size_t size)
444 {
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);
448 return 0;
449 }
450
451 static int
bpf_object__init_kversion(struct bpf_object * obj,void * data,size_t size)452 bpf_object__init_kversion(struct bpf_object *obj,
453 void *data, size_t size)
454 {
455 u32 kver;
456
457 if (size != sizeof(kver)) {
458 pr_warning("invalid kver section in %s\n", obj->path);
459 return -LIBBPF_ERRNO__FORMAT;
460 }
461 memcpy(&kver, data, sizeof(kver));
462 obj->kern_version = kver;
463 pr_debug("kernel version of %s is %x\n", obj->path,
464 obj->kern_version);
465 return 0;
466 }
467
468 static int
bpf_object__init_maps(struct bpf_object * obj,void * data,size_t size)469 bpf_object__init_maps(struct bpf_object *obj, void *data,
470 size_t size)
471 {
472 if (size == 0) {
473 pr_debug("%s doesn't need map definition\n",
474 obj->path);
475 return 0;
476 }
477
478 obj->maps_buf = malloc(size);
479 if (!obj->maps_buf) {
480 pr_warning("malloc maps failed: %s\n", obj->path);
481 return -ENOMEM;
482 }
483
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);
487 return 0;
488 }
489
section_have_execinstr(struct bpf_object * obj,int idx)490 static bool section_have_execinstr(struct bpf_object *obj, int idx)
491 {
492 Elf_Scn *scn;
493 GElf_Shdr sh;
494
495 scn = elf_getscn(obj->efile.elf, idx);
496 if (!scn)
497 return false;
498
499 if (gelf_getshdr(scn, &sh) != &sh)
500 return false;
501
502 if (sh.sh_flags & SHF_EXECINSTR)
503 return true;
504
505 return false;
506 }
507
bpf_object__elf_collect(struct bpf_object * obj)508 static int bpf_object__elf_collect(struct bpf_object *obj)
509 {
510 Elf *elf = obj->efile.elf;
511 GElf_Ehdr *ep = &obj->efile.ehdr;
512 Elf_Scn *scn = NULL;
513 int idx = 0, err = 0;
514
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",
518 obj->path);
519 return -LIBBPF_ERRNO__FORMAT;
520 }
521
522 while ((scn = elf_nextscn(elf, scn)) != NULL) {
523 char *name;
524 GElf_Shdr sh;
525 Elf_Data *data;
526
527 idx++;
528 if (gelf_getshdr(scn, &sh) != &sh) {
529 pr_warning("failed to get section header from %s\n",
530 obj->path);
531 err = -LIBBPF_ERRNO__FORMAT;
532 goto out;
533 }
534
535 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
536 if (!name) {
537 pr_warning("failed to get section name from %s\n",
538 obj->path);
539 err = -LIBBPF_ERRNO__FORMAT;
540 goto out;
541 }
542
543 data = elf_getdata(scn, 0);
544 if (!data) {
545 pr_warning("failed to get section data from %s(%s)\n",
546 name, obj->path);
547 err = -LIBBPF_ERRNO__FORMAT;
548 goto out;
549 }
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,
553 (int)sh.sh_type);
554
555 if (strcmp(name, "license") == 0)
556 err = bpf_object__init_license(obj,
557 data->d_buf,
558 data->d_size);
559 else if (strcmp(name, "version") == 0)
560 err = bpf_object__init_kversion(obj,
561 data->d_buf,
562 data->d_size);
563 else if (strcmp(name, "maps") == 0)
564 err = bpf_object__init_maps(obj, data->d_buf,
565 data->d_size);
566 else if (sh.sh_type == SHT_SYMTAB) {
567 if (obj->efile.symbols) {
568 pr_warning("bpf: multiple SYMTAB in %s\n",
569 obj->path);
570 err = -LIBBPF_ERRNO__FORMAT;
571 } else
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);
578 if (err) {
579 char errmsg[STRERR_BUFSIZE];
580
581 strerror_r(-err, errmsg, sizeof(errmsg));
582 pr_warning("failed to alloc program %s (%s): %s",
583 name, obj->path, errmsg);
584 }
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 */
589
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",
593 name, idx, sec);
594 continue;
595 }
596
597 reloc = realloc(reloc,
598 sizeof(*obj->efile.reloc) * nr_reloc);
599 if (!reloc) {
600 pr_warning("realloc failed\n");
601 err = -ENOMEM;
602 } else {
603 int n = nr_reloc - 1;
604
605 obj->efile.reloc = reloc;
606 obj->efile.nr_reloc = nr_reloc;
607
608 obj->efile.reloc[n].shdr = sh;
609 obj->efile.reloc[n].data = data;
610 }
611 }
612 if (err)
613 goto out;
614 }
615 out:
616 return err;
617 }
618
619 static struct bpf_program *
bpf_object__find_prog_by_idx(struct bpf_object * obj,int idx)620 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
621 {
622 struct bpf_program *prog;
623 size_t i;
624
625 for (i = 0; i < obj->nr_programs; i++) {
626 prog = &obj->programs[i];
627 if (prog->idx == idx)
628 return prog;
629 }
630 return NULL;
631 }
632
633 static int
bpf_program__collect_reloc(struct bpf_program * prog,size_t nr_maps,GElf_Shdr * shdr,Elf_Data * data,Elf_Data * symbols)634 bpf_program__collect_reloc(struct bpf_program *prog,
635 size_t nr_maps, GElf_Shdr *shdr,
636 Elf_Data *data, Elf_Data *symbols)
637 {
638 int i, nrels;
639
640 pr_debug("collecting relocating info for: '%s'\n",
641 prog->section_name);
642 nrels = shdr->sh_size / shdr->sh_entsize;
643
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");
647 return -ENOMEM;
648 }
649 prog->nr_reloc = nrels;
650
651 for (i = 0; i < nrels; i++) {
652 GElf_Sym sym;
653 GElf_Rel rel;
654 unsigned int insn_idx;
655 struct bpf_insn *insns = prog->insns;
656 size_t map_idx;
657
658 if (!gelf_getrel(data, i, &rel)) {
659 pr_warning("relocation: failed to get %d reloc\n", i);
660 return -LIBBPF_ERRNO__FORMAT;
661 }
662
663 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
664 pr_debug("relocation: insn_idx=%u\n", insn_idx);
665
666 if (!gelf_getsym(symbols,
667 GELF_R_SYM(rel.r_info),
668 &sym)) {
669 pr_warning("relocation: symbol %"PRIx64" not found\n",
670 GELF_R_SYM(rel.r_info));
671 return -LIBBPF_ERRNO__FORMAT;
672 }
673
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;
678 }
679
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;
685 }
686
687 prog->reloc_desc[i].insn_idx = insn_idx;
688 prog->reloc_desc[i].map_idx = map_idx;
689 }
690 return 0;
691 }
692
693 static int
bpf_object__create_maps(struct bpf_object * obj)694 bpf_object__create_maps(struct bpf_object *obj)
695 {
696 unsigned int i;
697 size_t nr_maps;
698 int *pfd;
699
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",
703 obj->path);
704 return 0;
705 }
706
707 obj->map_fds = malloc(sizeof(int) * nr_maps);
708 if (!obj->map_fds) {
709 pr_warning("realloc perf_bpf_map_fds failed\n");
710 return -ENOMEM;
711 }
712 obj->nr_map_fds = nr_maps;
713
714 /* fill all fd with -1 */
715 memset(obj->map_fds, -1, sizeof(int) * nr_maps);
716
717 pfd = obj->map_fds;
718 for (i = 0; i < nr_maps; i++) {
719 struct bpf_map_def def;
720
721 def = *(struct bpf_map_def *)(obj->maps_buf +
722 i * sizeof(struct bpf_map_def));
723
724 *pfd = bpf_create_map(def.type,
725 def.key_size,
726 def.value_size,
727 def.max_entries);
728 if (*pfd < 0) {
729 size_t j;
730 int err = *pfd;
731
732 pr_warning("failed to create map: %s\n",
733 strerror(errno));
734 for (j = 0; j < i; j++)
735 zclose(obj->map_fds[j]);
736 obj->nr_map_fds = 0;
737 zfree(&obj->map_fds);
738 return err;
739 }
740 pr_debug("create map: fd=%d\n", *pfd);
741 pfd++;
742 }
743
744 zfree(&obj->maps_buf);
745 obj->maps_buf_sz = 0;
746 return 0;
747 }
748
749 static int
bpf_program__relocate(struct bpf_program * prog,int * map_fds)750 bpf_program__relocate(struct bpf_program *prog, int *map_fds)
751 {
752 int i;
753
754 if (!prog || !prog->reloc_desc)
755 return 0;
756
757 for (i = 0; i < prog->nr_reloc; i++) {
758 int insn_idx, map_idx;
759 struct bpf_insn *insns = prog->insns;
760
761 insn_idx = prog->reloc_desc[i].insn_idx;
762 map_idx = prog->reloc_desc[i].map_idx;
763
764 if (insn_idx >= (int)prog->insns_cnt) {
765 pr_warning("relocation out of range: '%s'\n",
766 prog->section_name);
767 return -LIBBPF_ERRNO__RELOC;
768 }
769 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
770 insns[insn_idx].imm = map_fds[map_idx];
771 }
772
773 zfree(&prog->reloc_desc);
774 prog->nr_reloc = 0;
775 return 0;
776 }
777
778
779 static int
bpf_object__relocate(struct bpf_object * obj)780 bpf_object__relocate(struct bpf_object *obj)
781 {
782 struct bpf_program *prog;
783 size_t i;
784 int err;
785
786 for (i = 0; i < obj->nr_programs; i++) {
787 prog = &obj->programs[i];
788
789 err = bpf_program__relocate(prog, obj->map_fds);
790 if (err) {
791 pr_warning("failed to relocate '%s'\n",
792 prog->section_name);
793 return err;
794 }
795 }
796 return 0;
797 }
798
bpf_object__collect_reloc(struct bpf_object * obj)799 static int bpf_object__collect_reloc(struct bpf_object *obj)
800 {
801 int i, err;
802
803 if (!obj_elf_valid(obj)) {
804 pr_warning("Internal error: elf object is closed\n");
805 return -LIBBPF_ERRNO__INTERNAL;
806 }
807
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);
815
816 if (shdr->sh_type != SHT_REL) {
817 pr_warning("internal error at %d\n", __LINE__);
818 return -LIBBPF_ERRNO__INTERNAL;
819 }
820
821 prog = bpf_object__find_prog_by_idx(obj, idx);
822 if (!prog) {
823 pr_warning("relocation failed: no %d section\n",
824 idx);
825 return -LIBBPF_ERRNO__RELOC;
826 }
827
828 err = bpf_program__collect_reloc(prog, nr_maps,
829 shdr, data,
830 obj->efile.symbols);
831 if (err)
832 return err;
833 }
834 return 0;
835 }
836
837 static int
load_program(struct bpf_insn * insns,int insns_cnt,char * license,u32 kern_version,int * pfd)838 load_program(struct bpf_insn *insns, int insns_cnt,
839 char *license, u32 kern_version, int *pfd)
840 {
841 int ret;
842 char *log_buf;
843
844 if (!insns || !insns_cnt)
845 return -EINVAL;
846
847 log_buf = malloc(BPF_LOG_BUF_SIZE);
848 if (!log_buf)
849 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
850
851 ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
852 insns_cnt, license, kern_version,
853 log_buf, BPF_LOG_BUF_SIZE);
854
855 if (ret >= 0) {
856 *pfd = ret;
857 ret = 0;
858 goto out;
859 }
860
861 ret = -LIBBPF_ERRNO__LOAD;
862 pr_warning("load bpf program failed: %s\n", strerror(errno));
863
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");
869 } else {
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;
877 }
878 }
879
880 out:
881 free(log_buf);
882 return ret;
883 }
884
885 static int
bpf_program__load(struct bpf_program * prog,char * license,u32 kern_version)886 bpf_program__load(struct bpf_program *prog,
887 char *license, u32 kern_version)
888 {
889 int err, fd;
890
891 err = load_program(prog->insns, prog->insns_cnt,
892 license, kern_version, &fd);
893 if (!err)
894 prog->fd = fd;
895
896 if (err)
897 pr_warning("failed to load program '%s'\n",
898 prog->section_name);
899 zfree(&prog->insns);
900 prog->insns_cnt = 0;
901 return err;
902 }
903
904 static int
bpf_object__load_progs(struct bpf_object * obj)905 bpf_object__load_progs(struct bpf_object *obj)
906 {
907 size_t i;
908 int err;
909
910 for (i = 0; i < obj->nr_programs; i++) {
911 err = bpf_program__load(&obj->programs[i],
912 obj->license,
913 obj->kern_version);
914 if (err)
915 return err;
916 }
917 return 0;
918 }
919
bpf_object__validate(struct bpf_object * obj)920 static int bpf_object__validate(struct bpf_object *obj)
921 {
922 if (obj->kern_version == 0) {
923 pr_warning("%s doesn't provide kernel version\n",
924 obj->path);
925 return -LIBBPF_ERRNO__KVERSION;
926 }
927 return 0;
928 }
929
930 static struct bpf_object *
__bpf_object__open(const char * path,void * obj_buf,size_t obj_buf_sz)931 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
932 {
933 struct bpf_object *obj;
934 int err;
935
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);
939 }
940
941 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
942 if (IS_ERR(obj))
943 return obj;
944
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);
950
951 bpf_object__elf_finish(obj);
952 return obj;
953 out:
954 bpf_object__close(obj);
955 return ERR_PTR(err);
956 }
957
bpf_object__open(const char * path)958 struct bpf_object *bpf_object__open(const char *path)
959 {
960 /* param validation */
961 if (!path)
962 return NULL;
963
964 pr_debug("loading %s\n", path);
965
966 return __bpf_object__open(path, NULL, 0);
967 }
968
bpf_object__open_buffer(void * obj_buf,size_t obj_buf_sz,const char * name)969 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
970 size_t obj_buf_sz,
971 const char *name)
972 {
973 char tmp_name[64];
974
975 /* param validation */
976 if (!obj_buf || obj_buf_sz <= 0)
977 return NULL;
978
979 if (!name) {
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';
984 name = tmp_name;
985 }
986 pr_debug("loading object '%s' from buffer\n",
987 name);
988
989 return __bpf_object__open(name, obj_buf, obj_buf_sz);
990 }
991
bpf_object__unload(struct bpf_object * obj)992 int bpf_object__unload(struct bpf_object *obj)
993 {
994 size_t i;
995
996 if (!obj)
997 return -EINVAL;
998
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;
1003
1004 for (i = 0; i < obj->nr_programs; i++)
1005 bpf_program__unload(&obj->programs[i]);
1006
1007 return 0;
1008 }
1009
bpf_object__load(struct bpf_object * obj)1010 int bpf_object__load(struct bpf_object *obj)
1011 {
1012 int err;
1013
1014 if (!obj)
1015 return -EINVAL;
1016
1017 if (obj->loaded) {
1018 pr_warning("object should not be loaded twice\n");
1019 return -EINVAL;
1020 }
1021
1022 obj->loaded = true;
1023
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);
1027
1028 return 0;
1029 out:
1030 bpf_object__unload(obj);
1031 pr_warning("failed to load object '%s'\n", obj->path);
1032 return err;
1033 }
1034
bpf_object__close(struct bpf_object * obj)1035 void bpf_object__close(struct bpf_object *obj)
1036 {
1037 size_t i;
1038
1039 if (!obj)
1040 return;
1041
1042 bpf_object__elf_finish(obj);
1043 bpf_object__unload(obj);
1044
1045 zfree(&obj->maps_buf);
1046
1047 if (obj->programs && obj->nr_programs) {
1048 for (i = 0; i < obj->nr_programs; i++)
1049 bpf_program__exit(&obj->programs[i]);
1050 }
1051 zfree(&obj->programs);
1052
1053 list_del(&obj->list);
1054 free(obj);
1055 }
1056
1057 struct bpf_object *
bpf_object__next(struct bpf_object * prev)1058 bpf_object__next(struct bpf_object *prev)
1059 {
1060 struct bpf_object *next;
1061
1062 if (!prev)
1063 next = list_first_entry(&bpf_objects_list,
1064 struct bpf_object,
1065 list);
1066 else
1067 next = list_next_entry(prev, list);
1068
1069 /* Empty list is noticed here so don't need checking on entry. */
1070 if (&next->list == &bpf_objects_list)
1071 return NULL;
1072
1073 return next;
1074 }
1075
1076 const char *
bpf_object__get_name(struct bpf_object * obj)1077 bpf_object__get_name(struct bpf_object *obj)
1078 {
1079 if (!obj)
1080 return ERR_PTR(-EINVAL);
1081 return obj->path;
1082 }
1083
1084 unsigned int
bpf_object__get_kversion(struct bpf_object * obj)1085 bpf_object__get_kversion(struct bpf_object *obj)
1086 {
1087 if (!obj)
1088 return 0;
1089 return obj->kern_version;
1090 }
1091
1092 struct bpf_program *
bpf_program__next(struct bpf_program * prev,struct bpf_object * obj)1093 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1094 {
1095 size_t idx;
1096
1097 if (!obj->programs)
1098 return NULL;
1099 /* First handler */
1100 if (prev == NULL)
1101 return &obj->programs[0];
1102
1103 if (prev->obj != obj) {
1104 pr_warning("error: program handler doesn't match object\n");
1105 return NULL;
1106 }
1107
1108 idx = (prev - obj->programs) + 1;
1109 if (idx >= obj->nr_programs)
1110 return NULL;
1111 return &obj->programs[idx];
1112 }
1113
bpf_program__set_private(struct bpf_program * prog,void * priv,bpf_program_clear_priv_t clear_priv)1114 int bpf_program__set_private(struct bpf_program *prog,
1115 void *priv,
1116 bpf_program_clear_priv_t clear_priv)
1117 {
1118 if (prog->priv && prog->clear_priv)
1119 prog->clear_priv(prog, prog->priv);
1120
1121 prog->priv = priv;
1122 prog->clear_priv = clear_priv;
1123 return 0;
1124 }
1125
bpf_program__get_private(struct bpf_program * prog,void ** ppriv)1126 int bpf_program__get_private(struct bpf_program *prog, void **ppriv)
1127 {
1128 *ppriv = prog->priv;
1129 return 0;
1130 }
1131
bpf_program__title(struct bpf_program * prog,bool needs_copy)1132 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1133 {
1134 const char *title;
1135
1136 title = prog->section_name;
1137 if (needs_copy) {
1138 title = strdup(title);
1139 if (!title) {
1140 pr_warning("failed to strdup program title\n");
1141 return ERR_PTR(-ENOMEM);
1142 }
1143 }
1144
1145 return title;
1146 }
1147
bpf_program__fd(struct bpf_program * prog)1148 int bpf_program__fd(struct bpf_program *prog)
1149 {
1150 return prog->fd;
1151 }
1152