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