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