blob: 5b83875b359496bd7735f9d2015de77c3afc0c36 [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.
Joe Stringerf3675402017-01-26 13:19:56 -08007 * Copyright (C) 2017 Nicira, Inc.
Wang Nan203d1ca2016-07-04 11:02:42 +00008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License (not later!)
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nan1b76c132015-07-01 02:13:51 +000021 */
22
23#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000024#include <stdio.h>
25#include <stdarg.h>
Joe Stringerf3675402017-01-26 13:19:56 -080026#include <libgen.h>
Wang Nan34090912015-07-01 02:14:02 +000027#include <inttypes.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000028#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000029#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000030#include <fcntl.h>
31#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000032#include <asm/unistd.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080033#include <linux/err.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000034#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000035#include <linux/bpf.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000036#include <linux/list.h>
Joe Stringerf3675402017-01-26 13:19:56 -080037#include <linux/limits.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/vfs.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000041#include <libelf.h>
42#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000043
44#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000045#include "bpf.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000046
Wang Nan9b161372016-07-18 06:01:08 +000047#ifndef EM_BPF
48#define EM_BPF 247
49#endif
50
Joe Stringerf3675402017-01-26 13:19:56 -080051#ifndef BPF_FS_MAGIC
52#define BPF_FS_MAGIC 0xcafe4a11
53#endif
54
Wang Nanb3f59d62015-07-01 02:13:52 +000055#define __printf(a, b) __attribute__((format(printf, a, b)))
56
57__printf(1, 2)
58static int __base_pr(const char *format, ...)
59{
60 va_list args;
61 int err;
62
63 va_start(args, format);
64 err = vfprintf(stderr, format, args);
65 va_end(args);
66 return err;
67}
68
69static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
70static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
71static __printf(1, 2) libbpf_print_fn_t __pr_debug;
72
73#define __pr(func, fmt, ...) \
74do { \
75 if ((func)) \
76 (func)("libbpf: " fmt, ##__VA_ARGS__); \
77} while (0)
78
79#define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
80#define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
81#define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
82
83void libbpf_set_print(libbpf_print_fn_t warn,
84 libbpf_print_fn_t info,
85 libbpf_print_fn_t debug)
86{
87 __pr_warning = warn;
88 __pr_info = info;
89 __pr_debug = debug;
90}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000091
Wang Nan6371ca3b2015-11-06 13:49:37 +000092#define STRERR_BUFSIZE 128
93
94#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
95#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
96#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
97
98static const char *libbpf_strerror_table[NR_ERRNO] = {
99 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
100 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
101 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
Colin Ian Kingde8a63b2016-06-28 13:23:37 +0100102 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
Wang Nan6371ca3b2015-11-06 13:49:37 +0000103 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
104 [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
105 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
106 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
107 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
Wang Nan705fa212016-07-13 10:44:02 +0000108 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
Wang Nan6371ca3b2015-11-06 13:49:37 +0000109};
110
111int libbpf_strerror(int err, char *buf, size_t size)
112{
113 if (!buf || !size)
114 return -1;
115
116 err = err > 0 ? err : -err;
117
118 if (err < __LIBBPF_ERRNO__START) {
119 int ret;
120
121 ret = strerror_r(err, buf, size);
122 buf[size - 1] = '\0';
123 return ret;
124 }
125
126 if (err < __LIBBPF_ERRNO__END) {
127 const char *msg;
128
129 msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
130 snprintf(buf, size, "%s", msg);
131 buf[size - 1] = '\0';
132 return 0;
133 }
134
135 snprintf(buf, size, "Unknown libbpf error %d", err);
136 buf[size - 1] = '\0';
137 return -1;
138}
139
140#define CHECK_ERR(action, err, out) do { \
141 err = action; \
142 if (err) \
143 goto out; \
144} while(0)
145
146
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000147/* Copied from tools/perf/util/util.h */
148#ifndef zfree
149# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
150#endif
151
152#ifndef zclose
153# define zclose(fd) ({ \
154 int ___err = 0; \
155 if ((fd) >= 0) \
156 ___err = close((fd)); \
157 fd = -1; \
158 ___err; })
159#endif
160
161#ifdef HAVE_LIBELF_MMAP_SUPPORT
162# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
163#else
164# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
165#endif
166
Wang Nana5b8bd42015-07-01 02:14:00 +0000167/*
168 * bpf_prog should be a better name but it has been used in
169 * linux/filter.h.
170 */
171struct bpf_program {
172 /* Index in elf obj file, for relocation use. */
173 int idx;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700174 char *name;
Wang Nana5b8bd42015-07-01 02:14:00 +0000175 char *section_name;
176 struct bpf_insn *insns;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800177 size_t insns_cnt, main_prog_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000178 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000179
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800180 struct reloc_desc {
181 enum {
182 RELO_LD64,
183 RELO_CALL,
184 } type;
Wang Nan34090912015-07-01 02:14:02 +0000185 int insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800186 union {
187 int map_idx;
188 int text_off;
189 };
Wang Nan34090912015-07-01 02:14:02 +0000190 } *reloc_desc;
191 int nr_reloc;
Wang Nan55cffde2015-07-01 02:14:07 +0000192
Wang Nanb5805632015-11-16 12:10:09 +0000193 struct {
194 int nr;
195 int *fds;
196 } instances;
197 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000198
199 struct bpf_object *obj;
200 void *priv;
201 bpf_program_clear_priv_t clear_priv;
Wang Nana5b8bd42015-07-01 02:14:00 +0000202};
203
Wang Nan9d759a92015-11-27 08:47:35 +0000204struct bpf_map {
205 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000206 char *name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000207 size_t offset;
Wang Nan9d759a92015-11-27 08:47:35 +0000208 struct bpf_map_def def;
209 void *priv;
210 bpf_map_clear_priv_t clear_priv;
211};
212
Wang Nan9a208ef2015-07-01 02:14:10 +0000213static LIST_HEAD(bpf_objects_list);
214
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000215struct bpf_object {
Wang Nancb1e5e92015-07-01 02:13:57 +0000216 char license[64];
217 u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000218
Wang Nana5b8bd42015-07-01 02:14:00 +0000219 struct bpf_program *programs;
220 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000221 struct bpf_map *maps;
222 size_t nr_maps;
223
Wang Nan52d33522015-07-01 02:14:04 +0000224 bool loaded;
Wang Nana5b8bd42015-07-01 02:14:00 +0000225
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000226 /*
227 * Information when doing elf related work. Only valid if fd
228 * is valid.
229 */
230 struct {
231 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000232 void *obj_buf;
233 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000234 Elf *elf;
235 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000236 Elf_Data *symbols;
Wang Nan77ba9a52015-12-08 02:25:30 +0000237 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000238 struct {
239 GElf_Shdr shdr;
240 Elf_Data *data;
241 } *reloc;
242 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000243 int maps_shndx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800244 int text_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000245 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000246 /*
247 * All loaded bpf_object is linked in a list, which is
248 * hidden to caller. bpf_objects__<func> handlers deal with
249 * all objects.
250 */
251 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000252
253 void *priv;
254 bpf_object_clear_priv_t clear_priv;
255
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000256 char path[];
257};
258#define obj_elf_valid(o) ((o)->efile.elf)
259
Wang Nan55cffde2015-07-01 02:14:07 +0000260static void bpf_program__unload(struct bpf_program *prog)
261{
Wang Nanb5805632015-11-16 12:10:09 +0000262 int i;
263
Wang Nan55cffde2015-07-01 02:14:07 +0000264 if (!prog)
265 return;
266
Wang Nanb5805632015-11-16 12:10:09 +0000267 /*
268 * If the object is opened but the program was never loaded,
269 * it is possible that prog->instances.nr == -1.
270 */
271 if (prog->instances.nr > 0) {
272 for (i = 0; i < prog->instances.nr; i++)
273 zclose(prog->instances.fds[i]);
274 } else if (prog->instances.nr != -1) {
275 pr_warning("Internal error: instances.nr is %d\n",
276 prog->instances.nr);
277 }
278
279 prog->instances.nr = -1;
280 zfree(&prog->instances.fds);
Wang Nan55cffde2015-07-01 02:14:07 +0000281}
282
Wang Nana5b8bd42015-07-01 02:14:00 +0000283static void bpf_program__exit(struct bpf_program *prog)
284{
285 if (!prog)
286 return;
287
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000288 if (prog->clear_priv)
289 prog->clear_priv(prog, prog->priv);
290
291 prog->priv = NULL;
292 prog->clear_priv = NULL;
293
Wang Nan55cffde2015-07-01 02:14:07 +0000294 bpf_program__unload(prog);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700295 zfree(&prog->name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000296 zfree(&prog->section_name);
297 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000298 zfree(&prog->reloc_desc);
299
300 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000301 prog->insns_cnt = 0;
302 prog->idx = -1;
303}
304
305static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700306bpf_program__init(void *data, size_t size, char *section_name, int idx,
307 struct bpf_program *prog)
Wang Nana5b8bd42015-07-01 02:14:00 +0000308{
309 if (size < sizeof(struct bpf_insn)) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700310 pr_warning("corrupted section '%s'\n", section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000311 return -EINVAL;
312 }
313
314 bzero(prog, sizeof(*prog));
315
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700316 prog->section_name = strdup(section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000317 if (!prog->section_name) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700318 pr_warning("failed to alloc name for prog under section %s\n",
319 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000320 goto errout;
321 }
322
323 prog->insns = malloc(size);
324 if (!prog->insns) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700325 pr_warning("failed to alloc insns for prog under section %s\n",
326 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000327 goto errout;
328 }
329 prog->insns_cnt = size / sizeof(struct bpf_insn);
330 memcpy(prog->insns, data,
331 prog->insns_cnt * sizeof(struct bpf_insn));
332 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000333 prog->instances.fds = NULL;
334 prog->instances.nr = -1;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000335 prog->type = BPF_PROG_TYPE_KPROBE;
Wang Nana5b8bd42015-07-01 02:14:00 +0000336
337 return 0;
338errout:
339 bpf_program__exit(prog);
340 return -ENOMEM;
341}
342
343static int
344bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700345 char *section_name, int idx)
Wang Nana5b8bd42015-07-01 02:14:00 +0000346{
347 struct bpf_program prog, *progs;
348 int nr_progs, err;
349
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700350 err = bpf_program__init(data, size, section_name, idx, &prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000351 if (err)
352 return err;
353
354 progs = obj->programs;
355 nr_progs = obj->nr_programs;
356
357 progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
358 if (!progs) {
359 /*
360 * In this case the original obj->programs
361 * is still valid, so don't need special treat for
362 * bpf_close_object().
363 */
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700364 pr_warning("failed to alloc a new program under section '%s'\n",
365 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000366 bpf_program__exit(&prog);
367 return -ENOMEM;
368 }
369
370 pr_debug("found program %s\n", prog.section_name);
371 obj->programs = progs;
372 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000373 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000374 progs[nr_progs] = prog;
375 return 0;
376}
377
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700378static int
379bpf_object__init_prog_names(struct bpf_object *obj)
380{
381 Elf_Data *symbols = obj->efile.symbols;
382 struct bpf_program *prog;
383 size_t pi, si;
384
385 for (pi = 0; pi < obj->nr_programs; pi++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800386 const char *name = NULL;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700387
388 prog = &obj->programs[pi];
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800389 if (prog->idx == obj->efile.text_shndx) {
390 name = ".text";
391 goto skip_search;
392 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700393
394 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
395 si++) {
396 GElf_Sym sym;
397
398 if (!gelf_getsym(symbols, si, &sym))
399 continue;
400 if (sym.st_shndx != prog->idx)
401 continue;
Roman Gushchinfe4d44b2017-12-13 15:18:52 +0000402 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
403 continue;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700404
405 name = elf_strptr(obj->efile.elf,
406 obj->efile.strtabidx,
407 sym.st_name);
408 if (!name) {
409 pr_warning("failed to get sym name string for prog %s\n",
410 prog->section_name);
411 return -LIBBPF_ERRNO__LIBELF;
412 }
413 }
414
415 if (!name) {
416 pr_warning("failed to find sym for prog %s\n",
417 prog->section_name);
418 return -EINVAL;
419 }
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800420skip_search:
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700421 prog->name = strdup(name);
422 if (!prog->name) {
423 pr_warning("failed to allocate memory for prog sym %s\n",
424 name);
425 return -ENOMEM;
426 }
427 }
428
429 return 0;
430}
431
Wang Nan6c956392015-07-01 02:13:54 +0000432static struct bpf_object *bpf_object__new(const char *path,
433 void *obj_buf,
434 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000435{
436 struct bpf_object *obj;
437
438 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
439 if (!obj) {
440 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000441 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000442 }
443
444 strcpy(obj->path, path);
445 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000446
447 /*
448 * Caller of this function should also calls
449 * bpf_object__elf_finish() after data collection to return
450 * obj_buf to user. If not, we should duplicate the buffer to
451 * avoid user freeing them before elf finish.
452 */
453 obj->efile.obj_buf = obj_buf;
454 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000455 obj->efile.maps_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000456
Wang Nan52d33522015-07-01 02:14:04 +0000457 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000458
459 INIT_LIST_HEAD(&obj->list);
460 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000461 return obj;
462}
463
464static void bpf_object__elf_finish(struct bpf_object *obj)
465{
466 if (!obj_elf_valid(obj))
467 return;
468
469 if (obj->efile.elf) {
470 elf_end(obj->efile.elf);
471 obj->efile.elf = NULL;
472 }
Wang Nanbec7d682015-07-01 02:13:59 +0000473 obj->efile.symbols = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000474
475 zfree(&obj->efile.reloc);
476 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000477 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000478 obj->efile.obj_buf = NULL;
479 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000480}
481
482static int bpf_object__elf_init(struct bpf_object *obj)
483{
484 int err = 0;
485 GElf_Ehdr *ep;
486
487 if (obj_elf_valid(obj)) {
488 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000489 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000490 }
491
Wang Nan6c956392015-07-01 02:13:54 +0000492 if (obj->efile.obj_buf_sz > 0) {
493 /*
494 * obj_buf should have been validated by
495 * bpf_object__open_buffer().
496 */
497 obj->efile.elf = elf_memory(obj->efile.obj_buf,
498 obj->efile.obj_buf_sz);
499 } else {
500 obj->efile.fd = open(obj->path, O_RDONLY);
501 if (obj->efile.fd < 0) {
502 pr_warning("failed to open %s: %s\n", obj->path,
503 strerror(errno));
504 return -errno;
505 }
506
507 obj->efile.elf = elf_begin(obj->efile.fd,
508 LIBBPF_ELF_C_READ_MMAP,
509 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000510 }
511
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000512 if (!obj->efile.elf) {
513 pr_warning("failed to open %s as ELF file\n",
514 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000515 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000516 goto errout;
517 }
518
519 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
520 pr_warning("failed to get EHDR from %s\n",
521 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000522 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000523 goto errout;
524 }
525 ep = &obj->efile.ehdr;
526
Wang Nan9b161372016-07-18 06:01:08 +0000527 /* Old LLVM set e_machine to EM_NONE */
528 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000529 pr_warning("%s is not an eBPF object file\n",
530 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000531 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000532 goto errout;
533 }
534
535 return 0;
536errout:
537 bpf_object__elf_finish(obj);
538 return err;
539}
540
Wang Nancc4228d2015-07-01 02:13:55 +0000541static int
542bpf_object__check_endianness(struct bpf_object *obj)
543{
544 static unsigned int const endian = 1;
545
546 switch (obj->efile.ehdr.e_ident[EI_DATA]) {
547 case ELFDATA2LSB:
548 /* We are big endian, BPF obj is little endian. */
549 if (*(unsigned char const *)&endian != 1)
550 goto mismatch;
551 break;
552
553 case ELFDATA2MSB:
554 /* We are little endian, BPF obj is big endian. */
555 if (*(unsigned char const *)&endian != 0)
556 goto mismatch;
557 break;
558 default:
Wang Nan6371ca3b2015-11-06 13:49:37 +0000559 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000560 }
561
562 return 0;
563
564mismatch:
565 pr_warning("Error: endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000566 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000567}
568
Wang Nancb1e5e92015-07-01 02:13:57 +0000569static int
570bpf_object__init_license(struct bpf_object *obj,
571 void *data, size_t size)
572{
573 memcpy(obj->license, data,
574 min(size, sizeof(obj->license) - 1));
575 pr_debug("license of %s is %s\n", obj->path, obj->license);
576 return 0;
577}
578
579static int
580bpf_object__init_kversion(struct bpf_object *obj,
581 void *data, size_t size)
582{
583 u32 kver;
584
585 if (size != sizeof(kver)) {
586 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000587 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000588 }
589 memcpy(&kver, data, sizeof(kver));
590 obj->kern_version = kver;
591 pr_debug("kernel version of %s is %x\n", obj->path,
592 obj->kern_version);
593 return 0;
594}
595
Eric Leblond4708bbd2016-11-15 04:05:47 +0000596static int compare_bpf_map(const void *_a, const void *_b)
597{
598 const struct bpf_map *a = _a;
599 const struct bpf_map *b = _b;
600
601 return a->offset - b->offset;
602}
603
604static int
605bpf_object__init_maps(struct bpf_object *obj)
606{
Craig Gallekb13c5c12017-10-05 10:41:57 -0400607 int i, map_idx, map_def_sz, nr_maps = 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000608 Elf_Scn *scn;
609 Elf_Data *data;
610 Elf_Data *symbols = obj->efile.symbols;
611
612 if (obj->efile.maps_shndx < 0)
613 return -EINVAL;
614 if (!symbols)
615 return -EINVAL;
616
617 scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
618 if (scn)
619 data = elf_getdata(scn, NULL);
620 if (!scn || !data) {
621 pr_warning("failed to get Elf_Data from map section %d\n",
622 obj->efile.maps_shndx);
623 return -EINVAL;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000624 }
625
Eric Leblond4708bbd2016-11-15 04:05:47 +0000626 /*
627 * Count number of maps. Each map has a name.
628 * Array of maps is not supported: only the first element is
629 * considered.
630 *
631 * TODO: Detect array of map and report error.
632 */
633 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
634 GElf_Sym sym;
635
636 if (!gelf_getsym(symbols, i, &sym))
637 continue;
638 if (sym.st_shndx != obj->efile.maps_shndx)
639 continue;
640 nr_maps++;
641 }
642
643 /* Alloc obj->maps and fill nr_maps. */
644 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
645 nr_maps, data->d_size);
646
647 if (!nr_maps)
648 return 0;
Wang Nan9d759a92015-11-27 08:47:35 +0000649
Craig Gallekb13c5c12017-10-05 10:41:57 -0400650 /* Assume equally sized map definitions */
651 map_def_sz = data->d_size / nr_maps;
652 if (!data->d_size || (data->d_size % nr_maps) != 0) {
653 pr_warning("unable to determine map definition size "
654 "section %s, %d maps in %zd bytes\n",
655 obj->path, nr_maps, data->d_size);
656 return -EINVAL;
657 }
658
Wang Nan9d759a92015-11-27 08:47:35 +0000659 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
660 if (!obj->maps) {
661 pr_warning("alloc maps for object failed\n");
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000662 return -ENOMEM;
663 }
Wang Nan9d759a92015-11-27 08:47:35 +0000664 obj->nr_maps = nr_maps;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000665
Eric Leblond4708bbd2016-11-15 04:05:47 +0000666 /*
667 * fill all fd with -1 so won't close incorrect
668 * fd (fd=0 is stdin) when failure (zclose won't close
669 * negative fd)).
670 */
671 for (i = 0; i < nr_maps; i++)
Wang Nan9d759a92015-11-27 08:47:35 +0000672 obj->maps[i].fd = -1;
673
Eric Leblond4708bbd2016-11-15 04:05:47 +0000674 /*
675 * Fill obj->maps using data in "maps" section.
676 */
677 for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +0000678 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +0000679 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000680 struct bpf_map_def *def;
Wang Nan561bbcc2015-11-27 08:47:36 +0000681
682 if (!gelf_getsym(symbols, i, &sym))
683 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000684 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000685 continue;
686
687 map_name = elf_strptr(obj->efile.elf,
Wang Nan77ba9a52015-12-08 02:25:30 +0000688 obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000689 sym.st_name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000690 obj->maps[map_idx].offset = sym.st_value;
Craig Gallekb13c5c12017-10-05 10:41:57 -0400691 if (sym.st_value + map_def_sz > data->d_size) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000692 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
693 obj->path, map_name);
694 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000695 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000696
Wang Nan561bbcc2015-11-27 08:47:36 +0000697 obj->maps[map_idx].name = strdup(map_name);
Wang Nan973170e2015-12-08 02:25:29 +0000698 if (!obj->maps[map_idx].name) {
699 pr_warning("failed to alloc map name\n");
700 return -ENOMEM;
701 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000702 pr_debug("map %d is \"%s\"\n", map_idx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000703 obj->maps[map_idx].name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000704 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400705 /*
706 * If the definition of the map in the object file fits in
707 * bpf_map_def, copy it. Any extra fields in our version
708 * of bpf_map_def will default to zero as a result of the
709 * calloc above.
710 */
711 if (map_def_sz <= sizeof(struct bpf_map_def)) {
712 memcpy(&obj->maps[map_idx].def, def, map_def_sz);
713 } else {
714 /*
715 * Here the map structure being read is bigger than what
716 * we expect, truncate if the excess bits are all zero.
717 * If they are not zero, reject this map as
718 * incompatible.
719 */
720 char *b;
721 for (b = ((char *)def) + sizeof(struct bpf_map_def);
722 b < ((char *)def) + map_def_sz; b++) {
723 if (*b != 0) {
724 pr_warning("maps section in %s: \"%s\" "
725 "has unrecognized, non-zero "
726 "options\n",
727 obj->path, map_name);
728 return -EINVAL;
729 }
730 }
731 memcpy(&obj->maps[map_idx].def, def,
732 sizeof(struct bpf_map_def));
733 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000734 map_idx++;
Wang Nan561bbcc2015-11-27 08:47:36 +0000735 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000736
737 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400738 return 0;
Wang Nan561bbcc2015-11-27 08:47:36 +0000739}
740
Wang Nan29603662015-07-01 02:13:56 +0000741static int bpf_object__elf_collect(struct bpf_object *obj)
742{
743 Elf *elf = obj->efile.elf;
744 GElf_Ehdr *ep = &obj->efile.ehdr;
745 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +0000746 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +0000747
748 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
749 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
750 pr_warning("failed to get e_shstrndx from %s\n",
751 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000752 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000753 }
754
755 while ((scn = elf_nextscn(elf, scn)) != NULL) {
756 char *name;
757 GElf_Shdr sh;
758 Elf_Data *data;
759
760 idx++;
761 if (gelf_getshdr(scn, &sh) != &sh) {
762 pr_warning("failed to get section header from %s\n",
763 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000764 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000765 goto out;
766 }
767
768 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
769 if (!name) {
770 pr_warning("failed to get section name from %s\n",
771 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000772 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000773 goto out;
774 }
775
776 data = elf_getdata(scn, 0);
777 if (!data) {
778 pr_warning("failed to get section data from %s(%s)\n",
779 name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000780 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +0000781 goto out;
782 }
783 pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
784 name, (unsigned long)data->d_size,
785 (int)sh.sh_link, (unsigned long)sh.sh_flags,
786 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +0000787
788 if (strcmp(name, "license") == 0)
789 err = bpf_object__init_license(obj,
790 data->d_buf,
791 data->d_size);
792 else if (strcmp(name, "version") == 0)
793 err = bpf_object__init_kversion(obj,
794 data->d_buf,
795 data->d_size);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000796 else if (strcmp(name, "maps") == 0)
Wang Nan666810e2016-01-25 09:55:49 +0000797 obj->efile.maps_shndx = idx;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000798 else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +0000799 if (obj->efile.symbols) {
800 pr_warning("bpf: multiple SYMTAB in %s\n",
801 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000802 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +0000803 } else {
Wang Nanbec7d682015-07-01 02:13:59 +0000804 obj->efile.symbols = data;
Wang Nan77ba9a52015-12-08 02:25:30 +0000805 obj->efile.strtabidx = sh.sh_link;
806 }
Wang Nana5b8bd42015-07-01 02:14:00 +0000807 } else if ((sh.sh_type == SHT_PROGBITS) &&
808 (sh.sh_flags & SHF_EXECINSTR) &&
809 (data->d_size > 0)) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800810 if (strcmp(name, ".text") == 0)
811 obj->efile.text_shndx = idx;
Wang Nana5b8bd42015-07-01 02:14:00 +0000812 err = bpf_object__add_program(obj, data->d_buf,
813 data->d_size, name, idx);
814 if (err) {
Wang Nan6371ca3b2015-11-06 13:49:37 +0000815 char errmsg[STRERR_BUFSIZE];
816
Wang Nana5b8bd42015-07-01 02:14:00 +0000817 strerror_r(-err, errmsg, sizeof(errmsg));
818 pr_warning("failed to alloc program %s (%s): %s",
819 name, obj->path, errmsg);
820 }
Wang Nanb62f06e2015-07-01 02:14:01 +0000821 } else if (sh.sh_type == SHT_REL) {
822 void *reloc = obj->efile.reloc;
823 int nr_reloc = obj->efile.nr_reloc + 1;
824
825 reloc = realloc(reloc,
826 sizeof(*obj->efile.reloc) * nr_reloc);
827 if (!reloc) {
828 pr_warning("realloc failed\n");
829 err = -ENOMEM;
830 } else {
831 int n = nr_reloc - 1;
832
833 obj->efile.reloc = reloc;
834 obj->efile.nr_reloc = nr_reloc;
835
836 obj->efile.reloc[n].shdr = sh;
837 obj->efile.reloc[n].data = data;
838 }
Wang Nanbec7d682015-07-01 02:13:59 +0000839 }
Wang Nancb1e5e92015-07-01 02:13:57 +0000840 if (err)
841 goto out;
Wang Nan29603662015-07-01 02:13:56 +0000842 }
Wang Nan561bbcc2015-11-27 08:47:36 +0000843
Wang Nan77ba9a52015-12-08 02:25:30 +0000844 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
845 pr_warning("Corrupted ELF file: index of strtab invalid\n");
846 return LIBBPF_ERRNO__FORMAT;
847 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700848 if (obj->efile.maps_shndx >= 0) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000849 err = bpf_object__init_maps(obj);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700850 if (err)
851 goto out;
852 }
853 err = bpf_object__init_prog_names(obj);
Wang Nan29603662015-07-01 02:13:56 +0000854out:
855 return err;
856}
857
Wang Nan34090912015-07-01 02:14:02 +0000858static struct bpf_program *
859bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
860{
861 struct bpf_program *prog;
862 size_t i;
863
864 for (i = 0; i < obj->nr_programs; i++) {
865 prog = &obj->programs[i];
866 if (prog->idx == idx)
867 return prog;
868 }
869 return NULL;
870}
871
872static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800873bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
874 Elf_Data *data, struct bpf_object *obj)
Wang Nan34090912015-07-01 02:14:02 +0000875{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800876 Elf_Data *symbols = obj->efile.symbols;
877 int text_shndx = obj->efile.text_shndx;
878 int maps_shndx = obj->efile.maps_shndx;
879 struct bpf_map *maps = obj->maps;
880 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +0000881 int i, nrels;
882
883 pr_debug("collecting relocating info for: '%s'\n",
884 prog->section_name);
885 nrels = shdr->sh_size / shdr->sh_entsize;
886
887 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
888 if (!prog->reloc_desc) {
889 pr_warning("failed to alloc memory in relocation\n");
890 return -ENOMEM;
891 }
892 prog->nr_reloc = nrels;
893
894 for (i = 0; i < nrels; i++) {
895 GElf_Sym sym;
896 GElf_Rel rel;
897 unsigned int insn_idx;
898 struct bpf_insn *insns = prog->insns;
899 size_t map_idx;
900
901 if (!gelf_getrel(data, i, &rel)) {
902 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000903 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000904 }
905
Wang Nan34090912015-07-01 02:14:02 +0000906 if (!gelf_getsym(symbols,
907 GELF_R_SYM(rel.r_info),
908 &sym)) {
909 pr_warning("relocation: symbol %"PRIx64" not found\n",
910 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +0000911 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +0000912 }
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800913 pr_debug("relo for %ld value %ld name %d\n",
914 rel.r_info >> 32, sym.st_value, sym.st_name);
Wang Nan34090912015-07-01 02:14:02 +0000915
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800916 if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
Wang Nan666810e2016-01-25 09:55:49 +0000917 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
918 prog->section_name, sym.st_shndx);
919 return -LIBBPF_ERRNO__RELOC;
920 }
921
922 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
923 pr_debug("relocation: insn_idx=%u\n", insn_idx);
924
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800925 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
926 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
927 pr_warning("incorrect bpf_call opcode\n");
928 return -LIBBPF_ERRNO__RELOC;
929 }
930 prog->reloc_desc[i].type = RELO_CALL;
931 prog->reloc_desc[i].insn_idx = insn_idx;
932 prog->reloc_desc[i].text_off = sym.st_value;
933 continue;
934 }
935
Wang Nan34090912015-07-01 02:14:02 +0000936 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
937 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
938 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000939 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000940 }
941
Joe Stringer94e5ade2017-01-22 17:11:22 -0800942 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
943 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
944 if (maps[map_idx].offset == sym.st_value) {
945 pr_debug("relocation: find map %zd (%s) for insn %u\n",
946 map_idx, maps[map_idx].name, insn_idx);
947 break;
948 }
949 }
950
Wang Nan34090912015-07-01 02:14:02 +0000951 if (map_idx >= nr_maps) {
952 pr_warning("bpf relocation: map_idx %d large than %d\n",
953 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000954 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000955 }
956
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800957 prog->reloc_desc[i].type = RELO_LD64;
Wang Nan34090912015-07-01 02:14:02 +0000958 prog->reloc_desc[i].insn_idx = insn_idx;
959 prog->reloc_desc[i].map_idx = map_idx;
960 }
961 return 0;
962}
963
Wang Nan52d33522015-07-01 02:14:04 +0000964static int
965bpf_object__create_maps(struct bpf_object *obj)
966{
967 unsigned int i;
Wang Nan52d33522015-07-01 02:14:04 +0000968
Wang Nan9d759a92015-11-27 08:47:35 +0000969 for (i = 0; i < obj->nr_maps; i++) {
970 struct bpf_map_def *def = &obj->maps[i].def;
971 int *pfd = &obj->maps[i].fd;
Wang Nan52d33522015-07-01 02:14:04 +0000972
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700973 *pfd = bpf_create_map_name(def->type,
974 obj->maps[i].name,
975 def->key_size,
976 def->value_size,
977 def->max_entries,
Craig Gallekfe9b5f72017-10-05 10:41:58 -0400978 def->map_flags);
Wang Nan52d33522015-07-01 02:14:04 +0000979 if (*pfd < 0) {
980 size_t j;
981 int err = *pfd;
982
Eric Leblond49bf4b32017-08-20 21:48:14 +0200983 pr_warning("failed to create map (name: '%s'): %s\n",
984 obj->maps[i].name,
Wang Nan52d33522015-07-01 02:14:04 +0000985 strerror(errno));
986 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +0000987 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +0000988 return err;
989 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000990 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +0000991 }
992
Wang Nan52d33522015-07-01 02:14:04 +0000993 return 0;
994}
995
Wang Nan8a47a6c2015-07-01 02:14:05 +0000996static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800997bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
998 struct reloc_desc *relo)
999{
1000 struct bpf_insn *insn, *new_insn;
1001 struct bpf_program *text;
1002 size_t new_cnt;
1003
1004 if (relo->type != RELO_CALL)
1005 return -LIBBPF_ERRNO__RELOC;
1006
1007 if (prog->idx == obj->efile.text_shndx) {
1008 pr_warning("relo in .text insn %d into off %d\n",
1009 relo->insn_idx, relo->text_off);
1010 return -LIBBPF_ERRNO__RELOC;
1011 }
1012
1013 if (prog->main_prog_cnt == 0) {
1014 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1015 if (!text) {
1016 pr_warning("no .text section found yet relo into text exist\n");
1017 return -LIBBPF_ERRNO__RELOC;
1018 }
1019 new_cnt = prog->insns_cnt + text->insns_cnt;
1020 new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
1021 if (!new_insn) {
1022 pr_warning("oom in prog realloc\n");
1023 return -ENOMEM;
1024 }
1025 memcpy(new_insn + prog->insns_cnt, text->insns,
1026 text->insns_cnt * sizeof(*insn));
1027 prog->insns = new_insn;
1028 prog->main_prog_cnt = prog->insns_cnt;
1029 prog->insns_cnt = new_cnt;
1030 }
1031 insn = &prog->insns[relo->insn_idx];
1032 insn->imm += prog->main_prog_cnt - relo->insn_idx;
1033 pr_debug("added %zd insn from %s to prog %s\n",
1034 text->insns_cnt, text->section_name, prog->section_name);
1035 return 0;
1036}
1037
1038static int
Wang Nan9d759a92015-11-27 08:47:35 +00001039bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +00001040{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001041 int i, err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001042
1043 if (!prog || !prog->reloc_desc)
1044 return 0;
1045
1046 for (i = 0; i < prog->nr_reloc; i++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001047 if (prog->reloc_desc[i].type == RELO_LD64) {
1048 struct bpf_insn *insns = prog->insns;
1049 int insn_idx, map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001050
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001051 insn_idx = prog->reloc_desc[i].insn_idx;
1052 map_idx = prog->reloc_desc[i].map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001053
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001054 if (insn_idx >= (int)prog->insns_cnt) {
1055 pr_warning("relocation out of range: '%s'\n",
1056 prog->section_name);
1057 return -LIBBPF_ERRNO__RELOC;
1058 }
1059 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1060 insns[insn_idx].imm = obj->maps[map_idx].fd;
1061 } else {
1062 err = bpf_program__reloc_text(prog, obj,
1063 &prog->reloc_desc[i]);
1064 if (err)
1065 return err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001066 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00001067 }
1068
1069 zfree(&prog->reloc_desc);
1070 prog->nr_reloc = 0;
1071 return 0;
1072}
1073
1074
1075static int
1076bpf_object__relocate(struct bpf_object *obj)
1077{
1078 struct bpf_program *prog;
1079 size_t i;
1080 int err;
1081
1082 for (i = 0; i < obj->nr_programs; i++) {
1083 prog = &obj->programs[i];
1084
Wang Nan9d759a92015-11-27 08:47:35 +00001085 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +00001086 if (err) {
1087 pr_warning("failed to relocate '%s'\n",
1088 prog->section_name);
1089 return err;
1090 }
1091 }
1092 return 0;
1093}
1094
Wang Nan34090912015-07-01 02:14:02 +00001095static int bpf_object__collect_reloc(struct bpf_object *obj)
1096{
1097 int i, err;
1098
1099 if (!obj_elf_valid(obj)) {
1100 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001101 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001102 }
1103
1104 for (i = 0; i < obj->efile.nr_reloc; i++) {
1105 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1106 Elf_Data *data = obj->efile.reloc[i].data;
1107 int idx = shdr->sh_info;
1108 struct bpf_program *prog;
Wang Nan34090912015-07-01 02:14:02 +00001109
1110 if (shdr->sh_type != SHT_REL) {
1111 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001112 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001113 }
1114
1115 prog = bpf_object__find_prog_by_idx(obj, idx);
1116 if (!prog) {
1117 pr_warning("relocation failed: no %d section\n",
1118 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001119 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00001120 }
1121
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001122 err = bpf_program__collect_reloc(prog,
Wang Nan34090912015-07-01 02:14:02 +00001123 shdr, data,
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001124 obj);
Wang Nan34090912015-07-01 02:14:02 +00001125 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00001126 return err;
Wang Nan34090912015-07-01 02:14:02 +00001127 }
1128 return 0;
1129}
1130
Wang Nan55cffde2015-07-01 02:14:07 +00001131static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001132load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
Wang Nan5f44e4c82016-07-13 10:44:01 +00001133 int insns_cnt, char *license, u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00001134{
1135 int ret;
1136 char *log_buf;
1137
1138 if (!insns || !insns_cnt)
1139 return -EINVAL;
1140
1141 log_buf = malloc(BPF_LOG_BUF_SIZE);
1142 if (!log_buf)
1143 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1144
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001145 ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
1146 kern_version, log_buf, BPF_LOG_BUF_SIZE);
Wang Nan55cffde2015-07-01 02:14:07 +00001147
1148 if (ret >= 0) {
1149 *pfd = ret;
1150 ret = 0;
1151 goto out;
1152 }
1153
Wang Nan6371ca3b2015-11-06 13:49:37 +00001154 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +00001155 pr_warning("load bpf program failed: %s\n", strerror(errno));
1156
Wang Nan6371ca3b2015-11-06 13:49:37 +00001157 if (log_buf && log_buf[0] != '\0') {
1158 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00001159 pr_warning("-- BEGIN DUMP LOG ---\n");
1160 pr_warning("\n%s\n", log_buf);
1161 pr_warning("-- END LOG --\n");
Wang Nan705fa212016-07-13 10:44:02 +00001162 } else if (insns_cnt >= BPF_MAXINSNS) {
1163 pr_warning("Program too large (%d insns), at most %d insns\n",
1164 insns_cnt, BPF_MAXINSNS);
1165 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001166 } else {
Wang Nan705fa212016-07-13 10:44:02 +00001167 /* Wrong program type? */
1168 if (type != BPF_PROG_TYPE_KPROBE) {
1169 int fd;
1170
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001171 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
1172 insns, insns_cnt, license,
1173 kern_version, NULL, 0);
Wang Nan705fa212016-07-13 10:44:02 +00001174 if (fd >= 0) {
1175 close(fd);
1176 ret = -LIBBPF_ERRNO__PROGTYPE;
1177 goto out;
1178 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00001179 }
Wang Nan705fa212016-07-13 10:44:02 +00001180
1181 if (log_buf)
1182 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00001183 }
1184
1185out:
1186 free(log_buf);
1187 return ret;
1188}
1189
1190static int
1191bpf_program__load(struct bpf_program *prog,
1192 char *license, u32 kern_version)
1193{
Wang Nanb5805632015-11-16 12:10:09 +00001194 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00001195
Wang Nanb5805632015-11-16 12:10:09 +00001196 if (prog->instances.nr < 0 || !prog->instances.fds) {
1197 if (prog->preprocessor) {
1198 pr_warning("Internal error: can't load program '%s'\n",
1199 prog->section_name);
1200 return -LIBBPF_ERRNO__INTERNAL;
1201 }
Wang Nan55cffde2015-07-01 02:14:07 +00001202
Wang Nanb5805632015-11-16 12:10:09 +00001203 prog->instances.fds = malloc(sizeof(int));
1204 if (!prog->instances.fds) {
1205 pr_warning("Not enough memory for BPF fds\n");
1206 return -ENOMEM;
1207 }
1208 prog->instances.nr = 1;
1209 prog->instances.fds[0] = -1;
1210 }
1211
1212 if (!prog->preprocessor) {
1213 if (prog->instances.nr != 1) {
1214 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1215 prog->section_name, prog->instances.nr);
1216 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001217 err = load_program(prog->type, prog->name, prog->insns,
1218 prog->insns_cnt, license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00001219 if (!err)
1220 prog->instances.fds[0] = fd;
1221 goto out;
1222 }
1223
1224 for (i = 0; i < prog->instances.nr; i++) {
1225 struct bpf_prog_prep_result result;
1226 bpf_program_prep_t preprocessor = prog->preprocessor;
1227
1228 bzero(&result, sizeof(result));
1229 err = preprocessor(prog, i, prog->insns,
1230 prog->insns_cnt, &result);
1231 if (err) {
1232 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1233 i, prog->section_name);
1234 goto out;
1235 }
1236
1237 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1238 pr_debug("Skip loading the %dth instance of program '%s'\n",
1239 i, prog->section_name);
1240 prog->instances.fds[i] = -1;
1241 if (result.pfd)
1242 *result.pfd = -1;
1243 continue;
1244 }
1245
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001246 err = load_program(prog->type, prog->name,
1247 result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00001248 result.new_insn_cnt,
1249 license, kern_version, &fd);
1250
1251 if (err) {
1252 pr_warning("Loading the %dth instance of program '%s' failed\n",
1253 i, prog->section_name);
1254 goto out;
1255 }
1256
1257 if (result.pfd)
1258 *result.pfd = fd;
1259 prog->instances.fds[i] = fd;
1260 }
1261out:
Wang Nan55cffde2015-07-01 02:14:07 +00001262 if (err)
1263 pr_warning("failed to load program '%s'\n",
1264 prog->section_name);
1265 zfree(&prog->insns);
1266 prog->insns_cnt = 0;
1267 return err;
1268}
1269
1270static int
1271bpf_object__load_progs(struct bpf_object *obj)
1272{
1273 size_t i;
1274 int err;
1275
1276 for (i = 0; i < obj->nr_programs; i++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001277 if (obj->programs[i].idx == obj->efile.text_shndx)
1278 continue;
Wang Nan55cffde2015-07-01 02:14:07 +00001279 err = bpf_program__load(&obj->programs[i],
1280 obj->license,
1281 obj->kern_version);
1282 if (err)
1283 return err;
1284 }
1285 return 0;
1286}
1287
Wang Nancb1e5e92015-07-01 02:13:57 +00001288static int bpf_object__validate(struct bpf_object *obj)
1289{
1290 if (obj->kern_version == 0) {
1291 pr_warning("%s doesn't provide kernel version\n",
1292 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001293 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00001294 }
1295 return 0;
1296}
1297
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001298static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +00001299__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001300{
1301 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001302 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001303
1304 if (elf_version(EV_CURRENT) == EV_NONE) {
1305 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001306 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001307 }
1308
Wang Nan6c956392015-07-01 02:13:54 +00001309 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001310 if (IS_ERR(obj))
1311 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001312
Wang Nan6371ca3b2015-11-06 13:49:37 +00001313 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1314 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1315 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1316 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1317 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001318
1319 bpf_object__elf_finish(obj);
1320 return obj;
1321out:
1322 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001323 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001324}
1325
1326struct bpf_object *bpf_object__open(const char *path)
1327{
1328 /* param validation */
1329 if (!path)
1330 return NULL;
1331
1332 pr_debug("loading %s\n", path);
1333
Wang Nan6c956392015-07-01 02:13:54 +00001334 return __bpf_object__open(path, NULL, 0);
1335}
1336
1337struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001338 size_t obj_buf_sz,
1339 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001340{
Wang Nanacf860a2015-08-27 02:30:55 +00001341 char tmp_name[64];
1342
Wang Nan6c956392015-07-01 02:13:54 +00001343 /* param validation */
1344 if (!obj_buf || obj_buf_sz <= 0)
1345 return NULL;
1346
Wang Nanacf860a2015-08-27 02:30:55 +00001347 if (!name) {
1348 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1349 (unsigned long)obj_buf,
1350 (unsigned long)obj_buf_sz);
1351 tmp_name[sizeof(tmp_name) - 1] = '\0';
1352 name = tmp_name;
1353 }
1354 pr_debug("loading object '%s' from buffer\n",
1355 name);
Wang Nan6c956392015-07-01 02:13:54 +00001356
Wang Nanacf860a2015-08-27 02:30:55 +00001357 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001358}
1359
Wang Nan52d33522015-07-01 02:14:04 +00001360int bpf_object__unload(struct bpf_object *obj)
1361{
1362 size_t i;
1363
1364 if (!obj)
1365 return -EINVAL;
1366
Wang Nan9d759a92015-11-27 08:47:35 +00001367 for (i = 0; i < obj->nr_maps; i++)
1368 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001369
Wang Nan55cffde2015-07-01 02:14:07 +00001370 for (i = 0; i < obj->nr_programs; i++)
1371 bpf_program__unload(&obj->programs[i]);
1372
Wang Nan52d33522015-07-01 02:14:04 +00001373 return 0;
1374}
1375
1376int bpf_object__load(struct bpf_object *obj)
1377{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001378 int err;
1379
Wang Nan52d33522015-07-01 02:14:04 +00001380 if (!obj)
1381 return -EINVAL;
1382
1383 if (obj->loaded) {
1384 pr_warning("object should not be loaded twice\n");
1385 return -EINVAL;
1386 }
1387
1388 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001389
1390 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1391 CHECK_ERR(bpf_object__relocate(obj), err, out);
1392 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001393
1394 return 0;
1395out:
1396 bpf_object__unload(obj);
1397 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001398 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001399}
1400
Joe Stringerf3675402017-01-26 13:19:56 -08001401static int check_path(const char *path)
1402{
1403 struct statfs st_fs;
1404 char *dname, *dir;
1405 int err = 0;
1406
1407 if (path == NULL)
1408 return -EINVAL;
1409
1410 dname = strdup(path);
1411 if (dname == NULL)
1412 return -ENOMEM;
1413
1414 dir = dirname(dname);
1415 if (statfs(dir, &st_fs)) {
1416 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1417 err = -errno;
1418 }
1419 free(dname);
1420
1421 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1422 pr_warning("specified path %s is not on BPF FS\n", path);
1423 err = -EINVAL;
1424 }
1425
1426 return err;
1427}
1428
1429int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1430 int instance)
1431{
1432 int err;
1433
1434 err = check_path(path);
1435 if (err)
1436 return err;
1437
1438 if (prog == NULL) {
1439 pr_warning("invalid program pointer\n");
1440 return -EINVAL;
1441 }
1442
1443 if (instance < 0 || instance >= prog->instances.nr) {
1444 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1445 instance, prog->section_name, prog->instances.nr);
1446 return -EINVAL;
1447 }
1448
1449 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1450 pr_warning("failed to pin program: %s\n", strerror(errno));
1451 return -errno;
1452 }
1453 pr_debug("pinned program '%s'\n", path);
1454
1455 return 0;
1456}
1457
1458static int make_dir(const char *path)
1459{
1460 int err = 0;
1461
1462 if (mkdir(path, 0700) && errno != EEXIST)
1463 err = -errno;
1464
1465 if (err)
1466 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1467 return err;
1468}
1469
1470int bpf_program__pin(struct bpf_program *prog, const char *path)
1471{
1472 int i, err;
1473
1474 err = check_path(path);
1475 if (err)
1476 return err;
1477
1478 if (prog == NULL) {
1479 pr_warning("invalid program pointer\n");
1480 return -EINVAL;
1481 }
1482
1483 if (prog->instances.nr <= 0) {
1484 pr_warning("no instances of prog %s to pin\n",
1485 prog->section_name);
1486 return -EINVAL;
1487 }
1488
1489 err = make_dir(path);
1490 if (err)
1491 return err;
1492
1493 for (i = 0; i < prog->instances.nr; i++) {
1494 char buf[PATH_MAX];
1495 int len;
1496
1497 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1498 if (len < 0)
1499 return -EINVAL;
1500 else if (len >= PATH_MAX)
1501 return -ENAMETOOLONG;
1502
1503 err = bpf_program__pin_instance(prog, buf, i);
1504 if (err)
1505 return err;
1506 }
1507
1508 return 0;
1509}
1510
Joe Stringerb6989f32017-01-26 13:19:57 -08001511int bpf_map__pin(struct bpf_map *map, const char *path)
1512{
1513 int err;
1514
1515 err = check_path(path);
1516 if (err)
1517 return err;
1518
1519 if (map == NULL) {
1520 pr_warning("invalid map pointer\n");
1521 return -EINVAL;
1522 }
1523
1524 if (bpf_obj_pin(map->fd, path)) {
1525 pr_warning("failed to pin map: %s\n", strerror(errno));
1526 return -errno;
1527 }
1528
1529 pr_debug("pinned map '%s'\n", path);
1530 return 0;
1531}
1532
Joe Stringerd5148d82017-01-26 13:19:58 -08001533int bpf_object__pin(struct bpf_object *obj, const char *path)
1534{
1535 struct bpf_program *prog;
1536 struct bpf_map *map;
1537 int err;
1538
1539 if (!obj)
1540 return -ENOENT;
1541
1542 if (!obj->loaded) {
1543 pr_warning("object not yet loaded; load it first\n");
1544 return -ENOENT;
1545 }
1546
1547 err = make_dir(path);
1548 if (err)
1549 return err;
1550
1551 bpf_map__for_each(map, obj) {
1552 char buf[PATH_MAX];
1553 int len;
1554
1555 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1556 bpf_map__name(map));
1557 if (len < 0)
1558 return -EINVAL;
1559 else if (len >= PATH_MAX)
1560 return -ENAMETOOLONG;
1561
1562 err = bpf_map__pin(map, buf);
1563 if (err)
1564 return err;
1565 }
1566
1567 bpf_object__for_each_program(prog, obj) {
1568 char buf[PATH_MAX];
1569 int len;
1570
1571 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1572 prog->section_name);
1573 if (len < 0)
1574 return -EINVAL;
1575 else if (len >= PATH_MAX)
1576 return -ENAMETOOLONG;
1577
1578 err = bpf_program__pin(prog, buf);
1579 if (err)
1580 return err;
1581 }
1582
1583 return 0;
1584}
1585
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001586void bpf_object__close(struct bpf_object *obj)
1587{
Wang Nana5b8bd42015-07-01 02:14:00 +00001588 size_t i;
1589
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001590 if (!obj)
1591 return;
1592
Wang Nan10931d22016-11-26 07:03:26 +00001593 if (obj->clear_priv)
1594 obj->clear_priv(obj, obj->priv);
1595
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001596 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001597 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001598
Wang Nan9d759a92015-11-27 08:47:35 +00001599 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001600 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00001601 if (obj->maps[i].clear_priv)
1602 obj->maps[i].clear_priv(&obj->maps[i],
1603 obj->maps[i].priv);
1604 obj->maps[i].priv = NULL;
1605 obj->maps[i].clear_priv = NULL;
1606 }
1607 zfree(&obj->maps);
1608 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00001609
1610 if (obj->programs && obj->nr_programs) {
1611 for (i = 0; i < obj->nr_programs; i++)
1612 bpf_program__exit(&obj->programs[i]);
1613 }
1614 zfree(&obj->programs);
1615
Wang Nan9a208ef2015-07-01 02:14:10 +00001616 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001617 free(obj);
1618}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001619
Wang Nan9a208ef2015-07-01 02:14:10 +00001620struct bpf_object *
1621bpf_object__next(struct bpf_object *prev)
1622{
1623 struct bpf_object *next;
1624
1625 if (!prev)
1626 next = list_first_entry(&bpf_objects_list,
1627 struct bpf_object,
1628 list);
1629 else
1630 next = list_next_entry(prev, list);
1631
1632 /* Empty list is noticed here so don't need checking on entry. */
1633 if (&next->list == &bpf_objects_list)
1634 return NULL;
1635
1636 return next;
1637}
1638
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001639const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00001640{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001641 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001642}
1643
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001644unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00001645{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001646 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00001647}
1648
Wang Nan10931d22016-11-26 07:03:26 +00001649int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1650 bpf_object_clear_priv_t clear_priv)
1651{
1652 if (obj->priv && obj->clear_priv)
1653 obj->clear_priv(obj, obj->priv);
1654
1655 obj->priv = priv;
1656 obj->clear_priv = clear_priv;
1657 return 0;
1658}
1659
1660void *bpf_object__priv(struct bpf_object *obj)
1661{
1662 return obj ? obj->priv : ERR_PTR(-EINVAL);
1663}
1664
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001665struct bpf_program *
1666bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1667{
1668 size_t idx;
1669
1670 if (!obj->programs)
1671 return NULL;
1672 /* First handler */
1673 if (prev == NULL)
1674 return &obj->programs[0];
1675
1676 if (prev->obj != obj) {
1677 pr_warning("error: program handler doesn't match object\n");
1678 return NULL;
1679 }
1680
1681 idx = (prev - obj->programs) + 1;
1682 if (idx >= obj->nr_programs)
1683 return NULL;
1684 return &obj->programs[idx];
1685}
1686
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001687int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1688 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001689{
1690 if (prog->priv && prog->clear_priv)
1691 prog->clear_priv(prog, prog->priv);
1692
1693 prog->priv = priv;
1694 prog->clear_priv = clear_priv;
1695 return 0;
1696}
1697
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001698void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001699{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001700 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001701}
1702
Namhyung Kim715f8db2015-11-03 20:21:05 +09001703const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001704{
1705 const char *title;
1706
1707 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001708 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001709 title = strdup(title);
1710 if (!title) {
1711 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001712 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001713 }
1714 }
1715
1716 return title;
1717}
1718
1719int bpf_program__fd(struct bpf_program *prog)
1720{
Wang Nanb5805632015-11-16 12:10:09 +00001721 return bpf_program__nth_fd(prog, 0);
1722}
1723
1724int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1725 bpf_program_prep_t prep)
1726{
1727 int *instances_fds;
1728
1729 if (nr_instances <= 0 || !prep)
1730 return -EINVAL;
1731
1732 if (prog->instances.nr > 0 || prog->instances.fds) {
1733 pr_warning("Can't set pre-processor after loading\n");
1734 return -EINVAL;
1735 }
1736
1737 instances_fds = malloc(sizeof(int) * nr_instances);
1738 if (!instances_fds) {
1739 pr_warning("alloc memory failed for fds\n");
1740 return -ENOMEM;
1741 }
1742
1743 /* fill all fd with -1 */
1744 memset(instances_fds, -1, sizeof(int) * nr_instances);
1745
1746 prog->instances.nr = nr_instances;
1747 prog->instances.fds = instances_fds;
1748 prog->preprocessor = prep;
1749 return 0;
1750}
1751
1752int bpf_program__nth_fd(struct bpf_program *prog, int n)
1753{
1754 int fd;
1755
1756 if (n >= prog->instances.nr || n < 0) {
1757 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1758 n, prog->section_name, prog->instances.nr);
1759 return -EINVAL;
1760 }
1761
1762 fd = prog->instances.fds[n];
1763 if (fd < 0) {
1764 pr_warning("%dth instance of program '%s' is invalid\n",
1765 n, prog->section_name);
1766 return -ENOENT;
1767 }
1768
1769 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001770}
Wang Nan9d759a92015-11-27 08:47:35 +00001771
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07001772void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00001773{
1774 prog->type = type;
1775}
1776
Wang Nan5f44e4c82016-07-13 10:44:01 +00001777static bool bpf_program__is_type(struct bpf_program *prog,
1778 enum bpf_prog_type type)
1779{
1780 return prog ? (prog->type == type) : false;
1781}
1782
Joe Stringered794072017-01-22 17:11:23 -08001783#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1784int bpf_program__set_##NAME(struct bpf_program *prog) \
1785{ \
1786 if (!prog) \
1787 return -EINVAL; \
1788 bpf_program__set_type(prog, TYPE); \
1789 return 0; \
1790} \
1791 \
1792bool bpf_program__is_##NAME(struct bpf_program *prog) \
1793{ \
1794 return bpf_program__is_type(prog, TYPE); \
1795} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00001796
Joe Stringer7803ba72017-01-22 17:11:24 -08001797BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
Joe Stringered794072017-01-22 17:11:23 -08001798BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08001799BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1800BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08001801BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08001802BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1803BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00001804
Roman Gushchin583c9002017-12-13 15:18:51 +00001805#define BPF_PROG_SEC(string, type) { string, sizeof(string), type }
1806static const struct {
1807 const char *sec;
1808 size_t len;
1809 enum bpf_prog_type prog_type;
1810} section_names[] = {
1811 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
1812 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
1813 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
1814 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
1815 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
1816 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
1817 BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
1818 BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
1819 BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
1820 BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
1821 BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
1822};
1823#undef BPF_PROG_SEC
1824
1825static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog)
1826{
1827 int i;
1828
1829 if (!prog->section_name)
1830 goto err;
1831
1832 for (i = 0; i < ARRAY_SIZE(section_names); i++)
1833 if (strncmp(prog->section_name, section_names[i].sec,
1834 section_names[i].len) == 0)
1835 return section_names[i].prog_type;
1836
1837err:
1838 pr_warning("failed to guess program type based on section name %s\n",
1839 prog->section_name);
1840
1841 return BPF_PROG_TYPE_UNSPEC;
1842}
1843
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001844int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001845{
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001846 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00001847}
1848
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001849const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001850{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001851 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001852}
1853
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001854const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00001855{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001856 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001857}
1858
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001859int bpf_map__set_priv(struct bpf_map *map, void *priv,
1860 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00001861{
1862 if (!map)
1863 return -EINVAL;
1864
1865 if (map->priv) {
1866 if (map->clear_priv)
1867 map->clear_priv(map, map->priv);
1868 }
1869
1870 map->priv = priv;
1871 map->clear_priv = clear_priv;
1872 return 0;
1873}
1874
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001875void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001876{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001877 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001878}
1879
1880struct bpf_map *
1881bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1882{
1883 size_t idx;
1884 struct bpf_map *s, *e;
1885
1886 if (!obj || !obj->maps)
1887 return NULL;
1888
1889 s = obj->maps;
1890 e = obj->maps + obj->nr_maps;
1891
1892 if (prev == NULL)
1893 return s;
1894
1895 if ((prev < s) || (prev >= e)) {
1896 pr_warning("error in %s: map handler doesn't belong to object\n",
1897 __func__);
1898 return NULL;
1899 }
1900
1901 idx = (prev - obj->maps) + 1;
1902 if (idx >= obj->nr_maps)
1903 return NULL;
1904 return &obj->maps[idx];
1905}
Wang Nan561bbcc2015-11-27 08:47:36 +00001906
1907struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001908bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00001909{
1910 struct bpf_map *pos;
1911
1912 bpf_map__for_each(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00001913 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00001914 return pos;
1915 }
1916 return NULL;
1917}
Wang Nan5a6acad2016-11-26 07:03:27 +00001918
1919struct bpf_map *
1920bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1921{
1922 int i;
1923
1924 for (i = 0; i < obj->nr_maps; i++) {
1925 if (obj->maps[i].offset == offset)
1926 return &obj->maps[i];
1927 }
1928 return ERR_PTR(-ENOENT);
1929}
Joe Stringere28ff1a2017-01-22 17:11:25 -08001930
1931long libbpf_get_error(const void *ptr)
1932{
1933 if (IS_ERR(ptr))
1934 return PTR_ERR(ptr);
1935 return 0;
1936}
John Fastabend6f6d33f2017-08-15 22:34:22 -07001937
1938int bpf_prog_load(const char *file, enum bpf_prog_type type,
1939 struct bpf_object **pobj, int *prog_fd)
1940{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001941 struct bpf_program *prog, *first_prog = NULL;
John Fastabend6f6d33f2017-08-15 22:34:22 -07001942 struct bpf_object *obj;
1943 int err;
1944
1945 obj = bpf_object__open(file);
1946 if (IS_ERR(obj))
1947 return -ENOENT;
1948
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001949 bpf_object__for_each_program(prog, obj) {
1950 /*
1951 * If type is not specified, try to guess it based on
1952 * section name.
1953 */
1954 if (type == BPF_PROG_TYPE_UNSPEC) {
1955 type = bpf_program__guess_type(prog);
1956 if (type == BPF_PROG_TYPE_UNSPEC) {
1957 bpf_object__close(obj);
1958 return -EINVAL;
1959 }
1960 }
1961
1962 bpf_program__set_type(prog, type);
1963 if (prog->idx != obj->efile.text_shndx && !first_prog)
1964 first_prog = prog;
1965 }
1966
1967 if (!first_prog) {
1968 pr_warning("object file doesn't contain bpf program\n");
John Fastabend6f6d33f2017-08-15 22:34:22 -07001969 bpf_object__close(obj);
1970 return -ENOENT;
1971 }
1972
John Fastabend6f6d33f2017-08-15 22:34:22 -07001973 err = bpf_object__load(obj);
1974 if (err) {
1975 bpf_object__close(obj);
1976 return -EINVAL;
1977 }
1978
1979 *pobj = obj;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001980 *prog_fd = bpf_program__fd(first_prog);
John Fastabend6f6d33f2017-08-15 22:34:22 -07001981 return 0;
1982}