xref: /linux/tools/lib/bpf/libbpf.c (revision 246880958ac93989c97c73ae1e60b78b4c4c88c5)
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  * Copyright (C) 2019 Isovalent, Inc.
11  */
12 
13 #ifndef _GNU_SOURCE
14 #define _GNU_SOURCE
15 #endif
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <libgen.h>
20 #include <inttypes.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <endian.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <asm/unistd.h>
27 #include <linux/err.h>
28 #include <linux/kernel.h>
29 #include <linux/bpf.h>
30 #include <linux/btf.h>
31 #include <linux/filter.h>
32 #include <linux/list.h>
33 #include <linux/limits.h>
34 #include <linux/perf_event.h>
35 #include <linux/ring_buffer.h>
36 #include <linux/version.h>
37 #include <sys/epoll.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/vfs.h>
43 #include <sys/utsname.h>
44 #include <tools/libc_compat.h>
45 #include <libelf.h>
46 #include <gelf.h>
47 
48 #include "libbpf.h"
49 #include "bpf.h"
50 #include "btf.h"
51 #include "str_error.h"
52 #include "libbpf_internal.h"
53 #include "hashmap.h"
54 
55 #ifndef EM_BPF
56 #define EM_BPF 247
57 #endif
58 
59 #ifndef BPF_FS_MAGIC
60 #define BPF_FS_MAGIC		0xcafe4a11
61 #endif
62 
63 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
64  * compilation if user enables corresponding warning. Disable it explicitly.
65  */
66 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
67 
68 #define __printf(a, b)	__attribute__((format(printf, a, b)))
69 
70 static int __base_pr(enum libbpf_print_level level, const char *format,
71 		     va_list args)
72 {
73 	if (level == LIBBPF_DEBUG)
74 		return 0;
75 
76 	return vfprintf(stderr, format, args);
77 }
78 
79 static libbpf_print_fn_t __libbpf_pr = __base_pr;
80 
81 libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
82 {
83 	libbpf_print_fn_t old_print_fn = __libbpf_pr;
84 
85 	__libbpf_pr = fn;
86 	return old_print_fn;
87 }
88 
89 __printf(2, 3)
90 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
91 {
92 	va_list args;
93 
94 	if (!__libbpf_pr)
95 		return;
96 
97 	va_start(args, format);
98 	__libbpf_pr(level, format, args);
99 	va_end(args);
100 }
101 
102 #define STRERR_BUFSIZE  128
103 
104 #define CHECK_ERR(action, err, out) do {	\
105 	err = action;			\
106 	if (err)			\
107 		goto out;		\
108 } while(0)
109 
110 
111 /* Copied from tools/perf/util/util.h */
112 #ifndef zfree
113 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
114 #endif
115 
116 #ifndef zclose
117 # define zclose(fd) ({			\
118 	int ___err = 0;			\
119 	if ((fd) >= 0)			\
120 		___err = close((fd));	\
121 	fd = -1;			\
122 	___err; })
123 #endif
124 
125 #ifdef HAVE_LIBELF_MMAP_SUPPORT
126 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
127 #else
128 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
129 #endif
130 
131 static inline __u64 ptr_to_u64(const void *ptr)
132 {
133 	return (__u64) (unsigned long) ptr;
134 }
135 
136 struct bpf_capabilities {
137 	/* v4.14: kernel support for program & map names. */
138 	__u32 name:1;
139 	/* v5.2: kernel support for global data sections. */
140 	__u32 global_data:1;
141 	/* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
142 	__u32 btf_func:1;
143 	/* BTF_KIND_VAR and BTF_KIND_DATASEC support */
144 	__u32 btf_datasec:1;
145 };
146 
147 /*
148  * bpf_prog should be a better name but it has been used in
149  * linux/filter.h.
150  */
151 struct bpf_program {
152 	/* Index in elf obj file, for relocation use. */
153 	int idx;
154 	char *name;
155 	int prog_ifindex;
156 	char *section_name;
157 	/* section_name with / replaced by _; makes recursive pinning
158 	 * in bpf_object__pin_programs easier
159 	 */
160 	char *pin_name;
161 	struct bpf_insn *insns;
162 	size_t insns_cnt, main_prog_cnt;
163 	enum bpf_prog_type type;
164 
165 	struct reloc_desc {
166 		enum {
167 			RELO_LD64,
168 			RELO_CALL,
169 			RELO_DATA,
170 		} type;
171 		int insn_idx;
172 		union {
173 			int map_idx;
174 			int text_off;
175 		};
176 	} *reloc_desc;
177 	int nr_reloc;
178 	int log_level;
179 
180 	struct {
181 		int nr;
182 		int *fds;
183 	} instances;
184 	bpf_program_prep_t preprocessor;
185 
186 	struct bpf_object *obj;
187 	void *priv;
188 	bpf_program_clear_priv_t clear_priv;
189 
190 	enum bpf_attach_type expected_attach_type;
191 	void *func_info;
192 	__u32 func_info_rec_size;
193 	__u32 func_info_cnt;
194 
195 	struct bpf_capabilities *caps;
196 
197 	void *line_info;
198 	__u32 line_info_rec_size;
199 	__u32 line_info_cnt;
200 	__u32 prog_flags;
201 };
202 
203 enum libbpf_map_type {
204 	LIBBPF_MAP_UNSPEC,
205 	LIBBPF_MAP_DATA,
206 	LIBBPF_MAP_BSS,
207 	LIBBPF_MAP_RODATA,
208 };
209 
210 static const char * const libbpf_type_to_btf_name[] = {
211 	[LIBBPF_MAP_DATA]	= ".data",
212 	[LIBBPF_MAP_BSS]	= ".bss",
213 	[LIBBPF_MAP_RODATA]	= ".rodata",
214 };
215 
216 struct bpf_map {
217 	int fd;
218 	char *name;
219 	int sec_idx;
220 	size_t sec_offset;
221 	int map_ifindex;
222 	int inner_map_fd;
223 	struct bpf_map_def def;
224 	__u32 btf_key_type_id;
225 	__u32 btf_value_type_id;
226 	void *priv;
227 	bpf_map_clear_priv_t clear_priv;
228 	enum libbpf_map_type libbpf_type;
229 };
230 
231 struct bpf_secdata {
232 	void *rodata;
233 	void *data;
234 };
235 
236 static LIST_HEAD(bpf_objects_list);
237 
238 struct bpf_object {
239 	char name[BPF_OBJ_NAME_LEN];
240 	char license[64];
241 	__u32 kern_version;
242 
243 	struct bpf_program *programs;
244 	size_t nr_programs;
245 	struct bpf_map *maps;
246 	size_t nr_maps;
247 	size_t maps_cap;
248 	struct bpf_secdata sections;
249 
250 	bool loaded;
251 	bool has_pseudo_calls;
252 	bool relaxed_core_relocs;
253 
254 	/*
255 	 * Information when doing elf related work. Only valid if fd
256 	 * is valid.
257 	 */
258 	struct {
259 		int fd;
260 		const void *obj_buf;
261 		size_t obj_buf_sz;
262 		Elf *elf;
263 		GElf_Ehdr ehdr;
264 		Elf_Data *symbols;
265 		Elf_Data *data;
266 		Elf_Data *rodata;
267 		Elf_Data *bss;
268 		size_t strtabidx;
269 		struct {
270 			GElf_Shdr shdr;
271 			Elf_Data *data;
272 		} *reloc;
273 		int nr_reloc;
274 		int maps_shndx;
275 		int btf_maps_shndx;
276 		int text_shndx;
277 		int data_shndx;
278 		int rodata_shndx;
279 		int bss_shndx;
280 	} efile;
281 	/*
282 	 * All loaded bpf_object is linked in a list, which is
283 	 * hidden to caller. bpf_objects__<func> handlers deal with
284 	 * all objects.
285 	 */
286 	struct list_head list;
287 
288 	struct btf *btf;
289 	struct btf_ext *btf_ext;
290 
291 	void *priv;
292 	bpf_object_clear_priv_t clear_priv;
293 
294 	struct bpf_capabilities caps;
295 
296 	char path[];
297 };
298 #define obj_elf_valid(o)	((o)->efile.elf)
299 
300 void bpf_program__unload(struct bpf_program *prog)
301 {
302 	int i;
303 
304 	if (!prog)
305 		return;
306 
307 	/*
308 	 * If the object is opened but the program was never loaded,
309 	 * it is possible that prog->instances.nr == -1.
310 	 */
311 	if (prog->instances.nr > 0) {
312 		for (i = 0; i < prog->instances.nr; i++)
313 			zclose(prog->instances.fds[i]);
314 	} else if (prog->instances.nr != -1) {
315 		pr_warn("Internal error: instances.nr is %d\n",
316 			prog->instances.nr);
317 	}
318 
319 	prog->instances.nr = -1;
320 	zfree(&prog->instances.fds);
321 
322 	zfree(&prog->func_info);
323 	zfree(&prog->line_info);
324 }
325 
326 static void bpf_program__exit(struct bpf_program *prog)
327 {
328 	if (!prog)
329 		return;
330 
331 	if (prog->clear_priv)
332 		prog->clear_priv(prog, prog->priv);
333 
334 	prog->priv = NULL;
335 	prog->clear_priv = NULL;
336 
337 	bpf_program__unload(prog);
338 	zfree(&prog->name);
339 	zfree(&prog->section_name);
340 	zfree(&prog->pin_name);
341 	zfree(&prog->insns);
342 	zfree(&prog->reloc_desc);
343 
344 	prog->nr_reloc = 0;
345 	prog->insns_cnt = 0;
346 	prog->idx = -1;
347 }
348 
349 static char *__bpf_program__pin_name(struct bpf_program *prog)
350 {
351 	char *name, *p;
352 
353 	name = p = strdup(prog->section_name);
354 	while ((p = strchr(p, '/')))
355 		*p = '_';
356 
357 	return name;
358 }
359 
360 static int
361 bpf_program__init(void *data, size_t size, char *section_name, int idx,
362 		  struct bpf_program *prog)
363 {
364 	const size_t bpf_insn_sz = sizeof(struct bpf_insn);
365 
366 	if (size == 0 || size % bpf_insn_sz) {
367 		pr_warn("corrupted section '%s', size: %zu\n",
368 			section_name, size);
369 		return -EINVAL;
370 	}
371 
372 	memset(prog, 0, sizeof(*prog));
373 
374 	prog->section_name = strdup(section_name);
375 	if (!prog->section_name) {
376 		pr_warn("failed to alloc name for prog under section(%d) %s\n",
377 			idx, section_name);
378 		goto errout;
379 	}
380 
381 	prog->pin_name = __bpf_program__pin_name(prog);
382 	if (!prog->pin_name) {
383 		pr_warn("failed to alloc pin name for prog under section(%d) %s\n",
384 			idx, section_name);
385 		goto errout;
386 	}
387 
388 	prog->insns = malloc(size);
389 	if (!prog->insns) {
390 		pr_warn("failed to alloc insns for prog under section %s\n",
391 			section_name);
392 		goto errout;
393 	}
394 	prog->insns_cnt = size / bpf_insn_sz;
395 	memcpy(prog->insns, data, size);
396 	prog->idx = idx;
397 	prog->instances.fds = NULL;
398 	prog->instances.nr = -1;
399 	prog->type = BPF_PROG_TYPE_UNSPEC;
400 
401 	return 0;
402 errout:
403 	bpf_program__exit(prog);
404 	return -ENOMEM;
405 }
406 
407 static int
408 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
409 			char *section_name, int idx)
410 {
411 	struct bpf_program prog, *progs;
412 	int nr_progs, err;
413 
414 	err = bpf_program__init(data, size, section_name, idx, &prog);
415 	if (err)
416 		return err;
417 
418 	prog.caps = &obj->caps;
419 	progs = obj->programs;
420 	nr_progs = obj->nr_programs;
421 
422 	progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
423 	if (!progs) {
424 		/*
425 		 * In this case the original obj->programs
426 		 * is still valid, so don't need special treat for
427 		 * bpf_close_object().
428 		 */
429 		pr_warn("failed to alloc a new program under section '%s'\n",
430 			section_name);
431 		bpf_program__exit(&prog);
432 		return -ENOMEM;
433 	}
434 
435 	pr_debug("found program %s\n", prog.section_name);
436 	obj->programs = progs;
437 	obj->nr_programs = nr_progs + 1;
438 	prog.obj = obj;
439 	progs[nr_progs] = prog;
440 	return 0;
441 }
442 
443 static int
444 bpf_object__init_prog_names(struct bpf_object *obj)
445 {
446 	Elf_Data *symbols = obj->efile.symbols;
447 	struct bpf_program *prog;
448 	size_t pi, si;
449 
450 	for (pi = 0; pi < obj->nr_programs; pi++) {
451 		const char *name = NULL;
452 
453 		prog = &obj->programs[pi];
454 
455 		for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
456 		     si++) {
457 			GElf_Sym sym;
458 
459 			if (!gelf_getsym(symbols, si, &sym))
460 				continue;
461 			if (sym.st_shndx != prog->idx)
462 				continue;
463 			if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
464 				continue;
465 
466 			name = elf_strptr(obj->efile.elf,
467 					  obj->efile.strtabidx,
468 					  sym.st_name);
469 			if (!name) {
470 				pr_warn("failed to get sym name string for prog %s\n",
471 					prog->section_name);
472 				return -LIBBPF_ERRNO__LIBELF;
473 			}
474 		}
475 
476 		if (!name && prog->idx == obj->efile.text_shndx)
477 			name = ".text";
478 
479 		if (!name) {
480 			pr_warn("failed to find sym for prog %s\n",
481 				prog->section_name);
482 			return -EINVAL;
483 		}
484 
485 		prog->name = strdup(name);
486 		if (!prog->name) {
487 			pr_warn("failed to allocate memory for prog sym %s\n",
488 				name);
489 			return -ENOMEM;
490 		}
491 	}
492 
493 	return 0;
494 }
495 
496 static __u32 get_kernel_version(void)
497 {
498 	__u32 major, minor, patch;
499 	struct utsname info;
500 
501 	uname(&info);
502 	if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
503 		return 0;
504 	return KERNEL_VERSION(major, minor, patch);
505 }
506 
507 static struct bpf_object *bpf_object__new(const char *path,
508 					  const void *obj_buf,
509 					  size_t obj_buf_sz,
510 					  const char *obj_name)
511 {
512 	struct bpf_object *obj;
513 	char *end;
514 
515 	obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
516 	if (!obj) {
517 		pr_warn("alloc memory failed for %s\n", path);
518 		return ERR_PTR(-ENOMEM);
519 	}
520 
521 	strcpy(obj->path, path);
522 	if (obj_name) {
523 		strncpy(obj->name, obj_name, sizeof(obj->name) - 1);
524 		obj->name[sizeof(obj->name) - 1] = 0;
525 	} else {
526 		/* Using basename() GNU version which doesn't modify arg. */
527 		strncpy(obj->name, basename((void *)path),
528 			sizeof(obj->name) - 1);
529 		end = strchr(obj->name, '.');
530 		if (end)
531 			*end = 0;
532 	}
533 
534 	obj->efile.fd = -1;
535 	/*
536 	 * Caller of this function should also call
537 	 * bpf_object__elf_finish() after data collection to return
538 	 * obj_buf to user. If not, we should duplicate the buffer to
539 	 * avoid user freeing them before elf finish.
540 	 */
541 	obj->efile.obj_buf = obj_buf;
542 	obj->efile.obj_buf_sz = obj_buf_sz;
543 	obj->efile.maps_shndx = -1;
544 	obj->efile.btf_maps_shndx = -1;
545 	obj->efile.data_shndx = -1;
546 	obj->efile.rodata_shndx = -1;
547 	obj->efile.bss_shndx = -1;
548 
549 	obj->kern_version = get_kernel_version();
550 	obj->loaded = false;
551 
552 	INIT_LIST_HEAD(&obj->list);
553 	list_add(&obj->list, &bpf_objects_list);
554 	return obj;
555 }
556 
557 static void bpf_object__elf_finish(struct bpf_object *obj)
558 {
559 	if (!obj_elf_valid(obj))
560 		return;
561 
562 	if (obj->efile.elf) {
563 		elf_end(obj->efile.elf);
564 		obj->efile.elf = NULL;
565 	}
566 	obj->efile.symbols = NULL;
567 	obj->efile.data = NULL;
568 	obj->efile.rodata = NULL;
569 	obj->efile.bss = NULL;
570 
571 	zfree(&obj->efile.reloc);
572 	obj->efile.nr_reloc = 0;
573 	zclose(obj->efile.fd);
574 	obj->efile.obj_buf = NULL;
575 	obj->efile.obj_buf_sz = 0;
576 }
577 
578 static int bpf_object__elf_init(struct bpf_object *obj)
579 {
580 	int err = 0;
581 	GElf_Ehdr *ep;
582 
583 	if (obj_elf_valid(obj)) {
584 		pr_warn("elf init: internal error\n");
585 		return -LIBBPF_ERRNO__LIBELF;
586 	}
587 
588 	if (obj->efile.obj_buf_sz > 0) {
589 		/*
590 		 * obj_buf should have been validated by
591 		 * bpf_object__open_buffer().
592 		 */
593 		obj->efile.elf = elf_memory((char *)obj->efile.obj_buf,
594 					    obj->efile.obj_buf_sz);
595 	} else {
596 		obj->efile.fd = open(obj->path, O_RDONLY);
597 		if (obj->efile.fd < 0) {
598 			char errmsg[STRERR_BUFSIZE], *cp;
599 
600 			err = -errno;
601 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
602 			pr_warn("failed to open %s: %s\n", obj->path, cp);
603 			return err;
604 		}
605 
606 		obj->efile.elf = elf_begin(obj->efile.fd,
607 					   LIBBPF_ELF_C_READ_MMAP, NULL);
608 	}
609 
610 	if (!obj->efile.elf) {
611 		pr_warn("failed to open %s as ELF file\n", obj->path);
612 		err = -LIBBPF_ERRNO__LIBELF;
613 		goto errout;
614 	}
615 
616 	if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
617 		pr_warn("failed to get EHDR from %s\n", obj->path);
618 		err = -LIBBPF_ERRNO__FORMAT;
619 		goto errout;
620 	}
621 	ep = &obj->efile.ehdr;
622 
623 	/* Old LLVM set e_machine to EM_NONE */
624 	if (ep->e_type != ET_REL ||
625 	    (ep->e_machine && ep->e_machine != EM_BPF)) {
626 		pr_warn("%s is not an eBPF object file\n", obj->path);
627 		err = -LIBBPF_ERRNO__FORMAT;
628 		goto errout;
629 	}
630 
631 	return 0;
632 errout:
633 	bpf_object__elf_finish(obj);
634 	return err;
635 }
636 
637 static int bpf_object__check_endianness(struct bpf_object *obj)
638 {
639 #if __BYTE_ORDER == __LITTLE_ENDIAN
640 	if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
641 		return 0;
642 #elif __BYTE_ORDER == __BIG_ENDIAN
643 	if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
644 		return 0;
645 #else
646 # error "Unrecognized __BYTE_ORDER__"
647 #endif
648 	pr_warn("endianness mismatch.\n");
649 	return -LIBBPF_ERRNO__ENDIAN;
650 }
651 
652 static int
653 bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
654 {
655 	memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
656 	pr_debug("license of %s is %s\n", obj->path, obj->license);
657 	return 0;
658 }
659 
660 static int
661 bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
662 {
663 	__u32 kver;
664 
665 	if (size != sizeof(kver)) {
666 		pr_warn("invalid kver section in %s\n", obj->path);
667 		return -LIBBPF_ERRNO__FORMAT;
668 	}
669 	memcpy(&kver, data, sizeof(kver));
670 	obj->kern_version = kver;
671 	pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
672 	return 0;
673 }
674 
675 static int compare_bpf_map(const void *_a, const void *_b)
676 {
677 	const struct bpf_map *a = _a;
678 	const struct bpf_map *b = _b;
679 
680 	if (a->sec_idx != b->sec_idx)
681 		return a->sec_idx - b->sec_idx;
682 	return a->sec_offset - b->sec_offset;
683 }
684 
685 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
686 {
687 	if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
688 	    type == BPF_MAP_TYPE_HASH_OF_MAPS)
689 		return true;
690 	return false;
691 }
692 
693 static int bpf_object_search_section_size(const struct bpf_object *obj,
694 					  const char *name, size_t *d_size)
695 {
696 	const GElf_Ehdr *ep = &obj->efile.ehdr;
697 	Elf *elf = obj->efile.elf;
698 	Elf_Scn *scn = NULL;
699 	int idx = 0;
700 
701 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
702 		const char *sec_name;
703 		Elf_Data *data;
704 		GElf_Shdr sh;
705 
706 		idx++;
707 		if (gelf_getshdr(scn, &sh) != &sh) {
708 			pr_warn("failed to get section(%d) header from %s\n",
709 				idx, obj->path);
710 			return -EIO;
711 		}
712 
713 		sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
714 		if (!sec_name) {
715 			pr_warn("failed to get section(%d) name from %s\n",
716 				idx, obj->path);
717 			return -EIO;
718 		}
719 
720 		if (strcmp(name, sec_name))
721 			continue;
722 
723 		data = elf_getdata(scn, 0);
724 		if (!data) {
725 			pr_warn("failed to get section(%d) data from %s(%s)\n",
726 				idx, name, obj->path);
727 			return -EIO;
728 		}
729 
730 		*d_size = data->d_size;
731 		return 0;
732 	}
733 
734 	return -ENOENT;
735 }
736 
737 int bpf_object__section_size(const struct bpf_object *obj, const char *name,
738 			     __u32 *size)
739 {
740 	int ret = -ENOENT;
741 	size_t d_size;
742 
743 	*size = 0;
744 	if (!name) {
745 		return -EINVAL;
746 	} else if (!strcmp(name, ".data")) {
747 		if (obj->efile.data)
748 			*size = obj->efile.data->d_size;
749 	} else if (!strcmp(name, ".bss")) {
750 		if (obj->efile.bss)
751 			*size = obj->efile.bss->d_size;
752 	} else if (!strcmp(name, ".rodata")) {
753 		if (obj->efile.rodata)
754 			*size = obj->efile.rodata->d_size;
755 	} else {
756 		ret = bpf_object_search_section_size(obj, name, &d_size);
757 		if (!ret)
758 			*size = d_size;
759 	}
760 
761 	return *size ? 0 : ret;
762 }
763 
764 int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
765 				__u32 *off)
766 {
767 	Elf_Data *symbols = obj->efile.symbols;
768 	const char *sname;
769 	size_t si;
770 
771 	if (!name || !off)
772 		return -EINVAL;
773 
774 	for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
775 		GElf_Sym sym;
776 
777 		if (!gelf_getsym(symbols, si, &sym))
778 			continue;
779 		if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
780 		    GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
781 			continue;
782 
783 		sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
784 				   sym.st_name);
785 		if (!sname) {
786 			pr_warn("failed to get sym name string for var %s\n",
787 				name);
788 			return -EIO;
789 		}
790 		if (strcmp(name, sname) == 0) {
791 			*off = sym.st_value;
792 			return 0;
793 		}
794 	}
795 
796 	return -ENOENT;
797 }
798 
799 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
800 {
801 	struct bpf_map *new_maps;
802 	size_t new_cap;
803 	int i;
804 
805 	if (obj->nr_maps < obj->maps_cap)
806 		return &obj->maps[obj->nr_maps++];
807 
808 	new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
809 	new_maps = realloc(obj->maps, new_cap * sizeof(*obj->maps));
810 	if (!new_maps) {
811 		pr_warn("alloc maps for object failed\n");
812 		return ERR_PTR(-ENOMEM);
813 	}
814 
815 	obj->maps_cap = new_cap;
816 	obj->maps = new_maps;
817 
818 	/* zero out new maps */
819 	memset(obj->maps + obj->nr_maps, 0,
820 	       (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
821 	/*
822 	 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
823 	 * when failure (zclose won't close negative fd)).
824 	 */
825 	for (i = obj->nr_maps; i < obj->maps_cap; i++) {
826 		obj->maps[i].fd = -1;
827 		obj->maps[i].inner_map_fd = -1;
828 	}
829 
830 	return &obj->maps[obj->nr_maps++];
831 }
832 
833 static int
834 bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
835 			      int sec_idx, Elf_Data *data, void **data_buff)
836 {
837 	char map_name[BPF_OBJ_NAME_LEN];
838 	struct bpf_map_def *def;
839 	struct bpf_map *map;
840 
841 	map = bpf_object__add_map(obj);
842 	if (IS_ERR(map))
843 		return PTR_ERR(map);
844 
845 	map->libbpf_type = type;
846 	map->sec_idx = sec_idx;
847 	map->sec_offset = 0;
848 	snprintf(map_name, sizeof(map_name), "%.8s%.7s", obj->name,
849 		 libbpf_type_to_btf_name[type]);
850 	map->name = strdup(map_name);
851 	if (!map->name) {
852 		pr_warn("failed to alloc map name\n");
853 		return -ENOMEM;
854 	}
855 	pr_debug("map '%s' (global data): at sec_idx %d, offset %zu.\n",
856 		 map_name, map->sec_idx, map->sec_offset);
857 
858 	def = &map->def;
859 	def->type = BPF_MAP_TYPE_ARRAY;
860 	def->key_size = sizeof(int);
861 	def->value_size = data->d_size;
862 	def->max_entries = 1;
863 	def->map_flags = type == LIBBPF_MAP_RODATA ? BPF_F_RDONLY_PROG : 0;
864 	if (data_buff) {
865 		*data_buff = malloc(data->d_size);
866 		if (!*data_buff) {
867 			zfree(&map->name);
868 			pr_warn("failed to alloc map content buffer\n");
869 			return -ENOMEM;
870 		}
871 		memcpy(*data_buff, data->d_buf, data->d_size);
872 	}
873 
874 	pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
875 	return 0;
876 }
877 
878 static int bpf_object__init_global_data_maps(struct bpf_object *obj)
879 {
880 	int err;
881 
882 	if (!obj->caps.global_data)
883 		return 0;
884 	/*
885 	 * Populate obj->maps with libbpf internal maps.
886 	 */
887 	if (obj->efile.data_shndx >= 0) {
888 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
889 						    obj->efile.data_shndx,
890 						    obj->efile.data,
891 						    &obj->sections.data);
892 		if (err)
893 			return err;
894 	}
895 	if (obj->efile.rodata_shndx >= 0) {
896 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
897 						    obj->efile.rodata_shndx,
898 						    obj->efile.rodata,
899 						    &obj->sections.rodata);
900 		if (err)
901 			return err;
902 	}
903 	if (obj->efile.bss_shndx >= 0) {
904 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
905 						    obj->efile.bss_shndx,
906 						    obj->efile.bss, NULL);
907 		if (err)
908 			return err;
909 	}
910 	return 0;
911 }
912 
913 static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
914 {
915 	Elf_Data *symbols = obj->efile.symbols;
916 	int i, map_def_sz = 0, nr_maps = 0, nr_syms;
917 	Elf_Data *data = NULL;
918 	Elf_Scn *scn;
919 
920 	if (obj->efile.maps_shndx < 0)
921 		return 0;
922 
923 	if (!symbols)
924 		return -EINVAL;
925 
926 	scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
927 	if (scn)
928 		data = elf_getdata(scn, NULL);
929 	if (!scn || !data) {
930 		pr_warn("failed to get Elf_Data from map section %d\n",
931 			obj->efile.maps_shndx);
932 		return -EINVAL;
933 	}
934 
935 	/*
936 	 * Count number of maps. Each map has a name.
937 	 * Array of maps is not supported: only the first element is
938 	 * considered.
939 	 *
940 	 * TODO: Detect array of map and report error.
941 	 */
942 	nr_syms = symbols->d_size / sizeof(GElf_Sym);
943 	for (i = 0; i < nr_syms; i++) {
944 		GElf_Sym sym;
945 
946 		if (!gelf_getsym(symbols, i, &sym))
947 			continue;
948 		if (sym.st_shndx != obj->efile.maps_shndx)
949 			continue;
950 		nr_maps++;
951 	}
952 	/* Assume equally sized map definitions */
953 	pr_debug("maps in %s: %d maps in %zd bytes\n",
954 		 obj->path, nr_maps, data->d_size);
955 
956 	map_def_sz = data->d_size / nr_maps;
957 	if (!data->d_size || (data->d_size % nr_maps) != 0) {
958 		pr_warn("unable to determine map definition size "
959 			"section %s, %d maps in %zd bytes\n",
960 			obj->path, nr_maps, data->d_size);
961 		return -EINVAL;
962 	}
963 
964 	/* Fill obj->maps using data in "maps" section.  */
965 	for (i = 0; i < nr_syms; i++) {
966 		GElf_Sym sym;
967 		const char *map_name;
968 		struct bpf_map_def *def;
969 		struct bpf_map *map;
970 
971 		if (!gelf_getsym(symbols, i, &sym))
972 			continue;
973 		if (sym.st_shndx != obj->efile.maps_shndx)
974 			continue;
975 
976 		map = bpf_object__add_map(obj);
977 		if (IS_ERR(map))
978 			return PTR_ERR(map);
979 
980 		map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
981 				      sym.st_name);
982 		if (!map_name) {
983 			pr_warn("failed to get map #%d name sym string for obj %s\n",
984 				i, obj->path);
985 			return -LIBBPF_ERRNO__FORMAT;
986 		}
987 
988 		map->libbpf_type = LIBBPF_MAP_UNSPEC;
989 		map->sec_idx = sym.st_shndx;
990 		map->sec_offset = sym.st_value;
991 		pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
992 			 map_name, map->sec_idx, map->sec_offset);
993 		if (sym.st_value + map_def_sz > data->d_size) {
994 			pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
995 				obj->path, map_name);
996 			return -EINVAL;
997 		}
998 
999 		map->name = strdup(map_name);
1000 		if (!map->name) {
1001 			pr_warn("failed to alloc map name\n");
1002 			return -ENOMEM;
1003 		}
1004 		pr_debug("map %d is \"%s\"\n", i, map->name);
1005 		def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
1006 		/*
1007 		 * If the definition of the map in the object file fits in
1008 		 * bpf_map_def, copy it.  Any extra fields in our version
1009 		 * of bpf_map_def will default to zero as a result of the
1010 		 * calloc above.
1011 		 */
1012 		if (map_def_sz <= sizeof(struct bpf_map_def)) {
1013 			memcpy(&map->def, def, map_def_sz);
1014 		} else {
1015 			/*
1016 			 * Here the map structure being read is bigger than what
1017 			 * we expect, truncate if the excess bits are all zero.
1018 			 * If they are not zero, reject this map as
1019 			 * incompatible.
1020 			 */
1021 			char *b;
1022 			for (b = ((char *)def) + sizeof(struct bpf_map_def);
1023 			     b < ((char *)def) + map_def_sz; b++) {
1024 				if (*b != 0) {
1025 					pr_warn("maps section in %s: \"%s\" "
1026 						"has unrecognized, non-zero "
1027 						"options\n",
1028 						obj->path, map_name);
1029 					if (strict)
1030 						return -EINVAL;
1031 				}
1032 			}
1033 			memcpy(&map->def, def, sizeof(struct bpf_map_def));
1034 		}
1035 	}
1036 	return 0;
1037 }
1038 
1039 static const struct btf_type *
1040 skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
1041 {
1042 	const struct btf_type *t = btf__type_by_id(btf, id);
1043 
1044 	if (res_id)
1045 		*res_id = id;
1046 
1047 	while (btf_is_mod(t) || btf_is_typedef(t)) {
1048 		if (res_id)
1049 			*res_id = t->type;
1050 		t = btf__type_by_id(btf, t->type);
1051 	}
1052 
1053 	return t;
1054 }
1055 
1056 /*
1057  * Fetch integer attribute of BTF map definition. Such attributes are
1058  * represented using a pointer to an array, in which dimensionality of array
1059  * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1060  * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1061  * type definition, while using only sizeof(void *) space in ELF data section.
1062  */
1063 static bool get_map_field_int(const char *map_name, const struct btf *btf,
1064 			      const struct btf_type *def,
1065 			      const struct btf_member *m, __u32 *res) {
1066 	const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
1067 	const char *name = btf__name_by_offset(btf, m->name_off);
1068 	const struct btf_array *arr_info;
1069 	const struct btf_type *arr_t;
1070 
1071 	if (!btf_is_ptr(t)) {
1072 		pr_warn("map '%s': attr '%s': expected PTR, got %u.\n",
1073 			map_name, name, btf_kind(t));
1074 		return false;
1075 	}
1076 
1077 	arr_t = btf__type_by_id(btf, t->type);
1078 	if (!arr_t) {
1079 		pr_warn("map '%s': attr '%s': type [%u] not found.\n",
1080 			map_name, name, t->type);
1081 		return false;
1082 	}
1083 	if (!btf_is_array(arr_t)) {
1084 		pr_warn("map '%s': attr '%s': expected ARRAY, got %u.\n",
1085 			map_name, name, btf_kind(arr_t));
1086 		return false;
1087 	}
1088 	arr_info = btf_array(arr_t);
1089 	*res = arr_info->nelems;
1090 	return true;
1091 }
1092 
1093 static int bpf_object__init_user_btf_map(struct bpf_object *obj,
1094 					 const struct btf_type *sec,
1095 					 int var_idx, int sec_idx,
1096 					 const Elf_Data *data, bool strict)
1097 {
1098 	const struct btf_type *var, *def, *t;
1099 	const struct btf_var_secinfo *vi;
1100 	const struct btf_var *var_extra;
1101 	const struct btf_member *m;
1102 	const char *map_name;
1103 	struct bpf_map *map;
1104 	int vlen, i;
1105 
1106 	vi = btf_var_secinfos(sec) + var_idx;
1107 	var = btf__type_by_id(obj->btf, vi->type);
1108 	var_extra = btf_var(var);
1109 	map_name = btf__name_by_offset(obj->btf, var->name_off);
1110 	vlen = btf_vlen(var);
1111 
1112 	if (map_name == NULL || map_name[0] == '\0') {
1113 		pr_warn("map #%d: empty name.\n", var_idx);
1114 		return -EINVAL;
1115 	}
1116 	if ((__u64)vi->offset + vi->size > data->d_size) {
1117 		pr_warn("map '%s' BTF data is corrupted.\n", map_name);
1118 		return -EINVAL;
1119 	}
1120 	if (!btf_is_var(var)) {
1121 		pr_warn("map '%s': unexpected var kind %u.\n",
1122 			map_name, btf_kind(var));
1123 		return -EINVAL;
1124 	}
1125 	if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
1126 	    var_extra->linkage != BTF_VAR_STATIC) {
1127 		pr_warn("map '%s': unsupported var linkage %u.\n",
1128 			map_name, var_extra->linkage);
1129 		return -EOPNOTSUPP;
1130 	}
1131 
1132 	def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
1133 	if (!btf_is_struct(def)) {
1134 		pr_warn("map '%s': unexpected def kind %u.\n",
1135 			map_name, btf_kind(var));
1136 		return -EINVAL;
1137 	}
1138 	if (def->size > vi->size) {
1139 		pr_warn("map '%s': invalid def size.\n", map_name);
1140 		return -EINVAL;
1141 	}
1142 
1143 	map = bpf_object__add_map(obj);
1144 	if (IS_ERR(map))
1145 		return PTR_ERR(map);
1146 	map->name = strdup(map_name);
1147 	if (!map->name) {
1148 		pr_warn("map '%s': failed to alloc map name.\n", map_name);
1149 		return -ENOMEM;
1150 	}
1151 	map->libbpf_type = LIBBPF_MAP_UNSPEC;
1152 	map->def.type = BPF_MAP_TYPE_UNSPEC;
1153 	map->sec_idx = sec_idx;
1154 	map->sec_offset = vi->offset;
1155 	pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
1156 		 map_name, map->sec_idx, map->sec_offset);
1157 
1158 	vlen = btf_vlen(def);
1159 	m = btf_members(def);
1160 	for (i = 0; i < vlen; i++, m++) {
1161 		const char *name = btf__name_by_offset(obj->btf, m->name_off);
1162 
1163 		if (!name) {
1164 			pr_warn("map '%s': invalid field #%d.\n", map_name, i);
1165 			return -EINVAL;
1166 		}
1167 		if (strcmp(name, "type") == 0) {
1168 			if (!get_map_field_int(map_name, obj->btf, def, m,
1169 					       &map->def.type))
1170 				return -EINVAL;
1171 			pr_debug("map '%s': found type = %u.\n",
1172 				 map_name, map->def.type);
1173 		} else if (strcmp(name, "max_entries") == 0) {
1174 			if (!get_map_field_int(map_name, obj->btf, def, m,
1175 					       &map->def.max_entries))
1176 				return -EINVAL;
1177 			pr_debug("map '%s': found max_entries = %u.\n",
1178 				 map_name, map->def.max_entries);
1179 		} else if (strcmp(name, "map_flags") == 0) {
1180 			if (!get_map_field_int(map_name, obj->btf, def, m,
1181 					       &map->def.map_flags))
1182 				return -EINVAL;
1183 			pr_debug("map '%s': found map_flags = %u.\n",
1184 				 map_name, map->def.map_flags);
1185 		} else if (strcmp(name, "key_size") == 0) {
1186 			__u32 sz;
1187 
1188 			if (!get_map_field_int(map_name, obj->btf, def, m,
1189 					       &sz))
1190 				return -EINVAL;
1191 			pr_debug("map '%s': found key_size = %u.\n",
1192 				 map_name, sz);
1193 			if (map->def.key_size && map->def.key_size != sz) {
1194 				pr_warn("map '%s': conflicting key size %u != %u.\n",
1195 					map_name, map->def.key_size, sz);
1196 				return -EINVAL;
1197 			}
1198 			map->def.key_size = sz;
1199 		} else if (strcmp(name, "key") == 0) {
1200 			__s64 sz;
1201 
1202 			t = btf__type_by_id(obj->btf, m->type);
1203 			if (!t) {
1204 				pr_warn("map '%s': key type [%d] not found.\n",
1205 					map_name, m->type);
1206 				return -EINVAL;
1207 			}
1208 			if (!btf_is_ptr(t)) {
1209 				pr_warn("map '%s': key spec is not PTR: %u.\n",
1210 					map_name, btf_kind(t));
1211 				return -EINVAL;
1212 			}
1213 			sz = btf__resolve_size(obj->btf, t->type);
1214 			if (sz < 0) {
1215 				pr_warn("map '%s': can't determine key size for type [%u]: %lld.\n",
1216 					map_name, t->type, sz);
1217 				return sz;
1218 			}
1219 			pr_debug("map '%s': found key [%u], sz = %lld.\n",
1220 				 map_name, t->type, sz);
1221 			if (map->def.key_size && map->def.key_size != sz) {
1222 				pr_warn("map '%s': conflicting key size %u != %lld.\n",
1223 					map_name, map->def.key_size, sz);
1224 				return -EINVAL;
1225 			}
1226 			map->def.key_size = sz;
1227 			map->btf_key_type_id = t->type;
1228 		} else if (strcmp(name, "value_size") == 0) {
1229 			__u32 sz;
1230 
1231 			if (!get_map_field_int(map_name, obj->btf, def, m,
1232 					       &sz))
1233 				return -EINVAL;
1234 			pr_debug("map '%s': found value_size = %u.\n",
1235 				 map_name, sz);
1236 			if (map->def.value_size && map->def.value_size != sz) {
1237 				pr_warn("map '%s': conflicting value size %u != %u.\n",
1238 					map_name, map->def.value_size, sz);
1239 				return -EINVAL;
1240 			}
1241 			map->def.value_size = sz;
1242 		} else if (strcmp(name, "value") == 0) {
1243 			__s64 sz;
1244 
1245 			t = btf__type_by_id(obj->btf, m->type);
1246 			if (!t) {
1247 				pr_warn("map '%s': value type [%d] not found.\n",
1248 					map_name, m->type);
1249 				return -EINVAL;
1250 			}
1251 			if (!btf_is_ptr(t)) {
1252 				pr_warn("map '%s': value spec is not PTR: %u.\n",
1253 					map_name, btf_kind(t));
1254 				return -EINVAL;
1255 			}
1256 			sz = btf__resolve_size(obj->btf, t->type);
1257 			if (sz < 0) {
1258 				pr_warn("map '%s': can't determine value size for type [%u]: %lld.\n",
1259 					map_name, t->type, sz);
1260 				return sz;
1261 			}
1262 			pr_debug("map '%s': found value [%u], sz = %lld.\n",
1263 				 map_name, t->type, sz);
1264 			if (map->def.value_size && map->def.value_size != sz) {
1265 				pr_warn("map '%s': conflicting value size %u != %lld.\n",
1266 					map_name, map->def.value_size, sz);
1267 				return -EINVAL;
1268 			}
1269 			map->def.value_size = sz;
1270 			map->btf_value_type_id = t->type;
1271 		} else {
1272 			if (strict) {
1273 				pr_warn("map '%s': unknown field '%s'.\n",
1274 					map_name, name);
1275 				return -ENOTSUP;
1276 			}
1277 			pr_debug("map '%s': ignoring unknown field '%s'.\n",
1278 				 map_name, name);
1279 		}
1280 	}
1281 
1282 	if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
1283 		pr_warn("map '%s': map type isn't specified.\n", map_name);
1284 		return -EINVAL;
1285 	}
1286 
1287 	return 0;
1288 }
1289 
1290 static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict)
1291 {
1292 	const struct btf_type *sec = NULL;
1293 	int nr_types, i, vlen, err;
1294 	const struct btf_type *t;
1295 	const char *name;
1296 	Elf_Data *data;
1297 	Elf_Scn *scn;
1298 
1299 	if (obj->efile.btf_maps_shndx < 0)
1300 		return 0;
1301 
1302 	scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx);
1303 	if (scn)
1304 		data = elf_getdata(scn, NULL);
1305 	if (!scn || !data) {
1306 		pr_warn("failed to get Elf_Data from map section %d (%s)\n",
1307 			obj->efile.maps_shndx, MAPS_ELF_SEC);
1308 		return -EINVAL;
1309 	}
1310 
1311 	nr_types = btf__get_nr_types(obj->btf);
1312 	for (i = 1; i <= nr_types; i++) {
1313 		t = btf__type_by_id(obj->btf, i);
1314 		if (!btf_is_datasec(t))
1315 			continue;
1316 		name = btf__name_by_offset(obj->btf, t->name_off);
1317 		if (strcmp(name, MAPS_ELF_SEC) == 0) {
1318 			sec = t;
1319 			break;
1320 		}
1321 	}
1322 
1323 	if (!sec) {
1324 		pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
1325 		return -ENOENT;
1326 	}
1327 
1328 	vlen = btf_vlen(sec);
1329 	for (i = 0; i < vlen; i++) {
1330 		err = bpf_object__init_user_btf_map(obj, sec, i,
1331 						    obj->efile.btf_maps_shndx,
1332 						    data, strict);
1333 		if (err)
1334 			return err;
1335 	}
1336 
1337 	return 0;
1338 }
1339 
1340 static int bpf_object__init_maps(struct bpf_object *obj, bool relaxed_maps)
1341 {
1342 	bool strict = !relaxed_maps;
1343 	int err;
1344 
1345 	err = bpf_object__init_user_maps(obj, strict);
1346 	if (err)
1347 		return err;
1348 
1349 	err = bpf_object__init_user_btf_maps(obj, strict);
1350 	if (err)
1351 		return err;
1352 
1353 	err = bpf_object__init_global_data_maps(obj);
1354 	if (err)
1355 		return err;
1356 
1357 	if (obj->nr_maps) {
1358 		qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]),
1359 		      compare_bpf_map);
1360 	}
1361 	return 0;
1362 }
1363 
1364 static bool section_have_execinstr(struct bpf_object *obj, int idx)
1365 {
1366 	Elf_Scn *scn;
1367 	GElf_Shdr sh;
1368 
1369 	scn = elf_getscn(obj->efile.elf, idx);
1370 	if (!scn)
1371 		return false;
1372 
1373 	if (gelf_getshdr(scn, &sh) != &sh)
1374 		return false;
1375 
1376 	if (sh.sh_flags & SHF_EXECINSTR)
1377 		return true;
1378 
1379 	return false;
1380 }
1381 
1382 static void bpf_object__sanitize_btf(struct bpf_object *obj)
1383 {
1384 	bool has_datasec = obj->caps.btf_datasec;
1385 	bool has_func = obj->caps.btf_func;
1386 	struct btf *btf = obj->btf;
1387 	struct btf_type *t;
1388 	int i, j, vlen;
1389 
1390 	if (!obj->btf || (has_func && has_datasec))
1391 		return;
1392 
1393 	for (i = 1; i <= btf__get_nr_types(btf); i++) {
1394 		t = (struct btf_type *)btf__type_by_id(btf, i);
1395 
1396 		if (!has_datasec && btf_is_var(t)) {
1397 			/* replace VAR with INT */
1398 			t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1399 			/*
1400 			 * using size = 1 is the safest choice, 4 will be too
1401 			 * big and cause kernel BTF validation failure if
1402 			 * original variable took less than 4 bytes
1403 			 */
1404 			t->size = 1;
1405 			*(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
1406 		} else if (!has_datasec && btf_is_datasec(t)) {
1407 			/* replace DATASEC with STRUCT */
1408 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
1409 			struct btf_member *m = btf_members(t);
1410 			struct btf_type *vt;
1411 			char *name;
1412 
1413 			name = (char *)btf__name_by_offset(btf, t->name_off);
1414 			while (*name) {
1415 				if (*name == '.')
1416 					*name = '_';
1417 				name++;
1418 			}
1419 
1420 			vlen = btf_vlen(t);
1421 			t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
1422 			for (j = 0; j < vlen; j++, v++, m++) {
1423 				/* order of field assignments is important */
1424 				m->offset = v->offset * 8;
1425 				m->type = v->type;
1426 				/* preserve variable name as member name */
1427 				vt = (void *)btf__type_by_id(btf, v->type);
1428 				m->name_off = vt->name_off;
1429 			}
1430 		} else if (!has_func && btf_is_func_proto(t)) {
1431 			/* replace FUNC_PROTO with ENUM */
1432 			vlen = btf_vlen(t);
1433 			t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
1434 			t->size = sizeof(__u32); /* kernel enforced */
1435 		} else if (!has_func && btf_is_func(t)) {
1436 			/* replace FUNC with TYPEDEF */
1437 			t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
1438 		}
1439 	}
1440 }
1441 
1442 static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
1443 {
1444 	if (!obj->btf_ext)
1445 		return;
1446 
1447 	if (!obj->caps.btf_func) {
1448 		btf_ext__free(obj->btf_ext);
1449 		obj->btf_ext = NULL;
1450 	}
1451 }
1452 
1453 static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj)
1454 {
1455 	return obj->efile.btf_maps_shndx >= 0;
1456 }
1457 
1458 static int bpf_object__init_btf(struct bpf_object *obj,
1459 				Elf_Data *btf_data,
1460 				Elf_Data *btf_ext_data)
1461 {
1462 	bool btf_required = bpf_object__is_btf_mandatory(obj);
1463 	int err = 0;
1464 
1465 	if (btf_data) {
1466 		obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
1467 		if (IS_ERR(obj->btf)) {
1468 			pr_warn("Error loading ELF section %s: %d.\n",
1469 				BTF_ELF_SEC, err);
1470 			goto out;
1471 		}
1472 		err = btf__finalize_data(obj, obj->btf);
1473 		if (err) {
1474 			pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err);
1475 			goto out;
1476 		}
1477 	}
1478 	if (btf_ext_data) {
1479 		if (!obj->btf) {
1480 			pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
1481 				 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
1482 			goto out;
1483 		}
1484 		obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
1485 					    btf_ext_data->d_size);
1486 		if (IS_ERR(obj->btf_ext)) {
1487 			pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n",
1488 				BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
1489 			obj->btf_ext = NULL;
1490 			goto out;
1491 		}
1492 	}
1493 out:
1494 	if (err || IS_ERR(obj->btf)) {
1495 		if (btf_required)
1496 			err = err ? : PTR_ERR(obj->btf);
1497 		else
1498 			err = 0;
1499 		if (!IS_ERR_OR_NULL(obj->btf))
1500 			btf__free(obj->btf);
1501 		obj->btf = NULL;
1502 	}
1503 	if (btf_required && !obj->btf) {
1504 		pr_warn("BTF is required, but is missing or corrupted.\n");
1505 		return err == 0 ? -ENOENT : err;
1506 	}
1507 	return 0;
1508 }
1509 
1510 static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
1511 {
1512 	int err = 0;
1513 
1514 	if (!obj->btf)
1515 		return 0;
1516 
1517 	bpf_object__sanitize_btf(obj);
1518 	bpf_object__sanitize_btf_ext(obj);
1519 
1520 	err = btf__load(obj->btf);
1521 	if (err) {
1522 		pr_warn("Error loading %s into kernel: %d.\n",
1523 			BTF_ELF_SEC, err);
1524 		btf__free(obj->btf);
1525 		obj->btf = NULL;
1526 		/* btf_ext can't exist without btf, so free it as well */
1527 		if (obj->btf_ext) {
1528 			btf_ext__free(obj->btf_ext);
1529 			obj->btf_ext = NULL;
1530 		}
1531 
1532 		if (bpf_object__is_btf_mandatory(obj))
1533 			return err;
1534 	}
1535 	return 0;
1536 }
1537 
1538 static int bpf_object__elf_collect(struct bpf_object *obj, bool relaxed_maps)
1539 {
1540 	Elf *elf = obj->efile.elf;
1541 	GElf_Ehdr *ep = &obj->efile.ehdr;
1542 	Elf_Data *btf_ext_data = NULL;
1543 	Elf_Data *btf_data = NULL;
1544 	Elf_Scn *scn = NULL;
1545 	int idx = 0, err = 0;
1546 
1547 	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
1548 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
1549 		pr_warn("failed to get e_shstrndx from %s\n", obj->path);
1550 		return -LIBBPF_ERRNO__FORMAT;
1551 	}
1552 
1553 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
1554 		char *name;
1555 		GElf_Shdr sh;
1556 		Elf_Data *data;
1557 
1558 		idx++;
1559 		if (gelf_getshdr(scn, &sh) != &sh) {
1560 			pr_warn("failed to get section(%d) header from %s\n",
1561 				idx, obj->path);
1562 			return -LIBBPF_ERRNO__FORMAT;
1563 		}
1564 
1565 		name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1566 		if (!name) {
1567 			pr_warn("failed to get section(%d) name from %s\n",
1568 				idx, obj->path);
1569 			return -LIBBPF_ERRNO__FORMAT;
1570 		}
1571 
1572 		data = elf_getdata(scn, 0);
1573 		if (!data) {
1574 			pr_warn("failed to get section(%d) data from %s(%s)\n",
1575 				idx, name, obj->path);
1576 			return -LIBBPF_ERRNO__FORMAT;
1577 		}
1578 		pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
1579 			 idx, name, (unsigned long)data->d_size,
1580 			 (int)sh.sh_link, (unsigned long)sh.sh_flags,
1581 			 (int)sh.sh_type);
1582 
1583 		if (strcmp(name, "license") == 0) {
1584 			err = bpf_object__init_license(obj,
1585 						       data->d_buf,
1586 						       data->d_size);
1587 			if (err)
1588 				return err;
1589 		} else if (strcmp(name, "version") == 0) {
1590 			err = bpf_object__init_kversion(obj,
1591 							data->d_buf,
1592 							data->d_size);
1593 			if (err)
1594 				return err;
1595 		} else if (strcmp(name, "maps") == 0) {
1596 			obj->efile.maps_shndx = idx;
1597 		} else if (strcmp(name, MAPS_ELF_SEC) == 0) {
1598 			obj->efile.btf_maps_shndx = idx;
1599 		} else if (strcmp(name, BTF_ELF_SEC) == 0) {
1600 			btf_data = data;
1601 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
1602 			btf_ext_data = data;
1603 		} else if (sh.sh_type == SHT_SYMTAB) {
1604 			if (obj->efile.symbols) {
1605 				pr_warn("bpf: multiple SYMTAB in %s\n",
1606 					obj->path);
1607 				return -LIBBPF_ERRNO__FORMAT;
1608 			}
1609 			obj->efile.symbols = data;
1610 			obj->efile.strtabidx = sh.sh_link;
1611 		} else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
1612 			if (sh.sh_flags & SHF_EXECINSTR) {
1613 				if (strcmp(name, ".text") == 0)
1614 					obj->efile.text_shndx = idx;
1615 				err = bpf_object__add_program(obj, data->d_buf,
1616 							      data->d_size, name, idx);
1617 				if (err) {
1618 					char errmsg[STRERR_BUFSIZE];
1619 					char *cp = libbpf_strerror_r(-err, errmsg,
1620 								     sizeof(errmsg));
1621 
1622 					pr_warn("failed to alloc program %s (%s): %s",
1623 						name, obj->path, cp);
1624 					return err;
1625 				}
1626 			} else if (strcmp(name, ".data") == 0) {
1627 				obj->efile.data = data;
1628 				obj->efile.data_shndx = idx;
1629 			} else if (strcmp(name, ".rodata") == 0) {
1630 				obj->efile.rodata = data;
1631 				obj->efile.rodata_shndx = idx;
1632 			} else {
1633 				pr_debug("skip section(%d) %s\n", idx, name);
1634 			}
1635 		} else if (sh.sh_type == SHT_REL) {
1636 			int nr_reloc = obj->efile.nr_reloc;
1637 			void *reloc = obj->efile.reloc;
1638 			int sec = sh.sh_info; /* points to other section */
1639 
1640 			/* Only do relo for section with exec instructions */
1641 			if (!section_have_execinstr(obj, sec)) {
1642 				pr_debug("skip relo %s(%d) for section(%d)\n",
1643 					 name, idx, sec);
1644 				continue;
1645 			}
1646 
1647 			reloc = reallocarray(reloc, nr_reloc + 1,
1648 					     sizeof(*obj->efile.reloc));
1649 			if (!reloc) {
1650 				pr_warn("realloc failed\n");
1651 				return -ENOMEM;
1652 			}
1653 
1654 			obj->efile.reloc = reloc;
1655 			obj->efile.nr_reloc++;
1656 
1657 			obj->efile.reloc[nr_reloc].shdr = sh;
1658 			obj->efile.reloc[nr_reloc].data = data;
1659 		} else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) {
1660 			obj->efile.bss = data;
1661 			obj->efile.bss_shndx = idx;
1662 		} else {
1663 			pr_debug("skip section(%d) %s\n", idx, name);
1664 		}
1665 	}
1666 
1667 	if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
1668 		pr_warn("Corrupted ELF file: index of strtab invalid\n");
1669 		return -LIBBPF_ERRNO__FORMAT;
1670 	}
1671 	err = bpf_object__init_btf(obj, btf_data, btf_ext_data);
1672 	if (!err)
1673 		err = bpf_object__init_maps(obj, relaxed_maps);
1674 	if (!err)
1675 		err = bpf_object__sanitize_and_load_btf(obj);
1676 	if (!err)
1677 		err = bpf_object__init_prog_names(obj);
1678 	return err;
1679 }
1680 
1681 static struct bpf_program *
1682 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
1683 {
1684 	struct bpf_program *prog;
1685 	size_t i;
1686 
1687 	for (i = 0; i < obj->nr_programs; i++) {
1688 		prog = &obj->programs[i];
1689 		if (prog->idx == idx)
1690 			return prog;
1691 	}
1692 	return NULL;
1693 }
1694 
1695 struct bpf_program *
1696 bpf_object__find_program_by_title(const struct bpf_object *obj,
1697 				  const char *title)
1698 {
1699 	struct bpf_program *pos;
1700 
1701 	bpf_object__for_each_program(pos, obj) {
1702 		if (pos->section_name && !strcmp(pos->section_name, title))
1703 			return pos;
1704 	}
1705 	return NULL;
1706 }
1707 
1708 static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
1709 				      int shndx)
1710 {
1711 	return shndx == obj->efile.data_shndx ||
1712 	       shndx == obj->efile.bss_shndx ||
1713 	       shndx == obj->efile.rodata_shndx;
1714 }
1715 
1716 static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
1717 				      int shndx)
1718 {
1719 	return shndx == obj->efile.maps_shndx ||
1720 	       shndx == obj->efile.btf_maps_shndx;
1721 }
1722 
1723 static bool bpf_object__relo_in_known_section(const struct bpf_object *obj,
1724 					      int shndx)
1725 {
1726 	return shndx == obj->efile.text_shndx ||
1727 	       bpf_object__shndx_is_maps(obj, shndx) ||
1728 	       bpf_object__shndx_is_data(obj, shndx);
1729 }
1730 
1731 static enum libbpf_map_type
1732 bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
1733 {
1734 	if (shndx == obj->efile.data_shndx)
1735 		return LIBBPF_MAP_DATA;
1736 	else if (shndx == obj->efile.bss_shndx)
1737 		return LIBBPF_MAP_BSS;
1738 	else if (shndx == obj->efile.rodata_shndx)
1739 		return LIBBPF_MAP_RODATA;
1740 	else
1741 		return LIBBPF_MAP_UNSPEC;
1742 }
1743 
1744 static int
1745 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
1746 			   Elf_Data *data, struct bpf_object *obj)
1747 {
1748 	Elf_Data *symbols = obj->efile.symbols;
1749 	struct bpf_map *maps = obj->maps;
1750 	size_t nr_maps = obj->nr_maps;
1751 	int i, nrels;
1752 
1753 	pr_debug("collecting relocating info for: '%s'\n", prog->section_name);
1754 	nrels = shdr->sh_size / shdr->sh_entsize;
1755 
1756 	prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
1757 	if (!prog->reloc_desc) {
1758 		pr_warn("failed to alloc memory in relocation\n");
1759 		return -ENOMEM;
1760 	}
1761 	prog->nr_reloc = nrels;
1762 
1763 	for (i = 0; i < nrels; i++) {
1764 		struct bpf_insn *insns = prog->insns;
1765 		enum libbpf_map_type type;
1766 		unsigned int insn_idx;
1767 		unsigned int shdr_idx;
1768 		const char *name;
1769 		size_t map_idx;
1770 		GElf_Sym sym;
1771 		GElf_Rel rel;
1772 
1773 		if (!gelf_getrel(data, i, &rel)) {
1774 			pr_warn("relocation: failed to get %d reloc\n", i);
1775 			return -LIBBPF_ERRNO__FORMAT;
1776 		}
1777 
1778 		if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
1779 			pr_warn("relocation: symbol %"PRIx64" not found\n",
1780 				GELF_R_SYM(rel.r_info));
1781 			return -LIBBPF_ERRNO__FORMAT;
1782 		}
1783 
1784 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1785 				  sym.st_name) ? : "<?>";
1786 
1787 		pr_debug("relo for %lld value %lld name %d (\'%s\')\n",
1788 			 (long long) (rel.r_info >> 32),
1789 			 (long long) sym.st_value, sym.st_name, name);
1790 
1791 		shdr_idx = sym.st_shndx;
1792 		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
1793 		pr_debug("relocation: insn_idx=%u, shdr_idx=%u\n",
1794 			 insn_idx, shdr_idx);
1795 
1796 		if (shdr_idx >= SHN_LORESERVE) {
1797 			pr_warn("relocation: not yet supported relo for non-static global \'%s\' variable in special section (0x%x) found in insns[%d].code 0x%x\n",
1798 				name, shdr_idx, insn_idx,
1799 				insns[insn_idx].code);
1800 			return -LIBBPF_ERRNO__RELOC;
1801 		}
1802 		if (!bpf_object__relo_in_known_section(obj, shdr_idx)) {
1803 			pr_warn("Program '%s' contains unrecognized relo data pointing to section %u\n",
1804 				prog->section_name, shdr_idx);
1805 			return -LIBBPF_ERRNO__RELOC;
1806 		}
1807 
1808 		if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
1809 			if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
1810 				pr_warn("incorrect bpf_call opcode\n");
1811 				return -LIBBPF_ERRNO__RELOC;
1812 			}
1813 			prog->reloc_desc[i].type = RELO_CALL;
1814 			prog->reloc_desc[i].insn_idx = insn_idx;
1815 			prog->reloc_desc[i].text_off = sym.st_value;
1816 			obj->has_pseudo_calls = true;
1817 			continue;
1818 		}
1819 
1820 		if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
1821 			pr_warn("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
1822 				insn_idx, insns[insn_idx].code);
1823 			return -LIBBPF_ERRNO__RELOC;
1824 		}
1825 
1826 		if (bpf_object__shndx_is_maps(obj, shdr_idx) ||
1827 		    bpf_object__shndx_is_data(obj, shdr_idx)) {
1828 			type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
1829 			if (type != LIBBPF_MAP_UNSPEC) {
1830 				if (GELF_ST_BIND(sym.st_info) == STB_GLOBAL) {
1831 					pr_warn("bpf: relocation: not yet supported relo for non-static global \'%s\' variable found in insns[%d].code 0x%x\n",
1832 						name, insn_idx, insns[insn_idx].code);
1833 					return -LIBBPF_ERRNO__RELOC;
1834 				}
1835 				if (!obj->caps.global_data) {
1836 					pr_warn("bpf: relocation: kernel does not support global \'%s\' variable access in insns[%d]\n",
1837 						name, insn_idx);
1838 					return -LIBBPF_ERRNO__RELOC;
1839 				}
1840 			}
1841 
1842 			for (map_idx = 0; map_idx < nr_maps; map_idx++) {
1843 				if (maps[map_idx].libbpf_type != type)
1844 					continue;
1845 				if (type != LIBBPF_MAP_UNSPEC ||
1846 				    (maps[map_idx].sec_idx == sym.st_shndx &&
1847 				     maps[map_idx].sec_offset == sym.st_value)) {
1848 					pr_debug("relocation: found map %zd (%s, sec_idx %d, offset %zu) for insn %u\n",
1849 						 map_idx, maps[map_idx].name,
1850 						 maps[map_idx].sec_idx,
1851 						 maps[map_idx].sec_offset,
1852 						 insn_idx);
1853 					break;
1854 				}
1855 			}
1856 
1857 			if (map_idx >= nr_maps) {
1858 				pr_warn("bpf relocation: map_idx %d larger than %d\n",
1859 					(int)map_idx, (int)nr_maps - 1);
1860 				return -LIBBPF_ERRNO__RELOC;
1861 			}
1862 
1863 			prog->reloc_desc[i].type = type != LIBBPF_MAP_UNSPEC ?
1864 						   RELO_DATA : RELO_LD64;
1865 			prog->reloc_desc[i].insn_idx = insn_idx;
1866 			prog->reloc_desc[i].map_idx = map_idx;
1867 		}
1868 	}
1869 	return 0;
1870 }
1871 
1872 static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
1873 {
1874 	struct bpf_map_def *def = &map->def;
1875 	__u32 key_type_id = 0, value_type_id = 0;
1876 	int ret;
1877 
1878 	/* if it's BTF-defined map, we don't need to search for type IDs */
1879 	if (map->sec_idx == obj->efile.btf_maps_shndx)
1880 		return 0;
1881 
1882 	if (!bpf_map__is_internal(map)) {
1883 		ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
1884 					   def->value_size, &key_type_id,
1885 					   &value_type_id);
1886 	} else {
1887 		/*
1888 		 * LLVM annotates global data differently in BTF, that is,
1889 		 * only as '.data', '.bss' or '.rodata'.
1890 		 */
1891 		ret = btf__find_by_name(obj->btf,
1892 				libbpf_type_to_btf_name[map->libbpf_type]);
1893 	}
1894 	if (ret < 0)
1895 		return ret;
1896 
1897 	map->btf_key_type_id = key_type_id;
1898 	map->btf_value_type_id = bpf_map__is_internal(map) ?
1899 				 ret : value_type_id;
1900 	return 0;
1901 }
1902 
1903 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1904 {
1905 	struct bpf_map_info info = {};
1906 	__u32 len = sizeof(info);
1907 	int new_fd, err;
1908 	char *new_name;
1909 
1910 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
1911 	if (err)
1912 		return err;
1913 
1914 	new_name = strdup(info.name);
1915 	if (!new_name)
1916 		return -errno;
1917 
1918 	new_fd = open("/", O_RDONLY | O_CLOEXEC);
1919 	if (new_fd < 0)
1920 		goto err_free_new_name;
1921 
1922 	new_fd = dup3(fd, new_fd, O_CLOEXEC);
1923 	if (new_fd < 0)
1924 		goto err_close_new_fd;
1925 
1926 	err = zclose(map->fd);
1927 	if (err)
1928 		goto err_close_new_fd;
1929 	free(map->name);
1930 
1931 	map->fd = new_fd;
1932 	map->name = new_name;
1933 	map->def.type = info.type;
1934 	map->def.key_size = info.key_size;
1935 	map->def.value_size = info.value_size;
1936 	map->def.max_entries = info.max_entries;
1937 	map->def.map_flags = info.map_flags;
1938 	map->btf_key_type_id = info.btf_key_type_id;
1939 	map->btf_value_type_id = info.btf_value_type_id;
1940 
1941 	return 0;
1942 
1943 err_close_new_fd:
1944 	close(new_fd);
1945 err_free_new_name:
1946 	free(new_name);
1947 	return -errno;
1948 }
1949 
1950 int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
1951 {
1952 	if (!map || !max_entries)
1953 		return -EINVAL;
1954 
1955 	/* If map already created, its attributes can't be changed. */
1956 	if (map->fd >= 0)
1957 		return -EBUSY;
1958 
1959 	map->def.max_entries = max_entries;
1960 
1961 	return 0;
1962 }
1963 
1964 static int
1965 bpf_object__probe_name(struct bpf_object *obj)
1966 {
1967 	struct bpf_load_program_attr attr;
1968 	char *cp, errmsg[STRERR_BUFSIZE];
1969 	struct bpf_insn insns[] = {
1970 		BPF_MOV64_IMM(BPF_REG_0, 0),
1971 		BPF_EXIT_INSN(),
1972 	};
1973 	int ret;
1974 
1975 	/* make sure basic loading works */
1976 
1977 	memset(&attr, 0, sizeof(attr));
1978 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
1979 	attr.insns = insns;
1980 	attr.insns_cnt = ARRAY_SIZE(insns);
1981 	attr.license = "GPL";
1982 
1983 	ret = bpf_load_program_xattr(&attr, NULL, 0);
1984 	if (ret < 0) {
1985 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
1986 		pr_warn("Error in %s():%s(%d). Couldn't load basic 'r0 = 0' BPF program.\n",
1987 			__func__, cp, errno);
1988 		return -errno;
1989 	}
1990 	close(ret);
1991 
1992 	/* now try the same program, but with the name */
1993 
1994 	attr.name = "test";
1995 	ret = bpf_load_program_xattr(&attr, NULL, 0);
1996 	if (ret >= 0) {
1997 		obj->caps.name = 1;
1998 		close(ret);
1999 	}
2000 
2001 	return 0;
2002 }
2003 
2004 static int
2005 bpf_object__probe_global_data(struct bpf_object *obj)
2006 {
2007 	struct bpf_load_program_attr prg_attr;
2008 	struct bpf_create_map_attr map_attr;
2009 	char *cp, errmsg[STRERR_BUFSIZE];
2010 	struct bpf_insn insns[] = {
2011 		BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
2012 		BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
2013 		BPF_MOV64_IMM(BPF_REG_0, 0),
2014 		BPF_EXIT_INSN(),
2015 	};
2016 	int ret, map;
2017 
2018 	memset(&map_attr, 0, sizeof(map_attr));
2019 	map_attr.map_type = BPF_MAP_TYPE_ARRAY;
2020 	map_attr.key_size = sizeof(int);
2021 	map_attr.value_size = 32;
2022 	map_attr.max_entries = 1;
2023 
2024 	map = bpf_create_map_xattr(&map_attr);
2025 	if (map < 0) {
2026 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2027 		pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
2028 			__func__, cp, errno);
2029 		return -errno;
2030 	}
2031 
2032 	insns[0].imm = map;
2033 
2034 	memset(&prg_attr, 0, sizeof(prg_attr));
2035 	prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
2036 	prg_attr.insns = insns;
2037 	prg_attr.insns_cnt = ARRAY_SIZE(insns);
2038 	prg_attr.license = "GPL";
2039 
2040 	ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
2041 	if (ret >= 0) {
2042 		obj->caps.global_data = 1;
2043 		close(ret);
2044 	}
2045 
2046 	close(map);
2047 	return 0;
2048 }
2049 
2050 static int bpf_object__probe_btf_func(struct bpf_object *obj)
2051 {
2052 	const char strs[] = "\0int\0x\0a";
2053 	/* void x(int a) {} */
2054 	__u32 types[] = {
2055 		/* int */
2056 		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
2057 		/* FUNC_PROTO */                                /* [2] */
2058 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
2059 		BTF_PARAM_ENC(7, 1),
2060 		/* FUNC x */                                    /* [3] */
2061 		BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
2062 	};
2063 	int btf_fd;
2064 
2065 	btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2066 				      strs, sizeof(strs));
2067 	if (btf_fd >= 0) {
2068 		obj->caps.btf_func = 1;
2069 		close(btf_fd);
2070 		return 1;
2071 	}
2072 
2073 	return 0;
2074 }
2075 
2076 static int bpf_object__probe_btf_datasec(struct bpf_object *obj)
2077 {
2078 	const char strs[] = "\0x\0.data";
2079 	/* static int a; */
2080 	__u32 types[] = {
2081 		/* int */
2082 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
2083 		/* VAR x */                                     /* [2] */
2084 		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
2085 		BTF_VAR_STATIC,
2086 		/* DATASEC val */                               /* [3] */
2087 		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
2088 		BTF_VAR_SECINFO_ENC(2, 0, 4),
2089 	};
2090 	int btf_fd;
2091 
2092 	btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
2093 				      strs, sizeof(strs));
2094 	if (btf_fd >= 0) {
2095 		obj->caps.btf_datasec = 1;
2096 		close(btf_fd);
2097 		return 1;
2098 	}
2099 
2100 	return 0;
2101 }
2102 
2103 static int
2104 bpf_object__probe_caps(struct bpf_object *obj)
2105 {
2106 	int (*probe_fn[])(struct bpf_object *obj) = {
2107 		bpf_object__probe_name,
2108 		bpf_object__probe_global_data,
2109 		bpf_object__probe_btf_func,
2110 		bpf_object__probe_btf_datasec,
2111 	};
2112 	int i, ret;
2113 
2114 	for (i = 0; i < ARRAY_SIZE(probe_fn); i++) {
2115 		ret = probe_fn[i](obj);
2116 		if (ret < 0)
2117 			pr_debug("Probe #%d failed with %d.\n", i, ret);
2118 	}
2119 
2120 	return 0;
2121 }
2122 
2123 static int
2124 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
2125 {
2126 	char *cp, errmsg[STRERR_BUFSIZE];
2127 	int err, zero = 0;
2128 	__u8 *data;
2129 
2130 	/* Nothing to do here since kernel already zero-initializes .bss map. */
2131 	if (map->libbpf_type == LIBBPF_MAP_BSS)
2132 		return 0;
2133 
2134 	data = map->libbpf_type == LIBBPF_MAP_DATA ?
2135 	       obj->sections.data : obj->sections.rodata;
2136 
2137 	err = bpf_map_update_elem(map->fd, &zero, data, 0);
2138 	/* Freeze .rodata map as read-only from syscall side. */
2139 	if (!err && map->libbpf_type == LIBBPF_MAP_RODATA) {
2140 		err = bpf_map_freeze(map->fd);
2141 		if (err) {
2142 			cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
2143 			pr_warn("Error freezing map(%s) as read-only: %s\n",
2144 				map->name, cp);
2145 			err = 0;
2146 		}
2147 	}
2148 	return err;
2149 }
2150 
2151 static int
2152 bpf_object__create_maps(struct bpf_object *obj)
2153 {
2154 	struct bpf_create_map_attr create_attr = {};
2155 	int nr_cpus = 0;
2156 	unsigned int i;
2157 	int err;
2158 
2159 	for (i = 0; i < obj->nr_maps; i++) {
2160 		struct bpf_map *map = &obj->maps[i];
2161 		struct bpf_map_def *def = &map->def;
2162 		char *cp, errmsg[STRERR_BUFSIZE];
2163 		int *pfd = &map->fd;
2164 
2165 		if (map->fd >= 0) {
2166 			pr_debug("skip map create (preset) %s: fd=%d\n",
2167 				 map->name, map->fd);
2168 			continue;
2169 		}
2170 
2171 		if (obj->caps.name)
2172 			create_attr.name = map->name;
2173 		create_attr.map_ifindex = map->map_ifindex;
2174 		create_attr.map_type = def->type;
2175 		create_attr.map_flags = def->map_flags;
2176 		create_attr.key_size = def->key_size;
2177 		create_attr.value_size = def->value_size;
2178 		if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
2179 		    !def->max_entries) {
2180 			if (!nr_cpus)
2181 				nr_cpus = libbpf_num_possible_cpus();
2182 			if (nr_cpus < 0) {
2183 				pr_warn("failed to determine number of system CPUs: %d\n",
2184 					nr_cpus);
2185 				err = nr_cpus;
2186 				goto err_out;
2187 			}
2188 			pr_debug("map '%s': setting size to %d\n",
2189 				 map->name, nr_cpus);
2190 			create_attr.max_entries = nr_cpus;
2191 		} else {
2192 			create_attr.max_entries = def->max_entries;
2193 		}
2194 		create_attr.btf_fd = 0;
2195 		create_attr.btf_key_type_id = 0;
2196 		create_attr.btf_value_type_id = 0;
2197 		if (bpf_map_type__is_map_in_map(def->type) &&
2198 		    map->inner_map_fd >= 0)
2199 			create_attr.inner_map_fd = map->inner_map_fd;
2200 
2201 		if (obj->btf && !bpf_map_find_btf_info(obj, map)) {
2202 			create_attr.btf_fd = btf__fd(obj->btf);
2203 			create_attr.btf_key_type_id = map->btf_key_type_id;
2204 			create_attr.btf_value_type_id = map->btf_value_type_id;
2205 		}
2206 
2207 		*pfd = bpf_create_map_xattr(&create_attr);
2208 		if (*pfd < 0 && (create_attr.btf_key_type_id ||
2209 				 create_attr.btf_value_type_id)) {
2210 			err = -errno;
2211 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
2212 			pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
2213 				map->name, cp, err);
2214 			create_attr.btf_fd = 0;
2215 			create_attr.btf_key_type_id = 0;
2216 			create_attr.btf_value_type_id = 0;
2217 			map->btf_key_type_id = 0;
2218 			map->btf_value_type_id = 0;
2219 			*pfd = bpf_create_map_xattr(&create_attr);
2220 		}
2221 
2222 		if (*pfd < 0) {
2223 			size_t j;
2224 
2225 			err = -errno;
2226 err_out:
2227 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
2228 			pr_warn("failed to create map (name: '%s'): %s(%d)\n",
2229 				map->name, cp, err);
2230 			for (j = 0; j < i; j++)
2231 				zclose(obj->maps[j].fd);
2232 			return err;
2233 		}
2234 
2235 		if (bpf_map__is_internal(map)) {
2236 			err = bpf_object__populate_internal_map(obj, map);
2237 			if (err < 0) {
2238 				zclose(*pfd);
2239 				goto err_out;
2240 			}
2241 		}
2242 
2243 		pr_debug("created map %s: fd=%d\n", map->name, *pfd);
2244 	}
2245 
2246 	return 0;
2247 }
2248 
2249 static int
2250 check_btf_ext_reloc_err(struct bpf_program *prog, int err,
2251 			void *btf_prog_info, const char *info_name)
2252 {
2253 	if (err != -ENOENT) {
2254 		pr_warn("Error in loading %s for sec %s.\n",
2255 			info_name, prog->section_name);
2256 		return err;
2257 	}
2258 
2259 	/* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
2260 
2261 	if (btf_prog_info) {
2262 		/*
2263 		 * Some info has already been found but has problem
2264 		 * in the last btf_ext reloc. Must have to error out.
2265 		 */
2266 		pr_warn("Error in relocating %s for sec %s.\n",
2267 			info_name, prog->section_name);
2268 		return err;
2269 	}
2270 
2271 	/* Have problem loading the very first info. Ignore the rest. */
2272 	pr_warn("Cannot find %s for main program sec %s. Ignore all %s.\n",
2273 		info_name, prog->section_name, info_name);
2274 	return 0;
2275 }
2276 
2277 static int
2278 bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
2279 			  const char *section_name,  __u32 insn_offset)
2280 {
2281 	int err;
2282 
2283 	if (!insn_offset || prog->func_info) {
2284 		/*
2285 		 * !insn_offset => main program
2286 		 *
2287 		 * For sub prog, the main program's func_info has to
2288 		 * be loaded first (i.e. prog->func_info != NULL)
2289 		 */
2290 		err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
2291 					       section_name, insn_offset,
2292 					       &prog->func_info,
2293 					       &prog->func_info_cnt);
2294 		if (err)
2295 			return check_btf_ext_reloc_err(prog, err,
2296 						       prog->func_info,
2297 						       "bpf_func_info");
2298 
2299 		prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
2300 	}
2301 
2302 	if (!insn_offset || prog->line_info) {
2303 		err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
2304 					       section_name, insn_offset,
2305 					       &prog->line_info,
2306 					       &prog->line_info_cnt);
2307 		if (err)
2308 			return check_btf_ext_reloc_err(prog, err,
2309 						       prog->line_info,
2310 						       "bpf_line_info");
2311 
2312 		prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
2313 	}
2314 
2315 	return 0;
2316 }
2317 
2318 #define BPF_CORE_SPEC_MAX_LEN 64
2319 
2320 /* represents BPF CO-RE field or array element accessor */
2321 struct bpf_core_accessor {
2322 	__u32 type_id;		/* struct/union type or array element type */
2323 	__u32 idx;		/* field index or array index */
2324 	const char *name;	/* field name or NULL for array accessor */
2325 };
2326 
2327 struct bpf_core_spec {
2328 	const struct btf *btf;
2329 	/* high-level spec: named fields and array indices only */
2330 	struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
2331 	/* high-level spec length */
2332 	int len;
2333 	/* raw, low-level spec: 1-to-1 with accessor spec string */
2334 	int raw_spec[BPF_CORE_SPEC_MAX_LEN];
2335 	/* raw spec length */
2336 	int raw_len;
2337 	/* field byte offset represented by spec */
2338 	__u32 offset;
2339 };
2340 
2341 static bool str_is_empty(const char *s)
2342 {
2343 	return !s || !s[0];
2344 }
2345 
2346 /*
2347  * Turn bpf_field_reloc into a low- and high-level spec representation,
2348  * validating correctness along the way, as well as calculating resulting
2349  * field offset (in bytes), specified by accessor string. Low-level spec
2350  * captures every single level of nestedness, including traversing anonymous
2351  * struct/union members. High-level one only captures semantically meaningful
2352  * "turning points": named fields and array indicies.
2353  * E.g., for this case:
2354  *
2355  *   struct sample {
2356  *       int __unimportant;
2357  *       struct {
2358  *           int __1;
2359  *           int __2;
2360  *           int a[7];
2361  *       };
2362  *   };
2363  *
2364  *   struct sample *s = ...;
2365  *
2366  *   int x = &s->a[3]; // access string = '0:1:2:3'
2367  *
2368  * Low-level spec has 1:1 mapping with each element of access string (it's
2369  * just a parsed access string representation): [0, 1, 2, 3].
2370  *
2371  * High-level spec will capture only 3 points:
2372  *   - intial zero-index access by pointer (&s->... is the same as &s[0]...);
2373  *   - field 'a' access (corresponds to '2' in low-level spec);
2374  *   - array element #3 access (corresponds to '3' in low-level spec).
2375  *
2376  */
2377 static int bpf_core_spec_parse(const struct btf *btf,
2378 			       __u32 type_id,
2379 			       const char *spec_str,
2380 			       struct bpf_core_spec *spec)
2381 {
2382 	int access_idx, parsed_len, i;
2383 	const struct btf_type *t;
2384 	const char *name;
2385 	__u32 id;
2386 	__s64 sz;
2387 
2388 	if (str_is_empty(spec_str) || *spec_str == ':')
2389 		return -EINVAL;
2390 
2391 	memset(spec, 0, sizeof(*spec));
2392 	spec->btf = btf;
2393 
2394 	/* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
2395 	while (*spec_str) {
2396 		if (*spec_str == ':')
2397 			++spec_str;
2398 		if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
2399 			return -EINVAL;
2400 		if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2401 			return -E2BIG;
2402 		spec_str += parsed_len;
2403 		spec->raw_spec[spec->raw_len++] = access_idx;
2404 	}
2405 
2406 	if (spec->raw_len == 0)
2407 		return -EINVAL;
2408 
2409 	/* first spec value is always reloc type array index */
2410 	t = skip_mods_and_typedefs(btf, type_id, &id);
2411 	if (!t)
2412 		return -EINVAL;
2413 
2414 	access_idx = spec->raw_spec[0];
2415 	spec->spec[0].type_id = id;
2416 	spec->spec[0].idx = access_idx;
2417 	spec->len++;
2418 
2419 	sz = btf__resolve_size(btf, id);
2420 	if (sz < 0)
2421 		return sz;
2422 	spec->offset = access_idx * sz;
2423 
2424 	for (i = 1; i < spec->raw_len; i++) {
2425 		t = skip_mods_and_typedefs(btf, id, &id);
2426 		if (!t)
2427 			return -EINVAL;
2428 
2429 		access_idx = spec->raw_spec[i];
2430 
2431 		if (btf_is_composite(t)) {
2432 			const struct btf_member *m;
2433 			__u32 offset;
2434 
2435 			if (access_idx >= btf_vlen(t))
2436 				return -EINVAL;
2437 			if (btf_member_bitfield_size(t, access_idx))
2438 				return -EINVAL;
2439 
2440 			offset = btf_member_bit_offset(t, access_idx);
2441 			if (offset % 8)
2442 				return -EINVAL;
2443 			spec->offset += offset / 8;
2444 
2445 			m = btf_members(t) + access_idx;
2446 			if (m->name_off) {
2447 				name = btf__name_by_offset(btf, m->name_off);
2448 				if (str_is_empty(name))
2449 					return -EINVAL;
2450 
2451 				spec->spec[spec->len].type_id = id;
2452 				spec->spec[spec->len].idx = access_idx;
2453 				spec->spec[spec->len].name = name;
2454 				spec->len++;
2455 			}
2456 
2457 			id = m->type;
2458 		} else if (btf_is_array(t)) {
2459 			const struct btf_array *a = btf_array(t);
2460 
2461 			t = skip_mods_and_typedefs(btf, a->type, &id);
2462 			if (!t || access_idx >= a->nelems)
2463 				return -EINVAL;
2464 
2465 			spec->spec[spec->len].type_id = id;
2466 			spec->spec[spec->len].idx = access_idx;
2467 			spec->len++;
2468 
2469 			sz = btf__resolve_size(btf, id);
2470 			if (sz < 0)
2471 				return sz;
2472 			spec->offset += access_idx * sz;
2473 		} else {
2474 			pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %d\n",
2475 				type_id, spec_str, i, id, btf_kind(t));
2476 			return -EINVAL;
2477 		}
2478 	}
2479 
2480 	return 0;
2481 }
2482 
2483 static bool bpf_core_is_flavor_sep(const char *s)
2484 {
2485 	/* check X___Y name pattern, where X and Y are not underscores */
2486 	return s[0] != '_' &&				      /* X */
2487 	       s[1] == '_' && s[2] == '_' && s[3] == '_' &&   /* ___ */
2488 	       s[4] != '_';				      /* Y */
2489 }
2490 
2491 /* Given 'some_struct_name___with_flavor' return the length of a name prefix
2492  * before last triple underscore. Struct name part after last triple
2493  * underscore is ignored by BPF CO-RE relocation during relocation matching.
2494  */
2495 static size_t bpf_core_essential_name_len(const char *name)
2496 {
2497 	size_t n = strlen(name);
2498 	int i;
2499 
2500 	for (i = n - 5; i >= 0; i--) {
2501 		if (bpf_core_is_flavor_sep(name + i))
2502 			return i + 1;
2503 	}
2504 	return n;
2505 }
2506 
2507 /* dynamically sized list of type IDs */
2508 struct ids_vec {
2509 	__u32 *data;
2510 	int len;
2511 };
2512 
2513 static void bpf_core_free_cands(struct ids_vec *cand_ids)
2514 {
2515 	free(cand_ids->data);
2516 	free(cand_ids);
2517 }
2518 
2519 static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
2520 					   __u32 local_type_id,
2521 					   const struct btf *targ_btf)
2522 {
2523 	size_t local_essent_len, targ_essent_len;
2524 	const char *local_name, *targ_name;
2525 	const struct btf_type *t;
2526 	struct ids_vec *cand_ids;
2527 	__u32 *new_ids;
2528 	int i, err, n;
2529 
2530 	t = btf__type_by_id(local_btf, local_type_id);
2531 	if (!t)
2532 		return ERR_PTR(-EINVAL);
2533 
2534 	local_name = btf__name_by_offset(local_btf, t->name_off);
2535 	if (str_is_empty(local_name))
2536 		return ERR_PTR(-EINVAL);
2537 	local_essent_len = bpf_core_essential_name_len(local_name);
2538 
2539 	cand_ids = calloc(1, sizeof(*cand_ids));
2540 	if (!cand_ids)
2541 		return ERR_PTR(-ENOMEM);
2542 
2543 	n = btf__get_nr_types(targ_btf);
2544 	for (i = 1; i <= n; i++) {
2545 		t = btf__type_by_id(targ_btf, i);
2546 		targ_name = btf__name_by_offset(targ_btf, t->name_off);
2547 		if (str_is_empty(targ_name))
2548 			continue;
2549 
2550 		targ_essent_len = bpf_core_essential_name_len(targ_name);
2551 		if (targ_essent_len != local_essent_len)
2552 			continue;
2553 
2554 		if (strncmp(local_name, targ_name, local_essent_len) == 0) {
2555 			pr_debug("[%d] %s: found candidate [%d] %s\n",
2556 				 local_type_id, local_name, i, targ_name);
2557 			new_ids = realloc(cand_ids->data, cand_ids->len + 1);
2558 			if (!new_ids) {
2559 				err = -ENOMEM;
2560 				goto err_out;
2561 			}
2562 			cand_ids->data = new_ids;
2563 			cand_ids->data[cand_ids->len++] = i;
2564 		}
2565 	}
2566 	return cand_ids;
2567 err_out:
2568 	bpf_core_free_cands(cand_ids);
2569 	return ERR_PTR(err);
2570 }
2571 
2572 /* Check two types for compatibility, skipping const/volatile/restrict and
2573  * typedefs, to ensure we are relocating offset to the compatible entities:
2574  *   - any two STRUCTs/UNIONs are compatible and can be mixed;
2575  *   - any two FWDs are compatible;
2576  *   - any two PTRs are always compatible;
2577  *   - for ENUMs, check sizes, names are ignored;
2578  *   - for INT, size and bitness should match, signedness is ignored;
2579  *   - for ARRAY, dimensionality is ignored, element types are checked for
2580  *     compatibility recursively;
2581  *   - everything else shouldn't be ever a target of relocation.
2582  * These rules are not set in stone and probably will be adjusted as we get
2583  * more experience with using BPF CO-RE relocations.
2584  */
2585 static int bpf_core_fields_are_compat(const struct btf *local_btf,
2586 				      __u32 local_id,
2587 				      const struct btf *targ_btf,
2588 				      __u32 targ_id)
2589 {
2590 	const struct btf_type *local_type, *targ_type;
2591 
2592 recur:
2593 	local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
2594 	targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
2595 	if (!local_type || !targ_type)
2596 		return -EINVAL;
2597 
2598 	if (btf_is_composite(local_type) && btf_is_composite(targ_type))
2599 		return 1;
2600 	if (btf_kind(local_type) != btf_kind(targ_type))
2601 		return 0;
2602 
2603 	switch (btf_kind(local_type)) {
2604 	case BTF_KIND_FWD:
2605 	case BTF_KIND_PTR:
2606 		return 1;
2607 	case BTF_KIND_ENUM:
2608 		return local_type->size == targ_type->size;
2609 	case BTF_KIND_INT:
2610 		return btf_int_offset(local_type) == 0 &&
2611 		       btf_int_offset(targ_type) == 0 &&
2612 		       local_type->size == targ_type->size &&
2613 		       btf_int_bits(local_type) == btf_int_bits(targ_type);
2614 	case BTF_KIND_ARRAY:
2615 		local_id = btf_array(local_type)->type;
2616 		targ_id = btf_array(targ_type)->type;
2617 		goto recur;
2618 	default:
2619 		pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
2620 			btf_kind(local_type), local_id, targ_id);
2621 		return 0;
2622 	}
2623 }
2624 
2625 /*
2626  * Given single high-level named field accessor in local type, find
2627  * corresponding high-level accessor for a target type. Along the way,
2628  * maintain low-level spec for target as well. Also keep updating target
2629  * offset.
2630  *
2631  * Searching is performed through recursive exhaustive enumeration of all
2632  * fields of a struct/union. If there are any anonymous (embedded)
2633  * structs/unions, they are recursively searched as well. If field with
2634  * desired name is found, check compatibility between local and target types,
2635  * before returning result.
2636  *
2637  * 1 is returned, if field is found.
2638  * 0 is returned if no compatible field is found.
2639  * <0 is returned on error.
2640  */
2641 static int bpf_core_match_member(const struct btf *local_btf,
2642 				 const struct bpf_core_accessor *local_acc,
2643 				 const struct btf *targ_btf,
2644 				 __u32 targ_id,
2645 				 struct bpf_core_spec *spec,
2646 				 __u32 *next_targ_id)
2647 {
2648 	const struct btf_type *local_type, *targ_type;
2649 	const struct btf_member *local_member, *m;
2650 	const char *local_name, *targ_name;
2651 	__u32 local_id;
2652 	int i, n, found;
2653 
2654 	targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
2655 	if (!targ_type)
2656 		return -EINVAL;
2657 	if (!btf_is_composite(targ_type))
2658 		return 0;
2659 
2660 	local_id = local_acc->type_id;
2661 	local_type = btf__type_by_id(local_btf, local_id);
2662 	local_member = btf_members(local_type) + local_acc->idx;
2663 	local_name = btf__name_by_offset(local_btf, local_member->name_off);
2664 
2665 	n = btf_vlen(targ_type);
2666 	m = btf_members(targ_type);
2667 	for (i = 0; i < n; i++, m++) {
2668 		__u32 offset;
2669 
2670 		/* bitfield relocations not supported */
2671 		if (btf_member_bitfield_size(targ_type, i))
2672 			continue;
2673 		offset = btf_member_bit_offset(targ_type, i);
2674 		if (offset % 8)
2675 			continue;
2676 
2677 		/* too deep struct/union/array nesting */
2678 		if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2679 			return -E2BIG;
2680 
2681 		/* speculate this member will be the good one */
2682 		spec->offset += offset / 8;
2683 		spec->raw_spec[spec->raw_len++] = i;
2684 
2685 		targ_name = btf__name_by_offset(targ_btf, m->name_off);
2686 		if (str_is_empty(targ_name)) {
2687 			/* embedded struct/union, we need to go deeper */
2688 			found = bpf_core_match_member(local_btf, local_acc,
2689 						      targ_btf, m->type,
2690 						      spec, next_targ_id);
2691 			if (found) /* either found or error */
2692 				return found;
2693 		} else if (strcmp(local_name, targ_name) == 0) {
2694 			/* matching named field */
2695 			struct bpf_core_accessor *targ_acc;
2696 
2697 			targ_acc = &spec->spec[spec->len++];
2698 			targ_acc->type_id = targ_id;
2699 			targ_acc->idx = i;
2700 			targ_acc->name = targ_name;
2701 
2702 			*next_targ_id = m->type;
2703 			found = bpf_core_fields_are_compat(local_btf,
2704 							   local_member->type,
2705 							   targ_btf, m->type);
2706 			if (!found)
2707 				spec->len--; /* pop accessor */
2708 			return found;
2709 		}
2710 		/* member turned out not to be what we looked for */
2711 		spec->offset -= offset / 8;
2712 		spec->raw_len--;
2713 	}
2714 
2715 	return 0;
2716 }
2717 
2718 /*
2719  * Try to match local spec to a target type and, if successful, produce full
2720  * target spec (high-level, low-level + offset).
2721  */
2722 static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
2723 			       const struct btf *targ_btf, __u32 targ_id,
2724 			       struct bpf_core_spec *targ_spec)
2725 {
2726 	const struct btf_type *targ_type;
2727 	const struct bpf_core_accessor *local_acc;
2728 	struct bpf_core_accessor *targ_acc;
2729 	int i, sz, matched;
2730 
2731 	memset(targ_spec, 0, sizeof(*targ_spec));
2732 	targ_spec->btf = targ_btf;
2733 
2734 	local_acc = &local_spec->spec[0];
2735 	targ_acc = &targ_spec->spec[0];
2736 
2737 	for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
2738 		targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
2739 						   &targ_id);
2740 		if (!targ_type)
2741 			return -EINVAL;
2742 
2743 		if (local_acc->name) {
2744 			matched = bpf_core_match_member(local_spec->btf,
2745 							local_acc,
2746 							targ_btf, targ_id,
2747 							targ_spec, &targ_id);
2748 			if (matched <= 0)
2749 				return matched;
2750 		} else {
2751 			/* for i=0, targ_id is already treated as array element
2752 			 * type (because it's the original struct), for others
2753 			 * we should find array element type first
2754 			 */
2755 			if (i > 0) {
2756 				const struct btf_array *a;
2757 
2758 				if (!btf_is_array(targ_type))
2759 					return 0;
2760 
2761 				a = btf_array(targ_type);
2762 				if (local_acc->idx >= a->nelems)
2763 					return 0;
2764 				if (!skip_mods_and_typedefs(targ_btf, a->type,
2765 							    &targ_id))
2766 					return -EINVAL;
2767 			}
2768 
2769 			/* too deep struct/union/array nesting */
2770 			if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
2771 				return -E2BIG;
2772 
2773 			targ_acc->type_id = targ_id;
2774 			targ_acc->idx = local_acc->idx;
2775 			targ_acc->name = NULL;
2776 			targ_spec->len++;
2777 			targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
2778 			targ_spec->raw_len++;
2779 
2780 			sz = btf__resolve_size(targ_btf, targ_id);
2781 			if (sz < 0)
2782 				return sz;
2783 			targ_spec->offset += local_acc->idx * sz;
2784 		}
2785 	}
2786 
2787 	return 1;
2788 }
2789 
2790 /*
2791  * Patch relocatable BPF instruction.
2792  *
2793  * Patched value is determined by relocation kind and target specification.
2794  * For field existence relocation target spec will be NULL if field is not
2795  * found.
2796  * Expected insn->imm value is determined using relocation kind and local
2797  * spec, and is checked before patching instruction. If actual insn->imm value
2798  * is wrong, bail out with error.
2799  *
2800  * Currently three kinds of BPF instructions are supported:
2801  * 1. rX = <imm> (assignment with immediate operand);
2802  * 2. rX += <imm> (arithmetic operations with immediate operand);
2803  */
2804 static int bpf_core_reloc_insn(struct bpf_program *prog,
2805 			       const struct bpf_field_reloc *relo,
2806 			       const struct bpf_core_spec *local_spec,
2807 			       const struct bpf_core_spec *targ_spec)
2808 {
2809 	__u32 orig_val, new_val;
2810 	struct bpf_insn *insn;
2811 	int insn_idx;
2812 	__u8 class;
2813 
2814 	if (relo->insn_off % sizeof(struct bpf_insn))
2815 		return -EINVAL;
2816 	insn_idx = relo->insn_off / sizeof(struct bpf_insn);
2817 
2818 	switch (relo->kind) {
2819 	case BPF_FIELD_BYTE_OFFSET:
2820 		orig_val = local_spec->offset;
2821 		if (targ_spec) {
2822 			new_val = targ_spec->offset;
2823 		} else {
2824 			pr_warn("prog '%s': patching insn #%d w/ failed reloc, imm %d -> %d\n",
2825 				bpf_program__title(prog, false), insn_idx,
2826 				orig_val, -1);
2827 			new_val = (__u32)-1;
2828 		}
2829 		break;
2830 	case BPF_FIELD_EXISTS:
2831 		orig_val = 1; /* can't generate EXISTS relo w/o local field */
2832 		new_val = targ_spec ? 1 : 0;
2833 		break;
2834 	default:
2835 		pr_warn("prog '%s': unknown relo %d at insn #%d'\n",
2836 			bpf_program__title(prog, false),
2837 			relo->kind, insn_idx);
2838 		return -EINVAL;
2839 	}
2840 
2841 	insn = &prog->insns[insn_idx];
2842 	class = BPF_CLASS(insn->code);
2843 
2844 	if (class == BPF_ALU || class == BPF_ALU64) {
2845 		if (BPF_SRC(insn->code) != BPF_K)
2846 			return -EINVAL;
2847 		if (insn->imm != orig_val)
2848 			return -EINVAL;
2849 		insn->imm = new_val;
2850 		pr_debug("prog '%s': patched insn #%d (ALU/ALU64) imm %d -> %d\n",
2851 			 bpf_program__title(prog, false),
2852 			 insn_idx, orig_val, new_val);
2853 	} else {
2854 		pr_warn("prog '%s': trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n",
2855 			bpf_program__title(prog, false),
2856 			insn_idx, insn->code, insn->src_reg, insn->dst_reg,
2857 			insn->off, insn->imm);
2858 		return -EINVAL;
2859 	}
2860 
2861 	return 0;
2862 }
2863 
2864 static struct btf *btf_load_raw(const char *path)
2865 {
2866 	struct btf *btf;
2867 	size_t read_cnt;
2868 	struct stat st;
2869 	void *data;
2870 	FILE *f;
2871 
2872 	if (stat(path, &st))
2873 		return ERR_PTR(-errno);
2874 
2875 	data = malloc(st.st_size);
2876 	if (!data)
2877 		return ERR_PTR(-ENOMEM);
2878 
2879 	f = fopen(path, "rb");
2880 	if (!f) {
2881 		btf = ERR_PTR(-errno);
2882 		goto cleanup;
2883 	}
2884 
2885 	read_cnt = fread(data, 1, st.st_size, f);
2886 	fclose(f);
2887 	if (read_cnt < st.st_size) {
2888 		btf = ERR_PTR(-EBADF);
2889 		goto cleanup;
2890 	}
2891 
2892 	btf = btf__new(data, read_cnt);
2893 
2894 cleanup:
2895 	free(data);
2896 	return btf;
2897 }
2898 
2899 /*
2900  * Probe few well-known locations for vmlinux kernel image and try to load BTF
2901  * data out of it to use for target BTF.
2902  */
2903 static struct btf *bpf_core_find_kernel_btf(void)
2904 {
2905 	struct {
2906 		const char *path_fmt;
2907 		bool raw_btf;
2908 	} locations[] = {
2909 		/* try canonical vmlinux BTF through sysfs first */
2910 		{ "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
2911 		/* fall back to trying to find vmlinux ELF on disk otherwise */
2912 		{ "/boot/vmlinux-%1$s" },
2913 		{ "/lib/modules/%1$s/vmlinux-%1$s" },
2914 		{ "/lib/modules/%1$s/build/vmlinux" },
2915 		{ "/usr/lib/modules/%1$s/kernel/vmlinux" },
2916 		{ "/usr/lib/debug/boot/vmlinux-%1$s" },
2917 		{ "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
2918 		{ "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
2919 	};
2920 	char path[PATH_MAX + 1];
2921 	struct utsname buf;
2922 	struct btf *btf;
2923 	int i;
2924 
2925 	uname(&buf);
2926 
2927 	for (i = 0; i < ARRAY_SIZE(locations); i++) {
2928 		snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
2929 
2930 		if (access(path, R_OK))
2931 			continue;
2932 
2933 		if (locations[i].raw_btf)
2934 			btf = btf_load_raw(path);
2935 		else
2936 			btf = btf__parse_elf(path, NULL);
2937 
2938 		pr_debug("loading kernel BTF '%s': %ld\n",
2939 			 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
2940 		if (IS_ERR(btf))
2941 			continue;
2942 
2943 		return btf;
2944 	}
2945 
2946 	pr_warn("failed to find valid kernel BTF\n");
2947 	return ERR_PTR(-ESRCH);
2948 }
2949 
2950 /* Output spec definition in the format:
2951  * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
2952  * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
2953  */
2954 static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
2955 {
2956 	const struct btf_type *t;
2957 	const char *s;
2958 	__u32 type_id;
2959 	int i;
2960 
2961 	type_id = spec->spec[0].type_id;
2962 	t = btf__type_by_id(spec->btf, type_id);
2963 	s = btf__name_by_offset(spec->btf, t->name_off);
2964 	libbpf_print(level, "[%u] %s + ", type_id, s);
2965 
2966 	for (i = 0; i < spec->raw_len; i++)
2967 		libbpf_print(level, "%d%s", spec->raw_spec[i],
2968 			     i == spec->raw_len - 1 ? " => " : ":");
2969 
2970 	libbpf_print(level, "%u @ &x", spec->offset);
2971 
2972 	for (i = 0; i < spec->len; i++) {
2973 		if (spec->spec[i].name)
2974 			libbpf_print(level, ".%s", spec->spec[i].name);
2975 		else
2976 			libbpf_print(level, "[%u]", spec->spec[i].idx);
2977 	}
2978 
2979 }
2980 
2981 static size_t bpf_core_hash_fn(const void *key, void *ctx)
2982 {
2983 	return (size_t)key;
2984 }
2985 
2986 static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
2987 {
2988 	return k1 == k2;
2989 }
2990 
2991 static void *u32_as_hash_key(__u32 x)
2992 {
2993 	return (void *)(uintptr_t)x;
2994 }
2995 
2996 /*
2997  * CO-RE relocate single instruction.
2998  *
2999  * The outline and important points of the algorithm:
3000  * 1. For given local type, find corresponding candidate target types.
3001  *    Candidate type is a type with the same "essential" name, ignoring
3002  *    everything after last triple underscore (___). E.g., `sample`,
3003  *    `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
3004  *    for each other. Names with triple underscore are referred to as
3005  *    "flavors" and are useful, among other things, to allow to
3006  *    specify/support incompatible variations of the same kernel struct, which
3007  *    might differ between different kernel versions and/or build
3008  *    configurations.
3009  *
3010  *    N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
3011  *    converter, when deduplicated BTF of a kernel still contains more than
3012  *    one different types with the same name. In that case, ___2, ___3, etc
3013  *    are appended starting from second name conflict. But start flavors are
3014  *    also useful to be defined "locally", in BPF program, to extract same
3015  *    data from incompatible changes between different kernel
3016  *    versions/configurations. For instance, to handle field renames between
3017  *    kernel versions, one can use two flavors of the struct name with the
3018  *    same common name and use conditional relocations to extract that field,
3019  *    depending on target kernel version.
3020  * 2. For each candidate type, try to match local specification to this
3021  *    candidate target type. Matching involves finding corresponding
3022  *    high-level spec accessors, meaning that all named fields should match,
3023  *    as well as all array accesses should be within the actual bounds. Also,
3024  *    types should be compatible (see bpf_core_fields_are_compat for details).
3025  * 3. It is supported and expected that there might be multiple flavors
3026  *    matching the spec. As long as all the specs resolve to the same set of
3027  *    offsets across all candidates, there is no error. If there is any
3028  *    ambiguity, CO-RE relocation will fail. This is necessary to accomodate
3029  *    imprefection of BTF deduplication, which can cause slight duplication of
3030  *    the same BTF type, if some directly or indirectly referenced (by
3031  *    pointer) type gets resolved to different actual types in different
3032  *    object files. If such situation occurs, deduplicated BTF will end up
3033  *    with two (or more) structurally identical types, which differ only in
3034  *    types they refer to through pointer. This should be OK in most cases and
3035  *    is not an error.
3036  * 4. Candidate types search is performed by linearly scanning through all
3037  *    types in target BTF. It is anticipated that this is overall more
3038  *    efficient memory-wise and not significantly worse (if not better)
3039  *    CPU-wise compared to prebuilding a map from all local type names to
3040  *    a list of candidate type names. It's also sped up by caching resolved
3041  *    list of matching candidates per each local "root" type ID, that has at
3042  *    least one bpf_field_reloc associated with it. This list is shared
3043  *    between multiple relocations for the same type ID and is updated as some
3044  *    of the candidates are pruned due to structural incompatibility.
3045  */
3046 static int bpf_core_reloc_field(struct bpf_program *prog,
3047 				 const struct bpf_field_reloc *relo,
3048 				 int relo_idx,
3049 				 const struct btf *local_btf,
3050 				 const struct btf *targ_btf,
3051 				 struct hashmap *cand_cache)
3052 {
3053 	const char *prog_name = bpf_program__title(prog, false);
3054 	struct bpf_core_spec local_spec, cand_spec, targ_spec;
3055 	const void *type_key = u32_as_hash_key(relo->type_id);
3056 	const struct btf_type *local_type, *cand_type;
3057 	const char *local_name, *cand_name;
3058 	struct ids_vec *cand_ids;
3059 	__u32 local_id, cand_id;
3060 	const char *spec_str;
3061 	int i, j, err;
3062 
3063 	local_id = relo->type_id;
3064 	local_type = btf__type_by_id(local_btf, local_id);
3065 	if (!local_type)
3066 		return -EINVAL;
3067 
3068 	local_name = btf__name_by_offset(local_btf, local_type->name_off);
3069 	if (str_is_empty(local_name))
3070 		return -EINVAL;
3071 
3072 	spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
3073 	if (str_is_empty(spec_str))
3074 		return -EINVAL;
3075 
3076 	err = bpf_core_spec_parse(local_btf, local_id, spec_str, &local_spec);
3077 	if (err) {
3078 		pr_warn("prog '%s': relo #%d: parsing [%d] %s + %s failed: %d\n",
3079 			prog_name, relo_idx, local_id, local_name, spec_str,
3080 			err);
3081 		return -EINVAL;
3082 	}
3083 
3084 	pr_debug("prog '%s': relo #%d: spec is ", prog_name, relo_idx);
3085 	bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec);
3086 	libbpf_print(LIBBPF_DEBUG, "\n");
3087 
3088 	if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) {
3089 		cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf);
3090 		if (IS_ERR(cand_ids)) {
3091 			pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s: %ld",
3092 				prog_name, relo_idx, local_id, local_name,
3093 				PTR_ERR(cand_ids));
3094 			return PTR_ERR(cand_ids);
3095 		}
3096 		err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL);
3097 		if (err) {
3098 			bpf_core_free_cands(cand_ids);
3099 			return err;
3100 		}
3101 	}
3102 
3103 	for (i = 0, j = 0; i < cand_ids->len; i++) {
3104 		cand_id = cand_ids->data[i];
3105 		cand_type = btf__type_by_id(targ_btf, cand_id);
3106 		cand_name = btf__name_by_offset(targ_btf, cand_type->name_off);
3107 
3108 		err = bpf_core_spec_match(&local_spec, targ_btf,
3109 					  cand_id, &cand_spec);
3110 		pr_debug("prog '%s': relo #%d: matching candidate #%d %s against spec ",
3111 			 prog_name, relo_idx, i, cand_name);
3112 		bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec);
3113 		libbpf_print(LIBBPF_DEBUG, ": %d\n", err);
3114 		if (err < 0) {
3115 			pr_warn("prog '%s': relo #%d: matching error: %d\n",
3116 				prog_name, relo_idx, err);
3117 			return err;
3118 		}
3119 		if (err == 0)
3120 			continue;
3121 
3122 		if (j == 0) {
3123 			targ_spec = cand_spec;
3124 		} else if (cand_spec.offset != targ_spec.offset) {
3125 			/* if there are many candidates, they should all
3126 			 * resolve to the same offset
3127 			 */
3128 			pr_warn("prog '%s': relo #%d: offset ambiguity: %u != %u\n",
3129 				prog_name, relo_idx, cand_spec.offset,
3130 				targ_spec.offset);
3131 			return -EINVAL;
3132 		}
3133 
3134 		cand_ids->data[j++] = cand_spec.spec[0].type_id;
3135 	}
3136 
3137 	/*
3138 	 * For BPF_FIELD_EXISTS relo or when relaxed CO-RE reloc mode is
3139 	 * requested, it's expected that we might not find any candidates.
3140 	 * In this case, if field wasn't found in any candidate, the list of
3141 	 * candidates shouldn't change at all, we'll just handle relocating
3142 	 * appropriately, depending on relo's kind.
3143 	 */
3144 	if (j > 0)
3145 		cand_ids->len = j;
3146 
3147 	if (j == 0 && !prog->obj->relaxed_core_relocs &&
3148 	    relo->kind != BPF_FIELD_EXISTS) {
3149 		pr_warn("prog '%s': relo #%d: no matching targets found for [%d] %s + %s\n",
3150 			prog_name, relo_idx, local_id, local_name, spec_str);
3151 		return -ESRCH;
3152 	}
3153 
3154 	/* bpf_core_reloc_insn should know how to handle missing targ_spec */
3155 	err = bpf_core_reloc_insn(prog, relo, &local_spec,
3156 				  j ? &targ_spec : NULL);
3157 	if (err) {
3158 		pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
3159 			prog_name, relo_idx, relo->insn_off, err);
3160 		return -EINVAL;
3161 	}
3162 
3163 	return 0;
3164 }
3165 
3166 static int
3167 bpf_core_reloc_fields(struct bpf_object *obj, const char *targ_btf_path)
3168 {
3169 	const struct btf_ext_info_sec *sec;
3170 	const struct bpf_field_reloc *rec;
3171 	const struct btf_ext_info *seg;
3172 	struct hashmap_entry *entry;
3173 	struct hashmap *cand_cache = NULL;
3174 	struct bpf_program *prog;
3175 	struct btf *targ_btf;
3176 	const char *sec_name;
3177 	int i, err = 0;
3178 
3179 	if (targ_btf_path)
3180 		targ_btf = btf__parse_elf(targ_btf_path, NULL);
3181 	else
3182 		targ_btf = bpf_core_find_kernel_btf();
3183 	if (IS_ERR(targ_btf)) {
3184 		pr_warn("failed to get target BTF: %ld\n", PTR_ERR(targ_btf));
3185 		return PTR_ERR(targ_btf);
3186 	}
3187 
3188 	cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
3189 	if (IS_ERR(cand_cache)) {
3190 		err = PTR_ERR(cand_cache);
3191 		goto out;
3192 	}
3193 
3194 	seg = &obj->btf_ext->field_reloc_info;
3195 	for_each_btf_ext_sec(seg, sec) {
3196 		sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
3197 		if (str_is_empty(sec_name)) {
3198 			err = -EINVAL;
3199 			goto out;
3200 		}
3201 		prog = bpf_object__find_program_by_title(obj, sec_name);
3202 		if (!prog) {
3203 			pr_warn("failed to find program '%s' for CO-RE offset relocation\n",
3204 				sec_name);
3205 			err = -EINVAL;
3206 			goto out;
3207 		}
3208 
3209 		pr_debug("prog '%s': performing %d CO-RE offset relocs\n",
3210 			 sec_name, sec->num_info);
3211 
3212 		for_each_btf_ext_rec(seg, sec, i, rec) {
3213 			err = bpf_core_reloc_field(prog, rec, i, obj->btf,
3214 						   targ_btf, cand_cache);
3215 			if (err) {
3216 				pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
3217 					sec_name, i, err);
3218 				goto out;
3219 			}
3220 		}
3221 	}
3222 
3223 out:
3224 	btf__free(targ_btf);
3225 	if (!IS_ERR_OR_NULL(cand_cache)) {
3226 		hashmap__for_each_entry(cand_cache, entry, i) {
3227 			bpf_core_free_cands(entry->value);
3228 		}
3229 		hashmap__free(cand_cache);
3230 	}
3231 	return err;
3232 }
3233 
3234 static int
3235 bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
3236 {
3237 	int err = 0;
3238 
3239 	if (obj->btf_ext->field_reloc_info.len)
3240 		err = bpf_core_reloc_fields(obj, targ_btf_path);
3241 
3242 	return err;
3243 }
3244 
3245 static int
3246 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
3247 			struct reloc_desc *relo)
3248 {
3249 	struct bpf_insn *insn, *new_insn;
3250 	struct bpf_program *text;
3251 	size_t new_cnt;
3252 	int err;
3253 
3254 	if (relo->type != RELO_CALL)
3255 		return -LIBBPF_ERRNO__RELOC;
3256 
3257 	if (prog->idx == obj->efile.text_shndx) {
3258 		pr_warn("relo in .text insn %d into off %d\n",
3259 			relo->insn_idx, relo->text_off);
3260 		return -LIBBPF_ERRNO__RELOC;
3261 	}
3262 
3263 	if (prog->main_prog_cnt == 0) {
3264 		text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
3265 		if (!text) {
3266 			pr_warn("no .text section found yet relo into text exist\n");
3267 			return -LIBBPF_ERRNO__RELOC;
3268 		}
3269 		new_cnt = prog->insns_cnt + text->insns_cnt;
3270 		new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
3271 		if (!new_insn) {
3272 			pr_warn("oom in prog realloc\n");
3273 			return -ENOMEM;
3274 		}
3275 
3276 		if (obj->btf_ext) {
3277 			err = bpf_program_reloc_btf_ext(prog, obj,
3278 							text->section_name,
3279 							prog->insns_cnt);
3280 			if (err)
3281 				return err;
3282 		}
3283 
3284 		memcpy(new_insn + prog->insns_cnt, text->insns,
3285 		       text->insns_cnt * sizeof(*insn));
3286 		prog->insns = new_insn;
3287 		prog->main_prog_cnt = prog->insns_cnt;
3288 		prog->insns_cnt = new_cnt;
3289 		pr_debug("added %zd insn from %s to prog %s\n",
3290 			 text->insns_cnt, text->section_name,
3291 			 prog->section_name);
3292 	}
3293 	insn = &prog->insns[relo->insn_idx];
3294 	insn->imm += prog->main_prog_cnt - relo->insn_idx;
3295 	return 0;
3296 }
3297 
3298 static int
3299 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
3300 {
3301 	int i, err;
3302 
3303 	if (!prog)
3304 		return 0;
3305 
3306 	if (obj->btf_ext) {
3307 		err = bpf_program_reloc_btf_ext(prog, obj,
3308 						prog->section_name, 0);
3309 		if (err)
3310 			return err;
3311 	}
3312 
3313 	if (!prog->reloc_desc)
3314 		return 0;
3315 
3316 	for (i = 0; i < prog->nr_reloc; i++) {
3317 		if (prog->reloc_desc[i].type == RELO_LD64 ||
3318 		    prog->reloc_desc[i].type == RELO_DATA) {
3319 			bool relo_data = prog->reloc_desc[i].type == RELO_DATA;
3320 			struct bpf_insn *insns = prog->insns;
3321 			int insn_idx, map_idx;
3322 
3323 			insn_idx = prog->reloc_desc[i].insn_idx;
3324 			map_idx = prog->reloc_desc[i].map_idx;
3325 
3326 			if (insn_idx + 1 >= (int)prog->insns_cnt) {
3327 				pr_warn("relocation out of range: '%s'\n",
3328 					prog->section_name);
3329 				return -LIBBPF_ERRNO__RELOC;
3330 			}
3331 
3332 			if (!relo_data) {
3333 				insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
3334 			} else {
3335 				insns[insn_idx].src_reg = BPF_PSEUDO_MAP_VALUE;
3336 				insns[insn_idx + 1].imm = insns[insn_idx].imm;
3337 			}
3338 			insns[insn_idx].imm = obj->maps[map_idx].fd;
3339 		} else if (prog->reloc_desc[i].type == RELO_CALL) {
3340 			err = bpf_program__reloc_text(prog, obj,
3341 						      &prog->reloc_desc[i]);
3342 			if (err)
3343 				return err;
3344 		}
3345 	}
3346 
3347 	zfree(&prog->reloc_desc);
3348 	prog->nr_reloc = 0;
3349 	return 0;
3350 }
3351 
3352 static int
3353 bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
3354 {
3355 	struct bpf_program *prog;
3356 	size_t i;
3357 	int err;
3358 
3359 	if (obj->btf_ext) {
3360 		err = bpf_object__relocate_core(obj, targ_btf_path);
3361 		if (err) {
3362 			pr_warn("failed to perform CO-RE relocations: %d\n",
3363 				err);
3364 			return err;
3365 		}
3366 	}
3367 	for (i = 0; i < obj->nr_programs; i++) {
3368 		prog = &obj->programs[i];
3369 
3370 		err = bpf_program__relocate(prog, obj);
3371 		if (err) {
3372 			pr_warn("failed to relocate '%s'\n", prog->section_name);
3373 			return err;
3374 		}
3375 	}
3376 	return 0;
3377 }
3378 
3379 static int bpf_object__collect_reloc(struct bpf_object *obj)
3380 {
3381 	int i, err;
3382 
3383 	if (!obj_elf_valid(obj)) {
3384 		pr_warn("Internal error: elf object is closed\n");
3385 		return -LIBBPF_ERRNO__INTERNAL;
3386 	}
3387 
3388 	for (i = 0; i < obj->efile.nr_reloc; i++) {
3389 		GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
3390 		Elf_Data *data = obj->efile.reloc[i].data;
3391 		int idx = shdr->sh_info;
3392 		struct bpf_program *prog;
3393 
3394 		if (shdr->sh_type != SHT_REL) {
3395 			pr_warn("internal error at %d\n", __LINE__);
3396 			return -LIBBPF_ERRNO__INTERNAL;
3397 		}
3398 
3399 		prog = bpf_object__find_prog_by_idx(obj, idx);
3400 		if (!prog) {
3401 			pr_warn("relocation failed: no section(%d)\n", idx);
3402 			return -LIBBPF_ERRNO__RELOC;
3403 		}
3404 
3405 		err = bpf_program__collect_reloc(prog, shdr, data, obj);
3406 		if (err)
3407 			return err;
3408 	}
3409 	return 0;
3410 }
3411 
3412 static int
3413 load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
3414 	     char *license, __u32 kern_version, int *pfd)
3415 {
3416 	struct bpf_load_program_attr load_attr;
3417 	char *cp, errmsg[STRERR_BUFSIZE];
3418 	int log_buf_size = BPF_LOG_BUF_SIZE;
3419 	char *log_buf;
3420 	int btf_fd, ret;
3421 
3422 	if (!insns || !insns_cnt)
3423 		return -EINVAL;
3424 
3425 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
3426 	load_attr.prog_type = prog->type;
3427 	load_attr.expected_attach_type = prog->expected_attach_type;
3428 	if (prog->caps->name)
3429 		load_attr.name = prog->name;
3430 	load_attr.insns = insns;
3431 	load_attr.insns_cnt = insns_cnt;
3432 	load_attr.license = license;
3433 	load_attr.kern_version = kern_version;
3434 	load_attr.prog_ifindex = prog->prog_ifindex;
3435 	/* if .BTF.ext was loaded, kernel supports associated BTF for prog */
3436 	if (prog->obj->btf_ext)
3437 		btf_fd = bpf_object__btf_fd(prog->obj);
3438 	else
3439 		btf_fd = -1;
3440 	load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0;
3441 	load_attr.func_info = prog->func_info;
3442 	load_attr.func_info_rec_size = prog->func_info_rec_size;
3443 	load_attr.func_info_cnt = prog->func_info_cnt;
3444 	load_attr.line_info = prog->line_info;
3445 	load_attr.line_info_rec_size = prog->line_info_rec_size;
3446 	load_attr.line_info_cnt = prog->line_info_cnt;
3447 	load_attr.log_level = prog->log_level;
3448 	load_attr.prog_flags = prog->prog_flags;
3449 
3450 retry_load:
3451 	log_buf = malloc(log_buf_size);
3452 	if (!log_buf)
3453 		pr_warn("Alloc log buffer for bpf loader error, continue without log\n");
3454 
3455 	ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
3456 
3457 	if (ret >= 0) {
3458 		if (load_attr.log_level)
3459 			pr_debug("verifier log:\n%s", log_buf);
3460 		*pfd = ret;
3461 		ret = 0;
3462 		goto out;
3463 	}
3464 
3465 	if (errno == ENOSPC) {
3466 		log_buf_size <<= 1;
3467 		free(log_buf);
3468 		goto retry_load;
3469 	}
3470 	ret = -LIBBPF_ERRNO__LOAD;
3471 	cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
3472 	pr_warn("load bpf program failed: %s\n", cp);
3473 
3474 	if (log_buf && log_buf[0] != '\0') {
3475 		ret = -LIBBPF_ERRNO__VERIFY;
3476 		pr_warn("-- BEGIN DUMP LOG ---\n");
3477 		pr_warn("\n%s\n", log_buf);
3478 		pr_warn("-- END LOG --\n");
3479 	} else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
3480 		pr_warn("Program too large (%zu insns), at most %d insns\n",
3481 			load_attr.insns_cnt, BPF_MAXINSNS);
3482 		ret = -LIBBPF_ERRNO__PROG2BIG;
3483 	} else {
3484 		/* Wrong program type? */
3485 		if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
3486 			int fd;
3487 
3488 			load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
3489 			load_attr.expected_attach_type = 0;
3490 			fd = bpf_load_program_xattr(&load_attr, NULL, 0);
3491 			if (fd >= 0) {
3492 				close(fd);
3493 				ret = -LIBBPF_ERRNO__PROGTYPE;
3494 				goto out;
3495 			}
3496 		}
3497 
3498 		if (log_buf)
3499 			ret = -LIBBPF_ERRNO__KVER;
3500 	}
3501 
3502 out:
3503 	free(log_buf);
3504 	return ret;
3505 }
3506 
3507 int
3508 bpf_program__load(struct bpf_program *prog,
3509 		  char *license, __u32 kern_version)
3510 {
3511 	int err = 0, fd, i;
3512 
3513 	if (prog->instances.nr < 0 || !prog->instances.fds) {
3514 		if (prog->preprocessor) {
3515 			pr_warn("Internal error: can't load program '%s'\n",
3516 				prog->section_name);
3517 			return -LIBBPF_ERRNO__INTERNAL;
3518 		}
3519 
3520 		prog->instances.fds = malloc(sizeof(int));
3521 		if (!prog->instances.fds) {
3522 			pr_warn("Not enough memory for BPF fds\n");
3523 			return -ENOMEM;
3524 		}
3525 		prog->instances.nr = 1;
3526 		prog->instances.fds[0] = -1;
3527 	}
3528 
3529 	if (!prog->preprocessor) {
3530 		if (prog->instances.nr != 1) {
3531 			pr_warn("Program '%s' is inconsistent: nr(%d) != 1\n",
3532 				prog->section_name, prog->instances.nr);
3533 		}
3534 		err = load_program(prog, prog->insns, prog->insns_cnt,
3535 				   license, kern_version, &fd);
3536 		if (!err)
3537 			prog->instances.fds[0] = fd;
3538 		goto out;
3539 	}
3540 
3541 	for (i = 0; i < prog->instances.nr; i++) {
3542 		struct bpf_prog_prep_result result;
3543 		bpf_program_prep_t preprocessor = prog->preprocessor;
3544 
3545 		memset(&result, 0, sizeof(result));
3546 		err = preprocessor(prog, i, prog->insns,
3547 				   prog->insns_cnt, &result);
3548 		if (err) {
3549 			pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
3550 				i, prog->section_name);
3551 			goto out;
3552 		}
3553 
3554 		if (!result.new_insn_ptr || !result.new_insn_cnt) {
3555 			pr_debug("Skip loading the %dth instance of program '%s'\n",
3556 				 i, prog->section_name);
3557 			prog->instances.fds[i] = -1;
3558 			if (result.pfd)
3559 				*result.pfd = -1;
3560 			continue;
3561 		}
3562 
3563 		err = load_program(prog, result.new_insn_ptr,
3564 				   result.new_insn_cnt,
3565 				   license, kern_version, &fd);
3566 
3567 		if (err) {
3568 			pr_warn("Loading the %dth instance of program '%s' failed\n",
3569 				i, prog->section_name);
3570 			goto out;
3571 		}
3572 
3573 		if (result.pfd)
3574 			*result.pfd = fd;
3575 		prog->instances.fds[i] = fd;
3576 	}
3577 out:
3578 	if (err)
3579 		pr_warn("failed to load program '%s'\n", prog->section_name);
3580 	zfree(&prog->insns);
3581 	prog->insns_cnt = 0;
3582 	return err;
3583 }
3584 
3585 static bool bpf_program__is_function_storage(const struct bpf_program *prog,
3586 					     const struct bpf_object *obj)
3587 {
3588 	return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
3589 }
3590 
3591 static int
3592 bpf_object__load_progs(struct bpf_object *obj, int log_level)
3593 {
3594 	size_t i;
3595 	int err;
3596 
3597 	for (i = 0; i < obj->nr_programs; i++) {
3598 		if (bpf_program__is_function_storage(&obj->programs[i], obj))
3599 			continue;
3600 		obj->programs[i].log_level |= log_level;
3601 		err = bpf_program__load(&obj->programs[i],
3602 					obj->license,
3603 					obj->kern_version);
3604 		if (err)
3605 			return err;
3606 	}
3607 	return 0;
3608 }
3609 
3610 static struct bpf_object *
3611 __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
3612 		   struct bpf_object_open_opts *opts)
3613 {
3614 	struct bpf_program *prog;
3615 	struct bpf_object *obj;
3616 	const char *obj_name;
3617 	char tmp_name[64];
3618 	bool relaxed_maps;
3619 	int err;
3620 
3621 	if (elf_version(EV_CURRENT) == EV_NONE) {
3622 		pr_warn("failed to init libelf for %s\n",
3623 			path ? : "(mem buf)");
3624 		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
3625 	}
3626 
3627 	if (!OPTS_VALID(opts, bpf_object_open_opts))
3628 		return ERR_PTR(-EINVAL);
3629 
3630 	obj_name = OPTS_GET(opts, object_name, path);
3631 	if (obj_buf) {
3632 		if (!obj_name) {
3633 			snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
3634 				 (unsigned long)obj_buf,
3635 				 (unsigned long)obj_buf_sz);
3636 			obj_name = tmp_name;
3637 		}
3638 		path = obj_name;
3639 		pr_debug("loading object '%s' from buffer\n", obj_name);
3640 	}
3641 
3642 	obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
3643 	if (IS_ERR(obj))
3644 		return obj;
3645 
3646 	obj->relaxed_core_relocs = OPTS_GET(opts, relaxed_core_relocs, false);
3647 	relaxed_maps = OPTS_GET(opts, relaxed_maps, false);
3648 
3649 	CHECK_ERR(bpf_object__elf_init(obj), err, out);
3650 	CHECK_ERR(bpf_object__check_endianness(obj), err, out);
3651 	CHECK_ERR(bpf_object__probe_caps(obj), err, out);
3652 	CHECK_ERR(bpf_object__elf_collect(obj, relaxed_maps), err, out);
3653 	CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
3654 	bpf_object__elf_finish(obj);
3655 
3656 	bpf_object__for_each_program(prog, obj) {
3657 		enum bpf_prog_type prog_type;
3658 		enum bpf_attach_type attach_type;
3659 
3660 		err = libbpf_prog_type_by_name(prog->section_name, &prog_type,
3661 					       &attach_type);
3662 		if (err == -ESRCH)
3663 			/* couldn't guess, but user might manually specify */
3664 			continue;
3665 		if (err)
3666 			goto out;
3667 
3668 		bpf_program__set_type(prog, prog_type);
3669 		bpf_program__set_expected_attach_type(prog, attach_type);
3670 	}
3671 
3672 	return obj;
3673 out:
3674 	bpf_object__close(obj);
3675 	return ERR_PTR(err);
3676 }
3677 
3678 static struct bpf_object *
3679 __bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags)
3680 {
3681 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
3682 		.relaxed_maps = flags & MAPS_RELAX_COMPAT,
3683 	);
3684 
3685 	/* param validation */
3686 	if (!attr->file)
3687 		return NULL;
3688 
3689 	pr_debug("loading %s\n", attr->file);
3690 	return __bpf_object__open(attr->file, NULL, 0, &opts);
3691 }
3692 
3693 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
3694 {
3695 	return __bpf_object__open_xattr(attr, 0);
3696 }
3697 
3698 struct bpf_object *bpf_object__open(const char *path)
3699 {
3700 	struct bpf_object_open_attr attr = {
3701 		.file		= path,
3702 		.prog_type	= BPF_PROG_TYPE_UNSPEC,
3703 	};
3704 
3705 	return bpf_object__open_xattr(&attr);
3706 }
3707 
3708 struct bpf_object *
3709 bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)
3710 {
3711 	if (!path)
3712 		return ERR_PTR(-EINVAL);
3713 
3714 	pr_debug("loading %s\n", path);
3715 
3716 	return __bpf_object__open(path, NULL, 0, opts);
3717 }
3718 
3719 struct bpf_object *
3720 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
3721 		     struct bpf_object_open_opts *opts)
3722 {
3723 	if (!obj_buf || obj_buf_sz == 0)
3724 		return ERR_PTR(-EINVAL);
3725 
3726 	return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts);
3727 }
3728 
3729 struct bpf_object *
3730 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
3731 			const char *name)
3732 {
3733 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
3734 		.object_name = name,
3735 		/* wrong default, but backwards-compatible */
3736 		.relaxed_maps = true,
3737 	);
3738 
3739 	/* returning NULL is wrong, but backwards-compatible */
3740 	if (!obj_buf || obj_buf_sz == 0)
3741 		return NULL;
3742 
3743 	return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
3744 }
3745 
3746 int bpf_object__unload(struct bpf_object *obj)
3747 {
3748 	size_t i;
3749 
3750 	if (!obj)
3751 		return -EINVAL;
3752 
3753 	for (i = 0; i < obj->nr_maps; i++)
3754 		zclose(obj->maps[i].fd);
3755 
3756 	for (i = 0; i < obj->nr_programs; i++)
3757 		bpf_program__unload(&obj->programs[i]);
3758 
3759 	return 0;
3760 }
3761 
3762 int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
3763 {
3764 	struct bpf_object *obj;
3765 	int err;
3766 
3767 	if (!attr)
3768 		return -EINVAL;
3769 	obj = attr->obj;
3770 	if (!obj)
3771 		return -EINVAL;
3772 
3773 	if (obj->loaded) {
3774 		pr_warn("object should not be loaded twice\n");
3775 		return -EINVAL;
3776 	}
3777 
3778 	obj->loaded = true;
3779 
3780 	CHECK_ERR(bpf_object__create_maps(obj), err, out);
3781 	CHECK_ERR(bpf_object__relocate(obj, attr->target_btf_path), err, out);
3782 	CHECK_ERR(bpf_object__load_progs(obj, attr->log_level), err, out);
3783 
3784 	return 0;
3785 out:
3786 	bpf_object__unload(obj);
3787 	pr_warn("failed to load object '%s'\n", obj->path);
3788 	return err;
3789 }
3790 
3791 int bpf_object__load(struct bpf_object *obj)
3792 {
3793 	struct bpf_object_load_attr attr = {
3794 		.obj = obj,
3795 	};
3796 
3797 	return bpf_object__load_xattr(&attr);
3798 }
3799 
3800 static int check_path(const char *path)
3801 {
3802 	char *cp, errmsg[STRERR_BUFSIZE];
3803 	struct statfs st_fs;
3804 	char *dname, *dir;
3805 	int err = 0;
3806 
3807 	if (path == NULL)
3808 		return -EINVAL;
3809 
3810 	dname = strdup(path);
3811 	if (dname == NULL)
3812 		return -ENOMEM;
3813 
3814 	dir = dirname(dname);
3815 	if (statfs(dir, &st_fs)) {
3816 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
3817 		pr_warn("failed to statfs %s: %s\n", dir, cp);
3818 		err = -errno;
3819 	}
3820 	free(dname);
3821 
3822 	if (!err && st_fs.f_type != BPF_FS_MAGIC) {
3823 		pr_warn("specified path %s is not on BPF FS\n", path);
3824 		err = -EINVAL;
3825 	}
3826 
3827 	return err;
3828 }
3829 
3830 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
3831 			      int instance)
3832 {
3833 	char *cp, errmsg[STRERR_BUFSIZE];
3834 	int err;
3835 
3836 	err = check_path(path);
3837 	if (err)
3838 		return err;
3839 
3840 	if (prog == NULL) {
3841 		pr_warn("invalid program pointer\n");
3842 		return -EINVAL;
3843 	}
3844 
3845 	if (instance < 0 || instance >= prog->instances.nr) {
3846 		pr_warn("invalid prog instance %d of prog %s (max %d)\n",
3847 			instance, prog->section_name, prog->instances.nr);
3848 		return -EINVAL;
3849 	}
3850 
3851 	if (bpf_obj_pin(prog->instances.fds[instance], path)) {
3852 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
3853 		pr_warn("failed to pin program: %s\n", cp);
3854 		return -errno;
3855 	}
3856 	pr_debug("pinned program '%s'\n", path);
3857 
3858 	return 0;
3859 }
3860 
3861 int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
3862 				int instance)
3863 {
3864 	int err;
3865 
3866 	err = check_path(path);
3867 	if (err)
3868 		return err;
3869 
3870 	if (prog == NULL) {
3871 		pr_warn("invalid program pointer\n");
3872 		return -EINVAL;
3873 	}
3874 
3875 	if (instance < 0 || instance >= prog->instances.nr) {
3876 		pr_warn("invalid prog instance %d of prog %s (max %d)\n",
3877 			instance, prog->section_name, prog->instances.nr);
3878 		return -EINVAL;
3879 	}
3880 
3881 	err = unlink(path);
3882 	if (err != 0)
3883 		return -errno;
3884 	pr_debug("unpinned program '%s'\n", path);
3885 
3886 	return 0;
3887 }
3888 
3889 static int make_dir(const char *path)
3890 {
3891 	char *cp, errmsg[STRERR_BUFSIZE];
3892 	int err = 0;
3893 
3894 	if (mkdir(path, 0700) && errno != EEXIST)
3895 		err = -errno;
3896 
3897 	if (err) {
3898 		cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
3899 		pr_warn("failed to mkdir %s: %s\n", path, cp);
3900 	}
3901 	return err;
3902 }
3903 
3904 int bpf_program__pin(struct bpf_program *prog, const char *path)
3905 {
3906 	int i, err;
3907 
3908 	err = check_path(path);
3909 	if (err)
3910 		return err;
3911 
3912 	if (prog == NULL) {
3913 		pr_warn("invalid program pointer\n");
3914 		return -EINVAL;
3915 	}
3916 
3917 	if (prog->instances.nr <= 0) {
3918 		pr_warn("no instances of prog %s to pin\n",
3919 			   prog->section_name);
3920 		return -EINVAL;
3921 	}
3922 
3923 	if (prog->instances.nr == 1) {
3924 		/* don't create subdirs when pinning single instance */
3925 		return bpf_program__pin_instance(prog, path, 0);
3926 	}
3927 
3928 	err = make_dir(path);
3929 	if (err)
3930 		return err;
3931 
3932 	for (i = 0; i < prog->instances.nr; i++) {
3933 		char buf[PATH_MAX];
3934 		int len;
3935 
3936 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
3937 		if (len < 0) {
3938 			err = -EINVAL;
3939 			goto err_unpin;
3940 		} else if (len >= PATH_MAX) {
3941 			err = -ENAMETOOLONG;
3942 			goto err_unpin;
3943 		}
3944 
3945 		err = bpf_program__pin_instance(prog, buf, i);
3946 		if (err)
3947 			goto err_unpin;
3948 	}
3949 
3950 	return 0;
3951 
3952 err_unpin:
3953 	for (i = i - 1; i >= 0; i--) {
3954 		char buf[PATH_MAX];
3955 		int len;
3956 
3957 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
3958 		if (len < 0)
3959 			continue;
3960 		else if (len >= PATH_MAX)
3961 			continue;
3962 
3963 		bpf_program__unpin_instance(prog, buf, i);
3964 	}
3965 
3966 	rmdir(path);
3967 
3968 	return err;
3969 }
3970 
3971 int bpf_program__unpin(struct bpf_program *prog, const char *path)
3972 {
3973 	int i, err;
3974 
3975 	err = check_path(path);
3976 	if (err)
3977 		return err;
3978 
3979 	if (prog == NULL) {
3980 		pr_warn("invalid program pointer\n");
3981 		return -EINVAL;
3982 	}
3983 
3984 	if (prog->instances.nr <= 0) {
3985 		pr_warn("no instances of prog %s to pin\n",
3986 			   prog->section_name);
3987 		return -EINVAL;
3988 	}
3989 
3990 	if (prog->instances.nr == 1) {
3991 		/* don't create subdirs when pinning single instance */
3992 		return bpf_program__unpin_instance(prog, path, 0);
3993 	}
3994 
3995 	for (i = 0; i < prog->instances.nr; i++) {
3996 		char buf[PATH_MAX];
3997 		int len;
3998 
3999 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
4000 		if (len < 0)
4001 			return -EINVAL;
4002 		else if (len >= PATH_MAX)
4003 			return -ENAMETOOLONG;
4004 
4005 		err = bpf_program__unpin_instance(prog, buf, i);
4006 		if (err)
4007 			return err;
4008 	}
4009 
4010 	err = rmdir(path);
4011 	if (err)
4012 		return -errno;
4013 
4014 	return 0;
4015 }
4016 
4017 int bpf_map__pin(struct bpf_map *map, const char *path)
4018 {
4019 	char *cp, errmsg[STRERR_BUFSIZE];
4020 	int err;
4021 
4022 	err = check_path(path);
4023 	if (err)
4024 		return err;
4025 
4026 	if (map == NULL) {
4027 		pr_warn("invalid map pointer\n");
4028 		return -EINVAL;
4029 	}
4030 
4031 	if (bpf_obj_pin(map->fd, path)) {
4032 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
4033 		pr_warn("failed to pin map: %s\n", cp);
4034 		return -errno;
4035 	}
4036 
4037 	pr_debug("pinned map '%s'\n", path);
4038 
4039 	return 0;
4040 }
4041 
4042 int bpf_map__unpin(struct bpf_map *map, const char *path)
4043 {
4044 	int err;
4045 
4046 	err = check_path(path);
4047 	if (err)
4048 		return err;
4049 
4050 	if (map == NULL) {
4051 		pr_warn("invalid map pointer\n");
4052 		return -EINVAL;
4053 	}
4054 
4055 	err = unlink(path);
4056 	if (err != 0)
4057 		return -errno;
4058 	pr_debug("unpinned map '%s'\n", path);
4059 
4060 	return 0;
4061 }
4062 
4063 int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
4064 {
4065 	struct bpf_map *map;
4066 	int err;
4067 
4068 	if (!obj)
4069 		return -ENOENT;
4070 
4071 	if (!obj->loaded) {
4072 		pr_warn("object not yet loaded; load it first\n");
4073 		return -ENOENT;
4074 	}
4075 
4076 	err = make_dir(path);
4077 	if (err)
4078 		return err;
4079 
4080 	bpf_object__for_each_map(map, obj) {
4081 		char buf[PATH_MAX];
4082 		int len;
4083 
4084 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4085 			       bpf_map__name(map));
4086 		if (len < 0) {
4087 			err = -EINVAL;
4088 			goto err_unpin_maps;
4089 		} else if (len >= PATH_MAX) {
4090 			err = -ENAMETOOLONG;
4091 			goto err_unpin_maps;
4092 		}
4093 
4094 		err = bpf_map__pin(map, buf);
4095 		if (err)
4096 			goto err_unpin_maps;
4097 	}
4098 
4099 	return 0;
4100 
4101 err_unpin_maps:
4102 	while ((map = bpf_map__prev(map, obj))) {
4103 		char buf[PATH_MAX];
4104 		int len;
4105 
4106 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4107 			       bpf_map__name(map));
4108 		if (len < 0)
4109 			continue;
4110 		else if (len >= PATH_MAX)
4111 			continue;
4112 
4113 		bpf_map__unpin(map, buf);
4114 	}
4115 
4116 	return err;
4117 }
4118 
4119 int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
4120 {
4121 	struct bpf_map *map;
4122 	int err;
4123 
4124 	if (!obj)
4125 		return -ENOENT;
4126 
4127 	bpf_object__for_each_map(map, obj) {
4128 		char buf[PATH_MAX];
4129 		int len;
4130 
4131 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4132 			       bpf_map__name(map));
4133 		if (len < 0)
4134 			return -EINVAL;
4135 		else if (len >= PATH_MAX)
4136 			return -ENAMETOOLONG;
4137 
4138 		err = bpf_map__unpin(map, buf);
4139 		if (err)
4140 			return err;
4141 	}
4142 
4143 	return 0;
4144 }
4145 
4146 int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
4147 {
4148 	struct bpf_program *prog;
4149 	int err;
4150 
4151 	if (!obj)
4152 		return -ENOENT;
4153 
4154 	if (!obj->loaded) {
4155 		pr_warn("object not yet loaded; load it first\n");
4156 		return -ENOENT;
4157 	}
4158 
4159 	err = make_dir(path);
4160 	if (err)
4161 		return err;
4162 
4163 	bpf_object__for_each_program(prog, obj) {
4164 		char buf[PATH_MAX];
4165 		int len;
4166 
4167 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4168 			       prog->pin_name);
4169 		if (len < 0) {
4170 			err = -EINVAL;
4171 			goto err_unpin_programs;
4172 		} else if (len >= PATH_MAX) {
4173 			err = -ENAMETOOLONG;
4174 			goto err_unpin_programs;
4175 		}
4176 
4177 		err = bpf_program__pin(prog, buf);
4178 		if (err)
4179 			goto err_unpin_programs;
4180 	}
4181 
4182 	return 0;
4183 
4184 err_unpin_programs:
4185 	while ((prog = bpf_program__prev(prog, obj))) {
4186 		char buf[PATH_MAX];
4187 		int len;
4188 
4189 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4190 			       prog->pin_name);
4191 		if (len < 0)
4192 			continue;
4193 		else if (len >= PATH_MAX)
4194 			continue;
4195 
4196 		bpf_program__unpin(prog, buf);
4197 	}
4198 
4199 	return err;
4200 }
4201 
4202 int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
4203 {
4204 	struct bpf_program *prog;
4205 	int err;
4206 
4207 	if (!obj)
4208 		return -ENOENT;
4209 
4210 	bpf_object__for_each_program(prog, obj) {
4211 		char buf[PATH_MAX];
4212 		int len;
4213 
4214 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
4215 			       prog->pin_name);
4216 		if (len < 0)
4217 			return -EINVAL;
4218 		else if (len >= PATH_MAX)
4219 			return -ENAMETOOLONG;
4220 
4221 		err = bpf_program__unpin(prog, buf);
4222 		if (err)
4223 			return err;
4224 	}
4225 
4226 	return 0;
4227 }
4228 
4229 int bpf_object__pin(struct bpf_object *obj, const char *path)
4230 {
4231 	int err;
4232 
4233 	err = bpf_object__pin_maps(obj, path);
4234 	if (err)
4235 		return err;
4236 
4237 	err = bpf_object__pin_programs(obj, path);
4238 	if (err) {
4239 		bpf_object__unpin_maps(obj, path);
4240 		return err;
4241 	}
4242 
4243 	return 0;
4244 }
4245 
4246 void bpf_object__close(struct bpf_object *obj)
4247 {
4248 	size_t i;
4249 
4250 	if (!obj)
4251 		return;
4252 
4253 	if (obj->clear_priv)
4254 		obj->clear_priv(obj, obj->priv);
4255 
4256 	bpf_object__elf_finish(obj);
4257 	bpf_object__unload(obj);
4258 	btf__free(obj->btf);
4259 	btf_ext__free(obj->btf_ext);
4260 
4261 	for (i = 0; i < obj->nr_maps; i++) {
4262 		zfree(&obj->maps[i].name);
4263 		if (obj->maps[i].clear_priv)
4264 			obj->maps[i].clear_priv(&obj->maps[i],
4265 						obj->maps[i].priv);
4266 		obj->maps[i].priv = NULL;
4267 		obj->maps[i].clear_priv = NULL;
4268 	}
4269 
4270 	zfree(&obj->sections.rodata);
4271 	zfree(&obj->sections.data);
4272 	zfree(&obj->maps);
4273 	obj->nr_maps = 0;
4274 
4275 	if (obj->programs && obj->nr_programs) {
4276 		for (i = 0; i < obj->nr_programs; i++)
4277 			bpf_program__exit(&obj->programs[i]);
4278 	}
4279 	zfree(&obj->programs);
4280 
4281 	list_del(&obj->list);
4282 	free(obj);
4283 }
4284 
4285 struct bpf_object *
4286 bpf_object__next(struct bpf_object *prev)
4287 {
4288 	struct bpf_object *next;
4289 
4290 	if (!prev)
4291 		next = list_first_entry(&bpf_objects_list,
4292 					struct bpf_object,
4293 					list);
4294 	else
4295 		next = list_next_entry(prev, list);
4296 
4297 	/* Empty list is noticed here so don't need checking on entry. */
4298 	if (&next->list == &bpf_objects_list)
4299 		return NULL;
4300 
4301 	return next;
4302 }
4303 
4304 const char *bpf_object__name(const struct bpf_object *obj)
4305 {
4306 	return obj ? obj->name : ERR_PTR(-EINVAL);
4307 }
4308 
4309 unsigned int bpf_object__kversion(const struct bpf_object *obj)
4310 {
4311 	return obj ? obj->kern_version : 0;
4312 }
4313 
4314 struct btf *bpf_object__btf(const struct bpf_object *obj)
4315 {
4316 	return obj ? obj->btf : NULL;
4317 }
4318 
4319 int bpf_object__btf_fd(const struct bpf_object *obj)
4320 {
4321 	return obj->btf ? btf__fd(obj->btf) : -1;
4322 }
4323 
4324 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
4325 			 bpf_object_clear_priv_t clear_priv)
4326 {
4327 	if (obj->priv && obj->clear_priv)
4328 		obj->clear_priv(obj, obj->priv);
4329 
4330 	obj->priv = priv;
4331 	obj->clear_priv = clear_priv;
4332 	return 0;
4333 }
4334 
4335 void *bpf_object__priv(const struct bpf_object *obj)
4336 {
4337 	return obj ? obj->priv : ERR_PTR(-EINVAL);
4338 }
4339 
4340 static struct bpf_program *
4341 __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
4342 		    bool forward)
4343 {
4344 	size_t nr_programs = obj->nr_programs;
4345 	ssize_t idx;
4346 
4347 	if (!nr_programs)
4348 		return NULL;
4349 
4350 	if (!p)
4351 		/* Iter from the beginning */
4352 		return forward ? &obj->programs[0] :
4353 			&obj->programs[nr_programs - 1];
4354 
4355 	if (p->obj != obj) {
4356 		pr_warn("error: program handler doesn't match object\n");
4357 		return NULL;
4358 	}
4359 
4360 	idx = (p - obj->programs) + (forward ? 1 : -1);
4361 	if (idx >= obj->nr_programs || idx < 0)
4362 		return NULL;
4363 	return &obj->programs[idx];
4364 }
4365 
4366 struct bpf_program *
4367 bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
4368 {
4369 	struct bpf_program *prog = prev;
4370 
4371 	do {
4372 		prog = __bpf_program__iter(prog, obj, true);
4373 	} while (prog && bpf_program__is_function_storage(prog, obj));
4374 
4375 	return prog;
4376 }
4377 
4378 struct bpf_program *
4379 bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
4380 {
4381 	struct bpf_program *prog = next;
4382 
4383 	do {
4384 		prog = __bpf_program__iter(prog, obj, false);
4385 	} while (prog && bpf_program__is_function_storage(prog, obj));
4386 
4387 	return prog;
4388 }
4389 
4390 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
4391 			  bpf_program_clear_priv_t clear_priv)
4392 {
4393 	if (prog->priv && prog->clear_priv)
4394 		prog->clear_priv(prog, prog->priv);
4395 
4396 	prog->priv = priv;
4397 	prog->clear_priv = clear_priv;
4398 	return 0;
4399 }
4400 
4401 void *bpf_program__priv(const struct bpf_program *prog)
4402 {
4403 	return prog ? prog->priv : ERR_PTR(-EINVAL);
4404 }
4405 
4406 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
4407 {
4408 	prog->prog_ifindex = ifindex;
4409 }
4410 
4411 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
4412 {
4413 	const char *title;
4414 
4415 	title = prog->section_name;
4416 	if (needs_copy) {
4417 		title = strdup(title);
4418 		if (!title) {
4419 			pr_warn("failed to strdup program title\n");
4420 			return ERR_PTR(-ENOMEM);
4421 		}
4422 	}
4423 
4424 	return title;
4425 }
4426 
4427 int bpf_program__fd(const struct bpf_program *prog)
4428 {
4429 	return bpf_program__nth_fd(prog, 0);
4430 }
4431 
4432 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
4433 			  bpf_program_prep_t prep)
4434 {
4435 	int *instances_fds;
4436 
4437 	if (nr_instances <= 0 || !prep)
4438 		return -EINVAL;
4439 
4440 	if (prog->instances.nr > 0 || prog->instances.fds) {
4441 		pr_warn("Can't set pre-processor after loading\n");
4442 		return -EINVAL;
4443 	}
4444 
4445 	instances_fds = malloc(sizeof(int) * nr_instances);
4446 	if (!instances_fds) {
4447 		pr_warn("alloc memory failed for fds\n");
4448 		return -ENOMEM;
4449 	}
4450 
4451 	/* fill all fd with -1 */
4452 	memset(instances_fds, -1, sizeof(int) * nr_instances);
4453 
4454 	prog->instances.nr = nr_instances;
4455 	prog->instances.fds = instances_fds;
4456 	prog->preprocessor = prep;
4457 	return 0;
4458 }
4459 
4460 int bpf_program__nth_fd(const struct bpf_program *prog, int n)
4461 {
4462 	int fd;
4463 
4464 	if (!prog)
4465 		return -EINVAL;
4466 
4467 	if (n >= prog->instances.nr || n < 0) {
4468 		pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
4469 			n, prog->section_name, prog->instances.nr);
4470 		return -EINVAL;
4471 	}
4472 
4473 	fd = prog->instances.fds[n];
4474 	if (fd < 0) {
4475 		pr_warn("%dth instance of program '%s' is invalid\n",
4476 			n, prog->section_name);
4477 		return -ENOENT;
4478 	}
4479 
4480 	return fd;
4481 }
4482 
4483 enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog)
4484 {
4485 	return prog->type;
4486 }
4487 
4488 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
4489 {
4490 	prog->type = type;
4491 }
4492 
4493 static bool bpf_program__is_type(const struct bpf_program *prog,
4494 				 enum bpf_prog_type type)
4495 {
4496 	return prog ? (prog->type == type) : false;
4497 }
4498 
4499 #define BPF_PROG_TYPE_FNS(NAME, TYPE)				\
4500 int bpf_program__set_##NAME(struct bpf_program *prog)		\
4501 {								\
4502 	if (!prog)						\
4503 		return -EINVAL;					\
4504 	bpf_program__set_type(prog, TYPE);			\
4505 	return 0;						\
4506 }								\
4507 								\
4508 bool bpf_program__is_##NAME(const struct bpf_program *prog)	\
4509 {								\
4510 	return bpf_program__is_type(prog, TYPE);		\
4511 }								\
4512 
4513 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
4514 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
4515 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
4516 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
4517 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
4518 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
4519 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
4520 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
4521 
4522 enum bpf_attach_type
4523 bpf_program__get_expected_attach_type(struct bpf_program *prog)
4524 {
4525 	return prog->expected_attach_type;
4526 }
4527 
4528 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
4529 					   enum bpf_attach_type type)
4530 {
4531 	prog->expected_attach_type = type;
4532 }
4533 
4534 #define BPF_PROG_SEC_IMPL(string, ptype, eatype, is_attachable, btf, atype) \
4535 	{ string, sizeof(string) - 1, ptype, eatype, is_attachable, btf, atype }
4536 
4537 /* Programs that can NOT be attached. */
4538 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0)
4539 
4540 /* Programs that can be attached. */
4541 #define BPF_APROG_SEC(string, ptype, atype) \
4542 	BPF_PROG_SEC_IMPL(string, ptype, 0, 1, 0, atype)
4543 
4544 /* Programs that must specify expected attach type at load time. */
4545 #define BPF_EAPROG_SEC(string, ptype, eatype) \
4546 	BPF_PROG_SEC_IMPL(string, ptype, eatype, 1, 0, eatype)
4547 
4548 /* Programs that use BTF to identify attach point */
4549 #define BPF_PROG_BTF(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 1, 0)
4550 
4551 /* Programs that can be attached but attach type can't be identified by section
4552  * name. Kept for backward compatibility.
4553  */
4554 #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
4555 
4556 static const struct {
4557 	const char *sec;
4558 	size_t len;
4559 	enum bpf_prog_type prog_type;
4560 	enum bpf_attach_type expected_attach_type;
4561 	bool is_attachable;
4562 	bool is_attach_btf;
4563 	enum bpf_attach_type attach_type;
4564 } section_names[] = {
4565 	BPF_PROG_SEC("socket",			BPF_PROG_TYPE_SOCKET_FILTER),
4566 	BPF_PROG_SEC("kprobe/",			BPF_PROG_TYPE_KPROBE),
4567 	BPF_PROG_SEC("uprobe/",			BPF_PROG_TYPE_KPROBE),
4568 	BPF_PROG_SEC("kretprobe/",		BPF_PROG_TYPE_KPROBE),
4569 	BPF_PROG_SEC("uretprobe/",		BPF_PROG_TYPE_KPROBE),
4570 	BPF_PROG_SEC("classifier",		BPF_PROG_TYPE_SCHED_CLS),
4571 	BPF_PROG_SEC("action",			BPF_PROG_TYPE_SCHED_ACT),
4572 	BPF_PROG_SEC("tracepoint/",		BPF_PROG_TYPE_TRACEPOINT),
4573 	BPF_PROG_SEC("tp/",			BPF_PROG_TYPE_TRACEPOINT),
4574 	BPF_PROG_SEC("raw_tracepoint/",		BPF_PROG_TYPE_RAW_TRACEPOINT),
4575 	BPF_PROG_SEC("raw_tp/",			BPF_PROG_TYPE_RAW_TRACEPOINT),
4576 	BPF_PROG_BTF("tp_btf/",			BPF_PROG_TYPE_RAW_TRACEPOINT),
4577 	BPF_PROG_SEC("xdp",			BPF_PROG_TYPE_XDP),
4578 	BPF_PROG_SEC("perf_event",		BPF_PROG_TYPE_PERF_EVENT),
4579 	BPF_PROG_SEC("lwt_in",			BPF_PROG_TYPE_LWT_IN),
4580 	BPF_PROG_SEC("lwt_out",			BPF_PROG_TYPE_LWT_OUT),
4581 	BPF_PROG_SEC("lwt_xmit",		BPF_PROG_TYPE_LWT_XMIT),
4582 	BPF_PROG_SEC("lwt_seg6local",		BPF_PROG_TYPE_LWT_SEG6LOCAL),
4583 	BPF_APROG_SEC("cgroup_skb/ingress",	BPF_PROG_TYPE_CGROUP_SKB,
4584 						BPF_CGROUP_INET_INGRESS),
4585 	BPF_APROG_SEC("cgroup_skb/egress",	BPF_PROG_TYPE_CGROUP_SKB,
4586 						BPF_CGROUP_INET_EGRESS),
4587 	BPF_APROG_COMPAT("cgroup/skb",		BPF_PROG_TYPE_CGROUP_SKB),
4588 	BPF_APROG_SEC("cgroup/sock",		BPF_PROG_TYPE_CGROUP_SOCK,
4589 						BPF_CGROUP_INET_SOCK_CREATE),
4590 	BPF_EAPROG_SEC("cgroup/post_bind4",	BPF_PROG_TYPE_CGROUP_SOCK,
4591 						BPF_CGROUP_INET4_POST_BIND),
4592 	BPF_EAPROG_SEC("cgroup/post_bind6",	BPF_PROG_TYPE_CGROUP_SOCK,
4593 						BPF_CGROUP_INET6_POST_BIND),
4594 	BPF_APROG_SEC("cgroup/dev",		BPF_PROG_TYPE_CGROUP_DEVICE,
4595 						BPF_CGROUP_DEVICE),
4596 	BPF_APROG_SEC("sockops",		BPF_PROG_TYPE_SOCK_OPS,
4597 						BPF_CGROUP_SOCK_OPS),
4598 	BPF_APROG_SEC("sk_skb/stream_parser",	BPF_PROG_TYPE_SK_SKB,
4599 						BPF_SK_SKB_STREAM_PARSER),
4600 	BPF_APROG_SEC("sk_skb/stream_verdict",	BPF_PROG_TYPE_SK_SKB,
4601 						BPF_SK_SKB_STREAM_VERDICT),
4602 	BPF_APROG_COMPAT("sk_skb",		BPF_PROG_TYPE_SK_SKB),
4603 	BPF_APROG_SEC("sk_msg",			BPF_PROG_TYPE_SK_MSG,
4604 						BPF_SK_MSG_VERDICT),
4605 	BPF_APROG_SEC("lirc_mode2",		BPF_PROG_TYPE_LIRC_MODE2,
4606 						BPF_LIRC_MODE2),
4607 	BPF_APROG_SEC("flow_dissector",		BPF_PROG_TYPE_FLOW_DISSECTOR,
4608 						BPF_FLOW_DISSECTOR),
4609 	BPF_EAPROG_SEC("cgroup/bind4",		BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4610 						BPF_CGROUP_INET4_BIND),
4611 	BPF_EAPROG_SEC("cgroup/bind6",		BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4612 						BPF_CGROUP_INET6_BIND),
4613 	BPF_EAPROG_SEC("cgroup/connect4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4614 						BPF_CGROUP_INET4_CONNECT),
4615 	BPF_EAPROG_SEC("cgroup/connect6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4616 						BPF_CGROUP_INET6_CONNECT),
4617 	BPF_EAPROG_SEC("cgroup/sendmsg4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4618 						BPF_CGROUP_UDP4_SENDMSG),
4619 	BPF_EAPROG_SEC("cgroup/sendmsg6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4620 						BPF_CGROUP_UDP6_SENDMSG),
4621 	BPF_EAPROG_SEC("cgroup/recvmsg4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4622 						BPF_CGROUP_UDP4_RECVMSG),
4623 	BPF_EAPROG_SEC("cgroup/recvmsg6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
4624 						BPF_CGROUP_UDP6_RECVMSG),
4625 	BPF_EAPROG_SEC("cgroup/sysctl",		BPF_PROG_TYPE_CGROUP_SYSCTL,
4626 						BPF_CGROUP_SYSCTL),
4627 	BPF_EAPROG_SEC("cgroup/getsockopt",	BPF_PROG_TYPE_CGROUP_SOCKOPT,
4628 						BPF_CGROUP_GETSOCKOPT),
4629 	BPF_EAPROG_SEC("cgroup/setsockopt",	BPF_PROG_TYPE_CGROUP_SOCKOPT,
4630 						BPF_CGROUP_SETSOCKOPT),
4631 };
4632 
4633 #undef BPF_PROG_SEC_IMPL
4634 #undef BPF_PROG_SEC
4635 #undef BPF_APROG_SEC
4636 #undef BPF_EAPROG_SEC
4637 #undef BPF_APROG_COMPAT
4638 
4639 #define MAX_TYPE_NAME_SIZE 32
4640 
4641 static char *libbpf_get_type_names(bool attach_type)
4642 {
4643 	int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
4644 	char *buf;
4645 
4646 	buf = malloc(len);
4647 	if (!buf)
4648 		return NULL;
4649 
4650 	buf[0] = '\0';
4651 	/* Forge string buf with all available names */
4652 	for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4653 		if (attach_type && !section_names[i].is_attachable)
4654 			continue;
4655 
4656 		if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
4657 			free(buf);
4658 			return NULL;
4659 		}
4660 		strcat(buf, " ");
4661 		strcat(buf, section_names[i].sec);
4662 	}
4663 
4664 	return buf;
4665 }
4666 
4667 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
4668 			     enum bpf_attach_type *expected_attach_type)
4669 {
4670 	char *type_names;
4671 	int i;
4672 
4673 	if (!name)
4674 		return -EINVAL;
4675 
4676 	for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4677 		if (strncmp(name, section_names[i].sec, section_names[i].len))
4678 			continue;
4679 		*prog_type = section_names[i].prog_type;
4680 		*expected_attach_type = section_names[i].expected_attach_type;
4681 		if (section_names[i].is_attach_btf) {
4682 			struct btf *btf = bpf_core_find_kernel_btf();
4683 			char raw_tp_btf_name[128] = "btf_trace_";
4684 			char *dst = raw_tp_btf_name + sizeof("btf_trace_") - 1;
4685 			int ret;
4686 
4687 			if (IS_ERR(btf)) {
4688 				pr_warn("vmlinux BTF is not found\n");
4689 				return -EINVAL;
4690 			}
4691 			/* prepend "btf_trace_" prefix per kernel convention */
4692 			strncat(dst, name + section_names[i].len,
4693 				sizeof(raw_tp_btf_name) - sizeof("btf_trace_"));
4694 			ret = btf__find_by_name(btf, raw_tp_btf_name);
4695 			btf__free(btf);
4696 			if (ret <= 0) {
4697 				pr_warn("%s is not found in vmlinux BTF\n", dst);
4698 				return -EINVAL;
4699 			}
4700 			*expected_attach_type = ret;
4701 		}
4702 		return 0;
4703 	}
4704 	pr_warn("failed to guess program type based on ELF section name '%s'\n", name);
4705 	type_names = libbpf_get_type_names(false);
4706 	if (type_names != NULL) {
4707 		pr_info("supported section(type) names are:%s\n", type_names);
4708 		free(type_names);
4709 	}
4710 
4711 	return -ESRCH;
4712 }
4713 
4714 int libbpf_attach_type_by_name(const char *name,
4715 			       enum bpf_attach_type *attach_type)
4716 {
4717 	char *type_names;
4718 	int i;
4719 
4720 	if (!name)
4721 		return -EINVAL;
4722 
4723 	for (i = 0; i < ARRAY_SIZE(section_names); i++) {
4724 		if (strncmp(name, section_names[i].sec, section_names[i].len))
4725 			continue;
4726 		if (!section_names[i].is_attachable)
4727 			return -EINVAL;
4728 		*attach_type = section_names[i].attach_type;
4729 		return 0;
4730 	}
4731 	pr_warn("failed to guess attach type based on ELF section name '%s'\n", name);
4732 	type_names = libbpf_get_type_names(true);
4733 	if (type_names != NULL) {
4734 		pr_info("attachable section(type) names are:%s\n", type_names);
4735 		free(type_names);
4736 	}
4737 
4738 	return -EINVAL;
4739 }
4740 
4741 int bpf_map__fd(const struct bpf_map *map)
4742 {
4743 	return map ? map->fd : -EINVAL;
4744 }
4745 
4746 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
4747 {
4748 	return map ? &map->def : ERR_PTR(-EINVAL);
4749 }
4750 
4751 const char *bpf_map__name(const struct bpf_map *map)
4752 {
4753 	return map ? map->name : NULL;
4754 }
4755 
4756 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
4757 {
4758 	return map ? map->btf_key_type_id : 0;
4759 }
4760 
4761 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
4762 {
4763 	return map ? map->btf_value_type_id : 0;
4764 }
4765 
4766 int bpf_map__set_priv(struct bpf_map *map, void *priv,
4767 		     bpf_map_clear_priv_t clear_priv)
4768 {
4769 	if (!map)
4770 		return -EINVAL;
4771 
4772 	if (map->priv) {
4773 		if (map->clear_priv)
4774 			map->clear_priv(map, map->priv);
4775 	}
4776 
4777 	map->priv = priv;
4778 	map->clear_priv = clear_priv;
4779 	return 0;
4780 }
4781 
4782 void *bpf_map__priv(const struct bpf_map *map)
4783 {
4784 	return map ? map->priv : ERR_PTR(-EINVAL);
4785 }
4786 
4787 bool bpf_map__is_offload_neutral(const struct bpf_map *map)
4788 {
4789 	return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
4790 }
4791 
4792 bool bpf_map__is_internal(const struct bpf_map *map)
4793 {
4794 	return map->libbpf_type != LIBBPF_MAP_UNSPEC;
4795 }
4796 
4797 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
4798 {
4799 	map->map_ifindex = ifindex;
4800 }
4801 
4802 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
4803 {
4804 	if (!bpf_map_type__is_map_in_map(map->def.type)) {
4805 		pr_warn("error: unsupported map type\n");
4806 		return -EINVAL;
4807 	}
4808 	if (map->inner_map_fd != -1) {
4809 		pr_warn("error: inner_map_fd already specified\n");
4810 		return -EINVAL;
4811 	}
4812 	map->inner_map_fd = fd;
4813 	return 0;
4814 }
4815 
4816 static struct bpf_map *
4817 __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
4818 {
4819 	ssize_t idx;
4820 	struct bpf_map *s, *e;
4821 
4822 	if (!obj || !obj->maps)
4823 		return NULL;
4824 
4825 	s = obj->maps;
4826 	e = obj->maps + obj->nr_maps;
4827 
4828 	if ((m < s) || (m >= e)) {
4829 		pr_warn("error in %s: map handler doesn't belong to object\n",
4830 			 __func__);
4831 		return NULL;
4832 	}
4833 
4834 	idx = (m - obj->maps) + i;
4835 	if (idx >= obj->nr_maps || idx < 0)
4836 		return NULL;
4837 	return &obj->maps[idx];
4838 }
4839 
4840 struct bpf_map *
4841 bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
4842 {
4843 	if (prev == NULL)
4844 		return obj->maps;
4845 
4846 	return __bpf_map__iter(prev, obj, 1);
4847 }
4848 
4849 struct bpf_map *
4850 bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
4851 {
4852 	if (next == NULL) {
4853 		if (!obj->nr_maps)
4854 			return NULL;
4855 		return obj->maps + obj->nr_maps - 1;
4856 	}
4857 
4858 	return __bpf_map__iter(next, obj, -1);
4859 }
4860 
4861 struct bpf_map *
4862 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
4863 {
4864 	struct bpf_map *pos;
4865 
4866 	bpf_object__for_each_map(pos, obj) {
4867 		if (pos->name && !strcmp(pos->name, name))
4868 			return pos;
4869 	}
4870 	return NULL;
4871 }
4872 
4873 int
4874 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
4875 {
4876 	return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
4877 }
4878 
4879 struct bpf_map *
4880 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
4881 {
4882 	return ERR_PTR(-ENOTSUP);
4883 }
4884 
4885 long libbpf_get_error(const void *ptr)
4886 {
4887 	return PTR_ERR_OR_ZERO(ptr);
4888 }
4889 
4890 int bpf_prog_load(const char *file, enum bpf_prog_type type,
4891 		  struct bpf_object **pobj, int *prog_fd)
4892 {
4893 	struct bpf_prog_load_attr attr;
4894 
4895 	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
4896 	attr.file = file;
4897 	attr.prog_type = type;
4898 	attr.expected_attach_type = 0;
4899 
4900 	return bpf_prog_load_xattr(&attr, pobj, prog_fd);
4901 }
4902 
4903 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
4904 			struct bpf_object **pobj, int *prog_fd)
4905 {
4906 	struct bpf_object_open_attr open_attr = {};
4907 	struct bpf_program *prog, *first_prog = NULL;
4908 	struct bpf_object *obj;
4909 	struct bpf_map *map;
4910 	int err;
4911 
4912 	if (!attr)
4913 		return -EINVAL;
4914 	if (!attr->file)
4915 		return -EINVAL;
4916 
4917 	open_attr.file = attr->file;
4918 	open_attr.prog_type = attr->prog_type;
4919 
4920 	obj = bpf_object__open_xattr(&open_attr);
4921 	if (IS_ERR_OR_NULL(obj))
4922 		return -ENOENT;
4923 
4924 	bpf_object__for_each_program(prog, obj) {
4925 		enum bpf_attach_type attach_type = attr->expected_attach_type;
4926 		/*
4927 		 * to preserve backwards compatibility, bpf_prog_load treats
4928 		 * attr->prog_type, if specified, as an override to whatever
4929 		 * bpf_object__open guessed
4930 		 */
4931 		if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) {
4932 			bpf_program__set_type(prog, attr->prog_type);
4933 			bpf_program__set_expected_attach_type(prog,
4934 							      attach_type);
4935 		}
4936 		if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) {
4937 			/*
4938 			 * we haven't guessed from section name and user
4939 			 * didn't provide a fallback type, too bad...
4940 			 */
4941 			bpf_object__close(obj);
4942 			return -EINVAL;
4943 		}
4944 
4945 		prog->prog_ifindex = attr->ifindex;
4946 		prog->log_level = attr->log_level;
4947 		prog->prog_flags = attr->prog_flags;
4948 		if (!first_prog)
4949 			first_prog = prog;
4950 	}
4951 
4952 	bpf_object__for_each_map(map, obj) {
4953 		if (!bpf_map__is_offload_neutral(map))
4954 			map->map_ifindex = attr->ifindex;
4955 	}
4956 
4957 	if (!first_prog) {
4958 		pr_warn("object file doesn't contain bpf program\n");
4959 		bpf_object__close(obj);
4960 		return -ENOENT;
4961 	}
4962 
4963 	err = bpf_object__load(obj);
4964 	if (err) {
4965 		bpf_object__close(obj);
4966 		return -EINVAL;
4967 	}
4968 
4969 	*pobj = obj;
4970 	*prog_fd = bpf_program__fd(first_prog);
4971 	return 0;
4972 }
4973 
4974 struct bpf_link {
4975 	int (*destroy)(struct bpf_link *link);
4976 };
4977 
4978 int bpf_link__destroy(struct bpf_link *link)
4979 {
4980 	int err;
4981 
4982 	if (!link)
4983 		return 0;
4984 
4985 	err = link->destroy(link);
4986 	free(link);
4987 
4988 	return err;
4989 }
4990 
4991 struct bpf_link_fd {
4992 	struct bpf_link link; /* has to be at the top of struct */
4993 	int fd; /* hook FD */
4994 };
4995 
4996 static int bpf_link__destroy_perf_event(struct bpf_link *link)
4997 {
4998 	struct bpf_link_fd *l = (void *)link;
4999 	int err;
5000 
5001 	err = ioctl(l->fd, PERF_EVENT_IOC_DISABLE, 0);
5002 	if (err)
5003 		err = -errno;
5004 
5005 	close(l->fd);
5006 	return err;
5007 }
5008 
5009 struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
5010 						int pfd)
5011 {
5012 	char errmsg[STRERR_BUFSIZE];
5013 	struct bpf_link_fd *link;
5014 	int prog_fd, err;
5015 
5016 	if (pfd < 0) {
5017 		pr_warn("program '%s': invalid perf event FD %d\n",
5018 			bpf_program__title(prog, false), pfd);
5019 		return ERR_PTR(-EINVAL);
5020 	}
5021 	prog_fd = bpf_program__fd(prog);
5022 	if (prog_fd < 0) {
5023 		pr_warn("program '%s': can't attach BPF program w/o FD (did you load it?)\n",
5024 			bpf_program__title(prog, false));
5025 		return ERR_PTR(-EINVAL);
5026 	}
5027 
5028 	link = malloc(sizeof(*link));
5029 	if (!link)
5030 		return ERR_PTR(-ENOMEM);
5031 	link->link.destroy = &bpf_link__destroy_perf_event;
5032 	link->fd = pfd;
5033 
5034 	if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
5035 		err = -errno;
5036 		free(link);
5037 		pr_warn("program '%s': failed to attach to pfd %d: %s\n",
5038 			bpf_program__title(prog, false), pfd,
5039 			   libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5040 		return ERR_PTR(err);
5041 	}
5042 	if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
5043 		err = -errno;
5044 		free(link);
5045 		pr_warn("program '%s': failed to enable pfd %d: %s\n",
5046 			bpf_program__title(prog, false), pfd,
5047 			   libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5048 		return ERR_PTR(err);
5049 	}
5050 	return (struct bpf_link *)link;
5051 }
5052 
5053 /*
5054  * this function is expected to parse integer in the range of [0, 2^31-1] from
5055  * given file using scanf format string fmt. If actual parsed value is
5056  * negative, the result might be indistinguishable from error
5057  */
5058 static int parse_uint_from_file(const char *file, const char *fmt)
5059 {
5060 	char buf[STRERR_BUFSIZE];
5061 	int err, ret;
5062 	FILE *f;
5063 
5064 	f = fopen(file, "r");
5065 	if (!f) {
5066 		err = -errno;
5067 		pr_debug("failed to open '%s': %s\n", file,
5068 			 libbpf_strerror_r(err, buf, sizeof(buf)));
5069 		return err;
5070 	}
5071 	err = fscanf(f, fmt, &ret);
5072 	if (err != 1) {
5073 		err = err == EOF ? -EIO : -errno;
5074 		pr_debug("failed to parse '%s': %s\n", file,
5075 			libbpf_strerror_r(err, buf, sizeof(buf)));
5076 		fclose(f);
5077 		return err;
5078 	}
5079 	fclose(f);
5080 	return ret;
5081 }
5082 
5083 static int determine_kprobe_perf_type(void)
5084 {
5085 	const char *file = "/sys/bus/event_source/devices/kprobe/type";
5086 
5087 	return parse_uint_from_file(file, "%d\n");
5088 }
5089 
5090 static int determine_uprobe_perf_type(void)
5091 {
5092 	const char *file = "/sys/bus/event_source/devices/uprobe/type";
5093 
5094 	return parse_uint_from_file(file, "%d\n");
5095 }
5096 
5097 static int determine_kprobe_retprobe_bit(void)
5098 {
5099 	const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
5100 
5101 	return parse_uint_from_file(file, "config:%d\n");
5102 }
5103 
5104 static int determine_uprobe_retprobe_bit(void)
5105 {
5106 	const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
5107 
5108 	return parse_uint_from_file(file, "config:%d\n");
5109 }
5110 
5111 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
5112 				 uint64_t offset, int pid)
5113 {
5114 	struct perf_event_attr attr = {};
5115 	char errmsg[STRERR_BUFSIZE];
5116 	int type, pfd, err;
5117 
5118 	type = uprobe ? determine_uprobe_perf_type()
5119 		      : determine_kprobe_perf_type();
5120 	if (type < 0) {
5121 		pr_warn("failed to determine %s perf type: %s\n",
5122 			uprobe ? "uprobe" : "kprobe",
5123 			libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
5124 		return type;
5125 	}
5126 	if (retprobe) {
5127 		int bit = uprobe ? determine_uprobe_retprobe_bit()
5128 				 : determine_kprobe_retprobe_bit();
5129 
5130 		if (bit < 0) {
5131 			pr_warn("failed to determine %s retprobe bit: %s\n",
5132 				uprobe ? "uprobe" : "kprobe",
5133 				libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
5134 			return bit;
5135 		}
5136 		attr.config |= 1 << bit;
5137 	}
5138 	attr.size = sizeof(attr);
5139 	attr.type = type;
5140 	attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
5141 	attr.config2 = offset;		 /* kprobe_addr or probe_offset */
5142 
5143 	/* pid filter is meaningful only for uprobes */
5144 	pfd = syscall(__NR_perf_event_open, &attr,
5145 		      pid < 0 ? -1 : pid /* pid */,
5146 		      pid == -1 ? 0 : -1 /* cpu */,
5147 		      -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
5148 	if (pfd < 0) {
5149 		err = -errno;
5150 		pr_warn("%s perf_event_open() failed: %s\n",
5151 			uprobe ? "uprobe" : "kprobe",
5152 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5153 		return err;
5154 	}
5155 	return pfd;
5156 }
5157 
5158 struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
5159 					    bool retprobe,
5160 					    const char *func_name)
5161 {
5162 	char errmsg[STRERR_BUFSIZE];
5163 	struct bpf_link *link;
5164 	int pfd, err;
5165 
5166 	pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
5167 				    0 /* offset */, -1 /* pid */);
5168 	if (pfd < 0) {
5169 		pr_warn("program '%s': failed to create %s '%s' perf event: %s\n",
5170 			bpf_program__title(prog, false),
5171 			retprobe ? "kretprobe" : "kprobe", func_name,
5172 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5173 		return ERR_PTR(pfd);
5174 	}
5175 	link = bpf_program__attach_perf_event(prog, pfd);
5176 	if (IS_ERR(link)) {
5177 		close(pfd);
5178 		err = PTR_ERR(link);
5179 		pr_warn("program '%s': failed to attach to %s '%s': %s\n",
5180 			bpf_program__title(prog, false),
5181 			retprobe ? "kretprobe" : "kprobe", func_name,
5182 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5183 		return link;
5184 	}
5185 	return link;
5186 }
5187 
5188 struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
5189 					    bool retprobe, pid_t pid,
5190 					    const char *binary_path,
5191 					    size_t func_offset)
5192 {
5193 	char errmsg[STRERR_BUFSIZE];
5194 	struct bpf_link *link;
5195 	int pfd, err;
5196 
5197 	pfd = perf_event_open_probe(true /* uprobe */, retprobe,
5198 				    binary_path, func_offset, pid);
5199 	if (pfd < 0) {
5200 		pr_warn("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
5201 			bpf_program__title(prog, false),
5202 			retprobe ? "uretprobe" : "uprobe",
5203 			binary_path, func_offset,
5204 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5205 		return ERR_PTR(pfd);
5206 	}
5207 	link = bpf_program__attach_perf_event(prog, pfd);
5208 	if (IS_ERR(link)) {
5209 		close(pfd);
5210 		err = PTR_ERR(link);
5211 		pr_warn("program '%s': failed to attach to %s '%s:0x%zx': %s\n",
5212 			bpf_program__title(prog, false),
5213 			retprobe ? "uretprobe" : "uprobe",
5214 			binary_path, func_offset,
5215 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5216 		return link;
5217 	}
5218 	return link;
5219 }
5220 
5221 static int determine_tracepoint_id(const char *tp_category,
5222 				   const char *tp_name)
5223 {
5224 	char file[PATH_MAX];
5225 	int ret;
5226 
5227 	ret = snprintf(file, sizeof(file),
5228 		       "/sys/kernel/debug/tracing/events/%s/%s/id",
5229 		       tp_category, tp_name);
5230 	if (ret < 0)
5231 		return -errno;
5232 	if (ret >= sizeof(file)) {
5233 		pr_debug("tracepoint %s/%s path is too long\n",
5234 			 tp_category, tp_name);
5235 		return -E2BIG;
5236 	}
5237 	return parse_uint_from_file(file, "%d\n");
5238 }
5239 
5240 static int perf_event_open_tracepoint(const char *tp_category,
5241 				      const char *tp_name)
5242 {
5243 	struct perf_event_attr attr = {};
5244 	char errmsg[STRERR_BUFSIZE];
5245 	int tp_id, pfd, err;
5246 
5247 	tp_id = determine_tracepoint_id(tp_category, tp_name);
5248 	if (tp_id < 0) {
5249 		pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
5250 			tp_category, tp_name,
5251 			libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
5252 		return tp_id;
5253 	}
5254 
5255 	attr.type = PERF_TYPE_TRACEPOINT;
5256 	attr.size = sizeof(attr);
5257 	attr.config = tp_id;
5258 
5259 	pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
5260 		      -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
5261 	if (pfd < 0) {
5262 		err = -errno;
5263 		pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
5264 			tp_category, tp_name,
5265 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5266 		return err;
5267 	}
5268 	return pfd;
5269 }
5270 
5271 struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
5272 						const char *tp_category,
5273 						const char *tp_name)
5274 {
5275 	char errmsg[STRERR_BUFSIZE];
5276 	struct bpf_link *link;
5277 	int pfd, err;
5278 
5279 	pfd = perf_event_open_tracepoint(tp_category, tp_name);
5280 	if (pfd < 0) {
5281 		pr_warn("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
5282 			bpf_program__title(prog, false),
5283 			tp_category, tp_name,
5284 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5285 		return ERR_PTR(pfd);
5286 	}
5287 	link = bpf_program__attach_perf_event(prog, pfd);
5288 	if (IS_ERR(link)) {
5289 		close(pfd);
5290 		err = PTR_ERR(link);
5291 		pr_warn("program '%s': failed to attach to tracepoint '%s/%s': %s\n",
5292 			bpf_program__title(prog, false),
5293 			tp_category, tp_name,
5294 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
5295 		return link;
5296 	}
5297 	return link;
5298 }
5299 
5300 static int bpf_link__destroy_fd(struct bpf_link *link)
5301 {
5302 	struct bpf_link_fd *l = (void *)link;
5303 
5304 	return close(l->fd);
5305 }
5306 
5307 struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
5308 						    const char *tp_name)
5309 {
5310 	char errmsg[STRERR_BUFSIZE];
5311 	struct bpf_link_fd *link;
5312 	int prog_fd, pfd;
5313 
5314 	prog_fd = bpf_program__fd(prog);
5315 	if (prog_fd < 0) {
5316 		pr_warn("program '%s': can't attach before loaded\n",
5317 			bpf_program__title(prog, false));
5318 		return ERR_PTR(-EINVAL);
5319 	}
5320 
5321 	link = malloc(sizeof(*link));
5322 	if (!link)
5323 		return ERR_PTR(-ENOMEM);
5324 	link->link.destroy = &bpf_link__destroy_fd;
5325 
5326 	pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
5327 	if (pfd < 0) {
5328 		pfd = -errno;
5329 		free(link);
5330 		pr_warn("program '%s': failed to attach to raw tracepoint '%s': %s\n",
5331 			bpf_program__title(prog, false), tp_name,
5332 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5333 		return ERR_PTR(pfd);
5334 	}
5335 	link->fd = pfd;
5336 	return (struct bpf_link *)link;
5337 }
5338 
5339 enum bpf_perf_event_ret
5340 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
5341 			   void **copy_mem, size_t *copy_size,
5342 			   bpf_perf_event_print_t fn, void *private_data)
5343 {
5344 	struct perf_event_mmap_page *header = mmap_mem;
5345 	__u64 data_head = ring_buffer_read_head(header);
5346 	__u64 data_tail = header->data_tail;
5347 	void *base = ((__u8 *)header) + page_size;
5348 	int ret = LIBBPF_PERF_EVENT_CONT;
5349 	struct perf_event_header *ehdr;
5350 	size_t ehdr_size;
5351 
5352 	while (data_head != data_tail) {
5353 		ehdr = base + (data_tail & (mmap_size - 1));
5354 		ehdr_size = ehdr->size;
5355 
5356 		if (((void *)ehdr) + ehdr_size > base + mmap_size) {
5357 			void *copy_start = ehdr;
5358 			size_t len_first = base + mmap_size - copy_start;
5359 			size_t len_secnd = ehdr_size - len_first;
5360 
5361 			if (*copy_size < ehdr_size) {
5362 				free(*copy_mem);
5363 				*copy_mem = malloc(ehdr_size);
5364 				if (!*copy_mem) {
5365 					*copy_size = 0;
5366 					ret = LIBBPF_PERF_EVENT_ERROR;
5367 					break;
5368 				}
5369 				*copy_size = ehdr_size;
5370 			}
5371 
5372 			memcpy(*copy_mem, copy_start, len_first);
5373 			memcpy(*copy_mem + len_first, base, len_secnd);
5374 			ehdr = *copy_mem;
5375 		}
5376 
5377 		ret = fn(ehdr, private_data);
5378 		data_tail += ehdr_size;
5379 		if (ret != LIBBPF_PERF_EVENT_CONT)
5380 			break;
5381 	}
5382 
5383 	ring_buffer_write_tail(header, data_tail);
5384 	return ret;
5385 }
5386 
5387 struct perf_buffer;
5388 
5389 struct perf_buffer_params {
5390 	struct perf_event_attr *attr;
5391 	/* if event_cb is specified, it takes precendence */
5392 	perf_buffer_event_fn event_cb;
5393 	/* sample_cb and lost_cb are higher-level common-case callbacks */
5394 	perf_buffer_sample_fn sample_cb;
5395 	perf_buffer_lost_fn lost_cb;
5396 	void *ctx;
5397 	int cpu_cnt;
5398 	int *cpus;
5399 	int *map_keys;
5400 };
5401 
5402 struct perf_cpu_buf {
5403 	struct perf_buffer *pb;
5404 	void *base; /* mmap()'ed memory */
5405 	void *buf; /* for reconstructing segmented data */
5406 	size_t buf_size;
5407 	int fd;
5408 	int cpu;
5409 	int map_key;
5410 };
5411 
5412 struct perf_buffer {
5413 	perf_buffer_event_fn event_cb;
5414 	perf_buffer_sample_fn sample_cb;
5415 	perf_buffer_lost_fn lost_cb;
5416 	void *ctx; /* passed into callbacks */
5417 
5418 	size_t page_size;
5419 	size_t mmap_size;
5420 	struct perf_cpu_buf **cpu_bufs;
5421 	struct epoll_event *events;
5422 	int cpu_cnt;
5423 	int epoll_fd; /* perf event FD */
5424 	int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
5425 };
5426 
5427 static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
5428 				      struct perf_cpu_buf *cpu_buf)
5429 {
5430 	if (!cpu_buf)
5431 		return;
5432 	if (cpu_buf->base &&
5433 	    munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
5434 		pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
5435 	if (cpu_buf->fd >= 0) {
5436 		ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
5437 		close(cpu_buf->fd);
5438 	}
5439 	free(cpu_buf->buf);
5440 	free(cpu_buf);
5441 }
5442 
5443 void perf_buffer__free(struct perf_buffer *pb)
5444 {
5445 	int i;
5446 
5447 	if (!pb)
5448 		return;
5449 	if (pb->cpu_bufs) {
5450 		for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
5451 			struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
5452 
5453 			bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
5454 			perf_buffer__free_cpu_buf(pb, cpu_buf);
5455 		}
5456 		free(pb->cpu_bufs);
5457 	}
5458 	if (pb->epoll_fd >= 0)
5459 		close(pb->epoll_fd);
5460 	free(pb->events);
5461 	free(pb);
5462 }
5463 
5464 static struct perf_cpu_buf *
5465 perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
5466 			  int cpu, int map_key)
5467 {
5468 	struct perf_cpu_buf *cpu_buf;
5469 	char msg[STRERR_BUFSIZE];
5470 	int err;
5471 
5472 	cpu_buf = calloc(1, sizeof(*cpu_buf));
5473 	if (!cpu_buf)
5474 		return ERR_PTR(-ENOMEM);
5475 
5476 	cpu_buf->pb = pb;
5477 	cpu_buf->cpu = cpu;
5478 	cpu_buf->map_key = map_key;
5479 
5480 	cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
5481 			      -1, PERF_FLAG_FD_CLOEXEC);
5482 	if (cpu_buf->fd < 0) {
5483 		err = -errno;
5484 		pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
5485 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5486 		goto error;
5487 	}
5488 
5489 	cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
5490 			     PROT_READ | PROT_WRITE, MAP_SHARED,
5491 			     cpu_buf->fd, 0);
5492 	if (cpu_buf->base == MAP_FAILED) {
5493 		cpu_buf->base = NULL;
5494 		err = -errno;
5495 		pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
5496 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5497 		goto error;
5498 	}
5499 
5500 	if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
5501 		err = -errno;
5502 		pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
5503 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
5504 		goto error;
5505 	}
5506 
5507 	return cpu_buf;
5508 
5509 error:
5510 	perf_buffer__free_cpu_buf(pb, cpu_buf);
5511 	return (struct perf_cpu_buf *)ERR_PTR(err);
5512 }
5513 
5514 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
5515 					      struct perf_buffer_params *p);
5516 
5517 struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
5518 				     const struct perf_buffer_opts *opts)
5519 {
5520 	struct perf_buffer_params p = {};
5521 	struct perf_event_attr attr = { 0, };
5522 
5523 	attr.config = PERF_COUNT_SW_BPF_OUTPUT,
5524 	attr.type = PERF_TYPE_SOFTWARE;
5525 	attr.sample_type = PERF_SAMPLE_RAW;
5526 	attr.sample_period = 1;
5527 	attr.wakeup_events = 1;
5528 
5529 	p.attr = &attr;
5530 	p.sample_cb = opts ? opts->sample_cb : NULL;
5531 	p.lost_cb = opts ? opts->lost_cb : NULL;
5532 	p.ctx = opts ? opts->ctx : NULL;
5533 
5534 	return __perf_buffer__new(map_fd, page_cnt, &p);
5535 }
5536 
5537 struct perf_buffer *
5538 perf_buffer__new_raw(int map_fd, size_t page_cnt,
5539 		     const struct perf_buffer_raw_opts *opts)
5540 {
5541 	struct perf_buffer_params p = {};
5542 
5543 	p.attr = opts->attr;
5544 	p.event_cb = opts->event_cb;
5545 	p.ctx = opts->ctx;
5546 	p.cpu_cnt = opts->cpu_cnt;
5547 	p.cpus = opts->cpus;
5548 	p.map_keys = opts->map_keys;
5549 
5550 	return __perf_buffer__new(map_fd, page_cnt, &p);
5551 }
5552 
5553 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
5554 					      struct perf_buffer_params *p)
5555 {
5556 	struct bpf_map_info map = {};
5557 	char msg[STRERR_BUFSIZE];
5558 	struct perf_buffer *pb;
5559 	__u32 map_info_len;
5560 	int err, i;
5561 
5562 	if (page_cnt & (page_cnt - 1)) {
5563 		pr_warn("page count should be power of two, but is %zu\n",
5564 			page_cnt);
5565 		return ERR_PTR(-EINVAL);
5566 	}
5567 
5568 	map_info_len = sizeof(map);
5569 	err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
5570 	if (err) {
5571 		err = -errno;
5572 		pr_warn("failed to get map info for map FD %d: %s\n",
5573 			map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
5574 		return ERR_PTR(err);
5575 	}
5576 
5577 	if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
5578 		pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
5579 			map.name);
5580 		return ERR_PTR(-EINVAL);
5581 	}
5582 
5583 	pb = calloc(1, sizeof(*pb));
5584 	if (!pb)
5585 		return ERR_PTR(-ENOMEM);
5586 
5587 	pb->event_cb = p->event_cb;
5588 	pb->sample_cb = p->sample_cb;
5589 	pb->lost_cb = p->lost_cb;
5590 	pb->ctx = p->ctx;
5591 
5592 	pb->page_size = getpagesize();
5593 	pb->mmap_size = pb->page_size * page_cnt;
5594 	pb->map_fd = map_fd;
5595 
5596 	pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
5597 	if (pb->epoll_fd < 0) {
5598 		err = -errno;
5599 		pr_warn("failed to create epoll instance: %s\n",
5600 			libbpf_strerror_r(err, msg, sizeof(msg)));
5601 		goto error;
5602 	}
5603 
5604 	if (p->cpu_cnt > 0) {
5605 		pb->cpu_cnt = p->cpu_cnt;
5606 	} else {
5607 		pb->cpu_cnt = libbpf_num_possible_cpus();
5608 		if (pb->cpu_cnt < 0) {
5609 			err = pb->cpu_cnt;
5610 			goto error;
5611 		}
5612 		if (map.max_entries < pb->cpu_cnt)
5613 			pb->cpu_cnt = map.max_entries;
5614 	}
5615 
5616 	pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
5617 	if (!pb->events) {
5618 		err = -ENOMEM;
5619 		pr_warn("failed to allocate events: out of memory\n");
5620 		goto error;
5621 	}
5622 	pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
5623 	if (!pb->cpu_bufs) {
5624 		err = -ENOMEM;
5625 		pr_warn("failed to allocate buffers: out of memory\n");
5626 		goto error;
5627 	}
5628 
5629 	for (i = 0; i < pb->cpu_cnt; i++) {
5630 		struct perf_cpu_buf *cpu_buf;
5631 		int cpu, map_key;
5632 
5633 		cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
5634 		map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
5635 
5636 		cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
5637 		if (IS_ERR(cpu_buf)) {
5638 			err = PTR_ERR(cpu_buf);
5639 			goto error;
5640 		}
5641 
5642 		pb->cpu_bufs[i] = cpu_buf;
5643 
5644 		err = bpf_map_update_elem(pb->map_fd, &map_key,
5645 					  &cpu_buf->fd, 0);
5646 		if (err) {
5647 			err = -errno;
5648 			pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
5649 				cpu, map_key, cpu_buf->fd,
5650 				libbpf_strerror_r(err, msg, sizeof(msg)));
5651 			goto error;
5652 		}
5653 
5654 		pb->events[i].events = EPOLLIN;
5655 		pb->events[i].data.ptr = cpu_buf;
5656 		if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
5657 			      &pb->events[i]) < 0) {
5658 			err = -errno;
5659 			pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
5660 				cpu, cpu_buf->fd,
5661 				libbpf_strerror_r(err, msg, sizeof(msg)));
5662 			goto error;
5663 		}
5664 	}
5665 
5666 	return pb;
5667 
5668 error:
5669 	if (pb)
5670 		perf_buffer__free(pb);
5671 	return ERR_PTR(err);
5672 }
5673 
5674 struct perf_sample_raw {
5675 	struct perf_event_header header;
5676 	uint32_t size;
5677 	char data[0];
5678 };
5679 
5680 struct perf_sample_lost {
5681 	struct perf_event_header header;
5682 	uint64_t id;
5683 	uint64_t lost;
5684 	uint64_t sample_id;
5685 };
5686 
5687 static enum bpf_perf_event_ret
5688 perf_buffer__process_record(struct perf_event_header *e, void *ctx)
5689 {
5690 	struct perf_cpu_buf *cpu_buf = ctx;
5691 	struct perf_buffer *pb = cpu_buf->pb;
5692 	void *data = e;
5693 
5694 	/* user wants full control over parsing perf event */
5695 	if (pb->event_cb)
5696 		return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
5697 
5698 	switch (e->type) {
5699 	case PERF_RECORD_SAMPLE: {
5700 		struct perf_sample_raw *s = data;
5701 
5702 		if (pb->sample_cb)
5703 			pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
5704 		break;
5705 	}
5706 	case PERF_RECORD_LOST: {
5707 		struct perf_sample_lost *s = data;
5708 
5709 		if (pb->lost_cb)
5710 			pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
5711 		break;
5712 	}
5713 	default:
5714 		pr_warn("unknown perf sample type %d\n", e->type);
5715 		return LIBBPF_PERF_EVENT_ERROR;
5716 	}
5717 	return LIBBPF_PERF_EVENT_CONT;
5718 }
5719 
5720 static int perf_buffer__process_records(struct perf_buffer *pb,
5721 					struct perf_cpu_buf *cpu_buf)
5722 {
5723 	enum bpf_perf_event_ret ret;
5724 
5725 	ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
5726 					 pb->page_size, &cpu_buf->buf,
5727 					 &cpu_buf->buf_size,
5728 					 perf_buffer__process_record, cpu_buf);
5729 	if (ret != LIBBPF_PERF_EVENT_CONT)
5730 		return ret;
5731 	return 0;
5732 }
5733 
5734 int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
5735 {
5736 	int i, cnt, err;
5737 
5738 	cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
5739 	for (i = 0; i < cnt; i++) {
5740 		struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
5741 
5742 		err = perf_buffer__process_records(pb, cpu_buf);
5743 		if (err) {
5744 			pr_warn("error while processing records: %d\n", err);
5745 			return err;
5746 		}
5747 	}
5748 	return cnt < 0 ? -errno : cnt;
5749 }
5750 
5751 struct bpf_prog_info_array_desc {
5752 	int	array_offset;	/* e.g. offset of jited_prog_insns */
5753 	int	count_offset;	/* e.g. offset of jited_prog_len */
5754 	int	size_offset;	/* > 0: offset of rec size,
5755 				 * < 0: fix size of -size_offset
5756 				 */
5757 };
5758 
5759 static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
5760 	[BPF_PROG_INFO_JITED_INSNS] = {
5761 		offsetof(struct bpf_prog_info, jited_prog_insns),
5762 		offsetof(struct bpf_prog_info, jited_prog_len),
5763 		-1,
5764 	},
5765 	[BPF_PROG_INFO_XLATED_INSNS] = {
5766 		offsetof(struct bpf_prog_info, xlated_prog_insns),
5767 		offsetof(struct bpf_prog_info, xlated_prog_len),
5768 		-1,
5769 	},
5770 	[BPF_PROG_INFO_MAP_IDS] = {
5771 		offsetof(struct bpf_prog_info, map_ids),
5772 		offsetof(struct bpf_prog_info, nr_map_ids),
5773 		-(int)sizeof(__u32),
5774 	},
5775 	[BPF_PROG_INFO_JITED_KSYMS] = {
5776 		offsetof(struct bpf_prog_info, jited_ksyms),
5777 		offsetof(struct bpf_prog_info, nr_jited_ksyms),
5778 		-(int)sizeof(__u64),
5779 	},
5780 	[BPF_PROG_INFO_JITED_FUNC_LENS] = {
5781 		offsetof(struct bpf_prog_info, jited_func_lens),
5782 		offsetof(struct bpf_prog_info, nr_jited_func_lens),
5783 		-(int)sizeof(__u32),
5784 	},
5785 	[BPF_PROG_INFO_FUNC_INFO] = {
5786 		offsetof(struct bpf_prog_info, func_info),
5787 		offsetof(struct bpf_prog_info, nr_func_info),
5788 		offsetof(struct bpf_prog_info, func_info_rec_size),
5789 	},
5790 	[BPF_PROG_INFO_LINE_INFO] = {
5791 		offsetof(struct bpf_prog_info, line_info),
5792 		offsetof(struct bpf_prog_info, nr_line_info),
5793 		offsetof(struct bpf_prog_info, line_info_rec_size),
5794 	},
5795 	[BPF_PROG_INFO_JITED_LINE_INFO] = {
5796 		offsetof(struct bpf_prog_info, jited_line_info),
5797 		offsetof(struct bpf_prog_info, nr_jited_line_info),
5798 		offsetof(struct bpf_prog_info, jited_line_info_rec_size),
5799 	},
5800 	[BPF_PROG_INFO_PROG_TAGS] = {
5801 		offsetof(struct bpf_prog_info, prog_tags),
5802 		offsetof(struct bpf_prog_info, nr_prog_tags),
5803 		-(int)sizeof(__u8) * BPF_TAG_SIZE,
5804 	},
5805 
5806 };
5807 
5808 static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info, int offset)
5809 {
5810 	__u32 *array = (__u32 *)info;
5811 
5812 	if (offset >= 0)
5813 		return array[offset / sizeof(__u32)];
5814 	return -(int)offset;
5815 }
5816 
5817 static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info, int offset)
5818 {
5819 	__u64 *array = (__u64 *)info;
5820 
5821 	if (offset >= 0)
5822 		return array[offset / sizeof(__u64)];
5823 	return -(int)offset;
5824 }
5825 
5826 static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
5827 					 __u32 val)
5828 {
5829 	__u32 *array = (__u32 *)info;
5830 
5831 	if (offset >= 0)
5832 		array[offset / sizeof(__u32)] = val;
5833 }
5834 
5835 static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
5836 					 __u64 val)
5837 {
5838 	__u64 *array = (__u64 *)info;
5839 
5840 	if (offset >= 0)
5841 		array[offset / sizeof(__u64)] = val;
5842 }
5843 
5844 struct bpf_prog_info_linear *
5845 bpf_program__get_prog_info_linear(int fd, __u64 arrays)
5846 {
5847 	struct bpf_prog_info_linear *info_linear;
5848 	struct bpf_prog_info info = {};
5849 	__u32 info_len = sizeof(info);
5850 	__u32 data_len = 0;
5851 	int i, err;
5852 	void *ptr;
5853 
5854 	if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
5855 		return ERR_PTR(-EINVAL);
5856 
5857 	/* step 1: get array dimensions */
5858 	err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
5859 	if (err) {
5860 		pr_debug("can't get prog info: %s", strerror(errno));
5861 		return ERR_PTR(-EFAULT);
5862 	}
5863 
5864 	/* step 2: calculate total size of all arrays */
5865 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5866 		bool include_array = (arrays & (1UL << i)) > 0;
5867 		struct bpf_prog_info_array_desc *desc;
5868 		__u32 count, size;
5869 
5870 		desc = bpf_prog_info_array_desc + i;
5871 
5872 		/* kernel is too old to support this field */
5873 		if (info_len < desc->array_offset + sizeof(__u32) ||
5874 		    info_len < desc->count_offset + sizeof(__u32) ||
5875 		    (desc->size_offset > 0 && info_len < desc->size_offset))
5876 			include_array = false;
5877 
5878 		if (!include_array) {
5879 			arrays &= ~(1UL << i);	/* clear the bit */
5880 			continue;
5881 		}
5882 
5883 		count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5884 		size  = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5885 
5886 		data_len += count * size;
5887 	}
5888 
5889 	/* step 3: allocate continuous memory */
5890 	data_len = roundup(data_len, sizeof(__u64));
5891 	info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
5892 	if (!info_linear)
5893 		return ERR_PTR(-ENOMEM);
5894 
5895 	/* step 4: fill data to info_linear->info */
5896 	info_linear->arrays = arrays;
5897 	memset(&info_linear->info, 0, sizeof(info));
5898 	ptr = info_linear->data;
5899 
5900 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5901 		struct bpf_prog_info_array_desc *desc;
5902 		__u32 count, size;
5903 
5904 		if ((arrays & (1UL << i)) == 0)
5905 			continue;
5906 
5907 		desc  = bpf_prog_info_array_desc + i;
5908 		count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5909 		size  = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5910 		bpf_prog_info_set_offset_u32(&info_linear->info,
5911 					     desc->count_offset, count);
5912 		bpf_prog_info_set_offset_u32(&info_linear->info,
5913 					     desc->size_offset, size);
5914 		bpf_prog_info_set_offset_u64(&info_linear->info,
5915 					     desc->array_offset,
5916 					     ptr_to_u64(ptr));
5917 		ptr += count * size;
5918 	}
5919 
5920 	/* step 5: call syscall again to get required arrays */
5921 	err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
5922 	if (err) {
5923 		pr_debug("can't get prog info: %s", strerror(errno));
5924 		free(info_linear);
5925 		return ERR_PTR(-EFAULT);
5926 	}
5927 
5928 	/* step 6: verify the data */
5929 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5930 		struct bpf_prog_info_array_desc *desc;
5931 		__u32 v1, v2;
5932 
5933 		if ((arrays & (1UL << i)) == 0)
5934 			continue;
5935 
5936 		desc = bpf_prog_info_array_desc + i;
5937 		v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
5938 		v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
5939 						   desc->count_offset);
5940 		if (v1 != v2)
5941 			pr_warn("%s: mismatch in element count\n", __func__);
5942 
5943 		v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
5944 		v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
5945 						   desc->size_offset);
5946 		if (v1 != v2)
5947 			pr_warn("%s: mismatch in rec size\n", __func__);
5948 	}
5949 
5950 	/* step 7: update info_len and data_len */
5951 	info_linear->info_len = sizeof(struct bpf_prog_info);
5952 	info_linear->data_len = data_len;
5953 
5954 	return info_linear;
5955 }
5956 
5957 void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
5958 {
5959 	int i;
5960 
5961 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5962 		struct bpf_prog_info_array_desc *desc;
5963 		__u64 addr, offs;
5964 
5965 		if ((info_linear->arrays & (1UL << i)) == 0)
5966 			continue;
5967 
5968 		desc = bpf_prog_info_array_desc + i;
5969 		addr = bpf_prog_info_read_offset_u64(&info_linear->info,
5970 						     desc->array_offset);
5971 		offs = addr - ptr_to_u64(info_linear->data);
5972 		bpf_prog_info_set_offset_u64(&info_linear->info,
5973 					     desc->array_offset, offs);
5974 	}
5975 }
5976 
5977 void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
5978 {
5979 	int i;
5980 
5981 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
5982 		struct bpf_prog_info_array_desc *desc;
5983 		__u64 addr, offs;
5984 
5985 		if ((info_linear->arrays & (1UL << i)) == 0)
5986 			continue;
5987 
5988 		desc = bpf_prog_info_array_desc + i;
5989 		offs = bpf_prog_info_read_offset_u64(&info_linear->info,
5990 						     desc->array_offset);
5991 		addr = offs + ptr_to_u64(info_linear->data);
5992 		bpf_prog_info_set_offset_u64(&info_linear->info,
5993 					     desc->array_offset, addr);
5994 	}
5995 }
5996 
5997 int libbpf_num_possible_cpus(void)
5998 {
5999 	static const char *fcpu = "/sys/devices/system/cpu/possible";
6000 	int len = 0, n = 0, il = 0, ir = 0;
6001 	unsigned int start = 0, end = 0;
6002 	int tmp_cpus = 0;
6003 	static int cpus;
6004 	char buf[128];
6005 	int error = 0;
6006 	int fd = -1;
6007 
6008 	tmp_cpus = READ_ONCE(cpus);
6009 	if (tmp_cpus > 0)
6010 		return tmp_cpus;
6011 
6012 	fd = open(fcpu, O_RDONLY);
6013 	if (fd < 0) {
6014 		error = errno;
6015 		pr_warn("Failed to open file %s: %s\n", fcpu, strerror(error));
6016 		return -error;
6017 	}
6018 	len = read(fd, buf, sizeof(buf));
6019 	close(fd);
6020 	if (len <= 0) {
6021 		error = len ? errno : EINVAL;
6022 		pr_warn("Failed to read # of possible cpus from %s: %s\n",
6023 			fcpu, strerror(error));
6024 		return -error;
6025 	}
6026 	if (len == sizeof(buf)) {
6027 		pr_warn("File %s size overflow\n", fcpu);
6028 		return -EOVERFLOW;
6029 	}
6030 	buf[len] = '\0';
6031 
6032 	for (ir = 0, tmp_cpus = 0; ir <= len; ir++) {
6033 		/* Each sub string separated by ',' has format \d+-\d+ or \d+ */
6034 		if (buf[ir] == ',' || buf[ir] == '\0') {
6035 			buf[ir] = '\0';
6036 			n = sscanf(&buf[il], "%u-%u", &start, &end);
6037 			if (n <= 0) {
6038 				pr_warn("Failed to get # CPUs from %s\n",
6039 					&buf[il]);
6040 				return -EINVAL;
6041 			} else if (n == 1) {
6042 				end = start;
6043 			}
6044 			tmp_cpus += end - start + 1;
6045 			il = ir + 1;
6046 		}
6047 	}
6048 	if (tmp_cpus <= 0) {
6049 		pr_warn("Invalid #CPUs %d from %s\n", tmp_cpus, fcpu);
6050 		return -EINVAL;
6051 	}
6052 
6053 	WRITE_ONCE(cpus, tmp_cpus);
6054 	return tmp_cpus;
6055 }
6056