blob: 371cb40a23046f6337294d870c25797f71d2a355 [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
Wang Nan9b161372016-07-18 06:01:08 +000040#ifndef EM_BPF
41#define EM_BPF 247
42#endif
43
Wang Nanb3f59d62015-07-01 02:13:52 +000044#define __printf(a, b) __attribute__((format(printf, a, b)))
45
46__printf(1, 2)
47static int __base_pr(const char *format, ...)
48{
49 va_list args;
50 int err;
51
52 va_start(args, format);
53 err = vfprintf(stderr, format, args);
54 va_end(args);
55 return err;
56}
57
58static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
59static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
60static __printf(1, 2) libbpf_print_fn_t __pr_debug;
61
62#define __pr(func, fmt, ...) \
63do { \
64 if ((func)) \
65 (func)("libbpf: " fmt, ##__VA_ARGS__); \
66} while (0)
67
68#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
69#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
70#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
71
72void libbpf_set_print(libbpf_print_fn_t warn,
73 libbpf_print_fn_t info,
74 libbpf_print_fn_t debug)
75{
76 __pr_warning = warn;
77 __pr_info = info;
78 __pr_debug = debug;
79}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000080
Wang Nan6371ca3b2015-11-06 13:49:37 +000081#define STRERR_BUFSIZE 128
82
83#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
84#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
85#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
86
87static const char *libbpf_strerror_table[NR_ERRNO] = {
88 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
89 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
90 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
Colin Ian Kingde8a63b2016-06-28 13:23:37 +010091 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
Wang Nan6371ca3b2015-11-06 13:49:37 +000092 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
93 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
94 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
95 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
96 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
Wang Nan705fa212016-07-13 10:44:02 +000097 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
Wang Nan6371ca3b2015-11-06 13:49:37 +000098};
99
100int libbpf_strerror(int err, char *buf, size_t size)
101{
102 if (!buf || !size)
103 return -1;
104
105 err = err > 0 ? err : -err;
106
107 if (err < __LIBBPF_ERRNO__START) {
108 int ret;
109
110 ret = strerror_r(err, buf, size);
111 buf[size - 1] = '\0';
112 return ret;
113 }
114
115 if (err < __LIBBPF_ERRNO__END) {
116 const char *msg;
117
118 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
119 snprintf(buf, size, "%s", msg);
120 buf[size - 1] = '\0';
121 return 0;
122 }
123
124 snprintf(buf, size, "Unknown libbpf error %d", err);
125 buf[size - 1] = '\0';
126 return -1;
127}
128
129#define CHECK_ERR(action, err, out) do { \
130 err = action; \
131 if (err) \
132 goto out; \
133} while(0)
134
135
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000136/* Copied from tools/perf/util/util.h */
137#ifndef zfree
138# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
139#endif
140
141#ifndef zclose
142# define zclose(fd) ({ \
143 int ___err = 0; \
144 if ((fd) >= 0) \
145 ___err = close((fd)); \
146 fd = -1; \
147 ___err; })
148#endif
149
150#ifdef HAVE_LIBELF_MMAP_SUPPORT
151# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
152#else
153# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
154#endif
155
Wang Nana5b8bd42015-07-01 02:14:00 +0000156/*
157 * bpf_prog should be a better name but it has been used in
158 * linux/filter.h.
159 */
160struct bpf_program {
161 /* Index in elf obj file, for relocation use. */
162 int idx;
163 char *section_name;
164 struct bpf_insn *insns;
165 size_t insns_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000166 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000167
168 struct {
169 int insn_idx;
170 int map_idx;
171 } *reloc_desc;
172 int nr_reloc;
Wang Nan55cffde2015-07-01 02:14:07 +0000173
Wang Nanb5805632015-11-16 12:10:09 +0000174 struct {
175 int nr;
176 int *fds;
177 } instances;
178 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000179
180 struct bpf_object *obj;
181 void *priv;
182 bpf_program_clear_priv_t clear_priv;
Wang Nana5b8bd42015-07-01 02:14:00 +0000183};
184
Wang Nan9d759a92015-11-27 08:47:35 +0000185struct bpf_map {
186 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000187 char *name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000188 size_t offset;
Wang Nan9d759a92015-11-27 08:47:35 +0000189 struct bpf_map_def def;
190 void *priv;
191 bpf_map_clear_priv_t clear_priv;
192};
193
Wang Nan9a208ef2015-07-01 02:14:10 +0000194static LIST_HEAD(bpf_objects_list);
195
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000196struct bpf_object {
Wang Nancb1e5e92015-07-01 02:13:57 +0000197 char license[64];
198 u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000199
Wang Nana5b8bd42015-07-01 02:14:00 +0000200 struct bpf_program *programs;
201 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000202 struct bpf_map *maps;
203 size_t nr_maps;
204
Wang Nan52d33522015-07-01 02:14:04 +0000205 bool loaded;
Wang Nana5b8bd42015-07-01 02:14:00 +0000206
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000207 /*
208 * Information when doing elf related work. Only valid if fd
209 * is valid.
210 */
211 struct {
212 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000213 void *obj_buf;
214 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000215 Elf *elf;
216 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000217 Elf_Data *symbols;
Wang Nan77ba9a52015-12-08 02:25:30 +0000218 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000219 struct {
220 GElf_Shdr shdr;
221 Elf_Data *data;
222 } *reloc;
223 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000224 int maps_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000225 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000226 /*
227 * All loaded bpf_object is linked in a list, which is
228 * hidden to caller. bpf_objects__<func> handlers deal with
229 * all objects.
230 */
231 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000232
233 void *priv;
234 bpf_object_clear_priv_t clear_priv;
235
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000236 char path[];
237};
238#define obj_elf_valid(o) ((o)->efile.elf)
239
Wang Nan55cffde2015-07-01 02:14:07 +0000240static void bpf_program__unload(struct bpf_program *prog)
241{
Wang Nanb5805632015-11-16 12:10:09 +0000242 int i;
243
Wang Nan55cffde2015-07-01 02:14:07 +0000244 if (!prog)
245 return;
246
Wang Nanb5805632015-11-16 12:10:09 +0000247 /*
248 * If the object is opened but the program was never loaded,
249 * it is possible that prog->instances.nr == -1.
250 */
251 if (prog->instances.nr > 0) {
252 for (i = 0; i < prog->instances.nr; i++)
253 zclose(prog->instances.fds[i]);
254 } else if (prog->instances.nr != -1) {
255 pr_warning("Internal error: instances.nr is %d\n",
256 prog->instances.nr);
257 }
258
259 prog->instances.nr = -1;
260 zfree(&prog->instances.fds);
Wang Nan55cffde2015-07-01 02:14:07 +0000261}
262
Wang Nana5b8bd42015-07-01 02:14:00 +0000263static void bpf_program__exit(struct bpf_program *prog)
264{
265 if (!prog)
266 return;
267
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000268 if (prog->clear_priv)
269 prog->clear_priv(prog, prog->priv);
270
271 prog->priv = NULL;
272 prog->clear_priv = NULL;
273
Wang Nan55cffde2015-07-01 02:14:07 +0000274 bpf_program__unload(prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000275 zfree(&prog->section_name);
276 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000277 zfree(&prog->reloc_desc);
278
279 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000280 prog->insns_cnt = 0;
281 prog->idx = -1;
282}
283
284static int
285bpf_program__init(void *data, size_t size, char *name, int idx,
286 struct bpf_program *prog)
287{
288 if (size < sizeof(struct bpf_insn)) {
289 pr_warning("corrupted section '%s'\n", name);
290 return -EINVAL;
291 }
292
293 bzero(prog, sizeof(*prog));
294
295 prog->section_name = strdup(name);
296 if (!prog->section_name) {
297 pr_warning("failed to alloc name for prog %s\n",
298 name);
299 goto errout;
300 }
301
302 prog->insns = malloc(size);
303 if (!prog->insns) {
304 pr_warning("failed to alloc insns for %s\n", name);
305 goto errout;
306 }
307 prog->insns_cnt = size / sizeof(struct bpf_insn);
308 memcpy(prog->insns, data,
309 prog->insns_cnt * sizeof(struct bpf_insn));
310 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000311 prog->instances.fds = NULL;
312 prog->instances.nr = -1;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000313 prog->type = BPF_PROG_TYPE_KPROBE;
Wang Nana5b8bd42015-07-01 02:14:00 +0000314
315 return 0;
316errout:
317 bpf_program__exit(prog);
318 return -ENOMEM;
319}
320
321static int
322bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
323 char *name, int idx)
324{
325 struct bpf_program prog, *progs;
326 int nr_progs, err;
327
328 err = bpf_program__init(data, size, name, idx, &prog);
329 if (err)
330 return err;
331
332 progs = obj->programs;
333 nr_progs = obj->nr_programs;
334
335 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
336 if (!progs) {
337 /*
338 * In this case the original obj->programs
339 * is still valid, so don't need special treat for
340 * bpf_close_object().
341 */
342 pr_warning("failed to alloc a new program '%s'\n",
343 name);
344 bpf_program__exit(&prog);
345 return -ENOMEM;
346 }
347
348 pr_debug("found program %s\n", prog.section_name);
349 obj->programs = progs;
350 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000351 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000352 progs[nr_progs] = prog;
353 return 0;
354}
355
Wang Nan6c956392015-07-01 02:13:54 +0000356static struct bpf_object *bpf_object__new(const char *path,
357 void *obj_buf,
358 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000359{
360 struct bpf_object *obj;
361
362 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
363 if (!obj) {
364 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000365 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000366 }
367
368 strcpy(obj->path, path);
369 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000370
371 /*
372 * Caller of this function should also calls
373 * bpf_object__elf_finish() after data collection to return
374 * obj_buf to user. If not, we should duplicate the buffer to
375 * avoid user freeing them before elf finish.
376 */
377 obj->efile.obj_buf = obj_buf;
378 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000379 obj->efile.maps_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000380
Wang Nan52d33522015-07-01 02:14:04 +0000381 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000382
383 INIT_LIST_HEAD(&obj->list);
384 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000385 return obj;
386}
387
388static void bpf_object__elf_finish(struct bpf_object *obj)
389{
390 if (!obj_elf_valid(obj))
391 return;
392
393 if (obj->efile.elf) {
394 elf_end(obj->efile.elf);
395 obj->efile.elf = NULL;
396 }
Wang Nanbec7d682015-07-01 02:13:59 +0000397 obj->efile.symbols = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000398
399 zfree(&obj->efile.reloc);
400 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000401 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000402 obj->efile.obj_buf = NULL;
403 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000404}
405
406static int bpf_object__elf_init(struct bpf_object *obj)
407{
408 int err = 0;
409 GElf_Ehdr *ep;
410
411 if (obj_elf_valid(obj)) {
412 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000413 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000414 }
415
Wang Nan6c956392015-07-01 02:13:54 +0000416 if (obj->efile.obj_buf_sz > 0) {
417 /*
418 * obj_buf should have been validated by
419 * bpf_object__open_buffer().
420 */
421 obj->efile.elf = elf_memory(obj->efile.obj_buf,
422 obj->efile.obj_buf_sz);
423 } else {
424 obj->efile.fd = open(obj->path, O_RDONLY);
425 if (obj->efile.fd < 0) {
426 pr_warning("failed to open %s: %s\n", obj->path,
427 strerror(errno));
428 return -errno;
429 }
430
431 obj->efile.elf = elf_begin(obj->efile.fd,
432 LIBBPF_ELF_C_READ_MMAP,
433 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000434 }
435
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000436 if (!obj->efile.elf) {
437 pr_warning("failed to open %s as ELF file\n",
438 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000439 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000440 goto errout;
441 }
442
443 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
444 pr_warning("failed to get EHDR from %s\n",
445 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000446 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000447 goto errout;
448 }
449 ep = &obj->efile.ehdr;
450
Wang Nan9b161372016-07-18 06:01:08 +0000451 /* Old LLVM set e_machine to EM_NONE */
452 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000453 pr_warning("%s is not an eBPF object file\n",
454 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000455 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000456 goto errout;
457 }
458
459 return 0;
460errout:
461 bpf_object__elf_finish(obj);
462 return err;
463}
464
Wang Nancc4228d2015-07-01 02:13:55 +0000465static int
466bpf_object__check_endianness(struct bpf_object *obj)
467{
468 static unsigned int const endian = 1;
469
470 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
471 case ELFDATA2LSB:
472 /* We are big endian, BPF obj is little endian. */
473 if (*(unsigned char const *)&endian != 1)
474 goto mismatch;
475 break;
476
477 case ELFDATA2MSB:
478 /* We are little endian, BPF obj is big endian. */
479 if (*(unsigned char const *)&endian != 0)
480 goto mismatch;
481 break;
482 default:
Wang Nan6371ca3b2015-11-06 13:49:37 +0000483 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000484 }
485
486 return 0;
487
488mismatch:
489 pr_warning("Error: endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000490 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000491}
492
Wang Nancb1e5e92015-07-01 02:13:57 +0000493static int
494bpf_object__init_license(struct bpf_object *obj,
495 void *data, size_t size)
496{
497 memcpy(obj->license, data,
498 min(size, sizeof(obj->license) - 1));
499 pr_debug("license of %s is %s\n", obj->path, obj->license);
500 return 0;
501}
502
503static int
504bpf_object__init_kversion(struct bpf_object *obj,
505 void *data, size_t size)
506{
507 u32 kver;
508
509 if (size != sizeof(kver)) {
510 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000511 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000512 }
513 memcpy(&kver, data, sizeof(kver));
514 obj->kern_version = kver;
515 pr_debug("kernel version of %s is %x\n", obj->path,
516 obj->kern_version);
517 return 0;
518}
519
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000520static int
Eric Leblond4708bbd2016-11-15 04:05:47 +0000521bpf_object__validate_maps(struct bpf_object *obj)
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000522{
Wang Nan9d759a92015-11-27 08:47:35 +0000523 int i;
524
Eric Leblond4708bbd2016-11-15 04:05:47 +0000525 /*
526 * If there's only 1 map, the only error case should have been
527 * catched in bpf_object__init_maps().
528 */
529 if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000530 return 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000531
532 for (i = 1; i < obj->nr_maps; i++) {
533 const struct bpf_map *a = &obj->maps[i - 1];
534 const struct bpf_map *b = &obj->maps[i];
535
536 if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
537 pr_warning("corrupted map section in %s: map \"%s\" too small\n",
538 obj->path, a->name);
539 return -EINVAL;
540 }
541 }
542 return 0;
543}
544
545static int compare_bpf_map(const void *_a, const void *_b)
546{
547 const struct bpf_map *a = _a;
548 const struct bpf_map *b = _b;
549
550 return a->offset - b->offset;
551}
552
553static int
554bpf_object__init_maps(struct bpf_object *obj)
555{
556 int i, map_idx, nr_maps = 0;
557 Elf_Scn *scn;
558 Elf_Data *data;
559 Elf_Data *symbols = obj->efile.symbols;
560
561 if (obj->efile.maps_shndx < 0)
562 return -EINVAL;
563 if (!symbols)
564 return -EINVAL;
565
566 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
567 if (scn)
568 data = elf_getdata(scn, NULL);
569 if (!scn || !data) {
570 pr_warning("failed to get Elf_Data from map section %d\n",
571 obj->efile.maps_shndx);
572 return -EINVAL;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000573 }
574
Eric Leblond4708bbd2016-11-15 04:05:47 +0000575 /*
576 * Count number of maps. Each map has a name.
577 * Array of maps is not supported: only the first element is
578 * considered.
579 *
580 * TODO: Detect array of map and report error.
581 */
582 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
583 GElf_Sym sym;
584
585 if (!gelf_getsym(symbols, i, &sym))
586 continue;
587 if (sym.st_shndx != obj->efile.maps_shndx)
588 continue;
589 nr_maps++;
590 }
591
592 /* Alloc obj->maps and fill nr_maps. */
593 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
594 nr_maps, data->d_size);
595
596 if (!nr_maps)
597 return 0;
Wang Nan9d759a92015-11-27 08:47:35 +0000598
599 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
600 if (!obj->maps) {
601 pr_warning("alloc maps for object failed\n");
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000602 return -ENOMEM;
603 }
Wang Nan9d759a92015-11-27 08:47:35 +0000604 obj->nr_maps = nr_maps;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000605
Eric Leblond4708bbd2016-11-15 04:05:47 +0000606 /*
607 * fill all fd with -1 so won't close incorrect
608 * fd (fd=0 is stdin) when failure (zclose won't close
609 * negative fd)).
610 */
611 for (i = 0; i < nr_maps; i++)
Wang Nan9d759a92015-11-27 08:47:35 +0000612 obj->maps[i].fd = -1;
613
Eric Leblond4708bbd2016-11-15 04:05:47 +0000614 /*
615 * Fill obj->maps using data in "maps" section.
616 */
617 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +0000618 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +0000619 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000620 struct bpf_map_def *def;
Wang Nan561bbcc2015-11-27 08:47:36 +0000621
622 if (!gelf_getsym(symbols, i, &sym))
623 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000624 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000625 continue;
626
627 map_name = elf_strptr(obj->efile.elf,
Wang Nan77ba9a52015-12-08 02:25:30 +0000628 obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000629 sym.st_name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000630 obj->maps[map_idx].offset = sym.st_value;
631 if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
632 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
633 obj->path, map_name);
634 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000635 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000636
Wang Nan561bbcc2015-11-27 08:47:36 +0000637 obj->maps[map_idx].name = strdup(map_name);
Wang Nan973170e2015-12-08 02:25:29 +0000638 if (!obj->maps[map_idx].name) {
639 pr_warning("failed to alloc map name\n");
640 return -ENOMEM;
641 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000642 pr_debug("map %d is \"%s\"\n", map_idx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000643 obj->maps[map_idx].name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000644 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
645 obj->maps[map_idx].def = *def;
646 map_idx++;
Wang Nan561bbcc2015-11-27 08:47:36 +0000647 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000648
649 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
650 return bpf_object__validate_maps(obj);
Wang Nan561bbcc2015-11-27 08:47:36 +0000651}
652
Wang Nan29603662015-07-01 02:13:56 +0000653static int bpf_object__elf_collect(struct bpf_object *obj)
654{
655 Elf *elf = obj->efile.elf;
656 GElf_Ehdr *ep = &obj->efile.ehdr;
657 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +0000658 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +0000659
660 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
661 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
662 pr_warning("failed to get e_shstrndx from %s\n",
663 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000664 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000665 }
666
667 while ((scn = elf_nextscn(elf, scn)) != NULL) {
668 char *name;
669 GElf_Shdr sh;
670 Elf_Data *data;
671
672 idx++;
673 if (gelf_getshdr(scn, &sh) != &sh) {
674 pr_warning("failed to get section header from %s\n",
675 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000676 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000677 goto out;
678 }
679
680 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
681 if (!name) {
682 pr_warning("failed to get section name from %s\n",
683 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000684 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000685 goto out;
686 }
687
688 data = elf_getdata(scn, 0);
689 if (!data) {
690 pr_warning("failed to get section data from %s(%s)\n",
691 name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000692 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000693 goto out;
694 }
695 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
696 name, (unsigned long)data->d_size,
697 (int)sh.sh_link, (unsigned long)sh.sh_flags,
698 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +0000699
700 if (strcmp(name, "license") == 0)
701 err = bpf_object__init_license(obj,
702 data->d_buf,
703 data->d_size);
704 else if (strcmp(name, "version") == 0)
705 err = bpf_object__init_kversion(obj,
706 data->d_buf,
707 data->d_size);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000708 else if (strcmp(name, "maps") == 0)
Wang Nan666810e2016-01-25 09:55:49 +0000709 obj->efile.maps_shndx = idx;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000710 else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +0000711 if (obj->efile.symbols) {
712 pr_warning("bpf: multiple SYMTAB in %s\n",
713 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000714 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +0000715 } else {
Wang Nanbec7d682015-07-01 02:13:59 +0000716 obj->efile.symbols = data;
Wang Nan77ba9a52015-12-08 02:25:30 +0000717 obj->efile.strtabidx = sh.sh_link;
718 }
Wang Nana5b8bd42015-07-01 02:14:00 +0000719 } else if ((sh.sh_type == SHT_PROGBITS) &&
720 (sh.sh_flags & SHF_EXECINSTR) &&
721 (data->d_size > 0)) {
722 err = bpf_object__add_program(obj, data->d_buf,
723 data->d_size, name, idx);
724 if (err) {
Wang Nan6371ca3b2015-11-06 13:49:37 +0000725 char errmsg[STRERR_BUFSIZE];
726
Wang Nana5b8bd42015-07-01 02:14:00 +0000727 strerror_r(-err, errmsg, sizeof(errmsg));
728 pr_warning("failed to alloc program %s (%s): %s",
729 name, obj->path, errmsg);
730 }
Wang Nanb62f06e2015-07-01 02:14:01 +0000731 } else if (sh.sh_type == SHT_REL) {
732 void *reloc = obj->efile.reloc;
733 int nr_reloc = obj->efile.nr_reloc + 1;
734
735 reloc = realloc(reloc,
736 sizeof(*obj->efile.reloc) * nr_reloc);
737 if (!reloc) {
738 pr_warning("realloc failed\n");
739 err = -ENOMEM;
740 } else {
741 int n = nr_reloc - 1;
742
743 obj->efile.reloc = reloc;
744 obj->efile.nr_reloc = nr_reloc;
745
746 obj->efile.reloc[n].shdr = sh;
747 obj->efile.reloc[n].data = data;
748 }
Wang Nanbec7d682015-07-01 02:13:59 +0000749 }
Wang Nancb1e5e92015-07-01 02:13:57 +0000750 if (err)
751 goto out;
Wang Nan29603662015-07-01 02:13:56 +0000752 }
Wang Nan561bbcc2015-11-27 08:47:36 +0000753
Wang Nan77ba9a52015-12-08 02:25:30 +0000754 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
755 pr_warning("Corrupted ELF file: index of strtab invalid\n");
756 return LIBBPF_ERRNO__FORMAT;
757 }
Wang Nan666810e2016-01-25 09:55:49 +0000758 if (obj->efile.maps_shndx >= 0)
Eric Leblond4708bbd2016-11-15 04:05:47 +0000759 err = bpf_object__init_maps(obj);
Wang Nan29603662015-07-01 02:13:56 +0000760out:
761 return err;
762}
763
Wang Nan34090912015-07-01 02:14:02 +0000764static struct bpf_program *
765bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
766{
767 struct bpf_program *prog;
768 size_t i;
769
770 for (i = 0; i < obj->nr_programs; i++) {
771 prog = &obj->programs[i];
772 if (prog->idx == idx)
773 return prog;
774 }
775 return NULL;
776}
777
778static int
779bpf_program__collect_reloc(struct bpf_program *prog,
780 size_t nr_maps, GElf_Shdr *shdr,
Wang Nan666810e2016-01-25 09:55:49 +0000781 Elf_Data *data, Elf_Data *symbols,
Joe Stringer94e5ade2017-01-22 17:11:22 -0800782 int maps_shndx, struct bpf_map *maps)
Wang Nan34090912015-07-01 02:14:02 +0000783{
784 int i, nrels;
785
786 pr_debug("collecting relocating info for: '%s'\n",
787 prog->section_name);
788 nrels = shdr->sh_size / shdr->sh_entsize;
789
790 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
791 if (!prog->reloc_desc) {
792 pr_warning("failed to alloc memory in relocation\n");
793 return -ENOMEM;
794 }
795 prog->nr_reloc = nrels;
796
797 for (i = 0; i < nrels; i++) {
798 GElf_Sym sym;
799 GElf_Rel rel;
800 unsigned int insn_idx;
801 struct bpf_insn *insns = prog->insns;
802 size_t map_idx;
803
804 if (!gelf_getrel(data, i, &rel)) {
805 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000806 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000807 }
808
Wang Nan34090912015-07-01 02:14:02 +0000809 if (!gelf_getsym(symbols,
810 GELF_R_SYM(rel.r_info),
811 &sym)) {
812 pr_warning("relocation: symbol %"PRIx64" not found\n",
813 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +0000814 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000815 }
816
Wang Nan666810e2016-01-25 09:55:49 +0000817 if (sym.st_shndx != maps_shndx) {
818 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
819 prog->section_name, sym.st_shndx);
820 return -LIBBPF_ERRNO__RELOC;
821 }
822
823 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
824 pr_debug("relocation: insn_idx=%u\n", insn_idx);
825
Wang Nan34090912015-07-01 02:14:02 +0000826 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
827 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
828 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000829 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000830 }
831
Joe Stringer94e5ade2017-01-22 17:11:22 -0800832 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
833 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
834 if (maps[map_idx].offset == sym.st_value) {
835 pr_debug("relocation: find map %zd (%s) for insn %u\n",
836 map_idx, maps[map_idx].name, insn_idx);
837 break;
838 }
839 }
840
Wang Nan34090912015-07-01 02:14:02 +0000841 if (map_idx >= nr_maps) {
842 pr_warning("bpf relocation: map_idx %d large than %d\n",
843 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000844 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000845 }
846
847 prog->reloc_desc[i].insn_idx = insn_idx;
848 prog->reloc_desc[i].map_idx = map_idx;
849 }
850 return 0;
851}
852
Wang Nan52d33522015-07-01 02:14:04 +0000853static int
854bpf_object__create_maps(struct bpf_object *obj)
855{
856 unsigned int i;
Wang Nan52d33522015-07-01 02:14:04 +0000857
Wang Nan9d759a92015-11-27 08:47:35 +0000858 for (i = 0; i < obj->nr_maps; i++) {
859 struct bpf_map_def *def = &obj->maps[i].def;
860 int *pfd = &obj->maps[i].fd;
Wang Nan52d33522015-07-01 02:14:04 +0000861
Wang Nan9d759a92015-11-27 08:47:35 +0000862 *pfd = bpf_create_map(def->type,
863 def->key_size,
864 def->value_size,
Joe Stringera5580c72016-12-08 18:46:16 -0800865 def->max_entries,
866 0);
Wang Nan52d33522015-07-01 02:14:04 +0000867 if (*pfd < 0) {
868 size_t j;
869 int err = *pfd;
870
871 pr_warning("failed to create map: %s\n",
872 strerror(errno));
873 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +0000874 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +0000875 return err;
876 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000877 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +0000878 }
879
Wang Nan52d33522015-07-01 02:14:04 +0000880 return 0;
881}
882
Wang Nan8a47a6c2015-07-01 02:14:05 +0000883static int
Wang Nan9d759a92015-11-27 08:47:35 +0000884bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +0000885{
886 int i;
887
888 if (!prog || !prog->reloc_desc)
889 return 0;
890
891 for (i = 0; i < prog->nr_reloc; i++) {
892 int insn_idx, map_idx;
893 struct bpf_insn *insns = prog->insns;
894
895 insn_idx = prog->reloc_desc[i].insn_idx;
896 map_idx = prog->reloc_desc[i].map_idx;
897
898 if (insn_idx >= (int)prog->insns_cnt) {
899 pr_warning("relocation out of range: '%s'\n",
900 prog->section_name);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000901 return -LIBBPF_ERRNO__RELOC;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000902 }
903 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Wang Nan9d759a92015-11-27 08:47:35 +0000904 insns[insn_idx].imm = obj->maps[map_idx].fd;
Wang Nan8a47a6c2015-07-01 02:14:05 +0000905 }
906
907 zfree(&prog->reloc_desc);
908 prog->nr_reloc = 0;
909 return 0;
910}
911
912
913static int
914bpf_object__relocate(struct bpf_object *obj)
915{
916 struct bpf_program *prog;
917 size_t i;
918 int err;
919
920 for (i = 0; i < obj->nr_programs; i++) {
921 prog = &obj->programs[i];
922
Wang Nan9d759a92015-11-27 08:47:35 +0000923 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +0000924 if (err) {
925 pr_warning("failed to relocate '%s'\n",
926 prog->section_name);
927 return err;
928 }
929 }
930 return 0;
931}
932
Wang Nan34090912015-07-01 02:14:02 +0000933static int bpf_object__collect_reloc(struct bpf_object *obj)
934{
935 int i, err;
936
937 if (!obj_elf_valid(obj)) {
938 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000939 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000940 }
941
942 for (i = 0; i < obj->efile.nr_reloc; i++) {
943 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
944 Elf_Data *data = obj->efile.reloc[i].data;
945 int idx = shdr->sh_info;
946 struct bpf_program *prog;
Wang Nan9d759a92015-11-27 08:47:35 +0000947 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +0000948
949 if (shdr->sh_type != SHT_REL) {
950 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000951 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +0000952 }
953
954 prog = bpf_object__find_prog_by_idx(obj, idx);
955 if (!prog) {
956 pr_warning("relocation failed: no %d section\n",
957 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000958 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000959 }
960
961 err = bpf_program__collect_reloc(prog, nr_maps,
962 shdr, data,
Wang Nan666810e2016-01-25 09:55:49 +0000963 obj->efile.symbols,
Joe Stringer94e5ade2017-01-22 17:11:22 -0800964 obj->efile.maps_shndx,
965 obj->maps);
Wang Nan34090912015-07-01 02:14:02 +0000966 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +0000967 return err;
Wang Nan34090912015-07-01 02:14:02 +0000968 }
969 return 0;
970}
971
Wang Nan55cffde2015-07-01 02:14:07 +0000972static int
Wang Nan5f44e4c82016-07-13 10:44:01 +0000973load_program(enum bpf_prog_type type, struct bpf_insn *insns,
974 int insns_cnt, char *license, u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +0000975{
976 int ret;
977 char *log_buf;
978
979 if (!insns || !insns_cnt)
980 return -EINVAL;
981
982 log_buf = malloc(BPF_LOG_BUF_SIZE);
983 if (!log_buf)
984 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
985
Wang Nan5f44e4c82016-07-13 10:44:01 +0000986 ret = bpf_load_program(type, insns, insns_cnt, license,
987 kern_version, log_buf, BPF_LOG_BUF_SIZE);
Wang Nan55cffde2015-07-01 02:14:07 +0000988
989 if (ret >= 0) {
990 *pfd = ret;
991 ret = 0;
992 goto out;
993 }
994
Wang Nan6371ca3b2015-11-06 13:49:37 +0000995 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +0000996 pr_warning("load bpf program failed: %s\n", strerror(errno));
997
Wang Nan6371ca3b2015-11-06 13:49:37 +0000998 if (log_buf && log_buf[0] != '\0') {
999 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00001000 pr_warning("-- BEGIN DUMP LOG ---\n");
1001 pr_warning("\n%s\n", log_buf);
1002 pr_warning("-- END LOG --\n");
Wang Nan705fa212016-07-13 10:44:02 +00001003 } else if (insns_cnt >= BPF_MAXINSNS) {
1004 pr_warning("Program too large (%d insns), at most %d insns\n",
1005 insns_cnt, BPF_MAXINSNS);
1006 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001007 } else {
Wang Nan705fa212016-07-13 10:44:02 +00001008 /* Wrong program type? */
1009 if (type != BPF_PROG_TYPE_KPROBE) {
1010 int fd;
1011
1012 fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
1013 insns_cnt, license, kern_version,
1014 NULL, 0);
1015 if (fd >= 0) {
1016 close(fd);
1017 ret = -LIBBPF_ERRNO__PROGTYPE;
1018 goto out;
1019 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00001020 }
Wang Nan705fa212016-07-13 10:44:02 +00001021
1022 if (log_buf)
1023 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00001024 }
1025
1026out:
1027 free(log_buf);
1028 return ret;
1029}
1030
1031static int
1032bpf_program__load(struct bpf_program *prog,
1033 char *license, u32 kern_version)
1034{
Wang Nanb5805632015-11-16 12:10:09 +00001035 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00001036
Wang Nanb5805632015-11-16 12:10:09 +00001037 if (prog->instances.nr < 0 || !prog->instances.fds) {
1038 if (prog->preprocessor) {
1039 pr_warning("Internal error: can't load program '%s'\n",
1040 prog->section_name);
1041 return -LIBBPF_ERRNO__INTERNAL;
1042 }
Wang Nan55cffde2015-07-01 02:14:07 +00001043
Wang Nanb5805632015-11-16 12:10:09 +00001044 prog->instances.fds = malloc(sizeof(int));
1045 if (!prog->instances.fds) {
1046 pr_warning("Not enough memory for BPF fds\n");
1047 return -ENOMEM;
1048 }
1049 prog->instances.nr = 1;
1050 prog->instances.fds[0] = -1;
1051 }
1052
1053 if (!prog->preprocessor) {
1054 if (prog->instances.nr != 1) {
1055 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1056 prog->section_name, prog->instances.nr);
1057 }
Wang Nan5f44e4c82016-07-13 10:44:01 +00001058 err = load_program(prog->type, prog->insns, prog->insns_cnt,
Wang Nanb5805632015-11-16 12:10:09 +00001059 license, kern_version, &fd);
1060 if (!err)
1061 prog->instances.fds[0] = fd;
1062 goto out;
1063 }
1064
1065 for (i = 0; i < prog->instances.nr; i++) {
1066 struct bpf_prog_prep_result result;
1067 bpf_program_prep_t preprocessor = prog->preprocessor;
1068
1069 bzero(&result, sizeof(result));
1070 err = preprocessor(prog, i, prog->insns,
1071 prog->insns_cnt, &result);
1072 if (err) {
1073 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1074 i, prog->section_name);
1075 goto out;
1076 }
1077
1078 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1079 pr_debug("Skip loading the %dth instance of program '%s'\n",
1080 i, prog->section_name);
1081 prog->instances.fds[i] = -1;
1082 if (result.pfd)
1083 *result.pfd = -1;
1084 continue;
1085 }
1086
Wang Nan5f44e4c82016-07-13 10:44:01 +00001087 err = load_program(prog->type, result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00001088 result.new_insn_cnt,
1089 license, kern_version, &fd);
1090
1091 if (err) {
1092 pr_warning("Loading the %dth instance of program '%s' failed\n",
1093 i, prog->section_name);
1094 goto out;
1095 }
1096
1097 if (result.pfd)
1098 *result.pfd = fd;
1099 prog->instances.fds[i] = fd;
1100 }
1101out:
Wang Nan55cffde2015-07-01 02:14:07 +00001102 if (err)
1103 pr_warning("failed to load program '%s'\n",
1104 prog->section_name);
1105 zfree(&prog->insns);
1106 prog->insns_cnt = 0;
1107 return err;
1108}
1109
1110static int
1111bpf_object__load_progs(struct bpf_object *obj)
1112{
1113 size_t i;
1114 int err;
1115
1116 for (i = 0; i < obj->nr_programs; i++) {
1117 err = bpf_program__load(&obj->programs[i],
1118 obj->license,
1119 obj->kern_version);
1120 if (err)
1121 return err;
1122 }
1123 return 0;
1124}
1125
Wang Nancb1e5e92015-07-01 02:13:57 +00001126static int bpf_object__validate(struct bpf_object *obj)
1127{
1128 if (obj->kern_version == 0) {
1129 pr_warning("%s doesn't provide kernel version\n",
1130 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001131 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00001132 }
1133 return 0;
1134}
1135
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001136static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +00001137__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001138{
1139 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001140 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001141
1142 if (elf_version(EV_CURRENT) == EV_NONE) {
1143 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001144 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001145 }
1146
Wang Nan6c956392015-07-01 02:13:54 +00001147 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001148 if (IS_ERR(obj))
1149 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001150
Wang Nan6371ca3b2015-11-06 13:49:37 +00001151 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1152 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1153 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1154 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1155 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001156
1157 bpf_object__elf_finish(obj);
1158 return obj;
1159out:
1160 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001161 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001162}
1163
1164struct bpf_object *bpf_object__open(const char *path)
1165{
1166 /* param validation */
1167 if (!path)
1168 return NULL;
1169
1170 pr_debug("loading %s\n", path);
1171
Wang Nan6c956392015-07-01 02:13:54 +00001172 return __bpf_object__open(path, NULL, 0);
1173}
1174
1175struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001176 size_t obj_buf_sz,
1177 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001178{
Wang Nanacf860a2015-08-27 02:30:55 +00001179 char tmp_name[64];
1180
Wang Nan6c956392015-07-01 02:13:54 +00001181 /* param validation */
1182 if (!obj_buf || obj_buf_sz <= 0)
1183 return NULL;
1184
Wang Nanacf860a2015-08-27 02:30:55 +00001185 if (!name) {
1186 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1187 (unsigned long)obj_buf,
1188 (unsigned long)obj_buf_sz);
1189 tmp_name[sizeof(tmp_name) - 1] = '\0';
1190 name = tmp_name;
1191 }
1192 pr_debug("loading object '%s' from buffer\n",
1193 name);
Wang Nan6c956392015-07-01 02:13:54 +00001194
Wang Nanacf860a2015-08-27 02:30:55 +00001195 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001196}
1197
Wang Nan52d33522015-07-01 02:14:04 +00001198int bpf_object__unload(struct bpf_object *obj)
1199{
1200 size_t i;
1201
1202 if (!obj)
1203 return -EINVAL;
1204
Wang Nan9d759a92015-11-27 08:47:35 +00001205 for (i = 0; i < obj->nr_maps; i++)
1206 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001207
Wang Nan55cffde2015-07-01 02:14:07 +00001208 for (i = 0; i < obj->nr_programs; i++)
1209 bpf_program__unload(&obj->programs[i]);
1210
Wang Nan52d33522015-07-01 02:14:04 +00001211 return 0;
1212}
1213
1214int bpf_object__load(struct bpf_object *obj)
1215{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001216 int err;
1217
Wang Nan52d33522015-07-01 02:14:04 +00001218 if (!obj)
1219 return -EINVAL;
1220
1221 if (obj->loaded) {
1222 pr_warning("object should not be loaded twice\n");
1223 return -EINVAL;
1224 }
1225
1226 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001227
1228 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1229 CHECK_ERR(bpf_object__relocate(obj), err, out);
1230 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001231
1232 return 0;
1233out:
1234 bpf_object__unload(obj);
1235 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001236 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001237}
1238
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001239void bpf_object__close(struct bpf_object *obj)
1240{
Wang Nana5b8bd42015-07-01 02:14:00 +00001241 size_t i;
1242
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001243 if (!obj)
1244 return;
1245
Wang Nan10931d22016-11-26 07:03:26 +00001246 if (obj->clear_priv)
1247 obj->clear_priv(obj, obj->priv);
1248
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001249 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001250 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001251
Wang Nan9d759a92015-11-27 08:47:35 +00001252 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001253 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00001254 if (obj->maps[i].clear_priv)
1255 obj->maps[i].clear_priv(&obj->maps[i],
1256 obj->maps[i].priv);
1257 obj->maps[i].priv = NULL;
1258 obj->maps[i].clear_priv = NULL;
1259 }
1260 zfree(&obj->maps);
1261 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00001262
1263 if (obj->programs && obj->nr_programs) {
1264 for (i = 0; i < obj->nr_programs; i++)
1265 bpf_program__exit(&obj->programs[i]);
1266 }
1267 zfree(&obj->programs);
1268
Wang Nan9a208ef2015-07-01 02:14:10 +00001269 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001270 free(obj);
1271}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001272
Wang Nan9a208ef2015-07-01 02:14:10 +00001273struct bpf_object *
1274bpf_object__next(struct bpf_object *prev)
1275{
1276 struct bpf_object *next;
1277
1278 if (!prev)
1279 next = list_first_entry(&bpf_objects_list,
1280 struct bpf_object,
1281 list);
1282 else
1283 next = list_next_entry(prev, list);
1284
1285 /* Empty list is noticed here so don't need checking on entry. */
1286 if (&next->list == &bpf_objects_list)
1287 return NULL;
1288
1289 return next;
1290}
1291
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001292const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00001293{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001294 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001295}
1296
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001297unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00001298{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001299 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00001300}
1301
Wang Nan10931d22016-11-26 07:03:26 +00001302int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1303 bpf_object_clear_priv_t clear_priv)
1304{
1305 if (obj->priv && obj->clear_priv)
1306 obj->clear_priv(obj, obj->priv);
1307
1308 obj->priv = priv;
1309 obj->clear_priv = clear_priv;
1310 return 0;
1311}
1312
1313void *bpf_object__priv(struct bpf_object *obj)
1314{
1315 return obj ? obj->priv : ERR_PTR(-EINVAL);
1316}
1317
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001318struct bpf_program *
1319bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1320{
1321 size_t idx;
1322
1323 if (!obj->programs)
1324 return NULL;
1325 /* First handler */
1326 if (prev == NULL)
1327 return &obj->programs[0];
1328
1329 if (prev->obj != obj) {
1330 pr_warning("error: program handler doesn't match object\n");
1331 return NULL;
1332 }
1333
1334 idx = (prev - obj->programs) + 1;
1335 if (idx >= obj->nr_programs)
1336 return NULL;
1337 return &obj->programs[idx];
1338}
1339
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001340int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1341 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001342{
1343 if (prog->priv && prog->clear_priv)
1344 prog->clear_priv(prog, prog->priv);
1345
1346 prog->priv = priv;
1347 prog->clear_priv = clear_priv;
1348 return 0;
1349}
1350
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001351void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001352{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001353 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001354}
1355
Namhyung Kim715f8db2015-11-03 20:21:05 +09001356const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001357{
1358 const char *title;
1359
1360 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001361 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001362 title = strdup(title);
1363 if (!title) {
1364 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001365 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001366 }
1367 }
1368
1369 return title;
1370}
1371
1372int bpf_program__fd(struct bpf_program *prog)
1373{
Wang Nanb5805632015-11-16 12:10:09 +00001374 return bpf_program__nth_fd(prog, 0);
1375}
1376
1377int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1378 bpf_program_prep_t prep)
1379{
1380 int *instances_fds;
1381
1382 if (nr_instances <= 0 || !prep)
1383 return -EINVAL;
1384
1385 if (prog->instances.nr > 0 || prog->instances.fds) {
1386 pr_warning("Can't set pre-processor after loading\n");
1387 return -EINVAL;
1388 }
1389
1390 instances_fds = malloc(sizeof(int) * nr_instances);
1391 if (!instances_fds) {
1392 pr_warning("alloc memory failed for fds\n");
1393 return -ENOMEM;
1394 }
1395
1396 /* fill all fd with -1 */
1397 memset(instances_fds, -1, sizeof(int) * nr_instances);
1398
1399 prog->instances.nr = nr_instances;
1400 prog->instances.fds = instances_fds;
1401 prog->preprocessor = prep;
1402 return 0;
1403}
1404
1405int bpf_program__nth_fd(struct bpf_program *prog, int n)
1406{
1407 int fd;
1408
1409 if (n >= prog->instances.nr || n < 0) {
1410 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1411 n, prog->section_name, prog->instances.nr);
1412 return -EINVAL;
1413 }
1414
1415 fd = prog->instances.fds[n];
1416 if (fd < 0) {
1417 pr_warning("%dth instance of program '%s' is invalid\n",
1418 n, prog->section_name);
1419 return -ENOENT;
1420 }
1421
1422 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001423}
Wang Nan9d759a92015-11-27 08:47:35 +00001424
Wang Nan5f44e4c82016-07-13 10:44:01 +00001425static void bpf_program__set_type(struct bpf_program *prog,
1426 enum bpf_prog_type type)
1427{
1428 prog->type = type;
1429}
1430
Wang Nan5f44e4c82016-07-13 10:44:01 +00001431static bool bpf_program__is_type(struct bpf_program *prog,
1432 enum bpf_prog_type type)
1433{
1434 return prog ? (prog->type == type) : false;
1435}
1436
Joe Stringered794072017-01-22 17:11:23 -08001437#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1438int bpf_program__set_##NAME(struct bpf_program *prog) \
1439{ \
1440 if (!prog) \
1441 return -EINVAL; \
1442 bpf_program__set_type(prog, TYPE); \
1443 return 0; \
1444} \
1445 \
1446bool bpf_program__is_##NAME(struct bpf_program *prog) \
1447{ \
1448 return bpf_program__is_type(prog, TYPE); \
1449} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00001450
Joe Stringered794072017-01-22 17:11:23 -08001451BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
1452BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00001453
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001454int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001455{
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001456 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00001457}
1458
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001459const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001460{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001461 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001462}
1463
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001464const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00001465{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001466 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001467}
1468
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001469int bpf_map__set_priv(struct bpf_map *map, void *priv,
1470 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00001471{
1472 if (!map)
1473 return -EINVAL;
1474
1475 if (map->priv) {
1476 if (map->clear_priv)
1477 map->clear_priv(map, map->priv);
1478 }
1479
1480 map->priv = priv;
1481 map->clear_priv = clear_priv;
1482 return 0;
1483}
1484
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001485void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001486{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001487 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001488}
1489
1490struct bpf_map *
1491bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1492{
1493 size_t idx;
1494 struct bpf_map *s, *e;
1495
1496 if (!obj || !obj->maps)
1497 return NULL;
1498
1499 s = obj->maps;
1500 e = obj->maps + obj->nr_maps;
1501
1502 if (prev == NULL)
1503 return s;
1504
1505 if ((prev < s) || (prev >= e)) {
1506 pr_warning("error in %s: map handler doesn't belong to object\n",
1507 __func__);
1508 return NULL;
1509 }
1510
1511 idx = (prev - obj->maps) + 1;
1512 if (idx >= obj->nr_maps)
1513 return NULL;
1514 return &obj->maps[idx];
1515}
Wang Nan561bbcc2015-11-27 08:47:36 +00001516
1517struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001518bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00001519{
1520 struct bpf_map *pos;
1521
1522 bpf_map__for_each(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00001523 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00001524 return pos;
1525 }
1526 return NULL;
1527}
Wang Nan5a6acad2016-11-26 07:03:27 +00001528
1529struct bpf_map *
1530bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1531{
1532 int i;
1533
1534 for (i = 0; i < obj->nr_maps; i++) {
1535 if (obj->maps[i].offset == offset)
1536 return &obj->maps[i];
1537 }
1538 return ERR_PTR(-ENOENT);
1539}