1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
10 #include <linux/btf.h>
14 #include "libbpf_internal.h"
16 #define max(a, b) ((a) > (b) ? (a) : (b))
17 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define BTF_MAX_NR_TYPES 0x7fffffff
20 #define BTF_MAX_STR_OFFSET 0x7fffffff
22 #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
23 ((k) == BTF_KIND_VOLATILE) || \
24 ((k) == BTF_KIND_CONST) || \
25 ((k) == BTF_KIND_RESTRICT))
27 #define IS_VAR(k) ((k) == BTF_KIND_VAR)
29 static struct btf_type btf_void;
33 struct btf_header *hdr;
36 struct btf_type **types;
47 * info points to the individual info section (e.g. func_info and
48 * line_info) from the .BTF.ext. It does not include the __u32 rec_size.
57 struct btf_ext_header *hdr;
60 struct btf_ext_info func_info;
61 struct btf_ext_info line_info;
65 struct btf_ext_info_sec {
68 /* Followed by num_info * record_size number of bytes */
72 /* The minimum bpf_func_info checked by the loader */
73 struct bpf_func_info_min {
78 /* The minimum bpf_line_info checked by the loader */
79 struct bpf_line_info_min {
86 static inline __u64 ptr_to_u64(const void *ptr)
88 return (__u64) (unsigned long) ptr;
91 static int btf_add_type(struct btf *btf, struct btf_type *t)
93 if (btf->types_size - btf->nr_types < 2) {
94 struct btf_type **new_types;
95 __u32 expand_by, new_size;
97 if (btf->types_size == BTF_MAX_NR_TYPES)
100 expand_by = max(btf->types_size >> 2, 16);
101 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
103 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
107 if (btf->nr_types == 0)
108 new_types[0] = &btf_void;
110 btf->types = new_types;
111 btf->types_size = new_size;
114 btf->types[++(btf->nr_types)] = t;
119 static int btf_parse_hdr(struct btf *btf)
121 const struct btf_header *hdr = btf->hdr;
124 if (btf->data_size < sizeof(struct btf_header)) {
125 pr_debug("BTF header not found\n");
129 if (hdr->magic != BTF_MAGIC) {
130 pr_debug("Invalid BTF magic:%x\n", hdr->magic);
134 if (hdr->version != BTF_VERSION) {
135 pr_debug("Unsupported BTF version:%u\n", hdr->version);
140 pr_debug("Unsupported BTF flags:%x\n", hdr->flags);
144 meta_left = btf->data_size - sizeof(*hdr);
146 pr_debug("BTF has no data\n");
150 if (meta_left < hdr->type_off) {
151 pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off);
155 if (meta_left < hdr->str_off) {
156 pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off);
160 if (hdr->type_off >= hdr->str_off) {
161 pr_debug("BTF type section offset >= string section offset. No type?\n");
165 if (hdr->type_off & 0x02) {
166 pr_debug("BTF type section is not aligned to 4 bytes\n");
170 btf->nohdr_data = btf->hdr + 1;
175 static int btf_parse_str_sec(struct btf *btf)
177 const struct btf_header *hdr = btf->hdr;
178 const char *start = btf->nohdr_data + hdr->str_off;
179 const char *end = start + btf->hdr->str_len;
181 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
182 start[0] || end[-1]) {
183 pr_debug("Invalid BTF string section\n");
187 btf->strings = start;
192 static int btf_type_size(struct btf_type *t)
194 int base_size = sizeof(struct btf_type);
195 __u16 vlen = BTF_INFO_VLEN(t->info);
197 switch (BTF_INFO_KIND(t->info)) {
200 case BTF_KIND_VOLATILE:
201 case BTF_KIND_RESTRICT:
203 case BTF_KIND_TYPEDEF:
207 return base_size + sizeof(__u32);
209 return base_size + vlen * sizeof(struct btf_enum);
211 return base_size + sizeof(struct btf_array);
212 case BTF_KIND_STRUCT:
214 return base_size + vlen * sizeof(struct btf_member);
215 case BTF_KIND_FUNC_PROTO:
216 return base_size + vlen * sizeof(struct btf_param);
218 return base_size + sizeof(struct btf_var);
219 case BTF_KIND_DATASEC:
220 return base_size + vlen * sizeof(struct btf_var_secinfo);
222 pr_debug("Unsupported BTF_KIND:%u\n", BTF_INFO_KIND(t->info));
227 static int btf_parse_type_sec(struct btf *btf)
229 struct btf_header *hdr = btf->hdr;
230 void *nohdr_data = btf->nohdr_data;
231 void *next_type = nohdr_data + hdr->type_off;
232 void *end_type = nohdr_data + hdr->str_off;
234 while (next_type < end_type) {
235 struct btf_type *t = next_type;
239 type_size = btf_type_size(t);
242 next_type += type_size;
243 err = btf_add_type(btf, t);
251 __u32 btf__get_nr_types(const struct btf *btf)
253 return btf->nr_types;
256 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
258 if (type_id > btf->nr_types)
261 return btf->types[type_id];
264 static bool btf_type_is_void(const struct btf_type *t)
266 return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
269 static bool btf_type_is_void_or_null(const struct btf_type *t)
271 return !t || btf_type_is_void(t);
274 #define MAX_RESOLVE_DEPTH 32
276 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
278 const struct btf_array *array;
279 const struct btf_type *t;
284 t = btf__type_by_id(btf, type_id);
285 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
287 switch (BTF_INFO_KIND(t->info)) {
289 case BTF_KIND_STRUCT:
292 case BTF_KIND_DATASEC:
296 size = sizeof(void *);
298 case BTF_KIND_TYPEDEF:
299 case BTF_KIND_VOLATILE:
301 case BTF_KIND_RESTRICT:
306 array = (const struct btf_array *)(t + 1);
307 if (nelems && array->nelems > UINT32_MAX / nelems)
309 nelems *= array->nelems;
310 type_id = array->type;
316 t = btf__type_by_id(btf, type_id);
323 if (nelems && size > UINT32_MAX / nelems)
326 return nelems * size;
329 int btf__resolve_type(const struct btf *btf, __u32 type_id)
331 const struct btf_type *t;
334 t = btf__type_by_id(btf, type_id);
335 while (depth < MAX_RESOLVE_DEPTH &&
336 !btf_type_is_void_or_null(t) &&
337 (IS_MODIFIER(BTF_INFO_KIND(t->info)) ||
338 IS_VAR(BTF_INFO_KIND(t->info)))) {
340 t = btf__type_by_id(btf, type_id);
344 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
350 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
354 if (!strcmp(type_name, "void"))
357 for (i = 1; i <= btf->nr_types; i++) {
358 const struct btf_type *t = btf->types[i];
359 const char *name = btf__name_by_offset(btf, t->name_off);
361 if (name && !strcmp(type_name, name))
368 void btf__free(struct btf *btf)
381 struct btf *btf__new(__u8 *data, __u32 size)
386 btf = calloc(1, sizeof(struct btf));
388 return ERR_PTR(-ENOMEM);
392 btf->data = malloc(size);
398 memcpy(btf->data, data, size);
399 btf->data_size = size;
401 err = btf_parse_hdr(btf);
405 err = btf_parse_str_sec(btf);
409 err = btf_parse_type_sec(btf);
420 static int compare_vsi_off(const void *_a, const void *_b)
422 const struct btf_var_secinfo *a = _a;
423 const struct btf_var_secinfo *b = _b;
425 return a->offset - b->offset;
428 static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
431 __u32 size = 0, off = 0, i, vars = BTF_INFO_VLEN(t->info);
432 const char *name = btf__name_by_offset(btf, t->name_off);
433 const struct btf_type *t_var;
434 struct btf_var_secinfo *vsi;
439 pr_debug("No name found in string section for DATASEC kind.\n");
443 ret = bpf_object__section_size(obj, name, &size);
444 if (ret || !size || (t->size && t->size != size)) {
445 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
451 for (i = 0, vsi = (struct btf_var_secinfo *)(t + 1);
452 i < vars; i++, vsi++) {
453 t_var = btf__type_by_id(btf, vsi->type);
454 var = (struct btf_var *)(t_var + 1);
456 if (BTF_INFO_KIND(t_var->info) != BTF_KIND_VAR) {
457 pr_debug("Non-VAR type seen in section %s\n", name);
461 if (var->linkage == BTF_VAR_STATIC)
464 name = btf__name_by_offset(btf, t_var->name_off);
466 pr_debug("No name found in string section for VAR kind\n");
470 ret = bpf_object__variable_offset(obj, name, &off);
472 pr_debug("No offset found in symbol table for VAR %s\n", name);
479 qsort(t + 1, vars, sizeof(*vsi), compare_vsi_off);
483 int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
488 for (i = 1; i <= btf->nr_types; i++) {
489 struct btf_type *t = btf->types[i];
491 /* Loader needs to fix up some of the things compiler
492 * couldn't get its hands on while emitting BTF. This
493 * is section size and global variable offset. We use
494 * the info from the ELF itself for this purpose.
496 if (BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC) {
497 err = btf_fixup_datasec(obj, btf, t);
506 int btf__load(struct btf *btf)
508 __u32 log_buf_size = BPF_LOG_BUF_SIZE;
509 char *log_buf = NULL;
515 log_buf = malloc(log_buf_size);
521 btf->fd = bpf_load_btf(btf->data, btf->data_size,
522 log_buf, log_buf_size, false);
525 pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
527 pr_warning("%s\n", log_buf);
536 int btf__fd(const struct btf *btf)
541 const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
543 *size = btf->data_size;
547 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
549 if (offset < btf->hdr->str_len)
550 return &btf->strings[offset];
555 int btf__get_from_id(__u32 id, struct btf **btf)
557 struct bpf_btf_info btf_info = { 0 };
558 __u32 len = sizeof(btf_info);
566 btf_fd = bpf_btf_get_fd_by_id(id);
570 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
571 * let's start with a sane default - 4KiB here - and resize it only if
572 * bpf_obj_get_info_by_fd() needs a bigger buffer.
574 btf_info.btf_size = 4096;
575 last_size = btf_info.btf_size;
576 ptr = malloc(last_size);
582 memset(ptr, 0, last_size);
583 btf_info.btf = ptr_to_u64(ptr);
584 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
586 if (!err && btf_info.btf_size > last_size) {
589 last_size = btf_info.btf_size;
590 temp_ptr = realloc(ptr, last_size);
596 memset(ptr, 0, last_size);
597 btf_info.btf = ptr_to_u64(ptr);
598 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
601 if (err || btf_info.btf_size > last_size) {
606 *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
619 int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
620 __u32 expected_key_size, __u32 expected_value_size,
621 __u32 *key_type_id, __u32 *value_type_id)
623 const struct btf_type *container_type;
624 const struct btf_member *key, *value;
625 const size_t max_name = 256;
626 char container_name[max_name];
627 __s64 key_size, value_size;
630 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
632 pr_warning("map:%s length of '____btf_map_%s' is too long\n",
637 container_id = btf__find_by_name(btf, container_name);
638 if (container_id < 0) {
639 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
640 map_name, container_name);
644 container_type = btf__type_by_id(btf, container_id);
645 if (!container_type) {
646 pr_warning("map:%s cannot find BTF type for container_id:%u\n",
647 map_name, container_id);
651 if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT ||
652 BTF_INFO_VLEN(container_type->info) < 2) {
653 pr_warning("map:%s container_name:%s is an invalid container struct\n",
654 map_name, container_name);
658 key = (struct btf_member *)(container_type + 1);
661 key_size = btf__resolve_size(btf, key->type);
663 pr_warning("map:%s invalid BTF key_type_size\n", map_name);
667 if (expected_key_size != key_size) {
668 pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
669 map_name, (__u32)key_size, expected_key_size);
673 value_size = btf__resolve_size(btf, value->type);
674 if (value_size < 0) {
675 pr_warning("map:%s invalid BTF value_type_size\n", map_name);
679 if (expected_value_size != value_size) {
680 pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
681 map_name, (__u32)value_size, expected_value_size);
685 *key_type_id = key->type;
686 *value_type_id = value->type;
691 struct btf_ext_sec_setup_param {
695 struct btf_ext_info *ext_info;
699 static int btf_ext_setup_info(struct btf_ext *btf_ext,
700 struct btf_ext_sec_setup_param *ext_sec)
702 const struct btf_ext_info_sec *sinfo;
703 struct btf_ext_info *ext_info;
704 __u32 info_left, record_size;
705 /* The start of the info sec (including the __u32 record_size). */
708 if (ext_sec->off & 0x03) {
709 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
714 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
715 info_left = ext_sec->len;
717 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
718 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
719 ext_sec->desc, ext_sec->off, ext_sec->len);
723 /* At least a record size */
724 if (info_left < sizeof(__u32)) {
725 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
729 /* The record size needs to meet the minimum standard */
730 record_size = *(__u32 *)info;
731 if (record_size < ext_sec->min_rec_size ||
732 record_size & 0x03) {
733 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
734 ext_sec->desc, record_size);
738 sinfo = info + sizeof(__u32);
739 info_left -= sizeof(__u32);
741 /* If no records, return failure now so .BTF.ext won't be used. */
743 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
748 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
749 __u64 total_record_size;
752 if (info_left < sec_hdrlen) {
753 pr_debug("%s section header is not found in .BTF.ext\n",
758 num_records = sinfo->num_info;
759 if (num_records == 0) {
760 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
765 total_record_size = sec_hdrlen +
766 (__u64)num_records * record_size;
767 if (info_left < total_record_size) {
768 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
773 info_left -= total_record_size;
774 sinfo = (void *)sinfo + total_record_size;
777 ext_info = ext_sec->ext_info;
778 ext_info->len = ext_sec->len - sizeof(__u32);
779 ext_info->rec_size = record_size;
780 ext_info->info = info + sizeof(__u32);
785 static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
787 struct btf_ext_sec_setup_param param = {
788 .off = btf_ext->hdr->func_info_off,
789 .len = btf_ext->hdr->func_info_len,
790 .min_rec_size = sizeof(struct bpf_func_info_min),
791 .ext_info = &btf_ext->func_info,
795 return btf_ext_setup_info(btf_ext, ¶m);
798 static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
800 struct btf_ext_sec_setup_param param = {
801 .off = btf_ext->hdr->line_info_off,
802 .len = btf_ext->hdr->line_info_len,
803 .min_rec_size = sizeof(struct bpf_line_info_min),
804 .ext_info = &btf_ext->line_info,
808 return btf_ext_setup_info(btf_ext, ¶m);
811 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
813 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
815 if (data_size < offsetof(struct btf_ext_header, func_info_off) ||
816 data_size < hdr->hdr_len) {
817 pr_debug("BTF.ext header not found");
821 if (hdr->magic != BTF_MAGIC) {
822 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
826 if (hdr->version != BTF_VERSION) {
827 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
832 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
836 if (data_size == hdr->hdr_len) {
837 pr_debug("BTF.ext has no data\n");
844 void btf_ext__free(struct btf_ext *btf_ext)
852 struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
854 struct btf_ext *btf_ext;
857 err = btf_ext_parse_hdr(data, size);
861 btf_ext = calloc(1, sizeof(struct btf_ext));
863 return ERR_PTR(-ENOMEM);
865 btf_ext->data_size = size;
866 btf_ext->data = malloc(size);
867 if (!btf_ext->data) {
871 memcpy(btf_ext->data, data, size);
873 err = btf_ext_setup_func_info(btf_ext);
877 err = btf_ext_setup_line_info(btf_ext);
883 btf_ext__free(btf_ext);
890 const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
892 *size = btf_ext->data_size;
893 return btf_ext->data;
896 static int btf_ext_reloc_info(const struct btf *btf,
897 const struct btf_ext_info *ext_info,
898 const char *sec_name, __u32 insns_cnt,
899 void **info, __u32 *cnt)
901 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
902 __u32 i, record_size, existing_len, records_len;
903 struct btf_ext_info_sec *sinfo;
904 const char *info_sec_name;
908 record_size = ext_info->rec_size;
909 sinfo = ext_info->info;
910 remain_len = ext_info->len;
911 while (remain_len > 0) {
912 records_len = sinfo->num_info * record_size;
913 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
914 if (strcmp(info_sec_name, sec_name)) {
915 remain_len -= sec_hdrlen + records_len;
916 sinfo = (void *)sinfo + sec_hdrlen + records_len;
920 existing_len = (*cnt) * record_size;
921 data = realloc(*info, existing_len + records_len);
925 memcpy(data + existing_len, sinfo->data, records_len);
926 /* adjust insn_off only, the rest data will be passed
929 for (i = 0; i < sinfo->num_info; i++) {
932 insn_off = data + existing_len + (i * record_size);
933 *insn_off = *insn_off / sizeof(struct bpf_insn) +
937 *cnt += sinfo->num_info;
944 int btf_ext__reloc_func_info(const struct btf *btf,
945 const struct btf_ext *btf_ext,
946 const char *sec_name, __u32 insns_cnt,
947 void **func_info, __u32 *cnt)
949 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
950 insns_cnt, func_info, cnt);
953 int btf_ext__reloc_line_info(const struct btf *btf,
954 const struct btf_ext *btf_ext,
955 const char *sec_name, __u32 insns_cnt,
956 void **line_info, __u32 *cnt)
958 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
959 insns_cnt, line_info, cnt);
962 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
964 return btf_ext->func_info.rec_size;
967 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
969 return btf_ext->line_info.rec_size;
974 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
975 const struct btf_dedup_opts *opts);
976 static void btf_dedup_free(struct btf_dedup *d);
977 static int btf_dedup_strings(struct btf_dedup *d);
978 static int btf_dedup_prim_types(struct btf_dedup *d);
979 static int btf_dedup_struct_types(struct btf_dedup *d);
980 static int btf_dedup_ref_types(struct btf_dedup *d);
981 static int btf_dedup_compact_types(struct btf_dedup *d);
982 static int btf_dedup_remap_types(struct btf_dedup *d);
985 * Deduplicate BTF types and strings.
987 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
988 * section with all BTF type descriptors and string data. It overwrites that
989 * memory in-place with deduplicated types and strings without any loss of
990 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
991 * is provided, all the strings referenced from .BTF.ext section are honored
992 * and updated to point to the right offsets after deduplication.
994 * If function returns with error, type/string data might be garbled and should
997 * More verbose and detailed description of both problem btf_dedup is solving,
998 * as well as solution could be found at:
999 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
1001 * Problem description and justification
1002 * =====================================
1004 * BTF type information is typically emitted either as a result of conversion
1005 * from DWARF to BTF or directly by compiler. In both cases, each compilation
1006 * unit contains information about a subset of all the types that are used
1007 * in an application. These subsets are frequently overlapping and contain a lot
1008 * of duplicated information when later concatenated together into a single
1009 * binary. This algorithm ensures that each unique type is represented by single
1010 * BTF type descriptor, greatly reducing resulting size of BTF data.
1012 * Compilation unit isolation and subsequent duplication of data is not the only
1013 * problem. The same type hierarchy (e.g., struct and all the type that struct
1014 * references) in different compilation units can be represented in BTF to
1015 * various degrees of completeness (or, rather, incompleteness) due to
1016 * struct/union forward declarations.
1018 * Let's take a look at an example, that we'll use to better understand the
1019 * problem (and solution). Suppose we have two compilation units, each using
1020 * same `struct S`, but each of them having incomplete type information about
1049 * In case of CU #1, BTF data will know only that `struct B` exist (but no
1050 * more), but will know the complete type information about `struct A`. While
1051 * for CU #2, it will know full type information about `struct B`, but will
1052 * only know about forward declaration of `struct A` (in BTF terms, it will
1053 * have `BTF_KIND_FWD` type descriptor with name `B`).
1055 * This compilation unit isolation means that it's possible that there is no
1056 * single CU with complete type information describing structs `S`, `A`, and
1057 * `B`. Also, we might get tons of duplicated and redundant type information.
1059 * Additional complication we need to keep in mind comes from the fact that
1060 * types, in general, can form graphs containing cycles, not just DAGs.
1062 * While algorithm does deduplication, it also merges and resolves type
1063 * information (unless disabled throught `struct btf_opts`), whenever possible.
1064 * E.g., in the example above with two compilation units having partial type
1065 * information for structs `A` and `B`, the output of algorithm will emit
1066 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
1067 * (as well as type information for `int` and pointers), as if they were defined
1068 * in a single compilation unit as:
1088 * Algorithm completes its work in 6 separate passes:
1090 * 1. Strings deduplication.
1091 * 2. Primitive types deduplication (int, enum, fwd).
1092 * 3. Struct/union types deduplication.
1093 * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
1094 * protos, and const/volatile/restrict modifiers).
1095 * 5. Types compaction.
1096 * 6. Types remapping.
1098 * Algorithm determines canonical type descriptor, which is a single
1099 * representative type for each truly unique type. This canonical type is the
1100 * one that will go into final deduplicated BTF type information. For
1101 * struct/unions, it is also the type that algorithm will merge additional type
1102 * information into (while resolving FWDs), as it discovers it from data in
1103 * other CUs. Each input BTF type eventually gets either mapped to itself, if
1104 * that type is canonical, or to some other type, if that type is equivalent
1105 * and was chosen as canonical representative. This mapping is stored in
1106 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
1107 * FWD type got resolved to.
1109 * To facilitate fast discovery of canonical types, we also maintain canonical
1110 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
1111 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
1112 * that match that signature. With sufficiently good choice of type signature
1113 * hashing function, we can limit number of canonical types for each unique type
1114 * signature to a very small number, allowing to find canonical type for any
1115 * duplicated type very quickly.
1117 * Struct/union deduplication is the most critical part and algorithm for
1118 * deduplicating structs/unions is described in greater details in comments for
1119 * `btf_dedup_is_equiv` function.
1121 int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
1122 const struct btf_dedup_opts *opts)
1124 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
1128 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
1132 err = btf_dedup_strings(d);
1134 pr_debug("btf_dedup_strings failed:%d\n", err);
1137 err = btf_dedup_prim_types(d);
1139 pr_debug("btf_dedup_prim_types failed:%d\n", err);
1142 err = btf_dedup_struct_types(d);
1144 pr_debug("btf_dedup_struct_types failed:%d\n", err);
1147 err = btf_dedup_ref_types(d);
1149 pr_debug("btf_dedup_ref_types failed:%d\n", err);
1152 err = btf_dedup_compact_types(d);
1154 pr_debug("btf_dedup_compact_types failed:%d\n", err);
1157 err = btf_dedup_remap_types(d);
1159 pr_debug("btf_dedup_remap_types failed:%d\n", err);
1168 #define BTF_DEDUP_TABLE_DEFAULT_SIZE (1 << 14)
1169 #define BTF_DEDUP_TABLE_MAX_SIZE_LOG 31
1170 #define BTF_UNPROCESSED_ID ((__u32)-1)
1171 #define BTF_IN_PROGRESS_ID ((__u32)-2)
1173 struct btf_dedup_node {
1174 struct btf_dedup_node *next;
1179 /* .BTF section to be deduped in-place */
1182 * Optional .BTF.ext section. When provided, any strings referenced
1183 * from it will be taken into account when deduping strings
1185 struct btf_ext *btf_ext;
1187 * This is a map from any type's signature hash to a list of possible
1188 * canonical representative type candidates. Hash collisions are
1189 * ignored, so even types of various kinds can share same list of
1190 * candidates, which is fine because we rely on subsequent
1191 * btf_xxx_equal() checks to authoritatively verify type equality.
1193 struct btf_dedup_node **dedup_table;
1194 /* Canonical types map */
1196 /* Hypothetical mapping, used during type graph equivalence checks */
1201 /* Various option modifying behavior of algorithm */
1202 struct btf_dedup_opts opts;
1205 struct btf_str_ptr {
1211 struct btf_str_ptrs {
1212 struct btf_str_ptr *ptrs;
1218 static inline __u32 hash_combine(__u32 h, __u32 value)
1220 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
1221 #define GOLDEN_RATIO_PRIME 0x9e370001UL
1222 return h * 37 + value * GOLDEN_RATIO_PRIME;
1223 #undef GOLDEN_RATIO_PRIME
1226 #define for_each_dedup_cand(d, hash, node) \
1227 for (node = d->dedup_table[hash & (d->opts.dedup_table_size - 1)]; \
1231 static int btf_dedup_table_add(struct btf_dedup *d, __u32 hash, __u32 type_id)
1233 struct btf_dedup_node *node = malloc(sizeof(struct btf_dedup_node));
1234 int bucket = hash & (d->opts.dedup_table_size - 1);
1238 node->type_id = type_id;
1239 node->next = d->dedup_table[bucket];
1240 d->dedup_table[bucket] = node;
1244 static int btf_dedup_hypot_map_add(struct btf_dedup *d,
1245 __u32 from_id, __u32 to_id)
1247 if (d->hypot_cnt == d->hypot_cap) {
1250 d->hypot_cap += max(16, d->hypot_cap / 2);
1251 new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap);
1254 d->hypot_list = new_list;
1256 d->hypot_list[d->hypot_cnt++] = from_id;
1257 d->hypot_map[from_id] = to_id;
1261 static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
1265 for (i = 0; i < d->hypot_cnt; i++)
1266 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
1270 static void btf_dedup_table_free(struct btf_dedup *d)
1272 struct btf_dedup_node *head, *tmp;
1275 if (!d->dedup_table)
1278 for (i = 0; i < d->opts.dedup_table_size; i++) {
1279 while (d->dedup_table[i]) {
1280 tmp = d->dedup_table[i];
1281 d->dedup_table[i] = tmp->next;
1285 head = d->dedup_table[i];
1293 free(d->dedup_table);
1294 d->dedup_table = NULL;
1297 static void btf_dedup_free(struct btf_dedup *d)
1299 btf_dedup_table_free(d);
1305 d->hypot_map = NULL;
1307 free(d->hypot_list);
1308 d->hypot_list = NULL;
1313 /* Find closest power of two >= to size, capped at 2^max_size_log */
1314 static __u32 roundup_pow2_max(__u32 size, int max_size_log)
1318 for (i = 0; i < max_size_log && (1U << i) < size; i++)
1324 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1325 const struct btf_dedup_opts *opts)
1327 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
1332 return ERR_PTR(-ENOMEM);
1334 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
1335 sz = opts && opts->dedup_table_size ? opts->dedup_table_size
1336 : BTF_DEDUP_TABLE_DEFAULT_SIZE;
1337 sz = roundup_pow2_max(sz, BTF_DEDUP_TABLE_MAX_SIZE_LOG);
1338 d->opts.dedup_table_size = sz;
1341 d->btf_ext = btf_ext;
1343 d->dedup_table = calloc(d->opts.dedup_table_size,
1344 sizeof(struct btf_dedup_node *));
1345 if (!d->dedup_table) {
1350 d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1355 /* special BTF "void" type is made canonical immediately */
1357 for (i = 1; i <= btf->nr_types; i++) {
1358 struct btf_type *t = d->btf->types[i];
1359 __u16 kind = BTF_INFO_KIND(t->info);
1361 /* VAR and DATASEC are never deduped and are self-canonical */
1362 if (kind == BTF_KIND_VAR || kind == BTF_KIND_DATASEC)
1365 d->map[i] = BTF_UNPROCESSED_ID;
1368 d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1369 if (!d->hypot_map) {
1373 for (i = 0; i <= btf->nr_types; i++)
1374 d->hypot_map[i] = BTF_UNPROCESSED_ID;
1379 return ERR_PTR(err);
1385 typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
1388 * Iterate over all possible places in .BTF and .BTF.ext that can reference
1389 * string and pass pointer to it to a provided callback `fn`.
1391 static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
1393 void *line_data_cur, *line_data_end;
1394 int i, j, r, rec_size;
1397 for (i = 1; i <= d->btf->nr_types; i++) {
1398 t = d->btf->types[i];
1399 r = fn(&t->name_off, ctx);
1403 switch (BTF_INFO_KIND(t->info)) {
1404 case BTF_KIND_STRUCT:
1405 case BTF_KIND_UNION: {
1406 struct btf_member *m = (struct btf_member *)(t + 1);
1407 __u16 vlen = BTF_INFO_VLEN(t->info);
1409 for (j = 0; j < vlen; j++) {
1410 r = fn(&m->name_off, ctx);
1417 case BTF_KIND_ENUM: {
1418 struct btf_enum *m = (struct btf_enum *)(t + 1);
1419 __u16 vlen = BTF_INFO_VLEN(t->info);
1421 for (j = 0; j < vlen; j++) {
1422 r = fn(&m->name_off, ctx);
1429 case BTF_KIND_FUNC_PROTO: {
1430 struct btf_param *m = (struct btf_param *)(t + 1);
1431 __u16 vlen = BTF_INFO_VLEN(t->info);
1433 for (j = 0; j < vlen; j++) {
1434 r = fn(&m->name_off, ctx);
1449 line_data_cur = d->btf_ext->line_info.info;
1450 line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
1451 rec_size = d->btf_ext->line_info.rec_size;
1453 while (line_data_cur < line_data_end) {
1454 struct btf_ext_info_sec *sec = line_data_cur;
1455 struct bpf_line_info_min *line_info;
1456 __u32 num_info = sec->num_info;
1458 r = fn(&sec->sec_name_off, ctx);
1462 line_data_cur += sizeof(struct btf_ext_info_sec);
1463 for (i = 0; i < num_info; i++) {
1464 line_info = line_data_cur;
1465 r = fn(&line_info->file_name_off, ctx);
1468 r = fn(&line_info->line_off, ctx);
1471 line_data_cur += rec_size;
1478 static int str_sort_by_content(const void *a1, const void *a2)
1480 const struct btf_str_ptr *p1 = a1;
1481 const struct btf_str_ptr *p2 = a2;
1483 return strcmp(p1->str, p2->str);
1486 static int str_sort_by_offset(const void *a1, const void *a2)
1488 const struct btf_str_ptr *p1 = a1;
1489 const struct btf_str_ptr *p2 = a2;
1491 if (p1->str != p2->str)
1492 return p1->str < p2->str ? -1 : 1;
1496 static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
1498 const struct btf_str_ptr *p = pelem;
1500 if (str_ptr != p->str)
1501 return (const char *)str_ptr < p->str ? -1 : 1;
1505 static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
1507 struct btf_str_ptrs *strs;
1508 struct btf_str_ptr *s;
1510 if (*str_off_ptr == 0)
1514 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1515 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1522 static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
1524 struct btf_str_ptrs *strs;
1525 struct btf_str_ptr *s;
1527 if (*str_off_ptr == 0)
1531 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1532 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1535 *str_off_ptr = s->new_off;
1540 * Dedup string and filter out those that are not referenced from either .BTF
1541 * or .BTF.ext (if provided) sections.
1543 * This is done by building index of all strings in BTF's string section,
1544 * then iterating over all entities that can reference strings (e.g., type
1545 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
1546 * strings as used. After that all used strings are deduped and compacted into
1547 * sequential blob of memory and new offsets are calculated. Then all the string
1548 * references are iterated again and rewritten using new offsets.
1550 static int btf_dedup_strings(struct btf_dedup *d)
1552 const struct btf_header *hdr = d->btf->hdr;
1553 char *start = (char *)d->btf->nohdr_data + hdr->str_off;
1554 char *end = start + d->btf->hdr->str_len;
1555 char *p = start, *tmp_strs = NULL;
1556 struct btf_str_ptrs strs = {
1562 int i, j, err = 0, grp_idx;
1565 /* build index of all strings */
1567 if (strs.cnt + 1 > strs.cap) {
1568 struct btf_str_ptr *new_ptrs;
1570 strs.cap += max(strs.cnt / 2, 16);
1571 new_ptrs = realloc(strs.ptrs,
1572 sizeof(strs.ptrs[0]) * strs.cap);
1577 strs.ptrs = new_ptrs;
1580 strs.ptrs[strs.cnt].str = p;
1581 strs.ptrs[strs.cnt].used = false;
1587 /* temporary storage for deduplicated strings */
1588 tmp_strs = malloc(d->btf->hdr->str_len);
1594 /* mark all used strings */
1595 strs.ptrs[0].used = true;
1596 err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
1600 /* sort strings by context, so that we can identify duplicates */
1601 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
1604 * iterate groups of equal strings and if any instance in a group was
1605 * referenced, emit single instance and remember new offset
1609 grp_used = strs.ptrs[0].used;
1610 /* iterate past end to avoid code duplication after loop */
1611 for (i = 1; i <= strs.cnt; i++) {
1613 * when i == strs.cnt, we want to skip string comparison and go
1614 * straight to handling last group of strings (otherwise we'd
1615 * need to handle last group after the loop w/ duplicated code)
1618 !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
1619 grp_used = grp_used || strs.ptrs[i].used;
1624 * this check would have been required after the loop to handle
1625 * last group of strings, but due to <= condition in a loop
1626 * we avoid that duplication
1629 int new_off = p - tmp_strs;
1630 __u32 len = strlen(strs.ptrs[grp_idx].str);
1632 memmove(p, strs.ptrs[grp_idx].str, len + 1);
1633 for (j = grp_idx; j < i; j++)
1634 strs.ptrs[j].new_off = new_off;
1640 grp_used = strs.ptrs[i].used;
1644 /* replace original strings with deduped ones */
1645 d->btf->hdr->str_len = p - tmp_strs;
1646 memmove(start, tmp_strs, d->btf->hdr->str_len);
1647 end = start + d->btf->hdr->str_len;
1649 /* restore original order for further binary search lookups */
1650 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
1652 /* remap string offsets */
1653 err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
1657 d->btf->hdr->str_len = end - start;
1665 static __u32 btf_hash_common(struct btf_type *t)
1669 h = hash_combine(0, t->name_off);
1670 h = hash_combine(h, t->info);
1671 h = hash_combine(h, t->size);
1675 static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
1677 return t1->name_off == t2->name_off &&
1678 t1->info == t2->info &&
1679 t1->size == t2->size;
1682 /* Calculate type signature hash of INT. */
1683 static __u32 btf_hash_int(struct btf_type *t)
1685 __u32 info = *(__u32 *)(t + 1);
1688 h = btf_hash_common(t);
1689 h = hash_combine(h, info);
1693 /* Check structural equality of two INTs. */
1694 static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
1698 if (!btf_equal_common(t1, t2))
1700 info1 = *(__u32 *)(t1 + 1);
1701 info2 = *(__u32 *)(t2 + 1);
1702 return info1 == info2;
1705 /* Calculate type signature hash of ENUM. */
1706 static __u32 btf_hash_enum(struct btf_type *t)
1710 /* don't hash vlen and enum members to support enum fwd resolving */
1711 h = hash_combine(0, t->name_off);
1712 h = hash_combine(h, t->info & ~0xffff);
1713 h = hash_combine(h, t->size);
1717 /* Check structural equality of two ENUMs. */
1718 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
1720 struct btf_enum *m1, *m2;
1724 if (!btf_equal_common(t1, t2))
1727 vlen = BTF_INFO_VLEN(t1->info);
1728 m1 = (struct btf_enum *)(t1 + 1);
1729 m2 = (struct btf_enum *)(t2 + 1);
1730 for (i = 0; i < vlen; i++) {
1731 if (m1->name_off != m2->name_off || m1->val != m2->val)
1739 static inline bool btf_is_enum_fwd(struct btf_type *t)
1741 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM &&
1742 BTF_INFO_VLEN(t->info) == 0;
1745 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
1747 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
1748 return btf_equal_enum(t1, t2);
1749 /* ignore vlen when comparing */
1750 return t1->name_off == t2->name_off &&
1751 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
1752 t1->size == t2->size;
1756 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
1757 * as referenced type IDs equivalence is established separately during type
1758 * graph equivalence check algorithm.
1760 static __u32 btf_hash_struct(struct btf_type *t)
1762 struct btf_member *member = (struct btf_member *)(t + 1);
1763 __u32 vlen = BTF_INFO_VLEN(t->info);
1764 __u32 h = btf_hash_common(t);
1767 for (i = 0; i < vlen; i++) {
1768 h = hash_combine(h, member->name_off);
1769 h = hash_combine(h, member->offset);
1770 /* no hashing of referenced type ID, it can be unresolved yet */
1777 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1778 * IDs. This check is performed during type graph equivalence check and
1779 * referenced types equivalence is checked separately.
1781 static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
1783 struct btf_member *m1, *m2;
1787 if (!btf_equal_common(t1, t2))
1790 vlen = BTF_INFO_VLEN(t1->info);
1791 m1 = (struct btf_member *)(t1 + 1);
1792 m2 = (struct btf_member *)(t2 + 1);
1793 for (i = 0; i < vlen; i++) {
1794 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
1803 * Calculate type signature hash of ARRAY, including referenced type IDs,
1804 * under assumption that they were already resolved to canonical type IDs and
1805 * are not going to change.
1807 static __u32 btf_hash_array(struct btf_type *t)
1809 struct btf_array *info = (struct btf_array *)(t + 1);
1810 __u32 h = btf_hash_common(t);
1812 h = hash_combine(h, info->type);
1813 h = hash_combine(h, info->index_type);
1814 h = hash_combine(h, info->nelems);
1819 * Check exact equality of two ARRAYs, taking into account referenced
1820 * type IDs, under assumption that they were already resolved to canonical
1821 * type IDs and are not going to change.
1822 * This function is called during reference types deduplication to compare
1823 * ARRAY to potential canonical representative.
1825 static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
1827 struct btf_array *info1, *info2;
1829 if (!btf_equal_common(t1, t2))
1832 info1 = (struct btf_array *)(t1 + 1);
1833 info2 = (struct btf_array *)(t2 + 1);
1834 return info1->type == info2->type &&
1835 info1->index_type == info2->index_type &&
1836 info1->nelems == info2->nelems;
1840 * Check structural compatibility of two ARRAYs, ignoring referenced type
1841 * IDs. This check is performed during type graph equivalence check and
1842 * referenced types equivalence is checked separately.
1844 static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
1846 struct btf_array *info1, *info2;
1848 if (!btf_equal_common(t1, t2))
1851 info1 = (struct btf_array *)(t1 + 1);
1852 info2 = (struct btf_array *)(t2 + 1);
1853 return info1->nelems == info2->nelems;
1857 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
1858 * under assumption that they were already resolved to canonical type IDs and
1859 * are not going to change.
1861 static inline __u32 btf_hash_fnproto(struct btf_type *t)
1863 struct btf_param *member = (struct btf_param *)(t + 1);
1864 __u16 vlen = BTF_INFO_VLEN(t->info);
1865 __u32 h = btf_hash_common(t);
1868 for (i = 0; i < vlen; i++) {
1869 h = hash_combine(h, member->name_off);
1870 h = hash_combine(h, member->type);
1877 * Check exact equality of two FUNC_PROTOs, taking into account referenced
1878 * type IDs, under assumption that they were already resolved to canonical
1879 * type IDs and are not going to change.
1880 * This function is called during reference types deduplication to compare
1881 * FUNC_PROTO to potential canonical representative.
1883 static inline bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
1885 struct btf_param *m1, *m2;
1889 if (!btf_equal_common(t1, t2))
1892 vlen = BTF_INFO_VLEN(t1->info);
1893 m1 = (struct btf_param *)(t1 + 1);
1894 m2 = (struct btf_param *)(t2 + 1);
1895 for (i = 0; i < vlen; i++) {
1896 if (m1->name_off != m2->name_off || m1->type != m2->type)
1905 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1906 * IDs. This check is performed during type graph equivalence check and
1907 * referenced types equivalence is checked separately.
1909 static inline bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
1911 struct btf_param *m1, *m2;
1915 /* skip return type ID */
1916 if (t1->name_off != t2->name_off || t1->info != t2->info)
1919 vlen = BTF_INFO_VLEN(t1->info);
1920 m1 = (struct btf_param *)(t1 + 1);
1921 m2 = (struct btf_param *)(t2 + 1);
1922 for (i = 0; i < vlen; i++) {
1923 if (m1->name_off != m2->name_off)
1932 * Deduplicate primitive types, that can't reference other types, by calculating
1933 * their type signature hash and comparing them with any possible canonical
1934 * candidate. If no canonical candidate matches, type itself is marked as
1935 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
1937 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
1939 struct btf_type *t = d->btf->types[type_id];
1940 struct btf_type *cand;
1941 struct btf_dedup_node *cand_node;
1942 /* if we don't find equivalent type, then we are canonical */
1943 __u32 new_id = type_id;
1946 switch (BTF_INFO_KIND(t->info)) {
1947 case BTF_KIND_CONST:
1948 case BTF_KIND_VOLATILE:
1949 case BTF_KIND_RESTRICT:
1951 case BTF_KIND_TYPEDEF:
1952 case BTF_KIND_ARRAY:
1953 case BTF_KIND_STRUCT:
1954 case BTF_KIND_UNION:
1956 case BTF_KIND_FUNC_PROTO:
1958 case BTF_KIND_DATASEC:
1962 h = btf_hash_int(t);
1963 for_each_dedup_cand(d, h, cand_node) {
1964 cand = d->btf->types[cand_node->type_id];
1965 if (btf_equal_int(t, cand)) {
1966 new_id = cand_node->type_id;
1973 h = btf_hash_enum(t);
1974 for_each_dedup_cand(d, h, cand_node) {
1975 cand = d->btf->types[cand_node->type_id];
1976 if (btf_equal_enum(t, cand)) {
1977 new_id = cand_node->type_id;
1980 if (d->opts.dont_resolve_fwds)
1982 if (btf_compat_enum(t, cand)) {
1983 if (btf_is_enum_fwd(t)) {
1984 /* resolve fwd to full enum */
1985 new_id = cand_node->type_id;
1988 /* resolve canonical enum fwd to full enum */
1989 d->map[cand_node->type_id] = type_id;
1995 h = btf_hash_common(t);
1996 for_each_dedup_cand(d, h, cand_node) {
1997 cand = d->btf->types[cand_node->type_id];
1998 if (btf_equal_common(t, cand)) {
1999 new_id = cand_node->type_id;
2009 d->map[type_id] = new_id;
2010 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2016 static int btf_dedup_prim_types(struct btf_dedup *d)
2020 for (i = 1; i <= d->btf->nr_types; i++) {
2021 err = btf_dedup_prim_type(d, i);
2029 * Check whether type is already mapped into canonical one (could be to itself).
2031 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
2033 return d->map[type_id] <= BTF_MAX_NR_TYPES;
2037 * Resolve type ID into its canonical type ID, if any; otherwise return original
2038 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
2039 * STRUCT/UNION link and resolve it into canonical type ID as well.
2041 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
2043 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2044 type_id = d->map[type_id];
2049 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
2052 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
2054 __u32 orig_type_id = type_id;
2056 if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2059 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2060 type_id = d->map[type_id];
2062 if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2065 return orig_type_id;
2069 static inline __u16 btf_fwd_kind(struct btf_type *t)
2071 return BTF_INFO_KFLAG(t->info) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
2075 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
2076 * call it "candidate graph" in this description for brevity) to a type graph
2077 * formed by (potential) canonical struct/union ("canonical graph" for brevity
2078 * here, though keep in mind that not all types in canonical graph are
2079 * necessarily canonical representatives themselves, some of them might be
2080 * duplicates or its uniqueness might not have been established yet).
2082 * - >0, if type graphs are equivalent;
2083 * - 0, if not equivalent;
2086 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
2087 * equivalence of BTF types at each step. If at any point BTF types in candidate
2088 * and canonical graphs are not compatible structurally, whole graphs are
2089 * incompatible. If types are structurally equivalent (i.e., all information
2090 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
2091 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
2092 * If a type references other types, then those referenced types are checked
2093 * for equivalence recursively.
2095 * During DFS traversal, if we find that for current `canon_id` type we
2096 * already have some mapping in hypothetical map, we check for two possible
2098 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
2099 * happen when type graphs have cycles. In this case we assume those two
2100 * types are equivalent.
2101 * - `canon_id` is mapped to different type. This is contradiction in our
2102 * hypothetical mapping, because same graph in canonical graph corresponds
2103 * to two different types in candidate graph, which for equivalent type
2104 * graphs shouldn't happen. This condition terminates equivalence check
2105 * with negative result.
2107 * If type graphs traversal exhausts types to check and find no contradiction,
2108 * then type graphs are equivalent.
2110 * When checking types for equivalence, there is one special case: FWD types.
2111 * If FWD type resolution is allowed and one of the types (either from canonical
2112 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
2113 * flag) and their names match, hypothetical mapping is updated to point from
2114 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
2115 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
2117 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
2118 * if there are two exactly named (or anonymous) structs/unions that are
2119 * compatible structurally, one of which has FWD field, while other is concrete
2120 * STRUCT/UNION, but according to C sources they are different structs/unions
2121 * that are referencing different types with the same name. This is extremely
2122 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
2123 * this logic is causing problems.
2125 * Doing FWD resolution means that both candidate and/or canonical graphs can
2126 * consists of portions of the graph that come from multiple compilation units.
2127 * This is due to the fact that types within single compilation unit are always
2128 * deduplicated and FWDs are already resolved, if referenced struct/union
2129 * definiton is available. So, if we had unresolved FWD and found corresponding
2130 * STRUCT/UNION, they will be from different compilation units. This
2131 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
2132 * type graph will likely have at least two different BTF types that describe
2133 * same type (e.g., most probably there will be two different BTF types for the
2134 * same 'int' primitive type) and could even have "overlapping" parts of type
2135 * graph that describe same subset of types.
2137 * This in turn means that our assumption that each type in canonical graph
2138 * must correspond to exactly one type in candidate graph might not hold
2139 * anymore and will make it harder to detect contradictions using hypothetical
2140 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
2141 * resolution only in canonical graph. FWDs in candidate graphs are never
2142 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
2144 * - Both types in canonical and candidate graphs are FWDs. If they are
2145 * structurally equivalent, then they can either be both resolved to the
2146 * same STRUCT/UNION or not resolved at all. In both cases they are
2147 * equivalent and there is no need to resolve FWD on candidate side.
2148 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
2149 * so nothing to resolve as well, algorithm will check equivalence anyway.
2150 * - Type in canonical graph is FWD, while type in candidate is concrete
2151 * STRUCT/UNION. In this case candidate graph comes from single compilation
2152 * unit, so there is exactly one BTF type for each unique C type. After
2153 * resolving FWD into STRUCT/UNION, there might be more than one BTF type
2154 * in canonical graph mapping to single BTF type in candidate graph, but
2155 * because hypothetical mapping maps from canonical to candidate types, it's
2156 * alright, and we still maintain the property of having single `canon_id`
2157 * mapping to single `cand_id` (there could be two different `canon_id`
2158 * mapped to the same `cand_id`, but it's not contradictory).
2159 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
2160 * graph is FWD. In this case we are just going to check compatibility of
2161 * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
2162 * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
2163 * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
2164 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
2167 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
2170 struct btf_type *cand_type;
2171 struct btf_type *canon_type;
2172 __u32 hypot_type_id;
2177 /* if both resolve to the same canonical, they must be equivalent */
2178 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
2181 canon_id = resolve_fwd_id(d, canon_id);
2183 hypot_type_id = d->hypot_map[canon_id];
2184 if (hypot_type_id <= BTF_MAX_NR_TYPES)
2185 return hypot_type_id == cand_id;
2187 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
2190 cand_type = d->btf->types[cand_id];
2191 canon_type = d->btf->types[canon_id];
2192 cand_kind = BTF_INFO_KIND(cand_type->info);
2193 canon_kind = BTF_INFO_KIND(canon_type->info);
2195 if (cand_type->name_off != canon_type->name_off)
2198 /* FWD <--> STRUCT/UNION equivalence check, if enabled */
2199 if (!d->opts.dont_resolve_fwds
2200 && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
2201 && cand_kind != canon_kind) {
2205 if (cand_kind == BTF_KIND_FWD) {
2206 real_kind = canon_kind;
2207 fwd_kind = btf_fwd_kind(cand_type);
2209 real_kind = cand_kind;
2210 fwd_kind = btf_fwd_kind(canon_type);
2212 return fwd_kind == real_kind;
2215 if (cand_kind != canon_kind)
2218 switch (cand_kind) {
2220 return btf_equal_int(cand_type, canon_type);
2223 if (d->opts.dont_resolve_fwds)
2224 return btf_equal_enum(cand_type, canon_type);
2226 return btf_compat_enum(cand_type, canon_type);
2229 return btf_equal_common(cand_type, canon_type);
2231 case BTF_KIND_CONST:
2232 case BTF_KIND_VOLATILE:
2233 case BTF_KIND_RESTRICT:
2235 case BTF_KIND_TYPEDEF:
2237 if (cand_type->info != canon_type->info)
2239 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2241 case BTF_KIND_ARRAY: {
2242 struct btf_array *cand_arr, *canon_arr;
2244 if (!btf_compat_array(cand_type, canon_type))
2246 cand_arr = (struct btf_array *)(cand_type + 1);
2247 canon_arr = (struct btf_array *)(canon_type + 1);
2248 eq = btf_dedup_is_equiv(d,
2249 cand_arr->index_type, canon_arr->index_type);
2252 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
2255 case BTF_KIND_STRUCT:
2256 case BTF_KIND_UNION: {
2257 struct btf_member *cand_m, *canon_m;
2260 if (!btf_shallow_equal_struct(cand_type, canon_type))
2262 vlen = BTF_INFO_VLEN(cand_type->info);
2263 cand_m = (struct btf_member *)(cand_type + 1);
2264 canon_m = (struct btf_member *)(canon_type + 1);
2265 for (i = 0; i < vlen; i++) {
2266 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
2276 case BTF_KIND_FUNC_PROTO: {
2277 struct btf_param *cand_p, *canon_p;
2280 if (!btf_compat_fnproto(cand_type, canon_type))
2282 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2285 vlen = BTF_INFO_VLEN(cand_type->info);
2286 cand_p = (struct btf_param *)(cand_type + 1);
2287 canon_p = (struct btf_param *)(canon_type + 1);
2288 for (i = 0; i < vlen; i++) {
2289 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
2305 * Use hypothetical mapping, produced by successful type graph equivalence
2306 * check, to augment existing struct/union canonical mapping, where possible.
2308 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
2309 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
2310 * it doesn't matter if FWD type was part of canonical graph or candidate one,
2311 * we are recording the mapping anyway. As opposed to carefulness required
2312 * for struct/union correspondence mapping (described below), for FWD resolution
2313 * it's not important, as by the time that FWD type (reference type) will be
2314 * deduplicated all structs/unions will be deduped already anyway.
2316 * Recording STRUCT/UNION mapping is purely a performance optimization and is
2317 * not required for correctness. It needs to be done carefully to ensure that
2318 * struct/union from candidate's type graph is not mapped into corresponding
2319 * struct/union from canonical type graph that itself hasn't been resolved into
2320 * canonical representative. The only guarantee we have is that canonical
2321 * struct/union was determined as canonical and that won't change. But any
2322 * types referenced through that struct/union fields could have been not yet
2323 * resolved, so in case like that it's too early to establish any kind of
2324 * correspondence between structs/unions.
2326 * No canonical correspondence is derived for primitive types (they are already
2327 * deduplicated completely already anyway) or reference types (they rely on
2328 * stability of struct/union canonical relationship for equivalence checks).
2330 static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
2332 __u32 cand_type_id, targ_type_id;
2333 __u16 t_kind, c_kind;
2337 for (i = 0; i < d->hypot_cnt; i++) {
2338 cand_type_id = d->hypot_list[i];
2339 targ_type_id = d->hypot_map[cand_type_id];
2340 t_id = resolve_type_id(d, targ_type_id);
2341 c_id = resolve_type_id(d, cand_type_id);
2342 t_kind = BTF_INFO_KIND(d->btf->types[t_id]->info);
2343 c_kind = BTF_INFO_KIND(d->btf->types[c_id]->info);
2345 * Resolve FWD into STRUCT/UNION.
2346 * It's ok to resolve FWD into STRUCT/UNION that's not yet
2347 * mapped to canonical representative (as opposed to
2348 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
2349 * eventually that struct is going to be mapped and all resolved
2350 * FWDs will automatically resolve to correct canonical
2351 * representative. This will happen before ref type deduping,
2352 * which critically depends on stability of these mapping. This
2353 * stability is not a requirement for STRUCT/UNION equivalence
2356 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
2357 d->map[c_id] = t_id;
2358 else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
2359 d->map[t_id] = c_id;
2361 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
2362 c_kind != BTF_KIND_FWD &&
2363 is_type_mapped(d, c_id) &&
2364 !is_type_mapped(d, t_id)) {
2366 * as a perf optimization, we can map struct/union
2367 * that's part of type graph we just verified for
2368 * equivalence. We can do that for struct/union that has
2369 * canonical representative only, though.
2371 d->map[t_id] = c_id;
2377 * Deduplicate struct/union types.
2379 * For each struct/union type its type signature hash is calculated, taking
2380 * into account type's name, size, number, order and names of fields, but
2381 * ignoring type ID's referenced from fields, because they might not be deduped
2382 * completely until after reference types deduplication phase. This type hash
2383 * is used to iterate over all potential canonical types, sharing same hash.
2384 * For each canonical candidate we check whether type graphs that they form
2385 * (through referenced types in fields and so on) are equivalent using algorithm
2386 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
2387 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
2388 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
2389 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
2390 * potentially map other structs/unions to their canonical representatives,
2391 * if such relationship hasn't yet been established. This speeds up algorithm
2392 * by eliminating some of the duplicate work.
2394 * If no matching canonical representative was found, struct/union is marked
2395 * as canonical for itself and is added into btf_dedup->dedup_table hash map
2396 * for further look ups.
2398 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
2400 struct btf_dedup_node *cand_node;
2401 struct btf_type *cand_type, *t;
2402 /* if we don't find equivalent type, then we are canonical */
2403 __u32 new_id = type_id;
2407 /* already deduped or is in process of deduping (loop detected) */
2408 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2411 t = d->btf->types[type_id];
2412 kind = BTF_INFO_KIND(t->info);
2414 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
2417 h = btf_hash_struct(t);
2418 for_each_dedup_cand(d, h, cand_node) {
2422 * Even though btf_dedup_is_equiv() checks for
2423 * btf_shallow_equal_struct() internally when checking two
2424 * structs (unions) for equivalence, we need to guard here
2425 * from picking matching FWD type as a dedup candidate.
2426 * This can happen due to hash collision. In such case just
2427 * relying on btf_dedup_is_equiv() would lead to potentially
2428 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
2429 * FWD and compatible STRUCT/UNION are considered equivalent.
2431 cand_type = d->btf->types[cand_node->type_id];
2432 if (!btf_shallow_equal_struct(t, cand_type))
2435 btf_dedup_clear_hypot_map(d);
2436 eq = btf_dedup_is_equiv(d, type_id, cand_node->type_id);
2441 new_id = cand_node->type_id;
2442 btf_dedup_merge_hypot_map(d);
2446 d->map[type_id] = new_id;
2447 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2453 static int btf_dedup_struct_types(struct btf_dedup *d)
2457 for (i = 1; i <= d->btf->nr_types; i++) {
2458 err = btf_dedup_struct_type(d, i);
2466 * Deduplicate reference type.
2468 * Once all primitive and struct/union types got deduplicated, we can easily
2469 * deduplicate all other (reference) BTF types. This is done in two steps:
2471 * 1. Resolve all referenced type IDs into their canonical type IDs. This
2472 * resolution can be done either immediately for primitive or struct/union types
2473 * (because they were deduped in previous two phases) or recursively for
2474 * reference types. Recursion will always terminate at either primitive or
2475 * struct/union type, at which point we can "unwind" chain of reference types
2476 * one by one. There is no danger of encountering cycles because in C type
2477 * system the only way to form type cycle is through struct/union, so any chain
2478 * of reference types, even those taking part in a type cycle, will inevitably
2479 * reach struct/union at some point.
2481 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
2482 * becomes "stable", in the sense that no further deduplication will cause
2483 * any changes to it. With that, it's now possible to calculate type's signature
2484 * hash (this time taking into account referenced type IDs) and loop over all
2485 * potential canonical representatives. If no match was found, current type
2486 * will become canonical representative of itself and will be added into
2487 * btf_dedup->dedup_table as another possible canonical representative.
2489 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
2491 struct btf_dedup_node *cand_node;
2492 struct btf_type *t, *cand;
2493 /* if we don't find equivalent type, then we are representative type */
2494 __u32 new_id = type_id;
2498 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
2500 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2501 return resolve_type_id(d, type_id);
2503 t = d->btf->types[type_id];
2504 d->map[type_id] = BTF_IN_PROGRESS_ID;
2506 switch (BTF_INFO_KIND(t->info)) {
2507 case BTF_KIND_CONST:
2508 case BTF_KIND_VOLATILE:
2509 case BTF_KIND_RESTRICT:
2511 case BTF_KIND_TYPEDEF:
2513 ref_type_id = btf_dedup_ref_type(d, t->type);
2514 if (ref_type_id < 0)
2516 t->type = ref_type_id;
2518 h = btf_hash_common(t);
2519 for_each_dedup_cand(d, h, cand_node) {
2520 cand = d->btf->types[cand_node->type_id];
2521 if (btf_equal_common(t, cand)) {
2522 new_id = cand_node->type_id;
2528 case BTF_KIND_ARRAY: {
2529 struct btf_array *info = (struct btf_array *)(t + 1);
2531 ref_type_id = btf_dedup_ref_type(d, info->type);
2532 if (ref_type_id < 0)
2534 info->type = ref_type_id;
2536 ref_type_id = btf_dedup_ref_type(d, info->index_type);
2537 if (ref_type_id < 0)
2539 info->index_type = ref_type_id;
2541 h = btf_hash_array(t);
2542 for_each_dedup_cand(d, h, cand_node) {
2543 cand = d->btf->types[cand_node->type_id];
2544 if (btf_equal_array(t, cand)) {
2545 new_id = cand_node->type_id;
2552 case BTF_KIND_FUNC_PROTO: {
2553 struct btf_param *param;
2557 ref_type_id = btf_dedup_ref_type(d, t->type);
2558 if (ref_type_id < 0)
2560 t->type = ref_type_id;
2562 vlen = BTF_INFO_VLEN(t->info);
2563 param = (struct btf_param *)(t + 1);
2564 for (i = 0; i < vlen; i++) {
2565 ref_type_id = btf_dedup_ref_type(d, param->type);
2566 if (ref_type_id < 0)
2568 param->type = ref_type_id;
2572 h = btf_hash_fnproto(t);
2573 for_each_dedup_cand(d, h, cand_node) {
2574 cand = d->btf->types[cand_node->type_id];
2575 if (btf_equal_fnproto(t, cand)) {
2576 new_id = cand_node->type_id;
2587 d->map[type_id] = new_id;
2588 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2594 static int btf_dedup_ref_types(struct btf_dedup *d)
2598 for (i = 1; i <= d->btf->nr_types; i++) {
2599 err = btf_dedup_ref_type(d, i);
2603 btf_dedup_table_free(d);
2610 * After we established for each type its corresponding canonical representative
2611 * type, we now can eliminate types that are not canonical and leave only
2612 * canonical ones layed out sequentially in memory by copying them over
2613 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
2614 * a map from original type ID to a new compacted type ID, which will be used
2615 * during next phase to "fix up" type IDs, referenced from struct/union and
2618 static int btf_dedup_compact_types(struct btf_dedup *d)
2620 struct btf_type **new_types;
2621 __u32 next_type_id = 1;
2622 char *types_start, *p;
2625 /* we are going to reuse hypot_map to store compaction remapping */
2626 d->hypot_map[0] = 0;
2627 for (i = 1; i <= d->btf->nr_types; i++)
2628 d->hypot_map[i] = BTF_UNPROCESSED_ID;
2630 types_start = d->btf->nohdr_data + d->btf->hdr->type_off;
2633 for (i = 1; i <= d->btf->nr_types; i++) {
2637 len = btf_type_size(d->btf->types[i]);
2641 memmove(p, d->btf->types[i], len);
2642 d->hypot_map[i] = next_type_id;
2643 d->btf->types[next_type_id] = (struct btf_type *)p;
2648 /* shrink struct btf's internal types index and update btf_header */
2649 d->btf->nr_types = next_type_id - 1;
2650 d->btf->types_size = d->btf->nr_types;
2651 d->btf->hdr->type_len = p - types_start;
2652 new_types = realloc(d->btf->types,
2653 (1 + d->btf->nr_types) * sizeof(struct btf_type *));
2656 d->btf->types = new_types;
2658 /* make sure string section follows type information without gaps */
2659 d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data;
2660 memmove(p, d->btf->strings, d->btf->hdr->str_len);
2661 d->btf->strings = p;
2662 p += d->btf->hdr->str_len;
2664 d->btf->data_size = p - (char *)d->btf->data;
2669 * Figure out final (deduplicated and compacted) type ID for provided original
2670 * `type_id` by first resolving it into corresponding canonical type ID and
2671 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
2672 * which is populated during compaction phase.
2674 static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
2676 __u32 resolved_type_id, new_type_id;
2678 resolved_type_id = resolve_type_id(d, type_id);
2679 new_type_id = d->hypot_map[resolved_type_id];
2680 if (new_type_id > BTF_MAX_NR_TYPES)
2686 * Remap referenced type IDs into deduped type IDs.
2688 * After BTF types are deduplicated and compacted, their final type IDs may
2689 * differ from original ones. The map from original to a corresponding
2690 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
2691 * compaction phase. During remapping phase we are rewriting all type IDs
2692 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
2693 * their final deduped type IDs.
2695 static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
2697 struct btf_type *t = d->btf->types[type_id];
2700 switch (BTF_INFO_KIND(t->info)) {
2706 case BTF_KIND_CONST:
2707 case BTF_KIND_VOLATILE:
2708 case BTF_KIND_RESTRICT:
2710 case BTF_KIND_TYPEDEF:
2713 r = btf_dedup_remap_type_id(d, t->type);
2719 case BTF_KIND_ARRAY: {
2720 struct btf_array *arr_info = (struct btf_array *)(t + 1);
2722 r = btf_dedup_remap_type_id(d, arr_info->type);
2726 r = btf_dedup_remap_type_id(d, arr_info->index_type);
2729 arr_info->index_type = r;
2733 case BTF_KIND_STRUCT:
2734 case BTF_KIND_UNION: {
2735 struct btf_member *member = (struct btf_member *)(t + 1);
2736 __u16 vlen = BTF_INFO_VLEN(t->info);
2738 for (i = 0; i < vlen; i++) {
2739 r = btf_dedup_remap_type_id(d, member->type);
2748 case BTF_KIND_FUNC_PROTO: {
2749 struct btf_param *param = (struct btf_param *)(t + 1);
2750 __u16 vlen = BTF_INFO_VLEN(t->info);
2752 r = btf_dedup_remap_type_id(d, t->type);
2757 for (i = 0; i < vlen; i++) {
2758 r = btf_dedup_remap_type_id(d, param->type);
2767 case BTF_KIND_DATASEC: {
2768 struct btf_var_secinfo *var = (struct btf_var_secinfo *)(t + 1);
2769 __u16 vlen = BTF_INFO_VLEN(t->info);
2771 for (i = 0; i < vlen; i++) {
2772 r = btf_dedup_remap_type_id(d, var->type);
2788 static int btf_dedup_remap_types(struct btf_dedup *d)
2792 for (i = 1; i <= d->btf->nr_types; i++) {
2793 r = btf_dedup_remap_type(d, i);