1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
10 #include <linux/err.h>
11 #include <linux/btf.h>
16 #include "libbpf_internal.h"
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 bool btf_check_endianness(const GElf_Ehdr *ehdr)
422 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
423 return ehdr->e_ident[EI_DATA] == ELFDATA2LSB;
424 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
425 return ehdr->e_ident[EI_DATA] == ELFDATA2MSB;
427 # error "Unrecognized __BYTE_ORDER__"
431 struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
433 Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
434 int err = 0, fd = -1, idx = 0;
435 struct btf *btf = NULL;
440 if (elf_version(EV_CURRENT) == EV_NONE) {
441 pr_warning("failed to init libelf for %s\n", path);
442 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
445 fd = open(path, O_RDONLY);
448 pr_warning("failed to open %s: %s\n", path, strerror(errno));
452 err = -LIBBPF_ERRNO__FORMAT;
454 elf = elf_begin(fd, ELF_C_READ, NULL);
456 pr_warning("failed to open %s as ELF file\n", path);
459 if (!gelf_getehdr(elf, &ehdr)) {
460 pr_warning("failed to get EHDR from %s\n", path);
463 if (!btf_check_endianness(&ehdr)) {
464 pr_warning("non-native ELF endianness is not supported\n");
467 if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
468 pr_warning("failed to get e_shstrndx from %s\n", path);
472 while ((scn = elf_nextscn(elf, scn)) != NULL) {
477 if (gelf_getshdr(scn, &sh) != &sh) {
478 pr_warning("failed to get section(%d) header from %s\n",
482 name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
484 pr_warning("failed to get section(%d) name from %s\n",
488 if (strcmp(name, BTF_ELF_SEC) == 0) {
489 btf_data = elf_getdata(scn, 0);
491 pr_warning("failed to get section(%d, %s) data from %s\n",
496 } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
497 btf_ext_data = elf_getdata(scn, 0);
499 pr_warning("failed to get section(%d, %s) data from %s\n",
513 btf = btf__new(btf_data->d_buf, btf_data->d_size);
517 if (btf_ext && btf_ext_data) {
518 *btf_ext = btf_ext__new(btf_ext_data->d_buf,
519 btf_ext_data->d_size);
520 if (IS_ERR(*btf_ext))
522 } else if (btf_ext) {
533 * btf is always parsed before btf_ext, so no need to clean up
534 * btf_ext, if btf loading failed
538 if (btf_ext && IS_ERR(*btf_ext)) {
540 err = PTR_ERR(*btf_ext);
546 static int compare_vsi_off(const void *_a, const void *_b)
548 const struct btf_var_secinfo *a = _a;
549 const struct btf_var_secinfo *b = _b;
551 return a->offset - b->offset;
554 static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
557 __u32 size = 0, off = 0, i, vars = BTF_INFO_VLEN(t->info);
558 const char *name = btf__name_by_offset(btf, t->name_off);
559 const struct btf_type *t_var;
560 struct btf_var_secinfo *vsi;
565 pr_debug("No name found in string section for DATASEC kind.\n");
569 ret = bpf_object__section_size(obj, name, &size);
570 if (ret || !size || (t->size && t->size != size)) {
571 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
577 for (i = 0, vsi = (struct btf_var_secinfo *)(t + 1);
578 i < vars; i++, vsi++) {
579 t_var = btf__type_by_id(btf, vsi->type);
580 var = (struct btf_var *)(t_var + 1);
582 if (BTF_INFO_KIND(t_var->info) != BTF_KIND_VAR) {
583 pr_debug("Non-VAR type seen in section %s\n", name);
587 if (var->linkage == BTF_VAR_STATIC)
590 name = btf__name_by_offset(btf, t_var->name_off);
592 pr_debug("No name found in string section for VAR kind\n");
596 ret = bpf_object__variable_offset(obj, name, &off);
598 pr_debug("No offset found in symbol table for VAR %s\n", name);
605 qsort(t + 1, vars, sizeof(*vsi), compare_vsi_off);
609 int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
614 for (i = 1; i <= btf->nr_types; i++) {
615 struct btf_type *t = btf->types[i];
617 /* Loader needs to fix up some of the things compiler
618 * couldn't get its hands on while emitting BTF. This
619 * is section size and global variable offset. We use
620 * the info from the ELF itself for this purpose.
622 if (BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC) {
623 err = btf_fixup_datasec(obj, btf, t);
632 int btf__load(struct btf *btf)
634 __u32 log_buf_size = BPF_LOG_BUF_SIZE;
635 char *log_buf = NULL;
641 log_buf = malloc(log_buf_size);
647 btf->fd = bpf_load_btf(btf->data, btf->data_size,
648 log_buf, log_buf_size, false);
651 pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
653 pr_warning("%s\n", log_buf);
662 int btf__fd(const struct btf *btf)
667 const void *btf__get_raw_data(const struct btf *btf, __u32 *size)
669 *size = btf->data_size;
673 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
675 if (offset < btf->hdr->str_len)
676 return &btf->strings[offset];
681 int btf__get_from_id(__u32 id, struct btf **btf)
683 struct bpf_btf_info btf_info = { 0 };
684 __u32 len = sizeof(btf_info);
692 btf_fd = bpf_btf_get_fd_by_id(id);
696 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
697 * let's start with a sane default - 4KiB here - and resize it only if
698 * bpf_obj_get_info_by_fd() needs a bigger buffer.
700 btf_info.btf_size = 4096;
701 last_size = btf_info.btf_size;
702 ptr = malloc(last_size);
708 memset(ptr, 0, last_size);
709 btf_info.btf = ptr_to_u64(ptr);
710 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
712 if (!err && btf_info.btf_size > last_size) {
715 last_size = btf_info.btf_size;
716 temp_ptr = realloc(ptr, last_size);
722 memset(ptr, 0, last_size);
723 btf_info.btf = ptr_to_u64(ptr);
724 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
727 if (err || btf_info.btf_size > last_size) {
732 *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
745 int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
746 __u32 expected_key_size, __u32 expected_value_size,
747 __u32 *key_type_id, __u32 *value_type_id)
749 const struct btf_type *container_type;
750 const struct btf_member *key, *value;
751 const size_t max_name = 256;
752 char container_name[max_name];
753 __s64 key_size, value_size;
756 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
758 pr_warning("map:%s length of '____btf_map_%s' is too long\n",
763 container_id = btf__find_by_name(btf, container_name);
764 if (container_id < 0) {
765 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
766 map_name, container_name);
770 container_type = btf__type_by_id(btf, container_id);
771 if (!container_type) {
772 pr_warning("map:%s cannot find BTF type for container_id:%u\n",
773 map_name, container_id);
777 if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT ||
778 BTF_INFO_VLEN(container_type->info) < 2) {
779 pr_warning("map:%s container_name:%s is an invalid container struct\n",
780 map_name, container_name);
784 key = (struct btf_member *)(container_type + 1);
787 key_size = btf__resolve_size(btf, key->type);
789 pr_warning("map:%s invalid BTF key_type_size\n", map_name);
793 if (expected_key_size != key_size) {
794 pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
795 map_name, (__u32)key_size, expected_key_size);
799 value_size = btf__resolve_size(btf, value->type);
800 if (value_size < 0) {
801 pr_warning("map:%s invalid BTF value_type_size\n", map_name);
805 if (expected_value_size != value_size) {
806 pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
807 map_name, (__u32)value_size, expected_value_size);
811 *key_type_id = key->type;
812 *value_type_id = value->type;
817 struct btf_ext_sec_setup_param {
821 struct btf_ext_info *ext_info;
825 static int btf_ext_setup_info(struct btf_ext *btf_ext,
826 struct btf_ext_sec_setup_param *ext_sec)
828 const struct btf_ext_info_sec *sinfo;
829 struct btf_ext_info *ext_info;
830 __u32 info_left, record_size;
831 /* The start of the info sec (including the __u32 record_size). */
834 if (ext_sec->off & 0x03) {
835 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
840 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
841 info_left = ext_sec->len;
843 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
844 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
845 ext_sec->desc, ext_sec->off, ext_sec->len);
849 /* At least a record size */
850 if (info_left < sizeof(__u32)) {
851 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
855 /* The record size needs to meet the minimum standard */
856 record_size = *(__u32 *)info;
857 if (record_size < ext_sec->min_rec_size ||
858 record_size & 0x03) {
859 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
860 ext_sec->desc, record_size);
864 sinfo = info + sizeof(__u32);
865 info_left -= sizeof(__u32);
867 /* If no records, return failure now so .BTF.ext won't be used. */
869 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
874 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
875 __u64 total_record_size;
878 if (info_left < sec_hdrlen) {
879 pr_debug("%s section header is not found in .BTF.ext\n",
884 num_records = sinfo->num_info;
885 if (num_records == 0) {
886 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
891 total_record_size = sec_hdrlen +
892 (__u64)num_records * record_size;
893 if (info_left < total_record_size) {
894 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
899 info_left -= total_record_size;
900 sinfo = (void *)sinfo + total_record_size;
903 ext_info = ext_sec->ext_info;
904 ext_info->len = ext_sec->len - sizeof(__u32);
905 ext_info->rec_size = record_size;
906 ext_info->info = info + sizeof(__u32);
911 static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
913 struct btf_ext_sec_setup_param param = {
914 .off = btf_ext->hdr->func_info_off,
915 .len = btf_ext->hdr->func_info_len,
916 .min_rec_size = sizeof(struct bpf_func_info_min),
917 .ext_info = &btf_ext->func_info,
921 return btf_ext_setup_info(btf_ext, ¶m);
924 static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
926 struct btf_ext_sec_setup_param param = {
927 .off = btf_ext->hdr->line_info_off,
928 .len = btf_ext->hdr->line_info_len,
929 .min_rec_size = sizeof(struct bpf_line_info_min),
930 .ext_info = &btf_ext->line_info,
934 return btf_ext_setup_info(btf_ext, ¶m);
937 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
939 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
941 if (data_size < offsetof(struct btf_ext_header, func_info_off) ||
942 data_size < hdr->hdr_len) {
943 pr_debug("BTF.ext header not found");
947 if (hdr->magic != BTF_MAGIC) {
948 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
952 if (hdr->version != BTF_VERSION) {
953 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
958 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
962 if (data_size == hdr->hdr_len) {
963 pr_debug("BTF.ext has no data\n");
970 void btf_ext__free(struct btf_ext *btf_ext)
978 struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
980 struct btf_ext *btf_ext;
983 err = btf_ext_parse_hdr(data, size);
987 btf_ext = calloc(1, sizeof(struct btf_ext));
989 return ERR_PTR(-ENOMEM);
991 btf_ext->data_size = size;
992 btf_ext->data = malloc(size);
993 if (!btf_ext->data) {
997 memcpy(btf_ext->data, data, size);
999 err = btf_ext_setup_func_info(btf_ext);
1003 err = btf_ext_setup_line_info(btf_ext);
1009 btf_ext__free(btf_ext);
1010 return ERR_PTR(err);
1016 const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
1018 *size = btf_ext->data_size;
1019 return btf_ext->data;
1022 static int btf_ext_reloc_info(const struct btf *btf,
1023 const struct btf_ext_info *ext_info,
1024 const char *sec_name, __u32 insns_cnt,
1025 void **info, __u32 *cnt)
1027 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
1028 __u32 i, record_size, existing_len, records_len;
1029 struct btf_ext_info_sec *sinfo;
1030 const char *info_sec_name;
1034 record_size = ext_info->rec_size;
1035 sinfo = ext_info->info;
1036 remain_len = ext_info->len;
1037 while (remain_len > 0) {
1038 records_len = sinfo->num_info * record_size;
1039 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
1040 if (strcmp(info_sec_name, sec_name)) {
1041 remain_len -= sec_hdrlen + records_len;
1042 sinfo = (void *)sinfo + sec_hdrlen + records_len;
1046 existing_len = (*cnt) * record_size;
1047 data = realloc(*info, existing_len + records_len);
1051 memcpy(data + existing_len, sinfo->data, records_len);
1052 /* adjust insn_off only, the rest data will be passed
1055 for (i = 0; i < sinfo->num_info; i++) {
1058 insn_off = data + existing_len + (i * record_size);
1059 *insn_off = *insn_off / sizeof(struct bpf_insn) +
1063 *cnt += sinfo->num_info;
1070 int btf_ext__reloc_func_info(const struct btf *btf,
1071 const struct btf_ext *btf_ext,
1072 const char *sec_name, __u32 insns_cnt,
1073 void **func_info, __u32 *cnt)
1075 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
1076 insns_cnt, func_info, cnt);
1079 int btf_ext__reloc_line_info(const struct btf *btf,
1080 const struct btf_ext *btf_ext,
1081 const char *sec_name, __u32 insns_cnt,
1082 void **line_info, __u32 *cnt)
1084 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
1085 insns_cnt, line_info, cnt);
1088 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
1090 return btf_ext->func_info.rec_size;
1093 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
1095 return btf_ext->line_info.rec_size;
1100 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1101 const struct btf_dedup_opts *opts);
1102 static void btf_dedup_free(struct btf_dedup *d);
1103 static int btf_dedup_strings(struct btf_dedup *d);
1104 static int btf_dedup_prim_types(struct btf_dedup *d);
1105 static int btf_dedup_struct_types(struct btf_dedup *d);
1106 static int btf_dedup_ref_types(struct btf_dedup *d);
1107 static int btf_dedup_compact_types(struct btf_dedup *d);
1108 static int btf_dedup_remap_types(struct btf_dedup *d);
1111 * Deduplicate BTF types and strings.
1113 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
1114 * section with all BTF type descriptors and string data. It overwrites that
1115 * memory in-place with deduplicated types and strings without any loss of
1116 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
1117 * is provided, all the strings referenced from .BTF.ext section are honored
1118 * and updated to point to the right offsets after deduplication.
1120 * If function returns with error, type/string data might be garbled and should
1123 * More verbose and detailed description of both problem btf_dedup is solving,
1124 * as well as solution could be found at:
1125 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
1127 * Problem description and justification
1128 * =====================================
1130 * BTF type information is typically emitted either as a result of conversion
1131 * from DWARF to BTF or directly by compiler. In both cases, each compilation
1132 * unit contains information about a subset of all the types that are used
1133 * in an application. These subsets are frequently overlapping and contain a lot
1134 * of duplicated information when later concatenated together into a single
1135 * binary. This algorithm ensures that each unique type is represented by single
1136 * BTF type descriptor, greatly reducing resulting size of BTF data.
1138 * Compilation unit isolation and subsequent duplication of data is not the only
1139 * problem. The same type hierarchy (e.g., struct and all the type that struct
1140 * references) in different compilation units can be represented in BTF to
1141 * various degrees of completeness (or, rather, incompleteness) due to
1142 * struct/union forward declarations.
1144 * Let's take a look at an example, that we'll use to better understand the
1145 * problem (and solution). Suppose we have two compilation units, each using
1146 * same `struct S`, but each of them having incomplete type information about
1175 * In case of CU #1, BTF data will know only that `struct B` exist (but no
1176 * more), but will know the complete type information about `struct A`. While
1177 * for CU #2, it will know full type information about `struct B`, but will
1178 * only know about forward declaration of `struct A` (in BTF terms, it will
1179 * have `BTF_KIND_FWD` type descriptor with name `B`).
1181 * This compilation unit isolation means that it's possible that there is no
1182 * single CU with complete type information describing structs `S`, `A`, and
1183 * `B`. Also, we might get tons of duplicated and redundant type information.
1185 * Additional complication we need to keep in mind comes from the fact that
1186 * types, in general, can form graphs containing cycles, not just DAGs.
1188 * While algorithm does deduplication, it also merges and resolves type
1189 * information (unless disabled throught `struct btf_opts`), whenever possible.
1190 * E.g., in the example above with two compilation units having partial type
1191 * information for structs `A` and `B`, the output of algorithm will emit
1192 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
1193 * (as well as type information for `int` and pointers), as if they were defined
1194 * in a single compilation unit as:
1214 * Algorithm completes its work in 6 separate passes:
1216 * 1. Strings deduplication.
1217 * 2. Primitive types deduplication (int, enum, fwd).
1218 * 3. Struct/union types deduplication.
1219 * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
1220 * protos, and const/volatile/restrict modifiers).
1221 * 5. Types compaction.
1222 * 6. Types remapping.
1224 * Algorithm determines canonical type descriptor, which is a single
1225 * representative type for each truly unique type. This canonical type is the
1226 * one that will go into final deduplicated BTF type information. For
1227 * struct/unions, it is also the type that algorithm will merge additional type
1228 * information into (while resolving FWDs), as it discovers it from data in
1229 * other CUs. Each input BTF type eventually gets either mapped to itself, if
1230 * that type is canonical, or to some other type, if that type is equivalent
1231 * and was chosen as canonical representative. This mapping is stored in
1232 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
1233 * FWD type got resolved to.
1235 * To facilitate fast discovery of canonical types, we also maintain canonical
1236 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
1237 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
1238 * that match that signature. With sufficiently good choice of type signature
1239 * hashing function, we can limit number of canonical types for each unique type
1240 * signature to a very small number, allowing to find canonical type for any
1241 * duplicated type very quickly.
1243 * Struct/union deduplication is the most critical part and algorithm for
1244 * deduplicating structs/unions is described in greater details in comments for
1245 * `btf_dedup_is_equiv` function.
1247 int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
1248 const struct btf_dedup_opts *opts)
1250 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
1254 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
1258 err = btf_dedup_strings(d);
1260 pr_debug("btf_dedup_strings failed:%d\n", err);
1263 err = btf_dedup_prim_types(d);
1265 pr_debug("btf_dedup_prim_types failed:%d\n", err);
1268 err = btf_dedup_struct_types(d);
1270 pr_debug("btf_dedup_struct_types failed:%d\n", err);
1273 err = btf_dedup_ref_types(d);
1275 pr_debug("btf_dedup_ref_types failed:%d\n", err);
1278 err = btf_dedup_compact_types(d);
1280 pr_debug("btf_dedup_compact_types failed:%d\n", err);
1283 err = btf_dedup_remap_types(d);
1285 pr_debug("btf_dedup_remap_types failed:%d\n", err);
1294 #define BTF_UNPROCESSED_ID ((__u32)-1)
1295 #define BTF_IN_PROGRESS_ID ((__u32)-2)
1298 /* .BTF section to be deduped in-place */
1301 * Optional .BTF.ext section. When provided, any strings referenced
1302 * from it will be taken into account when deduping strings
1304 struct btf_ext *btf_ext;
1306 * This is a map from any type's signature hash to a list of possible
1307 * canonical representative type candidates. Hash collisions are
1308 * ignored, so even types of various kinds can share same list of
1309 * candidates, which is fine because we rely on subsequent
1310 * btf_xxx_equal() checks to authoritatively verify type equality.
1312 struct hashmap *dedup_table;
1313 /* Canonical types map */
1315 /* Hypothetical mapping, used during type graph equivalence checks */
1320 /* Various option modifying behavior of algorithm */
1321 struct btf_dedup_opts opts;
1324 struct btf_str_ptr {
1330 struct btf_str_ptrs {
1331 struct btf_str_ptr *ptrs;
1337 static long hash_combine(long h, long value)
1339 return h * 31 + value;
1342 #define for_each_dedup_cand(d, node, hash) \
1343 hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
1345 static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
1347 return hashmap__append(d->dedup_table,
1348 (void *)hash, (void *)(long)type_id);
1351 static int btf_dedup_hypot_map_add(struct btf_dedup *d,
1352 __u32 from_id, __u32 to_id)
1354 if (d->hypot_cnt == d->hypot_cap) {
1357 d->hypot_cap += max(16, d->hypot_cap / 2);
1358 new_list = realloc(d->hypot_list, sizeof(__u32) * d->hypot_cap);
1361 d->hypot_list = new_list;
1363 d->hypot_list[d->hypot_cnt++] = from_id;
1364 d->hypot_map[from_id] = to_id;
1368 static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
1372 for (i = 0; i < d->hypot_cnt; i++)
1373 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
1377 static void btf_dedup_free(struct btf_dedup *d)
1379 hashmap__free(d->dedup_table);
1380 d->dedup_table = NULL;
1386 d->hypot_map = NULL;
1388 free(d->hypot_list);
1389 d->hypot_list = NULL;
1394 static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
1399 static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
1404 static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
1409 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
1410 const struct btf_dedup_opts *opts)
1412 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
1413 hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
1417 return ERR_PTR(-ENOMEM);
1419 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
1420 /* dedup_table_size is now used only to force collisions in tests */
1421 if (opts && opts->dedup_table_size == 1)
1422 hash_fn = btf_dedup_collision_hash_fn;
1425 d->btf_ext = btf_ext;
1427 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
1428 if (IS_ERR(d->dedup_table)) {
1429 err = PTR_ERR(d->dedup_table);
1430 d->dedup_table = NULL;
1434 d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1439 /* special BTF "void" type is made canonical immediately */
1441 for (i = 1; i <= btf->nr_types; i++) {
1442 struct btf_type *t = d->btf->types[i];
1443 __u16 kind = BTF_INFO_KIND(t->info);
1445 /* VAR and DATASEC are never deduped and are self-canonical */
1446 if (kind == BTF_KIND_VAR || kind == BTF_KIND_DATASEC)
1449 d->map[i] = BTF_UNPROCESSED_ID;
1452 d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
1453 if (!d->hypot_map) {
1457 for (i = 0; i <= btf->nr_types; i++)
1458 d->hypot_map[i] = BTF_UNPROCESSED_ID;
1463 return ERR_PTR(err);
1469 typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
1472 * Iterate over all possible places in .BTF and .BTF.ext that can reference
1473 * string and pass pointer to it to a provided callback `fn`.
1475 static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
1477 void *line_data_cur, *line_data_end;
1478 int i, j, r, rec_size;
1481 for (i = 1; i <= d->btf->nr_types; i++) {
1482 t = d->btf->types[i];
1483 r = fn(&t->name_off, ctx);
1487 switch (BTF_INFO_KIND(t->info)) {
1488 case BTF_KIND_STRUCT:
1489 case BTF_KIND_UNION: {
1490 struct btf_member *m = (struct btf_member *)(t + 1);
1491 __u16 vlen = BTF_INFO_VLEN(t->info);
1493 for (j = 0; j < vlen; j++) {
1494 r = fn(&m->name_off, ctx);
1501 case BTF_KIND_ENUM: {
1502 struct btf_enum *m = (struct btf_enum *)(t + 1);
1503 __u16 vlen = BTF_INFO_VLEN(t->info);
1505 for (j = 0; j < vlen; j++) {
1506 r = fn(&m->name_off, ctx);
1513 case BTF_KIND_FUNC_PROTO: {
1514 struct btf_param *m = (struct btf_param *)(t + 1);
1515 __u16 vlen = BTF_INFO_VLEN(t->info);
1517 for (j = 0; j < vlen; j++) {
1518 r = fn(&m->name_off, ctx);
1533 line_data_cur = d->btf_ext->line_info.info;
1534 line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
1535 rec_size = d->btf_ext->line_info.rec_size;
1537 while (line_data_cur < line_data_end) {
1538 struct btf_ext_info_sec *sec = line_data_cur;
1539 struct bpf_line_info_min *line_info;
1540 __u32 num_info = sec->num_info;
1542 r = fn(&sec->sec_name_off, ctx);
1546 line_data_cur += sizeof(struct btf_ext_info_sec);
1547 for (i = 0; i < num_info; i++) {
1548 line_info = line_data_cur;
1549 r = fn(&line_info->file_name_off, ctx);
1552 r = fn(&line_info->line_off, ctx);
1555 line_data_cur += rec_size;
1562 static int str_sort_by_content(const void *a1, const void *a2)
1564 const struct btf_str_ptr *p1 = a1;
1565 const struct btf_str_ptr *p2 = a2;
1567 return strcmp(p1->str, p2->str);
1570 static int str_sort_by_offset(const void *a1, const void *a2)
1572 const struct btf_str_ptr *p1 = a1;
1573 const struct btf_str_ptr *p2 = a2;
1575 if (p1->str != p2->str)
1576 return p1->str < p2->str ? -1 : 1;
1580 static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
1582 const struct btf_str_ptr *p = pelem;
1584 if (str_ptr != p->str)
1585 return (const char *)str_ptr < p->str ? -1 : 1;
1589 static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
1591 struct btf_str_ptrs *strs;
1592 struct btf_str_ptr *s;
1594 if (*str_off_ptr == 0)
1598 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1599 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1606 static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
1608 struct btf_str_ptrs *strs;
1609 struct btf_str_ptr *s;
1611 if (*str_off_ptr == 0)
1615 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
1616 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
1619 *str_off_ptr = s->new_off;
1624 * Dedup string and filter out those that are not referenced from either .BTF
1625 * or .BTF.ext (if provided) sections.
1627 * This is done by building index of all strings in BTF's string section,
1628 * then iterating over all entities that can reference strings (e.g., type
1629 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
1630 * strings as used. After that all used strings are deduped and compacted into
1631 * sequential blob of memory and new offsets are calculated. Then all the string
1632 * references are iterated again and rewritten using new offsets.
1634 static int btf_dedup_strings(struct btf_dedup *d)
1636 const struct btf_header *hdr = d->btf->hdr;
1637 char *start = (char *)d->btf->nohdr_data + hdr->str_off;
1638 char *end = start + d->btf->hdr->str_len;
1639 char *p = start, *tmp_strs = NULL;
1640 struct btf_str_ptrs strs = {
1646 int i, j, err = 0, grp_idx;
1649 /* build index of all strings */
1651 if (strs.cnt + 1 > strs.cap) {
1652 struct btf_str_ptr *new_ptrs;
1654 strs.cap += max(strs.cnt / 2, 16);
1655 new_ptrs = realloc(strs.ptrs,
1656 sizeof(strs.ptrs[0]) * strs.cap);
1661 strs.ptrs = new_ptrs;
1664 strs.ptrs[strs.cnt].str = p;
1665 strs.ptrs[strs.cnt].used = false;
1671 /* temporary storage for deduplicated strings */
1672 tmp_strs = malloc(d->btf->hdr->str_len);
1678 /* mark all used strings */
1679 strs.ptrs[0].used = true;
1680 err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
1684 /* sort strings by context, so that we can identify duplicates */
1685 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
1688 * iterate groups of equal strings and if any instance in a group was
1689 * referenced, emit single instance and remember new offset
1693 grp_used = strs.ptrs[0].used;
1694 /* iterate past end to avoid code duplication after loop */
1695 for (i = 1; i <= strs.cnt; i++) {
1697 * when i == strs.cnt, we want to skip string comparison and go
1698 * straight to handling last group of strings (otherwise we'd
1699 * need to handle last group after the loop w/ duplicated code)
1702 !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
1703 grp_used = grp_used || strs.ptrs[i].used;
1708 * this check would have been required after the loop to handle
1709 * last group of strings, but due to <= condition in a loop
1710 * we avoid that duplication
1713 int new_off = p - tmp_strs;
1714 __u32 len = strlen(strs.ptrs[grp_idx].str);
1716 memmove(p, strs.ptrs[grp_idx].str, len + 1);
1717 for (j = grp_idx; j < i; j++)
1718 strs.ptrs[j].new_off = new_off;
1724 grp_used = strs.ptrs[i].used;
1728 /* replace original strings with deduped ones */
1729 d->btf->hdr->str_len = p - tmp_strs;
1730 memmove(start, tmp_strs, d->btf->hdr->str_len);
1731 end = start + d->btf->hdr->str_len;
1733 /* restore original order for further binary search lookups */
1734 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
1736 /* remap string offsets */
1737 err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
1741 d->btf->hdr->str_len = end - start;
1749 static long btf_hash_common(struct btf_type *t)
1753 h = hash_combine(0, t->name_off);
1754 h = hash_combine(h, t->info);
1755 h = hash_combine(h, t->size);
1759 static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
1761 return t1->name_off == t2->name_off &&
1762 t1->info == t2->info &&
1763 t1->size == t2->size;
1766 /* Calculate type signature hash of INT. */
1767 static long btf_hash_int(struct btf_type *t)
1769 __u32 info = *(__u32 *)(t + 1);
1772 h = btf_hash_common(t);
1773 h = hash_combine(h, info);
1777 /* Check structural equality of two INTs. */
1778 static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
1782 if (!btf_equal_common(t1, t2))
1784 info1 = *(__u32 *)(t1 + 1);
1785 info2 = *(__u32 *)(t2 + 1);
1786 return info1 == info2;
1789 /* Calculate type signature hash of ENUM. */
1790 static long btf_hash_enum(struct btf_type *t)
1794 /* don't hash vlen and enum members to support enum fwd resolving */
1795 h = hash_combine(0, t->name_off);
1796 h = hash_combine(h, t->info & ~0xffff);
1797 h = hash_combine(h, t->size);
1801 /* Check structural equality of two ENUMs. */
1802 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
1804 struct btf_enum *m1, *m2;
1808 if (!btf_equal_common(t1, t2))
1811 vlen = BTF_INFO_VLEN(t1->info);
1812 m1 = (struct btf_enum *)(t1 + 1);
1813 m2 = (struct btf_enum *)(t2 + 1);
1814 for (i = 0; i < vlen; i++) {
1815 if (m1->name_off != m2->name_off || m1->val != m2->val)
1823 static inline bool btf_is_enum_fwd(struct btf_type *t)
1825 return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM &&
1826 BTF_INFO_VLEN(t->info) == 0;
1829 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
1831 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
1832 return btf_equal_enum(t1, t2);
1833 /* ignore vlen when comparing */
1834 return t1->name_off == t2->name_off &&
1835 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
1836 t1->size == t2->size;
1840 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
1841 * as referenced type IDs equivalence is established separately during type
1842 * graph equivalence check algorithm.
1844 static long btf_hash_struct(struct btf_type *t)
1846 struct btf_member *member = (struct btf_member *)(t + 1);
1847 __u32 vlen = BTF_INFO_VLEN(t->info);
1848 long h = btf_hash_common(t);
1851 for (i = 0; i < vlen; i++) {
1852 h = hash_combine(h, member->name_off);
1853 h = hash_combine(h, member->offset);
1854 /* no hashing of referenced type ID, it can be unresolved yet */
1861 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1862 * IDs. This check is performed during type graph equivalence check and
1863 * referenced types equivalence is checked separately.
1865 static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
1867 struct btf_member *m1, *m2;
1871 if (!btf_equal_common(t1, t2))
1874 vlen = BTF_INFO_VLEN(t1->info);
1875 m1 = (struct btf_member *)(t1 + 1);
1876 m2 = (struct btf_member *)(t2 + 1);
1877 for (i = 0; i < vlen; i++) {
1878 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
1887 * Calculate type signature hash of ARRAY, including referenced type IDs,
1888 * under assumption that they were already resolved to canonical type IDs and
1889 * are not going to change.
1891 static long btf_hash_array(struct btf_type *t)
1893 struct btf_array *info = (struct btf_array *)(t + 1);
1894 long h = btf_hash_common(t);
1896 h = hash_combine(h, info->type);
1897 h = hash_combine(h, info->index_type);
1898 h = hash_combine(h, info->nelems);
1903 * Check exact equality of two ARRAYs, taking into account referenced
1904 * type IDs, under assumption that they were already resolved to canonical
1905 * type IDs and are not going to change.
1906 * This function is called during reference types deduplication to compare
1907 * ARRAY to potential canonical representative.
1909 static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
1911 struct btf_array *info1, *info2;
1913 if (!btf_equal_common(t1, t2))
1916 info1 = (struct btf_array *)(t1 + 1);
1917 info2 = (struct btf_array *)(t2 + 1);
1918 return info1->type == info2->type &&
1919 info1->index_type == info2->index_type &&
1920 info1->nelems == info2->nelems;
1924 * Check structural compatibility of two ARRAYs, ignoring referenced type
1925 * IDs. This check is performed during type graph equivalence check and
1926 * referenced types equivalence is checked separately.
1928 static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
1930 struct btf_array *info1, *info2;
1932 if (!btf_equal_common(t1, t2))
1935 info1 = (struct btf_array *)(t1 + 1);
1936 info2 = (struct btf_array *)(t2 + 1);
1937 return info1->nelems == info2->nelems;
1941 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
1942 * under assumption that they were already resolved to canonical type IDs and
1943 * are not going to change.
1945 static long btf_hash_fnproto(struct btf_type *t)
1947 struct btf_param *member = (struct btf_param *)(t + 1);
1948 __u16 vlen = BTF_INFO_VLEN(t->info);
1949 long h = btf_hash_common(t);
1952 for (i = 0; i < vlen; i++) {
1953 h = hash_combine(h, member->name_off);
1954 h = hash_combine(h, member->type);
1961 * Check exact equality of two FUNC_PROTOs, taking into account referenced
1962 * type IDs, under assumption that they were already resolved to canonical
1963 * type IDs and are not going to change.
1964 * This function is called during reference types deduplication to compare
1965 * FUNC_PROTO to potential canonical representative.
1967 static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
1969 struct btf_param *m1, *m2;
1973 if (!btf_equal_common(t1, t2))
1976 vlen = BTF_INFO_VLEN(t1->info);
1977 m1 = (struct btf_param *)(t1 + 1);
1978 m2 = (struct btf_param *)(t2 + 1);
1979 for (i = 0; i < vlen; i++) {
1980 if (m1->name_off != m2->name_off || m1->type != m2->type)
1989 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
1990 * IDs. This check is performed during type graph equivalence check and
1991 * referenced types equivalence is checked separately.
1993 static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
1995 struct btf_param *m1, *m2;
1999 /* skip return type ID */
2000 if (t1->name_off != t2->name_off || t1->info != t2->info)
2003 vlen = BTF_INFO_VLEN(t1->info);
2004 m1 = (struct btf_param *)(t1 + 1);
2005 m2 = (struct btf_param *)(t2 + 1);
2006 for (i = 0; i < vlen; i++) {
2007 if (m1->name_off != m2->name_off)
2016 * Deduplicate primitive types, that can't reference other types, by calculating
2017 * their type signature hash and comparing them with any possible canonical
2018 * candidate. If no canonical candidate matches, type itself is marked as
2019 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
2021 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
2023 struct btf_type *t = d->btf->types[type_id];
2024 struct hashmap_entry *hash_entry;
2025 struct btf_type *cand;
2026 /* if we don't find equivalent type, then we are canonical */
2027 __u32 new_id = type_id;
2031 switch (BTF_INFO_KIND(t->info)) {
2032 case BTF_KIND_CONST:
2033 case BTF_KIND_VOLATILE:
2034 case BTF_KIND_RESTRICT:
2036 case BTF_KIND_TYPEDEF:
2037 case BTF_KIND_ARRAY:
2038 case BTF_KIND_STRUCT:
2039 case BTF_KIND_UNION:
2041 case BTF_KIND_FUNC_PROTO:
2043 case BTF_KIND_DATASEC:
2047 h = btf_hash_int(t);
2048 for_each_dedup_cand(d, hash_entry, h) {
2049 cand_id = (__u32)(long)hash_entry->value;
2050 cand = d->btf->types[cand_id];
2051 if (btf_equal_int(t, cand)) {
2059 h = btf_hash_enum(t);
2060 for_each_dedup_cand(d, hash_entry, h) {
2061 cand_id = (__u32)(long)hash_entry->value;
2062 cand = d->btf->types[cand_id];
2063 if (btf_equal_enum(t, cand)) {
2067 if (d->opts.dont_resolve_fwds)
2069 if (btf_compat_enum(t, cand)) {
2070 if (btf_is_enum_fwd(t)) {
2071 /* resolve fwd to full enum */
2075 /* resolve canonical enum fwd to full enum */
2076 d->map[cand_id] = type_id;
2082 h = btf_hash_common(t);
2083 for_each_dedup_cand(d, hash_entry, h) {
2084 cand_id = (__u32)(long)hash_entry->value;
2085 cand = d->btf->types[cand_id];
2086 if (btf_equal_common(t, cand)) {
2097 d->map[type_id] = new_id;
2098 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2104 static int btf_dedup_prim_types(struct btf_dedup *d)
2108 for (i = 1; i <= d->btf->nr_types; i++) {
2109 err = btf_dedup_prim_type(d, i);
2117 * Check whether type is already mapped into canonical one (could be to itself).
2119 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
2121 return d->map[type_id] <= BTF_MAX_NR_TYPES;
2125 * Resolve type ID into its canonical type ID, if any; otherwise return original
2126 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
2127 * STRUCT/UNION link and resolve it into canonical type ID as well.
2129 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
2131 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2132 type_id = d->map[type_id];
2137 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
2140 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
2142 __u32 orig_type_id = type_id;
2144 if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2147 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
2148 type_id = d->map[type_id];
2150 if (BTF_INFO_KIND(d->btf->types[type_id]->info) != BTF_KIND_FWD)
2153 return orig_type_id;
2157 static inline __u16 btf_fwd_kind(struct btf_type *t)
2159 return BTF_INFO_KFLAG(t->info) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
2163 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
2164 * call it "candidate graph" in this description for brevity) to a type graph
2165 * formed by (potential) canonical struct/union ("canonical graph" for brevity
2166 * here, though keep in mind that not all types in canonical graph are
2167 * necessarily canonical representatives themselves, some of them might be
2168 * duplicates or its uniqueness might not have been established yet).
2170 * - >0, if type graphs are equivalent;
2171 * - 0, if not equivalent;
2174 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
2175 * equivalence of BTF types at each step. If at any point BTF types in candidate
2176 * and canonical graphs are not compatible structurally, whole graphs are
2177 * incompatible. If types are structurally equivalent (i.e., all information
2178 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
2179 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
2180 * If a type references other types, then those referenced types are checked
2181 * for equivalence recursively.
2183 * During DFS traversal, if we find that for current `canon_id` type we
2184 * already have some mapping in hypothetical map, we check for two possible
2186 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
2187 * happen when type graphs have cycles. In this case we assume those two
2188 * types are equivalent.
2189 * - `canon_id` is mapped to different type. This is contradiction in our
2190 * hypothetical mapping, because same graph in canonical graph corresponds
2191 * to two different types in candidate graph, which for equivalent type
2192 * graphs shouldn't happen. This condition terminates equivalence check
2193 * with negative result.
2195 * If type graphs traversal exhausts types to check and find no contradiction,
2196 * then type graphs are equivalent.
2198 * When checking types for equivalence, there is one special case: FWD types.
2199 * If FWD type resolution is allowed and one of the types (either from canonical
2200 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
2201 * flag) and their names match, hypothetical mapping is updated to point from
2202 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
2203 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
2205 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
2206 * if there are two exactly named (or anonymous) structs/unions that are
2207 * compatible structurally, one of which has FWD field, while other is concrete
2208 * STRUCT/UNION, but according to C sources they are different structs/unions
2209 * that are referencing different types with the same name. This is extremely
2210 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
2211 * this logic is causing problems.
2213 * Doing FWD resolution means that both candidate and/or canonical graphs can
2214 * consists of portions of the graph that come from multiple compilation units.
2215 * This is due to the fact that types within single compilation unit are always
2216 * deduplicated and FWDs are already resolved, if referenced struct/union
2217 * definiton is available. So, if we had unresolved FWD and found corresponding
2218 * STRUCT/UNION, they will be from different compilation units. This
2219 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
2220 * type graph will likely have at least two different BTF types that describe
2221 * same type (e.g., most probably there will be two different BTF types for the
2222 * same 'int' primitive type) and could even have "overlapping" parts of type
2223 * graph that describe same subset of types.
2225 * This in turn means that our assumption that each type in canonical graph
2226 * must correspond to exactly one type in candidate graph might not hold
2227 * anymore and will make it harder to detect contradictions using hypothetical
2228 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
2229 * resolution only in canonical graph. FWDs in candidate graphs are never
2230 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
2232 * - Both types in canonical and candidate graphs are FWDs. If they are
2233 * structurally equivalent, then they can either be both resolved to the
2234 * same STRUCT/UNION or not resolved at all. In both cases they are
2235 * equivalent and there is no need to resolve FWD on candidate side.
2236 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
2237 * so nothing to resolve as well, algorithm will check equivalence anyway.
2238 * - Type in canonical graph is FWD, while type in candidate is concrete
2239 * STRUCT/UNION. In this case candidate graph comes from single compilation
2240 * unit, so there is exactly one BTF type for each unique C type. After
2241 * resolving FWD into STRUCT/UNION, there might be more than one BTF type
2242 * in canonical graph mapping to single BTF type in candidate graph, but
2243 * because hypothetical mapping maps from canonical to candidate types, it's
2244 * alright, and we still maintain the property of having single `canon_id`
2245 * mapping to single `cand_id` (there could be two different `canon_id`
2246 * mapped to the same `cand_id`, but it's not contradictory).
2247 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
2248 * graph is FWD. In this case we are just going to check compatibility of
2249 * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
2250 * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
2251 * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
2252 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
2255 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
2258 struct btf_type *cand_type;
2259 struct btf_type *canon_type;
2260 __u32 hypot_type_id;
2265 /* if both resolve to the same canonical, they must be equivalent */
2266 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
2269 canon_id = resolve_fwd_id(d, canon_id);
2271 hypot_type_id = d->hypot_map[canon_id];
2272 if (hypot_type_id <= BTF_MAX_NR_TYPES)
2273 return hypot_type_id == cand_id;
2275 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
2278 cand_type = d->btf->types[cand_id];
2279 canon_type = d->btf->types[canon_id];
2280 cand_kind = BTF_INFO_KIND(cand_type->info);
2281 canon_kind = BTF_INFO_KIND(canon_type->info);
2283 if (cand_type->name_off != canon_type->name_off)
2286 /* FWD <--> STRUCT/UNION equivalence check, if enabled */
2287 if (!d->opts.dont_resolve_fwds
2288 && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
2289 && cand_kind != canon_kind) {
2293 if (cand_kind == BTF_KIND_FWD) {
2294 real_kind = canon_kind;
2295 fwd_kind = btf_fwd_kind(cand_type);
2297 real_kind = cand_kind;
2298 fwd_kind = btf_fwd_kind(canon_type);
2300 return fwd_kind == real_kind;
2303 if (cand_kind != canon_kind)
2306 switch (cand_kind) {
2308 return btf_equal_int(cand_type, canon_type);
2311 if (d->opts.dont_resolve_fwds)
2312 return btf_equal_enum(cand_type, canon_type);
2314 return btf_compat_enum(cand_type, canon_type);
2317 return btf_equal_common(cand_type, canon_type);
2319 case BTF_KIND_CONST:
2320 case BTF_KIND_VOLATILE:
2321 case BTF_KIND_RESTRICT:
2323 case BTF_KIND_TYPEDEF:
2325 if (cand_type->info != canon_type->info)
2327 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2329 case BTF_KIND_ARRAY: {
2330 struct btf_array *cand_arr, *canon_arr;
2332 if (!btf_compat_array(cand_type, canon_type))
2334 cand_arr = (struct btf_array *)(cand_type + 1);
2335 canon_arr = (struct btf_array *)(canon_type + 1);
2336 eq = btf_dedup_is_equiv(d,
2337 cand_arr->index_type, canon_arr->index_type);
2340 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
2343 case BTF_KIND_STRUCT:
2344 case BTF_KIND_UNION: {
2345 struct btf_member *cand_m, *canon_m;
2348 if (!btf_shallow_equal_struct(cand_type, canon_type))
2350 vlen = BTF_INFO_VLEN(cand_type->info);
2351 cand_m = (struct btf_member *)(cand_type + 1);
2352 canon_m = (struct btf_member *)(canon_type + 1);
2353 for (i = 0; i < vlen; i++) {
2354 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
2364 case BTF_KIND_FUNC_PROTO: {
2365 struct btf_param *cand_p, *canon_p;
2368 if (!btf_compat_fnproto(cand_type, canon_type))
2370 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
2373 vlen = BTF_INFO_VLEN(cand_type->info);
2374 cand_p = (struct btf_param *)(cand_type + 1);
2375 canon_p = (struct btf_param *)(canon_type + 1);
2376 for (i = 0; i < vlen; i++) {
2377 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
2393 * Use hypothetical mapping, produced by successful type graph equivalence
2394 * check, to augment existing struct/union canonical mapping, where possible.
2396 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
2397 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
2398 * it doesn't matter if FWD type was part of canonical graph or candidate one,
2399 * we are recording the mapping anyway. As opposed to carefulness required
2400 * for struct/union correspondence mapping (described below), for FWD resolution
2401 * it's not important, as by the time that FWD type (reference type) will be
2402 * deduplicated all structs/unions will be deduped already anyway.
2404 * Recording STRUCT/UNION mapping is purely a performance optimization and is
2405 * not required for correctness. It needs to be done carefully to ensure that
2406 * struct/union from candidate's type graph is not mapped into corresponding
2407 * struct/union from canonical type graph that itself hasn't been resolved into
2408 * canonical representative. The only guarantee we have is that canonical
2409 * struct/union was determined as canonical and that won't change. But any
2410 * types referenced through that struct/union fields could have been not yet
2411 * resolved, so in case like that it's too early to establish any kind of
2412 * correspondence between structs/unions.
2414 * No canonical correspondence is derived for primitive types (they are already
2415 * deduplicated completely already anyway) or reference types (they rely on
2416 * stability of struct/union canonical relationship for equivalence checks).
2418 static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
2420 __u32 cand_type_id, targ_type_id;
2421 __u16 t_kind, c_kind;
2425 for (i = 0; i < d->hypot_cnt; i++) {
2426 cand_type_id = d->hypot_list[i];
2427 targ_type_id = d->hypot_map[cand_type_id];
2428 t_id = resolve_type_id(d, targ_type_id);
2429 c_id = resolve_type_id(d, cand_type_id);
2430 t_kind = BTF_INFO_KIND(d->btf->types[t_id]->info);
2431 c_kind = BTF_INFO_KIND(d->btf->types[c_id]->info);
2433 * Resolve FWD into STRUCT/UNION.
2434 * It's ok to resolve FWD into STRUCT/UNION that's not yet
2435 * mapped to canonical representative (as opposed to
2436 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
2437 * eventually that struct is going to be mapped and all resolved
2438 * FWDs will automatically resolve to correct canonical
2439 * representative. This will happen before ref type deduping,
2440 * which critically depends on stability of these mapping. This
2441 * stability is not a requirement for STRUCT/UNION equivalence
2444 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
2445 d->map[c_id] = t_id;
2446 else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
2447 d->map[t_id] = c_id;
2449 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
2450 c_kind != BTF_KIND_FWD &&
2451 is_type_mapped(d, c_id) &&
2452 !is_type_mapped(d, t_id)) {
2454 * as a perf optimization, we can map struct/union
2455 * that's part of type graph we just verified for
2456 * equivalence. We can do that for struct/union that has
2457 * canonical representative only, though.
2459 d->map[t_id] = c_id;
2465 * Deduplicate struct/union types.
2467 * For each struct/union type its type signature hash is calculated, taking
2468 * into account type's name, size, number, order and names of fields, but
2469 * ignoring type ID's referenced from fields, because they might not be deduped
2470 * completely until after reference types deduplication phase. This type hash
2471 * is used to iterate over all potential canonical types, sharing same hash.
2472 * For each canonical candidate we check whether type graphs that they form
2473 * (through referenced types in fields and so on) are equivalent using algorithm
2474 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
2475 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
2476 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
2477 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
2478 * potentially map other structs/unions to their canonical representatives,
2479 * if such relationship hasn't yet been established. This speeds up algorithm
2480 * by eliminating some of the duplicate work.
2482 * If no matching canonical representative was found, struct/union is marked
2483 * as canonical for itself and is added into btf_dedup->dedup_table hash map
2484 * for further look ups.
2486 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
2488 struct btf_type *cand_type, *t;
2489 struct hashmap_entry *hash_entry;
2490 /* if we don't find equivalent type, then we are canonical */
2491 __u32 new_id = type_id;
2495 /* already deduped or is in process of deduping (loop detected) */
2496 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2499 t = d->btf->types[type_id];
2500 kind = BTF_INFO_KIND(t->info);
2502 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
2505 h = btf_hash_struct(t);
2506 for_each_dedup_cand(d, hash_entry, h) {
2507 __u32 cand_id = (__u32)(long)hash_entry->value;
2511 * Even though btf_dedup_is_equiv() checks for
2512 * btf_shallow_equal_struct() internally when checking two
2513 * structs (unions) for equivalence, we need to guard here
2514 * from picking matching FWD type as a dedup candidate.
2515 * This can happen due to hash collision. In such case just
2516 * relying on btf_dedup_is_equiv() would lead to potentially
2517 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
2518 * FWD and compatible STRUCT/UNION are considered equivalent.
2520 cand_type = d->btf->types[cand_id];
2521 if (!btf_shallow_equal_struct(t, cand_type))
2524 btf_dedup_clear_hypot_map(d);
2525 eq = btf_dedup_is_equiv(d, type_id, cand_id);
2531 btf_dedup_merge_hypot_map(d);
2535 d->map[type_id] = new_id;
2536 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2542 static int btf_dedup_struct_types(struct btf_dedup *d)
2546 for (i = 1; i <= d->btf->nr_types; i++) {
2547 err = btf_dedup_struct_type(d, i);
2555 * Deduplicate reference type.
2557 * Once all primitive and struct/union types got deduplicated, we can easily
2558 * deduplicate all other (reference) BTF types. This is done in two steps:
2560 * 1. Resolve all referenced type IDs into their canonical type IDs. This
2561 * resolution can be done either immediately for primitive or struct/union types
2562 * (because they were deduped in previous two phases) or recursively for
2563 * reference types. Recursion will always terminate at either primitive or
2564 * struct/union type, at which point we can "unwind" chain of reference types
2565 * one by one. There is no danger of encountering cycles because in C type
2566 * system the only way to form type cycle is through struct/union, so any chain
2567 * of reference types, even those taking part in a type cycle, will inevitably
2568 * reach struct/union at some point.
2570 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
2571 * becomes "stable", in the sense that no further deduplication will cause
2572 * any changes to it. With that, it's now possible to calculate type's signature
2573 * hash (this time taking into account referenced type IDs) and loop over all
2574 * potential canonical representatives. If no match was found, current type
2575 * will become canonical representative of itself and will be added into
2576 * btf_dedup->dedup_table as another possible canonical representative.
2578 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
2580 struct hashmap_entry *hash_entry;
2581 __u32 new_id = type_id, cand_id;
2582 struct btf_type *t, *cand;
2583 /* if we don't find equivalent type, then we are representative type */
2587 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
2589 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
2590 return resolve_type_id(d, type_id);
2592 t = d->btf->types[type_id];
2593 d->map[type_id] = BTF_IN_PROGRESS_ID;
2595 switch (BTF_INFO_KIND(t->info)) {
2596 case BTF_KIND_CONST:
2597 case BTF_KIND_VOLATILE:
2598 case BTF_KIND_RESTRICT:
2600 case BTF_KIND_TYPEDEF:
2602 ref_type_id = btf_dedup_ref_type(d, t->type);
2603 if (ref_type_id < 0)
2605 t->type = ref_type_id;
2607 h = btf_hash_common(t);
2608 for_each_dedup_cand(d, hash_entry, h) {
2609 cand_id = (__u32)(long)hash_entry->value;
2610 cand = d->btf->types[cand_id];
2611 if (btf_equal_common(t, cand)) {
2618 case BTF_KIND_ARRAY: {
2619 struct btf_array *info = (struct btf_array *)(t + 1);
2621 ref_type_id = btf_dedup_ref_type(d, info->type);
2622 if (ref_type_id < 0)
2624 info->type = ref_type_id;
2626 ref_type_id = btf_dedup_ref_type(d, info->index_type);
2627 if (ref_type_id < 0)
2629 info->index_type = ref_type_id;
2631 h = btf_hash_array(t);
2632 for_each_dedup_cand(d, hash_entry, h) {
2633 cand_id = (__u32)(long)hash_entry->value;
2634 cand = d->btf->types[cand_id];
2635 if (btf_equal_array(t, cand)) {
2643 case BTF_KIND_FUNC_PROTO: {
2644 struct btf_param *param;
2648 ref_type_id = btf_dedup_ref_type(d, t->type);
2649 if (ref_type_id < 0)
2651 t->type = ref_type_id;
2653 vlen = BTF_INFO_VLEN(t->info);
2654 param = (struct btf_param *)(t + 1);
2655 for (i = 0; i < vlen; i++) {
2656 ref_type_id = btf_dedup_ref_type(d, param->type);
2657 if (ref_type_id < 0)
2659 param->type = ref_type_id;
2663 h = btf_hash_fnproto(t);
2664 for_each_dedup_cand(d, hash_entry, h) {
2665 cand_id = (__u32)(long)hash_entry->value;
2666 cand = d->btf->types[cand_id];
2667 if (btf_equal_fnproto(t, cand)) {
2679 d->map[type_id] = new_id;
2680 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
2686 static int btf_dedup_ref_types(struct btf_dedup *d)
2690 for (i = 1; i <= d->btf->nr_types; i++) {
2691 err = btf_dedup_ref_type(d, i);
2695 /* we won't need d->dedup_table anymore */
2696 hashmap__free(d->dedup_table);
2697 d->dedup_table = NULL;
2704 * After we established for each type its corresponding canonical representative
2705 * type, we now can eliminate types that are not canonical and leave only
2706 * canonical ones layed out sequentially in memory by copying them over
2707 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
2708 * a map from original type ID to a new compacted type ID, which will be used
2709 * during next phase to "fix up" type IDs, referenced from struct/union and
2712 static int btf_dedup_compact_types(struct btf_dedup *d)
2714 struct btf_type **new_types;
2715 __u32 next_type_id = 1;
2716 char *types_start, *p;
2719 /* we are going to reuse hypot_map to store compaction remapping */
2720 d->hypot_map[0] = 0;
2721 for (i = 1; i <= d->btf->nr_types; i++)
2722 d->hypot_map[i] = BTF_UNPROCESSED_ID;
2724 types_start = d->btf->nohdr_data + d->btf->hdr->type_off;
2727 for (i = 1; i <= d->btf->nr_types; i++) {
2731 len = btf_type_size(d->btf->types[i]);
2735 memmove(p, d->btf->types[i], len);
2736 d->hypot_map[i] = next_type_id;
2737 d->btf->types[next_type_id] = (struct btf_type *)p;
2742 /* shrink struct btf's internal types index and update btf_header */
2743 d->btf->nr_types = next_type_id - 1;
2744 d->btf->types_size = d->btf->nr_types;
2745 d->btf->hdr->type_len = p - types_start;
2746 new_types = realloc(d->btf->types,
2747 (1 + d->btf->nr_types) * sizeof(struct btf_type *));
2750 d->btf->types = new_types;
2752 /* make sure string section follows type information without gaps */
2753 d->btf->hdr->str_off = p - (char *)d->btf->nohdr_data;
2754 memmove(p, d->btf->strings, d->btf->hdr->str_len);
2755 d->btf->strings = p;
2756 p += d->btf->hdr->str_len;
2758 d->btf->data_size = p - (char *)d->btf->data;
2763 * Figure out final (deduplicated and compacted) type ID for provided original
2764 * `type_id` by first resolving it into corresponding canonical type ID and
2765 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
2766 * which is populated during compaction phase.
2768 static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
2770 __u32 resolved_type_id, new_type_id;
2772 resolved_type_id = resolve_type_id(d, type_id);
2773 new_type_id = d->hypot_map[resolved_type_id];
2774 if (new_type_id > BTF_MAX_NR_TYPES)
2780 * Remap referenced type IDs into deduped type IDs.
2782 * After BTF types are deduplicated and compacted, their final type IDs may
2783 * differ from original ones. The map from original to a corresponding
2784 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
2785 * compaction phase. During remapping phase we are rewriting all type IDs
2786 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
2787 * their final deduped type IDs.
2789 static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
2791 struct btf_type *t = d->btf->types[type_id];
2794 switch (BTF_INFO_KIND(t->info)) {
2800 case BTF_KIND_CONST:
2801 case BTF_KIND_VOLATILE:
2802 case BTF_KIND_RESTRICT:
2804 case BTF_KIND_TYPEDEF:
2807 r = btf_dedup_remap_type_id(d, t->type);
2813 case BTF_KIND_ARRAY: {
2814 struct btf_array *arr_info = (struct btf_array *)(t + 1);
2816 r = btf_dedup_remap_type_id(d, arr_info->type);
2820 r = btf_dedup_remap_type_id(d, arr_info->index_type);
2823 arr_info->index_type = r;
2827 case BTF_KIND_STRUCT:
2828 case BTF_KIND_UNION: {
2829 struct btf_member *member = (struct btf_member *)(t + 1);
2830 __u16 vlen = BTF_INFO_VLEN(t->info);
2832 for (i = 0; i < vlen; i++) {
2833 r = btf_dedup_remap_type_id(d, member->type);
2842 case BTF_KIND_FUNC_PROTO: {
2843 struct btf_param *param = (struct btf_param *)(t + 1);
2844 __u16 vlen = BTF_INFO_VLEN(t->info);
2846 r = btf_dedup_remap_type_id(d, t->type);
2851 for (i = 0; i < vlen; i++) {
2852 r = btf_dedup_remap_type_id(d, param->type);
2861 case BTF_KIND_DATASEC: {
2862 struct btf_var_secinfo *var = (struct btf_var_secinfo *)(t + 1);
2863 __u16 vlen = BTF_INFO_VLEN(t->info);
2865 for (i = 0; i < vlen; i++) {
2866 r = btf_dedup_remap_type_id(d, var->type);
2882 static int btf_dedup_remap_types(struct btf_dedup *d)
2886 for (i = 1; i <= d->btf->nr_types; i++) {
2887 r = btf_dedup_remap_type(d, i);