blob: 60ec9694923a7a2dc1f6d3d9f500a1820f46a907 [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
Eric Leblond6061a3d2018-01-30 21:55:03 +01002
Wang Nan1b76c132015-07-01 02:13:51 +00003/*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
Joe Stringerf3675402017-01-26 13:19:56 -08009 * Copyright (C) 2017 Nicira, Inc.
Daniel Borkmannd8599002019-04-09 23:20:13 +020010 * Copyright (C) 2019 Isovalent, Inc.
Wang Nan1b76c132015-07-01 02:13:51 +000011 */
12
Yonghong Songb4269952018-11-29 15:31:45 -080013#ifndef _GNU_SOURCE
Jakub Kicinski531b0142018-07-10 14:43:05 -070014#define _GNU_SOURCE
Yonghong Songb4269952018-11-29 15:31:45 -080015#endif
Wang Nan1b76c132015-07-01 02:13:51 +000016#include <stdlib.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000017#include <stdio.h>
18#include <stdarg.h>
Joe Stringerf3675402017-01-26 13:19:56 -080019#include <libgen.h>
Wang Nan34090912015-07-01 02:14:02 +000020#include <inttypes.h>
Wang Nanb3f59d62015-07-01 02:13:52 +000021#include <string.h>
Wang Nan1b76c132015-07-01 02:13:51 +000022#include <unistd.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000023#include <fcntl.h>
24#include <errno.h>
Wang Nan1b76c132015-07-01 02:13:51 +000025#include <asm/unistd.h>
Joe Stringere28ff1a2017-01-22 17:11:25 -080026#include <linux/err.h>
Wang Nancb1e5e92015-07-01 02:13:57 +000027#include <linux/kernel.h>
Wang Nan1b76c132015-07-01 02:13:51 +000028#include <linux/bpf.h>
Martin KaFai Lau38d5d3b2018-07-24 08:40:22 -070029#include <linux/btf.h>
Stanislav Fomichev47eff612018-11-20 17:11:19 -080030#include <linux/filter.h>
Wang Nan9a208ef2015-07-01 02:14:10 +000031#include <linux/list.h>
Joe Stringerf3675402017-01-26 13:19:56 -080032#include <linux/limits.h>
Yonghong Song438363c2018-10-09 16:14:47 -070033#include <linux/perf_event.h>
Daniel Borkmanna64af0e2018-10-19 15:51:03 +020034#include <linux/ring_buffer.h>
Joe Stringerf3675402017-01-26 13:19:56 -080035#include <sys/stat.h>
36#include <sys/types.h>
37#include <sys/vfs.h>
Jakub Kicinski531b0142018-07-10 14:43:05 -070038#include <tools/libc_compat.h>
Wang Nan1a5e3fb2015-07-01 02:13:53 +000039#include <libelf.h>
40#include <gelf.h>
Wang Nan1b76c132015-07-01 02:13:51 +000041
42#include "libbpf.h"
Wang Nan52d33522015-07-01 02:14:04 +000043#include "bpf.h"
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070044#include "btf.h"
Arnaldo Carvalho de Melo6d419072018-09-14 16:47:14 -030045#include "str_error.h"
Andrii Nakryikod7c4b392019-05-10 14:13:15 -070046#include "libbpf_internal.h"
Wang Nanb3f59d62015-07-01 02:13:52 +000047
Wang Nan9b161372016-07-18 06:01:08 +000048#ifndef EM_BPF
49#define EM_BPF 247
50#endif
51
Joe Stringerf3675402017-01-26 13:19:56 -080052#ifndef BPF_FS_MAGIC
53#define BPF_FS_MAGIC 0xcafe4a11
54#endif
55
Andrey Ignatovff466b52019-04-06 22:37:34 -070056/* vsprintf() in __base_pr() uses nonliteral format string. It may break
57 * compilation if user enables corresponding warning. Disable it explicitly.
58 */
59#pragma GCC diagnostic ignored "-Wformat-nonliteral"
60
Wang Nanb3f59d62015-07-01 02:13:52 +000061#define __printf(a, b) __attribute__((format(printf, a, b)))
62
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080063static int __base_pr(enum libbpf_print_level level, const char *format,
64 va_list args)
Wang Nanb3f59d62015-07-01 02:13:52 +000065{
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080066 if (level == LIBBPF_DEBUG)
67 return 0;
68
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080069 return vfprintf(stderr, format, args);
Wang Nanb3f59d62015-07-01 02:13:52 +000070}
71
Stanislav Fomicheva8a1f7d2019-02-04 16:20:55 -080072static libbpf_print_fn_t __libbpf_pr = __base_pr;
Wang Nanb3f59d62015-07-01 02:13:52 +000073
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080074void libbpf_set_print(libbpf_print_fn_t fn)
Wang Nanb3f59d62015-07-01 02:13:52 +000075{
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080076 __libbpf_pr = fn;
Wang Nanb3f59d62015-07-01 02:13:52 +000077}
Wang Nan1a5e3fb2015-07-01 02:13:53 +000078
Yonghong Song8461ef82019-02-01 16:14:14 -080079__printf(2, 3)
80void libbpf_print(enum libbpf_print_level level, const char *format, ...)
81{
82 va_list args;
83
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080084 if (!__libbpf_pr)
85 return;
86
Yonghong Song8461ef82019-02-01 16:14:14 -080087 va_start(args, format);
Yonghong Song6f1ae8b2019-02-01 16:14:17 -080088 __libbpf_pr(level, format, args);
Yonghong Song8461ef82019-02-01 16:14:14 -080089 va_end(args);
90}
91
Wang Nan6371ca3b2015-11-06 13:49:37 +000092#define STRERR_BUFSIZE 128
93
Wang Nan6371ca3b2015-11-06 13:49:37 +000094#define CHECK_ERR(action, err, out) do { \
95 err = action; \
96 if (err) \
97 goto out; \
98} while(0)
99
100
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000101/* Copied from tools/perf/util/util.h */
102#ifndef zfree
103# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
104#endif
105
106#ifndef zclose
107# define zclose(fd) ({ \
108 int ___err = 0; \
109 if ((fd) >= 0) \
110 ___err = close((fd)); \
111 fd = -1; \
112 ___err; })
113#endif
114
115#ifdef HAVE_LIBELF_MMAP_SUPPORT
116# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
117#else
118# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
119#endif
120
Song Liu34be16462019-03-11 22:30:38 -0700121static inline __u64 ptr_to_u64(const void *ptr)
122{
123 return (__u64) (unsigned long) ptr;
124}
125
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800126struct bpf_capabilities {
127 /* v4.14: kernel support for program & map names. */
128 __u32 name:1;
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200129 /* v5.2: kernel support for global data sections. */
130 __u32 global_data:1;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -0700131 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
132 __u32 btf_func:1;
133 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
134 __u32 btf_datasec:1;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800135};
136
Wang Nana5b8bd42015-07-01 02:14:00 +0000137/*
138 * bpf_prog should be a better name but it has been used in
139 * linux/filter.h.
140 */
141struct bpf_program {
142 /* Index in elf obj file, for relocation use. */
143 int idx;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700144 char *name;
David Beckettf0307a72018-05-16 14:02:49 -0700145 int prog_ifindex;
Wang Nana5b8bd42015-07-01 02:14:00 +0000146 char *section_name;
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800147 /* section_name with / replaced by _; makes recursive pinning
148 * in bpf_object__pin_programs easier
149 */
150 char *pin_name;
Wang Nana5b8bd42015-07-01 02:14:00 +0000151 struct bpf_insn *insns;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800152 size_t insns_cnt, main_prog_cnt;
Wang Nan5f44e4c82016-07-13 10:44:01 +0000153 enum bpf_prog_type type;
Wang Nan34090912015-07-01 02:14:02 +0000154
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800155 struct reloc_desc {
156 enum {
157 RELO_LD64,
158 RELO_CALL,
Daniel Borkmannd8599002019-04-09 23:20:13 +0200159 RELO_DATA,
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800160 } type;
Wang Nan34090912015-07-01 02:14:02 +0000161 int insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800162 union {
163 int map_idx;
164 int text_off;
165 };
Wang Nan34090912015-07-01 02:14:02 +0000166 } *reloc_desc;
167 int nr_reloc;
Alexei Starovoitovda11b412019-04-01 21:27:47 -0700168 int log_level;
Wang Nan55cffde2015-07-01 02:14:07 +0000169
Wang Nanb5805632015-11-16 12:10:09 +0000170 struct {
171 int nr;
172 int *fds;
173 } instances;
174 bpf_program_prep_t preprocessor;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000175
176 struct bpf_object *obj;
177 void *priv;
178 bpf_program_clear_priv_t clear_priv;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700179
180 enum bpf_attach_type expected_attach_type;
Yonghong Song2993e052018-11-19 15:29:16 -0800181 int btf_fd;
182 void *func_info;
183 __u32 func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800184 __u32 func_info_cnt;
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800185
186 struct bpf_capabilities *caps;
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800187
188 void *line_info;
189 __u32 line_info_rec_size;
190 __u32 line_info_cnt;
Jiong Wang04656192019-05-24 23:25:19 +0100191 __u32 prog_flags;
Wang Nana5b8bd42015-07-01 02:14:00 +0000192};
193
Daniel Borkmannd8599002019-04-09 23:20:13 +0200194enum libbpf_map_type {
195 LIBBPF_MAP_UNSPEC,
196 LIBBPF_MAP_DATA,
197 LIBBPF_MAP_BSS,
198 LIBBPF_MAP_RODATA,
199};
200
201static const char * const libbpf_type_to_btf_name[] = {
202 [LIBBPF_MAP_DATA] = ".data",
203 [LIBBPF_MAP_BSS] = ".bss",
204 [LIBBPF_MAP_RODATA] = ".rodata",
205};
206
Wang Nan9d759a92015-11-27 08:47:35 +0000207struct bpf_map {
208 int fd;
Wang Nan561bbcc2015-11-27 08:47:36 +0000209 char *name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000210 size_t offset;
David Beckettf0307a72018-05-16 14:02:49 -0700211 int map_ifindex;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800212 int inner_map_fd;
Wang Nan9d759a92015-11-27 08:47:35 +0000213 struct bpf_map_def def;
Martin KaFai Lau5b891af2018-07-24 08:40:21 -0700214 __u32 btf_key_type_id;
215 __u32 btf_value_type_id;
Wang Nan9d759a92015-11-27 08:47:35 +0000216 void *priv;
217 bpf_map_clear_priv_t clear_priv;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200218 enum libbpf_map_type libbpf_type;
219};
220
221struct bpf_secdata {
222 void *rodata;
223 void *data;
Wang Nan9d759a92015-11-27 08:47:35 +0000224};
225
Wang Nan9a208ef2015-07-01 02:14:10 +0000226static LIST_HEAD(bpf_objects_list);
227
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000228struct bpf_object {
Daniel Borkmannd8599002019-04-09 23:20:13 +0200229 char name[BPF_OBJ_NAME_LEN];
Wang Nancb1e5e92015-07-01 02:13:57 +0000230 char license[64];
Yonghong Song438363c2018-10-09 16:14:47 -0700231 __u32 kern_version;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000232
Wang Nana5b8bd42015-07-01 02:14:00 +0000233 struct bpf_program *programs;
234 size_t nr_programs;
Wang Nan9d759a92015-11-27 08:47:35 +0000235 struct bpf_map *maps;
236 size_t nr_maps;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200237 struct bpf_secdata sections;
Wang Nan9d759a92015-11-27 08:47:35 +0000238
Wang Nan52d33522015-07-01 02:14:04 +0000239 bool loaded;
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700240 bool has_pseudo_calls;
Wang Nana5b8bd42015-07-01 02:14:00 +0000241
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000242 /*
243 * Information when doing elf related work. Only valid if fd
244 * is valid.
245 */
246 struct {
247 int fd;
Wang Nan6c956392015-07-01 02:13:54 +0000248 void *obj_buf;
249 size_t obj_buf_sz;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000250 Elf *elf;
251 GElf_Ehdr ehdr;
Wang Nanbec7d682015-07-01 02:13:59 +0000252 Elf_Data *symbols;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200253 Elf_Data *data;
254 Elf_Data *rodata;
255 Elf_Data *bss;
Wang Nan77ba9a52015-12-08 02:25:30 +0000256 size_t strtabidx;
Wang Nanb62f06e2015-07-01 02:14:01 +0000257 struct {
258 GElf_Shdr shdr;
259 Elf_Data *data;
260 } *reloc;
261 int nr_reloc;
Wang Nan666810e2016-01-25 09:55:49 +0000262 int maps_shndx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800263 int text_shndx;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200264 int data_shndx;
265 int rodata_shndx;
266 int bss_shndx;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000267 } efile;
Wang Nan9a208ef2015-07-01 02:14:10 +0000268 /*
269 * All loaded bpf_object is linked in a list, which is
270 * hidden to caller. bpf_objects__<func> handlers deal with
271 * all objects.
272 */
273 struct list_head list;
Wang Nan10931d22016-11-26 07:03:26 +0000274
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700275 struct btf *btf;
Yonghong Song2993e052018-11-19 15:29:16 -0800276 struct btf_ext *btf_ext;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700277
Wang Nan10931d22016-11-26 07:03:26 +0000278 void *priv;
279 bpf_object_clear_priv_t clear_priv;
280
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800281 struct bpf_capabilities caps;
282
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000283 char path[];
284};
285#define obj_elf_valid(o) ((o)->efile.elf)
286
Joe Stringer29cd77f2018-10-02 13:35:39 -0700287void bpf_program__unload(struct bpf_program *prog)
Wang Nan55cffde2015-07-01 02:14:07 +0000288{
Wang Nanb5805632015-11-16 12:10:09 +0000289 int i;
290
Wang Nan55cffde2015-07-01 02:14:07 +0000291 if (!prog)
292 return;
293
Wang Nanb5805632015-11-16 12:10:09 +0000294 /*
295 * If the object is opened but the program was never loaded,
296 * it is possible that prog->instances.nr == -1.
297 */
298 if (prog->instances.nr > 0) {
299 for (i = 0; i < prog->instances.nr; i++)
300 zclose(prog->instances.fds[i]);
301 } else if (prog->instances.nr != -1) {
302 pr_warning("Internal error: instances.nr is %d\n",
303 prog->instances.nr);
304 }
305
306 prog->instances.nr = -1;
307 zfree(&prog->instances.fds);
Yonghong Song2993e052018-11-19 15:29:16 -0800308
309 zclose(prog->btf_fd);
310 zfree(&prog->func_info);
Prashant Bhole07a09d12018-12-17 16:57:50 +0900311 zfree(&prog->line_info);
Wang Nan55cffde2015-07-01 02:14:07 +0000312}
313
Wang Nana5b8bd42015-07-01 02:14:00 +0000314static void bpf_program__exit(struct bpf_program *prog)
315{
316 if (!prog)
317 return;
318
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000319 if (prog->clear_priv)
320 prog->clear_priv(prog, prog->priv);
321
322 prog->priv = NULL;
323 prog->clear_priv = NULL;
324
Wang Nan55cffde2015-07-01 02:14:07 +0000325 bpf_program__unload(prog);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700326 zfree(&prog->name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000327 zfree(&prog->section_name);
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800328 zfree(&prog->pin_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000329 zfree(&prog->insns);
Wang Nan34090912015-07-01 02:14:02 +0000330 zfree(&prog->reloc_desc);
331
332 prog->nr_reloc = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +0000333 prog->insns_cnt = 0;
334 prog->idx = -1;
335}
336
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800337static char *__bpf_program__pin_name(struct bpf_program *prog)
338{
339 char *name, *p;
340
341 name = p = strdup(prog->section_name);
342 while ((p = strchr(p, '/')))
343 *p = '_';
344
345 return name;
346}
347
Wang Nana5b8bd42015-07-01 02:14:00 +0000348static int
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700349bpf_program__init(void *data, size_t size, char *section_name, int idx,
350 struct bpf_program *prog)
Wang Nana5b8bd42015-07-01 02:14:00 +0000351{
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700352 const size_t bpf_insn_sz = sizeof(struct bpf_insn);
353
354 if (size == 0 || size % bpf_insn_sz) {
355 pr_warning("corrupted section '%s', size: %zu\n",
356 section_name, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000357 return -EINVAL;
358 }
359
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800360 memset(prog, 0, sizeof(*prog));
Wang Nana5b8bd42015-07-01 02:14:00 +0000361
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700362 prog->section_name = strdup(section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000363 if (!prog->section_name) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +0100364 pr_warning("failed to alloc name for prog under section(%d) %s\n",
365 idx, section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000366 goto errout;
367 }
368
Stanislav Fomichev33a2c752018-11-09 08:21:43 -0800369 prog->pin_name = __bpf_program__pin_name(prog);
370 if (!prog->pin_name) {
371 pr_warning("failed to alloc pin name for prog under section(%d) %s\n",
372 idx, section_name);
373 goto errout;
374 }
375
Wang Nana5b8bd42015-07-01 02:14:00 +0000376 prog->insns = malloc(size);
377 if (!prog->insns) {
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700378 pr_warning("failed to alloc insns for prog under section %s\n",
379 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000380 goto errout;
381 }
Andrii Nakryiko8ca990c2019-05-29 10:36:03 -0700382 prog->insns_cnt = size / bpf_insn_sz;
383 memcpy(prog->insns, data, size);
Wang Nana5b8bd42015-07-01 02:14:00 +0000384 prog->idx = idx;
Wang Nanb5805632015-11-16 12:10:09 +0000385 prog->instances.fds = NULL;
386 prog->instances.nr = -1;
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -0800387 prog->type = BPF_PROG_TYPE_UNSPEC;
Yonghong Song2993e052018-11-19 15:29:16 -0800388 prog->btf_fd = -1;
Wang Nana5b8bd42015-07-01 02:14:00 +0000389
390 return 0;
391errout:
392 bpf_program__exit(prog);
393 return -ENOMEM;
394}
395
396static int
397bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700398 char *section_name, int idx)
Wang Nana5b8bd42015-07-01 02:14:00 +0000399{
400 struct bpf_program prog, *progs;
401 int nr_progs, err;
402
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700403 err = bpf_program__init(data, size, section_name, idx, &prog);
Wang Nana5b8bd42015-07-01 02:14:00 +0000404 if (err)
405 return err;
406
Stanislav Fomichev47eff612018-11-20 17:11:19 -0800407 prog.caps = &obj->caps;
Wang Nana5b8bd42015-07-01 02:14:00 +0000408 progs = obj->programs;
409 nr_progs = obj->nr_programs;
410
Jakub Kicinski531b0142018-07-10 14:43:05 -0700411 progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
Wang Nana5b8bd42015-07-01 02:14:00 +0000412 if (!progs) {
413 /*
414 * In this case the original obj->programs
415 * is still valid, so don't need special treat for
416 * bpf_close_object().
417 */
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700418 pr_warning("failed to alloc a new program under section '%s'\n",
419 section_name);
Wang Nana5b8bd42015-07-01 02:14:00 +0000420 bpf_program__exit(&prog);
421 return -ENOMEM;
422 }
423
424 pr_debug("found program %s\n", prog.section_name);
425 obj->programs = progs;
426 obj->nr_programs = nr_progs + 1;
Wang Nanaa9b1ac2015-07-01 02:14:08 +0000427 prog.obj = obj;
Wang Nana5b8bd42015-07-01 02:14:00 +0000428 progs[nr_progs] = prog;
429 return 0;
430}
431
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700432static int
433bpf_object__init_prog_names(struct bpf_object *obj)
434{
435 Elf_Data *symbols = obj->efile.symbols;
436 struct bpf_program *prog;
437 size_t pi, si;
438
439 for (pi = 0; pi < obj->nr_programs; pi++) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -0800440 const char *name = NULL;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700441
442 prog = &obj->programs[pi];
443
444 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
445 si++) {
446 GElf_Sym sym;
447
448 if (!gelf_getsym(symbols, si, &sym))
449 continue;
450 if (sym.st_shndx != prog->idx)
451 continue;
Roman Gushchinfe4d44b2017-12-13 15:18:52 +0000452 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
453 continue;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700454
455 name = elf_strptr(obj->efile.elf,
456 obj->efile.strtabidx,
457 sym.st_name);
458 if (!name) {
459 pr_warning("failed to get sym name string for prog %s\n",
460 prog->section_name);
461 return -LIBBPF_ERRNO__LIBELF;
462 }
463 }
464
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700465 if (!name && prog->idx == obj->efile.text_shndx)
466 name = ".text";
467
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700468 if (!name) {
469 pr_warning("failed to find sym for prog %s\n",
470 prog->section_name);
471 return -EINVAL;
472 }
Jakub Kicinski9a94f272018-06-28 14:41:38 -0700473
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700474 prog->name = strdup(name);
475 if (!prog->name) {
476 pr_warning("failed to allocate memory for prog sym %s\n",
477 name);
478 return -ENOMEM;
479 }
480 }
481
482 return 0;
483}
484
Wang Nan6c956392015-07-01 02:13:54 +0000485static struct bpf_object *bpf_object__new(const char *path,
486 void *obj_buf,
487 size_t obj_buf_sz)
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000488{
489 struct bpf_object *obj;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200490 char *end;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000491
492 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
493 if (!obj) {
494 pr_warning("alloc memory failed for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000495 return ERR_PTR(-ENOMEM);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000496 }
497
498 strcpy(obj->path, path);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200499 /* Using basename() GNU version which doesn't modify arg. */
500 strncpy(obj->name, basename((void *)path),
501 sizeof(obj->name) - 1);
502 end = strchr(obj->name, '.');
503 if (end)
504 *end = 0;
Wang Nan6c956392015-07-01 02:13:54 +0000505
Daniel Borkmannd8599002019-04-09 23:20:13 +0200506 obj->efile.fd = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000507 /*
508 * Caller of this function should also calls
509 * bpf_object__elf_finish() after data collection to return
510 * obj_buf to user. If not, we should duplicate the buffer to
511 * avoid user freeing them before elf finish.
512 */
513 obj->efile.obj_buf = obj_buf;
514 obj->efile.obj_buf_sz = obj_buf_sz;
Wang Nan666810e2016-01-25 09:55:49 +0000515 obj->efile.maps_shndx = -1;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200516 obj->efile.data_shndx = -1;
517 obj->efile.rodata_shndx = -1;
518 obj->efile.bss_shndx = -1;
Wang Nan6c956392015-07-01 02:13:54 +0000519
Wang Nan52d33522015-07-01 02:14:04 +0000520 obj->loaded = false;
Wang Nan9a208ef2015-07-01 02:14:10 +0000521
522 INIT_LIST_HEAD(&obj->list);
523 list_add(&obj->list, &bpf_objects_list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000524 return obj;
525}
526
527static void bpf_object__elf_finish(struct bpf_object *obj)
528{
529 if (!obj_elf_valid(obj))
530 return;
531
532 if (obj->efile.elf) {
533 elf_end(obj->efile.elf);
534 obj->efile.elf = NULL;
535 }
Wang Nanbec7d682015-07-01 02:13:59 +0000536 obj->efile.symbols = NULL;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200537 obj->efile.data = NULL;
538 obj->efile.rodata = NULL;
539 obj->efile.bss = NULL;
Wang Nanb62f06e2015-07-01 02:14:01 +0000540
541 zfree(&obj->efile.reloc);
542 obj->efile.nr_reloc = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000543 zclose(obj->efile.fd);
Wang Nan6c956392015-07-01 02:13:54 +0000544 obj->efile.obj_buf = NULL;
545 obj->efile.obj_buf_sz = 0;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000546}
547
548static int bpf_object__elf_init(struct bpf_object *obj)
549{
550 int err = 0;
551 GElf_Ehdr *ep;
552
553 if (obj_elf_valid(obj)) {
554 pr_warning("elf init: internal error\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000555 return -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000556 }
557
Wang Nan6c956392015-07-01 02:13:54 +0000558 if (obj->efile.obj_buf_sz > 0) {
559 /*
560 * obj_buf should have been validated by
561 * bpf_object__open_buffer().
562 */
563 obj->efile.elf = elf_memory(obj->efile.obj_buf,
564 obj->efile.obj_buf_sz);
565 } else {
566 obj->efile.fd = open(obj->path, O_RDONLY);
567 if (obj->efile.fd < 0) {
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700568 char errmsg[STRERR_BUFSIZE], *cp;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +0200569
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700570 err = -errno;
571 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +0200572 pr_warning("failed to open %s: %s\n", obj->path, cp);
Andrii Nakryikobe5c5d42019-05-29 10:36:04 -0700573 return err;
Wang Nan6c956392015-07-01 02:13:54 +0000574 }
575
576 obj->efile.elf = elf_begin(obj->efile.fd,
577 LIBBPF_ELF_C_READ_MMAP,
578 NULL);
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000579 }
580
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000581 if (!obj->efile.elf) {
582 pr_warning("failed to open %s as ELF file\n",
583 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000584 err = -LIBBPF_ERRNO__LIBELF;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000585 goto errout;
586 }
587
588 if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
589 pr_warning("failed to get EHDR from %s\n",
590 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000591 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000592 goto errout;
593 }
594 ep = &obj->efile.ehdr;
595
Wang Nan9b161372016-07-18 06:01:08 +0000596 /* Old LLVM set e_machine to EM_NONE */
597 if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000598 pr_warning("%s is not an eBPF object file\n",
599 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000600 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan1a5e3fb2015-07-01 02:13:53 +0000601 goto errout;
602 }
603
604 return 0;
605errout:
606 bpf_object__elf_finish(obj);
607 return err;
608}
609
Andrii Nakryiko12ef5632019-05-29 10:36:05 -0700610static int bpf_object__check_endianness(struct bpf_object *obj)
Wang Nancc4228d2015-07-01 02:13:55 +0000611{
Andrii Nakryiko12ef5632019-05-29 10:36:05 -0700612#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
613 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
614 return 0;
615#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
616 if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
617 return 0;
618#else
619# error "Unrecognized __BYTE_ORDER__"
620#endif
621 pr_warning("endianness mismatch.\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000622 return -LIBBPF_ERRNO__ENDIAN;
Wang Nancc4228d2015-07-01 02:13:55 +0000623}
624
Wang Nancb1e5e92015-07-01 02:13:57 +0000625static int
626bpf_object__init_license(struct bpf_object *obj,
627 void *data, size_t size)
628{
629 memcpy(obj->license, data,
630 min(size, sizeof(obj->license) - 1));
631 pr_debug("license of %s is %s\n", obj->path, obj->license);
632 return 0;
633}
634
635static int
636bpf_object__init_kversion(struct bpf_object *obj,
637 void *data, size_t size)
638{
Yonghong Song438363c2018-10-09 16:14:47 -0700639 __u32 kver;
Wang Nancb1e5e92015-07-01 02:13:57 +0000640
641 if (size != sizeof(kver)) {
642 pr_warning("invalid kver section in %s\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000643 return -LIBBPF_ERRNO__FORMAT;
Wang Nancb1e5e92015-07-01 02:13:57 +0000644 }
645 memcpy(&kver, data, sizeof(kver));
646 obj->kern_version = kver;
647 pr_debug("kernel version of %s is %x\n", obj->path,
648 obj->kern_version);
649 return 0;
650}
651
Eric Leblond4708bbd2016-11-15 04:05:47 +0000652static int compare_bpf_map(const void *_a, const void *_b)
653{
654 const struct bpf_map *a = _a;
655 const struct bpf_map *b = _b;
656
657 return a->offset - b->offset;
658}
659
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800660static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
661{
662 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
663 type == BPF_MAP_TYPE_HASH_OF_MAPS)
664 return true;
665 return false;
666}
667
Daniel Borkmann1713d682019-04-09 23:20:14 +0200668static int bpf_object_search_section_size(const struct bpf_object *obj,
669 const char *name, size_t *d_size)
670{
671 const GElf_Ehdr *ep = &obj->efile.ehdr;
672 Elf *elf = obj->efile.elf;
673 Elf_Scn *scn = NULL;
674 int idx = 0;
675
676 while ((scn = elf_nextscn(elf, scn)) != NULL) {
677 const char *sec_name;
678 Elf_Data *data;
679 GElf_Shdr sh;
680
681 idx++;
682 if (gelf_getshdr(scn, &sh) != &sh) {
683 pr_warning("failed to get section(%d) header from %s\n",
684 idx, obj->path);
685 return -EIO;
686 }
687
688 sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
689 if (!sec_name) {
690 pr_warning("failed to get section(%d) name from %s\n",
691 idx, obj->path);
692 return -EIO;
693 }
694
695 if (strcmp(name, sec_name))
696 continue;
697
698 data = elf_getdata(scn, 0);
699 if (!data) {
700 pr_warning("failed to get section(%d) data from %s(%s)\n",
701 idx, name, obj->path);
702 return -EIO;
703 }
704
705 *d_size = data->d_size;
706 return 0;
707 }
708
709 return -ENOENT;
710}
711
712int bpf_object__section_size(const struct bpf_object *obj, const char *name,
713 __u32 *size)
714{
715 int ret = -ENOENT;
716 size_t d_size;
717
718 *size = 0;
719 if (!name) {
720 return -EINVAL;
721 } else if (!strcmp(name, ".data")) {
722 if (obj->efile.data)
723 *size = obj->efile.data->d_size;
724 } else if (!strcmp(name, ".bss")) {
725 if (obj->efile.bss)
726 *size = obj->efile.bss->d_size;
727 } else if (!strcmp(name, ".rodata")) {
728 if (obj->efile.rodata)
729 *size = obj->efile.rodata->d_size;
730 } else {
731 ret = bpf_object_search_section_size(obj, name, &d_size);
732 if (!ret)
733 *size = d_size;
734 }
735
736 return *size ? 0 : ret;
737}
738
739int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
740 __u32 *off)
741{
742 Elf_Data *symbols = obj->efile.symbols;
743 const char *sname;
744 size_t si;
745
746 if (!name || !off)
747 return -EINVAL;
748
749 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
750 GElf_Sym sym;
751
752 if (!gelf_getsym(symbols, si, &sym))
753 continue;
754 if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
755 GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
756 continue;
757
758 sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
759 sym.st_name);
760 if (!sname) {
761 pr_warning("failed to get sym name string for var %s\n",
762 name);
763 return -EIO;
764 }
765 if (strcmp(name, sname) == 0) {
766 *off = sym.st_value;
767 return 0;
768 }
769 }
770
771 return -ENOENT;
772}
773
Daniel Borkmannd8599002019-04-09 23:20:13 +0200774static bool bpf_object__has_maps(const struct bpf_object *obj)
775{
776 return obj->efile.maps_shndx >= 0 ||
777 obj->efile.data_shndx >= 0 ||
778 obj->efile.rodata_shndx >= 0 ||
779 obj->efile.bss_shndx >= 0;
780}
781
782static int
783bpf_object__init_internal_map(struct bpf_object *obj, struct bpf_map *map,
784 enum libbpf_map_type type, Elf_Data *data,
785 void **data_buff)
786{
787 struct bpf_map_def *def = &map->def;
788 char map_name[BPF_OBJ_NAME_LEN];
789
790 map->libbpf_type = type;
791 map->offset = ~(typeof(map->offset))0;
792 snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name,
793 libbpf_type_to_btf_name[type]);
794 map->name = strdup(map_name);
795 if (!map->name) {
796 pr_warning("failed to alloc map name\n");
797 return -ENOMEM;
798 }
799
800 def->type = BPF_MAP_TYPE_ARRAY;
801 def->key_size = sizeof(int);
802 def->value_size = data->d_size;
803 def->max_entries = 1;
804 def->map_flags = type == LIBBPF_MAP_RODATA ?
805 BPF_F_RDONLY_PROG : 0;
806 if (data_buff) {
807 *data_buff = malloc(data->d_size);
808 if (!*data_buff) {
809 zfree(&map->name);
810 pr_warning("failed to alloc map content buffer\n");
811 return -ENOMEM;
812 }
813 memcpy(*data_buff, data->d_buf, data->d_size);
814 }
815
Andrii Nakryikoe1d1dc42019-04-16 11:47:17 -0700816 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
Daniel Borkmannd8599002019-04-09 23:20:13 +0200817 return 0;
818}
819
Eric Leblond4708bbd2016-11-15 04:05:47 +0000820static int
John Fastabendc034a172018-10-15 11:19:55 -0700821bpf_object__init_maps(struct bpf_object *obj, int flags)
Eric Leblond4708bbd2016-11-15 04:05:47 +0000822{
Andrey Ignatovd5adbdd2019-04-10 18:36:43 -0700823 int i, map_idx, map_def_sz = 0, nr_syms, nr_maps = 0, nr_maps_glob = 0;
John Fastabendc034a172018-10-15 11:19:55 -0700824 bool strict = !(flags & MAPS_RELAX_COMPAT);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000825 Elf_Data *symbols = obj->efile.symbols;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200826 Elf_Data *data = NULL;
827 int ret = 0;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000828
Eric Leblond4708bbd2016-11-15 04:05:47 +0000829 if (!symbols)
830 return -EINVAL;
Daniel Borkmannd8599002019-04-09 23:20:13 +0200831 nr_syms = symbols->d_size / sizeof(GElf_Sym);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000832
Daniel Borkmannd8599002019-04-09 23:20:13 +0200833 if (obj->efile.maps_shndx >= 0) {
834 Elf_Scn *scn = elf_getscn(obj->efile.elf,
835 obj->efile.maps_shndx);
836
837 if (scn)
838 data = elf_getdata(scn, NULL);
839 if (!scn || !data) {
840 pr_warning("failed to get Elf_Data from map section %d\n",
841 obj->efile.maps_shndx);
842 return -EINVAL;
843 }
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000844 }
845
Eric Leblond4708bbd2016-11-15 04:05:47 +0000846 /*
847 * Count number of maps. Each map has a name.
848 * Array of maps is not supported: only the first element is
849 * considered.
850 *
851 * TODO: Detect array of map and report error.
852 */
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200853 if (obj->caps.global_data) {
854 if (obj->efile.data_shndx >= 0)
855 nr_maps_glob++;
856 if (obj->efile.rodata_shndx >= 0)
857 nr_maps_glob++;
858 if (obj->efile.bss_shndx >= 0)
859 nr_maps_glob++;
860 }
861
Daniel Borkmannd8599002019-04-09 23:20:13 +0200862 for (i = 0; data && i < nr_syms; i++) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000863 GElf_Sym sym;
864
865 if (!gelf_getsym(symbols, i, &sym))
866 continue;
867 if (sym.st_shndx != obj->efile.maps_shndx)
868 continue;
869 nr_maps++;
870 }
871
Daniel Borkmannd8599002019-04-09 23:20:13 +0200872 if (!nr_maps && !nr_maps_glob)
Eric Leblond4708bbd2016-11-15 04:05:47 +0000873 return 0;
Wang Nan9d759a92015-11-27 08:47:35 +0000874
Craig Gallekb13c5c12017-10-05 10:41:57 -0400875 /* Assume equally sized map definitions */
Daniel Borkmannd8599002019-04-09 23:20:13 +0200876 if (data) {
Daniel Borkmann4f8827d2019-04-24 00:45:57 +0200877 pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
878 nr_maps, data->d_size);
879
Daniel Borkmannd8599002019-04-09 23:20:13 +0200880 map_def_sz = data->d_size / nr_maps;
881 if (!data->d_size || (data->d_size % nr_maps) != 0) {
882 pr_warning("unable to determine map definition size "
883 "section %s, %d maps in %zd bytes\n",
884 obj->path, nr_maps, data->d_size);
885 return -EINVAL;
886 }
Craig Gallekb13c5c12017-10-05 10:41:57 -0400887 }
888
Daniel Borkmannd8599002019-04-09 23:20:13 +0200889 nr_maps += nr_maps_glob;
Wang Nan9d759a92015-11-27 08:47:35 +0000890 obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
891 if (!obj->maps) {
892 pr_warning("alloc maps for object failed\n");
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000893 return -ENOMEM;
894 }
Wang Nan9d759a92015-11-27 08:47:35 +0000895 obj->nr_maps = nr_maps;
Wang Nan0b3d1ef2015-07-01 02:13:58 +0000896
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800897 for (i = 0; i < nr_maps; i++) {
898 /*
899 * fill all fd with -1 so won't close incorrect
900 * fd (fd=0 is stdin) when failure (zclose won't close
901 * negative fd)).
902 */
Wang Nan9d759a92015-11-27 08:47:35 +0000903 obj->maps[i].fd = -1;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -0800904 obj->maps[i].inner_map_fd = -1;
905 }
Wang Nan9d759a92015-11-27 08:47:35 +0000906
Eric Leblond4708bbd2016-11-15 04:05:47 +0000907 /*
908 * Fill obj->maps using data in "maps" section.
909 */
Daniel Borkmannd8599002019-04-09 23:20:13 +0200910 for (i = 0, map_idx = 0; data && i < nr_syms; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +0000911 GElf_Sym sym;
Wang Nan561bbcc2015-11-27 08:47:36 +0000912 const char *map_name;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000913 struct bpf_map_def *def;
Wang Nan561bbcc2015-11-27 08:47:36 +0000914
915 if (!gelf_getsym(symbols, i, &sym))
916 continue;
Wang Nan666810e2016-01-25 09:55:49 +0000917 if (sym.st_shndx != obj->efile.maps_shndx)
Wang Nan561bbcc2015-11-27 08:47:36 +0000918 continue;
919
920 map_name = elf_strptr(obj->efile.elf,
Wang Nan77ba9a52015-12-08 02:25:30 +0000921 obj->efile.strtabidx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000922 sym.st_name);
Andrii Nakryikoc51829b2019-05-29 10:36:06 -0700923 if (!map_name) {
924 pr_warning("failed to get map #%d name sym string for obj %s\n",
925 map_idx, obj->path);
926 return -LIBBPF_ERRNO__FORMAT;
927 }
Daniel Borkmannd8599002019-04-09 23:20:13 +0200928
929 obj->maps[map_idx].libbpf_type = LIBBPF_MAP_UNSPEC;
Eric Leblond4708bbd2016-11-15 04:05:47 +0000930 obj->maps[map_idx].offset = sym.st_value;
Craig Gallekb13c5c12017-10-05 10:41:57 -0400931 if (sym.st_value + map_def_sz > data->d_size) {
Eric Leblond4708bbd2016-11-15 04:05:47 +0000932 pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
933 obj->path, map_name);
934 return -EINVAL;
Wang Nan561bbcc2015-11-27 08:47:36 +0000935 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000936
Wang Nan561bbcc2015-11-27 08:47:36 +0000937 obj->maps[map_idx].name = strdup(map_name);
Wang Nan973170e2015-12-08 02:25:29 +0000938 if (!obj->maps[map_idx].name) {
939 pr_warning("failed to alloc map name\n");
940 return -ENOMEM;
941 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000942 pr_debug("map %d is \"%s\"\n", map_idx,
Wang Nan561bbcc2015-11-27 08:47:36 +0000943 obj->maps[map_idx].name);
Eric Leblond4708bbd2016-11-15 04:05:47 +0000944 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
Craig Gallekb13c5c12017-10-05 10:41:57 -0400945 /*
946 * If the definition of the map in the object file fits in
947 * bpf_map_def, copy it. Any extra fields in our version
948 * of bpf_map_def will default to zero as a result of the
949 * calloc above.
950 */
951 if (map_def_sz <= sizeof(struct bpf_map_def)) {
952 memcpy(&obj->maps[map_idx].def, def, map_def_sz);
953 } else {
954 /*
955 * Here the map structure being read is bigger than what
956 * we expect, truncate if the excess bits are all zero.
957 * If they are not zero, reject this map as
958 * incompatible.
959 */
960 char *b;
961 for (b = ((char *)def) + sizeof(struct bpf_map_def);
962 b < ((char *)def) + map_def_sz; b++) {
963 if (*b != 0) {
964 pr_warning("maps section in %s: \"%s\" "
965 "has unrecognized, non-zero "
966 "options\n",
967 obj->path, map_name);
John Fastabendc034a172018-10-15 11:19:55 -0700968 if (strict)
969 return -EINVAL;
Craig Gallekb13c5c12017-10-05 10:41:57 -0400970 }
971 }
972 memcpy(&obj->maps[map_idx].def, def,
973 sizeof(struct bpf_map_def));
974 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000975 map_idx++;
Wang Nan561bbcc2015-11-27 08:47:36 +0000976 }
Eric Leblond4708bbd2016-11-15 04:05:47 +0000977
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200978 if (!obj->caps.global_data)
979 goto finalize;
980
Daniel Borkmannd8599002019-04-09 23:20:13 +0200981 /*
982 * Populate rest of obj->maps with libbpf internal maps.
983 */
984 if (obj->efile.data_shndx >= 0)
985 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
986 LIBBPF_MAP_DATA,
987 obj->efile.data,
988 &obj->sections.data);
989 if (!ret && obj->efile.rodata_shndx >= 0)
990 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
991 LIBBPF_MAP_RODATA,
992 obj->efile.rodata,
993 &obj->sections.rodata);
994 if (!ret && obj->efile.bss_shndx >= 0)
995 ret = bpf_object__init_internal_map(obj, &obj->maps[map_idx++],
996 LIBBPF_MAP_BSS,
997 obj->efile.bss, NULL);
Daniel Borkmann8837fe52019-04-24 00:45:56 +0200998finalize:
Daniel Borkmannd8599002019-04-09 23:20:13 +0200999 if (!ret)
1000 qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
1001 compare_bpf_map);
1002 return ret;
Wang Nan561bbcc2015-11-27 08:47:36 +00001003}
1004
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01001005static bool section_have_execinstr(struct bpf_object *obj, int idx)
1006{
1007 Elf_Scn *scn;
1008 GElf_Shdr sh;
1009
1010 scn = elf_getscn(obj->efile.elf, idx);
1011 if (!scn)
1012 return false;
1013
1014 if (gelf_getshdr(scn, &sh) != &sh)
1015 return false;
1016
1017 if (sh.sh_flags & SHF_EXECINSTR)
1018 return true;
1019
1020 return false;
1021}
1022
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001023static void bpf_object__sanitize_btf(struct bpf_object *obj)
1024{
1025 bool has_datasec = obj->caps.btf_datasec;
1026 bool has_func = obj->caps.btf_func;
1027 struct btf *btf = obj->btf;
1028 struct btf_type *t;
1029 int i, j, vlen;
1030 __u16 kind;
1031
1032 if (!obj->btf || (has_func && has_datasec))
1033 return;
1034
1035 for (i = 1; i <= btf__get_nr_types(btf); i++) {
1036 t = (struct btf_type *)btf__type_by_id(btf, i);
1037 kind = BTF_INFO_KIND(t->info);
1038
1039 if (!has_datasec && kind == BTF_KIND_VAR) {
1040 /* replace VAR with INT */
1041 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1042 t->size = sizeof(int);
1043 *(int *)(t+1) = BTF_INT_ENC(0, 0, 32);
1044 } else if (!has_datasec && kind == BTF_KIND_DATASEC) {
1045 /* replace DATASEC with STRUCT */
1046 struct btf_var_secinfo *v = (void *)(t + 1);
1047 struct btf_member *m = (void *)(t + 1);
1048 struct btf_type *vt;
1049 char *name;
1050
1051 name = (char *)btf__name_by_offset(btf, t->name_off);
1052 while (*name) {
1053 if (*name == '.')
1054 *name = '_';
1055 name++;
1056 }
1057
1058 vlen = BTF_INFO_VLEN(t->info);
1059 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
1060 for (j = 0; j < vlen; j++, v++, m++) {
1061 /* order of field assignments is important */
1062 m->offset = v->offset * 8;
1063 m->type = v->type;
1064 /* preserve variable name as member name */
1065 vt = (void *)btf__type_by_id(btf, v->type);
1066 m->name_off = vt->name_off;
1067 }
1068 } else if (!has_func && kind == BTF_KIND_FUNC_PROTO) {
1069 /* replace FUNC_PROTO with ENUM */
1070 vlen = BTF_INFO_VLEN(t->info);
1071 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
1072 t->size = sizeof(__u32); /* kernel enforced */
1073 } else if (!has_func && kind == BTF_KIND_FUNC) {
1074 /* replace FUNC with TYPEDEF */
1075 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
1076 }
1077 }
1078}
1079
1080static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
1081{
1082 if (!obj->btf_ext)
1083 return;
1084
1085 if (!obj->caps.btf_func) {
1086 btf_ext__free(obj->btf_ext);
1087 obj->btf_ext = NULL;
1088 }
1089}
1090
John Fastabendc034a172018-10-15 11:19:55 -07001091static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
Wang Nan29603662015-07-01 02:13:56 +00001092{
1093 Elf *elf = obj->efile.elf;
1094 GElf_Ehdr *ep = &obj->efile.ehdr;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001095 Elf_Data *btf_ext_data = NULL;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001096 Elf_Data *btf_data = NULL;
Wang Nan29603662015-07-01 02:13:56 +00001097 Elf_Scn *scn = NULL;
Wang Nan666810e2016-01-25 09:55:49 +00001098 int idx = 0, err = 0;
Wang Nan29603662015-07-01 02:13:56 +00001099
1100 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
1101 if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
1102 pr_warning("failed to get e_shstrndx from %s\n",
1103 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001104 return -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001105 }
1106
1107 while ((scn = elf_nextscn(elf, scn)) != NULL) {
1108 char *name;
1109 GElf_Shdr sh;
1110 Elf_Data *data;
1111
1112 idx++;
1113 if (gelf_getshdr(scn, &sh) != &sh) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001114 pr_warning("failed to get section(%d) header from %s\n",
1115 idx, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001116 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001117 goto out;
1118 }
1119
1120 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1121 if (!name) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001122 pr_warning("failed to get section(%d) name from %s\n",
1123 idx, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001124 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001125 goto out;
1126 }
1127
1128 data = elf_getdata(scn, 0);
1129 if (!data) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001130 pr_warning("failed to get section(%d) data from %s(%s)\n",
1131 idx, name, obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001132 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan29603662015-07-01 02:13:56 +00001133 goto out;
1134 }
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001135 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
1136 idx, name, (unsigned long)data->d_size,
Wang Nan29603662015-07-01 02:13:56 +00001137 (int)sh.sh_link, (unsigned long)sh.sh_flags,
1138 (int)sh.sh_type);
Wang Nancb1e5e92015-07-01 02:13:57 +00001139
Daniel Borkmann1713d682019-04-09 23:20:14 +02001140 if (strcmp(name, "license") == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00001141 err = bpf_object__init_license(obj,
1142 data->d_buf,
1143 data->d_size);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001144 } else if (strcmp(name, "version") == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00001145 err = bpf_object__init_kversion(obj,
1146 data->d_buf,
1147 data->d_size);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001148 } else if (strcmp(name, "maps") == 0) {
Wang Nan666810e2016-01-25 09:55:49 +00001149 obj->efile.maps_shndx = idx;
Daniel Borkmann1713d682019-04-09 23:20:14 +02001150 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
1151 btf_data = data;
Yonghong Song2993e052018-11-19 15:29:16 -08001152 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001153 btf_ext_data = data;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001154 } else if (sh.sh_type == SHT_SYMTAB) {
Wang Nanbec7d682015-07-01 02:13:59 +00001155 if (obj->efile.symbols) {
1156 pr_warning("bpf: multiple SYMTAB in %s\n",
1157 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001158 err = -LIBBPF_ERRNO__FORMAT;
Wang Nan77ba9a52015-12-08 02:25:30 +00001159 } else {
Wang Nanbec7d682015-07-01 02:13:59 +00001160 obj->efile.symbols = data;
Wang Nan77ba9a52015-12-08 02:25:30 +00001161 obj->efile.strtabidx = sh.sh_link;
1162 }
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001163 } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
1164 if (sh.sh_flags & SHF_EXECINSTR) {
1165 if (strcmp(name, ".text") == 0)
1166 obj->efile.text_shndx = idx;
1167 err = bpf_object__add_program(obj, data->d_buf,
1168 data->d_size, name, idx);
1169 if (err) {
1170 char errmsg[STRERR_BUFSIZE];
1171 char *cp = libbpf_strerror_r(-err, errmsg,
1172 sizeof(errmsg));
Wang Nan6371ca3b2015-11-06 13:49:37 +00001173
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001174 pr_warning("failed to alloc program %s (%s): %s",
1175 name, obj->path, cp);
1176 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001177 } else if (strcmp(name, ".data") == 0) {
1178 obj->efile.data = data;
1179 obj->efile.data_shndx = idx;
1180 } else if (strcmp(name, ".rodata") == 0) {
1181 obj->efile.rodata = data;
1182 obj->efile.rodata_shndx = idx;
1183 } else {
1184 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nana5b8bd42015-07-01 02:14:00 +00001185 }
Wang Nanb62f06e2015-07-01 02:14:01 +00001186 } else if (sh.sh_type == SHT_REL) {
1187 void *reloc = obj->efile.reloc;
1188 int nr_reloc = obj->efile.nr_reloc + 1;
Jesper Dangaard Brouere3d91b02018-02-08 12:48:32 +01001189 int sec = sh.sh_info; /* points to other section */
1190
1191 /* Only do relo for section with exec instructions */
1192 if (!section_have_execinstr(obj, sec)) {
1193 pr_debug("skip relo %s(%d) for section(%d)\n",
1194 name, idx, sec);
1195 continue;
1196 }
Wang Nanb62f06e2015-07-01 02:14:01 +00001197
Jakub Kicinski531b0142018-07-10 14:43:05 -07001198 reloc = reallocarray(reloc, nr_reloc,
1199 sizeof(*obj->efile.reloc));
Wang Nanb62f06e2015-07-01 02:14:01 +00001200 if (!reloc) {
1201 pr_warning("realloc failed\n");
1202 err = -ENOMEM;
1203 } else {
1204 int n = nr_reloc - 1;
1205
1206 obj->efile.reloc = reloc;
1207 obj->efile.nr_reloc = nr_reloc;
1208
1209 obj->efile.reloc[n].shdr = sh;
1210 obj->efile.reloc[n].data = data;
1211 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001212 } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
1213 obj->efile.bss = data;
1214 obj->efile.bss_shndx = idx;
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01001215 } else {
1216 pr_debug("skip section(%d) %s\n", idx, name);
Wang Nanbec7d682015-07-01 02:13:59 +00001217 }
Wang Nancb1e5e92015-07-01 02:13:57 +00001218 if (err)
1219 goto out;
Wang Nan29603662015-07-01 02:13:56 +00001220 }
Wang Nan561bbcc2015-11-27 08:47:36 +00001221
Wang Nan77ba9a52015-12-08 02:25:30 +00001222 if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
1223 pr_warning("Corrupted ELF file: index of strtab invalid\n");
1224 return LIBBPF_ERRNO__FORMAT;
1225 }
Daniel Borkmann1713d682019-04-09 23:20:14 +02001226 if (btf_data) {
1227 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
1228 if (IS_ERR(obj->btf)) {
1229 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1230 BTF_ELF_SEC, PTR_ERR(obj->btf));
1231 obj->btf = NULL;
1232 } else {
1233 err = btf__finalize_data(obj, obj->btf);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001234 if (!err) {
1235 bpf_object__sanitize_btf(obj);
Daniel Borkmann1713d682019-04-09 23:20:14 +02001236 err = btf__load(obj->btf);
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001237 }
Daniel Borkmann1713d682019-04-09 23:20:14 +02001238 if (err) {
1239 pr_warning("Error finalizing and loading %s into kernel: %d. Ignored and continue.\n",
1240 BTF_ELF_SEC, err);
1241 btf__free(obj->btf);
1242 obj->btf = NULL;
1243 err = 0;
1244 }
1245 }
1246 }
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001247 if (btf_ext_data) {
1248 if (!obj->btf) {
1249 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
1250 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
1251 } else {
1252 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
Yonghong Song8461ef82019-02-01 16:14:14 -08001253 btf_ext_data->d_size);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001254 if (IS_ERR(obj->btf_ext)) {
1255 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
1256 BTF_EXT_ELF_SEC,
1257 PTR_ERR(obj->btf_ext));
1258 obj->btf_ext = NULL;
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001259 } else {
1260 bpf_object__sanitize_btf_ext(obj);
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001261 }
1262 }
1263 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001264 if (bpf_object__has_maps(obj)) {
John Fastabendc034a172018-10-15 11:19:55 -07001265 err = bpf_object__init_maps(obj, flags);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -07001266 if (err)
1267 goto out;
1268 }
1269 err = bpf_object__init_prog_names(obj);
Wang Nan29603662015-07-01 02:13:56 +00001270out:
1271 return err;
1272}
1273
Wang Nan34090912015-07-01 02:14:02 +00001274static struct bpf_program *
1275bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
1276{
1277 struct bpf_program *prog;
1278 size_t i;
1279
1280 for (i = 0; i < obj->nr_programs; i++) {
1281 prog = &obj->programs[i];
1282 if (prog->idx == idx)
1283 return prog;
1284 }
1285 return NULL;
1286}
1287
Jakub Kicinski6d4b1982018-07-26 14:32:19 -07001288struct bpf_program *
1289bpf_object__find_program_by_title(struct bpf_object *obj, const char *title)
1290{
1291 struct bpf_program *pos;
1292
1293 bpf_object__for_each_program(pos, obj) {
1294 if (pos->section_name && !strcmp(pos->section_name, title))
1295 return pos;
1296 }
1297 return NULL;
1298}
1299
Daniel Borkmannd8599002019-04-09 23:20:13 +02001300static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
1301 int shndx)
1302{
1303 return shndx == obj->efile.data_shndx ||
1304 shndx == obj->efile.bss_shndx ||
1305 shndx == obj->efile.rodata_shndx;
1306}
1307
1308static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
1309 int shndx)
1310{
1311 return shndx == obj->efile.maps_shndx;
1312}
1313
1314static bool bpf_object__relo_in_known_section(const struct bpf_object *obj,
1315 int shndx)
1316{
1317 return shndx == obj->efile.text_shndx ||
1318 bpf_object__shndx_is_maps(obj, shndx) ||
1319 bpf_object__shndx_is_data(obj, shndx);
1320}
1321
1322static enum libbpf_map_type
1323bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
1324{
1325 if (shndx == obj->efile.data_shndx)
1326 return LIBBPF_MAP_DATA;
1327 else if (shndx == obj->efile.bss_shndx)
1328 return LIBBPF_MAP_BSS;
1329 else if (shndx == obj->efile.rodata_shndx)
1330 return LIBBPF_MAP_RODATA;
1331 else
1332 return LIBBPF_MAP_UNSPEC;
1333}
1334
Wang Nan34090912015-07-01 02:14:02 +00001335static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001336bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
1337 Elf_Data *data, struct bpf_object *obj)
Wang Nan34090912015-07-01 02:14:02 +00001338{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001339 Elf_Data *symbols = obj->efile.symbols;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001340 struct bpf_map *maps = obj->maps;
1341 size_t nr_maps = obj->nr_maps;
Wang Nan34090912015-07-01 02:14:02 +00001342 int i, nrels;
1343
1344 pr_debug("collecting relocating info for: '%s'\n",
1345 prog->section_name);
1346 nrels = shdr->sh_size / shdr->sh_entsize;
1347
1348 prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
1349 if (!prog->reloc_desc) {
1350 pr_warning("failed to alloc memory in relocation\n");
1351 return -ENOMEM;
1352 }
1353 prog->nr_reloc = nrels;
1354
1355 for (i = 0; i < nrels; i++) {
1356 GElf_Sym sym;
1357 GElf_Rel rel;
1358 unsigned int insn_idx;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001359 unsigned int shdr_idx;
Wang Nan34090912015-07-01 02:14:02 +00001360 struct bpf_insn *insns = prog->insns;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001361 enum libbpf_map_type type;
1362 const char *name;
Wang Nan34090912015-07-01 02:14:02 +00001363 size_t map_idx;
1364
1365 if (!gelf_getrel(data, i, &rel)) {
1366 pr_warning("relocation: failed to get %d reloc\n", i);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001367 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00001368 }
1369
Wang Nan34090912015-07-01 02:14:02 +00001370 if (!gelf_getsym(symbols,
1371 GELF_R_SYM(rel.r_info),
1372 &sym)) {
1373 pr_warning("relocation: symbol %"PRIx64" not found\n",
1374 GELF_R_SYM(rel.r_info));
Wang Nan6371ca3b2015-11-06 13:49:37 +00001375 return -LIBBPF_ERRNO__FORMAT;
Wang Nan34090912015-07-01 02:14:02 +00001376 }
1377
Daniel Borkmannd8599002019-04-09 23:20:13 +02001378 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1379 sym.st_name) ? : "<?>";
1380
1381 pr_debug("relo for %lld value %lld name %d (\'%s\')\n",
1382 (long long) (rel.r_info >> 32),
1383 (long long) sym.st_value, sym.st_name, name);
1384
1385 shdr_idx = sym.st_shndx;
1386 if (!bpf_object__relo_in_known_section(obj, shdr_idx)) {
1387 pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n",
1388 prog->section_name, shdr_idx);
Wang Nan666810e2016-01-25 09:55:49 +00001389 return -LIBBPF_ERRNO__RELOC;
1390 }
1391
1392 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
1393 pr_debug("relocation: insn_idx=%u\n", insn_idx);
1394
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001395 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
1396 if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
1397 pr_warning("incorrect bpf_call opcode\n");
1398 return -LIBBPF_ERRNO__RELOC;
1399 }
1400 prog->reloc_desc[i].type = RELO_CALL;
1401 prog->reloc_desc[i].insn_idx = insn_idx;
1402 prog->reloc_desc[i].text_off = sym.st_value;
Jakub Kicinski9a94f272018-06-28 14:41:38 -07001403 obj->has_pseudo_calls = true;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001404 continue;
1405 }
1406
Wang Nan34090912015-07-01 02:14:02 +00001407 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
1408 pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
1409 insn_idx, insns[insn_idx].code);
Wang Nan6371ca3b2015-11-06 13:49:37 +00001410 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00001411 }
1412
Daniel Borkmannd8599002019-04-09 23:20:13 +02001413 if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
1414 bpf_object__shndx_is_data(obj, shdr_idx)) {
1415 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001416 if (type != LIBBPF_MAP_UNSPEC) {
1417 if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
1418 pr_warning("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1419 name, insn_idx, insns[insn_idx].code);
1420 return -LIBBPF_ERRNO__RELOC;
1421 }
1422 if (!obj->caps.global_data) {
1423 pr_warning("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
1424 name, insn_idx);
1425 return -LIBBPF_ERRNO__RELOC;
1426 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001427 }
1428
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001429 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02001430 if (maps[map_idx].libbpf_type != type)
1431 continue;
1432 if (type != LIBBPF_MAP_UNSPEC ||
1433 (type == LIBBPF_MAP_UNSPEC &&
1434 maps[map_idx].offset == sym.st_value)) {
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001435 pr_debug("relocation: find map %zd (%s) for insn %u\n",
1436 map_idx, maps[map_idx].name, insn_idx);
1437 break;
1438 }
Joe Stringer94e5ade2017-01-22 17:11:22 -08001439 }
Joe Stringer94e5ade2017-01-22 17:11:22 -08001440
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001441 if (map_idx >= nr_maps) {
1442 pr_warning("bpf relocation: map_idx %d large than %d\n",
1443 (int)map_idx, (int)nr_maps - 1);
1444 return -LIBBPF_ERRNO__RELOC;
1445 }
Wang Nan34090912015-07-01 02:14:02 +00001446
Daniel Borkmannd8599002019-04-09 23:20:13 +02001447 prog->reloc_desc[i].type = type != LIBBPF_MAP_UNSPEC ?
1448 RELO_DATA : RELO_LD64;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001449 prog->reloc_desc[i].insn_idx = insn_idx;
1450 prog->reloc_desc[i].map_idx = map_idx;
1451 }
Wang Nan34090912015-07-01 02:14:02 +00001452 }
1453 return 0;
1454}
1455
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001456static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
1457{
1458 struct bpf_map_def *def = &map->def;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001459 __u32 key_type_id = 0, value_type_id = 0;
Yonghong Song96408c42019-02-04 11:00:58 -08001460 int ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001461
Daniel Borkmannd8599002019-04-09 23:20:13 +02001462 if (!bpf_map__is_internal(map)) {
1463 ret = btf__get_map_kv_tids(btf, map->name, def->key_size,
1464 def->value_size, &key_type_id,
1465 &value_type_id);
1466 } else {
1467 /*
1468 * LLVM annotates global data differently in BTF, that is,
1469 * only as '.data', '.bss' or '.rodata'.
1470 */
1471 ret = btf__find_by_name(btf,
1472 libbpf_type_to_btf_name[map->libbpf_type]);
1473 }
1474 if (ret < 0)
Yonghong Song96408c42019-02-04 11:00:58 -08001475 return ret;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001476
Yonghong Song96408c42019-02-04 11:00:58 -08001477 map->btf_key_type_id = key_type_id;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001478 map->btf_value_type_id = bpf_map__is_internal(map) ?
1479 ret : value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001480 return 0;
1481}
1482
Jakub Kicinski26736eb2018-07-10 14:43:06 -07001483int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1484{
1485 struct bpf_map_info info = {};
1486 __u32 len = sizeof(info);
1487 int new_fd, err;
1488 char *new_name;
1489
1490 err = bpf_obj_get_info_by_fd(fd, &info, &len);
1491 if (err)
1492 return err;
1493
1494 new_name = strdup(info.name);
1495 if (!new_name)
1496 return -errno;
1497
1498 new_fd = open("/", O_RDONLY | O_CLOEXEC);
1499 if (new_fd < 0)
1500 goto err_free_new_name;
1501
1502 new_fd = dup3(fd, new_fd, O_CLOEXEC);
1503 if (new_fd < 0)
1504 goto err_close_new_fd;
1505
1506 err = zclose(map->fd);
1507 if (err)
1508 goto err_close_new_fd;
1509 free(map->name);
1510
1511 map->fd = new_fd;
1512 map->name = new_name;
1513 map->def.type = info.type;
1514 map->def.key_size = info.key_size;
1515 map->def.value_size = info.value_size;
1516 map->def.max_entries = info.max_entries;
1517 map->def.map_flags = info.map_flags;
1518 map->btf_key_type_id = info.btf_key_type_id;
1519 map->btf_value_type_id = info.btf_value_type_id;
1520
1521 return 0;
1522
1523err_close_new_fd:
1524 close(new_fd);
1525err_free_new_name:
1526 free(new_name);
1527 return -errno;
1528}
1529
Andrey Ignatov1a11a4c2019-02-14 15:01:42 -08001530int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
1531{
1532 if (!map || !max_entries)
1533 return -EINVAL;
1534
1535 /* If map already created, its attributes can't be changed. */
1536 if (map->fd >= 0)
1537 return -EBUSY;
1538
1539 map->def.max_entries = max_entries;
1540
1541 return 0;
1542}
1543
Wang Nan52d33522015-07-01 02:14:04 +00001544static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08001545bpf_object__probe_name(struct bpf_object *obj)
1546{
1547 struct bpf_load_program_attr attr;
1548 char *cp, errmsg[STRERR_BUFSIZE];
1549 struct bpf_insn insns[] = {
1550 BPF_MOV64_IMM(BPF_REG_0, 0),
1551 BPF_EXIT_INSN(),
1552 };
1553 int ret;
1554
1555 /* make sure basic loading works */
1556
1557 memset(&attr, 0, sizeof(attr));
1558 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1559 attr.insns = insns;
1560 attr.insns_cnt = ARRAY_SIZE(insns);
1561 attr.license = "GPL";
1562
1563 ret = bpf_load_program_xattr(&attr, NULL, 0);
1564 if (ret < 0) {
1565 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1566 pr_warning("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
1567 __func__, cp, errno);
1568 return -errno;
1569 }
1570 close(ret);
1571
1572 /* now try the same program, but with the name */
1573
1574 attr.name = "test";
1575 ret = bpf_load_program_xattr(&attr, NULL, 0);
1576 if (ret >= 0) {
1577 obj->caps.name = 1;
1578 close(ret);
1579 }
1580
1581 return 0;
1582}
1583
1584static int
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001585bpf_object__probe_global_data(struct bpf_object *obj)
1586{
1587 struct bpf_load_program_attr prg_attr;
1588 struct bpf_create_map_attr map_attr;
1589 char *cp, errmsg[STRERR_BUFSIZE];
1590 struct bpf_insn insns[] = {
1591 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
1592 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
1593 BPF_MOV64_IMM(BPF_REG_0, 0),
1594 BPF_EXIT_INSN(),
1595 };
1596 int ret, map;
1597
1598 memset(&map_attr, 0, sizeof(map_attr));
1599 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
1600 map_attr.key_size = sizeof(int);
1601 map_attr.value_size = 32;
1602 map_attr.max_entries = 1;
1603
1604 map = bpf_create_map_xattr(&map_attr);
1605 if (map < 0) {
1606 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1607 pr_warning("Error in %s():%s(%d). Couldn't create simple array map.\n",
1608 __func__, cp, errno);
1609 return -errno;
1610 }
1611
1612 insns[0].imm = map;
1613
1614 memset(&prg_attr, 0, sizeof(prg_attr));
1615 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1616 prg_attr.insns = insns;
1617 prg_attr.insns_cnt = ARRAY_SIZE(insns);
1618 prg_attr.license = "GPL";
1619
1620 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
1621 if (ret >= 0) {
1622 obj->caps.global_data = 1;
1623 close(ret);
1624 }
1625
1626 close(map);
1627 return 0;
1628}
1629
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001630static int bpf_object__probe_btf_func(struct bpf_object *obj)
1631{
1632 const char strs[] = "\0int\0x\0a";
1633 /* void x(int a) {} */
1634 __u32 types[] = {
1635 /* int */
1636 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1637 /* FUNC_PROTO */ /* [2] */
1638 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
1639 BTF_PARAM_ENC(7, 1),
1640 /* FUNC x */ /* [3] */
1641 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
1642 };
1643 int res;
1644
1645 res = libbpf__probe_raw_btf((char *)types, sizeof(types),
1646 strs, sizeof(strs));
1647 if (res < 0)
1648 return res;
1649 if (res > 0)
1650 obj->caps.btf_func = 1;
1651 return 0;
1652}
1653
1654static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
1655{
1656 const char strs[] = "\0x\0.data";
1657 /* static int a; */
1658 __u32 types[] = {
1659 /* int */
1660 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1661 /* VAR x */ /* [2] */
1662 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
1663 BTF_VAR_STATIC,
1664 /* DATASEC val */ /* [3] */
1665 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
1666 BTF_VAR_SECINFO_ENC(2, 0, 4),
1667 };
1668 int res;
1669
1670 res = libbpf__probe_raw_btf((char *)types, sizeof(types),
1671 strs, sizeof(strs));
1672 if (res < 0)
1673 return res;
1674 if (res > 0)
1675 obj->caps.btf_datasec = 1;
1676 return 0;
1677}
1678
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001679static int
Stanislav Fomichev47eff612018-11-20 17:11:19 -08001680bpf_object__probe_caps(struct bpf_object *obj)
1681{
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001682 int (*probe_fn[])(struct bpf_object *obj) = {
1683 bpf_object__probe_name,
1684 bpf_object__probe_global_data,
Andrii Nakryikod7c4b392019-05-10 14:13:15 -07001685 bpf_object__probe_btf_func,
1686 bpf_object__probe_btf_datasec,
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001687 };
1688 int i, ret;
1689
1690 for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
1691 ret = probe_fn[i](obj);
1692 if (ret < 0)
Stanislav Fomichev15ea1642019-05-14 20:38:49 -07001693 pr_debug("Probe #%d failed with %d.\n", i, ret);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02001694 }
1695
1696 return 0;
Stanislav Fomichev47eff612018-11-20 17:11:19 -08001697}
1698
1699static int
Daniel Borkmannd8599002019-04-09 23:20:13 +02001700bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
1701{
1702 char *cp, errmsg[STRERR_BUFSIZE];
1703 int err, zero = 0;
1704 __u8 *data;
1705
1706 /* Nothing to do here since kernel already zero-initializes .bss map. */
1707 if (map->libbpf_type == LIBBPF_MAP_BSS)
1708 return 0;
1709
1710 data = map->libbpf_type == LIBBPF_MAP_DATA ?
1711 obj->sections.data : obj->sections.rodata;
1712
1713 err = bpf_map_update_elem(map->fd, &zero, data, 0);
1714 /* Freeze .rodata map as read-only from syscall side. */
1715 if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) {
1716 err = bpf_map_freeze(map->fd);
1717 if (err) {
1718 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1719 pr_warning("Error freezing map(%s) as read-only: %s\n",
1720 map->name, cp);
1721 err = 0;
1722 }
1723 }
1724 return err;
1725}
1726
1727static int
Wang Nan52d33522015-07-01 02:14:04 +00001728bpf_object__create_maps(struct bpf_object *obj)
1729{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001730 struct bpf_create_map_attr create_attr = {};
Wang Nan52d33522015-07-01 02:14:04 +00001731 unsigned int i;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001732 int err;
Wang Nan52d33522015-07-01 02:14:04 +00001733
Wang Nan9d759a92015-11-27 08:47:35 +00001734 for (i = 0; i < obj->nr_maps; i++) {
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001735 struct bpf_map *map = &obj->maps[i];
1736 struct bpf_map_def *def = &map->def;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02001737 char *cp, errmsg[STRERR_BUFSIZE];
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001738 int *pfd = &map->fd;
Wang Nan52d33522015-07-01 02:14:04 +00001739
Jakub Kicinski26736eb2018-07-10 14:43:06 -07001740 if (map->fd >= 0) {
1741 pr_debug("skip map create (preset) %s: fd=%d\n",
1742 map->name, map->fd);
1743 continue;
1744 }
1745
Stanislav Fomichev94cb3102018-11-20 17:11:20 -08001746 if (obj->caps.name)
1747 create_attr.name = map->name;
David Beckettf0307a72018-05-16 14:02:49 -07001748 create_attr.map_ifindex = map->map_ifindex;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001749 create_attr.map_type = def->type;
1750 create_attr.map_flags = def->map_flags;
1751 create_attr.key_size = def->key_size;
1752 create_attr.value_size = def->value_size;
1753 create_attr.max_entries = def->max_entries;
1754 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07001755 create_attr.btf_key_type_id = 0;
1756 create_attr.btf_value_type_id = 0;
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08001757 if (bpf_map_type__is_map_in_map(def->type) &&
1758 map->inner_map_fd >= 0)
1759 create_attr.inner_map_fd = map->inner_map_fd;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001760
1761 if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1762 create_attr.btf_fd = btf__fd(obj->btf);
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07001763 create_attr.btf_key_type_id = map->btf_key_type_id;
1764 create_attr.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001765 }
1766
1767 *pfd = bpf_create_map_xattr(&create_attr);
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07001768 if (*pfd < 0 && create_attr.btf_key_type_id) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07001769 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001770 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02001771 map->name, cp, errno);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001772 create_attr.btf_fd = 0;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07001773 create_attr.btf_key_type_id = 0;
1774 create_attr.btf_value_type_id = 0;
1775 map->btf_key_type_id = 0;
1776 map->btf_value_type_id = 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001777 *pfd = bpf_create_map_xattr(&create_attr);
1778 }
1779
Wang Nan52d33522015-07-01 02:14:04 +00001780 if (*pfd < 0) {
1781 size_t j;
Wang Nan52d33522015-07-01 02:14:04 +00001782
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001783 err = *pfd;
Daniel Borkmannd8599002019-04-09 23:20:13 +02001784err_out:
Andrey Ignatov24d6a802018-10-03 15:26:41 -07001785 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Eric Leblond49bf4b32017-08-20 21:48:14 +02001786 pr_warning("failed to create map (name: '%s'): %s\n",
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02001787 map->name, cp);
Wang Nan52d33522015-07-01 02:14:04 +00001788 for (j = 0; j < i; j++)
Wang Nan9d759a92015-11-27 08:47:35 +00001789 zclose(obj->maps[j].fd);
Wang Nan52d33522015-07-01 02:14:04 +00001790 return err;
1791 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001792
1793 if (bpf_map__is_internal(map)) {
1794 err = bpf_object__populate_internal_map(obj, map);
1795 if (err < 0) {
1796 zclose(*pfd);
1797 goto err_out;
1798 }
1799 }
1800
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07001801 pr_debug("create map %s: fd=%d\n", map->name, *pfd);
Wang Nan52d33522015-07-01 02:14:04 +00001802 }
1803
Wang Nan52d33522015-07-01 02:14:04 +00001804 return 0;
1805}
1806
Wang Nan8a47a6c2015-07-01 02:14:05 +00001807static int
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001808check_btf_ext_reloc_err(struct bpf_program *prog, int err,
1809 void *btf_prog_info, const char *info_name)
1810{
1811 if (err != -ENOENT) {
1812 pr_warning("Error in loading %s for sec %s.\n",
1813 info_name, prog->section_name);
1814 return err;
1815 }
1816
1817 /* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
1818
1819 if (btf_prog_info) {
1820 /*
1821 * Some info has already been found but has problem
1822 * in the last btf_ext reloc. Must have to error
1823 * out.
1824 */
1825 pr_warning("Error in relocating %s for sec %s.\n",
1826 info_name, prog->section_name);
1827 return err;
1828 }
1829
1830 /*
1831 * Have problem loading the very first info. Ignore
1832 * the rest.
1833 */
1834 pr_warning("Cannot find %s for main program sec %s. Ignore all %s.\n",
1835 info_name, prog->section_name, info_name);
1836 return 0;
1837}
1838
1839static int
1840bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
1841 const char *section_name, __u32 insn_offset)
1842{
1843 int err;
1844
1845 if (!insn_offset || prog->func_info) {
1846 /*
1847 * !insn_offset => main program
1848 *
1849 * For sub prog, the main program's func_info has to
1850 * be loaded first (i.e. prog->func_info != NULL)
1851 */
1852 err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
1853 section_name, insn_offset,
1854 &prog->func_info,
1855 &prog->func_info_cnt);
1856 if (err)
1857 return check_btf_ext_reloc_err(prog, err,
1858 prog->func_info,
1859 "bpf_func_info");
1860
1861 prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
1862 }
1863
Martin KaFai Lau3d650142018-12-07 16:42:31 -08001864 if (!insn_offset || prog->line_info) {
1865 err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
1866 section_name, insn_offset,
1867 &prog->line_info,
1868 &prog->line_info_cnt);
1869 if (err)
1870 return check_btf_ext_reloc_err(prog, err,
1871 prog->line_info,
1872 "bpf_line_info");
1873
1874 prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
1875 }
1876
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001877 if (!insn_offset)
1878 prog->btf_fd = btf__fd(obj->btf);
1879
1880 return 0;
1881}
1882
1883static int
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001884bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1885 struct reloc_desc *relo)
1886{
1887 struct bpf_insn *insn, *new_insn;
1888 struct bpf_program *text;
1889 size_t new_cnt;
Yonghong Song2993e052018-11-19 15:29:16 -08001890 int err;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001891
1892 if (relo->type != RELO_CALL)
1893 return -LIBBPF_ERRNO__RELOC;
1894
1895 if (prog->idx == obj->efile.text_shndx) {
1896 pr_warning("relo in .text insn %d into off %d\n",
1897 relo->insn_idx, relo->text_off);
1898 return -LIBBPF_ERRNO__RELOC;
1899 }
1900
1901 if (prog->main_prog_cnt == 0) {
1902 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1903 if (!text) {
1904 pr_warning("no .text section found yet relo into text exist\n");
1905 return -LIBBPF_ERRNO__RELOC;
1906 }
1907 new_cnt = prog->insns_cnt + text->insns_cnt;
Jakub Kicinski531b0142018-07-10 14:43:05 -07001908 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001909 if (!new_insn) {
1910 pr_warning("oom in prog realloc\n");
1911 return -ENOMEM;
1912 }
Yonghong Song2993e052018-11-19 15:29:16 -08001913
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001914 if (obj->btf_ext) {
1915 err = bpf_program_reloc_btf_ext(prog, obj,
1916 text->section_name,
1917 prog->insns_cnt);
1918 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08001919 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08001920 }
1921
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001922 memcpy(new_insn + prog->insns_cnt, text->insns,
1923 text->insns_cnt * sizeof(*insn));
1924 prog->insns = new_insn;
1925 prog->main_prog_cnt = prog->insns_cnt;
1926 prog->insns_cnt = new_cnt;
Jeremy Clineb1a2ce82018-02-20 01:00:07 +00001927 pr_debug("added %zd insn from %s to prog %s\n",
1928 text->insns_cnt, text->section_name,
1929 prog->section_name);
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001930 }
1931 insn = &prog->insns[relo->insn_idx];
1932 insn->imm += prog->main_prog_cnt - relo->insn_idx;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001933 return 0;
1934}
1935
1936static int
Wang Nan9d759a92015-11-27 08:47:35 +00001937bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
Wang Nan8a47a6c2015-07-01 02:14:05 +00001938{
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001939 int i, err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001940
Yonghong Song2993e052018-11-19 15:29:16 -08001941 if (!prog)
1942 return 0;
1943
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08001944 if (obj->btf_ext) {
1945 err = bpf_program_reloc_btf_ext(prog, obj,
1946 prog->section_name, 0);
1947 if (err)
Yonghong Song2993e052018-11-19 15:29:16 -08001948 return err;
Yonghong Song2993e052018-11-19 15:29:16 -08001949 }
1950
1951 if (!prog->reloc_desc)
Wang Nan8a47a6c2015-07-01 02:14:05 +00001952 return 0;
1953
1954 for (i = 0; i < prog->nr_reloc; i++) {
Daniel Borkmannd8599002019-04-09 23:20:13 +02001955 if (prog->reloc_desc[i].type == RELO_LD64 ||
1956 prog->reloc_desc[i].type == RELO_DATA) {
1957 bool relo_data = prog->reloc_desc[i].type == RELO_DATA;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001958 struct bpf_insn *insns = prog->insns;
1959 int insn_idx, map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001960
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001961 insn_idx = prog->reloc_desc[i].insn_idx;
1962 map_idx = prog->reloc_desc[i].map_idx;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001963
Daniel Borkmannd8599002019-04-09 23:20:13 +02001964 if (insn_idx + 1 >= (int)prog->insns_cnt) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001965 pr_warning("relocation out of range: '%s'\n",
1966 prog->section_name);
1967 return -LIBBPF_ERRNO__RELOC;
1968 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02001969
1970 if (!relo_data) {
1971 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1972 } else {
1973 insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE;
1974 insns[insn_idx + 1].imm = insns[insn_idx].imm;
1975 }
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001976 insns[insn_idx].imm = obj->maps[map_idx].fd;
Joe Stringerf8c7a4d2019-04-09 23:20:12 +02001977 } else if (prog->reloc_desc[i].type == RELO_CALL) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08001978 err = bpf_program__reloc_text(prog, obj,
1979 &prog->reloc_desc[i]);
1980 if (err)
1981 return err;
Wang Nan8a47a6c2015-07-01 02:14:05 +00001982 }
Wang Nan8a47a6c2015-07-01 02:14:05 +00001983 }
1984
1985 zfree(&prog->reloc_desc);
1986 prog->nr_reloc = 0;
1987 return 0;
1988}
1989
1990
1991static int
1992bpf_object__relocate(struct bpf_object *obj)
1993{
1994 struct bpf_program *prog;
1995 size_t i;
1996 int err;
1997
1998 for (i = 0; i < obj->nr_programs; i++) {
1999 prog = &obj->programs[i];
2000
Wang Nan9d759a92015-11-27 08:47:35 +00002001 err = bpf_program__relocate(prog, obj);
Wang Nan8a47a6c2015-07-01 02:14:05 +00002002 if (err) {
2003 pr_warning("failed to relocate '%s'\n",
2004 prog->section_name);
2005 return err;
2006 }
2007 }
2008 return 0;
2009}
2010
Wang Nan34090912015-07-01 02:14:02 +00002011static int bpf_object__collect_reloc(struct bpf_object *obj)
2012{
2013 int i, err;
2014
2015 if (!obj_elf_valid(obj)) {
2016 pr_warning("Internal error: elf object is closed\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00002017 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00002018 }
2019
2020 for (i = 0; i < obj->efile.nr_reloc; i++) {
2021 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
2022 Elf_Data *data = obj->efile.reloc[i].data;
2023 int idx = shdr->sh_info;
2024 struct bpf_program *prog;
Wang Nan34090912015-07-01 02:14:02 +00002025
2026 if (shdr->sh_type != SHT_REL) {
2027 pr_warning("internal error at %d\n", __LINE__);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002028 return -LIBBPF_ERRNO__INTERNAL;
Wang Nan34090912015-07-01 02:14:02 +00002029 }
2030
2031 prog = bpf_object__find_prog_by_idx(obj, idx);
2032 if (!prog) {
Jesper Dangaard Brouer077c0662018-02-08 12:48:17 +01002033 pr_warning("relocation failed: no section(%d)\n", idx);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002034 return -LIBBPF_ERRNO__RELOC;
Wang Nan34090912015-07-01 02:14:02 +00002035 }
2036
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002037 err = bpf_program__collect_reloc(prog,
Wang Nan34090912015-07-01 02:14:02 +00002038 shdr, data,
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002039 obj);
Wang Nan34090912015-07-01 02:14:02 +00002040 if (err)
Wang Nan6371ca3b2015-11-06 13:49:37 +00002041 return err;
Wang Nan34090912015-07-01 02:14:02 +00002042 }
2043 return 0;
2044}
2045
Wang Nan55cffde2015-07-01 02:14:07 +00002046static int
Yonghong Song2993e052018-11-19 15:29:16 -08002047load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002048 char *license, __u32 kern_version, int *pfd)
Wang Nan55cffde2015-07-01 02:14:07 +00002049{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002050 struct bpf_load_program_attr load_attr;
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002051 char *cp, errmsg[STRERR_BUFSIZE];
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002052 int log_buf_size = BPF_LOG_BUF_SIZE;
Wang Nan55cffde2015-07-01 02:14:07 +00002053 char *log_buf;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002054 int ret;
Wang Nan55cffde2015-07-01 02:14:07 +00002055
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002056 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
Yonghong Song2993e052018-11-19 15:29:16 -08002057 load_attr.prog_type = prog->type;
2058 load_attr.expected_attach_type = prog->expected_attach_type;
Stanislav Fomichev5b32a232018-11-20 17:11:21 -08002059 if (prog->caps->name)
2060 load_attr.name = prog->name;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002061 load_attr.insns = insns;
2062 load_attr.insns_cnt = insns_cnt;
2063 load_attr.license = license;
2064 load_attr.kern_version = kern_version;
Yonghong Song2993e052018-11-19 15:29:16 -08002065 load_attr.prog_ifindex = prog->prog_ifindex;
Yonghong Song462c1242018-11-21 11:22:42 -08002066 load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0;
Yonghong Song2993e052018-11-19 15:29:16 -08002067 load_attr.func_info = prog->func_info;
2068 load_attr.func_info_rec_size = prog->func_info_rec_size;
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002069 load_attr.func_info_cnt = prog->func_info_cnt;
Martin KaFai Lau3d650142018-12-07 16:42:31 -08002070 load_attr.line_info = prog->line_info;
2071 load_attr.line_info_rec_size = prog->line_info_rec_size;
2072 load_attr.line_info_cnt = prog->line_info_cnt;
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002073 load_attr.log_level = prog->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01002074 load_attr.prog_flags = prog->prog_flags;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002075 if (!load_attr.insns || !load_attr.insns_cnt)
Wang Nan55cffde2015-07-01 02:14:07 +00002076 return -EINVAL;
2077
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002078retry_load:
2079 log_buf = malloc(log_buf_size);
Wang Nan55cffde2015-07-01 02:14:07 +00002080 if (!log_buf)
2081 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
2082
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002083 ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
Wang Nan55cffde2015-07-01 02:14:07 +00002084
2085 if (ret >= 0) {
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002086 if (load_attr.log_level)
2087 pr_debug("verifier log:\n%s", log_buf);
Wang Nan55cffde2015-07-01 02:14:07 +00002088 *pfd = ret;
2089 ret = 0;
2090 goto out;
2091 }
2092
Alexei Starovoitovda11b412019-04-01 21:27:47 -07002093 if (errno == ENOSPC) {
2094 log_buf_size <<= 1;
2095 free(log_buf);
2096 goto retry_load;
2097 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00002098 ret = -LIBBPF_ERRNO__LOAD;
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002099 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002100 pr_warning("load bpf program failed: %s\n", cp);
Wang Nan55cffde2015-07-01 02:14:07 +00002101
Wang Nan6371ca3b2015-11-06 13:49:37 +00002102 if (log_buf && log_buf[0] != '\0') {
2103 ret = -LIBBPF_ERRNO__VERIFY;
Wang Nan55cffde2015-07-01 02:14:07 +00002104 pr_warning("-- BEGIN DUMP LOG ---\n");
2105 pr_warning("\n%s\n", log_buf);
2106 pr_warning("-- END LOG --\n");
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002107 } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
2108 pr_warning("Program too large (%zu insns), at most %d insns\n",
2109 load_attr.insns_cnt, BPF_MAXINSNS);
Wang Nan705fa212016-07-13 10:44:02 +00002110 ret = -LIBBPF_ERRNO__PROG2BIG;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002111 } else {
Wang Nan705fa212016-07-13 10:44:02 +00002112 /* Wrong program type? */
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002113 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
Wang Nan705fa212016-07-13 10:44:02 +00002114 int fd;
2115
Andrey Ignatovd7be1432018-03-30 15:08:01 -07002116 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
2117 load_attr.expected_attach_type = 0;
2118 fd = bpf_load_program_xattr(&load_attr, NULL, 0);
Wang Nan705fa212016-07-13 10:44:02 +00002119 if (fd >= 0) {
2120 close(fd);
2121 ret = -LIBBPF_ERRNO__PROGTYPE;
2122 goto out;
2123 }
Wang Nan6371ca3b2015-11-06 13:49:37 +00002124 }
Wang Nan705fa212016-07-13 10:44:02 +00002125
2126 if (log_buf)
2127 ret = -LIBBPF_ERRNO__KVER;
Wang Nan55cffde2015-07-01 02:14:07 +00002128 }
2129
2130out:
2131 free(log_buf);
2132 return ret;
2133}
2134
Joe Stringer29cd77f2018-10-02 13:35:39 -07002135int
Wang Nan55cffde2015-07-01 02:14:07 +00002136bpf_program__load(struct bpf_program *prog,
Andrey Ignatove5b08632018-10-03 15:26:43 -07002137 char *license, __u32 kern_version)
Wang Nan55cffde2015-07-01 02:14:07 +00002138{
Wang Nanb5805632015-11-16 12:10:09 +00002139 int err = 0, fd, i;
Wang Nan55cffde2015-07-01 02:14:07 +00002140
Wang Nanb5805632015-11-16 12:10:09 +00002141 if (prog->instances.nr < 0 || !prog->instances.fds) {
2142 if (prog->preprocessor) {
2143 pr_warning("Internal error: can't load program '%s'\n",
2144 prog->section_name);
2145 return -LIBBPF_ERRNO__INTERNAL;
2146 }
Wang Nan55cffde2015-07-01 02:14:07 +00002147
Wang Nanb5805632015-11-16 12:10:09 +00002148 prog->instances.fds = malloc(sizeof(int));
2149 if (!prog->instances.fds) {
2150 pr_warning("Not enough memory for BPF fds\n");
2151 return -ENOMEM;
2152 }
2153 prog->instances.nr = 1;
2154 prog->instances.fds[0] = -1;
2155 }
2156
2157 if (!prog->preprocessor) {
2158 if (prog->instances.nr != 1) {
2159 pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
2160 prog->section_name, prog->instances.nr);
2161 }
Yonghong Song2993e052018-11-19 15:29:16 -08002162 err = load_program(prog, prog->insns, prog->insns_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002163 license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00002164 if (!err)
2165 prog->instances.fds[0] = fd;
2166 goto out;
2167 }
2168
2169 for (i = 0; i < prog->instances.nr; i++) {
2170 struct bpf_prog_prep_result result;
2171 bpf_program_prep_t preprocessor = prog->preprocessor;
2172
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -08002173 memset(&result, 0, sizeof(result));
Wang Nanb5805632015-11-16 12:10:09 +00002174 err = preprocessor(prog, i, prog->insns,
2175 prog->insns_cnt, &result);
2176 if (err) {
2177 pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
2178 i, prog->section_name);
2179 goto out;
2180 }
2181
2182 if (!result.new_insn_ptr || !result.new_insn_cnt) {
2183 pr_debug("Skip loading the %dth instance of program '%s'\n",
2184 i, prog->section_name);
2185 prog->instances.fds[i] = -1;
2186 if (result.pfd)
2187 *result.pfd = -1;
2188 continue;
2189 }
2190
Yonghong Song2993e052018-11-19 15:29:16 -08002191 err = load_program(prog, result.new_insn_ptr,
Wang Nanb5805632015-11-16 12:10:09 +00002192 result.new_insn_cnt,
Martin KaFai Lauf0187f02018-12-07 16:42:29 -08002193 license, kern_version, &fd);
Wang Nanb5805632015-11-16 12:10:09 +00002194
2195 if (err) {
2196 pr_warning("Loading the %dth instance of program '%s' failed\n",
2197 i, prog->section_name);
2198 goto out;
2199 }
2200
2201 if (result.pfd)
2202 *result.pfd = fd;
2203 prog->instances.fds[i] = fd;
2204 }
2205out:
Wang Nan55cffde2015-07-01 02:14:07 +00002206 if (err)
2207 pr_warning("failed to load program '%s'\n",
2208 prog->section_name);
2209 zfree(&prog->insns);
2210 prog->insns_cnt = 0;
2211 return err;
2212}
2213
Jakub Kicinski9a94f272018-06-28 14:41:38 -07002214static bool bpf_program__is_function_storage(struct bpf_program *prog,
2215 struct bpf_object *obj)
2216{
2217 return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
2218}
2219
Wang Nan55cffde2015-07-01 02:14:07 +00002220static int
Quentin Monnet60276f92019-05-24 11:36:47 +01002221bpf_object__load_progs(struct bpf_object *obj, int log_level)
Wang Nan55cffde2015-07-01 02:14:07 +00002222{
2223 size_t i;
2224 int err;
2225
2226 for (i = 0; i < obj->nr_programs; i++) {
Jakub Kicinski9a94f272018-06-28 14:41:38 -07002227 if (bpf_program__is_function_storage(&obj->programs[i], obj))
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08002228 continue;
Quentin Monnet501b1252019-05-29 15:26:41 +01002229 obj->programs[i].log_level |= log_level;
Wang Nan55cffde2015-07-01 02:14:07 +00002230 err = bpf_program__load(&obj->programs[i],
2231 obj->license,
2232 obj->kern_version);
2233 if (err)
2234 return err;
2235 }
2236 return 0;
2237}
2238
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002239static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
Wang Nancb1e5e92015-07-01 02:13:57 +00002240{
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002241 switch (type) {
2242 case BPF_PROG_TYPE_SOCKET_FILTER:
2243 case BPF_PROG_TYPE_SCHED_CLS:
2244 case BPF_PROG_TYPE_SCHED_ACT:
2245 case BPF_PROG_TYPE_XDP:
2246 case BPF_PROG_TYPE_CGROUP_SKB:
2247 case BPF_PROG_TYPE_CGROUP_SOCK:
2248 case BPF_PROG_TYPE_LWT_IN:
2249 case BPF_PROG_TYPE_LWT_OUT:
2250 case BPF_PROG_TYPE_LWT_XMIT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01002251 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002252 case BPF_PROG_TYPE_SOCK_OPS:
2253 case BPF_PROG_TYPE_SK_SKB:
2254 case BPF_PROG_TYPE_CGROUP_DEVICE:
2255 case BPF_PROG_TYPE_SK_MSG:
2256 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Sean Young6bdd5332018-05-27 12:24:10 +01002257 case BPF_PROG_TYPE_LIRC_MODE2:
Martin KaFai Lau6bc8529c2018-08-08 01:01:30 -07002258 case BPF_PROG_TYPE_SK_REUSEPORT:
Petar Penkovc22fbae2018-09-14 07:46:20 -07002259 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002260 case BPF_PROG_TYPE_UNSPEC:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002261 case BPF_PROG_TYPE_TRACEPOINT:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002262 case BPF_PROG_TYPE_RAW_TRACEPOINT:
Matt Mullins4635b0a2019-04-26 11:49:50 -07002263 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -08002264 case BPF_PROG_TYPE_PERF_EVENT:
Andrey Ignatov063cc9f2019-03-08 09:15:26 -08002265 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Nikita V. Shirokov47ae7e32018-11-23 12:58:12 -08002266 return false;
2267 case BPF_PROG_TYPE_KPROBE:
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002268 default:
2269 return true;
2270 }
2271}
2272
2273static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
2274{
2275 if (needs_kver && obj->kern_version == 0) {
Wang Nancb1e5e92015-07-01 02:13:57 +00002276 pr_warning("%s doesn't provide kernel version\n",
2277 obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002278 return -LIBBPF_ERRNO__KVERSION;
Wang Nancb1e5e92015-07-01 02:13:57 +00002279 }
2280 return 0;
2281}
2282
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002283static struct bpf_object *
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002284__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
John Fastabendc034a172018-10-15 11:19:55 -07002285 bool needs_kver, int flags)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002286{
2287 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002288 int err;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002289
2290 if (elf_version(EV_CURRENT) == EV_NONE) {
2291 pr_warning("failed to init libelf for %s\n", path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002292 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002293 }
2294
Wang Nan6c956392015-07-01 02:13:54 +00002295 obj = bpf_object__new(path, obj_buf, obj_buf_sz);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002296 if (IS_ERR(obj))
2297 return obj;
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002298
Wang Nan6371ca3b2015-11-06 13:49:37 +00002299 CHECK_ERR(bpf_object__elf_init(obj), err, out);
2300 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
Daniel Borkmann8837fe52019-04-24 00:45:56 +02002301 CHECK_ERR(bpf_object__probe_caps(obj), err, out);
John Fastabendc034a172018-10-15 11:19:55 -07002302 CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002303 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
Jakub Kicinski17387dd2018-05-10 10:24:42 -07002304 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002305
2306 bpf_object__elf_finish(obj);
2307 return obj;
2308out:
2309 bpf_object__close(obj);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002310 return ERR_PTR(err);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002311}
2312
John Fastabendc034a172018-10-15 11:19:55 -07002313struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
2314 int flags)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002315{
2316 /* param validation */
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002317 if (!attr->file)
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002318 return NULL;
2319
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002320 pr_debug("loading %s\n", attr->file);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002321
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002322 return __bpf_object__open(attr->file, NULL, 0,
John Fastabendc034a172018-10-15 11:19:55 -07002323 bpf_prog_type__needs_kver(attr->prog_type),
2324 flags);
2325}
2326
2327struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
2328{
2329 return __bpf_object__open_xattr(attr, 0);
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07002330}
2331
2332struct bpf_object *bpf_object__open(const char *path)
2333{
2334 struct bpf_object_open_attr attr = {
2335 .file = path,
2336 .prog_type = BPF_PROG_TYPE_UNSPEC,
2337 };
2338
2339 return bpf_object__open_xattr(&attr);
Wang Nan6c956392015-07-01 02:13:54 +00002340}
2341
2342struct bpf_object *bpf_object__open_buffer(void *obj_buf,
Wang Nanacf860a2015-08-27 02:30:55 +00002343 size_t obj_buf_sz,
2344 const char *name)
Wang Nan6c956392015-07-01 02:13:54 +00002345{
Wang Nanacf860a2015-08-27 02:30:55 +00002346 char tmp_name[64];
2347
Wang Nan6c956392015-07-01 02:13:54 +00002348 /* param validation */
2349 if (!obj_buf || obj_buf_sz <= 0)
2350 return NULL;
2351
Wang Nanacf860a2015-08-27 02:30:55 +00002352 if (!name) {
2353 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
2354 (unsigned long)obj_buf,
2355 (unsigned long)obj_buf_sz);
2356 tmp_name[sizeof(tmp_name) - 1] = '\0';
2357 name = tmp_name;
2358 }
2359 pr_debug("loading object '%s' from buffer\n",
2360 name);
Wang Nan6c956392015-07-01 02:13:54 +00002361
John Fastabendc034a172018-10-15 11:19:55 -07002362 return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002363}
2364
Wang Nan52d33522015-07-01 02:14:04 +00002365int bpf_object__unload(struct bpf_object *obj)
2366{
2367 size_t i;
2368
2369 if (!obj)
2370 return -EINVAL;
2371
Wang Nan9d759a92015-11-27 08:47:35 +00002372 for (i = 0; i < obj->nr_maps; i++)
2373 zclose(obj->maps[i].fd);
Wang Nan52d33522015-07-01 02:14:04 +00002374
Wang Nan55cffde2015-07-01 02:14:07 +00002375 for (i = 0; i < obj->nr_programs; i++)
2376 bpf_program__unload(&obj->programs[i]);
2377
Wang Nan52d33522015-07-01 02:14:04 +00002378 return 0;
2379}
2380
Quentin Monnet60276f92019-05-24 11:36:47 +01002381int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
Wang Nan52d33522015-07-01 02:14:04 +00002382{
Quentin Monnet60276f92019-05-24 11:36:47 +01002383 struct bpf_object *obj;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002384 int err;
2385
Quentin Monnet60276f92019-05-24 11:36:47 +01002386 if (!attr)
2387 return -EINVAL;
2388 obj = attr->obj;
Wang Nan52d33522015-07-01 02:14:04 +00002389 if (!obj)
2390 return -EINVAL;
2391
2392 if (obj->loaded) {
2393 pr_warning("object should not be loaded twice\n");
2394 return -EINVAL;
2395 }
2396
2397 obj->loaded = true;
Wang Nan6371ca3b2015-11-06 13:49:37 +00002398
2399 CHECK_ERR(bpf_object__create_maps(obj), err, out);
2400 CHECK_ERR(bpf_object__relocate(obj), err, out);
Quentin Monnet60276f92019-05-24 11:36:47 +01002401 CHECK_ERR(bpf_object__load_progs(obj, attr->log_level), err, out);
Wang Nan52d33522015-07-01 02:14:04 +00002402
2403 return 0;
2404out:
2405 bpf_object__unload(obj);
2406 pr_warning("failed to load object '%s'\n", obj->path);
Wang Nan6371ca3b2015-11-06 13:49:37 +00002407 return err;
Wang Nan52d33522015-07-01 02:14:04 +00002408}
2409
Quentin Monnet60276f92019-05-24 11:36:47 +01002410int bpf_object__load(struct bpf_object *obj)
2411{
2412 struct bpf_object_load_attr attr = {
2413 .obj = obj,
2414 };
2415
2416 return bpf_object__load_xattr(&attr);
2417}
2418
Joe Stringerf3675402017-01-26 13:19:56 -08002419static int check_path(const char *path)
2420{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002421 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002422 struct statfs st_fs;
2423 char *dname, *dir;
2424 int err = 0;
2425
2426 if (path == NULL)
2427 return -EINVAL;
2428
2429 dname = strdup(path);
2430 if (dname == NULL)
2431 return -ENOMEM;
2432
2433 dir = dirname(dname);
2434 if (statfs(dir, &st_fs)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002435 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002436 pr_warning("failed to statfs %s: %s\n", dir, cp);
Joe Stringerf3675402017-01-26 13:19:56 -08002437 err = -errno;
2438 }
2439 free(dname);
2440
2441 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
2442 pr_warning("specified path %s is not on BPF FS\n", path);
2443 err = -EINVAL;
2444 }
2445
2446 return err;
2447}
2448
2449int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
2450 int instance)
2451{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002452 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002453 int err;
2454
2455 err = check_path(path);
2456 if (err)
2457 return err;
2458
2459 if (prog == NULL) {
2460 pr_warning("invalid program pointer\n");
2461 return -EINVAL;
2462 }
2463
2464 if (instance < 0 || instance >= prog->instances.nr) {
2465 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2466 instance, prog->section_name, prog->instances.nr);
2467 return -EINVAL;
2468 }
2469
2470 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002471 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002472 pr_warning("failed to pin program: %s\n", cp);
Joe Stringerf3675402017-01-26 13:19:56 -08002473 return -errno;
2474 }
2475 pr_debug("pinned program '%s'\n", path);
2476
2477 return 0;
2478}
2479
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002480int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
2481 int instance)
2482{
2483 int err;
2484
2485 err = check_path(path);
2486 if (err)
2487 return err;
2488
2489 if (prog == NULL) {
2490 pr_warning("invalid program pointer\n");
2491 return -EINVAL;
2492 }
2493
2494 if (instance < 0 || instance >= prog->instances.nr) {
2495 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
2496 instance, prog->section_name, prog->instances.nr);
2497 return -EINVAL;
2498 }
2499
2500 err = unlink(path);
2501 if (err != 0)
2502 return -errno;
2503 pr_debug("unpinned program '%s'\n", path);
2504
2505 return 0;
2506}
2507
Joe Stringerf3675402017-01-26 13:19:56 -08002508static int make_dir(const char *path)
2509{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002510 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerf3675402017-01-26 13:19:56 -08002511 int err = 0;
2512
2513 if (mkdir(path, 0700) && errno != EEXIST)
2514 err = -errno;
2515
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002516 if (err) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002517 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002518 pr_warning("failed to mkdir %s: %s\n", path, cp);
2519 }
Joe Stringerf3675402017-01-26 13:19:56 -08002520 return err;
2521}
2522
2523int bpf_program__pin(struct bpf_program *prog, const char *path)
2524{
2525 int i, err;
2526
2527 err = check_path(path);
2528 if (err)
2529 return err;
2530
2531 if (prog == NULL) {
2532 pr_warning("invalid program pointer\n");
2533 return -EINVAL;
2534 }
2535
2536 if (prog->instances.nr <= 0) {
2537 pr_warning("no instances of prog %s to pin\n",
2538 prog->section_name);
2539 return -EINVAL;
2540 }
2541
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08002542 if (prog->instances.nr == 1) {
2543 /* don't create subdirs when pinning single instance */
2544 return bpf_program__pin_instance(prog, path, 0);
2545 }
2546
Joe Stringerf3675402017-01-26 13:19:56 -08002547 err = make_dir(path);
2548 if (err)
2549 return err;
2550
2551 for (i = 0; i < prog->instances.nr; i++) {
2552 char buf[PATH_MAX];
2553 int len;
2554
2555 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002556 if (len < 0) {
2557 err = -EINVAL;
2558 goto err_unpin;
2559 } else if (len >= PATH_MAX) {
2560 err = -ENAMETOOLONG;
2561 goto err_unpin;
2562 }
2563
2564 err = bpf_program__pin_instance(prog, buf, i);
2565 if (err)
2566 goto err_unpin;
2567 }
2568
2569 return 0;
2570
2571err_unpin:
2572 for (i = i - 1; i >= 0; i--) {
2573 char buf[PATH_MAX];
2574 int len;
2575
2576 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
2577 if (len < 0)
2578 continue;
2579 else if (len >= PATH_MAX)
2580 continue;
2581
2582 bpf_program__unpin_instance(prog, buf, i);
2583 }
2584
2585 rmdir(path);
2586
2587 return err;
2588}
2589
2590int bpf_program__unpin(struct bpf_program *prog, const char *path)
2591{
2592 int i, err;
2593
2594 err = check_path(path);
2595 if (err)
2596 return err;
2597
2598 if (prog == NULL) {
2599 pr_warning("invalid program pointer\n");
2600 return -EINVAL;
2601 }
2602
2603 if (prog->instances.nr <= 0) {
2604 pr_warning("no instances of prog %s to pin\n",
2605 prog->section_name);
2606 return -EINVAL;
2607 }
2608
Stanislav Fomichevfd734c52018-11-09 08:21:42 -08002609 if (prog->instances.nr == 1) {
2610 /* don't create subdirs when pinning single instance */
2611 return bpf_program__unpin_instance(prog, path, 0);
2612 }
2613
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002614 for (i = 0; i < prog->instances.nr; i++) {
2615 char buf[PATH_MAX];
2616 int len;
2617
2618 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
Joe Stringerf3675402017-01-26 13:19:56 -08002619 if (len < 0)
2620 return -EINVAL;
2621 else if (len >= PATH_MAX)
2622 return -ENAMETOOLONG;
2623
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002624 err = bpf_program__unpin_instance(prog, buf, i);
Joe Stringerf3675402017-01-26 13:19:56 -08002625 if (err)
2626 return err;
2627 }
2628
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002629 err = rmdir(path);
2630 if (err)
2631 return -errno;
2632
Joe Stringerf3675402017-01-26 13:19:56 -08002633 return 0;
2634}
2635
Joe Stringerb6989f32017-01-26 13:19:57 -08002636int bpf_map__pin(struct bpf_map *map, const char *path)
2637{
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002638 char *cp, errmsg[STRERR_BUFSIZE];
Joe Stringerb6989f32017-01-26 13:19:57 -08002639 int err;
2640
2641 err = check_path(path);
2642 if (err)
2643 return err;
2644
2645 if (map == NULL) {
2646 pr_warning("invalid map pointer\n");
2647 return -EINVAL;
2648 }
2649
2650 if (bpf_obj_pin(map->fd, path)) {
Andrey Ignatov24d6a802018-10-03 15:26:41 -07002651 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
Thomas Richter1ce6a9f2018-07-30 10:53:23 +02002652 pr_warning("failed to pin map: %s\n", cp);
Joe Stringerb6989f32017-01-26 13:19:57 -08002653 return -errno;
2654 }
2655
2656 pr_debug("pinned map '%s'\n", path);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002657
Joe Stringerb6989f32017-01-26 13:19:57 -08002658 return 0;
2659}
2660
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002661int bpf_map__unpin(struct bpf_map *map, const char *path)
Joe Stringerd5148d82017-01-26 13:19:58 -08002662{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002663 int err;
2664
2665 err = check_path(path);
2666 if (err)
2667 return err;
2668
2669 if (map == NULL) {
2670 pr_warning("invalid map pointer\n");
2671 return -EINVAL;
2672 }
2673
2674 err = unlink(path);
2675 if (err != 0)
2676 return -errno;
2677 pr_debug("unpinned map '%s'\n", path);
2678
2679 return 0;
2680}
2681
2682int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
2683{
Joe Stringerd5148d82017-01-26 13:19:58 -08002684 struct bpf_map *map;
2685 int err;
2686
2687 if (!obj)
2688 return -ENOENT;
2689
2690 if (!obj->loaded) {
2691 pr_warning("object not yet loaded; load it first\n");
2692 return -ENOENT;
2693 }
2694
2695 err = make_dir(path);
2696 if (err)
2697 return err;
2698
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08002699 bpf_object__for_each_map(map, obj) {
Joe Stringerd5148d82017-01-26 13:19:58 -08002700 char buf[PATH_MAX];
2701 int len;
2702
2703 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2704 bpf_map__name(map));
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002705 if (len < 0) {
2706 err = -EINVAL;
2707 goto err_unpin_maps;
2708 } else if (len >= PATH_MAX) {
2709 err = -ENAMETOOLONG;
2710 goto err_unpin_maps;
2711 }
2712
2713 err = bpf_map__pin(map, buf);
2714 if (err)
2715 goto err_unpin_maps;
2716 }
2717
2718 return 0;
2719
2720err_unpin_maps:
2721 while ((map = bpf_map__prev(map, obj))) {
2722 char buf[PATH_MAX];
2723 int len;
2724
2725 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2726 bpf_map__name(map));
2727 if (len < 0)
2728 continue;
2729 else if (len >= PATH_MAX)
2730 continue;
2731
2732 bpf_map__unpin(map, buf);
2733 }
2734
2735 return err;
2736}
2737
2738int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
2739{
2740 struct bpf_map *map;
2741 int err;
2742
2743 if (!obj)
2744 return -ENOENT;
2745
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08002746 bpf_object__for_each_map(map, obj) {
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002747 char buf[PATH_MAX];
2748 int len;
2749
2750 len = snprintf(buf, PATH_MAX, "%s/%s", path,
2751 bpf_map__name(map));
Joe Stringerd5148d82017-01-26 13:19:58 -08002752 if (len < 0)
2753 return -EINVAL;
2754 else if (len >= PATH_MAX)
2755 return -ENAMETOOLONG;
2756
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002757 err = bpf_map__unpin(map, buf);
Joe Stringerd5148d82017-01-26 13:19:58 -08002758 if (err)
2759 return err;
2760 }
2761
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002762 return 0;
2763}
2764
2765int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
2766{
2767 struct bpf_program *prog;
2768 int err;
2769
2770 if (!obj)
2771 return -ENOENT;
2772
2773 if (!obj->loaded) {
2774 pr_warning("object not yet loaded; load it first\n");
2775 return -ENOENT;
2776 }
2777
2778 err = make_dir(path);
2779 if (err)
2780 return err;
2781
2782 bpf_object__for_each_program(prog, obj) {
2783 char buf[PATH_MAX];
2784 int len;
2785
2786 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08002787 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002788 if (len < 0) {
2789 err = -EINVAL;
2790 goto err_unpin_programs;
2791 } else if (len >= PATH_MAX) {
2792 err = -ENAMETOOLONG;
2793 goto err_unpin_programs;
2794 }
2795
2796 err = bpf_program__pin(prog, buf);
2797 if (err)
2798 goto err_unpin_programs;
2799 }
2800
2801 return 0;
2802
2803err_unpin_programs:
2804 while ((prog = bpf_program__prev(prog, obj))) {
2805 char buf[PATH_MAX];
2806 int len;
2807
2808 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08002809 prog->pin_name);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002810 if (len < 0)
2811 continue;
2812 else if (len >= PATH_MAX)
2813 continue;
2814
2815 bpf_program__unpin(prog, buf);
2816 }
2817
2818 return err;
2819}
2820
2821int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
2822{
2823 struct bpf_program *prog;
2824 int err;
2825
2826 if (!obj)
2827 return -ENOENT;
2828
Joe Stringerd5148d82017-01-26 13:19:58 -08002829 bpf_object__for_each_program(prog, obj) {
2830 char buf[PATH_MAX];
2831 int len;
2832
2833 len = snprintf(buf, PATH_MAX, "%s/%s", path,
Stanislav Fomichev33a2c752018-11-09 08:21:43 -08002834 prog->pin_name);
Joe Stringerd5148d82017-01-26 13:19:58 -08002835 if (len < 0)
2836 return -EINVAL;
2837 else if (len >= PATH_MAX)
2838 return -ENAMETOOLONG;
2839
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002840 err = bpf_program__unpin(prog, buf);
Joe Stringerd5148d82017-01-26 13:19:58 -08002841 if (err)
2842 return err;
2843 }
2844
2845 return 0;
2846}
2847
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002848int bpf_object__pin(struct bpf_object *obj, const char *path)
2849{
2850 int err;
2851
2852 err = bpf_object__pin_maps(obj, path);
2853 if (err)
2854 return err;
2855
2856 err = bpf_object__pin_programs(obj, path);
2857 if (err) {
2858 bpf_object__unpin_maps(obj, path);
2859 return err;
2860 }
2861
2862 return 0;
2863}
2864
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002865void bpf_object__close(struct bpf_object *obj)
2866{
Wang Nana5b8bd42015-07-01 02:14:00 +00002867 size_t i;
2868
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002869 if (!obj)
2870 return;
2871
Wang Nan10931d22016-11-26 07:03:26 +00002872 if (obj->clear_priv)
2873 obj->clear_priv(obj, obj->priv);
2874
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002875 bpf_object__elf_finish(obj);
Wang Nan52d33522015-07-01 02:14:04 +00002876 bpf_object__unload(obj);
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002877 btf__free(obj->btf);
Yonghong Song2993e052018-11-19 15:29:16 -08002878 btf_ext__free(obj->btf_ext);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002879
Wang Nan9d759a92015-11-27 08:47:35 +00002880 for (i = 0; i < obj->nr_maps; i++) {
Wang Nan561bbcc2015-11-27 08:47:36 +00002881 zfree(&obj->maps[i].name);
Wang Nan9d759a92015-11-27 08:47:35 +00002882 if (obj->maps[i].clear_priv)
2883 obj->maps[i].clear_priv(&obj->maps[i],
2884 obj->maps[i].priv);
2885 obj->maps[i].priv = NULL;
2886 obj->maps[i].clear_priv = NULL;
2887 }
Daniel Borkmannd8599002019-04-09 23:20:13 +02002888
2889 zfree(&obj->sections.rodata);
2890 zfree(&obj->sections.data);
Wang Nan9d759a92015-11-27 08:47:35 +00002891 zfree(&obj->maps);
2892 obj->nr_maps = 0;
Wang Nana5b8bd42015-07-01 02:14:00 +00002893
2894 if (obj->programs && obj->nr_programs) {
2895 for (i = 0; i < obj->nr_programs; i++)
2896 bpf_program__exit(&obj->programs[i]);
2897 }
2898 zfree(&obj->programs);
2899
Wang Nan9a208ef2015-07-01 02:14:10 +00002900 list_del(&obj->list);
Wang Nan1a5e3fb2015-07-01 02:13:53 +00002901 free(obj);
2902}
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002903
Wang Nan9a208ef2015-07-01 02:14:10 +00002904struct bpf_object *
2905bpf_object__next(struct bpf_object *prev)
2906{
2907 struct bpf_object *next;
2908
2909 if (!prev)
2910 next = list_first_entry(&bpf_objects_list,
2911 struct bpf_object,
2912 list);
2913 else
2914 next = list_next_entry(prev, list);
2915
2916 /* Empty list is noticed here so don't need checking on entry. */
2917 if (&next->list == &bpf_objects_list)
2918 return NULL;
2919
2920 return next;
2921}
2922
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03002923const char *bpf_object__name(struct bpf_object *obj)
Wang Nanacf860a2015-08-27 02:30:55 +00002924{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03002925 return obj ? obj->path : ERR_PTR(-EINVAL);
Wang Nanacf860a2015-08-27 02:30:55 +00002926}
2927
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03002928unsigned int bpf_object__kversion(struct bpf_object *obj)
Wang Nan45825d82015-11-06 13:49:38 +00002929{
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03002930 return obj ? obj->kern_version : 0;
Wang Nan45825d82015-11-06 13:49:38 +00002931}
2932
Andrey Ignatov789f6ba2019-02-14 15:01:43 -08002933struct btf *bpf_object__btf(struct bpf_object *obj)
2934{
2935 return obj ? obj->btf : NULL;
2936}
2937
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07002938int bpf_object__btf_fd(const struct bpf_object *obj)
2939{
2940 return obj->btf ? btf__fd(obj->btf) : -1;
2941}
2942
Wang Nan10931d22016-11-26 07:03:26 +00002943int bpf_object__set_priv(struct bpf_object *obj, void *priv,
2944 bpf_object_clear_priv_t clear_priv)
2945{
2946 if (obj->priv && obj->clear_priv)
2947 obj->clear_priv(obj, obj->priv);
2948
2949 obj->priv = priv;
2950 obj->clear_priv = clear_priv;
2951 return 0;
2952}
2953
2954void *bpf_object__priv(struct bpf_object *obj)
2955{
2956 return obj ? obj->priv : ERR_PTR(-EINVAL);
2957}
2958
Jakub Kicinskieac7d842018-06-28 14:41:39 -07002959static struct bpf_program *
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002960__bpf_program__iter(struct bpf_program *p, struct bpf_object *obj, bool forward)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002961{
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002962 size_t nr_programs = obj->nr_programs;
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002963 ssize_t idx;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002964
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002965 if (!nr_programs)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002966 return NULL;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002967
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002968 if (!p)
2969 /* Iter from the beginning */
2970 return forward ? &obj->programs[0] :
2971 &obj->programs[nr_programs - 1];
2972
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002973 if (p->obj != obj) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002974 pr_warning("error: program handler doesn't match object\n");
2975 return NULL;
2976 }
2977
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002978 idx = (p - obj->programs) + (forward ? 1 : -1);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002979 if (idx >= obj->nr_programs || idx < 0)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00002980 return NULL;
2981 return &obj->programs[idx];
2982}
2983
Jakub Kicinskieac7d842018-06-28 14:41:39 -07002984struct bpf_program *
2985bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
2986{
2987 struct bpf_program *prog = prev;
2988
2989 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08002990 prog = __bpf_program__iter(prog, obj, true);
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08002991 } while (prog && bpf_program__is_function_storage(prog, obj));
2992
2993 return prog;
2994}
2995
2996struct bpf_program *
2997bpf_program__prev(struct bpf_program *next, struct bpf_object *obj)
2998{
2999 struct bpf_program *prog = next;
3000
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003001 do {
Martin KaFai Laua83d6e72018-11-12 15:44:53 -08003002 prog = __bpf_program__iter(prog, obj, false);
Jakub Kicinskieac7d842018-06-28 14:41:39 -07003003 } while (prog && bpf_program__is_function_storage(prog, obj));
3004
3005 return prog;
3006}
3007
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03003008int bpf_program__set_priv(struct bpf_program *prog, void *priv,
3009 bpf_program_clear_priv_t clear_priv)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003010{
3011 if (prog->priv && prog->clear_priv)
3012 prog->clear_priv(prog, prog->priv);
3013
3014 prog->priv = priv;
3015 prog->clear_priv = clear_priv;
3016 return 0;
3017}
3018
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03003019void *bpf_program__priv(struct bpf_program *prog)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003020{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -03003021 return prog ? prog->priv : ERR_PTR(-EINVAL);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003022}
3023
Jakub Kicinski9aba3612018-06-28 14:41:37 -07003024void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
3025{
3026 prog->prog_ifindex = ifindex;
3027}
3028
Namhyung Kim715f8db2015-11-03 20:21:05 +09003029const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003030{
3031 const char *title;
3032
3033 title = prog->section_name;
Namhyung Kim715f8db2015-11-03 20:21:05 +09003034 if (needs_copy) {
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003035 title = strdup(title);
3036 if (!title) {
3037 pr_warning("failed to strdup program title\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +00003038 return ERR_PTR(-ENOMEM);
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003039 }
3040 }
3041
3042 return title;
3043}
3044
3045int bpf_program__fd(struct bpf_program *prog)
3046{
Wang Nanb5805632015-11-16 12:10:09 +00003047 return bpf_program__nth_fd(prog, 0);
3048}
3049
3050int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
3051 bpf_program_prep_t prep)
3052{
3053 int *instances_fds;
3054
3055 if (nr_instances <= 0 || !prep)
3056 return -EINVAL;
3057
3058 if (prog->instances.nr > 0 || prog->instances.fds) {
3059 pr_warning("Can't set pre-processor after loading\n");
3060 return -EINVAL;
3061 }
3062
3063 instances_fds = malloc(sizeof(int) * nr_instances);
3064 if (!instances_fds) {
3065 pr_warning("alloc memory failed for fds\n");
3066 return -ENOMEM;
3067 }
3068
3069 /* fill all fd with -1 */
3070 memset(instances_fds, -1, sizeof(int) * nr_instances);
3071
3072 prog->instances.nr = nr_instances;
3073 prog->instances.fds = instances_fds;
3074 prog->preprocessor = prep;
3075 return 0;
3076}
3077
3078int bpf_program__nth_fd(struct bpf_program *prog, int n)
3079{
3080 int fd;
3081
Jakub Kicinski1e960042018-07-26 14:32:18 -07003082 if (!prog)
3083 return -EINVAL;
3084
Wang Nanb5805632015-11-16 12:10:09 +00003085 if (n >= prog->instances.nr || n < 0) {
3086 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
3087 n, prog->section_name, prog->instances.nr);
3088 return -EINVAL;
3089 }
3090
3091 fd = prog->instances.fds[n];
3092 if (fd < 0) {
3093 pr_warning("%dth instance of program '%s' is invalid\n",
3094 n, prog->section_name);
3095 return -ENOENT;
3096 }
3097
3098 return fd;
Wang Nanaa9b1ac2015-07-01 02:14:08 +00003099}
Wang Nan9d759a92015-11-27 08:47:35 +00003100
Alexei Starovoitovdd26b7f2017-03-30 21:45:40 -07003101void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
Wang Nan5f44e4c82016-07-13 10:44:01 +00003102{
3103 prog->type = type;
3104}
3105
Wang Nan5f44e4c82016-07-13 10:44:01 +00003106static bool bpf_program__is_type(struct bpf_program *prog,
3107 enum bpf_prog_type type)
3108{
3109 return prog ? (prog->type == type) : false;
3110}
3111
Joe Stringered794072017-01-22 17:11:23 -08003112#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
3113int bpf_program__set_##NAME(struct bpf_program *prog) \
3114{ \
3115 if (!prog) \
3116 return -EINVAL; \
3117 bpf_program__set_type(prog, TYPE); \
3118 return 0; \
3119} \
3120 \
3121bool bpf_program__is_##NAME(struct bpf_program *prog) \
3122{ \
3123 return bpf_program__is_type(prog, TYPE); \
3124} \
Wang Nan5f44e4c82016-07-13 10:44:01 +00003125
Joe Stringer7803ba72017-01-22 17:11:24 -08003126BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
Joe Stringered794072017-01-22 17:11:23 -08003127BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
Joe Stringer7803ba72017-01-22 17:11:24 -08003128BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
3129BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
Joe Stringered794072017-01-22 17:11:23 -08003130BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
Andrey Ignatove14c93fd2018-04-17 10:28:46 -07003131BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
Joe Stringer7803ba72017-01-22 17:11:24 -08003132BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
3133BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
Wang Nan5f44e4c82016-07-13 10:44:01 +00003134
John Fastabend16962b22018-04-23 14:30:38 -07003135void bpf_program__set_expected_attach_type(struct bpf_program *prog,
3136 enum bpf_attach_type type)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003137{
3138 prog->expected_attach_type = type;
3139}
3140
Andrey Ignatov36153532018-10-31 12:57:18 -07003141#define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, atype) \
3142 { string, sizeof(string) - 1, ptype, eatype, is_attachable, atype }
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003143
Andrey Ignatov956b6202018-09-26 15:24:53 -07003144/* Programs that can NOT be attached. */
Andrey Ignatov36153532018-10-31 12:57:18 -07003145#define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0)
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003146
Andrey Ignatov956b6202018-09-26 15:24:53 -07003147/* Programs that can be attached. */
3148#define BPF_APROG_SEC(string, ptype, atype) \
Andrey Ignatov36153532018-10-31 12:57:18 -07003149 BPF_PROG_SEC_IMPL(string, ptype, 0, 1, atype)
Andrey Ignatov81efee72018-04-17 10:28:45 -07003150
Andrey Ignatov956b6202018-09-26 15:24:53 -07003151/* Programs that must specify expected attach type at load time. */
3152#define BPF_EAPROG_SEC(string, ptype, eatype) \
Andrey Ignatov36153532018-10-31 12:57:18 -07003153 BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, eatype)
Andrey Ignatov956b6202018-09-26 15:24:53 -07003154
3155/* Programs that can be attached but attach type can't be identified by section
3156 * name. Kept for backward compatibility.
3157 */
3158#define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
Andrey Ignatove50b0a62018-03-30 15:08:03 -07003159
Roman Gushchin583c9002017-12-13 15:18:51 +00003160static const struct {
3161 const char *sec;
3162 size_t len;
3163 enum bpf_prog_type prog_type;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003164 enum bpf_attach_type expected_attach_type;
Andrey Ignatov36153532018-10-31 12:57:18 -07003165 int is_attachable;
Andrey Ignatov956b6202018-09-26 15:24:53 -07003166 enum bpf_attach_type attach_type;
Roman Gushchin583c9002017-12-13 15:18:51 +00003167} section_names[] = {
Andrey Ignatov956b6202018-09-26 15:24:53 -07003168 BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
3169 BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
3170 BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
3171 BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
3172 BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
3173 BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
3174 BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
3175 BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
3176 BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
3177 BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
3178 BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
3179 BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
3180 BPF_PROG_SEC("lwt_seg6local", BPF_PROG_TYPE_LWT_SEG6LOCAL),
Andrey Ignatovbafa7af2018-09-26 15:24:54 -07003181 BPF_APROG_SEC("cgroup_skb/ingress", BPF_PROG_TYPE_CGROUP_SKB,
3182 BPF_CGROUP_INET_INGRESS),
3183 BPF_APROG_SEC("cgroup_skb/egress", BPF_PROG_TYPE_CGROUP_SKB,
3184 BPF_CGROUP_INET_EGRESS),
Andrey Ignatov956b6202018-09-26 15:24:53 -07003185 BPF_APROG_COMPAT("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
3186 BPF_APROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK,
3187 BPF_CGROUP_INET_SOCK_CREATE),
3188 BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
3189 BPF_CGROUP_INET4_POST_BIND),
3190 BPF_EAPROG_SEC("cgroup/post_bind6", BPF_PROG_TYPE_CGROUP_SOCK,
3191 BPF_CGROUP_INET6_POST_BIND),
3192 BPF_APROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE,
3193 BPF_CGROUP_DEVICE),
3194 BPF_APROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS,
3195 BPF_CGROUP_SOCK_OPS),
Andrey Ignatovc6f68512018-09-26 15:24:55 -07003196 BPF_APROG_SEC("sk_skb/stream_parser", BPF_PROG_TYPE_SK_SKB,
3197 BPF_SK_SKB_STREAM_PARSER),
3198 BPF_APROG_SEC("sk_skb/stream_verdict", BPF_PROG_TYPE_SK_SKB,
3199 BPF_SK_SKB_STREAM_VERDICT),
Andrey Ignatov956b6202018-09-26 15:24:53 -07003200 BPF_APROG_COMPAT("sk_skb", BPF_PROG_TYPE_SK_SKB),
3201 BPF_APROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG,
3202 BPF_SK_MSG_VERDICT),
3203 BPF_APROG_SEC("lirc_mode2", BPF_PROG_TYPE_LIRC_MODE2,
3204 BPF_LIRC_MODE2),
3205 BPF_APROG_SEC("flow_dissector", BPF_PROG_TYPE_FLOW_DISSECTOR,
3206 BPF_FLOW_DISSECTOR),
3207 BPF_EAPROG_SEC("cgroup/bind4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3208 BPF_CGROUP_INET4_BIND),
3209 BPF_EAPROG_SEC("cgroup/bind6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3210 BPF_CGROUP_INET6_BIND),
3211 BPF_EAPROG_SEC("cgroup/connect4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3212 BPF_CGROUP_INET4_CONNECT),
3213 BPF_EAPROG_SEC("cgroup/connect6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3214 BPF_CGROUP_INET6_CONNECT),
3215 BPF_EAPROG_SEC("cgroup/sendmsg4", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3216 BPF_CGROUP_UDP4_SENDMSG),
3217 BPF_EAPROG_SEC("cgroup/sendmsg6", BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
3218 BPF_CGROUP_UDP6_SENDMSG),
Andrey Ignatov063cc9f2019-03-08 09:15:26 -08003219 BPF_EAPROG_SEC("cgroup/sysctl", BPF_PROG_TYPE_CGROUP_SYSCTL,
3220 BPF_CGROUP_SYSCTL),
Roman Gushchin583c9002017-12-13 15:18:51 +00003221};
Roman Gushchin583c9002017-12-13 15:18:51 +00003222
Andrey Ignatov956b6202018-09-26 15:24:53 -07003223#undef BPF_PROG_SEC_IMPL
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003224#undef BPF_PROG_SEC
Andrey Ignatov956b6202018-09-26 15:24:53 -07003225#undef BPF_APROG_SEC
3226#undef BPF_EAPROG_SEC
3227#undef BPF_APROG_COMPAT
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003228
Taeung Songc76e4c22019-01-21 22:06:38 +09003229#define MAX_TYPE_NAME_SIZE 32
3230
3231static char *libbpf_get_type_names(bool attach_type)
3232{
3233 int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
3234 char *buf;
3235
3236 buf = malloc(len);
3237 if (!buf)
3238 return NULL;
3239
3240 buf[0] = '\0';
3241 /* Forge string buf with all available names */
3242 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3243 if (attach_type && !section_names[i].is_attachable)
3244 continue;
3245
3246 if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
3247 free(buf);
3248 return NULL;
3249 }
3250 strcat(buf, " ");
3251 strcat(buf, section_names[i].sec);
3252 }
3253
3254 return buf;
3255}
3256
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003257int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
3258 enum bpf_attach_type *expected_attach_type)
Roman Gushchin583c9002017-12-13 15:18:51 +00003259{
Taeung Songc76e4c22019-01-21 22:06:38 +09003260 char *type_names;
Roman Gushchin583c9002017-12-13 15:18:51 +00003261 int i;
3262
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003263 if (!name)
3264 return -EINVAL;
Roman Gushchin583c9002017-12-13 15:18:51 +00003265
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003266 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3267 if (strncmp(name, section_names[i].sec, section_names[i].len))
3268 continue;
3269 *prog_type = section_names[i].prog_type;
3270 *expected_attach_type = section_names[i].expected_attach_type;
3271 return 0;
3272 }
Taeung Songc76e4c22019-01-21 22:06:38 +09003273 pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
3274 type_names = libbpf_get_type_names(false);
3275 if (type_names != NULL) {
3276 pr_info("supported section(type) names are:%s\n", type_names);
3277 free(type_names);
3278 }
3279
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003280 return -EINVAL;
3281}
Roman Gushchin583c9002017-12-13 15:18:51 +00003282
Andrey Ignatov956b6202018-09-26 15:24:53 -07003283int libbpf_attach_type_by_name(const char *name,
3284 enum bpf_attach_type *attach_type)
3285{
Taeung Songc76e4c22019-01-21 22:06:38 +09003286 char *type_names;
Andrey Ignatov956b6202018-09-26 15:24:53 -07003287 int i;
3288
3289 if (!name)
3290 return -EINVAL;
3291
3292 for (i = 0; i < ARRAY_SIZE(section_names); i++) {
3293 if (strncmp(name, section_names[i].sec, section_names[i].len))
3294 continue;
Andrey Ignatov36153532018-10-31 12:57:18 -07003295 if (!section_names[i].is_attachable)
Andrey Ignatov956b6202018-09-26 15:24:53 -07003296 return -EINVAL;
3297 *attach_type = section_names[i].attach_type;
3298 return 0;
3299 }
Taeung Songc76e4c22019-01-21 22:06:38 +09003300 pr_warning("failed to guess attach type based on ELF section name '%s'\n", name);
3301 type_names = libbpf_get_type_names(true);
3302 if (type_names != NULL) {
3303 pr_info("attachable section(type) names are:%s\n", type_names);
3304 free(type_names);
3305 }
3306
Andrey Ignatov956b6202018-09-26 15:24:53 -07003307 return -EINVAL;
3308}
3309
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003310static int
3311bpf_program__identify_section(struct bpf_program *prog,
3312 enum bpf_prog_type *prog_type,
3313 enum bpf_attach_type *expected_attach_type)
3314{
3315 return libbpf_prog_type_by_name(prog->section_name, prog_type,
3316 expected_attach_type);
Roman Gushchin583c9002017-12-13 15:18:51 +00003317}
3318
Arnaldo Carvalho de Melo6e009e652016-06-03 12:15:52 -03003319int bpf_map__fd(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003320{
Arnaldo Carvalho de Melo6e009e652016-06-03 12:15:52 -03003321 return map ? map->fd : -EINVAL;
Wang Nan9d759a92015-11-27 08:47:35 +00003322}
3323
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03003324const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003325{
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03003326 return map ? &map->def : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00003327}
3328
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03003329const char *bpf_map__name(struct bpf_map *map)
Wang Nan561bbcc2015-11-27 08:47:36 +00003330{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03003331 return map ? map->name : NULL;
Wang Nan561bbcc2015-11-27 08:47:36 +00003332}
3333
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07003334__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003335{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003336 return map ? map->btf_key_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003337}
3338
Martin KaFai Lau5b891af2018-07-24 08:40:21 -07003339__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003340{
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -07003341 return map ? map->btf_value_type_id : 0;
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -07003342}
3343
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03003344int bpf_map__set_priv(struct bpf_map *map, void *priv,
3345 bpf_map_clear_priv_t clear_priv)
Wang Nan9d759a92015-11-27 08:47:35 +00003346{
3347 if (!map)
3348 return -EINVAL;
3349
3350 if (map->priv) {
3351 if (map->clear_priv)
3352 map->clear_priv(map, map->priv);
3353 }
3354
3355 map->priv = priv;
3356 map->clear_priv = clear_priv;
3357 return 0;
3358}
3359
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03003360void *bpf_map__priv(struct bpf_map *map)
Wang Nan9d759a92015-11-27 08:47:35 +00003361{
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03003362 return map ? map->priv : ERR_PTR(-EINVAL);
Wang Nan9d759a92015-11-27 08:47:35 +00003363}
3364
Jakub Kicinskif83fb222018-07-10 14:43:01 -07003365bool bpf_map__is_offload_neutral(struct bpf_map *map)
3366{
3367 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
3368}
3369
Daniel Borkmannd8599002019-04-09 23:20:13 +02003370bool bpf_map__is_internal(struct bpf_map *map)
3371{
3372 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
3373}
3374
Jakub Kicinski9aba3612018-06-28 14:41:37 -07003375void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
3376{
3377 map->map_ifindex = ifindex;
3378}
3379
Nikita V. Shirokovaddb9fc2018-11-20 20:55:56 -08003380int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
3381{
3382 if (!bpf_map_type__is_map_in_map(map->def.type)) {
3383 pr_warning("error: unsupported map type\n");
3384 return -EINVAL;
3385 }
3386 if (map->inner_map_fd != -1) {
3387 pr_warning("error: inner_map_fd already specified\n");
3388 return -EINVAL;
3389 }
3390 map->inner_map_fd = fd;
3391 return 0;
3392}
3393
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003394static struct bpf_map *
3395__bpf_map__iter(struct bpf_map *m, struct bpf_object *obj, int i)
Wang Nan9d759a92015-11-27 08:47:35 +00003396{
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003397 ssize_t idx;
Wang Nan9d759a92015-11-27 08:47:35 +00003398 struct bpf_map *s, *e;
3399
3400 if (!obj || !obj->maps)
3401 return NULL;
3402
3403 s = obj->maps;
3404 e = obj->maps + obj->nr_maps;
3405
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003406 if ((m < s) || (m >= e)) {
Wang Nan9d759a92015-11-27 08:47:35 +00003407 pr_warning("error in %s: map handler doesn't belong to object\n",
3408 __func__);
3409 return NULL;
3410 }
3411
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003412 idx = (m - obj->maps) + i;
3413 if (idx >= obj->nr_maps || idx < 0)
Wang Nan9d759a92015-11-27 08:47:35 +00003414 return NULL;
3415 return &obj->maps[idx];
3416}
Wang Nan561bbcc2015-11-27 08:47:36 +00003417
3418struct bpf_map *
Stanislav Fomichev0c19a9f2018-11-09 08:21:41 -08003419bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
3420{
3421 if (prev == NULL)
3422 return obj->maps;
3423
3424 return __bpf_map__iter(prev, obj, 1);
3425}
3426
3427struct bpf_map *
3428bpf_map__prev(struct bpf_map *next, struct bpf_object *obj)
3429{
3430 if (next == NULL) {
3431 if (!obj->nr_maps)
3432 return NULL;
3433 return obj->maps + obj->nr_maps - 1;
3434 }
3435
3436 return __bpf_map__iter(next, obj, -1);
3437}
3438
3439struct bpf_map *
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03003440bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
Wang Nan561bbcc2015-11-27 08:47:36 +00003441{
3442 struct bpf_map *pos;
3443
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003444 bpf_object__for_each_map(pos, obj) {
Wang Nan973170e2015-12-08 02:25:29 +00003445 if (pos->name && !strcmp(pos->name, name))
Wang Nan561bbcc2015-11-27 08:47:36 +00003446 return pos;
3447 }
3448 return NULL;
3449}
Wang Nan5a6acad2016-11-26 07:03:27 +00003450
Maciej Fijalkowskif3cea322019-02-01 22:42:23 +01003451int
3452bpf_object__find_map_fd_by_name(struct bpf_object *obj, const char *name)
3453{
3454 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
3455}
3456
Wang Nan5a6acad2016-11-26 07:03:27 +00003457struct bpf_map *
3458bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
3459{
3460 int i;
3461
3462 for (i = 0; i < obj->nr_maps; i++) {
3463 if (obj->maps[i].offset == offset)
3464 return &obj->maps[i];
3465 }
3466 return ERR_PTR(-ENOENT);
3467}
Joe Stringere28ff1a2017-01-22 17:11:25 -08003468
3469long libbpf_get_error(const void *ptr)
3470{
Hariprasad Kelamd98363b2019-05-25 14:32:57 +05303471 return PTR_ERR_OR_ZERO(ptr);
Joe Stringere28ff1a2017-01-22 17:11:25 -08003472}
John Fastabend6f6d33f2017-08-15 22:34:22 -07003473
3474int bpf_prog_load(const char *file, enum bpf_prog_type type,
3475 struct bpf_object **pobj, int *prog_fd)
3476{
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003477 struct bpf_prog_load_attr attr;
3478
3479 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
3480 attr.file = file;
3481 attr.prog_type = type;
3482 attr.expected_attach_type = 0;
3483
3484 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
3485}
3486
3487int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
3488 struct bpf_object **pobj, int *prog_fd)
3489{
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07003490 struct bpf_object_open_attr open_attr = {
3491 .file = attr->file,
3492 .prog_type = attr->prog_type,
3493 };
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003494 struct bpf_program *prog, *first_prog = NULL;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003495 enum bpf_attach_type expected_attach_type;
3496 enum bpf_prog_type prog_type;
John Fastabend6f6d33f2017-08-15 22:34:22 -07003497 struct bpf_object *obj;
David Beckettf0307a72018-05-16 14:02:49 -07003498 struct bpf_map *map;
John Fastabend6f6d33f2017-08-15 22:34:22 -07003499 int err;
3500
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003501 if (!attr)
3502 return -EINVAL;
Jakub Kicinski17387dd2018-05-10 10:24:42 -07003503 if (!attr->file)
3504 return -EINVAL;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003505
Jakub Kicinski07f2d4e2018-07-10 14:43:02 -07003506 obj = bpf_object__open_xattr(&open_attr);
Jakub Kicinski35976832018-05-10 10:09:34 -07003507 if (IS_ERR_OR_NULL(obj))
John Fastabend6f6d33f2017-08-15 22:34:22 -07003508 return -ENOENT;
3509
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003510 bpf_object__for_each_program(prog, obj) {
3511 /*
3512 * If type is not specified, try to guess it based on
3513 * section name.
3514 */
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003515 prog_type = attr->prog_type;
David Beckettf0307a72018-05-16 14:02:49 -07003516 prog->prog_ifindex = attr->ifindex;
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003517 expected_attach_type = attr->expected_attach_type;
3518 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
Jakub Kicinskib60df2a2018-07-10 14:42:59 -07003519 err = bpf_program__identify_section(prog, &prog_type,
3520 &expected_attach_type);
3521 if (err < 0) {
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003522 bpf_object__close(obj);
3523 return -EINVAL;
3524 }
3525 }
3526
Andrey Ignatovd7be1432018-03-30 15:08:01 -07003527 bpf_program__set_type(prog, prog_type);
3528 bpf_program__set_expected_attach_type(prog,
3529 expected_attach_type);
3530
Alexei Starovoitovda11b412019-04-01 21:27:47 -07003531 prog->log_level = attr->log_level;
Jiong Wang04656192019-05-24 23:25:19 +01003532 prog->prog_flags = attr->prog_flags;
Taeung Song69495d22018-09-03 08:30:07 +09003533 if (!first_prog)
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003534 first_prog = prog;
3535 }
3536
Jakub Kicinskif74a53d92019-02-27 19:04:12 -08003537 bpf_object__for_each_map(map, obj) {
Jakub Kicinskif83fb222018-07-10 14:43:01 -07003538 if (!bpf_map__is_offload_neutral(map))
3539 map->map_ifindex = attr->ifindex;
David Beckettf0307a72018-05-16 14:02:49 -07003540 }
3541
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003542 if (!first_prog) {
3543 pr_warning("object file doesn't contain bpf program\n");
John Fastabend6f6d33f2017-08-15 22:34:22 -07003544 bpf_object__close(obj);
3545 return -ENOENT;
3546 }
3547
John Fastabend6f6d33f2017-08-15 22:34:22 -07003548 err = bpf_object__load(obj);
3549 if (err) {
3550 bpf_object__close(obj);
3551 return -EINVAL;
3552 }
3553
3554 *pobj = obj;
Alexei Starovoitov48cca7e2017-12-14 17:55:10 -08003555 *prog_fd = bpf_program__fd(first_prog);
John Fastabend6f6d33f2017-08-15 22:34:22 -07003556 return 0;
3557}
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003558
3559enum bpf_perf_event_ret
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003560bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
3561 void **copy_mem, size_t *copy_size,
3562 bpf_perf_event_print_t fn, void *private_data)
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003563{
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003564 struct perf_event_mmap_page *header = mmap_mem;
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02003565 __u64 data_head = ring_buffer_read_head(header);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003566 __u64 data_tail = header->data_tail;
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003567 void *base = ((__u8 *)header) + page_size;
3568 int ret = LIBBPF_PERF_EVENT_CONT;
3569 struct perf_event_header *ehdr;
3570 size_t ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003571
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003572 while (data_head != data_tail) {
3573 ehdr = base + (data_tail & (mmap_size - 1));
3574 ehdr_size = ehdr->size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003575
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003576 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
3577 void *copy_start = ehdr;
3578 size_t len_first = base + mmap_size - copy_start;
3579 size_t len_secnd = ehdr_size - len_first;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003580
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003581 if (*copy_size < ehdr_size) {
3582 free(*copy_mem);
3583 *copy_mem = malloc(ehdr_size);
3584 if (!*copy_mem) {
3585 *copy_size = 0;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003586 ret = LIBBPF_PERF_EVENT_ERROR;
3587 break;
3588 }
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003589 *copy_size = ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003590 }
3591
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003592 memcpy(*copy_mem, copy_start, len_first);
3593 memcpy(*copy_mem + len_first, base, len_secnd);
3594 ehdr = *copy_mem;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003595 }
3596
Daniel Borkmann3dca2112018-10-21 02:09:28 +02003597 ret = fn(ehdr, private_data);
3598 data_tail += ehdr_size;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003599 if (ret != LIBBPF_PERF_EVENT_CONT)
3600 break;
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003601 }
3602
Daniel Borkmanna64af0e2018-10-19 15:51:03 +02003603 ring_buffer_write_tail(header, data_tail);
Jakub Kicinskid0cabbb2018-05-10 10:24:40 -07003604 return ret;
3605}
Song Liu34be16462019-03-11 22:30:38 -07003606
3607struct bpf_prog_info_array_desc {
3608 int array_offset; /* e.g. offset of jited_prog_insns */
3609 int count_offset; /* e.g. offset of jited_prog_len */
3610 int size_offset; /* > 0: offset of rec size,
3611 * < 0: fix size of -size_offset
3612 */
3613};
3614
3615static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
3616 [BPF_PROG_INFO_JITED_INSNS] = {
3617 offsetof(struct bpf_prog_info, jited_prog_insns),
3618 offsetof(struct bpf_prog_info, jited_prog_len),
3619 -1,
3620 },
3621 [BPF_PROG_INFO_XLATED_INSNS] = {
3622 offsetof(struct bpf_prog_info, xlated_prog_insns),
3623 offsetof(struct bpf_prog_info, xlated_prog_len),
3624 -1,
3625 },
3626 [BPF_PROG_INFO_MAP_IDS] = {
3627 offsetof(struct bpf_prog_info, map_ids),
3628 offsetof(struct bpf_prog_info, nr_map_ids),
3629 -(int)sizeof(__u32),
3630 },
3631 [BPF_PROG_INFO_JITED_KSYMS] = {
3632 offsetof(struct bpf_prog_info, jited_ksyms),
3633 offsetof(struct bpf_prog_info, nr_jited_ksyms),
3634 -(int)sizeof(__u64),
3635 },
3636 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
3637 offsetof(struct bpf_prog_info, jited_func_lens),
3638 offsetof(struct bpf_prog_info, nr_jited_func_lens),
3639 -(int)sizeof(__u32),
3640 },
3641 [BPF_PROG_INFO_FUNC_INFO] = {
3642 offsetof(struct bpf_prog_info, func_info),
3643 offsetof(struct bpf_prog_info, nr_func_info),
3644 offsetof(struct bpf_prog_info, func_info_rec_size),
3645 },
3646 [BPF_PROG_INFO_LINE_INFO] = {
3647 offsetof(struct bpf_prog_info, line_info),
3648 offsetof(struct bpf_prog_info, nr_line_info),
3649 offsetof(struct bpf_prog_info, line_info_rec_size),
3650 },
3651 [BPF_PROG_INFO_JITED_LINE_INFO] = {
3652 offsetof(struct bpf_prog_info, jited_line_info),
3653 offsetof(struct bpf_prog_info, nr_jited_line_info),
3654 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
3655 },
3656 [BPF_PROG_INFO_PROG_TAGS] = {
3657 offsetof(struct bpf_prog_info, prog_tags),
3658 offsetof(struct bpf_prog_info, nr_prog_tags),
3659 -(int)sizeof(__u8) * BPF_TAG_SIZE,
3660 },
3661
3662};
3663
3664static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, int offset)
3665{
3666 __u32 *array = (__u32 *)info;
3667
3668 if (offset >= 0)
3669 return array[offset / sizeof(__u32)];
3670 return -(int)offset;
3671}
3672
3673static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, int offset)
3674{
3675 __u64 *array = (__u64 *)info;
3676
3677 if (offset >= 0)
3678 return array[offset / sizeof(__u64)];
3679 return -(int)offset;
3680}
3681
3682static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
3683 __u32 val)
3684{
3685 __u32 *array = (__u32 *)info;
3686
3687 if (offset >= 0)
3688 array[offset / sizeof(__u32)] = val;
3689}
3690
3691static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
3692 __u64 val)
3693{
3694 __u64 *array = (__u64 *)info;
3695
3696 if (offset >= 0)
3697 array[offset / sizeof(__u64)] = val;
3698}
3699
3700struct bpf_prog_info_linear *
3701bpf_program__get_prog_info_linear(int fd, __u64 arrays)
3702{
3703 struct bpf_prog_info_linear *info_linear;
3704 struct bpf_prog_info info = {};
3705 __u32 info_len = sizeof(info);
3706 __u32 data_len = 0;
3707 int i, err;
3708 void *ptr;
3709
3710 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
3711 return ERR_PTR(-EINVAL);
3712
3713 /* step 1: get array dimensions */
3714 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
3715 if (err) {
3716 pr_debug("can't get prog info: %s", strerror(errno));
3717 return ERR_PTR(-EFAULT);
3718 }
3719
3720 /* step 2: calculate total size of all arrays */
3721 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3722 bool include_array = (arrays & (1UL << i)) > 0;
3723 struct bpf_prog_info_array_desc *desc;
3724 __u32 count, size;
3725
3726 desc = bpf_prog_info_array_desc + i;
3727
3728 /* kernel is too old to support this field */
3729 if (info_len < desc->array_offset + sizeof(__u32) ||
3730 info_len < desc->count_offset + sizeof(__u32) ||
3731 (desc->size_offset > 0 && info_len < desc->size_offset))
3732 include_array = false;
3733
3734 if (!include_array) {
3735 arrays &= ~(1UL << i); /* clear the bit */
3736 continue;
3737 }
3738
3739 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3740 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3741
3742 data_len += count * size;
3743 }
3744
3745 /* step 3: allocate continuous memory */
3746 data_len = roundup(data_len, sizeof(__u64));
3747 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
3748 if (!info_linear)
3749 return ERR_PTR(-ENOMEM);
3750
3751 /* step 4: fill data to info_linear->info */
3752 info_linear->arrays = arrays;
3753 memset(&info_linear->info, 0, sizeof(info));
3754 ptr = info_linear->data;
3755
3756 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3757 struct bpf_prog_info_array_desc *desc;
3758 __u32 count, size;
3759
3760 if ((arrays & (1UL << i)) == 0)
3761 continue;
3762
3763 desc = bpf_prog_info_array_desc + i;
3764 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3765 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3766 bpf_prog_info_set_offset_u32(&info_linear->info,
3767 desc->count_offset, count);
3768 bpf_prog_info_set_offset_u32(&info_linear->info,
3769 desc->size_offset, size);
3770 bpf_prog_info_set_offset_u64(&info_linear->info,
3771 desc->array_offset,
3772 ptr_to_u64(ptr));
3773 ptr += count * size;
3774 }
3775
3776 /* step 5: call syscall again to get required arrays */
3777 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
3778 if (err) {
3779 pr_debug("can't get prog info: %s", strerror(errno));
3780 free(info_linear);
3781 return ERR_PTR(-EFAULT);
3782 }
3783
3784 /* step 6: verify the data */
3785 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3786 struct bpf_prog_info_array_desc *desc;
3787 __u32 v1, v2;
3788
3789 if ((arrays & (1UL << i)) == 0)
3790 continue;
3791
3792 desc = bpf_prog_info_array_desc + i;
3793 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
3794 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
3795 desc->count_offset);
3796 if (v1 != v2)
3797 pr_warning("%s: mismatch in element count\n", __func__);
3798
3799 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
3800 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
3801 desc->size_offset);
3802 if (v1 != v2)
3803 pr_warning("%s: mismatch in rec size\n", __func__);
3804 }
3805
3806 /* step 7: update info_len and data_len */
3807 info_linear->info_len = sizeof(struct bpf_prog_info);
3808 info_linear->data_len = data_len;
3809
3810 return info_linear;
3811}
3812
3813void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
3814{
3815 int i;
3816
3817 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3818 struct bpf_prog_info_array_desc *desc;
3819 __u64 addr, offs;
3820
3821 if ((info_linear->arrays & (1UL << i)) == 0)
3822 continue;
3823
3824 desc = bpf_prog_info_array_desc + i;
3825 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
3826 desc->array_offset);
3827 offs = addr - ptr_to_u64(info_linear->data);
3828 bpf_prog_info_set_offset_u64(&info_linear->info,
3829 desc->array_offset, offs);
3830 }
3831}
3832
3833void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
3834{
3835 int i;
3836
3837 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
3838 struct bpf_prog_info_array_desc *desc;
3839 __u64 addr, offs;
3840
3841 if ((info_linear->arrays & (1UL << i)) == 0)
3842 continue;
3843
3844 desc = bpf_prog_info_array_desc + i;
3845 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
3846 desc->array_offset);
3847 addr = offs + ptr_to_u64(info_linear->data);
3848 bpf_prog_info_set_offset_u64(&info_linear->info,
3849 desc->array_offset, addr);
3850 }
3851}