xref: /linux-6.15/tools/lib/bpf/libbpf.c (revision f0fdfefb)
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 <[email protected]>
7  * Copyright (C) 2015 Wang Nan <[email protected]>
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 <limits.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <endian.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <asm/unistd.h>
29 #include <linux/err.h>
30 #include <linux/kernel.h>
31 #include <linux/bpf.h>
32 #include <linux/btf.h>
33 #include <linux/filter.h>
34 #include <linux/list.h>
35 #include <linux/limits.h>
36 #include <linux/perf_event.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/version.h>
39 #include <sys/epoll.h>
40 #include <sys/ioctl.h>
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/vfs.h>
45 #include <sys/utsname.h>
46 #include <sys/resource.h>
47 #include <libelf.h>
48 #include <gelf.h>
49 #include <zlib.h>
50 
51 #include "libbpf.h"
52 #include "bpf.h"
53 #include "btf.h"
54 #include "str_error.h"
55 #include "libbpf_internal.h"
56 #include "hashmap.h"
57 
58 #ifndef EM_BPF
59 #define EM_BPF 247
60 #endif
61 
62 #ifndef BPF_FS_MAGIC
63 #define BPF_FS_MAGIC		0xcafe4a11
64 #endif
65 
66 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
67  * compilation if user enables corresponding warning. Disable it explicitly.
68  */
69 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
70 
71 #define __printf(a, b)	__attribute__((format(printf, a, b)))
72 
73 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
74 static struct bpf_program *bpf_object__find_prog_by_idx(struct bpf_object *obj,
75 							int idx);
76 static const struct btf_type *
77 skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id);
78 
79 static int __base_pr(enum libbpf_print_level level, const char *format,
80 		     va_list args)
81 {
82 	if (level == LIBBPF_DEBUG)
83 		return 0;
84 
85 	return vfprintf(stderr, format, args);
86 }
87 
88 static libbpf_print_fn_t __libbpf_pr = __base_pr;
89 
90 libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
91 {
92 	libbpf_print_fn_t old_print_fn = __libbpf_pr;
93 
94 	__libbpf_pr = fn;
95 	return old_print_fn;
96 }
97 
98 __printf(2, 3)
99 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
100 {
101 	va_list args;
102 
103 	if (!__libbpf_pr)
104 		return;
105 
106 	va_start(args, format);
107 	__libbpf_pr(level, format, args);
108 	va_end(args);
109 }
110 
111 static void pr_perm_msg(int err)
112 {
113 	struct rlimit limit;
114 	char buf[100];
115 
116 	if (err != -EPERM || geteuid() != 0)
117 		return;
118 
119 	err = getrlimit(RLIMIT_MEMLOCK, &limit);
120 	if (err)
121 		return;
122 
123 	if (limit.rlim_cur == RLIM_INFINITY)
124 		return;
125 
126 	if (limit.rlim_cur < 1024)
127 		snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
128 	else if (limit.rlim_cur < 1024*1024)
129 		snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
130 	else
131 		snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
132 
133 	pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
134 		buf);
135 }
136 
137 #define STRERR_BUFSIZE  128
138 
139 /* Copied from tools/perf/util/util.h */
140 #ifndef zfree
141 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
142 #endif
143 
144 #ifndef zclose
145 # define zclose(fd) ({			\
146 	int ___err = 0;			\
147 	if ((fd) >= 0)			\
148 		___err = close((fd));	\
149 	fd = -1;			\
150 	___err; })
151 #endif
152 
153 static inline __u64 ptr_to_u64(const void *ptr)
154 {
155 	return (__u64) (unsigned long) ptr;
156 }
157 
158 enum kern_feature_id {
159 	/* v4.14: kernel support for program & map names. */
160 	FEAT_PROG_NAME,
161 	/* v5.2: kernel support for global data sections. */
162 	FEAT_GLOBAL_DATA,
163 	/* BTF support */
164 	FEAT_BTF,
165 	/* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
166 	FEAT_BTF_FUNC,
167 	/* BTF_KIND_VAR and BTF_KIND_DATASEC support */
168 	FEAT_BTF_DATASEC,
169 	/* BTF_FUNC_GLOBAL is supported */
170 	FEAT_BTF_GLOBAL_FUNC,
171 	/* BPF_F_MMAPABLE is supported for arrays */
172 	FEAT_ARRAY_MMAP,
173 	/* kernel support for expected_attach_type in BPF_PROG_LOAD */
174 	FEAT_EXP_ATTACH_TYPE,
175 	/* bpf_probe_read_{kernel,user}[_str] helpers */
176 	FEAT_PROBE_READ_KERN,
177 	__FEAT_CNT,
178 };
179 
180 static bool kernel_supports(enum kern_feature_id feat_id);
181 
182 enum reloc_type {
183 	RELO_LD64,
184 	RELO_CALL,
185 	RELO_DATA,
186 	RELO_EXTERN,
187 };
188 
189 struct reloc_desc {
190 	enum reloc_type type;
191 	int insn_idx;
192 	int map_idx;
193 	int sym_off;
194 };
195 
196 struct bpf_sec_def;
197 
198 typedef struct bpf_link *(*attach_fn_t)(const struct bpf_sec_def *sec,
199 					struct bpf_program *prog);
200 
201 struct bpf_sec_def {
202 	const char *sec;
203 	size_t len;
204 	enum bpf_prog_type prog_type;
205 	enum bpf_attach_type expected_attach_type;
206 	bool is_exp_attach_type_optional;
207 	bool is_attachable;
208 	bool is_attach_btf;
209 	attach_fn_t attach_fn;
210 };
211 
212 /*
213  * bpf_prog should be a better name but it has been used in
214  * linux/filter.h.
215  */
216 struct bpf_program {
217 	/* Index in elf obj file, for relocation use. */
218 	int idx;
219 	char *name;
220 	int prog_ifindex;
221 	char *section_name;
222 	const struct bpf_sec_def *sec_def;
223 	/* section_name with / replaced by _; makes recursive pinning
224 	 * in bpf_object__pin_programs easier
225 	 */
226 	char *pin_name;
227 	struct bpf_insn *insns;
228 	size_t insns_cnt, main_prog_cnt;
229 	enum bpf_prog_type type;
230 	bool load;
231 
232 	struct reloc_desc *reloc_desc;
233 	int nr_reloc;
234 	int log_level;
235 
236 	struct {
237 		int nr;
238 		int *fds;
239 	} instances;
240 	bpf_program_prep_t preprocessor;
241 
242 	struct bpf_object *obj;
243 	void *priv;
244 	bpf_program_clear_priv_t clear_priv;
245 
246 	enum bpf_attach_type expected_attach_type;
247 	__u32 attach_btf_id;
248 	__u32 attach_prog_fd;
249 	void *func_info;
250 	__u32 func_info_rec_size;
251 	__u32 func_info_cnt;
252 
253 	void *line_info;
254 	__u32 line_info_rec_size;
255 	__u32 line_info_cnt;
256 	__u32 prog_flags;
257 };
258 
259 struct bpf_struct_ops {
260 	const char *tname;
261 	const struct btf_type *type;
262 	struct bpf_program **progs;
263 	__u32 *kern_func_off;
264 	/* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
265 	void *data;
266 	/* e.g. struct bpf_struct_ops_tcp_congestion_ops in
267 	 *      btf_vmlinux's format.
268 	 * struct bpf_struct_ops_tcp_congestion_ops {
269 	 *	[... some other kernel fields ...]
270 	 *	struct tcp_congestion_ops data;
271 	 * }
272 	 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
273 	 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
274 	 * from "data".
275 	 */
276 	void *kern_vdata;
277 	__u32 type_id;
278 };
279 
280 #define DATA_SEC ".data"
281 #define BSS_SEC ".bss"
282 #define RODATA_SEC ".rodata"
283 #define KCONFIG_SEC ".kconfig"
284 #define KSYMS_SEC ".ksyms"
285 #define STRUCT_OPS_SEC ".struct_ops"
286 
287 enum libbpf_map_type {
288 	LIBBPF_MAP_UNSPEC,
289 	LIBBPF_MAP_DATA,
290 	LIBBPF_MAP_BSS,
291 	LIBBPF_MAP_RODATA,
292 	LIBBPF_MAP_KCONFIG,
293 };
294 
295 static const char * const libbpf_type_to_btf_name[] = {
296 	[LIBBPF_MAP_DATA]	= DATA_SEC,
297 	[LIBBPF_MAP_BSS]	= BSS_SEC,
298 	[LIBBPF_MAP_RODATA]	= RODATA_SEC,
299 	[LIBBPF_MAP_KCONFIG]	= KCONFIG_SEC,
300 };
301 
302 struct bpf_map {
303 	char *name;
304 	int fd;
305 	int sec_idx;
306 	size_t sec_offset;
307 	int map_ifindex;
308 	int inner_map_fd;
309 	struct bpf_map_def def;
310 	__u32 numa_node;
311 	__u32 btf_var_idx;
312 	__u32 btf_key_type_id;
313 	__u32 btf_value_type_id;
314 	__u32 btf_vmlinux_value_type_id;
315 	void *priv;
316 	bpf_map_clear_priv_t clear_priv;
317 	enum libbpf_map_type libbpf_type;
318 	void *mmaped;
319 	struct bpf_struct_ops *st_ops;
320 	struct bpf_map *inner_map;
321 	void **init_slots;
322 	int init_slots_sz;
323 	char *pin_path;
324 	bool pinned;
325 	bool reused;
326 };
327 
328 enum extern_type {
329 	EXT_UNKNOWN,
330 	EXT_KCFG,
331 	EXT_KSYM,
332 };
333 
334 enum kcfg_type {
335 	KCFG_UNKNOWN,
336 	KCFG_CHAR,
337 	KCFG_BOOL,
338 	KCFG_INT,
339 	KCFG_TRISTATE,
340 	KCFG_CHAR_ARR,
341 };
342 
343 struct extern_desc {
344 	enum extern_type type;
345 	int sym_idx;
346 	int btf_id;
347 	int sec_btf_id;
348 	const char *name;
349 	bool is_set;
350 	bool is_weak;
351 	union {
352 		struct {
353 			enum kcfg_type type;
354 			int sz;
355 			int align;
356 			int data_off;
357 			bool is_signed;
358 		} kcfg;
359 		struct {
360 			unsigned long long addr;
361 		} ksym;
362 	};
363 };
364 
365 static LIST_HEAD(bpf_objects_list);
366 
367 struct bpf_object {
368 	char name[BPF_OBJ_NAME_LEN];
369 	char license[64];
370 	__u32 kern_version;
371 
372 	struct bpf_program *programs;
373 	size_t nr_programs;
374 	struct bpf_map *maps;
375 	size_t nr_maps;
376 	size_t maps_cap;
377 
378 	char *kconfig;
379 	struct extern_desc *externs;
380 	int nr_extern;
381 	int kconfig_map_idx;
382 
383 	bool loaded;
384 	bool has_pseudo_calls;
385 
386 	/*
387 	 * Information when doing elf related work. Only valid if fd
388 	 * is valid.
389 	 */
390 	struct {
391 		int fd;
392 		const void *obj_buf;
393 		size_t obj_buf_sz;
394 		Elf *elf;
395 		GElf_Ehdr ehdr;
396 		Elf_Data *symbols;
397 		Elf_Data *data;
398 		Elf_Data *rodata;
399 		Elf_Data *bss;
400 		Elf_Data *st_ops_data;
401 		size_t strtabidx;
402 		struct {
403 			GElf_Shdr shdr;
404 			Elf_Data *data;
405 		} *reloc_sects;
406 		int nr_reloc_sects;
407 		int maps_shndx;
408 		int btf_maps_shndx;
409 		__u32 btf_maps_sec_btf_id;
410 		int text_shndx;
411 		int symbols_shndx;
412 		int data_shndx;
413 		int rodata_shndx;
414 		int bss_shndx;
415 		int st_ops_shndx;
416 	} efile;
417 	/*
418 	 * All loaded bpf_object is linked in a list, which is
419 	 * hidden to caller. bpf_objects__<func> handlers deal with
420 	 * all objects.
421 	 */
422 	struct list_head list;
423 
424 	struct btf *btf;
425 	/* Parse and load BTF vmlinux if any of the programs in the object need
426 	 * it at load time.
427 	 */
428 	struct btf *btf_vmlinux;
429 	struct btf_ext *btf_ext;
430 
431 	void *priv;
432 	bpf_object_clear_priv_t clear_priv;
433 
434 	char path[];
435 };
436 #define obj_elf_valid(o)	((o)->efile.elf)
437 
438 void bpf_program__unload(struct bpf_program *prog)
439 {
440 	int i;
441 
442 	if (!prog)
443 		return;
444 
445 	/*
446 	 * If the object is opened but the program was never loaded,
447 	 * it is possible that prog->instances.nr == -1.
448 	 */
449 	if (prog->instances.nr > 0) {
450 		for (i = 0; i < prog->instances.nr; i++)
451 			zclose(prog->instances.fds[i]);
452 	} else if (prog->instances.nr != -1) {
453 		pr_warn("Internal error: instances.nr is %d\n",
454 			prog->instances.nr);
455 	}
456 
457 	prog->instances.nr = -1;
458 	zfree(&prog->instances.fds);
459 
460 	zfree(&prog->func_info);
461 	zfree(&prog->line_info);
462 }
463 
464 static void bpf_program__exit(struct bpf_program *prog)
465 {
466 	if (!prog)
467 		return;
468 
469 	if (prog->clear_priv)
470 		prog->clear_priv(prog, prog->priv);
471 
472 	prog->priv = NULL;
473 	prog->clear_priv = NULL;
474 
475 	bpf_program__unload(prog);
476 	zfree(&prog->name);
477 	zfree(&prog->section_name);
478 	zfree(&prog->pin_name);
479 	zfree(&prog->insns);
480 	zfree(&prog->reloc_desc);
481 
482 	prog->nr_reloc = 0;
483 	prog->insns_cnt = 0;
484 	prog->idx = -1;
485 }
486 
487 static char *__bpf_program__pin_name(struct bpf_program *prog)
488 {
489 	char *name, *p;
490 
491 	name = p = strdup(prog->section_name);
492 	while ((p = strchr(p, '/')))
493 		*p = '_';
494 
495 	return name;
496 }
497 
498 static int
499 bpf_program__init(void *data, size_t size, char *section_name, int idx,
500 		  struct bpf_program *prog)
501 {
502 	const size_t bpf_insn_sz = sizeof(struct bpf_insn);
503 
504 	if (size == 0 || size % bpf_insn_sz) {
505 		pr_warn("corrupted section '%s', size: %zu\n",
506 			section_name, size);
507 		return -EINVAL;
508 	}
509 
510 	memset(prog, 0, sizeof(*prog));
511 
512 	prog->section_name = strdup(section_name);
513 	if (!prog->section_name) {
514 		pr_warn("failed to alloc name for prog under section(%d) %s\n",
515 			idx, section_name);
516 		goto errout;
517 	}
518 
519 	prog->pin_name = __bpf_program__pin_name(prog);
520 	if (!prog->pin_name) {
521 		pr_warn("failed to alloc pin name for prog under section(%d) %s\n",
522 			idx, section_name);
523 		goto errout;
524 	}
525 
526 	prog->insns = malloc(size);
527 	if (!prog->insns) {
528 		pr_warn("failed to alloc insns for prog under section %s\n",
529 			section_name);
530 		goto errout;
531 	}
532 	prog->insns_cnt = size / bpf_insn_sz;
533 	memcpy(prog->insns, data, size);
534 	prog->idx = idx;
535 	prog->instances.fds = NULL;
536 	prog->instances.nr = -1;
537 	prog->type = BPF_PROG_TYPE_UNSPEC;
538 	prog->load = true;
539 
540 	return 0;
541 errout:
542 	bpf_program__exit(prog);
543 	return -ENOMEM;
544 }
545 
546 static int
547 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
548 			char *section_name, int idx)
549 {
550 	struct bpf_program prog, *progs;
551 	int nr_progs, err;
552 
553 	err = bpf_program__init(data, size, section_name, idx, &prog);
554 	if (err)
555 		return err;
556 
557 	progs = obj->programs;
558 	nr_progs = obj->nr_programs;
559 
560 	progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
561 	if (!progs) {
562 		/*
563 		 * In this case the original obj->programs
564 		 * is still valid, so don't need special treat for
565 		 * bpf_close_object().
566 		 */
567 		pr_warn("failed to alloc a new program under section '%s'\n",
568 			section_name);
569 		bpf_program__exit(&prog);
570 		return -ENOMEM;
571 	}
572 
573 	pr_debug("found program %s\n", prog.section_name);
574 	obj->programs = progs;
575 	obj->nr_programs = nr_progs + 1;
576 	prog.obj = obj;
577 	progs[nr_progs] = prog;
578 	return 0;
579 }
580 
581 static int
582 bpf_object__init_prog_names(struct bpf_object *obj)
583 {
584 	Elf_Data *symbols = obj->efile.symbols;
585 	struct bpf_program *prog;
586 	size_t pi, si;
587 
588 	for (pi = 0; pi < obj->nr_programs; pi++) {
589 		const char *name = NULL;
590 
591 		prog = &obj->programs[pi];
592 
593 		for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
594 		     si++) {
595 			GElf_Sym sym;
596 
597 			if (!gelf_getsym(symbols, si, &sym))
598 				continue;
599 			if (sym.st_shndx != prog->idx)
600 				continue;
601 			if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
602 				continue;
603 
604 			name = elf_strptr(obj->efile.elf,
605 					  obj->efile.strtabidx,
606 					  sym.st_name);
607 			if (!name) {
608 				pr_warn("failed to get sym name string for prog %s\n",
609 					prog->section_name);
610 				return -LIBBPF_ERRNO__LIBELF;
611 			}
612 		}
613 
614 		if (!name && prog->idx == obj->efile.text_shndx)
615 			name = ".text";
616 
617 		if (!name) {
618 			pr_warn("failed to find sym for prog %s\n",
619 				prog->section_name);
620 			return -EINVAL;
621 		}
622 
623 		prog->name = strdup(name);
624 		if (!prog->name) {
625 			pr_warn("failed to allocate memory for prog sym %s\n",
626 				name);
627 			return -ENOMEM;
628 		}
629 	}
630 
631 	return 0;
632 }
633 
634 static __u32 get_kernel_version(void)
635 {
636 	__u32 major, minor, patch;
637 	struct utsname info;
638 
639 	uname(&info);
640 	if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
641 		return 0;
642 	return KERNEL_VERSION(major, minor, patch);
643 }
644 
645 static const struct btf_member *
646 find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
647 {
648 	struct btf_member *m;
649 	int i;
650 
651 	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
652 		if (btf_member_bit_offset(t, i) == bit_offset)
653 			return m;
654 	}
655 
656 	return NULL;
657 }
658 
659 static const struct btf_member *
660 find_member_by_name(const struct btf *btf, const struct btf_type *t,
661 		    const char *name)
662 {
663 	struct btf_member *m;
664 	int i;
665 
666 	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
667 		if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
668 			return m;
669 	}
670 
671 	return NULL;
672 }
673 
674 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
675 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
676 				   const char *name, __u32 kind);
677 
678 static int
679 find_struct_ops_kern_types(const struct btf *btf, const char *tname,
680 			   const struct btf_type **type, __u32 *type_id,
681 			   const struct btf_type **vtype, __u32 *vtype_id,
682 			   const struct btf_member **data_member)
683 {
684 	const struct btf_type *kern_type, *kern_vtype;
685 	const struct btf_member *kern_data_member;
686 	__s32 kern_vtype_id, kern_type_id;
687 	__u32 i;
688 
689 	kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
690 	if (kern_type_id < 0) {
691 		pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
692 			tname);
693 		return kern_type_id;
694 	}
695 	kern_type = btf__type_by_id(btf, kern_type_id);
696 
697 	/* Find the corresponding "map_value" type that will be used
698 	 * in map_update(BPF_MAP_TYPE_STRUCT_OPS).  For example,
699 	 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
700 	 * btf_vmlinux.
701 	 */
702 	kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
703 						tname, BTF_KIND_STRUCT);
704 	if (kern_vtype_id < 0) {
705 		pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
706 			STRUCT_OPS_VALUE_PREFIX, tname);
707 		return kern_vtype_id;
708 	}
709 	kern_vtype = btf__type_by_id(btf, kern_vtype_id);
710 
711 	/* Find "struct tcp_congestion_ops" from
712 	 * struct bpf_struct_ops_tcp_congestion_ops {
713 	 *	[ ... ]
714 	 *	struct tcp_congestion_ops data;
715 	 * }
716 	 */
717 	kern_data_member = btf_members(kern_vtype);
718 	for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
719 		if (kern_data_member->type == kern_type_id)
720 			break;
721 	}
722 	if (i == btf_vlen(kern_vtype)) {
723 		pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
724 			tname, STRUCT_OPS_VALUE_PREFIX, tname);
725 		return -EINVAL;
726 	}
727 
728 	*type = kern_type;
729 	*type_id = kern_type_id;
730 	*vtype = kern_vtype;
731 	*vtype_id = kern_vtype_id;
732 	*data_member = kern_data_member;
733 
734 	return 0;
735 }
736 
737 static bool bpf_map__is_struct_ops(const struct bpf_map *map)
738 {
739 	return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
740 }
741 
742 /* Init the map's fields that depend on kern_btf */
743 static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
744 					 const struct btf *btf,
745 					 const struct btf *kern_btf)
746 {
747 	const struct btf_member *member, *kern_member, *kern_data_member;
748 	const struct btf_type *type, *kern_type, *kern_vtype;
749 	__u32 i, kern_type_id, kern_vtype_id, kern_data_off;
750 	struct bpf_struct_ops *st_ops;
751 	void *data, *kern_data;
752 	const char *tname;
753 	int err;
754 
755 	st_ops = map->st_ops;
756 	type = st_ops->type;
757 	tname = st_ops->tname;
758 	err = find_struct_ops_kern_types(kern_btf, tname,
759 					 &kern_type, &kern_type_id,
760 					 &kern_vtype, &kern_vtype_id,
761 					 &kern_data_member);
762 	if (err)
763 		return err;
764 
765 	pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
766 		 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
767 
768 	map->def.value_size = kern_vtype->size;
769 	map->btf_vmlinux_value_type_id = kern_vtype_id;
770 
771 	st_ops->kern_vdata = calloc(1, kern_vtype->size);
772 	if (!st_ops->kern_vdata)
773 		return -ENOMEM;
774 
775 	data = st_ops->data;
776 	kern_data_off = kern_data_member->offset / 8;
777 	kern_data = st_ops->kern_vdata + kern_data_off;
778 
779 	member = btf_members(type);
780 	for (i = 0; i < btf_vlen(type); i++, member++) {
781 		const struct btf_type *mtype, *kern_mtype;
782 		__u32 mtype_id, kern_mtype_id;
783 		void *mdata, *kern_mdata;
784 		__s64 msize, kern_msize;
785 		__u32 moff, kern_moff;
786 		__u32 kern_member_idx;
787 		const char *mname;
788 
789 		mname = btf__name_by_offset(btf, member->name_off);
790 		kern_member = find_member_by_name(kern_btf, kern_type, mname);
791 		if (!kern_member) {
792 			pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
793 				map->name, mname);
794 			return -ENOTSUP;
795 		}
796 
797 		kern_member_idx = kern_member - btf_members(kern_type);
798 		if (btf_member_bitfield_size(type, i) ||
799 		    btf_member_bitfield_size(kern_type, kern_member_idx)) {
800 			pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
801 				map->name, mname);
802 			return -ENOTSUP;
803 		}
804 
805 		moff = member->offset / 8;
806 		kern_moff = kern_member->offset / 8;
807 
808 		mdata = data + moff;
809 		kern_mdata = kern_data + kern_moff;
810 
811 		mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
812 		kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
813 						    &kern_mtype_id);
814 		if (BTF_INFO_KIND(mtype->info) !=
815 		    BTF_INFO_KIND(kern_mtype->info)) {
816 			pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
817 				map->name, mname, BTF_INFO_KIND(mtype->info),
818 				BTF_INFO_KIND(kern_mtype->info));
819 			return -ENOTSUP;
820 		}
821 
822 		if (btf_is_ptr(mtype)) {
823 			struct bpf_program *prog;
824 
825 			mtype = skip_mods_and_typedefs(btf, mtype->type, &mtype_id);
826 			kern_mtype = skip_mods_and_typedefs(kern_btf,
827 							    kern_mtype->type,
828 							    &kern_mtype_id);
829 			if (!btf_is_func_proto(mtype) ||
830 			    !btf_is_func_proto(kern_mtype)) {
831 				pr_warn("struct_ops init_kern %s: non func ptr %s is not supported\n",
832 					map->name, mname);
833 				return -ENOTSUP;
834 			}
835 
836 			prog = st_ops->progs[i];
837 			if (!prog) {
838 				pr_debug("struct_ops init_kern %s: func ptr %s is not set\n",
839 					 map->name, mname);
840 				continue;
841 			}
842 
843 			prog->attach_btf_id = kern_type_id;
844 			prog->expected_attach_type = kern_member_idx;
845 
846 			st_ops->kern_func_off[i] = kern_data_off + kern_moff;
847 
848 			pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
849 				 map->name, mname, prog->name, moff,
850 				 kern_moff);
851 
852 			continue;
853 		}
854 
855 		msize = btf__resolve_size(btf, mtype_id);
856 		kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
857 		if (msize < 0 || kern_msize < 0 || msize != kern_msize) {
858 			pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
859 				map->name, mname, (ssize_t)msize,
860 				(ssize_t)kern_msize);
861 			return -ENOTSUP;
862 		}
863 
864 		pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
865 			 map->name, mname, (unsigned int)msize,
866 			 moff, kern_moff);
867 		memcpy(kern_mdata, mdata, msize);
868 	}
869 
870 	return 0;
871 }
872 
873 static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
874 {
875 	struct bpf_map *map;
876 	size_t i;
877 	int err;
878 
879 	for (i = 0; i < obj->nr_maps; i++) {
880 		map = &obj->maps[i];
881 
882 		if (!bpf_map__is_struct_ops(map))
883 			continue;
884 
885 		err = bpf_map__init_kern_struct_ops(map, obj->btf,
886 						    obj->btf_vmlinux);
887 		if (err)
888 			return err;
889 	}
890 
891 	return 0;
892 }
893 
894 static int bpf_object__init_struct_ops_maps(struct bpf_object *obj)
895 {
896 	const struct btf_type *type, *datasec;
897 	const struct btf_var_secinfo *vsi;
898 	struct bpf_struct_ops *st_ops;
899 	const char *tname, *var_name;
900 	__s32 type_id, datasec_id;
901 	const struct btf *btf;
902 	struct bpf_map *map;
903 	__u32 i;
904 
905 	if (obj->efile.st_ops_shndx == -1)
906 		return 0;
907 
908 	btf = obj->btf;
909 	datasec_id = btf__find_by_name_kind(btf, STRUCT_OPS_SEC,
910 					    BTF_KIND_DATASEC);
911 	if (datasec_id < 0) {
912 		pr_warn("struct_ops init: DATASEC %s not found\n",
913 			STRUCT_OPS_SEC);
914 		return -EINVAL;
915 	}
916 
917 	datasec = btf__type_by_id(btf, datasec_id);
918 	vsi = btf_var_secinfos(datasec);
919 	for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
920 		type = btf__type_by_id(obj->btf, vsi->type);
921 		var_name = btf__name_by_offset(obj->btf, type->name_off);
922 
923 		type_id = btf__resolve_type(obj->btf, vsi->type);
924 		if (type_id < 0) {
925 			pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
926 				vsi->type, STRUCT_OPS_SEC);
927 			return -EINVAL;
928 		}
929 
930 		type = btf__type_by_id(obj->btf, type_id);
931 		tname = btf__name_by_offset(obj->btf, type->name_off);
932 		if (!tname[0]) {
933 			pr_warn("struct_ops init: anonymous type is not supported\n");
934 			return -ENOTSUP;
935 		}
936 		if (!btf_is_struct(type)) {
937 			pr_warn("struct_ops init: %s is not a struct\n", tname);
938 			return -EINVAL;
939 		}
940 
941 		map = bpf_object__add_map(obj);
942 		if (IS_ERR(map))
943 			return PTR_ERR(map);
944 
945 		map->sec_idx = obj->efile.st_ops_shndx;
946 		map->sec_offset = vsi->offset;
947 		map->name = strdup(var_name);
948 		if (!map->name)
949 			return -ENOMEM;
950 
951 		map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
952 		map->def.key_size = sizeof(int);
953 		map->def.value_size = type->size;
954 		map->def.max_entries = 1;
955 
956 		map->st_ops = calloc(1, sizeof(*map->st_ops));
957 		if (!map->st_ops)
958 			return -ENOMEM;
959 		st_ops = map->st_ops;
960 		st_ops->data = malloc(type->size);
961 		st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
962 		st_ops->kern_func_off = malloc(btf_vlen(type) *
963 					       sizeof(*st_ops->kern_func_off));
964 		if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
965 			return -ENOMEM;
966 
967 		if (vsi->offset + type->size > obj->efile.st_ops_data->d_size) {
968 			pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
969 				var_name, STRUCT_OPS_SEC);
970 			return -EINVAL;
971 		}
972 
973 		memcpy(st_ops->data,
974 		       obj->efile.st_ops_data->d_buf + vsi->offset,
975 		       type->size);
976 		st_ops->tname = tname;
977 		st_ops->type = type;
978 		st_ops->type_id = type_id;
979 
980 		pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
981 			 tname, type_id, var_name, vsi->offset);
982 	}
983 
984 	return 0;
985 }
986 
987 static struct bpf_object *bpf_object__new(const char *path,
988 					  const void *obj_buf,
989 					  size_t obj_buf_sz,
990 					  const char *obj_name)
991 {
992 	struct bpf_object *obj;
993 	char *end;
994 
995 	obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
996 	if (!obj) {
997 		pr_warn("alloc memory failed for %s\n", path);
998 		return ERR_PTR(-ENOMEM);
999 	}
1000 
1001 	strcpy(obj->path, path);
1002 	if (obj_name) {
1003 		strncpy(obj->name, obj_name, sizeof(obj->name) - 1);
1004 		obj->name[sizeof(obj->name) - 1] = 0;
1005 	} else {
1006 		/* Using basename() GNU version which doesn't modify arg. */
1007 		strncpy(obj->name, basename((void *)path),
1008 			sizeof(obj->name) - 1);
1009 		end = strchr(obj->name, '.');
1010 		if (end)
1011 			*end = 0;
1012 	}
1013 
1014 	obj->efile.fd = -1;
1015 	/*
1016 	 * Caller of this function should also call
1017 	 * bpf_object__elf_finish() after data collection to return
1018 	 * obj_buf to user. If not, we should duplicate the buffer to
1019 	 * avoid user freeing them before elf finish.
1020 	 */
1021 	obj->efile.obj_buf = obj_buf;
1022 	obj->efile.obj_buf_sz = obj_buf_sz;
1023 	obj->efile.maps_shndx = -1;
1024 	obj->efile.btf_maps_shndx = -1;
1025 	obj->efile.data_shndx = -1;
1026 	obj->efile.rodata_shndx = -1;
1027 	obj->efile.bss_shndx = -1;
1028 	obj->efile.st_ops_shndx = -1;
1029 	obj->kconfig_map_idx = -1;
1030 
1031 	obj->kern_version = get_kernel_version();
1032 	obj->loaded = false;
1033 
1034 	INIT_LIST_HEAD(&obj->list);
1035 	list_add(&obj->list, &bpf_objects_list);
1036 	return obj;
1037 }
1038 
1039 static void bpf_object__elf_finish(struct bpf_object *obj)
1040 {
1041 	if (!obj_elf_valid(obj))
1042 		return;
1043 
1044 	if (obj->efile.elf) {
1045 		elf_end(obj->efile.elf);
1046 		obj->efile.elf = NULL;
1047 	}
1048 	obj->efile.symbols = NULL;
1049 	obj->efile.data = NULL;
1050 	obj->efile.rodata = NULL;
1051 	obj->efile.bss = NULL;
1052 	obj->efile.st_ops_data = NULL;
1053 
1054 	zfree(&obj->efile.reloc_sects);
1055 	obj->efile.nr_reloc_sects = 0;
1056 	zclose(obj->efile.fd);
1057 	obj->efile.obj_buf = NULL;
1058 	obj->efile.obj_buf_sz = 0;
1059 }
1060 
1061 /* if libelf is old and doesn't support mmap(), fall back to read() */
1062 #ifndef ELF_C_READ_MMAP
1063 #define ELF_C_READ_MMAP ELF_C_READ
1064 #endif
1065 
1066 static int bpf_object__elf_init(struct bpf_object *obj)
1067 {
1068 	int err = 0;
1069 	GElf_Ehdr *ep;
1070 
1071 	if (obj_elf_valid(obj)) {
1072 		pr_warn("elf init: internal error\n");
1073 		return -LIBBPF_ERRNO__LIBELF;
1074 	}
1075 
1076 	if (obj->efile.obj_buf_sz > 0) {
1077 		/*
1078 		 * obj_buf should have been validated by
1079 		 * bpf_object__open_buffer().
1080 		 */
1081 		obj->efile.elf = elf_memory((char *)obj->efile.obj_buf,
1082 					    obj->efile.obj_buf_sz);
1083 	} else {
1084 		obj->efile.fd = open(obj->path, O_RDONLY);
1085 		if (obj->efile.fd < 0) {
1086 			char errmsg[STRERR_BUFSIZE], *cp;
1087 
1088 			err = -errno;
1089 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
1090 			pr_warn("failed to open %s: %s\n", obj->path, cp);
1091 			return err;
1092 		}
1093 
1094 		obj->efile.elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1095 	}
1096 
1097 	if (!obj->efile.elf) {
1098 		pr_warn("failed to open %s as ELF file\n", obj->path);
1099 		err = -LIBBPF_ERRNO__LIBELF;
1100 		goto errout;
1101 	}
1102 
1103 	if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
1104 		pr_warn("failed to get EHDR from %s\n", obj->path);
1105 		err = -LIBBPF_ERRNO__FORMAT;
1106 		goto errout;
1107 	}
1108 	ep = &obj->efile.ehdr;
1109 
1110 	/* Old LLVM set e_machine to EM_NONE */
1111 	if (ep->e_type != ET_REL ||
1112 	    (ep->e_machine && ep->e_machine != EM_BPF)) {
1113 		pr_warn("%s is not an eBPF object file\n", obj->path);
1114 		err = -LIBBPF_ERRNO__FORMAT;
1115 		goto errout;
1116 	}
1117 
1118 	return 0;
1119 errout:
1120 	bpf_object__elf_finish(obj);
1121 	return err;
1122 }
1123 
1124 static int bpf_object__check_endianness(struct bpf_object *obj)
1125 {
1126 #if __BYTE_ORDER == __LITTLE_ENDIAN
1127 	if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1128 		return 0;
1129 #elif __BYTE_ORDER == __BIG_ENDIAN
1130 	if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
1131 		return 0;
1132 #else
1133 # error "Unrecognized __BYTE_ORDER__"
1134 #endif
1135 	pr_warn("endianness mismatch.\n");
1136 	return -LIBBPF_ERRNO__ENDIAN;
1137 }
1138 
1139 static int
1140 bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
1141 {
1142 	memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
1143 	pr_debug("license of %s is %s\n", obj->path, obj->license);
1144 	return 0;
1145 }
1146 
1147 static int
1148 bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1149 {
1150 	__u32 kver;
1151 
1152 	if (size != sizeof(kver)) {
1153 		pr_warn("invalid kver section in %s\n", obj->path);
1154 		return -LIBBPF_ERRNO__FORMAT;
1155 	}
1156 	memcpy(&kver, data, sizeof(kver));
1157 	obj->kern_version = kver;
1158 	pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1159 	return 0;
1160 }
1161 
1162 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1163 {
1164 	if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1165 	    type == BPF_MAP_TYPE_HASH_OF_MAPS)
1166 		return true;
1167 	return false;
1168 }
1169 
1170 static int bpf_object_search_section_size(const struct bpf_object *obj,
1171 					  const char *name, size_t *d_size)
1172 {
1173 	const GElf_Ehdr *ep = &obj->efile.ehdr;
1174 	Elf *elf = obj->efile.elf;
1175 	Elf_Scn *scn = NULL;
1176 	int idx = 0;
1177 
1178 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
1179 		const char *sec_name;
1180 		Elf_Data *data;
1181 		GElf_Shdr sh;
1182 
1183 		idx++;
1184 		if (gelf_getshdr(scn, &sh) != &sh) {
1185 			pr_warn("failed to get section(%d) header from %s\n",
1186 				idx, obj->path);
1187 			return -EIO;
1188 		}
1189 
1190 		sec_name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
1191 		if (!sec_name) {
1192 			pr_warn("failed to get section(%d) name from %s\n",
1193 				idx, obj->path);
1194 			return -EIO;
1195 		}
1196 
1197 		if (strcmp(name, sec_name))
1198 			continue;
1199 
1200 		data = elf_getdata(scn, 0);
1201 		if (!data) {
1202 			pr_warn("failed to get section(%d) data from %s(%s)\n",
1203 				idx, name, obj->path);
1204 			return -EIO;
1205 		}
1206 
1207 		*d_size = data->d_size;
1208 		return 0;
1209 	}
1210 
1211 	return -ENOENT;
1212 }
1213 
1214 int bpf_object__section_size(const struct bpf_object *obj, const char *name,
1215 			     __u32 *size)
1216 {
1217 	int ret = -ENOENT;
1218 	size_t d_size;
1219 
1220 	*size = 0;
1221 	if (!name) {
1222 		return -EINVAL;
1223 	} else if (!strcmp(name, DATA_SEC)) {
1224 		if (obj->efile.data)
1225 			*size = obj->efile.data->d_size;
1226 	} else if (!strcmp(name, BSS_SEC)) {
1227 		if (obj->efile.bss)
1228 			*size = obj->efile.bss->d_size;
1229 	} else if (!strcmp(name, RODATA_SEC)) {
1230 		if (obj->efile.rodata)
1231 			*size = obj->efile.rodata->d_size;
1232 	} else if (!strcmp(name, STRUCT_OPS_SEC)) {
1233 		if (obj->efile.st_ops_data)
1234 			*size = obj->efile.st_ops_data->d_size;
1235 	} else {
1236 		ret = bpf_object_search_section_size(obj, name, &d_size);
1237 		if (!ret)
1238 			*size = d_size;
1239 	}
1240 
1241 	return *size ? 0 : ret;
1242 }
1243 
1244 int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
1245 				__u32 *off)
1246 {
1247 	Elf_Data *symbols = obj->efile.symbols;
1248 	const char *sname;
1249 	size_t si;
1250 
1251 	if (!name || !off)
1252 		return -EINVAL;
1253 
1254 	for (si = 0; si < symbols->d_size / sizeof(GElf_Sym); si++) {
1255 		GElf_Sym sym;
1256 
1257 		if (!gelf_getsym(symbols, si, &sym))
1258 			continue;
1259 		if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
1260 		    GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
1261 			continue;
1262 
1263 		sname = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1264 				   sym.st_name);
1265 		if (!sname) {
1266 			pr_warn("failed to get sym name string for var %s\n",
1267 				name);
1268 			return -EIO;
1269 		}
1270 		if (strcmp(name, sname) == 0) {
1271 			*off = sym.st_value;
1272 			return 0;
1273 		}
1274 	}
1275 
1276 	return -ENOENT;
1277 }
1278 
1279 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
1280 {
1281 	struct bpf_map *new_maps;
1282 	size_t new_cap;
1283 	int i;
1284 
1285 	if (obj->nr_maps < obj->maps_cap)
1286 		return &obj->maps[obj->nr_maps++];
1287 
1288 	new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
1289 	new_maps = libbpf_reallocarray(obj->maps, new_cap, sizeof(*obj->maps));
1290 	if (!new_maps) {
1291 		pr_warn("alloc maps for object failed\n");
1292 		return ERR_PTR(-ENOMEM);
1293 	}
1294 
1295 	obj->maps_cap = new_cap;
1296 	obj->maps = new_maps;
1297 
1298 	/* zero out new maps */
1299 	memset(obj->maps + obj->nr_maps, 0,
1300 	       (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
1301 	/*
1302 	 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
1303 	 * when failure (zclose won't close negative fd)).
1304 	 */
1305 	for (i = obj->nr_maps; i < obj->maps_cap; i++) {
1306 		obj->maps[i].fd = -1;
1307 		obj->maps[i].inner_map_fd = -1;
1308 	}
1309 
1310 	return &obj->maps[obj->nr_maps++];
1311 }
1312 
1313 static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1314 {
1315 	long page_sz = sysconf(_SC_PAGE_SIZE);
1316 	size_t map_sz;
1317 
1318 	map_sz = (size_t)roundup(map->def.value_size, 8) * map->def.max_entries;
1319 	map_sz = roundup(map_sz, page_sz);
1320 	return map_sz;
1321 }
1322 
1323 static char *internal_map_name(struct bpf_object *obj,
1324 			       enum libbpf_map_type type)
1325 {
1326 	char map_name[BPF_OBJ_NAME_LEN], *p;
1327 	const char *sfx = libbpf_type_to_btf_name[type];
1328 	int sfx_len = max((size_t)7, strlen(sfx));
1329 	int pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1,
1330 			  strlen(obj->name));
1331 
1332 	snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
1333 		 sfx_len, libbpf_type_to_btf_name[type]);
1334 
1335 	/* sanitise map name to characters allowed by kernel */
1336 	for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1337 		if (!isalnum(*p) && *p != '_' && *p != '.')
1338 			*p = '_';
1339 
1340 	return strdup(map_name);
1341 }
1342 
1343 static int
1344 bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
1345 			      int sec_idx, void *data, size_t data_sz)
1346 {
1347 	struct bpf_map_def *def;
1348 	struct bpf_map *map;
1349 	int err;
1350 
1351 	map = bpf_object__add_map(obj);
1352 	if (IS_ERR(map))
1353 		return PTR_ERR(map);
1354 
1355 	map->libbpf_type = type;
1356 	map->sec_idx = sec_idx;
1357 	map->sec_offset = 0;
1358 	map->name = internal_map_name(obj, type);
1359 	if (!map->name) {
1360 		pr_warn("failed to alloc map name\n");
1361 		return -ENOMEM;
1362 	}
1363 
1364 	def = &map->def;
1365 	def->type = BPF_MAP_TYPE_ARRAY;
1366 	def->key_size = sizeof(int);
1367 	def->value_size = data_sz;
1368 	def->max_entries = 1;
1369 	def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
1370 			 ? BPF_F_RDONLY_PROG : 0;
1371 	def->map_flags |= BPF_F_MMAPABLE;
1372 
1373 	pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1374 		 map->name, map->sec_idx, map->sec_offset, def->map_flags);
1375 
1376 	map->mmaped = mmap(NULL, bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
1377 			   MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1378 	if (map->mmaped == MAP_FAILED) {
1379 		err = -errno;
1380 		map->mmaped = NULL;
1381 		pr_warn("failed to alloc map '%s' content buffer: %d\n",
1382 			map->name, err);
1383 		zfree(&map->name);
1384 		return err;
1385 	}
1386 
1387 	if (data)
1388 		memcpy(map->mmaped, data, data_sz);
1389 
1390 	pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
1391 	return 0;
1392 }
1393 
1394 static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1395 {
1396 	int err;
1397 
1398 	/*
1399 	 * Populate obj->maps with libbpf internal maps.
1400 	 */
1401 	if (obj->efile.data_shndx >= 0) {
1402 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
1403 						    obj->efile.data_shndx,
1404 						    obj->efile.data->d_buf,
1405 						    obj->efile.data->d_size);
1406 		if (err)
1407 			return err;
1408 	}
1409 	if (obj->efile.rodata_shndx >= 0) {
1410 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
1411 						    obj->efile.rodata_shndx,
1412 						    obj->efile.rodata->d_buf,
1413 						    obj->efile.rodata->d_size);
1414 		if (err)
1415 			return err;
1416 	}
1417 	if (obj->efile.bss_shndx >= 0) {
1418 		err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
1419 						    obj->efile.bss_shndx,
1420 						    NULL,
1421 						    obj->efile.bss->d_size);
1422 		if (err)
1423 			return err;
1424 	}
1425 	return 0;
1426 }
1427 
1428 
1429 static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1430 					       const void *name)
1431 {
1432 	int i;
1433 
1434 	for (i = 0; i < obj->nr_extern; i++) {
1435 		if (strcmp(obj->externs[i].name, name) == 0)
1436 			return &obj->externs[i];
1437 	}
1438 	return NULL;
1439 }
1440 
1441 static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1442 			      char value)
1443 {
1444 	switch (ext->kcfg.type) {
1445 	case KCFG_BOOL:
1446 		if (value == 'm') {
1447 			pr_warn("extern (kcfg) %s=%c should be tristate or char\n",
1448 				ext->name, value);
1449 			return -EINVAL;
1450 		}
1451 		*(bool *)ext_val = value == 'y' ? true : false;
1452 		break;
1453 	case KCFG_TRISTATE:
1454 		if (value == 'y')
1455 			*(enum libbpf_tristate *)ext_val = TRI_YES;
1456 		else if (value == 'm')
1457 			*(enum libbpf_tristate *)ext_val = TRI_MODULE;
1458 		else /* value == 'n' */
1459 			*(enum libbpf_tristate *)ext_val = TRI_NO;
1460 		break;
1461 	case KCFG_CHAR:
1462 		*(char *)ext_val = value;
1463 		break;
1464 	case KCFG_UNKNOWN:
1465 	case KCFG_INT:
1466 	case KCFG_CHAR_ARR:
1467 	default:
1468 		pr_warn("extern (kcfg) %s=%c should be bool, tristate, or char\n",
1469 			ext->name, value);
1470 		return -EINVAL;
1471 	}
1472 	ext->is_set = true;
1473 	return 0;
1474 }
1475 
1476 static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
1477 			      const char *value)
1478 {
1479 	size_t len;
1480 
1481 	if (ext->kcfg.type != KCFG_CHAR_ARR) {
1482 		pr_warn("extern (kcfg) %s=%s should be char array\n", ext->name, value);
1483 		return -EINVAL;
1484 	}
1485 
1486 	len = strlen(value);
1487 	if (value[len - 1] != '"') {
1488 		pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
1489 			ext->name, value);
1490 		return -EINVAL;
1491 	}
1492 
1493 	/* strip quotes */
1494 	len -= 2;
1495 	if (len >= ext->kcfg.sz) {
1496 		pr_warn("extern (kcfg) '%s': long string config %s of (%zu bytes) truncated to %d bytes\n",
1497 			ext->name, value, len, ext->kcfg.sz - 1);
1498 		len = ext->kcfg.sz - 1;
1499 	}
1500 	memcpy(ext_val, value + 1, len);
1501 	ext_val[len] = '\0';
1502 	ext->is_set = true;
1503 	return 0;
1504 }
1505 
1506 static int parse_u64(const char *value, __u64 *res)
1507 {
1508 	char *value_end;
1509 	int err;
1510 
1511 	errno = 0;
1512 	*res = strtoull(value, &value_end, 0);
1513 	if (errno) {
1514 		err = -errno;
1515 		pr_warn("failed to parse '%s' as integer: %d\n", value, err);
1516 		return err;
1517 	}
1518 	if (*value_end) {
1519 		pr_warn("failed to parse '%s' as integer completely\n", value);
1520 		return -EINVAL;
1521 	}
1522 	return 0;
1523 }
1524 
1525 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
1526 {
1527 	int bit_sz = ext->kcfg.sz * 8;
1528 
1529 	if (ext->kcfg.sz == 8)
1530 		return true;
1531 
1532 	/* Validate that value stored in u64 fits in integer of `ext->sz`
1533 	 * bytes size without any loss of information. If the target integer
1534 	 * is signed, we rely on the following limits of integer type of
1535 	 * Y bits and subsequent transformation:
1536 	 *
1537 	 *     -2^(Y-1) <= X           <= 2^(Y-1) - 1
1538 	 *            0 <= X + 2^(Y-1) <= 2^Y - 1
1539 	 *            0 <= X + 2^(Y-1) <  2^Y
1540 	 *
1541 	 *  For unsigned target integer, check that all the (64 - Y) bits are
1542 	 *  zero.
1543 	 */
1544 	if (ext->kcfg.is_signed)
1545 		return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
1546 	else
1547 		return (v >> bit_sz) == 0;
1548 }
1549 
1550 static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
1551 			      __u64 value)
1552 {
1553 	if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
1554 		pr_warn("extern (kcfg) %s=%llu should be integer\n",
1555 			ext->name, (unsigned long long)value);
1556 		return -EINVAL;
1557 	}
1558 	if (!is_kcfg_value_in_range(ext, value)) {
1559 		pr_warn("extern (kcfg) %s=%llu value doesn't fit in %d bytes\n",
1560 			ext->name, (unsigned long long)value, ext->kcfg.sz);
1561 		return -ERANGE;
1562 	}
1563 	switch (ext->kcfg.sz) {
1564 		case 1: *(__u8 *)ext_val = value; break;
1565 		case 2: *(__u16 *)ext_val = value; break;
1566 		case 4: *(__u32 *)ext_val = value; break;
1567 		case 8: *(__u64 *)ext_val = value; break;
1568 		default:
1569 			return -EINVAL;
1570 	}
1571 	ext->is_set = true;
1572 	return 0;
1573 }
1574 
1575 static int bpf_object__process_kconfig_line(struct bpf_object *obj,
1576 					    char *buf, void *data)
1577 {
1578 	struct extern_desc *ext;
1579 	char *sep, *value;
1580 	int len, err = 0;
1581 	void *ext_val;
1582 	__u64 num;
1583 
1584 	if (strncmp(buf, "CONFIG_", 7))
1585 		return 0;
1586 
1587 	sep = strchr(buf, '=');
1588 	if (!sep) {
1589 		pr_warn("failed to parse '%s': no separator\n", buf);
1590 		return -EINVAL;
1591 	}
1592 
1593 	/* Trim ending '\n' */
1594 	len = strlen(buf);
1595 	if (buf[len - 1] == '\n')
1596 		buf[len - 1] = '\0';
1597 	/* Split on '=' and ensure that a value is present. */
1598 	*sep = '\0';
1599 	if (!sep[1]) {
1600 		*sep = '=';
1601 		pr_warn("failed to parse '%s': no value\n", buf);
1602 		return -EINVAL;
1603 	}
1604 
1605 	ext = find_extern_by_name(obj, buf);
1606 	if (!ext || ext->is_set)
1607 		return 0;
1608 
1609 	ext_val = data + ext->kcfg.data_off;
1610 	value = sep + 1;
1611 
1612 	switch (*value) {
1613 	case 'y': case 'n': case 'm':
1614 		err = set_kcfg_value_tri(ext, ext_val, *value);
1615 		break;
1616 	case '"':
1617 		err = set_kcfg_value_str(ext, ext_val, value);
1618 		break;
1619 	default:
1620 		/* assume integer */
1621 		err = parse_u64(value, &num);
1622 		if (err) {
1623 			pr_warn("extern (kcfg) %s=%s should be integer\n",
1624 				ext->name, value);
1625 			return err;
1626 		}
1627 		err = set_kcfg_value_num(ext, ext_val, num);
1628 		break;
1629 	}
1630 	if (err)
1631 		return err;
1632 	pr_debug("extern (kcfg) %s=%s\n", ext->name, value);
1633 	return 0;
1634 }
1635 
1636 static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
1637 {
1638 	char buf[PATH_MAX];
1639 	struct utsname uts;
1640 	int len, err = 0;
1641 	gzFile file;
1642 
1643 	uname(&uts);
1644 	len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
1645 	if (len < 0)
1646 		return -EINVAL;
1647 	else if (len >= PATH_MAX)
1648 		return -ENAMETOOLONG;
1649 
1650 	/* gzopen also accepts uncompressed files. */
1651 	file = gzopen(buf, "r");
1652 	if (!file)
1653 		file = gzopen("/proc/config.gz", "r");
1654 
1655 	if (!file) {
1656 		pr_warn("failed to open system Kconfig\n");
1657 		return -ENOENT;
1658 	}
1659 
1660 	while (gzgets(file, buf, sizeof(buf))) {
1661 		err = bpf_object__process_kconfig_line(obj, buf, data);
1662 		if (err) {
1663 			pr_warn("error parsing system Kconfig line '%s': %d\n",
1664 				buf, err);
1665 			goto out;
1666 		}
1667 	}
1668 
1669 out:
1670 	gzclose(file);
1671 	return err;
1672 }
1673 
1674 static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
1675 					const char *config, void *data)
1676 {
1677 	char buf[PATH_MAX];
1678 	int err = 0;
1679 	FILE *file;
1680 
1681 	file = fmemopen((void *)config, strlen(config), "r");
1682 	if (!file) {
1683 		err = -errno;
1684 		pr_warn("failed to open in-memory Kconfig: %d\n", err);
1685 		return err;
1686 	}
1687 
1688 	while (fgets(buf, sizeof(buf), file)) {
1689 		err = bpf_object__process_kconfig_line(obj, buf, data);
1690 		if (err) {
1691 			pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
1692 				buf, err);
1693 			break;
1694 		}
1695 	}
1696 
1697 	fclose(file);
1698 	return err;
1699 }
1700 
1701 static int bpf_object__init_kconfig_map(struct bpf_object *obj)
1702 {
1703 	struct extern_desc *last_ext = NULL, *ext;
1704 	size_t map_sz;
1705 	int i, err;
1706 
1707 	for (i = 0; i < obj->nr_extern; i++) {
1708 		ext = &obj->externs[i];
1709 		if (ext->type == EXT_KCFG)
1710 			last_ext = ext;
1711 	}
1712 
1713 	if (!last_ext)
1714 		return 0;
1715 
1716 	map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
1717 	err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
1718 					    obj->efile.symbols_shndx,
1719 					    NULL, map_sz);
1720 	if (err)
1721 		return err;
1722 
1723 	obj->kconfig_map_idx = obj->nr_maps - 1;
1724 
1725 	return 0;
1726 }
1727 
1728 static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
1729 {
1730 	Elf_Data *symbols = obj->efile.symbols;
1731 	int i, map_def_sz = 0, nr_maps = 0, nr_syms;
1732 	Elf_Data *data = NULL;
1733 	Elf_Scn *scn;
1734 
1735 	if (obj->efile.maps_shndx < 0)
1736 		return 0;
1737 
1738 	if (!symbols)
1739 		return -EINVAL;
1740 
1741 	scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
1742 	if (scn)
1743 		data = elf_getdata(scn, NULL);
1744 	if (!scn || !data) {
1745 		pr_warn("failed to get Elf_Data from map section %d\n",
1746 			obj->efile.maps_shndx);
1747 		return -EINVAL;
1748 	}
1749 
1750 	/*
1751 	 * Count number of maps. Each map has a name.
1752 	 * Array of maps is not supported: only the first element is
1753 	 * considered.
1754 	 *
1755 	 * TODO: Detect array of map and report error.
1756 	 */
1757 	nr_syms = symbols->d_size / sizeof(GElf_Sym);
1758 	for (i = 0; i < nr_syms; i++) {
1759 		GElf_Sym sym;
1760 
1761 		if (!gelf_getsym(symbols, i, &sym))
1762 			continue;
1763 		if (sym.st_shndx != obj->efile.maps_shndx)
1764 			continue;
1765 		nr_maps++;
1766 	}
1767 	/* Assume equally sized map definitions */
1768 	pr_debug("maps in %s: %d maps in %zd bytes\n",
1769 		 obj->path, nr_maps, data->d_size);
1770 
1771 	if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) {
1772 		pr_warn("unable to determine map definition size section %s, %d maps in %zd bytes\n",
1773 			obj->path, nr_maps, data->d_size);
1774 		return -EINVAL;
1775 	}
1776 	map_def_sz = data->d_size / nr_maps;
1777 
1778 	/* Fill obj->maps using data in "maps" section.  */
1779 	for (i = 0; i < nr_syms; i++) {
1780 		GElf_Sym sym;
1781 		const char *map_name;
1782 		struct bpf_map_def *def;
1783 		struct bpf_map *map;
1784 
1785 		if (!gelf_getsym(symbols, i, &sym))
1786 			continue;
1787 		if (sym.st_shndx != obj->efile.maps_shndx)
1788 			continue;
1789 
1790 		map = bpf_object__add_map(obj);
1791 		if (IS_ERR(map))
1792 			return PTR_ERR(map);
1793 
1794 		map_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
1795 				      sym.st_name);
1796 		if (!map_name) {
1797 			pr_warn("failed to get map #%d name sym string for obj %s\n",
1798 				i, obj->path);
1799 			return -LIBBPF_ERRNO__FORMAT;
1800 		}
1801 
1802 		map->libbpf_type = LIBBPF_MAP_UNSPEC;
1803 		map->sec_idx = sym.st_shndx;
1804 		map->sec_offset = sym.st_value;
1805 		pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
1806 			 map_name, map->sec_idx, map->sec_offset);
1807 		if (sym.st_value + map_def_sz > data->d_size) {
1808 			pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
1809 				obj->path, map_name);
1810 			return -EINVAL;
1811 		}
1812 
1813 		map->name = strdup(map_name);
1814 		if (!map->name) {
1815 			pr_warn("failed to alloc map name\n");
1816 			return -ENOMEM;
1817 		}
1818 		pr_debug("map %d is \"%s\"\n", i, map->name);
1819 		def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
1820 		/*
1821 		 * If the definition of the map in the object file fits in
1822 		 * bpf_map_def, copy it.  Any extra fields in our version
1823 		 * of bpf_map_def will default to zero as a result of the
1824 		 * calloc above.
1825 		 */
1826 		if (map_def_sz <= sizeof(struct bpf_map_def)) {
1827 			memcpy(&map->def, def, map_def_sz);
1828 		} else {
1829 			/*
1830 			 * Here the map structure being read is bigger than what
1831 			 * we expect, truncate if the excess bits are all zero.
1832 			 * If they are not zero, reject this map as
1833 			 * incompatible.
1834 			 */
1835 			char *b;
1836 
1837 			for (b = ((char *)def) + sizeof(struct bpf_map_def);
1838 			     b < ((char *)def) + map_def_sz; b++) {
1839 				if (*b != 0) {
1840 					pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n",
1841 						obj->path, map_name);
1842 					if (strict)
1843 						return -EINVAL;
1844 				}
1845 			}
1846 			memcpy(&map->def, def, sizeof(struct bpf_map_def));
1847 		}
1848 	}
1849 	return 0;
1850 }
1851 
1852 static const struct btf_type *
1853 skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
1854 {
1855 	const struct btf_type *t = btf__type_by_id(btf, id);
1856 
1857 	if (res_id)
1858 		*res_id = id;
1859 
1860 	while (btf_is_mod(t) || btf_is_typedef(t)) {
1861 		if (res_id)
1862 			*res_id = t->type;
1863 		t = btf__type_by_id(btf, t->type);
1864 	}
1865 
1866 	return t;
1867 }
1868 
1869 static const struct btf_type *
1870 resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
1871 {
1872 	const struct btf_type *t;
1873 
1874 	t = skip_mods_and_typedefs(btf, id, NULL);
1875 	if (!btf_is_ptr(t))
1876 		return NULL;
1877 
1878 	t = skip_mods_and_typedefs(btf, t->type, res_id);
1879 
1880 	return btf_is_func_proto(t) ? t : NULL;
1881 }
1882 
1883 static const char *btf_kind_str(const struct btf_type *t)
1884 {
1885 	switch (btf_kind(t)) {
1886 	case BTF_KIND_UNKN: return "void";
1887 	case BTF_KIND_INT: return "int";
1888 	case BTF_KIND_PTR: return "ptr";
1889 	case BTF_KIND_ARRAY: return "array";
1890 	case BTF_KIND_STRUCT: return "struct";
1891 	case BTF_KIND_UNION: return "union";
1892 	case BTF_KIND_ENUM: return "enum";
1893 	case BTF_KIND_FWD: return "fwd";
1894 	case BTF_KIND_TYPEDEF: return "typedef";
1895 	case BTF_KIND_VOLATILE: return "volatile";
1896 	case BTF_KIND_CONST: return "const";
1897 	case BTF_KIND_RESTRICT: return "restrict";
1898 	case BTF_KIND_FUNC: return "func";
1899 	case BTF_KIND_FUNC_PROTO: return "func_proto";
1900 	case BTF_KIND_VAR: return "var";
1901 	case BTF_KIND_DATASEC: return "datasec";
1902 	default: return "unknown";
1903 	}
1904 }
1905 
1906 /*
1907  * Fetch integer attribute of BTF map definition. Such attributes are
1908  * represented using a pointer to an array, in which dimensionality of array
1909  * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
1910  * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
1911  * type definition, while using only sizeof(void *) space in ELF data section.
1912  */
1913 static bool get_map_field_int(const char *map_name, const struct btf *btf,
1914 			      const struct btf_member *m, __u32 *res)
1915 {
1916 	const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
1917 	const char *name = btf__name_by_offset(btf, m->name_off);
1918 	const struct btf_array *arr_info;
1919 	const struct btf_type *arr_t;
1920 
1921 	if (!btf_is_ptr(t)) {
1922 		pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
1923 			map_name, name, btf_kind_str(t));
1924 		return false;
1925 	}
1926 
1927 	arr_t = btf__type_by_id(btf, t->type);
1928 	if (!arr_t) {
1929 		pr_warn("map '%s': attr '%s': type [%u] not found.\n",
1930 			map_name, name, t->type);
1931 		return false;
1932 	}
1933 	if (!btf_is_array(arr_t)) {
1934 		pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
1935 			map_name, name, btf_kind_str(arr_t));
1936 		return false;
1937 	}
1938 	arr_info = btf_array(arr_t);
1939 	*res = arr_info->nelems;
1940 	return true;
1941 }
1942 
1943 static int build_map_pin_path(struct bpf_map *map, const char *path)
1944 {
1945 	char buf[PATH_MAX];
1946 	int len;
1947 
1948 	if (!path)
1949 		path = "/sys/fs/bpf";
1950 
1951 	len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
1952 	if (len < 0)
1953 		return -EINVAL;
1954 	else if (len >= PATH_MAX)
1955 		return -ENAMETOOLONG;
1956 
1957 	return bpf_map__set_pin_path(map, buf);
1958 }
1959 
1960 
1961 static int parse_btf_map_def(struct bpf_object *obj,
1962 			     struct bpf_map *map,
1963 			     const struct btf_type *def,
1964 			     bool strict, bool is_inner,
1965 			     const char *pin_root_path)
1966 {
1967 	const struct btf_type *t;
1968 	const struct btf_member *m;
1969 	int vlen, i;
1970 
1971 	vlen = btf_vlen(def);
1972 	m = btf_members(def);
1973 	for (i = 0; i < vlen; i++, m++) {
1974 		const char *name = btf__name_by_offset(obj->btf, m->name_off);
1975 
1976 		if (!name) {
1977 			pr_warn("map '%s': invalid field #%d.\n", map->name, i);
1978 			return -EINVAL;
1979 		}
1980 		if (strcmp(name, "type") == 0) {
1981 			if (!get_map_field_int(map->name, obj->btf, m,
1982 					       &map->def.type))
1983 				return -EINVAL;
1984 			pr_debug("map '%s': found type = %u.\n",
1985 				 map->name, map->def.type);
1986 		} else if (strcmp(name, "max_entries") == 0) {
1987 			if (!get_map_field_int(map->name, obj->btf, m,
1988 					       &map->def.max_entries))
1989 				return -EINVAL;
1990 			pr_debug("map '%s': found max_entries = %u.\n",
1991 				 map->name, map->def.max_entries);
1992 		} else if (strcmp(name, "map_flags") == 0) {
1993 			if (!get_map_field_int(map->name, obj->btf, m,
1994 					       &map->def.map_flags))
1995 				return -EINVAL;
1996 			pr_debug("map '%s': found map_flags = %u.\n",
1997 				 map->name, map->def.map_flags);
1998 		} else if (strcmp(name, "numa_node") == 0) {
1999 			if (!get_map_field_int(map->name, obj->btf, m, &map->numa_node))
2000 				return -EINVAL;
2001 			pr_debug("map '%s': found numa_node = %u.\n", map->name, map->numa_node);
2002 		} else if (strcmp(name, "key_size") == 0) {
2003 			__u32 sz;
2004 
2005 			if (!get_map_field_int(map->name, obj->btf, m, &sz))
2006 				return -EINVAL;
2007 			pr_debug("map '%s': found key_size = %u.\n",
2008 				 map->name, sz);
2009 			if (map->def.key_size && map->def.key_size != sz) {
2010 				pr_warn("map '%s': conflicting key size %u != %u.\n",
2011 					map->name, map->def.key_size, sz);
2012 				return -EINVAL;
2013 			}
2014 			map->def.key_size = sz;
2015 		} else if (strcmp(name, "key") == 0) {
2016 			__s64 sz;
2017 
2018 			t = btf__type_by_id(obj->btf, m->type);
2019 			if (!t) {
2020 				pr_warn("map '%s': key type [%d] not found.\n",
2021 					map->name, m->type);
2022 				return -EINVAL;
2023 			}
2024 			if (!btf_is_ptr(t)) {
2025 				pr_warn("map '%s': key spec is not PTR: %s.\n",
2026 					map->name, btf_kind_str(t));
2027 				return -EINVAL;
2028 			}
2029 			sz = btf__resolve_size(obj->btf, t->type);
2030 			if (sz < 0) {
2031 				pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2032 					map->name, t->type, (ssize_t)sz);
2033 				return sz;
2034 			}
2035 			pr_debug("map '%s': found key [%u], sz = %zd.\n",
2036 				 map->name, t->type, (ssize_t)sz);
2037 			if (map->def.key_size && map->def.key_size != sz) {
2038 				pr_warn("map '%s': conflicting key size %u != %zd.\n",
2039 					map->name, map->def.key_size, (ssize_t)sz);
2040 				return -EINVAL;
2041 			}
2042 			map->def.key_size = sz;
2043 			map->btf_key_type_id = t->type;
2044 		} else if (strcmp(name, "value_size") == 0) {
2045 			__u32 sz;
2046 
2047 			if (!get_map_field_int(map->name, obj->btf, m, &sz))
2048 				return -EINVAL;
2049 			pr_debug("map '%s': found value_size = %u.\n",
2050 				 map->name, sz);
2051 			if (map->def.value_size && map->def.value_size != sz) {
2052 				pr_warn("map '%s': conflicting value size %u != %u.\n",
2053 					map->name, map->def.value_size, sz);
2054 				return -EINVAL;
2055 			}
2056 			map->def.value_size = sz;
2057 		} else if (strcmp(name, "value") == 0) {
2058 			__s64 sz;
2059 
2060 			t = btf__type_by_id(obj->btf, m->type);
2061 			if (!t) {
2062 				pr_warn("map '%s': value type [%d] not found.\n",
2063 					map->name, m->type);
2064 				return -EINVAL;
2065 			}
2066 			if (!btf_is_ptr(t)) {
2067 				pr_warn("map '%s': value spec is not PTR: %s.\n",
2068 					map->name, btf_kind_str(t));
2069 				return -EINVAL;
2070 			}
2071 			sz = btf__resolve_size(obj->btf, t->type);
2072 			if (sz < 0) {
2073 				pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2074 					map->name, t->type, (ssize_t)sz);
2075 				return sz;
2076 			}
2077 			pr_debug("map '%s': found value [%u], sz = %zd.\n",
2078 				 map->name, t->type, (ssize_t)sz);
2079 			if (map->def.value_size && map->def.value_size != sz) {
2080 				pr_warn("map '%s': conflicting value size %u != %zd.\n",
2081 					map->name, map->def.value_size, (ssize_t)sz);
2082 				return -EINVAL;
2083 			}
2084 			map->def.value_size = sz;
2085 			map->btf_value_type_id = t->type;
2086 		}
2087 		else if (strcmp(name, "values") == 0) {
2088 			int err;
2089 
2090 			if (is_inner) {
2091 				pr_warn("map '%s': multi-level inner maps not supported.\n",
2092 					map->name);
2093 				return -ENOTSUP;
2094 			}
2095 			if (i != vlen - 1) {
2096 				pr_warn("map '%s': '%s' member should be last.\n",
2097 					map->name, name);
2098 				return -EINVAL;
2099 			}
2100 			if (!bpf_map_type__is_map_in_map(map->def.type)) {
2101 				pr_warn("map '%s': should be map-in-map.\n",
2102 					map->name);
2103 				return -ENOTSUP;
2104 			}
2105 			if (map->def.value_size && map->def.value_size != 4) {
2106 				pr_warn("map '%s': conflicting value size %u != 4.\n",
2107 					map->name, map->def.value_size);
2108 				return -EINVAL;
2109 			}
2110 			map->def.value_size = 4;
2111 			t = btf__type_by_id(obj->btf, m->type);
2112 			if (!t) {
2113 				pr_warn("map '%s': map-in-map inner type [%d] not found.\n",
2114 					map->name, m->type);
2115 				return -EINVAL;
2116 			}
2117 			if (!btf_is_array(t) || btf_array(t)->nelems) {
2118 				pr_warn("map '%s': map-in-map inner spec is not a zero-sized array.\n",
2119 					map->name);
2120 				return -EINVAL;
2121 			}
2122 			t = skip_mods_and_typedefs(obj->btf, btf_array(t)->type,
2123 						   NULL);
2124 			if (!btf_is_ptr(t)) {
2125 				pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2126 					map->name, btf_kind_str(t));
2127 				return -EINVAL;
2128 			}
2129 			t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
2130 			if (!btf_is_struct(t)) {
2131 				pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2132 					map->name, btf_kind_str(t));
2133 				return -EINVAL;
2134 			}
2135 
2136 			map->inner_map = calloc(1, sizeof(*map->inner_map));
2137 			if (!map->inner_map)
2138 				return -ENOMEM;
2139 			map->inner_map->sec_idx = obj->efile.btf_maps_shndx;
2140 			map->inner_map->name = malloc(strlen(map->name) +
2141 						      sizeof(".inner") + 1);
2142 			if (!map->inner_map->name)
2143 				return -ENOMEM;
2144 			sprintf(map->inner_map->name, "%s.inner", map->name);
2145 
2146 			err = parse_btf_map_def(obj, map->inner_map, t, strict,
2147 						true /* is_inner */, NULL);
2148 			if (err)
2149 				return err;
2150 		} else if (strcmp(name, "pinning") == 0) {
2151 			__u32 val;
2152 			int err;
2153 
2154 			if (is_inner) {
2155 				pr_debug("map '%s': inner def can't be pinned.\n",
2156 					 map->name);
2157 				return -EINVAL;
2158 			}
2159 			if (!get_map_field_int(map->name, obj->btf, m, &val))
2160 				return -EINVAL;
2161 			pr_debug("map '%s': found pinning = %u.\n",
2162 				 map->name, val);
2163 
2164 			if (val != LIBBPF_PIN_NONE &&
2165 			    val != LIBBPF_PIN_BY_NAME) {
2166 				pr_warn("map '%s': invalid pinning value %u.\n",
2167 					map->name, val);
2168 				return -EINVAL;
2169 			}
2170 			if (val == LIBBPF_PIN_BY_NAME) {
2171 				err = build_map_pin_path(map, pin_root_path);
2172 				if (err) {
2173 					pr_warn("map '%s': couldn't build pin path.\n",
2174 						map->name);
2175 					return err;
2176 				}
2177 			}
2178 		} else {
2179 			if (strict) {
2180 				pr_warn("map '%s': unknown field '%s'.\n",
2181 					map->name, name);
2182 				return -ENOTSUP;
2183 			}
2184 			pr_debug("map '%s': ignoring unknown field '%s'.\n",
2185 				 map->name, name);
2186 		}
2187 	}
2188 
2189 	if (map->def.type == BPF_MAP_TYPE_UNSPEC) {
2190 		pr_warn("map '%s': map type isn't specified.\n", map->name);
2191 		return -EINVAL;
2192 	}
2193 
2194 	return 0;
2195 }
2196 
2197 static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2198 					 const struct btf_type *sec,
2199 					 int var_idx, int sec_idx,
2200 					 const Elf_Data *data, bool strict,
2201 					 const char *pin_root_path)
2202 {
2203 	const struct btf_type *var, *def;
2204 	const struct btf_var_secinfo *vi;
2205 	const struct btf_var *var_extra;
2206 	const char *map_name;
2207 	struct bpf_map *map;
2208 
2209 	vi = btf_var_secinfos(sec) + var_idx;
2210 	var = btf__type_by_id(obj->btf, vi->type);
2211 	var_extra = btf_var(var);
2212 	map_name = btf__name_by_offset(obj->btf, var->name_off);
2213 
2214 	if (map_name == NULL || map_name[0] == '\0') {
2215 		pr_warn("map #%d: empty name.\n", var_idx);
2216 		return -EINVAL;
2217 	}
2218 	if ((__u64)vi->offset + vi->size > data->d_size) {
2219 		pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2220 		return -EINVAL;
2221 	}
2222 	if (!btf_is_var(var)) {
2223 		pr_warn("map '%s': unexpected var kind %s.\n",
2224 			map_name, btf_kind_str(var));
2225 		return -EINVAL;
2226 	}
2227 	if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2228 	    var_extra->linkage != BTF_VAR_STATIC) {
2229 		pr_warn("map '%s': unsupported var linkage %u.\n",
2230 			map_name, var_extra->linkage);
2231 		return -EOPNOTSUPP;
2232 	}
2233 
2234 	def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2235 	if (!btf_is_struct(def)) {
2236 		pr_warn("map '%s': unexpected def kind %s.\n",
2237 			map_name, btf_kind_str(var));
2238 		return -EINVAL;
2239 	}
2240 	if (def->size > vi->size) {
2241 		pr_warn("map '%s': invalid def size.\n", map_name);
2242 		return -EINVAL;
2243 	}
2244 
2245 	map = bpf_object__add_map(obj);
2246 	if (IS_ERR(map))
2247 		return PTR_ERR(map);
2248 	map->name = strdup(map_name);
2249 	if (!map->name) {
2250 		pr_warn("map '%s': failed to alloc map name.\n", map_name);
2251 		return -ENOMEM;
2252 	}
2253 	map->libbpf_type = LIBBPF_MAP_UNSPEC;
2254 	map->def.type = BPF_MAP_TYPE_UNSPEC;
2255 	map->sec_idx = sec_idx;
2256 	map->sec_offset = vi->offset;
2257 	map->btf_var_idx = var_idx;
2258 	pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2259 		 map_name, map->sec_idx, map->sec_offset);
2260 
2261 	return parse_btf_map_def(obj, map, def, strict, false, pin_root_path);
2262 }
2263 
2264 static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2265 					  const char *pin_root_path)
2266 {
2267 	const struct btf_type *sec = NULL;
2268 	int nr_types, i, vlen, err;
2269 	const struct btf_type *t;
2270 	const char *name;
2271 	Elf_Data *data;
2272 	Elf_Scn *scn;
2273 
2274 	if (obj->efile.btf_maps_shndx < 0)
2275 		return 0;
2276 
2277 	scn = elf_getscn(obj->efile.elf, obj->efile.btf_maps_shndx);
2278 	if (scn)
2279 		data = elf_getdata(scn, NULL);
2280 	if (!scn || !data) {
2281 		pr_warn("failed to get Elf_Data from map section %d (%s)\n",
2282 			obj->efile.maps_shndx, MAPS_ELF_SEC);
2283 		return -EINVAL;
2284 	}
2285 
2286 	nr_types = btf__get_nr_types(obj->btf);
2287 	for (i = 1; i <= nr_types; i++) {
2288 		t = btf__type_by_id(obj->btf, i);
2289 		if (!btf_is_datasec(t))
2290 			continue;
2291 		name = btf__name_by_offset(obj->btf, t->name_off);
2292 		if (strcmp(name, MAPS_ELF_SEC) == 0) {
2293 			sec = t;
2294 			obj->efile.btf_maps_sec_btf_id = i;
2295 			break;
2296 		}
2297 	}
2298 
2299 	if (!sec) {
2300 		pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
2301 		return -ENOENT;
2302 	}
2303 
2304 	vlen = btf_vlen(sec);
2305 	for (i = 0; i < vlen; i++) {
2306 		err = bpf_object__init_user_btf_map(obj, sec, i,
2307 						    obj->efile.btf_maps_shndx,
2308 						    data, strict,
2309 						    pin_root_path);
2310 		if (err)
2311 			return err;
2312 	}
2313 
2314 	return 0;
2315 }
2316 
2317 static int bpf_object__init_maps(struct bpf_object *obj,
2318 				 const struct bpf_object_open_opts *opts)
2319 {
2320 	const char *pin_root_path;
2321 	bool strict;
2322 	int err;
2323 
2324 	strict = !OPTS_GET(opts, relaxed_maps, false);
2325 	pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
2326 
2327 	err = bpf_object__init_user_maps(obj, strict);
2328 	err = err ?: bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
2329 	err = err ?: bpf_object__init_global_data_maps(obj);
2330 	err = err ?: bpf_object__init_kconfig_map(obj);
2331 	err = err ?: bpf_object__init_struct_ops_maps(obj);
2332 	if (err)
2333 		return err;
2334 
2335 	return 0;
2336 }
2337 
2338 static bool section_have_execinstr(struct bpf_object *obj, int idx)
2339 {
2340 	Elf_Scn *scn;
2341 	GElf_Shdr sh;
2342 
2343 	scn = elf_getscn(obj->efile.elf, idx);
2344 	if (!scn)
2345 		return false;
2346 
2347 	if (gelf_getshdr(scn, &sh) != &sh)
2348 		return false;
2349 
2350 	if (sh.sh_flags & SHF_EXECINSTR)
2351 		return true;
2352 
2353 	return false;
2354 }
2355 
2356 static bool btf_needs_sanitization(struct bpf_object *obj)
2357 {
2358 	bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
2359 	bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
2360 	bool has_func = kernel_supports(FEAT_BTF_FUNC);
2361 
2362 	return !has_func || !has_datasec || !has_func_global;
2363 }
2364 
2365 static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
2366 {
2367 	bool has_func_global = kernel_supports(FEAT_BTF_GLOBAL_FUNC);
2368 	bool has_datasec = kernel_supports(FEAT_BTF_DATASEC);
2369 	bool has_func = kernel_supports(FEAT_BTF_FUNC);
2370 	struct btf_type *t;
2371 	int i, j, vlen;
2372 
2373 	for (i = 1; i <= btf__get_nr_types(btf); i++) {
2374 		t = (struct btf_type *)btf__type_by_id(btf, i);
2375 
2376 		if (!has_datasec && btf_is_var(t)) {
2377 			/* replace VAR with INT */
2378 			t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
2379 			/*
2380 			 * using size = 1 is the safest choice, 4 will be too
2381 			 * big and cause kernel BTF validation failure if
2382 			 * original variable took less than 4 bytes
2383 			 */
2384 			t->size = 1;
2385 			*(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
2386 		} else if (!has_datasec && btf_is_datasec(t)) {
2387 			/* replace DATASEC with STRUCT */
2388 			const struct btf_var_secinfo *v = btf_var_secinfos(t);
2389 			struct btf_member *m = btf_members(t);
2390 			struct btf_type *vt;
2391 			char *name;
2392 
2393 			name = (char *)btf__name_by_offset(btf, t->name_off);
2394 			while (*name) {
2395 				if (*name == '.')
2396 					*name = '_';
2397 				name++;
2398 			}
2399 
2400 			vlen = btf_vlen(t);
2401 			t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
2402 			for (j = 0; j < vlen; j++, v++, m++) {
2403 				/* order of field assignments is important */
2404 				m->offset = v->offset * 8;
2405 				m->type = v->type;
2406 				/* preserve variable name as member name */
2407 				vt = (void *)btf__type_by_id(btf, v->type);
2408 				m->name_off = vt->name_off;
2409 			}
2410 		} else if (!has_func && btf_is_func_proto(t)) {
2411 			/* replace FUNC_PROTO with ENUM */
2412 			vlen = btf_vlen(t);
2413 			t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
2414 			t->size = sizeof(__u32); /* kernel enforced */
2415 		} else if (!has_func && btf_is_func(t)) {
2416 			/* replace FUNC with TYPEDEF */
2417 			t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
2418 		} else if (!has_func_global && btf_is_func(t)) {
2419 			/* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2420 			t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
2421 		}
2422 	}
2423 }
2424 
2425 static bool libbpf_needs_btf(const struct bpf_object *obj)
2426 {
2427 	return obj->efile.btf_maps_shndx >= 0 ||
2428 	       obj->efile.st_ops_shndx >= 0 ||
2429 	       obj->nr_extern > 0;
2430 }
2431 
2432 static bool kernel_needs_btf(const struct bpf_object *obj)
2433 {
2434 	return obj->efile.st_ops_shndx >= 0;
2435 }
2436 
2437 static int bpf_object__init_btf(struct bpf_object *obj,
2438 				Elf_Data *btf_data,
2439 				Elf_Data *btf_ext_data)
2440 {
2441 	int err = -ENOENT;
2442 
2443 	if (btf_data) {
2444 		obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
2445 		if (IS_ERR(obj->btf)) {
2446 			err = PTR_ERR(obj->btf);
2447 			obj->btf = NULL;
2448 			pr_warn("Error loading ELF section %s: %d.\n",
2449 				BTF_ELF_SEC, err);
2450 			goto out;
2451 		}
2452 		/* enforce 8-byte pointers for BPF-targeted BTFs */
2453 		btf__set_pointer_size(obj->btf, 8);
2454 		err = 0;
2455 	}
2456 	if (btf_ext_data) {
2457 		if (!obj->btf) {
2458 			pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2459 				 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
2460 			goto out;
2461 		}
2462 		obj->btf_ext = btf_ext__new(btf_ext_data->d_buf,
2463 					    btf_ext_data->d_size);
2464 		if (IS_ERR(obj->btf_ext)) {
2465 			pr_warn("Error loading ELF section %s: %ld. Ignored and continue.\n",
2466 				BTF_EXT_ELF_SEC, PTR_ERR(obj->btf_ext));
2467 			obj->btf_ext = NULL;
2468 			goto out;
2469 		}
2470 	}
2471 out:
2472 	if (err && libbpf_needs_btf(obj)) {
2473 		pr_warn("BTF is required, but is missing or corrupted.\n");
2474 		return err;
2475 	}
2476 	return 0;
2477 }
2478 
2479 static int bpf_object__finalize_btf(struct bpf_object *obj)
2480 {
2481 	int err;
2482 
2483 	if (!obj->btf)
2484 		return 0;
2485 
2486 	err = btf__finalize_data(obj, obj->btf);
2487 	if (err) {
2488 		pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err);
2489 		return err;
2490 	}
2491 
2492 	return 0;
2493 }
2494 
2495 static inline bool libbpf_prog_needs_vmlinux_btf(struct bpf_program *prog)
2496 {
2497 	if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
2498 	    prog->type == BPF_PROG_TYPE_LSM)
2499 		return true;
2500 
2501 	/* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
2502 	 * also need vmlinux BTF
2503 	 */
2504 	if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
2505 		return true;
2506 
2507 	return false;
2508 }
2509 
2510 static int bpf_object__load_vmlinux_btf(struct bpf_object *obj)
2511 {
2512 	bool need_vmlinux_btf = false;
2513 	struct bpf_program *prog;
2514 	int err;
2515 
2516 	/* CO-RE relocations need kernel BTF */
2517 	if (obj->btf_ext && obj->btf_ext->core_relo_info.len)
2518 		need_vmlinux_btf = true;
2519 
2520 	bpf_object__for_each_program(prog, obj) {
2521 		if (!prog->load)
2522 			continue;
2523 		if (libbpf_prog_needs_vmlinux_btf(prog)) {
2524 			need_vmlinux_btf = true;
2525 			break;
2526 		}
2527 	}
2528 
2529 	if (!need_vmlinux_btf)
2530 		return 0;
2531 
2532 	obj->btf_vmlinux = libbpf_find_kernel_btf();
2533 	if (IS_ERR(obj->btf_vmlinux)) {
2534 		err = PTR_ERR(obj->btf_vmlinux);
2535 		pr_warn("Error loading vmlinux BTF: %d\n", err);
2536 		obj->btf_vmlinux = NULL;
2537 		return err;
2538 	}
2539 	return 0;
2540 }
2541 
2542 static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
2543 {
2544 	struct btf *kern_btf = obj->btf;
2545 	bool btf_mandatory, sanitize;
2546 	int err = 0;
2547 
2548 	if (!obj->btf)
2549 		return 0;
2550 
2551 	if (!kernel_supports(FEAT_BTF)) {
2552 		if (kernel_needs_btf(obj)) {
2553 			err = -EOPNOTSUPP;
2554 			goto report;
2555 		}
2556 		pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
2557 		return 0;
2558 	}
2559 
2560 	sanitize = btf_needs_sanitization(obj);
2561 	if (sanitize) {
2562 		const void *raw_data;
2563 		__u32 sz;
2564 
2565 		/* clone BTF to sanitize a copy and leave the original intact */
2566 		raw_data = btf__get_raw_data(obj->btf, &sz);
2567 		kern_btf = btf__new(raw_data, sz);
2568 		if (IS_ERR(kern_btf))
2569 			return PTR_ERR(kern_btf);
2570 
2571 		/* enforce 8-byte pointers for BPF-targeted BTFs */
2572 		btf__set_pointer_size(obj->btf, 8);
2573 		bpf_object__sanitize_btf(obj, kern_btf);
2574 	}
2575 
2576 	err = btf__load(kern_btf);
2577 	if (sanitize) {
2578 		if (!err) {
2579 			/* move fd to libbpf's BTF */
2580 			btf__set_fd(obj->btf, btf__fd(kern_btf));
2581 			btf__set_fd(kern_btf, -1);
2582 		}
2583 		btf__free(kern_btf);
2584 	}
2585 report:
2586 	if (err) {
2587 		btf_mandatory = kernel_needs_btf(obj);
2588 		pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
2589 			btf_mandatory ? "BTF is mandatory, can't proceed."
2590 				      : "BTF is optional, ignoring.");
2591 		if (!btf_mandatory)
2592 			err = 0;
2593 	}
2594 	return err;
2595 }
2596 
2597 static int bpf_object__elf_collect(struct bpf_object *obj)
2598 {
2599 	Elf *elf = obj->efile.elf;
2600 	GElf_Ehdr *ep = &obj->efile.ehdr;
2601 	Elf_Data *btf_ext_data = NULL;
2602 	Elf_Data *btf_data = NULL;
2603 	Elf_Scn *scn = NULL;
2604 	int idx = 0, err = 0;
2605 
2606 	/* Elf is corrupted/truncated, avoid calling elf_strptr. */
2607 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
2608 		pr_warn("failed to get e_shstrndx from %s\n", obj->path);
2609 		return -LIBBPF_ERRNO__FORMAT;
2610 	}
2611 
2612 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
2613 		char *name;
2614 		GElf_Shdr sh;
2615 		Elf_Data *data;
2616 
2617 		idx++;
2618 		if (gelf_getshdr(scn, &sh) != &sh) {
2619 			pr_warn("failed to get section(%d) header from %s\n",
2620 				idx, obj->path);
2621 			return -LIBBPF_ERRNO__FORMAT;
2622 		}
2623 
2624 		name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
2625 		if (!name) {
2626 			pr_warn("failed to get section(%d) name from %s\n",
2627 				idx, obj->path);
2628 			return -LIBBPF_ERRNO__FORMAT;
2629 		}
2630 
2631 		data = elf_getdata(scn, 0);
2632 		if (!data) {
2633 			pr_warn("failed to get section(%d) data from %s(%s)\n",
2634 				idx, name, obj->path);
2635 			return -LIBBPF_ERRNO__FORMAT;
2636 		}
2637 		pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
2638 			 idx, name, (unsigned long)data->d_size,
2639 			 (int)sh.sh_link, (unsigned long)sh.sh_flags,
2640 			 (int)sh.sh_type);
2641 
2642 		if (strcmp(name, "license") == 0) {
2643 			err = bpf_object__init_license(obj,
2644 						       data->d_buf,
2645 						       data->d_size);
2646 			if (err)
2647 				return err;
2648 		} else if (strcmp(name, "version") == 0) {
2649 			err = bpf_object__init_kversion(obj,
2650 							data->d_buf,
2651 							data->d_size);
2652 			if (err)
2653 				return err;
2654 		} else if (strcmp(name, "maps") == 0) {
2655 			obj->efile.maps_shndx = idx;
2656 		} else if (strcmp(name, MAPS_ELF_SEC) == 0) {
2657 			obj->efile.btf_maps_shndx = idx;
2658 		} else if (strcmp(name, BTF_ELF_SEC) == 0) {
2659 			btf_data = data;
2660 		} else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
2661 			btf_ext_data = data;
2662 		} else if (sh.sh_type == SHT_SYMTAB) {
2663 			if (obj->efile.symbols) {
2664 				pr_warn("bpf: multiple SYMTAB in %s\n",
2665 					obj->path);
2666 				return -LIBBPF_ERRNO__FORMAT;
2667 			}
2668 			obj->efile.symbols = data;
2669 			obj->efile.symbols_shndx = idx;
2670 			obj->efile.strtabidx = sh.sh_link;
2671 		} else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
2672 			if (sh.sh_flags & SHF_EXECINSTR) {
2673 				if (strcmp(name, ".text") == 0)
2674 					obj->efile.text_shndx = idx;
2675 				err = bpf_object__add_program(obj, data->d_buf,
2676 							      data->d_size,
2677 							      name, idx);
2678 				if (err) {
2679 					char errmsg[STRERR_BUFSIZE];
2680 					char *cp;
2681 
2682 					cp = libbpf_strerror_r(-err, errmsg,
2683 							       sizeof(errmsg));
2684 					pr_warn("failed to alloc program %s (%s): %s",
2685 						name, obj->path, cp);
2686 					return err;
2687 				}
2688 			} else if (strcmp(name, DATA_SEC) == 0) {
2689 				obj->efile.data = data;
2690 				obj->efile.data_shndx = idx;
2691 			} else if (strcmp(name, RODATA_SEC) == 0) {
2692 				obj->efile.rodata = data;
2693 				obj->efile.rodata_shndx = idx;
2694 			} else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
2695 				obj->efile.st_ops_data = data;
2696 				obj->efile.st_ops_shndx = idx;
2697 			} else {
2698 				pr_debug("skip section(%d) %s\n", idx, name);
2699 			}
2700 		} else if (sh.sh_type == SHT_REL) {
2701 			int nr_sects = obj->efile.nr_reloc_sects;
2702 			void *sects = obj->efile.reloc_sects;
2703 			int sec = sh.sh_info; /* points to other section */
2704 
2705 			/* Only do relo for section with exec instructions */
2706 			if (!section_have_execinstr(obj, sec) &&
2707 			    strcmp(name, ".rel" STRUCT_OPS_SEC) &&
2708 			    strcmp(name, ".rel" MAPS_ELF_SEC)) {
2709 				pr_debug("skip relo %s(%d) for section(%d)\n",
2710 					 name, idx, sec);
2711 				continue;
2712 			}
2713 
2714 			sects = libbpf_reallocarray(sects, nr_sects + 1,
2715 						    sizeof(*obj->efile.reloc_sects));
2716 			if (!sects) {
2717 				pr_warn("reloc_sects realloc failed\n");
2718 				return -ENOMEM;
2719 			}
2720 
2721 			obj->efile.reloc_sects = sects;
2722 			obj->efile.nr_reloc_sects++;
2723 
2724 			obj->efile.reloc_sects[nr_sects].shdr = sh;
2725 			obj->efile.reloc_sects[nr_sects].data = data;
2726 		} else if (sh.sh_type == SHT_NOBITS &&
2727 			   strcmp(name, BSS_SEC) == 0) {
2728 			obj->efile.bss = data;
2729 			obj->efile.bss_shndx = idx;
2730 		} else {
2731 			pr_debug("skip section(%d) %s\n", idx, name);
2732 		}
2733 	}
2734 
2735 	if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
2736 		pr_warn("Corrupted ELF file: index of strtab invalid\n");
2737 		return -LIBBPF_ERRNO__FORMAT;
2738 	}
2739 	return bpf_object__init_btf(obj, btf_data, btf_ext_data);
2740 }
2741 
2742 static bool sym_is_extern(const GElf_Sym *sym)
2743 {
2744 	int bind = GELF_ST_BIND(sym->st_info);
2745 	/* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
2746 	return sym->st_shndx == SHN_UNDEF &&
2747 	       (bind == STB_GLOBAL || bind == STB_WEAK) &&
2748 	       GELF_ST_TYPE(sym->st_info) == STT_NOTYPE;
2749 }
2750 
2751 static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
2752 {
2753 	const struct btf_type *t;
2754 	const char *var_name;
2755 	int i, n;
2756 
2757 	if (!btf)
2758 		return -ESRCH;
2759 
2760 	n = btf__get_nr_types(btf);
2761 	for (i = 1; i <= n; i++) {
2762 		t = btf__type_by_id(btf, i);
2763 
2764 		if (!btf_is_var(t))
2765 			continue;
2766 
2767 		var_name = btf__name_by_offset(btf, t->name_off);
2768 		if (strcmp(var_name, ext_name))
2769 			continue;
2770 
2771 		if (btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
2772 			return -EINVAL;
2773 
2774 		return i;
2775 	}
2776 
2777 	return -ENOENT;
2778 }
2779 
2780 static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
2781 	const struct btf_var_secinfo *vs;
2782 	const struct btf_type *t;
2783 	int i, j, n;
2784 
2785 	if (!btf)
2786 		return -ESRCH;
2787 
2788 	n = btf__get_nr_types(btf);
2789 	for (i = 1; i <= n; i++) {
2790 		t = btf__type_by_id(btf, i);
2791 
2792 		if (!btf_is_datasec(t))
2793 			continue;
2794 
2795 		vs = btf_var_secinfos(t);
2796 		for (j = 0; j < btf_vlen(t); j++, vs++) {
2797 			if (vs->type == ext_btf_id)
2798 				return i;
2799 		}
2800 	}
2801 
2802 	return -ENOENT;
2803 }
2804 
2805 static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
2806 				     bool *is_signed)
2807 {
2808 	const struct btf_type *t;
2809 	const char *name;
2810 
2811 	t = skip_mods_and_typedefs(btf, id, NULL);
2812 	name = btf__name_by_offset(btf, t->name_off);
2813 
2814 	if (is_signed)
2815 		*is_signed = false;
2816 	switch (btf_kind(t)) {
2817 	case BTF_KIND_INT: {
2818 		int enc = btf_int_encoding(t);
2819 
2820 		if (enc & BTF_INT_BOOL)
2821 			return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
2822 		if (is_signed)
2823 			*is_signed = enc & BTF_INT_SIGNED;
2824 		if (t->size == 1)
2825 			return KCFG_CHAR;
2826 		if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
2827 			return KCFG_UNKNOWN;
2828 		return KCFG_INT;
2829 	}
2830 	case BTF_KIND_ENUM:
2831 		if (t->size != 4)
2832 			return KCFG_UNKNOWN;
2833 		if (strcmp(name, "libbpf_tristate"))
2834 			return KCFG_UNKNOWN;
2835 		return KCFG_TRISTATE;
2836 	case BTF_KIND_ARRAY:
2837 		if (btf_array(t)->nelems == 0)
2838 			return KCFG_UNKNOWN;
2839 		if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
2840 			return KCFG_UNKNOWN;
2841 		return KCFG_CHAR_ARR;
2842 	default:
2843 		return KCFG_UNKNOWN;
2844 	}
2845 }
2846 
2847 static int cmp_externs(const void *_a, const void *_b)
2848 {
2849 	const struct extern_desc *a = _a;
2850 	const struct extern_desc *b = _b;
2851 
2852 	if (a->type != b->type)
2853 		return a->type < b->type ? -1 : 1;
2854 
2855 	if (a->type == EXT_KCFG) {
2856 		/* descending order by alignment requirements */
2857 		if (a->kcfg.align != b->kcfg.align)
2858 			return a->kcfg.align > b->kcfg.align ? -1 : 1;
2859 		/* ascending order by size, within same alignment class */
2860 		if (a->kcfg.sz != b->kcfg.sz)
2861 			return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
2862 	}
2863 
2864 	/* resolve ties by name */
2865 	return strcmp(a->name, b->name);
2866 }
2867 
2868 static int find_int_btf_id(const struct btf *btf)
2869 {
2870 	const struct btf_type *t;
2871 	int i, n;
2872 
2873 	n = btf__get_nr_types(btf);
2874 	for (i = 1; i <= n; i++) {
2875 		t = btf__type_by_id(btf, i);
2876 
2877 		if (btf_is_int(t) && btf_int_bits(t) == 32)
2878 			return i;
2879 	}
2880 
2881 	return 0;
2882 }
2883 
2884 static int bpf_object__collect_externs(struct bpf_object *obj)
2885 {
2886 	struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
2887 	const struct btf_type *t;
2888 	struct extern_desc *ext;
2889 	int i, n, off;
2890 	const char *ext_name, *sec_name;
2891 	Elf_Scn *scn;
2892 	GElf_Shdr sh;
2893 
2894 	if (!obj->efile.symbols)
2895 		return 0;
2896 
2897 	scn = elf_getscn(obj->efile.elf, obj->efile.symbols_shndx);
2898 	if (!scn)
2899 		return -LIBBPF_ERRNO__FORMAT;
2900 	if (gelf_getshdr(scn, &sh) != &sh)
2901 		return -LIBBPF_ERRNO__FORMAT;
2902 	n = sh.sh_size / sh.sh_entsize;
2903 
2904 	pr_debug("looking for externs among %d symbols...\n", n);
2905 	for (i = 0; i < n; i++) {
2906 		GElf_Sym sym;
2907 
2908 		if (!gelf_getsym(obj->efile.symbols, i, &sym))
2909 			return -LIBBPF_ERRNO__FORMAT;
2910 		if (!sym_is_extern(&sym))
2911 			continue;
2912 		ext_name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
2913 				      sym.st_name);
2914 		if (!ext_name || !ext_name[0])
2915 			continue;
2916 
2917 		ext = obj->externs;
2918 		ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
2919 		if (!ext)
2920 			return -ENOMEM;
2921 		obj->externs = ext;
2922 		ext = &ext[obj->nr_extern];
2923 		memset(ext, 0, sizeof(*ext));
2924 		obj->nr_extern++;
2925 
2926 		ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
2927 		if (ext->btf_id <= 0) {
2928 			pr_warn("failed to find BTF for extern '%s': %d\n",
2929 				ext_name, ext->btf_id);
2930 			return ext->btf_id;
2931 		}
2932 		t = btf__type_by_id(obj->btf, ext->btf_id);
2933 		ext->name = btf__name_by_offset(obj->btf, t->name_off);
2934 		ext->sym_idx = i;
2935 		ext->is_weak = GELF_ST_BIND(sym.st_info) == STB_WEAK;
2936 
2937 		ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
2938 		if (ext->sec_btf_id <= 0) {
2939 			pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
2940 				ext_name, ext->btf_id, ext->sec_btf_id);
2941 			return ext->sec_btf_id;
2942 		}
2943 		sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
2944 		sec_name = btf__name_by_offset(obj->btf, sec->name_off);
2945 
2946 		if (strcmp(sec_name, KCONFIG_SEC) == 0) {
2947 			kcfg_sec = sec;
2948 			ext->type = EXT_KCFG;
2949 			ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
2950 			if (ext->kcfg.sz <= 0) {
2951 				pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
2952 					ext_name, ext->kcfg.sz);
2953 				return ext->kcfg.sz;
2954 			}
2955 			ext->kcfg.align = btf__align_of(obj->btf, t->type);
2956 			if (ext->kcfg.align <= 0) {
2957 				pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
2958 					ext_name, ext->kcfg.align);
2959 				return -EINVAL;
2960 			}
2961 			ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
2962 						        &ext->kcfg.is_signed);
2963 			if (ext->kcfg.type == KCFG_UNKNOWN) {
2964 				pr_warn("extern (kcfg) '%s' type is unsupported\n", ext_name);
2965 				return -ENOTSUP;
2966 			}
2967 		} else if (strcmp(sec_name, KSYMS_SEC) == 0) {
2968 			const struct btf_type *vt;
2969 
2970 			ksym_sec = sec;
2971 			ext->type = EXT_KSYM;
2972 
2973 			vt = skip_mods_and_typedefs(obj->btf, t->type, NULL);
2974 			if (!btf_is_void(vt)) {
2975 				pr_warn("extern (ksym) '%s' is not typeless (void)\n", ext_name);
2976 				return -ENOTSUP;
2977 			}
2978 		} else {
2979 			pr_warn("unrecognized extern section '%s'\n", sec_name);
2980 			return -ENOTSUP;
2981 		}
2982 	}
2983 	pr_debug("collected %d externs total\n", obj->nr_extern);
2984 
2985 	if (!obj->nr_extern)
2986 		return 0;
2987 
2988 	/* sort externs by type, for kcfg ones also by (align, size, name) */
2989 	qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
2990 
2991 	/* for .ksyms section, we need to turn all externs into allocated
2992 	 * variables in BTF to pass kernel verification; we do this by
2993 	 * pretending that each extern is a 8-byte variable
2994 	 */
2995 	if (ksym_sec) {
2996 		/* find existing 4-byte integer type in BTF to use for fake
2997 		 * extern variables in DATASEC
2998 		 */
2999 		int int_btf_id = find_int_btf_id(obj->btf);
3000 
3001 		for (i = 0; i < obj->nr_extern; i++) {
3002 			ext = &obj->externs[i];
3003 			if (ext->type != EXT_KSYM)
3004 				continue;
3005 			pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
3006 				 i, ext->sym_idx, ext->name);
3007 		}
3008 
3009 		sec = ksym_sec;
3010 		n = btf_vlen(sec);
3011 		for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
3012 			struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3013 			struct btf_type *vt;
3014 
3015 			vt = (void *)btf__type_by_id(obj->btf, vs->type);
3016 			ext_name = btf__name_by_offset(obj->btf, vt->name_off);
3017 			ext = find_extern_by_name(obj, ext_name);
3018 			if (!ext) {
3019 				pr_warn("failed to find extern definition for BTF var '%s'\n",
3020 					ext_name);
3021 				return -ESRCH;
3022 			}
3023 			btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3024 			vt->type = int_btf_id;
3025 			vs->offset = off;
3026 			vs->size = sizeof(int);
3027 		}
3028 		sec->size = off;
3029 	}
3030 
3031 	if (kcfg_sec) {
3032 		sec = kcfg_sec;
3033 		/* for kcfg externs calculate their offsets within a .kconfig map */
3034 		off = 0;
3035 		for (i = 0; i < obj->nr_extern; i++) {
3036 			ext = &obj->externs[i];
3037 			if (ext->type != EXT_KCFG)
3038 				continue;
3039 
3040 			ext->kcfg.data_off = roundup(off, ext->kcfg.align);
3041 			off = ext->kcfg.data_off + ext->kcfg.sz;
3042 			pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
3043 				 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
3044 		}
3045 		sec->size = off;
3046 		n = btf_vlen(sec);
3047 		for (i = 0; i < n; i++) {
3048 			struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3049 
3050 			t = btf__type_by_id(obj->btf, vs->type);
3051 			ext_name = btf__name_by_offset(obj->btf, t->name_off);
3052 			ext = find_extern_by_name(obj, ext_name);
3053 			if (!ext) {
3054 				pr_warn("failed to find extern definition for BTF var '%s'\n",
3055 					ext_name);
3056 				return -ESRCH;
3057 			}
3058 			btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3059 			vs->offset = ext->kcfg.data_off;
3060 		}
3061 	}
3062 	return 0;
3063 }
3064 
3065 static struct bpf_program *
3066 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
3067 {
3068 	struct bpf_program *prog;
3069 	size_t i;
3070 
3071 	for (i = 0; i < obj->nr_programs; i++) {
3072 		prog = &obj->programs[i];
3073 		if (prog->idx == idx)
3074 			return prog;
3075 	}
3076 	return NULL;
3077 }
3078 
3079 struct bpf_program *
3080 bpf_object__find_program_by_title(const struct bpf_object *obj,
3081 				  const char *title)
3082 {
3083 	struct bpf_program *pos;
3084 
3085 	bpf_object__for_each_program(pos, obj) {
3086 		if (pos->section_name && !strcmp(pos->section_name, title))
3087 			return pos;
3088 	}
3089 	return NULL;
3090 }
3091 
3092 struct bpf_program *
3093 bpf_object__find_program_by_name(const struct bpf_object *obj,
3094 				 const char *name)
3095 {
3096 	struct bpf_program *prog;
3097 
3098 	bpf_object__for_each_program(prog, obj) {
3099 		if (!strcmp(prog->name, name))
3100 			return prog;
3101 	}
3102 	return NULL;
3103 }
3104 
3105 static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
3106 				      int shndx)
3107 {
3108 	return shndx == obj->efile.data_shndx ||
3109 	       shndx == obj->efile.bss_shndx ||
3110 	       shndx == obj->efile.rodata_shndx;
3111 }
3112 
3113 static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
3114 				      int shndx)
3115 {
3116 	return shndx == obj->efile.maps_shndx ||
3117 	       shndx == obj->efile.btf_maps_shndx;
3118 }
3119 
3120 static enum libbpf_map_type
3121 bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
3122 {
3123 	if (shndx == obj->efile.data_shndx)
3124 		return LIBBPF_MAP_DATA;
3125 	else if (shndx == obj->efile.bss_shndx)
3126 		return LIBBPF_MAP_BSS;
3127 	else if (shndx == obj->efile.rodata_shndx)
3128 		return LIBBPF_MAP_RODATA;
3129 	else if (shndx == obj->efile.symbols_shndx)
3130 		return LIBBPF_MAP_KCONFIG;
3131 	else
3132 		return LIBBPF_MAP_UNSPEC;
3133 }
3134 
3135 static int bpf_program__record_reloc(struct bpf_program *prog,
3136 				     struct reloc_desc *reloc_desc,
3137 				     __u32 insn_idx, const char *name,
3138 				     const GElf_Sym *sym, const GElf_Rel *rel)
3139 {
3140 	struct bpf_insn *insn = &prog->insns[insn_idx];
3141 	size_t map_idx, nr_maps = prog->obj->nr_maps;
3142 	struct bpf_object *obj = prog->obj;
3143 	__u32 shdr_idx = sym->st_shndx;
3144 	enum libbpf_map_type type;
3145 	struct bpf_map *map;
3146 
3147 	/* sub-program call relocation */
3148 	if (insn->code == (BPF_JMP | BPF_CALL)) {
3149 		if (insn->src_reg != BPF_PSEUDO_CALL) {
3150 			pr_warn("incorrect bpf_call opcode\n");
3151 			return -LIBBPF_ERRNO__RELOC;
3152 		}
3153 		/* text_shndx can be 0, if no default "main" program exists */
3154 		if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
3155 			pr_warn("bad call relo against section %u\n", shdr_idx);
3156 			return -LIBBPF_ERRNO__RELOC;
3157 		}
3158 		if (sym->st_value % 8) {
3159 			pr_warn("bad call relo offset: %zu\n",
3160 				(size_t)sym->st_value);
3161 			return -LIBBPF_ERRNO__RELOC;
3162 		}
3163 		reloc_desc->type = RELO_CALL;
3164 		reloc_desc->insn_idx = insn_idx;
3165 		reloc_desc->sym_off = sym->st_value;
3166 		obj->has_pseudo_calls = true;
3167 		return 0;
3168 	}
3169 
3170 	if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
3171 		pr_warn("invalid relo for insns[%d].code 0x%x\n",
3172 			insn_idx, insn->code);
3173 		return -LIBBPF_ERRNO__RELOC;
3174 	}
3175 
3176 	if (sym_is_extern(sym)) {
3177 		int sym_idx = GELF_R_SYM(rel->r_info);
3178 		int i, n = obj->nr_extern;
3179 		struct extern_desc *ext;
3180 
3181 		for (i = 0; i < n; i++) {
3182 			ext = &obj->externs[i];
3183 			if (ext->sym_idx == sym_idx)
3184 				break;
3185 		}
3186 		if (i >= n) {
3187 			pr_warn("extern relo failed to find extern for sym %d\n",
3188 				sym_idx);
3189 			return -LIBBPF_ERRNO__RELOC;
3190 		}
3191 		pr_debug("found extern #%d '%s' (sym %d) for insn %u\n",
3192 			 i, ext->name, ext->sym_idx, insn_idx);
3193 		reloc_desc->type = RELO_EXTERN;
3194 		reloc_desc->insn_idx = insn_idx;
3195 		reloc_desc->sym_off = i; /* sym_off stores extern index */
3196 		return 0;
3197 	}
3198 
3199 	if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
3200 		pr_warn("invalid relo for \'%s\' in special section 0x%x; forgot to initialize global var?..\n",
3201 			name, shdr_idx);
3202 		return -LIBBPF_ERRNO__RELOC;
3203 	}
3204 
3205 	type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
3206 
3207 	/* generic map reference relocation */
3208 	if (type == LIBBPF_MAP_UNSPEC) {
3209 		if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
3210 			pr_warn("bad map relo against section %u\n",
3211 				shdr_idx);
3212 			return -LIBBPF_ERRNO__RELOC;
3213 		}
3214 		for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3215 			map = &obj->maps[map_idx];
3216 			if (map->libbpf_type != type ||
3217 			    map->sec_idx != sym->st_shndx ||
3218 			    map->sec_offset != sym->st_value)
3219 				continue;
3220 			pr_debug("found map %zd (%s, sec %d, off %zu) for insn %u\n",
3221 				 map_idx, map->name, map->sec_idx,
3222 				 map->sec_offset, insn_idx);
3223 			break;
3224 		}
3225 		if (map_idx >= nr_maps) {
3226 			pr_warn("map relo failed to find map for sec %u, off %zu\n",
3227 				shdr_idx, (size_t)sym->st_value);
3228 			return -LIBBPF_ERRNO__RELOC;
3229 		}
3230 		reloc_desc->type = RELO_LD64;
3231 		reloc_desc->insn_idx = insn_idx;
3232 		reloc_desc->map_idx = map_idx;
3233 		reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
3234 		return 0;
3235 	}
3236 
3237 	/* global data map relocation */
3238 	if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
3239 		pr_warn("bad data relo against section %u\n", shdr_idx);
3240 		return -LIBBPF_ERRNO__RELOC;
3241 	}
3242 	for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3243 		map = &obj->maps[map_idx];
3244 		if (map->libbpf_type != type)
3245 			continue;
3246 		pr_debug("found data map %zd (%s, sec %d, off %zu) for insn %u\n",
3247 			 map_idx, map->name, map->sec_idx, map->sec_offset,
3248 			 insn_idx);
3249 		break;
3250 	}
3251 	if (map_idx >= nr_maps) {
3252 		pr_warn("data relo failed to find map for sec %u\n",
3253 			shdr_idx);
3254 		return -LIBBPF_ERRNO__RELOC;
3255 	}
3256 
3257 	reloc_desc->type = RELO_DATA;
3258 	reloc_desc->insn_idx = insn_idx;
3259 	reloc_desc->map_idx = map_idx;
3260 	reloc_desc->sym_off = sym->st_value;
3261 	return 0;
3262 }
3263 
3264 static int
3265 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
3266 			   Elf_Data *data, struct bpf_object *obj)
3267 {
3268 	Elf_Data *symbols = obj->efile.symbols;
3269 	int err, i, nrels;
3270 
3271 	pr_debug("collecting relocating info for: '%s'\n", prog->section_name);
3272 	nrels = shdr->sh_size / shdr->sh_entsize;
3273 
3274 	prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
3275 	if (!prog->reloc_desc) {
3276 		pr_warn("failed to alloc memory in relocation\n");
3277 		return -ENOMEM;
3278 	}
3279 	prog->nr_reloc = nrels;
3280 
3281 	for (i = 0; i < nrels; i++) {
3282 		const char *name;
3283 		__u32 insn_idx;
3284 		GElf_Sym sym;
3285 		GElf_Rel rel;
3286 
3287 		if (!gelf_getrel(data, i, &rel)) {
3288 			pr_warn("relocation: failed to get %d reloc\n", i);
3289 			return -LIBBPF_ERRNO__FORMAT;
3290 		}
3291 		if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
3292 			pr_warn("relocation: symbol %"PRIx64" not found\n",
3293 				GELF_R_SYM(rel.r_info));
3294 			return -LIBBPF_ERRNO__FORMAT;
3295 		}
3296 		if (rel.r_offset % sizeof(struct bpf_insn))
3297 			return -LIBBPF_ERRNO__FORMAT;
3298 
3299 		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
3300 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
3301 				  sym.st_name) ? : "<?>";
3302 
3303 		pr_debug("relo for shdr %u, symb %zu, value %zu, type %d, bind %d, name %d (\'%s\'), insn %u\n",
3304 			 (__u32)sym.st_shndx, (size_t)GELF_R_SYM(rel.r_info),
3305 			 (size_t)sym.st_value, GELF_ST_TYPE(sym.st_info),
3306 			 GELF_ST_BIND(sym.st_info), sym.st_name, name,
3307 			 insn_idx);
3308 
3309 		err = bpf_program__record_reloc(prog, &prog->reloc_desc[i],
3310 						insn_idx, name, &sym, &rel);
3311 		if (err)
3312 			return err;
3313 	}
3314 	return 0;
3315 }
3316 
3317 static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
3318 {
3319 	struct bpf_map_def *def = &map->def;
3320 	__u32 key_type_id = 0, value_type_id = 0;
3321 	int ret;
3322 
3323 	/* if it's BTF-defined map, we don't need to search for type IDs.
3324 	 * For struct_ops map, it does not need btf_key_type_id and
3325 	 * btf_value_type_id.
3326 	 */
3327 	if (map->sec_idx == obj->efile.btf_maps_shndx ||
3328 	    bpf_map__is_struct_ops(map))
3329 		return 0;
3330 
3331 	if (!bpf_map__is_internal(map)) {
3332 		ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
3333 					   def->value_size, &key_type_id,
3334 					   &value_type_id);
3335 	} else {
3336 		/*
3337 		 * LLVM annotates global data differently in BTF, that is,
3338 		 * only as '.data', '.bss' or '.rodata'.
3339 		 */
3340 		ret = btf__find_by_name(obj->btf,
3341 				libbpf_type_to_btf_name[map->libbpf_type]);
3342 	}
3343 	if (ret < 0)
3344 		return ret;
3345 
3346 	map->btf_key_type_id = key_type_id;
3347 	map->btf_value_type_id = bpf_map__is_internal(map) ?
3348 				 ret : value_type_id;
3349 	return 0;
3350 }
3351 
3352 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
3353 {
3354 	struct bpf_map_info info = {};
3355 	__u32 len = sizeof(info);
3356 	int new_fd, err;
3357 	char *new_name;
3358 
3359 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
3360 	if (err)
3361 		return err;
3362 
3363 	new_name = strdup(info.name);
3364 	if (!new_name)
3365 		return -errno;
3366 
3367 	new_fd = open("/", O_RDONLY | O_CLOEXEC);
3368 	if (new_fd < 0) {
3369 		err = -errno;
3370 		goto err_free_new_name;
3371 	}
3372 
3373 	new_fd = dup3(fd, new_fd, O_CLOEXEC);
3374 	if (new_fd < 0) {
3375 		err = -errno;
3376 		goto err_close_new_fd;
3377 	}
3378 
3379 	err = zclose(map->fd);
3380 	if (err) {
3381 		err = -errno;
3382 		goto err_close_new_fd;
3383 	}
3384 	free(map->name);
3385 
3386 	map->fd = new_fd;
3387 	map->name = new_name;
3388 	map->def.type = info.type;
3389 	map->def.key_size = info.key_size;
3390 	map->def.value_size = info.value_size;
3391 	map->def.max_entries = info.max_entries;
3392 	map->def.map_flags = info.map_flags;
3393 	map->btf_key_type_id = info.btf_key_type_id;
3394 	map->btf_value_type_id = info.btf_value_type_id;
3395 	map->reused = true;
3396 
3397 	return 0;
3398 
3399 err_close_new_fd:
3400 	close(new_fd);
3401 err_free_new_name:
3402 	free(new_name);
3403 	return err;
3404 }
3405 
3406 __u32 bpf_map__max_entries(const struct bpf_map *map)
3407 {
3408 	return map->def.max_entries;
3409 }
3410 
3411 int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
3412 {
3413 	if (map->fd >= 0)
3414 		return -EBUSY;
3415 	map->def.max_entries = max_entries;
3416 	return 0;
3417 }
3418 
3419 int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
3420 {
3421 	if (!map || !max_entries)
3422 		return -EINVAL;
3423 
3424 	return bpf_map__set_max_entries(map, max_entries);
3425 }
3426 
3427 static int
3428 bpf_object__probe_loading(struct bpf_object *obj)
3429 {
3430 	struct bpf_load_program_attr attr;
3431 	char *cp, errmsg[STRERR_BUFSIZE];
3432 	struct bpf_insn insns[] = {
3433 		BPF_MOV64_IMM(BPF_REG_0, 0),
3434 		BPF_EXIT_INSN(),
3435 	};
3436 	int ret;
3437 
3438 	/* make sure basic loading works */
3439 
3440 	memset(&attr, 0, sizeof(attr));
3441 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3442 	attr.insns = insns;
3443 	attr.insns_cnt = ARRAY_SIZE(insns);
3444 	attr.license = "GPL";
3445 
3446 	ret = bpf_load_program_xattr(&attr, NULL, 0);
3447 	if (ret < 0) {
3448 		ret = errno;
3449 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
3450 		pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
3451 			"program. Make sure your kernel supports BPF "
3452 			"(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
3453 			"set to big enough value.\n", __func__, cp, ret);
3454 		return -ret;
3455 	}
3456 	close(ret);
3457 
3458 	return 0;
3459 }
3460 
3461 static int probe_fd(int fd)
3462 {
3463 	if (fd >= 0)
3464 		close(fd);
3465 	return fd >= 0;
3466 }
3467 
3468 static int probe_kern_prog_name(void)
3469 {
3470 	struct bpf_load_program_attr attr;
3471 	struct bpf_insn insns[] = {
3472 		BPF_MOV64_IMM(BPF_REG_0, 0),
3473 		BPF_EXIT_INSN(),
3474 	};
3475 	int ret;
3476 
3477 	/* make sure loading with name works */
3478 
3479 	memset(&attr, 0, sizeof(attr));
3480 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3481 	attr.insns = insns;
3482 	attr.insns_cnt = ARRAY_SIZE(insns);
3483 	attr.license = "GPL";
3484 	attr.name = "test";
3485 	ret = bpf_load_program_xattr(&attr, NULL, 0);
3486 	return probe_fd(ret);
3487 }
3488 
3489 static int probe_kern_global_data(void)
3490 {
3491 	struct bpf_load_program_attr prg_attr;
3492 	struct bpf_create_map_attr map_attr;
3493 	char *cp, errmsg[STRERR_BUFSIZE];
3494 	struct bpf_insn insns[] = {
3495 		BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
3496 		BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
3497 		BPF_MOV64_IMM(BPF_REG_0, 0),
3498 		BPF_EXIT_INSN(),
3499 	};
3500 	int ret, map;
3501 
3502 	memset(&map_attr, 0, sizeof(map_attr));
3503 	map_attr.map_type = BPF_MAP_TYPE_ARRAY;
3504 	map_attr.key_size = sizeof(int);
3505 	map_attr.value_size = 32;
3506 	map_attr.max_entries = 1;
3507 
3508 	map = bpf_create_map_xattr(&map_attr);
3509 	if (map < 0) {
3510 		ret = -errno;
3511 		cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
3512 		pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
3513 			__func__, cp, -ret);
3514 		return ret;
3515 	}
3516 
3517 	insns[0].imm = map;
3518 
3519 	memset(&prg_attr, 0, sizeof(prg_attr));
3520 	prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
3521 	prg_attr.insns = insns;
3522 	prg_attr.insns_cnt = ARRAY_SIZE(insns);
3523 	prg_attr.license = "GPL";
3524 
3525 	ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
3526 	close(map);
3527 	return probe_fd(ret);
3528 }
3529 
3530 static int probe_kern_btf(void)
3531 {
3532 	static const char strs[] = "\0int";
3533 	__u32 types[] = {
3534 		/* int */
3535 		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
3536 	};
3537 
3538 	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3539 					     strs, sizeof(strs)));
3540 }
3541 
3542 static int probe_kern_btf_func(void)
3543 {
3544 	static const char strs[] = "\0int\0x\0a";
3545 	/* void x(int a) {} */
3546 	__u32 types[] = {
3547 		/* int */
3548 		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3549 		/* FUNC_PROTO */                                /* [2] */
3550 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3551 		BTF_PARAM_ENC(7, 1),
3552 		/* FUNC x */                                    /* [3] */
3553 		BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
3554 	};
3555 
3556 	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3557 					     strs, sizeof(strs)));
3558 }
3559 
3560 static int probe_kern_btf_func_global(void)
3561 {
3562 	static const char strs[] = "\0int\0x\0a";
3563 	/* static void x(int a) {} */
3564 	__u32 types[] = {
3565 		/* int */
3566 		BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3567 		/* FUNC_PROTO */                                /* [2] */
3568 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
3569 		BTF_PARAM_ENC(7, 1),
3570 		/* FUNC x BTF_FUNC_GLOBAL */                    /* [3] */
3571 		BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
3572 	};
3573 
3574 	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3575 					     strs, sizeof(strs)));
3576 }
3577 
3578 static int probe_kern_btf_datasec(void)
3579 {
3580 	static const char strs[] = "\0x\0.data";
3581 	/* static int a; */
3582 	__u32 types[] = {
3583 		/* int */
3584 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3585 		/* VAR x */                                     /* [2] */
3586 		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3587 		BTF_VAR_STATIC,
3588 		/* DATASEC val */                               /* [3] */
3589 		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3590 		BTF_VAR_SECINFO_ENC(2, 0, 4),
3591 	};
3592 
3593 	return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
3594 					     strs, sizeof(strs)));
3595 }
3596 
3597 static int probe_kern_array_mmap(void)
3598 {
3599 	struct bpf_create_map_attr attr = {
3600 		.map_type = BPF_MAP_TYPE_ARRAY,
3601 		.map_flags = BPF_F_MMAPABLE,
3602 		.key_size = sizeof(int),
3603 		.value_size = sizeof(int),
3604 		.max_entries = 1,
3605 	};
3606 
3607 	return probe_fd(bpf_create_map_xattr(&attr));
3608 }
3609 
3610 static int probe_kern_exp_attach_type(void)
3611 {
3612 	struct bpf_load_program_attr attr;
3613 	struct bpf_insn insns[] = {
3614 		BPF_MOV64_IMM(BPF_REG_0, 0),
3615 		BPF_EXIT_INSN(),
3616 	};
3617 
3618 	memset(&attr, 0, sizeof(attr));
3619 	/* use any valid combination of program type and (optional)
3620 	 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
3621 	 * to see if kernel supports expected_attach_type field for
3622 	 * BPF_PROG_LOAD command
3623 	 */
3624 	attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
3625 	attr.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE;
3626 	attr.insns = insns;
3627 	attr.insns_cnt = ARRAY_SIZE(insns);
3628 	attr.license = "GPL";
3629 
3630 	return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
3631 }
3632 
3633 static int probe_kern_probe_read_kernel(void)
3634 {
3635 	struct bpf_load_program_attr attr;
3636 	struct bpf_insn insns[] = {
3637 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),	/* r1 = r10 (fp) */
3638 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),	/* r1 += -8 */
3639 		BPF_MOV64_IMM(BPF_REG_2, 8),		/* r2 = 8 */
3640 		BPF_MOV64_IMM(BPF_REG_3, 0),		/* r3 = 0 */
3641 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
3642 		BPF_EXIT_INSN(),
3643 	};
3644 
3645 	memset(&attr, 0, sizeof(attr));
3646 	attr.prog_type = BPF_PROG_TYPE_KPROBE;
3647 	attr.insns = insns;
3648 	attr.insns_cnt = ARRAY_SIZE(insns);
3649 	attr.license = "GPL";
3650 
3651 	return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
3652 }
3653 
3654 enum kern_feature_result {
3655 	FEAT_UNKNOWN = 0,
3656 	FEAT_SUPPORTED = 1,
3657 	FEAT_MISSING = 2,
3658 };
3659 
3660 typedef int (*feature_probe_fn)(void);
3661 
3662 static struct kern_feature_desc {
3663 	const char *desc;
3664 	feature_probe_fn probe;
3665 	enum kern_feature_result res;
3666 } feature_probes[__FEAT_CNT] = {
3667 	[FEAT_PROG_NAME] = {
3668 		"BPF program name", probe_kern_prog_name,
3669 	},
3670 	[FEAT_GLOBAL_DATA] = {
3671 		"global variables", probe_kern_global_data,
3672 	},
3673 	[FEAT_BTF] = {
3674 		"minimal BTF", probe_kern_btf,
3675 	},
3676 	[FEAT_BTF_FUNC] = {
3677 		"BTF functions", probe_kern_btf_func,
3678 	},
3679 	[FEAT_BTF_GLOBAL_FUNC] = {
3680 		"BTF global function", probe_kern_btf_func_global,
3681 	},
3682 	[FEAT_BTF_DATASEC] = {
3683 		"BTF data section and variable", probe_kern_btf_datasec,
3684 	},
3685 	[FEAT_ARRAY_MMAP] = {
3686 		"ARRAY map mmap()", probe_kern_array_mmap,
3687 	},
3688 	[FEAT_EXP_ATTACH_TYPE] = {
3689 		"BPF_PROG_LOAD expected_attach_type attribute",
3690 		probe_kern_exp_attach_type,
3691 	},
3692 	[FEAT_PROBE_READ_KERN] = {
3693 		"bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
3694 	}
3695 };
3696 
3697 static bool kernel_supports(enum kern_feature_id feat_id)
3698 {
3699 	struct kern_feature_desc *feat = &feature_probes[feat_id];
3700 	int ret;
3701 
3702 	if (READ_ONCE(feat->res) == FEAT_UNKNOWN) {
3703 		ret = feat->probe();
3704 		if (ret > 0) {
3705 			WRITE_ONCE(feat->res, FEAT_SUPPORTED);
3706 		} else if (ret == 0) {
3707 			WRITE_ONCE(feat->res, FEAT_MISSING);
3708 		} else {
3709 			pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
3710 			WRITE_ONCE(feat->res, FEAT_MISSING);
3711 		}
3712 	}
3713 
3714 	return READ_ONCE(feat->res) == FEAT_SUPPORTED;
3715 }
3716 
3717 static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
3718 {
3719 	struct bpf_map_info map_info = {};
3720 	char msg[STRERR_BUFSIZE];
3721 	__u32 map_info_len;
3722 
3723 	map_info_len = sizeof(map_info);
3724 
3725 	if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) {
3726 		pr_warn("failed to get map info for map FD %d: %s\n",
3727 			map_fd, libbpf_strerror_r(errno, msg, sizeof(msg)));
3728 		return false;
3729 	}
3730 
3731 	return (map_info.type == map->def.type &&
3732 		map_info.key_size == map->def.key_size &&
3733 		map_info.value_size == map->def.value_size &&
3734 		map_info.max_entries == map->def.max_entries &&
3735 		map_info.map_flags == map->def.map_flags);
3736 }
3737 
3738 static int
3739 bpf_object__reuse_map(struct bpf_map *map)
3740 {
3741 	char *cp, errmsg[STRERR_BUFSIZE];
3742 	int err, pin_fd;
3743 
3744 	pin_fd = bpf_obj_get(map->pin_path);
3745 	if (pin_fd < 0) {
3746 		err = -errno;
3747 		if (err == -ENOENT) {
3748 			pr_debug("found no pinned map to reuse at '%s'\n",
3749 				 map->pin_path);
3750 			return 0;
3751 		}
3752 
3753 		cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
3754 		pr_warn("couldn't retrieve pinned map '%s': %s\n",
3755 			map->pin_path, cp);
3756 		return err;
3757 	}
3758 
3759 	if (!map_is_reuse_compat(map, pin_fd)) {
3760 		pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
3761 			map->pin_path);
3762 		close(pin_fd);
3763 		return -EINVAL;
3764 	}
3765 
3766 	err = bpf_map__reuse_fd(map, pin_fd);
3767 	if (err) {
3768 		close(pin_fd);
3769 		return err;
3770 	}
3771 	map->pinned = true;
3772 	pr_debug("reused pinned map at '%s'\n", map->pin_path);
3773 
3774 	return 0;
3775 }
3776 
3777 static int
3778 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
3779 {
3780 	enum libbpf_map_type map_type = map->libbpf_type;
3781 	char *cp, errmsg[STRERR_BUFSIZE];
3782 	int err, zero = 0;
3783 
3784 	err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
3785 	if (err) {
3786 		err = -errno;
3787 		cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
3788 		pr_warn("Error setting initial map(%s) contents: %s\n",
3789 			map->name, cp);
3790 		return err;
3791 	}
3792 
3793 	/* Freeze .rodata and .kconfig map as read-only from syscall side. */
3794 	if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
3795 		err = bpf_map_freeze(map->fd);
3796 		if (err) {
3797 			err = -errno;
3798 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
3799 			pr_warn("Error freezing map(%s) as read-only: %s\n",
3800 				map->name, cp);
3801 			return err;
3802 		}
3803 	}
3804 	return 0;
3805 }
3806 
3807 static void bpf_map__destroy(struct bpf_map *map);
3808 
3809 static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
3810 {
3811 	struct bpf_create_map_attr create_attr;
3812 	struct bpf_map_def *def = &map->def;
3813 
3814 	memset(&create_attr, 0, sizeof(create_attr));
3815 
3816 	if (kernel_supports(FEAT_PROG_NAME))
3817 		create_attr.name = map->name;
3818 	create_attr.map_ifindex = map->map_ifindex;
3819 	create_attr.map_type = def->type;
3820 	create_attr.map_flags = def->map_flags;
3821 	create_attr.key_size = def->key_size;
3822 	create_attr.value_size = def->value_size;
3823 	create_attr.numa_node = map->numa_node;
3824 
3825 	if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !def->max_entries) {
3826 		int nr_cpus;
3827 
3828 		nr_cpus = libbpf_num_possible_cpus();
3829 		if (nr_cpus < 0) {
3830 			pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
3831 				map->name, nr_cpus);
3832 			return nr_cpus;
3833 		}
3834 		pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
3835 		create_attr.max_entries = nr_cpus;
3836 	} else {
3837 		create_attr.max_entries = def->max_entries;
3838 	}
3839 
3840 	if (bpf_map__is_struct_ops(map))
3841 		create_attr.btf_vmlinux_value_type_id =
3842 			map->btf_vmlinux_value_type_id;
3843 
3844 	create_attr.btf_fd = 0;
3845 	create_attr.btf_key_type_id = 0;
3846 	create_attr.btf_value_type_id = 0;
3847 	if (obj->btf && btf__fd(obj->btf) >= 0 && !bpf_map_find_btf_info(obj, map)) {
3848 		create_attr.btf_fd = btf__fd(obj->btf);
3849 		create_attr.btf_key_type_id = map->btf_key_type_id;
3850 		create_attr.btf_value_type_id = map->btf_value_type_id;
3851 	}
3852 
3853 	if (bpf_map_type__is_map_in_map(def->type)) {
3854 		if (map->inner_map) {
3855 			int err;
3856 
3857 			err = bpf_object__create_map(obj, map->inner_map);
3858 			if (err) {
3859 				pr_warn("map '%s': failed to create inner map: %d\n",
3860 					map->name, err);
3861 				return err;
3862 			}
3863 			map->inner_map_fd = bpf_map__fd(map->inner_map);
3864 		}
3865 		if (map->inner_map_fd >= 0)
3866 			create_attr.inner_map_fd = map->inner_map_fd;
3867 	}
3868 
3869 	map->fd = bpf_create_map_xattr(&create_attr);
3870 	if (map->fd < 0 && (create_attr.btf_key_type_id ||
3871 			    create_attr.btf_value_type_id)) {
3872 		char *cp, errmsg[STRERR_BUFSIZE];
3873 		int err = -errno;
3874 
3875 		cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
3876 		pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
3877 			map->name, cp, err);
3878 		create_attr.btf_fd = 0;
3879 		create_attr.btf_key_type_id = 0;
3880 		create_attr.btf_value_type_id = 0;
3881 		map->btf_key_type_id = 0;
3882 		map->btf_value_type_id = 0;
3883 		map->fd = bpf_create_map_xattr(&create_attr);
3884 	}
3885 
3886 	if (map->fd < 0)
3887 		return -errno;
3888 
3889 	if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
3890 		bpf_map__destroy(map->inner_map);
3891 		zfree(&map->inner_map);
3892 	}
3893 
3894 	return 0;
3895 }
3896 
3897 static int
3898 bpf_object__create_maps(struct bpf_object *obj)
3899 {
3900 	struct bpf_map *map;
3901 	char *cp, errmsg[STRERR_BUFSIZE];
3902 	unsigned int i, j;
3903 	int err;
3904 
3905 	for (i = 0; i < obj->nr_maps; i++) {
3906 		map = &obj->maps[i];
3907 
3908 		if (map->pin_path) {
3909 			err = bpf_object__reuse_map(map);
3910 			if (err) {
3911 				pr_warn("map '%s': error reusing pinned map\n",
3912 					map->name);
3913 				goto err_out;
3914 			}
3915 		}
3916 
3917 		if (map->fd >= 0) {
3918 			pr_debug("map '%s': skipping creation (preset fd=%d)\n",
3919 				 map->name, map->fd);
3920 			continue;
3921 		}
3922 
3923 		err = bpf_object__create_map(obj, map);
3924 		if (err)
3925 			goto err_out;
3926 
3927 		pr_debug("map '%s': created successfully, fd=%d\n", map->name,
3928 			 map->fd);
3929 
3930 		if (bpf_map__is_internal(map)) {
3931 			err = bpf_object__populate_internal_map(obj, map);
3932 			if (err < 0) {
3933 				zclose(map->fd);
3934 				goto err_out;
3935 			}
3936 		}
3937 
3938 		if (map->init_slots_sz) {
3939 			for (j = 0; j < map->init_slots_sz; j++) {
3940 				const struct bpf_map *targ_map;
3941 				int fd;
3942 
3943 				if (!map->init_slots[j])
3944 					continue;
3945 
3946 				targ_map = map->init_slots[j];
3947 				fd = bpf_map__fd(targ_map);
3948 				err = bpf_map_update_elem(map->fd, &j, &fd, 0);
3949 				if (err) {
3950 					err = -errno;
3951 					pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
3952 						map->name, j, targ_map->name,
3953 						fd, err);
3954 					goto err_out;
3955 				}
3956 				pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
3957 					 map->name, j, targ_map->name, fd);
3958 			}
3959 			zfree(&map->init_slots);
3960 			map->init_slots_sz = 0;
3961 		}
3962 
3963 		if (map->pin_path && !map->pinned) {
3964 			err = bpf_map__pin(map, NULL);
3965 			if (err) {
3966 				pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
3967 					map->name, map->pin_path, err);
3968 				zclose(map->fd);
3969 				goto err_out;
3970 			}
3971 		}
3972 	}
3973 
3974 	return 0;
3975 
3976 err_out:
3977 	cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
3978 	pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
3979 	pr_perm_msg(err);
3980 	for (j = 0; j < i; j++)
3981 		zclose(obj->maps[j].fd);
3982 	return err;
3983 }
3984 
3985 static int
3986 check_btf_ext_reloc_err(struct bpf_program *prog, int err,
3987 			void *btf_prog_info, const char *info_name)
3988 {
3989 	if (err != -ENOENT) {
3990 		pr_warn("Error in loading %s for sec %s.\n",
3991 			info_name, prog->section_name);
3992 		return err;
3993 	}
3994 
3995 	/* err == -ENOENT (i.e. prog->section_name not found in btf_ext) */
3996 
3997 	if (btf_prog_info) {
3998 		/*
3999 		 * Some info has already been found but has problem
4000 		 * in the last btf_ext reloc. Must have to error out.
4001 		 */
4002 		pr_warn("Error in relocating %s for sec %s.\n",
4003 			info_name, prog->section_name);
4004 		return err;
4005 	}
4006 
4007 	/* Have problem loading the very first info. Ignore the rest. */
4008 	pr_warn("Cannot find %s for main program sec %s. Ignore all %s.\n",
4009 		info_name, prog->section_name, info_name);
4010 	return 0;
4011 }
4012 
4013 static int
4014 bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
4015 			  const char *section_name,  __u32 insn_offset)
4016 {
4017 	int err;
4018 
4019 	if (!insn_offset || prog->func_info) {
4020 		/*
4021 		 * !insn_offset => main program
4022 		 *
4023 		 * For sub prog, the main program's func_info has to
4024 		 * be loaded first (i.e. prog->func_info != NULL)
4025 		 */
4026 		err = btf_ext__reloc_func_info(obj->btf, obj->btf_ext,
4027 					       section_name, insn_offset,
4028 					       &prog->func_info,
4029 					       &prog->func_info_cnt);
4030 		if (err)
4031 			return check_btf_ext_reloc_err(prog, err,
4032 						       prog->func_info,
4033 						       "bpf_func_info");
4034 
4035 		prog->func_info_rec_size = btf_ext__func_info_rec_size(obj->btf_ext);
4036 	}
4037 
4038 	if (!insn_offset || prog->line_info) {
4039 		err = btf_ext__reloc_line_info(obj->btf, obj->btf_ext,
4040 					       section_name, insn_offset,
4041 					       &prog->line_info,
4042 					       &prog->line_info_cnt);
4043 		if (err)
4044 			return check_btf_ext_reloc_err(prog, err,
4045 						       prog->line_info,
4046 						       "bpf_line_info");
4047 
4048 		prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
4049 	}
4050 
4051 	return 0;
4052 }
4053 
4054 #define BPF_CORE_SPEC_MAX_LEN 64
4055 
4056 /* represents BPF CO-RE field or array element accessor */
4057 struct bpf_core_accessor {
4058 	__u32 type_id;		/* struct/union type or array element type */
4059 	__u32 idx;		/* field index or array index */
4060 	const char *name;	/* field name or NULL for array accessor */
4061 };
4062 
4063 struct bpf_core_spec {
4064 	const struct btf *btf;
4065 	/* high-level spec: named fields and array indices only */
4066 	struct bpf_core_accessor spec[BPF_CORE_SPEC_MAX_LEN];
4067 	/* original unresolved (no skip_mods_or_typedefs) root type ID */
4068 	__u32 root_type_id;
4069 	/* CO-RE relocation kind */
4070 	enum bpf_core_relo_kind relo_kind;
4071 	/* high-level spec length */
4072 	int len;
4073 	/* raw, low-level spec: 1-to-1 with accessor spec string */
4074 	int raw_spec[BPF_CORE_SPEC_MAX_LEN];
4075 	/* raw spec length */
4076 	int raw_len;
4077 	/* field bit offset represented by spec */
4078 	__u32 bit_offset;
4079 };
4080 
4081 static bool str_is_empty(const char *s)
4082 {
4083 	return !s || !s[0];
4084 }
4085 
4086 static bool is_flex_arr(const struct btf *btf,
4087 			const struct bpf_core_accessor *acc,
4088 			const struct btf_array *arr)
4089 {
4090 	const struct btf_type *t;
4091 
4092 	/* not a flexible array, if not inside a struct or has non-zero size */
4093 	if (!acc->name || arr->nelems > 0)
4094 		return false;
4095 
4096 	/* has to be the last member of enclosing struct */
4097 	t = btf__type_by_id(btf, acc->type_id);
4098 	return acc->idx == btf_vlen(t) - 1;
4099 }
4100 
4101 static const char *core_relo_kind_str(enum bpf_core_relo_kind kind)
4102 {
4103 	switch (kind) {
4104 	case BPF_FIELD_BYTE_OFFSET: return "byte_off";
4105 	case BPF_FIELD_BYTE_SIZE: return "byte_sz";
4106 	case BPF_FIELD_EXISTS: return "field_exists";
4107 	case BPF_FIELD_SIGNED: return "signed";
4108 	case BPF_FIELD_LSHIFT_U64: return "lshift_u64";
4109 	case BPF_FIELD_RSHIFT_U64: return "rshift_u64";
4110 	case BPF_TYPE_ID_LOCAL: return "local_type_id";
4111 	case BPF_TYPE_ID_TARGET: return "target_type_id";
4112 	case BPF_TYPE_EXISTS: return "type_exists";
4113 	case BPF_TYPE_SIZE: return "type_size";
4114 	case BPF_ENUMVAL_EXISTS: return "enumval_exists";
4115 	case BPF_ENUMVAL_VALUE: return "enumval_value";
4116 	default: return "unknown";
4117 	}
4118 }
4119 
4120 static bool core_relo_is_field_based(enum bpf_core_relo_kind kind)
4121 {
4122 	switch (kind) {
4123 	case BPF_FIELD_BYTE_OFFSET:
4124 	case BPF_FIELD_BYTE_SIZE:
4125 	case BPF_FIELD_EXISTS:
4126 	case BPF_FIELD_SIGNED:
4127 	case BPF_FIELD_LSHIFT_U64:
4128 	case BPF_FIELD_RSHIFT_U64:
4129 		return true;
4130 	default:
4131 		return false;
4132 	}
4133 }
4134 
4135 static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
4136 {
4137 	switch (kind) {
4138 	case BPF_TYPE_ID_LOCAL:
4139 	case BPF_TYPE_ID_TARGET:
4140 	case BPF_TYPE_EXISTS:
4141 	case BPF_TYPE_SIZE:
4142 		return true;
4143 	default:
4144 		return false;
4145 	}
4146 }
4147 
4148 static bool core_relo_is_enumval_based(enum bpf_core_relo_kind kind)
4149 {
4150 	switch (kind) {
4151 	case BPF_ENUMVAL_EXISTS:
4152 	case BPF_ENUMVAL_VALUE:
4153 		return true;
4154 	default:
4155 		return false;
4156 	}
4157 }
4158 
4159 /*
4160  * Turn bpf_core_relo into a low- and high-level spec representation,
4161  * validating correctness along the way, as well as calculating resulting
4162  * field bit offset, specified by accessor string. Low-level spec captures
4163  * every single level of nestedness, including traversing anonymous
4164  * struct/union members. High-level one only captures semantically meaningful
4165  * "turning points": named fields and array indicies.
4166  * E.g., for this case:
4167  *
4168  *   struct sample {
4169  *       int __unimportant;
4170  *       struct {
4171  *           int __1;
4172  *           int __2;
4173  *           int a[7];
4174  *       };
4175  *   };
4176  *
4177  *   struct sample *s = ...;
4178  *
4179  *   int x = &s->a[3]; // access string = '0:1:2:3'
4180  *
4181  * Low-level spec has 1:1 mapping with each element of access string (it's
4182  * just a parsed access string representation): [0, 1, 2, 3].
4183  *
4184  * High-level spec will capture only 3 points:
4185  *   - intial zero-index access by pointer (&s->... is the same as &s[0]...);
4186  *   - field 'a' access (corresponds to '2' in low-level spec);
4187  *   - array element #3 access (corresponds to '3' in low-level spec).
4188  *
4189  * Type-based relocations (TYPE_EXISTS/TYPE_SIZE,
4190  * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
4191  * spec and raw_spec are kept empty.
4192  *
4193  * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
4194  * string to specify enumerator's value index that need to be relocated.
4195  */
4196 static int bpf_core_parse_spec(const struct btf *btf,
4197 			       __u32 type_id,
4198 			       const char *spec_str,
4199 			       enum bpf_core_relo_kind relo_kind,
4200 			       struct bpf_core_spec *spec)
4201 {
4202 	int access_idx, parsed_len, i;
4203 	struct bpf_core_accessor *acc;
4204 	const struct btf_type *t;
4205 	const char *name;
4206 	__u32 id;
4207 	__s64 sz;
4208 
4209 	if (str_is_empty(spec_str) || *spec_str == ':')
4210 		return -EINVAL;
4211 
4212 	memset(spec, 0, sizeof(*spec));
4213 	spec->btf = btf;
4214 	spec->root_type_id = type_id;
4215 	spec->relo_kind = relo_kind;
4216 
4217 	/* type-based relocations don't have a field access string */
4218 	if (core_relo_is_type_based(relo_kind)) {
4219 		if (strcmp(spec_str, "0"))
4220 			return -EINVAL;
4221 		return 0;
4222 	}
4223 
4224 	/* parse spec_str="0:1:2:3:4" into array raw_spec=[0, 1, 2, 3, 4] */
4225 	while (*spec_str) {
4226 		if (*spec_str == ':')
4227 			++spec_str;
4228 		if (sscanf(spec_str, "%d%n", &access_idx, &parsed_len) != 1)
4229 			return -EINVAL;
4230 		if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4231 			return -E2BIG;
4232 		spec_str += parsed_len;
4233 		spec->raw_spec[spec->raw_len++] = access_idx;
4234 	}
4235 
4236 	if (spec->raw_len == 0)
4237 		return -EINVAL;
4238 
4239 	t = skip_mods_and_typedefs(btf, type_id, &id);
4240 	if (!t)
4241 		return -EINVAL;
4242 
4243 	access_idx = spec->raw_spec[0];
4244 	acc = &spec->spec[0];
4245 	acc->type_id = id;
4246 	acc->idx = access_idx;
4247 	spec->len++;
4248 
4249 	if (core_relo_is_enumval_based(relo_kind)) {
4250 		if (!btf_is_enum(t) || spec->raw_len > 1 || access_idx >= btf_vlen(t))
4251 			return -EINVAL;
4252 
4253 		/* record enumerator name in a first accessor */
4254 		acc->name = btf__name_by_offset(btf, btf_enum(t)[access_idx].name_off);
4255 		return 0;
4256 	}
4257 
4258 	if (!core_relo_is_field_based(relo_kind))
4259 		return -EINVAL;
4260 
4261 	sz = btf__resolve_size(btf, id);
4262 	if (sz < 0)
4263 		return sz;
4264 	spec->bit_offset = access_idx * sz * 8;
4265 
4266 	for (i = 1; i < spec->raw_len; i++) {
4267 		t = skip_mods_and_typedefs(btf, id, &id);
4268 		if (!t)
4269 			return -EINVAL;
4270 
4271 		access_idx = spec->raw_spec[i];
4272 		acc = &spec->spec[spec->len];
4273 
4274 		if (btf_is_composite(t)) {
4275 			const struct btf_member *m;
4276 			__u32 bit_offset;
4277 
4278 			if (access_idx >= btf_vlen(t))
4279 				return -EINVAL;
4280 
4281 			bit_offset = btf_member_bit_offset(t, access_idx);
4282 			spec->bit_offset += bit_offset;
4283 
4284 			m = btf_members(t) + access_idx;
4285 			if (m->name_off) {
4286 				name = btf__name_by_offset(btf, m->name_off);
4287 				if (str_is_empty(name))
4288 					return -EINVAL;
4289 
4290 				acc->type_id = id;
4291 				acc->idx = access_idx;
4292 				acc->name = name;
4293 				spec->len++;
4294 			}
4295 
4296 			id = m->type;
4297 		} else if (btf_is_array(t)) {
4298 			const struct btf_array *a = btf_array(t);
4299 			bool flex;
4300 
4301 			t = skip_mods_and_typedefs(btf, a->type, &id);
4302 			if (!t)
4303 				return -EINVAL;
4304 
4305 			flex = is_flex_arr(btf, acc - 1, a);
4306 			if (!flex && access_idx >= a->nelems)
4307 				return -EINVAL;
4308 
4309 			spec->spec[spec->len].type_id = id;
4310 			spec->spec[spec->len].idx = access_idx;
4311 			spec->len++;
4312 
4313 			sz = btf__resolve_size(btf, id);
4314 			if (sz < 0)
4315 				return sz;
4316 			spec->bit_offset += access_idx * sz * 8;
4317 		} else {
4318 			pr_warn("relo for [%u] %s (at idx %d) captures type [%d] of unexpected kind %s\n",
4319 				type_id, spec_str, i, id, btf_kind_str(t));
4320 			return -EINVAL;
4321 		}
4322 	}
4323 
4324 	return 0;
4325 }
4326 
4327 static bool bpf_core_is_flavor_sep(const char *s)
4328 {
4329 	/* check X___Y name pattern, where X and Y are not underscores */
4330 	return s[0] != '_' &&				      /* X */
4331 	       s[1] == '_' && s[2] == '_' && s[3] == '_' &&   /* ___ */
4332 	       s[4] != '_';				      /* Y */
4333 }
4334 
4335 /* Given 'some_struct_name___with_flavor' return the length of a name prefix
4336  * before last triple underscore. Struct name part after last triple
4337  * underscore is ignored by BPF CO-RE relocation during relocation matching.
4338  */
4339 static size_t bpf_core_essential_name_len(const char *name)
4340 {
4341 	size_t n = strlen(name);
4342 	int i;
4343 
4344 	for (i = n - 5; i >= 0; i--) {
4345 		if (bpf_core_is_flavor_sep(name + i))
4346 			return i + 1;
4347 	}
4348 	return n;
4349 }
4350 
4351 /* dynamically sized list of type IDs */
4352 struct ids_vec {
4353 	__u32 *data;
4354 	int len;
4355 };
4356 
4357 static void bpf_core_free_cands(struct ids_vec *cand_ids)
4358 {
4359 	free(cand_ids->data);
4360 	free(cand_ids);
4361 }
4362 
4363 static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,
4364 					   __u32 local_type_id,
4365 					   const struct btf *targ_btf)
4366 {
4367 	size_t local_essent_len, targ_essent_len;
4368 	const char *local_name, *targ_name;
4369 	const struct btf_type *t, *local_t;
4370 	struct ids_vec *cand_ids;
4371 	__u32 *new_ids;
4372 	int i, err, n;
4373 
4374 	local_t = btf__type_by_id(local_btf, local_type_id);
4375 	if (!local_t)
4376 		return ERR_PTR(-EINVAL);
4377 
4378 	local_name = btf__name_by_offset(local_btf, local_t->name_off);
4379 	if (str_is_empty(local_name))
4380 		return ERR_PTR(-EINVAL);
4381 	local_essent_len = bpf_core_essential_name_len(local_name);
4382 
4383 	cand_ids = calloc(1, sizeof(*cand_ids));
4384 	if (!cand_ids)
4385 		return ERR_PTR(-ENOMEM);
4386 
4387 	n = btf__get_nr_types(targ_btf);
4388 	for (i = 1; i <= n; i++) {
4389 		t = btf__type_by_id(targ_btf, i);
4390 		if (btf_kind(t) != btf_kind(local_t))
4391 			continue;
4392 
4393 		targ_name = btf__name_by_offset(targ_btf, t->name_off);
4394 		if (str_is_empty(targ_name))
4395 			continue;
4396 
4397 		targ_essent_len = bpf_core_essential_name_len(targ_name);
4398 		if (targ_essent_len != local_essent_len)
4399 			continue;
4400 
4401 		if (strncmp(local_name, targ_name, local_essent_len) == 0) {
4402 			pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s\n",
4403 				 local_type_id, btf_kind_str(local_t),
4404 				 local_name, i, btf_kind_str(t), targ_name);
4405 			new_ids = libbpf_reallocarray(cand_ids->data,
4406 						      cand_ids->len + 1,
4407 						      sizeof(*cand_ids->data));
4408 			if (!new_ids) {
4409 				err = -ENOMEM;
4410 				goto err_out;
4411 			}
4412 			cand_ids->data = new_ids;
4413 			cand_ids->data[cand_ids->len++] = i;
4414 		}
4415 	}
4416 	return cand_ids;
4417 err_out:
4418 	bpf_core_free_cands(cand_ids);
4419 	return ERR_PTR(err);
4420 }
4421 
4422 /* Check two types for compatibility for the purpose of field access
4423  * relocation. const/volatile/restrict and typedefs are skipped to ensure we
4424  * are relocating semantically compatible entities:
4425  *   - any two STRUCTs/UNIONs are compatible and can be mixed;
4426  *   - any two FWDs are compatible, if their names match (modulo flavor suffix);
4427  *   - any two PTRs are always compatible;
4428  *   - for ENUMs, names should be the same (ignoring flavor suffix) or at
4429  *     least one of enums should be anonymous;
4430  *   - for ENUMs, check sizes, names are ignored;
4431  *   - for INT, size and signedness are ignored;
4432  *   - for ARRAY, dimensionality is ignored, element types are checked for
4433  *     compatibility recursively;
4434  *   - everything else shouldn't be ever a target of relocation.
4435  * These rules are not set in stone and probably will be adjusted as we get
4436  * more experience with using BPF CO-RE relocations.
4437  */
4438 static int bpf_core_fields_are_compat(const struct btf *local_btf,
4439 				      __u32 local_id,
4440 				      const struct btf *targ_btf,
4441 				      __u32 targ_id)
4442 {
4443 	const struct btf_type *local_type, *targ_type;
4444 
4445 recur:
4446 	local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
4447 	targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
4448 	if (!local_type || !targ_type)
4449 		return -EINVAL;
4450 
4451 	if (btf_is_composite(local_type) && btf_is_composite(targ_type))
4452 		return 1;
4453 	if (btf_kind(local_type) != btf_kind(targ_type))
4454 		return 0;
4455 
4456 	switch (btf_kind(local_type)) {
4457 	case BTF_KIND_PTR:
4458 		return 1;
4459 	case BTF_KIND_FWD:
4460 	case BTF_KIND_ENUM: {
4461 		const char *local_name, *targ_name;
4462 		size_t local_len, targ_len;
4463 
4464 		local_name = btf__name_by_offset(local_btf,
4465 						 local_type->name_off);
4466 		targ_name = btf__name_by_offset(targ_btf, targ_type->name_off);
4467 		local_len = bpf_core_essential_name_len(local_name);
4468 		targ_len = bpf_core_essential_name_len(targ_name);
4469 		/* one of them is anonymous or both w/ same flavor-less names */
4470 		return local_len == 0 || targ_len == 0 ||
4471 		       (local_len == targ_len &&
4472 			strncmp(local_name, targ_name, local_len) == 0);
4473 	}
4474 	case BTF_KIND_INT:
4475 		/* just reject deprecated bitfield-like integers; all other
4476 		 * integers are by default compatible between each other
4477 		 */
4478 		return btf_int_offset(local_type) == 0 &&
4479 		       btf_int_offset(targ_type) == 0;
4480 	case BTF_KIND_ARRAY:
4481 		local_id = btf_array(local_type)->type;
4482 		targ_id = btf_array(targ_type)->type;
4483 		goto recur;
4484 	default:
4485 		pr_warn("unexpected kind %d relocated, local [%d], target [%d]\n",
4486 			btf_kind(local_type), local_id, targ_id);
4487 		return 0;
4488 	}
4489 }
4490 
4491 /*
4492  * Given single high-level named field accessor in local type, find
4493  * corresponding high-level accessor for a target type. Along the way,
4494  * maintain low-level spec for target as well. Also keep updating target
4495  * bit offset.
4496  *
4497  * Searching is performed through recursive exhaustive enumeration of all
4498  * fields of a struct/union. If there are any anonymous (embedded)
4499  * structs/unions, they are recursively searched as well. If field with
4500  * desired name is found, check compatibility between local and target types,
4501  * before returning result.
4502  *
4503  * 1 is returned, if field is found.
4504  * 0 is returned if no compatible field is found.
4505  * <0 is returned on error.
4506  */
4507 static int bpf_core_match_member(const struct btf *local_btf,
4508 				 const struct bpf_core_accessor *local_acc,
4509 				 const struct btf *targ_btf,
4510 				 __u32 targ_id,
4511 				 struct bpf_core_spec *spec,
4512 				 __u32 *next_targ_id)
4513 {
4514 	const struct btf_type *local_type, *targ_type;
4515 	const struct btf_member *local_member, *m;
4516 	const char *local_name, *targ_name;
4517 	__u32 local_id;
4518 	int i, n, found;
4519 
4520 	targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
4521 	if (!targ_type)
4522 		return -EINVAL;
4523 	if (!btf_is_composite(targ_type))
4524 		return 0;
4525 
4526 	local_id = local_acc->type_id;
4527 	local_type = btf__type_by_id(local_btf, local_id);
4528 	local_member = btf_members(local_type) + local_acc->idx;
4529 	local_name = btf__name_by_offset(local_btf, local_member->name_off);
4530 
4531 	n = btf_vlen(targ_type);
4532 	m = btf_members(targ_type);
4533 	for (i = 0; i < n; i++, m++) {
4534 		__u32 bit_offset;
4535 
4536 		bit_offset = btf_member_bit_offset(targ_type, i);
4537 
4538 		/* too deep struct/union/array nesting */
4539 		if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4540 			return -E2BIG;
4541 
4542 		/* speculate this member will be the good one */
4543 		spec->bit_offset += bit_offset;
4544 		spec->raw_spec[spec->raw_len++] = i;
4545 
4546 		targ_name = btf__name_by_offset(targ_btf, m->name_off);
4547 		if (str_is_empty(targ_name)) {
4548 			/* embedded struct/union, we need to go deeper */
4549 			found = bpf_core_match_member(local_btf, local_acc,
4550 						      targ_btf, m->type,
4551 						      spec, next_targ_id);
4552 			if (found) /* either found or error */
4553 				return found;
4554 		} else if (strcmp(local_name, targ_name) == 0) {
4555 			/* matching named field */
4556 			struct bpf_core_accessor *targ_acc;
4557 
4558 			targ_acc = &spec->spec[spec->len++];
4559 			targ_acc->type_id = targ_id;
4560 			targ_acc->idx = i;
4561 			targ_acc->name = targ_name;
4562 
4563 			*next_targ_id = m->type;
4564 			found = bpf_core_fields_are_compat(local_btf,
4565 							   local_member->type,
4566 							   targ_btf, m->type);
4567 			if (!found)
4568 				spec->len--; /* pop accessor */
4569 			return found;
4570 		}
4571 		/* member turned out not to be what we looked for */
4572 		spec->bit_offset -= bit_offset;
4573 		spec->raw_len--;
4574 	}
4575 
4576 	return 0;
4577 }
4578 
4579 /* Check local and target types for compatibility. This check is used for
4580  * type-based CO-RE relocations and follow slightly different rules than
4581  * field-based relocations. This function assumes that root types were already
4582  * checked for name match. Beyond that initial root-level name check, names
4583  * are completely ignored. Compatibility rules are as follows:
4584  *   - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
4585  *     kind should match for local and target types (i.e., STRUCT is not
4586  *     compatible with UNION);
4587  *   - for ENUMs, the size is ignored;
4588  *   - for INT, size and signedness are ignored;
4589  *   - for ARRAY, dimensionality is ignored, element types are checked for
4590  *     compatibility recursively;
4591  *   - CONST/VOLATILE/RESTRICT modifiers are ignored;
4592  *   - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
4593  *   - FUNC_PROTOs are compatible if they have compatible signature: same
4594  *     number of input args and compatible return and argument types.
4595  * These rules are not set in stone and probably will be adjusted as we get
4596  * more experience with using BPF CO-RE relocations.
4597  */
4598 static int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
4599 				     const struct btf *targ_btf, __u32 targ_id)
4600 {
4601 	const struct btf_type *local_type, *targ_type;
4602 	int depth = 32; /* max recursion depth */
4603 
4604 	/* caller made sure that names match (ignoring flavor suffix) */
4605 	local_type = btf__type_by_id(local_btf, local_id);
4606 	targ_type = btf__type_by_id(local_btf, local_id);
4607 	if (btf_kind(local_type) != btf_kind(targ_type))
4608 		return 0;
4609 
4610 recur:
4611 	depth--;
4612 	if (depth < 0)
4613 		return -EINVAL;
4614 
4615 	local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
4616 	targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
4617 	if (!local_type || !targ_type)
4618 		return -EINVAL;
4619 
4620 	if (btf_kind(local_type) != btf_kind(targ_type))
4621 		return 0;
4622 
4623 	switch (btf_kind(local_type)) {
4624 	case BTF_KIND_UNKN:
4625 	case BTF_KIND_STRUCT:
4626 	case BTF_KIND_UNION:
4627 	case BTF_KIND_ENUM:
4628 	case BTF_KIND_FWD:
4629 		return 1;
4630 	case BTF_KIND_INT:
4631 		/* just reject deprecated bitfield-like integers; all other
4632 		 * integers are by default compatible between each other
4633 		 */
4634 		return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
4635 	case BTF_KIND_PTR:
4636 		local_id = local_type->type;
4637 		targ_id = targ_type->type;
4638 		goto recur;
4639 	case BTF_KIND_ARRAY:
4640 		local_id = btf_array(local_type)->type;
4641 		targ_id = btf_array(targ_type)->type;
4642 		goto recur;
4643 	case BTF_KIND_FUNC_PROTO: {
4644 		struct btf_param *local_p = btf_params(local_type);
4645 		struct btf_param *targ_p = btf_params(targ_type);
4646 		__u16 local_vlen = btf_vlen(local_type);
4647 		__u16 targ_vlen = btf_vlen(targ_type);
4648 		int i, err;
4649 
4650 		if (local_vlen != targ_vlen)
4651 			return 0;
4652 
4653 		for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
4654 			skip_mods_and_typedefs(local_btf, local_p->type, &local_id);
4655 			skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id);
4656 			err = bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id);
4657 			if (err <= 0)
4658 				return err;
4659 		}
4660 
4661 		/* tail recurse for return type check */
4662 		skip_mods_and_typedefs(local_btf, local_type->type, &local_id);
4663 		skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
4664 		goto recur;
4665 	}
4666 	default:
4667 		pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
4668 			btf_kind_str(local_type), local_id, targ_id);
4669 		return 0;
4670 	}
4671 }
4672 
4673 /*
4674  * Try to match local spec to a target type and, if successful, produce full
4675  * target spec (high-level, low-level + bit offset).
4676  */
4677 static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
4678 			       const struct btf *targ_btf, __u32 targ_id,
4679 			       struct bpf_core_spec *targ_spec)
4680 {
4681 	const struct btf_type *targ_type;
4682 	const struct bpf_core_accessor *local_acc;
4683 	struct bpf_core_accessor *targ_acc;
4684 	int i, sz, matched;
4685 
4686 	memset(targ_spec, 0, sizeof(*targ_spec));
4687 	targ_spec->btf = targ_btf;
4688 	targ_spec->root_type_id = targ_id;
4689 	targ_spec->relo_kind = local_spec->relo_kind;
4690 
4691 	if (core_relo_is_type_based(local_spec->relo_kind)) {
4692 		return bpf_core_types_are_compat(local_spec->btf,
4693 						 local_spec->root_type_id,
4694 						 targ_btf, targ_id);
4695 	}
4696 
4697 	local_acc = &local_spec->spec[0];
4698 	targ_acc = &targ_spec->spec[0];
4699 
4700 	if (core_relo_is_enumval_based(local_spec->relo_kind)) {
4701 		size_t local_essent_len, targ_essent_len;
4702 		const struct btf_enum *e;
4703 		const char *targ_name;
4704 
4705 		/* has to resolve to an enum */
4706 		targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id, &targ_id);
4707 		if (!btf_is_enum(targ_type))
4708 			return 0;
4709 
4710 		local_essent_len = bpf_core_essential_name_len(local_acc->name);
4711 
4712 		for (i = 0, e = btf_enum(targ_type); i < btf_vlen(targ_type); i++, e++) {
4713 			targ_name = btf__name_by_offset(targ_spec->btf, e->name_off);
4714 			targ_essent_len = bpf_core_essential_name_len(targ_name);
4715 			if (targ_essent_len != local_essent_len)
4716 				continue;
4717 			if (strncmp(local_acc->name, targ_name, local_essent_len) == 0) {
4718 				targ_acc->type_id = targ_id;
4719 				targ_acc->idx = i;
4720 				targ_acc->name = targ_name;
4721 				targ_spec->len++;
4722 				targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
4723 				targ_spec->raw_len++;
4724 				return 1;
4725 			}
4726 		}
4727 		return 0;
4728 	}
4729 
4730 	if (!core_relo_is_field_based(local_spec->relo_kind))
4731 		return -EINVAL;
4732 
4733 	for (i = 0; i < local_spec->len; i++, local_acc++, targ_acc++) {
4734 		targ_type = skip_mods_and_typedefs(targ_spec->btf, targ_id,
4735 						   &targ_id);
4736 		if (!targ_type)
4737 			return -EINVAL;
4738 
4739 		if (local_acc->name) {
4740 			matched = bpf_core_match_member(local_spec->btf,
4741 							local_acc,
4742 							targ_btf, targ_id,
4743 							targ_spec, &targ_id);
4744 			if (matched <= 0)
4745 				return matched;
4746 		} else {
4747 			/* for i=0, targ_id is already treated as array element
4748 			 * type (because it's the original struct), for others
4749 			 * we should find array element type first
4750 			 */
4751 			if (i > 0) {
4752 				const struct btf_array *a;
4753 				bool flex;
4754 
4755 				if (!btf_is_array(targ_type))
4756 					return 0;
4757 
4758 				a = btf_array(targ_type);
4759 				flex = is_flex_arr(targ_btf, targ_acc - 1, a);
4760 				if (!flex && local_acc->idx >= a->nelems)
4761 					return 0;
4762 				if (!skip_mods_and_typedefs(targ_btf, a->type,
4763 							    &targ_id))
4764 					return -EINVAL;
4765 			}
4766 
4767 			/* too deep struct/union/array nesting */
4768 			if (targ_spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
4769 				return -E2BIG;
4770 
4771 			targ_acc->type_id = targ_id;
4772 			targ_acc->idx = local_acc->idx;
4773 			targ_acc->name = NULL;
4774 			targ_spec->len++;
4775 			targ_spec->raw_spec[targ_spec->raw_len] = targ_acc->idx;
4776 			targ_spec->raw_len++;
4777 
4778 			sz = btf__resolve_size(targ_btf, targ_id);
4779 			if (sz < 0)
4780 				return sz;
4781 			targ_spec->bit_offset += local_acc->idx * sz * 8;
4782 		}
4783 	}
4784 
4785 	return 1;
4786 }
4787 
4788 static int bpf_core_calc_field_relo(const struct bpf_program *prog,
4789 				    const struct bpf_core_relo *relo,
4790 				    const struct bpf_core_spec *spec,
4791 				    __u32 *val, bool *validate)
4792 {
4793 	const struct bpf_core_accessor *acc;
4794 	const struct btf_type *t;
4795 	__u32 byte_off, byte_sz, bit_off, bit_sz;
4796 	const struct btf_member *m;
4797 	const struct btf_type *mt;
4798 	bool bitfield;
4799 	__s64 sz;
4800 
4801 	if (relo->kind == BPF_FIELD_EXISTS) {
4802 		*val = spec ? 1 : 0;
4803 		return 0;
4804 	}
4805 
4806 	if (!spec)
4807 		return -EUCLEAN; /* request instruction poisoning */
4808 
4809 	acc = &spec->spec[spec->len - 1];
4810 	t = btf__type_by_id(spec->btf, acc->type_id);
4811 
4812 	/* a[n] accessor needs special handling */
4813 	if (!acc->name) {
4814 		if (relo->kind == BPF_FIELD_BYTE_OFFSET) {
4815 			*val = spec->bit_offset / 8;
4816 		} else if (relo->kind == BPF_FIELD_BYTE_SIZE) {
4817 			sz = btf__resolve_size(spec->btf, acc->type_id);
4818 			if (sz < 0)
4819 				return -EINVAL;
4820 			*val = sz;
4821 		} else {
4822 			pr_warn("prog '%s': relo %d at insn #%d can't be applied to array access\n",
4823 				bpf_program__title(prog, false),
4824 				relo->kind, relo->insn_off / 8);
4825 			return -EINVAL;
4826 		}
4827 		if (validate)
4828 			*validate = true;
4829 		return 0;
4830 	}
4831 
4832 	m = btf_members(t) + acc->idx;
4833 	mt = skip_mods_and_typedefs(spec->btf, m->type, NULL);
4834 	bit_off = spec->bit_offset;
4835 	bit_sz = btf_member_bitfield_size(t, acc->idx);
4836 
4837 	bitfield = bit_sz > 0;
4838 	if (bitfield) {
4839 		byte_sz = mt->size;
4840 		byte_off = bit_off / 8 / byte_sz * byte_sz;
4841 		/* figure out smallest int size necessary for bitfield load */
4842 		while (bit_off + bit_sz - byte_off * 8 > byte_sz * 8) {
4843 			if (byte_sz >= 8) {
4844 				/* bitfield can't be read with 64-bit read */
4845 				pr_warn("prog '%s': relo %d at insn #%d can't be satisfied for bitfield\n",
4846 					bpf_program__title(prog, false),
4847 					relo->kind, relo->insn_off / 8);
4848 				return -E2BIG;
4849 			}
4850 			byte_sz *= 2;
4851 			byte_off = bit_off / 8 / byte_sz * byte_sz;
4852 		}
4853 	} else {
4854 		sz = btf__resolve_size(spec->btf, m->type);
4855 		if (sz < 0)
4856 			return -EINVAL;
4857 		byte_sz = sz;
4858 		byte_off = spec->bit_offset / 8;
4859 		bit_sz = byte_sz * 8;
4860 	}
4861 
4862 	/* for bitfields, all the relocatable aspects are ambiguous and we
4863 	 * might disagree with compiler, so turn off validation of expected
4864 	 * value, except for signedness
4865 	 */
4866 	if (validate)
4867 		*validate = !bitfield;
4868 
4869 	switch (relo->kind) {
4870 	case BPF_FIELD_BYTE_OFFSET:
4871 		*val = byte_off;
4872 		break;
4873 	case BPF_FIELD_BYTE_SIZE:
4874 		*val = byte_sz;
4875 		break;
4876 	case BPF_FIELD_SIGNED:
4877 		/* enums will be assumed unsigned */
4878 		*val = btf_is_enum(mt) ||
4879 		       (btf_int_encoding(mt) & BTF_INT_SIGNED);
4880 		if (validate)
4881 			*validate = true; /* signedness is never ambiguous */
4882 		break;
4883 	case BPF_FIELD_LSHIFT_U64:
4884 #if __BYTE_ORDER == __LITTLE_ENDIAN
4885 		*val = 64 - (bit_off + bit_sz - byte_off  * 8);
4886 #else
4887 		*val = (8 - byte_sz) * 8 + (bit_off - byte_off * 8);
4888 #endif
4889 		break;
4890 	case BPF_FIELD_RSHIFT_U64:
4891 		*val = 64 - bit_sz;
4892 		if (validate)
4893 			*validate = true; /* right shift is never ambiguous */
4894 		break;
4895 	case BPF_FIELD_EXISTS:
4896 	default:
4897 		return -EOPNOTSUPP;
4898 	}
4899 
4900 	return 0;
4901 }
4902 
4903 static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
4904 				   const struct bpf_core_spec *spec,
4905 				   __u32 *val)
4906 {
4907 	__s64 sz;
4908 
4909 	/* type-based relos return zero when target type is not found */
4910 	if (!spec) {
4911 		*val = 0;
4912 		return 0;
4913 	}
4914 
4915 	switch (relo->kind) {
4916 	case BPF_TYPE_ID_TARGET:
4917 		*val = spec->root_type_id;
4918 		break;
4919 	case BPF_TYPE_EXISTS:
4920 		*val = 1;
4921 		break;
4922 	case BPF_TYPE_SIZE:
4923 		sz = btf__resolve_size(spec->btf, spec->root_type_id);
4924 		if (sz < 0)
4925 			return -EINVAL;
4926 		*val = sz;
4927 		break;
4928 	case BPF_TYPE_ID_LOCAL:
4929 	/* BPF_TYPE_ID_LOCAL is handled specially and shouldn't get here */
4930 	default:
4931 		return -EOPNOTSUPP;
4932 	}
4933 
4934 	return 0;
4935 }
4936 
4937 static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
4938 				      const struct bpf_core_spec *spec,
4939 				      __u32 *val)
4940 {
4941 	const struct btf_type *t;
4942 	const struct btf_enum *e;
4943 
4944 	switch (relo->kind) {
4945 	case BPF_ENUMVAL_EXISTS:
4946 		*val = spec ? 1 : 0;
4947 		break;
4948 	case BPF_ENUMVAL_VALUE:
4949 		if (!spec)
4950 			return -EUCLEAN; /* request instruction poisoning */
4951 		t = btf__type_by_id(spec->btf, spec->spec[0].type_id);
4952 		e = btf_enum(t) + spec->spec[0].idx;
4953 		*val = e->val;
4954 		break;
4955 	default:
4956 		return -EOPNOTSUPP;
4957 	}
4958 
4959 	return 0;
4960 }
4961 
4962 struct bpf_core_relo_res
4963 {
4964 	/* expected value in the instruction, unless validate == false */
4965 	__u32 orig_val;
4966 	/* new value that needs to be patched up to */
4967 	__u32 new_val;
4968 	/* relocation unsuccessful, poison instruction, but don't fail load */
4969 	bool poison;
4970 	/* some relocations can't be validated against orig_val */
4971 	bool validate;
4972 };
4973 
4974 /* Calculate original and target relocation values, given local and target
4975  * specs and relocation kind. These values are calculated for each candidate.
4976  * If there are multiple candidates, resulting values should all be consistent
4977  * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
4978  * If instruction has to be poisoned, *poison will be set to true.
4979  */
4980 static int bpf_core_calc_relo(const struct bpf_program *prog,
4981 			      const struct bpf_core_relo *relo,
4982 			      int relo_idx,
4983 			      const struct bpf_core_spec *local_spec,
4984 			      const struct bpf_core_spec *targ_spec,
4985 			      struct bpf_core_relo_res *res)
4986 {
4987 	int err = -EOPNOTSUPP;
4988 
4989 	res->orig_val = 0;
4990 	res->new_val = 0;
4991 	res->poison = false;
4992 	res->validate = true;
4993 
4994 	if (core_relo_is_field_based(relo->kind)) {
4995 		err = bpf_core_calc_field_relo(prog, relo, local_spec, &res->orig_val, &res->validate);
4996 		err = err ?: bpf_core_calc_field_relo(prog, relo, targ_spec, &res->new_val, NULL);
4997 	} else if (core_relo_is_type_based(relo->kind)) {
4998 		err = bpf_core_calc_type_relo(relo, local_spec, &res->orig_val);
4999 		err = err ?: bpf_core_calc_type_relo(relo, targ_spec, &res->new_val);
5000 	} else if (core_relo_is_enumval_based(relo->kind)) {
5001 		err = bpf_core_calc_enumval_relo(relo, local_spec, &res->orig_val);
5002 		err = err ?: bpf_core_calc_enumval_relo(relo, targ_spec, &res->new_val);
5003 	}
5004 
5005 	if (err == -EUCLEAN) {
5006 		/* EUCLEAN is used to signal instruction poisoning request */
5007 		res->poison = true;
5008 		err = 0;
5009 	} else if (err == -EOPNOTSUPP) {
5010 		/* EOPNOTSUPP means unknown/unsupported relocation */
5011 		pr_warn("prog '%s': relo #%d: unrecognized CO-RE relocation %s (%d) at insn #%d\n",
5012 			bpf_program__title(prog, false), relo_idx,
5013 			core_relo_kind_str(relo->kind), relo->kind, relo->insn_off / 8);
5014 	}
5015 
5016 	return err;
5017 }
5018 
5019 /*
5020  * Turn instruction for which CO_RE relocation failed into invalid one with
5021  * distinct signature.
5022  */
5023 static void bpf_core_poison_insn(struct bpf_program *prog, int relo_idx,
5024 				 int insn_idx, struct bpf_insn *insn)
5025 {
5026 	pr_debug("prog '%s': relo #%d: substituting insn #%d w/ invalid insn\n",
5027 		 bpf_program__title(prog, false), relo_idx, insn_idx);
5028 	insn->code = BPF_JMP | BPF_CALL;
5029 	insn->dst_reg = 0;
5030 	insn->src_reg = 0;
5031 	insn->off = 0;
5032 	/* if this instruction is reachable (not a dead code),
5033 	 * verifier will complain with the following message:
5034 	 * invalid func unknown#195896080
5035 	 */
5036 	insn->imm = 195896080; /* => 0xbad2310 => "bad relo" */
5037 }
5038 
5039 static bool is_ldimm64(struct bpf_insn *insn)
5040 {
5041 	return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
5042 }
5043 
5044 /*
5045  * Patch relocatable BPF instruction.
5046  *
5047  * Patched value is determined by relocation kind and target specification.
5048  * For existence relocations target spec will be NULL if field/type is not found.
5049  * Expected insn->imm value is determined using relocation kind and local
5050  * spec, and is checked before patching instruction. If actual insn->imm value
5051  * is wrong, bail out with error.
5052  *
5053  * Currently three kinds of BPF instructions are supported:
5054  * 1. rX = <imm> (assignment with immediate operand);
5055  * 2. rX += <imm> (arithmetic operations with immediate operand);
5056  * 3. rX = <imm64> (load with 64-bit immediate value).
5057  */
5058 static int bpf_core_patch_insn(struct bpf_program *prog,
5059 			       const struct bpf_core_relo *relo,
5060 			       int relo_idx,
5061 			       const struct bpf_core_relo_res *res)
5062 {
5063 	__u32 orig_val, new_val;
5064 	struct bpf_insn *insn;
5065 	int insn_idx;
5066 	__u8 class;
5067 
5068 	if (relo->insn_off % sizeof(struct bpf_insn))
5069 		return -EINVAL;
5070 	insn_idx = relo->insn_off / sizeof(struct bpf_insn);
5071 	insn = &prog->insns[insn_idx];
5072 	class = BPF_CLASS(insn->code);
5073 
5074 	if (res->poison) {
5075 		/* poison second part of ldimm64 to avoid confusing error from
5076 		 * verifier about "unknown opcode 00"
5077 		 */
5078 		if (is_ldimm64(insn))
5079 			bpf_core_poison_insn(prog, relo_idx, insn_idx + 1, insn + 1);
5080 		bpf_core_poison_insn(prog, relo_idx, insn_idx, insn);
5081 		return 0;
5082 	}
5083 
5084 	orig_val = res->orig_val;
5085 	new_val = res->new_val;
5086 
5087 	switch (class) {
5088 	case BPF_ALU:
5089 	case BPF_ALU64:
5090 		if (BPF_SRC(insn->code) != BPF_K)
5091 			return -EINVAL;
5092 		if (res->validate && insn->imm != orig_val) {
5093 			pr_warn("prog '%s': relo #%d: unexpected insn #%d (ALU/ALU64) value: got %u, exp %u -> %u\n",
5094 				bpf_program__title(prog, false), relo_idx,
5095 				insn_idx, insn->imm, orig_val, new_val);
5096 			return -EINVAL;
5097 		}
5098 		orig_val = insn->imm;
5099 		insn->imm = new_val;
5100 		pr_debug("prog '%s': relo #%d: patched insn #%d (ALU/ALU64) imm %u -> %u\n",
5101 			 bpf_program__title(prog, false), relo_idx, insn_idx,
5102 			 orig_val, new_val);
5103 		break;
5104 	case BPF_LDX:
5105 	case BPF_ST:
5106 	case BPF_STX:
5107 		if (res->validate && insn->off != orig_val) {
5108 			pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %u -> %u\n",
5109 				bpf_program__title(prog, false), relo_idx,
5110 				insn_idx, insn->off, orig_val, new_val);
5111 			return -EINVAL;
5112 		}
5113 		if (new_val > SHRT_MAX) {
5114 			pr_warn("prog '%s': relo #%d: insn #%d (LDX/ST/STX) value too big: %u\n",
5115 				bpf_program__title(prog, false), relo_idx,
5116 				insn_idx, new_val);
5117 			return -ERANGE;
5118 		}
5119 		orig_val = insn->off;
5120 		insn->off = new_val;
5121 		pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %u -> %u\n",
5122 			 bpf_program__title(prog, false), relo_idx, insn_idx,
5123 			 orig_val, new_val);
5124 		break;
5125 	case BPF_LD: {
5126 		__u64 imm;
5127 
5128 		if (!is_ldimm64(insn) ||
5129 		    insn[0].src_reg != 0 || insn[0].off != 0 ||
5130 		    insn_idx + 1 >= prog->insns_cnt ||
5131 		    insn[1].code != 0 || insn[1].dst_reg != 0 ||
5132 		    insn[1].src_reg != 0 || insn[1].off != 0) {
5133 			pr_warn("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n",
5134 				bpf_program__title(prog, false), relo_idx, insn_idx);
5135 			return -EINVAL;
5136 		}
5137 
5138 		imm = insn[0].imm + ((__u64)insn[1].imm << 32);
5139 		if (res->validate && imm != orig_val) {
5140 			pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %u -> %u\n",
5141 				bpf_program__title(prog, false), relo_idx,
5142 				insn_idx, imm, orig_val, new_val);
5143 			return -EINVAL;
5144 		}
5145 
5146 		insn[0].imm = new_val;
5147 		insn[1].imm = 0; /* currently only 32-bit values are supported */
5148 		pr_debug("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %u\n",
5149 			 bpf_program__title(prog, false), relo_idx, insn_idx,
5150 			 imm, new_val);
5151 		break;
5152 	}
5153 	default:
5154 		pr_warn("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:0x%x, src:0x%x, dst:0x%x, off:0x%x, imm:0x%x\n",
5155 			bpf_program__title(prog, false), relo_idx,
5156 			insn_idx, insn->code, insn->src_reg, insn->dst_reg,
5157 			insn->off, insn->imm);
5158 		return -EINVAL;
5159 	}
5160 
5161 	return 0;
5162 }
5163 
5164 /* Output spec definition in the format:
5165  * [<type-id>] (<type-name>) + <raw-spec> => <offset>@<spec>,
5166  * where <spec> is a C-syntax view of recorded field access, e.g.: x.a[3].b
5167  */
5168 static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
5169 {
5170 	const struct btf_type *t;
5171 	const struct btf_enum *e;
5172 	const char *s;
5173 	__u32 type_id;
5174 	int i;
5175 
5176 	type_id = spec->root_type_id;
5177 	t = btf__type_by_id(spec->btf, type_id);
5178 	s = btf__name_by_offset(spec->btf, t->name_off);
5179 
5180 	libbpf_print(level, "[%u] %s %s", type_id, btf_kind_str(t), str_is_empty(s) ? "<anon>" : s);
5181 
5182 	if (core_relo_is_type_based(spec->relo_kind))
5183 		return;
5184 
5185 	if (core_relo_is_enumval_based(spec->relo_kind)) {
5186 		t = skip_mods_and_typedefs(spec->btf, type_id, NULL);
5187 		e = btf_enum(t) + spec->raw_spec[0];
5188 		s = btf__name_by_offset(spec->btf, e->name_off);
5189 
5190 		libbpf_print(level, "::%s = %u", s, e->val);
5191 		return;
5192 	}
5193 
5194 	if (core_relo_is_field_based(spec->relo_kind)) {
5195 		for (i = 0; i < spec->len; i++) {
5196 			if (spec->spec[i].name)
5197 				libbpf_print(level, ".%s", spec->spec[i].name);
5198 			else if (i > 0 || spec->spec[i].idx > 0)
5199 				libbpf_print(level, "[%u]", spec->spec[i].idx);
5200 		}
5201 
5202 		libbpf_print(level, " (");
5203 		for (i = 0; i < spec->raw_len; i++)
5204 			libbpf_print(level, "%s%d", i == 0 ? "" : ":", spec->raw_spec[i]);
5205 
5206 		if (spec->bit_offset % 8)
5207 			libbpf_print(level, " @ offset %u.%u)",
5208 				     spec->bit_offset / 8, spec->bit_offset % 8);
5209 		else
5210 			libbpf_print(level, " @ offset %u)", spec->bit_offset / 8);
5211 		return;
5212 	}
5213 }
5214 
5215 static size_t bpf_core_hash_fn(const void *key, void *ctx)
5216 {
5217 	return (size_t)key;
5218 }
5219 
5220 static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
5221 {
5222 	return k1 == k2;
5223 }
5224 
5225 static void *u32_as_hash_key(__u32 x)
5226 {
5227 	return (void *)(uintptr_t)x;
5228 }
5229 
5230 /*
5231  * CO-RE relocate single instruction.
5232  *
5233  * The outline and important points of the algorithm:
5234  * 1. For given local type, find corresponding candidate target types.
5235  *    Candidate type is a type with the same "essential" name, ignoring
5236  *    everything after last triple underscore (___). E.g., `sample`,
5237  *    `sample___flavor_one`, `sample___flavor_another_one`, are all candidates
5238  *    for each other. Names with triple underscore are referred to as
5239  *    "flavors" and are useful, among other things, to allow to
5240  *    specify/support incompatible variations of the same kernel struct, which
5241  *    might differ between different kernel versions and/or build
5242  *    configurations.
5243  *
5244  *    N.B. Struct "flavors" could be generated by bpftool's BTF-to-C
5245  *    converter, when deduplicated BTF of a kernel still contains more than
5246  *    one different types with the same name. In that case, ___2, ___3, etc
5247  *    are appended starting from second name conflict. But start flavors are
5248  *    also useful to be defined "locally", in BPF program, to extract same
5249  *    data from incompatible changes between different kernel
5250  *    versions/configurations. For instance, to handle field renames between
5251  *    kernel versions, one can use two flavors of the struct name with the
5252  *    same common name and use conditional relocations to extract that field,
5253  *    depending on target kernel version.
5254  * 2. For each candidate type, try to match local specification to this
5255  *    candidate target type. Matching involves finding corresponding
5256  *    high-level spec accessors, meaning that all named fields should match,
5257  *    as well as all array accesses should be within the actual bounds. Also,
5258  *    types should be compatible (see bpf_core_fields_are_compat for details).
5259  * 3. It is supported and expected that there might be multiple flavors
5260  *    matching the spec. As long as all the specs resolve to the same set of
5261  *    offsets across all candidates, there is no error. If there is any
5262  *    ambiguity, CO-RE relocation will fail. This is necessary to accomodate
5263  *    imprefection of BTF deduplication, which can cause slight duplication of
5264  *    the same BTF type, if some directly or indirectly referenced (by
5265  *    pointer) type gets resolved to different actual types in different
5266  *    object files. If such situation occurs, deduplicated BTF will end up
5267  *    with two (or more) structurally identical types, which differ only in
5268  *    types they refer to through pointer. This should be OK in most cases and
5269  *    is not an error.
5270  * 4. Candidate types search is performed by linearly scanning through all
5271  *    types in target BTF. It is anticipated that this is overall more
5272  *    efficient memory-wise and not significantly worse (if not better)
5273  *    CPU-wise compared to prebuilding a map from all local type names to
5274  *    a list of candidate type names. It's also sped up by caching resolved
5275  *    list of matching candidates per each local "root" type ID, that has at
5276  *    least one bpf_core_relo associated with it. This list is shared
5277  *    between multiple relocations for the same type ID and is updated as some
5278  *    of the candidates are pruned due to structural incompatibility.
5279  */
5280 static int bpf_core_apply_relo(struct bpf_program *prog,
5281 			       const struct bpf_core_relo *relo,
5282 			       int relo_idx,
5283 			       const struct btf *local_btf,
5284 			       const struct btf *targ_btf,
5285 			       struct hashmap *cand_cache)
5286 {
5287 	const char *prog_name = bpf_program__title(prog, false);
5288 	struct bpf_core_spec local_spec, cand_spec, targ_spec;
5289 	const void *type_key = u32_as_hash_key(relo->type_id);
5290 	struct bpf_core_relo_res cand_res, targ_res;
5291 	const struct btf_type *local_type;
5292 	const char *local_name;
5293 	struct ids_vec *cand_ids;
5294 	__u32 local_id, cand_id;
5295 	const char *spec_str;
5296 	int i, j, err;
5297 
5298 	local_id = relo->type_id;
5299 	local_type = btf__type_by_id(local_btf, local_id);
5300 	if (!local_type)
5301 		return -EINVAL;
5302 
5303 	local_name = btf__name_by_offset(local_btf, local_type->name_off);
5304 	if (!local_name)
5305 		return -EINVAL;
5306 
5307 	spec_str = btf__name_by_offset(local_btf, relo->access_str_off);
5308 	if (str_is_empty(spec_str))
5309 		return -EINVAL;
5310 
5311 	err = bpf_core_parse_spec(local_btf, local_id, spec_str, relo->kind, &local_spec);
5312 	if (err) {
5313 		pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
5314 			prog_name, relo_idx, local_id, btf_kind_str(local_type),
5315 			str_is_empty(local_name) ? "<anon>" : local_name,
5316 			spec_str, err);
5317 		return -EINVAL;
5318 	}
5319 
5320 	pr_debug("prog '%s': relo #%d: kind <%s> (%d), spec is ", prog_name,
5321 		 relo_idx, core_relo_kind_str(relo->kind), relo->kind);
5322 	bpf_core_dump_spec(LIBBPF_DEBUG, &local_spec);
5323 	libbpf_print(LIBBPF_DEBUG, "\n");
5324 
5325 	/* TYPE_ID_LOCAL relo is special and doesn't need candidate search */
5326 	if (relo->kind == BPF_TYPE_ID_LOCAL) {
5327 		targ_res.validate = true;
5328 		targ_res.poison = false;
5329 		targ_res.orig_val = local_spec.root_type_id;
5330 		targ_res.new_val = local_spec.root_type_id;
5331 		goto patch_insn;
5332 	}
5333 
5334 	/* libbpf doesn't support candidate search for anonymous types */
5335 	if (str_is_empty(spec_str)) {
5336 		pr_warn("prog '%s': relo #%d: <%s> (%d) relocation doesn't support anonymous types\n",
5337 			prog_name, relo_idx, core_relo_kind_str(relo->kind), relo->kind);
5338 		return -EOPNOTSUPP;
5339 	}
5340 
5341 	if (!hashmap__find(cand_cache, type_key, (void **)&cand_ids)) {
5342 		cand_ids = bpf_core_find_cands(local_btf, local_id, targ_btf);
5343 		if (IS_ERR(cand_ids)) {
5344 			pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld",
5345 				prog_name, relo_idx, local_id, btf_kind_str(local_type),
5346 				local_name, PTR_ERR(cand_ids));
5347 			return PTR_ERR(cand_ids);
5348 		}
5349 		err = hashmap__set(cand_cache, type_key, cand_ids, NULL, NULL);
5350 		if (err) {
5351 			bpf_core_free_cands(cand_ids);
5352 			return err;
5353 		}
5354 	}
5355 
5356 	for (i = 0, j = 0; i < cand_ids->len; i++) {
5357 		cand_id = cand_ids->data[i];
5358 		err = bpf_core_spec_match(&local_spec, targ_btf, cand_id, &cand_spec);
5359 		if (err < 0) {
5360 			pr_warn("prog '%s': relo #%d: error matching candidate #%d ",
5361 				prog_name, relo_idx, i);
5362 			bpf_core_dump_spec(LIBBPF_WARN, &cand_spec);
5363 			libbpf_print(LIBBPF_WARN, ": %d\n", err);
5364 			return err;
5365 		}
5366 
5367 		pr_debug("prog '%s': relo #%d: %s candidate #%d ", prog_name,
5368 			 relo_idx, err == 0 ? "non-matching" : "matching", i);
5369 		bpf_core_dump_spec(LIBBPF_DEBUG, &cand_spec);
5370 		libbpf_print(LIBBPF_DEBUG, "\n");
5371 
5372 		if (err == 0)
5373 			continue;
5374 
5375 		err = bpf_core_calc_relo(prog, relo, relo_idx, &local_spec, &cand_spec, &cand_res);
5376 		if (err)
5377 			return err;
5378 
5379 		if (j == 0) {
5380 			targ_res = cand_res;
5381 			targ_spec = cand_spec;
5382 		} else if (cand_spec.bit_offset != targ_spec.bit_offset) {
5383 			/* if there are many field relo candidates, they
5384 			 * should all resolve to the same bit offset
5385 			 */
5386 			pr_warn("prog '%s': relo #%d: field offset ambiguity: %u != %u\n",
5387 				prog_name, relo_idx, cand_spec.bit_offset,
5388 				targ_spec.bit_offset);
5389 			return -EINVAL;
5390 		} else if (cand_res.poison != targ_res.poison || cand_res.new_val != targ_res.new_val) {
5391 			/* all candidates should result in the same relocation
5392 			 * decision and value, otherwise it's dangerous to
5393 			 * proceed due to ambiguity
5394 			 */
5395 			pr_warn("prog '%s': relo #%d: relocation decision ambiguity: %s %u != %s %u\n",
5396 				prog_name, relo_idx,
5397 				cand_res.poison ? "failure" : "success", cand_res.new_val,
5398 				targ_res.poison ? "failure" : "success", targ_res.new_val);
5399 			return -EINVAL;
5400 		}
5401 
5402 		cand_ids->data[j++] = cand_spec.root_type_id;
5403 	}
5404 
5405 	/*
5406 	 * For BPF_FIELD_EXISTS relo or when used BPF program has field
5407 	 * existence checks or kernel version/config checks, it's expected
5408 	 * that we might not find any candidates. In this case, if field
5409 	 * wasn't found in any candidate, the list of candidates shouldn't
5410 	 * change at all, we'll just handle relocating appropriately,
5411 	 * depending on relo's kind.
5412 	 */
5413 	if (j > 0)
5414 		cand_ids->len = j;
5415 
5416 	/*
5417 	 * If no candidates were found, it might be both a programmer error,
5418 	 * as well as expected case, depending whether instruction w/
5419 	 * relocation is guarded in some way that makes it unreachable (dead
5420 	 * code) if relocation can't be resolved. This is handled in
5421 	 * bpf_core_patch_insn() uniformly by replacing that instruction with
5422 	 * BPF helper call insn (using invalid helper ID). If that instruction
5423 	 * is indeed unreachable, then it will be ignored and eliminated by
5424 	 * verifier. If it was an error, then verifier will complain and point
5425 	 * to a specific instruction number in its log.
5426 	 */
5427 	if (j == 0) {
5428 		pr_debug("prog '%s': relo #%d: no matching targets found\n",
5429 			 prog_name, relo_idx);
5430 
5431 		/* calculate single target relo result explicitly */
5432 		err = bpf_core_calc_relo(prog, relo, relo_idx, &local_spec, NULL, &targ_res);
5433 		if (err)
5434 			return err;
5435 	}
5436 
5437 patch_insn:
5438 	/* bpf_core_patch_insn() should know how to handle missing targ_spec */
5439 	err = bpf_core_patch_insn(prog, relo, relo_idx, &targ_res);
5440 	if (err) {
5441 		pr_warn("prog '%s': relo #%d: failed to patch insn at offset %d: %d\n",
5442 			prog_name, relo_idx, relo->insn_off, err);
5443 		return -EINVAL;
5444 	}
5445 
5446 	return 0;
5447 }
5448 
5449 static int
5450 bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
5451 {
5452 	const struct btf_ext_info_sec *sec;
5453 	const struct bpf_core_relo *rec;
5454 	const struct btf_ext_info *seg;
5455 	struct hashmap_entry *entry;
5456 	struct hashmap *cand_cache = NULL;
5457 	struct bpf_program *prog;
5458 	struct btf *targ_btf;
5459 	const char *sec_name;
5460 	int i, err = 0;
5461 
5462 	if (obj->btf_ext->core_relo_info.len == 0)
5463 		return 0;
5464 
5465 	if (targ_btf_path)
5466 		targ_btf = btf__parse_elf(targ_btf_path, NULL);
5467 	else
5468 		targ_btf = obj->btf_vmlinux;
5469 	if (IS_ERR_OR_NULL(targ_btf)) {
5470 		pr_warn("failed to get target BTF: %ld\n", PTR_ERR(targ_btf));
5471 		return PTR_ERR(targ_btf);
5472 	}
5473 
5474 	cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5475 	if (IS_ERR(cand_cache)) {
5476 		err = PTR_ERR(cand_cache);
5477 		goto out;
5478 	}
5479 
5480 	seg = &obj->btf_ext->core_relo_info;
5481 	for_each_btf_ext_sec(seg, sec) {
5482 		sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5483 		if (str_is_empty(sec_name)) {
5484 			err = -EINVAL;
5485 			goto out;
5486 		}
5487 		prog = NULL;
5488 		for (i = 0; i < obj->nr_programs; i++) {
5489 			if (!strcmp(obj->programs[i].section_name, sec_name)) {
5490 				prog = &obj->programs[i];
5491 				break;
5492 			}
5493 		}
5494 		if (!prog) {
5495 			pr_warn("failed to find program '%s' for CO-RE offset relocation\n",
5496 				sec_name);
5497 			err = -EINVAL;
5498 			goto out;
5499 		}
5500 
5501 		pr_debug("prog '%s': performing %d CO-RE offset relocs\n",
5502 			 sec_name, sec->num_info);
5503 
5504 		for_each_btf_ext_rec(seg, sec, i, rec) {
5505 			err = bpf_core_apply_relo(prog, rec, i, obj->btf,
5506 						  targ_btf, cand_cache);
5507 			if (err) {
5508 				pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
5509 					sec_name, i, err);
5510 				goto out;
5511 			}
5512 		}
5513 	}
5514 
5515 out:
5516 	/* obj->btf_vmlinux is freed at the end of object load phase */
5517 	if (targ_btf != obj->btf_vmlinux)
5518 		btf__free(targ_btf);
5519 	if (!IS_ERR_OR_NULL(cand_cache)) {
5520 		hashmap__for_each_entry(cand_cache, entry, i) {
5521 			bpf_core_free_cands(entry->value);
5522 		}
5523 		hashmap__free(cand_cache);
5524 	}
5525 	return err;
5526 }
5527 
5528 static int
5529 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
5530 			struct reloc_desc *relo)
5531 {
5532 	struct bpf_insn *insn, *new_insn;
5533 	struct bpf_program *text;
5534 	size_t new_cnt;
5535 	int err;
5536 
5537 	if (prog->idx != obj->efile.text_shndx && prog->main_prog_cnt == 0) {
5538 		text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
5539 		if (!text) {
5540 			pr_warn("no .text section found yet relo into text exist\n");
5541 			return -LIBBPF_ERRNO__RELOC;
5542 		}
5543 		new_cnt = prog->insns_cnt + text->insns_cnt;
5544 		new_insn = libbpf_reallocarray(prog->insns, new_cnt, sizeof(*insn));
5545 		if (!new_insn) {
5546 			pr_warn("oom in prog realloc\n");
5547 			return -ENOMEM;
5548 		}
5549 		prog->insns = new_insn;
5550 
5551 		if (obj->btf_ext) {
5552 			err = bpf_program_reloc_btf_ext(prog, obj,
5553 							text->section_name,
5554 							prog->insns_cnt);
5555 			if (err)
5556 				return err;
5557 		}
5558 
5559 		memcpy(new_insn + prog->insns_cnt, text->insns,
5560 		       text->insns_cnt * sizeof(*insn));
5561 		prog->main_prog_cnt = prog->insns_cnt;
5562 		prog->insns_cnt = new_cnt;
5563 		pr_debug("added %zd insn from %s to prog %s\n",
5564 			 text->insns_cnt, text->section_name,
5565 			 prog->section_name);
5566 	}
5567 
5568 	insn = &prog->insns[relo->insn_idx];
5569 	insn->imm += relo->sym_off / 8 + prog->main_prog_cnt - relo->insn_idx;
5570 	return 0;
5571 }
5572 
5573 static int
5574 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
5575 {
5576 	int i, err;
5577 
5578 	if (!prog)
5579 		return 0;
5580 
5581 	if (obj->btf_ext) {
5582 		err = bpf_program_reloc_btf_ext(prog, obj,
5583 						prog->section_name, 0);
5584 		if (err)
5585 			return err;
5586 	}
5587 
5588 	if (!prog->reloc_desc)
5589 		return 0;
5590 
5591 	for (i = 0; i < prog->nr_reloc; i++) {
5592 		struct reloc_desc *relo = &prog->reloc_desc[i];
5593 		struct bpf_insn *insn = &prog->insns[relo->insn_idx];
5594 		struct extern_desc *ext;
5595 
5596 		if (relo->insn_idx + 1 >= (int)prog->insns_cnt) {
5597 			pr_warn("relocation out of range: '%s'\n",
5598 				prog->section_name);
5599 			return -LIBBPF_ERRNO__RELOC;
5600 		}
5601 
5602 		switch (relo->type) {
5603 		case RELO_LD64:
5604 			insn[0].src_reg = BPF_PSEUDO_MAP_FD;
5605 			insn[0].imm = obj->maps[relo->map_idx].fd;
5606 			break;
5607 		case RELO_DATA:
5608 			insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
5609 			insn[1].imm = insn[0].imm + relo->sym_off;
5610 			insn[0].imm = obj->maps[relo->map_idx].fd;
5611 			break;
5612 		case RELO_EXTERN:
5613 			ext = &obj->externs[relo->sym_off];
5614 			if (ext->type == EXT_KCFG) {
5615 				insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
5616 				insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
5617 				insn[1].imm = ext->kcfg.data_off;
5618 			} else /* EXT_KSYM */ {
5619 				insn[0].imm = (__u32)ext->ksym.addr;
5620 				insn[1].imm = ext->ksym.addr >> 32;
5621 			}
5622 			break;
5623 		case RELO_CALL:
5624 			err = bpf_program__reloc_text(prog, obj, relo);
5625 			if (err)
5626 				return err;
5627 			break;
5628 		default:
5629 			pr_warn("relo #%d: bad relo type %d\n", i, relo->type);
5630 			return -EINVAL;
5631 		}
5632 	}
5633 
5634 	zfree(&prog->reloc_desc);
5635 	prog->nr_reloc = 0;
5636 	return 0;
5637 }
5638 
5639 static int
5640 bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
5641 {
5642 	struct bpf_program *prog;
5643 	size_t i;
5644 	int err;
5645 
5646 	if (obj->btf_ext) {
5647 		err = bpf_object__relocate_core(obj, targ_btf_path);
5648 		if (err) {
5649 			pr_warn("failed to perform CO-RE relocations: %d\n",
5650 				err);
5651 			return err;
5652 		}
5653 	}
5654 	/* ensure .text is relocated first, as it's going to be copied as-is
5655 	 * later for sub-program calls
5656 	 */
5657 	for (i = 0; i < obj->nr_programs; i++) {
5658 		prog = &obj->programs[i];
5659 		if (prog->idx != obj->efile.text_shndx)
5660 			continue;
5661 
5662 		err = bpf_program__relocate(prog, obj);
5663 		if (err) {
5664 			pr_warn("failed to relocate '%s'\n", prog->section_name);
5665 			return err;
5666 		}
5667 		break;
5668 	}
5669 	/* now relocate everything but .text, which by now is relocated
5670 	 * properly, so we can copy raw sub-program instructions as is safely
5671 	 */
5672 	for (i = 0; i < obj->nr_programs; i++) {
5673 		prog = &obj->programs[i];
5674 		if (prog->idx == obj->efile.text_shndx)
5675 			continue;
5676 
5677 		err = bpf_program__relocate(prog, obj);
5678 		if (err) {
5679 			pr_warn("failed to relocate '%s'\n", prog->section_name);
5680 			return err;
5681 		}
5682 	}
5683 	return 0;
5684 }
5685 
5686 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
5687 					    GElf_Shdr *shdr, Elf_Data *data);
5688 
5689 static int bpf_object__collect_map_relos(struct bpf_object *obj,
5690 					 GElf_Shdr *shdr, Elf_Data *data)
5691 {
5692 	const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
5693 	int i, j, nrels, new_sz;
5694 	const struct btf_var_secinfo *vi = NULL;
5695 	const struct btf_type *sec, *var, *def;
5696 	const struct btf_member *member;
5697 	struct bpf_map *map, *targ_map;
5698 	const char *name, *mname;
5699 	Elf_Data *symbols;
5700 	unsigned int moff;
5701 	GElf_Sym sym;
5702 	GElf_Rel rel;
5703 	void *tmp;
5704 
5705 	if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
5706 		return -EINVAL;
5707 	sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
5708 	if (!sec)
5709 		return -EINVAL;
5710 
5711 	symbols = obj->efile.symbols;
5712 	nrels = shdr->sh_size / shdr->sh_entsize;
5713 	for (i = 0; i < nrels; i++) {
5714 		if (!gelf_getrel(data, i, &rel)) {
5715 			pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
5716 			return -LIBBPF_ERRNO__FORMAT;
5717 		}
5718 		if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
5719 			pr_warn(".maps relo #%d: symbol %zx not found\n",
5720 				i, (size_t)GELF_R_SYM(rel.r_info));
5721 			return -LIBBPF_ERRNO__FORMAT;
5722 		}
5723 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
5724 				  sym.st_name) ? : "<?>";
5725 		if (sym.st_shndx != obj->efile.btf_maps_shndx) {
5726 			pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
5727 				i, name);
5728 			return -LIBBPF_ERRNO__RELOC;
5729 		}
5730 
5731 		pr_debug(".maps relo #%d: for %zd value %zd rel.r_offset %zu name %d ('%s')\n",
5732 			 i, (ssize_t)(rel.r_info >> 32), (size_t)sym.st_value,
5733 			 (size_t)rel.r_offset, sym.st_name, name);
5734 
5735 		for (j = 0; j < obj->nr_maps; j++) {
5736 			map = &obj->maps[j];
5737 			if (map->sec_idx != obj->efile.btf_maps_shndx)
5738 				continue;
5739 
5740 			vi = btf_var_secinfos(sec) + map->btf_var_idx;
5741 			if (vi->offset <= rel.r_offset &&
5742 			    rel.r_offset + bpf_ptr_sz <= vi->offset + vi->size)
5743 				break;
5744 		}
5745 		if (j == obj->nr_maps) {
5746 			pr_warn(".maps relo #%d: cannot find map '%s' at rel.r_offset %zu\n",
5747 				i, name, (size_t)rel.r_offset);
5748 			return -EINVAL;
5749 		}
5750 
5751 		if (!bpf_map_type__is_map_in_map(map->def.type))
5752 			return -EINVAL;
5753 		if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
5754 		    map->def.key_size != sizeof(int)) {
5755 			pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
5756 				i, map->name, sizeof(int));
5757 			return -EINVAL;
5758 		}
5759 
5760 		targ_map = bpf_object__find_map_by_name(obj, name);
5761 		if (!targ_map)
5762 			return -ESRCH;
5763 
5764 		var = btf__type_by_id(obj->btf, vi->type);
5765 		def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
5766 		if (btf_vlen(def) == 0)
5767 			return -EINVAL;
5768 		member = btf_members(def) + btf_vlen(def) - 1;
5769 		mname = btf__name_by_offset(obj->btf, member->name_off);
5770 		if (strcmp(mname, "values"))
5771 			return -EINVAL;
5772 
5773 		moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
5774 		if (rel.r_offset - vi->offset < moff)
5775 			return -EINVAL;
5776 
5777 		moff = rel.r_offset - vi->offset - moff;
5778 		/* here we use BPF pointer size, which is always 64 bit, as we
5779 		 * are parsing ELF that was built for BPF target
5780 		 */
5781 		if (moff % bpf_ptr_sz)
5782 			return -EINVAL;
5783 		moff /= bpf_ptr_sz;
5784 		if (moff >= map->init_slots_sz) {
5785 			new_sz = moff + 1;
5786 			tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
5787 			if (!tmp)
5788 				return -ENOMEM;
5789 			map->init_slots = tmp;
5790 			memset(map->init_slots + map->init_slots_sz, 0,
5791 			       (new_sz - map->init_slots_sz) * host_ptr_sz);
5792 			map->init_slots_sz = new_sz;
5793 		}
5794 		map->init_slots[moff] = targ_map;
5795 
5796 		pr_debug(".maps relo #%d: map '%s' slot [%d] points to map '%s'\n",
5797 			 i, map->name, moff, name);
5798 	}
5799 
5800 	return 0;
5801 }
5802 
5803 static int bpf_object__collect_reloc(struct bpf_object *obj)
5804 {
5805 	int i, err;
5806 
5807 	if (!obj_elf_valid(obj)) {
5808 		pr_warn("Internal error: elf object is closed\n");
5809 		return -LIBBPF_ERRNO__INTERNAL;
5810 	}
5811 
5812 	for (i = 0; i < obj->efile.nr_reloc_sects; i++) {
5813 		GElf_Shdr *shdr = &obj->efile.reloc_sects[i].shdr;
5814 		Elf_Data *data = obj->efile.reloc_sects[i].data;
5815 		int idx = shdr->sh_info;
5816 		struct bpf_program *prog;
5817 
5818 		if (shdr->sh_type != SHT_REL) {
5819 			pr_warn("internal error at %d\n", __LINE__);
5820 			return -LIBBPF_ERRNO__INTERNAL;
5821 		}
5822 
5823 		if (idx == obj->efile.st_ops_shndx) {
5824 			err = bpf_object__collect_st_ops_relos(obj, shdr, data);
5825 		} else if (idx == obj->efile.btf_maps_shndx) {
5826 			err = bpf_object__collect_map_relos(obj, shdr, data);
5827 		} else {
5828 			prog = bpf_object__find_prog_by_idx(obj, idx);
5829 			if (!prog) {
5830 				pr_warn("relocation failed: no prog in section(%d)\n", idx);
5831 				return -LIBBPF_ERRNO__RELOC;
5832 			}
5833 			err = bpf_program__collect_reloc(prog, shdr, data, obj);
5834 		}
5835 		if (err)
5836 			return err;
5837 	}
5838 	return 0;
5839 }
5840 
5841 static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
5842 {
5843 	__u8 class = BPF_CLASS(insn->code);
5844 
5845 	if ((class == BPF_JMP || class == BPF_JMP32) &&
5846 	    BPF_OP(insn->code) == BPF_CALL &&
5847 	    BPF_SRC(insn->code) == BPF_K &&
5848 	    insn->src_reg == 0 && insn->dst_reg == 0) {
5849 		    if (func_id)
5850 			    *func_id = insn->imm;
5851 		    return true;
5852 	}
5853 	return false;
5854 }
5855 
5856 static int bpf_object__sanitize_prog(struct bpf_object* obj, struct bpf_program *prog)
5857 {
5858 	struct bpf_insn *insn = prog->insns;
5859 	enum bpf_func_id func_id;
5860 	int i;
5861 
5862 	for (i = 0; i < prog->insns_cnt; i++, insn++) {
5863 		if (!insn_is_helper_call(insn, &func_id))
5864 			continue;
5865 
5866 		/* on kernels that don't yet support
5867 		 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
5868 		 * to bpf_probe_read() which works well for old kernels
5869 		 */
5870 		switch (func_id) {
5871 		case BPF_FUNC_probe_read_kernel:
5872 		case BPF_FUNC_probe_read_user:
5873 			if (!kernel_supports(FEAT_PROBE_READ_KERN))
5874 				insn->imm = BPF_FUNC_probe_read;
5875 			break;
5876 		case BPF_FUNC_probe_read_kernel_str:
5877 		case BPF_FUNC_probe_read_user_str:
5878 			if (!kernel_supports(FEAT_PROBE_READ_KERN))
5879 				insn->imm = BPF_FUNC_probe_read_str;
5880 			break;
5881 		default:
5882 			break;
5883 		}
5884 	}
5885 	return 0;
5886 }
5887 
5888 static int
5889 load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
5890 	     char *license, __u32 kern_version, int *pfd)
5891 {
5892 	struct bpf_load_program_attr load_attr;
5893 	char *cp, errmsg[STRERR_BUFSIZE];
5894 	size_t log_buf_size = 0;
5895 	char *log_buf = NULL;
5896 	int btf_fd, ret;
5897 
5898 	if (!insns || !insns_cnt)
5899 		return -EINVAL;
5900 
5901 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
5902 	load_attr.prog_type = prog->type;
5903 	/* old kernels might not support specifying expected_attach_type */
5904 	if (!kernel_supports(FEAT_EXP_ATTACH_TYPE) && prog->sec_def &&
5905 	    prog->sec_def->is_exp_attach_type_optional)
5906 		load_attr.expected_attach_type = 0;
5907 	else
5908 		load_attr.expected_attach_type = prog->expected_attach_type;
5909 	if (kernel_supports(FEAT_PROG_NAME))
5910 		load_attr.name = prog->name;
5911 	load_attr.insns = insns;
5912 	load_attr.insns_cnt = insns_cnt;
5913 	load_attr.license = license;
5914 	if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
5915 	    prog->type == BPF_PROG_TYPE_LSM) {
5916 		load_attr.attach_btf_id = prog->attach_btf_id;
5917 	} else if (prog->type == BPF_PROG_TYPE_TRACING ||
5918 		   prog->type == BPF_PROG_TYPE_EXT) {
5919 		load_attr.attach_prog_fd = prog->attach_prog_fd;
5920 		load_attr.attach_btf_id = prog->attach_btf_id;
5921 	} else {
5922 		load_attr.kern_version = kern_version;
5923 		load_attr.prog_ifindex = prog->prog_ifindex;
5924 	}
5925 	/* specify func_info/line_info only if kernel supports them */
5926 	btf_fd = bpf_object__btf_fd(prog->obj);
5927 	if (btf_fd >= 0 && kernel_supports(FEAT_BTF_FUNC)) {
5928 		load_attr.prog_btf_fd = btf_fd;
5929 		load_attr.func_info = prog->func_info;
5930 		load_attr.func_info_rec_size = prog->func_info_rec_size;
5931 		load_attr.func_info_cnt = prog->func_info_cnt;
5932 		load_attr.line_info = prog->line_info;
5933 		load_attr.line_info_rec_size = prog->line_info_rec_size;
5934 		load_attr.line_info_cnt = prog->line_info_cnt;
5935 	}
5936 	load_attr.log_level = prog->log_level;
5937 	load_attr.prog_flags = prog->prog_flags;
5938 
5939 retry_load:
5940 	if (log_buf_size) {
5941 		log_buf = malloc(log_buf_size);
5942 		if (!log_buf)
5943 			return -ENOMEM;
5944 
5945 		*log_buf = 0;
5946 	}
5947 
5948 	ret = bpf_load_program_xattr(&load_attr, log_buf, log_buf_size);
5949 
5950 	if (ret >= 0) {
5951 		if (log_buf && load_attr.log_level)
5952 			pr_debug("verifier log:\n%s", log_buf);
5953 		*pfd = ret;
5954 		ret = 0;
5955 		goto out;
5956 	}
5957 
5958 	if (!log_buf || errno == ENOSPC) {
5959 		log_buf_size = max((size_t)BPF_LOG_BUF_SIZE,
5960 				   log_buf_size << 1);
5961 
5962 		free(log_buf);
5963 		goto retry_load;
5964 	}
5965 	ret = -errno;
5966 	cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
5967 	pr_warn("load bpf program failed: %s\n", cp);
5968 	pr_perm_msg(ret);
5969 
5970 	if (log_buf && log_buf[0] != '\0') {
5971 		ret = -LIBBPF_ERRNO__VERIFY;
5972 		pr_warn("-- BEGIN DUMP LOG ---\n");
5973 		pr_warn("\n%s\n", log_buf);
5974 		pr_warn("-- END LOG --\n");
5975 	} else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
5976 		pr_warn("Program too large (%zu insns), at most %d insns\n",
5977 			load_attr.insns_cnt, BPF_MAXINSNS);
5978 		ret = -LIBBPF_ERRNO__PROG2BIG;
5979 	} else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
5980 		/* Wrong program type? */
5981 		int fd;
5982 
5983 		load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
5984 		load_attr.expected_attach_type = 0;
5985 		fd = bpf_load_program_xattr(&load_attr, NULL, 0);
5986 		if (fd >= 0) {
5987 			close(fd);
5988 			ret = -LIBBPF_ERRNO__PROGTYPE;
5989 			goto out;
5990 		}
5991 	}
5992 
5993 out:
5994 	free(log_buf);
5995 	return ret;
5996 }
5997 
5998 static int libbpf_find_attach_btf_id(struct bpf_program *prog);
5999 
6000 int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
6001 {
6002 	int err = 0, fd, i, btf_id;
6003 
6004 	if (prog->obj->loaded) {
6005 		pr_warn("prog '%s'('%s'): can't load after object was loaded\n",
6006 			prog->name, prog->section_name);
6007 		return -EINVAL;
6008 	}
6009 
6010 	if ((prog->type == BPF_PROG_TYPE_TRACING ||
6011 	     prog->type == BPF_PROG_TYPE_LSM ||
6012 	     prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
6013 		btf_id = libbpf_find_attach_btf_id(prog);
6014 		if (btf_id <= 0)
6015 			return btf_id;
6016 		prog->attach_btf_id = btf_id;
6017 	}
6018 
6019 	if (prog->instances.nr < 0 || !prog->instances.fds) {
6020 		if (prog->preprocessor) {
6021 			pr_warn("Internal error: can't load program '%s'\n",
6022 				prog->section_name);
6023 			return -LIBBPF_ERRNO__INTERNAL;
6024 		}
6025 
6026 		prog->instances.fds = malloc(sizeof(int));
6027 		if (!prog->instances.fds) {
6028 			pr_warn("Not enough memory for BPF fds\n");
6029 			return -ENOMEM;
6030 		}
6031 		prog->instances.nr = 1;
6032 		prog->instances.fds[0] = -1;
6033 	}
6034 
6035 	if (!prog->preprocessor) {
6036 		if (prog->instances.nr != 1) {
6037 			pr_warn("Program '%s' is inconsistent: nr(%d) != 1\n",
6038 				prog->section_name, prog->instances.nr);
6039 		}
6040 		err = load_program(prog, prog->insns, prog->insns_cnt,
6041 				   license, kern_ver, &fd);
6042 		if (!err)
6043 			prog->instances.fds[0] = fd;
6044 		goto out;
6045 	}
6046 
6047 	for (i = 0; i < prog->instances.nr; i++) {
6048 		struct bpf_prog_prep_result result;
6049 		bpf_program_prep_t preprocessor = prog->preprocessor;
6050 
6051 		memset(&result, 0, sizeof(result));
6052 		err = preprocessor(prog, i, prog->insns,
6053 				   prog->insns_cnt, &result);
6054 		if (err) {
6055 			pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
6056 				i, prog->section_name);
6057 			goto out;
6058 		}
6059 
6060 		if (!result.new_insn_ptr || !result.new_insn_cnt) {
6061 			pr_debug("Skip loading the %dth instance of program '%s'\n",
6062 				 i, prog->section_name);
6063 			prog->instances.fds[i] = -1;
6064 			if (result.pfd)
6065 				*result.pfd = -1;
6066 			continue;
6067 		}
6068 
6069 		err = load_program(prog, result.new_insn_ptr,
6070 				   result.new_insn_cnt, license, kern_ver, &fd);
6071 		if (err) {
6072 			pr_warn("Loading the %dth instance of program '%s' failed\n",
6073 				i, prog->section_name);
6074 			goto out;
6075 		}
6076 
6077 		if (result.pfd)
6078 			*result.pfd = fd;
6079 		prog->instances.fds[i] = fd;
6080 	}
6081 out:
6082 	if (err)
6083 		pr_warn("failed to load program '%s'\n", prog->section_name);
6084 	zfree(&prog->insns);
6085 	prog->insns_cnt = 0;
6086 	return err;
6087 }
6088 
6089 static bool bpf_program__is_function_storage(const struct bpf_program *prog,
6090 					     const struct bpf_object *obj)
6091 {
6092 	return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
6093 }
6094 
6095 static int
6096 bpf_object__load_progs(struct bpf_object *obj, int log_level)
6097 {
6098 	struct bpf_program *prog;
6099 	size_t i;
6100 	int err;
6101 
6102 	for (i = 0; i < obj->nr_programs; i++) {
6103 		prog = &obj->programs[i];
6104 		err = bpf_object__sanitize_prog(obj, prog);
6105 		if (err)
6106 			return err;
6107 	}
6108 
6109 	for (i = 0; i < obj->nr_programs; i++) {
6110 		prog = &obj->programs[i];
6111 		if (bpf_program__is_function_storage(prog, obj))
6112 			continue;
6113 		if (!prog->load) {
6114 			pr_debug("prog '%s'('%s'): skipped loading\n",
6115 				 prog->name, prog->section_name);
6116 			continue;
6117 		}
6118 		prog->log_level |= log_level;
6119 		err = bpf_program__load(prog, obj->license, obj->kern_version);
6120 		if (err)
6121 			return err;
6122 	}
6123 	return 0;
6124 }
6125 
6126 static const struct bpf_sec_def *find_sec_def(const char *sec_name);
6127 
6128 static struct bpf_object *
6129 __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
6130 		   const struct bpf_object_open_opts *opts)
6131 {
6132 	const char *obj_name, *kconfig;
6133 	struct bpf_program *prog;
6134 	struct bpf_object *obj;
6135 	char tmp_name[64];
6136 	int err;
6137 
6138 	if (elf_version(EV_CURRENT) == EV_NONE) {
6139 		pr_warn("failed to init libelf for %s\n",
6140 			path ? : "(mem buf)");
6141 		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
6142 	}
6143 
6144 	if (!OPTS_VALID(opts, bpf_object_open_opts))
6145 		return ERR_PTR(-EINVAL);
6146 
6147 	obj_name = OPTS_GET(opts, object_name, NULL);
6148 	if (obj_buf) {
6149 		if (!obj_name) {
6150 			snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
6151 				 (unsigned long)obj_buf,
6152 				 (unsigned long)obj_buf_sz);
6153 			obj_name = tmp_name;
6154 		}
6155 		path = obj_name;
6156 		pr_debug("loading object '%s' from buffer\n", obj_name);
6157 	}
6158 
6159 	obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
6160 	if (IS_ERR(obj))
6161 		return obj;
6162 
6163 	kconfig = OPTS_GET(opts, kconfig, NULL);
6164 	if (kconfig) {
6165 		obj->kconfig = strdup(kconfig);
6166 		if (!obj->kconfig)
6167 			return ERR_PTR(-ENOMEM);
6168 	}
6169 
6170 	err = bpf_object__elf_init(obj);
6171 	err = err ? : bpf_object__check_endianness(obj);
6172 	err = err ? : bpf_object__elf_collect(obj);
6173 	err = err ? : bpf_object__collect_externs(obj);
6174 	err = err ? : bpf_object__finalize_btf(obj);
6175 	err = err ? : bpf_object__init_maps(obj, opts);
6176 	err = err ? : bpf_object__init_prog_names(obj);
6177 	err = err ? : bpf_object__collect_reloc(obj);
6178 	if (err)
6179 		goto out;
6180 	bpf_object__elf_finish(obj);
6181 
6182 	bpf_object__for_each_program(prog, obj) {
6183 		prog->sec_def = find_sec_def(prog->section_name);
6184 		if (!prog->sec_def)
6185 			/* couldn't guess, but user might manually specify */
6186 			continue;
6187 
6188 		bpf_program__set_type(prog, prog->sec_def->prog_type);
6189 		bpf_program__set_expected_attach_type(prog,
6190 				prog->sec_def->expected_attach_type);
6191 
6192 		if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING ||
6193 		    prog->sec_def->prog_type == BPF_PROG_TYPE_EXT)
6194 			prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
6195 	}
6196 
6197 	return obj;
6198 out:
6199 	bpf_object__close(obj);
6200 	return ERR_PTR(err);
6201 }
6202 
6203 static struct bpf_object *
6204 __bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags)
6205 {
6206 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
6207 		.relaxed_maps = flags & MAPS_RELAX_COMPAT,
6208 	);
6209 
6210 	/* param validation */
6211 	if (!attr->file)
6212 		return NULL;
6213 
6214 	pr_debug("loading %s\n", attr->file);
6215 	return __bpf_object__open(attr->file, NULL, 0, &opts);
6216 }
6217 
6218 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
6219 {
6220 	return __bpf_object__open_xattr(attr, 0);
6221 }
6222 
6223 struct bpf_object *bpf_object__open(const char *path)
6224 {
6225 	struct bpf_object_open_attr attr = {
6226 		.file		= path,
6227 		.prog_type	= BPF_PROG_TYPE_UNSPEC,
6228 	};
6229 
6230 	return bpf_object__open_xattr(&attr);
6231 }
6232 
6233 struct bpf_object *
6234 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
6235 {
6236 	if (!path)
6237 		return ERR_PTR(-EINVAL);
6238 
6239 	pr_debug("loading %s\n", path);
6240 
6241 	return __bpf_object__open(path, NULL, 0, opts);
6242 }
6243 
6244 struct bpf_object *
6245 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
6246 		     const struct bpf_object_open_opts *opts)
6247 {
6248 	if (!obj_buf || obj_buf_sz == 0)
6249 		return ERR_PTR(-EINVAL);
6250 
6251 	return __bpf_object__open(NULL, obj_buf, obj_buf_sz, opts);
6252 }
6253 
6254 struct bpf_object *
6255 bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
6256 			const char *name)
6257 {
6258 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
6259 		.object_name = name,
6260 		/* wrong default, but backwards-compatible */
6261 		.relaxed_maps = true,
6262 	);
6263 
6264 	/* returning NULL is wrong, but backwards-compatible */
6265 	if (!obj_buf || obj_buf_sz == 0)
6266 		return NULL;
6267 
6268 	return bpf_object__open_mem(obj_buf, obj_buf_sz, &opts);
6269 }
6270 
6271 int bpf_object__unload(struct bpf_object *obj)
6272 {
6273 	size_t i;
6274 
6275 	if (!obj)
6276 		return -EINVAL;
6277 
6278 	for (i = 0; i < obj->nr_maps; i++) {
6279 		zclose(obj->maps[i].fd);
6280 		if (obj->maps[i].st_ops)
6281 			zfree(&obj->maps[i].st_ops->kern_vdata);
6282 	}
6283 
6284 	for (i = 0; i < obj->nr_programs; i++)
6285 		bpf_program__unload(&obj->programs[i]);
6286 
6287 	return 0;
6288 }
6289 
6290 static int bpf_object__sanitize_maps(struct bpf_object *obj)
6291 {
6292 	struct bpf_map *m;
6293 
6294 	bpf_object__for_each_map(m, obj) {
6295 		if (!bpf_map__is_internal(m))
6296 			continue;
6297 		if (!kernel_supports(FEAT_GLOBAL_DATA)) {
6298 			pr_warn("kernel doesn't support global data\n");
6299 			return -ENOTSUP;
6300 		}
6301 		if (!kernel_supports(FEAT_ARRAY_MMAP))
6302 			m->def.map_flags ^= BPF_F_MMAPABLE;
6303 	}
6304 
6305 	return 0;
6306 }
6307 
6308 static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
6309 {
6310 	char sym_type, sym_name[500];
6311 	unsigned long long sym_addr;
6312 	struct extern_desc *ext;
6313 	int ret, err = 0;
6314 	FILE *f;
6315 
6316 	f = fopen("/proc/kallsyms", "r");
6317 	if (!f) {
6318 		err = -errno;
6319 		pr_warn("failed to open /proc/kallsyms: %d\n", err);
6320 		return err;
6321 	}
6322 
6323 	while (true) {
6324 		ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
6325 			     &sym_addr, &sym_type, sym_name);
6326 		if (ret == EOF && feof(f))
6327 			break;
6328 		if (ret != 3) {
6329 			pr_warn("failed to read kallsyms entry: %d\n", ret);
6330 			err = -EINVAL;
6331 			goto out;
6332 		}
6333 
6334 		ext = find_extern_by_name(obj, sym_name);
6335 		if (!ext || ext->type != EXT_KSYM)
6336 			continue;
6337 
6338 		if (ext->is_set && ext->ksym.addr != sym_addr) {
6339 			pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n",
6340 				sym_name, ext->ksym.addr, sym_addr);
6341 			err = -EINVAL;
6342 			goto out;
6343 		}
6344 		if (!ext->is_set) {
6345 			ext->is_set = true;
6346 			ext->ksym.addr = sym_addr;
6347 			pr_debug("extern (ksym) %s=0x%llx\n", sym_name, sym_addr);
6348 		}
6349 	}
6350 
6351 out:
6352 	fclose(f);
6353 	return err;
6354 }
6355 
6356 static int bpf_object__resolve_externs(struct bpf_object *obj,
6357 				       const char *extra_kconfig)
6358 {
6359 	bool need_config = false, need_kallsyms = false;
6360 	struct extern_desc *ext;
6361 	void *kcfg_data = NULL;
6362 	int err, i;
6363 
6364 	if (obj->nr_extern == 0)
6365 		return 0;
6366 
6367 	if (obj->kconfig_map_idx >= 0)
6368 		kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
6369 
6370 	for (i = 0; i < obj->nr_extern; i++) {
6371 		ext = &obj->externs[i];
6372 
6373 		if (ext->type == EXT_KCFG &&
6374 		    strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
6375 			void *ext_val = kcfg_data + ext->kcfg.data_off;
6376 			__u32 kver = get_kernel_version();
6377 
6378 			if (!kver) {
6379 				pr_warn("failed to get kernel version\n");
6380 				return -EINVAL;
6381 			}
6382 			err = set_kcfg_value_num(ext, ext_val, kver);
6383 			if (err)
6384 				return err;
6385 			pr_debug("extern (kcfg) %s=0x%x\n", ext->name, kver);
6386 		} else if (ext->type == EXT_KCFG &&
6387 			   strncmp(ext->name, "CONFIG_", 7) == 0) {
6388 			need_config = true;
6389 		} else if (ext->type == EXT_KSYM) {
6390 			need_kallsyms = true;
6391 		} else {
6392 			pr_warn("unrecognized extern '%s'\n", ext->name);
6393 			return -EINVAL;
6394 		}
6395 	}
6396 	if (need_config && extra_kconfig) {
6397 		err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
6398 		if (err)
6399 			return -EINVAL;
6400 		need_config = false;
6401 		for (i = 0; i < obj->nr_extern; i++) {
6402 			ext = &obj->externs[i];
6403 			if (ext->type == EXT_KCFG && !ext->is_set) {
6404 				need_config = true;
6405 				break;
6406 			}
6407 		}
6408 	}
6409 	if (need_config) {
6410 		err = bpf_object__read_kconfig_file(obj, kcfg_data);
6411 		if (err)
6412 			return -EINVAL;
6413 	}
6414 	if (need_kallsyms) {
6415 		err = bpf_object__read_kallsyms_file(obj);
6416 		if (err)
6417 			return -EINVAL;
6418 	}
6419 	for (i = 0; i < obj->nr_extern; i++) {
6420 		ext = &obj->externs[i];
6421 
6422 		if (!ext->is_set && !ext->is_weak) {
6423 			pr_warn("extern %s (strong) not resolved\n", ext->name);
6424 			return -ESRCH;
6425 		} else if (!ext->is_set) {
6426 			pr_debug("extern %s (weak) not resolved, defaulting to zero\n",
6427 				 ext->name);
6428 		}
6429 	}
6430 
6431 	return 0;
6432 }
6433 
6434 int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
6435 {
6436 	struct bpf_object *obj;
6437 	int err, i;
6438 
6439 	if (!attr)
6440 		return -EINVAL;
6441 	obj = attr->obj;
6442 	if (!obj)
6443 		return -EINVAL;
6444 
6445 	if (obj->loaded) {
6446 		pr_warn("object '%s': load can't be attempted twice\n", obj->name);
6447 		return -EINVAL;
6448 	}
6449 
6450 	err = bpf_object__probe_loading(obj);
6451 	err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
6452 	err = err ? : bpf_object__sanitize_and_load_btf(obj);
6453 	err = err ? : bpf_object__sanitize_maps(obj);
6454 	err = err ? : bpf_object__load_vmlinux_btf(obj);
6455 	err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
6456 	err = err ? : bpf_object__create_maps(obj);
6457 	err = err ? : bpf_object__relocate(obj, attr->target_btf_path);
6458 	err = err ? : bpf_object__load_progs(obj, attr->log_level);
6459 
6460 	btf__free(obj->btf_vmlinux);
6461 	obj->btf_vmlinux = NULL;
6462 
6463 	obj->loaded = true; /* doesn't matter if successfully or not */
6464 
6465 	if (err)
6466 		goto out;
6467 
6468 	return 0;
6469 out:
6470 	/* unpin any maps that were auto-pinned during load */
6471 	for (i = 0; i < obj->nr_maps; i++)
6472 		if (obj->maps[i].pinned && !obj->maps[i].reused)
6473 			bpf_map__unpin(&obj->maps[i], NULL);
6474 
6475 	bpf_object__unload(obj);
6476 	pr_warn("failed to load object '%s'\n", obj->path);
6477 	return err;
6478 }
6479 
6480 int bpf_object__load(struct bpf_object *obj)
6481 {
6482 	struct bpf_object_load_attr attr = {
6483 		.obj = obj,
6484 	};
6485 
6486 	return bpf_object__load_xattr(&attr);
6487 }
6488 
6489 static int make_parent_dir(const char *path)
6490 {
6491 	char *cp, errmsg[STRERR_BUFSIZE];
6492 	char *dname, *dir;
6493 	int err = 0;
6494 
6495 	dname = strdup(path);
6496 	if (dname == NULL)
6497 		return -ENOMEM;
6498 
6499 	dir = dirname(dname);
6500 	if (mkdir(dir, 0700) && errno != EEXIST)
6501 		err = -errno;
6502 
6503 	free(dname);
6504 	if (err) {
6505 		cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
6506 		pr_warn("failed to mkdir %s: %s\n", path, cp);
6507 	}
6508 	return err;
6509 }
6510 
6511 static int check_path(const char *path)
6512 {
6513 	char *cp, errmsg[STRERR_BUFSIZE];
6514 	struct statfs st_fs;
6515 	char *dname, *dir;
6516 	int err = 0;
6517 
6518 	if (path == NULL)
6519 		return -EINVAL;
6520 
6521 	dname = strdup(path);
6522 	if (dname == NULL)
6523 		return -ENOMEM;
6524 
6525 	dir = dirname(dname);
6526 	if (statfs(dir, &st_fs)) {
6527 		cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
6528 		pr_warn("failed to statfs %s: %s\n", dir, cp);
6529 		err = -errno;
6530 	}
6531 	free(dname);
6532 
6533 	if (!err && st_fs.f_type != BPF_FS_MAGIC) {
6534 		pr_warn("specified path %s is not on BPF FS\n", path);
6535 		err = -EINVAL;
6536 	}
6537 
6538 	return err;
6539 }
6540 
6541 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
6542 			      int instance)
6543 {
6544 	char *cp, errmsg[STRERR_BUFSIZE];
6545 	int err;
6546 
6547 	err = make_parent_dir(path);
6548 	if (err)
6549 		return err;
6550 
6551 	err = check_path(path);
6552 	if (err)
6553 		return err;
6554 
6555 	if (prog == NULL) {
6556 		pr_warn("invalid program pointer\n");
6557 		return -EINVAL;
6558 	}
6559 
6560 	if (instance < 0 || instance >= prog->instances.nr) {
6561 		pr_warn("invalid prog instance %d of prog %s (max %d)\n",
6562 			instance, prog->section_name, prog->instances.nr);
6563 		return -EINVAL;
6564 	}
6565 
6566 	if (bpf_obj_pin(prog->instances.fds[instance], path)) {
6567 		err = -errno;
6568 		cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
6569 		pr_warn("failed to pin program: %s\n", cp);
6570 		return err;
6571 	}
6572 	pr_debug("pinned program '%s'\n", path);
6573 
6574 	return 0;
6575 }
6576 
6577 int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
6578 				int instance)
6579 {
6580 	int err;
6581 
6582 	err = check_path(path);
6583 	if (err)
6584 		return err;
6585 
6586 	if (prog == NULL) {
6587 		pr_warn("invalid program pointer\n");
6588 		return -EINVAL;
6589 	}
6590 
6591 	if (instance < 0 || instance >= prog->instances.nr) {
6592 		pr_warn("invalid prog instance %d of prog %s (max %d)\n",
6593 			instance, prog->section_name, prog->instances.nr);
6594 		return -EINVAL;
6595 	}
6596 
6597 	err = unlink(path);
6598 	if (err != 0)
6599 		return -errno;
6600 	pr_debug("unpinned program '%s'\n", path);
6601 
6602 	return 0;
6603 }
6604 
6605 int bpf_program__pin(struct bpf_program *prog, const char *path)
6606 {
6607 	int i, err;
6608 
6609 	err = make_parent_dir(path);
6610 	if (err)
6611 		return err;
6612 
6613 	err = check_path(path);
6614 	if (err)
6615 		return err;
6616 
6617 	if (prog == NULL) {
6618 		pr_warn("invalid program pointer\n");
6619 		return -EINVAL;
6620 	}
6621 
6622 	if (prog->instances.nr <= 0) {
6623 		pr_warn("no instances of prog %s to pin\n",
6624 			   prog->section_name);
6625 		return -EINVAL;
6626 	}
6627 
6628 	if (prog->instances.nr == 1) {
6629 		/* don't create subdirs when pinning single instance */
6630 		return bpf_program__pin_instance(prog, path, 0);
6631 	}
6632 
6633 	for (i = 0; i < prog->instances.nr; i++) {
6634 		char buf[PATH_MAX];
6635 		int len;
6636 
6637 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
6638 		if (len < 0) {
6639 			err = -EINVAL;
6640 			goto err_unpin;
6641 		} else if (len >= PATH_MAX) {
6642 			err = -ENAMETOOLONG;
6643 			goto err_unpin;
6644 		}
6645 
6646 		err = bpf_program__pin_instance(prog, buf, i);
6647 		if (err)
6648 			goto err_unpin;
6649 	}
6650 
6651 	return 0;
6652 
6653 err_unpin:
6654 	for (i = i - 1; i >= 0; i--) {
6655 		char buf[PATH_MAX];
6656 		int len;
6657 
6658 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
6659 		if (len < 0)
6660 			continue;
6661 		else if (len >= PATH_MAX)
6662 			continue;
6663 
6664 		bpf_program__unpin_instance(prog, buf, i);
6665 	}
6666 
6667 	rmdir(path);
6668 
6669 	return err;
6670 }
6671 
6672 int bpf_program__unpin(struct bpf_program *prog, const char *path)
6673 {
6674 	int i, err;
6675 
6676 	err = check_path(path);
6677 	if (err)
6678 		return err;
6679 
6680 	if (prog == NULL) {
6681 		pr_warn("invalid program pointer\n");
6682 		return -EINVAL;
6683 	}
6684 
6685 	if (prog->instances.nr <= 0) {
6686 		pr_warn("no instances of prog %s to pin\n",
6687 			   prog->section_name);
6688 		return -EINVAL;
6689 	}
6690 
6691 	if (prog->instances.nr == 1) {
6692 		/* don't create subdirs when pinning single instance */
6693 		return bpf_program__unpin_instance(prog, path, 0);
6694 	}
6695 
6696 	for (i = 0; i < prog->instances.nr; i++) {
6697 		char buf[PATH_MAX];
6698 		int len;
6699 
6700 		len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
6701 		if (len < 0)
6702 			return -EINVAL;
6703 		else if (len >= PATH_MAX)
6704 			return -ENAMETOOLONG;
6705 
6706 		err = bpf_program__unpin_instance(prog, buf, i);
6707 		if (err)
6708 			return err;
6709 	}
6710 
6711 	err = rmdir(path);
6712 	if (err)
6713 		return -errno;
6714 
6715 	return 0;
6716 }
6717 
6718 int bpf_map__pin(struct bpf_map *map, const char *path)
6719 {
6720 	char *cp, errmsg[STRERR_BUFSIZE];
6721 	int err;
6722 
6723 	if (map == NULL) {
6724 		pr_warn("invalid map pointer\n");
6725 		return -EINVAL;
6726 	}
6727 
6728 	if (map->pin_path) {
6729 		if (path && strcmp(path, map->pin_path)) {
6730 			pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
6731 				bpf_map__name(map), map->pin_path, path);
6732 			return -EINVAL;
6733 		} else if (map->pinned) {
6734 			pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
6735 				 bpf_map__name(map), map->pin_path);
6736 			return 0;
6737 		}
6738 	} else {
6739 		if (!path) {
6740 			pr_warn("missing a path to pin map '%s' at\n",
6741 				bpf_map__name(map));
6742 			return -EINVAL;
6743 		} else if (map->pinned) {
6744 			pr_warn("map '%s' already pinned\n", bpf_map__name(map));
6745 			return -EEXIST;
6746 		}
6747 
6748 		map->pin_path = strdup(path);
6749 		if (!map->pin_path) {
6750 			err = -errno;
6751 			goto out_err;
6752 		}
6753 	}
6754 
6755 	err = make_parent_dir(map->pin_path);
6756 	if (err)
6757 		return err;
6758 
6759 	err = check_path(map->pin_path);
6760 	if (err)
6761 		return err;
6762 
6763 	if (bpf_obj_pin(map->fd, map->pin_path)) {
6764 		err = -errno;
6765 		goto out_err;
6766 	}
6767 
6768 	map->pinned = true;
6769 	pr_debug("pinned map '%s'\n", map->pin_path);
6770 
6771 	return 0;
6772 
6773 out_err:
6774 	cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
6775 	pr_warn("failed to pin map: %s\n", cp);
6776 	return err;
6777 }
6778 
6779 int bpf_map__unpin(struct bpf_map *map, const char *path)
6780 {
6781 	int err;
6782 
6783 	if (map == NULL) {
6784 		pr_warn("invalid map pointer\n");
6785 		return -EINVAL;
6786 	}
6787 
6788 	if (map->pin_path) {
6789 		if (path && strcmp(path, map->pin_path)) {
6790 			pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
6791 				bpf_map__name(map), map->pin_path, path);
6792 			return -EINVAL;
6793 		}
6794 		path = map->pin_path;
6795 	} else if (!path) {
6796 		pr_warn("no path to unpin map '%s' from\n",
6797 			bpf_map__name(map));
6798 		return -EINVAL;
6799 	}
6800 
6801 	err = check_path(path);
6802 	if (err)
6803 		return err;
6804 
6805 	err = unlink(path);
6806 	if (err != 0)
6807 		return -errno;
6808 
6809 	map->pinned = false;
6810 	pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
6811 
6812 	return 0;
6813 }
6814 
6815 int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
6816 {
6817 	char *new = NULL;
6818 
6819 	if (path) {
6820 		new = strdup(path);
6821 		if (!new)
6822 			return -errno;
6823 	}
6824 
6825 	free(map->pin_path);
6826 	map->pin_path = new;
6827 	return 0;
6828 }
6829 
6830 const char *bpf_map__get_pin_path(const struct bpf_map *map)
6831 {
6832 	return map->pin_path;
6833 }
6834 
6835 bool bpf_map__is_pinned(const struct bpf_map *map)
6836 {
6837 	return map->pinned;
6838 }
6839 
6840 int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
6841 {
6842 	struct bpf_map *map;
6843 	int err;
6844 
6845 	if (!obj)
6846 		return -ENOENT;
6847 
6848 	if (!obj->loaded) {
6849 		pr_warn("object not yet loaded; load it first\n");
6850 		return -ENOENT;
6851 	}
6852 
6853 	bpf_object__for_each_map(map, obj) {
6854 		char *pin_path = NULL;
6855 		char buf[PATH_MAX];
6856 
6857 		if (path) {
6858 			int len;
6859 
6860 			len = snprintf(buf, PATH_MAX, "%s/%s", path,
6861 				       bpf_map__name(map));
6862 			if (len < 0) {
6863 				err = -EINVAL;
6864 				goto err_unpin_maps;
6865 			} else if (len >= PATH_MAX) {
6866 				err = -ENAMETOOLONG;
6867 				goto err_unpin_maps;
6868 			}
6869 			pin_path = buf;
6870 		} else if (!map->pin_path) {
6871 			continue;
6872 		}
6873 
6874 		err = bpf_map__pin(map, pin_path);
6875 		if (err)
6876 			goto err_unpin_maps;
6877 	}
6878 
6879 	return 0;
6880 
6881 err_unpin_maps:
6882 	while ((map = bpf_map__prev(map, obj))) {
6883 		if (!map->pin_path)
6884 			continue;
6885 
6886 		bpf_map__unpin(map, NULL);
6887 	}
6888 
6889 	return err;
6890 }
6891 
6892 int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
6893 {
6894 	struct bpf_map *map;
6895 	int err;
6896 
6897 	if (!obj)
6898 		return -ENOENT;
6899 
6900 	bpf_object__for_each_map(map, obj) {
6901 		char *pin_path = NULL;
6902 		char buf[PATH_MAX];
6903 
6904 		if (path) {
6905 			int len;
6906 
6907 			len = snprintf(buf, PATH_MAX, "%s/%s", path,
6908 				       bpf_map__name(map));
6909 			if (len < 0)
6910 				return -EINVAL;
6911 			else if (len >= PATH_MAX)
6912 				return -ENAMETOOLONG;
6913 			pin_path = buf;
6914 		} else if (!map->pin_path) {
6915 			continue;
6916 		}
6917 
6918 		err = bpf_map__unpin(map, pin_path);
6919 		if (err)
6920 			return err;
6921 	}
6922 
6923 	return 0;
6924 }
6925 
6926 int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
6927 {
6928 	struct bpf_program *prog;
6929 	int err;
6930 
6931 	if (!obj)
6932 		return -ENOENT;
6933 
6934 	if (!obj->loaded) {
6935 		pr_warn("object not yet loaded; load it first\n");
6936 		return -ENOENT;
6937 	}
6938 
6939 	bpf_object__for_each_program(prog, obj) {
6940 		char buf[PATH_MAX];
6941 		int len;
6942 
6943 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
6944 			       prog->pin_name);
6945 		if (len < 0) {
6946 			err = -EINVAL;
6947 			goto err_unpin_programs;
6948 		} else if (len >= PATH_MAX) {
6949 			err = -ENAMETOOLONG;
6950 			goto err_unpin_programs;
6951 		}
6952 
6953 		err = bpf_program__pin(prog, buf);
6954 		if (err)
6955 			goto err_unpin_programs;
6956 	}
6957 
6958 	return 0;
6959 
6960 err_unpin_programs:
6961 	while ((prog = bpf_program__prev(prog, obj))) {
6962 		char buf[PATH_MAX];
6963 		int len;
6964 
6965 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
6966 			       prog->pin_name);
6967 		if (len < 0)
6968 			continue;
6969 		else if (len >= PATH_MAX)
6970 			continue;
6971 
6972 		bpf_program__unpin(prog, buf);
6973 	}
6974 
6975 	return err;
6976 }
6977 
6978 int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
6979 {
6980 	struct bpf_program *prog;
6981 	int err;
6982 
6983 	if (!obj)
6984 		return -ENOENT;
6985 
6986 	bpf_object__for_each_program(prog, obj) {
6987 		char buf[PATH_MAX];
6988 		int len;
6989 
6990 		len = snprintf(buf, PATH_MAX, "%s/%s", path,
6991 			       prog->pin_name);
6992 		if (len < 0)
6993 			return -EINVAL;
6994 		else if (len >= PATH_MAX)
6995 			return -ENAMETOOLONG;
6996 
6997 		err = bpf_program__unpin(prog, buf);
6998 		if (err)
6999 			return err;
7000 	}
7001 
7002 	return 0;
7003 }
7004 
7005 int bpf_object__pin(struct bpf_object *obj, const char *path)
7006 {
7007 	int err;
7008 
7009 	err = bpf_object__pin_maps(obj, path);
7010 	if (err)
7011 		return err;
7012 
7013 	err = bpf_object__pin_programs(obj, path);
7014 	if (err) {
7015 		bpf_object__unpin_maps(obj, path);
7016 		return err;
7017 	}
7018 
7019 	return 0;
7020 }
7021 
7022 static void bpf_map__destroy(struct bpf_map *map)
7023 {
7024 	if (map->clear_priv)
7025 		map->clear_priv(map, map->priv);
7026 	map->priv = NULL;
7027 	map->clear_priv = NULL;
7028 
7029 	if (map->inner_map) {
7030 		bpf_map__destroy(map->inner_map);
7031 		zfree(&map->inner_map);
7032 	}
7033 
7034 	zfree(&map->init_slots);
7035 	map->init_slots_sz = 0;
7036 
7037 	if (map->mmaped) {
7038 		munmap(map->mmaped, bpf_map_mmap_sz(map));
7039 		map->mmaped = NULL;
7040 	}
7041 
7042 	if (map->st_ops) {
7043 		zfree(&map->st_ops->data);
7044 		zfree(&map->st_ops->progs);
7045 		zfree(&map->st_ops->kern_func_off);
7046 		zfree(&map->st_ops);
7047 	}
7048 
7049 	zfree(&map->name);
7050 	zfree(&map->pin_path);
7051 
7052 	if (map->fd >= 0)
7053 		zclose(map->fd);
7054 }
7055 
7056 void bpf_object__close(struct bpf_object *obj)
7057 {
7058 	size_t i;
7059 
7060 	if (IS_ERR_OR_NULL(obj))
7061 		return;
7062 
7063 	if (obj->clear_priv)
7064 		obj->clear_priv(obj, obj->priv);
7065 
7066 	bpf_object__elf_finish(obj);
7067 	bpf_object__unload(obj);
7068 	btf__free(obj->btf);
7069 	btf_ext__free(obj->btf_ext);
7070 
7071 	for (i = 0; i < obj->nr_maps; i++)
7072 		bpf_map__destroy(&obj->maps[i]);
7073 
7074 	zfree(&obj->kconfig);
7075 	zfree(&obj->externs);
7076 	obj->nr_extern = 0;
7077 
7078 	zfree(&obj->maps);
7079 	obj->nr_maps = 0;
7080 
7081 	if (obj->programs && obj->nr_programs) {
7082 		for (i = 0; i < obj->nr_programs; i++)
7083 			bpf_program__exit(&obj->programs[i]);
7084 	}
7085 	zfree(&obj->programs);
7086 
7087 	list_del(&obj->list);
7088 	free(obj);
7089 }
7090 
7091 struct bpf_object *
7092 bpf_object__next(struct bpf_object *prev)
7093 {
7094 	struct bpf_object *next;
7095 
7096 	if (!prev)
7097 		next = list_first_entry(&bpf_objects_list,
7098 					struct bpf_object,
7099 					list);
7100 	else
7101 		next = list_next_entry(prev, list);
7102 
7103 	/* Empty list is noticed here so don't need checking on entry. */
7104 	if (&next->list == &bpf_objects_list)
7105 		return NULL;
7106 
7107 	return next;
7108 }
7109 
7110 const char *bpf_object__name(const struct bpf_object *obj)
7111 {
7112 	return obj ? obj->name : ERR_PTR(-EINVAL);
7113 }
7114 
7115 unsigned int bpf_object__kversion(const struct bpf_object *obj)
7116 {
7117 	return obj ? obj->kern_version : 0;
7118 }
7119 
7120 struct btf *bpf_object__btf(const struct bpf_object *obj)
7121 {
7122 	return obj ? obj->btf : NULL;
7123 }
7124 
7125 int bpf_object__btf_fd(const struct bpf_object *obj)
7126 {
7127 	return obj->btf ? btf__fd(obj->btf) : -1;
7128 }
7129 
7130 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
7131 			 bpf_object_clear_priv_t clear_priv)
7132 {
7133 	if (obj->priv && obj->clear_priv)
7134 		obj->clear_priv(obj, obj->priv);
7135 
7136 	obj->priv = priv;
7137 	obj->clear_priv = clear_priv;
7138 	return 0;
7139 }
7140 
7141 void *bpf_object__priv(const struct bpf_object *obj)
7142 {
7143 	return obj ? obj->priv : ERR_PTR(-EINVAL);
7144 }
7145 
7146 static struct bpf_program *
7147 __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
7148 		    bool forward)
7149 {
7150 	size_t nr_programs = obj->nr_programs;
7151 	ssize_t idx;
7152 
7153 	if (!nr_programs)
7154 		return NULL;
7155 
7156 	if (!p)
7157 		/* Iter from the beginning */
7158 		return forward ? &obj->programs[0] :
7159 			&obj->programs[nr_programs - 1];
7160 
7161 	if (p->obj != obj) {
7162 		pr_warn("error: program handler doesn't match object\n");
7163 		return NULL;
7164 	}
7165 
7166 	idx = (p - obj->programs) + (forward ? 1 : -1);
7167 	if (idx >= obj->nr_programs || idx < 0)
7168 		return NULL;
7169 	return &obj->programs[idx];
7170 }
7171 
7172 struct bpf_program *
7173 bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
7174 {
7175 	struct bpf_program *prog = prev;
7176 
7177 	do {
7178 		prog = __bpf_program__iter(prog, obj, true);
7179 	} while (prog && bpf_program__is_function_storage(prog, obj));
7180 
7181 	return prog;
7182 }
7183 
7184 struct bpf_program *
7185 bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
7186 {
7187 	struct bpf_program *prog = next;
7188 
7189 	do {
7190 		prog = __bpf_program__iter(prog, obj, false);
7191 	} while (prog && bpf_program__is_function_storage(prog, obj));
7192 
7193 	return prog;
7194 }
7195 
7196 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
7197 			  bpf_program_clear_priv_t clear_priv)
7198 {
7199 	if (prog->priv && prog->clear_priv)
7200 		prog->clear_priv(prog, prog->priv);
7201 
7202 	prog->priv = priv;
7203 	prog->clear_priv = clear_priv;
7204 	return 0;
7205 }
7206 
7207 void *bpf_program__priv(const struct bpf_program *prog)
7208 {
7209 	return prog ? prog->priv : ERR_PTR(-EINVAL);
7210 }
7211 
7212 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
7213 {
7214 	prog->prog_ifindex = ifindex;
7215 }
7216 
7217 const char *bpf_program__name(const struct bpf_program *prog)
7218 {
7219 	return prog->name;
7220 }
7221 
7222 const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
7223 {
7224 	const char *title;
7225 
7226 	title = prog->section_name;
7227 	if (needs_copy) {
7228 		title = strdup(title);
7229 		if (!title) {
7230 			pr_warn("failed to strdup program title\n");
7231 			return ERR_PTR(-ENOMEM);
7232 		}
7233 	}
7234 
7235 	return title;
7236 }
7237 
7238 bool bpf_program__autoload(const struct bpf_program *prog)
7239 {
7240 	return prog->load;
7241 }
7242 
7243 int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
7244 {
7245 	if (prog->obj->loaded)
7246 		return -EINVAL;
7247 
7248 	prog->load = autoload;
7249 	return 0;
7250 }
7251 
7252 int bpf_program__fd(const struct bpf_program *prog)
7253 {
7254 	return bpf_program__nth_fd(prog, 0);
7255 }
7256 
7257 size_t bpf_program__size(const struct bpf_program *prog)
7258 {
7259 	return prog->insns_cnt * sizeof(struct bpf_insn);
7260 }
7261 
7262 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
7263 			  bpf_program_prep_t prep)
7264 {
7265 	int *instances_fds;
7266 
7267 	if (nr_instances <= 0 || !prep)
7268 		return -EINVAL;
7269 
7270 	if (prog->instances.nr > 0 || prog->instances.fds) {
7271 		pr_warn("Can't set pre-processor after loading\n");
7272 		return -EINVAL;
7273 	}
7274 
7275 	instances_fds = malloc(sizeof(int) * nr_instances);
7276 	if (!instances_fds) {
7277 		pr_warn("alloc memory failed for fds\n");
7278 		return -ENOMEM;
7279 	}
7280 
7281 	/* fill all fd with -1 */
7282 	memset(instances_fds, -1, sizeof(int) * nr_instances);
7283 
7284 	prog->instances.nr = nr_instances;
7285 	prog->instances.fds = instances_fds;
7286 	prog->preprocessor = prep;
7287 	return 0;
7288 }
7289 
7290 int bpf_program__nth_fd(const struct bpf_program *prog, int n)
7291 {
7292 	int fd;
7293 
7294 	if (!prog)
7295 		return -EINVAL;
7296 
7297 	if (n >= prog->instances.nr || n < 0) {
7298 		pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
7299 			n, prog->section_name, prog->instances.nr);
7300 		return -EINVAL;
7301 	}
7302 
7303 	fd = prog->instances.fds[n];
7304 	if (fd < 0) {
7305 		pr_warn("%dth instance of program '%s' is invalid\n",
7306 			n, prog->section_name);
7307 		return -ENOENT;
7308 	}
7309 
7310 	return fd;
7311 }
7312 
7313 enum bpf_prog_type bpf_program__get_type(struct bpf_program *prog)
7314 {
7315 	return prog->type;
7316 }
7317 
7318 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
7319 {
7320 	prog->type = type;
7321 }
7322 
7323 static bool bpf_program__is_type(const struct bpf_program *prog,
7324 				 enum bpf_prog_type type)
7325 {
7326 	return prog ? (prog->type == type) : false;
7327 }
7328 
7329 #define BPF_PROG_TYPE_FNS(NAME, TYPE)				\
7330 int bpf_program__set_##NAME(struct bpf_program *prog)		\
7331 {								\
7332 	if (!prog)						\
7333 		return -EINVAL;					\
7334 	bpf_program__set_type(prog, TYPE);			\
7335 	return 0;						\
7336 }								\
7337 								\
7338 bool bpf_program__is_##NAME(const struct bpf_program *prog)	\
7339 {								\
7340 	return bpf_program__is_type(prog, TYPE);		\
7341 }								\
7342 
7343 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
7344 BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
7345 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
7346 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
7347 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
7348 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
7349 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
7350 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
7351 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
7352 BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING);
7353 BPF_PROG_TYPE_FNS(struct_ops, BPF_PROG_TYPE_STRUCT_OPS);
7354 BPF_PROG_TYPE_FNS(extension, BPF_PROG_TYPE_EXT);
7355 BPF_PROG_TYPE_FNS(sk_lookup, BPF_PROG_TYPE_SK_LOOKUP);
7356 
7357 enum bpf_attach_type
7358 bpf_program__get_expected_attach_type(struct bpf_program *prog)
7359 {
7360 	return prog->expected_attach_type;
7361 }
7362 
7363 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
7364 					   enum bpf_attach_type type)
7365 {
7366 	prog->expected_attach_type = type;
7367 }
7368 
7369 #define BPF_PROG_SEC_IMPL(string, ptype, eatype, eatype_optional,	    \
7370 			  attachable, attach_btf)			    \
7371 	{								    \
7372 		.sec = string,						    \
7373 		.len = sizeof(string) - 1,				    \
7374 		.prog_type = ptype,					    \
7375 		.expected_attach_type = eatype,				    \
7376 		.is_exp_attach_type_optional = eatype_optional,		    \
7377 		.is_attachable = attachable,				    \
7378 		.is_attach_btf = attach_btf,				    \
7379 	}
7380 
7381 /* Programs that can NOT be attached. */
7382 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_IMPL(string, ptype, 0, 0, 0, 0)
7383 
7384 /* Programs that can be attached. */
7385 #define BPF_APROG_SEC(string, ptype, atype) \
7386 	BPF_PROG_SEC_IMPL(string, ptype, atype, true, 1, 0)
7387 
7388 /* Programs that must specify expected attach type at load time. */
7389 #define BPF_EAPROG_SEC(string, ptype, eatype) \
7390 	BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 1, 0)
7391 
7392 /* Programs that use BTF to identify attach point */
7393 #define BPF_PROG_BTF(string, ptype, eatype) \
7394 	BPF_PROG_SEC_IMPL(string, ptype, eatype, false, 0, 1)
7395 
7396 /* Programs that can be attached but attach type can't be identified by section
7397  * name. Kept for backward compatibility.
7398  */
7399 #define BPF_APROG_COMPAT(string, ptype) BPF_PROG_SEC(string, ptype)
7400 
7401 #define SEC_DEF(sec_pfx, ptype, ...) {					    \
7402 	.sec = sec_pfx,							    \
7403 	.len = sizeof(sec_pfx) - 1,					    \
7404 	.prog_type = BPF_PROG_TYPE_##ptype,				    \
7405 	__VA_ARGS__							    \
7406 }
7407 
7408 static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
7409 				      struct bpf_program *prog);
7410 static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
7411 				  struct bpf_program *prog);
7412 static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
7413 				      struct bpf_program *prog);
7414 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
7415 				     struct bpf_program *prog);
7416 static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
7417 				   struct bpf_program *prog);
7418 static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
7419 				    struct bpf_program *prog);
7420 
7421 static const struct bpf_sec_def section_defs[] = {
7422 	BPF_PROG_SEC("socket",			BPF_PROG_TYPE_SOCKET_FILTER),
7423 	BPF_PROG_SEC("sk_reuseport",		BPF_PROG_TYPE_SK_REUSEPORT),
7424 	SEC_DEF("kprobe/", KPROBE,
7425 		.attach_fn = attach_kprobe),
7426 	BPF_PROG_SEC("uprobe/",			BPF_PROG_TYPE_KPROBE),
7427 	SEC_DEF("kretprobe/", KPROBE,
7428 		.attach_fn = attach_kprobe),
7429 	BPF_PROG_SEC("uretprobe/",		BPF_PROG_TYPE_KPROBE),
7430 	BPF_PROG_SEC("classifier",		BPF_PROG_TYPE_SCHED_CLS),
7431 	BPF_PROG_SEC("action",			BPF_PROG_TYPE_SCHED_ACT),
7432 	SEC_DEF("tracepoint/", TRACEPOINT,
7433 		.attach_fn = attach_tp),
7434 	SEC_DEF("tp/", TRACEPOINT,
7435 		.attach_fn = attach_tp),
7436 	SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT,
7437 		.attach_fn = attach_raw_tp),
7438 	SEC_DEF("raw_tp/", RAW_TRACEPOINT,
7439 		.attach_fn = attach_raw_tp),
7440 	SEC_DEF("tp_btf/", TRACING,
7441 		.expected_attach_type = BPF_TRACE_RAW_TP,
7442 		.is_attach_btf = true,
7443 		.attach_fn = attach_trace),
7444 	SEC_DEF("fentry/", TRACING,
7445 		.expected_attach_type = BPF_TRACE_FENTRY,
7446 		.is_attach_btf = true,
7447 		.attach_fn = attach_trace),
7448 	SEC_DEF("fmod_ret/", TRACING,
7449 		.expected_attach_type = BPF_MODIFY_RETURN,
7450 		.is_attach_btf = true,
7451 		.attach_fn = attach_trace),
7452 	SEC_DEF("fexit/", TRACING,
7453 		.expected_attach_type = BPF_TRACE_FEXIT,
7454 		.is_attach_btf = true,
7455 		.attach_fn = attach_trace),
7456 	SEC_DEF("freplace/", EXT,
7457 		.is_attach_btf = true,
7458 		.attach_fn = attach_trace),
7459 	SEC_DEF("lsm/", LSM,
7460 		.is_attach_btf = true,
7461 		.expected_attach_type = BPF_LSM_MAC,
7462 		.attach_fn = attach_lsm),
7463 	SEC_DEF("iter/", TRACING,
7464 		.expected_attach_type = BPF_TRACE_ITER,
7465 		.is_attach_btf = true,
7466 		.attach_fn = attach_iter),
7467 	BPF_EAPROG_SEC("xdp_devmap/",		BPF_PROG_TYPE_XDP,
7468 						BPF_XDP_DEVMAP),
7469 	BPF_EAPROG_SEC("xdp_cpumap/",		BPF_PROG_TYPE_XDP,
7470 						BPF_XDP_CPUMAP),
7471 	BPF_EAPROG_SEC("xdp",			BPF_PROG_TYPE_XDP,
7472 						BPF_XDP),
7473 	BPF_PROG_SEC("perf_event",		BPF_PROG_TYPE_PERF_EVENT),
7474 	BPF_PROG_SEC("lwt_in",			BPF_PROG_TYPE_LWT_IN),
7475 	BPF_PROG_SEC("lwt_out",			BPF_PROG_TYPE_LWT_OUT),
7476 	BPF_PROG_SEC("lwt_xmit",		BPF_PROG_TYPE_LWT_XMIT),
7477 	BPF_PROG_SEC("lwt_seg6local",		BPF_PROG_TYPE_LWT_SEG6LOCAL),
7478 	BPF_APROG_SEC("cgroup_skb/ingress",	BPF_PROG_TYPE_CGROUP_SKB,
7479 						BPF_CGROUP_INET_INGRESS),
7480 	BPF_APROG_SEC("cgroup_skb/egress",	BPF_PROG_TYPE_CGROUP_SKB,
7481 						BPF_CGROUP_INET_EGRESS),
7482 	BPF_APROG_COMPAT("cgroup/skb",		BPF_PROG_TYPE_CGROUP_SKB),
7483 	BPF_EAPROG_SEC("cgroup/sock_create",	BPF_PROG_TYPE_CGROUP_SOCK,
7484 						BPF_CGROUP_INET_SOCK_CREATE),
7485 	BPF_EAPROG_SEC("cgroup/sock_release",	BPF_PROG_TYPE_CGROUP_SOCK,
7486 						BPF_CGROUP_INET_SOCK_RELEASE),
7487 	BPF_APROG_SEC("cgroup/sock",		BPF_PROG_TYPE_CGROUP_SOCK,
7488 						BPF_CGROUP_INET_SOCK_CREATE),
7489 	BPF_EAPROG_SEC("cgroup/post_bind4",	BPF_PROG_TYPE_CGROUP_SOCK,
7490 						BPF_CGROUP_INET4_POST_BIND),
7491 	BPF_EAPROG_SEC("cgroup/post_bind6",	BPF_PROG_TYPE_CGROUP_SOCK,
7492 						BPF_CGROUP_INET6_POST_BIND),
7493 	BPF_APROG_SEC("cgroup/dev",		BPF_PROG_TYPE_CGROUP_DEVICE,
7494 						BPF_CGROUP_DEVICE),
7495 	BPF_APROG_SEC("sockops",		BPF_PROG_TYPE_SOCK_OPS,
7496 						BPF_CGROUP_SOCK_OPS),
7497 	BPF_APROG_SEC("sk_skb/stream_parser",	BPF_PROG_TYPE_SK_SKB,
7498 						BPF_SK_SKB_STREAM_PARSER),
7499 	BPF_APROG_SEC("sk_skb/stream_verdict",	BPF_PROG_TYPE_SK_SKB,
7500 						BPF_SK_SKB_STREAM_VERDICT),
7501 	BPF_APROG_COMPAT("sk_skb",		BPF_PROG_TYPE_SK_SKB),
7502 	BPF_APROG_SEC("sk_msg",			BPF_PROG_TYPE_SK_MSG,
7503 						BPF_SK_MSG_VERDICT),
7504 	BPF_APROG_SEC("lirc_mode2",		BPF_PROG_TYPE_LIRC_MODE2,
7505 						BPF_LIRC_MODE2),
7506 	BPF_APROG_SEC("flow_dissector",		BPF_PROG_TYPE_FLOW_DISSECTOR,
7507 						BPF_FLOW_DISSECTOR),
7508 	BPF_EAPROG_SEC("cgroup/bind4",		BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7509 						BPF_CGROUP_INET4_BIND),
7510 	BPF_EAPROG_SEC("cgroup/bind6",		BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7511 						BPF_CGROUP_INET6_BIND),
7512 	BPF_EAPROG_SEC("cgroup/connect4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7513 						BPF_CGROUP_INET4_CONNECT),
7514 	BPF_EAPROG_SEC("cgroup/connect6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7515 						BPF_CGROUP_INET6_CONNECT),
7516 	BPF_EAPROG_SEC("cgroup/sendmsg4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7517 						BPF_CGROUP_UDP4_SENDMSG),
7518 	BPF_EAPROG_SEC("cgroup/sendmsg6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7519 						BPF_CGROUP_UDP6_SENDMSG),
7520 	BPF_EAPROG_SEC("cgroup/recvmsg4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7521 						BPF_CGROUP_UDP4_RECVMSG),
7522 	BPF_EAPROG_SEC("cgroup/recvmsg6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7523 						BPF_CGROUP_UDP6_RECVMSG),
7524 	BPF_EAPROG_SEC("cgroup/getpeername4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7525 						BPF_CGROUP_INET4_GETPEERNAME),
7526 	BPF_EAPROG_SEC("cgroup/getpeername6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7527 						BPF_CGROUP_INET6_GETPEERNAME),
7528 	BPF_EAPROG_SEC("cgroup/getsockname4",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7529 						BPF_CGROUP_INET4_GETSOCKNAME),
7530 	BPF_EAPROG_SEC("cgroup/getsockname6",	BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
7531 						BPF_CGROUP_INET6_GETSOCKNAME),
7532 	BPF_EAPROG_SEC("cgroup/sysctl",		BPF_PROG_TYPE_CGROUP_SYSCTL,
7533 						BPF_CGROUP_SYSCTL),
7534 	BPF_EAPROG_SEC("cgroup/getsockopt",	BPF_PROG_TYPE_CGROUP_SOCKOPT,
7535 						BPF_CGROUP_GETSOCKOPT),
7536 	BPF_EAPROG_SEC("cgroup/setsockopt",	BPF_PROG_TYPE_CGROUP_SOCKOPT,
7537 						BPF_CGROUP_SETSOCKOPT),
7538 	BPF_PROG_SEC("struct_ops",		BPF_PROG_TYPE_STRUCT_OPS),
7539 	BPF_EAPROG_SEC("sk_lookup/",		BPF_PROG_TYPE_SK_LOOKUP,
7540 						BPF_SK_LOOKUP),
7541 };
7542 
7543 #undef BPF_PROG_SEC_IMPL
7544 #undef BPF_PROG_SEC
7545 #undef BPF_APROG_SEC
7546 #undef BPF_EAPROG_SEC
7547 #undef BPF_APROG_COMPAT
7548 #undef SEC_DEF
7549 
7550 #define MAX_TYPE_NAME_SIZE 32
7551 
7552 static const struct bpf_sec_def *find_sec_def(const char *sec_name)
7553 {
7554 	int i, n = ARRAY_SIZE(section_defs);
7555 
7556 	for (i = 0; i < n; i++) {
7557 		if (strncmp(sec_name,
7558 			    section_defs[i].sec, section_defs[i].len))
7559 			continue;
7560 		return &section_defs[i];
7561 	}
7562 	return NULL;
7563 }
7564 
7565 static char *libbpf_get_type_names(bool attach_type)
7566 {
7567 	int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
7568 	char *buf;
7569 
7570 	buf = malloc(len);
7571 	if (!buf)
7572 		return NULL;
7573 
7574 	buf[0] = '\0';
7575 	/* Forge string buf with all available names */
7576 	for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
7577 		if (attach_type && !section_defs[i].is_attachable)
7578 			continue;
7579 
7580 		if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
7581 			free(buf);
7582 			return NULL;
7583 		}
7584 		strcat(buf, " ");
7585 		strcat(buf, section_defs[i].sec);
7586 	}
7587 
7588 	return buf;
7589 }
7590 
7591 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
7592 			     enum bpf_attach_type *expected_attach_type)
7593 {
7594 	const struct bpf_sec_def *sec_def;
7595 	char *type_names;
7596 
7597 	if (!name)
7598 		return -EINVAL;
7599 
7600 	sec_def = find_sec_def(name);
7601 	if (sec_def) {
7602 		*prog_type = sec_def->prog_type;
7603 		*expected_attach_type = sec_def->expected_attach_type;
7604 		return 0;
7605 	}
7606 
7607 	pr_debug("failed to guess program type from ELF section '%s'\n", name);
7608 	type_names = libbpf_get_type_names(false);
7609 	if (type_names != NULL) {
7610 		pr_debug("supported section(type) names are:%s\n", type_names);
7611 		free(type_names);
7612 	}
7613 
7614 	return -ESRCH;
7615 }
7616 
7617 static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
7618 						     size_t offset)
7619 {
7620 	struct bpf_map *map;
7621 	size_t i;
7622 
7623 	for (i = 0; i < obj->nr_maps; i++) {
7624 		map = &obj->maps[i];
7625 		if (!bpf_map__is_struct_ops(map))
7626 			continue;
7627 		if (map->sec_offset <= offset &&
7628 		    offset - map->sec_offset < map->def.value_size)
7629 			return map;
7630 	}
7631 
7632 	return NULL;
7633 }
7634 
7635 /* Collect the reloc from ELF and populate the st_ops->progs[] */
7636 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
7637 					    GElf_Shdr *shdr, Elf_Data *data)
7638 {
7639 	const struct btf_member *member;
7640 	struct bpf_struct_ops *st_ops;
7641 	struct bpf_program *prog;
7642 	unsigned int shdr_idx;
7643 	const struct btf *btf;
7644 	struct bpf_map *map;
7645 	Elf_Data *symbols;
7646 	unsigned int moff;
7647 	const char *name;
7648 	__u32 member_idx;
7649 	GElf_Sym sym;
7650 	GElf_Rel rel;
7651 	int i, nrels;
7652 
7653 	symbols = obj->efile.symbols;
7654 	btf = obj->btf;
7655 	nrels = shdr->sh_size / shdr->sh_entsize;
7656 	for (i = 0; i < nrels; i++) {
7657 		if (!gelf_getrel(data, i, &rel)) {
7658 			pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
7659 			return -LIBBPF_ERRNO__FORMAT;
7660 		}
7661 
7662 		if (!gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym)) {
7663 			pr_warn("struct_ops reloc: symbol %zx not found\n",
7664 				(size_t)GELF_R_SYM(rel.r_info));
7665 			return -LIBBPF_ERRNO__FORMAT;
7666 		}
7667 
7668 		name = elf_strptr(obj->efile.elf, obj->efile.strtabidx,
7669 				  sym.st_name) ? : "<?>";
7670 		map = find_struct_ops_map_by_offset(obj, rel.r_offset);
7671 		if (!map) {
7672 			pr_warn("struct_ops reloc: cannot find map at rel.r_offset %zu\n",
7673 				(size_t)rel.r_offset);
7674 			return -EINVAL;
7675 		}
7676 
7677 		moff = rel.r_offset - map->sec_offset;
7678 		shdr_idx = sym.st_shndx;
7679 		st_ops = map->st_ops;
7680 		pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel.r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
7681 			 map->name,
7682 			 (long long)(rel.r_info >> 32),
7683 			 (long long)sym.st_value,
7684 			 shdr_idx, (size_t)rel.r_offset,
7685 			 map->sec_offset, sym.st_name, name);
7686 
7687 		if (shdr_idx >= SHN_LORESERVE) {
7688 			pr_warn("struct_ops reloc %s: rel.r_offset %zu shdr_idx %u unsupported non-static function\n",
7689 				map->name, (size_t)rel.r_offset, shdr_idx);
7690 			return -LIBBPF_ERRNO__RELOC;
7691 		}
7692 
7693 		member = find_member_by_offset(st_ops->type, moff * 8);
7694 		if (!member) {
7695 			pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
7696 				map->name, moff);
7697 			return -EINVAL;
7698 		}
7699 		member_idx = member - btf_members(st_ops->type);
7700 		name = btf__name_by_offset(btf, member->name_off);
7701 
7702 		if (!resolve_func_ptr(btf, member->type, NULL)) {
7703 			pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
7704 				map->name, name);
7705 			return -EINVAL;
7706 		}
7707 
7708 		prog = bpf_object__find_prog_by_idx(obj, shdr_idx);
7709 		if (!prog) {
7710 			pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
7711 				map->name, shdr_idx, name);
7712 			return -EINVAL;
7713 		}
7714 
7715 		if (prog->type == BPF_PROG_TYPE_UNSPEC) {
7716 			const struct bpf_sec_def *sec_def;
7717 
7718 			sec_def = find_sec_def(prog->section_name);
7719 			if (sec_def &&
7720 			    sec_def->prog_type != BPF_PROG_TYPE_STRUCT_OPS) {
7721 				/* for pr_warn */
7722 				prog->type = sec_def->prog_type;
7723 				goto invalid_prog;
7724 			}
7725 
7726 			prog->type = BPF_PROG_TYPE_STRUCT_OPS;
7727 			prog->attach_btf_id = st_ops->type_id;
7728 			prog->expected_attach_type = member_idx;
7729 		} else if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
7730 			   prog->attach_btf_id != st_ops->type_id ||
7731 			   prog->expected_attach_type != member_idx) {
7732 			goto invalid_prog;
7733 		}
7734 		st_ops->progs[member_idx] = prog;
7735 	}
7736 
7737 	return 0;
7738 
7739 invalid_prog:
7740 	pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n",
7741 		map->name, prog->name, prog->section_name, prog->type,
7742 		prog->attach_btf_id, prog->expected_attach_type, name);
7743 	return -EINVAL;
7744 }
7745 
7746 #define BTF_TRACE_PREFIX "btf_trace_"
7747 #define BTF_LSM_PREFIX "bpf_lsm_"
7748 #define BTF_ITER_PREFIX "bpf_iter_"
7749 #define BTF_MAX_NAME_SIZE 128
7750 
7751 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
7752 				   const char *name, __u32 kind)
7753 {
7754 	char btf_type_name[BTF_MAX_NAME_SIZE];
7755 	int ret;
7756 
7757 	ret = snprintf(btf_type_name, sizeof(btf_type_name),
7758 		       "%s%s", prefix, name);
7759 	/* snprintf returns the number of characters written excluding the
7760 	 * the terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
7761 	 * indicates truncation.
7762 	 */
7763 	if (ret < 0 || ret >= sizeof(btf_type_name))
7764 		return -ENAMETOOLONG;
7765 	return btf__find_by_name_kind(btf, btf_type_name, kind);
7766 }
7767 
7768 static inline int __find_vmlinux_btf_id(struct btf *btf, const char *name,
7769 					enum bpf_attach_type attach_type)
7770 {
7771 	int err;
7772 
7773 	if (attach_type == BPF_TRACE_RAW_TP)
7774 		err = find_btf_by_prefix_kind(btf, BTF_TRACE_PREFIX, name,
7775 					      BTF_KIND_TYPEDEF);
7776 	else if (attach_type == BPF_LSM_MAC)
7777 		err = find_btf_by_prefix_kind(btf, BTF_LSM_PREFIX, name,
7778 					      BTF_KIND_FUNC);
7779 	else if (attach_type == BPF_TRACE_ITER)
7780 		err = find_btf_by_prefix_kind(btf, BTF_ITER_PREFIX, name,
7781 					      BTF_KIND_FUNC);
7782 	else
7783 		err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
7784 
7785 	if (err <= 0)
7786 		pr_warn("%s is not found in vmlinux BTF\n", name);
7787 
7788 	return err;
7789 }
7790 
7791 int libbpf_find_vmlinux_btf_id(const char *name,
7792 			       enum bpf_attach_type attach_type)
7793 {
7794 	struct btf *btf;
7795 	int err;
7796 
7797 	btf = libbpf_find_kernel_btf();
7798 	if (IS_ERR(btf)) {
7799 		pr_warn("vmlinux BTF is not found\n");
7800 		return -EINVAL;
7801 	}
7802 
7803 	err = __find_vmlinux_btf_id(btf, name, attach_type);
7804 	btf__free(btf);
7805 	return err;
7806 }
7807 
7808 static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
7809 {
7810 	struct bpf_prog_info_linear *info_linear;
7811 	struct bpf_prog_info *info;
7812 	struct btf *btf = NULL;
7813 	int err = -EINVAL;
7814 
7815 	info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0);
7816 	if (IS_ERR_OR_NULL(info_linear)) {
7817 		pr_warn("failed get_prog_info_linear for FD %d\n",
7818 			attach_prog_fd);
7819 		return -EINVAL;
7820 	}
7821 	info = &info_linear->info;
7822 	if (!info->btf_id) {
7823 		pr_warn("The target program doesn't have BTF\n");
7824 		goto out;
7825 	}
7826 	if (btf__get_from_id(info->btf_id, &btf)) {
7827 		pr_warn("Failed to get BTF of the program\n");
7828 		goto out;
7829 	}
7830 	err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
7831 	btf__free(btf);
7832 	if (err <= 0) {
7833 		pr_warn("%s is not found in prog's BTF\n", name);
7834 		goto out;
7835 	}
7836 out:
7837 	free(info_linear);
7838 	return err;
7839 }
7840 
7841 static int libbpf_find_attach_btf_id(struct bpf_program *prog)
7842 {
7843 	enum bpf_attach_type attach_type = prog->expected_attach_type;
7844 	__u32 attach_prog_fd = prog->attach_prog_fd;
7845 	const char *name = prog->section_name;
7846 	int i, err;
7847 
7848 	if (!name)
7849 		return -EINVAL;
7850 
7851 	for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
7852 		if (!section_defs[i].is_attach_btf)
7853 			continue;
7854 		if (strncmp(name, section_defs[i].sec, section_defs[i].len))
7855 			continue;
7856 		if (attach_prog_fd)
7857 			err = libbpf_find_prog_btf_id(name + section_defs[i].len,
7858 						      attach_prog_fd);
7859 		else
7860 			err = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
7861 						    name + section_defs[i].len,
7862 						    attach_type);
7863 		return err;
7864 	}
7865 	pr_warn("failed to identify btf_id based on ELF section name '%s'\n", name);
7866 	return -ESRCH;
7867 }
7868 
7869 int libbpf_attach_type_by_name(const char *name,
7870 			       enum bpf_attach_type *attach_type)
7871 {
7872 	char *type_names;
7873 	int i;
7874 
7875 	if (!name)
7876 		return -EINVAL;
7877 
7878 	for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
7879 		if (strncmp(name, section_defs[i].sec, section_defs[i].len))
7880 			continue;
7881 		if (!section_defs[i].is_attachable)
7882 			return -EINVAL;
7883 		*attach_type = section_defs[i].expected_attach_type;
7884 		return 0;
7885 	}
7886 	pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
7887 	type_names = libbpf_get_type_names(true);
7888 	if (type_names != NULL) {
7889 		pr_debug("attachable section(type) names are:%s\n", type_names);
7890 		free(type_names);
7891 	}
7892 
7893 	return -EINVAL;
7894 }
7895 
7896 int bpf_map__fd(const struct bpf_map *map)
7897 {
7898 	return map ? map->fd : -EINVAL;
7899 }
7900 
7901 const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
7902 {
7903 	return map ? &map->def : ERR_PTR(-EINVAL);
7904 }
7905 
7906 const char *bpf_map__name(const struct bpf_map *map)
7907 {
7908 	return map ? map->name : NULL;
7909 }
7910 
7911 enum bpf_map_type bpf_map__type(const struct bpf_map *map)
7912 {
7913 	return map->def.type;
7914 }
7915 
7916 int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
7917 {
7918 	if (map->fd >= 0)
7919 		return -EBUSY;
7920 	map->def.type = type;
7921 	return 0;
7922 }
7923 
7924 __u32 bpf_map__map_flags(const struct bpf_map *map)
7925 {
7926 	return map->def.map_flags;
7927 }
7928 
7929 int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
7930 {
7931 	if (map->fd >= 0)
7932 		return -EBUSY;
7933 	map->def.map_flags = flags;
7934 	return 0;
7935 }
7936 
7937 __u32 bpf_map__numa_node(const struct bpf_map *map)
7938 {
7939 	return map->numa_node;
7940 }
7941 
7942 int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
7943 {
7944 	if (map->fd >= 0)
7945 		return -EBUSY;
7946 	map->numa_node = numa_node;
7947 	return 0;
7948 }
7949 
7950 __u32 bpf_map__key_size(const struct bpf_map *map)
7951 {
7952 	return map->def.key_size;
7953 }
7954 
7955 int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
7956 {
7957 	if (map->fd >= 0)
7958 		return -EBUSY;
7959 	map->def.key_size = size;
7960 	return 0;
7961 }
7962 
7963 __u32 bpf_map__value_size(const struct bpf_map *map)
7964 {
7965 	return map->def.value_size;
7966 }
7967 
7968 int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
7969 {
7970 	if (map->fd >= 0)
7971 		return -EBUSY;
7972 	map->def.value_size = size;
7973 	return 0;
7974 }
7975 
7976 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
7977 {
7978 	return map ? map->btf_key_type_id : 0;
7979 }
7980 
7981 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
7982 {
7983 	return map ? map->btf_value_type_id : 0;
7984 }
7985 
7986 int bpf_map__set_priv(struct bpf_map *map, void *priv,
7987 		     bpf_map_clear_priv_t clear_priv)
7988 {
7989 	if (!map)
7990 		return -EINVAL;
7991 
7992 	if (map->priv) {
7993 		if (map->clear_priv)
7994 			map->clear_priv(map, map->priv);
7995 	}
7996 
7997 	map->priv = priv;
7998 	map->clear_priv = clear_priv;
7999 	return 0;
8000 }
8001 
8002 void *bpf_map__priv(const struct bpf_map *map)
8003 {
8004 	return map ? map->priv : ERR_PTR(-EINVAL);
8005 }
8006 
8007 int bpf_map__set_initial_value(struct bpf_map *map,
8008 			       const void *data, size_t size)
8009 {
8010 	if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
8011 	    size != map->def.value_size || map->fd >= 0)
8012 		return -EINVAL;
8013 
8014 	memcpy(map->mmaped, data, size);
8015 	return 0;
8016 }
8017 
8018 bool bpf_map__is_offload_neutral(const struct bpf_map *map)
8019 {
8020 	return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
8021 }
8022 
8023 bool bpf_map__is_internal(const struct bpf_map *map)
8024 {
8025 	return map->libbpf_type != LIBBPF_MAP_UNSPEC;
8026 }
8027 
8028 __u32 bpf_map__ifindex(const struct bpf_map *map)
8029 {
8030 	return map->map_ifindex;
8031 }
8032 
8033 int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
8034 {
8035 	if (map->fd >= 0)
8036 		return -EBUSY;
8037 	map->map_ifindex = ifindex;
8038 	return 0;
8039 }
8040 
8041 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
8042 {
8043 	if (!bpf_map_type__is_map_in_map(map->def.type)) {
8044 		pr_warn("error: unsupported map type\n");
8045 		return -EINVAL;
8046 	}
8047 	if (map->inner_map_fd != -1) {
8048 		pr_warn("error: inner_map_fd already specified\n");
8049 		return -EINVAL;
8050 	}
8051 	map->inner_map_fd = fd;
8052 	return 0;
8053 }
8054 
8055 static struct bpf_map *
8056 __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
8057 {
8058 	ssize_t idx;
8059 	struct bpf_map *s, *e;
8060 
8061 	if (!obj || !obj->maps)
8062 		return NULL;
8063 
8064 	s = obj->maps;
8065 	e = obj->maps + obj->nr_maps;
8066 
8067 	if ((m < s) || (m >= e)) {
8068 		pr_warn("error in %s: map handler doesn't belong to object\n",
8069 			 __func__);
8070 		return NULL;
8071 	}
8072 
8073 	idx = (m - obj->maps) + i;
8074 	if (idx >= obj->nr_maps || idx < 0)
8075 		return NULL;
8076 	return &obj->maps[idx];
8077 }
8078 
8079 struct bpf_map *
8080 bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
8081 {
8082 	if (prev == NULL)
8083 		return obj->maps;
8084 
8085 	return __bpf_map__iter(prev, obj, 1);
8086 }
8087 
8088 struct bpf_map *
8089 bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
8090 {
8091 	if (next == NULL) {
8092 		if (!obj->nr_maps)
8093 			return NULL;
8094 		return obj->maps + obj->nr_maps - 1;
8095 	}
8096 
8097 	return __bpf_map__iter(next, obj, -1);
8098 }
8099 
8100 struct bpf_map *
8101 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
8102 {
8103 	struct bpf_map *pos;
8104 
8105 	bpf_object__for_each_map(pos, obj) {
8106 		if (pos->name && !strcmp(pos->name, name))
8107 			return pos;
8108 	}
8109 	return NULL;
8110 }
8111 
8112 int
8113 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
8114 {
8115 	return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
8116 }
8117 
8118 struct bpf_map *
8119 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
8120 {
8121 	return ERR_PTR(-ENOTSUP);
8122 }
8123 
8124 long libbpf_get_error(const void *ptr)
8125 {
8126 	return PTR_ERR_OR_ZERO(ptr);
8127 }
8128 
8129 int bpf_prog_load(const char *file, enum bpf_prog_type type,
8130 		  struct bpf_object **pobj, int *prog_fd)
8131 {
8132 	struct bpf_prog_load_attr attr;
8133 
8134 	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
8135 	attr.file = file;
8136 	attr.prog_type = type;
8137 	attr.expected_attach_type = 0;
8138 
8139 	return bpf_prog_load_xattr(&attr, pobj, prog_fd);
8140 }
8141 
8142 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
8143 			struct bpf_object **pobj, int *prog_fd)
8144 {
8145 	struct bpf_object_open_attr open_attr = {};
8146 	struct bpf_program *prog, *first_prog = NULL;
8147 	struct bpf_object *obj;
8148 	struct bpf_map *map;
8149 	int err;
8150 
8151 	if (!attr)
8152 		return -EINVAL;
8153 	if (!attr->file)
8154 		return -EINVAL;
8155 
8156 	open_attr.file = attr->file;
8157 	open_attr.prog_type = attr->prog_type;
8158 
8159 	obj = bpf_object__open_xattr(&open_attr);
8160 	if (IS_ERR_OR_NULL(obj))
8161 		return -ENOENT;
8162 
8163 	bpf_object__for_each_program(prog, obj) {
8164 		enum bpf_attach_type attach_type = attr->expected_attach_type;
8165 		/*
8166 		 * to preserve backwards compatibility, bpf_prog_load treats
8167 		 * attr->prog_type, if specified, as an override to whatever
8168 		 * bpf_object__open guessed
8169 		 */
8170 		if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) {
8171 			bpf_program__set_type(prog, attr->prog_type);
8172 			bpf_program__set_expected_attach_type(prog,
8173 							      attach_type);
8174 		}
8175 		if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) {
8176 			/*
8177 			 * we haven't guessed from section name and user
8178 			 * didn't provide a fallback type, too bad...
8179 			 */
8180 			bpf_object__close(obj);
8181 			return -EINVAL;
8182 		}
8183 
8184 		prog->prog_ifindex = attr->ifindex;
8185 		prog->log_level = attr->log_level;
8186 		prog->prog_flags = attr->prog_flags;
8187 		if (!first_prog)
8188 			first_prog = prog;
8189 	}
8190 
8191 	bpf_object__for_each_map(map, obj) {
8192 		if (!bpf_map__is_offload_neutral(map))
8193 			map->map_ifindex = attr->ifindex;
8194 	}
8195 
8196 	if (!first_prog) {
8197 		pr_warn("object file doesn't contain bpf program\n");
8198 		bpf_object__close(obj);
8199 		return -ENOENT;
8200 	}
8201 
8202 	err = bpf_object__load(obj);
8203 	if (err) {
8204 		bpf_object__close(obj);
8205 		return err;
8206 	}
8207 
8208 	*pobj = obj;
8209 	*prog_fd = bpf_program__fd(first_prog);
8210 	return 0;
8211 }
8212 
8213 struct bpf_link {
8214 	int (*detach)(struct bpf_link *link);
8215 	int (*destroy)(struct bpf_link *link);
8216 	char *pin_path;		/* NULL, if not pinned */
8217 	int fd;			/* hook FD, -1 if not applicable */
8218 	bool disconnected;
8219 };
8220 
8221 /* Replace link's underlying BPF program with the new one */
8222 int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
8223 {
8224 	return bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
8225 }
8226 
8227 /* Release "ownership" of underlying BPF resource (typically, BPF program
8228  * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
8229  * link, when destructed through bpf_link__destroy() call won't attempt to
8230  * detach/unregisted that BPF resource. This is useful in situations where,
8231  * say, attached BPF program has to outlive userspace program that attached it
8232  * in the system. Depending on type of BPF program, though, there might be
8233  * additional steps (like pinning BPF program in BPF FS) necessary to ensure
8234  * exit of userspace program doesn't trigger automatic detachment and clean up
8235  * inside the kernel.
8236  */
8237 void bpf_link__disconnect(struct bpf_link *link)
8238 {
8239 	link->disconnected = true;
8240 }
8241 
8242 int bpf_link__destroy(struct bpf_link *link)
8243 {
8244 	int err = 0;
8245 
8246 	if (IS_ERR_OR_NULL(link))
8247 		return 0;
8248 
8249 	if (!link->disconnected && link->detach)
8250 		err = link->detach(link);
8251 	if (link->destroy)
8252 		link->destroy(link);
8253 	if (link->pin_path)
8254 		free(link->pin_path);
8255 	free(link);
8256 
8257 	return err;
8258 }
8259 
8260 int bpf_link__fd(const struct bpf_link *link)
8261 {
8262 	return link->fd;
8263 }
8264 
8265 const char *bpf_link__pin_path(const struct bpf_link *link)
8266 {
8267 	return link->pin_path;
8268 }
8269 
8270 static int bpf_link__detach_fd(struct bpf_link *link)
8271 {
8272 	return close(link->fd);
8273 }
8274 
8275 struct bpf_link *bpf_link__open(const char *path)
8276 {
8277 	struct bpf_link *link;
8278 	int fd;
8279 
8280 	fd = bpf_obj_get(path);
8281 	if (fd < 0) {
8282 		fd = -errno;
8283 		pr_warn("failed to open link at %s: %d\n", path, fd);
8284 		return ERR_PTR(fd);
8285 	}
8286 
8287 	link = calloc(1, sizeof(*link));
8288 	if (!link) {
8289 		close(fd);
8290 		return ERR_PTR(-ENOMEM);
8291 	}
8292 	link->detach = &bpf_link__detach_fd;
8293 	link->fd = fd;
8294 
8295 	link->pin_path = strdup(path);
8296 	if (!link->pin_path) {
8297 		bpf_link__destroy(link);
8298 		return ERR_PTR(-ENOMEM);
8299 	}
8300 
8301 	return link;
8302 }
8303 
8304 int bpf_link__detach(struct bpf_link *link)
8305 {
8306 	return bpf_link_detach(link->fd) ? -errno : 0;
8307 }
8308 
8309 int bpf_link__pin(struct bpf_link *link, const char *path)
8310 {
8311 	int err;
8312 
8313 	if (link->pin_path)
8314 		return -EBUSY;
8315 	err = make_parent_dir(path);
8316 	if (err)
8317 		return err;
8318 	err = check_path(path);
8319 	if (err)
8320 		return err;
8321 
8322 	link->pin_path = strdup(path);
8323 	if (!link->pin_path)
8324 		return -ENOMEM;
8325 
8326 	if (bpf_obj_pin(link->fd, link->pin_path)) {
8327 		err = -errno;
8328 		zfree(&link->pin_path);
8329 		return err;
8330 	}
8331 
8332 	pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
8333 	return 0;
8334 }
8335 
8336 int bpf_link__unpin(struct bpf_link *link)
8337 {
8338 	int err;
8339 
8340 	if (!link->pin_path)
8341 		return -EINVAL;
8342 
8343 	err = unlink(link->pin_path);
8344 	if (err != 0)
8345 		return -errno;
8346 
8347 	pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
8348 	zfree(&link->pin_path);
8349 	return 0;
8350 }
8351 
8352 static int bpf_link__detach_perf_event(struct bpf_link *link)
8353 {
8354 	int err;
8355 
8356 	err = ioctl(link->fd, PERF_EVENT_IOC_DISABLE, 0);
8357 	if (err)
8358 		err = -errno;
8359 
8360 	close(link->fd);
8361 	return err;
8362 }
8363 
8364 struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog,
8365 						int pfd)
8366 {
8367 	char errmsg[STRERR_BUFSIZE];
8368 	struct bpf_link *link;
8369 	int prog_fd, err;
8370 
8371 	if (pfd < 0) {
8372 		pr_warn("program '%s': invalid perf event FD %d\n",
8373 			bpf_program__title(prog, false), pfd);
8374 		return ERR_PTR(-EINVAL);
8375 	}
8376 	prog_fd = bpf_program__fd(prog);
8377 	if (prog_fd < 0) {
8378 		pr_warn("program '%s': can't attach BPF program w/o FD (did you load it?)\n",
8379 			bpf_program__title(prog, false));
8380 		return ERR_PTR(-EINVAL);
8381 	}
8382 
8383 	link = calloc(1, sizeof(*link));
8384 	if (!link)
8385 		return ERR_PTR(-ENOMEM);
8386 	link->detach = &bpf_link__detach_perf_event;
8387 	link->fd = pfd;
8388 
8389 	if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
8390 		err = -errno;
8391 		free(link);
8392 		pr_warn("program '%s': failed to attach to pfd %d: %s\n",
8393 			bpf_program__title(prog, false), pfd,
8394 			   libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8395 		if (err == -EPROTO)
8396 			pr_warn("program '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
8397 				bpf_program__title(prog, false), pfd);
8398 		return ERR_PTR(err);
8399 	}
8400 	if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
8401 		err = -errno;
8402 		free(link);
8403 		pr_warn("program '%s': failed to enable pfd %d: %s\n",
8404 			bpf_program__title(prog, false), pfd,
8405 			   libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8406 		return ERR_PTR(err);
8407 	}
8408 	return link;
8409 }
8410 
8411 /*
8412  * this function is expected to parse integer in the range of [0, 2^31-1] from
8413  * given file using scanf format string fmt. If actual parsed value is
8414  * negative, the result might be indistinguishable from error
8415  */
8416 static int parse_uint_from_file(const char *file, const char *fmt)
8417 {
8418 	char buf[STRERR_BUFSIZE];
8419 	int err, ret;
8420 	FILE *f;
8421 
8422 	f = fopen(file, "r");
8423 	if (!f) {
8424 		err = -errno;
8425 		pr_debug("failed to open '%s': %s\n", file,
8426 			 libbpf_strerror_r(err, buf, sizeof(buf)));
8427 		return err;
8428 	}
8429 	err = fscanf(f, fmt, &ret);
8430 	if (err != 1) {
8431 		err = err == EOF ? -EIO : -errno;
8432 		pr_debug("failed to parse '%s': %s\n", file,
8433 			libbpf_strerror_r(err, buf, sizeof(buf)));
8434 		fclose(f);
8435 		return err;
8436 	}
8437 	fclose(f);
8438 	return ret;
8439 }
8440 
8441 static int determine_kprobe_perf_type(void)
8442 {
8443 	const char *file = "/sys/bus/event_source/devices/kprobe/type";
8444 
8445 	return parse_uint_from_file(file, "%d\n");
8446 }
8447 
8448 static int determine_uprobe_perf_type(void)
8449 {
8450 	const char *file = "/sys/bus/event_source/devices/uprobe/type";
8451 
8452 	return parse_uint_from_file(file, "%d\n");
8453 }
8454 
8455 static int determine_kprobe_retprobe_bit(void)
8456 {
8457 	const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
8458 
8459 	return parse_uint_from_file(file, "config:%d\n");
8460 }
8461 
8462 static int determine_uprobe_retprobe_bit(void)
8463 {
8464 	const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
8465 
8466 	return parse_uint_from_file(file, "config:%d\n");
8467 }
8468 
8469 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
8470 				 uint64_t offset, int pid)
8471 {
8472 	struct perf_event_attr attr = {};
8473 	char errmsg[STRERR_BUFSIZE];
8474 	int type, pfd, err;
8475 
8476 	type = uprobe ? determine_uprobe_perf_type()
8477 		      : determine_kprobe_perf_type();
8478 	if (type < 0) {
8479 		pr_warn("failed to determine %s perf type: %s\n",
8480 			uprobe ? "uprobe" : "kprobe",
8481 			libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
8482 		return type;
8483 	}
8484 	if (retprobe) {
8485 		int bit = uprobe ? determine_uprobe_retprobe_bit()
8486 				 : determine_kprobe_retprobe_bit();
8487 
8488 		if (bit < 0) {
8489 			pr_warn("failed to determine %s retprobe bit: %s\n",
8490 				uprobe ? "uprobe" : "kprobe",
8491 				libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
8492 			return bit;
8493 		}
8494 		attr.config |= 1 << bit;
8495 	}
8496 	attr.size = sizeof(attr);
8497 	attr.type = type;
8498 	attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
8499 	attr.config2 = offset;		 /* kprobe_addr or probe_offset */
8500 
8501 	/* pid filter is meaningful only for uprobes */
8502 	pfd = syscall(__NR_perf_event_open, &attr,
8503 		      pid < 0 ? -1 : pid /* pid */,
8504 		      pid == -1 ? 0 : -1 /* cpu */,
8505 		      -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
8506 	if (pfd < 0) {
8507 		err = -errno;
8508 		pr_warn("%s perf_event_open() failed: %s\n",
8509 			uprobe ? "uprobe" : "kprobe",
8510 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8511 		return err;
8512 	}
8513 	return pfd;
8514 }
8515 
8516 struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
8517 					    bool retprobe,
8518 					    const char *func_name)
8519 {
8520 	char errmsg[STRERR_BUFSIZE];
8521 	struct bpf_link *link;
8522 	int pfd, err;
8523 
8524 	pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name,
8525 				    0 /* offset */, -1 /* pid */);
8526 	if (pfd < 0) {
8527 		pr_warn("program '%s': failed to create %s '%s' perf event: %s\n",
8528 			bpf_program__title(prog, false),
8529 			retprobe ? "kretprobe" : "kprobe", func_name,
8530 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
8531 		return ERR_PTR(pfd);
8532 	}
8533 	link = bpf_program__attach_perf_event(prog, pfd);
8534 	if (IS_ERR(link)) {
8535 		close(pfd);
8536 		err = PTR_ERR(link);
8537 		pr_warn("program '%s': failed to attach to %s '%s': %s\n",
8538 			bpf_program__title(prog, false),
8539 			retprobe ? "kretprobe" : "kprobe", func_name,
8540 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8541 		return link;
8542 	}
8543 	return link;
8544 }
8545 
8546 static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
8547 				      struct bpf_program *prog)
8548 {
8549 	const char *func_name;
8550 	bool retprobe;
8551 
8552 	func_name = bpf_program__title(prog, false) + sec->len;
8553 	retprobe = strcmp(sec->sec, "kretprobe/") == 0;
8554 
8555 	return bpf_program__attach_kprobe(prog, retprobe, func_name);
8556 }
8557 
8558 struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
8559 					    bool retprobe, pid_t pid,
8560 					    const char *binary_path,
8561 					    size_t func_offset)
8562 {
8563 	char errmsg[STRERR_BUFSIZE];
8564 	struct bpf_link *link;
8565 	int pfd, err;
8566 
8567 	pfd = perf_event_open_probe(true /* uprobe */, retprobe,
8568 				    binary_path, func_offset, pid);
8569 	if (pfd < 0) {
8570 		pr_warn("program '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
8571 			bpf_program__title(prog, false),
8572 			retprobe ? "uretprobe" : "uprobe",
8573 			binary_path, func_offset,
8574 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
8575 		return ERR_PTR(pfd);
8576 	}
8577 	link = bpf_program__attach_perf_event(prog, pfd);
8578 	if (IS_ERR(link)) {
8579 		close(pfd);
8580 		err = PTR_ERR(link);
8581 		pr_warn("program '%s': failed to attach to %s '%s:0x%zx': %s\n",
8582 			bpf_program__title(prog, false),
8583 			retprobe ? "uretprobe" : "uprobe",
8584 			binary_path, func_offset,
8585 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8586 		return link;
8587 	}
8588 	return link;
8589 }
8590 
8591 static int determine_tracepoint_id(const char *tp_category,
8592 				   const char *tp_name)
8593 {
8594 	char file[PATH_MAX];
8595 	int ret;
8596 
8597 	ret = snprintf(file, sizeof(file),
8598 		       "/sys/kernel/debug/tracing/events/%s/%s/id",
8599 		       tp_category, tp_name);
8600 	if (ret < 0)
8601 		return -errno;
8602 	if (ret >= sizeof(file)) {
8603 		pr_debug("tracepoint %s/%s path is too long\n",
8604 			 tp_category, tp_name);
8605 		return -E2BIG;
8606 	}
8607 	return parse_uint_from_file(file, "%d\n");
8608 }
8609 
8610 static int perf_event_open_tracepoint(const char *tp_category,
8611 				      const char *tp_name)
8612 {
8613 	struct perf_event_attr attr = {};
8614 	char errmsg[STRERR_BUFSIZE];
8615 	int tp_id, pfd, err;
8616 
8617 	tp_id = determine_tracepoint_id(tp_category, tp_name);
8618 	if (tp_id < 0) {
8619 		pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
8620 			tp_category, tp_name,
8621 			libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
8622 		return tp_id;
8623 	}
8624 
8625 	attr.type = PERF_TYPE_TRACEPOINT;
8626 	attr.size = sizeof(attr);
8627 	attr.config = tp_id;
8628 
8629 	pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
8630 		      -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
8631 	if (pfd < 0) {
8632 		err = -errno;
8633 		pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
8634 			tp_category, tp_name,
8635 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8636 		return err;
8637 	}
8638 	return pfd;
8639 }
8640 
8641 struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
8642 						const char *tp_category,
8643 						const char *tp_name)
8644 {
8645 	char errmsg[STRERR_BUFSIZE];
8646 	struct bpf_link *link;
8647 	int pfd, err;
8648 
8649 	pfd = perf_event_open_tracepoint(tp_category, tp_name);
8650 	if (pfd < 0) {
8651 		pr_warn("program '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
8652 			bpf_program__title(prog, false),
8653 			tp_category, tp_name,
8654 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
8655 		return ERR_PTR(pfd);
8656 	}
8657 	link = bpf_program__attach_perf_event(prog, pfd);
8658 	if (IS_ERR(link)) {
8659 		close(pfd);
8660 		err = PTR_ERR(link);
8661 		pr_warn("program '%s': failed to attach to tracepoint '%s/%s': %s\n",
8662 			bpf_program__title(prog, false),
8663 			tp_category, tp_name,
8664 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
8665 		return link;
8666 	}
8667 	return link;
8668 }
8669 
8670 static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
8671 				  struct bpf_program *prog)
8672 {
8673 	char *sec_name, *tp_cat, *tp_name;
8674 	struct bpf_link *link;
8675 
8676 	sec_name = strdup(bpf_program__title(prog, false));
8677 	if (!sec_name)
8678 		return ERR_PTR(-ENOMEM);
8679 
8680 	/* extract "tp/<category>/<name>" */
8681 	tp_cat = sec_name + sec->len;
8682 	tp_name = strchr(tp_cat, '/');
8683 	if (!tp_name) {
8684 		link = ERR_PTR(-EINVAL);
8685 		goto out;
8686 	}
8687 	*tp_name = '\0';
8688 	tp_name++;
8689 
8690 	link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
8691 out:
8692 	free(sec_name);
8693 	return link;
8694 }
8695 
8696 struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
8697 						    const char *tp_name)
8698 {
8699 	char errmsg[STRERR_BUFSIZE];
8700 	struct bpf_link *link;
8701 	int prog_fd, pfd;
8702 
8703 	prog_fd = bpf_program__fd(prog);
8704 	if (prog_fd < 0) {
8705 		pr_warn("program '%s': can't attach before loaded\n",
8706 			bpf_program__title(prog, false));
8707 		return ERR_PTR(-EINVAL);
8708 	}
8709 
8710 	link = calloc(1, sizeof(*link));
8711 	if (!link)
8712 		return ERR_PTR(-ENOMEM);
8713 	link->detach = &bpf_link__detach_fd;
8714 
8715 	pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
8716 	if (pfd < 0) {
8717 		pfd = -errno;
8718 		free(link);
8719 		pr_warn("program '%s': failed to attach to raw tracepoint '%s': %s\n",
8720 			bpf_program__title(prog, false), tp_name,
8721 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
8722 		return ERR_PTR(pfd);
8723 	}
8724 	link->fd = pfd;
8725 	return link;
8726 }
8727 
8728 static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec,
8729 				      struct bpf_program *prog)
8730 {
8731 	const char *tp_name = bpf_program__title(prog, false) + sec->len;
8732 
8733 	return bpf_program__attach_raw_tracepoint(prog, tp_name);
8734 }
8735 
8736 /* Common logic for all BPF program types that attach to a btf_id */
8737 static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
8738 {
8739 	char errmsg[STRERR_BUFSIZE];
8740 	struct bpf_link *link;
8741 	int prog_fd, pfd;
8742 
8743 	prog_fd = bpf_program__fd(prog);
8744 	if (prog_fd < 0) {
8745 		pr_warn("program '%s': can't attach before loaded\n",
8746 			bpf_program__title(prog, false));
8747 		return ERR_PTR(-EINVAL);
8748 	}
8749 
8750 	link = calloc(1, sizeof(*link));
8751 	if (!link)
8752 		return ERR_PTR(-ENOMEM);
8753 	link->detach = &bpf_link__detach_fd;
8754 
8755 	pfd = bpf_raw_tracepoint_open(NULL, prog_fd);
8756 	if (pfd < 0) {
8757 		pfd = -errno;
8758 		free(link);
8759 		pr_warn("program '%s': failed to attach: %s\n",
8760 			bpf_program__title(prog, false),
8761 			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
8762 		return ERR_PTR(pfd);
8763 	}
8764 	link->fd = pfd;
8765 	return (struct bpf_link *)link;
8766 }
8767 
8768 struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)
8769 {
8770 	return bpf_program__attach_btf_id(prog);
8771 }
8772 
8773 struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
8774 {
8775 	return bpf_program__attach_btf_id(prog);
8776 }
8777 
8778 static struct bpf_link *attach_trace(const struct bpf_sec_def *sec,
8779 				     struct bpf_program *prog)
8780 {
8781 	return bpf_program__attach_trace(prog);
8782 }
8783 
8784 static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
8785 				   struct bpf_program *prog)
8786 {
8787 	return bpf_program__attach_lsm(prog);
8788 }
8789 
8790 static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
8791 				    struct bpf_program *prog)
8792 {
8793 	return bpf_program__attach_iter(prog, NULL);
8794 }
8795 
8796 static struct bpf_link *
8797 bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
8798 		       const char *target_name)
8799 {
8800 	enum bpf_attach_type attach_type;
8801 	char errmsg[STRERR_BUFSIZE];
8802 	struct bpf_link *link;
8803 	int prog_fd, link_fd;
8804 
8805 	prog_fd = bpf_program__fd(prog);
8806 	if (prog_fd < 0) {
8807 		pr_warn("program '%s': can't attach before loaded\n",
8808 			bpf_program__title(prog, false));
8809 		return ERR_PTR(-EINVAL);
8810 	}
8811 
8812 	link = calloc(1, sizeof(*link));
8813 	if (!link)
8814 		return ERR_PTR(-ENOMEM);
8815 	link->detach = &bpf_link__detach_fd;
8816 
8817 	attach_type = bpf_program__get_expected_attach_type(prog);
8818 	link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
8819 	if (link_fd < 0) {
8820 		link_fd = -errno;
8821 		free(link);
8822 		pr_warn("program '%s': failed to attach to %s: %s\n",
8823 			bpf_program__title(prog, false), target_name,
8824 			libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
8825 		return ERR_PTR(link_fd);
8826 	}
8827 	link->fd = link_fd;
8828 	return link;
8829 }
8830 
8831 struct bpf_link *
8832 bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
8833 {
8834 	return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
8835 }
8836 
8837 struct bpf_link *
8838 bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
8839 {
8840 	return bpf_program__attach_fd(prog, netns_fd, "netns");
8841 }
8842 
8843 struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
8844 {
8845 	/* target_fd/target_ifindex use the same field in LINK_CREATE */
8846 	return bpf_program__attach_fd(prog, ifindex, "xdp");
8847 }
8848 
8849 struct bpf_link *
8850 bpf_program__attach_iter(struct bpf_program *prog,
8851 			 const struct bpf_iter_attach_opts *opts)
8852 {
8853 	DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
8854 	char errmsg[STRERR_BUFSIZE];
8855 	struct bpf_link *link;
8856 	int prog_fd, link_fd;
8857 	__u32 target_fd = 0;
8858 
8859 	if (!OPTS_VALID(opts, bpf_iter_attach_opts))
8860 		return ERR_PTR(-EINVAL);
8861 
8862 	link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
8863 	link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
8864 
8865 	prog_fd = bpf_program__fd(prog);
8866 	if (prog_fd < 0) {
8867 		pr_warn("program '%s': can't attach before loaded\n",
8868 			bpf_program__title(prog, false));
8869 		return ERR_PTR(-EINVAL);
8870 	}
8871 
8872 	link = calloc(1, sizeof(*link));
8873 	if (!link)
8874 		return ERR_PTR(-ENOMEM);
8875 	link->detach = &bpf_link__detach_fd;
8876 
8877 	link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
8878 				  &link_create_opts);
8879 	if (link_fd < 0) {
8880 		link_fd = -errno;
8881 		free(link);
8882 		pr_warn("program '%s': failed to attach to iterator: %s\n",
8883 			bpf_program__title(prog, false),
8884 			libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
8885 		return ERR_PTR(link_fd);
8886 	}
8887 	link->fd = link_fd;
8888 	return link;
8889 }
8890 
8891 struct bpf_link *bpf_program__attach(struct bpf_program *prog)
8892 {
8893 	const struct bpf_sec_def *sec_def;
8894 
8895 	sec_def = find_sec_def(bpf_program__title(prog, false));
8896 	if (!sec_def || !sec_def->attach_fn)
8897 		return ERR_PTR(-ESRCH);
8898 
8899 	return sec_def->attach_fn(sec_def, prog);
8900 }
8901 
8902 static int bpf_link__detach_struct_ops(struct bpf_link *link)
8903 {
8904 	__u32 zero = 0;
8905 
8906 	if (bpf_map_delete_elem(link->fd, &zero))
8907 		return -errno;
8908 
8909 	return 0;
8910 }
8911 
8912 struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map)
8913 {
8914 	struct bpf_struct_ops *st_ops;
8915 	struct bpf_link *link;
8916 	__u32 i, zero = 0;
8917 	int err;
8918 
8919 	if (!bpf_map__is_struct_ops(map) || map->fd == -1)
8920 		return ERR_PTR(-EINVAL);
8921 
8922 	link = calloc(1, sizeof(*link));
8923 	if (!link)
8924 		return ERR_PTR(-EINVAL);
8925 
8926 	st_ops = map->st_ops;
8927 	for (i = 0; i < btf_vlen(st_ops->type); i++) {
8928 		struct bpf_program *prog = st_ops->progs[i];
8929 		void *kern_data;
8930 		int prog_fd;
8931 
8932 		if (!prog)
8933 			continue;
8934 
8935 		prog_fd = bpf_program__fd(prog);
8936 		kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
8937 		*(unsigned long *)kern_data = prog_fd;
8938 	}
8939 
8940 	err = bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0);
8941 	if (err) {
8942 		err = -errno;
8943 		free(link);
8944 		return ERR_PTR(err);
8945 	}
8946 
8947 	link->detach = bpf_link__detach_struct_ops;
8948 	link->fd = map->fd;
8949 
8950 	return link;
8951 }
8952 
8953 enum bpf_perf_event_ret
8954 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
8955 			   void **copy_mem, size_t *copy_size,
8956 			   bpf_perf_event_print_t fn, void *private_data)
8957 {
8958 	struct perf_event_mmap_page *header = mmap_mem;
8959 	__u64 data_head = ring_buffer_read_head(header);
8960 	__u64 data_tail = header->data_tail;
8961 	void *base = ((__u8 *)header) + page_size;
8962 	int ret = LIBBPF_PERF_EVENT_CONT;
8963 	struct perf_event_header *ehdr;
8964 	size_t ehdr_size;
8965 
8966 	while (data_head != data_tail) {
8967 		ehdr = base + (data_tail & (mmap_size - 1));
8968 		ehdr_size = ehdr->size;
8969 
8970 		if (((void *)ehdr) + ehdr_size > base + mmap_size) {
8971 			void *copy_start = ehdr;
8972 			size_t len_first = base + mmap_size - copy_start;
8973 			size_t len_secnd = ehdr_size - len_first;
8974 
8975 			if (*copy_size < ehdr_size) {
8976 				free(*copy_mem);
8977 				*copy_mem = malloc(ehdr_size);
8978 				if (!*copy_mem) {
8979 					*copy_size = 0;
8980 					ret = LIBBPF_PERF_EVENT_ERROR;
8981 					break;
8982 				}
8983 				*copy_size = ehdr_size;
8984 			}
8985 
8986 			memcpy(*copy_mem, copy_start, len_first);
8987 			memcpy(*copy_mem + len_first, base, len_secnd);
8988 			ehdr = *copy_mem;
8989 		}
8990 
8991 		ret = fn(ehdr, private_data);
8992 		data_tail += ehdr_size;
8993 		if (ret != LIBBPF_PERF_EVENT_CONT)
8994 			break;
8995 	}
8996 
8997 	ring_buffer_write_tail(header, data_tail);
8998 	return ret;
8999 }
9000 
9001 struct perf_buffer;
9002 
9003 struct perf_buffer_params {
9004 	struct perf_event_attr *attr;
9005 	/* if event_cb is specified, it takes precendence */
9006 	perf_buffer_event_fn event_cb;
9007 	/* sample_cb and lost_cb are higher-level common-case callbacks */
9008 	perf_buffer_sample_fn sample_cb;
9009 	perf_buffer_lost_fn lost_cb;
9010 	void *ctx;
9011 	int cpu_cnt;
9012 	int *cpus;
9013 	int *map_keys;
9014 };
9015 
9016 struct perf_cpu_buf {
9017 	struct perf_buffer *pb;
9018 	void *base; /* mmap()'ed memory */
9019 	void *buf; /* for reconstructing segmented data */
9020 	size_t buf_size;
9021 	int fd;
9022 	int cpu;
9023 	int map_key;
9024 };
9025 
9026 struct perf_buffer {
9027 	perf_buffer_event_fn event_cb;
9028 	perf_buffer_sample_fn sample_cb;
9029 	perf_buffer_lost_fn lost_cb;
9030 	void *ctx; /* passed into callbacks */
9031 
9032 	size_t page_size;
9033 	size_t mmap_size;
9034 	struct perf_cpu_buf **cpu_bufs;
9035 	struct epoll_event *events;
9036 	int cpu_cnt; /* number of allocated CPU buffers */
9037 	int epoll_fd; /* perf event FD */
9038 	int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
9039 };
9040 
9041 static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
9042 				      struct perf_cpu_buf *cpu_buf)
9043 {
9044 	if (!cpu_buf)
9045 		return;
9046 	if (cpu_buf->base &&
9047 	    munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
9048 		pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
9049 	if (cpu_buf->fd >= 0) {
9050 		ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
9051 		close(cpu_buf->fd);
9052 	}
9053 	free(cpu_buf->buf);
9054 	free(cpu_buf);
9055 }
9056 
9057 void perf_buffer__free(struct perf_buffer *pb)
9058 {
9059 	int i;
9060 
9061 	if (IS_ERR_OR_NULL(pb))
9062 		return;
9063 	if (pb->cpu_bufs) {
9064 		for (i = 0; i < pb->cpu_cnt; i++) {
9065 			struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
9066 
9067 			if (!cpu_buf)
9068 				continue;
9069 
9070 			bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
9071 			perf_buffer__free_cpu_buf(pb, cpu_buf);
9072 		}
9073 		free(pb->cpu_bufs);
9074 	}
9075 	if (pb->epoll_fd >= 0)
9076 		close(pb->epoll_fd);
9077 	free(pb->events);
9078 	free(pb);
9079 }
9080 
9081 static struct perf_cpu_buf *
9082 perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
9083 			  int cpu, int map_key)
9084 {
9085 	struct perf_cpu_buf *cpu_buf;
9086 	char msg[STRERR_BUFSIZE];
9087 	int err;
9088 
9089 	cpu_buf = calloc(1, sizeof(*cpu_buf));
9090 	if (!cpu_buf)
9091 		return ERR_PTR(-ENOMEM);
9092 
9093 	cpu_buf->pb = pb;
9094 	cpu_buf->cpu = cpu;
9095 	cpu_buf->map_key = map_key;
9096 
9097 	cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
9098 			      -1, PERF_FLAG_FD_CLOEXEC);
9099 	if (cpu_buf->fd < 0) {
9100 		err = -errno;
9101 		pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
9102 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
9103 		goto error;
9104 	}
9105 
9106 	cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
9107 			     PROT_READ | PROT_WRITE, MAP_SHARED,
9108 			     cpu_buf->fd, 0);
9109 	if (cpu_buf->base == MAP_FAILED) {
9110 		cpu_buf->base = NULL;
9111 		err = -errno;
9112 		pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
9113 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
9114 		goto error;
9115 	}
9116 
9117 	if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
9118 		err = -errno;
9119 		pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
9120 			cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
9121 		goto error;
9122 	}
9123 
9124 	return cpu_buf;
9125 
9126 error:
9127 	perf_buffer__free_cpu_buf(pb, cpu_buf);
9128 	return (struct perf_cpu_buf *)ERR_PTR(err);
9129 }
9130 
9131 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
9132 					      struct perf_buffer_params *p);
9133 
9134 struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
9135 				     const struct perf_buffer_opts *opts)
9136 {
9137 	struct perf_buffer_params p = {};
9138 	struct perf_event_attr attr = { 0, };
9139 
9140 	attr.config = PERF_COUNT_SW_BPF_OUTPUT;
9141 	attr.type = PERF_TYPE_SOFTWARE;
9142 	attr.sample_type = PERF_SAMPLE_RAW;
9143 	attr.sample_period = 1;
9144 	attr.wakeup_events = 1;
9145 
9146 	p.attr = &attr;
9147 	p.sample_cb = opts ? opts->sample_cb : NULL;
9148 	p.lost_cb = opts ? opts->lost_cb : NULL;
9149 	p.ctx = opts ? opts->ctx : NULL;
9150 
9151 	return __perf_buffer__new(map_fd, page_cnt, &p);
9152 }
9153 
9154 struct perf_buffer *
9155 perf_buffer__new_raw(int map_fd, size_t page_cnt,
9156 		     const struct perf_buffer_raw_opts *opts)
9157 {
9158 	struct perf_buffer_params p = {};
9159 
9160 	p.attr = opts->attr;
9161 	p.event_cb = opts->event_cb;
9162 	p.ctx = opts->ctx;
9163 	p.cpu_cnt = opts->cpu_cnt;
9164 	p.cpus = opts->cpus;
9165 	p.map_keys = opts->map_keys;
9166 
9167 	return __perf_buffer__new(map_fd, page_cnt, &p);
9168 }
9169 
9170 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
9171 					      struct perf_buffer_params *p)
9172 {
9173 	const char *online_cpus_file = "/sys/devices/system/cpu/online";
9174 	struct bpf_map_info map;
9175 	char msg[STRERR_BUFSIZE];
9176 	struct perf_buffer *pb;
9177 	bool *online = NULL;
9178 	__u32 map_info_len;
9179 	int err, i, j, n;
9180 
9181 	if (page_cnt & (page_cnt - 1)) {
9182 		pr_warn("page count should be power of two, but is %zu\n",
9183 			page_cnt);
9184 		return ERR_PTR(-EINVAL);
9185 	}
9186 
9187 	/* best-effort sanity checks */
9188 	memset(&map, 0, sizeof(map));
9189 	map_info_len = sizeof(map);
9190 	err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
9191 	if (err) {
9192 		err = -errno;
9193 		/* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
9194 		 * -EBADFD, -EFAULT, or -E2BIG on real error
9195 		 */
9196 		if (err != -EINVAL) {
9197 			pr_warn("failed to get map info for map FD %d: %s\n",
9198 				map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
9199 			return ERR_PTR(err);
9200 		}
9201 		pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
9202 			 map_fd);
9203 	} else {
9204 		if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
9205 			pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
9206 				map.name);
9207 			return ERR_PTR(-EINVAL);
9208 		}
9209 	}
9210 
9211 	pb = calloc(1, sizeof(*pb));
9212 	if (!pb)
9213 		return ERR_PTR(-ENOMEM);
9214 
9215 	pb->event_cb = p->event_cb;
9216 	pb->sample_cb = p->sample_cb;
9217 	pb->lost_cb = p->lost_cb;
9218 	pb->ctx = p->ctx;
9219 
9220 	pb->page_size = getpagesize();
9221 	pb->mmap_size = pb->page_size * page_cnt;
9222 	pb->map_fd = map_fd;
9223 
9224 	pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
9225 	if (pb->epoll_fd < 0) {
9226 		err = -errno;
9227 		pr_warn("failed to create epoll instance: %s\n",
9228 			libbpf_strerror_r(err, msg, sizeof(msg)));
9229 		goto error;
9230 	}
9231 
9232 	if (p->cpu_cnt > 0) {
9233 		pb->cpu_cnt = p->cpu_cnt;
9234 	} else {
9235 		pb->cpu_cnt = libbpf_num_possible_cpus();
9236 		if (pb->cpu_cnt < 0) {
9237 			err = pb->cpu_cnt;
9238 			goto error;
9239 		}
9240 		if (map.max_entries && map.max_entries < pb->cpu_cnt)
9241 			pb->cpu_cnt = map.max_entries;
9242 	}
9243 
9244 	pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
9245 	if (!pb->events) {
9246 		err = -ENOMEM;
9247 		pr_warn("failed to allocate events: out of memory\n");
9248 		goto error;
9249 	}
9250 	pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
9251 	if (!pb->cpu_bufs) {
9252 		err = -ENOMEM;
9253 		pr_warn("failed to allocate buffers: out of memory\n");
9254 		goto error;
9255 	}
9256 
9257 	err = parse_cpu_mask_file(online_cpus_file, &online, &n);
9258 	if (err) {
9259 		pr_warn("failed to get online CPU mask: %d\n", err);
9260 		goto error;
9261 	}
9262 
9263 	for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
9264 		struct perf_cpu_buf *cpu_buf;
9265 		int cpu, map_key;
9266 
9267 		cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
9268 		map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
9269 
9270 		/* in case user didn't explicitly requested particular CPUs to
9271 		 * be attached to, skip offline/not present CPUs
9272 		 */
9273 		if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
9274 			continue;
9275 
9276 		cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
9277 		if (IS_ERR(cpu_buf)) {
9278 			err = PTR_ERR(cpu_buf);
9279 			goto error;
9280 		}
9281 
9282 		pb->cpu_bufs[j] = cpu_buf;
9283 
9284 		err = bpf_map_update_elem(pb->map_fd, &map_key,
9285 					  &cpu_buf->fd, 0);
9286 		if (err) {
9287 			err = -errno;
9288 			pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
9289 				cpu, map_key, cpu_buf->fd,
9290 				libbpf_strerror_r(err, msg, sizeof(msg)));
9291 			goto error;
9292 		}
9293 
9294 		pb->events[j].events = EPOLLIN;
9295 		pb->events[j].data.ptr = cpu_buf;
9296 		if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
9297 			      &pb->events[j]) < 0) {
9298 			err = -errno;
9299 			pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
9300 				cpu, cpu_buf->fd,
9301 				libbpf_strerror_r(err, msg, sizeof(msg)));
9302 			goto error;
9303 		}
9304 		j++;
9305 	}
9306 	pb->cpu_cnt = j;
9307 	free(online);
9308 
9309 	return pb;
9310 
9311 error:
9312 	free(online);
9313 	if (pb)
9314 		perf_buffer__free(pb);
9315 	return ERR_PTR(err);
9316 }
9317 
9318 struct perf_sample_raw {
9319 	struct perf_event_header header;
9320 	uint32_t size;
9321 	char data[];
9322 };
9323 
9324 struct perf_sample_lost {
9325 	struct perf_event_header header;
9326 	uint64_t id;
9327 	uint64_t lost;
9328 	uint64_t sample_id;
9329 };
9330 
9331 static enum bpf_perf_event_ret
9332 perf_buffer__process_record(struct perf_event_header *e, void *ctx)
9333 {
9334 	struct perf_cpu_buf *cpu_buf = ctx;
9335 	struct perf_buffer *pb = cpu_buf->pb;
9336 	void *data = e;
9337 
9338 	/* user wants full control over parsing perf event */
9339 	if (pb->event_cb)
9340 		return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
9341 
9342 	switch (e->type) {
9343 	case PERF_RECORD_SAMPLE: {
9344 		struct perf_sample_raw *s = data;
9345 
9346 		if (pb->sample_cb)
9347 			pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
9348 		break;
9349 	}
9350 	case PERF_RECORD_LOST: {
9351 		struct perf_sample_lost *s = data;
9352 
9353 		if (pb->lost_cb)
9354 			pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
9355 		break;
9356 	}
9357 	default:
9358 		pr_warn("unknown perf sample type %d\n", e->type);
9359 		return LIBBPF_PERF_EVENT_ERROR;
9360 	}
9361 	return LIBBPF_PERF_EVENT_CONT;
9362 }
9363 
9364 static int perf_buffer__process_records(struct perf_buffer *pb,
9365 					struct perf_cpu_buf *cpu_buf)
9366 {
9367 	enum bpf_perf_event_ret ret;
9368 
9369 	ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
9370 					 pb->page_size, &cpu_buf->buf,
9371 					 &cpu_buf->buf_size,
9372 					 perf_buffer__process_record, cpu_buf);
9373 	if (ret != LIBBPF_PERF_EVENT_CONT)
9374 		return ret;
9375 	return 0;
9376 }
9377 
9378 int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
9379 {
9380 	int i, cnt, err;
9381 
9382 	cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
9383 	for (i = 0; i < cnt; i++) {
9384 		struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
9385 
9386 		err = perf_buffer__process_records(pb, cpu_buf);
9387 		if (err) {
9388 			pr_warn("error while processing records: %d\n", err);
9389 			return err;
9390 		}
9391 	}
9392 	return cnt < 0 ? -errno : cnt;
9393 }
9394 
9395 int perf_buffer__consume(struct perf_buffer *pb)
9396 {
9397 	int i, err;
9398 
9399 	for (i = 0; i < pb->cpu_cnt; i++) {
9400 		struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
9401 
9402 		if (!cpu_buf)
9403 			continue;
9404 
9405 		err = perf_buffer__process_records(pb, cpu_buf);
9406 		if (err) {
9407 			pr_warn("error while processing records: %d\n", err);
9408 			return err;
9409 		}
9410 	}
9411 	return 0;
9412 }
9413 
9414 struct bpf_prog_info_array_desc {
9415 	int	array_offset;	/* e.g. offset of jited_prog_insns */
9416 	int	count_offset;	/* e.g. offset of jited_prog_len */
9417 	int	size_offset;	/* > 0: offset of rec size,
9418 				 * < 0: fix size of -size_offset
9419 				 */
9420 };
9421 
9422 static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
9423 	[BPF_PROG_INFO_JITED_INSNS] = {
9424 		offsetof(struct bpf_prog_info, jited_prog_insns),
9425 		offsetof(struct bpf_prog_info, jited_prog_len),
9426 		-1,
9427 	},
9428 	[BPF_PROG_INFO_XLATED_INSNS] = {
9429 		offsetof(struct bpf_prog_info, xlated_prog_insns),
9430 		offsetof(struct bpf_prog_info, xlated_prog_len),
9431 		-1,
9432 	},
9433 	[BPF_PROG_INFO_MAP_IDS] = {
9434 		offsetof(struct bpf_prog_info, map_ids),
9435 		offsetof(struct bpf_prog_info, nr_map_ids),
9436 		-(int)sizeof(__u32),
9437 	},
9438 	[BPF_PROG_INFO_JITED_KSYMS] = {
9439 		offsetof(struct bpf_prog_info, jited_ksyms),
9440 		offsetof(struct bpf_prog_info, nr_jited_ksyms),
9441 		-(int)sizeof(__u64),
9442 	},
9443 	[BPF_PROG_INFO_JITED_FUNC_LENS] = {
9444 		offsetof(struct bpf_prog_info, jited_func_lens),
9445 		offsetof(struct bpf_prog_info, nr_jited_func_lens),
9446 		-(int)sizeof(__u32),
9447 	},
9448 	[BPF_PROG_INFO_FUNC_INFO] = {
9449 		offsetof(struct bpf_prog_info, func_info),
9450 		offsetof(struct bpf_prog_info, nr_func_info),
9451 		offsetof(struct bpf_prog_info, func_info_rec_size),
9452 	},
9453 	[BPF_PROG_INFO_LINE_INFO] = {
9454 		offsetof(struct bpf_prog_info, line_info),
9455 		offsetof(struct bpf_prog_info, nr_line_info),
9456 		offsetof(struct bpf_prog_info, line_info_rec_size),
9457 	},
9458 	[BPF_PROG_INFO_JITED_LINE_INFO] = {
9459 		offsetof(struct bpf_prog_info, jited_line_info),
9460 		offsetof(struct bpf_prog_info, nr_jited_line_info),
9461 		offsetof(struct bpf_prog_info, jited_line_info_rec_size),
9462 	},
9463 	[BPF_PROG_INFO_PROG_TAGS] = {
9464 		offsetof(struct bpf_prog_info, prog_tags),
9465 		offsetof(struct bpf_prog_info, nr_prog_tags),
9466 		-(int)sizeof(__u8) * BPF_TAG_SIZE,
9467 	},
9468 
9469 };
9470 
9471 static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info,
9472 					   int offset)
9473 {
9474 	__u32 *array = (__u32 *)info;
9475 
9476 	if (offset >= 0)
9477 		return array[offset / sizeof(__u32)];
9478 	return -(int)offset;
9479 }
9480 
9481 static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info,
9482 					   int offset)
9483 {
9484 	__u64 *array = (__u64 *)info;
9485 
9486 	if (offset >= 0)
9487 		return array[offset / sizeof(__u64)];
9488 	return -(int)offset;
9489 }
9490 
9491 static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
9492 					 __u32 val)
9493 {
9494 	__u32 *array = (__u32 *)info;
9495 
9496 	if (offset >= 0)
9497 		array[offset / sizeof(__u32)] = val;
9498 }
9499 
9500 static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
9501 					 __u64 val)
9502 {
9503 	__u64 *array = (__u64 *)info;
9504 
9505 	if (offset >= 0)
9506 		array[offset / sizeof(__u64)] = val;
9507 }
9508 
9509 struct bpf_prog_info_linear *
9510 bpf_program__get_prog_info_linear(int fd, __u64 arrays)
9511 {
9512 	struct bpf_prog_info_linear *info_linear;
9513 	struct bpf_prog_info info = {};
9514 	__u32 info_len = sizeof(info);
9515 	__u32 data_len = 0;
9516 	int i, err;
9517 	void *ptr;
9518 
9519 	if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
9520 		return ERR_PTR(-EINVAL);
9521 
9522 	/* step 1: get array dimensions */
9523 	err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
9524 	if (err) {
9525 		pr_debug("can't get prog info: %s", strerror(errno));
9526 		return ERR_PTR(-EFAULT);
9527 	}
9528 
9529 	/* step 2: calculate total size of all arrays */
9530 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
9531 		bool include_array = (arrays & (1UL << i)) > 0;
9532 		struct bpf_prog_info_array_desc *desc;
9533 		__u32 count, size;
9534 
9535 		desc = bpf_prog_info_array_desc + i;
9536 
9537 		/* kernel is too old to support this field */
9538 		if (info_len < desc->array_offset + sizeof(__u32) ||
9539 		    info_len < desc->count_offset + sizeof(__u32) ||
9540 		    (desc->size_offset > 0 && info_len < desc->size_offset))
9541 			include_array = false;
9542 
9543 		if (!include_array) {
9544 			arrays &= ~(1UL << i);	/* clear the bit */
9545 			continue;
9546 		}
9547 
9548 		count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
9549 		size  = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
9550 
9551 		data_len += count * size;
9552 	}
9553 
9554 	/* step 3: allocate continuous memory */
9555 	data_len = roundup(data_len, sizeof(__u64));
9556 	info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
9557 	if (!info_linear)
9558 		return ERR_PTR(-ENOMEM);
9559 
9560 	/* step 4: fill data to info_linear->info */
9561 	info_linear->arrays = arrays;
9562 	memset(&info_linear->info, 0, sizeof(info));
9563 	ptr = info_linear->data;
9564 
9565 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
9566 		struct bpf_prog_info_array_desc *desc;
9567 		__u32 count, size;
9568 
9569 		if ((arrays & (1UL << i)) == 0)
9570 			continue;
9571 
9572 		desc  = bpf_prog_info_array_desc + i;
9573 		count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
9574 		size  = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
9575 		bpf_prog_info_set_offset_u32(&info_linear->info,
9576 					     desc->count_offset, count);
9577 		bpf_prog_info_set_offset_u32(&info_linear->info,
9578 					     desc->size_offset, size);
9579 		bpf_prog_info_set_offset_u64(&info_linear->info,
9580 					     desc->array_offset,
9581 					     ptr_to_u64(ptr));
9582 		ptr += count * size;
9583 	}
9584 
9585 	/* step 5: call syscall again to get required arrays */
9586 	err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
9587 	if (err) {
9588 		pr_debug("can't get prog info: %s", strerror(errno));
9589 		free(info_linear);
9590 		return ERR_PTR(-EFAULT);
9591 	}
9592 
9593 	/* step 6: verify the data */
9594 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
9595 		struct bpf_prog_info_array_desc *desc;
9596 		__u32 v1, v2;
9597 
9598 		if ((arrays & (1UL << i)) == 0)
9599 			continue;
9600 
9601 		desc = bpf_prog_info_array_desc + i;
9602 		v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
9603 		v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
9604 						   desc->count_offset);
9605 		if (v1 != v2)
9606 			pr_warn("%s: mismatch in element count\n", __func__);
9607 
9608 		v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
9609 		v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
9610 						   desc->size_offset);
9611 		if (v1 != v2)
9612 			pr_warn("%s: mismatch in rec size\n", __func__);
9613 	}
9614 
9615 	/* step 7: update info_len and data_len */
9616 	info_linear->info_len = sizeof(struct bpf_prog_info);
9617 	info_linear->data_len = data_len;
9618 
9619 	return info_linear;
9620 }
9621 
9622 void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
9623 {
9624 	int i;
9625 
9626 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
9627 		struct bpf_prog_info_array_desc *desc;
9628 		__u64 addr, offs;
9629 
9630 		if ((info_linear->arrays & (1UL << i)) == 0)
9631 			continue;
9632 
9633 		desc = bpf_prog_info_array_desc + i;
9634 		addr = bpf_prog_info_read_offset_u64(&info_linear->info,
9635 						     desc->array_offset);
9636 		offs = addr - ptr_to_u64(info_linear->data);
9637 		bpf_prog_info_set_offset_u64(&info_linear->info,
9638 					     desc->array_offset, offs);
9639 	}
9640 }
9641 
9642 void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
9643 {
9644 	int i;
9645 
9646 	for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
9647 		struct bpf_prog_info_array_desc *desc;
9648 		__u64 addr, offs;
9649 
9650 		if ((info_linear->arrays & (1UL << i)) == 0)
9651 			continue;
9652 
9653 		desc = bpf_prog_info_array_desc + i;
9654 		offs = bpf_prog_info_read_offset_u64(&info_linear->info,
9655 						     desc->array_offset);
9656 		addr = offs + ptr_to_u64(info_linear->data);
9657 		bpf_prog_info_set_offset_u64(&info_linear->info,
9658 					     desc->array_offset, addr);
9659 	}
9660 }
9661 
9662 int bpf_program__set_attach_target(struct bpf_program *prog,
9663 				   int attach_prog_fd,
9664 				   const char *attach_func_name)
9665 {
9666 	int btf_id;
9667 
9668 	if (!prog || attach_prog_fd < 0 || !attach_func_name)
9669 		return -EINVAL;
9670 
9671 	if (attach_prog_fd)
9672 		btf_id = libbpf_find_prog_btf_id(attach_func_name,
9673 						 attach_prog_fd);
9674 	else
9675 		btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
9676 					       attach_func_name,
9677 					       prog->expected_attach_type);
9678 
9679 	if (btf_id < 0)
9680 		return btf_id;
9681 
9682 	prog->attach_btf_id = btf_id;
9683 	prog->attach_prog_fd = attach_prog_fd;
9684 	return 0;
9685 }
9686 
9687 int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
9688 {
9689 	int err = 0, n, len, start, end = -1;
9690 	bool *tmp;
9691 
9692 	*mask = NULL;
9693 	*mask_sz = 0;
9694 
9695 	/* Each sub string separated by ',' has format \d+-\d+ or \d+ */
9696 	while (*s) {
9697 		if (*s == ',' || *s == '\n') {
9698 			s++;
9699 			continue;
9700 		}
9701 		n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
9702 		if (n <= 0 || n > 2) {
9703 			pr_warn("Failed to get CPU range %s: %d\n", s, n);
9704 			err = -EINVAL;
9705 			goto cleanup;
9706 		} else if (n == 1) {
9707 			end = start;
9708 		}
9709 		if (start < 0 || start > end) {
9710 			pr_warn("Invalid CPU range [%d,%d] in %s\n",
9711 				start, end, s);
9712 			err = -EINVAL;
9713 			goto cleanup;
9714 		}
9715 		tmp = realloc(*mask, end + 1);
9716 		if (!tmp) {
9717 			err = -ENOMEM;
9718 			goto cleanup;
9719 		}
9720 		*mask = tmp;
9721 		memset(tmp + *mask_sz, 0, start - *mask_sz);
9722 		memset(tmp + start, 1, end - start + 1);
9723 		*mask_sz = end + 1;
9724 		s += len;
9725 	}
9726 	if (!*mask_sz) {
9727 		pr_warn("Empty CPU range\n");
9728 		return -EINVAL;
9729 	}
9730 	return 0;
9731 cleanup:
9732 	free(*mask);
9733 	*mask = NULL;
9734 	return err;
9735 }
9736 
9737 int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
9738 {
9739 	int fd, err = 0, len;
9740 	char buf[128];
9741 
9742 	fd = open(fcpu, O_RDONLY);
9743 	if (fd < 0) {
9744 		err = -errno;
9745 		pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
9746 		return err;
9747 	}
9748 	len = read(fd, buf, sizeof(buf));
9749 	close(fd);
9750 	if (len <= 0) {
9751 		err = len ? -errno : -EINVAL;
9752 		pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
9753 		return err;
9754 	}
9755 	if (len >= sizeof(buf)) {
9756 		pr_warn("CPU mask is too big in file %s\n", fcpu);
9757 		return -E2BIG;
9758 	}
9759 	buf[len] = '\0';
9760 
9761 	return parse_cpu_mask_str(buf, mask, mask_sz);
9762 }
9763 
9764 int libbpf_num_possible_cpus(void)
9765 {
9766 	static const char *fcpu = "/sys/devices/system/cpu/possible";
9767 	static int cpus;
9768 	int err, n, i, tmp_cpus;
9769 	bool *mask;
9770 
9771 	tmp_cpus = READ_ONCE(cpus);
9772 	if (tmp_cpus > 0)
9773 		return tmp_cpus;
9774 
9775 	err = parse_cpu_mask_file(fcpu, &mask, &n);
9776 	if (err)
9777 		return err;
9778 
9779 	tmp_cpus = 0;
9780 	for (i = 0; i < n; i++) {
9781 		if (mask[i])
9782 			tmp_cpus++;
9783 	}
9784 	free(mask);
9785 
9786 	WRITE_ONCE(cpus, tmp_cpus);
9787 	return tmp_cpus;
9788 }
9789 
9790 int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
9791 			      const struct bpf_object_open_opts *opts)
9792 {
9793 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
9794 		.object_name = s->name,
9795 	);
9796 	struct bpf_object *obj;
9797 	int i;
9798 
9799 	/* Attempt to preserve opts->object_name, unless overriden by user
9800 	 * explicitly. Overwriting object name for skeletons is discouraged,
9801 	 * as it breaks global data maps, because they contain object name
9802 	 * prefix as their own map name prefix. When skeleton is generated,
9803 	 * bpftool is making an assumption that this name will stay the same.
9804 	 */
9805 	if (opts) {
9806 		memcpy(&skel_opts, opts, sizeof(*opts));
9807 		if (!opts->object_name)
9808 			skel_opts.object_name = s->name;
9809 	}
9810 
9811 	obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
9812 	if (IS_ERR(obj)) {
9813 		pr_warn("failed to initialize skeleton BPF object '%s': %ld\n",
9814 			s->name, PTR_ERR(obj));
9815 		return PTR_ERR(obj);
9816 	}
9817 
9818 	*s->obj = obj;
9819 
9820 	for (i = 0; i < s->map_cnt; i++) {
9821 		struct bpf_map **map = s->maps[i].map;
9822 		const char *name = s->maps[i].name;
9823 		void **mmaped = s->maps[i].mmaped;
9824 
9825 		*map = bpf_object__find_map_by_name(obj, name);
9826 		if (!*map) {
9827 			pr_warn("failed to find skeleton map '%s'\n", name);
9828 			return -ESRCH;
9829 		}
9830 
9831 		/* externs shouldn't be pre-setup from user code */
9832 		if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
9833 			*mmaped = (*map)->mmaped;
9834 	}
9835 
9836 	for (i = 0; i < s->prog_cnt; i++) {
9837 		struct bpf_program **prog = s->progs[i].prog;
9838 		const char *name = s->progs[i].name;
9839 
9840 		*prog = bpf_object__find_program_by_name(obj, name);
9841 		if (!*prog) {
9842 			pr_warn("failed to find skeleton program '%s'\n", name);
9843 			return -ESRCH;
9844 		}
9845 	}
9846 
9847 	return 0;
9848 }
9849 
9850 int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
9851 {
9852 	int i, err;
9853 
9854 	err = bpf_object__load(*s->obj);
9855 	if (err) {
9856 		pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
9857 		return err;
9858 	}
9859 
9860 	for (i = 0; i < s->map_cnt; i++) {
9861 		struct bpf_map *map = *s->maps[i].map;
9862 		size_t mmap_sz = bpf_map_mmap_sz(map);
9863 		int prot, map_fd = bpf_map__fd(map);
9864 		void **mmaped = s->maps[i].mmaped;
9865 
9866 		if (!mmaped)
9867 			continue;
9868 
9869 		if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
9870 			*mmaped = NULL;
9871 			continue;
9872 		}
9873 
9874 		if (map->def.map_flags & BPF_F_RDONLY_PROG)
9875 			prot = PROT_READ;
9876 		else
9877 			prot = PROT_READ | PROT_WRITE;
9878 
9879 		/* Remap anonymous mmap()-ed "map initialization image" as
9880 		 * a BPF map-backed mmap()-ed memory, but preserving the same
9881 		 * memory address. This will cause kernel to change process'
9882 		 * page table to point to a different piece of kernel memory,
9883 		 * but from userspace point of view memory address (and its
9884 		 * contents, being identical at this point) will stay the
9885 		 * same. This mapping will be released by bpf_object__close()
9886 		 * as per normal clean up procedure, so we don't need to worry
9887 		 * about it from skeleton's clean up perspective.
9888 		 */
9889 		*mmaped = mmap(map->mmaped, mmap_sz, prot,
9890 				MAP_SHARED | MAP_FIXED, map_fd, 0);
9891 		if (*mmaped == MAP_FAILED) {
9892 			err = -errno;
9893 			*mmaped = NULL;
9894 			pr_warn("failed to re-mmap() map '%s': %d\n",
9895 				 bpf_map__name(map), err);
9896 			return err;
9897 		}
9898 	}
9899 
9900 	return 0;
9901 }
9902 
9903 int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
9904 {
9905 	int i;
9906 
9907 	for (i = 0; i < s->prog_cnt; i++) {
9908 		struct bpf_program *prog = *s->progs[i].prog;
9909 		struct bpf_link **link = s->progs[i].link;
9910 		const struct bpf_sec_def *sec_def;
9911 		const char *sec_name = bpf_program__title(prog, false);
9912 
9913 		if (!prog->load)
9914 			continue;
9915 
9916 		sec_def = find_sec_def(sec_name);
9917 		if (!sec_def || !sec_def->attach_fn)
9918 			continue;
9919 
9920 		*link = sec_def->attach_fn(sec_def, prog);
9921 		if (IS_ERR(*link)) {
9922 			pr_warn("failed to auto-attach program '%s': %ld\n",
9923 				bpf_program__name(prog), PTR_ERR(*link));
9924 			return PTR_ERR(*link);
9925 		}
9926 	}
9927 
9928 	return 0;
9929 }
9930 
9931 void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
9932 {
9933 	int i;
9934 
9935 	for (i = 0; i < s->prog_cnt; i++) {
9936 		struct bpf_link **link = s->progs[i].link;
9937 
9938 		bpf_link__destroy(*link);
9939 		*link = NULL;
9940 	}
9941 }
9942 
9943 void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
9944 {
9945 	if (s->progs)
9946 		bpf_object__detach_skeleton(s);
9947 	if (s->obj)
9948 		bpf_object__close(*s->obj);
9949 	free(s->maps);
9950 	free(s->progs);
9951 	free(s);
9952 }
9953