xref: /openbmc/linux/tools/lib/bpf/btf.c (revision 2fc3fc0bcdcc5429947d1e5ed8f6f0a77ba80997)
11bc38b8fSAlexei Starovoitov // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
28a138aedSMartin KaFai Lau /* Copyright (c) 2018 Facebook */
38a138aedSMartin KaFai Lau 
496408c43SYonghong Song #include <stdio.h>
58a138aedSMartin KaFai Lau #include <stdlib.h>
68a138aedSMartin KaFai Lau #include <string.h>
7e6c64855SAndrii Nakryiko #include <fcntl.h>
88a138aedSMartin KaFai Lau #include <unistd.h>
98a138aedSMartin KaFai Lau #include <errno.h>
108a138aedSMartin KaFai Lau #include <linux/err.h>
118a138aedSMartin KaFai Lau #include <linux/btf.h>
12e6c64855SAndrii Nakryiko #include <gelf.h>
138a138aedSMartin KaFai Lau #include "btf.h"
148a138aedSMartin KaFai Lau #include "bpf.h"
158461ef8bSYonghong Song #include "libbpf.h"
16d72386feSAndrii Nakryiko #include "libbpf_internal.h"
17*2fc3fc0bSAndrii Nakryiko #include "hashmap.h"
188a138aedSMartin KaFai Lau 
198a138aedSMartin KaFai Lau #define max(a, b) ((a) > (b) ? (a) : (b))
208a138aedSMartin KaFai Lau #define min(a, b) ((a) < (b) ? (a) : (b))
218a138aedSMartin KaFai Lau 
225aab392cSAndrii Nakryiko #define BTF_MAX_NR_TYPES 0x7fffffff
235aab392cSAndrii Nakryiko #define BTF_MAX_STR_OFFSET 0x7fffffff
248a138aedSMartin KaFai Lau 
2592b57121SOkash Khawaja #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
2692b57121SOkash Khawaja 		((k) == BTF_KIND_VOLATILE) || \
2792b57121SOkash Khawaja 		((k) == BTF_KIND_CONST) || \
2892b57121SOkash Khawaja 		((k) == BTF_KIND_RESTRICT))
2992b57121SOkash Khawaja 
301713d68bSDaniel Borkmann #define IS_VAR(k) ((k) == BTF_KIND_VAR)
311713d68bSDaniel Borkmann 
328a138aedSMartin KaFai Lau static struct btf_type btf_void;
338a138aedSMartin KaFai Lau 
348a138aedSMartin KaFai Lau struct btf {
358a138aedSMartin KaFai Lau 	union {
368a138aedSMartin KaFai Lau 		struct btf_header *hdr;
378a138aedSMartin KaFai Lau 		void *data;
388a138aedSMartin KaFai Lau 	};
398a138aedSMartin KaFai Lau 	struct btf_type **types;
408a138aedSMartin KaFai Lau 	const char *strings;
418a138aedSMartin KaFai Lau 	void *nohdr_data;
425b891af7SMartin KaFai Lau 	__u32 nr_types;
435b891af7SMartin KaFai Lau 	__u32 types_size;
445b891af7SMartin KaFai Lau 	__u32 data_size;
458a138aedSMartin KaFai Lau 	int fd;
468a138aedSMartin KaFai Lau };
478a138aedSMartin KaFai Lau 
483d650141SMartin KaFai Lau struct btf_ext_info {
493d650141SMartin KaFai Lau 	/*
50ae4ab4b4SAndrii Nakryiko 	 * info points to the individual info section (e.g. func_info and
51ae4ab4b4SAndrii Nakryiko 	 * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
523d650141SMartin KaFai Lau 	 */
533d650141SMartin KaFai Lau 	void *info;
543d650141SMartin KaFai Lau 	__u32 rec_size;
553d650141SMartin KaFai Lau 	__u32 len;
562993e051SYonghong Song };
572993e051SYonghong Song 
583d650141SMartin KaFai Lau struct btf_ext {
59ae4ab4b4SAndrii Nakryiko 	union {
60ae4ab4b4SAndrii Nakryiko 		struct btf_ext_header *hdr;
61ae4ab4b4SAndrii Nakryiko 		void *data;
62ae4ab4b4SAndrii Nakryiko 	};
633d650141SMartin KaFai Lau 	struct btf_ext_info func_info;
643d650141SMartin KaFai Lau 	struct btf_ext_info line_info;
65ae4ab4b4SAndrii Nakryiko 	__u32 data_size;
663d650141SMartin KaFai Lau };
673d650141SMartin KaFai Lau 
683d650141SMartin KaFai Lau struct btf_ext_info_sec {
69f0187f0bSMartin KaFai Lau 	__u32	sec_name_off;
703d650141SMartin KaFai Lau 	__u32	num_info;
713d650141SMartin KaFai Lau 	/* Followed by num_info * record_size number of bytes */
72f0187f0bSMartin KaFai Lau 	__u8	data[0];
73f0187f0bSMartin KaFai Lau };
74f0187f0bSMartin KaFai Lau 
752993e051SYonghong Song /* The minimum bpf_func_info checked by the loader */
762993e051SYonghong Song struct bpf_func_info_min {
7784ecc1f9SMartin KaFai Lau 	__u32   insn_off;
782993e051SYonghong Song 	__u32   type_id;
792993e051SYonghong Song };
802993e051SYonghong Song 
813d650141SMartin KaFai Lau /* The minimum bpf_line_info checked by the loader */
823d650141SMartin KaFai Lau struct bpf_line_info_min {
833d650141SMartin KaFai Lau 	__u32	insn_off;
843d650141SMartin KaFai Lau 	__u32	file_name_off;
853d650141SMartin KaFai Lau 	__u32	line_off;
863d650141SMartin KaFai Lau 	__u32	line_col;
873d650141SMartin KaFai Lau };
883d650141SMartin KaFai Lau 
89d7f5b5e0SYonghong Song static inline __u64 ptr_to_u64(const void *ptr)
90d7f5b5e0SYonghong Song {
91d7f5b5e0SYonghong Song 	return (__u64) (unsigned long) ptr;
92d7f5b5e0SYonghong Song }
93d7f5b5e0SYonghong Song 
948a138aedSMartin KaFai Lau static int btf_add_type(struct btf *btf, struct btf_type *t)
958a138aedSMartin KaFai Lau {
968a138aedSMartin KaFai Lau 	if (btf->types_size - btf->nr_types < 2) {
978a138aedSMartin KaFai Lau 		struct btf_type **new_types;
985b891af7SMartin KaFai Lau 		__u32 expand_by, new_size;
998a138aedSMartin KaFai Lau 
1008a138aedSMartin KaFai Lau 		if (btf->types_size == BTF_MAX_NR_TYPES)
1018a138aedSMartin KaFai Lau 			return -E2BIG;
1028a138aedSMartin KaFai Lau 
1038a138aedSMartin KaFai Lau 		expand_by = max(btf->types_size >> 2, 16);
1048a138aedSMartin KaFai Lau 		new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
1058a138aedSMartin KaFai Lau 
1068a138aedSMartin KaFai Lau 		new_types = realloc(btf->types, sizeof(*new_types) * new_size);
1078a138aedSMartin KaFai Lau 		if (!new_types)
1088a138aedSMartin KaFai Lau 			return -ENOMEM;
1098a138aedSMartin KaFai Lau 
1108a138aedSMartin KaFai Lau 		if (btf->nr_types == 0)
1118a138aedSMartin KaFai Lau 			new_types[0] = &btf_void;
1128a138aedSMartin KaFai Lau 
1138a138aedSMartin KaFai Lau 		btf->types = new_types;
1148a138aedSMartin KaFai Lau 		btf->types_size = new_size;
1158a138aedSMartin KaFai Lau 	}
1168a138aedSMartin KaFai Lau 
1178a138aedSMartin KaFai Lau 	btf->types[++(btf->nr_types)] = t;
1188a138aedSMartin KaFai Lau 
1198a138aedSMartin KaFai Lau 	return 0;
1208a138aedSMartin KaFai Lau }
1218a138aedSMartin KaFai Lau 
1228461ef8bSYonghong Song static int btf_parse_hdr(struct btf *btf)
1238a138aedSMartin KaFai Lau {
1248a138aedSMartin KaFai Lau 	const struct btf_header *hdr = btf->hdr;
1255b891af7SMartin KaFai Lau 	__u32 meta_left;
1268a138aedSMartin KaFai Lau 
1278a138aedSMartin KaFai Lau 	if (btf->data_size < sizeof(struct btf_header)) {
1288461ef8bSYonghong Song 		pr_debug("BTF header not found\n");
1298a138aedSMartin KaFai Lau 		return -EINVAL;
1308a138aedSMartin KaFai Lau 	}
1318a138aedSMartin KaFai Lau 
1328a138aedSMartin KaFai Lau 	if (hdr->magic != BTF_MAGIC) {
1338461ef8bSYonghong Song 		pr_debug("Invalid BTF magic:%x\n", hdr->magic);
1348a138aedSMartin KaFai Lau 		return -EINVAL;
1358a138aedSMartin KaFai Lau 	}
1368a138aedSMartin KaFai Lau 
1378a138aedSMartin KaFai Lau 	if (hdr->version != BTF_VERSION) {
1388461ef8bSYonghong Song 		pr_debug("Unsupported BTF version:%u\n", hdr->version);
1398a138aedSMartin KaFai Lau 		return -ENOTSUP;
1408a138aedSMartin KaFai Lau 	}
1418a138aedSMartin KaFai Lau 
1428a138aedSMartin KaFai Lau 	if (hdr->flags) {
1438461ef8bSYonghong Song 		pr_debug("Unsupported BTF flags:%x\n", hdr->flags);
1448a138aedSMartin KaFai Lau 		return -ENOTSUP;
1458a138aedSMartin KaFai Lau 	}
1468a138aedSMartin KaFai Lau 
1478a138aedSMartin KaFai Lau 	meta_left = btf->data_size - sizeof(*hdr);
1488a138aedSMartin KaFai Lau 	if (!meta_left) {
1498461ef8bSYonghong Song 		pr_debug("BTF has no data\n");
1508a138aedSMartin KaFai Lau 		return -EINVAL;
1518a138aedSMartin KaFai Lau 	}
1528a138aedSMartin KaFai Lau 
1538a138aedSMartin KaFai Lau 	if (meta_left < hdr->type_off) {
1548461ef8bSYonghong Song 		pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off);
1558a138aedSMartin KaFai Lau 		return -EINVAL;
1568a138aedSMartin KaFai Lau 	}
1578a138aedSMartin KaFai Lau 
1588a138aedSMartin KaFai Lau 	if (meta_left < hdr->str_off) {
1598461ef8bSYonghong Song 		pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off);
1608a138aedSMartin KaFai Lau 		return -EINVAL;
1618a138aedSMartin KaFai Lau 	}
1628a138aedSMartin KaFai Lau 
1638a138aedSMartin KaFai Lau 	if (hdr->type_off >= hdr->str_off) {
1648461ef8bSYonghong Song 		pr_debug("BTF type section offset >= string section offset. No type?\n");
1658a138aedSMartin KaFai Lau 		return -EINVAL;
1668a138aedSMartin KaFai Lau 	}
1678a138aedSMartin KaFai Lau 
1688a138aedSMartin KaFai Lau 	if (hdr->type_off & 0x02) {
1698461ef8bSYonghong Song 		pr_debug("BTF type section is not aligned to 4 bytes\n");
1708a138aedSMartin KaFai Lau 		return -EINVAL;
1718a138aedSMartin KaFai Lau 	}
1728a138aedSMartin KaFai Lau 
1738a138aedSMartin KaFai Lau 	btf->nohdr_data = btf->hdr + 1;
1748a138aedSMartin KaFai Lau 
1758a138aedSMartin KaFai Lau 	return 0;
1768a138aedSMartin KaFai Lau }
1778a138aedSMartin KaFai Lau 
1788461ef8bSYonghong Song static int btf_parse_str_sec(struct btf *btf)
1798a138aedSMartin KaFai Lau {
1808a138aedSMartin KaFai Lau 	const struct btf_header *hdr = btf->hdr;
1818a138aedSMartin KaFai Lau 	const char *start = btf->nohdr_data + hdr->str_off;
1828a138aedSMartin KaFai Lau 	const char *end = start + btf->hdr->str_len;
1838a138aedSMartin KaFai Lau 
1845aab392cSAndrii Nakryiko 	if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
1858a138aedSMartin KaFai Lau 	    start[0] || end[-1]) {
1868461ef8bSYonghong Song 		pr_debug("Invalid BTF string section\n");
1878a138aedSMartin KaFai Lau 		return -EINVAL;
1888a138aedSMartin KaFai Lau 	}
1898a138aedSMartin KaFai Lau 
1908a138aedSMartin KaFai Lau 	btf->strings = start;
1918a138aedSMartin KaFai Lau 
1928a138aedSMartin KaFai Lau 	return 0;
1938a138aedSMartin KaFai Lau }
1948a138aedSMartin KaFai Lau 
19569eaab04SAndrii Nakryiko static int btf_type_size(struct btf_type *t)
19669eaab04SAndrii Nakryiko {
19769eaab04SAndrii Nakryiko 	int base_size = sizeof(struct btf_type);
19869eaab04SAndrii Nakryiko 	__u16 vlen = BTF_INFO_VLEN(t->info);
19969eaab04SAndrii Nakryiko 
20069eaab04SAndrii Nakryiko 	switch (BTF_INFO_KIND(t->info)) {
20169eaab04SAndrii Nakryiko 	case BTF_KIND_FWD:
20269eaab04SAndrii Nakryiko 	case BTF_KIND_CONST:
20369eaab04SAndrii Nakryiko 	case BTF_KIND_VOLATILE:
20469eaab04SAndrii Nakryiko 	case BTF_KIND_RESTRICT:
20569eaab04SAndrii Nakryiko 	case BTF_KIND_PTR:
20669eaab04SAndrii Nakryiko 	case BTF_KIND_TYPEDEF:
20769eaab04SAndrii Nakryiko 	case BTF_KIND_FUNC:
20869eaab04SAndrii Nakryiko 		return base_size;
20969eaab04SAndrii Nakryiko 	case BTF_KIND_INT:
21069eaab04SAndrii Nakryiko 		return base_size + sizeof(__u32);
21169eaab04SAndrii Nakryiko 	case BTF_KIND_ENUM:
21269eaab04SAndrii Nakryiko 		return base_size + vlen * sizeof(struct btf_enum);
21369eaab04SAndrii Nakryiko 	case BTF_KIND_ARRAY:
21469eaab04SAndrii Nakryiko 		return base_size + sizeof(struct btf_array);
21569eaab04SAndrii Nakryiko 	case BTF_KIND_STRUCT:
21669eaab04SAndrii Nakryiko 	case BTF_KIND_UNION:
21769eaab04SAndrii Nakryiko 		return base_size + vlen * sizeof(struct btf_member);
21869eaab04SAndrii Nakryiko 	case BTF_KIND_FUNC_PROTO:
21969eaab04SAndrii Nakryiko 		return base_size + vlen * sizeof(struct btf_param);
2201713d68bSDaniel Borkmann 	case BTF_KIND_VAR:
2211713d68bSDaniel Borkmann 		return base_size + sizeof(struct btf_var);
2221713d68bSDaniel Borkmann 	case BTF_KIND_DATASEC:
2231713d68bSDaniel Borkmann 		return base_size + vlen * sizeof(struct btf_var_secinfo);
22469eaab04SAndrii Nakryiko 	default:
22569eaab04SAndrii Nakryiko 		pr_debug("Unsupported BTF_KIND:%u\n", BTF_INFO_KIND(t->info));
22669eaab04SAndrii Nakryiko 		return -EINVAL;
22769eaab04SAndrii Nakryiko 	}
22869eaab04SAndrii Nakryiko }
22969eaab04SAndrii Nakryiko 
2308461ef8bSYonghong Song static int btf_parse_type_sec(struct btf *btf)
2318a138aedSMartin KaFai Lau {
2328a138aedSMartin KaFai Lau 	struct btf_header *hdr = btf->hdr;
2338a138aedSMartin KaFai Lau 	void *nohdr_data = btf->nohdr_data;
2348a138aedSMartin KaFai Lau 	void *next_type = nohdr_data + hdr->type_off;
2358a138aedSMartin KaFai Lau 	void *end_type = nohdr_data + hdr->str_off;
2368a138aedSMartin KaFai Lau 
2378a138aedSMartin KaFai Lau 	while (next_type < end_type) {
2388a138aedSMartin KaFai Lau 		struct btf_type *t = next_type;
23969eaab04SAndrii Nakryiko 		int type_size;
2408a138aedSMartin KaFai Lau 		int err;
2418a138aedSMartin KaFai Lau 
24269eaab04SAndrii Nakryiko 		type_size = btf_type_size(t);
24369eaab04SAndrii Nakryiko 		if (type_size < 0)
24469eaab04SAndrii Nakryiko 			return type_size;
24569eaab04SAndrii Nakryiko 		next_type += type_size;
2468a138aedSMartin KaFai Lau 		err = btf_add_type(btf, t);
2478a138aedSMartin KaFai Lau 		if (err)
2488a138aedSMartin KaFai Lau 			return err;
2498a138aedSMartin KaFai Lau 	}
2508a138aedSMartin KaFai Lau 
2518a138aedSMartin KaFai Lau 	return 0;
2528a138aedSMartin KaFai Lau }
2538a138aedSMartin KaFai Lau 
2549c651127SAndrii Nakryiko __u32 btf__get_nr_types(const struct btf *btf)
2559c651127SAndrii Nakryiko {
2569c651127SAndrii Nakryiko 	return btf->nr_types;
2579c651127SAndrii Nakryiko }
2589c651127SAndrii Nakryiko 
25938d5d3b3SMartin KaFai Lau const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
2608a138aedSMartin KaFai Lau {
2618a138aedSMartin KaFai Lau 	if (type_id > btf->nr_types)
2628a138aedSMartin KaFai Lau 		return NULL;
2638a138aedSMartin KaFai Lau 
2648a138aedSMartin KaFai Lau 	return btf->types[type_id];
2658a138aedSMartin KaFai Lau }
2668a138aedSMartin KaFai Lau 
2678a138aedSMartin KaFai Lau static bool btf_type_is_void(const struct btf_type *t)
2688a138aedSMartin KaFai Lau {
2698a138aedSMartin KaFai Lau 	return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
2708a138aedSMartin KaFai Lau }
2718a138aedSMartin KaFai Lau 
2728a138aedSMartin KaFai Lau static bool btf_type_is_void_or_null(const struct btf_type *t)
2738a138aedSMartin KaFai Lau {
2748a138aedSMartin KaFai Lau 	return !t || btf_type_is_void(t);
2758a138aedSMartin KaFai Lau }
2768a138aedSMartin KaFai Lau 
2778a138aedSMartin KaFai Lau #define MAX_RESOLVE_DEPTH 32
2788a138aedSMartin KaFai Lau 
2795b891af7SMartin KaFai Lau __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
2808a138aedSMartin KaFai Lau {
2818a138aedSMartin KaFai Lau 	const struct btf_array *array;
2828a138aedSMartin KaFai Lau 	const struct btf_type *t;
2835b891af7SMartin KaFai Lau 	__u32 nelems = 1;
2845b891af7SMartin KaFai Lau 	__s64 size = -1;
2858a138aedSMartin KaFai Lau 	int i;
2868a138aedSMartin KaFai Lau 
28792b57121SOkash Khawaja 	t = btf__type_by_id(btf, type_id);
2888a138aedSMartin KaFai Lau 	for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
2898a138aedSMartin KaFai Lau 	     i++) {
2908a138aedSMartin KaFai Lau 		switch (BTF_INFO_KIND(t->info)) {
29169eaab04SAndrii Nakryiko 		case BTF_KIND_INT:
29269eaab04SAndrii Nakryiko 		case BTF_KIND_STRUCT:
29369eaab04SAndrii Nakryiko 		case BTF_KIND_UNION:
29469eaab04SAndrii Nakryiko 		case BTF_KIND_ENUM:
2951713d68bSDaniel Borkmann 		case BTF_KIND_DATASEC:
29669eaab04SAndrii Nakryiko 			size = t->size;
29769eaab04SAndrii Nakryiko 			goto done;
29869eaab04SAndrii Nakryiko 		case BTF_KIND_PTR:
29969eaab04SAndrii Nakryiko 			size = sizeof(void *);
30069eaab04SAndrii Nakryiko 			goto done;
3018a138aedSMartin KaFai Lau 		case BTF_KIND_TYPEDEF:
3028a138aedSMartin KaFai Lau 		case BTF_KIND_VOLATILE:
3038a138aedSMartin KaFai Lau 		case BTF_KIND_CONST:
3048a138aedSMartin KaFai Lau 		case BTF_KIND_RESTRICT:
3051713d68bSDaniel Borkmann 		case BTF_KIND_VAR:
3068a138aedSMartin KaFai Lau 			type_id = t->type;
3078a138aedSMartin KaFai Lau 			break;
3088a138aedSMartin KaFai Lau 		case BTF_KIND_ARRAY:
3098a138aedSMartin KaFai Lau 			array = (const struct btf_array *)(t + 1);
3108a138aedSMartin KaFai Lau 			if (nelems && array->nelems > UINT32_MAX / nelems)
3118a138aedSMartin KaFai Lau 				return -E2BIG;
3128a138aedSMartin KaFai Lau 			nelems *= array->nelems;
3138a138aedSMartin KaFai Lau 			type_id = array->type;
3148a138aedSMartin KaFai Lau 			break;
3158a138aedSMartin KaFai Lau 		default:
3168a138aedSMartin KaFai Lau 			return -EINVAL;
3178a138aedSMartin KaFai Lau 		}
3188a138aedSMartin KaFai Lau 
31992b57121SOkash Khawaja 		t = btf__type_by_id(btf, type_id);
3208a138aedSMartin KaFai Lau 	}
3218a138aedSMartin KaFai Lau 
3228a138aedSMartin KaFai Lau 	if (size < 0)
3238a138aedSMartin KaFai Lau 		return -EINVAL;
3248a138aedSMartin KaFai Lau 
32569eaab04SAndrii Nakryiko done:
3268a138aedSMartin KaFai Lau 	if (nelems && size > UINT32_MAX / nelems)
3278a138aedSMartin KaFai Lau 		return -E2BIG;
3288a138aedSMartin KaFai Lau 
3298a138aedSMartin KaFai Lau 	return nelems * size;
3308a138aedSMartin KaFai Lau }
3318a138aedSMartin KaFai Lau 
33292b57121SOkash Khawaja int btf__resolve_type(const struct btf *btf, __u32 type_id)
33392b57121SOkash Khawaja {
33492b57121SOkash Khawaja 	const struct btf_type *t;
33592b57121SOkash Khawaja 	int depth = 0;
33692b57121SOkash Khawaja 
33792b57121SOkash Khawaja 	t = btf__type_by_id(btf, type_id);
33892b57121SOkash Khawaja 	while (depth < MAX_RESOLVE_DEPTH &&
33992b57121SOkash Khawaja 	       !btf_type_is_void_or_null(t) &&
3401713d68bSDaniel Borkmann 	       (IS_MODIFIER(BTF_INFO_KIND(t->info)) ||
3411713d68bSDaniel Borkmann 		IS_VAR(BTF_INFO_KIND(t->info)))) {
34292b57121SOkash Khawaja 		type_id = t->type;
34392b57121SOkash Khawaja 		t = btf__type_by_id(btf, type_id);
34492b57121SOkash Khawaja 		depth++;
34592b57121SOkash Khawaja 	}
34692b57121SOkash Khawaja 
34792b57121SOkash Khawaja 	if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
34892b57121SOkash Khawaja 		return -EINVAL;
34992b57121SOkash Khawaja 
35092b57121SOkash Khawaja 	return type_id;
35192b57121SOkash Khawaja }
35292b57121SOkash Khawaja 
3535b891af7SMartin KaFai Lau __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
3548a138aedSMartin KaFai Lau {
3555b891af7SMartin KaFai Lau 	__u32 i;
3568a138aedSMartin KaFai Lau 
3578a138aedSMartin KaFai Lau 	if (!strcmp(type_name, "void"))
3588a138aedSMartin KaFai Lau 		return 0;
3598a138aedSMartin KaFai Lau 
3608a138aedSMartin KaFai Lau 	for (i = 1; i <= btf->nr_types; i++) {
3618a138aedSMartin KaFai Lau 		const struct btf_type *t = btf->types[i];
36292b57121SOkash Khawaja 		const char *name = btf__name_by_offset(btf, t->name_off);
3638a138aedSMartin KaFai Lau 
3648a138aedSMartin KaFai Lau 		if (name && !strcmp(type_name, name))
3658a138aedSMartin KaFai Lau 			return i;
3668a138aedSMartin KaFai Lau 	}
3678a138aedSMartin KaFai Lau 
3688a138aedSMartin KaFai Lau 	return -ENOENT;
3698a138aedSMartin KaFai Lau }
3708a138aedSMartin KaFai Lau 
3718a138aedSMartin KaFai Lau void btf__free(struct btf *btf)
3728a138aedSMartin KaFai Lau {
3738a138aedSMartin KaFai Lau 	if (!btf)
3748a138aedSMartin KaFai Lau 		return;
3758a138aedSMartin KaFai Lau 
3768a138aedSMartin KaFai Lau 	if (btf->fd != -1)
3778a138aedSMartin KaFai Lau 		close(btf->fd);
3788a138aedSMartin KaFai Lau 
3798a138aedSMartin KaFai Lau 	free(btf->data);
3808a138aedSMartin KaFai Lau 	free(btf->types);
3818a138aedSMartin KaFai Lau 	free(btf);
3828a138aedSMartin KaFai Lau }
3838a138aedSMartin KaFai Lau 
3848461ef8bSYonghong Song struct btf *btf__new(__u8 *data, __u32 size)
3858a138aedSMartin KaFai Lau {
3868a138aedSMartin KaFai Lau 	struct btf *btf;
3878a138aedSMartin KaFai Lau 	int err;
3888a138aedSMartin KaFai Lau 
3898a138aedSMartin KaFai Lau 	btf = calloc(1, sizeof(struct btf));
3908a138aedSMartin KaFai Lau 	if (!btf)
3918a138aedSMartin KaFai Lau 		return ERR_PTR(-ENOMEM);
3928a138aedSMartin KaFai Lau 
3938a138aedSMartin KaFai Lau 	btf->fd = -1;
3948a138aedSMartin KaFai Lau 
3958a138aedSMartin KaFai Lau 	btf->data = malloc(size);
3968a138aedSMartin KaFai Lau 	if (!btf->data) {
3978a138aedSMartin KaFai Lau 		err = -ENOMEM;
3988a138aedSMartin KaFai Lau 		goto done;
3998a138aedSMartin KaFai Lau 	}
4008a138aedSMartin KaFai Lau 
4018a138aedSMartin KaFai Lau 	memcpy(btf->data, data, size);
4028a138aedSMartin KaFai Lau 	btf->data_size = size;
4038a138aedSMartin KaFai Lau 
4048461ef8bSYonghong Song 	err = btf_parse_hdr(btf);
4058a138aedSMartin KaFai Lau 	if (err)
4068a138aedSMartin KaFai Lau 		goto done;
4078a138aedSMartin KaFai Lau 
4088461ef8bSYonghong Song 	err = btf_parse_str_sec(btf);
4098a138aedSMartin KaFai Lau 	if (err)
4108a138aedSMartin KaFai Lau 		goto done;
4118a138aedSMartin KaFai Lau 
4128461ef8bSYonghong Song 	err = btf_parse_type_sec(btf);
4138a138aedSMartin KaFai Lau 
4148a138aedSMartin KaFai Lau done:
4158a138aedSMartin KaFai Lau 	if (err) {
4168a138aedSMartin KaFai Lau 		btf__free(btf);
4178a138aedSMartin KaFai Lau 		return ERR_PTR(err);
4188a138aedSMartin KaFai Lau 	}
4198a138aedSMartin KaFai Lau 
4208a138aedSMartin KaFai Lau 	return btf;
4218a138aedSMartin KaFai Lau }
4228a138aedSMartin KaFai Lau 
423e6c64855SAndrii Nakryiko static bool btf_check_endianness(const GElf_Ehdr *ehdr)
424e6c64855SAndrii Nakryiko {
425e6c64855SAndrii Nakryiko #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
426e6c64855SAndrii Nakryiko 	return ehdr->e_ident[EI_DATA] == ELFDATA2LSB;
427e6c64855SAndrii Nakryiko #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
428e6c64855SAndrii Nakryiko 	return ehdr->e_ident[EI_DATA] == ELFDATA2MSB;
429e6c64855SAndrii Nakryiko #else
430e6c64855SAndrii Nakryiko # error "Unrecognized __BYTE_ORDER__"
431e6c64855SAndrii Nakryiko #endif
432e6c64855SAndrii Nakryiko }
433e6c64855SAndrii Nakryiko 
434e6c64855SAndrii Nakryiko struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
435e6c64855SAndrii Nakryiko {
436e6c64855SAndrii Nakryiko 	Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
437e6c64855SAndrii Nakryiko 	int err = 0, fd = -1, idx = 0;
438e6c64855SAndrii Nakryiko 	struct btf *btf = NULL;
439e6c64855SAndrii Nakryiko 	Elf_Scn *scn = NULL;
440e6c64855SAndrii Nakryiko 	Elf *elf = NULL;
441e6c64855SAndrii Nakryiko 	GElf_Ehdr ehdr;
442e6c64855SAndrii Nakryiko 
443e6c64855SAndrii Nakryiko 	if (elf_version(EV_CURRENT) == EV_NONE) {
444e6c64855SAndrii Nakryiko 		pr_warning("failed to init libelf for %s\n", path);
445e6c64855SAndrii Nakryiko 		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
446e6c64855SAndrii Nakryiko 	}
447e6c64855SAndrii Nakryiko 
448e6c64855SAndrii Nakryiko 	fd = open(path, O_RDONLY);
449e6c64855SAndrii Nakryiko 	if (fd < 0) {
450e6c64855SAndrii Nakryiko 		err = -errno;
451e6c64855SAndrii Nakryiko 		pr_warning("failed to open %s: %s\n", path, strerror(errno));
452e6c64855SAndrii Nakryiko 		return ERR_PTR(err);
453e6c64855SAndrii Nakryiko 	}
454e6c64855SAndrii Nakryiko 
455e6c64855SAndrii Nakryiko 	err = -LIBBPF_ERRNO__FORMAT;
456e6c64855SAndrii Nakryiko 
457e6c64855SAndrii Nakryiko 	elf = elf_begin(fd, ELF_C_READ, NULL);
458e6c64855SAndrii Nakryiko 	if (!elf) {
459e6c64855SAndrii Nakryiko 		pr_warning("failed to open %s as ELF file\n", path);
460e6c64855SAndrii Nakryiko 		goto done;
461e6c64855SAndrii Nakryiko 	}
462e6c64855SAndrii Nakryiko 	if (!gelf_getehdr(elf, &ehdr)) {
463e6c64855SAndrii Nakryiko 		pr_warning("failed to get EHDR from %s\n", path);
464e6c64855SAndrii Nakryiko 		goto done;
465e6c64855SAndrii Nakryiko 	}
466e6c64855SAndrii Nakryiko 	if (!btf_check_endianness(&ehdr)) {
467e6c64855SAndrii Nakryiko 		pr_warning("non-native ELF endianness is not supported\n");
468e6c64855SAndrii Nakryiko 		goto done;
469e6c64855SAndrii Nakryiko 	}
470e6c64855SAndrii Nakryiko 	if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
471e6c64855SAndrii Nakryiko 		pr_warning("failed to get e_shstrndx from %s\n", path);
472e6c64855SAndrii Nakryiko 		goto done;
473e6c64855SAndrii Nakryiko 	}
474e6c64855SAndrii Nakryiko 
475e6c64855SAndrii Nakryiko 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
476e6c64855SAndrii Nakryiko 		GElf_Shdr sh;
477e6c64855SAndrii Nakryiko 		char *name;
478e6c64855SAndrii Nakryiko 
479e6c64855SAndrii Nakryiko 		idx++;
480e6c64855SAndrii Nakryiko 		if (gelf_getshdr(scn, &sh) != &sh) {
481e6c64855SAndrii Nakryiko 			pr_warning("failed to get section(%d) header from %s\n",
482e6c64855SAndrii Nakryiko 				   idx, path);
483e6c64855SAndrii Nakryiko 			goto done;
484e6c64855SAndrii Nakryiko 		}
485e6c64855SAndrii Nakryiko 		name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
486e6c64855SAndrii Nakryiko 		if (!name) {
487e6c64855SAndrii Nakryiko 			pr_warning("failed to get section(%d) name from %s\n",
488e6c64855SAndrii Nakryiko 				   idx, path);
489e6c64855SAndrii Nakryiko 			goto done;
490e6c64855SAndrii Nakryiko 		}
491e6c64855SAndrii Nakryiko 		if (strcmp(name, BTF_ELF_SEC) == 0) {
492e6c64855SAndrii Nakryiko 			btf_data = elf_getdata(scn, 0);
493e6c64855SAndrii Nakryiko 			if (!btf_data) {
494e6c64855SAndrii Nakryiko 				pr_warning("failed to get section(%d, %s) data from %s\n",
495e6c64855SAndrii Nakryiko 					   idx, name, path);
496e6c64855SAndrii Nakryiko 				goto done;
497e6c64855SAndrii Nakryiko 			}
498e6c64855SAndrii Nakryiko 			continue;
499e6c64855SAndrii Nakryiko 		} else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
500e6c64855SAndrii Nakryiko 			btf_ext_data = elf_getdata(scn, 0);
501e6c64855SAndrii Nakryiko 			if (!btf_ext_data) {
502e6c64855SAndrii Nakryiko 				pr_warning("failed to get section(%d, %s) data from %s\n",
503e6c64855SAndrii Nakryiko 					   idx, name, path);
504e6c64855SAndrii Nakryiko 				goto done;
505e6c64855SAndrii Nakryiko 			}
506e6c64855SAndrii Nakryiko 			continue;
507e6c64855SAndrii Nakryiko 		}
508e6c64855SAndrii Nakryiko 	}
509e6c64855SAndrii Nakryiko 
510e6c64855SAndrii Nakryiko 	err = 0;
511e6c64855SAndrii Nakryiko 
512e6c64855SAndrii Nakryiko 	if (!btf_data) {
513e6c64855SAndrii Nakryiko 		err = -ENOENT;
514e6c64855SAndrii Nakryiko 		goto done;
515e6c64855SAndrii Nakryiko 	}
516e6c64855SAndrii Nakryiko 	btf = btf__new(btf_data->d_buf, btf_data->d_size);
517e6c64855SAndrii Nakryiko 	if (IS_ERR(btf))
518e6c64855SAndrii Nakryiko 		goto done;
519e6c64855SAndrii Nakryiko 
520e6c64855SAndrii Nakryiko 	if (btf_ext && btf_ext_data) {
521e6c64855SAndrii Nakryiko 		*btf_ext = btf_ext__new(btf_ext_data->d_buf,
522e6c64855SAndrii Nakryiko 					btf_ext_data->d_size);
523e6c64855SAndrii Nakryiko 		if (IS_ERR(*btf_ext))
524e6c64855SAndrii Nakryiko 			goto done;
525e6c64855SAndrii Nakryiko 	} else if (btf_ext) {
526e6c64855SAndrii Nakryiko 		*btf_ext = NULL;
527e6c64855SAndrii Nakryiko 	}
528e6c64855SAndrii Nakryiko done:
529e6c64855SAndrii Nakryiko 	if (elf)
530e6c64855SAndrii Nakryiko 		elf_end(elf);
531e6c64855SAndrii Nakryiko 	close(fd);
532e6c64855SAndrii Nakryiko 
533e6c64855SAndrii Nakryiko 	if (err)
534e6c64855SAndrii Nakryiko 		return ERR_PTR(err);
535e6c64855SAndrii Nakryiko 	/*
536e6c64855SAndrii Nakryiko 	 * btf is always parsed before btf_ext, so no need to clean up
537e6c64855SAndrii Nakryiko 	 * btf_ext, if btf loading failed
538e6c64855SAndrii Nakryiko 	 */
539e6c64855SAndrii Nakryiko 	if (IS_ERR(btf))
540e6c64855SAndrii Nakryiko 		return btf;
541e6c64855SAndrii Nakryiko 	if (btf_ext && IS_ERR(*btf_ext)) {
542e6c64855SAndrii Nakryiko 		btf__free(btf);
543e6c64855SAndrii Nakryiko 		err = PTR_ERR(*btf_ext);
544e6c64855SAndrii Nakryiko 		return ERR_PTR(err);
545e6c64855SAndrii Nakryiko 	}
546e6c64855SAndrii Nakryiko 	return btf;
547e6c64855SAndrii Nakryiko }
548e6c64855SAndrii Nakryiko 
5491713d68bSDaniel Borkmann static int compare_vsi_off(const void *_a, const void *_b)
5501713d68bSDaniel Borkmann {
5511713d68bSDaniel Borkmann 	const struct btf_var_secinfo *a = _a;
5521713d68bSDaniel Borkmann 	const struct btf_var_secinfo *b = _b;
5531713d68bSDaniel Borkmann 
5541713d68bSDaniel Borkmann 	return a->offset - b->offset;
5551713d68bSDaniel Borkmann }
5561713d68bSDaniel Borkmann 
5571713d68bSDaniel Borkmann static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
5581713d68bSDaniel Borkmann 			     struct btf_type *t)
5591713d68bSDaniel Borkmann {
5601713d68bSDaniel Borkmann 	__u32 size = 0, off = 0, i, vars = BTF_INFO_VLEN(t->info);
5611713d68bSDaniel Borkmann 	const char *name = btf__name_by_offset(btf, t->name_off);
5621713d68bSDaniel Borkmann 	const struct btf_type *t_var;
5631713d68bSDaniel Borkmann 	struct btf_var_secinfo *vsi;
5641713d68bSDaniel Borkmann 	struct btf_var *var;
5651713d68bSDaniel Borkmann 	int ret;
5661713d68bSDaniel Borkmann 
5671713d68bSDaniel Borkmann 	if (!name) {
5681713d68bSDaniel Borkmann 		pr_debug("No name found in string section for DATASEC kind.\n");
5691713d68bSDaniel Borkmann 		return -ENOENT;
5701713d68bSDaniel Borkmann 	}
5711713d68bSDaniel Borkmann 
5721713d68bSDaniel Borkmann 	ret = bpf_object__section_size(obj, name, &size);
5731713d68bSDaniel Borkmann 	if (ret || !size || (t->size && t->size != size)) {
5741713d68bSDaniel Borkmann 		pr_debug("Invalid size for section %s: %u bytes\n", name, size);
5751713d68bSDaniel Borkmann 		return -ENOENT;
5761713d68bSDaniel Borkmann 	}
5771713d68bSDaniel Borkmann 
5781713d68bSDaniel Borkmann 	t->size = size;
5791713d68bSDaniel Borkmann 
5801713d68bSDaniel Borkmann 	for (i = 0, vsi = (struct btf_var_secinfo *)(t + 1);
5811713d68bSDaniel Borkmann 	     i < vars; i++, vsi++) {
5821713d68bSDaniel Borkmann 		t_var = btf__type_by_id(btf, vsi->type);
5831713d68bSDaniel Borkmann 		var = (struct btf_var *)(t_var + 1);
5841713d68bSDaniel Borkmann 
5851713d68bSDaniel Borkmann 		if (BTF_INFO_KIND(t_var->info) != BTF_KIND_VAR) {
5861713d68bSDaniel Borkmann 			pr_debug("Non-VAR type seen in section %s\n", name);
5871713d68bSDaniel Borkmann 			return -EINVAL;
5881713d68bSDaniel Borkmann 		}
5891713d68bSDaniel Borkmann 
5901713d68bSDaniel Borkmann 		if (var->linkage == BTF_VAR_STATIC)
5911713d68bSDaniel Borkmann 			continue;
5921713d68bSDaniel Borkmann 
5931713d68bSDaniel Borkmann 		name = btf__name_by_offset(btf, t_var->name_off);
5941713d68bSDaniel Borkmann 		if (!name) {
5951713d68bSDaniel Borkmann 			pr_debug("No name found in string section for VAR kind\n");
5961713d68bSDaniel Borkmann 			return -ENOENT;
5971713d68bSDaniel Borkmann 		}
5981713d68bSDaniel Borkmann 
5991713d68bSDaniel Borkmann 		ret = bpf_object__variable_offset(obj, name, &off);
6001713d68bSDaniel Borkmann 		if (ret) {
6011713d68bSDaniel Borkmann 			pr_debug("No offset found in symbol table for VAR %s\n", name);
6021713d68bSDaniel Borkmann 			return -ENOENT;
6031713d68bSDaniel Borkmann 		}
6041713d68bSDaniel Borkmann 
6051713d68bSDaniel Borkmann 		vsi->offset = off;
6061713d68bSDaniel Borkmann 	}
6071713d68bSDaniel Borkmann 
6081713d68bSDaniel Borkmann 	qsort(t + 1, vars, sizeof(*vsi), compare_vsi_off);
6091713d68bSDaniel Borkmann 	return 0;
6101713d68bSDaniel Borkmann }
6111713d68bSDaniel Borkmann 
6121713d68bSDaniel Borkmann int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
6131713d68bSDaniel Borkmann {
6141713d68bSDaniel Borkmann 	int err = 0;
6151713d68bSDaniel Borkmann 	__u32 i;
6161713d68bSDaniel Borkmann 
6171713d68bSDaniel Borkmann 	for (i = 1; i <= btf->nr_types; i++) {
6181713d68bSDaniel Borkmann 		struct btf_type *t = btf->types[i];
6191713d68bSDaniel Borkmann 
6201713d68bSDaniel Borkmann 		/* Loader needs to fix up some of the things compiler
6211713d68bSDaniel Borkmann 		 * couldn't get its hands on while emitting BTF. This
6221713d68bSDaniel Borkmann 		 * is section size and global variable offset. We use
6231713d68bSDaniel Borkmann 		 * the info from the ELF itself for this purpose.
6241713d68bSDaniel Borkmann 		 */
6251713d68bSDaniel Borkmann 		if (BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC) {
6261713d68bSDaniel Borkmann 			err = btf_fixup_datasec(obj, btf, t);
6271713d68bSDaniel Borkmann 			if (err)
6281713d68bSDaniel Borkmann 				break;
6291713d68bSDaniel Borkmann 		}
6301713d68bSDaniel Borkmann 	}
6311713d68bSDaniel Borkmann 
6321713d68bSDaniel Borkmann 	return err;
6331713d68bSDaniel Borkmann }
6341713d68bSDaniel Borkmann 
635d29d87f7SAndrii Nakryiko int btf__load(struct btf *btf)
636d29d87f7SAndrii Nakryiko {
637d29d87f7SAndrii Nakryiko 	__u32 log_buf_size = BPF_LOG_BUF_SIZE;
638d29d87f7SAndrii Nakryiko 	char *log_buf = NULL;
639d29d87f7SAndrii Nakryiko 	int err = 0;
640d29d87f7SAndrii Nakryiko 
641d29d87f7SAndrii Nakryiko 	if (btf->fd >= 0)
642d29d87f7SAndrii Nakryiko 		return -EEXIST;
643d29d87f7SAndrii Nakryiko 
644d29d87f7SAndrii Nakryiko 	log_buf = malloc(log_buf_size);
645d29d87f7SAndrii Nakryiko 	if (!log_buf)
646d29d87f7SAndrii Nakryiko 		return -ENOMEM;
647d29d87f7SAndrii Nakryiko 
648d29d87f7SAndrii Nakryiko 	*log_buf = 0;
649d29d87f7SAndrii Nakryiko 
650d29d87f7SAndrii Nakryiko 	btf->fd = bpf_load_btf(btf->data, btf->data_size,
651d29d87f7SAndrii Nakryiko 			       log_buf, log_buf_size, false);
652d29d87f7SAndrii Nakryiko 	if (btf->fd < 0) {
653d29d87f7SAndrii Nakryiko 		err = -errno;
654d29d87f7SAndrii Nakryiko 		pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
655d29d87f7SAndrii Nakryiko 		if (*log_buf)
656d29d87f7SAndrii Nakryiko 			pr_warning("%s\n", log_buf);
657d29d87f7SAndrii Nakryiko 		goto done;
658d29d87f7SAndrii Nakryiko 	}
659d29d87f7SAndrii Nakryiko 
660d29d87f7SAndrii Nakryiko done:
661d29d87f7SAndrii Nakryiko 	free(log_buf);
662d29d87f7SAndrii Nakryiko 	return err;
663d29d87f7SAndrii Nakryiko }
664d29d87f7SAndrii Nakryiko 
6658a138aedSMartin KaFai Lau int btf__fd(const struct btf *btf)
6668a138aedSMartin KaFai Lau {
6678a138aedSMartin KaFai Lau 	return btf->fd;
6688a138aedSMartin KaFai Lau }
66992b57121SOkash Khawaja 
67002c87446SAndrii Nakryiko const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
67102c87446SAndrii Nakryiko {
67202c87446SAndrii Nakryiko 	*size = btf->data_size;
67302c87446SAndrii Nakryiko 	return btf->data;
67402c87446SAndrii Nakryiko }
67502c87446SAndrii Nakryiko 
67692b57121SOkash Khawaja const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
67792b57121SOkash Khawaja {
67892b57121SOkash Khawaja 	if (offset < btf->hdr->str_len)
67992b57121SOkash Khawaja 		return &btf->strings[offset];
68092b57121SOkash Khawaja 	else
68192b57121SOkash Khawaja 		return NULL;
68292b57121SOkash Khawaja }
6832993e051SYonghong Song 
6841d2f44caSMartin KaFai Lau int btf__get_from_id(__u32 id, struct btf **btf)
685d7f5b5e0SYonghong Song {
686d7f5b5e0SYonghong Song 	struct bpf_btf_info btf_info = { 0 };
687d7f5b5e0SYonghong Song 	__u32 len = sizeof(btf_info);
688d7f5b5e0SYonghong Song 	__u32 last_size;
689d7f5b5e0SYonghong Song 	int btf_fd;
690d7f5b5e0SYonghong Song 	void *ptr;
691d7f5b5e0SYonghong Song 	int err;
692d7f5b5e0SYonghong Song 
693d7f5b5e0SYonghong Song 	err = 0;
694d7f5b5e0SYonghong Song 	*btf = NULL;
695d7f5b5e0SYonghong Song 	btf_fd = bpf_btf_get_fd_by_id(id);
696d7f5b5e0SYonghong Song 	if (btf_fd < 0)
697d7f5b5e0SYonghong Song 		return 0;
698d7f5b5e0SYonghong Song 
699d7f5b5e0SYonghong Song 	/* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
700d7f5b5e0SYonghong Song 	 * let's start with a sane default - 4KiB here - and resize it only if
701d7f5b5e0SYonghong Song 	 * bpf_obj_get_info_by_fd() needs a bigger buffer.
702d7f5b5e0SYonghong Song 	 */
703d7f5b5e0SYonghong Song 	btf_info.btf_size = 4096;
704d7f5b5e0SYonghong Song 	last_size = btf_info.btf_size;
705d7f5b5e0SYonghong Song 	ptr = malloc(last_size);
706d7f5b5e0SYonghong Song 	if (!ptr) {
707d7f5b5e0SYonghong Song 		err = -ENOMEM;
708d7f5b5e0SYonghong Song 		goto exit_free;
709d7f5b5e0SYonghong Song 	}
710d7f5b5e0SYonghong Song 
7111ad9cbb8SAndrii Nakryiko 	memset(ptr, 0, last_size);
712d7f5b5e0SYonghong Song 	btf_info.btf = ptr_to_u64(ptr);
713d7f5b5e0SYonghong Song 	err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
714d7f5b5e0SYonghong Song 
715d7f5b5e0SYonghong Song 	if (!err && btf_info.btf_size > last_size) {
716d7f5b5e0SYonghong Song 		void *temp_ptr;
717d7f5b5e0SYonghong Song 
718d7f5b5e0SYonghong Song 		last_size = btf_info.btf_size;
719d7f5b5e0SYonghong Song 		temp_ptr = realloc(ptr, last_size);
720d7f5b5e0SYonghong Song 		if (!temp_ptr) {
721d7f5b5e0SYonghong Song 			err = -ENOMEM;
722d7f5b5e0SYonghong Song 			goto exit_free;
723d7f5b5e0SYonghong Song 		}
724d7f5b5e0SYonghong Song 		ptr = temp_ptr;
7251ad9cbb8SAndrii Nakryiko 		memset(ptr, 0, last_size);
726d7f5b5e0SYonghong Song 		btf_info.btf = ptr_to_u64(ptr);
727d7f5b5e0SYonghong Song 		err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
728d7f5b5e0SYonghong Song 	}
729d7f5b5e0SYonghong Song 
730d7f5b5e0SYonghong Song 	if (err || btf_info.btf_size > last_size) {
731d7f5b5e0SYonghong Song 		err = errno;
732d7f5b5e0SYonghong Song 		goto exit_free;
733d7f5b5e0SYonghong Song 	}
734d7f5b5e0SYonghong Song 
7358461ef8bSYonghong Song 	*btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
736d7f5b5e0SYonghong Song 	if (IS_ERR(*btf)) {
737d7f5b5e0SYonghong Song 		err = PTR_ERR(*btf);
738d7f5b5e0SYonghong Song 		*btf = NULL;
739d7f5b5e0SYonghong Song 	}
740d7f5b5e0SYonghong Song 
741d7f5b5e0SYonghong Song exit_free:
742d7f5b5e0SYonghong Song 	close(btf_fd);
743d7f5b5e0SYonghong Song 	free(ptr);
744d7f5b5e0SYonghong Song 
745d7f5b5e0SYonghong Song 	return err;
746d7f5b5e0SYonghong Song }
747d7f5b5e0SYonghong Song 
748a6c109a6SYonghong Song int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
74996408c43SYonghong Song 			 __u32 expected_key_size, __u32 expected_value_size,
75096408c43SYonghong Song 			 __u32 *key_type_id, __u32 *value_type_id)
75196408c43SYonghong Song {
75296408c43SYonghong Song 	const struct btf_type *container_type;
75396408c43SYonghong Song 	const struct btf_member *key, *value;
75496408c43SYonghong Song 	const size_t max_name = 256;
75596408c43SYonghong Song 	char container_name[max_name];
75696408c43SYonghong Song 	__s64 key_size, value_size;
75796408c43SYonghong Song 	__s32 container_id;
75896408c43SYonghong Song 
75996408c43SYonghong Song 	if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
76096408c43SYonghong Song 	    max_name) {
76196408c43SYonghong Song 		pr_warning("map:%s length of '____btf_map_%s' is too long\n",
76296408c43SYonghong Song 			   map_name, map_name);
76396408c43SYonghong Song 		return -EINVAL;
76496408c43SYonghong Song 	}
76596408c43SYonghong Song 
76696408c43SYonghong Song 	container_id = btf__find_by_name(btf, container_name);
76796408c43SYonghong Song 	if (container_id < 0) {
768f7748e29SYonghong Song 		pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
76996408c43SYonghong Song 			 map_name, container_name);
77096408c43SYonghong Song 		return container_id;
77196408c43SYonghong Song 	}
77296408c43SYonghong Song 
77396408c43SYonghong Song 	container_type = btf__type_by_id(btf, container_id);
77496408c43SYonghong Song 	if (!container_type) {
77596408c43SYonghong Song 		pr_warning("map:%s cannot find BTF type for container_id:%u\n",
77696408c43SYonghong Song 			   map_name, container_id);
77796408c43SYonghong Song 		return -EINVAL;
77896408c43SYonghong Song 	}
77996408c43SYonghong Song 
78096408c43SYonghong Song 	if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT ||
78196408c43SYonghong Song 	    BTF_INFO_VLEN(container_type->info) < 2) {
78296408c43SYonghong Song 		pr_warning("map:%s container_name:%s is an invalid container struct\n",
78396408c43SYonghong Song 			   map_name, container_name);
78496408c43SYonghong Song 		return -EINVAL;
78596408c43SYonghong Song 	}
78696408c43SYonghong Song 
78796408c43SYonghong Song 	key = (struct btf_member *)(container_type + 1);
78896408c43SYonghong Song 	value = key + 1;
78996408c43SYonghong Song 
79096408c43SYonghong Song 	key_size = btf__resolve_size(btf, key->type);
79196408c43SYonghong Song 	if (key_size < 0) {
79296408c43SYonghong Song 		pr_warning("map:%s invalid BTF key_type_size\n", map_name);
79396408c43SYonghong Song 		return key_size;
79496408c43SYonghong Song 	}
79596408c43SYonghong Song 
79696408c43SYonghong Song 	if (expected_key_size != key_size) {
79796408c43SYonghong Song 		pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
79896408c43SYonghong Song 			   map_name, (__u32)key_size, expected_key_size);
79996408c43SYonghong Song 		return -EINVAL;
80096408c43SYonghong Song 	}
80196408c43SYonghong Song 
80296408c43SYonghong Song 	value_size = btf__resolve_size(btf, value->type);
80396408c43SYonghong Song 	if (value_size < 0) {
80496408c43SYonghong Song 		pr_warning("map:%s invalid BTF value_type_size\n", map_name);
80596408c43SYonghong Song 		return value_size;
80696408c43SYonghong Song 	}
80796408c43SYonghong Song 
80896408c43SYonghong Song 	if (expected_value_size != value_size) {
80996408c43SYonghong Song 		pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
81096408c43SYonghong Song 			   map_name, (__u32)value_size, expected_value_size);
81196408c43SYonghong Song 		return -EINVAL;
81296408c43SYonghong Song 	}
81396408c43SYonghong Song 
81496408c43SYonghong Song 	*key_type_id = key->type;
81596408c43SYonghong Song 	*value_type_id = value->type;
81696408c43SYonghong Song 
81796408c43SYonghong Song 	return 0;
81896408c43SYonghong Song }
81996408c43SYonghong Song 
820ae4ab4b4SAndrii Nakryiko struct btf_ext_sec_setup_param {
8213d650141SMartin KaFai Lau 	__u32 off;
8223d650141SMartin KaFai Lau 	__u32 len;
8233d650141SMartin KaFai Lau 	__u32 min_rec_size;
8243d650141SMartin KaFai Lau 	struct btf_ext_info *ext_info;
8253d650141SMartin KaFai Lau 	const char *desc;
8263d650141SMartin KaFai Lau };
8273d650141SMartin KaFai Lau 
828ae4ab4b4SAndrii Nakryiko static int btf_ext_setup_info(struct btf_ext *btf_ext,
829ae4ab4b4SAndrii Nakryiko 			      struct btf_ext_sec_setup_param *ext_sec)
8302993e051SYonghong Song {
8313d650141SMartin KaFai Lau 	const struct btf_ext_info_sec *sinfo;
8323d650141SMartin KaFai Lau 	struct btf_ext_info *ext_info;
833f0187f0bSMartin KaFai Lau 	__u32 info_left, record_size;
834f0187f0bSMartin KaFai Lau 	/* The start of the info sec (including the __u32 record_size). */
835ae4ab4b4SAndrii Nakryiko 	void *info;
836f0187f0bSMartin KaFai Lau 
8373d650141SMartin KaFai Lau 	if (ext_sec->off & 0x03) {
8388461ef8bSYonghong Song 		pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
8393d650141SMartin KaFai Lau 		     ext_sec->desc);
840f0187f0bSMartin KaFai Lau 		return -EINVAL;
841f0187f0bSMartin KaFai Lau 	}
842f0187f0bSMartin KaFai Lau 
843ae4ab4b4SAndrii Nakryiko 	info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
844ae4ab4b4SAndrii Nakryiko 	info_left = ext_sec->len;
845ae4ab4b4SAndrii Nakryiko 
846ae4ab4b4SAndrii Nakryiko 	if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
8478461ef8bSYonghong Song 		pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
8483d650141SMartin KaFai Lau 			 ext_sec->desc, ext_sec->off, ext_sec->len);
849f0187f0bSMartin KaFai Lau 		return -EINVAL;
850f0187f0bSMartin KaFai Lau 	}
851f0187f0bSMartin KaFai Lau 
8523d650141SMartin KaFai Lau 	/* At least a record size */
853f0187f0bSMartin KaFai Lau 	if (info_left < sizeof(__u32)) {
8548461ef8bSYonghong Song 		pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
8552993e051SYonghong Song 		return -EINVAL;
8562993e051SYonghong Song 	}
8572993e051SYonghong Song 
858f0187f0bSMartin KaFai Lau 	/* The record size needs to meet the minimum standard */
859f0187f0bSMartin KaFai Lau 	record_size = *(__u32 *)info;
8603d650141SMartin KaFai Lau 	if (record_size < ext_sec->min_rec_size ||
861f0187f0bSMartin KaFai Lau 	    record_size & 0x03) {
8628461ef8bSYonghong Song 		pr_debug("%s section in .BTF.ext has invalid record size %u\n",
8633d650141SMartin KaFai Lau 			 ext_sec->desc, record_size);
8642993e051SYonghong Song 		return -EINVAL;
8652993e051SYonghong Song 	}
8662993e051SYonghong Song 
867f0187f0bSMartin KaFai Lau 	sinfo = info + sizeof(__u32);
868f0187f0bSMartin KaFai Lau 	info_left -= sizeof(__u32);
8692993e051SYonghong Song 
8703d650141SMartin KaFai Lau 	/* If no records, return failure now so .BTF.ext won't be used. */
871f0187f0bSMartin KaFai Lau 	if (!info_left) {
8728461ef8bSYonghong Song 		pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
8732993e051SYonghong Song 		return -EINVAL;
8742993e051SYonghong Song 	}
8752993e051SYonghong Song 
876f0187f0bSMartin KaFai Lau 	while (info_left) {
8773d650141SMartin KaFai Lau 		unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
878f0187f0bSMartin KaFai Lau 		__u64 total_record_size;
879f0187f0bSMartin KaFai Lau 		__u32 num_records;
880f0187f0bSMartin KaFai Lau 
881f0187f0bSMartin KaFai Lau 		if (info_left < sec_hdrlen) {
8828461ef8bSYonghong Song 			pr_debug("%s section header is not found in .BTF.ext\n",
8833d650141SMartin KaFai Lau 			     ext_sec->desc);
8842993e051SYonghong Song 			return -EINVAL;
8852993e051SYonghong Song 		}
8862993e051SYonghong Song 
8873d650141SMartin KaFai Lau 		num_records = sinfo->num_info;
8882993e051SYonghong Song 		if (num_records == 0) {
8898461ef8bSYonghong Song 			pr_debug("%s section has incorrect num_records in .BTF.ext\n",
8903d650141SMartin KaFai Lau 			     ext_sec->desc);
8912993e051SYonghong Song 			return -EINVAL;
8922993e051SYonghong Song 		}
8932993e051SYonghong Song 
8942993e051SYonghong Song 		total_record_size = sec_hdrlen +
8952993e051SYonghong Song 				    (__u64)num_records * record_size;
896f0187f0bSMartin KaFai Lau 		if (info_left < total_record_size) {
8978461ef8bSYonghong Song 			pr_debug("%s section has incorrect num_records in .BTF.ext\n",
8983d650141SMartin KaFai Lau 			     ext_sec->desc);
8992993e051SYonghong Song 			return -EINVAL;
9002993e051SYonghong Song 		}
9012993e051SYonghong Song 
902f0187f0bSMartin KaFai Lau 		info_left -= total_record_size;
9032993e051SYonghong Song 		sinfo = (void *)sinfo + total_record_size;
9042993e051SYonghong Song 	}
9052993e051SYonghong Song 
9063d650141SMartin KaFai Lau 	ext_info = ext_sec->ext_info;
9073d650141SMartin KaFai Lau 	ext_info->len = ext_sec->len - sizeof(__u32);
9083d650141SMartin KaFai Lau 	ext_info->rec_size = record_size;
909ae4ab4b4SAndrii Nakryiko 	ext_info->info = info + sizeof(__u32);
910f0187f0bSMartin KaFai Lau 
9112993e051SYonghong Song 	return 0;
9122993e051SYonghong Song }
9132993e051SYonghong Song 
914ae4ab4b4SAndrii Nakryiko static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
9153d650141SMartin KaFai Lau {
916ae4ab4b4SAndrii Nakryiko 	struct btf_ext_sec_setup_param param = {
917ae4ab4b4SAndrii Nakryiko 		.off = btf_ext->hdr->func_info_off,
918ae4ab4b4SAndrii Nakryiko 		.len = btf_ext->hdr->func_info_len,
9193d650141SMartin KaFai Lau 		.min_rec_size = sizeof(struct bpf_func_info_min),
9203d650141SMartin KaFai Lau 		.ext_info = &btf_ext->func_info,
9213d650141SMartin KaFai Lau 		.desc = "func_info"
9223d650141SMartin KaFai Lau 	};
9233d650141SMartin KaFai Lau 
924ae4ab4b4SAndrii Nakryiko 	return btf_ext_setup_info(btf_ext, &param);
9253d650141SMartin KaFai Lau }
9263d650141SMartin KaFai Lau 
927ae4ab4b4SAndrii Nakryiko static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
9283d650141SMartin KaFai Lau {
929ae4ab4b4SAndrii Nakryiko 	struct btf_ext_sec_setup_param param = {
930ae4ab4b4SAndrii Nakryiko 		.off = btf_ext->hdr->line_info_off,
931ae4ab4b4SAndrii Nakryiko 		.len = btf_ext->hdr->line_info_len,
9323d650141SMartin KaFai Lau 		.min_rec_size = sizeof(struct bpf_line_info_min),
9333d650141SMartin KaFai Lau 		.ext_info = &btf_ext->line_info,
9343d650141SMartin KaFai Lau 		.desc = "line_info",
9353d650141SMartin KaFai Lau 	};
9363d650141SMartin KaFai Lau 
937ae4ab4b4SAndrii Nakryiko 	return btf_ext_setup_info(btf_ext, &param);
9383d650141SMartin KaFai Lau }
9393d650141SMartin KaFai Lau 
9408461ef8bSYonghong Song static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
9412993e051SYonghong Song {
9422993e051SYonghong Song 	const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
9432993e051SYonghong Song 
9442993e051SYonghong Song 	if (data_size < offsetof(struct btf_ext_header, func_info_off) ||
9452993e051SYonghong Song 	    data_size < hdr->hdr_len) {
9468461ef8bSYonghong Song 		pr_debug("BTF.ext header not found");
9472993e051SYonghong Song 		return -EINVAL;
9482993e051SYonghong Song 	}
9492993e051SYonghong Song 
9502993e051SYonghong Song 	if (hdr->magic != BTF_MAGIC) {
9518461ef8bSYonghong Song 		pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
9522993e051SYonghong Song 		return -EINVAL;
9532993e051SYonghong Song 	}
9542993e051SYonghong Song 
9552993e051SYonghong Song 	if (hdr->version != BTF_VERSION) {
9568461ef8bSYonghong Song 		pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
9572993e051SYonghong Song 		return -ENOTSUP;
9582993e051SYonghong Song 	}
9592993e051SYonghong Song 
9602993e051SYonghong Song 	if (hdr->flags) {
9618461ef8bSYonghong Song 		pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
9622993e051SYonghong Song 		return -ENOTSUP;
9632993e051SYonghong Song 	}
9642993e051SYonghong Song 
965f0187f0bSMartin KaFai Lau 	if (data_size == hdr->hdr_len) {
9668461ef8bSYonghong Song 		pr_debug("BTF.ext has no data\n");
9672993e051SYonghong Song 		return -EINVAL;
9682993e051SYonghong Song 	}
9692993e051SYonghong Song 
970f0187f0bSMartin KaFai Lau 	return 0;
9712993e051SYonghong Song }
9722993e051SYonghong Song 
9732993e051SYonghong Song void btf_ext__free(struct btf_ext *btf_ext)
9742993e051SYonghong Song {
9752993e051SYonghong Song 	if (!btf_ext)
9762993e051SYonghong Song 		return;
977ae4ab4b4SAndrii Nakryiko 	free(btf_ext->data);
9782993e051SYonghong Song 	free(btf_ext);
9792993e051SYonghong Song }
9802993e051SYonghong Song 
9818461ef8bSYonghong Song struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
9822993e051SYonghong Song {
9832993e051SYonghong Song 	struct btf_ext *btf_ext;
9842993e051SYonghong Song 	int err;
9852993e051SYonghong Song 
9868461ef8bSYonghong Song 	err = btf_ext_parse_hdr(data, size);
9872993e051SYonghong Song 	if (err)
9882993e051SYonghong Song 		return ERR_PTR(err);
9892993e051SYonghong Song 
9902993e051SYonghong Song 	btf_ext = calloc(1, sizeof(struct btf_ext));
9912993e051SYonghong Song 	if (!btf_ext)
9922993e051SYonghong Song 		return ERR_PTR(-ENOMEM);
9932993e051SYonghong Song 
994ae4ab4b4SAndrii Nakryiko 	btf_ext->data_size = size;
995ae4ab4b4SAndrii Nakryiko 	btf_ext->data = malloc(size);
996ae4ab4b4SAndrii Nakryiko 	if (!btf_ext->data) {
997ae4ab4b4SAndrii Nakryiko 		err = -ENOMEM;
998ae4ab4b4SAndrii Nakryiko 		goto done;
9992993e051SYonghong Song 	}
1000ae4ab4b4SAndrii Nakryiko 	memcpy(btf_ext->data, data, size);
10012993e051SYonghong Song 
1002ae4ab4b4SAndrii Nakryiko 	err = btf_ext_setup_func_info(btf_ext);
1003ae4ab4b4SAndrii Nakryiko 	if (err)
1004ae4ab4b4SAndrii Nakryiko 		goto done;
1005ae4ab4b4SAndrii Nakryiko 
1006ae4ab4b4SAndrii Nakryiko 	err = btf_ext_setup_line_info(btf_ext);
1007ae4ab4b4SAndrii Nakryiko 	if (err)
1008ae4ab4b4SAndrii Nakryiko 		goto done;
1009ae4ab4b4SAndrii Nakryiko 
1010ae4ab4b4SAndrii Nakryiko done:
10113d650141SMartin KaFai Lau 	if (err) {
10123d650141SMartin KaFai Lau 		btf_ext__free(btf_ext);
10133d650141SMartin KaFai Lau 		return ERR_PTR(err);
10143d650141SMartin KaFai Lau 	}
10153d650141SMartin KaFai Lau 
10162993e051SYonghong Song 	return btf_ext;
10172993e051SYonghong Song }
10182993e051SYonghong Song 
1019ae4ab4b4SAndrii Nakryiko const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
1020ae4ab4b4SAndrii Nakryiko {
1021ae4ab4b4SAndrii Nakryiko 	*size = btf_ext->data_size;
1022ae4ab4b4SAndrii Nakryiko 	return btf_ext->data;
1023ae4ab4b4SAndrii Nakryiko }
1024ae4ab4b4SAndrii Nakryiko 
10253d650141SMartin KaFai Lau static int btf_ext_reloc_info(const struct btf *btf,
10263d650141SMartin KaFai Lau 			      const struct btf_ext_info *ext_info,
10272993e051SYonghong Song 			      const char *sec_name, __u32 insns_cnt,
10283d650141SMartin KaFai Lau 			      void **info, __u32 *cnt)
10292993e051SYonghong Song {
10303d650141SMartin KaFai Lau 	__u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
10313d650141SMartin KaFai Lau 	__u32 i, record_size, existing_len, records_len;
10323d650141SMartin KaFai Lau 	struct btf_ext_info_sec *sinfo;
10332993e051SYonghong Song 	const char *info_sec_name;
10342993e051SYonghong Song 	__u64 remain_len;
10352993e051SYonghong Song 	void *data;
10362993e051SYonghong Song 
10373d650141SMartin KaFai Lau 	record_size = ext_info->rec_size;
10383d650141SMartin KaFai Lau 	sinfo = ext_info->info;
10393d650141SMartin KaFai Lau 	remain_len = ext_info->len;
10402993e051SYonghong Song 	while (remain_len > 0) {
10413d650141SMartin KaFai Lau 		records_len = sinfo->num_info * record_size;
10422993e051SYonghong Song 		info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
10432993e051SYonghong Song 		if (strcmp(info_sec_name, sec_name)) {
10442993e051SYonghong Song 			remain_len -= sec_hdrlen + records_len;
10452993e051SYonghong Song 			sinfo = (void *)sinfo + sec_hdrlen + records_len;
10462993e051SYonghong Song 			continue;
10472993e051SYonghong Song 		}
10482993e051SYonghong Song 
10493d650141SMartin KaFai Lau 		existing_len = (*cnt) * record_size;
10503d650141SMartin KaFai Lau 		data = realloc(*info, existing_len + records_len);
10512993e051SYonghong Song 		if (!data)
10522993e051SYonghong Song 			return -ENOMEM;
10532993e051SYonghong Song 
10543d650141SMartin KaFai Lau 		memcpy(data + existing_len, sinfo->data, records_len);
105584ecc1f9SMartin KaFai Lau 		/* adjust insn_off only, the rest data will be passed
10562993e051SYonghong Song 		 * to the kernel.
10572993e051SYonghong Song 		 */
10583d650141SMartin KaFai Lau 		for (i = 0; i < sinfo->num_info; i++) {
10593d650141SMartin KaFai Lau 			__u32 *insn_off;
10602993e051SYonghong Song 
10613d650141SMartin KaFai Lau 			insn_off = data + existing_len + (i * record_size);
10623d650141SMartin KaFai Lau 			*insn_off = *insn_off / sizeof(struct bpf_insn) +
10632993e051SYonghong Song 				insns_cnt;
10642993e051SYonghong Song 		}
10653d650141SMartin KaFai Lau 		*info = data;
10663d650141SMartin KaFai Lau 		*cnt += sinfo->num_info;
10672993e051SYonghong Song 		return 0;
10682993e051SYonghong Song 	}
10692993e051SYonghong Song 
1070f0187f0bSMartin KaFai Lau 	return -ENOENT;
1071f0187f0bSMartin KaFai Lau }
1072f0187f0bSMartin KaFai Lau 
1073ae4ab4b4SAndrii Nakryiko int btf_ext__reloc_func_info(const struct btf *btf,
1074ae4ab4b4SAndrii Nakryiko 			     const struct btf_ext *btf_ext,
10753d650141SMartin KaFai Lau 			     const char *sec_name, __u32 insns_cnt,
10763d650141SMartin KaFai Lau 			     void **func_info, __u32 *cnt)
10773d650141SMartin KaFai Lau {
10783d650141SMartin KaFai Lau 	return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
10793d650141SMartin KaFai Lau 				  insns_cnt, func_info, cnt);
10803d650141SMartin KaFai Lau }
10813d650141SMartin KaFai Lau 
1082ae4ab4b4SAndrii Nakryiko int btf_ext__reloc_line_info(const struct btf *btf,
1083ae4ab4b4SAndrii Nakryiko 			     const struct btf_ext *btf_ext,
10843d650141SMartin KaFai Lau 			     const char *sec_name, __u32 insns_cnt,
10853d650141SMartin KaFai Lau 			     void **line_info, __u32 *cnt)
10863d650141SMartin KaFai Lau {
10873d650141SMartin KaFai Lau 	return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
10883d650141SMartin KaFai Lau 				  insns_cnt, line_info, cnt);
10893d650141SMartin KaFai Lau }
10903d650141SMartin KaFai Lau 
1091f0187f0bSMartin KaFai Lau __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
1092f0187f0bSMartin KaFai Lau {
10933d650141SMartin KaFai Lau 	return btf_ext->func_info.rec_size;
10943d650141SMartin KaFai Lau }
10953d650141SMartin KaFai Lau 
10963d650141SMartin KaFai Lau __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
10973d650141SMartin KaFai Lau {
10983d650141SMartin KaFai Lau 	return btf_ext->line_info.rec_size;
10992993e051SYonghong Song }
1100d5caef5bSAndrii Nakryiko 
1101d5caef5bSAndrii Nakryiko struct btf_dedup;
1102d5caef5bSAndrii Nakryiko 
1103d5caef5bSAndrii Nakryiko static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1104d5caef5bSAndrii Nakryiko 				       const struct btf_dedup_opts *opts);
1105d5caef5bSAndrii Nakryiko static void btf_dedup_free(struct btf_dedup *d);
1106d5caef5bSAndrii Nakryiko static int btf_dedup_strings(struct btf_dedup *d);
1107d5caef5bSAndrii Nakryiko static int btf_dedup_prim_types(struct btf_dedup *d);
1108d5caef5bSAndrii Nakryiko static int btf_dedup_struct_types(struct btf_dedup *d);
1109d5caef5bSAndrii Nakryiko static int btf_dedup_ref_types(struct btf_dedup *d);
1110d5caef5bSAndrii Nakryiko static int btf_dedup_compact_types(struct btf_dedup *d);
1111d5caef5bSAndrii Nakryiko static int btf_dedup_remap_types(struct btf_dedup *d);
1112d5caef5bSAndrii Nakryiko 
1113d5caef5bSAndrii Nakryiko /*
1114d5caef5bSAndrii Nakryiko  * Deduplicate BTF types and strings.
1115d5caef5bSAndrii Nakryiko  *
1116d5caef5bSAndrii Nakryiko  * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
1117d5caef5bSAndrii Nakryiko  * section with all BTF type descriptors and string data. It overwrites that
1118d5caef5bSAndrii Nakryiko  * memory in-place with deduplicated types and strings without any loss of
1119d5caef5bSAndrii Nakryiko  * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
1120d5caef5bSAndrii Nakryiko  * is provided, all the strings referenced from .BTF.ext section are honored
1121d5caef5bSAndrii Nakryiko  * and updated to point to the right offsets after deduplication.
1122d5caef5bSAndrii Nakryiko  *
1123d5caef5bSAndrii Nakryiko  * If function returns with error, type/string data might be garbled and should
1124d5caef5bSAndrii Nakryiko  * be discarded.
1125d5caef5bSAndrii Nakryiko  *
1126d5caef5bSAndrii Nakryiko  * More verbose and detailed description of both problem btf_dedup is solving,
1127d5caef5bSAndrii Nakryiko  * as well as solution could be found at:
1128d5caef5bSAndrii Nakryiko  * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
1129d5caef5bSAndrii Nakryiko  *
1130d5caef5bSAndrii Nakryiko  * Problem description and justification
1131d5caef5bSAndrii Nakryiko  * =====================================
1132d5caef5bSAndrii Nakryiko  *
1133d5caef5bSAndrii Nakryiko  * BTF type information is typically emitted either as a result of conversion
1134d5caef5bSAndrii Nakryiko  * from DWARF to BTF or directly by compiler. In both cases, each compilation
1135d5caef5bSAndrii Nakryiko  * unit contains information about a subset of all the types that are used
1136d5caef5bSAndrii Nakryiko  * in an application. These subsets are frequently overlapping and contain a lot
1137d5caef5bSAndrii Nakryiko  * of duplicated information when later concatenated together into a single
1138d5caef5bSAndrii Nakryiko  * binary. This algorithm ensures that each unique type is represented by single
1139d5caef5bSAndrii Nakryiko  * BTF type descriptor, greatly reducing resulting size of BTF data.
1140d5caef5bSAndrii Nakryiko  *
1141d5caef5bSAndrii Nakryiko  * Compilation unit isolation and subsequent duplication of data is not the only
1142d5caef5bSAndrii Nakryiko  * problem. The same type hierarchy (e.g., struct and all the type that struct
1143d5caef5bSAndrii Nakryiko  * references) in different compilation units can be represented in BTF to
1144d5caef5bSAndrii Nakryiko  * various degrees of completeness (or, rather, incompleteness) due to
1145d5caef5bSAndrii Nakryiko  * struct/union forward declarations.
1146d5caef5bSAndrii Nakryiko  *
1147d5caef5bSAndrii Nakryiko  * Let's take a look at an example, that we'll use to better understand the
1148d5caef5bSAndrii Nakryiko  * problem (and solution). Suppose we have two compilation units, each using
1149d5caef5bSAndrii Nakryiko  * same `struct S`, but each of them having incomplete type information about
1150d5caef5bSAndrii Nakryiko  * struct's fields:
1151d5caef5bSAndrii Nakryiko  *
1152d5caef5bSAndrii Nakryiko  * // CU #1:
1153d5caef5bSAndrii Nakryiko  * struct S;
1154d5caef5bSAndrii Nakryiko  * struct A {
1155d5caef5bSAndrii Nakryiko  *	int a;
1156d5caef5bSAndrii Nakryiko  *	struct A* self;
1157d5caef5bSAndrii Nakryiko  *	struct S* parent;
1158d5caef5bSAndrii Nakryiko  * };
1159d5caef5bSAndrii Nakryiko  * struct B;
1160d5caef5bSAndrii Nakryiko  * struct S {
1161d5caef5bSAndrii Nakryiko  *	struct A* a_ptr;
1162d5caef5bSAndrii Nakryiko  *	struct B* b_ptr;
1163d5caef5bSAndrii Nakryiko  * };
1164d5caef5bSAndrii Nakryiko  *
1165d5caef5bSAndrii Nakryiko  * // CU #2:
1166d5caef5bSAndrii Nakryiko  * struct S;
1167d5caef5bSAndrii Nakryiko  * struct A;
1168d5caef5bSAndrii Nakryiko  * struct B {
1169d5caef5bSAndrii Nakryiko  *	int b;
1170d5caef5bSAndrii Nakryiko  *	struct B* self;
1171d5caef5bSAndrii Nakryiko  *	struct S* parent;
1172d5caef5bSAndrii Nakryiko  * };
1173d5caef5bSAndrii Nakryiko  * struct S {
1174d5caef5bSAndrii Nakryiko  *	struct A* a_ptr;
1175d5caef5bSAndrii Nakryiko  *	struct B* b_ptr;
1176d5caef5bSAndrii Nakryiko  * };
1177d5caef5bSAndrii Nakryiko  *
1178d5caef5bSAndrii Nakryiko  * In case of CU #1, BTF data will know only that `struct B` exist (but no
1179d5caef5bSAndrii Nakryiko  * more), but will know the complete type information about `struct A`. While
1180d5caef5bSAndrii Nakryiko  * for CU #2, it will know full type information about `struct B`, but will
1181d5caef5bSAndrii Nakryiko  * only know about forward declaration of `struct A` (in BTF terms, it will
1182d5caef5bSAndrii Nakryiko  * have `BTF_KIND_FWD` type descriptor with name `B`).
1183d5caef5bSAndrii Nakryiko  *
1184d5caef5bSAndrii Nakryiko  * This compilation unit isolation means that it's possible that there is no
1185d5caef5bSAndrii Nakryiko  * single CU with complete type information describing structs `S`, `A`, and
1186d5caef5bSAndrii Nakryiko  * `B`. Also, we might get tons of duplicated and redundant type information.
1187d5caef5bSAndrii Nakryiko  *
1188d5caef5bSAndrii Nakryiko  * Additional complication we need to keep in mind comes from the fact that
1189d5caef5bSAndrii Nakryiko  * types, in general, can form graphs containing cycles, not just DAGs.
1190d5caef5bSAndrii Nakryiko  *
1191d5caef5bSAndrii Nakryiko  * While algorithm does deduplication, it also merges and resolves type
1192d5caef5bSAndrii Nakryiko  * information (unless disabled throught `struct btf_opts`), whenever possible.
1193d5caef5bSAndrii Nakryiko  * E.g., in the example above with two compilation units having partial type
1194d5caef5bSAndrii Nakryiko  * information for structs `A` and `B`, the output of algorithm will emit
1195d5caef5bSAndrii Nakryiko  * a single copy of each BTF type that describes structs `A`, `B`, and `S`
1196d5caef5bSAndrii Nakryiko  * (as well as type information for `int` and pointers), as if they were defined
1197d5caef5bSAndrii Nakryiko  * in a single compilation unit as:
1198d5caef5bSAndrii Nakryiko  *
1199d5caef5bSAndrii Nakryiko  * struct A {
1200d5caef5bSAndrii Nakryiko  *	int a;
1201d5caef5bSAndrii Nakryiko  *	struct A* self;
1202d5caef5bSAndrii Nakryiko  *	struct S* parent;
1203d5caef5bSAndrii Nakryiko  * };
1204d5caef5bSAndrii Nakryiko  * struct B {
1205d5caef5bSAndrii Nakryiko  *	int b;
1206d5caef5bSAndrii Nakryiko  *	struct B* self;
1207d5caef5bSAndrii Nakryiko  *	struct S* parent;
1208d5caef5bSAndrii Nakryiko  * };
1209d5caef5bSAndrii Nakryiko  * struct S {
1210d5caef5bSAndrii Nakryiko  *	struct A* a_ptr;
1211d5caef5bSAndrii Nakryiko  *	struct B* b_ptr;
1212d5caef5bSAndrii Nakryiko  * };
1213d5caef5bSAndrii Nakryiko  *
1214d5caef5bSAndrii Nakryiko  * Algorithm summary
1215d5caef5bSAndrii Nakryiko  * =================
1216d5caef5bSAndrii Nakryiko  *
1217d5caef5bSAndrii Nakryiko  * Algorithm completes its work in 6 separate passes:
1218d5caef5bSAndrii Nakryiko  *
1219d5caef5bSAndrii Nakryiko  * 1. Strings deduplication.
1220d5caef5bSAndrii Nakryiko  * 2. Primitive types deduplication (int, enum, fwd).
1221d5caef5bSAndrii Nakryiko  * 3. Struct/union types deduplication.
1222d5caef5bSAndrii Nakryiko  * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
1223d5caef5bSAndrii Nakryiko  *    protos, and const/volatile/restrict modifiers).
1224d5caef5bSAndrii Nakryiko  * 5. Types compaction.
1225d5caef5bSAndrii Nakryiko  * 6. Types remapping.
1226d5caef5bSAndrii Nakryiko  *
1227d5caef5bSAndrii Nakryiko  * Algorithm determines canonical type descriptor, which is a single
1228d5caef5bSAndrii Nakryiko  * representative type for each truly unique type. This canonical type is the
1229d5caef5bSAndrii Nakryiko  * one that will go into final deduplicated BTF type information. For
1230d5caef5bSAndrii Nakryiko  * struct/unions, it is also the type that algorithm will merge additional type
1231d5caef5bSAndrii Nakryiko  * information into (while resolving FWDs), as it discovers it from data in
1232d5caef5bSAndrii Nakryiko  * other CUs. Each input BTF type eventually gets either mapped to itself, if
1233d5caef5bSAndrii Nakryiko  * that type is canonical, or to some other type, if that type is equivalent
1234d5caef5bSAndrii Nakryiko  * and was chosen as canonical representative. This mapping is stored in
1235d5caef5bSAndrii Nakryiko  * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
1236d5caef5bSAndrii Nakryiko  * FWD type got resolved to.
1237d5caef5bSAndrii Nakryiko  *
1238d5caef5bSAndrii Nakryiko  * To facilitate fast discovery of canonical types, we also maintain canonical
1239d5caef5bSAndrii Nakryiko  * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
1240d5caef5bSAndrii Nakryiko  * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
1241d5caef5bSAndrii Nakryiko  * that match that signature. With sufficiently good choice of type signature
1242d5caef5bSAndrii Nakryiko  * hashing function, we can limit number of canonical types for each unique type
1243d5caef5bSAndrii Nakryiko  * signature to a very small number, allowing to find canonical type for any
1244d5caef5bSAndrii Nakryiko  * duplicated type very quickly.
1245d5caef5bSAndrii Nakryiko  *
1246d5caef5bSAndrii Nakryiko  * Struct/union deduplication is the most critical part and algorithm for
1247d5caef5bSAndrii Nakryiko  * deduplicating structs/unions is described in greater details in comments for
1248d5caef5bSAndrii Nakryiko  * `btf_dedup_is_equiv` function.
1249d5caef5bSAndrii Nakryiko  */
1250d5caef5bSAndrii Nakryiko int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
1251d5caef5bSAndrii Nakryiko 	       const struct btf_dedup_opts *opts)
1252d5caef5bSAndrii Nakryiko {
1253d5caef5bSAndrii Nakryiko 	struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
1254d5caef5bSAndrii Nakryiko 	int err;
1255d5caef5bSAndrii Nakryiko 
1256d5caef5bSAndrii Nakryiko 	if (IS_ERR(d)) {
1257d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
1258d5caef5bSAndrii Nakryiko 		return -EINVAL;
1259d5caef5bSAndrii Nakryiko 	}
1260d5caef5bSAndrii Nakryiko 
1261d5caef5bSAndrii Nakryiko 	err = btf_dedup_strings(d);
1262d5caef5bSAndrii Nakryiko 	if (err < 0) {
1263d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_strings failed:%d\n", err);
1264d5caef5bSAndrii Nakryiko 		goto done;
1265d5caef5bSAndrii Nakryiko 	}
1266d5caef5bSAndrii Nakryiko 	err = btf_dedup_prim_types(d);
1267d5caef5bSAndrii Nakryiko 	if (err < 0) {
1268d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_prim_types failed:%d\n", err);
1269d5caef5bSAndrii Nakryiko 		goto done;
1270d5caef5bSAndrii Nakryiko 	}
1271d5caef5bSAndrii Nakryiko 	err = btf_dedup_struct_types(d);
1272d5caef5bSAndrii Nakryiko 	if (err < 0) {
1273d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_struct_types failed:%d\n", err);
1274d5caef5bSAndrii Nakryiko 		goto done;
1275d5caef5bSAndrii Nakryiko 	}
1276d5caef5bSAndrii Nakryiko 	err = btf_dedup_ref_types(d);
1277d5caef5bSAndrii Nakryiko 	if (err < 0) {
1278d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_ref_types failed:%d\n", err);
1279d5caef5bSAndrii Nakryiko 		goto done;
1280d5caef5bSAndrii Nakryiko 	}
1281d5caef5bSAndrii Nakryiko 	err = btf_dedup_compact_types(d);
1282d5caef5bSAndrii Nakryiko 	if (err < 0) {
1283d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_compact_types failed:%d\n", err);
1284d5caef5bSAndrii Nakryiko 		goto done;
1285d5caef5bSAndrii Nakryiko 	}
1286d5caef5bSAndrii Nakryiko 	err = btf_dedup_remap_types(d);
1287d5caef5bSAndrii Nakryiko 	if (err < 0) {
1288d5caef5bSAndrii Nakryiko 		pr_debug("btf_dedup_remap_types failed:%d\n", err);
1289d5caef5bSAndrii Nakryiko 		goto done;
1290d5caef5bSAndrii Nakryiko 	}
1291d5caef5bSAndrii Nakryiko 
1292d5caef5bSAndrii Nakryiko done:
1293d5caef5bSAndrii Nakryiko 	btf_dedup_free(d);
1294d5caef5bSAndrii Nakryiko 	return err;
1295d5caef5bSAndrii Nakryiko }
1296d5caef5bSAndrii Nakryiko 
1297d5caef5bSAndrii Nakryiko #define BTF_UNPROCESSED_ID ((__u32)-1)
1298d5caef5bSAndrii Nakryiko #define BTF_IN_PROGRESS_ID ((__u32)-2)
1299d5caef5bSAndrii Nakryiko 
1300d5caef5bSAndrii Nakryiko struct btf_dedup {
1301d5caef5bSAndrii Nakryiko 	/* .BTF section to be deduped in-place */
1302d5caef5bSAndrii Nakryiko 	struct btf *btf;
1303d5caef5bSAndrii Nakryiko 	/*
1304d5caef5bSAndrii Nakryiko 	 * Optional .BTF.ext section. When provided, any strings referenced
1305d5caef5bSAndrii Nakryiko 	 * from it will be taken into account when deduping strings
1306d5caef5bSAndrii Nakryiko 	 */
1307d5caef5bSAndrii Nakryiko 	struct btf_ext *btf_ext;
1308d5caef5bSAndrii Nakryiko 	/*
1309d5caef5bSAndrii Nakryiko 	 * This is a map from any type's signature hash to a list of possible
1310d5caef5bSAndrii Nakryiko 	 * canonical representative type candidates. Hash collisions are
1311d5caef5bSAndrii Nakryiko 	 * ignored, so even types of various kinds can share same list of
1312d5caef5bSAndrii Nakryiko 	 * candidates, which is fine because we rely on subsequent
1313d5caef5bSAndrii Nakryiko 	 * btf_xxx_equal() checks to authoritatively verify type equality.
1314d5caef5bSAndrii Nakryiko 	 */
1315*2fc3fc0bSAndrii Nakryiko 	struct hashmap *dedup_table;
1316d5caef5bSAndrii Nakryiko 	/* Canonical types map */
1317d5caef5bSAndrii Nakryiko 	__u32 *map;
1318d5caef5bSAndrii Nakryiko 	/* Hypothetical mapping, used during type graph equivalence checks */
1319d5caef5bSAndrii Nakryiko 	__u32 *hypot_map;
1320d5caef5bSAndrii Nakryiko 	__u32 *hypot_list;
1321d5caef5bSAndrii Nakryiko 	size_t hypot_cnt;
1322d5caef5bSAndrii Nakryiko 	size_t hypot_cap;
1323d5caef5bSAndrii Nakryiko 	/* Various option modifying behavior of algorithm */
1324d5caef5bSAndrii Nakryiko 	struct btf_dedup_opts opts;
1325d5caef5bSAndrii Nakryiko };
1326d5caef5bSAndrii Nakryiko 
1327d5caef5bSAndrii Nakryiko struct btf_str_ptr {
1328d5caef5bSAndrii Nakryiko 	const char *str;
1329d5caef5bSAndrii Nakryiko 	__u32 new_off;
1330d5caef5bSAndrii Nakryiko 	bool used;
1331d5caef5bSAndrii Nakryiko };
1332d5caef5bSAndrii Nakryiko 
1333d5caef5bSAndrii Nakryiko struct btf_str_ptrs {
1334d5caef5bSAndrii Nakryiko 	struct btf_str_ptr *ptrs;
1335d5caef5bSAndrii Nakryiko 	const char *data;
1336d5caef5bSAndrii Nakryiko 	__u32 cnt;
1337d5caef5bSAndrii Nakryiko 	__u32 cap;
1338d5caef5bSAndrii Nakryiko };
1339d5caef5bSAndrii Nakryiko 
1340*2fc3fc0bSAndrii Nakryiko static long hash_combine(long h, long value)
1341d5caef5bSAndrii Nakryiko {
1342*2fc3fc0bSAndrii Nakryiko 	return h * 31 + value;
1343d5caef5bSAndrii Nakryiko }
1344d5caef5bSAndrii Nakryiko 
1345*2fc3fc0bSAndrii Nakryiko #define for_each_dedup_cand(d, node, hash) \
1346*2fc3fc0bSAndrii Nakryiko 	hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
1347d5caef5bSAndrii Nakryiko 
1348*2fc3fc0bSAndrii Nakryiko static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
1349d5caef5bSAndrii Nakryiko {
1350*2fc3fc0bSAndrii Nakryiko 	return hashmap__append(d->dedup_table,
1351*2fc3fc0bSAndrii Nakryiko 			       (void *)hash, (void *)(long)type_id);
1352d5caef5bSAndrii Nakryiko }
1353d5caef5bSAndrii Nakryiko 
1354d5caef5bSAndrii Nakryiko static int btf_dedup_hypot_map_add(struct btf_dedup *d,
1355d5caef5bSAndrii Nakryiko 				   __u32 from_id, __u32 to_id)
1356d5caef5bSAndrii Nakryiko {
1357d5caef5bSAndrii Nakryiko 	if (d->hypot_cnt == d->hypot_cap) {
1358d5caef5bSAndrii Nakryiko 		__u32 *new_list;
1359d5caef5bSAndrii Nakryiko 
1360d5caef5bSAndrii Nakryiko 		d->hypot_cap += max(16, d->hypot_cap / 2);
1361d5caef5bSAndrii Nakryiko 		new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap);
1362d5caef5bSAndrii Nakryiko 		if (!new_list)
1363d5caef5bSAndrii Nakryiko 			return -ENOMEM;
1364d5caef5bSAndrii Nakryiko 		d->hypot_list = new_list;
1365d5caef5bSAndrii Nakryiko 	}
1366d5caef5bSAndrii Nakryiko 	d->hypot_list[d->hypot_cnt++] = from_id;
1367d5caef5bSAndrii Nakryiko 	d->hypot_map[from_id] = to_id;
1368d5caef5bSAndrii Nakryiko 	return 0;
1369d5caef5bSAndrii Nakryiko }
1370d5caef5bSAndrii Nakryiko 
1371d5caef5bSAndrii Nakryiko static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
1372d5caef5bSAndrii Nakryiko {
1373d5caef5bSAndrii Nakryiko 	int i;
1374d5caef5bSAndrii Nakryiko 
1375d5caef5bSAndrii Nakryiko 	for (i = 0; i < d->hypot_cnt; i++)
1376d5caef5bSAndrii Nakryiko 		d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
1377d5caef5bSAndrii Nakryiko 	d->hypot_cnt = 0;
1378d5caef5bSAndrii Nakryiko }
1379d5caef5bSAndrii Nakryiko 
1380d5caef5bSAndrii Nakryiko static void btf_dedup_free(struct btf_dedup *d)
1381d5caef5bSAndrii Nakryiko {
1382*2fc3fc0bSAndrii Nakryiko 	hashmap__free(d->dedup_table);
1383*2fc3fc0bSAndrii Nakryiko 	d->dedup_table = NULL;
1384d5caef5bSAndrii Nakryiko 
1385d5caef5bSAndrii Nakryiko 	free(d->map);
1386d5caef5bSAndrii Nakryiko 	d->map = NULL;
1387d5caef5bSAndrii Nakryiko 
1388d5caef5bSAndrii Nakryiko 	free(d->hypot_map);
1389d5caef5bSAndrii Nakryiko 	d->hypot_map = NULL;
1390d5caef5bSAndrii Nakryiko 
1391d5caef5bSAndrii Nakryiko 	free(d->hypot_list);
1392d5caef5bSAndrii Nakryiko 	d->hypot_list = NULL;
1393d5caef5bSAndrii Nakryiko 
1394d5caef5bSAndrii Nakryiko 	free(d);
1395d5caef5bSAndrii Nakryiko }
1396d5caef5bSAndrii Nakryiko 
1397*2fc3fc0bSAndrii Nakryiko static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
139851edf5f6SAndrii Nakryiko {
1399*2fc3fc0bSAndrii Nakryiko 	return (size_t)key;
140051edf5f6SAndrii Nakryiko }
140151edf5f6SAndrii Nakryiko 
1402*2fc3fc0bSAndrii Nakryiko static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
1403*2fc3fc0bSAndrii Nakryiko {
1404*2fc3fc0bSAndrii Nakryiko 	return 0;
1405*2fc3fc0bSAndrii Nakryiko }
1406*2fc3fc0bSAndrii Nakryiko 
1407*2fc3fc0bSAndrii Nakryiko static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
1408*2fc3fc0bSAndrii Nakryiko {
1409*2fc3fc0bSAndrii Nakryiko 	return k1 == k2;
1410*2fc3fc0bSAndrii Nakryiko }
141151edf5f6SAndrii Nakryiko 
1412d5caef5bSAndrii Nakryiko static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1413d5caef5bSAndrii Nakryiko 				       const struct btf_dedup_opts *opts)
1414d5caef5bSAndrii Nakryiko {
1415d5caef5bSAndrii Nakryiko 	struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
1416*2fc3fc0bSAndrii Nakryiko 	hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
1417d5caef5bSAndrii Nakryiko 	int i, err = 0;
1418d5caef5bSAndrii Nakryiko 
1419d5caef5bSAndrii Nakryiko 	if (!d)
1420d5caef5bSAndrii Nakryiko 		return ERR_PTR(-ENOMEM);
1421d5caef5bSAndrii Nakryiko 
142251edf5f6SAndrii Nakryiko 	d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
1423*2fc3fc0bSAndrii Nakryiko 	/* dedup_table_size is now used only to force collisions in tests */
1424*2fc3fc0bSAndrii Nakryiko 	if (opts && opts->dedup_table_size == 1)
1425*2fc3fc0bSAndrii Nakryiko 		hash_fn = btf_dedup_collision_hash_fn;
142651edf5f6SAndrii Nakryiko 
1427d5caef5bSAndrii Nakryiko 	d->btf = btf;
1428d5caef5bSAndrii Nakryiko 	d->btf_ext = btf_ext;
1429d5caef5bSAndrii Nakryiko 
1430*2fc3fc0bSAndrii Nakryiko 	d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
1431*2fc3fc0bSAndrii Nakryiko 	if (IS_ERR(d->dedup_table)) {
1432*2fc3fc0bSAndrii Nakryiko 		err = PTR_ERR(d->dedup_table);
1433*2fc3fc0bSAndrii Nakryiko 		d->dedup_table = NULL;
1434d5caef5bSAndrii Nakryiko 		goto done;
1435d5caef5bSAndrii Nakryiko 	}
1436d5caef5bSAndrii Nakryiko 
1437d5caef5bSAndrii Nakryiko 	d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1438d5caef5bSAndrii Nakryiko 	if (!d->map) {
1439d5caef5bSAndrii Nakryiko 		err = -ENOMEM;
1440d5caef5bSAndrii Nakryiko 		goto done;
1441d5caef5bSAndrii Nakryiko 	}
1442d5caef5bSAndrii Nakryiko 	/* special BTF "void" type is made canonical immediately */
1443d5caef5bSAndrii Nakryiko 	d->map[0] = 0;
1444189cf5a4SAndrii Nakryiko 	for (i = 1; i <= btf->nr_types; i++) {
1445189cf5a4SAndrii Nakryiko 		struct btf_type *t = d->btf->types[i];
1446189cf5a4SAndrii Nakryiko 		__u16 kind = BTF_INFO_KIND(t->info);
1447189cf5a4SAndrii Nakryiko 
1448189cf5a4SAndrii Nakryiko 		/* VAR and DATASEC are never deduped and are self-canonical */
1449189cf5a4SAndrii Nakryiko 		if (kind == BTF_KIND_VAR || kind == BTF_KIND_DATASEC)
1450189cf5a4SAndrii Nakryiko 			d->map[i] = i;
1451189cf5a4SAndrii Nakryiko 		else
1452d5caef5bSAndrii Nakryiko 			d->map[i] = BTF_UNPROCESSED_ID;
1453189cf5a4SAndrii Nakryiko 	}
1454d5caef5bSAndrii Nakryiko 
1455d5caef5bSAndrii Nakryiko 	d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1456d5caef5bSAndrii Nakryiko 	if (!d->hypot_map) {
1457d5caef5bSAndrii Nakryiko 		err = -ENOMEM;
1458d5caef5bSAndrii Nakryiko 		goto done;
1459d5caef5bSAndrii Nakryiko 	}
1460d5caef5bSAndrii Nakryiko 	for (i = 0; i <= btf->nr_types; i++)
1461d5caef5bSAndrii Nakryiko 		d->hypot_map[i] = BTF_UNPROCESSED_ID;
1462d5caef5bSAndrii Nakryiko 
1463d5caef5bSAndrii Nakryiko done:
1464d5caef5bSAndrii Nakryiko 	if (err) {
1465d5caef5bSAndrii Nakryiko 		btf_dedup_free(d);
1466d5caef5bSAndrii Nakryiko 		return ERR_PTR(err);
1467d5caef5bSAndrii Nakryiko 	}
1468d5caef5bSAndrii Nakryiko 
1469d5caef5bSAndrii Nakryiko 	return d;
1470d5caef5bSAndrii Nakryiko }
1471d5caef5bSAndrii Nakryiko 
1472d5caef5bSAndrii Nakryiko typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
1473d5caef5bSAndrii Nakryiko 
1474d5caef5bSAndrii Nakryiko /*
1475d5caef5bSAndrii Nakryiko  * Iterate over all possible places in .BTF and .BTF.ext that can reference
1476d5caef5bSAndrii Nakryiko  * string and pass pointer to it to a provided callback `fn`.
1477d5caef5bSAndrii Nakryiko  */
1478d5caef5bSAndrii Nakryiko static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
1479d5caef5bSAndrii Nakryiko {
1480d5caef5bSAndrii Nakryiko 	void *line_data_cur, *line_data_end;
1481d5caef5bSAndrii Nakryiko 	int i, j, r, rec_size;
1482d5caef5bSAndrii Nakryiko 	struct btf_type *t;
1483d5caef5bSAndrii Nakryiko 
1484d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
1485d5caef5bSAndrii Nakryiko 		t = d->btf->types[i];
1486d5caef5bSAndrii Nakryiko 		r = fn(&t->name_off, ctx);
1487d5caef5bSAndrii Nakryiko 		if (r)
1488d5caef5bSAndrii Nakryiko 			return r;
1489d5caef5bSAndrii Nakryiko 
1490d5caef5bSAndrii Nakryiko 		switch (BTF_INFO_KIND(t->info)) {
1491d5caef5bSAndrii Nakryiko 		case BTF_KIND_STRUCT:
1492d5caef5bSAndrii Nakryiko 		case BTF_KIND_UNION: {
1493d5caef5bSAndrii Nakryiko 			struct btf_member *m = (struct btf_member *)(t + 1);
1494d5caef5bSAndrii Nakryiko 			__u16 vlen = BTF_INFO_VLEN(t->info);
1495d5caef5bSAndrii Nakryiko 
1496d5caef5bSAndrii Nakryiko 			for (j = 0; j < vlen; j++) {
1497d5caef5bSAndrii Nakryiko 				r = fn(&m->name_off, ctx);
1498d5caef5bSAndrii Nakryiko 				if (r)
1499d5caef5bSAndrii Nakryiko 					return r;
1500d5caef5bSAndrii Nakryiko 				m++;
1501d5caef5bSAndrii Nakryiko 			}
1502d5caef5bSAndrii Nakryiko 			break;
1503d5caef5bSAndrii Nakryiko 		}
1504d5caef5bSAndrii Nakryiko 		case BTF_KIND_ENUM: {
1505d5caef5bSAndrii Nakryiko 			struct btf_enum *m = (struct btf_enum *)(t + 1);
1506d5caef5bSAndrii Nakryiko 			__u16 vlen = BTF_INFO_VLEN(t->info);
1507d5caef5bSAndrii Nakryiko 
1508d5caef5bSAndrii Nakryiko 			for (j = 0; j < vlen; j++) {
1509d5caef5bSAndrii Nakryiko 				r = fn(&m->name_off, ctx);
1510d5caef5bSAndrii Nakryiko 				if (r)
1511d5caef5bSAndrii Nakryiko 					return r;
1512d5caef5bSAndrii Nakryiko 				m++;
1513d5caef5bSAndrii Nakryiko 			}
1514d5caef5bSAndrii Nakryiko 			break;
1515d5caef5bSAndrii Nakryiko 		}
1516d5caef5bSAndrii Nakryiko 		case BTF_KIND_FUNC_PROTO: {
1517d5caef5bSAndrii Nakryiko 			struct btf_param *m = (struct btf_param *)(t + 1);
1518d5caef5bSAndrii Nakryiko 			__u16 vlen = BTF_INFO_VLEN(t->info);
1519d5caef5bSAndrii Nakryiko 
1520d5caef5bSAndrii Nakryiko 			for (j = 0; j < vlen; j++) {
1521d5caef5bSAndrii Nakryiko 				r = fn(&m->name_off, ctx);
1522d5caef5bSAndrii Nakryiko 				if (r)
1523d5caef5bSAndrii Nakryiko 					return r;
1524d5caef5bSAndrii Nakryiko 				m++;
1525d5caef5bSAndrii Nakryiko 			}
1526d5caef5bSAndrii Nakryiko 			break;
1527d5caef5bSAndrii Nakryiko 		}
1528d5caef5bSAndrii Nakryiko 		default:
1529d5caef5bSAndrii Nakryiko 			break;
1530d5caef5bSAndrii Nakryiko 		}
1531d5caef5bSAndrii Nakryiko 	}
1532d5caef5bSAndrii Nakryiko 
1533d5caef5bSAndrii Nakryiko 	if (!d->btf_ext)
1534d5caef5bSAndrii Nakryiko 		return 0;
1535d5caef5bSAndrii Nakryiko 
1536d5caef5bSAndrii Nakryiko 	line_data_cur = d->btf_ext->line_info.info;
1537d5caef5bSAndrii Nakryiko 	line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
1538d5caef5bSAndrii Nakryiko 	rec_size = d->btf_ext->line_info.rec_size;
1539d5caef5bSAndrii Nakryiko 
1540d5caef5bSAndrii Nakryiko 	while (line_data_cur < line_data_end) {
1541d5caef5bSAndrii Nakryiko 		struct btf_ext_info_sec *sec = line_data_cur;
1542d5caef5bSAndrii Nakryiko 		struct bpf_line_info_min *line_info;
1543d5caef5bSAndrii Nakryiko 		__u32 num_info = sec->num_info;
1544d5caef5bSAndrii Nakryiko 
1545d5caef5bSAndrii Nakryiko 		r = fn(&sec->sec_name_off, ctx);
1546d5caef5bSAndrii Nakryiko 		if (r)
1547d5caef5bSAndrii Nakryiko 			return r;
1548d5caef5bSAndrii Nakryiko 
1549d5caef5bSAndrii Nakryiko 		line_data_cur += sizeof(struct btf_ext_info_sec);
1550d5caef5bSAndrii Nakryiko 		for (i = 0; i < num_info; i++) {
1551d5caef5bSAndrii Nakryiko 			line_info = line_data_cur;
1552d5caef5bSAndrii Nakryiko 			r = fn(&line_info->file_name_off, ctx);
1553d5caef5bSAndrii Nakryiko 			if (r)
1554d5caef5bSAndrii Nakryiko 				return r;
1555d5caef5bSAndrii Nakryiko 			r = fn(&line_info->line_off, ctx);
1556d5caef5bSAndrii Nakryiko 			if (r)
1557d5caef5bSAndrii Nakryiko 				return r;
1558d5caef5bSAndrii Nakryiko 			line_data_cur += rec_size;
1559d5caef5bSAndrii Nakryiko 		}
1560d5caef5bSAndrii Nakryiko 	}
1561d5caef5bSAndrii Nakryiko 
1562d5caef5bSAndrii Nakryiko 	return 0;
1563d5caef5bSAndrii Nakryiko }
1564d5caef5bSAndrii Nakryiko 
1565d5caef5bSAndrii Nakryiko static int str_sort_by_content(const void *a1, const void *a2)
1566d5caef5bSAndrii Nakryiko {
1567d5caef5bSAndrii Nakryiko 	const struct btf_str_ptr *p1 = a1;
1568d5caef5bSAndrii Nakryiko 	const struct btf_str_ptr *p2 = a2;
1569d5caef5bSAndrii Nakryiko 
1570d5caef5bSAndrii Nakryiko 	return strcmp(p1->str, p2->str);
1571d5caef5bSAndrii Nakryiko }
1572d5caef5bSAndrii Nakryiko 
1573d5caef5bSAndrii Nakryiko static int str_sort_by_offset(const void *a1, const void *a2)
1574d5caef5bSAndrii Nakryiko {
1575d5caef5bSAndrii Nakryiko 	const struct btf_str_ptr *p1 = a1;
1576d5caef5bSAndrii Nakryiko 	const struct btf_str_ptr *p2 = a2;
1577d5caef5bSAndrii Nakryiko 
1578d5caef5bSAndrii Nakryiko 	if (p1->str != p2->str)
1579d5caef5bSAndrii Nakryiko 		return p1->str < p2->str ? -1 : 1;
1580d5caef5bSAndrii Nakryiko 	return 0;
1581d5caef5bSAndrii Nakryiko }
1582d5caef5bSAndrii Nakryiko 
1583d5caef5bSAndrii Nakryiko static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
1584d5caef5bSAndrii Nakryiko {
1585d5caef5bSAndrii Nakryiko 	const struct btf_str_ptr *p = pelem;
1586d5caef5bSAndrii Nakryiko 
1587d5caef5bSAndrii Nakryiko 	if (str_ptr != p->str)
1588d5caef5bSAndrii Nakryiko 		return (const char *)str_ptr < p->str ? -1 : 1;
1589d5caef5bSAndrii Nakryiko 	return 0;
1590d5caef5bSAndrii Nakryiko }
1591d5caef5bSAndrii Nakryiko 
1592d5caef5bSAndrii Nakryiko static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
1593d5caef5bSAndrii Nakryiko {
1594d5caef5bSAndrii Nakryiko 	struct btf_str_ptrs *strs;
1595d5caef5bSAndrii Nakryiko 	struct btf_str_ptr *s;
1596d5caef5bSAndrii Nakryiko 
1597d5caef5bSAndrii Nakryiko 	if (*str_off_ptr == 0)
1598d5caef5bSAndrii Nakryiko 		return 0;
1599d5caef5bSAndrii Nakryiko 
1600d5caef5bSAndrii Nakryiko 	strs = ctx;
1601d5caef5bSAndrii Nakryiko 	s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1602d5caef5bSAndrii Nakryiko 		    sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1603d5caef5bSAndrii Nakryiko 	if (!s)
1604d5caef5bSAndrii Nakryiko 		return -EINVAL;
1605d5caef5bSAndrii Nakryiko 	s->used = true;
1606d5caef5bSAndrii Nakryiko 	return 0;
1607d5caef5bSAndrii Nakryiko }
1608d5caef5bSAndrii Nakryiko 
1609d5caef5bSAndrii Nakryiko static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
1610d5caef5bSAndrii Nakryiko {
1611d5caef5bSAndrii Nakryiko 	struct btf_str_ptrs *strs;
1612d5caef5bSAndrii Nakryiko 	struct btf_str_ptr *s;
1613d5caef5bSAndrii Nakryiko 
1614d5caef5bSAndrii Nakryiko 	if (*str_off_ptr == 0)
1615d5caef5bSAndrii Nakryiko 		return 0;
1616d5caef5bSAndrii Nakryiko 
1617d5caef5bSAndrii Nakryiko 	strs = ctx;
1618d5caef5bSAndrii Nakryiko 	s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1619d5caef5bSAndrii Nakryiko 		    sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1620d5caef5bSAndrii Nakryiko 	if (!s)
1621d5caef5bSAndrii Nakryiko 		return -EINVAL;
1622d5caef5bSAndrii Nakryiko 	*str_off_ptr = s->new_off;
1623d5caef5bSAndrii Nakryiko 	return 0;
1624d5caef5bSAndrii Nakryiko }
1625d5caef5bSAndrii Nakryiko 
1626d5caef5bSAndrii Nakryiko /*
1627d5caef5bSAndrii Nakryiko  * Dedup string and filter out those that are not referenced from either .BTF
1628d5caef5bSAndrii Nakryiko  * or .BTF.ext (if provided) sections.
1629d5caef5bSAndrii Nakryiko  *
1630d5caef5bSAndrii Nakryiko  * This is done by building index of all strings in BTF's string section,
1631d5caef5bSAndrii Nakryiko  * then iterating over all entities that can reference strings (e.g., type
1632d5caef5bSAndrii Nakryiko  * names, struct field names, .BTF.ext line info, etc) and marking corresponding
1633d5caef5bSAndrii Nakryiko  * strings as used. After that all used strings are deduped and compacted into
1634d5caef5bSAndrii Nakryiko  * sequential blob of memory and new offsets are calculated. Then all the string
1635d5caef5bSAndrii Nakryiko  * references are iterated again and rewritten using new offsets.
1636d5caef5bSAndrii Nakryiko  */
1637d5caef5bSAndrii Nakryiko static int btf_dedup_strings(struct btf_dedup *d)
1638d5caef5bSAndrii Nakryiko {
1639d5caef5bSAndrii Nakryiko 	const struct btf_header *hdr = d->btf->hdr;
1640d5caef5bSAndrii Nakryiko 	char *start = (char *)d->btf->nohdr_data + hdr->str_off;
1641d5caef5bSAndrii Nakryiko 	char *end = start + d->btf->hdr->str_len;
1642d5caef5bSAndrii Nakryiko 	char *p = start, *tmp_strs = NULL;
1643d5caef5bSAndrii Nakryiko 	struct btf_str_ptrs strs = {
1644d5caef5bSAndrii Nakryiko 		.cnt = 0,
1645d5caef5bSAndrii Nakryiko 		.cap = 0,
1646d5caef5bSAndrii Nakryiko 		.ptrs = NULL,
1647d5caef5bSAndrii Nakryiko 		.data = start,
1648d5caef5bSAndrii Nakryiko 	};
1649d5caef5bSAndrii Nakryiko 	int i, j, err = 0, grp_idx;
1650d5caef5bSAndrii Nakryiko 	bool grp_used;
1651d5caef5bSAndrii Nakryiko 
1652d5caef5bSAndrii Nakryiko 	/* build index of all strings */
1653d5caef5bSAndrii Nakryiko 	while (p < end) {
1654d5caef5bSAndrii Nakryiko 		if (strs.cnt + 1 > strs.cap) {
1655d5caef5bSAndrii Nakryiko 			struct btf_str_ptr *new_ptrs;
1656d5caef5bSAndrii Nakryiko 
1657d5caef5bSAndrii Nakryiko 			strs.cap += max(strs.cnt / 2, 16);
1658d5caef5bSAndrii Nakryiko 			new_ptrs = realloc(strs.ptrs,
1659d5caef5bSAndrii Nakryiko 					   sizeof(strs.ptrs[0]) * strs.cap);
1660d5caef5bSAndrii Nakryiko 			if (!new_ptrs) {
1661d5caef5bSAndrii Nakryiko 				err = -ENOMEM;
1662d5caef5bSAndrii Nakryiko 				goto done;
1663d5caef5bSAndrii Nakryiko 			}
1664d5caef5bSAndrii Nakryiko 			strs.ptrs = new_ptrs;
1665d5caef5bSAndrii Nakryiko 		}
1666d5caef5bSAndrii Nakryiko 
1667d5caef5bSAndrii Nakryiko 		strs.ptrs[strs.cnt].str = p;
1668d5caef5bSAndrii Nakryiko 		strs.ptrs[strs.cnt].used = false;
1669d5caef5bSAndrii Nakryiko 
1670d5caef5bSAndrii Nakryiko 		p += strlen(p) + 1;
1671d5caef5bSAndrii Nakryiko 		strs.cnt++;
1672d5caef5bSAndrii Nakryiko 	}
1673d5caef5bSAndrii Nakryiko 
1674d5caef5bSAndrii Nakryiko 	/* temporary storage for deduplicated strings */
1675d5caef5bSAndrii Nakryiko 	tmp_strs = malloc(d->btf->hdr->str_len);
1676d5caef5bSAndrii Nakryiko 	if (!tmp_strs) {
1677d5caef5bSAndrii Nakryiko 		err = -ENOMEM;
1678d5caef5bSAndrii Nakryiko 		goto done;
1679d5caef5bSAndrii Nakryiko 	}
1680d5caef5bSAndrii Nakryiko 
1681d5caef5bSAndrii Nakryiko 	/* mark all used strings */
1682d5caef5bSAndrii Nakryiko 	strs.ptrs[0].used = true;
1683d5caef5bSAndrii Nakryiko 	err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
1684d5caef5bSAndrii Nakryiko 	if (err)
1685d5caef5bSAndrii Nakryiko 		goto done;
1686d5caef5bSAndrii Nakryiko 
1687d5caef5bSAndrii Nakryiko 	/* sort strings by context, so that we can identify duplicates */
1688d5caef5bSAndrii Nakryiko 	qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
1689d5caef5bSAndrii Nakryiko 
1690d5caef5bSAndrii Nakryiko 	/*
1691d5caef5bSAndrii Nakryiko 	 * iterate groups of equal strings and if any instance in a group was
1692d5caef5bSAndrii Nakryiko 	 * referenced, emit single instance and remember new offset
1693d5caef5bSAndrii Nakryiko 	 */
1694d5caef5bSAndrii Nakryiko 	p = tmp_strs;
1695d5caef5bSAndrii Nakryiko 	grp_idx = 0;
1696d5caef5bSAndrii Nakryiko 	grp_used = strs.ptrs[0].used;
1697d5caef5bSAndrii Nakryiko 	/* iterate past end to avoid code duplication after loop */
1698d5caef5bSAndrii Nakryiko 	for (i = 1; i <= strs.cnt; i++) {
1699d5caef5bSAndrii Nakryiko 		/*
1700d5caef5bSAndrii Nakryiko 		 * when i == strs.cnt, we want to skip string comparison and go
1701d5caef5bSAndrii Nakryiko 		 * straight to handling last group of strings (otherwise we'd
1702d5caef5bSAndrii Nakryiko 		 * need to handle last group after the loop w/ duplicated code)
1703d5caef5bSAndrii Nakryiko 		 */
1704d5caef5bSAndrii Nakryiko 		if (i < strs.cnt &&
1705d5caef5bSAndrii Nakryiko 		    !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
1706d5caef5bSAndrii Nakryiko 			grp_used = grp_used || strs.ptrs[i].used;
1707d5caef5bSAndrii Nakryiko 			continue;
1708d5caef5bSAndrii Nakryiko 		}
1709d5caef5bSAndrii Nakryiko 
1710d5caef5bSAndrii Nakryiko 		/*
1711d5caef5bSAndrii Nakryiko 		 * this check would have been required after the loop to handle
1712d5caef5bSAndrii Nakryiko 		 * last group of strings, but due to <= condition in a loop
1713d5caef5bSAndrii Nakryiko 		 * we avoid that duplication
1714d5caef5bSAndrii Nakryiko 		 */
1715d5caef5bSAndrii Nakryiko 		if (grp_used) {
1716d5caef5bSAndrii Nakryiko 			int new_off = p - tmp_strs;
1717d5caef5bSAndrii Nakryiko 			__u32 len = strlen(strs.ptrs[grp_idx].str);
1718d5caef5bSAndrii Nakryiko 
1719d5caef5bSAndrii Nakryiko 			memmove(p, strs.ptrs[grp_idx].str, len + 1);
1720d5caef5bSAndrii Nakryiko 			for (j = grp_idx; j < i; j++)
1721d5caef5bSAndrii Nakryiko 				strs.ptrs[j].new_off = new_off;
1722d5caef5bSAndrii Nakryiko 			p += len + 1;
1723d5caef5bSAndrii Nakryiko 		}
1724d5caef5bSAndrii Nakryiko 
1725d5caef5bSAndrii Nakryiko 		if (i < strs.cnt) {
1726d5caef5bSAndrii Nakryiko 			grp_idx = i;
1727d5caef5bSAndrii Nakryiko 			grp_used = strs.ptrs[i].used;
1728d5caef5bSAndrii Nakryiko 		}
1729d5caef5bSAndrii Nakryiko 	}
1730d5caef5bSAndrii Nakryiko 
1731d5caef5bSAndrii Nakryiko 	/* replace original strings with deduped ones */
1732d5caef5bSAndrii Nakryiko 	d->btf->hdr->str_len = p - tmp_strs;
1733d5caef5bSAndrii Nakryiko 	memmove(start, tmp_strs, d->btf->hdr->str_len);
1734d5caef5bSAndrii Nakryiko 	end = start + d->btf->hdr->str_len;
1735d5caef5bSAndrii Nakryiko 
1736d5caef5bSAndrii Nakryiko 	/* restore original order for further binary search lookups */
1737d5caef5bSAndrii Nakryiko 	qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
1738d5caef5bSAndrii Nakryiko 
1739d5caef5bSAndrii Nakryiko 	/* remap string offsets */
1740d5caef5bSAndrii Nakryiko 	err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
1741d5caef5bSAndrii Nakryiko 	if (err)
1742d5caef5bSAndrii Nakryiko 		goto done;
1743d5caef5bSAndrii Nakryiko 
1744d5caef5bSAndrii Nakryiko 	d->btf->hdr->str_len = end - start;
1745d5caef5bSAndrii Nakryiko 
1746d5caef5bSAndrii Nakryiko done:
1747d5caef5bSAndrii Nakryiko 	free(tmp_strs);
1748d5caef5bSAndrii Nakryiko 	free(strs.ptrs);
1749d5caef5bSAndrii Nakryiko 	return err;
1750d5caef5bSAndrii Nakryiko }
1751d5caef5bSAndrii Nakryiko 
1752*2fc3fc0bSAndrii Nakryiko static long btf_hash_common(struct btf_type *t)
1753d5caef5bSAndrii Nakryiko {
1754*2fc3fc0bSAndrii Nakryiko 	long h;
1755d5caef5bSAndrii Nakryiko 
1756d5caef5bSAndrii Nakryiko 	h = hash_combine(0, t->name_off);
1757d5caef5bSAndrii Nakryiko 	h = hash_combine(h, t->info);
1758d5caef5bSAndrii Nakryiko 	h = hash_combine(h, t->size);
1759d5caef5bSAndrii Nakryiko 	return h;
1760d5caef5bSAndrii Nakryiko }
1761d5caef5bSAndrii Nakryiko 
1762d5caef5bSAndrii Nakryiko static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
1763d5caef5bSAndrii Nakryiko {
1764d5caef5bSAndrii Nakryiko 	return t1->name_off == t2->name_off &&
1765d5caef5bSAndrii Nakryiko 	       t1->info == t2->info &&
1766d5caef5bSAndrii Nakryiko 	       t1->size == t2->size;
1767d5caef5bSAndrii Nakryiko }
1768d5caef5bSAndrii Nakryiko 
1769d5caef5bSAndrii Nakryiko /* Calculate type signature hash of INT. */
1770*2fc3fc0bSAndrii Nakryiko static long btf_hash_int(struct btf_type *t)
1771d5caef5bSAndrii Nakryiko {
1772d5caef5bSAndrii Nakryiko 	__u32 info = *(__u32 *)(t + 1);
1773*2fc3fc0bSAndrii Nakryiko 	long h;
1774d5caef5bSAndrii Nakryiko 
1775d5caef5bSAndrii Nakryiko 	h = btf_hash_common(t);
1776d5caef5bSAndrii Nakryiko 	h = hash_combine(h, info);
1777d5caef5bSAndrii Nakryiko 	return h;
1778d5caef5bSAndrii Nakryiko }
1779d5caef5bSAndrii Nakryiko 
1780d5caef5bSAndrii Nakryiko /* Check structural equality of two INTs. */
1781d5caef5bSAndrii Nakryiko static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
1782d5caef5bSAndrii Nakryiko {
1783d5caef5bSAndrii Nakryiko 	__u32 info1, info2;
1784d5caef5bSAndrii Nakryiko 
1785d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1786d5caef5bSAndrii Nakryiko 		return false;
1787d5caef5bSAndrii Nakryiko 	info1 = *(__u32 *)(t1 + 1);
1788d5caef5bSAndrii Nakryiko 	info2 = *(__u32 *)(t2 + 1);
1789d5caef5bSAndrii Nakryiko 	return info1 == info2;
1790d5caef5bSAndrii Nakryiko }
1791d5caef5bSAndrii Nakryiko 
1792d5caef5bSAndrii Nakryiko /* Calculate type signature hash of ENUM. */
1793*2fc3fc0bSAndrii Nakryiko static long btf_hash_enum(struct btf_type *t)
1794d5caef5bSAndrii Nakryiko {
1795*2fc3fc0bSAndrii Nakryiko 	long h;
1796d5caef5bSAndrii Nakryiko 
17979768095bSAndrii Nakryiko 	/* don't hash vlen and enum members to support enum fwd resolving */
17989768095bSAndrii Nakryiko 	h = hash_combine(0, t->name_off);
17999768095bSAndrii Nakryiko 	h = hash_combine(h, t->info & ~0xffff);
18009768095bSAndrii Nakryiko 	h = hash_combine(h, t->size);
1801d5caef5bSAndrii Nakryiko 	return h;
1802d5caef5bSAndrii Nakryiko }
1803d5caef5bSAndrii Nakryiko 
1804d5caef5bSAndrii Nakryiko /* Check structural equality of two ENUMs. */
1805d5caef5bSAndrii Nakryiko static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
1806d5caef5bSAndrii Nakryiko {
1807d5caef5bSAndrii Nakryiko 	struct btf_enum *m1, *m2;
1808d5caef5bSAndrii Nakryiko 	__u16 vlen;
1809d5caef5bSAndrii Nakryiko 	int i;
1810d5caef5bSAndrii Nakryiko 
1811d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1812d5caef5bSAndrii Nakryiko 		return false;
1813d5caef5bSAndrii Nakryiko 
1814d5caef5bSAndrii Nakryiko 	vlen = BTF_INFO_VLEN(t1->info);
1815d5caef5bSAndrii Nakryiko 	m1 = (struct btf_enum *)(t1 + 1);
1816d5caef5bSAndrii Nakryiko 	m2 = (struct btf_enum *)(t2 + 1);
1817d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
1818d5caef5bSAndrii Nakryiko 		if (m1->name_off != m2->name_off || m1->val != m2->val)
1819d5caef5bSAndrii Nakryiko 			return false;
1820d5caef5bSAndrii Nakryiko 		m1++;
1821d5caef5bSAndrii Nakryiko 		m2++;
1822d5caef5bSAndrii Nakryiko 	}
1823d5caef5bSAndrii Nakryiko 	return true;
1824d5caef5bSAndrii Nakryiko }
1825d5caef5bSAndrii Nakryiko 
18269768095bSAndrii Nakryiko static inline bool btf_is_enum_fwd(struct btf_type *t)
18279768095bSAndrii Nakryiko {
18289768095bSAndrii Nakryiko 	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM &&
18299768095bSAndrii Nakryiko 	       BTF_INFO_VLEN(t->info) == 0;
18309768095bSAndrii Nakryiko }
18319768095bSAndrii Nakryiko 
18329768095bSAndrii Nakryiko static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
18339768095bSAndrii Nakryiko {
18349768095bSAndrii Nakryiko 	if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
18359768095bSAndrii Nakryiko 		return btf_equal_enum(t1, t2);
18369768095bSAndrii Nakryiko 	/* ignore vlen when comparing */
18379768095bSAndrii Nakryiko 	return t1->name_off == t2->name_off &&
18389768095bSAndrii Nakryiko 	       (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
18399768095bSAndrii Nakryiko 	       t1->size == t2->size;
18409768095bSAndrii Nakryiko }
18419768095bSAndrii Nakryiko 
1842d5caef5bSAndrii Nakryiko /*
1843d5caef5bSAndrii Nakryiko  * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
1844d5caef5bSAndrii Nakryiko  * as referenced type IDs equivalence is established separately during type
1845d5caef5bSAndrii Nakryiko  * graph equivalence check algorithm.
1846d5caef5bSAndrii Nakryiko  */
1847*2fc3fc0bSAndrii Nakryiko static long btf_hash_struct(struct btf_type *t)
1848d5caef5bSAndrii Nakryiko {
1849d5caef5bSAndrii Nakryiko 	struct btf_member *member = (struct btf_member *)(t + 1);
1850d5caef5bSAndrii Nakryiko 	__u32 vlen = BTF_INFO_VLEN(t->info);
1851*2fc3fc0bSAndrii Nakryiko 	long h = btf_hash_common(t);
1852d5caef5bSAndrii Nakryiko 	int i;
1853d5caef5bSAndrii Nakryiko 
1854d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
1855d5caef5bSAndrii Nakryiko 		h = hash_combine(h, member->name_off);
1856d5caef5bSAndrii Nakryiko 		h = hash_combine(h, member->offset);
1857d5caef5bSAndrii Nakryiko 		/* no hashing of referenced type ID, it can be unresolved yet */
1858d5caef5bSAndrii Nakryiko 		member++;
1859d5caef5bSAndrii Nakryiko 	}
1860d5caef5bSAndrii Nakryiko 	return h;
1861d5caef5bSAndrii Nakryiko }
1862d5caef5bSAndrii Nakryiko 
1863d5caef5bSAndrii Nakryiko /*
1864d5caef5bSAndrii Nakryiko  * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1865d5caef5bSAndrii Nakryiko  * IDs. This check is performed during type graph equivalence check and
1866d5caef5bSAndrii Nakryiko  * referenced types equivalence is checked separately.
1867d5caef5bSAndrii Nakryiko  */
186891097fbeSAndrii Nakryiko static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
1869d5caef5bSAndrii Nakryiko {
1870d5caef5bSAndrii Nakryiko 	struct btf_member *m1, *m2;
1871d5caef5bSAndrii Nakryiko 	__u16 vlen;
1872d5caef5bSAndrii Nakryiko 	int i;
1873d5caef5bSAndrii Nakryiko 
1874d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1875d5caef5bSAndrii Nakryiko 		return false;
1876d5caef5bSAndrii Nakryiko 
1877d5caef5bSAndrii Nakryiko 	vlen = BTF_INFO_VLEN(t1->info);
1878d5caef5bSAndrii Nakryiko 	m1 = (struct btf_member *)(t1 + 1);
1879d5caef5bSAndrii Nakryiko 	m2 = (struct btf_member *)(t2 + 1);
1880d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
1881d5caef5bSAndrii Nakryiko 		if (m1->name_off != m2->name_off || m1->offset != m2->offset)
1882d5caef5bSAndrii Nakryiko 			return false;
1883d5caef5bSAndrii Nakryiko 		m1++;
1884d5caef5bSAndrii Nakryiko 		m2++;
1885d5caef5bSAndrii Nakryiko 	}
1886d5caef5bSAndrii Nakryiko 	return true;
1887d5caef5bSAndrii Nakryiko }
1888d5caef5bSAndrii Nakryiko 
1889d5caef5bSAndrii Nakryiko /*
1890d5caef5bSAndrii Nakryiko  * Calculate type signature hash of ARRAY, including referenced type IDs,
1891d5caef5bSAndrii Nakryiko  * under assumption that they were already resolved to canonical type IDs and
1892d5caef5bSAndrii Nakryiko  * are not going to change.
1893d5caef5bSAndrii Nakryiko  */
1894*2fc3fc0bSAndrii Nakryiko static long btf_hash_array(struct btf_type *t)
1895d5caef5bSAndrii Nakryiko {
1896d5caef5bSAndrii Nakryiko 	struct btf_array *info = (struct btf_array *)(t + 1);
1897*2fc3fc0bSAndrii Nakryiko 	long h = btf_hash_common(t);
1898d5caef5bSAndrii Nakryiko 
1899d5caef5bSAndrii Nakryiko 	h = hash_combine(h, info->type);
1900d5caef5bSAndrii Nakryiko 	h = hash_combine(h, info->index_type);
1901d5caef5bSAndrii Nakryiko 	h = hash_combine(h, info->nelems);
1902d5caef5bSAndrii Nakryiko 	return h;
1903d5caef5bSAndrii Nakryiko }
1904d5caef5bSAndrii Nakryiko 
1905d5caef5bSAndrii Nakryiko /*
1906d5caef5bSAndrii Nakryiko  * Check exact equality of two ARRAYs, taking into account referenced
1907d5caef5bSAndrii Nakryiko  * type IDs, under assumption that they were already resolved to canonical
1908d5caef5bSAndrii Nakryiko  * type IDs and are not going to change.
1909d5caef5bSAndrii Nakryiko  * This function is called during reference types deduplication to compare
1910d5caef5bSAndrii Nakryiko  * ARRAY to potential canonical representative.
1911d5caef5bSAndrii Nakryiko  */
1912d5caef5bSAndrii Nakryiko static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
1913d5caef5bSAndrii Nakryiko {
1914d5caef5bSAndrii Nakryiko 	struct btf_array *info1, *info2;
1915d5caef5bSAndrii Nakryiko 
1916d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1917d5caef5bSAndrii Nakryiko 		return false;
1918d5caef5bSAndrii Nakryiko 
1919d5caef5bSAndrii Nakryiko 	info1 = (struct btf_array *)(t1 + 1);
1920d5caef5bSAndrii Nakryiko 	info2 = (struct btf_array *)(t2 + 1);
1921d5caef5bSAndrii Nakryiko 	return info1->type == info2->type &&
1922d5caef5bSAndrii Nakryiko 	       info1->index_type == info2->index_type &&
1923d5caef5bSAndrii Nakryiko 	       info1->nelems == info2->nelems;
1924d5caef5bSAndrii Nakryiko }
1925d5caef5bSAndrii Nakryiko 
1926d5caef5bSAndrii Nakryiko /*
1927d5caef5bSAndrii Nakryiko  * Check structural compatibility of two ARRAYs, ignoring referenced type
1928d5caef5bSAndrii Nakryiko  * IDs. This check is performed during type graph equivalence check and
1929d5caef5bSAndrii Nakryiko  * referenced types equivalence is checked separately.
1930d5caef5bSAndrii Nakryiko  */
1931d5caef5bSAndrii Nakryiko static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
1932d5caef5bSAndrii Nakryiko {
1933d5caef5bSAndrii Nakryiko 	struct btf_array *info1, *info2;
1934d5caef5bSAndrii Nakryiko 
1935d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1936d5caef5bSAndrii Nakryiko 		return false;
1937d5caef5bSAndrii Nakryiko 
1938d5caef5bSAndrii Nakryiko 	info1 = (struct btf_array *)(t1 + 1);
1939d5caef5bSAndrii Nakryiko 	info2 = (struct btf_array *)(t2 + 1);
1940d5caef5bSAndrii Nakryiko 	return info1->nelems == info2->nelems;
1941d5caef5bSAndrii Nakryiko }
1942d5caef5bSAndrii Nakryiko 
1943d5caef5bSAndrii Nakryiko /*
1944d5caef5bSAndrii Nakryiko  * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
1945d5caef5bSAndrii Nakryiko  * under assumption that they were already resolved to canonical type IDs and
1946d5caef5bSAndrii Nakryiko  * are not going to change.
1947d5caef5bSAndrii Nakryiko  */
1948*2fc3fc0bSAndrii Nakryiko static long btf_hash_fnproto(struct btf_type *t)
1949d5caef5bSAndrii Nakryiko {
1950d5caef5bSAndrii Nakryiko 	struct btf_param *member = (struct btf_param *)(t + 1);
1951d5caef5bSAndrii Nakryiko 	__u16 vlen = BTF_INFO_VLEN(t->info);
1952*2fc3fc0bSAndrii Nakryiko 	long h = btf_hash_common(t);
1953d5caef5bSAndrii Nakryiko 	int i;
1954d5caef5bSAndrii Nakryiko 
1955d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
1956d5caef5bSAndrii Nakryiko 		h = hash_combine(h, member->name_off);
1957d5caef5bSAndrii Nakryiko 		h = hash_combine(h, member->type);
1958d5caef5bSAndrii Nakryiko 		member++;
1959d5caef5bSAndrii Nakryiko 	}
1960d5caef5bSAndrii Nakryiko 	return h;
1961d5caef5bSAndrii Nakryiko }
1962d5caef5bSAndrii Nakryiko 
1963d5caef5bSAndrii Nakryiko /*
1964d5caef5bSAndrii Nakryiko  * Check exact equality of two FUNC_PROTOs, taking into account referenced
1965d5caef5bSAndrii Nakryiko  * type IDs, under assumption that they were already resolved to canonical
1966d5caef5bSAndrii Nakryiko  * type IDs and are not going to change.
1967d5caef5bSAndrii Nakryiko  * This function is called during reference types deduplication to compare
1968d5caef5bSAndrii Nakryiko  * FUNC_PROTO to potential canonical representative.
1969d5caef5bSAndrii Nakryiko  */
1970*2fc3fc0bSAndrii Nakryiko static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
1971d5caef5bSAndrii Nakryiko {
1972d5caef5bSAndrii Nakryiko 	struct btf_param *m1, *m2;
1973d5caef5bSAndrii Nakryiko 	__u16 vlen;
1974d5caef5bSAndrii Nakryiko 	int i;
1975d5caef5bSAndrii Nakryiko 
1976d5caef5bSAndrii Nakryiko 	if (!btf_equal_common(t1, t2))
1977d5caef5bSAndrii Nakryiko 		return false;
1978d5caef5bSAndrii Nakryiko 
1979d5caef5bSAndrii Nakryiko 	vlen = BTF_INFO_VLEN(t1->info);
1980d5caef5bSAndrii Nakryiko 	m1 = (struct btf_param *)(t1 + 1);
1981d5caef5bSAndrii Nakryiko 	m2 = (struct btf_param *)(t2 + 1);
1982d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
1983d5caef5bSAndrii Nakryiko 		if (m1->name_off != m2->name_off || m1->type != m2->type)
1984d5caef5bSAndrii Nakryiko 			return false;
1985d5caef5bSAndrii Nakryiko 		m1++;
1986d5caef5bSAndrii Nakryiko 		m2++;
1987d5caef5bSAndrii Nakryiko 	}
1988d5caef5bSAndrii Nakryiko 	return true;
1989d5caef5bSAndrii Nakryiko }
1990d5caef5bSAndrii Nakryiko 
1991d5caef5bSAndrii Nakryiko /*
1992d5caef5bSAndrii Nakryiko  * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1993d5caef5bSAndrii Nakryiko  * IDs. This check is performed during type graph equivalence check and
1994d5caef5bSAndrii Nakryiko  * referenced types equivalence is checked separately.
1995d5caef5bSAndrii Nakryiko  */
1996*2fc3fc0bSAndrii Nakryiko static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
1997d5caef5bSAndrii Nakryiko {
1998d5caef5bSAndrii Nakryiko 	struct btf_param *m1, *m2;
1999d5caef5bSAndrii Nakryiko 	__u16 vlen;
2000d5caef5bSAndrii Nakryiko 	int i;
2001d5caef5bSAndrii Nakryiko 
2002d5caef5bSAndrii Nakryiko 	/* skip return type ID */
2003d5caef5bSAndrii Nakryiko 	if (t1->name_off != t2->name_off || t1->info != t2->info)
2004d5caef5bSAndrii Nakryiko 		return false;
2005d5caef5bSAndrii Nakryiko 
2006d5caef5bSAndrii Nakryiko 	vlen = BTF_INFO_VLEN(t1->info);
2007d5caef5bSAndrii Nakryiko 	m1 = (struct btf_param *)(t1 + 1);
2008d5caef5bSAndrii Nakryiko 	m2 = (struct btf_param *)(t2 + 1);
2009d5caef5bSAndrii Nakryiko 	for (i = 0; i < vlen; i++) {
2010d5caef5bSAndrii Nakryiko 		if (m1->name_off != m2->name_off)
2011d5caef5bSAndrii Nakryiko 			return false;
2012d5caef5bSAndrii Nakryiko 		m1++;
2013d5caef5bSAndrii Nakryiko 		m2++;
2014d5caef5bSAndrii Nakryiko 	}
2015d5caef5bSAndrii Nakryiko 	return true;
2016d5caef5bSAndrii Nakryiko }
2017d5caef5bSAndrii Nakryiko 
2018d5caef5bSAndrii Nakryiko /*
2019d5caef5bSAndrii Nakryiko  * Deduplicate primitive types, that can't reference other types, by calculating
2020d5caef5bSAndrii Nakryiko  * their type signature hash and comparing them with any possible canonical
2021d5caef5bSAndrii Nakryiko  * candidate. If no canonical candidate matches, type itself is marked as
2022d5caef5bSAndrii Nakryiko  * canonical and is added into `btf_dedup->dedup_table` as another candidate.
2023d5caef5bSAndrii Nakryiko  */
2024d5caef5bSAndrii Nakryiko static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
2025d5caef5bSAndrii Nakryiko {
2026d5caef5bSAndrii Nakryiko 	struct btf_type *t = d->btf->types[type_id];
2027*2fc3fc0bSAndrii Nakryiko 	struct hashmap_entry *hash_entry;
2028d5caef5bSAndrii Nakryiko 	struct btf_type *cand;
2029d5caef5bSAndrii Nakryiko 	/* if we don't find equivalent type, then we are canonical */
2030d5caef5bSAndrii Nakryiko 	__u32 new_id = type_id;
2031*2fc3fc0bSAndrii Nakryiko 	__u32 cand_id;
2032*2fc3fc0bSAndrii Nakryiko 	long h;
2033d5caef5bSAndrii Nakryiko 
2034d5caef5bSAndrii Nakryiko 	switch (BTF_INFO_KIND(t->info)) {
2035d5caef5bSAndrii Nakryiko 	case BTF_KIND_CONST:
2036d5caef5bSAndrii Nakryiko 	case BTF_KIND_VOLATILE:
2037d5caef5bSAndrii Nakryiko 	case BTF_KIND_RESTRICT:
2038d5caef5bSAndrii Nakryiko 	case BTF_KIND_PTR:
2039d5caef5bSAndrii Nakryiko 	case BTF_KIND_TYPEDEF:
2040d5caef5bSAndrii Nakryiko 	case BTF_KIND_ARRAY:
2041d5caef5bSAndrii Nakryiko 	case BTF_KIND_STRUCT:
2042d5caef5bSAndrii Nakryiko 	case BTF_KIND_UNION:
2043d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC:
2044d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC_PROTO:
2045189cf5a4SAndrii Nakryiko 	case BTF_KIND_VAR:
2046189cf5a4SAndrii Nakryiko 	case BTF_KIND_DATASEC:
2047d5caef5bSAndrii Nakryiko 		return 0;
2048d5caef5bSAndrii Nakryiko 
2049d5caef5bSAndrii Nakryiko 	case BTF_KIND_INT:
2050d5caef5bSAndrii Nakryiko 		h = btf_hash_int(t);
2051*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2052*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2053*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2054d5caef5bSAndrii Nakryiko 			if (btf_equal_int(t, cand)) {
2055*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2056d5caef5bSAndrii Nakryiko 				break;
2057d5caef5bSAndrii Nakryiko 			}
2058d5caef5bSAndrii Nakryiko 		}
2059d5caef5bSAndrii Nakryiko 		break;
2060d5caef5bSAndrii Nakryiko 
2061d5caef5bSAndrii Nakryiko 	case BTF_KIND_ENUM:
2062d5caef5bSAndrii Nakryiko 		h = btf_hash_enum(t);
2063*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2064*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2065*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2066d5caef5bSAndrii Nakryiko 			if (btf_equal_enum(t, cand)) {
2067*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2068d5caef5bSAndrii Nakryiko 				break;
2069d5caef5bSAndrii Nakryiko 			}
20709768095bSAndrii Nakryiko 			if (d->opts.dont_resolve_fwds)
20719768095bSAndrii Nakryiko 				continue;
20729768095bSAndrii Nakryiko 			if (btf_compat_enum(t, cand)) {
20739768095bSAndrii Nakryiko 				if (btf_is_enum_fwd(t)) {
20749768095bSAndrii Nakryiko 					/* resolve fwd to full enum */
2075*2fc3fc0bSAndrii Nakryiko 					new_id = cand_id;
20769768095bSAndrii Nakryiko 					break;
20779768095bSAndrii Nakryiko 				}
20789768095bSAndrii Nakryiko 				/* resolve canonical enum fwd to full enum */
2079*2fc3fc0bSAndrii Nakryiko 				d->map[cand_id] = type_id;
20809768095bSAndrii Nakryiko 			}
2081d5caef5bSAndrii Nakryiko 		}
2082d5caef5bSAndrii Nakryiko 		break;
2083d5caef5bSAndrii Nakryiko 
2084d5caef5bSAndrii Nakryiko 	case BTF_KIND_FWD:
2085d5caef5bSAndrii Nakryiko 		h = btf_hash_common(t);
2086*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2087*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2088*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2089d5caef5bSAndrii Nakryiko 			if (btf_equal_common(t, cand)) {
2090*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2091d5caef5bSAndrii Nakryiko 				break;
2092d5caef5bSAndrii Nakryiko 			}
2093d5caef5bSAndrii Nakryiko 		}
2094d5caef5bSAndrii Nakryiko 		break;
2095d5caef5bSAndrii Nakryiko 
2096d5caef5bSAndrii Nakryiko 	default:
2097d5caef5bSAndrii Nakryiko 		return -EINVAL;
2098d5caef5bSAndrii Nakryiko 	}
2099d5caef5bSAndrii Nakryiko 
2100d5caef5bSAndrii Nakryiko 	d->map[type_id] = new_id;
2101d5caef5bSAndrii Nakryiko 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2102d5caef5bSAndrii Nakryiko 		return -ENOMEM;
2103d5caef5bSAndrii Nakryiko 
2104d5caef5bSAndrii Nakryiko 	return 0;
2105d5caef5bSAndrii Nakryiko }
2106d5caef5bSAndrii Nakryiko 
2107d5caef5bSAndrii Nakryiko static int btf_dedup_prim_types(struct btf_dedup *d)
2108d5caef5bSAndrii Nakryiko {
2109d5caef5bSAndrii Nakryiko 	int i, err;
2110d5caef5bSAndrii Nakryiko 
2111d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
2112d5caef5bSAndrii Nakryiko 		err = btf_dedup_prim_type(d, i);
2113d5caef5bSAndrii Nakryiko 		if (err)
2114d5caef5bSAndrii Nakryiko 			return err;
2115d5caef5bSAndrii Nakryiko 	}
2116d5caef5bSAndrii Nakryiko 	return 0;
2117d5caef5bSAndrii Nakryiko }
2118d5caef5bSAndrii Nakryiko 
2119d5caef5bSAndrii Nakryiko /*
2120d5caef5bSAndrii Nakryiko  * Check whether type is already mapped into canonical one (could be to itself).
2121d5caef5bSAndrii Nakryiko  */
2122d5caef5bSAndrii Nakryiko static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
2123d5caef5bSAndrii Nakryiko {
21245aab392cSAndrii Nakryiko 	return d->map[type_id] <= BTF_MAX_NR_TYPES;
2125d5caef5bSAndrii Nakryiko }
2126d5caef5bSAndrii Nakryiko 
2127d5caef5bSAndrii Nakryiko /*
2128d5caef5bSAndrii Nakryiko  * Resolve type ID into its canonical type ID, if any; otherwise return original
2129d5caef5bSAndrii Nakryiko  * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
2130d5caef5bSAndrii Nakryiko  * STRUCT/UNION link and resolve it into canonical type ID as well.
2131d5caef5bSAndrii Nakryiko  */
2132d5caef5bSAndrii Nakryiko static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
2133d5caef5bSAndrii Nakryiko {
2134d5caef5bSAndrii Nakryiko 	while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2135d5caef5bSAndrii Nakryiko 		type_id = d->map[type_id];
2136d5caef5bSAndrii Nakryiko 	return type_id;
2137d5caef5bSAndrii Nakryiko }
2138d5caef5bSAndrii Nakryiko 
2139d5caef5bSAndrii Nakryiko /*
2140d5caef5bSAndrii Nakryiko  * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
2141d5caef5bSAndrii Nakryiko  * type ID.
2142d5caef5bSAndrii Nakryiko  */
2143d5caef5bSAndrii Nakryiko static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
2144d5caef5bSAndrii Nakryiko {
2145d5caef5bSAndrii Nakryiko 	__u32 orig_type_id = type_id;
2146d5caef5bSAndrii Nakryiko 
2147d5caef5bSAndrii Nakryiko 	if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2148d5caef5bSAndrii Nakryiko 		return type_id;
2149d5caef5bSAndrii Nakryiko 
2150d5caef5bSAndrii Nakryiko 	while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2151d5caef5bSAndrii Nakryiko 		type_id = d->map[type_id];
2152d5caef5bSAndrii Nakryiko 
2153d5caef5bSAndrii Nakryiko 	if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2154d5caef5bSAndrii Nakryiko 		return type_id;
2155d5caef5bSAndrii Nakryiko 
2156d5caef5bSAndrii Nakryiko 	return orig_type_id;
2157d5caef5bSAndrii Nakryiko }
2158d5caef5bSAndrii Nakryiko 
2159d5caef5bSAndrii Nakryiko 
2160d5caef5bSAndrii Nakryiko static inline __u16 btf_fwd_kind(struct btf_type *t)
2161d5caef5bSAndrii Nakryiko {
2162d5caef5bSAndrii Nakryiko 	return BTF_INFO_KFLAG(t->info) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
2163d5caef5bSAndrii Nakryiko }
2164d5caef5bSAndrii Nakryiko 
2165d5caef5bSAndrii Nakryiko /*
2166d5caef5bSAndrii Nakryiko  * Check equivalence of BTF type graph formed by candidate struct/union (we'll
2167d5caef5bSAndrii Nakryiko  * call it "candidate graph" in this description for brevity) to a type graph
2168d5caef5bSAndrii Nakryiko  * formed by (potential) canonical struct/union ("canonical graph" for brevity
2169d5caef5bSAndrii Nakryiko  * here, though keep in mind that not all types in canonical graph are
2170d5caef5bSAndrii Nakryiko  * necessarily canonical representatives themselves, some of them might be
2171d5caef5bSAndrii Nakryiko  * duplicates or its uniqueness might not have been established yet).
2172d5caef5bSAndrii Nakryiko  * Returns:
2173d5caef5bSAndrii Nakryiko  *  - >0, if type graphs are equivalent;
2174d5caef5bSAndrii Nakryiko  *  -  0, if not equivalent;
2175d5caef5bSAndrii Nakryiko  *  - <0, on error.
2176d5caef5bSAndrii Nakryiko  *
2177d5caef5bSAndrii Nakryiko  * Algorithm performs side-by-side DFS traversal of both type graphs and checks
2178d5caef5bSAndrii Nakryiko  * equivalence of BTF types at each step. If at any point BTF types in candidate
2179d5caef5bSAndrii Nakryiko  * and canonical graphs are not compatible structurally, whole graphs are
2180d5caef5bSAndrii Nakryiko  * incompatible. If types are structurally equivalent (i.e., all information
2181d5caef5bSAndrii Nakryiko  * except referenced type IDs is exactly the same), a mapping from `canon_id` to
2182d5caef5bSAndrii Nakryiko  * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
2183d5caef5bSAndrii Nakryiko  * If a type references other types, then those referenced types are checked
2184d5caef5bSAndrii Nakryiko  * for equivalence recursively.
2185d5caef5bSAndrii Nakryiko  *
2186d5caef5bSAndrii Nakryiko  * During DFS traversal, if we find that for current `canon_id` type we
2187d5caef5bSAndrii Nakryiko  * already have some mapping in hypothetical map, we check for two possible
2188d5caef5bSAndrii Nakryiko  * situations:
2189d5caef5bSAndrii Nakryiko  *   - `canon_id` is mapped to exactly the same type as `cand_id`. This will
2190d5caef5bSAndrii Nakryiko  *     happen when type graphs have cycles. In this case we assume those two
2191d5caef5bSAndrii Nakryiko  *     types are equivalent.
2192d5caef5bSAndrii Nakryiko  *   - `canon_id` is mapped to different type. This is contradiction in our
2193d5caef5bSAndrii Nakryiko  *     hypothetical mapping, because same graph in canonical graph corresponds
2194d5caef5bSAndrii Nakryiko  *     to two different types in candidate graph, which for equivalent type
2195d5caef5bSAndrii Nakryiko  *     graphs shouldn't happen. This condition terminates equivalence check
2196d5caef5bSAndrii Nakryiko  *     with negative result.
2197d5caef5bSAndrii Nakryiko  *
2198d5caef5bSAndrii Nakryiko  * If type graphs traversal exhausts types to check and find no contradiction,
2199d5caef5bSAndrii Nakryiko  * then type graphs are equivalent.
2200d5caef5bSAndrii Nakryiko  *
2201d5caef5bSAndrii Nakryiko  * When checking types for equivalence, there is one special case: FWD types.
2202d5caef5bSAndrii Nakryiko  * If FWD type resolution is allowed and one of the types (either from canonical
2203d5caef5bSAndrii Nakryiko  * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
2204d5caef5bSAndrii Nakryiko  * flag) and their names match, hypothetical mapping is updated to point from
2205d5caef5bSAndrii Nakryiko  * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
2206d5caef5bSAndrii Nakryiko  * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
2207d5caef5bSAndrii Nakryiko  *
2208d5caef5bSAndrii Nakryiko  * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
2209d5caef5bSAndrii Nakryiko  * if there are two exactly named (or anonymous) structs/unions that are
2210d5caef5bSAndrii Nakryiko  * compatible structurally, one of which has FWD field, while other is concrete
2211d5caef5bSAndrii Nakryiko  * STRUCT/UNION, but according to C sources they are different structs/unions
2212d5caef5bSAndrii Nakryiko  * that are referencing different types with the same name. This is extremely
2213d5caef5bSAndrii Nakryiko  * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
2214d5caef5bSAndrii Nakryiko  * this logic is causing problems.
2215d5caef5bSAndrii Nakryiko  *
2216d5caef5bSAndrii Nakryiko  * Doing FWD resolution means that both candidate and/or canonical graphs can
2217d5caef5bSAndrii Nakryiko  * consists of portions of the graph that come from multiple compilation units.
2218d5caef5bSAndrii Nakryiko  * This is due to the fact that types within single compilation unit are always
2219d5caef5bSAndrii Nakryiko  * deduplicated and FWDs are already resolved, if referenced struct/union
2220d5caef5bSAndrii Nakryiko  * definiton is available. So, if we had unresolved FWD and found corresponding
2221d5caef5bSAndrii Nakryiko  * STRUCT/UNION, they will be from different compilation units. This
2222d5caef5bSAndrii Nakryiko  * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
2223d5caef5bSAndrii Nakryiko  * type graph will likely have at least two different BTF types that describe
2224d5caef5bSAndrii Nakryiko  * same type (e.g., most probably there will be two different BTF types for the
2225d5caef5bSAndrii Nakryiko  * same 'int' primitive type) and could even have "overlapping" parts of type
2226d5caef5bSAndrii Nakryiko  * graph that describe same subset of types.
2227d5caef5bSAndrii Nakryiko  *
2228d5caef5bSAndrii Nakryiko  * This in turn means that our assumption that each type in canonical graph
2229d5caef5bSAndrii Nakryiko  * must correspond to exactly one type in candidate graph might not hold
2230d5caef5bSAndrii Nakryiko  * anymore and will make it harder to detect contradictions using hypothetical
2231d5caef5bSAndrii Nakryiko  * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
2232d5caef5bSAndrii Nakryiko  * resolution only in canonical graph. FWDs in candidate graphs are never
2233d5caef5bSAndrii Nakryiko  * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
2234d5caef5bSAndrii Nakryiko  * that can occur:
2235d5caef5bSAndrii Nakryiko  *   - Both types in canonical and candidate graphs are FWDs. If they are
2236d5caef5bSAndrii Nakryiko  *     structurally equivalent, then they can either be both resolved to the
2237d5caef5bSAndrii Nakryiko  *     same STRUCT/UNION or not resolved at all. In both cases they are
2238d5caef5bSAndrii Nakryiko  *     equivalent and there is no need to resolve FWD on candidate side.
2239d5caef5bSAndrii Nakryiko  *   - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
2240d5caef5bSAndrii Nakryiko  *     so nothing to resolve as well, algorithm will check equivalence anyway.
2241d5caef5bSAndrii Nakryiko  *   - Type in canonical graph is FWD, while type in candidate is concrete
2242d5caef5bSAndrii Nakryiko  *     STRUCT/UNION. In this case candidate graph comes from single compilation
2243d5caef5bSAndrii Nakryiko  *     unit, so there is exactly one BTF type for each unique C type. After
2244d5caef5bSAndrii Nakryiko  *     resolving FWD into STRUCT/UNION, there might be more than one BTF type
2245d5caef5bSAndrii Nakryiko  *     in canonical graph mapping to single BTF type in candidate graph, but
2246d5caef5bSAndrii Nakryiko  *     because hypothetical mapping maps from canonical to candidate types, it's
2247d5caef5bSAndrii Nakryiko  *     alright, and we still maintain the property of having single `canon_id`
2248d5caef5bSAndrii Nakryiko  *     mapping to single `cand_id` (there could be two different `canon_id`
2249d5caef5bSAndrii Nakryiko  *     mapped to the same `cand_id`, but it's not contradictory).
2250d5caef5bSAndrii Nakryiko  *   - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
2251d5caef5bSAndrii Nakryiko  *     graph is FWD. In this case we are just going to check compatibility of
2252d5caef5bSAndrii Nakryiko  *     STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
2253d5caef5bSAndrii Nakryiko  *     assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
2254d5caef5bSAndrii Nakryiko  *     a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
2255d5caef5bSAndrii Nakryiko  *     turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
2256d5caef5bSAndrii Nakryiko  *     canonical graph.
2257d5caef5bSAndrii Nakryiko  */
2258d5caef5bSAndrii Nakryiko static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
2259d5caef5bSAndrii Nakryiko 			      __u32 canon_id)
2260d5caef5bSAndrii Nakryiko {
2261d5caef5bSAndrii Nakryiko 	struct btf_type *cand_type;
2262d5caef5bSAndrii Nakryiko 	struct btf_type *canon_type;
2263d5caef5bSAndrii Nakryiko 	__u32 hypot_type_id;
2264d5caef5bSAndrii Nakryiko 	__u16 cand_kind;
2265d5caef5bSAndrii Nakryiko 	__u16 canon_kind;
2266d5caef5bSAndrii Nakryiko 	int i, eq;
2267d5caef5bSAndrii Nakryiko 
2268d5caef5bSAndrii Nakryiko 	/* if both resolve to the same canonical, they must be equivalent */
2269d5caef5bSAndrii Nakryiko 	if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
2270d5caef5bSAndrii Nakryiko 		return 1;
2271d5caef5bSAndrii Nakryiko 
2272d5caef5bSAndrii Nakryiko 	canon_id = resolve_fwd_id(d, canon_id);
2273d5caef5bSAndrii Nakryiko 
2274d5caef5bSAndrii Nakryiko 	hypot_type_id = d->hypot_map[canon_id];
22755aab392cSAndrii Nakryiko 	if (hypot_type_id <= BTF_MAX_NR_TYPES)
2276d5caef5bSAndrii Nakryiko 		return hypot_type_id == cand_id;
2277d5caef5bSAndrii Nakryiko 
2278d5caef5bSAndrii Nakryiko 	if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
2279d5caef5bSAndrii Nakryiko 		return -ENOMEM;
2280d5caef5bSAndrii Nakryiko 
2281d5caef5bSAndrii Nakryiko 	cand_type = d->btf->types[cand_id];
2282d5caef5bSAndrii Nakryiko 	canon_type = d->btf->types[canon_id];
2283d5caef5bSAndrii Nakryiko 	cand_kind = BTF_INFO_KIND(cand_type->info);
2284d5caef5bSAndrii Nakryiko 	canon_kind = BTF_INFO_KIND(canon_type->info);
2285d5caef5bSAndrii Nakryiko 
2286d5caef5bSAndrii Nakryiko 	if (cand_type->name_off != canon_type->name_off)
2287d5caef5bSAndrii Nakryiko 		return 0;
2288d5caef5bSAndrii Nakryiko 
2289d5caef5bSAndrii Nakryiko 	/* FWD <--> STRUCT/UNION equivalence check, if enabled */
2290d5caef5bSAndrii Nakryiko 	if (!d->opts.dont_resolve_fwds
2291d5caef5bSAndrii Nakryiko 	    && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
2292d5caef5bSAndrii Nakryiko 	    && cand_kind != canon_kind) {
2293d5caef5bSAndrii Nakryiko 		__u16 real_kind;
2294d5caef5bSAndrii Nakryiko 		__u16 fwd_kind;
2295d5caef5bSAndrii Nakryiko 
2296d5caef5bSAndrii Nakryiko 		if (cand_kind == BTF_KIND_FWD) {
2297d5caef5bSAndrii Nakryiko 			real_kind = canon_kind;
2298d5caef5bSAndrii Nakryiko 			fwd_kind = btf_fwd_kind(cand_type);
2299d5caef5bSAndrii Nakryiko 		} else {
2300d5caef5bSAndrii Nakryiko 			real_kind = cand_kind;
2301d5caef5bSAndrii Nakryiko 			fwd_kind = btf_fwd_kind(canon_type);
2302d5caef5bSAndrii Nakryiko 		}
2303d5caef5bSAndrii Nakryiko 		return fwd_kind == real_kind;
2304d5caef5bSAndrii Nakryiko 	}
2305d5caef5bSAndrii Nakryiko 
23069ec71c1cSAndrii Nakryiko 	if (cand_kind != canon_kind)
23079ec71c1cSAndrii Nakryiko 		return 0;
23089ec71c1cSAndrii Nakryiko 
2309d5caef5bSAndrii Nakryiko 	switch (cand_kind) {
2310d5caef5bSAndrii Nakryiko 	case BTF_KIND_INT:
2311d5caef5bSAndrii Nakryiko 		return btf_equal_int(cand_type, canon_type);
2312d5caef5bSAndrii Nakryiko 
2313d5caef5bSAndrii Nakryiko 	case BTF_KIND_ENUM:
23149768095bSAndrii Nakryiko 		if (d->opts.dont_resolve_fwds)
2315d5caef5bSAndrii Nakryiko 			return btf_equal_enum(cand_type, canon_type);
23169768095bSAndrii Nakryiko 		else
23179768095bSAndrii Nakryiko 			return btf_compat_enum(cand_type, canon_type);
2318d5caef5bSAndrii Nakryiko 
2319d5caef5bSAndrii Nakryiko 	case BTF_KIND_FWD:
2320d5caef5bSAndrii Nakryiko 		return btf_equal_common(cand_type, canon_type);
2321d5caef5bSAndrii Nakryiko 
2322d5caef5bSAndrii Nakryiko 	case BTF_KIND_CONST:
2323d5caef5bSAndrii Nakryiko 	case BTF_KIND_VOLATILE:
2324d5caef5bSAndrii Nakryiko 	case BTF_KIND_RESTRICT:
2325d5caef5bSAndrii Nakryiko 	case BTF_KIND_PTR:
2326d5caef5bSAndrii Nakryiko 	case BTF_KIND_TYPEDEF:
2327d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC:
23289768095bSAndrii Nakryiko 		if (cand_type->info != canon_type->info)
23299768095bSAndrii Nakryiko 			return 0;
2330d5caef5bSAndrii Nakryiko 		return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2331d5caef5bSAndrii Nakryiko 
2332d5caef5bSAndrii Nakryiko 	case BTF_KIND_ARRAY: {
2333d5caef5bSAndrii Nakryiko 		struct btf_array *cand_arr, *canon_arr;
2334d5caef5bSAndrii Nakryiko 
2335d5caef5bSAndrii Nakryiko 		if (!btf_compat_array(cand_type, canon_type))
2336d5caef5bSAndrii Nakryiko 			return 0;
2337d5caef5bSAndrii Nakryiko 		cand_arr = (struct btf_array *)(cand_type + 1);
2338d5caef5bSAndrii Nakryiko 		canon_arr = (struct btf_array *)(canon_type + 1);
2339d5caef5bSAndrii Nakryiko 		eq = btf_dedup_is_equiv(d,
2340d5caef5bSAndrii Nakryiko 			cand_arr->index_type, canon_arr->index_type);
2341d5caef5bSAndrii Nakryiko 		if (eq <= 0)
2342d5caef5bSAndrii Nakryiko 			return eq;
2343d5caef5bSAndrii Nakryiko 		return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
2344d5caef5bSAndrii Nakryiko 	}
2345d5caef5bSAndrii Nakryiko 
2346d5caef5bSAndrii Nakryiko 	case BTF_KIND_STRUCT:
2347d5caef5bSAndrii Nakryiko 	case BTF_KIND_UNION: {
2348d5caef5bSAndrii Nakryiko 		struct btf_member *cand_m, *canon_m;
2349d5caef5bSAndrii Nakryiko 		__u16 vlen;
2350d5caef5bSAndrii Nakryiko 
235191097fbeSAndrii Nakryiko 		if (!btf_shallow_equal_struct(cand_type, canon_type))
2352d5caef5bSAndrii Nakryiko 			return 0;
2353d5caef5bSAndrii Nakryiko 		vlen = BTF_INFO_VLEN(cand_type->info);
2354d5caef5bSAndrii Nakryiko 		cand_m = (struct btf_member *)(cand_type + 1);
2355d5caef5bSAndrii Nakryiko 		canon_m = (struct btf_member *)(canon_type + 1);
2356d5caef5bSAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2357d5caef5bSAndrii Nakryiko 			eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
2358d5caef5bSAndrii Nakryiko 			if (eq <= 0)
2359d5caef5bSAndrii Nakryiko 				return eq;
2360d5caef5bSAndrii Nakryiko 			cand_m++;
2361d5caef5bSAndrii Nakryiko 			canon_m++;
2362d5caef5bSAndrii Nakryiko 		}
2363d5caef5bSAndrii Nakryiko 
2364d5caef5bSAndrii Nakryiko 		return 1;
2365d5caef5bSAndrii Nakryiko 	}
2366d5caef5bSAndrii Nakryiko 
2367d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC_PROTO: {
2368d5caef5bSAndrii Nakryiko 		struct btf_param *cand_p, *canon_p;
2369d5caef5bSAndrii Nakryiko 		__u16 vlen;
2370d5caef5bSAndrii Nakryiko 
2371d5caef5bSAndrii Nakryiko 		if (!btf_compat_fnproto(cand_type, canon_type))
2372d5caef5bSAndrii Nakryiko 			return 0;
2373d5caef5bSAndrii Nakryiko 		eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2374d5caef5bSAndrii Nakryiko 		if (eq <= 0)
2375d5caef5bSAndrii Nakryiko 			return eq;
2376d5caef5bSAndrii Nakryiko 		vlen = BTF_INFO_VLEN(cand_type->info);
2377d5caef5bSAndrii Nakryiko 		cand_p = (struct btf_param *)(cand_type + 1);
2378d5caef5bSAndrii Nakryiko 		canon_p = (struct btf_param *)(canon_type + 1);
2379d5caef5bSAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2380d5caef5bSAndrii Nakryiko 			eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
2381d5caef5bSAndrii Nakryiko 			if (eq <= 0)
2382d5caef5bSAndrii Nakryiko 				return eq;
2383d5caef5bSAndrii Nakryiko 			cand_p++;
2384d5caef5bSAndrii Nakryiko 			canon_p++;
2385d5caef5bSAndrii Nakryiko 		}
2386d5caef5bSAndrii Nakryiko 		return 1;
2387d5caef5bSAndrii Nakryiko 	}
2388d5caef5bSAndrii Nakryiko 
2389d5caef5bSAndrii Nakryiko 	default:
2390d5caef5bSAndrii Nakryiko 		return -EINVAL;
2391d5caef5bSAndrii Nakryiko 	}
2392d5caef5bSAndrii Nakryiko 	return 0;
2393d5caef5bSAndrii Nakryiko }
2394d5caef5bSAndrii Nakryiko 
2395d5caef5bSAndrii Nakryiko /*
2396d5caef5bSAndrii Nakryiko  * Use hypothetical mapping, produced by successful type graph equivalence
2397d5caef5bSAndrii Nakryiko  * check, to augment existing struct/union canonical mapping, where possible.
2398d5caef5bSAndrii Nakryiko  *
2399d5caef5bSAndrii Nakryiko  * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
2400d5caef5bSAndrii Nakryiko  * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
2401d5caef5bSAndrii Nakryiko  * it doesn't matter if FWD type was part of canonical graph or candidate one,
2402d5caef5bSAndrii Nakryiko  * we are recording the mapping anyway. As opposed to carefulness required
2403d5caef5bSAndrii Nakryiko  * for struct/union correspondence mapping (described below), for FWD resolution
2404d5caef5bSAndrii Nakryiko  * it's not important, as by the time that FWD type (reference type) will be
2405d5caef5bSAndrii Nakryiko  * deduplicated all structs/unions will be deduped already anyway.
2406d5caef5bSAndrii Nakryiko  *
2407d5caef5bSAndrii Nakryiko  * Recording STRUCT/UNION mapping is purely a performance optimization and is
2408d5caef5bSAndrii Nakryiko  * not required for correctness. It needs to be done carefully to ensure that
2409d5caef5bSAndrii Nakryiko  * struct/union from candidate's type graph is not mapped into corresponding
2410d5caef5bSAndrii Nakryiko  * struct/union from canonical type graph that itself hasn't been resolved into
2411d5caef5bSAndrii Nakryiko  * canonical representative. The only guarantee we have is that canonical
2412d5caef5bSAndrii Nakryiko  * struct/union was determined as canonical and that won't change. But any
2413d5caef5bSAndrii Nakryiko  * types referenced through that struct/union fields could have been not yet
2414d5caef5bSAndrii Nakryiko  * resolved, so in case like that it's too early to establish any kind of
2415d5caef5bSAndrii Nakryiko  * correspondence between structs/unions.
2416d5caef5bSAndrii Nakryiko  *
2417d5caef5bSAndrii Nakryiko  * No canonical correspondence is derived for primitive types (they are already
2418d5caef5bSAndrii Nakryiko  * deduplicated completely already anyway) or reference types (they rely on
2419d5caef5bSAndrii Nakryiko  * stability of struct/union canonical relationship for equivalence checks).
2420d5caef5bSAndrii Nakryiko  */
2421d5caef5bSAndrii Nakryiko static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
2422d5caef5bSAndrii Nakryiko {
2423d5caef5bSAndrii Nakryiko 	__u32 cand_type_id, targ_type_id;
2424d5caef5bSAndrii Nakryiko 	__u16 t_kind, c_kind;
2425d5caef5bSAndrii Nakryiko 	__u32 t_id, c_id;
2426d5caef5bSAndrii Nakryiko 	int i;
2427d5caef5bSAndrii Nakryiko 
2428d5caef5bSAndrii Nakryiko 	for (i = 0; i < d->hypot_cnt; i++) {
2429d5caef5bSAndrii Nakryiko 		cand_type_id = d->hypot_list[i];
2430d5caef5bSAndrii Nakryiko 		targ_type_id = d->hypot_map[cand_type_id];
2431d5caef5bSAndrii Nakryiko 		t_id = resolve_type_id(d, targ_type_id);
2432d5caef5bSAndrii Nakryiko 		c_id = resolve_type_id(d, cand_type_id);
2433d5caef5bSAndrii Nakryiko 		t_kind = BTF_INFO_KIND(d->btf->types[t_id]->info);
2434d5caef5bSAndrii Nakryiko 		c_kind = BTF_INFO_KIND(d->btf->types[c_id]->info);
2435d5caef5bSAndrii Nakryiko 		/*
2436d5caef5bSAndrii Nakryiko 		 * Resolve FWD into STRUCT/UNION.
2437d5caef5bSAndrii Nakryiko 		 * It's ok to resolve FWD into STRUCT/UNION that's not yet
2438d5caef5bSAndrii Nakryiko 		 * mapped to canonical representative (as opposed to
2439d5caef5bSAndrii Nakryiko 		 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
2440d5caef5bSAndrii Nakryiko 		 * eventually that struct is going to be mapped and all resolved
2441d5caef5bSAndrii Nakryiko 		 * FWDs will automatically resolve to correct canonical
2442d5caef5bSAndrii Nakryiko 		 * representative. This will happen before ref type deduping,
2443d5caef5bSAndrii Nakryiko 		 * which critically depends on stability of these mapping. This
2444d5caef5bSAndrii Nakryiko 		 * stability is not a requirement for STRUCT/UNION equivalence
2445d5caef5bSAndrii Nakryiko 		 * checks, though.
2446d5caef5bSAndrii Nakryiko 		 */
2447d5caef5bSAndrii Nakryiko 		if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
2448d5caef5bSAndrii Nakryiko 			d->map[c_id] = t_id;
2449d5caef5bSAndrii Nakryiko 		else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
2450d5caef5bSAndrii Nakryiko 			d->map[t_id] = c_id;
2451d5caef5bSAndrii Nakryiko 
2452d5caef5bSAndrii Nakryiko 		if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
2453d5caef5bSAndrii Nakryiko 		    c_kind != BTF_KIND_FWD &&
2454d5caef5bSAndrii Nakryiko 		    is_type_mapped(d, c_id) &&
2455d5caef5bSAndrii Nakryiko 		    !is_type_mapped(d, t_id)) {
2456d5caef5bSAndrii Nakryiko 			/*
2457d5caef5bSAndrii Nakryiko 			 * as a perf optimization, we can map struct/union
2458d5caef5bSAndrii Nakryiko 			 * that's part of type graph we just verified for
2459d5caef5bSAndrii Nakryiko 			 * equivalence. We can do that for struct/union that has
2460d5caef5bSAndrii Nakryiko 			 * canonical representative only, though.
2461d5caef5bSAndrii Nakryiko 			 */
2462d5caef5bSAndrii Nakryiko 			d->map[t_id] = c_id;
2463d5caef5bSAndrii Nakryiko 		}
2464d5caef5bSAndrii Nakryiko 	}
2465d5caef5bSAndrii Nakryiko }
2466d5caef5bSAndrii Nakryiko 
2467d5caef5bSAndrii Nakryiko /*
2468d5caef5bSAndrii Nakryiko  * Deduplicate struct/union types.
2469d5caef5bSAndrii Nakryiko  *
2470d5caef5bSAndrii Nakryiko  * For each struct/union type its type signature hash is calculated, taking
2471d5caef5bSAndrii Nakryiko  * into account type's name, size, number, order and names of fields, but
2472d5caef5bSAndrii Nakryiko  * ignoring type ID's referenced from fields, because they might not be deduped
2473d5caef5bSAndrii Nakryiko  * completely until after reference types deduplication phase. This type hash
2474d5caef5bSAndrii Nakryiko  * is used to iterate over all potential canonical types, sharing same hash.
2475d5caef5bSAndrii Nakryiko  * For each canonical candidate we check whether type graphs that they form
2476d5caef5bSAndrii Nakryiko  * (through referenced types in fields and so on) are equivalent using algorithm
2477d5caef5bSAndrii Nakryiko  * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
2478d5caef5bSAndrii Nakryiko  * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
2479d5caef5bSAndrii Nakryiko  * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
2480d5caef5bSAndrii Nakryiko  * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
2481d5caef5bSAndrii Nakryiko  * potentially map other structs/unions to their canonical representatives,
2482d5caef5bSAndrii Nakryiko  * if such relationship hasn't yet been established. This speeds up algorithm
2483d5caef5bSAndrii Nakryiko  * by eliminating some of the duplicate work.
2484d5caef5bSAndrii Nakryiko  *
2485d5caef5bSAndrii Nakryiko  * If no matching canonical representative was found, struct/union is marked
2486d5caef5bSAndrii Nakryiko  * as canonical for itself and is added into btf_dedup->dedup_table hash map
2487d5caef5bSAndrii Nakryiko  * for further look ups.
2488d5caef5bSAndrii Nakryiko  */
2489d5caef5bSAndrii Nakryiko static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
2490d5caef5bSAndrii Nakryiko {
249191097fbeSAndrii Nakryiko 	struct btf_type *cand_type, *t;
2492*2fc3fc0bSAndrii Nakryiko 	struct hashmap_entry *hash_entry;
2493d5caef5bSAndrii Nakryiko 	/* if we don't find equivalent type, then we are canonical */
2494d5caef5bSAndrii Nakryiko 	__u32 new_id = type_id;
2495d5caef5bSAndrii Nakryiko 	__u16 kind;
2496*2fc3fc0bSAndrii Nakryiko 	long h;
2497d5caef5bSAndrii Nakryiko 
2498d5caef5bSAndrii Nakryiko 	/* already deduped or is in process of deduping (loop detected) */
24995aab392cSAndrii Nakryiko 	if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2500d5caef5bSAndrii Nakryiko 		return 0;
2501d5caef5bSAndrii Nakryiko 
2502d5caef5bSAndrii Nakryiko 	t = d->btf->types[type_id];
2503d5caef5bSAndrii Nakryiko 	kind = BTF_INFO_KIND(t->info);
2504d5caef5bSAndrii Nakryiko 
2505d5caef5bSAndrii Nakryiko 	if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
2506d5caef5bSAndrii Nakryiko 		return 0;
2507d5caef5bSAndrii Nakryiko 
2508d5caef5bSAndrii Nakryiko 	h = btf_hash_struct(t);
2509*2fc3fc0bSAndrii Nakryiko 	for_each_dedup_cand(d, hash_entry, h) {
2510*2fc3fc0bSAndrii Nakryiko 		__u32 cand_id = (__u32)(long)hash_entry->value;
2511d5caef5bSAndrii Nakryiko 		int eq;
2512d5caef5bSAndrii Nakryiko 
251391097fbeSAndrii Nakryiko 		/*
251491097fbeSAndrii Nakryiko 		 * Even though btf_dedup_is_equiv() checks for
251591097fbeSAndrii Nakryiko 		 * btf_shallow_equal_struct() internally when checking two
251691097fbeSAndrii Nakryiko 		 * structs (unions) for equivalence, we need to guard here
251791097fbeSAndrii Nakryiko 		 * from picking matching FWD type as a dedup candidate.
251891097fbeSAndrii Nakryiko 		 * This can happen due to hash collision. In such case just
251991097fbeSAndrii Nakryiko 		 * relying on btf_dedup_is_equiv() would lead to potentially
252091097fbeSAndrii Nakryiko 		 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
252191097fbeSAndrii Nakryiko 		 * FWD and compatible STRUCT/UNION are considered equivalent.
252291097fbeSAndrii Nakryiko 		 */
2523*2fc3fc0bSAndrii Nakryiko 		cand_type = d->btf->types[cand_id];
252491097fbeSAndrii Nakryiko 		if (!btf_shallow_equal_struct(t, cand_type))
252591097fbeSAndrii Nakryiko 			continue;
252691097fbeSAndrii Nakryiko 
2527d5caef5bSAndrii Nakryiko 		btf_dedup_clear_hypot_map(d);
2528*2fc3fc0bSAndrii Nakryiko 		eq = btf_dedup_is_equiv(d, type_id, cand_id);
2529d5caef5bSAndrii Nakryiko 		if (eq < 0)
2530d5caef5bSAndrii Nakryiko 			return eq;
2531d5caef5bSAndrii Nakryiko 		if (!eq)
2532d5caef5bSAndrii Nakryiko 			continue;
2533*2fc3fc0bSAndrii Nakryiko 		new_id = cand_id;
2534d5caef5bSAndrii Nakryiko 		btf_dedup_merge_hypot_map(d);
2535d5caef5bSAndrii Nakryiko 		break;
2536d5caef5bSAndrii Nakryiko 	}
2537d5caef5bSAndrii Nakryiko 
2538d5caef5bSAndrii Nakryiko 	d->map[type_id] = new_id;
2539d5caef5bSAndrii Nakryiko 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2540d5caef5bSAndrii Nakryiko 		return -ENOMEM;
2541d5caef5bSAndrii Nakryiko 
2542d5caef5bSAndrii Nakryiko 	return 0;
2543d5caef5bSAndrii Nakryiko }
2544d5caef5bSAndrii Nakryiko 
2545d5caef5bSAndrii Nakryiko static int btf_dedup_struct_types(struct btf_dedup *d)
2546d5caef5bSAndrii Nakryiko {
2547d5caef5bSAndrii Nakryiko 	int i, err;
2548d5caef5bSAndrii Nakryiko 
2549d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
2550d5caef5bSAndrii Nakryiko 		err = btf_dedup_struct_type(d, i);
2551d5caef5bSAndrii Nakryiko 		if (err)
2552d5caef5bSAndrii Nakryiko 			return err;
2553d5caef5bSAndrii Nakryiko 	}
2554d5caef5bSAndrii Nakryiko 	return 0;
2555d5caef5bSAndrii Nakryiko }
2556d5caef5bSAndrii Nakryiko 
2557d5caef5bSAndrii Nakryiko /*
2558d5caef5bSAndrii Nakryiko  * Deduplicate reference type.
2559d5caef5bSAndrii Nakryiko  *
2560d5caef5bSAndrii Nakryiko  * Once all primitive and struct/union types got deduplicated, we can easily
2561d5caef5bSAndrii Nakryiko  * deduplicate all other (reference) BTF types. This is done in two steps:
2562d5caef5bSAndrii Nakryiko  *
2563d5caef5bSAndrii Nakryiko  * 1. Resolve all referenced type IDs into their canonical type IDs. This
2564d5caef5bSAndrii Nakryiko  * resolution can be done either immediately for primitive or struct/union types
2565d5caef5bSAndrii Nakryiko  * (because they were deduped in previous two phases) or recursively for
2566d5caef5bSAndrii Nakryiko  * reference types. Recursion will always terminate at either primitive or
2567d5caef5bSAndrii Nakryiko  * struct/union type, at which point we can "unwind" chain of reference types
2568d5caef5bSAndrii Nakryiko  * one by one. There is no danger of encountering cycles because in C type
2569d5caef5bSAndrii Nakryiko  * system the only way to form type cycle is through struct/union, so any chain
2570d5caef5bSAndrii Nakryiko  * of reference types, even those taking part in a type cycle, will inevitably
2571d5caef5bSAndrii Nakryiko  * reach struct/union at some point.
2572d5caef5bSAndrii Nakryiko  *
2573d5caef5bSAndrii Nakryiko  * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
2574d5caef5bSAndrii Nakryiko  * becomes "stable", in the sense that no further deduplication will cause
2575d5caef5bSAndrii Nakryiko  * any changes to it. With that, it's now possible to calculate type's signature
2576d5caef5bSAndrii Nakryiko  * hash (this time taking into account referenced type IDs) and loop over all
2577d5caef5bSAndrii Nakryiko  * potential canonical representatives. If no match was found, current type
2578d5caef5bSAndrii Nakryiko  * will become canonical representative of itself and will be added into
2579d5caef5bSAndrii Nakryiko  * btf_dedup->dedup_table as another possible canonical representative.
2580d5caef5bSAndrii Nakryiko  */
2581d5caef5bSAndrii Nakryiko static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
2582d5caef5bSAndrii Nakryiko {
2583*2fc3fc0bSAndrii Nakryiko 	struct hashmap_entry *hash_entry;
2584*2fc3fc0bSAndrii Nakryiko 	__u32 new_id = type_id, cand_id;
2585d5caef5bSAndrii Nakryiko 	struct btf_type *t, *cand;
2586d5caef5bSAndrii Nakryiko 	/* if we don't find equivalent type, then we are representative type */
25873d8669e6SDan Carpenter 	int ref_type_id;
2588*2fc3fc0bSAndrii Nakryiko 	long h;
2589d5caef5bSAndrii Nakryiko 
2590d5caef5bSAndrii Nakryiko 	if (d->map[type_id] == BTF_IN_PROGRESS_ID)
2591d5caef5bSAndrii Nakryiko 		return -ELOOP;
25925aab392cSAndrii Nakryiko 	if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2593d5caef5bSAndrii Nakryiko 		return resolve_type_id(d, type_id);
2594d5caef5bSAndrii Nakryiko 
2595d5caef5bSAndrii Nakryiko 	t = d->btf->types[type_id];
2596d5caef5bSAndrii Nakryiko 	d->map[type_id] = BTF_IN_PROGRESS_ID;
2597d5caef5bSAndrii Nakryiko 
2598d5caef5bSAndrii Nakryiko 	switch (BTF_INFO_KIND(t->info)) {
2599d5caef5bSAndrii Nakryiko 	case BTF_KIND_CONST:
2600d5caef5bSAndrii Nakryiko 	case BTF_KIND_VOLATILE:
2601d5caef5bSAndrii Nakryiko 	case BTF_KIND_RESTRICT:
2602d5caef5bSAndrii Nakryiko 	case BTF_KIND_PTR:
2603d5caef5bSAndrii Nakryiko 	case BTF_KIND_TYPEDEF:
2604d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC:
2605d5caef5bSAndrii Nakryiko 		ref_type_id = btf_dedup_ref_type(d, t->type);
2606d5caef5bSAndrii Nakryiko 		if (ref_type_id < 0)
2607d5caef5bSAndrii Nakryiko 			return ref_type_id;
2608d5caef5bSAndrii Nakryiko 		t->type = ref_type_id;
2609d5caef5bSAndrii Nakryiko 
2610d5caef5bSAndrii Nakryiko 		h = btf_hash_common(t);
2611*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2612*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2613*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2614d5caef5bSAndrii Nakryiko 			if (btf_equal_common(t, cand)) {
2615*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2616d5caef5bSAndrii Nakryiko 				break;
2617d5caef5bSAndrii Nakryiko 			}
2618d5caef5bSAndrii Nakryiko 		}
2619d5caef5bSAndrii Nakryiko 		break;
2620d5caef5bSAndrii Nakryiko 
2621d5caef5bSAndrii Nakryiko 	case BTF_KIND_ARRAY: {
2622d5caef5bSAndrii Nakryiko 		struct btf_array *info = (struct btf_array *)(t + 1);
2623d5caef5bSAndrii Nakryiko 
2624d5caef5bSAndrii Nakryiko 		ref_type_id = btf_dedup_ref_type(d, info->type);
2625d5caef5bSAndrii Nakryiko 		if (ref_type_id < 0)
2626d5caef5bSAndrii Nakryiko 			return ref_type_id;
2627d5caef5bSAndrii Nakryiko 		info->type = ref_type_id;
2628d5caef5bSAndrii Nakryiko 
2629d5caef5bSAndrii Nakryiko 		ref_type_id = btf_dedup_ref_type(d, info->index_type);
2630d5caef5bSAndrii Nakryiko 		if (ref_type_id < 0)
2631d5caef5bSAndrii Nakryiko 			return ref_type_id;
2632d5caef5bSAndrii Nakryiko 		info->index_type = ref_type_id;
2633d5caef5bSAndrii Nakryiko 
2634d5caef5bSAndrii Nakryiko 		h = btf_hash_array(t);
2635*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2636*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2637*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2638d5caef5bSAndrii Nakryiko 			if (btf_equal_array(t, cand)) {
2639*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2640d5caef5bSAndrii Nakryiko 				break;
2641d5caef5bSAndrii Nakryiko 			}
2642d5caef5bSAndrii Nakryiko 		}
2643d5caef5bSAndrii Nakryiko 		break;
2644d5caef5bSAndrii Nakryiko 	}
2645d5caef5bSAndrii Nakryiko 
2646d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC_PROTO: {
2647d5caef5bSAndrii Nakryiko 		struct btf_param *param;
2648d5caef5bSAndrii Nakryiko 		__u16 vlen;
2649d5caef5bSAndrii Nakryiko 		int i;
2650d5caef5bSAndrii Nakryiko 
2651d5caef5bSAndrii Nakryiko 		ref_type_id = btf_dedup_ref_type(d, t->type);
2652d5caef5bSAndrii Nakryiko 		if (ref_type_id < 0)
2653d5caef5bSAndrii Nakryiko 			return ref_type_id;
2654d5caef5bSAndrii Nakryiko 		t->type = ref_type_id;
2655d5caef5bSAndrii Nakryiko 
2656d5caef5bSAndrii Nakryiko 		vlen = BTF_INFO_VLEN(t->info);
2657d5caef5bSAndrii Nakryiko 		param = (struct btf_param *)(t + 1);
2658d5caef5bSAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2659d5caef5bSAndrii Nakryiko 			ref_type_id = btf_dedup_ref_type(d, param->type);
2660d5caef5bSAndrii Nakryiko 			if (ref_type_id < 0)
2661d5caef5bSAndrii Nakryiko 				return ref_type_id;
2662d5caef5bSAndrii Nakryiko 			param->type = ref_type_id;
2663d5caef5bSAndrii Nakryiko 			param++;
2664d5caef5bSAndrii Nakryiko 		}
2665d5caef5bSAndrii Nakryiko 
2666d5caef5bSAndrii Nakryiko 		h = btf_hash_fnproto(t);
2667*2fc3fc0bSAndrii Nakryiko 		for_each_dedup_cand(d, hash_entry, h) {
2668*2fc3fc0bSAndrii Nakryiko 			cand_id = (__u32)(long)hash_entry->value;
2669*2fc3fc0bSAndrii Nakryiko 			cand = d->btf->types[cand_id];
2670d5caef5bSAndrii Nakryiko 			if (btf_equal_fnproto(t, cand)) {
2671*2fc3fc0bSAndrii Nakryiko 				new_id = cand_id;
2672d5caef5bSAndrii Nakryiko 				break;
2673d5caef5bSAndrii Nakryiko 			}
2674d5caef5bSAndrii Nakryiko 		}
2675d5caef5bSAndrii Nakryiko 		break;
2676d5caef5bSAndrii Nakryiko 	}
2677d5caef5bSAndrii Nakryiko 
2678d5caef5bSAndrii Nakryiko 	default:
2679d5caef5bSAndrii Nakryiko 		return -EINVAL;
2680d5caef5bSAndrii Nakryiko 	}
2681d5caef5bSAndrii Nakryiko 
2682d5caef5bSAndrii Nakryiko 	d->map[type_id] = new_id;
2683d5caef5bSAndrii Nakryiko 	if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2684d5caef5bSAndrii Nakryiko 		return -ENOMEM;
2685d5caef5bSAndrii Nakryiko 
2686d5caef5bSAndrii Nakryiko 	return new_id;
2687d5caef5bSAndrii Nakryiko }
2688d5caef5bSAndrii Nakryiko 
2689d5caef5bSAndrii Nakryiko static int btf_dedup_ref_types(struct btf_dedup *d)
2690d5caef5bSAndrii Nakryiko {
2691d5caef5bSAndrii Nakryiko 	int i, err;
2692d5caef5bSAndrii Nakryiko 
2693d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
2694d5caef5bSAndrii Nakryiko 		err = btf_dedup_ref_type(d, i);
2695d5caef5bSAndrii Nakryiko 		if (err < 0)
2696d5caef5bSAndrii Nakryiko 			return err;
2697d5caef5bSAndrii Nakryiko 	}
2698*2fc3fc0bSAndrii Nakryiko 	/* we won't need d->dedup_table anymore */
2699*2fc3fc0bSAndrii Nakryiko 	hashmap__free(d->dedup_table);
2700*2fc3fc0bSAndrii Nakryiko 	d->dedup_table = NULL;
2701d5caef5bSAndrii Nakryiko 	return 0;
2702d5caef5bSAndrii Nakryiko }
2703d5caef5bSAndrii Nakryiko 
2704d5caef5bSAndrii Nakryiko /*
2705d5caef5bSAndrii Nakryiko  * Compact types.
2706d5caef5bSAndrii Nakryiko  *
2707d5caef5bSAndrii Nakryiko  * After we established for each type its corresponding canonical representative
2708d5caef5bSAndrii Nakryiko  * type, we now can eliminate types that are not canonical and leave only
2709d5caef5bSAndrii Nakryiko  * canonical ones layed out sequentially in memory by copying them over
2710d5caef5bSAndrii Nakryiko  * duplicates. During compaction btf_dedup->hypot_map array is reused to store
2711d5caef5bSAndrii Nakryiko  * a map from original type ID to a new compacted type ID, which will be used
2712d5caef5bSAndrii Nakryiko  * during next phase to "fix up" type IDs, referenced from struct/union and
2713d5caef5bSAndrii Nakryiko  * reference types.
2714d5caef5bSAndrii Nakryiko  */
2715d5caef5bSAndrii Nakryiko static int btf_dedup_compact_types(struct btf_dedup *d)
2716d5caef5bSAndrii Nakryiko {
2717d5caef5bSAndrii Nakryiko 	struct btf_type **new_types;
2718d5caef5bSAndrii Nakryiko 	__u32 next_type_id = 1;
2719d5caef5bSAndrii Nakryiko 	char *types_start, *p;
2720d5caef5bSAndrii Nakryiko 	int i, len;
2721d5caef5bSAndrii Nakryiko 
2722d5caef5bSAndrii Nakryiko 	/* we are going to reuse hypot_map to store compaction remapping */
2723d5caef5bSAndrii Nakryiko 	d->hypot_map[0] = 0;
2724d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++)
2725d5caef5bSAndrii Nakryiko 		d->hypot_map[i] = BTF_UNPROCESSED_ID;
2726d5caef5bSAndrii Nakryiko 
2727d5caef5bSAndrii Nakryiko 	types_start = d->btf->nohdr_data + d->btf->hdr->type_off;
2728d5caef5bSAndrii Nakryiko 	p = types_start;
2729d5caef5bSAndrii Nakryiko 
2730d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
2731d5caef5bSAndrii Nakryiko 		if (d->map[i] != i)
2732d5caef5bSAndrii Nakryiko 			continue;
2733d5caef5bSAndrii Nakryiko 
2734d5caef5bSAndrii Nakryiko 		len = btf_type_size(d->btf->types[i]);
2735d5caef5bSAndrii Nakryiko 		if (len < 0)
2736d5caef5bSAndrii Nakryiko 			return len;
2737d5caef5bSAndrii Nakryiko 
2738d5caef5bSAndrii Nakryiko 		memmove(p, d->btf->types[i], len);
2739d5caef5bSAndrii Nakryiko 		d->hypot_map[i] = next_type_id;
2740d5caef5bSAndrii Nakryiko 		d->btf->types[next_type_id] = (struct btf_type *)p;
2741d5caef5bSAndrii Nakryiko 		p += len;
2742d5caef5bSAndrii Nakryiko 		next_type_id++;
2743d5caef5bSAndrii Nakryiko 	}
2744d5caef5bSAndrii Nakryiko 
2745d5caef5bSAndrii Nakryiko 	/* shrink struct btf's internal types index and update btf_header */
2746d5caef5bSAndrii Nakryiko 	d->btf->nr_types = next_type_id - 1;
2747d5caef5bSAndrii Nakryiko 	d->btf->types_size = d->btf->nr_types;
2748d5caef5bSAndrii Nakryiko 	d->btf->hdr->type_len = p - types_start;
2749d5caef5bSAndrii Nakryiko 	new_types = realloc(d->btf->types,
2750d5caef5bSAndrii Nakryiko 			    (1 + d->btf->nr_types) * sizeof(struct btf_type *));
2751d5caef5bSAndrii Nakryiko 	if (!new_types)
2752d5caef5bSAndrii Nakryiko 		return -ENOMEM;
2753d5caef5bSAndrii Nakryiko 	d->btf->types = new_types;
2754d5caef5bSAndrii Nakryiko 
2755d5caef5bSAndrii Nakryiko 	/* make sure string section follows type information without gaps */
2756d5caef5bSAndrii Nakryiko 	d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data;
2757d5caef5bSAndrii Nakryiko 	memmove(p, d->btf->strings, d->btf->hdr->str_len);
2758d5caef5bSAndrii Nakryiko 	d->btf->strings = p;
2759d5caef5bSAndrii Nakryiko 	p += d->btf->hdr->str_len;
2760d5caef5bSAndrii Nakryiko 
2761d5caef5bSAndrii Nakryiko 	d->btf->data_size = p - (char *)d->btf->data;
2762d5caef5bSAndrii Nakryiko 	return 0;
2763d5caef5bSAndrii Nakryiko }
2764d5caef5bSAndrii Nakryiko 
2765d5caef5bSAndrii Nakryiko /*
2766d5caef5bSAndrii Nakryiko  * Figure out final (deduplicated and compacted) type ID for provided original
2767d5caef5bSAndrii Nakryiko  * `type_id` by first resolving it into corresponding canonical type ID and
2768d5caef5bSAndrii Nakryiko  * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
2769d5caef5bSAndrii Nakryiko  * which is populated during compaction phase.
2770d5caef5bSAndrii Nakryiko  */
2771d5caef5bSAndrii Nakryiko static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
2772d5caef5bSAndrii Nakryiko {
2773d5caef5bSAndrii Nakryiko 	__u32 resolved_type_id, new_type_id;
2774d5caef5bSAndrii Nakryiko 
2775d5caef5bSAndrii Nakryiko 	resolved_type_id = resolve_type_id(d, type_id);
2776d5caef5bSAndrii Nakryiko 	new_type_id = d->hypot_map[resolved_type_id];
27775aab392cSAndrii Nakryiko 	if (new_type_id > BTF_MAX_NR_TYPES)
2778d5caef5bSAndrii Nakryiko 		return -EINVAL;
2779d5caef5bSAndrii Nakryiko 	return new_type_id;
2780d5caef5bSAndrii Nakryiko }
2781d5caef5bSAndrii Nakryiko 
2782d5caef5bSAndrii Nakryiko /*
2783d5caef5bSAndrii Nakryiko  * Remap referenced type IDs into deduped type IDs.
2784d5caef5bSAndrii Nakryiko  *
2785d5caef5bSAndrii Nakryiko  * After BTF types are deduplicated and compacted, their final type IDs may
2786d5caef5bSAndrii Nakryiko  * differ from original ones. The map from original to a corresponding
2787d5caef5bSAndrii Nakryiko  * deduped type ID is stored in btf_dedup->hypot_map and is populated during
2788d5caef5bSAndrii Nakryiko  * compaction phase. During remapping phase we are rewriting all type IDs
2789d5caef5bSAndrii Nakryiko  * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
2790d5caef5bSAndrii Nakryiko  * their final deduped type IDs.
2791d5caef5bSAndrii Nakryiko  */
2792d5caef5bSAndrii Nakryiko static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
2793d5caef5bSAndrii Nakryiko {
2794d5caef5bSAndrii Nakryiko 	struct btf_type *t = d->btf->types[type_id];
2795d5caef5bSAndrii Nakryiko 	int i, r;
2796d5caef5bSAndrii Nakryiko 
2797d5caef5bSAndrii Nakryiko 	switch (BTF_INFO_KIND(t->info)) {
2798d5caef5bSAndrii Nakryiko 	case BTF_KIND_INT:
2799d5caef5bSAndrii Nakryiko 	case BTF_KIND_ENUM:
2800d5caef5bSAndrii Nakryiko 		break;
2801d5caef5bSAndrii Nakryiko 
2802d5caef5bSAndrii Nakryiko 	case BTF_KIND_FWD:
2803d5caef5bSAndrii Nakryiko 	case BTF_KIND_CONST:
2804d5caef5bSAndrii Nakryiko 	case BTF_KIND_VOLATILE:
2805d5caef5bSAndrii Nakryiko 	case BTF_KIND_RESTRICT:
2806d5caef5bSAndrii Nakryiko 	case BTF_KIND_PTR:
2807d5caef5bSAndrii Nakryiko 	case BTF_KIND_TYPEDEF:
2808d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC:
2809189cf5a4SAndrii Nakryiko 	case BTF_KIND_VAR:
2810d5caef5bSAndrii Nakryiko 		r = btf_dedup_remap_type_id(d, t->type);
2811d5caef5bSAndrii Nakryiko 		if (r < 0)
2812d5caef5bSAndrii Nakryiko 			return r;
2813d5caef5bSAndrii Nakryiko 		t->type = r;
2814d5caef5bSAndrii Nakryiko 		break;
2815d5caef5bSAndrii Nakryiko 
2816d5caef5bSAndrii Nakryiko 	case BTF_KIND_ARRAY: {
2817d5caef5bSAndrii Nakryiko 		struct btf_array *arr_info = (struct btf_array *)(t + 1);
2818d5caef5bSAndrii Nakryiko 
2819d5caef5bSAndrii Nakryiko 		r = btf_dedup_remap_type_id(d, arr_info->type);
2820d5caef5bSAndrii Nakryiko 		if (r < 0)
2821d5caef5bSAndrii Nakryiko 			return r;
2822d5caef5bSAndrii Nakryiko 		arr_info->type = r;
2823d5caef5bSAndrii Nakryiko 		r = btf_dedup_remap_type_id(d, arr_info->index_type);
2824d5caef5bSAndrii Nakryiko 		if (r < 0)
2825d5caef5bSAndrii Nakryiko 			return r;
2826d5caef5bSAndrii Nakryiko 		arr_info->index_type = r;
2827d5caef5bSAndrii Nakryiko 		break;
2828d5caef5bSAndrii Nakryiko 	}
2829d5caef5bSAndrii Nakryiko 
2830d5caef5bSAndrii Nakryiko 	case BTF_KIND_STRUCT:
2831d5caef5bSAndrii Nakryiko 	case BTF_KIND_UNION: {
2832d5caef5bSAndrii Nakryiko 		struct btf_member *member = (struct btf_member *)(t + 1);
2833d5caef5bSAndrii Nakryiko 		__u16 vlen = BTF_INFO_VLEN(t->info);
2834d5caef5bSAndrii Nakryiko 
2835d5caef5bSAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2836d5caef5bSAndrii Nakryiko 			r = btf_dedup_remap_type_id(d, member->type);
2837d5caef5bSAndrii Nakryiko 			if (r < 0)
2838d5caef5bSAndrii Nakryiko 				return r;
2839d5caef5bSAndrii Nakryiko 			member->type = r;
2840d5caef5bSAndrii Nakryiko 			member++;
2841d5caef5bSAndrii Nakryiko 		}
2842d5caef5bSAndrii Nakryiko 		break;
2843d5caef5bSAndrii Nakryiko 	}
2844d5caef5bSAndrii Nakryiko 
2845d5caef5bSAndrii Nakryiko 	case BTF_KIND_FUNC_PROTO: {
2846d5caef5bSAndrii Nakryiko 		struct btf_param *param = (struct btf_param *)(t + 1);
2847d5caef5bSAndrii Nakryiko 		__u16 vlen = BTF_INFO_VLEN(t->info);
2848d5caef5bSAndrii Nakryiko 
2849d5caef5bSAndrii Nakryiko 		r = btf_dedup_remap_type_id(d, t->type);
2850d5caef5bSAndrii Nakryiko 		if (r < 0)
2851d5caef5bSAndrii Nakryiko 			return r;
2852d5caef5bSAndrii Nakryiko 		t->type = r;
2853d5caef5bSAndrii Nakryiko 
2854d5caef5bSAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2855d5caef5bSAndrii Nakryiko 			r = btf_dedup_remap_type_id(d, param->type);
2856d5caef5bSAndrii Nakryiko 			if (r < 0)
2857d5caef5bSAndrii Nakryiko 				return r;
2858d5caef5bSAndrii Nakryiko 			param->type = r;
2859d5caef5bSAndrii Nakryiko 			param++;
2860d5caef5bSAndrii Nakryiko 		}
2861d5caef5bSAndrii Nakryiko 		break;
2862d5caef5bSAndrii Nakryiko 	}
2863d5caef5bSAndrii Nakryiko 
2864189cf5a4SAndrii Nakryiko 	case BTF_KIND_DATASEC: {
2865189cf5a4SAndrii Nakryiko 		struct btf_var_secinfo *var = (struct btf_var_secinfo *)(t + 1);
2866189cf5a4SAndrii Nakryiko 		__u16 vlen = BTF_INFO_VLEN(t->info);
2867189cf5a4SAndrii Nakryiko 
2868189cf5a4SAndrii Nakryiko 		for (i = 0; i < vlen; i++) {
2869189cf5a4SAndrii Nakryiko 			r = btf_dedup_remap_type_id(d, var->type);
2870189cf5a4SAndrii Nakryiko 			if (r < 0)
2871189cf5a4SAndrii Nakryiko 				return r;
2872189cf5a4SAndrii Nakryiko 			var->type = r;
2873189cf5a4SAndrii Nakryiko 			var++;
2874189cf5a4SAndrii Nakryiko 		}
2875189cf5a4SAndrii Nakryiko 		break;
2876189cf5a4SAndrii Nakryiko 	}
2877189cf5a4SAndrii Nakryiko 
2878d5caef5bSAndrii Nakryiko 	default:
2879d5caef5bSAndrii Nakryiko 		return -EINVAL;
2880d5caef5bSAndrii Nakryiko 	}
2881d5caef5bSAndrii Nakryiko 
2882d5caef5bSAndrii Nakryiko 	return 0;
2883d5caef5bSAndrii Nakryiko }
2884d5caef5bSAndrii Nakryiko 
2885d5caef5bSAndrii Nakryiko static int btf_dedup_remap_types(struct btf_dedup *d)
2886d5caef5bSAndrii Nakryiko {
2887d5caef5bSAndrii Nakryiko 	int i, r;
2888d5caef5bSAndrii Nakryiko 
2889d5caef5bSAndrii Nakryiko 	for (i = 1; i <= d->btf->nr_types; i++) {
2890d5caef5bSAndrii Nakryiko 		r = btf_dedup_remap_type(d, i);
2891d5caef5bSAndrii Nakryiko 		if (r < 0)
2892d5caef5bSAndrii Nakryiko 			return r;
2893d5caef5bSAndrii Nakryiko 	}
2894d5caef5bSAndrii Nakryiko 	return 0;
2895d5caef5bSAndrii Nakryiko }
2896