xref: /openbmc/linux/tools/lib/bpf/btf.c (revision bb84bc51)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
3 
4 #include <endian.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/utsname.h>
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <linux/btf.h>
17 #include <gelf.h>
18 #include "btf.h"
19 #include "bpf.h"
20 #include "libbpf.h"
21 #include "libbpf_internal.h"
22 #include "hashmap.h"
23 
24 /* make sure libbpf doesn't use kernel-only integer typedefs */
25 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
26 
27 #define BTF_MAX_NR_TYPES 0x7fffffffU
28 #define BTF_MAX_STR_OFFSET 0x7fffffffU
29 
30 static struct btf_type btf_void;
31 
32 struct btf {
33 	union {
34 		struct btf_header *hdr;
35 		void *data;
36 	};
37 	struct btf_type **types;
38 	const char *strings;
39 	void *nohdr_data;
40 	__u32 nr_types;
41 	__u32 types_size;
42 	__u32 data_size;
43 	int fd;
44 };
45 
46 static inline __u64 ptr_to_u64(const void *ptr)
47 {
48 	return (__u64) (unsigned long) ptr;
49 }
50 
51 static int btf_add_type(struct btf *btf, struct btf_type *t)
52 {
53 	if (btf->types_size - btf->nr_types < 2) {
54 		struct btf_type **new_types;
55 		__u32 expand_by, new_size;
56 
57 		if (btf->types_size == BTF_MAX_NR_TYPES)
58 			return -E2BIG;
59 
60 		expand_by = max(btf->types_size >> 2, 16U);
61 		new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
62 
63 		new_types = realloc(btf->types, sizeof(*new_types) * new_size);
64 		if (!new_types)
65 			return -ENOMEM;
66 
67 		if (btf->nr_types == 0)
68 			new_types[0] = &btf_void;
69 
70 		btf->types = new_types;
71 		btf->types_size = new_size;
72 	}
73 
74 	btf->types[++(btf->nr_types)] = t;
75 
76 	return 0;
77 }
78 
79 static int btf_parse_hdr(struct btf *btf)
80 {
81 	const struct btf_header *hdr = btf->hdr;
82 	__u32 meta_left;
83 
84 	if (btf->data_size < sizeof(struct btf_header)) {
85 		pr_debug("BTF header not found\n");
86 		return -EINVAL;
87 	}
88 
89 	if (hdr->magic != BTF_MAGIC) {
90 		pr_debug("Invalid BTF magic:%x\n", hdr->magic);
91 		return -EINVAL;
92 	}
93 
94 	if (hdr->version != BTF_VERSION) {
95 		pr_debug("Unsupported BTF version:%u\n", hdr->version);
96 		return -ENOTSUP;
97 	}
98 
99 	if (hdr->flags) {
100 		pr_debug("Unsupported BTF flags:%x\n", hdr->flags);
101 		return -ENOTSUP;
102 	}
103 
104 	meta_left = btf->data_size - sizeof(*hdr);
105 	if (!meta_left) {
106 		pr_debug("BTF has no data\n");
107 		return -EINVAL;
108 	}
109 
110 	if (meta_left < hdr->type_off) {
111 		pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off);
112 		return -EINVAL;
113 	}
114 
115 	if (meta_left < hdr->str_off) {
116 		pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off);
117 		return -EINVAL;
118 	}
119 
120 	if (hdr->type_off >= hdr->str_off) {
121 		pr_debug("BTF type section offset >= string section offset. No type?\n");
122 		return -EINVAL;
123 	}
124 
125 	if (hdr->type_off & 0x02) {
126 		pr_debug("BTF type section is not aligned to 4 bytes\n");
127 		return -EINVAL;
128 	}
129 
130 	btf->nohdr_data = btf->hdr + 1;
131 
132 	return 0;
133 }
134 
135 static int btf_parse_str_sec(struct btf *btf)
136 {
137 	const struct btf_header *hdr = btf->hdr;
138 	const char *start = btf->nohdr_data + hdr->str_off;
139 	const char *end = start + btf->hdr->str_len;
140 
141 	if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
142 	    start[0] || end[-1]) {
143 		pr_debug("Invalid BTF string section\n");
144 		return -EINVAL;
145 	}
146 
147 	btf->strings = start;
148 
149 	return 0;
150 }
151 
152 static int btf_type_size(struct btf_type *t)
153 {
154 	int base_size = sizeof(struct btf_type);
155 	__u16 vlen = btf_vlen(t);
156 
157 	switch (btf_kind(t)) {
158 	case BTF_KIND_FWD:
159 	case BTF_KIND_CONST:
160 	case BTF_KIND_VOLATILE:
161 	case BTF_KIND_RESTRICT:
162 	case BTF_KIND_PTR:
163 	case BTF_KIND_TYPEDEF:
164 	case BTF_KIND_FUNC:
165 		return base_size;
166 	case BTF_KIND_INT:
167 		return base_size + sizeof(__u32);
168 	case BTF_KIND_ENUM:
169 		return base_size + vlen * sizeof(struct btf_enum);
170 	case BTF_KIND_ARRAY:
171 		return base_size + sizeof(struct btf_array);
172 	case BTF_KIND_STRUCT:
173 	case BTF_KIND_UNION:
174 		return base_size + vlen * sizeof(struct btf_member);
175 	case BTF_KIND_FUNC_PROTO:
176 		return base_size + vlen * sizeof(struct btf_param);
177 	case BTF_KIND_VAR:
178 		return base_size + sizeof(struct btf_var);
179 	case BTF_KIND_DATASEC:
180 		return base_size + vlen * sizeof(struct btf_var_secinfo);
181 	default:
182 		pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
183 		return -EINVAL;
184 	}
185 }
186 
187 static int btf_parse_type_sec(struct btf *btf)
188 {
189 	struct btf_header *hdr = btf->hdr;
190 	void *nohdr_data = btf->nohdr_data;
191 	void *next_type = nohdr_data + hdr->type_off;
192 	void *end_type = nohdr_data + hdr->str_off;
193 
194 	while (next_type < end_type) {
195 		struct btf_type *t = next_type;
196 		int type_size;
197 		int err;
198 
199 		type_size = btf_type_size(t);
200 		if (type_size < 0)
201 			return type_size;
202 		next_type += type_size;
203 		err = btf_add_type(btf, t);
204 		if (err)
205 			return err;
206 	}
207 
208 	return 0;
209 }
210 
211 __u32 btf__get_nr_types(const struct btf *btf)
212 {
213 	return btf->nr_types;
214 }
215 
216 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
217 {
218 	if (type_id > btf->nr_types)
219 		return NULL;
220 
221 	return btf->types[type_id];
222 }
223 
224 static bool btf_type_is_void(const struct btf_type *t)
225 {
226 	return t == &btf_void || btf_is_fwd(t);
227 }
228 
229 static bool btf_type_is_void_or_null(const struct btf_type *t)
230 {
231 	return !t || btf_type_is_void(t);
232 }
233 
234 #define MAX_RESOLVE_DEPTH 32
235 
236 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
237 {
238 	const struct btf_array *array;
239 	const struct btf_type *t;
240 	__u32 nelems = 1;
241 	__s64 size = -1;
242 	int i;
243 
244 	t = btf__type_by_id(btf, type_id);
245 	for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
246 	     i++) {
247 		switch (btf_kind(t)) {
248 		case BTF_KIND_INT:
249 		case BTF_KIND_STRUCT:
250 		case BTF_KIND_UNION:
251 		case BTF_KIND_ENUM:
252 		case BTF_KIND_DATASEC:
253 			size = t->size;
254 			goto done;
255 		case BTF_KIND_PTR:
256 			size = sizeof(void *);
257 			goto done;
258 		case BTF_KIND_TYPEDEF:
259 		case BTF_KIND_VOLATILE:
260 		case BTF_KIND_CONST:
261 		case BTF_KIND_RESTRICT:
262 		case BTF_KIND_VAR:
263 			type_id = t->type;
264 			break;
265 		case BTF_KIND_ARRAY:
266 			array = btf_array(t);
267 			if (nelems && array->nelems > UINT32_MAX / nelems)
268 				return -E2BIG;
269 			nelems *= array->nelems;
270 			type_id = array->type;
271 			break;
272 		default:
273 			return -EINVAL;
274 		}
275 
276 		t = btf__type_by_id(btf, type_id);
277 	}
278 
279 done:
280 	if (size < 0)
281 		return -EINVAL;
282 	if (nelems && size > UINT32_MAX / nelems)
283 		return -E2BIG;
284 
285 	return nelems * size;
286 }
287 
288 int btf__align_of(const struct btf *btf, __u32 id)
289 {
290 	const struct btf_type *t = btf__type_by_id(btf, id);
291 	__u16 kind = btf_kind(t);
292 
293 	switch (kind) {
294 	case BTF_KIND_INT:
295 	case BTF_KIND_ENUM:
296 		return min(sizeof(void *), (size_t)t->size);
297 	case BTF_KIND_PTR:
298 		return sizeof(void *);
299 	case BTF_KIND_TYPEDEF:
300 	case BTF_KIND_VOLATILE:
301 	case BTF_KIND_CONST:
302 	case BTF_KIND_RESTRICT:
303 		return btf__align_of(btf, t->type);
304 	case BTF_KIND_ARRAY:
305 		return btf__align_of(btf, btf_array(t)->type);
306 	case BTF_KIND_STRUCT:
307 	case BTF_KIND_UNION: {
308 		const struct btf_member *m = btf_members(t);
309 		__u16 vlen = btf_vlen(t);
310 		int i, max_align = 1, align;
311 
312 		for (i = 0; i < vlen; i++, m++) {
313 			align = btf__align_of(btf, m->type);
314 			if (align <= 0)
315 				return align;
316 			max_align = max(max_align, align);
317 		}
318 
319 		return max_align;
320 	}
321 	default:
322 		pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
323 		return 0;
324 	}
325 }
326 
327 int btf__resolve_type(const struct btf *btf, __u32 type_id)
328 {
329 	const struct btf_type *t;
330 	int depth = 0;
331 
332 	t = btf__type_by_id(btf, type_id);
333 	while (depth < MAX_RESOLVE_DEPTH &&
334 	       !btf_type_is_void_or_null(t) &&
335 	       (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
336 		type_id = t->type;
337 		t = btf__type_by_id(btf, type_id);
338 		depth++;
339 	}
340 
341 	if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
342 		return -EINVAL;
343 
344 	return type_id;
345 }
346 
347 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
348 {
349 	__u32 i;
350 
351 	if (!strcmp(type_name, "void"))
352 		return 0;
353 
354 	for (i = 1; i <= btf->nr_types; i++) {
355 		const struct btf_type *t = btf->types[i];
356 		const char *name = btf__name_by_offset(btf, t->name_off);
357 
358 		if (name && !strcmp(type_name, name))
359 			return i;
360 	}
361 
362 	return -ENOENT;
363 }
364 
365 __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
366 			     __u32 kind)
367 {
368 	__u32 i;
369 
370 	if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
371 		return 0;
372 
373 	for (i = 1; i <= btf->nr_types; i++) {
374 		const struct btf_type *t = btf->types[i];
375 		const char *name;
376 
377 		if (btf_kind(t) != kind)
378 			continue;
379 		name = btf__name_by_offset(btf, t->name_off);
380 		if (name && !strcmp(type_name, name))
381 			return i;
382 	}
383 
384 	return -ENOENT;
385 }
386 
387 void btf__free(struct btf *btf)
388 {
389 	if (IS_ERR_OR_NULL(btf))
390 		return;
391 
392 	if (btf->fd >= 0)
393 		close(btf->fd);
394 
395 	free(btf->data);
396 	free(btf->types);
397 	free(btf);
398 }
399 
400 struct btf *btf__new(const void *data, __u32 size)
401 {
402 	struct btf *btf;
403 	int err;
404 
405 	btf = calloc(1, sizeof(struct btf));
406 	if (!btf)
407 		return ERR_PTR(-ENOMEM);
408 
409 	btf->fd = -1;
410 
411 	btf->data = malloc(size);
412 	if (!btf->data) {
413 		err = -ENOMEM;
414 		goto done;
415 	}
416 
417 	memcpy(btf->data, data, size);
418 	btf->data_size = size;
419 
420 	err = btf_parse_hdr(btf);
421 	if (err)
422 		goto done;
423 
424 	err = btf_parse_str_sec(btf);
425 	if (err)
426 		goto done;
427 
428 	err = btf_parse_type_sec(btf);
429 
430 done:
431 	if (err) {
432 		btf__free(btf);
433 		return ERR_PTR(err);
434 	}
435 
436 	return btf;
437 }
438 
439 static bool btf_check_endianness(const GElf_Ehdr *ehdr)
440 {
441 #if __BYTE_ORDER == __LITTLE_ENDIAN
442 	return ehdr->e_ident[EI_DATA] == ELFDATA2LSB;
443 #elif __BYTE_ORDER == __BIG_ENDIAN
444 	return ehdr->e_ident[EI_DATA] == ELFDATA2MSB;
445 #else
446 # error "Unrecognized __BYTE_ORDER__"
447 #endif
448 }
449 
450 struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
451 {
452 	Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
453 	int err = 0, fd = -1, idx = 0;
454 	struct btf *btf = NULL;
455 	Elf_Scn *scn = NULL;
456 	Elf *elf = NULL;
457 	GElf_Ehdr ehdr;
458 
459 	if (elf_version(EV_CURRENT) == EV_NONE) {
460 		pr_warn("failed to init libelf for %s\n", path);
461 		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
462 	}
463 
464 	fd = open(path, O_RDONLY);
465 	if (fd < 0) {
466 		err = -errno;
467 		pr_warn("failed to open %s: %s\n", path, strerror(errno));
468 		return ERR_PTR(err);
469 	}
470 
471 	err = -LIBBPF_ERRNO__FORMAT;
472 
473 	elf = elf_begin(fd, ELF_C_READ, NULL);
474 	if (!elf) {
475 		pr_warn("failed to open %s as ELF file\n", path);
476 		goto done;
477 	}
478 	if (!gelf_getehdr(elf, &ehdr)) {
479 		pr_warn("failed to get EHDR from %s\n", path);
480 		goto done;
481 	}
482 	if (!btf_check_endianness(&ehdr)) {
483 		pr_warn("non-native ELF endianness is not supported\n");
484 		goto done;
485 	}
486 	if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
487 		pr_warn("failed to get e_shstrndx from %s\n", path);
488 		goto done;
489 	}
490 
491 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
492 		GElf_Shdr sh;
493 		char *name;
494 
495 		idx++;
496 		if (gelf_getshdr(scn, &sh) != &sh) {
497 			pr_warn("failed to get section(%d) header from %s\n",
498 				idx, path);
499 			goto done;
500 		}
501 		name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
502 		if (!name) {
503 			pr_warn("failed to get section(%d) name from %s\n",
504 				idx, path);
505 			goto done;
506 		}
507 		if (strcmp(name, BTF_ELF_SEC) == 0) {
508 			btf_data = elf_getdata(scn, 0);
509 			if (!btf_data) {
510 				pr_warn("failed to get section(%d, %s) data from %s\n",
511 					idx, name, path);
512 				goto done;
513 			}
514 			continue;
515 		} else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
516 			btf_ext_data = elf_getdata(scn, 0);
517 			if (!btf_ext_data) {
518 				pr_warn("failed to get section(%d, %s) data from %s\n",
519 					idx, name, path);
520 				goto done;
521 			}
522 			continue;
523 		}
524 	}
525 
526 	err = 0;
527 
528 	if (!btf_data) {
529 		err = -ENOENT;
530 		goto done;
531 	}
532 	btf = btf__new(btf_data->d_buf, btf_data->d_size);
533 	if (IS_ERR(btf))
534 		goto done;
535 
536 	if (btf_ext && btf_ext_data) {
537 		*btf_ext = btf_ext__new(btf_ext_data->d_buf,
538 					btf_ext_data->d_size);
539 		if (IS_ERR(*btf_ext))
540 			goto done;
541 	} else if (btf_ext) {
542 		*btf_ext = NULL;
543 	}
544 done:
545 	if (elf)
546 		elf_end(elf);
547 	close(fd);
548 
549 	if (err)
550 		return ERR_PTR(err);
551 	/*
552 	 * btf is always parsed before btf_ext, so no need to clean up
553 	 * btf_ext, if btf loading failed
554 	 */
555 	if (IS_ERR(btf))
556 		return btf;
557 	if (btf_ext && IS_ERR(*btf_ext)) {
558 		btf__free(btf);
559 		err = PTR_ERR(*btf_ext);
560 		return ERR_PTR(err);
561 	}
562 	return btf;
563 }
564 
565 struct btf *btf__parse_raw(const char *path)
566 {
567 	struct btf *btf = NULL;
568 	void *data = NULL;
569 	FILE *f = NULL;
570 	__u16 magic;
571 	int err = 0;
572 	long sz;
573 
574 	f = fopen(path, "rb");
575 	if (!f) {
576 		err = -errno;
577 		goto err_out;
578 	}
579 
580 	/* check BTF magic */
581 	if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
582 		err = -EIO;
583 		goto err_out;
584 	}
585 	if (magic != BTF_MAGIC) {
586 		/* definitely not a raw BTF */
587 		err = -EPROTO;
588 		goto err_out;
589 	}
590 
591 	/* get file size */
592 	if (fseek(f, 0, SEEK_END)) {
593 		err = -errno;
594 		goto err_out;
595 	}
596 	sz = ftell(f);
597 	if (sz < 0) {
598 		err = -errno;
599 		goto err_out;
600 	}
601 	/* rewind to the start */
602 	if (fseek(f, 0, SEEK_SET)) {
603 		err = -errno;
604 		goto err_out;
605 	}
606 
607 	/* pre-alloc memory and read all of BTF data */
608 	data = malloc(sz);
609 	if (!data) {
610 		err = -ENOMEM;
611 		goto err_out;
612 	}
613 	if (fread(data, 1, sz, f) < sz) {
614 		err = -EIO;
615 		goto err_out;
616 	}
617 
618 	/* finally parse BTF data */
619 	btf = btf__new(data, sz);
620 
621 err_out:
622 	free(data);
623 	if (f)
624 		fclose(f);
625 	return err ? ERR_PTR(err) : btf;
626 }
627 
628 struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
629 {
630 	struct btf *btf;
631 
632 	if (btf_ext)
633 		*btf_ext = NULL;
634 
635 	btf = btf__parse_raw(path);
636 	if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
637 		return btf;
638 
639 	return btf__parse_elf(path, btf_ext);
640 }
641 
642 static int compare_vsi_off(const void *_a, const void *_b)
643 {
644 	const struct btf_var_secinfo *a = _a;
645 	const struct btf_var_secinfo *b = _b;
646 
647 	return a->offset - b->offset;
648 }
649 
650 static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
651 			     struct btf_type *t)
652 {
653 	__u32 size = 0, off = 0, i, vars = btf_vlen(t);
654 	const char *name = btf__name_by_offset(btf, t->name_off);
655 	const struct btf_type *t_var;
656 	struct btf_var_secinfo *vsi;
657 	const struct btf_var *var;
658 	int ret;
659 
660 	if (!name) {
661 		pr_debug("No name found in string section for DATASEC kind.\n");
662 		return -ENOENT;
663 	}
664 
665 	/* .extern datasec size and var offsets were set correctly during
666 	 * extern collection step, so just skip straight to sorting variables
667 	 */
668 	if (t->size)
669 		goto sort_vars;
670 
671 	ret = bpf_object__section_size(obj, name, &size);
672 	if (ret || !size || (t->size && t->size != size)) {
673 		pr_debug("Invalid size for section %s: %u bytes\n", name, size);
674 		return -ENOENT;
675 	}
676 
677 	t->size = size;
678 
679 	for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
680 		t_var = btf__type_by_id(btf, vsi->type);
681 		var = btf_var(t_var);
682 
683 		if (!btf_is_var(t_var)) {
684 			pr_debug("Non-VAR type seen in section %s\n", name);
685 			return -EINVAL;
686 		}
687 
688 		if (var->linkage == BTF_VAR_STATIC)
689 			continue;
690 
691 		name = btf__name_by_offset(btf, t_var->name_off);
692 		if (!name) {
693 			pr_debug("No name found in string section for VAR kind\n");
694 			return -ENOENT;
695 		}
696 
697 		ret = bpf_object__variable_offset(obj, name, &off);
698 		if (ret) {
699 			pr_debug("No offset found in symbol table for VAR %s\n",
700 				 name);
701 			return -ENOENT;
702 		}
703 
704 		vsi->offset = off;
705 	}
706 
707 sort_vars:
708 	qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
709 	return 0;
710 }
711 
712 int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
713 {
714 	int err = 0;
715 	__u32 i;
716 
717 	for (i = 1; i <= btf->nr_types; i++) {
718 		struct btf_type *t = btf->types[i];
719 
720 		/* Loader needs to fix up some of the things compiler
721 		 * couldn't get its hands on while emitting BTF. This
722 		 * is section size and global variable offset. We use
723 		 * the info from the ELF itself for this purpose.
724 		 */
725 		if (btf_is_datasec(t)) {
726 			err = btf_fixup_datasec(obj, btf, t);
727 			if (err)
728 				break;
729 		}
730 	}
731 
732 	return err;
733 }
734 
735 int btf__load(struct btf *btf)
736 {
737 	__u32 log_buf_size = 0;
738 	char *log_buf = NULL;
739 	int err = 0;
740 
741 	if (btf->fd >= 0)
742 		return -EEXIST;
743 
744 retry_load:
745 	if (log_buf_size) {
746 		log_buf = malloc(log_buf_size);
747 		if (!log_buf)
748 			return -ENOMEM;
749 
750 		*log_buf = 0;
751 	}
752 
753 	btf->fd = bpf_load_btf(btf->data, btf->data_size,
754 			       log_buf, log_buf_size, false);
755 	if (btf->fd < 0) {
756 		if (!log_buf || errno == ENOSPC) {
757 			log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
758 					   log_buf_size << 1);
759 			free(log_buf);
760 			goto retry_load;
761 		}
762 
763 		err = -errno;
764 		pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
765 		if (*log_buf)
766 			pr_warn("%s\n", log_buf);
767 		goto done;
768 	}
769 
770 done:
771 	free(log_buf);
772 	return err;
773 }
774 
775 int btf__fd(const struct btf *btf)
776 {
777 	return btf->fd;
778 }
779 
780 void btf__set_fd(struct btf *btf, int fd)
781 {
782 	btf->fd = fd;
783 }
784 
785 const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
786 {
787 	*size = btf->data_size;
788 	return btf->data;
789 }
790 
791 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
792 {
793 	if (offset < btf->hdr->str_len)
794 		return &btf->strings[offset];
795 	else
796 		return NULL;
797 }
798 
799 int btf__get_from_id(__u32 id, struct btf **btf)
800 {
801 	struct bpf_btf_info btf_info = { 0 };
802 	__u32 len = sizeof(btf_info);
803 	__u32 last_size;
804 	int btf_fd;
805 	void *ptr;
806 	int err;
807 
808 	err = 0;
809 	*btf = NULL;
810 	btf_fd = bpf_btf_get_fd_by_id(id);
811 	if (btf_fd < 0)
812 		return 0;
813 
814 	/* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
815 	 * let's start with a sane default - 4KiB here - and resize it only if
816 	 * bpf_obj_get_info_by_fd() needs a bigger buffer.
817 	 */
818 	btf_info.btf_size = 4096;
819 	last_size = btf_info.btf_size;
820 	ptr = malloc(last_size);
821 	if (!ptr) {
822 		err = -ENOMEM;
823 		goto exit_free;
824 	}
825 
826 	memset(ptr, 0, last_size);
827 	btf_info.btf = ptr_to_u64(ptr);
828 	err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
829 
830 	if (!err && btf_info.btf_size > last_size) {
831 		void *temp_ptr;
832 
833 		last_size = btf_info.btf_size;
834 		temp_ptr = realloc(ptr, last_size);
835 		if (!temp_ptr) {
836 			err = -ENOMEM;
837 			goto exit_free;
838 		}
839 		ptr = temp_ptr;
840 		memset(ptr, 0, last_size);
841 		btf_info.btf = ptr_to_u64(ptr);
842 		err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
843 	}
844 
845 	if (err || btf_info.btf_size > last_size) {
846 		err = errno;
847 		goto exit_free;
848 	}
849 
850 	*btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
851 	if (IS_ERR(*btf)) {
852 		err = PTR_ERR(*btf);
853 		*btf = NULL;
854 	}
855 
856 exit_free:
857 	close(btf_fd);
858 	free(ptr);
859 
860 	return err;
861 }
862 
863 int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
864 			 __u32 expected_key_size, __u32 expected_value_size,
865 			 __u32 *key_type_id, __u32 *value_type_id)
866 {
867 	const struct btf_type *container_type;
868 	const struct btf_member *key, *value;
869 	const size_t max_name = 256;
870 	char container_name[max_name];
871 	__s64 key_size, value_size;
872 	__s32 container_id;
873 
874 	if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
875 	    max_name) {
876 		pr_warn("map:%s length of '____btf_map_%s' is too long\n",
877 			map_name, map_name);
878 		return -EINVAL;
879 	}
880 
881 	container_id = btf__find_by_name(btf, container_name);
882 	if (container_id < 0) {
883 		pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
884 			 map_name, container_name);
885 		return container_id;
886 	}
887 
888 	container_type = btf__type_by_id(btf, container_id);
889 	if (!container_type) {
890 		pr_warn("map:%s cannot find BTF type for container_id:%u\n",
891 			map_name, container_id);
892 		return -EINVAL;
893 	}
894 
895 	if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
896 		pr_warn("map:%s container_name:%s is an invalid container struct\n",
897 			map_name, container_name);
898 		return -EINVAL;
899 	}
900 
901 	key = btf_members(container_type);
902 	value = key + 1;
903 
904 	key_size = btf__resolve_size(btf, key->type);
905 	if (key_size < 0) {
906 		pr_warn("map:%s invalid BTF key_type_size\n", map_name);
907 		return key_size;
908 	}
909 
910 	if (expected_key_size != key_size) {
911 		pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
912 			map_name, (__u32)key_size, expected_key_size);
913 		return -EINVAL;
914 	}
915 
916 	value_size = btf__resolve_size(btf, value->type);
917 	if (value_size < 0) {
918 		pr_warn("map:%s invalid BTF value_type_size\n", map_name);
919 		return value_size;
920 	}
921 
922 	if (expected_value_size != value_size) {
923 		pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
924 			map_name, (__u32)value_size, expected_value_size);
925 		return -EINVAL;
926 	}
927 
928 	*key_type_id = key->type;
929 	*value_type_id = value->type;
930 
931 	return 0;
932 }
933 
934 struct btf_ext_sec_setup_param {
935 	__u32 off;
936 	__u32 len;
937 	__u32 min_rec_size;
938 	struct btf_ext_info *ext_info;
939 	const char *desc;
940 };
941 
942 static int btf_ext_setup_info(struct btf_ext *btf_ext,
943 			      struct btf_ext_sec_setup_param *ext_sec)
944 {
945 	const struct btf_ext_info_sec *sinfo;
946 	struct btf_ext_info *ext_info;
947 	__u32 info_left, record_size;
948 	/* The start of the info sec (including the __u32 record_size). */
949 	void *info;
950 
951 	if (ext_sec->len == 0)
952 		return 0;
953 
954 	if (ext_sec->off & 0x03) {
955 		pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
956 		     ext_sec->desc);
957 		return -EINVAL;
958 	}
959 
960 	info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
961 	info_left = ext_sec->len;
962 
963 	if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
964 		pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
965 			 ext_sec->desc, ext_sec->off, ext_sec->len);
966 		return -EINVAL;
967 	}
968 
969 	/* At least a record size */
970 	if (info_left < sizeof(__u32)) {
971 		pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
972 		return -EINVAL;
973 	}
974 
975 	/* The record size needs to meet the minimum standard */
976 	record_size = *(__u32 *)info;
977 	if (record_size < ext_sec->min_rec_size ||
978 	    record_size & 0x03) {
979 		pr_debug("%s section in .BTF.ext has invalid record size %u\n",
980 			 ext_sec->desc, record_size);
981 		return -EINVAL;
982 	}
983 
984 	sinfo = info + sizeof(__u32);
985 	info_left -= sizeof(__u32);
986 
987 	/* If no records, return failure now so .BTF.ext won't be used. */
988 	if (!info_left) {
989 		pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
990 		return -EINVAL;
991 	}
992 
993 	while (info_left) {
994 		unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
995 		__u64 total_record_size;
996 		__u32 num_records;
997 
998 		if (info_left < sec_hdrlen) {
999 			pr_debug("%s section header is not found in .BTF.ext\n",
1000 			     ext_sec->desc);
1001 			return -EINVAL;
1002 		}
1003 
1004 		num_records = sinfo->num_info;
1005 		if (num_records == 0) {
1006 			pr_debug("%s section has incorrect num_records in .BTF.ext\n",
1007 			     ext_sec->desc);
1008 			return -EINVAL;
1009 		}
1010 
1011 		total_record_size = sec_hdrlen +
1012 				    (__u64)num_records * record_size;
1013 		if (info_left < total_record_size) {
1014 			pr_debug("%s section has incorrect num_records in .BTF.ext\n",
1015 			     ext_sec->desc);
1016 			return -EINVAL;
1017 		}
1018 
1019 		info_left -= total_record_size;
1020 		sinfo = (void *)sinfo + total_record_size;
1021 	}
1022 
1023 	ext_info = ext_sec->ext_info;
1024 	ext_info->len = ext_sec->len - sizeof(__u32);
1025 	ext_info->rec_size = record_size;
1026 	ext_info->info = info + sizeof(__u32);
1027 
1028 	return 0;
1029 }
1030 
1031 static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
1032 {
1033 	struct btf_ext_sec_setup_param param = {
1034 		.off = btf_ext->hdr->func_info_off,
1035 		.len = btf_ext->hdr->func_info_len,
1036 		.min_rec_size = sizeof(struct bpf_func_info_min),
1037 		.ext_info = &btf_ext->func_info,
1038 		.desc = "func_info"
1039 	};
1040 
1041 	return btf_ext_setup_info(btf_ext, &param);
1042 }
1043 
1044 static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
1045 {
1046 	struct btf_ext_sec_setup_param param = {
1047 		.off = btf_ext->hdr->line_info_off,
1048 		.len = btf_ext->hdr->line_info_len,
1049 		.min_rec_size = sizeof(struct bpf_line_info_min),
1050 		.ext_info = &btf_ext->line_info,
1051 		.desc = "line_info",
1052 	};
1053 
1054 	return btf_ext_setup_info(btf_ext, &param);
1055 }
1056 
1057 static int btf_ext_setup_field_reloc(struct btf_ext *btf_ext)
1058 {
1059 	struct btf_ext_sec_setup_param param = {
1060 		.off = btf_ext->hdr->field_reloc_off,
1061 		.len = btf_ext->hdr->field_reloc_len,
1062 		.min_rec_size = sizeof(struct bpf_field_reloc),
1063 		.ext_info = &btf_ext->field_reloc_info,
1064 		.desc = "field_reloc",
1065 	};
1066 
1067 	return btf_ext_setup_info(btf_ext, &param);
1068 }
1069 
1070 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
1071 {
1072 	const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
1073 
1074 	if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
1075 	    data_size < hdr->hdr_len) {
1076 		pr_debug("BTF.ext header not found");
1077 		return -EINVAL;
1078 	}
1079 
1080 	if (hdr->magic != BTF_MAGIC) {
1081 		pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
1082 		return -EINVAL;
1083 	}
1084 
1085 	if (hdr->version != BTF_VERSION) {
1086 		pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
1087 		return -ENOTSUP;
1088 	}
1089 
1090 	if (hdr->flags) {
1091 		pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
1092 		return -ENOTSUP;
1093 	}
1094 
1095 	if (data_size == hdr->hdr_len) {
1096 		pr_debug("BTF.ext has no data\n");
1097 		return -EINVAL;
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 void btf_ext__free(struct btf_ext *btf_ext)
1104 {
1105 	if (IS_ERR_OR_NULL(btf_ext))
1106 		return;
1107 	free(btf_ext->data);
1108 	free(btf_ext);
1109 }
1110 
1111 struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
1112 {
1113 	struct btf_ext *btf_ext;
1114 	int err;
1115 
1116 	err = btf_ext_parse_hdr(data, size);
1117 	if (err)
1118 		return ERR_PTR(err);
1119 
1120 	btf_ext = calloc(1, sizeof(struct btf_ext));
1121 	if (!btf_ext)
1122 		return ERR_PTR(-ENOMEM);
1123 
1124 	btf_ext->data_size = size;
1125 	btf_ext->data = malloc(size);
1126 	if (!btf_ext->data) {
1127 		err = -ENOMEM;
1128 		goto done;
1129 	}
1130 	memcpy(btf_ext->data, data, size);
1131 
1132 	if (btf_ext->hdr->hdr_len <
1133 	    offsetofend(struct btf_ext_header, line_info_len))
1134 		goto done;
1135 	err = btf_ext_setup_func_info(btf_ext);
1136 	if (err)
1137 		goto done;
1138 
1139 	err = btf_ext_setup_line_info(btf_ext);
1140 	if (err)
1141 		goto done;
1142 
1143 	if (btf_ext->hdr->hdr_len <
1144 	    offsetofend(struct btf_ext_header, field_reloc_len))
1145 		goto done;
1146 	err = btf_ext_setup_field_reloc(btf_ext);
1147 	if (err)
1148 		goto done;
1149 
1150 done:
1151 	if (err) {
1152 		btf_ext__free(btf_ext);
1153 		return ERR_PTR(err);
1154 	}
1155 
1156 	return btf_ext;
1157 }
1158 
1159 const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
1160 {
1161 	*size = btf_ext->data_size;
1162 	return btf_ext->data;
1163 }
1164 
1165 static int btf_ext_reloc_info(const struct btf *btf,
1166 			      const struct btf_ext_info *ext_info,
1167 			      const char *sec_name, __u32 insns_cnt,
1168 			      void **info, __u32 *cnt)
1169 {
1170 	__u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
1171 	__u32 i, record_size, existing_len, records_len;
1172 	struct btf_ext_info_sec *sinfo;
1173 	const char *info_sec_name;
1174 	__u64 remain_len;
1175 	void *data;
1176 
1177 	record_size = ext_info->rec_size;
1178 	sinfo = ext_info->info;
1179 	remain_len = ext_info->len;
1180 	while (remain_len > 0) {
1181 		records_len = sinfo->num_info * record_size;
1182 		info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
1183 		if (strcmp(info_sec_name, sec_name)) {
1184 			remain_len -= sec_hdrlen + records_len;
1185 			sinfo = (void *)sinfo + sec_hdrlen + records_len;
1186 			continue;
1187 		}
1188 
1189 		existing_len = (*cnt) * record_size;
1190 		data = realloc(*info, existing_len + records_len);
1191 		if (!data)
1192 			return -ENOMEM;
1193 
1194 		memcpy(data + existing_len, sinfo->data, records_len);
1195 		/* adjust insn_off only, the rest data will be passed
1196 		 * to the kernel.
1197 		 */
1198 		for (i = 0; i < sinfo->num_info; i++) {
1199 			__u32 *insn_off;
1200 
1201 			insn_off = data + existing_len + (i * record_size);
1202 			*insn_off = *insn_off / sizeof(struct bpf_insn) +
1203 				insns_cnt;
1204 		}
1205 		*info = data;
1206 		*cnt += sinfo->num_info;
1207 		return 0;
1208 	}
1209 
1210 	return -ENOENT;
1211 }
1212 
1213 int btf_ext__reloc_func_info(const struct btf *btf,
1214 			     const struct btf_ext *btf_ext,
1215 			     const char *sec_name, __u32 insns_cnt,
1216 			     void **func_info, __u32 *cnt)
1217 {
1218 	return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
1219 				  insns_cnt, func_info, cnt);
1220 }
1221 
1222 int btf_ext__reloc_line_info(const struct btf *btf,
1223 			     const struct btf_ext *btf_ext,
1224 			     const char *sec_name, __u32 insns_cnt,
1225 			     void **line_info, __u32 *cnt)
1226 {
1227 	return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
1228 				  insns_cnt, line_info, cnt);
1229 }
1230 
1231 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
1232 {
1233 	return btf_ext->func_info.rec_size;
1234 }
1235 
1236 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
1237 {
1238 	return btf_ext->line_info.rec_size;
1239 }
1240 
1241 struct btf_dedup;
1242 
1243 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1244 				       const struct btf_dedup_opts *opts);
1245 static void btf_dedup_free(struct btf_dedup *d);
1246 static int btf_dedup_strings(struct btf_dedup *d);
1247 static int btf_dedup_prim_types(struct btf_dedup *d);
1248 static int btf_dedup_struct_types(struct btf_dedup *d);
1249 static int btf_dedup_ref_types(struct btf_dedup *d);
1250 static int btf_dedup_compact_types(struct btf_dedup *d);
1251 static int btf_dedup_remap_types(struct btf_dedup *d);
1252 
1253 /*
1254  * Deduplicate BTF types and strings.
1255  *
1256  * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
1257  * section with all BTF type descriptors and string data. It overwrites that
1258  * memory in-place with deduplicated types and strings without any loss of
1259  * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
1260  * is provided, all the strings referenced from .BTF.ext section are honored
1261  * and updated to point to the right offsets after deduplication.
1262  *
1263  * If function returns with error, type/string data might be garbled and should
1264  * be discarded.
1265  *
1266  * More verbose and detailed description of both problem btf_dedup is solving,
1267  * as well as solution could be found at:
1268  * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
1269  *
1270  * Problem description and justification
1271  * =====================================
1272  *
1273  * BTF type information is typically emitted either as a result of conversion
1274  * from DWARF to BTF or directly by compiler. In both cases, each compilation
1275  * unit contains information about a subset of all the types that are used
1276  * in an application. These subsets are frequently overlapping and contain a lot
1277  * of duplicated information when later concatenated together into a single
1278  * binary. This algorithm ensures that each unique type is represented by single
1279  * BTF type descriptor, greatly reducing resulting size of BTF data.
1280  *
1281  * Compilation unit isolation and subsequent duplication of data is not the only
1282  * problem. The same type hierarchy (e.g., struct and all the type that struct
1283  * references) in different compilation units can be represented in BTF to
1284  * various degrees of completeness (or, rather, incompleteness) due to
1285  * struct/union forward declarations.
1286  *
1287  * Let's take a look at an example, that we'll use to better understand the
1288  * problem (and solution). Suppose we have two compilation units, each using
1289  * same `struct S`, but each of them having incomplete type information about
1290  * struct's fields:
1291  *
1292  * // CU #1:
1293  * struct S;
1294  * struct A {
1295  *	int a;
1296  *	struct A* self;
1297  *	struct S* parent;
1298  * };
1299  * struct B;
1300  * struct S {
1301  *	struct A* a_ptr;
1302  *	struct B* b_ptr;
1303  * };
1304  *
1305  * // CU #2:
1306  * struct S;
1307  * struct A;
1308  * struct B {
1309  *	int b;
1310  *	struct B* self;
1311  *	struct S* parent;
1312  * };
1313  * struct S {
1314  *	struct A* a_ptr;
1315  *	struct B* b_ptr;
1316  * };
1317  *
1318  * In case of CU #1, BTF data will know only that `struct B` exist (but no
1319  * more), but will know the complete type information about `struct A`. While
1320  * for CU #2, it will know full type information about `struct B`, but will
1321  * only know about forward declaration of `struct A` (in BTF terms, it will
1322  * have `BTF_KIND_FWD` type descriptor with name `B`).
1323  *
1324  * This compilation unit isolation means that it's possible that there is no
1325  * single CU with complete type information describing structs `S`, `A`, and
1326  * `B`. Also, we might get tons of duplicated and redundant type information.
1327  *
1328  * Additional complication we need to keep in mind comes from the fact that
1329  * types, in general, can form graphs containing cycles, not just DAGs.
1330  *
1331  * While algorithm does deduplication, it also merges and resolves type
1332  * information (unless disabled throught `struct btf_opts`), whenever possible.
1333  * E.g., in the example above with two compilation units having partial type
1334  * information for structs `A` and `B`, the output of algorithm will emit
1335  * a single copy of each BTF type that describes structs `A`, `B`, and `S`
1336  * (as well as type information for `int` and pointers), as if they were defined
1337  * in a single compilation unit as:
1338  *
1339  * struct A {
1340  *	int a;
1341  *	struct A* self;
1342  *	struct S* parent;
1343  * };
1344  * struct B {
1345  *	int b;
1346  *	struct B* self;
1347  *	struct S* parent;
1348  * };
1349  * struct S {
1350  *	struct A* a_ptr;
1351  *	struct B* b_ptr;
1352  * };
1353  *
1354  * Algorithm summary
1355  * =================
1356  *
1357  * Algorithm completes its work in 6 separate passes:
1358  *
1359  * 1. Strings deduplication.
1360  * 2. Primitive types deduplication (int, enum, fwd).
1361  * 3. Struct/union types deduplication.
1362  * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
1363  *    protos, and const/volatile/restrict modifiers).
1364  * 5. Types compaction.
1365  * 6. Types remapping.
1366  *
1367  * Algorithm determines canonical type descriptor, which is a single
1368  * representative type for each truly unique type. This canonical type is the
1369  * one that will go into final deduplicated BTF type information. For
1370  * struct/unions, it is also the type that algorithm will merge additional type
1371  * information into (while resolving FWDs), as it discovers it from data in
1372  * other CUs. Each input BTF type eventually gets either mapped to itself, if
1373  * that type is canonical, or to some other type, if that type is equivalent
1374  * and was chosen as canonical representative. This mapping is stored in
1375  * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
1376  * FWD type got resolved to.
1377  *
1378  * To facilitate fast discovery of canonical types, we also maintain canonical
1379  * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
1380  * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
1381  * that match that signature. With sufficiently good choice of type signature
1382  * hashing function, we can limit number of canonical types for each unique type
1383  * signature to a very small number, allowing to find canonical type for any
1384  * duplicated type very quickly.
1385  *
1386  * Struct/union deduplication is the most critical part and algorithm for
1387  * deduplicating structs/unions is described in greater details in comments for
1388  * `btf_dedup_is_equiv` function.
1389  */
1390 int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
1391 	       const struct btf_dedup_opts *opts)
1392 {
1393 	struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
1394 	int err;
1395 
1396 	if (IS_ERR(d)) {
1397 		pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
1398 		return -EINVAL;
1399 	}
1400 
1401 	err = btf_dedup_strings(d);
1402 	if (err < 0) {
1403 		pr_debug("btf_dedup_strings failed:%d\n", err);
1404 		goto done;
1405 	}
1406 	err = btf_dedup_prim_types(d);
1407 	if (err < 0) {
1408 		pr_debug("btf_dedup_prim_types failed:%d\n", err);
1409 		goto done;
1410 	}
1411 	err = btf_dedup_struct_types(d);
1412 	if (err < 0) {
1413 		pr_debug("btf_dedup_struct_types failed:%d\n", err);
1414 		goto done;
1415 	}
1416 	err = btf_dedup_ref_types(d);
1417 	if (err < 0) {
1418 		pr_debug("btf_dedup_ref_types failed:%d\n", err);
1419 		goto done;
1420 	}
1421 	err = btf_dedup_compact_types(d);
1422 	if (err < 0) {
1423 		pr_debug("btf_dedup_compact_types failed:%d\n", err);
1424 		goto done;
1425 	}
1426 	err = btf_dedup_remap_types(d);
1427 	if (err < 0) {
1428 		pr_debug("btf_dedup_remap_types failed:%d\n", err);
1429 		goto done;
1430 	}
1431 
1432 done:
1433 	btf_dedup_free(d);
1434 	return err;
1435 }
1436 
1437 #define BTF_UNPROCESSED_ID ((__u32)-1)
1438 #define BTF_IN_PROGRESS_ID ((__u32)-2)
1439 
1440 struct btf_dedup {
1441 	/* .BTF section to be deduped in-place */
1442 	struct btf *btf;
1443 	/*
1444 	 * Optional .BTF.ext section. When provided, any strings referenced
1445 	 * from it will be taken into account when deduping strings
1446 	 */
1447 	struct btf_ext *btf_ext;
1448 	/*
1449 	 * This is a map from any type's signature hash to a list of possible
1450 	 * canonical representative type candidates. Hash collisions are
1451 	 * ignored, so even types of various kinds can share same list of
1452 	 * candidates, which is fine because we rely on subsequent
1453 	 * btf_xxx_equal() checks to authoritatively verify type equality.
1454 	 */
1455 	struct hashmap *dedup_table;
1456 	/* Canonical types map */
1457 	__u32 *map;
1458 	/* Hypothetical mapping, used during type graph equivalence checks */
1459 	__u32 *hypot_map;
1460 	__u32 *hypot_list;
1461 	size_t hypot_cnt;
1462 	size_t hypot_cap;
1463 	/* Various option modifying behavior of algorithm */
1464 	struct btf_dedup_opts opts;
1465 };
1466 
1467 struct btf_str_ptr {
1468 	const char *str;
1469 	__u32 new_off;
1470 	bool used;
1471 };
1472 
1473 struct btf_str_ptrs {
1474 	struct btf_str_ptr *ptrs;
1475 	const char *data;
1476 	__u32 cnt;
1477 	__u32 cap;
1478 };
1479 
1480 static long hash_combine(long h, long value)
1481 {
1482 	return h * 31 + value;
1483 }
1484 
1485 #define for_each_dedup_cand(d, node, hash) \
1486 	hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
1487 
1488 static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
1489 {
1490 	return hashmap__append(d->dedup_table,
1491 			       (void *)hash, (void *)(long)type_id);
1492 }
1493 
1494 static int btf_dedup_hypot_map_add(struct btf_dedup *d,
1495 				   __u32 from_id, __u32 to_id)
1496 {
1497 	if (d->hypot_cnt == d->hypot_cap) {
1498 		__u32 *new_list;
1499 
1500 		d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
1501 		new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap);
1502 		if (!new_list)
1503 			return -ENOMEM;
1504 		d->hypot_list = new_list;
1505 	}
1506 	d->hypot_list[d->hypot_cnt++] = from_id;
1507 	d->hypot_map[from_id] = to_id;
1508 	return 0;
1509 }
1510 
1511 static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
1512 {
1513 	int i;
1514 
1515 	for (i = 0; i < d->hypot_cnt; i++)
1516 		d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
1517 	d->hypot_cnt = 0;
1518 }
1519 
1520 static void btf_dedup_free(struct btf_dedup *d)
1521 {
1522 	hashmap__free(d->dedup_table);
1523 	d->dedup_table = NULL;
1524 
1525 	free(d->map);
1526 	d->map = NULL;
1527 
1528 	free(d->hypot_map);
1529 	d->hypot_map = NULL;
1530 
1531 	free(d->hypot_list);
1532 	d->hypot_list = NULL;
1533 
1534 	free(d);
1535 }
1536 
1537 static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
1538 {
1539 	return (size_t)key;
1540 }
1541 
1542 static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
1543 {
1544 	return 0;
1545 }
1546 
1547 static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
1548 {
1549 	return k1 == k2;
1550 }
1551 
1552 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1553 				       const struct btf_dedup_opts *opts)
1554 {
1555 	struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
1556 	hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
1557 	int i, err = 0;
1558 
1559 	if (!d)
1560 		return ERR_PTR(-ENOMEM);
1561 
1562 	d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
1563 	/* dedup_table_size is now used only to force collisions in tests */
1564 	if (opts && opts->dedup_table_size == 1)
1565 		hash_fn = btf_dedup_collision_hash_fn;
1566 
1567 	d->btf = btf;
1568 	d->btf_ext = btf_ext;
1569 
1570 	d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
1571 	if (IS_ERR(d->dedup_table)) {
1572 		err = PTR_ERR(d->dedup_table);
1573 		d->dedup_table = NULL;
1574 		goto done;
1575 	}
1576 
1577 	d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1578 	if (!d->map) {
1579 		err = -ENOMEM;
1580 		goto done;
1581 	}
1582 	/* special BTF "void" type is made canonical immediately */
1583 	d->map[0] = 0;
1584 	for (i = 1; i <= btf->nr_types; i++) {
1585 		struct btf_type *t = d->btf->types[i];
1586 
1587 		/* VAR and DATASEC are never deduped and are self-canonical */
1588 		if (btf_is_var(t) || btf_is_datasec(t))
1589 			d->map[i] = i;
1590 		else
1591 			d->map[i] = BTF_UNPROCESSED_ID;
1592 	}
1593 
1594 	d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1595 	if (!d->hypot_map) {
1596 		err = -ENOMEM;
1597 		goto done;
1598 	}
1599 	for (i = 0; i <= btf->nr_types; i++)
1600 		d->hypot_map[i] = BTF_UNPROCESSED_ID;
1601 
1602 done:
1603 	if (err) {
1604 		btf_dedup_free(d);
1605 		return ERR_PTR(err);
1606 	}
1607 
1608 	return d;
1609 }
1610 
1611 typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
1612 
1613 /*
1614  * Iterate over all possible places in .BTF and .BTF.ext that can reference
1615  * string and pass pointer to it to a provided callback `fn`.
1616  */
1617 static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
1618 {
1619 	void *line_data_cur, *line_data_end;
1620 	int i, j, r, rec_size;
1621 	struct btf_type *t;
1622 
1623 	for (i = 1; i <= d->btf->nr_types; i++) {
1624 		t = d->btf->types[i];
1625 		r = fn(&t->name_off, ctx);
1626 		if (r)
1627 			return r;
1628 
1629 		switch (btf_kind(t)) {
1630 		case BTF_KIND_STRUCT:
1631 		case BTF_KIND_UNION: {
1632 			struct btf_member *m = btf_members(t);
1633 			__u16 vlen = btf_vlen(t);
1634 
1635 			for (j = 0; j < vlen; j++) {
1636 				r = fn(&m->name_off, ctx);
1637 				if (r)
1638 					return r;
1639 				m++;
1640 			}
1641 			break;
1642 		}
1643 		case BTF_KIND_ENUM: {
1644 			struct btf_enum *m = btf_enum(t);
1645 			__u16 vlen = btf_vlen(t);
1646 
1647 			for (j = 0; j < vlen; j++) {
1648 				r = fn(&m->name_off, ctx);
1649 				if (r)
1650 					return r;
1651 				m++;
1652 			}
1653 			break;
1654 		}
1655 		case BTF_KIND_FUNC_PROTO: {
1656 			struct btf_param *m = btf_params(t);
1657 			__u16 vlen = btf_vlen(t);
1658 
1659 			for (j = 0; j < vlen; j++) {
1660 				r = fn(&m->name_off, ctx);
1661 				if (r)
1662 					return r;
1663 				m++;
1664 			}
1665 			break;
1666 		}
1667 		default:
1668 			break;
1669 		}
1670 	}
1671 
1672 	if (!d->btf_ext)
1673 		return 0;
1674 
1675 	line_data_cur = d->btf_ext->line_info.info;
1676 	line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
1677 	rec_size = d->btf_ext->line_info.rec_size;
1678 
1679 	while (line_data_cur < line_data_end) {
1680 		struct btf_ext_info_sec *sec = line_data_cur;
1681 		struct bpf_line_info_min *line_info;
1682 		__u32 num_info = sec->num_info;
1683 
1684 		r = fn(&sec->sec_name_off, ctx);
1685 		if (r)
1686 			return r;
1687 
1688 		line_data_cur += sizeof(struct btf_ext_info_sec);
1689 		for (i = 0; i < num_info; i++) {
1690 			line_info = line_data_cur;
1691 			r = fn(&line_info->file_name_off, ctx);
1692 			if (r)
1693 				return r;
1694 			r = fn(&line_info->line_off, ctx);
1695 			if (r)
1696 				return r;
1697 			line_data_cur += rec_size;
1698 		}
1699 	}
1700 
1701 	return 0;
1702 }
1703 
1704 static int str_sort_by_content(const void *a1, const void *a2)
1705 {
1706 	const struct btf_str_ptr *p1 = a1;
1707 	const struct btf_str_ptr *p2 = a2;
1708 
1709 	return strcmp(p1->str, p2->str);
1710 }
1711 
1712 static int str_sort_by_offset(const void *a1, const void *a2)
1713 {
1714 	const struct btf_str_ptr *p1 = a1;
1715 	const struct btf_str_ptr *p2 = a2;
1716 
1717 	if (p1->str != p2->str)
1718 		return p1->str < p2->str ? -1 : 1;
1719 	return 0;
1720 }
1721 
1722 static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
1723 {
1724 	const struct btf_str_ptr *p = pelem;
1725 
1726 	if (str_ptr != p->str)
1727 		return (const char *)str_ptr < p->str ? -1 : 1;
1728 	return 0;
1729 }
1730 
1731 static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
1732 {
1733 	struct btf_str_ptrs *strs;
1734 	struct btf_str_ptr *s;
1735 
1736 	if (*str_off_ptr == 0)
1737 		return 0;
1738 
1739 	strs = ctx;
1740 	s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1741 		    sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1742 	if (!s)
1743 		return -EINVAL;
1744 	s->used = true;
1745 	return 0;
1746 }
1747 
1748 static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
1749 {
1750 	struct btf_str_ptrs *strs;
1751 	struct btf_str_ptr *s;
1752 
1753 	if (*str_off_ptr == 0)
1754 		return 0;
1755 
1756 	strs = ctx;
1757 	s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1758 		    sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1759 	if (!s)
1760 		return -EINVAL;
1761 	*str_off_ptr = s->new_off;
1762 	return 0;
1763 }
1764 
1765 /*
1766  * Dedup string and filter out those that are not referenced from either .BTF
1767  * or .BTF.ext (if provided) sections.
1768  *
1769  * This is done by building index of all strings in BTF's string section,
1770  * then iterating over all entities that can reference strings (e.g., type
1771  * names, struct field names, .BTF.ext line info, etc) and marking corresponding
1772  * strings as used. After that all used strings are deduped and compacted into
1773  * sequential blob of memory and new offsets are calculated. Then all the string
1774  * references are iterated again and rewritten using new offsets.
1775  */
1776 static int btf_dedup_strings(struct btf_dedup *d)
1777 {
1778 	const struct btf_header *hdr = d->btf->hdr;
1779 	char *start = (char *)d->btf->nohdr_data + hdr->str_off;
1780 	char *end = start + d->btf->hdr->str_len;
1781 	char *p = start, *tmp_strs = NULL;
1782 	struct btf_str_ptrs strs = {
1783 		.cnt = 0,
1784 		.cap = 0,
1785 		.ptrs = NULL,
1786 		.data = start,
1787 	};
1788 	int i, j, err = 0, grp_idx;
1789 	bool grp_used;
1790 
1791 	/* build index of all strings */
1792 	while (p < end) {
1793 		if (strs.cnt + 1 > strs.cap) {
1794 			struct btf_str_ptr *new_ptrs;
1795 
1796 			strs.cap += max(strs.cnt / 2, 16U);
1797 			new_ptrs = realloc(strs.ptrs,
1798 					   sizeof(strs.ptrs[0]) * strs.cap);
1799 			if (!new_ptrs) {
1800 				err = -ENOMEM;
1801 				goto done;
1802 			}
1803 			strs.ptrs = new_ptrs;
1804 		}
1805 
1806 		strs.ptrs[strs.cnt].str = p;
1807 		strs.ptrs[strs.cnt].used = false;
1808 
1809 		p += strlen(p) + 1;
1810 		strs.cnt++;
1811 	}
1812 
1813 	/* temporary storage for deduplicated strings */
1814 	tmp_strs = malloc(d->btf->hdr->str_len);
1815 	if (!tmp_strs) {
1816 		err = -ENOMEM;
1817 		goto done;
1818 	}
1819 
1820 	/* mark all used strings */
1821 	strs.ptrs[0].used = true;
1822 	err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
1823 	if (err)
1824 		goto done;
1825 
1826 	/* sort strings by context, so that we can identify duplicates */
1827 	qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
1828 
1829 	/*
1830 	 * iterate groups of equal strings and if any instance in a group was
1831 	 * referenced, emit single instance and remember new offset
1832 	 */
1833 	p = tmp_strs;
1834 	grp_idx = 0;
1835 	grp_used = strs.ptrs[0].used;
1836 	/* iterate past end to avoid code duplication after loop */
1837 	for (i = 1; i <= strs.cnt; i++) {
1838 		/*
1839 		 * when i == strs.cnt, we want to skip string comparison and go
1840 		 * straight to handling last group of strings (otherwise we'd
1841 		 * need to handle last group after the loop w/ duplicated code)
1842 		 */
1843 		if (i < strs.cnt &&
1844 		    !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
1845 			grp_used = grp_used || strs.ptrs[i].used;
1846 			continue;
1847 		}
1848 
1849 		/*
1850 		 * this check would have been required after the loop to handle
1851 		 * last group of strings, but due to <= condition in a loop
1852 		 * we avoid that duplication
1853 		 */
1854 		if (grp_used) {
1855 			int new_off = p - tmp_strs;
1856 			__u32 len = strlen(strs.ptrs[grp_idx].str);
1857 
1858 			memmove(p, strs.ptrs[grp_idx].str, len + 1);
1859 			for (j = grp_idx; j < i; j++)
1860 				strs.ptrs[j].new_off = new_off;
1861 			p += len + 1;
1862 		}
1863 
1864 		if (i < strs.cnt) {
1865 			grp_idx = i;
1866 			grp_used = strs.ptrs[i].used;
1867 		}
1868 	}
1869 
1870 	/* replace original strings with deduped ones */
1871 	d->btf->hdr->str_len = p - tmp_strs;
1872 	memmove(start, tmp_strs, d->btf->hdr->str_len);
1873 	end = start + d->btf->hdr->str_len;
1874 
1875 	/* restore original order for further binary search lookups */
1876 	qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
1877 
1878 	/* remap string offsets */
1879 	err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
1880 	if (err)
1881 		goto done;
1882 
1883 	d->btf->hdr->str_len = end - start;
1884 
1885 done:
1886 	free(tmp_strs);
1887 	free(strs.ptrs);
1888 	return err;
1889 }
1890 
1891 static long btf_hash_common(struct btf_type *t)
1892 {
1893 	long h;
1894 
1895 	h = hash_combine(0, t->name_off);
1896 	h = hash_combine(h, t->info);
1897 	h = hash_combine(h, t->size);
1898 	return h;
1899 }
1900 
1901 static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
1902 {
1903 	return t1->name_off == t2->name_off &&
1904 	       t1->info == t2->info &&
1905 	       t1->size == t2->size;
1906 }
1907 
1908 /* Calculate type signature hash of INT. */
1909 static long btf_hash_int(struct btf_type *t)
1910 {
1911 	__u32 info = *(__u32 *)(t + 1);
1912 	long h;
1913 
1914 	h = btf_hash_common(t);
1915 	h = hash_combine(h, info);
1916 	return h;
1917 }
1918 
1919 /* Check structural equality of two INTs. */
1920 static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
1921 {
1922 	__u32 info1, info2;
1923 
1924 	if (!btf_equal_common(t1, t2))
1925 		return false;
1926 	info1 = *(__u32 *)(t1 + 1);
1927 	info2 = *(__u32 *)(t2 + 1);
1928 	return info1 == info2;
1929 }
1930 
1931 /* Calculate type signature hash of ENUM. */
1932 static long btf_hash_enum(struct btf_type *t)
1933 {
1934 	long h;
1935 
1936 	/* don't hash vlen and enum members to support enum fwd resolving */
1937 	h = hash_combine(0, t->name_off);
1938 	h = hash_combine(h, t->info & ~0xffff);
1939 	h = hash_combine(h, t->size);
1940 	return h;
1941 }
1942 
1943 /* Check structural equality of two ENUMs. */
1944 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
1945 {
1946 	const struct btf_enum *m1, *m2;
1947 	__u16 vlen;
1948 	int i;
1949 
1950 	if (!btf_equal_common(t1, t2))
1951 		return false;
1952 
1953 	vlen = btf_vlen(t1);
1954 	m1 = btf_enum(t1);
1955 	m2 = btf_enum(t2);
1956 	for (i = 0; i < vlen; i++) {
1957 		if (m1->name_off != m2->name_off || m1->val != m2->val)
1958 			return false;
1959 		m1++;
1960 		m2++;
1961 	}
1962 	return true;
1963 }
1964 
1965 static inline bool btf_is_enum_fwd(struct btf_type *t)
1966 {
1967 	return btf_is_enum(t) && btf_vlen(t) == 0;
1968 }
1969 
1970 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
1971 {
1972 	if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
1973 		return btf_equal_enum(t1, t2);
1974 	/* ignore vlen when comparing */
1975 	return t1->name_off == t2->name_off &&
1976 	       (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
1977 	       t1->size == t2->size;
1978 }
1979 
1980 /*
1981  * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
1982  * as referenced type IDs equivalence is established separately during type
1983  * graph equivalence check algorithm.
1984  */
1985 static long btf_hash_struct(struct btf_type *t)
1986 {
1987 	const struct btf_member *member = btf_members(t);
1988 	__u32 vlen = btf_vlen(t);
1989 	long h = btf_hash_common(t);
1990 	int i;
1991 
1992 	for (i = 0; i < vlen; i++) {
1993 		h = hash_combine(h, member->name_off);
1994 		h = hash_combine(h, member->offset);
1995 		/* no hashing of referenced type ID, it can be unresolved yet */
1996 		member++;
1997 	}
1998 	return h;
1999 }
2000 
2001 /*
2002  * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
2003  * IDs. This check is performed during type graph equivalence check and
2004  * referenced types equivalence is checked separately.
2005  */
2006 static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
2007 {
2008 	const struct btf_member *m1, *m2;
2009 	__u16 vlen;
2010 	int i;
2011 
2012 	if (!btf_equal_common(t1, t2))
2013 		return false;
2014 
2015 	vlen = btf_vlen(t1);
2016 	m1 = btf_members(t1);
2017 	m2 = btf_members(t2);
2018 	for (i = 0; i < vlen; i++) {
2019 		if (m1->name_off != m2->name_off || m1->offset != m2->offset)
2020 			return false;
2021 		m1++;
2022 		m2++;
2023 	}
2024 	return true;
2025 }
2026 
2027 /*
2028  * Calculate type signature hash of ARRAY, including referenced type IDs,
2029  * under assumption that they were already resolved to canonical type IDs and
2030  * are not going to change.
2031  */
2032 static long btf_hash_array(struct btf_type *t)
2033 {
2034 	const struct btf_array *info = btf_array(t);
2035 	long h = btf_hash_common(t);
2036 
2037 	h = hash_combine(h, info->type);
2038 	h = hash_combine(h, info->index_type);
2039 	h = hash_combine(h, info->nelems);
2040 	return h;
2041 }
2042 
2043 /*
2044  * Check exact equality of two ARRAYs, taking into account referenced
2045  * type IDs, under assumption that they were already resolved to canonical
2046  * type IDs and are not going to change.
2047  * This function is called during reference types deduplication to compare
2048  * ARRAY to potential canonical representative.
2049  */
2050 static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
2051 {
2052 	const struct btf_array *info1, *info2;
2053 
2054 	if (!btf_equal_common(t1, t2))
2055 		return false;
2056 
2057 	info1 = btf_array(t1);
2058 	info2 = btf_array(t2);
2059 	return info1->type == info2->type &&
2060 	       info1->index_type == info2->index_type &&
2061 	       info1->nelems == info2->nelems;
2062 }
2063 
2064 /*
2065  * Check structural compatibility of two ARRAYs, ignoring referenced type
2066  * IDs. This check is performed during type graph equivalence check and
2067  * referenced types equivalence is checked separately.
2068  */
2069 static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
2070 {
2071 	if (!btf_equal_common(t1, t2))
2072 		return false;
2073 
2074 	return btf_array(t1)->nelems == btf_array(t2)->nelems;
2075 }
2076 
2077 /*
2078  * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
2079  * under assumption that they were already resolved to canonical type IDs and
2080  * are not going to change.
2081  */
2082 static long btf_hash_fnproto(struct btf_type *t)
2083 {
2084 	const struct btf_param *member = btf_params(t);
2085 	__u16 vlen = btf_vlen(t);
2086 	long h = btf_hash_common(t);
2087 	int i;
2088 
2089 	for (i = 0; i < vlen; i++) {
2090 		h = hash_combine(h, member->name_off);
2091 		h = hash_combine(h, member->type);
2092 		member++;
2093 	}
2094 	return h;
2095 }
2096 
2097 /*
2098  * Check exact equality of two FUNC_PROTOs, taking into account referenced
2099  * type IDs, under assumption that they were already resolved to canonical
2100  * type IDs and are not going to change.
2101  * This function is called during reference types deduplication to compare
2102  * FUNC_PROTO to potential canonical representative.
2103  */
2104 static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
2105 {
2106 	const struct btf_param *m1, *m2;
2107 	__u16 vlen;
2108 	int i;
2109 
2110 	if (!btf_equal_common(t1, t2))
2111 		return false;
2112 
2113 	vlen = btf_vlen(t1);
2114 	m1 = btf_params(t1);
2115 	m2 = btf_params(t2);
2116 	for (i = 0; i < vlen; i++) {
2117 		if (m1->name_off != m2->name_off || m1->type != m2->type)
2118 			return false;
2119 		m1++;
2120 		m2++;
2121 	}
2122 	return true;
2123 }
2124 
2125 /*
2126  * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
2127  * IDs. This check is performed during type graph equivalence check and
2128  * referenced types equivalence is checked separately.
2129  */
2130 static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
2131 {
2132 	const struct btf_param *m1, *m2;
2133 	__u16 vlen;
2134 	int i;
2135 
2136 	/* skip return type ID */
2137 	if (t1->name_off != t2->name_off || t1->info != t2->info)
2138 		return false;
2139 
2140 	vlen = btf_vlen(t1);
2141 	m1 = btf_params(t1);
2142 	m2 = btf_params(t2);
2143 	for (i = 0; i < vlen; i++) {
2144 		if (m1->name_off != m2->name_off)
2145 			return false;
2146 		m1++;
2147 		m2++;
2148 	}
2149 	return true;
2150 }
2151 
2152 /*
2153  * Deduplicate primitive types, that can't reference other types, by calculating
2154  * their type signature hash and comparing them with any possible canonical
2155  * candidate. If no canonical candidate matches, type itself is marked as
2156  * canonical and is added into `btf_dedup->dedup_table` as another candidate.
2157  */
2158 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
2159 {
2160 	struct btf_type *t = d->btf->types[type_id];
2161 	struct hashmap_entry *hash_entry;
2162 	struct btf_type *cand;
2163 	/* if we don't find equivalent type, then we are canonical */
2164 	__u32 new_id = type_id;
2165 	__u32 cand_id;
2166 	long h;
2167 
2168 	switch (btf_kind(t)) {
2169 	case BTF_KIND_CONST:
2170 	case BTF_KIND_VOLATILE:
2171 	case BTF_KIND_RESTRICT:
2172 	case BTF_KIND_PTR:
2173 	case BTF_KIND_TYPEDEF:
2174 	case BTF_KIND_ARRAY:
2175 	case BTF_KIND_STRUCT:
2176 	case BTF_KIND_UNION:
2177 	case BTF_KIND_FUNC:
2178 	case BTF_KIND_FUNC_PROTO:
2179 	case BTF_KIND_VAR:
2180 	case BTF_KIND_DATASEC:
2181 		return 0;
2182 
2183 	case BTF_KIND_INT:
2184 		h = btf_hash_int(t);
2185 		for_each_dedup_cand(d, hash_entry, h) {
2186 			cand_id = (__u32)(long)hash_entry->value;
2187 			cand = d->btf->types[cand_id];
2188 			if (btf_equal_int(t, cand)) {
2189 				new_id = cand_id;
2190 				break;
2191 			}
2192 		}
2193 		break;
2194 
2195 	case BTF_KIND_ENUM:
2196 		h = btf_hash_enum(t);
2197 		for_each_dedup_cand(d, hash_entry, h) {
2198 			cand_id = (__u32)(long)hash_entry->value;
2199 			cand = d->btf->types[cand_id];
2200 			if (btf_equal_enum(t, cand)) {
2201 				new_id = cand_id;
2202 				break;
2203 			}
2204 			if (d->opts.dont_resolve_fwds)
2205 				continue;
2206 			if (btf_compat_enum(t, cand)) {
2207 				if (btf_is_enum_fwd(t)) {
2208 					/* resolve fwd to full enum */
2209 					new_id = cand_id;
2210 					break;
2211 				}
2212 				/* resolve canonical enum fwd to full enum */
2213 				d->map[cand_id] = type_id;
2214 			}
2215 		}
2216 		break;
2217 
2218 	case BTF_KIND_FWD:
2219 		h = btf_hash_common(t);
2220 		for_each_dedup_cand(d, hash_entry, h) {
2221 			cand_id = (__u32)(long)hash_entry->value;
2222 			cand = d->btf->types[cand_id];
2223 			if (btf_equal_common(t, cand)) {
2224 				new_id = cand_id;
2225 				break;
2226 			}
2227 		}
2228 		break;
2229 
2230 	default:
2231 		return -EINVAL;
2232 	}
2233 
2234 	d->map[type_id] = new_id;
2235 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2236 		return -ENOMEM;
2237 
2238 	return 0;
2239 }
2240 
2241 static int btf_dedup_prim_types(struct btf_dedup *d)
2242 {
2243 	int i, err;
2244 
2245 	for (i = 1; i <= d->btf->nr_types; i++) {
2246 		err = btf_dedup_prim_type(d, i);
2247 		if (err)
2248 			return err;
2249 	}
2250 	return 0;
2251 }
2252 
2253 /*
2254  * Check whether type is already mapped into canonical one (could be to itself).
2255  */
2256 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
2257 {
2258 	return d->map[type_id] <= BTF_MAX_NR_TYPES;
2259 }
2260 
2261 /*
2262  * Resolve type ID into its canonical type ID, if any; otherwise return original
2263  * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
2264  * STRUCT/UNION link and resolve it into canonical type ID as well.
2265  */
2266 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
2267 {
2268 	while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2269 		type_id = d->map[type_id];
2270 	return type_id;
2271 }
2272 
2273 /*
2274  * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
2275  * type ID.
2276  */
2277 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
2278 {
2279 	__u32 orig_type_id = type_id;
2280 
2281 	if (!btf_is_fwd(d->btf->types[type_id]))
2282 		return type_id;
2283 
2284 	while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2285 		type_id = d->map[type_id];
2286 
2287 	if (!btf_is_fwd(d->btf->types[type_id]))
2288 		return type_id;
2289 
2290 	return orig_type_id;
2291 }
2292 
2293 
2294 static inline __u16 btf_fwd_kind(struct btf_type *t)
2295 {
2296 	return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
2297 }
2298 
2299 /*
2300  * Check equivalence of BTF type graph formed by candidate struct/union (we'll
2301  * call it "candidate graph" in this description for brevity) to a type graph
2302  * formed by (potential) canonical struct/union ("canonical graph" for brevity
2303  * here, though keep in mind that not all types in canonical graph are
2304  * necessarily canonical representatives themselves, some of them might be
2305  * duplicates or its uniqueness might not have been established yet).
2306  * Returns:
2307  *  - >0, if type graphs are equivalent;
2308  *  -  0, if not equivalent;
2309  *  - <0, on error.
2310  *
2311  * Algorithm performs side-by-side DFS traversal of both type graphs and checks
2312  * equivalence of BTF types at each step. If at any point BTF types in candidate
2313  * and canonical graphs are not compatible structurally, whole graphs are
2314  * incompatible. If types are structurally equivalent (i.e., all information
2315  * except referenced type IDs is exactly the same), a mapping from `canon_id` to
2316  * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
2317  * If a type references other types, then those referenced types are checked
2318  * for equivalence recursively.
2319  *
2320  * During DFS traversal, if we find that for current `canon_id` type we
2321  * already have some mapping in hypothetical map, we check for two possible
2322  * situations:
2323  *   - `canon_id` is mapped to exactly the same type as `cand_id`. This will
2324  *     happen when type graphs have cycles. In this case we assume those two
2325  *     types are equivalent.
2326  *   - `canon_id` is mapped to different type. This is contradiction in our
2327  *     hypothetical mapping, because same graph in canonical graph corresponds
2328  *     to two different types in candidate graph, which for equivalent type
2329  *     graphs shouldn't happen. This condition terminates equivalence check
2330  *     with negative result.
2331  *
2332  * If type graphs traversal exhausts types to check and find no contradiction,
2333  * then type graphs are equivalent.
2334  *
2335  * When checking types for equivalence, there is one special case: FWD types.
2336  * If FWD type resolution is allowed and one of the types (either from canonical
2337  * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
2338  * flag) and their names match, hypothetical mapping is updated to point from
2339  * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
2340  * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
2341  *
2342  * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
2343  * if there are two exactly named (or anonymous) structs/unions that are
2344  * compatible structurally, one of which has FWD field, while other is concrete
2345  * STRUCT/UNION, but according to C sources they are different structs/unions
2346  * that are referencing different types with the same name. This is extremely
2347  * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
2348  * this logic is causing problems.
2349  *
2350  * Doing FWD resolution means that both candidate and/or canonical graphs can
2351  * consists of portions of the graph that come from multiple compilation units.
2352  * This is due to the fact that types within single compilation unit are always
2353  * deduplicated and FWDs are already resolved, if referenced struct/union
2354  * definiton is available. So, if we had unresolved FWD and found corresponding
2355  * STRUCT/UNION, they will be from different compilation units. This
2356  * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
2357  * type graph will likely have at least two different BTF types that describe
2358  * same type (e.g., most probably there will be two different BTF types for the
2359  * same 'int' primitive type) and could even have "overlapping" parts of type
2360  * graph that describe same subset of types.
2361  *
2362  * This in turn means that our assumption that each type in canonical graph
2363  * must correspond to exactly one type in candidate graph might not hold
2364  * anymore and will make it harder to detect contradictions using hypothetical
2365  * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
2366  * resolution only in canonical graph. FWDs in candidate graphs are never
2367  * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
2368  * that can occur:
2369  *   - Both types in canonical and candidate graphs are FWDs. If they are
2370  *     structurally equivalent, then they can either be both resolved to the
2371  *     same STRUCT/UNION or not resolved at all. In both cases they are
2372  *     equivalent and there is no need to resolve FWD on candidate side.
2373  *   - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
2374  *     so nothing to resolve as well, algorithm will check equivalence anyway.
2375  *   - Type in canonical graph is FWD, while type in candidate is concrete
2376  *     STRUCT/UNION. In this case candidate graph comes from single compilation
2377  *     unit, so there is exactly one BTF type for each unique C type. After
2378  *     resolving FWD into STRUCT/UNION, there might be more than one BTF type
2379  *     in canonical graph mapping to single BTF type in candidate graph, but
2380  *     because hypothetical mapping maps from canonical to candidate types, it's
2381  *     alright, and we still maintain the property of having single `canon_id`
2382  *     mapping to single `cand_id` (there could be two different `canon_id`
2383  *     mapped to the same `cand_id`, but it's not contradictory).
2384  *   - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
2385  *     graph is FWD. In this case we are just going to check compatibility of
2386  *     STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
2387  *     assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
2388  *     a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
2389  *     turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
2390  *     canonical graph.
2391  */
2392 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
2393 			      __u32 canon_id)
2394 {
2395 	struct btf_type *cand_type;
2396 	struct btf_type *canon_type;
2397 	__u32 hypot_type_id;
2398 	__u16 cand_kind;
2399 	__u16 canon_kind;
2400 	int i, eq;
2401 
2402 	/* if both resolve to the same canonical, they must be equivalent */
2403 	if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
2404 		return 1;
2405 
2406 	canon_id = resolve_fwd_id(d, canon_id);
2407 
2408 	hypot_type_id = d->hypot_map[canon_id];
2409 	if (hypot_type_id <= BTF_MAX_NR_TYPES)
2410 		return hypot_type_id == cand_id;
2411 
2412 	if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
2413 		return -ENOMEM;
2414 
2415 	cand_type = d->btf->types[cand_id];
2416 	canon_type = d->btf->types[canon_id];
2417 	cand_kind = btf_kind(cand_type);
2418 	canon_kind = btf_kind(canon_type);
2419 
2420 	if (cand_type->name_off != canon_type->name_off)
2421 		return 0;
2422 
2423 	/* FWD <--> STRUCT/UNION equivalence check, if enabled */
2424 	if (!d->opts.dont_resolve_fwds
2425 	    && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
2426 	    && cand_kind != canon_kind) {
2427 		__u16 real_kind;
2428 		__u16 fwd_kind;
2429 
2430 		if (cand_kind == BTF_KIND_FWD) {
2431 			real_kind = canon_kind;
2432 			fwd_kind = btf_fwd_kind(cand_type);
2433 		} else {
2434 			real_kind = cand_kind;
2435 			fwd_kind = btf_fwd_kind(canon_type);
2436 		}
2437 		return fwd_kind == real_kind;
2438 	}
2439 
2440 	if (cand_kind != canon_kind)
2441 		return 0;
2442 
2443 	switch (cand_kind) {
2444 	case BTF_KIND_INT:
2445 		return btf_equal_int(cand_type, canon_type);
2446 
2447 	case BTF_KIND_ENUM:
2448 		if (d->opts.dont_resolve_fwds)
2449 			return btf_equal_enum(cand_type, canon_type);
2450 		else
2451 			return btf_compat_enum(cand_type, canon_type);
2452 
2453 	case BTF_KIND_FWD:
2454 		return btf_equal_common(cand_type, canon_type);
2455 
2456 	case BTF_KIND_CONST:
2457 	case BTF_KIND_VOLATILE:
2458 	case BTF_KIND_RESTRICT:
2459 	case BTF_KIND_PTR:
2460 	case BTF_KIND_TYPEDEF:
2461 	case BTF_KIND_FUNC:
2462 		if (cand_type->info != canon_type->info)
2463 			return 0;
2464 		return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2465 
2466 	case BTF_KIND_ARRAY: {
2467 		const struct btf_array *cand_arr, *canon_arr;
2468 
2469 		if (!btf_compat_array(cand_type, canon_type))
2470 			return 0;
2471 		cand_arr = btf_array(cand_type);
2472 		canon_arr = btf_array(canon_type);
2473 		eq = btf_dedup_is_equiv(d,
2474 			cand_arr->index_type, canon_arr->index_type);
2475 		if (eq <= 0)
2476 			return eq;
2477 		return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
2478 	}
2479 
2480 	case BTF_KIND_STRUCT:
2481 	case BTF_KIND_UNION: {
2482 		const struct btf_member *cand_m, *canon_m;
2483 		__u16 vlen;
2484 
2485 		if (!btf_shallow_equal_struct(cand_type, canon_type))
2486 			return 0;
2487 		vlen = btf_vlen(cand_type);
2488 		cand_m = btf_members(cand_type);
2489 		canon_m = btf_members(canon_type);
2490 		for (i = 0; i < vlen; i++) {
2491 			eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
2492 			if (eq <= 0)
2493 				return eq;
2494 			cand_m++;
2495 			canon_m++;
2496 		}
2497 
2498 		return 1;
2499 	}
2500 
2501 	case BTF_KIND_FUNC_PROTO: {
2502 		const struct btf_param *cand_p, *canon_p;
2503 		__u16 vlen;
2504 
2505 		if (!btf_compat_fnproto(cand_type, canon_type))
2506 			return 0;
2507 		eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2508 		if (eq <= 0)
2509 			return eq;
2510 		vlen = btf_vlen(cand_type);
2511 		cand_p = btf_params(cand_type);
2512 		canon_p = btf_params(canon_type);
2513 		for (i = 0; i < vlen; i++) {
2514 			eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
2515 			if (eq <= 0)
2516 				return eq;
2517 			cand_p++;
2518 			canon_p++;
2519 		}
2520 		return 1;
2521 	}
2522 
2523 	default:
2524 		return -EINVAL;
2525 	}
2526 	return 0;
2527 }
2528 
2529 /*
2530  * Use hypothetical mapping, produced by successful type graph equivalence
2531  * check, to augment existing struct/union canonical mapping, where possible.
2532  *
2533  * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
2534  * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
2535  * it doesn't matter if FWD type was part of canonical graph or candidate one,
2536  * we are recording the mapping anyway. As opposed to carefulness required
2537  * for struct/union correspondence mapping (described below), for FWD resolution
2538  * it's not important, as by the time that FWD type (reference type) will be
2539  * deduplicated all structs/unions will be deduped already anyway.
2540  *
2541  * Recording STRUCT/UNION mapping is purely a performance optimization and is
2542  * not required for correctness. It needs to be done carefully to ensure that
2543  * struct/union from candidate's type graph is not mapped into corresponding
2544  * struct/union from canonical type graph that itself hasn't been resolved into
2545  * canonical representative. The only guarantee we have is that canonical
2546  * struct/union was determined as canonical and that won't change. But any
2547  * types referenced through that struct/union fields could have been not yet
2548  * resolved, so in case like that it's too early to establish any kind of
2549  * correspondence between structs/unions.
2550  *
2551  * No canonical correspondence is derived for primitive types (they are already
2552  * deduplicated completely already anyway) or reference types (they rely on
2553  * stability of struct/union canonical relationship for equivalence checks).
2554  */
2555 static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
2556 {
2557 	__u32 cand_type_id, targ_type_id;
2558 	__u16 t_kind, c_kind;
2559 	__u32 t_id, c_id;
2560 	int i;
2561 
2562 	for (i = 0; i < d->hypot_cnt; i++) {
2563 		cand_type_id = d->hypot_list[i];
2564 		targ_type_id = d->hypot_map[cand_type_id];
2565 		t_id = resolve_type_id(d, targ_type_id);
2566 		c_id = resolve_type_id(d, cand_type_id);
2567 		t_kind = btf_kind(d->btf->types[t_id]);
2568 		c_kind = btf_kind(d->btf->types[c_id]);
2569 		/*
2570 		 * Resolve FWD into STRUCT/UNION.
2571 		 * It's ok to resolve FWD into STRUCT/UNION that's not yet
2572 		 * mapped to canonical representative (as opposed to
2573 		 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
2574 		 * eventually that struct is going to be mapped and all resolved
2575 		 * FWDs will automatically resolve to correct canonical
2576 		 * representative. This will happen before ref type deduping,
2577 		 * which critically depends on stability of these mapping. This
2578 		 * stability is not a requirement for STRUCT/UNION equivalence
2579 		 * checks, though.
2580 		 */
2581 		if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
2582 			d->map[c_id] = t_id;
2583 		else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
2584 			d->map[t_id] = c_id;
2585 
2586 		if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
2587 		    c_kind != BTF_KIND_FWD &&
2588 		    is_type_mapped(d, c_id) &&
2589 		    !is_type_mapped(d, t_id)) {
2590 			/*
2591 			 * as a perf optimization, we can map struct/union
2592 			 * that's part of type graph we just verified for
2593 			 * equivalence. We can do that for struct/union that has
2594 			 * canonical representative only, though.
2595 			 */
2596 			d->map[t_id] = c_id;
2597 		}
2598 	}
2599 }
2600 
2601 /*
2602  * Deduplicate struct/union types.
2603  *
2604  * For each struct/union type its type signature hash is calculated, taking
2605  * into account type's name, size, number, order and names of fields, but
2606  * ignoring type ID's referenced from fields, because they might not be deduped
2607  * completely until after reference types deduplication phase. This type hash
2608  * is used to iterate over all potential canonical types, sharing same hash.
2609  * For each canonical candidate we check whether type graphs that they form
2610  * (through referenced types in fields and so on) are equivalent using algorithm
2611  * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
2612  * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
2613  * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
2614  * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
2615  * potentially map other structs/unions to their canonical representatives,
2616  * if such relationship hasn't yet been established. This speeds up algorithm
2617  * by eliminating some of the duplicate work.
2618  *
2619  * If no matching canonical representative was found, struct/union is marked
2620  * as canonical for itself and is added into btf_dedup->dedup_table hash map
2621  * for further look ups.
2622  */
2623 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
2624 {
2625 	struct btf_type *cand_type, *t;
2626 	struct hashmap_entry *hash_entry;
2627 	/* if we don't find equivalent type, then we are canonical */
2628 	__u32 new_id = type_id;
2629 	__u16 kind;
2630 	long h;
2631 
2632 	/* already deduped or is in process of deduping (loop detected) */
2633 	if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2634 		return 0;
2635 
2636 	t = d->btf->types[type_id];
2637 	kind = btf_kind(t);
2638 
2639 	if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
2640 		return 0;
2641 
2642 	h = btf_hash_struct(t);
2643 	for_each_dedup_cand(d, hash_entry, h) {
2644 		__u32 cand_id = (__u32)(long)hash_entry->value;
2645 		int eq;
2646 
2647 		/*
2648 		 * Even though btf_dedup_is_equiv() checks for
2649 		 * btf_shallow_equal_struct() internally when checking two
2650 		 * structs (unions) for equivalence, we need to guard here
2651 		 * from picking matching FWD type as a dedup candidate.
2652 		 * This can happen due to hash collision. In such case just
2653 		 * relying on btf_dedup_is_equiv() would lead to potentially
2654 		 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
2655 		 * FWD and compatible STRUCT/UNION are considered equivalent.
2656 		 */
2657 		cand_type = d->btf->types[cand_id];
2658 		if (!btf_shallow_equal_struct(t, cand_type))
2659 			continue;
2660 
2661 		btf_dedup_clear_hypot_map(d);
2662 		eq = btf_dedup_is_equiv(d, type_id, cand_id);
2663 		if (eq < 0)
2664 			return eq;
2665 		if (!eq)
2666 			continue;
2667 		new_id = cand_id;
2668 		btf_dedup_merge_hypot_map(d);
2669 		break;
2670 	}
2671 
2672 	d->map[type_id] = new_id;
2673 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2674 		return -ENOMEM;
2675 
2676 	return 0;
2677 }
2678 
2679 static int btf_dedup_struct_types(struct btf_dedup *d)
2680 {
2681 	int i, err;
2682 
2683 	for (i = 1; i <= d->btf->nr_types; i++) {
2684 		err = btf_dedup_struct_type(d, i);
2685 		if (err)
2686 			return err;
2687 	}
2688 	return 0;
2689 }
2690 
2691 /*
2692  * Deduplicate reference type.
2693  *
2694  * Once all primitive and struct/union types got deduplicated, we can easily
2695  * deduplicate all other (reference) BTF types. This is done in two steps:
2696  *
2697  * 1. Resolve all referenced type IDs into their canonical type IDs. This
2698  * resolution can be done either immediately for primitive or struct/union types
2699  * (because they were deduped in previous two phases) or recursively for
2700  * reference types. Recursion will always terminate at either primitive or
2701  * struct/union type, at which point we can "unwind" chain of reference types
2702  * one by one. There is no danger of encountering cycles because in C type
2703  * system the only way to form type cycle is through struct/union, so any chain
2704  * of reference types, even those taking part in a type cycle, will inevitably
2705  * reach struct/union at some point.
2706  *
2707  * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
2708  * becomes "stable", in the sense that no further deduplication will cause
2709  * any changes to it. With that, it's now possible to calculate type's signature
2710  * hash (this time taking into account referenced type IDs) and loop over all
2711  * potential canonical representatives. If no match was found, current type
2712  * will become canonical representative of itself and will be added into
2713  * btf_dedup->dedup_table as another possible canonical representative.
2714  */
2715 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
2716 {
2717 	struct hashmap_entry *hash_entry;
2718 	__u32 new_id = type_id, cand_id;
2719 	struct btf_type *t, *cand;
2720 	/* if we don't find equivalent type, then we are representative type */
2721 	int ref_type_id;
2722 	long h;
2723 
2724 	if (d->map[type_id] == BTF_IN_PROGRESS_ID)
2725 		return -ELOOP;
2726 	if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2727 		return resolve_type_id(d, type_id);
2728 
2729 	t = d->btf->types[type_id];
2730 	d->map[type_id] = BTF_IN_PROGRESS_ID;
2731 
2732 	switch (btf_kind(t)) {
2733 	case BTF_KIND_CONST:
2734 	case BTF_KIND_VOLATILE:
2735 	case BTF_KIND_RESTRICT:
2736 	case BTF_KIND_PTR:
2737 	case BTF_KIND_TYPEDEF:
2738 	case BTF_KIND_FUNC:
2739 		ref_type_id = btf_dedup_ref_type(d, t->type);
2740 		if (ref_type_id < 0)
2741 			return ref_type_id;
2742 		t->type = ref_type_id;
2743 
2744 		h = btf_hash_common(t);
2745 		for_each_dedup_cand(d, hash_entry, h) {
2746 			cand_id = (__u32)(long)hash_entry->value;
2747 			cand = d->btf->types[cand_id];
2748 			if (btf_equal_common(t, cand)) {
2749 				new_id = cand_id;
2750 				break;
2751 			}
2752 		}
2753 		break;
2754 
2755 	case BTF_KIND_ARRAY: {
2756 		struct btf_array *info = btf_array(t);
2757 
2758 		ref_type_id = btf_dedup_ref_type(d, info->type);
2759 		if (ref_type_id < 0)
2760 			return ref_type_id;
2761 		info->type = ref_type_id;
2762 
2763 		ref_type_id = btf_dedup_ref_type(d, info->index_type);
2764 		if (ref_type_id < 0)
2765 			return ref_type_id;
2766 		info->index_type = ref_type_id;
2767 
2768 		h = btf_hash_array(t);
2769 		for_each_dedup_cand(d, hash_entry, h) {
2770 			cand_id = (__u32)(long)hash_entry->value;
2771 			cand = d->btf->types[cand_id];
2772 			if (btf_equal_array(t, cand)) {
2773 				new_id = cand_id;
2774 				break;
2775 			}
2776 		}
2777 		break;
2778 	}
2779 
2780 	case BTF_KIND_FUNC_PROTO: {
2781 		struct btf_param *param;
2782 		__u16 vlen;
2783 		int i;
2784 
2785 		ref_type_id = btf_dedup_ref_type(d, t->type);
2786 		if (ref_type_id < 0)
2787 			return ref_type_id;
2788 		t->type = ref_type_id;
2789 
2790 		vlen = btf_vlen(t);
2791 		param = btf_params(t);
2792 		for (i = 0; i < vlen; i++) {
2793 			ref_type_id = btf_dedup_ref_type(d, param->type);
2794 			if (ref_type_id < 0)
2795 				return ref_type_id;
2796 			param->type = ref_type_id;
2797 			param++;
2798 		}
2799 
2800 		h = btf_hash_fnproto(t);
2801 		for_each_dedup_cand(d, hash_entry, h) {
2802 			cand_id = (__u32)(long)hash_entry->value;
2803 			cand = d->btf->types[cand_id];
2804 			if (btf_equal_fnproto(t, cand)) {
2805 				new_id = cand_id;
2806 				break;
2807 			}
2808 		}
2809 		break;
2810 	}
2811 
2812 	default:
2813 		return -EINVAL;
2814 	}
2815 
2816 	d->map[type_id] = new_id;
2817 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2818 		return -ENOMEM;
2819 
2820 	return new_id;
2821 }
2822 
2823 static int btf_dedup_ref_types(struct btf_dedup *d)
2824 {
2825 	int i, err;
2826 
2827 	for (i = 1; i <= d->btf->nr_types; i++) {
2828 		err = btf_dedup_ref_type(d, i);
2829 		if (err < 0)
2830 			return err;
2831 	}
2832 	/* we won't need d->dedup_table anymore */
2833 	hashmap__free(d->dedup_table);
2834 	d->dedup_table = NULL;
2835 	return 0;
2836 }
2837 
2838 /*
2839  * Compact types.
2840  *
2841  * After we established for each type its corresponding canonical representative
2842  * type, we now can eliminate types that are not canonical and leave only
2843  * canonical ones layed out sequentially in memory by copying them over
2844  * duplicates. During compaction btf_dedup->hypot_map array is reused to store
2845  * a map from original type ID to a new compacted type ID, which will be used
2846  * during next phase to "fix up" type IDs, referenced from struct/union and
2847  * reference types.
2848  */
2849 static int btf_dedup_compact_types(struct btf_dedup *d)
2850 {
2851 	struct btf_type **new_types;
2852 	__u32 next_type_id = 1;
2853 	char *types_start, *p;
2854 	int i, len;
2855 
2856 	/* we are going to reuse hypot_map to store compaction remapping */
2857 	d->hypot_map[0] = 0;
2858 	for (i = 1; i <= d->btf->nr_types; i++)
2859 		d->hypot_map[i] = BTF_UNPROCESSED_ID;
2860 
2861 	types_start = d->btf->nohdr_data + d->btf->hdr->type_off;
2862 	p = types_start;
2863 
2864 	for (i = 1; i <= d->btf->nr_types; i++) {
2865 		if (d->map[i] != i)
2866 			continue;
2867 
2868 		len = btf_type_size(d->btf->types[i]);
2869 		if (len < 0)
2870 			return len;
2871 
2872 		memmove(p, d->btf->types[i], len);
2873 		d->hypot_map[i] = next_type_id;
2874 		d->btf->types[next_type_id] = (struct btf_type *)p;
2875 		p += len;
2876 		next_type_id++;
2877 	}
2878 
2879 	/* shrink struct btf's internal types index and update btf_header */
2880 	d->btf->nr_types = next_type_id - 1;
2881 	d->btf->types_size = d->btf->nr_types;
2882 	d->btf->hdr->type_len = p - types_start;
2883 	new_types = realloc(d->btf->types,
2884 			    (1 + d->btf->nr_types) * sizeof(struct btf_type *));
2885 	if (!new_types)
2886 		return -ENOMEM;
2887 	d->btf->types = new_types;
2888 
2889 	/* make sure string section follows type information without gaps */
2890 	d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data;
2891 	memmove(p, d->btf->strings, d->btf->hdr->str_len);
2892 	d->btf->strings = p;
2893 	p += d->btf->hdr->str_len;
2894 
2895 	d->btf->data_size = p - (char *)d->btf->data;
2896 	return 0;
2897 }
2898 
2899 /*
2900  * Figure out final (deduplicated and compacted) type ID for provided original
2901  * `type_id` by first resolving it into corresponding canonical type ID and
2902  * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
2903  * which is populated during compaction phase.
2904  */
2905 static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
2906 {
2907 	__u32 resolved_type_id, new_type_id;
2908 
2909 	resolved_type_id = resolve_type_id(d, type_id);
2910 	new_type_id = d->hypot_map[resolved_type_id];
2911 	if (new_type_id > BTF_MAX_NR_TYPES)
2912 		return -EINVAL;
2913 	return new_type_id;
2914 }
2915 
2916 /*
2917  * Remap referenced type IDs into deduped type IDs.
2918  *
2919  * After BTF types are deduplicated and compacted, their final type IDs may
2920  * differ from original ones. The map from original to a corresponding
2921  * deduped type ID is stored in btf_dedup->hypot_map and is populated during
2922  * compaction phase. During remapping phase we are rewriting all type IDs
2923  * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
2924  * their final deduped type IDs.
2925  */
2926 static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
2927 {
2928 	struct btf_type *t = d->btf->types[type_id];
2929 	int i, r;
2930 
2931 	switch (btf_kind(t)) {
2932 	case BTF_KIND_INT:
2933 	case BTF_KIND_ENUM:
2934 		break;
2935 
2936 	case BTF_KIND_FWD:
2937 	case BTF_KIND_CONST:
2938 	case BTF_KIND_VOLATILE:
2939 	case BTF_KIND_RESTRICT:
2940 	case BTF_KIND_PTR:
2941 	case BTF_KIND_TYPEDEF:
2942 	case BTF_KIND_FUNC:
2943 	case BTF_KIND_VAR:
2944 		r = btf_dedup_remap_type_id(d, t->type);
2945 		if (r < 0)
2946 			return r;
2947 		t->type = r;
2948 		break;
2949 
2950 	case BTF_KIND_ARRAY: {
2951 		struct btf_array *arr_info = btf_array(t);
2952 
2953 		r = btf_dedup_remap_type_id(d, arr_info->type);
2954 		if (r < 0)
2955 			return r;
2956 		arr_info->type = r;
2957 		r = btf_dedup_remap_type_id(d, arr_info->index_type);
2958 		if (r < 0)
2959 			return r;
2960 		arr_info->index_type = r;
2961 		break;
2962 	}
2963 
2964 	case BTF_KIND_STRUCT:
2965 	case BTF_KIND_UNION: {
2966 		struct btf_member *member = btf_members(t);
2967 		__u16 vlen = btf_vlen(t);
2968 
2969 		for (i = 0; i < vlen; i++) {
2970 			r = btf_dedup_remap_type_id(d, member->type);
2971 			if (r < 0)
2972 				return r;
2973 			member->type = r;
2974 			member++;
2975 		}
2976 		break;
2977 	}
2978 
2979 	case BTF_KIND_FUNC_PROTO: {
2980 		struct btf_param *param = btf_params(t);
2981 		__u16 vlen = btf_vlen(t);
2982 
2983 		r = btf_dedup_remap_type_id(d, t->type);
2984 		if (r < 0)
2985 			return r;
2986 		t->type = r;
2987 
2988 		for (i = 0; i < vlen; i++) {
2989 			r = btf_dedup_remap_type_id(d, param->type);
2990 			if (r < 0)
2991 				return r;
2992 			param->type = r;
2993 			param++;
2994 		}
2995 		break;
2996 	}
2997 
2998 	case BTF_KIND_DATASEC: {
2999 		struct btf_var_secinfo *var = btf_var_secinfos(t);
3000 		__u16 vlen = btf_vlen(t);
3001 
3002 		for (i = 0; i < vlen; i++) {
3003 			r = btf_dedup_remap_type_id(d, var->type);
3004 			if (r < 0)
3005 				return r;
3006 			var->type = r;
3007 			var++;
3008 		}
3009 		break;
3010 	}
3011 
3012 	default:
3013 		return -EINVAL;
3014 	}
3015 
3016 	return 0;
3017 }
3018 
3019 static int btf_dedup_remap_types(struct btf_dedup *d)
3020 {
3021 	int i, r;
3022 
3023 	for (i = 1; i <= d->btf->nr_types; i++) {
3024 		r = btf_dedup_remap_type(d, i);
3025 		if (r < 0)
3026 			return r;
3027 	}
3028 	return 0;
3029 }
3030 
3031 /*
3032  * Probe few well-known locations for vmlinux kernel image and try to load BTF
3033  * data out of it to use for target BTF.
3034  */
3035 struct btf *libbpf_find_kernel_btf(void)
3036 {
3037 	struct {
3038 		const char *path_fmt;
3039 		bool raw_btf;
3040 	} locations[] = {
3041 		/* try canonical vmlinux BTF through sysfs first */
3042 		{ "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
3043 		/* fall back to trying to find vmlinux ELF on disk otherwise */
3044 		{ "/boot/vmlinux-%1$s" },
3045 		{ "/lib/modules/%1$s/vmlinux-%1$s" },
3046 		{ "/lib/modules/%1$s/build/vmlinux" },
3047 		{ "/usr/lib/modules/%1$s/kernel/vmlinux" },
3048 		{ "/usr/lib/debug/boot/vmlinux-%1$s" },
3049 		{ "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
3050 		{ "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
3051 	};
3052 	char path[PATH_MAX + 1];
3053 	struct utsname buf;
3054 	struct btf *btf;
3055 	int i;
3056 
3057 	uname(&buf);
3058 
3059 	for (i = 0; i < ARRAY_SIZE(locations); i++) {
3060 		snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
3061 
3062 		if (access(path, R_OK))
3063 			continue;
3064 
3065 		if (locations[i].raw_btf)
3066 			btf = btf__parse_raw(path);
3067 		else
3068 			btf = btf__parse_elf(path, NULL);
3069 
3070 		pr_debug("loading kernel BTF '%s': %ld\n",
3071 			 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
3072 		if (IS_ERR(btf))
3073 			continue;
3074 
3075 		return btf;
3076 	}
3077 
3078 	pr_warn("failed to find valid kernel BTF\n");
3079 	return ERR_PTR(-ESRCH);
3080 }
3081