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