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