xref: /linux/tools/lib/bpf/libbpf.c (revision ef347a340b1a8507c22ee3cf981cd5cd64188431)
1 // SPDX-License-Identifier: LGPL-2.1
2 
3 /*
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.
9  * Copyright (C) 2017 Nicira, Inc.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License (not later!)
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not,  see <http://www.gnu.org/licenses>
23  */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <libgen.h>
29 #include <inttypes.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <perf-sys.h>
35 #include <asm/unistd.h>
36 #include <linux/err.h>
37 #include <linux/kernel.h>
38 #include <linux/bpf.h>
39 #include <linux/list.h>
40 #include <linux/limits.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/vfs.h>
44 #include <libelf.h>
45 #include <gelf.h>
46 
47 #include "libbpf.h"
48 #include "bpf.h"
49 #include "btf.h"
50 
51 #ifndef EM_BPF
52 #define EM_BPF 247
53 #endif
54 
55 #ifndef BPF_FS_MAGIC
56 #define BPF_FS_MAGIC		0xcafe4a11
57 #endif
58 
59 #define __printf(a, b)	__attribute__((format(printf, a, b)))
60 
61 __printf(1, 2)
62 static int __base_pr(const char *format, ...)
63 {
64 	va_list args;
65 	int err;
66 
67 	va_start(args, format);
68 	err = vfprintf(stderr, format, args);
69 	va_end(args);
70 	return err;
71 }
72 
73 static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
74 static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
75 static __printf(1, 2) libbpf_print_fn_t __pr_debug;
76 
77 #define __pr(func, fmt, ...)	\
78 do {				\
79 	if ((func))		\
80 		(func)("libbpf: " fmt, ##__VA_ARGS__); \
81 } while (0)
82 
83 #define pr_warning(fmt, ...)	__pr(__pr_warning, fmt, ##__VA_ARGS__)
84 #define pr_info(fmt, ...)	__pr(__pr_info, fmt, ##__VA_ARGS__)
85 #define pr_debug(fmt, ...)	__pr(__pr_debug, fmt, ##__VA_ARGS__)
86 
87 void libbpf_set_print(libbpf_print_fn_t warn,
88 		      libbpf_print_fn_t info,
89 		      libbpf_print_fn_t debug)
90 {
91 	__pr_warning = warn;
92 	__pr_info = info;
93 	__pr_debug = debug;
94 }
95 
96 #define STRERR_BUFSIZE  128
97 
98 #define ERRNO_OFFSET(e)		((e) - __LIBBPF_ERRNO__START)
99 #define ERRCODE_OFFSET(c)	ERRNO_OFFSET(LIBBPF_ERRNO__##c)
100 #define NR_ERRNO	(__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
101 
102 static const char *libbpf_strerror_table[NR_ERRNO] = {
103 	[ERRCODE_OFFSET(LIBELF)]	= "Something wrong in libelf",
104 	[ERRCODE_OFFSET(FORMAT)]	= "BPF object format invalid",
105 	[ERRCODE_OFFSET(KVERSION)]	= "'version' section incorrect or lost",
106 	[ERRCODE_OFFSET(ENDIAN)]	= "Endian mismatch",
107 	[ERRCODE_OFFSET(INTERNAL)]	= "Internal error in libbpf",
108 	[ERRCODE_OFFSET(RELOC)]		= "Relocation failed",
109 	[ERRCODE_OFFSET(VERIFY)]	= "Kernel verifier blocks program loading",
110 	[ERRCODE_OFFSET(PROG2BIG)]	= "Program too big",
111 	[ERRCODE_OFFSET(KVER)]		= "Incorrect kernel version",
112 	[ERRCODE_OFFSET(PROGTYPE)]	= "Kernel doesn't support this program type",
113 	[ERRCODE_OFFSET(WRNGPID)]	= "Wrong pid in netlink message",
114 	[ERRCODE_OFFSET(INVSEQ)]	= "Invalid netlink sequence",
115 };
116 
117 int libbpf_strerror(int err, char *buf, size_t size)
118 {
119 	if (!buf || !size)
120 		return -1;
121 
122 	err = err > 0 ? err : -err;
123 
124 	if (err < __LIBBPF_ERRNO__START) {
125 		int ret;
126 
127 		ret = strerror_r(err, buf, size);
128 		buf[size - 1] = '\0';
129 		return ret;
130 	}
131 
132 	if (err < __LIBBPF_ERRNO__END) {
133 		const char *msg;
134 
135 		msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
136 		snprintf(buf, size, "%s", msg);
137 		buf[size - 1] = '\0';
138 		return 0;
139 	}
140 
141 	snprintf(buf, size, "Unknown libbpf error %d", err);
142 	buf[size - 1] = '\0';
143 	return -1;
144 }
145 
146 #define CHECK_ERR(action, err, out) do {	\
147 	err = action;			\
148 	if (err)			\
149 		goto out;		\
150 } while(0)
151 
152 
153 /* Copied from tools/perf/util/util.h */
154 #ifndef zfree
155 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
156 #endif
157 
158 #ifndef zclose
159 # define zclose(fd) ({			\
160 	int ___err = 0;			\
161 	if ((fd) >= 0)			\
162 		___err = close((fd));	\
163 	fd = -1;			\
164 	___err; })
165 #endif
166 
167 #ifdef HAVE_LIBELF_MMAP_SUPPORT
168 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
169 #else
170 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
171 #endif
172 
173 /*
174  * bpf_prog should be a better name but it has been used in
175  * linux/filter.h.
176  */
177 struct bpf_program {
178 	/* Index in elf obj file, for relocation use. */
179 	int idx;
180 	char *name;
181 	int prog_ifindex;
182 	char *section_name;
183 	struct bpf_insn *insns;
184 	size_t insns_cnt, main_prog_cnt;
185 	enum bpf_prog_type type;
186 
187 	struct reloc_desc {
188 		enum {
189 			RELO_LD64,
190 			RELO_CALL,
191 		} type;
192 		int insn_idx;
193 		union {
194 			int map_idx;
195 			int text_off;
196 		};
197 	} *reloc_desc;
198 	int nr_reloc;
199 
200 	struct {
201 		int nr;
202 		int *fds;
203 	} instances;
204 	bpf_program_prep_t preprocessor;
205 
206 	struct bpf_object *obj;
207 	void *priv;
208 	bpf_program_clear_priv_t clear_priv;
209 
210 	enum bpf_attach_type expected_attach_type;
211 };
212 
213 struct bpf_map {
214 	int fd;
215 	char *name;
216 	size_t offset;
217 	int map_ifindex;
218 	struct bpf_map_def def;
219 	uint32_t btf_key_type_id;
220 	uint32_t btf_value_type_id;
221 	void *priv;
222 	bpf_map_clear_priv_t clear_priv;
223 };
224 
225 static LIST_HEAD(bpf_objects_list);
226 
227 struct bpf_object {
228 	char license[64];
229 	u32 kern_version;
230 
231 	struct bpf_program *programs;
232 	size_t nr_programs;
233 	struct bpf_map *maps;
234 	size_t nr_maps;
235 
236 	bool loaded;
237 	bool has_pseudo_calls;
238 
239 	/*
240 	 * Information when doing elf related work. Only valid if fd
241 	 * is valid.
242 	 */
243 	struct {
244 		int fd;
245 		void *obj_buf;
246 		size_t obj_buf_sz;
247 		Elf *elf;
248 		GElf_Ehdr ehdr;
249 		Elf_Data *symbols;
250 		size_t strtabidx;
251 		struct {
252 			GElf_Shdr shdr;
253 			Elf_Data *data;
254 		} *reloc;
255 		int nr_reloc;
256 		int maps_shndx;
257 		int text_shndx;
258 	} efile;
259 	/*
260 	 * All loaded bpf_object is linked in a list, which is
261 	 * hidden to caller. bpf_objects__<func> handlers deal with
262 	 * all objects.
263 	 */
264 	struct list_head list;
265 
266 	struct btf *btf;
267 
268 	void *priv;
269 	bpf_object_clear_priv_t clear_priv;
270 
271 	char path[];
272 };
273 #define obj_elf_valid(o)	((o)->efile.elf)
274 
275 static void bpf_program__unload(struct bpf_program *prog)
276 {
277 	int i;
278 
279 	if (!prog)
280 		return;
281 
282 	/*
283 	 * If the object is opened but the program was never loaded,
284 	 * it is possible that prog->instances.nr == -1.
285 	 */
286 	if (prog->instances.nr > 0) {
287 		for (i = 0; i < prog->instances.nr; i++)
288 			zclose(prog->instances.fds[i]);
289 	} else if (prog->instances.nr != -1) {
290 		pr_warning("Internal error: instances.nr is %d\n",
291 			   prog->instances.nr);
292 	}
293 
294 	prog->instances.nr = -1;
295 	zfree(&prog->instances.fds);
296 }
297 
298 static void bpf_program__exit(struct bpf_program *prog)
299 {
300 	if (!prog)
301 		return;
302 
303 	if (prog->clear_priv)
304 		prog->clear_priv(prog, prog->priv);
305 
306 	prog->priv = NULL;
307 	prog->clear_priv = NULL;
308 
309 	bpf_program__unload(prog);
310 	zfree(&prog->name);
311 	zfree(&prog->section_name);
312 	zfree(&prog->insns);
313 	zfree(&prog->reloc_desc);
314 
315 	prog->nr_reloc = 0;
316 	prog->insns_cnt = 0;
317 	prog->idx = -1;
318 }
319 
320 static int
321 bpf_program__init(void *data, size_t size, char *section_name, int idx,
322 		  struct bpf_program *prog)
323 {
324 	if (size < sizeof(struct bpf_insn)) {
325 		pr_warning("corrupted section '%s'\n", section_name);
326 		return -EINVAL;
327 	}
328 
329 	bzero(prog, sizeof(*prog));
330 
331 	prog->section_name = strdup(section_name);
332 	if (!prog->section_name) {
333 		pr_warning("failed to alloc name for prog under section(%d) %s\n",
334 			   idx, section_name);
335 		goto errout;
336 	}
337 
338 	prog->insns = malloc(size);
339 	if (!prog->insns) {
340 		pr_warning("failed to alloc insns for prog under section %s\n",
341 			   section_name);
342 		goto errout;
343 	}
344 	prog->insns_cnt = size / sizeof(struct bpf_insn);
345 	memcpy(prog->insns, data,
346 	       prog->insns_cnt * sizeof(struct bpf_insn));
347 	prog->idx = idx;
348 	prog->instances.fds = NULL;
349 	prog->instances.nr = -1;
350 	prog->type = BPF_PROG_TYPE_KPROBE;
351 
352 	return 0;
353 errout:
354 	bpf_program__exit(prog);
355 	return -ENOMEM;
356 }
357 
358 static int
359 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
360 			char *section_name, int idx)
361 {
362 	struct bpf_program prog, *progs;
363 	int nr_progs, err;
364 
365 	err = bpf_program__init(data, size, section_name, idx, &prog);
366 	if (err)
367 		return err;
368 
369 	progs = obj->programs;
370 	nr_progs = obj->nr_programs;
371 
372 	progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
373 	if (!progs) {
374 		/*
375 		 * In this case the original obj->programs
376 		 * is still valid, so don't need special treat for
377 		 * bpf_close_object().
378 		 */
379 		pr_warning("failed to alloc a new program under section '%s'\n",
380 			   section_name);
381 		bpf_program__exit(&prog);
382 		return -ENOMEM;
383 	}
384 
385 	pr_debug("found program %s\n", prog.section_name);
386 	obj->programs = progs;
387 	obj->nr_programs = nr_progs + 1;
388 	prog.obj = obj;
389 	progs[nr_progs] = prog;
390 	return 0;
391 }
392 
393 static int
394 bpf_object__init_prog_names(struct bpf_object *obj)
395 {
396 	Elf_Data *symbols = obj->efile.symbols;
397 	struct bpf_program *prog;
398 	size_t pi, si;
399 
400 	for (pi = 0; pi < obj->nr_programs; pi++) {
401 		const char *name = NULL;
402 
403 		prog = &obj->programs[pi];
404 
405 		for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
406 		     si++) {
407 			GElf_Sym sym;
408 
409 			if (!gelf_getsym(symbols, si, &sym))
410 				continue;
411 			if (sym.st_shndx != prog->idx)
412 				continue;
413 			if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
414 				continue;
415 
416 			name = elf_strptr(obj->efile.elf,
417 					  obj->efile.strtabidx,
418 					  sym.st_name);
419 			if (!name) {
420 				pr_warning("failed to get sym name string for prog %s\n",
421 					   prog->section_name);
422 				return -LIBBPF_ERRNO__LIBELF;
423 			}
424 		}
425 
426 		if (!name && prog->idx == obj->efile.text_shndx)
427 			name = ".text";
428 
429 		if (!name) {
430 			pr_warning("failed to find sym for prog %s\n",
431 				   prog->section_name);
432 			return -EINVAL;
433 		}
434 
435 		prog->name = strdup(name);
436 		if (!prog->name) {
437 			pr_warning("failed to allocate memory for prog sym %s\n",
438 				   name);
439 			return -ENOMEM;
440 		}
441 	}
442 
443 	return 0;
444 }
445 
446 static struct bpf_object *bpf_object__new(const char *path,
447 					  void *obj_buf,
448 					  size_t obj_buf_sz)
449 {
450 	struct bpf_object *obj;
451 
452 	obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
453 	if (!obj) {
454 		pr_warning("alloc memory failed for %s\n", path);
455 		return ERR_PTR(-ENOMEM);
456 	}
457 
458 	strcpy(obj->path, path);
459 	obj->efile.fd = -1;
460 
461 	/*
462 	 * Caller of this function should also calls
463 	 * bpf_object__elf_finish() after data collection to return
464 	 * obj_buf to user. If not, we should duplicate the buffer to
465 	 * avoid user freeing them before elf finish.
466 	 */
467 	obj->efile.obj_buf = obj_buf;
468 	obj->efile.obj_buf_sz = obj_buf_sz;
469 	obj->efile.maps_shndx = -1;
470 
471 	obj->loaded = false;
472 
473 	INIT_LIST_HEAD(&obj->list);
474 	list_add(&obj->list, &bpf_objects_list);
475 	return obj;
476 }
477 
478 static void bpf_object__elf_finish(struct bpf_object *obj)
479 {
480 	if (!obj_elf_valid(obj))
481 		return;
482 
483 	if (obj->efile.elf) {
484 		elf_end(obj->efile.elf);
485 		obj->efile.elf = NULL;
486 	}
487 	obj->efile.symbols = NULL;
488 
489 	zfree(&obj->efile.reloc);
490 	obj->efile.nr_reloc = 0;
491 	zclose(obj->efile.fd);
492 	obj->efile.obj_buf = NULL;
493 	obj->efile.obj_buf_sz = 0;
494 }
495 
496 static int bpf_object__elf_init(struct bpf_object *obj)
497 {
498 	int err = 0;
499 	GElf_Ehdr *ep;
500 
501 	if (obj_elf_valid(obj)) {
502 		pr_warning("elf init: internal error\n");
503 		return -LIBBPF_ERRNO__LIBELF;
504 	}
505 
506 	if (obj->efile.obj_buf_sz > 0) {
507 		/*
508 		 * obj_buf should have been validated by
509 		 * bpf_object__open_buffer().
510 		 */
511 		obj->efile.elf = elf_memory(obj->efile.obj_buf,
512 					    obj->efile.obj_buf_sz);
513 	} else {
514 		obj->efile.fd = open(obj->path, O_RDONLY);
515 		if (obj->efile.fd < 0) {
516 			pr_warning("failed to open %s: %s\n", obj->path,
517 					strerror(errno));
518 			return -errno;
519 		}
520 
521 		obj->efile.elf = elf_begin(obj->efile.fd,
522 				LIBBPF_ELF_C_READ_MMAP,
523 				NULL);
524 	}
525 
526 	if (!obj->efile.elf) {
527 		pr_warning("failed to open %s as ELF file\n",
528 				obj->path);
529 		err = -LIBBPF_ERRNO__LIBELF;
530 		goto errout;
531 	}
532 
533 	if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
534 		pr_warning("failed to get EHDR from %s\n",
535 				obj->path);
536 		err = -LIBBPF_ERRNO__FORMAT;
537 		goto errout;
538 	}
539 	ep = &obj->efile.ehdr;
540 
541 	/* Old LLVM set e_machine to EM_NONE */
542 	if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
543 		pr_warning("%s is not an eBPF object file\n",
544 			obj->path);
545 		err = -LIBBPF_ERRNO__FORMAT;
546 		goto errout;
547 	}
548 
549 	return 0;
550 errout:
551 	bpf_object__elf_finish(obj);
552 	return err;
553 }
554 
555 static int
556 bpf_object__check_endianness(struct bpf_object *obj)
557 {
558 	static unsigned int const endian = 1;
559 
560 	switch (obj->efile.ehdr.e_ident[EI_DATA]) {
561 	case ELFDATA2LSB:
562 		/* We are big endian, BPF obj is little endian. */
563 		if (*(unsigned char const *)&endian != 1)
564 			goto mismatch;
565 		break;
566 
567 	case ELFDATA2MSB:
568 		/* We are little endian, BPF obj is big endian. */
569 		if (*(unsigned char const *)&endian != 0)
570 			goto mismatch;
571 		break;
572 	default:
573 		return -LIBBPF_ERRNO__ENDIAN;
574 	}
575 
576 	return 0;
577 
578 mismatch:
579 	pr_warning("Error: endianness mismatch.\n");
580 	return -LIBBPF_ERRNO__ENDIAN;
581 }
582 
583 static int
584 bpf_object__init_license(struct bpf_object *obj,
585 			 void *data, size_t size)
586 {
587 	memcpy(obj->license, data,
588 	       min(size, sizeof(obj->license) - 1));
589 	pr_debug("license of %s is %s\n", obj->path, obj->license);
590 	return 0;
591 }
592 
593 static int
594 bpf_object__init_kversion(struct bpf_object *obj,
595 			  void *data, size_t size)
596 {
597 	u32 kver;
598 
599 	if (size != sizeof(kver)) {
600 		pr_warning("invalid kver section in %s\n", obj->path);
601 		return -LIBBPF_ERRNO__FORMAT;
602 	}
603 	memcpy(&kver, data, sizeof(kver));
604 	obj->kern_version = kver;
605 	pr_debug("kernel version of %s is %x\n", obj->path,
606 		 obj->kern_version);
607 	return 0;
608 }
609 
610 static int compare_bpf_map(const void *_a, const void *_b)
611 {
612 	const struct bpf_map *a = _a;
613 	const struct bpf_map *b = _b;
614 
615 	return a->offset - b->offset;
616 }
617 
618 static int
619 bpf_object__init_maps(struct bpf_object *obj)
620 {
621 	int i, map_idx, map_def_sz, nr_maps = 0;
622 	Elf_Scn *scn;
623 	Elf_Data *data;
624 	Elf_Data *symbols = obj->efile.symbols;
625 
626 	if (obj->efile.maps_shndx < 0)
627 		return -EINVAL;
628 	if (!symbols)
629 		return -EINVAL;
630 
631 	scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
632 	if (scn)
633 		data = elf_getdata(scn, NULL);
634 	if (!scn || !data) {
635 		pr_warning("failed to get Elf_Data from map section %d\n",
636 			   obj->efile.maps_shndx);
637 		return -EINVAL;
638 	}
639 
640 	/*
641 	 * Count number of maps. Each map has a name.
642 	 * Array of maps is not supported: only the first element is
643 	 * considered.
644 	 *
645 	 * TODO: Detect array of map and report error.
646 	 */
647 	for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
648 		GElf_Sym sym;
649 
650 		if (!gelf_getsym(symbols, i, &sym))
651 			continue;
652 		if (sym.st_shndx != obj->efile.maps_shndx)
653 			continue;
654 		nr_maps++;
655 	}
656 
657 	/* Alloc obj->maps and fill nr_maps. */
658 	pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
659 		 nr_maps, data->d_size);
660 
661 	if (!nr_maps)
662 		return 0;
663 
664 	/* Assume equally sized map definitions */
665 	map_def_sz = data->d_size / nr_maps;
666 	if (!data->d_size || (data->d_size % nr_maps) != 0) {
667 		pr_warning("unable to determine map definition size "
668 			   "section %s, %d maps in %zd bytes\n",
669 			   obj->path, nr_maps, data->d_size);
670 		return -EINVAL;
671 	}
672 
673 	obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
674 	if (!obj->maps) {
675 		pr_warning("alloc maps for object failed\n");
676 		return -ENOMEM;
677 	}
678 	obj->nr_maps = nr_maps;
679 
680 	/*
681 	 * fill all fd with -1 so won't close incorrect
682 	 * fd (fd=0 is stdin) when failure (zclose won't close
683 	 * negative fd)).
684 	 */
685 	for (i = 0; i < nr_maps; i++)
686 		obj->maps[i].fd = -1;
687 
688 	/*
689 	 * Fill obj->maps using data in "maps" section.
690 	 */
691 	for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
692 		GElf_Sym sym;
693 		const char *map_name;
694 		struct bpf_map_def *def;
695 
696 		if (!gelf_getsym(symbols, i, &sym))
697 			continue;
698 		if (sym.st_shndx != obj->efile.maps_shndx)
699 			continue;
700 
701 		map_name = elf_strptr(obj->efile.elf,
702 				      obj->efile.strtabidx,
703 				      sym.st_name);
704 		obj->maps[map_idx].offset = sym.st_value;
705 		if (sym.st_value + map_def_sz > data->d_size) {
706 			pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
707 				   obj->path, map_name);
708 			return -EINVAL;
709 		}
710 
711 		obj->maps[map_idx].name = strdup(map_name);
712 		if (!obj->maps[map_idx].name) {
713 			pr_warning("failed to alloc map name\n");
714 			return -ENOMEM;
715 		}
716 		pr_debug("map %d is \"%s\"\n", map_idx,
717 			 obj->maps[map_idx].name);
718 		def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
719 		/*
720 		 * If the definition of the map in the object file fits in
721 		 * bpf_map_def, copy it.  Any extra fields in our version
722 		 * of bpf_map_def will default to zero as a result of the
723 		 * calloc above.
724 		 */
725 		if (map_def_sz <= sizeof(struct bpf_map_def)) {
726 			memcpy(&obj->maps[map_idx].def, def, map_def_sz);
727 		} else {
728 			/*
729 			 * Here the map structure being read is bigger than what
730 			 * we expect, truncate if the excess bits are all zero.
731 			 * If they are not zero, reject this map as
732 			 * incompatible.
733 			 */
734 			char *b;
735 			for (b = ((char *)def) + sizeof(struct bpf_map_def);
736 			     b < ((char *)def) + map_def_sz; b++) {
737 				if (*b != 0) {
738 					pr_warning("maps section in %s: \"%s\" "
739 						   "has unrecognized, non-zero "
740 						   "options\n",
741 						   obj->path, map_name);
742 					return -EINVAL;
743 				}
744 			}
745 			memcpy(&obj->maps[map_idx].def, def,
746 			       sizeof(struct bpf_map_def));
747 		}
748 		map_idx++;
749 	}
750 
751 	qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
752 	return 0;
753 }
754 
755 static bool section_have_execinstr(struct bpf_object *obj, int idx)
756 {
757 	Elf_Scn *scn;
758 	GElf_Shdr sh;
759 
760 	scn = elf_getscn(obj->efile.elf, idx);
761 	if (!scn)
762 		return false;
763 
764 	if (gelf_getshdr(scn, &sh) != &sh)
765 		return false;
766 
767 	if (sh.sh_flags & SHF_EXECINSTR)
768 		return true;
769 
770 	return false;
771 }
772 
773 static int bpf_object__elf_collect(struct bpf_object *obj)
774 {
775 	Elf *elf = obj->efile.elf;
776 	GElf_Ehdr *ep = &obj->efile.ehdr;
777 	Elf_Scn *scn = NULL;
778 	int idx = 0, err = 0;
779 
780 	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
781 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
782 		pr_warning("failed to get e_shstrndx from %s\n",
783 			   obj->path);
784 		return -LIBBPF_ERRNO__FORMAT;
785 	}
786 
787 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
788 		char *name;
789 		GElf_Shdr sh;
790 		Elf_Data *data;
791 
792 		idx++;
793 		if (gelf_getshdr(scn, &sh) != &sh) {
794 			pr_warning("failed to get section(%d) header from %s\n",
795 				   idx, obj->path);
796 			err = -LIBBPF_ERRNO__FORMAT;
797 			goto out;
798 		}
799 
800 		name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
801 		if (!name) {
802 			pr_warning("failed to get section(%d) name from %s\n",
803 				   idx, obj->path);
804 			err = -LIBBPF_ERRNO__FORMAT;
805 			goto out;
806 		}
807 
808 		data = elf_getdata(scn, 0);
809 		if (!data) {
810 			pr_warning("failed to get section(%d) data from %s(%s)\n",
811 				   idx, name, obj->path);
812 			err = -LIBBPF_ERRNO__FORMAT;
813 			goto out;
814 		}
815 		pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
816 			 idx, name, (unsigned long)data->d_size,
817 			 (int)sh.sh_link, (unsigned long)sh.sh_flags,
818 			 (int)sh.sh_type);
819 
820 		if (strcmp(name, "license") == 0)
821 			err = bpf_object__init_license(obj,
822 						       data->d_buf,
823 						       data->d_size);
824 		else if (strcmp(name, "version") == 0)
825 			err = bpf_object__init_kversion(obj,
826 							data->d_buf,
827 							data->d_size);
828 		else if (strcmp(name, "maps") == 0)
829 			obj->efile.maps_shndx = idx;
830 		else if (strcmp(name, BTF_ELF_SEC) == 0) {
831 			obj->btf = btf__new(data->d_buf, data->d_size,
832 					    __pr_debug);
833 			if (IS_ERR(obj->btf)) {
834 				pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
835 					   BTF_ELF_SEC, PTR_ERR(obj->btf));
836 				obj->btf = NULL;
837 			}
838 		} else if (sh.sh_type == SHT_SYMTAB) {
839 			if (obj->efile.symbols) {
840 				pr_warning("bpf: multiple SYMTAB in %s\n",
841 					   obj->path);
842 				err = -LIBBPF_ERRNO__FORMAT;
843 			} else {
844 				obj->efile.symbols = data;
845 				obj->efile.strtabidx = sh.sh_link;
846 			}
847 		} else if ((sh.sh_type == SHT_PROGBITS) &&
848 			   (sh.sh_flags & SHF_EXECINSTR) &&
849 			   (data->d_size > 0)) {
850 			if (strcmp(name, ".text") == 0)
851 				obj->efile.text_shndx = idx;
852 			err = bpf_object__add_program(obj, data->d_buf,
853 						      data->d_size, name, idx);
854 			if (err) {
855 				char errmsg[STRERR_BUFSIZE];
856 
857 				strerror_r(-err, errmsg, sizeof(errmsg));
858 				pr_warning("failed to alloc program %s (%s): %s",
859 					   name, obj->path, errmsg);
860 			}
861 		} else if (sh.sh_type == SHT_REL) {
862 			void *reloc = obj->efile.reloc;
863 			int nr_reloc = obj->efile.nr_reloc + 1;
864 			int sec = sh.sh_info; /* points to other section */
865 
866 			/* Only do relo for section with exec instructions */
867 			if (!section_have_execinstr(obj, sec)) {
868 				pr_debug("skip relo %s(%d) for section(%d)\n",
869 					 name, idx, sec);
870 				continue;
871 			}
872 
873 			reloc = realloc(reloc,
874 					sizeof(*obj->efile.reloc) * nr_reloc);
875 			if (!reloc) {
876 				pr_warning("realloc failed\n");
877 				err = -ENOMEM;
878 			} else {
879 				int n = nr_reloc - 1;
880 
881 				obj->efile.reloc = reloc;
882 				obj->efile.nr_reloc = nr_reloc;
883 
884 				obj->efile.reloc[n].shdr = sh;
885 				obj->efile.reloc[n].data = data;
886 			}
887 		} else {
888 			pr_debug("skip section(%d) %s\n", idx, name);
889 		}
890 		if (err)
891 			goto out;
892 	}
893 
894 	if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
895 		pr_warning("Corrupted ELF file: index of strtab invalid\n");
896 		return LIBBPF_ERRNO__FORMAT;
897 	}
898 	if (obj->efile.maps_shndx >= 0) {
899 		err = bpf_object__init_maps(obj);
900 		if (err)
901 			goto out;
902 	}
903 	err = bpf_object__init_prog_names(obj);
904 out:
905 	return err;
906 }
907 
908 static struct bpf_program *
909 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
910 {
911 	struct bpf_program *prog;
912 	size_t i;
913 
914 	for (i = 0; i < obj->nr_programs; i++) {
915 		prog = &obj->programs[i];
916 		if (prog->idx == idx)
917 			return prog;
918 	}
919 	return NULL;
920 }
921 
922 static int
923 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
924 			   Elf_Data *data, struct bpf_object *obj)
925 {
926 	Elf_Data *symbols = obj->efile.symbols;
927 	int text_shndx = obj->efile.text_shndx;
928 	int maps_shndx = obj->efile.maps_shndx;
929 	struct bpf_map *maps = obj->maps;
930 	size_t nr_maps = obj->nr_maps;
931 	int i, nrels;
932 
933 	pr_debug("collecting relocating info for: '%s'\n",
934 		 prog->section_name);
935 	nrels = shdr->sh_size / shdr->sh_entsize;
936 
937 	prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
938 	if (!prog->reloc_desc) {
939 		pr_warning("failed to alloc memory in relocation\n");
940 		return -ENOMEM;
941 	}
942 	prog->nr_reloc = nrels;
943 
944 	for (i = 0; i < nrels; i++) {
945 		GElf_Sym sym;
946 		GElf_Rel rel;
947 		unsigned int insn_idx;
948 		struct bpf_insn *insns = prog->insns;
949 		size_t map_idx;
950 
951 		if (!gelf_getrel(data, i, &rel)) {
952 			pr_warning("relocation: failed to get %d reloc\n", i);
953 			return -LIBBPF_ERRNO__FORMAT;
954 		}
955 
956 		if (!gelf_getsym(symbols,
957 				 GELF_R_SYM(rel.r_info),
958 				 &sym)) {
959 			pr_warning("relocation: symbol %"PRIx64" not found\n",
960 				   GELF_R_SYM(rel.r_info));
961 			return -LIBBPF_ERRNO__FORMAT;
962 		}
963 		pr_debug("relo for %lld value %lld name %d\n",
964 			 (long long) (rel.r_info >> 32),
965 			 (long long) sym.st_value, sym.st_name);
966 
967 		if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
968 			pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
969 				   prog->section_name, sym.st_shndx);
970 			return -LIBBPF_ERRNO__RELOC;
971 		}
972 
973 		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
974 		pr_debug("relocation: insn_idx=%u\n", insn_idx);
975 
976 		if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
977 			if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
978 				pr_warning("incorrect bpf_call opcode\n");
979 				return -LIBBPF_ERRNO__RELOC;
980 			}
981 			prog->reloc_desc[i].type = RELO_CALL;
982 			prog->reloc_desc[i].insn_idx = insn_idx;
983 			prog->reloc_desc[i].text_off = sym.st_value;
984 			obj->has_pseudo_calls = true;
985 			continue;
986 		}
987 
988 		if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
989 			pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
990 				   insn_idx, insns[insn_idx].code);
991 			return -LIBBPF_ERRNO__RELOC;
992 		}
993 
994 		/* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
995 		for (map_idx = 0; map_idx < nr_maps; map_idx++) {
996 			if (maps[map_idx].offset == sym.st_value) {
997 				pr_debug("relocation: find map %zd (%s) for insn %u\n",
998 					 map_idx, maps[map_idx].name, insn_idx);
999 				break;
1000 			}
1001 		}
1002 
1003 		if (map_idx >= nr_maps) {
1004 			pr_warning("bpf relocation: map_idx %d large than %d\n",
1005 				   (int)map_idx, (int)nr_maps - 1);
1006 			return -LIBBPF_ERRNO__RELOC;
1007 		}
1008 
1009 		prog->reloc_desc[i].type = RELO_LD64;
1010 		prog->reloc_desc[i].insn_idx = insn_idx;
1011 		prog->reloc_desc[i].map_idx = map_idx;
1012 	}
1013 	return 0;
1014 }
1015 
1016 static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
1017 {
1018 	struct bpf_map_def *def = &map->def;
1019 	const size_t max_name = 256;
1020 	int64_t key_size, value_size;
1021 	int32_t key_id, value_id;
1022 	char name[max_name];
1023 
1024 	/* Find key type by name from BTF */
1025 	if (snprintf(name, max_name, "%s_key", map->name) == max_name) {
1026 		pr_warning("map:%s length of BTF key_type:%s_key is too long\n",
1027 			   map->name, map->name);
1028 		return -EINVAL;
1029 	}
1030 
1031 	key_id = btf__find_by_name(btf, name);
1032 	if (key_id < 0) {
1033 		pr_debug("map:%s key_type:%s cannot be found in BTF\n",
1034 			 map->name, name);
1035 		return key_id;
1036 	}
1037 
1038 	key_size = btf__resolve_size(btf, key_id);
1039 	if (key_size < 0) {
1040 		pr_warning("map:%s key_type:%s cannot get the BTF type_size\n",
1041 			   map->name, name);
1042 		return key_size;
1043 	}
1044 
1045 	if (def->key_size != key_size) {
1046 		pr_warning("map:%s key_type:%s has BTF type_size:%u != key_size:%u\n",
1047 			   map->name, name, (unsigned int)key_size, def->key_size);
1048 		return -EINVAL;
1049 	}
1050 
1051 	/* Find value type from BTF */
1052 	if (snprintf(name, max_name, "%s_value", map->name) == max_name) {
1053 		pr_warning("map:%s length of BTF value_type:%s_value is too long\n",
1054 			  map->name, map->name);
1055 		return -EINVAL;
1056 	}
1057 
1058 	value_id = btf__find_by_name(btf, name);
1059 	if (value_id < 0) {
1060 		pr_debug("map:%s value_type:%s cannot be found in BTF\n",
1061 			 map->name, name);
1062 		return value_id;
1063 	}
1064 
1065 	value_size = btf__resolve_size(btf, value_id);
1066 	if (value_size < 0) {
1067 		pr_warning("map:%s value_type:%s cannot get the BTF type_size\n",
1068 			   map->name, name);
1069 		return value_size;
1070 	}
1071 
1072 	if (def->value_size != value_size) {
1073 		pr_warning("map:%s value_type:%s has BTF type_size:%u != value_size:%u\n",
1074 			   map->name, name, (unsigned int)value_size, def->value_size);
1075 		return -EINVAL;
1076 	}
1077 
1078 	map->btf_key_type_id = key_id;
1079 	map->btf_value_type_id = value_id;
1080 
1081 	return 0;
1082 }
1083 
1084 static int
1085 bpf_object__create_maps(struct bpf_object *obj)
1086 {
1087 	struct bpf_create_map_attr create_attr = {};
1088 	unsigned int i;
1089 	int err;
1090 
1091 	for (i = 0; i < obj->nr_maps; i++) {
1092 		struct bpf_map *map = &obj->maps[i];
1093 		struct bpf_map_def *def = &map->def;
1094 		int *pfd = &map->fd;
1095 
1096 		create_attr.name = map->name;
1097 		create_attr.map_ifindex = map->map_ifindex;
1098 		create_attr.map_type = def->type;
1099 		create_attr.map_flags = def->map_flags;
1100 		create_attr.key_size = def->key_size;
1101 		create_attr.value_size = def->value_size;
1102 		create_attr.max_entries = def->max_entries;
1103 		create_attr.btf_fd = 0;
1104 		create_attr.btf_key_type_id = 0;
1105 		create_attr.btf_value_type_id = 0;
1106 
1107 		if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1108 			create_attr.btf_fd = btf__fd(obj->btf);
1109 			create_attr.btf_key_type_id = map->btf_key_type_id;
1110 			create_attr.btf_value_type_id = map->btf_value_type_id;
1111 		}
1112 
1113 		*pfd = bpf_create_map_xattr(&create_attr);
1114 		if (*pfd < 0 && create_attr.btf_key_type_id) {
1115 			pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1116 				   map->name, strerror(errno), errno);
1117 			create_attr.btf_fd = 0;
1118 			create_attr.btf_key_type_id = 0;
1119 			create_attr.btf_value_type_id = 0;
1120 			map->btf_key_type_id = 0;
1121 			map->btf_value_type_id = 0;
1122 			*pfd = bpf_create_map_xattr(&create_attr);
1123 		}
1124 
1125 		if (*pfd < 0) {
1126 			size_t j;
1127 
1128 			err = *pfd;
1129 			pr_warning("failed to create map (name: '%s'): %s\n",
1130 				   map->name,
1131 				   strerror(errno));
1132 			for (j = 0; j < i; j++)
1133 				zclose(obj->maps[j].fd);
1134 			return err;
1135 		}
1136 		pr_debug("create map %s: fd=%d\n", map->name, *pfd);
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 static int
1143 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1144 			struct reloc_desc *relo)
1145 {
1146 	struct bpf_insn *insn, *new_insn;
1147 	struct bpf_program *text;
1148 	size_t new_cnt;
1149 
1150 	if (relo->type != RELO_CALL)
1151 		return -LIBBPF_ERRNO__RELOC;
1152 
1153 	if (prog->idx == obj->efile.text_shndx) {
1154 		pr_warning("relo in .text insn %d into off %d\n",
1155 			   relo->insn_idx, relo->text_off);
1156 		return -LIBBPF_ERRNO__RELOC;
1157 	}
1158 
1159 	if (prog->main_prog_cnt == 0) {
1160 		text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1161 		if (!text) {
1162 			pr_warning("no .text section found yet relo into text exist\n");
1163 			return -LIBBPF_ERRNO__RELOC;
1164 		}
1165 		new_cnt = prog->insns_cnt + text->insns_cnt;
1166 		new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
1167 		if (!new_insn) {
1168 			pr_warning("oom in prog realloc\n");
1169 			return -ENOMEM;
1170 		}
1171 		memcpy(new_insn + prog->insns_cnt, text->insns,
1172 		       text->insns_cnt * sizeof(*insn));
1173 		prog->insns = new_insn;
1174 		prog->main_prog_cnt = prog->insns_cnt;
1175 		prog->insns_cnt = new_cnt;
1176 		pr_debug("added %zd insn from %s to prog %s\n",
1177 			 text->insns_cnt, text->section_name,
1178 			 prog->section_name);
1179 	}
1180 	insn = &prog->insns[relo->insn_idx];
1181 	insn->imm += prog->main_prog_cnt - relo->insn_idx;
1182 	return 0;
1183 }
1184 
1185 static int
1186 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
1187 {
1188 	int i, err;
1189 
1190 	if (!prog || !prog->reloc_desc)
1191 		return 0;
1192 
1193 	for (i = 0; i < prog->nr_reloc; i++) {
1194 		if (prog->reloc_desc[i].type == RELO_LD64) {
1195 			struct bpf_insn *insns = prog->insns;
1196 			int insn_idx, map_idx;
1197 
1198 			insn_idx = prog->reloc_desc[i].insn_idx;
1199 			map_idx = prog->reloc_desc[i].map_idx;
1200 
1201 			if (insn_idx >= (int)prog->insns_cnt) {
1202 				pr_warning("relocation out of range: '%s'\n",
1203 					   prog->section_name);
1204 				return -LIBBPF_ERRNO__RELOC;
1205 			}
1206 			insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1207 			insns[insn_idx].imm = obj->maps[map_idx].fd;
1208 		} else {
1209 			err = bpf_program__reloc_text(prog, obj,
1210 						      &prog->reloc_desc[i]);
1211 			if (err)
1212 				return err;
1213 		}
1214 	}
1215 
1216 	zfree(&prog->reloc_desc);
1217 	prog->nr_reloc = 0;
1218 	return 0;
1219 }
1220 
1221 
1222 static int
1223 bpf_object__relocate(struct bpf_object *obj)
1224 {
1225 	struct bpf_program *prog;
1226 	size_t i;
1227 	int err;
1228 
1229 	for (i = 0; i < obj->nr_programs; i++) {
1230 		prog = &obj->programs[i];
1231 
1232 		err = bpf_program__relocate(prog, obj);
1233 		if (err) {
1234 			pr_warning("failed to relocate '%s'\n",
1235 				   prog->section_name);
1236 			return err;
1237 		}
1238 	}
1239 	return 0;
1240 }
1241 
1242 static int bpf_object__collect_reloc(struct bpf_object *obj)
1243 {
1244 	int i, err;
1245 
1246 	if (!obj_elf_valid(obj)) {
1247 		pr_warning("Internal error: elf object is closed\n");
1248 		return -LIBBPF_ERRNO__INTERNAL;
1249 	}
1250 
1251 	for (i = 0; i < obj->efile.nr_reloc; i++) {
1252 		GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1253 		Elf_Data *data = obj->efile.reloc[i].data;
1254 		int idx = shdr->sh_info;
1255 		struct bpf_program *prog;
1256 
1257 		if (shdr->sh_type != SHT_REL) {
1258 			pr_warning("internal error at %d\n", __LINE__);
1259 			return -LIBBPF_ERRNO__INTERNAL;
1260 		}
1261 
1262 		prog = bpf_object__find_prog_by_idx(obj, idx);
1263 		if (!prog) {
1264 			pr_warning("relocation failed: no section(%d)\n", idx);
1265 			return -LIBBPF_ERRNO__RELOC;
1266 		}
1267 
1268 		err = bpf_program__collect_reloc(prog,
1269 						 shdr, data,
1270 						 obj);
1271 		if (err)
1272 			return err;
1273 	}
1274 	return 0;
1275 }
1276 
1277 static int
1278 load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
1279 	     const char *name, struct bpf_insn *insns, int insns_cnt,
1280 	     char *license, u32 kern_version, int *pfd, int prog_ifindex)
1281 {
1282 	struct bpf_load_program_attr load_attr;
1283 	char *log_buf;
1284 	int ret;
1285 
1286 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
1287 	load_attr.prog_type = type;
1288 	load_attr.expected_attach_type = expected_attach_type;
1289 	load_attr.name = name;
1290 	load_attr.insns = insns;
1291 	load_attr.insns_cnt = insns_cnt;
1292 	load_attr.license = license;
1293 	load_attr.kern_version = kern_version;
1294 	load_attr.prog_ifindex = prog_ifindex;
1295 
1296 	if (!load_attr.insns || !load_attr.insns_cnt)
1297 		return -EINVAL;
1298 
1299 	log_buf = malloc(BPF_LOG_BUF_SIZE);
1300 	if (!log_buf)
1301 		pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1302 
1303 	ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
1304 
1305 	if (ret >= 0) {
1306 		*pfd = ret;
1307 		ret = 0;
1308 		goto out;
1309 	}
1310 
1311 	ret = -LIBBPF_ERRNO__LOAD;
1312 	pr_warning("load bpf program failed: %s\n", strerror(errno));
1313 
1314 	if (log_buf && log_buf[0] != '\0') {
1315 		ret = -LIBBPF_ERRNO__VERIFY;
1316 		pr_warning("-- BEGIN DUMP LOG ---\n");
1317 		pr_warning("\n%s\n", log_buf);
1318 		pr_warning("-- END LOG --\n");
1319 	} else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
1320 		pr_warning("Program too large (%zu insns), at most %d insns\n",
1321 			   load_attr.insns_cnt, BPF_MAXINSNS);
1322 		ret = -LIBBPF_ERRNO__PROG2BIG;
1323 	} else {
1324 		/* Wrong program type? */
1325 		if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
1326 			int fd;
1327 
1328 			load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
1329 			load_attr.expected_attach_type = 0;
1330 			fd = bpf_load_program_xattr(&load_attr, NULL, 0);
1331 			if (fd >= 0) {
1332 				close(fd);
1333 				ret = -LIBBPF_ERRNO__PROGTYPE;
1334 				goto out;
1335 			}
1336 		}
1337 
1338 		if (log_buf)
1339 			ret = -LIBBPF_ERRNO__KVER;
1340 	}
1341 
1342 out:
1343 	free(log_buf);
1344 	return ret;
1345 }
1346 
1347 static int
1348 bpf_program__load(struct bpf_program *prog,
1349 		  char *license, u32 kern_version)
1350 {
1351 	int err = 0, fd, i;
1352 
1353 	if (prog->instances.nr < 0 || !prog->instances.fds) {
1354 		if (prog->preprocessor) {
1355 			pr_warning("Internal error: can't load program '%s'\n",
1356 				   prog->section_name);
1357 			return -LIBBPF_ERRNO__INTERNAL;
1358 		}
1359 
1360 		prog->instances.fds = malloc(sizeof(int));
1361 		if (!prog->instances.fds) {
1362 			pr_warning("Not enough memory for BPF fds\n");
1363 			return -ENOMEM;
1364 		}
1365 		prog->instances.nr = 1;
1366 		prog->instances.fds[0] = -1;
1367 	}
1368 
1369 	if (!prog->preprocessor) {
1370 		if (prog->instances.nr != 1) {
1371 			pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1372 				   prog->section_name, prog->instances.nr);
1373 		}
1374 		err = load_program(prog->type, prog->expected_attach_type,
1375 				   prog->name, prog->insns, prog->insns_cnt,
1376 				   license, kern_version, &fd,
1377 				   prog->prog_ifindex);
1378 		if (!err)
1379 			prog->instances.fds[0] = fd;
1380 		goto out;
1381 	}
1382 
1383 	for (i = 0; i < prog->instances.nr; i++) {
1384 		struct bpf_prog_prep_result result;
1385 		bpf_program_prep_t preprocessor = prog->preprocessor;
1386 
1387 		bzero(&result, sizeof(result));
1388 		err = preprocessor(prog, i, prog->insns,
1389 				   prog->insns_cnt, &result);
1390 		if (err) {
1391 			pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1392 				   i, prog->section_name);
1393 			goto out;
1394 		}
1395 
1396 		if (!result.new_insn_ptr || !result.new_insn_cnt) {
1397 			pr_debug("Skip loading the %dth instance of program '%s'\n",
1398 				 i, prog->section_name);
1399 			prog->instances.fds[i] = -1;
1400 			if (result.pfd)
1401 				*result.pfd = -1;
1402 			continue;
1403 		}
1404 
1405 		err = load_program(prog->type, prog->expected_attach_type,
1406 				   prog->name, result.new_insn_ptr,
1407 				   result.new_insn_cnt,
1408 				   license, kern_version, &fd,
1409 				   prog->prog_ifindex);
1410 
1411 		if (err) {
1412 			pr_warning("Loading the %dth instance of program '%s' failed\n",
1413 					i, prog->section_name);
1414 			goto out;
1415 		}
1416 
1417 		if (result.pfd)
1418 			*result.pfd = fd;
1419 		prog->instances.fds[i] = fd;
1420 	}
1421 out:
1422 	if (err)
1423 		pr_warning("failed to load program '%s'\n",
1424 			   prog->section_name);
1425 	zfree(&prog->insns);
1426 	prog->insns_cnt = 0;
1427 	return err;
1428 }
1429 
1430 static bool bpf_program__is_function_storage(struct bpf_program *prog,
1431 					     struct bpf_object *obj)
1432 {
1433 	return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
1434 }
1435 
1436 static int
1437 bpf_object__load_progs(struct bpf_object *obj)
1438 {
1439 	size_t i;
1440 	int err;
1441 
1442 	for (i = 0; i < obj->nr_programs; i++) {
1443 		if (bpf_program__is_function_storage(&obj->programs[i], obj))
1444 			continue;
1445 		err = bpf_program__load(&obj->programs[i],
1446 					obj->license,
1447 					obj->kern_version);
1448 		if (err)
1449 			return err;
1450 	}
1451 	return 0;
1452 }
1453 
1454 static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
1455 {
1456 	switch (type) {
1457 	case BPF_PROG_TYPE_SOCKET_FILTER:
1458 	case BPF_PROG_TYPE_SCHED_CLS:
1459 	case BPF_PROG_TYPE_SCHED_ACT:
1460 	case BPF_PROG_TYPE_XDP:
1461 	case BPF_PROG_TYPE_CGROUP_SKB:
1462 	case BPF_PROG_TYPE_CGROUP_SOCK:
1463 	case BPF_PROG_TYPE_LWT_IN:
1464 	case BPF_PROG_TYPE_LWT_OUT:
1465 	case BPF_PROG_TYPE_LWT_XMIT:
1466 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
1467 	case BPF_PROG_TYPE_SOCK_OPS:
1468 	case BPF_PROG_TYPE_SK_SKB:
1469 	case BPF_PROG_TYPE_CGROUP_DEVICE:
1470 	case BPF_PROG_TYPE_SK_MSG:
1471 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1472 	case BPF_PROG_TYPE_LIRC_MODE2:
1473 		return false;
1474 	case BPF_PROG_TYPE_UNSPEC:
1475 	case BPF_PROG_TYPE_KPROBE:
1476 	case BPF_PROG_TYPE_TRACEPOINT:
1477 	case BPF_PROG_TYPE_PERF_EVENT:
1478 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
1479 	default:
1480 		return true;
1481 	}
1482 }
1483 
1484 static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
1485 {
1486 	if (needs_kver && obj->kern_version == 0) {
1487 		pr_warning("%s doesn't provide kernel version\n",
1488 			   obj->path);
1489 		return -LIBBPF_ERRNO__KVERSION;
1490 	}
1491 	return 0;
1492 }
1493 
1494 static struct bpf_object *
1495 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1496 		   bool needs_kver)
1497 {
1498 	struct bpf_object *obj;
1499 	int err;
1500 
1501 	if (elf_version(EV_CURRENT) == EV_NONE) {
1502 		pr_warning("failed to init libelf for %s\n", path);
1503 		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1504 	}
1505 
1506 	obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1507 	if (IS_ERR(obj))
1508 		return obj;
1509 
1510 	CHECK_ERR(bpf_object__elf_init(obj), err, out);
1511 	CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1512 	CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1513 	CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1514 	CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
1515 
1516 	bpf_object__elf_finish(obj);
1517 	return obj;
1518 out:
1519 	bpf_object__close(obj);
1520 	return ERR_PTR(err);
1521 }
1522 
1523 struct bpf_object *bpf_object__open(const char *path)
1524 {
1525 	/* param validation */
1526 	if (!path)
1527 		return NULL;
1528 
1529 	pr_debug("loading %s\n", path);
1530 
1531 	return __bpf_object__open(path, NULL, 0, true);
1532 }
1533 
1534 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1535 					   size_t obj_buf_sz,
1536 					   const char *name)
1537 {
1538 	char tmp_name[64];
1539 
1540 	/* param validation */
1541 	if (!obj_buf || obj_buf_sz <= 0)
1542 		return NULL;
1543 
1544 	if (!name) {
1545 		snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1546 			 (unsigned long)obj_buf,
1547 			 (unsigned long)obj_buf_sz);
1548 		tmp_name[sizeof(tmp_name) - 1] = '\0';
1549 		name = tmp_name;
1550 	}
1551 	pr_debug("loading object '%s' from buffer\n",
1552 		 name);
1553 
1554 	return __bpf_object__open(name, obj_buf, obj_buf_sz, true);
1555 }
1556 
1557 int bpf_object__unload(struct bpf_object *obj)
1558 {
1559 	size_t i;
1560 
1561 	if (!obj)
1562 		return -EINVAL;
1563 
1564 	for (i = 0; i < obj->nr_maps; i++)
1565 		zclose(obj->maps[i].fd);
1566 
1567 	for (i = 0; i < obj->nr_programs; i++)
1568 		bpf_program__unload(&obj->programs[i]);
1569 
1570 	return 0;
1571 }
1572 
1573 int bpf_object__load(struct bpf_object *obj)
1574 {
1575 	int err;
1576 
1577 	if (!obj)
1578 		return -EINVAL;
1579 
1580 	if (obj->loaded) {
1581 		pr_warning("object should not be loaded twice\n");
1582 		return -EINVAL;
1583 	}
1584 
1585 	obj->loaded = true;
1586 
1587 	CHECK_ERR(bpf_object__create_maps(obj), err, out);
1588 	CHECK_ERR(bpf_object__relocate(obj), err, out);
1589 	CHECK_ERR(bpf_object__load_progs(obj), err, out);
1590 
1591 	return 0;
1592 out:
1593 	bpf_object__unload(obj);
1594 	pr_warning("failed to load object '%s'\n", obj->path);
1595 	return err;
1596 }
1597 
1598 static int check_path(const char *path)
1599 {
1600 	struct statfs st_fs;
1601 	char *dname, *dir;
1602 	int err = 0;
1603 
1604 	if (path == NULL)
1605 		return -EINVAL;
1606 
1607 	dname = strdup(path);
1608 	if (dname == NULL)
1609 		return -ENOMEM;
1610 
1611 	dir = dirname(dname);
1612 	if (statfs(dir, &st_fs)) {
1613 		pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1614 		err = -errno;
1615 	}
1616 	free(dname);
1617 
1618 	if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1619 		pr_warning("specified path %s is not on BPF FS\n", path);
1620 		err = -EINVAL;
1621 	}
1622 
1623 	return err;
1624 }
1625 
1626 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1627 			      int instance)
1628 {
1629 	int err;
1630 
1631 	err = check_path(path);
1632 	if (err)
1633 		return err;
1634 
1635 	if (prog == NULL) {
1636 		pr_warning("invalid program pointer\n");
1637 		return -EINVAL;
1638 	}
1639 
1640 	if (instance < 0 || instance >= prog->instances.nr) {
1641 		pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1642 			   instance, prog->section_name, prog->instances.nr);
1643 		return -EINVAL;
1644 	}
1645 
1646 	if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1647 		pr_warning("failed to pin program: %s\n", strerror(errno));
1648 		return -errno;
1649 	}
1650 	pr_debug("pinned program '%s'\n", path);
1651 
1652 	return 0;
1653 }
1654 
1655 static int make_dir(const char *path)
1656 {
1657 	int err = 0;
1658 
1659 	if (mkdir(path, 0700) && errno != EEXIST)
1660 		err = -errno;
1661 
1662 	if (err)
1663 		pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1664 	return err;
1665 }
1666 
1667 int bpf_program__pin(struct bpf_program *prog, const char *path)
1668 {
1669 	int i, err;
1670 
1671 	err = check_path(path);
1672 	if (err)
1673 		return err;
1674 
1675 	if (prog == NULL) {
1676 		pr_warning("invalid program pointer\n");
1677 		return -EINVAL;
1678 	}
1679 
1680 	if (prog->instances.nr <= 0) {
1681 		pr_warning("no instances of prog %s to pin\n",
1682 			   prog->section_name);
1683 		return -EINVAL;
1684 	}
1685 
1686 	err = make_dir(path);
1687 	if (err)
1688 		return err;
1689 
1690 	for (i = 0; i < prog->instances.nr; i++) {
1691 		char buf[PATH_MAX];
1692 		int len;
1693 
1694 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1695 		if (len < 0)
1696 			return -EINVAL;
1697 		else if (len >= PATH_MAX)
1698 			return -ENAMETOOLONG;
1699 
1700 		err = bpf_program__pin_instance(prog, buf, i);
1701 		if (err)
1702 			return err;
1703 	}
1704 
1705 	return 0;
1706 }
1707 
1708 int bpf_map__pin(struct bpf_map *map, const char *path)
1709 {
1710 	int err;
1711 
1712 	err = check_path(path);
1713 	if (err)
1714 		return err;
1715 
1716 	if (map == NULL) {
1717 		pr_warning("invalid map pointer\n");
1718 		return -EINVAL;
1719 	}
1720 
1721 	if (bpf_obj_pin(map->fd, path)) {
1722 		pr_warning("failed to pin map: %s\n", strerror(errno));
1723 		return -errno;
1724 	}
1725 
1726 	pr_debug("pinned map '%s'\n", path);
1727 	return 0;
1728 }
1729 
1730 int bpf_object__pin(struct bpf_object *obj, const char *path)
1731 {
1732 	struct bpf_program *prog;
1733 	struct bpf_map *map;
1734 	int err;
1735 
1736 	if (!obj)
1737 		return -ENOENT;
1738 
1739 	if (!obj->loaded) {
1740 		pr_warning("object not yet loaded; load it first\n");
1741 		return -ENOENT;
1742 	}
1743 
1744 	err = make_dir(path);
1745 	if (err)
1746 		return err;
1747 
1748 	bpf_map__for_each(map, obj) {
1749 		char buf[PATH_MAX];
1750 		int len;
1751 
1752 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
1753 			       bpf_map__name(map));
1754 		if (len < 0)
1755 			return -EINVAL;
1756 		else if (len >= PATH_MAX)
1757 			return -ENAMETOOLONG;
1758 
1759 		err = bpf_map__pin(map, buf);
1760 		if (err)
1761 			return err;
1762 	}
1763 
1764 	bpf_object__for_each_program(prog, obj) {
1765 		char buf[PATH_MAX];
1766 		int len;
1767 
1768 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
1769 			       prog->section_name);
1770 		if (len < 0)
1771 			return -EINVAL;
1772 		else if (len >= PATH_MAX)
1773 			return -ENAMETOOLONG;
1774 
1775 		err = bpf_program__pin(prog, buf);
1776 		if (err)
1777 			return err;
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 void bpf_object__close(struct bpf_object *obj)
1784 {
1785 	size_t i;
1786 
1787 	if (!obj)
1788 		return;
1789 
1790 	if (obj->clear_priv)
1791 		obj->clear_priv(obj, obj->priv);
1792 
1793 	bpf_object__elf_finish(obj);
1794 	bpf_object__unload(obj);
1795 	btf__free(obj->btf);
1796 
1797 	for (i = 0; i < obj->nr_maps; i++) {
1798 		zfree(&obj->maps[i].name);
1799 		if (obj->maps[i].clear_priv)
1800 			obj->maps[i].clear_priv(&obj->maps[i],
1801 						obj->maps[i].priv);
1802 		obj->maps[i].priv = NULL;
1803 		obj->maps[i].clear_priv = NULL;
1804 	}
1805 	zfree(&obj->maps);
1806 	obj->nr_maps = 0;
1807 
1808 	if (obj->programs && obj->nr_programs) {
1809 		for (i = 0; i < obj->nr_programs; i++)
1810 			bpf_program__exit(&obj->programs[i]);
1811 	}
1812 	zfree(&obj->programs);
1813 
1814 	list_del(&obj->list);
1815 	free(obj);
1816 }
1817 
1818 struct bpf_object *
1819 bpf_object__next(struct bpf_object *prev)
1820 {
1821 	struct bpf_object *next;
1822 
1823 	if (!prev)
1824 		next = list_first_entry(&bpf_objects_list,
1825 					struct bpf_object,
1826 					list);
1827 	else
1828 		next = list_next_entry(prev, list);
1829 
1830 	/* Empty list is noticed here so don't need checking on entry. */
1831 	if (&next->list == &bpf_objects_list)
1832 		return NULL;
1833 
1834 	return next;
1835 }
1836 
1837 const char *bpf_object__name(struct bpf_object *obj)
1838 {
1839 	return obj ? obj->path : ERR_PTR(-EINVAL);
1840 }
1841 
1842 unsigned int bpf_object__kversion(struct bpf_object *obj)
1843 {
1844 	return obj ? obj->kern_version : 0;
1845 }
1846 
1847 int bpf_object__btf_fd(const struct bpf_object *obj)
1848 {
1849 	return obj->btf ? btf__fd(obj->btf) : -1;
1850 }
1851 
1852 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1853 			 bpf_object_clear_priv_t clear_priv)
1854 {
1855 	if (obj->priv && obj->clear_priv)
1856 		obj->clear_priv(obj, obj->priv);
1857 
1858 	obj->priv = priv;
1859 	obj->clear_priv = clear_priv;
1860 	return 0;
1861 }
1862 
1863 void *bpf_object__priv(struct bpf_object *obj)
1864 {
1865 	return obj ? obj->priv : ERR_PTR(-EINVAL);
1866 }
1867 
1868 static struct bpf_program *
1869 __bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1870 {
1871 	size_t idx;
1872 
1873 	if (!obj->programs)
1874 		return NULL;
1875 	/* First handler */
1876 	if (prev == NULL)
1877 		return &obj->programs[0];
1878 
1879 	if (prev->obj != obj) {
1880 		pr_warning("error: program handler doesn't match object\n");
1881 		return NULL;
1882 	}
1883 
1884 	idx = (prev - obj->programs) + 1;
1885 	if (idx >= obj->nr_programs)
1886 		return NULL;
1887 	return &obj->programs[idx];
1888 }
1889 
1890 struct bpf_program *
1891 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1892 {
1893 	struct bpf_program *prog = prev;
1894 
1895 	do {
1896 		prog = __bpf_program__next(prog, obj);
1897 	} while (prog && bpf_program__is_function_storage(prog, obj));
1898 
1899 	return prog;
1900 }
1901 
1902 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1903 			  bpf_program_clear_priv_t clear_priv)
1904 {
1905 	if (prog->priv && prog->clear_priv)
1906 		prog->clear_priv(prog, prog->priv);
1907 
1908 	prog->priv = priv;
1909 	prog->clear_priv = clear_priv;
1910 	return 0;
1911 }
1912 
1913 void *bpf_program__priv(struct bpf_program *prog)
1914 {
1915 	return prog ? prog->priv : ERR_PTR(-EINVAL);
1916 }
1917 
1918 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
1919 {
1920 	prog->prog_ifindex = ifindex;
1921 }
1922 
1923 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1924 {
1925 	const char *title;
1926 
1927 	title = prog->section_name;
1928 	if (needs_copy) {
1929 		title = strdup(title);
1930 		if (!title) {
1931 			pr_warning("failed to strdup program title\n");
1932 			return ERR_PTR(-ENOMEM);
1933 		}
1934 	}
1935 
1936 	return title;
1937 }
1938 
1939 int bpf_program__fd(struct bpf_program *prog)
1940 {
1941 	return bpf_program__nth_fd(prog, 0);
1942 }
1943 
1944 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1945 			  bpf_program_prep_t prep)
1946 {
1947 	int *instances_fds;
1948 
1949 	if (nr_instances <= 0 || !prep)
1950 		return -EINVAL;
1951 
1952 	if (prog->instances.nr > 0 || prog->instances.fds) {
1953 		pr_warning("Can't set pre-processor after loading\n");
1954 		return -EINVAL;
1955 	}
1956 
1957 	instances_fds = malloc(sizeof(int) * nr_instances);
1958 	if (!instances_fds) {
1959 		pr_warning("alloc memory failed for fds\n");
1960 		return -ENOMEM;
1961 	}
1962 
1963 	/* fill all fd with -1 */
1964 	memset(instances_fds, -1, sizeof(int) * nr_instances);
1965 
1966 	prog->instances.nr = nr_instances;
1967 	prog->instances.fds = instances_fds;
1968 	prog->preprocessor = prep;
1969 	return 0;
1970 }
1971 
1972 int bpf_program__nth_fd(struct bpf_program *prog, int n)
1973 {
1974 	int fd;
1975 
1976 	if (n >= prog->instances.nr || n < 0) {
1977 		pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1978 			   n, prog->section_name, prog->instances.nr);
1979 		return -EINVAL;
1980 	}
1981 
1982 	fd = prog->instances.fds[n];
1983 	if (fd < 0) {
1984 		pr_warning("%dth instance of program '%s' is invalid\n",
1985 			   n, prog->section_name);
1986 		return -ENOENT;
1987 	}
1988 
1989 	return fd;
1990 }
1991 
1992 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
1993 {
1994 	prog->type = type;
1995 }
1996 
1997 static bool bpf_program__is_type(struct bpf_program *prog,
1998 				 enum bpf_prog_type type)
1999 {
2000 	return prog ? (prog->type == type) : false;
2001 }
2002 
2003 #define BPF_PROG_TYPE_FNS(NAME, TYPE)			\
2004 int bpf_program__set_##NAME(struct bpf_program *prog)	\
2005 {							\
2006 	if (!prog)					\
2007 		return -EINVAL;				\
2008 	bpf_program__set_type(prog, TYPE);		\
2009 	return 0;					\
2010 }							\
2011 							\
2012 bool bpf_program__is_##NAME(struct bpf_program *prog)	\
2013 {							\
2014 	return bpf_program__is_type(prog, TYPE);	\
2015 }							\
2016 
2017 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
2018 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
2019 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
2020 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
2021 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
2022 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
2023 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
2024 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
2025 
2026 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
2027 					   enum bpf_attach_type type)
2028 {
2029 	prog->expected_attach_type = type;
2030 }
2031 
2032 #define BPF_PROG_SEC_FULL(string, ptype, atype) \
2033 	{ string, sizeof(string) - 1, ptype, atype }
2034 
2035 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
2036 
2037 #define BPF_S_PROG_SEC(string, ptype) \
2038 	BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype)
2039 
2040 #define BPF_SA_PROG_SEC(string, ptype) \
2041 	BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
2042 
2043 static const struct {
2044 	const char *sec;
2045 	size_t len;
2046 	enum bpf_prog_type prog_type;
2047 	enum bpf_attach_type expected_attach_type;
2048 } section_names[] = {
2049 	BPF_PROG_SEC("socket",		BPF_PROG_TYPE_SOCKET_FILTER),
2050 	BPF_PROG_SEC("kprobe/",		BPF_PROG_TYPE_KPROBE),
2051 	BPF_PROG_SEC("kretprobe/",	BPF_PROG_TYPE_KPROBE),
2052 	BPF_PROG_SEC("classifier",	BPF_PROG_TYPE_SCHED_CLS),
2053 	BPF_PROG_SEC("action",		BPF_PROG_TYPE_SCHED_ACT),
2054 	BPF_PROG_SEC("tracepoint/",	BPF_PROG_TYPE_TRACEPOINT),
2055 	BPF_PROG_SEC("raw_tracepoint/",	BPF_PROG_TYPE_RAW_TRACEPOINT),
2056 	BPF_PROG_SEC("xdp",		BPF_PROG_TYPE_XDP),
2057 	BPF_PROG_SEC("perf_event",	BPF_PROG_TYPE_PERF_EVENT),
2058 	BPF_PROG_SEC("cgroup/skb",	BPF_PROG_TYPE_CGROUP_SKB),
2059 	BPF_PROG_SEC("cgroup/sock",	BPF_PROG_TYPE_CGROUP_SOCK),
2060 	BPF_PROG_SEC("cgroup/dev",	BPF_PROG_TYPE_CGROUP_DEVICE),
2061 	BPF_PROG_SEC("lwt_in",		BPF_PROG_TYPE_LWT_IN),
2062 	BPF_PROG_SEC("lwt_out",		BPF_PROG_TYPE_LWT_OUT),
2063 	BPF_PROG_SEC("lwt_xmit",	BPF_PROG_TYPE_LWT_XMIT),
2064 	BPF_PROG_SEC("lwt_seg6local",	BPF_PROG_TYPE_LWT_SEG6LOCAL),
2065 	BPF_PROG_SEC("sockops",		BPF_PROG_TYPE_SOCK_OPS),
2066 	BPF_PROG_SEC("sk_skb",		BPF_PROG_TYPE_SK_SKB),
2067 	BPF_PROG_SEC("sk_msg",		BPF_PROG_TYPE_SK_MSG),
2068 	BPF_PROG_SEC("lirc_mode2",	BPF_PROG_TYPE_LIRC_MODE2),
2069 	BPF_SA_PROG_SEC("cgroup/bind4",	BPF_CGROUP_INET4_BIND),
2070 	BPF_SA_PROG_SEC("cgroup/bind6",	BPF_CGROUP_INET6_BIND),
2071 	BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
2072 	BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
2073 	BPF_SA_PROG_SEC("cgroup/sendmsg4", BPF_CGROUP_UDP4_SENDMSG),
2074 	BPF_SA_PROG_SEC("cgroup/sendmsg6", BPF_CGROUP_UDP6_SENDMSG),
2075 	BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND),
2076 	BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND),
2077 };
2078 
2079 #undef BPF_PROG_SEC
2080 #undef BPF_PROG_SEC_FULL
2081 #undef BPF_S_PROG_SEC
2082 #undef BPF_SA_PROG_SEC
2083 
2084 static int bpf_program__identify_section(struct bpf_program *prog)
2085 {
2086 	int i;
2087 
2088 	if (!prog->section_name)
2089 		goto err;
2090 
2091 	for (i = 0; i < ARRAY_SIZE(section_names); i++)
2092 		if (strncmp(prog->section_name, section_names[i].sec,
2093 			    section_names[i].len) == 0)
2094 			return i;
2095 
2096 err:
2097 	pr_warning("failed to guess program type based on section name %s\n",
2098 		   prog->section_name);
2099 
2100 	return -1;
2101 }
2102 
2103 int bpf_map__fd(struct bpf_map *map)
2104 {
2105 	return map ? map->fd : -EINVAL;
2106 }
2107 
2108 const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
2109 {
2110 	return map ? &map->def : ERR_PTR(-EINVAL);
2111 }
2112 
2113 const char *bpf_map__name(struct bpf_map *map)
2114 {
2115 	return map ? map->name : NULL;
2116 }
2117 
2118 uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map)
2119 {
2120 	return map ? map->btf_key_type_id : 0;
2121 }
2122 
2123 uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map)
2124 {
2125 	return map ? map->btf_value_type_id : 0;
2126 }
2127 
2128 int bpf_map__set_priv(struct bpf_map *map, void *priv,
2129 		     bpf_map_clear_priv_t clear_priv)
2130 {
2131 	if (!map)
2132 		return -EINVAL;
2133 
2134 	if (map->priv) {
2135 		if (map->clear_priv)
2136 			map->clear_priv(map, map->priv);
2137 	}
2138 
2139 	map->priv = priv;
2140 	map->clear_priv = clear_priv;
2141 	return 0;
2142 }
2143 
2144 void *bpf_map__priv(struct bpf_map *map)
2145 {
2146 	return map ? map->priv : ERR_PTR(-EINVAL);
2147 }
2148 
2149 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
2150 {
2151 	map->map_ifindex = ifindex;
2152 }
2153 
2154 struct bpf_map *
2155 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
2156 {
2157 	size_t idx;
2158 	struct bpf_map *s, *e;
2159 
2160 	if (!obj || !obj->maps)
2161 		return NULL;
2162 
2163 	s = obj->maps;
2164 	e = obj->maps + obj->nr_maps;
2165 
2166 	if (prev == NULL)
2167 		return s;
2168 
2169 	if ((prev < s) || (prev >= e)) {
2170 		pr_warning("error in %s: map handler doesn't belong to object\n",
2171 			   __func__);
2172 		return NULL;
2173 	}
2174 
2175 	idx = (prev - obj->maps) + 1;
2176 	if (idx >= obj->nr_maps)
2177 		return NULL;
2178 	return &obj->maps[idx];
2179 }
2180 
2181 struct bpf_map *
2182 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
2183 {
2184 	struct bpf_map *pos;
2185 
2186 	bpf_map__for_each(pos, obj) {
2187 		if (pos->name && !strcmp(pos->name, name))
2188 			return pos;
2189 	}
2190 	return NULL;
2191 }
2192 
2193 struct bpf_map *
2194 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
2195 {
2196 	int i;
2197 
2198 	for (i = 0; i < obj->nr_maps; i++) {
2199 		if (obj->maps[i].offset == offset)
2200 			return &obj->maps[i];
2201 	}
2202 	return ERR_PTR(-ENOENT);
2203 }
2204 
2205 long libbpf_get_error(const void *ptr)
2206 {
2207 	if (IS_ERR(ptr))
2208 		return PTR_ERR(ptr);
2209 	return 0;
2210 }
2211 
2212 int bpf_prog_load(const char *file, enum bpf_prog_type type,
2213 		  struct bpf_object **pobj, int *prog_fd)
2214 {
2215 	struct bpf_prog_load_attr attr;
2216 
2217 	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
2218 	attr.file = file;
2219 	attr.prog_type = type;
2220 	attr.expected_attach_type = 0;
2221 
2222 	return bpf_prog_load_xattr(&attr, pobj, prog_fd);
2223 }
2224 
2225 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2226 			struct bpf_object **pobj, int *prog_fd)
2227 {
2228 	struct bpf_program *prog, *first_prog = NULL;
2229 	enum bpf_attach_type expected_attach_type;
2230 	enum bpf_prog_type prog_type;
2231 	struct bpf_object *obj;
2232 	struct bpf_map *map;
2233 	int section_idx;
2234 	int err;
2235 
2236 	if (!attr)
2237 		return -EINVAL;
2238 	if (!attr->file)
2239 		return -EINVAL;
2240 
2241 	obj = __bpf_object__open(attr->file, NULL, 0,
2242 				 bpf_prog_type__needs_kver(attr->prog_type));
2243 	if (IS_ERR_OR_NULL(obj))
2244 		return -ENOENT;
2245 
2246 	bpf_object__for_each_program(prog, obj) {
2247 		/*
2248 		 * If type is not specified, try to guess it based on
2249 		 * section name.
2250 		 */
2251 		prog_type = attr->prog_type;
2252 		prog->prog_ifindex = attr->ifindex;
2253 		expected_attach_type = attr->expected_attach_type;
2254 		if (prog_type == BPF_PROG_TYPE_UNSPEC) {
2255 			section_idx = bpf_program__identify_section(prog);
2256 			if (section_idx < 0) {
2257 				bpf_object__close(obj);
2258 				return -EINVAL;
2259 			}
2260 			prog_type = section_names[section_idx].prog_type;
2261 			expected_attach_type =
2262 				section_names[section_idx].expected_attach_type;
2263 		}
2264 
2265 		bpf_program__set_type(prog, prog_type);
2266 		bpf_program__set_expected_attach_type(prog,
2267 						      expected_attach_type);
2268 
2269 		if (!bpf_program__is_function_storage(prog, obj) && !first_prog)
2270 			first_prog = prog;
2271 	}
2272 
2273 	bpf_map__for_each(map, obj) {
2274 		map->map_ifindex = attr->ifindex;
2275 	}
2276 
2277 	if (!first_prog) {
2278 		pr_warning("object file doesn't contain bpf program\n");
2279 		bpf_object__close(obj);
2280 		return -ENOENT;
2281 	}
2282 
2283 	err = bpf_object__load(obj);
2284 	if (err) {
2285 		bpf_object__close(obj);
2286 		return -EINVAL;
2287 	}
2288 
2289 	*pobj = obj;
2290 	*prog_fd = bpf_program__fd(first_prog);
2291 	return 0;
2292 }
2293 
2294 enum bpf_perf_event_ret
2295 bpf_perf_event_read_simple(void *mem, unsigned long size,
2296 			   unsigned long page_size, void **buf, size_t *buf_len,
2297 			   bpf_perf_event_print_t fn, void *priv)
2298 {
2299 	volatile struct perf_event_mmap_page *header = mem;
2300 	__u64 data_tail = header->data_tail;
2301 	__u64 data_head = header->data_head;
2302 	void *base, *begin, *end;
2303 	int ret;
2304 
2305 	asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
2306 	if (data_head == data_tail)
2307 		return LIBBPF_PERF_EVENT_CONT;
2308 
2309 	base = ((char *)header) + page_size;
2310 
2311 	begin = base + data_tail % size;
2312 	end = base + data_head % size;
2313 
2314 	while (begin != end) {
2315 		struct perf_event_header *ehdr;
2316 
2317 		ehdr = begin;
2318 		if (begin + ehdr->size > base + size) {
2319 			long len = base + size - begin;
2320 
2321 			if (*buf_len < ehdr->size) {
2322 				free(*buf);
2323 				*buf = malloc(ehdr->size);
2324 				if (!*buf) {
2325 					ret = LIBBPF_PERF_EVENT_ERROR;
2326 					break;
2327 				}
2328 				*buf_len = ehdr->size;
2329 			}
2330 
2331 			memcpy(*buf, begin, len);
2332 			memcpy(*buf + len, base, ehdr->size - len);
2333 			ehdr = (void *)*buf;
2334 			begin = base + ehdr->size - len;
2335 		} else if (begin + ehdr->size == base + size) {
2336 			begin = base;
2337 		} else {
2338 			begin += ehdr->size;
2339 		}
2340 
2341 		ret = fn(ehdr, priv);
2342 		if (ret != LIBBPF_PERF_EVENT_CONT)
2343 			break;
2344 
2345 		data_tail += ehdr->size;
2346 	}
2347 
2348 	__sync_synchronize(); /* smp_mb() */
2349 	header->data_tail = data_tail;
2350 
2351 	return ret;
2352 }
2353