blob: 4751936831d761809ab67e1c972a9ee0f239c463 [file] [log] [blame]
Wang Nan1b76c132015-07-01 02:13:51 +00001/*
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.
Wang Nan203d1ca2016-07-04 11:02:42 +00007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License (not later!)
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nan1b76c132015-07-01 02:13:51 +000020 */
21
22#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000023#include <stdio.h>
24#include <stdarg.h>
Wang Nan34090912015-07-01 02:14:02 +000025#include <inttypes.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000026#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000027#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000028#include <fcntl.h>
29#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000030#include <asm/unistd.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000031#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000032#include <linux/bpf.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000033#include <linux/list.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000034#include <libelf.h>
35#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000036
37#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000038#include "bpf.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000039
40#define __printf(a, b) __attribute__((format(printf, a, b)))
41
42__printf(1, 2)
43static int __base_pr(const char *format, ...)
44{
45 va_list args;
46 int err;
47
48 va_start(args, format);
49 err = vfprintf(stderr, format, args);
50 va_end(args);
51 return err;
52}
53
54static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
55static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
56static __printf(1, 2) libbpf_print_fn_t __pr_debug;
57
58#define __pr(func, fmt, ...) \
59do { \
60 if ((func)) \
61 (func)("libbpf: " fmt, ##__VA_ARGS__); \
62} while (0)
63
64#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
65#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
66#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
67
68void libbpf_set_print(libbpf_print_fn_t warn,
69 libbpf_print_fn_t info,
70 libbpf_print_fn_t debug)
71{
72 __pr_warning = warn;
73 __pr_info = info;
74 __pr_debug = debug;
75}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000076
Wang Nan6371ca3b2015-11-06 13:49:37 +000077#define STRERR_BUFSIZE 128
78
79#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
80#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
81#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
82
83static const char *libbpf_strerror_table[NR_ERRNO] = {
84 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
85 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
86 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
Colin Ian Kingde8a63b2016-06-28 13:23:37 +010087 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
Wang Nan6371ca3b2015-11-06 13:49:37 +000088 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
89 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
90 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
91 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
92 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
93};
94
95int libbpf_strerror(int err, char *buf, size_t size)
96{
97 if (!buf || !size)
98 return -1;
99
100 err = err > 0 ? err : -err;
101
102 if (err < __LIBBPF_ERRNO__START) {
103 int ret;
104
105 ret = strerror_r(err, buf, size);
106 buf[size - 1] = '\0';
107 return ret;
108 }
109
110 if (err < __LIBBPF_ERRNO__END) {
111 const char *msg;
112
113 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
114 snprintf(buf, size, "%s", msg);
115 buf[size - 1] = '\0';
116 return 0;
117 }
118
119 snprintf(buf, size, "Unknown libbpf error %d", err);
120 buf[size - 1] = '\0';
121 return -1;
122}
123
124#define CHECK_ERR(action, err, out) do { \
125 err = action; \
126 if (err) \
127 goto out; \
128} while(0)
129
130
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000131/* Copied from tools/perf/util/util.h */
132#ifndef zfree
133# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
134#endif
135
136#ifndef zclose
137# define zclose(fd) ({ \
138 int ___err = 0; \
139 if ((fd) >= 0) \
140 ___err = close((fd)); \
141 fd = -1; \
142 ___err; })
143#endif
144
145#ifdef HAVE_LIBELF_MMAP_SUPPORT
146# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
147#else
148# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
149#endif
150
Wang Nana5b8bd42015-07-01 02:14:00 +0000151/*
152 * bpf_prog should be a better name but it has been used in
153 * linux/filter.h.
154 */
155struct bpf_program {
156 /* Index in elf obj file, for relocation use. */
157 int idx;
158 char *section_name;
159 struct bpf_insn *insns;
160 size_t insns_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000161 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000162
163 struct {
164 int insn_idx;
165 int map_idx;
166 } *reloc_desc;
167 int nr_reloc;
Wang Nan55cffde2015-07-01 02:14:07 +0000168
Wang Nanb5805632015-11-16 12:10:09 +0000169 struct {
170 int nr;
171 int *fds;
172 } instances;
173 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000174
175 struct bpf_object *obj;
176 void *priv;
177 bpf_program_clear_priv_t clear_priv;
Wang Nana5b8bd42015-07-01 02:14:00 +0000178};
179
Wang Nan9d759a92015-11-27 08:47:35 +0000180struct bpf_map {
181 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000182 char *name;
Wang Nan9d759a92015-11-27 08:47:35 +0000183 struct bpf_map_def def;
184 void *priv;
185 bpf_map_clear_priv_t clear_priv;
186};
187
Wang Nan9a208ef2015-07-01 02:14:10 +0000188static LIST_HEAD(bpf_objects_list);
189
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000190struct bpf_object {
Wang Nancb1e5e92015-07-01 02:13:57 +0000191 char license[64];
192 u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000193
Wang Nana5b8bd42015-07-01 02:14:00 +0000194 struct bpf_program *programs;
195 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000196 struct bpf_map *maps;
197 size_t nr_maps;
198
Wang Nan52d33522015-07-01 02:14:04 +0000199 bool loaded;
Wang Nana5b8bd42015-07-01 02:14:00 +0000200
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000201 /*
202 * Information when doing elf related work. Only valid if fd
203 * is valid.
204 */
205 struct {
206 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000207 void *obj_buf;
208 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000209 Elf *elf;
210 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000211 Elf_Data *symbols;
Wang Nan77ba9a52015-12-08 02:25:30 +0000212 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000213 struct {
214 GElf_Shdr shdr;
215 Elf_Data *data;
216 } *reloc;
217 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000218 int maps_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000219 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000220 /*
221 * All loaded bpf_object is linked in a list, which is
222 * hidden to caller. bpf_objects__<func> handlers deal with
223 * all objects.
224 */
225 struct list_head list;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000226 char path[];
227};
228#define obj_elf_valid(o) ((o)->efile.elf)
229
Wang Nan55cffde2015-07-01 02:14:07 +0000230static void bpf_program__unload(struct bpf_program *prog)
231{
Wang Nanb5805632015-11-16 12:10:09 +0000232 int i;
233
Wang Nan55cffde2015-07-01 02:14:07 +0000234 if (!prog)
235 return;
236
Wang Nanb5805632015-11-16 12:10:09 +0000237 /*
238 * If the object is opened but the program was never loaded,
239 * it is possible that prog->instances.nr == -1.
240 */
241 if (prog->instances.nr > 0) {
242 for (i = 0; i < prog->instances.nr; i++)
243 zclose(prog->instances.fds[i]);
244 } else if (prog->instances.nr != -1) {
245 pr_warning("Internal error: instances.nr is %d\n",
246 prog->instances.nr);
247 }
248
249 prog->instances.nr = -1;
250 zfree(&prog->instances.fds);
Wang Nan55cffde2015-07-01 02:14:07 +0000251}
252
Wang Nana5b8bd42015-07-01 02:14:00 +0000253static void bpf_program__exit(struct bpf_program *prog)
254{
255 if (!prog)
256 return;
257
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000258 if (prog->clear_priv)
259 prog->clear_priv(prog, prog->priv);
260
261 prog->priv = NULL;
262 prog->clear_priv = NULL;
263
Wang Nan55cffde2015-07-01 02:14:07 +0000264 bpf_program__unload(prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000265 zfree(&prog->section_name);
266 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000267 zfree(&prog->reloc_desc);
268
269 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000270 prog->insns_cnt = 0;
271 prog->idx = -1;
272}
273
274static int
275bpf_program__init(void *data, size_t size, char *name, int idx,
276 struct bpf_program *prog)
277{
278 if (size < sizeof(struct bpf_insn)) {
279 pr_warning("corrupted section '%s'\n", name);
280 return -EINVAL;
281 }
282
283 bzero(prog, sizeof(*prog));
284
285 prog->section_name = strdup(name);
286 if (!prog->section_name) {
287 pr_warning("failed to alloc name for prog %s\n",
288 name);
289 goto errout;
290 }
291
292 prog->insns = malloc(size);
293 if (!prog->insns) {
294 pr_warning("failed to alloc insns for %s\n", name);
295 goto errout;
296 }
297 prog->insns_cnt = size / sizeof(struct bpf_insn);
298 memcpy(prog->insns, data,
299 prog->insns_cnt * sizeof(struct bpf_insn));
300 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000301 prog->instances.fds = NULL;
302 prog->instances.nr = -1;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000303 prog->type = BPF_PROG_TYPE_KPROBE;
Wang Nana5b8bd42015-07-01 02:14:00 +0000304
305 return 0;
306errout:
307 bpf_program__exit(prog);
308 return -ENOMEM;
309}
310
311static int
312bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
313 char *name, int idx)
314{
315 struct bpf_program prog, *progs;
316 int nr_progs, err;
317
318 err = bpf_program__init(data, size, name, idx, &prog);
319 if (err)
320 return err;
321
322 progs = obj->programs;
323 nr_progs = obj->nr_programs;
324
325 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
326 if (!progs) {
327 /*
328 * In this case the original obj->programs
329 * is still valid, so don't need special treat for
330 * bpf_close_object().
331 */
332 pr_warning("failed to alloc a new program '%s'\n",
333 name);
334 bpf_program__exit(&prog);
335 return -ENOMEM;
336 }
337
338 pr_debug("found program %s\n", prog.section_name);
339 obj->programs = progs;
340 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000341 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000342 progs[nr_progs] = prog;
343 return 0;
344}
345
Wang Nan6c956392015-07-01 02:13:54 +0000346static struct bpf_object *bpf_object__new(const char *path,
347 void *obj_buf,
348 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000349{
350 struct bpf_object *obj;
351
352 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
353 if (!obj) {
354 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000355 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000356 }
357
358 strcpy(obj->path, path);
359 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000360
361 /*
362 * Caller of this function should also calls
363 * bpf_object__elf_finish() after data collection to return
364 * obj_buf to user. If not, we should duplicate the buffer to
365 * avoid user freeing them before elf finish.
366 */
367 obj->efile.obj_buf = obj_buf;
368 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000369 obj->efile.maps_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000370
Wang Nan52d33522015-07-01 02:14:04 +0000371 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000372
373 INIT_LIST_HEAD(&obj->list);
374 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000375 return obj;
376}
377
378static void bpf_object__elf_finish(struct bpf_object *obj)
379{
380 if (!obj_elf_valid(obj))
381 return;
382
383 if (obj->efile.elf) {
384 elf_end(obj->efile.elf);
385 obj->efile.elf = NULL;
386 }
Wang Nanbec7d682015-07-01 02:13:59 +0000387 obj->efile.symbols = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000388
389 zfree(&obj->efile.reloc);
390 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000391 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000392 obj->efile.obj_buf = NULL;
393 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000394}
395
396static int bpf_object__elf_init(struct bpf_object *obj)
397{
398 int err = 0;
399 GElf_Ehdr *ep;
400
401 if (obj_elf_valid(obj)) {
402 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000403 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000404 }
405
Wang Nan6c956392015-07-01 02:13:54 +0000406 if (obj->efile.obj_buf_sz > 0) {
407 /*
408 * obj_buf should have been validated by
409 * bpf_object__open_buffer().
410 */
411 obj->efile.elf = elf_memory(obj->efile.obj_buf,
412 obj->efile.obj_buf_sz);
413 } else {
414 obj->efile.fd = open(obj->path, O_RDONLY);
415 if (obj->efile.fd < 0) {
416 pr_warning("failed to open %s: %s\n", obj->path,
417 strerror(errno));
418 return -errno;
419 }
420
421 obj->efile.elf = elf_begin(obj->efile.fd,
422 LIBBPF_ELF_C_READ_MMAP,
423 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000424 }
425
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000426 if (!obj->efile.elf) {
427 pr_warning("failed to open %s as ELF file\n",
428 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000429 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000430 goto errout;
431 }
432
433 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
434 pr_warning("failed to get EHDR from %s\n",
435 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000436 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000437 goto errout;
438 }
439 ep = &obj->efile.ehdr;
440
441 if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
442 pr_warning("%s is not an eBPF object file\n",
443 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000444 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000445 goto errout;
446 }
447
448 return 0;
449errout:
450 bpf_object__elf_finish(obj);
451 return err;
452}
453
Wang Nancc4228d2015-07-01 02:13:55 +0000454static int
455bpf_object__check_endianness(struct bpf_object *obj)
456{
457 static unsigned int const endian = 1;
458
459 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
460 case ELFDATA2LSB:
461 /* We are big endian, BPF obj is little endian. */
462 if (*(unsigned char const *)&endian != 1)
463 goto mismatch;
464 break;
465
466 case ELFDATA2MSB:
467 /* We are little endian, BPF obj is big endian. */
468 if (*(unsigned char const *)&endian != 0)
469 goto mismatch;
470 break;
471 default:
Wang Nan6371ca3b2015-11-06 13:49:37 +0000472 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000473 }
474
475 return 0;
476
477mismatch:
478 pr_warning("Error: endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000479 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000480}
481
Wang Nancb1e5e92015-07-01 02:13:57 +0000482static int
483bpf_object__init_license(struct bpf_object *obj,
484 void *data, size_t size)
485{
486 memcpy(obj->license, data,
487 min(size, sizeof(obj->license) - 1));
488 pr_debug("license of %s is %s\n", obj->path, obj->license);
489 return 0;
490}
491
492static int
493bpf_object__init_kversion(struct bpf_object *obj,
494 void *data, size_t size)
495{
496 u32 kver;
497
498 if (size != sizeof(kver)) {
499 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000500 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000501 }
502 memcpy(&kver, data, sizeof(kver));
503 obj->kern_version = kver;
504 pr_debug("kernel version of %s is %x\n", obj->path,
505 obj->kern_version);
506 return 0;
507}
508
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000509static int
510bpf_object__init_maps(struct bpf_object *obj, void *data,
511 size_t size)
512{
Wang Nan9d759a92015-11-27 08:47:35 +0000513 size_t nr_maps;
514 int i;
515
516 nr_maps = size / sizeof(struct bpf_map_def);
517 if (!data || !nr_maps) {
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000518 pr_debug("%s doesn't need map definition\n",
519 obj->path);
520 return 0;
521 }
522
Wang Nan9d759a92015-11-27 08:47:35 +0000523 pr_debug("maps in %s: %zd bytes\n", obj->path, size);
524
525 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
526 if (!obj->maps) {
527 pr_warning("alloc maps for object failed\n");
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000528 return -ENOMEM;
529 }
Wang Nan9d759a92015-11-27 08:47:35 +0000530 obj->nr_maps = nr_maps;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000531
Wang Nan9d759a92015-11-27 08:47:35 +0000532 for (i = 0; i < nr_maps; i++) {
533 struct bpf_map_def *def = &obj->maps[i].def;
534
535 /*
536 * fill all fd with -1 so won't close incorrect
537 * fd (fd=0 is stdin) when failure (zclose won't close
538 * negative fd)).
539 */
540 obj->maps[i].fd = -1;
541
542 /* Save map definition into obj->maps */
543 *def = ((struct bpf_map_def *)data)[i];
544 }
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000545 return 0;
546}
547
Wang Nan973170e2015-12-08 02:25:29 +0000548static int
Wang Nan666810e2016-01-25 09:55:49 +0000549bpf_object__init_maps_name(struct bpf_object *obj)
Wang Nan561bbcc2015-11-27 08:47:36 +0000550{
551 int i;
552 Elf_Data *symbols = obj->efile.symbols;
553
Wang Nan666810e2016-01-25 09:55:49 +0000554 if (!symbols || obj->efile.maps_shndx < 0)
Wang Nan973170e2015-12-08 02:25:29 +0000555 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000556
557 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
558 GElf_Sym sym;
559 size_t map_idx;
560 const char *map_name;
561
562 if (!gelf_getsym(symbols, i, &sym))
563 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000564 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000565 continue;
566
567 map_name = elf_strptr(obj->efile.elf,
Wang Nan77ba9a52015-12-08 02:25:30 +0000568 obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000569 sym.st_name);
570 map_idx = sym.st_value / sizeof(struct bpf_map_def);
571 if (map_idx >= obj->nr_maps) {
572 pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
573 map_name, map_idx, obj->nr_maps);
574 continue;
575 }
576 obj->maps[map_idx].name = strdup(map_name);
Wang Nan973170e2015-12-08 02:25:29 +0000577 if (!obj->maps[map_idx].name) {
578 pr_warning("failed to alloc map name\n");
579 return -ENOMEM;
580 }
Wang Nan561bbcc2015-11-27 08:47:36 +0000581 pr_debug("map %zu is \"%s\"\n", map_idx,
582 obj->maps[map_idx].name);
583 }
Wang Nan973170e2015-12-08 02:25:29 +0000584 return 0;
Wang Nan561bbcc2015-11-27 08:47:36 +0000585}
586
Wang Nan29603662015-07-01 02:13:56 +0000587static int bpf_object__elf_collect(struct bpf_object *obj)
588{
589 Elf *elf = obj->efile.elf;
590 GElf_Ehdr *ep = &obj->efile.ehdr;
591 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +0000592 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +0000593
594 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
595 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
596 pr_warning("failed to get e_shstrndx from %s\n",
597 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000598 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000599 }
600
601 while ((scn = elf_nextscn(elf, scn)) != NULL) {
602 char *name;
603 GElf_Shdr sh;
604 Elf_Data *data;
605
606 idx++;
607 if (gelf_getshdr(scn, &sh) != &sh) {
608 pr_warning("failed to get section header from %s\n",
609 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000610 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000611 goto out;
612 }
613
614 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
615 if (!name) {
616 pr_warning("failed to get section name from %s\n",
617 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000618 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000619 goto out;
620 }
621
622 data = elf_getdata(scn, 0);
623 if (!data) {
624 pr_warning("failed to get section data from %s(%s)\n",
625 name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000626 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000627 goto out;
628 }
629 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
630 name, (unsigned long)data->d_size,
631 (int)sh.sh_link, (unsigned long)sh.sh_flags,
632 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +0000633
634 if (strcmp(name, "license") == 0)
635 err = bpf_object__init_license(obj,
636 data->d_buf,
637 data->d_size);
638 else if (strcmp(name, "version") == 0)
639 err = bpf_object__init_kversion(obj,
640 data->d_buf,
641 data->d_size);
Wang Nan561bbcc2015-11-27 08:47:36 +0000642 else if (strcmp(name, "maps") == 0) {
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000643 err = bpf_object__init_maps(obj, data->d_buf,
644 data->d_size);
Wang Nan666810e2016-01-25 09:55:49 +0000645 obj->efile.maps_shndx = idx;
Wang Nan561bbcc2015-11-27 08:47:36 +0000646 } else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +0000647 if (obj->efile.symbols) {
648 pr_warning("bpf: multiple SYMTAB in %s\n",
649 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000650 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +0000651 } else {
Wang Nanbec7d682015-07-01 02:13:59 +0000652 obj->efile.symbols = data;
Wang Nan77ba9a52015-12-08 02:25:30 +0000653 obj->efile.strtabidx = sh.sh_link;
654 }
Wang Nana5b8bd42015-07-01 02:14:00 +0000655 } else if ((sh.sh_type == SHT_PROGBITS) &&
656 (sh.sh_flags & SHF_EXECINSTR) &&
657 (data->d_size > 0)) {
658 err = bpf_object__add_program(obj, data->d_buf,
659 data->d_size, name, idx);
660 if (err) {
Wang Nan6371ca3b2015-11-06 13:49:37 +0000661 char errmsg[STRERR_BUFSIZE];
662
Wang Nana5b8bd42015-07-01 02:14:00 +0000663 strerror_r(-err, errmsg, sizeof(errmsg));
664 pr_warning("failed to alloc program %s (%s): %s",
665 name, obj->path, errmsg);
666 }
Wang Nanb62f06e2015-07-01 02:14:01 +0000667 } else if (sh.sh_type == SHT_REL) {
668 void *reloc = obj->efile.reloc;
669 int nr_reloc = obj->efile.nr_reloc + 1;
670
671 reloc = realloc(reloc,
672 sizeof(*obj->efile.reloc) * nr_reloc);
673 if (!reloc) {
674 pr_warning("realloc failed\n");
675 err = -ENOMEM;
676 } else {
677 int n = nr_reloc - 1;
678
679 obj->efile.reloc = reloc;
680 obj->efile.nr_reloc = nr_reloc;
681
682 obj->efile.reloc[n].shdr = sh;
683 obj->efile.reloc[n].data = data;
684 }
Wang Nanbec7d682015-07-01 02:13:59 +0000685 }
Wang Nancb1e5e92015-07-01 02:13:57 +0000686 if (err)
687 goto out;
Wang Nan29603662015-07-01 02:13:56 +0000688 }
Wang Nan561bbcc2015-11-27 08:47:36 +0000689
Wang Nan77ba9a52015-12-08 02:25:30 +0000690 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
691 pr_warning("Corrupted ELF file: index of strtab invalid\n");
692 return LIBBPF_ERRNO__FORMAT;
693 }
Wang Nan666810e2016-01-25 09:55:49 +0000694 if (obj->efile.maps_shndx >= 0)
695 err = bpf_object__init_maps_name(obj);
Wang Nan29603662015-07-01 02:13:56 +0000696out:
697 return err;
698}
699
Wang Nan34090912015-07-01 02:14:02 +0000700static struct bpf_program *
701bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
702{
703 struct bpf_program *prog;
704 size_t i;
705
706 for (i = 0; i < obj->nr_programs; i++) {
707 prog = &obj->programs[i];
708 if (prog->idx == idx)
709 return prog;
710 }
711 return NULL;
712}
713
714static int
715bpf_program__collect_reloc(struct bpf_program *prog,
716 size_t nr_maps, GElf_Shdr *shdr,
Wang Nan666810e2016-01-25 09:55:49 +0000717 Elf_Data *data, Elf_Data *symbols,
718 int maps_shndx)
Wang Nan34090912015-07-01 02:14:02 +0000719{
720 int i, nrels;
721
722 pr_debug("collecting relocating info for: '%s'\n",
723 prog->section_name);
724 nrels = shdr->sh_size / shdr->sh_entsize;
725
726 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
727 if (!prog->reloc_desc) {
728 pr_warning("failed to alloc memory in relocation\n");
729 return -ENOMEM;
730 }
731 prog->nr_reloc = nrels;
732
733 for (i = 0; i < nrels; i++) {
734 GElf_Sym sym;
735 GElf_Rel rel;
736 unsigned int insn_idx;
737 struct bpf_insn *insns = prog->insns;
738 size_t map_idx;
739
740 if (!gelf_getrel(data, i, &rel)) {
741 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000742 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000743 }
744
Wang Nan34090912015-07-01 02:14:02 +0000745 if (!gelf_getsym(symbols,
746 GELF_R_SYM(rel.r_info),
747 &sym)) {
748 pr_warning("relocation: symbol %"PRIx64" not found\n",
749 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +0000750 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000751 }
752
Wang Nan666810e2016-01-25 09:55:49 +0000753 if (sym.st_shndx != maps_shndx) {
754 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
755 prog->section_name, sym.st_shndx);
756 return -LIBBPF_ERRNO__RELOC;
757 }
758
759 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
760 pr_debug("relocation: insn_idx=%u\n", insn_idx);
761
Wang Nan34090912015-07-01 02:14:02 +0000762 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
763 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
764 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000765 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000766 }
767
768 map_idx = sym.st_value / sizeof(struct bpf_map_def);
769 if (map_idx >= nr_maps) {
770 pr_warning("bpf relocation: map_idx %d large than %d\n",
771 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000772 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000773 }
774
775 prog->reloc_desc[i].insn_idx = insn_idx;
776 prog->reloc_desc[i].map_idx = map_idx;
777 }
778 return 0;
779}
780
Wang Nan52d33522015-07-01 02:14:04 +0000781static int
782bpf_object__create_maps(struct bpf_object *obj)
783{
784 unsigned int i;
Wang Nan52d33522015-07-01 02:14:04 +0000785
Wang Nan9d759a92015-11-27 08:47:35 +0000786 for (i = 0; i < obj->nr_maps; i++) {
787 struct bpf_map_def *def = &obj->maps[i].def;
788 int *pfd = &obj->maps[i].fd;
Wang Nan52d33522015-07-01 02:14:04 +0000789
Wang Nan9d759a92015-11-27 08:47:35 +0000790 *pfd = bpf_create_map(def->type,
791 def->key_size,
792 def->value_size,
793 def->max_entries);
Wang Nan52d33522015-07-01 02:14:04 +0000794 if (*pfd < 0) {
795 size_t j;
796 int err = *pfd;
797
798 pr_warning("failed to create map: %s\n",
799 strerror(errno));
800 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +0000801 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +0000802 return err;
803 }
804 pr_debug("create map: fd=%d\n", *pfd);
Wang Nan52d33522015-07-01 02:14:04 +0000805 }
806
Wang Nan52d33522015-07-01 02:14:04 +0000807 return 0;
808}
809
Wang Nan8a47a6c2015-07-01 02:14:05 +0000810static int
Wang Nan9d759a92015-11-27 08:47:35 +0000811bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +0000812{
813 int i;
814
815 if (!prog || !prog->reloc_desc)
816 return 0;
817
818 for (i = 0; i < prog->nr_reloc; i++) {
819 int insn_idx, map_idx;
820 struct bpf_insn *insns = prog->insns;
821
822 insn_idx = prog->reloc_desc[i].insn_idx;
823 map_idx = prog->reloc_desc[i].map_idx;
824
825 if (insn_idx >= (int)prog->insns_cnt) {
826 pr_warning("relocation out of range: '%s'\n",
827 prog->section_name);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000828 return -LIBBPF_ERRNO__RELOC;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000829 }
830 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Wang Nan9d759a92015-11-27 08:47:35 +0000831 insns[insn_idx].imm = obj->maps[map_idx].fd;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000832 }
833
834 zfree(&prog->reloc_desc);
835 prog->nr_reloc = 0;
836 return 0;
837}
838
839
840static int
841bpf_object__relocate(struct bpf_object *obj)
842{
843 struct bpf_program *prog;
844 size_t i;
845 int err;
846
847 for (i = 0; i < obj->nr_programs; i++) {
848 prog = &obj->programs[i];
849
Wang Nan9d759a92015-11-27 08:47:35 +0000850 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +0000851 if (err) {
852 pr_warning("failed to relocate '%s'\n",
853 prog->section_name);
854 return err;
855 }
856 }
857 return 0;
858}
859
Wang Nan34090912015-07-01 02:14:02 +0000860static int bpf_object__collect_reloc(struct bpf_object *obj)
861{
862 int i, err;
863
864 if (!obj_elf_valid(obj)) {
865 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000866 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000867 }
868
869 for (i = 0; i < obj->efile.nr_reloc; i++) {
870 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
871 Elf_Data *data = obj->efile.reloc[i].data;
872 int idx = shdr->sh_info;
873 struct bpf_program *prog;
Wang Nan9d759a92015-11-27 08:47:35 +0000874 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +0000875
876 if (shdr->sh_type != SHT_REL) {
877 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000878 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000879 }
880
881 prog = bpf_object__find_prog_by_idx(obj, idx);
882 if (!prog) {
883 pr_warning("relocation failed: no %d section\n",
884 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000885 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000886 }
887
888 err = bpf_program__collect_reloc(prog, nr_maps,
889 shdr, data,
Wang Nan666810e2016-01-25 09:55:49 +0000890 obj->efile.symbols,
891 obj->efile.maps_shndx);
Wang Nan34090912015-07-01 02:14:02 +0000892 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +0000893 return err;
Wang Nan34090912015-07-01 02:14:02 +0000894 }
895 return 0;
896}
897
Wang Nan55cffde2015-07-01 02:14:07 +0000898static int
Wang Nan5f44e4c82016-07-13 10:44:01 +0000899load_program(enum bpf_prog_type type, struct bpf_insn *insns,
900 int insns_cnt, char *license, u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +0000901{
902 int ret;
903 char *log_buf;
904
905 if (!insns || !insns_cnt)
906 return -EINVAL;
907
908 log_buf = malloc(BPF_LOG_BUF_SIZE);
909 if (!log_buf)
910 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
911
Wang Nan5f44e4c82016-07-13 10:44:01 +0000912 ret = bpf_load_program(type, insns, insns_cnt, license,
913 kern_version, log_buf, BPF_LOG_BUF_SIZE);
Wang Nan55cffde2015-07-01 02:14:07 +0000914
915 if (ret >= 0) {
916 *pfd = ret;
917 ret = 0;
918 goto out;
919 }
920
Wang Nan6371ca3b2015-11-06 13:49:37 +0000921 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +0000922 pr_warning("load bpf program failed: %s\n", strerror(errno));
923
Wang Nan6371ca3b2015-11-06 13:49:37 +0000924 if (log_buf && log_buf[0] != '\0') {
925 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +0000926 pr_warning("-- BEGIN DUMP LOG ---\n");
927 pr_warning("\n%s\n", log_buf);
928 pr_warning("-- END LOG --\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000929 } else {
930 if (insns_cnt >= BPF_MAXINSNS) {
931 pr_warning("Program too large (%d insns), at most %d insns\n",
932 insns_cnt, BPF_MAXINSNS);
933 ret = -LIBBPF_ERRNO__PROG2BIG;
934 } else if (log_buf) {
935 pr_warning("log buffer is empty\n");
936 ret = -LIBBPF_ERRNO__KVER;
937 }
Wang Nan55cffde2015-07-01 02:14:07 +0000938 }
939
940out:
941 free(log_buf);
942 return ret;
943}
944
945static int
946bpf_program__load(struct bpf_program *prog,
947 char *license, u32 kern_version)
948{
Wang Nanb5805632015-11-16 12:10:09 +0000949 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +0000950
Wang Nanb5805632015-11-16 12:10:09 +0000951 if (prog->instances.nr < 0 || !prog->instances.fds) {
952 if (prog->preprocessor) {
953 pr_warning("Internal error: can't load program '%s'\n",
954 prog->section_name);
955 return -LIBBPF_ERRNO__INTERNAL;
956 }
Wang Nan55cffde2015-07-01 02:14:07 +0000957
Wang Nanb5805632015-11-16 12:10:09 +0000958 prog->instances.fds = malloc(sizeof(int));
959 if (!prog->instances.fds) {
960 pr_warning("Not enough memory for BPF fds\n");
961 return -ENOMEM;
962 }
963 prog->instances.nr = 1;
964 prog->instances.fds[0] = -1;
965 }
966
967 if (!prog->preprocessor) {
968 if (prog->instances.nr != 1) {
969 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
970 prog->section_name, prog->instances.nr);
971 }
Wang Nan5f44e4c82016-07-13 10:44:01 +0000972 err = load_program(prog->type, prog->insns, prog->insns_cnt,
Wang Nanb5805632015-11-16 12:10:09 +0000973 license, kern_version, &fd);
974 if (!err)
975 prog->instances.fds[0] = fd;
976 goto out;
977 }
978
979 for (i = 0; i < prog->instances.nr; i++) {
980 struct bpf_prog_prep_result result;
981 bpf_program_prep_t preprocessor = prog->preprocessor;
982
983 bzero(&result, sizeof(result));
984 err = preprocessor(prog, i, prog->insns,
985 prog->insns_cnt, &result);
986 if (err) {
987 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
988 i, prog->section_name);
989 goto out;
990 }
991
992 if (!result.new_insn_ptr || !result.new_insn_cnt) {
993 pr_debug("Skip loading the %dth instance of program '%s'\n",
994 i, prog->section_name);
995 prog->instances.fds[i] = -1;
996 if (result.pfd)
997 *result.pfd = -1;
998 continue;
999 }
1000
Wang Nan5f44e4c82016-07-13 10:44:01 +00001001 err = load_program(prog->type, result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00001002 result.new_insn_cnt,
1003 license, kern_version, &fd);
1004
1005 if (err) {
1006 pr_warning("Loading the %dth instance of program '%s' failed\n",
1007 i, prog->section_name);
1008 goto out;
1009 }
1010
1011 if (result.pfd)
1012 *result.pfd = fd;
1013 prog->instances.fds[i] = fd;
1014 }
1015out:
Wang Nan55cffde2015-07-01 02:14:07 +00001016 if (err)
1017 pr_warning("failed to load program '%s'\n",
1018 prog->section_name);
1019 zfree(&prog->insns);
1020 prog->insns_cnt = 0;
1021 return err;
1022}
1023
1024static int
1025bpf_object__load_progs(struct bpf_object *obj)
1026{
1027 size_t i;
1028 int err;
1029
1030 for (i = 0; i < obj->nr_programs; i++) {
1031 err = bpf_program__load(&obj->programs[i],
1032 obj->license,
1033 obj->kern_version);
1034 if (err)
1035 return err;
1036 }
1037 return 0;
1038}
1039
Wang Nancb1e5e92015-07-01 02:13:57 +00001040static int bpf_object__validate(struct bpf_object *obj)
1041{
1042 if (obj->kern_version == 0) {
1043 pr_warning("%s doesn't provide kernel version\n",
1044 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001045 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00001046 }
1047 return 0;
1048}
1049
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001050static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +00001051__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001052{
1053 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001054 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001055
1056 if (elf_version(EV_CURRENT) == EV_NONE) {
1057 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001058 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001059 }
1060
Wang Nan6c956392015-07-01 02:13:54 +00001061 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001062 if (IS_ERR(obj))
1063 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001064
Wang Nan6371ca3b2015-11-06 13:49:37 +00001065 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1066 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1067 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1068 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1069 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001070
1071 bpf_object__elf_finish(obj);
1072 return obj;
1073out:
1074 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001075 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001076}
1077
1078struct bpf_object *bpf_object__open(const char *path)
1079{
1080 /* param validation */
1081 if (!path)
1082 return NULL;
1083
1084 pr_debug("loading %s\n", path);
1085
Wang Nan6c956392015-07-01 02:13:54 +00001086 return __bpf_object__open(path, NULL, 0);
1087}
1088
1089struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001090 size_t obj_buf_sz,
1091 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001092{
Wang Nanacf860a2015-08-27 02:30:55 +00001093 char tmp_name[64];
1094
Wang Nan6c956392015-07-01 02:13:54 +00001095 /* param validation */
1096 if (!obj_buf || obj_buf_sz <= 0)
1097 return NULL;
1098
Wang Nanacf860a2015-08-27 02:30:55 +00001099 if (!name) {
1100 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1101 (unsigned long)obj_buf,
1102 (unsigned long)obj_buf_sz);
1103 tmp_name[sizeof(tmp_name) - 1] = '\0';
1104 name = tmp_name;
1105 }
1106 pr_debug("loading object '%s' from buffer\n",
1107 name);
Wang Nan6c956392015-07-01 02:13:54 +00001108
Wang Nanacf860a2015-08-27 02:30:55 +00001109 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001110}
1111
Wang Nan52d33522015-07-01 02:14:04 +00001112int bpf_object__unload(struct bpf_object *obj)
1113{
1114 size_t i;
1115
1116 if (!obj)
1117 return -EINVAL;
1118
Wang Nan9d759a92015-11-27 08:47:35 +00001119 for (i = 0; i < obj->nr_maps; i++)
1120 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001121
Wang Nan55cffde2015-07-01 02:14:07 +00001122 for (i = 0; i < obj->nr_programs; i++)
1123 bpf_program__unload(&obj->programs[i]);
1124
Wang Nan52d33522015-07-01 02:14:04 +00001125 return 0;
1126}
1127
1128int bpf_object__load(struct bpf_object *obj)
1129{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001130 int err;
1131
Wang Nan52d33522015-07-01 02:14:04 +00001132 if (!obj)
1133 return -EINVAL;
1134
1135 if (obj->loaded) {
1136 pr_warning("object should not be loaded twice\n");
1137 return -EINVAL;
1138 }
1139
1140 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001141
1142 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1143 CHECK_ERR(bpf_object__relocate(obj), err, out);
1144 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001145
1146 return 0;
1147out:
1148 bpf_object__unload(obj);
1149 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001150 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001151}
1152
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001153void bpf_object__close(struct bpf_object *obj)
1154{
Wang Nana5b8bd42015-07-01 02:14:00 +00001155 size_t i;
1156
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001157 if (!obj)
1158 return;
1159
1160 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001161 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001162
Wang Nan9d759a92015-11-27 08:47:35 +00001163 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001164 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00001165 if (obj->maps[i].clear_priv)
1166 obj->maps[i].clear_priv(&obj->maps[i],
1167 obj->maps[i].priv);
1168 obj->maps[i].priv = NULL;
1169 obj->maps[i].clear_priv = NULL;
1170 }
1171 zfree(&obj->maps);
1172 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00001173
1174 if (obj->programs && obj->nr_programs) {
1175 for (i = 0; i < obj->nr_programs; i++)
1176 bpf_program__exit(&obj->programs[i]);
1177 }
1178 zfree(&obj->programs);
1179
Wang Nan9a208ef2015-07-01 02:14:10 +00001180 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001181 free(obj);
1182}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001183
Wang Nan9a208ef2015-07-01 02:14:10 +00001184struct bpf_object *
1185bpf_object__next(struct bpf_object *prev)
1186{
1187 struct bpf_object *next;
1188
1189 if (!prev)
1190 next = list_first_entry(&bpf_objects_list,
1191 struct bpf_object,
1192 list);
1193 else
1194 next = list_next_entry(prev, list);
1195
1196 /* Empty list is noticed here so don't need checking on entry. */
1197 if (&next->list == &bpf_objects_list)
1198 return NULL;
1199
1200 return next;
1201}
1202
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001203const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00001204{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001205 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001206}
1207
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001208unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00001209{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001210 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00001211}
1212
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001213struct bpf_program *
1214bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1215{
1216 size_t idx;
1217
1218 if (!obj->programs)
1219 return NULL;
1220 /* First handler */
1221 if (prev == NULL)
1222 return &obj->programs[0];
1223
1224 if (prev->obj != obj) {
1225 pr_warning("error: program handler doesn't match object\n");
1226 return NULL;
1227 }
1228
1229 idx = (prev - obj->programs) + 1;
1230 if (idx >= obj->nr_programs)
1231 return NULL;
1232 return &obj->programs[idx];
1233}
1234
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001235int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1236 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001237{
1238 if (prog->priv && prog->clear_priv)
1239 prog->clear_priv(prog, prog->priv);
1240
1241 prog->priv = priv;
1242 prog->clear_priv = clear_priv;
1243 return 0;
1244}
1245
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001246void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001247{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001248 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001249}
1250
Namhyung Kim715f8db2015-11-03 20:21:05 +09001251const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001252{
1253 const char *title;
1254
1255 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001256 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001257 title = strdup(title);
1258 if (!title) {
1259 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001260 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001261 }
1262 }
1263
1264 return title;
1265}
1266
1267int bpf_program__fd(struct bpf_program *prog)
1268{
Wang Nanb5805632015-11-16 12:10:09 +00001269 return bpf_program__nth_fd(prog, 0);
1270}
1271
1272int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1273 bpf_program_prep_t prep)
1274{
1275 int *instances_fds;
1276
1277 if (nr_instances <= 0 || !prep)
1278 return -EINVAL;
1279
1280 if (prog->instances.nr > 0 || prog->instances.fds) {
1281 pr_warning("Can't set pre-processor after loading\n");
1282 return -EINVAL;
1283 }
1284
1285 instances_fds = malloc(sizeof(int) * nr_instances);
1286 if (!instances_fds) {
1287 pr_warning("alloc memory failed for fds\n");
1288 return -ENOMEM;
1289 }
1290
1291 /* fill all fd with -1 */
1292 memset(instances_fds, -1, sizeof(int) * nr_instances);
1293
1294 prog->instances.nr = nr_instances;
1295 prog->instances.fds = instances_fds;
1296 prog->preprocessor = prep;
1297 return 0;
1298}
1299
1300int bpf_program__nth_fd(struct bpf_program *prog, int n)
1301{
1302 int fd;
1303
1304 if (n >= prog->instances.nr || n < 0) {
1305 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1306 n, prog->section_name, prog->instances.nr);
1307 return -EINVAL;
1308 }
1309
1310 fd = prog->instances.fds[n];
1311 if (fd < 0) {
1312 pr_warning("%dth instance of program '%s' is invalid\n",
1313 n, prog->section_name);
1314 return -ENOENT;
1315 }
1316
1317 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001318}
Wang Nan9d759a92015-11-27 08:47:35 +00001319
Wang Nan5f44e4c82016-07-13 10:44:01 +00001320static void bpf_program__set_type(struct bpf_program *prog,
1321 enum bpf_prog_type type)
1322{
1323 prog->type = type;
1324}
1325
1326int bpf_program__set_tracepoint(struct bpf_program *prog)
1327{
1328 if (!prog)
1329 return -EINVAL;
1330 bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
1331 return 0;
1332}
1333
1334int bpf_program__set_kprobe(struct bpf_program *prog)
1335{
1336 if (!prog)
1337 return -EINVAL;
1338 bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
1339 return 0;
1340}
1341
1342static bool bpf_program__is_type(struct bpf_program *prog,
1343 enum bpf_prog_type type)
1344{
1345 return prog ? (prog->type == type) : false;
1346}
1347
1348bool bpf_program__is_tracepoint(struct bpf_program *prog)
1349{
1350 return bpf_program__is_type(prog, BPF_PROG_TYPE_TRACEPOINT);
1351}
1352
1353bool bpf_program__is_kprobe(struct bpf_program *prog)
1354{
1355 return bpf_program__is_type(prog, BPF_PROG_TYPE_KPROBE);
1356}
1357
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001358int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001359{
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001360 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00001361}
1362
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001363const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001364{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001365 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001366}
1367
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001368const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00001369{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001370 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001371}
1372
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001373int bpf_map__set_priv(struct bpf_map *map, void *priv,
1374 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00001375{
1376 if (!map)
1377 return -EINVAL;
1378
1379 if (map->priv) {
1380 if (map->clear_priv)
1381 map->clear_priv(map, map->priv);
1382 }
1383
1384 map->priv = priv;
1385 map->clear_priv = clear_priv;
1386 return 0;
1387}
1388
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001389void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001390{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001391 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001392}
1393
1394struct bpf_map *
1395bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1396{
1397 size_t idx;
1398 struct bpf_map *s, *e;
1399
1400 if (!obj || !obj->maps)
1401 return NULL;
1402
1403 s = obj->maps;
1404 e = obj->maps + obj->nr_maps;
1405
1406 if (prev == NULL)
1407 return s;
1408
1409 if ((prev < s) || (prev >= e)) {
1410 pr_warning("error in %s: map handler doesn't belong to object\n",
1411 __func__);
1412 return NULL;
1413 }
1414
1415 idx = (prev - obj->maps) + 1;
1416 if (idx >= obj->nr_maps)
1417 return NULL;
1418 return &obj->maps[idx];
1419}
Wang Nan561bbcc2015-11-27 08:47:36 +00001420
1421struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001422bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00001423{
1424 struct bpf_map *pos;
1425
1426 bpf_map__for_each(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00001427 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00001428 return pos;
1429 }
1430 return NULL;
1431}