]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/lib/bpf/libbpf.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-artful-kernel.git] / tools / lib / bpf / libbpf.c
CommitLineData
1b76c13e
WN
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.
f367540c 7 * Copyright (C) 2017 Nicira, Inc.
203d1cac
WN
8 *
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!)
13 *
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.
18 *
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>
1b76c13e
WN
21 */
22
23#include <stdlib.h>
b3f59d66
WN
24#include <stdio.h>
25#include <stdarg.h>
f367540c 26#include <libgen.h>
34090915 27#include <inttypes.h>
b3f59d66 28#include <string.h>
1b76c13e 29#include <unistd.h>
1a5e3fb1
WN
30#include <fcntl.h>
31#include <errno.h>
1b76c13e 32#include <asm/unistd.h>
e28ff1a8 33#include <linux/err.h>
cb1e5e96 34#include <linux/kernel.h>
1b76c13e 35#include <linux/bpf.h>
9a208eff 36#include <linux/list.h>
f367540c
JS
37#include <linux/limits.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/vfs.h>
1a5e3fb1
WN
41#include <libelf.h>
42#include <gelf.h>
1b76c13e
WN
43
44#include "libbpf.h"
52d3352e 45#include "bpf.h"
b3f59d66 46
9b16137a
WN
47#ifndef EM_BPF
48#define EM_BPF 247
49#endif
50
f367540c
JS
51#ifndef BPF_FS_MAGIC
52#define BPF_FS_MAGIC 0xcafe4a11
53#endif
54
b3f59d66
WN
55#define __printf(a, b) __attribute__((format(printf, a, b)))
56
57__printf(1, 2)
58static int __base_pr(const char *format, ...)
59{
60 va_list args;
61 int err;
62
63 va_start(args, format);
64 err = vfprintf(stderr, format, args);
65 va_end(args);
66 return err;
67}
68
69static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
70static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
71static __printf(1, 2) libbpf_print_fn_t __pr_debug;
72
73#define __pr(func, fmt, ...) \
74do { \
75 if ((func)) \
76 (func)("libbpf: " fmt, ##__VA_ARGS__); \
77} while (0)
78
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__)
82
83void libbpf_set_print(libbpf_print_fn_t warn,
84 libbpf_print_fn_t info,
85 libbpf_print_fn_t debug)
86{
87 __pr_warning = warn;
88 __pr_info = info;
89 __pr_debug = debug;
90}
1a5e3fb1 91
6371ca3b
WN
92#define STRERR_BUFSIZE 128
93
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)
97
98static 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",
de8a63bd 102 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
6371ca3b
WN
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",
705fa219 108 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
6371ca3b
WN
109};
110
111int libbpf_strerror(int err, char *buf, size_t size)
112{
113 if (!buf || !size)
114 return -1;
115
116 err = err > 0 ? err : -err;
117
118 if (err < __LIBBPF_ERRNO__START) {
119 int ret;
120
121 ret = strerror_r(err, buf, size);
122 buf[size - 1] = '\0';
123 return ret;
124 }
125
126 if (err < __LIBBPF_ERRNO__END) {
127 const char *msg;
128
129 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
130 snprintf(buf, size, "%s", msg);
131 buf[size - 1] = '\0';
132 return 0;
133 }
134
135 snprintf(buf, size, "Unknown libbpf error %d", err);
136 buf[size - 1] = '\0';
137 return -1;
138}
139
140#define CHECK_ERR(action, err, out) do { \
141 err = action; \
142 if (err) \
143 goto out; \
144} while(0)
145
146
1a5e3fb1
WN
147/* Copied from tools/perf/util/util.h */
148#ifndef zfree
149# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
150#endif
151
152#ifndef zclose
153# define zclose(fd) ({ \
154 int ___err = 0; \
155 if ((fd) >= 0) \
156 ___err = close((fd)); \
157 fd = -1; \
158 ___err; })
159#endif
160
161#ifdef HAVE_LIBELF_MMAP_SUPPORT
162# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
163#else
164# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
165#endif
166
a5b8bd47
WN
167/*
168 * bpf_prog should be a better name but it has been used in
169 * linux/filter.h.
170 */
171struct bpf_program {
172 /* Index in elf obj file, for relocation use. */
173 int idx;
174 char *section_name;
175 struct bpf_insn *insns;
176 size_t insns_cnt;
5f44e4c8 177 enum bpf_prog_type type;
34090915
WN
178
179 struct {
180 int insn_idx;
181 int map_idx;
182 } *reloc_desc;
183 int nr_reloc;
55cffde2 184
b580563e
WN
185 struct {
186 int nr;
187 int *fds;
188 } instances;
189 bpf_program_prep_t preprocessor;
aa9b1ac3
WN
190
191 struct bpf_object *obj;
192 void *priv;
193 bpf_program_clear_priv_t clear_priv;
a5b8bd47
WN
194};
195
9d759a9b
WN
196struct bpf_map {
197 int fd;
561bbcca 198 char *name;
4708bbda 199 size_t offset;
9d759a9b
WN
200 struct bpf_map_def def;
201 void *priv;
202 bpf_map_clear_priv_t clear_priv;
203};
204
9a208eff
WN
205static LIST_HEAD(bpf_objects_list);
206
1a5e3fb1 207struct bpf_object {
cb1e5e96
WN
208 char license[64];
209 u32 kern_version;
0b3d1efa 210
a5b8bd47
WN
211 struct bpf_program *programs;
212 size_t nr_programs;
9d759a9b
WN
213 struct bpf_map *maps;
214 size_t nr_maps;
215
52d3352e 216 bool loaded;
a5b8bd47 217
1a5e3fb1
WN
218 /*
219 * Information when doing elf related work. Only valid if fd
220 * is valid.
221 */
222 struct {
223 int fd;
6c956392
WN
224 void *obj_buf;
225 size_t obj_buf_sz;
1a5e3fb1
WN
226 Elf *elf;
227 GElf_Ehdr ehdr;
bec7d68c 228 Elf_Data *symbols;
77ba9a5b 229 size_t strtabidx;
b62f06e8
WN
230 struct {
231 GElf_Shdr shdr;
232 Elf_Data *data;
233 } *reloc;
234 int nr_reloc;
666810e8 235 int maps_shndx;
1a5e3fb1 236 } efile;
9a208eff
WN
237 /*
238 * All loaded bpf_object is linked in a list, which is
239 * hidden to caller. bpf_objects__<func> handlers deal with
240 * all objects.
241 */
242 struct list_head list;
10931d24
WN
243
244 void *priv;
245 bpf_object_clear_priv_t clear_priv;
246
1a5e3fb1
WN
247 char path[];
248};
249#define obj_elf_valid(o) ((o)->efile.elf)
250
55cffde2
WN
251static void bpf_program__unload(struct bpf_program *prog)
252{
b580563e
WN
253 int i;
254
55cffde2
WN
255 if (!prog)
256 return;
257
b580563e
WN
258 /*
259 * If the object is opened but the program was never loaded,
260 * it is possible that prog->instances.nr == -1.
261 */
262 if (prog->instances.nr > 0) {
263 for (i = 0; i < prog->instances.nr; i++)
264 zclose(prog->instances.fds[i]);
265 } else if (prog->instances.nr != -1) {
266 pr_warning("Internal error: instances.nr is %d\n",
267 prog->instances.nr);
268 }
269
270 prog->instances.nr = -1;
271 zfree(&prog->instances.fds);
55cffde2
WN
272}
273
a5b8bd47
WN
274static void bpf_program__exit(struct bpf_program *prog)
275{
276 if (!prog)
277 return;
278
aa9b1ac3
WN
279 if (prog->clear_priv)
280 prog->clear_priv(prog, prog->priv);
281
282 prog->priv = NULL;
283 prog->clear_priv = NULL;
284
55cffde2 285 bpf_program__unload(prog);
a5b8bd47
WN
286 zfree(&prog->section_name);
287 zfree(&prog->insns);
34090915
WN
288 zfree(&prog->reloc_desc);
289
290 prog->nr_reloc = 0;
a5b8bd47
WN
291 prog->insns_cnt = 0;
292 prog->idx = -1;
293}
294
295static int
296bpf_program__init(void *data, size_t size, char *name, int idx,
297 struct bpf_program *prog)
298{
299 if (size < sizeof(struct bpf_insn)) {
300 pr_warning("corrupted section '%s'\n", name);
301 return -EINVAL;
302 }
303
304 bzero(prog, sizeof(*prog));
305
306 prog->section_name = strdup(name);
307 if (!prog->section_name) {
308 pr_warning("failed to alloc name for prog %s\n",
309 name);
310 goto errout;
311 }
312
313 prog->insns = malloc(size);
314 if (!prog->insns) {
315 pr_warning("failed to alloc insns for %s\n", name);
316 goto errout;
317 }
318 prog->insns_cnt = size / sizeof(struct bpf_insn);
319 memcpy(prog->insns, data,
320 prog->insns_cnt * sizeof(struct bpf_insn));
321 prog->idx = idx;
b580563e
WN
322 prog->instances.fds = NULL;
323 prog->instances.nr = -1;
5f44e4c8 324 prog->type = BPF_PROG_TYPE_KPROBE;
a5b8bd47
WN
325
326 return 0;
327errout:
328 bpf_program__exit(prog);
329 return -ENOMEM;
330}
331
332static int
333bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
334 char *name, int idx)
335{
336 struct bpf_program prog, *progs;
337 int nr_progs, err;
338
339 err = bpf_program__init(data, size, name, idx, &prog);
340 if (err)
341 return err;
342
343 progs = obj->programs;
344 nr_progs = obj->nr_programs;
345
346 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
347 if (!progs) {
348 /*
349 * In this case the original obj->programs
350 * is still valid, so don't need special treat for
351 * bpf_close_object().
352 */
353 pr_warning("failed to alloc a new program '%s'\n",
354 name);
355 bpf_program__exit(&prog);
356 return -ENOMEM;
357 }
358
359 pr_debug("found program %s\n", prog.section_name);
360 obj->programs = progs;
361 obj->nr_programs = nr_progs + 1;
aa9b1ac3 362 prog.obj = obj;
a5b8bd47
WN
363 progs[nr_progs] = prog;
364 return 0;
365}
366
6c956392
WN
367static struct bpf_object *bpf_object__new(const char *path,
368 void *obj_buf,
369 size_t obj_buf_sz)
1a5e3fb1
WN
370{
371 struct bpf_object *obj;
372
373 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
374 if (!obj) {
375 pr_warning("alloc memory failed for %s\n", path);
6371ca3b 376 return ERR_PTR(-ENOMEM);
1a5e3fb1
WN
377 }
378
379 strcpy(obj->path, path);
380 obj->efile.fd = -1;
6c956392
WN
381
382 /*
383 * Caller of this function should also calls
384 * bpf_object__elf_finish() after data collection to return
385 * obj_buf to user. If not, we should duplicate the buffer to
386 * avoid user freeing them before elf finish.
387 */
388 obj->efile.obj_buf = obj_buf;
389 obj->efile.obj_buf_sz = obj_buf_sz;
666810e8 390 obj->efile.maps_shndx = -1;
6c956392 391
52d3352e 392 obj->loaded = false;
9a208eff
WN
393
394 INIT_LIST_HEAD(&obj->list);
395 list_add(&obj->list, &bpf_objects_list);
1a5e3fb1
WN
396 return obj;
397}
398
399static void bpf_object__elf_finish(struct bpf_object *obj)
400{
401 if (!obj_elf_valid(obj))
402 return;
403
404 if (obj->efile.elf) {
405 elf_end(obj->efile.elf);
406 obj->efile.elf = NULL;
407 }
bec7d68c 408 obj->efile.symbols = NULL;
b62f06e8
WN
409
410 zfree(&obj->efile.reloc);
411 obj->efile.nr_reloc = 0;
1a5e3fb1 412 zclose(obj->efile.fd);
6c956392
WN
413 obj->efile.obj_buf = NULL;
414 obj->efile.obj_buf_sz = 0;
1a5e3fb1
WN
415}
416
417static int bpf_object__elf_init(struct bpf_object *obj)
418{
419 int err = 0;
420 GElf_Ehdr *ep;
421
422 if (obj_elf_valid(obj)) {
423 pr_warning("elf init: internal error\n");
6371ca3b 424 return -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
425 }
426
6c956392
WN
427 if (obj->efile.obj_buf_sz > 0) {
428 /*
429 * obj_buf should have been validated by
430 * bpf_object__open_buffer().
431 */
432 obj->efile.elf = elf_memory(obj->efile.obj_buf,
433 obj->efile.obj_buf_sz);
434 } else {
435 obj->efile.fd = open(obj->path, O_RDONLY);
436 if (obj->efile.fd < 0) {
437 pr_warning("failed to open %s: %s\n", obj->path,
438 strerror(errno));
439 return -errno;
440 }
441
442 obj->efile.elf = elf_begin(obj->efile.fd,
443 LIBBPF_ELF_C_READ_MMAP,
444 NULL);
1a5e3fb1
WN
445 }
446
1a5e3fb1
WN
447 if (!obj->efile.elf) {
448 pr_warning("failed to open %s as ELF file\n",
449 obj->path);
6371ca3b 450 err = -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
451 goto errout;
452 }
453
454 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
455 pr_warning("failed to get EHDR from %s\n",
456 obj->path);
6371ca3b 457 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
458 goto errout;
459 }
460 ep = &obj->efile.ehdr;
461
9b16137a
WN
462 /* Old LLVM set e_machine to EM_NONE */
463 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
1a5e3fb1
WN
464 pr_warning("%s is not an eBPF object file\n",
465 obj->path);
6371ca3b 466 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
467 goto errout;
468 }
469
470 return 0;
471errout:
472 bpf_object__elf_finish(obj);
473 return err;
474}
475
cc4228d5
WN
476static int
477bpf_object__check_endianness(struct bpf_object *obj)
478{
479 static unsigned int const endian = 1;
480
481 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
482 case ELFDATA2LSB:
483 /* We are big endian, BPF obj is little endian. */
484 if (*(unsigned char const *)&endian != 1)
485 goto mismatch;
486 break;
487
488 case ELFDATA2MSB:
489 /* We are little endian, BPF obj is big endian. */
490 if (*(unsigned char const *)&endian != 0)
491 goto mismatch;
492 break;
493 default:
6371ca3b 494 return -LIBBPF_ERRNO__ENDIAN;
cc4228d5
WN
495 }
496
497 return 0;
498
499mismatch:
500 pr_warning("Error: endianness mismatch.\n");
6371ca3b 501 return -LIBBPF_ERRNO__ENDIAN;
cc4228d5
WN
502}
503
cb1e5e96
WN
504static int
505bpf_object__init_license(struct bpf_object *obj,
506 void *data, size_t size)
507{
508 memcpy(obj->license, data,
509 min(size, sizeof(obj->license) - 1));
510 pr_debug("license of %s is %s\n", obj->path, obj->license);
511 return 0;
512}
513
514static int
515bpf_object__init_kversion(struct bpf_object *obj,
516 void *data, size_t size)
517{
518 u32 kver;
519
520 if (size != sizeof(kver)) {
521 pr_warning("invalid kver section in %s\n", obj->path);
6371ca3b 522 return -LIBBPF_ERRNO__FORMAT;
cb1e5e96
WN
523 }
524 memcpy(&kver, data, sizeof(kver));
525 obj->kern_version = kver;
526 pr_debug("kernel version of %s is %x\n", obj->path,
527 obj->kern_version);
528 return 0;
529}
530
0b3d1efa 531static int
4708bbda 532bpf_object__validate_maps(struct bpf_object *obj)
0b3d1efa 533{
9d759a9b
WN
534 int i;
535
4708bbda
EL
536 /*
537 * If there's only 1 map, the only error case should have been
538 * catched in bpf_object__init_maps().
539 */
540 if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
0b3d1efa 541 return 0;
0b3d1efa 542
4708bbda
EL
543 for (i = 1; i < obj->nr_maps; i++) {
544 const struct bpf_map *a = &obj->maps[i - 1];
545 const struct bpf_map *b = &obj->maps[i];
9d759a9b 546
4708bbda
EL
547 if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
548 pr_warning("corrupted map section in %s: map \"%s\" too small\n",
549 obj->path, a->name);
550 return -EINVAL;
551 }
0b3d1efa 552 }
4708bbda
EL
553 return 0;
554}
0b3d1efa 555
4708bbda
EL
556static int compare_bpf_map(const void *_a, const void *_b)
557{
558 const struct bpf_map *a = _a;
559 const struct bpf_map *b = _b;
9d759a9b 560
4708bbda 561 return a->offset - b->offset;
0b3d1efa
WN
562}
563
973170e6 564static int
4708bbda 565bpf_object__init_maps(struct bpf_object *obj)
561bbcca 566{
4708bbda
EL
567 int i, map_idx, nr_maps = 0;
568 Elf_Scn *scn;
569 Elf_Data *data;
561bbcca
WN
570 Elf_Data *symbols = obj->efile.symbols;
571
4708bbda
EL
572 if (obj->efile.maps_shndx < 0)
573 return -EINVAL;
574 if (!symbols)
575 return -EINVAL;
576
577 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
578 if (scn)
579 data = elf_getdata(scn, NULL);
580 if (!scn || !data) {
581 pr_warning("failed to get Elf_Data from map section %d\n",
582 obj->efile.maps_shndx);
973170e6 583 return -EINVAL;
4708bbda 584 }
561bbcca 585
4708bbda
EL
586 /*
587 * Count number of maps. Each map has a name.
588 * Array of maps is not supported: only the first element is
589 * considered.
590 *
591 * TODO: Detect array of map and report error.
592 */
561bbcca
WN
593 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
594 GElf_Sym sym;
4708bbda
EL
595
596 if (!gelf_getsym(symbols, i, &sym))
597 continue;
598 if (sym.st_shndx != obj->efile.maps_shndx)
599 continue;
600 nr_maps++;
601 }
602
603 /* Alloc obj->maps and fill nr_maps. */
604 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
605 nr_maps, data->d_size);
606
607 if (!nr_maps)
608 return 0;
609
610 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
611 if (!obj->maps) {
612 pr_warning("alloc maps for object failed\n");
613 return -ENOMEM;
614 }
615 obj->nr_maps = nr_maps;
616
617 /*
618 * fill all fd with -1 so won't close incorrect
619 * fd (fd=0 is stdin) when failure (zclose won't close
620 * negative fd)).
621 */
622 for (i = 0; i < nr_maps; i++)
623 obj->maps[i].fd = -1;
624
625 /*
626 * Fill obj->maps using data in "maps" section.
627 */
628 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
629 GElf_Sym sym;
561bbcca 630 const char *map_name;
4708bbda 631 struct bpf_map_def *def;
561bbcca
WN
632
633 if (!gelf_getsym(symbols, i, &sym))
634 continue;
666810e8 635 if (sym.st_shndx != obj->efile.maps_shndx)
561bbcca
WN
636 continue;
637
638 map_name = elf_strptr(obj->efile.elf,
77ba9a5b 639 obj->efile.strtabidx,
561bbcca 640 sym.st_name);
4708bbda
EL
641 obj->maps[map_idx].offset = sym.st_value;
642 if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
643 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
644 obj->path, map_name);
645 return -EINVAL;
561bbcca 646 }
4708bbda 647
561bbcca 648 obj->maps[map_idx].name = strdup(map_name);
973170e6
WN
649 if (!obj->maps[map_idx].name) {
650 pr_warning("failed to alloc map name\n");
651 return -ENOMEM;
652 }
4708bbda 653 pr_debug("map %d is \"%s\"\n", map_idx,
561bbcca 654 obj->maps[map_idx].name);
4708bbda
EL
655 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
656 obj->maps[map_idx].def = *def;
657 map_idx++;
561bbcca 658 }
4708bbda
EL
659
660 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
661 return bpf_object__validate_maps(obj);
561bbcca
WN
662}
663
29603665
WN
664static int bpf_object__elf_collect(struct bpf_object *obj)
665{
666 Elf *elf = obj->efile.elf;
667 GElf_Ehdr *ep = &obj->efile.ehdr;
668 Elf_Scn *scn = NULL;
666810e8 669 int idx = 0, err = 0;
29603665
WN
670
671 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
672 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
673 pr_warning("failed to get e_shstrndx from %s\n",
674 obj->path);
6371ca3b 675 return -LIBBPF_ERRNO__FORMAT;
29603665
WN
676 }
677
678 while ((scn = elf_nextscn(elf, scn)) != NULL) {
679 char *name;
680 GElf_Shdr sh;
681 Elf_Data *data;
682
683 idx++;
684 if (gelf_getshdr(scn, &sh) != &sh) {
685 pr_warning("failed to get section header from %s\n",
686 obj->path);
6371ca3b 687 err = -LIBBPF_ERRNO__FORMAT;
29603665
WN
688 goto out;
689 }
690
691 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
692 if (!name) {
693 pr_warning("failed to get section name from %s\n",
694 obj->path);
6371ca3b 695 err = -LIBBPF_ERRNO__FORMAT;
29603665
WN
696 goto out;
697 }
698
699 data = elf_getdata(scn, 0);
700 if (!data) {
701 pr_warning("failed to get section data from %s(%s)\n",
702 name, obj->path);
6371ca3b 703 err = -LIBBPF_ERRNO__FORMAT;
29603665
WN
704 goto out;
705 }
706 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
707 name, (unsigned long)data->d_size,
708 (int)sh.sh_link, (unsigned long)sh.sh_flags,
709 (int)sh.sh_type);
cb1e5e96
WN
710
711 if (strcmp(name, "license") == 0)
712 err = bpf_object__init_license(obj,
713 data->d_buf,
714 data->d_size);
715 else if (strcmp(name, "version") == 0)
716 err = bpf_object__init_kversion(obj,
717 data->d_buf,
718 data->d_size);
4708bbda 719 else if (strcmp(name, "maps") == 0)
666810e8 720 obj->efile.maps_shndx = idx;
4708bbda 721 else if (sh.sh_type == SHT_SYMTAB) {
bec7d68c
WN
722 if (obj->efile.symbols) {
723 pr_warning("bpf: multiple SYMTAB in %s\n",
724 obj->path);
6371ca3b 725 err = -LIBBPF_ERRNO__FORMAT;
77ba9a5b 726 } else {
bec7d68c 727 obj->efile.symbols = data;
77ba9a5b
WN
728 obj->efile.strtabidx = sh.sh_link;
729 }
a5b8bd47
WN
730 } else if ((sh.sh_type == SHT_PROGBITS) &&
731 (sh.sh_flags & SHF_EXECINSTR) &&
732 (data->d_size > 0)) {
733 err = bpf_object__add_program(obj, data->d_buf,
734 data->d_size, name, idx);
735 if (err) {
6371ca3b
WN
736 char errmsg[STRERR_BUFSIZE];
737
a5b8bd47
WN
738 strerror_r(-err, errmsg, sizeof(errmsg));
739 pr_warning("failed to alloc program %s (%s): %s",
740 name, obj->path, errmsg);
741 }
b62f06e8
WN
742 } else if (sh.sh_type == SHT_REL) {
743 void *reloc = obj->efile.reloc;
744 int nr_reloc = obj->efile.nr_reloc + 1;
745
746 reloc = realloc(reloc,
747 sizeof(*obj->efile.reloc) * nr_reloc);
748 if (!reloc) {
749 pr_warning("realloc failed\n");
750 err = -ENOMEM;
751 } else {
752 int n = nr_reloc - 1;
753
754 obj->efile.reloc = reloc;
755 obj->efile.nr_reloc = nr_reloc;
756
757 obj->efile.reloc[n].shdr = sh;
758 obj->efile.reloc[n].data = data;
759 }
bec7d68c 760 }
cb1e5e96
WN
761 if (err)
762 goto out;
29603665 763 }
561bbcca 764
77ba9a5b
WN
765 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
766 pr_warning("Corrupted ELF file: index of strtab invalid\n");
767 return LIBBPF_ERRNO__FORMAT;
768 }
666810e8 769 if (obj->efile.maps_shndx >= 0)
4708bbda 770 err = bpf_object__init_maps(obj);
29603665
WN
771out:
772 return err;
773}
774
34090915
WN
775static struct bpf_program *
776bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
777{
778 struct bpf_program *prog;
779 size_t i;
780
781 for (i = 0; i < obj->nr_programs; i++) {
782 prog = &obj->programs[i];
783 if (prog->idx == idx)
784 return prog;
785 }
786 return NULL;
787}
788
789static int
790bpf_program__collect_reloc(struct bpf_program *prog,
791 size_t nr_maps, GElf_Shdr *shdr,
666810e8 792 Elf_Data *data, Elf_Data *symbols,
94e5adec 793 int maps_shndx, struct bpf_map *maps)
34090915
WN
794{
795 int i, nrels;
796
797 pr_debug("collecting relocating info for: '%s'\n",
798 prog->section_name);
799 nrels = shdr->sh_size / shdr->sh_entsize;
800
801 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
802 if (!prog->reloc_desc) {
803 pr_warning("failed to alloc memory in relocation\n");
804 return -ENOMEM;
805 }
806 prog->nr_reloc = nrels;
807
808 for (i = 0; i < nrels; i++) {
809 GElf_Sym sym;
810 GElf_Rel rel;
811 unsigned int insn_idx;
812 struct bpf_insn *insns = prog->insns;
813 size_t map_idx;
814
815 if (!gelf_getrel(data, i, &rel)) {
816 pr_warning("relocation: failed to get %d reloc\n", i);
6371ca3b 817 return -LIBBPF_ERRNO__FORMAT;
34090915
WN
818 }
819
34090915
WN
820 if (!gelf_getsym(symbols,
821 GELF_R_SYM(rel.r_info),
822 &sym)) {
823 pr_warning("relocation: symbol %"PRIx64" not found\n",
824 GELF_R_SYM(rel.r_info));
6371ca3b 825 return -LIBBPF_ERRNO__FORMAT;
34090915
WN
826 }
827
666810e8
WN
828 if (sym.st_shndx != maps_shndx) {
829 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
830 prog->section_name, sym.st_shndx);
831 return -LIBBPF_ERRNO__RELOC;
832 }
833
834 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
835 pr_debug("relocation: insn_idx=%u\n", insn_idx);
836
34090915
WN
837 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
838 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
839 insn_idx, insns[insn_idx].code);
6371ca3b 840 return -LIBBPF_ERRNO__RELOC;
34090915
WN
841 }
842
94e5adec
JS
843 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
844 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
845 if (maps[map_idx].offset == sym.st_value) {
846 pr_debug("relocation: find map %zd (%s) for insn %u\n",
847 map_idx, maps[map_idx].name, insn_idx);
848 break;
849 }
850 }
851
34090915
WN
852 if (map_idx >= nr_maps) {
853 pr_warning("bpf relocation: map_idx %d large than %d\n",
854 (int)map_idx, (int)nr_maps - 1);
6371ca3b 855 return -LIBBPF_ERRNO__RELOC;
34090915
WN
856 }
857
858 prog->reloc_desc[i].insn_idx = insn_idx;
859 prog->reloc_desc[i].map_idx = map_idx;
860 }
861 return 0;
862}
863
52d3352e
WN
864static int
865bpf_object__create_maps(struct bpf_object *obj)
866{
867 unsigned int i;
52d3352e 868
9d759a9b
WN
869 for (i = 0; i < obj->nr_maps; i++) {
870 struct bpf_map_def *def = &obj->maps[i].def;
871 int *pfd = &obj->maps[i].fd;
52d3352e 872
9d759a9b
WN
873 *pfd = bpf_create_map(def->type,
874 def->key_size,
875 def->value_size,
a5580c7f
JS
876 def->max_entries,
877 0);
52d3352e
WN
878 if (*pfd < 0) {
879 size_t j;
880 int err = *pfd;
881
49bf4b36
EL
882 pr_warning("failed to create map (name: '%s'): %s\n",
883 obj->maps[i].name,
52d3352e
WN
884 strerror(errno));
885 for (j = 0; j < i; j++)
9d759a9b 886 zclose(obj->maps[j].fd);
52d3352e
WN
887 return err;
888 }
4708bbda 889 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
52d3352e
WN
890 }
891
52d3352e
WN
892 return 0;
893}
894
8a47a6c5 895static int
9d759a9b 896bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
8a47a6c5
WN
897{
898 int i;
899
900 if (!prog || !prog->reloc_desc)
901 return 0;
902
903 for (i = 0; i < prog->nr_reloc; i++) {
904 int insn_idx, map_idx;
905 struct bpf_insn *insns = prog->insns;
906
907 insn_idx = prog->reloc_desc[i].insn_idx;
908 map_idx = prog->reloc_desc[i].map_idx;
909
910 if (insn_idx >= (int)prog->insns_cnt) {
911 pr_warning("relocation out of range: '%s'\n",
912 prog->section_name);
6371ca3b 913 return -LIBBPF_ERRNO__RELOC;
8a47a6c5
WN
914 }
915 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
9d759a9b 916 insns[insn_idx].imm = obj->maps[map_idx].fd;
8a47a6c5
WN
917 }
918
919 zfree(&prog->reloc_desc);
920 prog->nr_reloc = 0;
921 return 0;
922}
923
924
925static int
926bpf_object__relocate(struct bpf_object *obj)
927{
928 struct bpf_program *prog;
929 size_t i;
930 int err;
931
932 for (i = 0; i < obj->nr_programs; i++) {
933 prog = &obj->programs[i];
934
9d759a9b 935 err = bpf_program__relocate(prog, obj);
8a47a6c5
WN
936 if (err) {
937 pr_warning("failed to relocate '%s'\n",
938 prog->section_name);
939 return err;
940 }
941 }
942 return 0;
943}
944
34090915
WN
945static int bpf_object__collect_reloc(struct bpf_object *obj)
946{
947 int i, err;
948
949 if (!obj_elf_valid(obj)) {
950 pr_warning("Internal error: elf object is closed\n");
6371ca3b 951 return -LIBBPF_ERRNO__INTERNAL;
34090915
WN
952 }
953
954 for (i = 0; i < obj->efile.nr_reloc; i++) {
955 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
956 Elf_Data *data = obj->efile.reloc[i].data;
957 int idx = shdr->sh_info;
958 struct bpf_program *prog;
9d759a9b 959 size_t nr_maps = obj->nr_maps;
34090915
WN
960
961 if (shdr->sh_type != SHT_REL) {
962 pr_warning("internal error at %d\n", __LINE__);
6371ca3b 963 return -LIBBPF_ERRNO__INTERNAL;
34090915
WN
964 }
965
966 prog = bpf_object__find_prog_by_idx(obj, idx);
967 if (!prog) {
968 pr_warning("relocation failed: no %d section\n",
969 idx);
6371ca3b 970 return -LIBBPF_ERRNO__RELOC;
34090915
WN
971 }
972
973 err = bpf_program__collect_reloc(prog, nr_maps,
974 shdr, data,
666810e8 975 obj->efile.symbols,
94e5adec
JS
976 obj->efile.maps_shndx,
977 obj->maps);
34090915 978 if (err)
6371ca3b 979 return err;
34090915
WN
980 }
981 return 0;
982}
983
55cffde2 984static int
5f44e4c8
WN
985load_program(enum bpf_prog_type type, struct bpf_insn *insns,
986 int insns_cnt, char *license, u32 kern_version, int *pfd)
55cffde2
WN
987{
988 int ret;
989 char *log_buf;
990
991 if (!insns || !insns_cnt)
992 return -EINVAL;
993
994 log_buf = malloc(BPF_LOG_BUF_SIZE);
995 if (!log_buf)
996 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
997
5f44e4c8
WN
998 ret = bpf_load_program(type, insns, insns_cnt, license,
999 kern_version, log_buf, BPF_LOG_BUF_SIZE);
55cffde2
WN
1000
1001 if (ret >= 0) {
1002 *pfd = ret;
1003 ret = 0;
1004 goto out;
1005 }
1006
6371ca3b 1007 ret = -LIBBPF_ERRNO__LOAD;
55cffde2
WN
1008 pr_warning("load bpf program failed: %s\n", strerror(errno));
1009
6371ca3b
WN
1010 if (log_buf && log_buf[0] != '\0') {
1011 ret = -LIBBPF_ERRNO__VERIFY;
55cffde2
WN
1012 pr_warning("-- BEGIN DUMP LOG ---\n");
1013 pr_warning("\n%s\n", log_buf);
1014 pr_warning("-- END LOG --\n");
705fa219
WN
1015 } else if (insns_cnt >= BPF_MAXINSNS) {
1016 pr_warning("Program too large (%d insns), at most %d insns\n",
1017 insns_cnt, BPF_MAXINSNS);
1018 ret = -LIBBPF_ERRNO__PROG2BIG;
6371ca3b 1019 } else {
705fa219
WN
1020 /* Wrong program type? */
1021 if (type != BPF_PROG_TYPE_KPROBE) {
1022 int fd;
1023
1024 fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
1025 insns_cnt, license, kern_version,
1026 NULL, 0);
1027 if (fd >= 0) {
1028 close(fd);
1029 ret = -LIBBPF_ERRNO__PROGTYPE;
1030 goto out;
1031 }
6371ca3b 1032 }
705fa219
WN
1033
1034 if (log_buf)
1035 ret = -LIBBPF_ERRNO__KVER;
55cffde2
WN
1036 }
1037
1038out:
1039 free(log_buf);
1040 return ret;
1041}
1042
1043static int
1044bpf_program__load(struct bpf_program *prog,
1045 char *license, u32 kern_version)
1046{
b580563e 1047 int err = 0, fd, i;
55cffde2 1048
b580563e
WN
1049 if (prog->instances.nr < 0 || !prog->instances.fds) {
1050 if (prog->preprocessor) {
1051 pr_warning("Internal error: can't load program '%s'\n",
1052 prog->section_name);
1053 return -LIBBPF_ERRNO__INTERNAL;
1054 }
55cffde2 1055
b580563e
WN
1056 prog->instances.fds = malloc(sizeof(int));
1057 if (!prog->instances.fds) {
1058 pr_warning("Not enough memory for BPF fds\n");
1059 return -ENOMEM;
1060 }
1061 prog->instances.nr = 1;
1062 prog->instances.fds[0] = -1;
1063 }
1064
1065 if (!prog->preprocessor) {
1066 if (prog->instances.nr != 1) {
1067 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1068 prog->section_name, prog->instances.nr);
1069 }
5f44e4c8 1070 err = load_program(prog->type, prog->insns, prog->insns_cnt,
b580563e
WN
1071 license, kern_version, &fd);
1072 if (!err)
1073 prog->instances.fds[0] = fd;
1074 goto out;
1075 }
1076
1077 for (i = 0; i < prog->instances.nr; i++) {
1078 struct bpf_prog_prep_result result;
1079 bpf_program_prep_t preprocessor = prog->preprocessor;
1080
1081 bzero(&result, sizeof(result));
1082 err = preprocessor(prog, i, prog->insns,
1083 prog->insns_cnt, &result);
1084 if (err) {
1085 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1086 i, prog->section_name);
1087 goto out;
1088 }
1089
1090 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1091 pr_debug("Skip loading the %dth instance of program '%s'\n",
1092 i, prog->section_name);
1093 prog->instances.fds[i] = -1;
1094 if (result.pfd)
1095 *result.pfd = -1;
1096 continue;
1097 }
1098
5f44e4c8 1099 err = load_program(prog->type, result.new_insn_ptr,
b580563e
WN
1100 result.new_insn_cnt,
1101 license, kern_version, &fd);
1102
1103 if (err) {
1104 pr_warning("Loading the %dth instance of program '%s' failed\n",
1105 i, prog->section_name);
1106 goto out;
1107 }
1108
1109 if (result.pfd)
1110 *result.pfd = fd;
1111 prog->instances.fds[i] = fd;
1112 }
1113out:
55cffde2
WN
1114 if (err)
1115 pr_warning("failed to load program '%s'\n",
1116 prog->section_name);
1117 zfree(&prog->insns);
1118 prog->insns_cnt = 0;
1119 return err;
1120}
1121
1122static int
1123bpf_object__load_progs(struct bpf_object *obj)
1124{
1125 size_t i;
1126 int err;
1127
1128 for (i = 0; i < obj->nr_programs; i++) {
1129 err = bpf_program__load(&obj->programs[i],
1130 obj->license,
1131 obj->kern_version);
1132 if (err)
1133 return err;
1134 }
1135 return 0;
1136}
1137
cb1e5e96
WN
1138static int bpf_object__validate(struct bpf_object *obj)
1139{
1140 if (obj->kern_version == 0) {
1141 pr_warning("%s doesn't provide kernel version\n",
1142 obj->path);
6371ca3b 1143 return -LIBBPF_ERRNO__KVERSION;
cb1e5e96
WN
1144 }
1145 return 0;
1146}
1147
1a5e3fb1 1148static struct bpf_object *
6c956392 1149__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
1a5e3fb1
WN
1150{
1151 struct bpf_object *obj;
6371ca3b 1152 int err;
1a5e3fb1
WN
1153
1154 if (elf_version(EV_CURRENT) == EV_NONE) {
1155 pr_warning("failed to init libelf for %s\n", path);
6371ca3b 1156 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1a5e3fb1
WN
1157 }
1158
6c956392 1159 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
6371ca3b
WN
1160 if (IS_ERR(obj))
1161 return obj;
1a5e3fb1 1162
6371ca3b
WN
1163 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1164 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1165 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1166 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1167 CHECK_ERR(bpf_object__validate(obj), err, out);
1a5e3fb1
WN
1168
1169 bpf_object__elf_finish(obj);
1170 return obj;
1171out:
1172 bpf_object__close(obj);
6371ca3b 1173 return ERR_PTR(err);
1a5e3fb1
WN
1174}
1175
1176struct bpf_object *bpf_object__open(const char *path)
1177{
1178 /* param validation */
1179 if (!path)
1180 return NULL;
1181
1182 pr_debug("loading %s\n", path);
1183
6c956392
WN
1184 return __bpf_object__open(path, NULL, 0);
1185}
1186
1187struct bpf_object *bpf_object__open_buffer(void *obj_buf,
acf860ae
WN
1188 size_t obj_buf_sz,
1189 const char *name)
6c956392 1190{
acf860ae
WN
1191 char tmp_name[64];
1192
6c956392
WN
1193 /* param validation */
1194 if (!obj_buf || obj_buf_sz <= 0)
1195 return NULL;
1196
acf860ae
WN
1197 if (!name) {
1198 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1199 (unsigned long)obj_buf,
1200 (unsigned long)obj_buf_sz);
1201 tmp_name[sizeof(tmp_name) - 1] = '\0';
1202 name = tmp_name;
1203 }
1204 pr_debug("loading object '%s' from buffer\n",
1205 name);
6c956392 1206
acf860ae 1207 return __bpf_object__open(name, obj_buf, obj_buf_sz);
1a5e3fb1
WN
1208}
1209
52d3352e
WN
1210int bpf_object__unload(struct bpf_object *obj)
1211{
1212 size_t i;
1213
1214 if (!obj)
1215 return -EINVAL;
1216
9d759a9b
WN
1217 for (i = 0; i < obj->nr_maps; i++)
1218 zclose(obj->maps[i].fd);
52d3352e 1219
55cffde2
WN
1220 for (i = 0; i < obj->nr_programs; i++)
1221 bpf_program__unload(&obj->programs[i]);
1222
52d3352e
WN
1223 return 0;
1224}
1225
1226int bpf_object__load(struct bpf_object *obj)
1227{
6371ca3b
WN
1228 int err;
1229
52d3352e
WN
1230 if (!obj)
1231 return -EINVAL;
1232
1233 if (obj->loaded) {
1234 pr_warning("object should not be loaded twice\n");
1235 return -EINVAL;
1236 }
1237
1238 obj->loaded = true;
6371ca3b
WN
1239
1240 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1241 CHECK_ERR(bpf_object__relocate(obj), err, out);
1242 CHECK_ERR(bpf_object__load_progs(obj), err, out);
52d3352e
WN
1243
1244 return 0;
1245out:
1246 bpf_object__unload(obj);
1247 pr_warning("failed to load object '%s'\n", obj->path);
6371ca3b 1248 return err;
52d3352e
WN
1249}
1250
f367540c
JS
1251static int check_path(const char *path)
1252{
1253 struct statfs st_fs;
1254 char *dname, *dir;
1255 int err = 0;
1256
1257 if (path == NULL)
1258 return -EINVAL;
1259
1260 dname = strdup(path);
1261 if (dname == NULL)
1262 return -ENOMEM;
1263
1264 dir = dirname(dname);
1265 if (statfs(dir, &st_fs)) {
1266 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1267 err = -errno;
1268 }
1269 free(dname);
1270
1271 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1272 pr_warning("specified path %s is not on BPF FS\n", path);
1273 err = -EINVAL;
1274 }
1275
1276 return err;
1277}
1278
1279int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1280 int instance)
1281{
1282 int err;
1283
1284 err = check_path(path);
1285 if (err)
1286 return err;
1287
1288 if (prog == NULL) {
1289 pr_warning("invalid program pointer\n");
1290 return -EINVAL;
1291 }
1292
1293 if (instance < 0 || instance >= prog->instances.nr) {
1294 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1295 instance, prog->section_name, prog->instances.nr);
1296 return -EINVAL;
1297 }
1298
1299 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1300 pr_warning("failed to pin program: %s\n", strerror(errno));
1301 return -errno;
1302 }
1303 pr_debug("pinned program '%s'\n", path);
1304
1305 return 0;
1306}
1307
1308static int make_dir(const char *path)
1309{
1310 int err = 0;
1311
1312 if (mkdir(path, 0700) && errno != EEXIST)
1313 err = -errno;
1314
1315 if (err)
1316 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1317 return err;
1318}
1319
1320int bpf_program__pin(struct bpf_program *prog, const char *path)
1321{
1322 int i, err;
1323
1324 err = check_path(path);
1325 if (err)
1326 return err;
1327
1328 if (prog == NULL) {
1329 pr_warning("invalid program pointer\n");
1330 return -EINVAL;
1331 }
1332
1333 if (prog->instances.nr <= 0) {
1334 pr_warning("no instances of prog %s to pin\n",
1335 prog->section_name);
1336 return -EINVAL;
1337 }
1338
1339 err = make_dir(path);
1340 if (err)
1341 return err;
1342
1343 for (i = 0; i < prog->instances.nr; i++) {
1344 char buf[PATH_MAX];
1345 int len;
1346
1347 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1348 if (len < 0)
1349 return -EINVAL;
1350 else if (len >= PATH_MAX)
1351 return -ENAMETOOLONG;
1352
1353 err = bpf_program__pin_instance(prog, buf, i);
1354 if (err)
1355 return err;
1356 }
1357
1358 return 0;
1359}
1360
b6989f35
JS
1361int bpf_map__pin(struct bpf_map *map, const char *path)
1362{
1363 int err;
1364
1365 err = check_path(path);
1366 if (err)
1367 return err;
1368
1369 if (map == NULL) {
1370 pr_warning("invalid map pointer\n");
1371 return -EINVAL;
1372 }
1373
1374 if (bpf_obj_pin(map->fd, path)) {
1375 pr_warning("failed to pin map: %s\n", strerror(errno));
1376 return -errno;
1377 }
1378
1379 pr_debug("pinned map '%s'\n", path);
1380 return 0;
1381}
1382
d5148d85
JS
1383int bpf_object__pin(struct bpf_object *obj, const char *path)
1384{
1385 struct bpf_program *prog;
1386 struct bpf_map *map;
1387 int err;
1388
1389 if (!obj)
1390 return -ENOENT;
1391
1392 if (!obj->loaded) {
1393 pr_warning("object not yet loaded; load it first\n");
1394 return -ENOENT;
1395 }
1396
1397 err = make_dir(path);
1398 if (err)
1399 return err;
1400
1401 bpf_map__for_each(map, obj) {
1402 char buf[PATH_MAX];
1403 int len;
1404
1405 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1406 bpf_map__name(map));
1407 if (len < 0)
1408 return -EINVAL;
1409 else if (len >= PATH_MAX)
1410 return -ENAMETOOLONG;
1411
1412 err = bpf_map__pin(map, buf);
1413 if (err)
1414 return err;
1415 }
1416
1417 bpf_object__for_each_program(prog, obj) {
1418 char buf[PATH_MAX];
1419 int len;
1420
1421 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1422 prog->section_name);
1423 if (len < 0)
1424 return -EINVAL;
1425 else if (len >= PATH_MAX)
1426 return -ENAMETOOLONG;
1427
1428 err = bpf_program__pin(prog, buf);
1429 if (err)
1430 return err;
1431 }
1432
1433 return 0;
1434}
1435
1a5e3fb1
WN
1436void bpf_object__close(struct bpf_object *obj)
1437{
a5b8bd47
WN
1438 size_t i;
1439
1a5e3fb1
WN
1440 if (!obj)
1441 return;
1442
10931d24
WN
1443 if (obj->clear_priv)
1444 obj->clear_priv(obj, obj->priv);
1445
1a5e3fb1 1446 bpf_object__elf_finish(obj);
52d3352e 1447 bpf_object__unload(obj);
1a5e3fb1 1448
9d759a9b 1449 for (i = 0; i < obj->nr_maps; i++) {
561bbcca 1450 zfree(&obj->maps[i].name);
9d759a9b
WN
1451 if (obj->maps[i].clear_priv)
1452 obj->maps[i].clear_priv(&obj->maps[i],
1453 obj->maps[i].priv);
1454 obj->maps[i].priv = NULL;
1455 obj->maps[i].clear_priv = NULL;
1456 }
1457 zfree(&obj->maps);
1458 obj->nr_maps = 0;
a5b8bd47
WN
1459
1460 if (obj->programs && obj->nr_programs) {
1461 for (i = 0; i < obj->nr_programs; i++)
1462 bpf_program__exit(&obj->programs[i]);
1463 }
1464 zfree(&obj->programs);
1465
9a208eff 1466 list_del(&obj->list);
1a5e3fb1
WN
1467 free(obj);
1468}
aa9b1ac3 1469
9a208eff
WN
1470struct bpf_object *
1471bpf_object__next(struct bpf_object *prev)
1472{
1473 struct bpf_object *next;
1474
1475 if (!prev)
1476 next = list_first_entry(&bpf_objects_list,
1477 struct bpf_object,
1478 list);
1479 else
1480 next = list_next_entry(prev, list);
1481
1482 /* Empty list is noticed here so don't need checking on entry. */
1483 if (&next->list == &bpf_objects_list)
1484 return NULL;
1485
1486 return next;
1487}
1488
a7fe0450 1489const char *bpf_object__name(struct bpf_object *obj)
acf860ae 1490{
a7fe0450 1491 return obj ? obj->path : ERR_PTR(-EINVAL);
acf860ae
WN
1492}
1493
a7fe0450 1494unsigned int bpf_object__kversion(struct bpf_object *obj)
45825d8a 1495{
a7fe0450 1496 return obj ? obj->kern_version : 0;
45825d8a
WN
1497}
1498
10931d24
WN
1499int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1500 bpf_object_clear_priv_t clear_priv)
1501{
1502 if (obj->priv && obj->clear_priv)
1503 obj->clear_priv(obj, obj->priv);
1504
1505 obj->priv = priv;
1506 obj->clear_priv = clear_priv;
1507 return 0;
1508}
1509
1510void *bpf_object__priv(struct bpf_object *obj)
1511{
1512 return obj ? obj->priv : ERR_PTR(-EINVAL);
1513}
1514
aa9b1ac3
WN
1515struct bpf_program *
1516bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1517{
1518 size_t idx;
1519
1520 if (!obj->programs)
1521 return NULL;
1522 /* First handler */
1523 if (prev == NULL)
1524 return &obj->programs[0];
1525
1526 if (prev->obj != obj) {
1527 pr_warning("error: program handler doesn't match object\n");
1528 return NULL;
1529 }
1530
1531 idx = (prev - obj->programs) + 1;
1532 if (idx >= obj->nr_programs)
1533 return NULL;
1534 return &obj->programs[idx];
1535}
1536
edb13ed4
ACM
1537int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1538 bpf_program_clear_priv_t clear_priv)
aa9b1ac3
WN
1539{
1540 if (prog->priv && prog->clear_priv)
1541 prog->clear_priv(prog, prog->priv);
1542
1543 prog->priv = priv;
1544 prog->clear_priv = clear_priv;
1545 return 0;
1546}
1547
be834ffb 1548void *bpf_program__priv(struct bpf_program *prog)
aa9b1ac3 1549{
be834ffb 1550 return prog ? prog->priv : ERR_PTR(-EINVAL);
aa9b1ac3
WN
1551}
1552
715f8db9 1553const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
aa9b1ac3
WN
1554{
1555 const char *title;
1556
1557 title = prog->section_name;
715f8db9 1558 if (needs_copy) {
aa9b1ac3
WN
1559 title = strdup(title);
1560 if (!title) {
1561 pr_warning("failed to strdup program title\n");
6371ca3b 1562 return ERR_PTR(-ENOMEM);
aa9b1ac3
WN
1563 }
1564 }
1565
1566 return title;
1567}
1568
1569int bpf_program__fd(struct bpf_program *prog)
1570{
b580563e
WN
1571 return bpf_program__nth_fd(prog, 0);
1572}
1573
1574int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1575 bpf_program_prep_t prep)
1576{
1577 int *instances_fds;
1578
1579 if (nr_instances <= 0 || !prep)
1580 return -EINVAL;
1581
1582 if (prog->instances.nr > 0 || prog->instances.fds) {
1583 pr_warning("Can't set pre-processor after loading\n");
1584 return -EINVAL;
1585 }
1586
1587 instances_fds = malloc(sizeof(int) * nr_instances);
1588 if (!instances_fds) {
1589 pr_warning("alloc memory failed for fds\n");
1590 return -ENOMEM;
1591 }
1592
1593 /* fill all fd with -1 */
1594 memset(instances_fds, -1, sizeof(int) * nr_instances);
1595
1596 prog->instances.nr = nr_instances;
1597 prog->instances.fds = instances_fds;
1598 prog->preprocessor = prep;
1599 return 0;
1600}
1601
1602int bpf_program__nth_fd(struct bpf_program *prog, int n)
1603{
1604 int fd;
1605
1606 if (n >= prog->instances.nr || n < 0) {
1607 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1608 n, prog->section_name, prog->instances.nr);
1609 return -EINVAL;
1610 }
1611
1612 fd = prog->instances.fds[n];
1613 if (fd < 0) {
1614 pr_warning("%dth instance of program '%s' is invalid\n",
1615 n, prog->section_name);
1616 return -ENOENT;
1617 }
1618
1619 return fd;
aa9b1ac3 1620}
9d759a9b 1621
dd26b7f5 1622void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
5f44e4c8
WN
1623{
1624 prog->type = type;
1625}
1626
5f44e4c8
WN
1627static bool bpf_program__is_type(struct bpf_program *prog,
1628 enum bpf_prog_type type)
1629{
1630 return prog ? (prog->type == type) : false;
1631}
1632
ed794073
JS
1633#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1634int bpf_program__set_##NAME(struct bpf_program *prog) \
1635{ \
1636 if (!prog) \
1637 return -EINVAL; \
1638 bpf_program__set_type(prog, TYPE); \
1639 return 0; \
1640} \
1641 \
1642bool bpf_program__is_##NAME(struct bpf_program *prog) \
1643{ \
1644 return bpf_program__is_type(prog, TYPE); \
1645} \
1646
7803ba73 1647BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
ed794073 1648BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
7803ba73
JS
1649BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1650BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
ed794073 1651BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
7803ba73
JS
1652BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1653BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
5f44e4c8 1654
6e009e65 1655int bpf_map__fd(struct bpf_map *map)
9d759a9b 1656{
6e009e65 1657 return map ? map->fd : -EINVAL;
9d759a9b
WN
1658}
1659
53897a78 1660const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
9d759a9b 1661{
53897a78 1662 return map ? &map->def : ERR_PTR(-EINVAL);
9d759a9b
WN
1663}
1664
009ad5d5 1665const char *bpf_map__name(struct bpf_map *map)
561bbcca 1666{
009ad5d5 1667 return map ? map->name : NULL;
561bbcca
WN
1668}
1669
edb13ed4
ACM
1670int bpf_map__set_priv(struct bpf_map *map, void *priv,
1671 bpf_map_clear_priv_t clear_priv)
9d759a9b
WN
1672{
1673 if (!map)
1674 return -EINVAL;
1675
1676 if (map->priv) {
1677 if (map->clear_priv)
1678 map->clear_priv(map, map->priv);
1679 }
1680
1681 map->priv = priv;
1682 map->clear_priv = clear_priv;
1683 return 0;
1684}
1685
b4cbfa56 1686void *bpf_map__priv(struct bpf_map *map)
9d759a9b 1687{
b4cbfa56 1688 return map ? map->priv : ERR_PTR(-EINVAL);
9d759a9b
WN
1689}
1690
1691struct bpf_map *
1692bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1693{
1694 size_t idx;
1695 struct bpf_map *s, *e;
1696
1697 if (!obj || !obj->maps)
1698 return NULL;
1699
1700 s = obj->maps;
1701 e = obj->maps + obj->nr_maps;
1702
1703 if (prev == NULL)
1704 return s;
1705
1706 if ((prev < s) || (prev >= e)) {
1707 pr_warning("error in %s: map handler doesn't belong to object\n",
1708 __func__);
1709 return NULL;
1710 }
1711
1712 idx = (prev - obj->maps) + 1;
1713 if (idx >= obj->nr_maps)
1714 return NULL;
1715 return &obj->maps[idx];
1716}
561bbcca
WN
1717
1718struct bpf_map *
a7fe0450 1719bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
561bbcca
WN
1720{
1721 struct bpf_map *pos;
1722
1723 bpf_map__for_each(pos, obj) {
973170e6 1724 if (pos->name && !strcmp(pos->name, name))
561bbcca
WN
1725 return pos;
1726 }
1727 return NULL;
1728}
5a6acad1
WN
1729
1730struct bpf_map *
1731bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1732{
1733 int i;
1734
1735 for (i = 0; i < obj->nr_maps; i++) {
1736 if (obj->maps[i].offset == offset)
1737 return &obj->maps[i];
1738 }
1739 return ERR_PTR(-ENOENT);
1740}
e28ff1a8
JS
1741
1742long libbpf_get_error(const void *ptr)
1743{
1744 if (IS_ERR(ptr))
1745 return PTR_ERR(ptr);
1746 return 0;
1747}