blob: 30c7763751181735e8ad620e12ae21adb3fda507 [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 }
David Miller7d9890e2017-12-19 15:53:11 -0500913 pr_debug("relo for %lld value %lld name %d\n",
914 (long long) (rel.r_info >> 32),
915 (long long) sym.st_value, sym.st_name);
Wang Nan34090912015-07-01 02:14:02 +0000916
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800917 if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
Wang Nan666810e2016-01-25 09:55:49 +0000918 pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
919 prog->section_name, sym.st_shndx);
920 return -LIBBPF_ERRNO__RELOC;
921 }
922
923 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
924 pr_debug("relocation: insn_idx=%u\n", insn_idx);
925
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800926 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
927 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
928 pr_warning("incorrect bpf_call opcode\n");
929 return -LIBBPF_ERRNO__RELOC;
930 }
931 prog->reloc_desc[i].type = RELO_CALL;
932 prog->reloc_desc[i].insn_idx = insn_idx;
933 prog->reloc_desc[i].text_off = sym.st_value;
934 continue;
935 }
936
Wang Nan34090912015-07-01 02:14:02 +0000937 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
938 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
939 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000940 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000941 }
942
Joe Stringer94e5ade2017-01-22 17:11:22 -0800943 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
944 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
945 if (maps[map_idx].offset == sym.st_value) {
946 pr_debug("relocation: find map %zd (%s) for insn %u\n",
947 map_idx, maps[map_idx].name, insn_idx);
948 break;
949 }
950 }
951
Wang Nan34090912015-07-01 02:14:02 +0000952 if (map_idx >= nr_maps) {
953 pr_warning("bpf relocation: map_idx %d large than %d\n",
954 (int)map_idx, (int)nr_maps - 1);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000955 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +0000956 }
957
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800958 prog->reloc_desc[i].type = RELO_LD64;
Wang Nan34090912015-07-01 02:14:02 +0000959 prog->reloc_desc[i].insn_idx = insn_idx;
960 prog->reloc_desc[i].map_idx = map_idx;
961 }
962 return 0;
963}
964
Wang Nan52d33522015-07-01 02:14:04 +0000965static int
966bpf_object__create_maps(struct bpf_object *obj)
967{
968 unsigned int i;
Wang Nan52d33522015-07-01 02:14:04 +0000969
Wang Nan9d759a92015-11-27 08:47:35 +0000970 for (i = 0; i < obj->nr_maps; i++) {
971 struct bpf_map_def *def = &obj->maps[i].def;
972 int *pfd = &obj->maps[i].fd;
Wang Nan52d33522015-07-01 02:14:04 +0000973
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700974 *pfd = bpf_create_map_name(def->type,
975 obj->maps[i].name,
976 def->key_size,
977 def->value_size,
978 def->max_entries,
Craig Gallekfe9b5f72017-10-05 10:41:58 -0400979 def->map_flags);
Wang Nan52d33522015-07-01 02:14:04 +0000980 if (*pfd < 0) {
981 size_t j;
982 int err = *pfd;
983
Eric Leblond49bf4b32017-08-20 21:48:14 +0200984 pr_warning("failed to create map (name: '%s'): %s\n",
985 obj->maps[i].name,
Wang Nan52d33522015-07-01 02:14:04 +0000986 strerror(errno));
987 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +0000988 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +0000989 return err;
990 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000991 pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +0000992 }
993
Wang Nan52d33522015-07-01 02:14:04 +0000994 return 0;
995}
996
Wang Nan8a47a6c2015-07-01 02:14:05 +0000997static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800998bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
999 struct reloc_desc *relo)
1000{
1001 struct bpf_insn *insn, *new_insn;
1002 struct bpf_program *text;
1003 size_t new_cnt;
1004
1005 if (relo->type != RELO_CALL)
1006 return -LIBBPF_ERRNO__RELOC;
1007
1008 if (prog->idx == obj->efile.text_shndx) {
1009 pr_warning("relo in .text insn %d into off %d\n",
1010 relo->insn_idx, relo->text_off);
1011 return -LIBBPF_ERRNO__RELOC;
1012 }
1013
1014 if (prog->main_prog_cnt == 0) {
1015 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1016 if (!text) {
1017 pr_warning("no .text section found yet relo into text exist\n");
1018 return -LIBBPF_ERRNO__RELOC;
1019 }
1020 new_cnt = prog->insns_cnt + text->insns_cnt;
1021 new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
1022 if (!new_insn) {
1023 pr_warning("oom in prog realloc\n");
1024 return -ENOMEM;
1025 }
1026 memcpy(new_insn + prog->insns_cnt, text->insns,
1027 text->insns_cnt * sizeof(*insn));
1028 prog->insns = new_insn;
1029 prog->main_prog_cnt = prog->insns_cnt;
1030 prog->insns_cnt = new_cnt;
1031 }
1032 insn = &prog->insns[relo->insn_idx];
1033 insn->imm += prog->main_prog_cnt - relo->insn_idx;
1034 pr_debug("added %zd insn from %s to prog %s\n",
1035 text->insns_cnt, text->section_name, prog->section_name);
1036 return 0;
1037}
1038
1039static int
Wang Nan9d759a92015-11-27 08:47:35 +00001040bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +00001041{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001042 int i, err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001043
1044 if (!prog || !prog->reloc_desc)
1045 return 0;
1046
1047 for (i = 0; i < prog->nr_reloc; i++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001048 if (prog->reloc_desc[i].type == RELO_LD64) {
1049 struct bpf_insn *insns = prog->insns;
1050 int insn_idx, map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001051
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001052 insn_idx = prog->reloc_desc[i].insn_idx;
1053 map_idx = prog->reloc_desc[i].map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001054
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001055 if (insn_idx >= (int)prog->insns_cnt) {
1056 pr_warning("relocation out of range: '%s'\n",
1057 prog->section_name);
1058 return -LIBBPF_ERRNO__RELOC;
1059 }
1060 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1061 insns[insn_idx].imm = obj->maps[map_idx].fd;
1062 } else {
1063 err = bpf_program__reloc_text(prog, obj,
1064 &prog->reloc_desc[i]);
1065 if (err)
1066 return err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001067 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00001068 }
1069
1070 zfree(&prog->reloc_desc);
1071 prog->nr_reloc = 0;
1072 return 0;
1073}
1074
1075
1076static int
1077bpf_object__relocate(struct bpf_object *obj)
1078{
1079 struct bpf_program *prog;
1080 size_t i;
1081 int err;
1082
1083 for (i = 0; i < obj->nr_programs; i++) {
1084 prog = &obj->programs[i];
1085
Wang Nan9d759a92015-11-27 08:47:35 +00001086 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +00001087 if (err) {
1088 pr_warning("failed to relocate '%s'\n",
1089 prog->section_name);
1090 return err;
1091 }
1092 }
1093 return 0;
1094}
1095
Wang Nan34090912015-07-01 02:14:02 +00001096static int bpf_object__collect_reloc(struct bpf_object *obj)
1097{
1098 int i, err;
1099
1100 if (!obj_elf_valid(obj)) {
1101 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001102 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001103 }
1104
1105 for (i = 0; i < obj->efile.nr_reloc; i++) {
1106 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1107 Elf_Data *data = obj->efile.reloc[i].data;
1108 int idx = shdr->sh_info;
1109 struct bpf_program *prog;
Wang Nan34090912015-07-01 02:14:02 +00001110
1111 if (shdr->sh_type != SHT_REL) {
1112 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001113 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00001114 }
1115
1116 prog = bpf_object__find_prog_by_idx(obj, idx);
1117 if (!prog) {
1118 pr_warning("relocation failed: no %d section\n",
1119 idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001120 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00001121 }
1122
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001123 err = bpf_program__collect_reloc(prog,
Wang Nan34090912015-07-01 02:14:02 +00001124 shdr, data,
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001125 obj);
Wang Nan34090912015-07-01 02:14:02 +00001126 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00001127 return err;
Wang Nan34090912015-07-01 02:14:02 +00001128 }
1129 return 0;
1130}
1131
Wang Nan55cffde2015-07-01 02:14:07 +00001132static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001133load_program(enum bpf_prog_type type, const char *name, struct bpf_insn *insns,
Wang Nan5f44e4c82016-07-13 10:44:01 +00001134 int insns_cnt, char *license, u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00001135{
1136 int ret;
1137 char *log_buf;
1138
1139 if (!insns || !insns_cnt)
1140 return -EINVAL;
1141
1142 log_buf = malloc(BPF_LOG_BUF_SIZE);
1143 if (!log_buf)
1144 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1145
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001146 ret = bpf_load_program_name(type, name, insns, insns_cnt, license,
1147 kern_version, log_buf, BPF_LOG_BUF_SIZE);
Wang Nan55cffde2015-07-01 02:14:07 +00001148
1149 if (ret >= 0) {
1150 *pfd = ret;
1151 ret = 0;
1152 goto out;
1153 }
1154
Wang Nan6371ca3b2015-11-06 13:49:37 +00001155 ret = -LIBBPF_ERRNO__LOAD;
Wang Nan55cffde2015-07-01 02:14:07 +00001156 pr_warning("load bpf program failed: %s\n", strerror(errno));
1157
Wang Nan6371ca3b2015-11-06 13:49:37 +00001158 if (log_buf && log_buf[0] != '\0') {
1159 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00001160 pr_warning("-- BEGIN DUMP LOG ---\n");
1161 pr_warning("\n%s\n", log_buf);
1162 pr_warning("-- END LOG --\n");
Wang Nan705fa212016-07-13 10:44:02 +00001163 } else if (insns_cnt >= BPF_MAXINSNS) {
1164 pr_warning("Program too large (%d insns), at most %d insns\n",
1165 insns_cnt, BPF_MAXINSNS);
1166 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001167 } else {
Wang Nan705fa212016-07-13 10:44:02 +00001168 /* Wrong program type? */
1169 if (type != BPF_PROG_TYPE_KPROBE) {
1170 int fd;
1171
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001172 fd = bpf_load_program_name(BPF_PROG_TYPE_KPROBE, name,
1173 insns, insns_cnt, license,
1174 kern_version, NULL, 0);
Wang Nan705fa212016-07-13 10:44:02 +00001175 if (fd >= 0) {
1176 close(fd);
1177 ret = -LIBBPF_ERRNO__PROGTYPE;
1178 goto out;
1179 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00001180 }
Wang Nan705fa212016-07-13 10:44:02 +00001181
1182 if (log_buf)
1183 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00001184 }
1185
1186out:
1187 free(log_buf);
1188 return ret;
1189}
1190
1191static int
1192bpf_program__load(struct bpf_program *prog,
1193 char *license, u32 kern_version)
1194{
Wang Nanb5805632015-11-16 12:10:09 +00001195 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00001196
Wang Nanb5805632015-11-16 12:10:09 +00001197 if (prog->instances.nr < 0 || !prog->instances.fds) {
1198 if (prog->preprocessor) {
1199 pr_warning("Internal error: can't load program '%s'\n",
1200 prog->section_name);
1201 return -LIBBPF_ERRNO__INTERNAL;
1202 }
Wang Nan55cffde2015-07-01 02:14:07 +00001203
Wang Nanb5805632015-11-16 12:10:09 +00001204 prog->instances.fds = malloc(sizeof(int));
1205 if (!prog->instances.fds) {
1206 pr_warning("Not enough memory for BPF fds\n");
1207 return -ENOMEM;
1208 }
1209 prog->instances.nr = 1;
1210 prog->instances.fds[0] = -1;
1211 }
1212
1213 if (!prog->preprocessor) {
1214 if (prog->instances.nr != 1) {
1215 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1216 prog->section_name, prog->instances.nr);
1217 }
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001218 err = load_program(prog->type, prog->name, prog->insns,
1219 prog->insns_cnt, license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00001220 if (!err)
1221 prog->instances.fds[0] = fd;
1222 goto out;
1223 }
1224
1225 for (i = 0; i < prog->instances.nr; i++) {
1226 struct bpf_prog_prep_result result;
1227 bpf_program_prep_t preprocessor = prog->preprocessor;
1228
1229 bzero(&result, sizeof(result));
1230 err = preprocessor(prog, i, prog->insns,
1231 prog->insns_cnt, &result);
1232 if (err) {
1233 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1234 i, prog->section_name);
1235 goto out;
1236 }
1237
1238 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1239 pr_debug("Skip loading the %dth instance of program '%s'\n",
1240 i, prog->section_name);
1241 prog->instances.fds[i] = -1;
1242 if (result.pfd)
1243 *result.pfd = -1;
1244 continue;
1245 }
1246
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001247 err = load_program(prog->type, prog->name,
1248 result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00001249 result.new_insn_cnt,
1250 license, kern_version, &fd);
1251
1252 if (err) {
1253 pr_warning("Loading the %dth instance of program '%s' failed\n",
1254 i, prog->section_name);
1255 goto out;
1256 }
1257
1258 if (result.pfd)
1259 *result.pfd = fd;
1260 prog->instances.fds[i] = fd;
1261 }
1262out:
Wang Nan55cffde2015-07-01 02:14:07 +00001263 if (err)
1264 pr_warning("failed to load program '%s'\n",
1265 prog->section_name);
1266 zfree(&prog->insns);
1267 prog->insns_cnt = 0;
1268 return err;
1269}
1270
1271static int
1272bpf_object__load_progs(struct bpf_object *obj)
1273{
1274 size_t i;
1275 int err;
1276
1277 for (i = 0; i < obj->nr_programs; i++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001278 if (obj->programs[i].idx == obj->efile.text_shndx)
1279 continue;
Wang Nan55cffde2015-07-01 02:14:07 +00001280 err = bpf_program__load(&obj->programs[i],
1281 obj->license,
1282 obj->kern_version);
1283 if (err)
1284 return err;
1285 }
1286 return 0;
1287}
1288
Wang Nancb1e5e92015-07-01 02:13:57 +00001289static int bpf_object__validate(struct bpf_object *obj)
1290{
1291 if (obj->kern_version == 0) {
1292 pr_warning("%s doesn't provide kernel version\n",
1293 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001294 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00001295 }
1296 return 0;
1297}
1298
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001299static struct bpf_object *
Wang Nan6c956392015-07-01 02:13:54 +00001300__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001301{
1302 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001303 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001304
1305 if (elf_version(EV_CURRENT) == EV_NONE) {
1306 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001307 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001308 }
1309
Wang Nan6c956392015-07-01 02:13:54 +00001310 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001311 if (IS_ERR(obj))
1312 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001313
Wang Nan6371ca3b2015-11-06 13:49:37 +00001314 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1315 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1316 CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1317 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1318 CHECK_ERR(bpf_object__validate(obj), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001319
1320 bpf_object__elf_finish(obj);
1321 return obj;
1322out:
1323 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001324 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001325}
1326
1327struct bpf_object *bpf_object__open(const char *path)
1328{
1329 /* param validation */
1330 if (!path)
1331 return NULL;
1332
1333 pr_debug("loading %s\n", path);
1334
Wang Nan6c956392015-07-01 02:13:54 +00001335 return __bpf_object__open(path, NULL, 0);
1336}
1337
1338struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00001339 size_t obj_buf_sz,
1340 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00001341{
Wang Nanacf860a2015-08-27 02:30:55 +00001342 char tmp_name[64];
1343
Wang Nan6c956392015-07-01 02:13:54 +00001344 /* param validation */
1345 if (!obj_buf || obj_buf_sz <= 0)
1346 return NULL;
1347
Wang Nanacf860a2015-08-27 02:30:55 +00001348 if (!name) {
1349 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1350 (unsigned long)obj_buf,
1351 (unsigned long)obj_buf_sz);
1352 tmp_name[sizeof(tmp_name) - 1] = '\0';
1353 name = tmp_name;
1354 }
1355 pr_debug("loading object '%s' from buffer\n",
1356 name);
Wang Nan6c956392015-07-01 02:13:54 +00001357
Wang Nanacf860a2015-08-27 02:30:55 +00001358 return __bpf_object__open(name, obj_buf, obj_buf_sz);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001359}
1360
Wang Nan52d33522015-07-01 02:14:04 +00001361int bpf_object__unload(struct bpf_object *obj)
1362{
1363 size_t i;
1364
1365 if (!obj)
1366 return -EINVAL;
1367
Wang Nan9d759a92015-11-27 08:47:35 +00001368 for (i = 0; i < obj->nr_maps; i++)
1369 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001370
Wang Nan55cffde2015-07-01 02:14:07 +00001371 for (i = 0; i < obj->nr_programs; i++)
1372 bpf_program__unload(&obj->programs[i]);
1373
Wang Nan52d33522015-07-01 02:14:04 +00001374 return 0;
1375}
1376
1377int bpf_object__load(struct bpf_object *obj)
1378{
Wang Nan6371ca3b2015-11-06 13:49:37 +00001379 int err;
1380
Wang Nan52d33522015-07-01 02:14:04 +00001381 if (!obj)
1382 return -EINVAL;
1383
1384 if (obj->loaded) {
1385 pr_warning("object should not be loaded twice\n");
1386 return -EINVAL;
1387 }
1388
1389 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001390
1391 CHECK_ERR(bpf_object__create_maps(obj), err, out);
1392 CHECK_ERR(bpf_object__relocate(obj), err, out);
1393 CHECK_ERR(bpf_object__load_progs(obj), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00001394
1395 return 0;
1396out:
1397 bpf_object__unload(obj);
1398 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001399 return err;
Wang Nan52d33522015-07-01 02:14:04 +00001400}
1401
Joe Stringerf3675402017-01-26 13:19:56 -08001402static int check_path(const char *path)
1403{
1404 struct statfs st_fs;
1405 char *dname, *dir;
1406 int err = 0;
1407
1408 if (path == NULL)
1409 return -EINVAL;
1410
1411 dname = strdup(path);
1412 if (dname == NULL)
1413 return -ENOMEM;
1414
1415 dir = dirname(dname);
1416 if (statfs(dir, &st_fs)) {
1417 pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1418 err = -errno;
1419 }
1420 free(dname);
1421
1422 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1423 pr_warning("specified path %s is not on BPF FS\n", path);
1424 err = -EINVAL;
1425 }
1426
1427 return err;
1428}
1429
1430int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1431 int instance)
1432{
1433 int err;
1434
1435 err = check_path(path);
1436 if (err)
1437 return err;
1438
1439 if (prog == NULL) {
1440 pr_warning("invalid program pointer\n");
1441 return -EINVAL;
1442 }
1443
1444 if (instance < 0 || instance >= prog->instances.nr) {
1445 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1446 instance, prog->section_name, prog->instances.nr);
1447 return -EINVAL;
1448 }
1449
1450 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1451 pr_warning("failed to pin program: %s\n", strerror(errno));
1452 return -errno;
1453 }
1454 pr_debug("pinned program '%s'\n", path);
1455
1456 return 0;
1457}
1458
1459static int make_dir(const char *path)
1460{
1461 int err = 0;
1462
1463 if (mkdir(path, 0700) && errno != EEXIST)
1464 err = -errno;
1465
1466 if (err)
1467 pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1468 return err;
1469}
1470
1471int bpf_program__pin(struct bpf_program *prog, const char *path)
1472{
1473 int i, err;
1474
1475 err = check_path(path);
1476 if (err)
1477 return err;
1478
1479 if (prog == NULL) {
1480 pr_warning("invalid program pointer\n");
1481 return -EINVAL;
1482 }
1483
1484 if (prog->instances.nr <= 0) {
1485 pr_warning("no instances of prog %s to pin\n",
1486 prog->section_name);
1487 return -EINVAL;
1488 }
1489
1490 err = make_dir(path);
1491 if (err)
1492 return err;
1493
1494 for (i = 0; i < prog->instances.nr; i++) {
1495 char buf[PATH_MAX];
1496 int len;
1497
1498 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1499 if (len < 0)
1500 return -EINVAL;
1501 else if (len >= PATH_MAX)
1502 return -ENAMETOOLONG;
1503
1504 err = bpf_program__pin_instance(prog, buf, i);
1505 if (err)
1506 return err;
1507 }
1508
1509 return 0;
1510}
1511
Joe Stringerb6989f32017-01-26 13:19:57 -08001512int bpf_map__pin(struct bpf_map *map, const char *path)
1513{
1514 int err;
1515
1516 err = check_path(path);
1517 if (err)
1518 return err;
1519
1520 if (map == NULL) {
1521 pr_warning("invalid map pointer\n");
1522 return -EINVAL;
1523 }
1524
1525 if (bpf_obj_pin(map->fd, path)) {
1526 pr_warning("failed to pin map: %s\n", strerror(errno));
1527 return -errno;
1528 }
1529
1530 pr_debug("pinned map '%s'\n", path);
1531 return 0;
1532}
1533
Joe Stringerd5148d82017-01-26 13:19:58 -08001534int bpf_object__pin(struct bpf_object *obj, const char *path)
1535{
1536 struct bpf_program *prog;
1537 struct bpf_map *map;
1538 int err;
1539
1540 if (!obj)
1541 return -ENOENT;
1542
1543 if (!obj->loaded) {
1544 pr_warning("object not yet loaded; load it first\n");
1545 return -ENOENT;
1546 }
1547
1548 err = make_dir(path);
1549 if (err)
1550 return err;
1551
1552 bpf_map__for_each(map, obj) {
1553 char buf[PATH_MAX];
1554 int len;
1555
1556 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1557 bpf_map__name(map));
1558 if (len < 0)
1559 return -EINVAL;
1560 else if (len >= PATH_MAX)
1561 return -ENAMETOOLONG;
1562
1563 err = bpf_map__pin(map, buf);
1564 if (err)
1565 return err;
1566 }
1567
1568 bpf_object__for_each_program(prog, obj) {
1569 char buf[PATH_MAX];
1570 int len;
1571
1572 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1573 prog->section_name);
1574 if (len < 0)
1575 return -EINVAL;
1576 else if (len >= PATH_MAX)
1577 return -ENAMETOOLONG;
1578
1579 err = bpf_program__pin(prog, buf);
1580 if (err)
1581 return err;
1582 }
1583
1584 return 0;
1585}
1586
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001587void bpf_object__close(struct bpf_object *obj)
1588{
Wang Nana5b8bd42015-07-01 02:14:00 +00001589 size_t i;
1590
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001591 if (!obj)
1592 return;
1593
Wang Nan10931d22016-11-26 07:03:26 +00001594 if (obj->clear_priv)
1595 obj->clear_priv(obj, obj->priv);
1596
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001597 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00001598 bpf_object__unload(obj);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001599
Wang Nan9d759a92015-11-27 08:47:35 +00001600 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00001601 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00001602 if (obj->maps[i].clear_priv)
1603 obj->maps[i].clear_priv(&obj->maps[i],
1604 obj->maps[i].priv);
1605 obj->maps[i].priv = NULL;
1606 obj->maps[i].clear_priv = NULL;
1607 }
1608 zfree(&obj->maps);
1609 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00001610
1611 if (obj->programs && obj->nr_programs) {
1612 for (i = 0; i < obj->nr_programs; i++)
1613 bpf_program__exit(&obj->programs[i]);
1614 }
1615 zfree(&obj->programs);
1616
Wang Nan9a208ef2015-07-01 02:14:10 +00001617 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00001618 free(obj);
1619}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001620
Wang Nan9a208ef2015-07-01 02:14:10 +00001621struct bpf_object *
1622bpf_object__next(struct bpf_object *prev)
1623{
1624 struct bpf_object *next;
1625
1626 if (!prev)
1627 next = list_first_entry(&bpf_objects_list,
1628 struct bpf_object,
1629 list);
1630 else
1631 next = list_next_entry(prev, list);
1632
1633 /* Empty list is noticed here so don't need checking on entry. */
1634 if (&next->list == &bpf_objects_list)
1635 return NULL;
1636
1637 return next;
1638}
1639
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001640const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00001641{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001642 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00001643}
1644
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001645unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00001646{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001647 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00001648}
1649
Wang Nan10931d22016-11-26 07:03:26 +00001650int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1651 bpf_object_clear_priv_t clear_priv)
1652{
1653 if (obj->priv && obj->clear_priv)
1654 obj->clear_priv(obj, obj->priv);
1655
1656 obj->priv = priv;
1657 obj->clear_priv = clear_priv;
1658 return 0;
1659}
1660
1661void *bpf_object__priv(struct bpf_object *obj)
1662{
1663 return obj ? obj->priv : ERR_PTR(-EINVAL);
1664}
1665
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001666struct bpf_program *
1667bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1668{
1669 size_t idx;
1670
1671 if (!obj->programs)
1672 return NULL;
1673 /* First handler */
1674 if (prev == NULL)
1675 return &obj->programs[0];
1676
1677 if (prev->obj != obj) {
1678 pr_warning("error: program handler doesn't match object\n");
1679 return NULL;
1680 }
1681
1682 idx = (prev - obj->programs) + 1;
1683 if (idx >= obj->nr_programs)
1684 return NULL;
1685 return &obj->programs[idx];
1686}
1687
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001688int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1689 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001690{
1691 if (prog->priv && prog->clear_priv)
1692 prog->clear_priv(prog, prog->priv);
1693
1694 prog->priv = priv;
1695 prog->clear_priv = clear_priv;
1696 return 0;
1697}
1698
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001699void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001700{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03001701 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001702}
1703
Namhyung Kim715f8db2015-11-03 20:21:05 +09001704const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001705{
1706 const char *title;
1707
1708 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09001709 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001710 title = strdup(title);
1711 if (!title) {
1712 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00001713 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001714 }
1715 }
1716
1717 return title;
1718}
1719
1720int bpf_program__fd(struct bpf_program *prog)
1721{
Wang Nanb5805632015-11-16 12:10:09 +00001722 return bpf_program__nth_fd(prog, 0);
1723}
1724
1725int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1726 bpf_program_prep_t prep)
1727{
1728 int *instances_fds;
1729
1730 if (nr_instances <= 0 || !prep)
1731 return -EINVAL;
1732
1733 if (prog->instances.nr > 0 || prog->instances.fds) {
1734 pr_warning("Can't set pre-processor after loading\n");
1735 return -EINVAL;
1736 }
1737
1738 instances_fds = malloc(sizeof(int) * nr_instances);
1739 if (!instances_fds) {
1740 pr_warning("alloc memory failed for fds\n");
1741 return -ENOMEM;
1742 }
1743
1744 /* fill all fd with -1 */
1745 memset(instances_fds, -1, sizeof(int) * nr_instances);
1746
1747 prog->instances.nr = nr_instances;
1748 prog->instances.fds = instances_fds;
1749 prog->preprocessor = prep;
1750 return 0;
1751}
1752
1753int bpf_program__nth_fd(struct bpf_program *prog, int n)
1754{
1755 int fd;
1756
1757 if (n >= prog->instances.nr || n < 0) {
1758 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1759 n, prog->section_name, prog->instances.nr);
1760 return -EINVAL;
1761 }
1762
1763 fd = prog->instances.fds[n];
1764 if (fd < 0) {
1765 pr_warning("%dth instance of program '%s' is invalid\n",
1766 n, prog->section_name);
1767 return -ENOENT;
1768 }
1769
1770 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00001771}
Wang Nan9d759a92015-11-27 08:47:35 +00001772
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07001773void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00001774{
1775 prog->type = type;
1776}
1777
Wang Nan5f44e4c82016-07-13 10:44:01 +00001778static bool bpf_program__is_type(struct bpf_program *prog,
1779 enum bpf_prog_type type)
1780{
1781 return prog ? (prog->type == type) : false;
1782}
1783
Joe Stringered794072017-01-22 17:11:23 -08001784#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
1785int bpf_program__set_##NAME(struct bpf_program *prog) \
1786{ \
1787 if (!prog) \
1788 return -EINVAL; \
1789 bpf_program__set_type(prog, TYPE); \
1790 return 0; \
1791} \
1792 \
1793bool bpf_program__is_##NAME(struct bpf_program *prog) \
1794{ \
1795 return bpf_program__is_type(prog, TYPE); \
1796} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00001797
Joe Stringer7803ba72017-01-22 17:11:24 -08001798BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
Joe Stringered794072017-01-22 17:11:23 -08001799BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08001800BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1801BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08001802BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08001803BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1804BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00001805
Quentin Monnetd77be682018-01-16 15:51:48 -08001806#define BPF_PROG_SEC(string, type) { string, sizeof(string) - 1, type }
Roman Gushchin583c9002017-12-13 15:18:51 +00001807static const struct {
1808 const char *sec;
1809 size_t len;
1810 enum bpf_prog_type prog_type;
1811} section_names[] = {
1812 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
1813 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
1814 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
1815 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
1816 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
1817 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
1818 BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
1819 BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
1820 BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
1821 BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
1822 BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
1823};
1824#undef BPF_PROG_SEC
1825
1826static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog)
1827{
1828 int i;
1829
1830 if (!prog->section_name)
1831 goto err;
1832
1833 for (i = 0; i < ARRAY_SIZE(section_names); i++)
1834 if (strncmp(prog->section_name, section_names[i].sec,
1835 section_names[i].len) == 0)
1836 return section_names[i].prog_type;
1837
1838err:
1839 pr_warning("failed to guess program type based on section name %s\n",
1840 prog->section_name);
1841
1842 return BPF_PROG_TYPE_UNSPEC;
1843}
1844
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001845int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001846{
Arnaldo Carvalho de Melo6e009e62016-06-03 12:15:52 -03001847 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00001848}
1849
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001850const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001851{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001852 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001853}
1854
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001855const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00001856{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001857 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00001858}
1859
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001860int bpf_map__set_priv(struct bpf_map *map, void *priv,
1861 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00001862{
1863 if (!map)
1864 return -EINVAL;
1865
1866 if (map->priv) {
1867 if (map->clear_priv)
1868 map->clear_priv(map, map->priv);
1869 }
1870
1871 map->priv = priv;
1872 map->clear_priv = clear_priv;
1873 return 0;
1874}
1875
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001876void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00001877{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001878 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00001879}
1880
1881struct bpf_map *
1882bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1883{
1884 size_t idx;
1885 struct bpf_map *s, *e;
1886
1887 if (!obj || !obj->maps)
1888 return NULL;
1889
1890 s = obj->maps;
1891 e = obj->maps + obj->nr_maps;
1892
1893 if (prev == NULL)
1894 return s;
1895
1896 if ((prev < s) || (prev >= e)) {
1897 pr_warning("error in %s: map handler doesn't belong to object\n",
1898 __func__);
1899 return NULL;
1900 }
1901
1902 idx = (prev - obj->maps) + 1;
1903 if (idx >= obj->nr_maps)
1904 return NULL;
1905 return &obj->maps[idx];
1906}
Wang Nan561bbcc2015-11-27 08:47:36 +00001907
1908struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001909bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00001910{
1911 struct bpf_map *pos;
1912
1913 bpf_map__for_each(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00001914 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00001915 return pos;
1916 }
1917 return NULL;
1918}
Wang Nan5a6acad2016-11-26 07:03:27 +00001919
1920struct bpf_map *
1921bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1922{
1923 int i;
1924
1925 for (i = 0; i < obj->nr_maps; i++) {
1926 if (obj->maps[i].offset == offset)
1927 return &obj->maps[i];
1928 }
1929 return ERR_PTR(-ENOENT);
1930}
Joe Stringere28ff1a2017-01-22 17:11:25 -08001931
1932long libbpf_get_error(const void *ptr)
1933{
1934 if (IS_ERR(ptr))
1935 return PTR_ERR(ptr);
1936 return 0;
1937}
John Fastabend6f6d33f2017-08-15 22:34:22 -07001938
1939int bpf_prog_load(const char *file, enum bpf_prog_type type,
1940 struct bpf_object **pobj, int *prog_fd)
1941{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001942 struct bpf_program *prog, *first_prog = NULL;
John Fastabend6f6d33f2017-08-15 22:34:22 -07001943 struct bpf_object *obj;
1944 int err;
1945
1946 obj = bpf_object__open(file);
1947 if (IS_ERR(obj))
1948 return -ENOENT;
1949
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001950 bpf_object__for_each_program(prog, obj) {
1951 /*
1952 * If type is not specified, try to guess it based on
1953 * section name.
1954 */
1955 if (type == BPF_PROG_TYPE_UNSPEC) {
1956 type = bpf_program__guess_type(prog);
1957 if (type == BPF_PROG_TYPE_UNSPEC) {
1958 bpf_object__close(obj);
1959 return -EINVAL;
1960 }
1961 }
1962
1963 bpf_program__set_type(prog, type);
1964 if (prog->idx != obj->efile.text_shndx && !first_prog)
1965 first_prog = prog;
1966 }
1967
1968 if (!first_prog) {
1969 pr_warning("object file doesn't contain bpf program\n");
John Fastabend6f6d33f2017-08-15 22:34:22 -07001970 bpf_object__close(obj);
1971 return -ENOENT;
1972 }
1973
John Fastabend6f6d33f2017-08-15 22:34:22 -07001974 err = bpf_object__load(obj);
1975 if (err) {
1976 bpf_object__close(obj);
1977 return -EINVAL;
1978 }
1979
1980 *pobj = obj;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001981 *prog_fd = bpf_program__fd(first_prog);
John Fastabend6f6d33f2017-08-15 22:34:22 -07001982 return 0;
1983}