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