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