1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
12 #include <sys/utsname.h>
13 #include <sys/param.h>
15 #include <linux/kernel.h>
16 #include <linux/err.h>
17 #include <linux/btf.h>
22 #include "libbpf_internal.h"
25 #define BTF_MAX_NR_TYPES 0x7fffffffU
26 #define BTF_MAX_STR_OFFSET 0x7fffffffU
28 static struct btf_type btf_void;
31 /* raw BTF data in native endianness */
33 /* raw BTF data in non-native endianness */
34 void *raw_data_swapped;
36 /* whether target endianness differs from the native one */
40 * When BTF is loaded from an ELF or raw memory it is stored
41 * in a contiguous memory block. The hdr, type_data, and, strs_data
42 * point inside that memory region to their respective parts of BTF
45 * +--------------------------------+
46 * | Header | Types | Strings |
47 * +--------------------------------+
52 * strs_data------------+
54 * If BTF data is later modified, e.g., due to types added or
55 * removed, BTF deduplication performed, etc, this contiguous
56 * representation is broken up into three independently allocated
57 * memory regions to be able to modify them independently.
58 * raw_data is nulled out at that point, but can be later allocated
59 * and cached again if user calls btf__get_raw_data(), at which point
60 * raw_data will contain a contiguous copy of header, types, and
63 * +----------+ +---------+ +-----------+
64 * | Header | | Types | | Strings |
65 * +----------+ +---------+ +-----------+
70 * strs_data------------------+
72 * +----------+---------+-----------+
73 * | Header | Types | Strings |
74 * raw_data----->+----------+---------+-----------+
76 struct btf_header *hdr;
79 size_t types_data_cap; /* used size stored in hdr->type_len */
81 /* type ID to `struct btf_type *` lookup index */
87 size_t strs_data_cap; /* used size stored in hdr->str_len */
89 /* lookup index for each unique string in strings section */
90 struct hashmap *strs_hash;
91 /* whether strings are already deduplicated */
93 /* BTF object FD, if loaded into kernel */
96 /* Pointer size (in bytes) for a target architecture of this BTF */
100 static inline __u64 ptr_to_u64(const void *ptr)
102 return (__u64) (unsigned long) ptr;
105 /* Ensure given dynamically allocated memory region pointed to by *data* with
106 * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
107 * memory to accomodate *add_cnt* new elements, assuming *cur_cnt* elements
108 * are already used. At most *max_cnt* elements can be ever allocated.
109 * If necessary, memory is reallocated and all existing data is copied over,
110 * new pointer to the memory region is stored at *data, new memory region
111 * capacity (in number of elements) is stored in *cap.
112 * On success, memory pointer to the beginning of unused memory is returned.
113 * On error, NULL is returned.
115 void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
116 size_t cur_cnt, size_t max_cnt, size_t add_cnt)
121 if (cur_cnt + add_cnt <= *cap_cnt)
122 return *data + cur_cnt * elem_sz;
124 /* requested more than the set limit */
125 if (cur_cnt + add_cnt > max_cnt)
129 new_cnt += new_cnt / 4; /* expand by 25% */
130 if (new_cnt < 16) /* but at least 16 elements */
132 if (new_cnt > max_cnt) /* but not exceeding a set limit */
134 if (new_cnt < cur_cnt + add_cnt) /* also ensure we have enough memory */
135 new_cnt = cur_cnt + add_cnt;
137 new_data = libbpf_reallocarray(*data, new_cnt, elem_sz);
141 /* zero out newly allocated portion of memory */
142 memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz);
146 return new_data + cur_cnt * elem_sz;
149 /* Ensure given dynamically allocated memory region has enough allocated space
150 * to accommodate *need_cnt* elements of size *elem_sz* bytes each
152 int btf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt)
156 if (need_cnt <= *cap_cnt)
159 p = btf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt);
166 static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
170 p = btf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32),
171 btf->nr_types + 1, BTF_MAX_NR_TYPES, 1);
179 static void btf_bswap_hdr(struct btf_header *h)
181 h->magic = bswap_16(h->magic);
182 h->hdr_len = bswap_32(h->hdr_len);
183 h->type_off = bswap_32(h->type_off);
184 h->type_len = bswap_32(h->type_len);
185 h->str_off = bswap_32(h->str_off);
186 h->str_len = bswap_32(h->str_len);
189 static int btf_parse_hdr(struct btf *btf)
191 struct btf_header *hdr = btf->hdr;
194 if (btf->raw_size < sizeof(struct btf_header)) {
195 pr_debug("BTF header not found\n");
199 if (hdr->magic == bswap_16(BTF_MAGIC)) {
200 btf->swapped_endian = true;
201 if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
202 pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
203 bswap_32(hdr->hdr_len));
207 } else if (hdr->magic != BTF_MAGIC) {
208 pr_debug("Invalid BTF magic: %x\n", hdr->magic);
212 if (btf->raw_size < hdr->hdr_len) {
213 pr_debug("BTF header len %u larger than data size %u\n",
214 hdr->hdr_len, btf->raw_size);
218 meta_left = btf->raw_size - hdr->hdr_len;
219 if (meta_left < (long long)hdr->str_off + hdr->str_len) {
220 pr_debug("Invalid BTF total size: %u\n", btf->raw_size);
224 if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) {
225 pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
226 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
230 if (hdr->type_off % 4) {
231 pr_debug("BTF type section is not aligned to 4 bytes\n");
238 static int btf_parse_str_sec(struct btf *btf)
240 const struct btf_header *hdr = btf->hdr;
241 const char *start = btf->strs_data;
242 const char *end = start + btf->hdr->str_len;
244 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET ||
245 start[0] || end[-1]) {
246 pr_debug("Invalid BTF string section\n");
253 static int btf_type_size(const struct btf_type *t)
255 const int base_size = sizeof(struct btf_type);
256 __u16 vlen = btf_vlen(t);
258 switch (btf_kind(t)) {
261 case BTF_KIND_VOLATILE:
262 case BTF_KIND_RESTRICT:
264 case BTF_KIND_TYPEDEF:
268 return base_size + sizeof(__u32);
270 return base_size + vlen * sizeof(struct btf_enum);
272 return base_size + sizeof(struct btf_array);
273 case BTF_KIND_STRUCT:
275 return base_size + vlen * sizeof(struct btf_member);
276 case BTF_KIND_FUNC_PROTO:
277 return base_size + vlen * sizeof(struct btf_param);
279 return base_size + sizeof(struct btf_var);
280 case BTF_KIND_DATASEC:
281 return base_size + vlen * sizeof(struct btf_var_secinfo);
283 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
288 static void btf_bswap_type_base(struct btf_type *t)
290 t->name_off = bswap_32(t->name_off);
291 t->info = bswap_32(t->info);
292 t->type = bswap_32(t->type);
295 static int btf_bswap_type_rest(struct btf_type *t)
297 struct btf_var_secinfo *v;
298 struct btf_member *m;
302 __u16 vlen = btf_vlen(t);
305 switch (btf_kind(t)) {
308 case BTF_KIND_VOLATILE:
309 case BTF_KIND_RESTRICT:
311 case BTF_KIND_TYPEDEF:
315 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
318 for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
319 e->name_off = bswap_32(e->name_off);
320 e->val = bswap_32(e->val);
325 a->type = bswap_32(a->type);
326 a->index_type = bswap_32(a->index_type);
327 a->nelems = bswap_32(a->nelems);
329 case BTF_KIND_STRUCT:
331 for (i = 0, m = btf_members(t); i < vlen; i++, m++) {
332 m->name_off = bswap_32(m->name_off);
333 m->type = bswap_32(m->type);
334 m->offset = bswap_32(m->offset);
337 case BTF_KIND_FUNC_PROTO:
338 for (i = 0, p = btf_params(t); i < vlen; i++, p++) {
339 p->name_off = bswap_32(p->name_off);
340 p->type = bswap_32(p->type);
344 btf_var(t)->linkage = bswap_32(btf_var(t)->linkage);
346 case BTF_KIND_DATASEC:
347 for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) {
348 v->type = bswap_32(v->type);
349 v->offset = bswap_32(v->offset);
350 v->size = bswap_32(v->size);
354 pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
359 static int btf_parse_type_sec(struct btf *btf)
361 struct btf_header *hdr = btf->hdr;
362 void *next_type = btf->types_data;
363 void *end_type = next_type + hdr->type_len;
364 int err, i = 0, type_size;
366 /* VOID (type_id == 0) is specially handled by btf__get_type_by_id(),
367 * so ensure we can never properly use its offset from index by
368 * setting it to a large value
370 err = btf_add_type_idx_entry(btf, UINT_MAX);
374 while (next_type + sizeof(struct btf_type) <= end_type) {
377 if (btf->swapped_endian)
378 btf_bswap_type_base(next_type);
380 type_size = btf_type_size(next_type);
383 if (next_type + type_size > end_type) {
384 pr_warn("BTF type [%d] is malformed\n", i);
388 if (btf->swapped_endian && btf_bswap_type_rest(next_type))
391 err = btf_add_type_idx_entry(btf, next_type - btf->types_data);
395 next_type += type_size;
399 if (next_type != end_type) {
400 pr_warn("BTF types data is malformed\n");
407 __u32 btf__get_nr_types(const struct btf *btf)
409 return btf->nr_types;
412 /* internal helper returning non-const pointer to a type */
413 static struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id)
418 return btf->types_data + btf->type_offs[type_id];
421 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
423 if (type_id > btf->nr_types)
425 return btf_type_by_id((struct btf *)btf, type_id);
428 static int determine_ptr_size(const struct btf *btf)
430 const struct btf_type *t;
434 for (i = 1; i <= btf->nr_types; i++) {
435 t = btf__type_by_id(btf, i);
439 name = btf__name_by_offset(btf, t->name_off);
443 if (strcmp(name, "long int") == 0 ||
444 strcmp(name, "long unsigned int") == 0) {
445 if (t->size != 4 && t->size != 8)
454 static size_t btf_ptr_sz(const struct btf *btf)
457 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
458 return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz;
461 /* Return pointer size this BTF instance assumes. The size is heuristically
462 * determined by looking for 'long' or 'unsigned long' integer type and
463 * recording its size in bytes. If BTF type information doesn't have any such
464 * type, this function returns 0. In the latter case, native architecture's
465 * pointer size is assumed, so will be either 4 or 8, depending on
466 * architecture that libbpf was compiled for. It's possible to override
467 * guessed value by using btf__set_pointer_size() API.
469 size_t btf__pointer_size(const struct btf *btf)
472 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
475 /* not enough BTF type info to guess */
481 /* Override or set pointer size in bytes. Only values of 4 and 8 are
484 int btf__set_pointer_size(struct btf *btf, size_t ptr_sz)
486 if (ptr_sz != 4 && ptr_sz != 8)
488 btf->ptr_sz = ptr_sz;
492 static bool is_host_big_endian(void)
494 #if __BYTE_ORDER == __LITTLE_ENDIAN
496 #elif __BYTE_ORDER == __BIG_ENDIAN
499 # error "Unrecognized __BYTE_ORDER__"
503 enum btf_endianness btf__endianness(const struct btf *btf)
505 if (is_host_big_endian())
506 return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN;
508 return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
511 int btf__set_endianness(struct btf *btf, enum btf_endianness endian)
513 if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN)
516 btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN);
517 if (!btf->swapped_endian) {
518 free(btf->raw_data_swapped);
519 btf->raw_data_swapped = NULL;
524 static bool btf_type_is_void(const struct btf_type *t)
526 return t == &btf_void || btf_is_fwd(t);
529 static bool btf_type_is_void_or_null(const struct btf_type *t)
531 return !t || btf_type_is_void(t);
534 #define MAX_RESOLVE_DEPTH 32
536 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
538 const struct btf_array *array;
539 const struct btf_type *t;
544 t = btf__type_by_id(btf, type_id);
545 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
547 switch (btf_kind(t)) {
549 case BTF_KIND_STRUCT:
552 case BTF_KIND_DATASEC:
556 size = btf_ptr_sz(btf);
558 case BTF_KIND_TYPEDEF:
559 case BTF_KIND_VOLATILE:
561 case BTF_KIND_RESTRICT:
566 array = btf_array(t);
567 if (nelems && array->nelems > UINT32_MAX / nelems)
569 nelems *= array->nelems;
570 type_id = array->type;
576 t = btf__type_by_id(btf, type_id);
582 if (nelems && size > UINT32_MAX / nelems)
585 return nelems * size;
588 int btf__align_of(const struct btf *btf, __u32 id)
590 const struct btf_type *t = btf__type_by_id(btf, id);
591 __u16 kind = btf_kind(t);
596 return min(btf_ptr_sz(btf), (size_t)t->size);
598 return btf_ptr_sz(btf);
599 case BTF_KIND_TYPEDEF:
600 case BTF_KIND_VOLATILE:
602 case BTF_KIND_RESTRICT:
603 return btf__align_of(btf, t->type);
605 return btf__align_of(btf, btf_array(t)->type);
606 case BTF_KIND_STRUCT:
607 case BTF_KIND_UNION: {
608 const struct btf_member *m = btf_members(t);
609 __u16 vlen = btf_vlen(t);
610 int i, max_align = 1, align;
612 for (i = 0; i < vlen; i++, m++) {
613 align = btf__align_of(btf, m->type);
616 max_align = max(max_align, align);
618 /* if field offset isn't aligned according to field
619 * type's alignment, then struct must be packed
621 if (btf_member_bitfield_size(t, i) == 0 &&
622 (m->offset % (8 * align)) != 0)
626 /* if struct/union size isn't a multiple of its alignment,
627 * then struct must be packed
629 if ((t->size % max_align) != 0)
635 pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
640 int btf__resolve_type(const struct btf *btf, __u32 type_id)
642 const struct btf_type *t;
645 t = btf__type_by_id(btf, type_id);
646 while (depth < MAX_RESOLVE_DEPTH &&
647 !btf_type_is_void_or_null(t) &&
648 (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
650 t = btf__type_by_id(btf, type_id);
654 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
660 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
664 if (!strcmp(type_name, "void"))
667 for (i = 1; i <= btf->nr_types; i++) {
668 const struct btf_type *t = btf__type_by_id(btf, i);
669 const char *name = btf__name_by_offset(btf, t->name_off);
671 if (name && !strcmp(type_name, name))
678 __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
683 if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
686 for (i = 1; i <= btf->nr_types; i++) {
687 const struct btf_type *t = btf__type_by_id(btf, i);
690 if (btf_kind(t) != kind)
692 name = btf__name_by_offset(btf, t->name_off);
693 if (name && !strcmp(type_name, name))
700 static bool btf_is_modifiable(const struct btf *btf)
702 return (void *)btf->hdr != btf->raw_data;
705 void btf__free(struct btf *btf)
707 if (IS_ERR_OR_NULL(btf))
713 if (btf_is_modifiable(btf)) {
714 /* if BTF was modified after loading, it will have a split
715 * in-memory representation for header, types, and strings
716 * sections, so we need to free all of them individually. It
717 * might still have a cached contiguous raw data present,
718 * which will be unconditionally freed below.
721 free(btf->types_data);
722 free(btf->strs_data);
725 free(btf->raw_data_swapped);
726 free(btf->type_offs);
730 struct btf *btf__new_empty(void)
734 btf = calloc(1, sizeof(*btf));
736 return ERR_PTR(-ENOMEM);
739 btf->ptr_sz = sizeof(void *);
740 btf->swapped_endian = false;
742 /* +1 for empty string at offset 0 */
743 btf->raw_size = sizeof(struct btf_header) + 1;
744 btf->raw_data = calloc(1, btf->raw_size);
745 if (!btf->raw_data) {
747 return ERR_PTR(-ENOMEM);
750 btf->hdr = btf->raw_data;
751 btf->hdr->hdr_len = sizeof(struct btf_header);
752 btf->hdr->magic = BTF_MAGIC;
753 btf->hdr->version = BTF_VERSION;
755 btf->types_data = btf->raw_data + btf->hdr->hdr_len;
756 btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
757 btf->hdr->str_len = 1; /* empty string at offset 0 */
762 struct btf *btf__new(const void *data, __u32 size)
767 btf = calloc(1, sizeof(struct btf));
769 return ERR_PTR(-ENOMEM);
771 btf->raw_data = malloc(size);
772 if (!btf->raw_data) {
776 memcpy(btf->raw_data, data, size);
777 btf->raw_size = size;
779 btf->hdr = btf->raw_data;
780 err = btf_parse_hdr(btf);
784 btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
785 btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
787 err = btf_parse_str_sec(btf);
788 err = err ?: btf_parse_type_sec(btf);
803 struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
805 Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
806 int err = 0, fd = -1, idx = 0;
807 struct btf *btf = NULL;
812 if (elf_version(EV_CURRENT) == EV_NONE) {
813 pr_warn("failed to init libelf for %s\n", path);
814 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
817 fd = open(path, O_RDONLY);
820 pr_warn("failed to open %s: %s\n", path, strerror(errno));
824 err = -LIBBPF_ERRNO__FORMAT;
826 elf = elf_begin(fd, ELF_C_READ, NULL);
828 pr_warn("failed to open %s as ELF file\n", path);
831 if (!gelf_getehdr(elf, &ehdr)) {
832 pr_warn("failed to get EHDR from %s\n", path);
835 if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
836 pr_warn("failed to get e_shstrndx from %s\n", path);
840 while ((scn = elf_nextscn(elf, scn)) != NULL) {
845 if (gelf_getshdr(scn, &sh) != &sh) {
846 pr_warn("failed to get section(%d) header from %s\n",
850 name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
852 pr_warn("failed to get section(%d) name from %s\n",
856 if (strcmp(name, BTF_ELF_SEC) == 0) {
857 btf_data = elf_getdata(scn, 0);
859 pr_warn("failed to get section(%d, %s) data from %s\n",
864 } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
865 btf_ext_data = elf_getdata(scn, 0);
867 pr_warn("failed to get section(%d, %s) data from %s\n",
881 btf = btf__new(btf_data->d_buf, btf_data->d_size);
885 switch (gelf_getclass(elf)) {
887 btf__set_pointer_size(btf, 4);
890 btf__set_pointer_size(btf, 8);
893 pr_warn("failed to get ELF class (bitness) for %s\n", path);
897 if (btf_ext && btf_ext_data) {
898 *btf_ext = btf_ext__new(btf_ext_data->d_buf,
899 btf_ext_data->d_size);
900 if (IS_ERR(*btf_ext))
902 } else if (btf_ext) {
913 * btf is always parsed before btf_ext, so no need to clean up
914 * btf_ext, if btf loading failed
918 if (btf_ext && IS_ERR(*btf_ext)) {
920 err = PTR_ERR(*btf_ext);
926 struct btf *btf__parse_raw(const char *path)
928 struct btf *btf = NULL;
935 f = fopen(path, "rb");
941 /* check BTF magic */
942 if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
946 if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) {
947 /* definitely not a raw BTF */
953 if (fseek(f, 0, SEEK_END)) {
962 /* rewind to the start */
963 if (fseek(f, 0, SEEK_SET)) {
968 /* pre-alloc memory and read all of BTF data */
974 if (fread(data, 1, sz, f) < sz) {
979 /* finally parse BTF data */
980 btf = btf__new(data, sz);
986 return err ? ERR_PTR(err) : btf;
989 struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
996 btf = btf__parse_raw(path);
997 if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
1000 return btf__parse_elf(path, btf_ext);
1003 static int compare_vsi_off(const void *_a, const void *_b)
1005 const struct btf_var_secinfo *a = _a;
1006 const struct btf_var_secinfo *b = _b;
1008 return a->offset - b->offset;
1011 static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
1014 __u32 size = 0, off = 0, i, vars = btf_vlen(t);
1015 const char *name = btf__name_by_offset(btf, t->name_off);
1016 const struct btf_type *t_var;
1017 struct btf_var_secinfo *vsi;
1018 const struct btf_var *var;
1022 pr_debug("No name found in string section for DATASEC kind.\n");
1026 /* .extern datasec size and var offsets were set correctly during
1027 * extern collection step, so just skip straight to sorting variables
1032 ret = bpf_object__section_size(obj, name, &size);
1033 if (ret || !size || (t->size && t->size != size)) {
1034 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
1040 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
1041 t_var = btf__type_by_id(btf, vsi->type);
1042 var = btf_var(t_var);
1044 if (!btf_is_var(t_var)) {
1045 pr_debug("Non-VAR type seen in section %s\n", name);
1049 if (var->linkage == BTF_VAR_STATIC)
1052 name = btf__name_by_offset(btf, t_var->name_off);
1054 pr_debug("No name found in string section for VAR kind\n");
1058 ret = bpf_object__variable_offset(obj, name, &off);
1060 pr_debug("No offset found in symbol table for VAR %s\n",
1069 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
1073 int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
1078 for (i = 1; i <= btf->nr_types; i++) {
1079 struct btf_type *t = btf_type_by_id(btf, i);
1081 /* Loader needs to fix up some of the things compiler
1082 * couldn't get its hands on while emitting BTF. This
1083 * is section size and global variable offset. We use
1084 * the info from the ELF itself for this purpose.
1086 if (btf_is_datasec(t)) {
1087 err = btf_fixup_datasec(obj, btf, t);
1096 static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
1098 int btf__load(struct btf *btf)
1100 __u32 log_buf_size = 0, raw_size;
1101 char *log_buf = NULL;
1110 log_buf = malloc(log_buf_size);
1117 raw_data = btf_get_raw_data(btf, &raw_size, false);
1122 /* cache native raw data representation */
1123 btf->raw_size = raw_size;
1124 btf->raw_data = raw_data;
1126 btf->fd = bpf_load_btf(raw_data, raw_size, log_buf, log_buf_size, false);
1128 if (!log_buf || errno == ENOSPC) {
1129 log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
1136 pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
1138 pr_warn("%s\n", log_buf);
1147 int btf__fd(const struct btf *btf)
1152 void btf__set_fd(struct btf *btf, int fd)
1157 static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
1159 struct btf_header *hdr = btf->hdr;
1165 data = swap_endian ? btf->raw_data_swapped : btf->raw_data;
1167 *size = btf->raw_size;
1171 data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
1172 data = calloc(1, data_sz);
1177 memcpy(p, hdr, hdr->hdr_len);
1182 memcpy(p, btf->types_data, hdr->type_len);
1184 for (i = 1; i <= btf->nr_types; i++) {
1185 t = p + btf->type_offs[i];
1186 /* btf_bswap_type_rest() relies on native t->info, so
1187 * we swap base type info after we swapped all the
1188 * additional information
1190 if (btf_bswap_type_rest(t))
1192 btf_bswap_type_base(t);
1197 memcpy(p, btf->strs_data, hdr->str_len);
1207 const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
1209 struct btf *btf = (struct btf *)btf_ro;
1213 data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
1217 btf->raw_size = data_sz;
1218 if (btf->swapped_endian)
1219 btf->raw_data_swapped = data;
1221 btf->raw_data = data;
1226 const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
1228 if (offset < btf->hdr->str_len)
1229 return btf->strs_data + offset;
1234 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
1236 return btf__str_by_offset(btf, offset);
1239 int btf__get_from_id(__u32 id, struct btf **btf)
1241 struct bpf_btf_info btf_info = { 0 };
1242 __u32 len = sizeof(btf_info);
1250 btf_fd = bpf_btf_get_fd_by_id(id);
1254 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
1255 * let's start with a sane default - 4KiB here - and resize it only if
1256 * bpf_obj_get_info_by_fd() needs a bigger buffer.
1258 btf_info.btf_size = 4096;
1259 last_size = btf_info.btf_size;
1260 ptr = malloc(last_size);
1266 memset(ptr, 0, last_size);
1267 btf_info.btf = ptr_to_u64(ptr);
1268 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1270 if (!err && btf_info.btf_size > last_size) {
1273 last_size = btf_info.btf_size;
1274 temp_ptr = realloc(ptr, last_size);
1280 memset(ptr, 0, last_size);
1281 btf_info.btf = ptr_to_u64(ptr);
1282 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1285 if (err || btf_info.btf_size > last_size) {
1290 *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
1292 err = PTR_ERR(*btf);
1303 int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
1304 __u32 expected_key_size, __u32 expected_value_size,
1305 __u32 *key_type_id, __u32 *value_type_id)
1307 const struct btf_type *container_type;
1308 const struct btf_member *key, *value;
1309 const size_t max_name = 256;
1310 char container_name[max_name];
1311 __s64 key_size, value_size;
1314 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
1316 pr_warn("map:%s length of '____btf_map_%s' is too long\n",
1317 map_name, map_name);
1321 container_id = btf__find_by_name(btf, container_name);
1322 if (container_id < 0) {
1323 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1324 map_name, container_name);
1325 return container_id;
1328 container_type = btf__type_by_id(btf, container_id);
1329 if (!container_type) {
1330 pr_warn("map:%s cannot find BTF type for container_id:%u\n",
1331 map_name, container_id);
1335 if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
1336 pr_warn("map:%s container_name:%s is an invalid container struct\n",
1337 map_name, container_name);
1341 key = btf_members(container_type);
1344 key_size = btf__resolve_size(btf, key->type);
1346 pr_warn("map:%s invalid BTF key_type_size\n", map_name);
1350 if (expected_key_size != key_size) {
1351 pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1352 map_name, (__u32)key_size, expected_key_size);
1356 value_size = btf__resolve_size(btf, value->type);
1357 if (value_size < 0) {
1358 pr_warn("map:%s invalid BTF value_type_size\n", map_name);
1362 if (expected_value_size != value_size) {
1363 pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1364 map_name, (__u32)value_size, expected_value_size);
1368 *key_type_id = key->type;
1369 *value_type_id = value->type;
1374 static size_t strs_hash_fn(const void *key, void *ctx)
1376 struct btf *btf = ctx;
1377 const char *str = btf->strs_data + (long)key;
1379 return str_hash(str);
1382 static bool strs_hash_equal_fn(const void *key1, const void *key2, void *ctx)
1384 struct btf *btf = ctx;
1385 const char *str1 = btf->strs_data + (long)key1;
1386 const char *str2 = btf->strs_data + (long)key2;
1388 return strcmp(str1, str2) == 0;
1391 static void btf_invalidate_raw_data(struct btf *btf)
1393 if (btf->raw_data) {
1394 free(btf->raw_data);
1395 btf->raw_data = NULL;
1397 if (btf->raw_data_swapped) {
1398 free(btf->raw_data_swapped);
1399 btf->raw_data_swapped = NULL;
1403 /* Ensure BTF is ready to be modified (by splitting into a three memory
1404 * regions for header, types, and strings). Also invalidate cached
1407 static int btf_ensure_modifiable(struct btf *btf)
1409 void *hdr, *types, *strs, *strs_end, *s;
1410 struct hashmap *hash = NULL;
1414 if (btf_is_modifiable(btf)) {
1415 /* any BTF modification invalidates raw_data */
1416 btf_invalidate_raw_data(btf);
1420 /* split raw data into three memory regions */
1421 hdr = malloc(btf->hdr->hdr_len);
1422 types = malloc(btf->hdr->type_len);
1423 strs = malloc(btf->hdr->str_len);
1424 if (!hdr || !types || !strs)
1427 memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
1428 memcpy(types, btf->types_data, btf->hdr->type_len);
1429 memcpy(strs, btf->strs_data, btf->hdr->str_len);
1431 /* build lookup index for all strings */
1432 hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, btf);
1434 err = PTR_ERR(hash);
1439 strs_end = strs + btf->hdr->str_len;
1440 for (off = 0, s = strs; s < strs_end; off += strlen(s) + 1, s = strs + off) {
1441 /* hashmap__add() returns EEXIST if string with the same
1442 * content already is in the hash map
1444 err = hashmap__add(hash, (void *)off, (void *)off);
1446 continue; /* duplicate */
1451 /* only when everything was successful, update internal state */
1453 btf->types_data = types;
1454 btf->types_data_cap = btf->hdr->type_len;
1455 btf->strs_data = strs;
1456 btf->strs_data_cap = btf->hdr->str_len;
1457 btf->strs_hash = hash;
1458 /* if BTF was created from scratch, all strings are guaranteed to be
1459 * unique and deduplicated
1461 btf->strs_deduped = btf->hdr->str_len <= 1;
1463 /* invalidate raw_data representation */
1464 btf_invalidate_raw_data(btf);
1469 hashmap__free(hash);
1476 static void *btf_add_str_mem(struct btf *btf, size_t add_sz)
1478 return btf_add_mem(&btf->strs_data, &btf->strs_data_cap, 1,
1479 btf->hdr->str_len, BTF_MAX_STR_OFFSET, add_sz);
1482 /* Find an offset in BTF string section that corresponds to a given string *s*.
1484 * - >0 offset into string section, if string is found;
1485 * - -ENOENT, if string is not in the string section;
1486 * - <0, on any other error.
1488 int btf__find_str(struct btf *btf, const char *s)
1490 long old_off, new_off, len;
1493 /* BTF needs to be in a modifiable state to build string lookup index */
1494 if (btf_ensure_modifiable(btf))
1497 /* see btf__add_str() for why we do this */
1498 len = strlen(s) + 1;
1499 p = btf_add_str_mem(btf, len);
1503 new_off = btf->hdr->str_len;
1506 if (hashmap__find(btf->strs_hash, (void *)new_off, (void **)&old_off))
1512 /* Add a string s to the BTF string section.
1514 * - > 0 offset into string section, on success;
1517 int btf__add_str(struct btf *btf, const char *s)
1519 long old_off, new_off, len;
1523 if (btf_ensure_modifiable(btf))
1526 /* Hashmap keys are always offsets within btf->strs_data, so to even
1527 * look up some string from the "outside", we need to first append it
1528 * at the end, so that it can be addressed with an offset. Luckily,
1529 * until btf->hdr->str_len is incremented, that string is just a piece
1530 * of garbage for the rest of BTF code, so no harm, no foul. On the
1531 * other hand, if the string is unique, it's already appended and
1532 * ready to be used, only a simple btf->hdr->str_len increment away.
1534 len = strlen(s) + 1;
1535 p = btf_add_str_mem(btf, len);
1539 new_off = btf->hdr->str_len;
1542 /* Now attempt to add the string, but only if the string with the same
1543 * contents doesn't exist already (HASHMAP_ADD strategy). If such
1544 * string exists, we'll get its offset in old_off (that's old_key).
1546 err = hashmap__insert(btf->strs_hash, (void *)new_off, (void *)new_off,
1547 HASHMAP_ADD, (const void **)&old_off, NULL);
1549 return old_off; /* duplicated string, return existing offset */
1553 btf->hdr->str_len += len; /* new unique string, adjust data length */
1557 static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
1559 return btf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
1560 btf->hdr->type_len, UINT_MAX, add_sz);
1563 static __u32 btf_type_info(int kind, int vlen, int kflag)
1565 return (kflag << 31) | (kind << 24) | vlen;
1568 static void btf_type_inc_vlen(struct btf_type *t)
1570 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
1574 * Append new BTF_KIND_INT type with:
1575 * - *name* - non-empty, non-NULL type name;
1576 * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
1577 * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
1579 * - >0, type ID of newly added BTF type;
1582 int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding)
1585 int sz, err, name_off;
1587 /* non-empty name */
1588 if (!name || !name[0])
1590 /* byte_sz must be power of 2 */
1591 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16)
1593 if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL))
1596 /* deconstruct BTF, if necessary, and invalidate raw_data */
1597 if (btf_ensure_modifiable(btf))
1600 sz = sizeof(struct btf_type) + sizeof(int);
1601 t = btf_add_type_mem(btf, sz);
1605 /* if something goes wrong later, we might end up with an extra string,
1606 * but that shouldn't be a problem, because BTF can't be constructed
1607 * completely anyway and will most probably be just discarded
1609 name_off = btf__add_str(btf, name);
1613 t->name_off = name_off;
1614 t->info = btf_type_info(BTF_KIND_INT, 0, 0);
1616 /* set INT info, we don't allow setting legacy bit offset/size */
1617 *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8);
1619 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1623 btf->hdr->type_len += sz;
1624 btf->hdr->str_off += sz;
1626 return btf->nr_types;
1629 /* it's completely legal to append BTF types with type IDs pointing forward to
1630 * types that haven't been appended yet, so we only make sure that id looks
1631 * sane, we can't guarantee that ID will always be valid
1633 static int validate_type_id(int id)
1635 if (id < 0 || id > BTF_MAX_NR_TYPES)
1640 /* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */
1641 static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id)
1644 int sz, name_off = 0, err;
1646 if (validate_type_id(ref_type_id))
1649 if (btf_ensure_modifiable(btf))
1652 sz = sizeof(struct btf_type);
1653 t = btf_add_type_mem(btf, sz);
1657 if (name && name[0]) {
1658 name_off = btf__add_str(btf, name);
1663 t->name_off = name_off;
1664 t->info = btf_type_info(kind, 0, 0);
1665 t->type = ref_type_id;
1667 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1671 btf->hdr->type_len += sz;
1672 btf->hdr->str_off += sz;
1674 return btf->nr_types;
1678 * Append new BTF_KIND_PTR type with:
1679 * - *ref_type_id* - referenced type ID, it might not exist yet;
1681 * - >0, type ID of newly added BTF type;
1684 int btf__add_ptr(struct btf *btf, int ref_type_id)
1686 return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id);
1690 * Append new BTF_KIND_ARRAY type with:
1691 * - *index_type_id* - type ID of the type describing array index;
1692 * - *elem_type_id* - type ID of the type describing array element;
1693 * - *nr_elems* - the size of the array;
1695 * - >0, type ID of newly added BTF type;
1698 int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems)
1701 struct btf_array *a;
1704 if (validate_type_id(index_type_id) || validate_type_id(elem_type_id))
1707 if (btf_ensure_modifiable(btf))
1710 sz = sizeof(struct btf_type) + sizeof(struct btf_array);
1711 t = btf_add_type_mem(btf, sz);
1716 t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0);
1720 a->type = elem_type_id;
1721 a->index_type = index_type_id;
1722 a->nelems = nr_elems;
1724 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1728 btf->hdr->type_len += sz;
1729 btf->hdr->str_off += sz;
1731 return btf->nr_types;
1734 /* generic STRUCT/UNION append function */
1735 static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz)
1738 int sz, err, name_off = 0;
1740 if (btf_ensure_modifiable(btf))
1743 sz = sizeof(struct btf_type);
1744 t = btf_add_type_mem(btf, sz);
1748 if (name && name[0]) {
1749 name_off = btf__add_str(btf, name);
1754 /* start out with vlen=0 and no kflag; this will be adjusted when
1755 * adding each member
1757 t->name_off = name_off;
1758 t->info = btf_type_info(kind, 0, 0);
1761 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1765 btf->hdr->type_len += sz;
1766 btf->hdr->str_off += sz;
1768 return btf->nr_types;
1772 * Append new BTF_KIND_STRUCT type with:
1773 * - *name* - name of the struct, can be NULL or empty for anonymous structs;
1774 * - *byte_sz* - size of the struct, in bytes;
1776 * Struct initially has no fields in it. Fields can be added by
1777 * btf__add_field() right after btf__add_struct() succeeds.
1780 * - >0, type ID of newly added BTF type;
1783 int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz)
1785 return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz);
1789 * Append new BTF_KIND_UNION type with:
1790 * - *name* - name of the union, can be NULL or empty for anonymous union;
1791 * - *byte_sz* - size of the union, in bytes;
1793 * Union initially has no fields in it. Fields can be added by
1794 * btf__add_field() right after btf__add_union() succeeds. All fields
1795 * should have *bit_offset* of 0.
1798 * - >0, type ID of newly added BTF type;
1801 int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
1803 return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz);
1807 * Append new field for the current STRUCT/UNION type with:
1808 * - *name* - name of the field, can be NULL or empty for anonymous field;
1809 * - *type_id* - type ID for the type describing field type;
1810 * - *bit_offset* - bit offset of the start of the field within struct/union;
1811 * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
1816 int btf__add_field(struct btf *btf, const char *name, int type_id,
1817 __u32 bit_offset, __u32 bit_size)
1820 struct btf_member *m;
1822 int sz, name_off = 0;
1824 /* last type should be union/struct */
1825 if (btf->nr_types == 0)
1827 t = btf_type_by_id(btf, btf->nr_types);
1828 if (!btf_is_composite(t))
1831 if (validate_type_id(type_id))
1833 /* best-effort bit field offset/size enforcement */
1834 is_bitfield = bit_size || (bit_offset % 8 != 0);
1835 if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff))
1838 /* only offset 0 is allowed for unions */
1839 if (btf_is_union(t) && bit_offset)
1842 /* decompose and invalidate raw data */
1843 if (btf_ensure_modifiable(btf))
1846 sz = sizeof(struct btf_member);
1847 m = btf_add_type_mem(btf, sz);
1851 if (name && name[0]) {
1852 name_off = btf__add_str(btf, name);
1857 m->name_off = name_off;
1859 m->offset = bit_offset | (bit_size << 24);
1861 /* btf_add_type_mem can invalidate t pointer */
1862 t = btf_type_by_id(btf, btf->nr_types);
1863 /* update parent type's vlen and kflag */
1864 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
1866 btf->hdr->type_len += sz;
1867 btf->hdr->str_off += sz;
1872 * Append new BTF_KIND_ENUM type with:
1873 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
1874 * - *byte_sz* - size of the enum, in bytes.
1876 * Enum initially has no enum values in it (and corresponds to enum forward
1877 * declaration). Enumerator values can be added by btf__add_enum_value()
1878 * immediately after btf__add_enum() succeeds.
1881 * - >0, type ID of newly added BTF type;
1884 int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
1887 int sz, err, name_off = 0;
1889 /* byte_sz must be power of 2 */
1890 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
1893 if (btf_ensure_modifiable(btf))
1896 sz = sizeof(struct btf_type);
1897 t = btf_add_type_mem(btf, sz);
1901 if (name && name[0]) {
1902 name_off = btf__add_str(btf, name);
1907 /* start out with vlen=0; it will be adjusted when adding enum values */
1908 t->name_off = name_off;
1909 t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
1912 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1916 btf->hdr->type_len += sz;
1917 btf->hdr->str_off += sz;
1919 return btf->nr_types;
1923 * Append new enum value for the current ENUM type with:
1924 * - *name* - name of the enumerator value, can't be NULL or empty;
1925 * - *value* - integer value corresponding to enum value *name*;
1930 int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
1936 /* last type should be BTF_KIND_ENUM */
1937 if (btf->nr_types == 0)
1939 t = btf_type_by_id(btf, btf->nr_types);
1940 if (!btf_is_enum(t))
1943 /* non-empty name */
1944 if (!name || !name[0])
1946 if (value < INT_MIN || value > UINT_MAX)
1949 /* decompose and invalidate raw data */
1950 if (btf_ensure_modifiable(btf))
1953 sz = sizeof(struct btf_enum);
1954 v = btf_add_type_mem(btf, sz);
1958 name_off = btf__add_str(btf, name);
1962 v->name_off = name_off;
1965 /* update parent type's vlen */
1966 t = btf_type_by_id(btf, btf->nr_types);
1967 btf_type_inc_vlen(t);
1969 btf->hdr->type_len += sz;
1970 btf->hdr->str_off += sz;
1975 * Append new BTF_KIND_FWD type with:
1976 * - *name*, non-empty/non-NULL name;
1977 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
1978 * BTF_FWD_UNION, or BTF_FWD_ENUM;
1980 * - >0, type ID of newly added BTF type;
1983 int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
1985 if (!name || !name[0])
1989 case BTF_FWD_STRUCT:
1990 case BTF_FWD_UNION: {
1994 id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0);
1997 t = btf_type_by_id(btf, id);
1998 t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION);
2002 /* enum forward in BTF currently is just an enum with no enum
2003 * values; we also assume a standard 4-byte size for it
2005 return btf__add_enum(btf, name, sizeof(int));
2012 * Append new BTF_KING_TYPEDEF type with:
2013 * - *name*, non-empty/non-NULL name;
2014 * - *ref_type_id* - referenced type ID, it might not exist yet;
2016 * - >0, type ID of newly added BTF type;
2019 int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)
2021 if (!name || !name[0])
2024 return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id);
2028 * Append new BTF_KIND_VOLATILE type with:
2029 * - *ref_type_id* - referenced type ID, it might not exist yet;
2031 * - >0, type ID of newly added BTF type;
2034 int btf__add_volatile(struct btf *btf, int ref_type_id)
2036 return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id);
2040 * Append new BTF_KIND_CONST type with:
2041 * - *ref_type_id* - referenced type ID, it might not exist yet;
2043 * - >0, type ID of newly added BTF type;
2046 int btf__add_const(struct btf *btf, int ref_type_id)
2048 return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id);
2052 * Append new BTF_KIND_RESTRICT type with:
2053 * - *ref_type_id* - referenced type ID, it might not exist yet;
2055 * - >0, type ID of newly added BTF type;
2058 int btf__add_restrict(struct btf *btf, int ref_type_id)
2060 return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
2064 * Append new BTF_KIND_FUNC type with:
2065 * - *name*, non-empty/non-NULL name;
2066 * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
2068 * - >0, type ID of newly added BTF type;
2071 int btf__add_func(struct btf *btf, const char *name,
2072 enum btf_func_linkage linkage, int proto_type_id)
2076 if (!name || !name[0])
2078 if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL &&
2079 linkage != BTF_FUNC_EXTERN)
2082 id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id);
2084 struct btf_type *t = btf_type_by_id(btf, id);
2086 t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0);
2092 * Append new BTF_KIND_FUNC_PROTO with:
2093 * - *ret_type_id* - type ID for return result of a function.
2095 * Function prototype initially has no arguments, but they can be added by
2096 * btf__add_func_param() one by one, immediately after
2097 * btf__add_func_proto() succeeded.
2100 * - >0, type ID of newly added BTF type;
2103 int btf__add_func_proto(struct btf *btf, int ret_type_id)
2108 if (validate_type_id(ret_type_id))
2111 if (btf_ensure_modifiable(btf))
2114 sz = sizeof(struct btf_type);
2115 t = btf_add_type_mem(btf, sz);
2119 /* start out with vlen=0; this will be adjusted when adding enum
2120 * values, if necessary
2123 t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0);
2124 t->type = ret_type_id;
2126 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
2130 btf->hdr->type_len += sz;
2131 btf->hdr->str_off += sz;
2133 return btf->nr_types;
2137 * Append new function parameter for current FUNC_PROTO type with:
2138 * - *name* - parameter name, can be NULL or empty;
2139 * - *type_id* - type ID describing the type of the parameter.
2144 int btf__add_func_param(struct btf *btf, const char *name, int type_id)
2147 struct btf_param *p;
2148 int sz, name_off = 0;
2150 if (validate_type_id(type_id))
2153 /* last type should be BTF_KIND_FUNC_PROTO */
2154 if (btf->nr_types == 0)
2156 t = btf_type_by_id(btf, btf->nr_types);
2157 if (!btf_is_func_proto(t))
2160 /* decompose and invalidate raw data */
2161 if (btf_ensure_modifiable(btf))
2164 sz = sizeof(struct btf_param);
2165 p = btf_add_type_mem(btf, sz);
2169 if (name && name[0]) {
2170 name_off = btf__add_str(btf, name);
2175 p->name_off = name_off;
2178 /* update parent type's vlen */
2179 t = btf_type_by_id(btf, btf->nr_types);
2180 btf_type_inc_vlen(t);
2182 btf->hdr->type_len += sz;
2183 btf->hdr->str_off += sz;
2188 * Append new BTF_KIND_VAR type with:
2189 * - *name* - non-empty/non-NULL name;
2190 * - *linkage* - variable linkage, one of BTF_VAR_STATIC,
2191 * BTF_VAR_GLOBAL_ALLOCATED, or BTF_VAR_GLOBAL_EXTERN;
2192 * - *type_id* - type ID of the type describing the type of the variable.
2194 * - >0, type ID of newly added BTF type;
2197 int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id)
2201 int sz, err, name_off;
2203 /* non-empty name */
2204 if (!name || !name[0])
2206 if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2207 linkage != BTF_VAR_GLOBAL_EXTERN)
2209 if (validate_type_id(type_id))
2212 /* deconstruct BTF, if necessary, and invalidate raw_data */
2213 if (btf_ensure_modifiable(btf))
2216 sz = sizeof(struct btf_type) + sizeof(struct btf_var);
2217 t = btf_add_type_mem(btf, sz);
2221 name_off = btf__add_str(btf, name);
2225 t->name_off = name_off;
2226 t->info = btf_type_info(BTF_KIND_VAR, 0, 0);
2230 v->linkage = linkage;
2232 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
2236 btf->hdr->type_len += sz;
2237 btf->hdr->str_off += sz;
2239 return btf->nr_types;
2243 * Append new BTF_KIND_DATASEC type with:
2244 * - *name* - non-empty/non-NULL name;
2245 * - *byte_sz* - data section size, in bytes.
2247 * Data section is initially empty. Variables info can be added with
2248 * btf__add_datasec_var_info() calls, after btf__add_datasec() succeeds.
2251 * - >0, type ID of newly added BTF type;
2254 int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
2257 int sz, err, name_off;
2259 /* non-empty name */
2260 if (!name || !name[0])
2263 if (btf_ensure_modifiable(btf))
2266 sz = sizeof(struct btf_type);
2267 t = btf_add_type_mem(btf, sz);
2271 name_off = btf__add_str(btf, name);
2275 /* start with vlen=0, which will be update as var_secinfos are added */
2276 t->name_off = name_off;
2277 t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0);
2280 err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
2284 btf->hdr->type_len += sz;
2285 btf->hdr->str_off += sz;
2287 return btf->nr_types;
2291 * Append new data section variable information entry for current DATASEC type:
2292 * - *var_type_id* - type ID, describing type of the variable;
2293 * - *offset* - variable offset within data section, in bytes;
2294 * - *byte_sz* - variable size, in bytes.
2300 int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz)
2303 struct btf_var_secinfo *v;
2306 /* last type should be BTF_KIND_DATASEC */
2307 if (btf->nr_types == 0)
2309 t = btf_type_by_id(btf, btf->nr_types);
2310 if (!btf_is_datasec(t))
2313 if (validate_type_id(var_type_id))
2316 /* decompose and invalidate raw data */
2317 if (btf_ensure_modifiable(btf))
2320 sz = sizeof(struct btf_var_secinfo);
2321 v = btf_add_type_mem(btf, sz);
2325 v->type = var_type_id;
2329 /* update parent type's vlen */
2330 t = btf_type_by_id(btf, btf->nr_types);
2331 btf_type_inc_vlen(t);
2333 btf->hdr->type_len += sz;
2334 btf->hdr->str_off += sz;
2338 struct btf_ext_sec_setup_param {
2342 struct btf_ext_info *ext_info;
2346 static int btf_ext_setup_info(struct btf_ext *btf_ext,
2347 struct btf_ext_sec_setup_param *ext_sec)
2349 const struct btf_ext_info_sec *sinfo;
2350 struct btf_ext_info *ext_info;
2351 __u32 info_left, record_size;
2352 /* The start of the info sec (including the __u32 record_size). */
2355 if (ext_sec->len == 0)
2358 if (ext_sec->off & 0x03) {
2359 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
2364 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
2365 info_left = ext_sec->len;
2367 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
2368 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
2369 ext_sec->desc, ext_sec->off, ext_sec->len);
2373 /* At least a record size */
2374 if (info_left < sizeof(__u32)) {
2375 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
2379 /* The record size needs to meet the minimum standard */
2380 record_size = *(__u32 *)info;
2381 if (record_size < ext_sec->min_rec_size ||
2382 record_size & 0x03) {
2383 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
2384 ext_sec->desc, record_size);
2388 sinfo = info + sizeof(__u32);
2389 info_left -= sizeof(__u32);
2391 /* If no records, return failure now so .BTF.ext won't be used. */
2393 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
2398 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
2399 __u64 total_record_size;
2402 if (info_left < sec_hdrlen) {
2403 pr_debug("%s section header is not found in .BTF.ext\n",
2408 num_records = sinfo->num_info;
2409 if (num_records == 0) {
2410 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2415 total_record_size = sec_hdrlen +
2416 (__u64)num_records * record_size;
2417 if (info_left < total_record_size) {
2418 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2423 info_left -= total_record_size;
2424 sinfo = (void *)sinfo + total_record_size;
2427 ext_info = ext_sec->ext_info;
2428 ext_info->len = ext_sec->len - sizeof(__u32);
2429 ext_info->rec_size = record_size;
2430 ext_info->info = info + sizeof(__u32);
2435 static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
2437 struct btf_ext_sec_setup_param param = {
2438 .off = btf_ext->hdr->func_info_off,
2439 .len = btf_ext->hdr->func_info_len,
2440 .min_rec_size = sizeof(struct bpf_func_info_min),
2441 .ext_info = &btf_ext->func_info,
2445 return btf_ext_setup_info(btf_ext, ¶m);
2448 static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
2450 struct btf_ext_sec_setup_param param = {
2451 .off = btf_ext->hdr->line_info_off,
2452 .len = btf_ext->hdr->line_info_len,
2453 .min_rec_size = sizeof(struct bpf_line_info_min),
2454 .ext_info = &btf_ext->line_info,
2455 .desc = "line_info",
2458 return btf_ext_setup_info(btf_ext, ¶m);
2461 static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
2463 struct btf_ext_sec_setup_param param = {
2464 .off = btf_ext->hdr->core_relo_off,
2465 .len = btf_ext->hdr->core_relo_len,
2466 .min_rec_size = sizeof(struct bpf_core_relo),
2467 .ext_info = &btf_ext->core_relo_info,
2468 .desc = "core_relo",
2471 return btf_ext_setup_info(btf_ext, ¶m);
2474 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
2476 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
2478 if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
2479 data_size < hdr->hdr_len) {
2480 pr_debug("BTF.ext header not found");
2484 if (hdr->magic == bswap_16(BTF_MAGIC)) {
2485 pr_warn("BTF.ext in non-native endianness is not supported\n");
2487 } else if (hdr->magic != BTF_MAGIC) {
2488 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
2492 if (hdr->version != BTF_VERSION) {
2493 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
2498 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
2502 if (data_size == hdr->hdr_len) {
2503 pr_debug("BTF.ext has no data\n");
2510 void btf_ext__free(struct btf_ext *btf_ext)
2512 if (IS_ERR_OR_NULL(btf_ext))
2514 free(btf_ext->data);
2518 struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
2520 struct btf_ext *btf_ext;
2523 err = btf_ext_parse_hdr(data, size);
2525 return ERR_PTR(err);
2527 btf_ext = calloc(1, sizeof(struct btf_ext));
2529 return ERR_PTR(-ENOMEM);
2531 btf_ext->data_size = size;
2532 btf_ext->data = malloc(size);
2533 if (!btf_ext->data) {
2537 memcpy(btf_ext->data, data, size);
2539 if (btf_ext->hdr->hdr_len <
2540 offsetofend(struct btf_ext_header, line_info_len))
2542 err = btf_ext_setup_func_info(btf_ext);
2546 err = btf_ext_setup_line_info(btf_ext);
2550 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
2552 err = btf_ext_setup_core_relos(btf_ext);
2558 btf_ext__free(btf_ext);
2559 return ERR_PTR(err);
2565 const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
2567 *size = btf_ext->data_size;
2568 return btf_ext->data;
2571 static int btf_ext_reloc_info(const struct btf *btf,
2572 const struct btf_ext_info *ext_info,
2573 const char *sec_name, __u32 insns_cnt,
2574 void **info, __u32 *cnt)
2576 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
2577 __u32 i, record_size, existing_len, records_len;
2578 struct btf_ext_info_sec *sinfo;
2579 const char *info_sec_name;
2583 record_size = ext_info->rec_size;
2584 sinfo = ext_info->info;
2585 remain_len = ext_info->len;
2586 while (remain_len > 0) {
2587 records_len = sinfo->num_info * record_size;
2588 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
2589 if (strcmp(info_sec_name, sec_name)) {
2590 remain_len -= sec_hdrlen + records_len;
2591 sinfo = (void *)sinfo + sec_hdrlen + records_len;
2595 existing_len = (*cnt) * record_size;
2596 data = realloc(*info, existing_len + records_len);
2600 memcpy(data + existing_len, sinfo->data, records_len);
2601 /* adjust insn_off only, the rest data will be passed
2604 for (i = 0; i < sinfo->num_info; i++) {
2607 insn_off = data + existing_len + (i * record_size);
2608 *insn_off = *insn_off / sizeof(struct bpf_insn) +
2612 *cnt += sinfo->num_info;
2619 int btf_ext__reloc_func_info(const struct btf *btf,
2620 const struct btf_ext *btf_ext,
2621 const char *sec_name, __u32 insns_cnt,
2622 void **func_info, __u32 *cnt)
2624 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
2625 insns_cnt, func_info, cnt);
2628 int btf_ext__reloc_line_info(const struct btf *btf,
2629 const struct btf_ext *btf_ext,
2630 const char *sec_name, __u32 insns_cnt,
2631 void **line_info, __u32 *cnt)
2633 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
2634 insns_cnt, line_info, cnt);
2637 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
2639 return btf_ext->func_info.rec_size;
2642 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
2644 return btf_ext->line_info.rec_size;
2649 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
2650 const struct btf_dedup_opts *opts);
2651 static void btf_dedup_free(struct btf_dedup *d);
2652 static int btf_dedup_strings(struct btf_dedup *d);
2653 static int btf_dedup_prim_types(struct btf_dedup *d);
2654 static int btf_dedup_struct_types(struct btf_dedup *d);
2655 static int btf_dedup_ref_types(struct btf_dedup *d);
2656 static int btf_dedup_compact_types(struct btf_dedup *d);
2657 static int btf_dedup_remap_types(struct btf_dedup *d);
2660 * Deduplicate BTF types and strings.
2662 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
2663 * section with all BTF type descriptors and string data. It overwrites that
2664 * memory in-place with deduplicated types and strings without any loss of
2665 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
2666 * is provided, all the strings referenced from .BTF.ext section are honored
2667 * and updated to point to the right offsets after deduplication.
2669 * If function returns with error, type/string data might be garbled and should
2672 * More verbose and detailed description of both problem btf_dedup is solving,
2673 * as well as solution could be found at:
2674 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
2676 * Problem description and justification
2677 * =====================================
2679 * BTF type information is typically emitted either as a result of conversion
2680 * from DWARF to BTF or directly by compiler. In both cases, each compilation
2681 * unit contains information about a subset of all the types that are used
2682 * in an application. These subsets are frequently overlapping and contain a lot
2683 * of duplicated information when later concatenated together into a single
2684 * binary. This algorithm ensures that each unique type is represented by single
2685 * BTF type descriptor, greatly reducing resulting size of BTF data.
2687 * Compilation unit isolation and subsequent duplication of data is not the only
2688 * problem. The same type hierarchy (e.g., struct and all the type that struct
2689 * references) in different compilation units can be represented in BTF to
2690 * various degrees of completeness (or, rather, incompleteness) due to
2691 * struct/union forward declarations.
2693 * Let's take a look at an example, that we'll use to better understand the
2694 * problem (and solution). Suppose we have two compilation units, each using
2695 * same `struct S`, but each of them having incomplete type information about
2724 * In case of CU #1, BTF data will know only that `struct B` exist (but no
2725 * more), but will know the complete type information about `struct A`. While
2726 * for CU #2, it will know full type information about `struct B`, but will
2727 * only know about forward declaration of `struct A` (in BTF terms, it will
2728 * have `BTF_KIND_FWD` type descriptor with name `B`).
2730 * This compilation unit isolation means that it's possible that there is no
2731 * single CU with complete type information describing structs `S`, `A`, and
2732 * `B`. Also, we might get tons of duplicated and redundant type information.
2734 * Additional complication we need to keep in mind comes from the fact that
2735 * types, in general, can form graphs containing cycles, not just DAGs.
2737 * While algorithm does deduplication, it also merges and resolves type
2738 * information (unless disabled throught `struct btf_opts`), whenever possible.
2739 * E.g., in the example above with two compilation units having partial type
2740 * information for structs `A` and `B`, the output of algorithm will emit
2741 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
2742 * (as well as type information for `int` and pointers), as if they were defined
2743 * in a single compilation unit as:
2763 * Algorithm completes its work in 6 separate passes:
2765 * 1. Strings deduplication.
2766 * 2. Primitive types deduplication (int, enum, fwd).
2767 * 3. Struct/union types deduplication.
2768 * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
2769 * protos, and const/volatile/restrict modifiers).
2770 * 5. Types compaction.
2771 * 6. Types remapping.
2773 * Algorithm determines canonical type descriptor, which is a single
2774 * representative type for each truly unique type. This canonical type is the
2775 * one that will go into final deduplicated BTF type information. For
2776 * struct/unions, it is also the type that algorithm will merge additional type
2777 * information into (while resolving FWDs), as it discovers it from data in
2778 * other CUs. Each input BTF type eventually gets either mapped to itself, if
2779 * that type is canonical, or to some other type, if that type is equivalent
2780 * and was chosen as canonical representative. This mapping is stored in
2781 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
2782 * FWD type got resolved to.
2784 * To facilitate fast discovery of canonical types, we also maintain canonical
2785 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
2786 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
2787 * that match that signature. With sufficiently good choice of type signature
2788 * hashing function, we can limit number of canonical types for each unique type
2789 * signature to a very small number, allowing to find canonical type for any
2790 * duplicated type very quickly.
2792 * Struct/union deduplication is the most critical part and algorithm for
2793 * deduplicating structs/unions is described in greater details in comments for
2794 * `btf_dedup_is_equiv` function.
2796 int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
2797 const struct btf_dedup_opts *opts)
2799 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
2803 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
2807 if (btf_ensure_modifiable(btf))
2810 err = btf_dedup_strings(d);
2812 pr_debug("btf_dedup_strings failed:%d\n", err);
2815 err = btf_dedup_prim_types(d);
2817 pr_debug("btf_dedup_prim_types failed:%d\n", err);
2820 err = btf_dedup_struct_types(d);
2822 pr_debug("btf_dedup_struct_types failed:%d\n", err);
2825 err = btf_dedup_ref_types(d);
2827 pr_debug("btf_dedup_ref_types failed:%d\n", err);
2830 err = btf_dedup_compact_types(d);
2832 pr_debug("btf_dedup_compact_types failed:%d\n", err);
2835 err = btf_dedup_remap_types(d);
2837 pr_debug("btf_dedup_remap_types failed:%d\n", err);
2846 #define BTF_UNPROCESSED_ID ((__u32)-1)
2847 #define BTF_IN_PROGRESS_ID ((__u32)-2)
2850 /* .BTF section to be deduped in-place */
2853 * Optional .BTF.ext section. When provided, any strings referenced
2854 * from it will be taken into account when deduping strings
2856 struct btf_ext *btf_ext;
2858 * This is a map from any type's signature hash to a list of possible
2859 * canonical representative type candidates. Hash collisions are
2860 * ignored, so even types of various kinds can share same list of
2861 * candidates, which is fine because we rely on subsequent
2862 * btf_xxx_equal() checks to authoritatively verify type equality.
2864 struct hashmap *dedup_table;
2865 /* Canonical types map */
2867 /* Hypothetical mapping, used during type graph equivalence checks */
2872 /* Various option modifying behavior of algorithm */
2873 struct btf_dedup_opts opts;
2876 struct btf_str_ptr {
2882 struct btf_str_ptrs {
2883 struct btf_str_ptr *ptrs;
2889 static long hash_combine(long h, long value)
2891 return h * 31 + value;
2894 #define for_each_dedup_cand(d, node, hash) \
2895 hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
2897 static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
2899 return hashmap__append(d->dedup_table,
2900 (void *)hash, (void *)(long)type_id);
2903 static int btf_dedup_hypot_map_add(struct btf_dedup *d,
2904 __u32 from_id, __u32 to_id)
2906 if (d->hypot_cnt == d->hypot_cap) {
2909 d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
2910 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
2913 d->hypot_list = new_list;
2915 d->hypot_list[d->hypot_cnt++] = from_id;
2916 d->hypot_map[from_id] = to_id;
2920 static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
2924 for (i = 0; i < d->hypot_cnt; i++)
2925 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
2929 static void btf_dedup_free(struct btf_dedup *d)
2931 hashmap__free(d->dedup_table);
2932 d->dedup_table = NULL;
2938 d->hypot_map = NULL;
2940 free(d->hypot_list);
2941 d->hypot_list = NULL;
2946 static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
2951 static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
2956 static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
2961 static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
2962 const struct btf_dedup_opts *opts)
2964 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
2965 hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
2969 return ERR_PTR(-ENOMEM);
2971 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
2972 /* dedup_table_size is now used only to force collisions in tests */
2973 if (opts && opts->dedup_table_size == 1)
2974 hash_fn = btf_dedup_collision_hash_fn;
2977 d->btf_ext = btf_ext;
2979 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
2980 if (IS_ERR(d->dedup_table)) {
2981 err = PTR_ERR(d->dedup_table);
2982 d->dedup_table = NULL;
2986 d->map = malloc(sizeof(__u32) * (1 + btf->nr_types));
2991 /* special BTF "void" type is made canonical immediately */
2993 for (i = 1; i <= btf->nr_types; i++) {
2994 struct btf_type *t = btf_type_by_id(d->btf, i);
2996 /* VAR and DATASEC are never deduped and are self-canonical */
2997 if (btf_is_var(t) || btf_is_datasec(t))
3000 d->map[i] = BTF_UNPROCESSED_ID;
3003 d->hypot_map = malloc(sizeof(__u32) * (1 + btf->nr_types));
3004 if (!d->hypot_map) {
3008 for (i = 0; i <= btf->nr_types; i++)
3009 d->hypot_map[i] = BTF_UNPROCESSED_ID;
3014 return ERR_PTR(err);
3020 typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
3023 * Iterate over all possible places in .BTF and .BTF.ext that can reference
3024 * string and pass pointer to it to a provided callback `fn`.
3026 static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
3028 void *line_data_cur, *line_data_end;
3029 int i, j, r, rec_size;
3032 for (i = 1; i <= d->btf->nr_types; i++) {
3033 t = btf_type_by_id(d->btf, i);
3034 r = fn(&t->name_off, ctx);
3038 switch (btf_kind(t)) {
3039 case BTF_KIND_STRUCT:
3040 case BTF_KIND_UNION: {
3041 struct btf_member *m = btf_members(t);
3042 __u16 vlen = btf_vlen(t);
3044 for (j = 0; j < vlen; j++) {
3045 r = fn(&m->name_off, ctx);
3052 case BTF_KIND_ENUM: {
3053 struct btf_enum *m = btf_enum(t);
3054 __u16 vlen = btf_vlen(t);
3056 for (j = 0; j < vlen; j++) {
3057 r = fn(&m->name_off, ctx);
3064 case BTF_KIND_FUNC_PROTO: {
3065 struct btf_param *m = btf_params(t);
3066 __u16 vlen = btf_vlen(t);
3068 for (j = 0; j < vlen; j++) {
3069 r = fn(&m->name_off, ctx);
3084 line_data_cur = d->btf_ext->line_info.info;
3085 line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
3086 rec_size = d->btf_ext->line_info.rec_size;
3088 while (line_data_cur < line_data_end) {
3089 struct btf_ext_info_sec *sec = line_data_cur;
3090 struct bpf_line_info_min *line_info;
3091 __u32 num_info = sec->num_info;
3093 r = fn(&sec->sec_name_off, ctx);
3097 line_data_cur += sizeof(struct btf_ext_info_sec);
3098 for (i = 0; i < num_info; i++) {
3099 line_info = line_data_cur;
3100 r = fn(&line_info->file_name_off, ctx);
3103 r = fn(&line_info->line_off, ctx);
3106 line_data_cur += rec_size;
3113 static int str_sort_by_content(const void *a1, const void *a2)
3115 const struct btf_str_ptr *p1 = a1;
3116 const struct btf_str_ptr *p2 = a2;
3118 return strcmp(p1->str, p2->str);
3121 static int str_sort_by_offset(const void *a1, const void *a2)
3123 const struct btf_str_ptr *p1 = a1;
3124 const struct btf_str_ptr *p2 = a2;
3126 if (p1->str != p2->str)
3127 return p1->str < p2->str ? -1 : 1;
3131 static int btf_dedup_str_ptr_cmp(const void *str_ptr, const void *pelem)
3133 const struct btf_str_ptr *p = pelem;
3135 if (str_ptr != p->str)
3136 return (const char *)str_ptr < p->str ? -1 : 1;
3140 static int btf_str_mark_as_used(__u32 *str_off_ptr, void *ctx)
3142 struct btf_str_ptrs *strs;
3143 struct btf_str_ptr *s;
3145 if (*str_off_ptr == 0)
3149 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
3150 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
3157 static int btf_str_remap_offset(__u32 *str_off_ptr, void *ctx)
3159 struct btf_str_ptrs *strs;
3160 struct btf_str_ptr *s;
3162 if (*str_off_ptr == 0)
3166 s = bsearch(strs->data + *str_off_ptr, strs->ptrs, strs->cnt,
3167 sizeof(struct btf_str_ptr), btf_dedup_str_ptr_cmp);
3170 *str_off_ptr = s->new_off;
3175 * Dedup string and filter out those that are not referenced from either .BTF
3176 * or .BTF.ext (if provided) sections.
3178 * This is done by building index of all strings in BTF's string section,
3179 * then iterating over all entities that can reference strings (e.g., type
3180 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
3181 * strings as used. After that all used strings are deduped and compacted into
3182 * sequential blob of memory and new offsets are calculated. Then all the string
3183 * references are iterated again and rewritten using new offsets.
3185 static int btf_dedup_strings(struct btf_dedup *d)
3187 char *start = d->btf->strs_data;
3188 char *end = start + d->btf->hdr->str_len;
3189 char *p = start, *tmp_strs = NULL;
3190 struct btf_str_ptrs strs = {
3196 int i, j, err = 0, grp_idx;
3199 if (d->btf->strs_deduped)
3202 /* build index of all strings */
3204 if (strs.cnt + 1 > strs.cap) {
3205 struct btf_str_ptr *new_ptrs;
3207 strs.cap += max(strs.cnt / 2, 16U);
3208 new_ptrs = libbpf_reallocarray(strs.ptrs, strs.cap, sizeof(strs.ptrs[0]));
3213 strs.ptrs = new_ptrs;
3216 strs.ptrs[strs.cnt].str = p;
3217 strs.ptrs[strs.cnt].used = false;
3223 /* temporary storage for deduplicated strings */
3224 tmp_strs = malloc(d->btf->hdr->str_len);
3230 /* mark all used strings */
3231 strs.ptrs[0].used = true;
3232 err = btf_for_each_str_off(d, btf_str_mark_as_used, &strs);
3236 /* sort strings by context, so that we can identify duplicates */
3237 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_content);
3240 * iterate groups of equal strings and if any instance in a group was
3241 * referenced, emit single instance and remember new offset
3245 grp_used = strs.ptrs[0].used;
3246 /* iterate past end to avoid code duplication after loop */
3247 for (i = 1; i <= strs.cnt; i++) {
3249 * when i == strs.cnt, we want to skip string comparison and go
3250 * straight to handling last group of strings (otherwise we'd
3251 * need to handle last group after the loop w/ duplicated code)
3254 !strcmp(strs.ptrs[i].str, strs.ptrs[grp_idx].str)) {
3255 grp_used = grp_used || strs.ptrs[i].used;
3260 * this check would have been required after the loop to handle
3261 * last group of strings, but due to <= condition in a loop
3262 * we avoid that duplication
3265 int new_off = p - tmp_strs;
3266 __u32 len = strlen(strs.ptrs[grp_idx].str);
3268 memmove(p, strs.ptrs[grp_idx].str, len + 1);
3269 for (j = grp_idx; j < i; j++)
3270 strs.ptrs[j].new_off = new_off;
3276 grp_used = strs.ptrs[i].used;
3280 /* replace original strings with deduped ones */
3281 d->btf->hdr->str_len = p - tmp_strs;
3282 memmove(start, tmp_strs, d->btf->hdr->str_len);
3283 end = start + d->btf->hdr->str_len;
3285 /* restore original order for further binary search lookups */
3286 qsort(strs.ptrs, strs.cnt, sizeof(strs.ptrs[0]), str_sort_by_offset);
3288 /* remap string offsets */
3289 err = btf_for_each_str_off(d, btf_str_remap_offset, &strs);
3293 d->btf->hdr->str_len = end - start;
3294 d->btf->strs_deduped = true;
3302 static long btf_hash_common(struct btf_type *t)
3306 h = hash_combine(0, t->name_off);
3307 h = hash_combine(h, t->info);
3308 h = hash_combine(h, t->size);
3312 static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
3314 return t1->name_off == t2->name_off &&
3315 t1->info == t2->info &&
3316 t1->size == t2->size;
3319 /* Calculate type signature hash of INT. */
3320 static long btf_hash_int(struct btf_type *t)
3322 __u32 info = *(__u32 *)(t + 1);
3325 h = btf_hash_common(t);
3326 h = hash_combine(h, info);
3330 /* Check structural equality of two INTs. */
3331 static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
3335 if (!btf_equal_common(t1, t2))
3337 info1 = *(__u32 *)(t1 + 1);
3338 info2 = *(__u32 *)(t2 + 1);
3339 return info1 == info2;
3342 /* Calculate type signature hash of ENUM. */
3343 static long btf_hash_enum(struct btf_type *t)
3347 /* don't hash vlen and enum members to support enum fwd resolving */
3348 h = hash_combine(0, t->name_off);
3349 h = hash_combine(h, t->info & ~0xffff);
3350 h = hash_combine(h, t->size);
3354 /* Check structural equality of two ENUMs. */
3355 static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
3357 const struct btf_enum *m1, *m2;
3361 if (!btf_equal_common(t1, t2))
3364 vlen = btf_vlen(t1);
3367 for (i = 0; i < vlen; i++) {
3368 if (m1->name_off != m2->name_off || m1->val != m2->val)
3376 static inline bool btf_is_enum_fwd(struct btf_type *t)
3378 return btf_is_enum(t) && btf_vlen(t) == 0;
3381 static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3383 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3384 return btf_equal_enum(t1, t2);
3385 /* ignore vlen when comparing */
3386 return t1->name_off == t2->name_off &&
3387 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3388 t1->size == t2->size;
3392 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
3393 * as referenced type IDs equivalence is established separately during type
3394 * graph equivalence check algorithm.
3396 static long btf_hash_struct(struct btf_type *t)
3398 const struct btf_member *member = btf_members(t);
3399 __u32 vlen = btf_vlen(t);
3400 long h = btf_hash_common(t);
3403 for (i = 0; i < vlen; i++) {
3404 h = hash_combine(h, member->name_off);
3405 h = hash_combine(h, member->offset);
3406 /* no hashing of referenced type ID, it can be unresolved yet */
3413 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
3414 * IDs. This check is performed during type graph equivalence check and
3415 * referenced types equivalence is checked separately.
3417 static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
3419 const struct btf_member *m1, *m2;
3423 if (!btf_equal_common(t1, t2))
3426 vlen = btf_vlen(t1);
3427 m1 = btf_members(t1);
3428 m2 = btf_members(t2);
3429 for (i = 0; i < vlen; i++) {
3430 if (m1->name_off != m2->name_off || m1->offset != m2->offset)
3439 * Calculate type signature hash of ARRAY, including referenced type IDs,
3440 * under assumption that they were already resolved to canonical type IDs and
3441 * are not going to change.
3443 static long btf_hash_array(struct btf_type *t)
3445 const struct btf_array *info = btf_array(t);
3446 long h = btf_hash_common(t);
3448 h = hash_combine(h, info->type);
3449 h = hash_combine(h, info->index_type);
3450 h = hash_combine(h, info->nelems);
3455 * Check exact equality of two ARRAYs, taking into account referenced
3456 * type IDs, under assumption that they were already resolved to canonical
3457 * type IDs and are not going to change.
3458 * This function is called during reference types deduplication to compare
3459 * ARRAY to potential canonical representative.
3461 static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
3463 const struct btf_array *info1, *info2;
3465 if (!btf_equal_common(t1, t2))
3468 info1 = btf_array(t1);
3469 info2 = btf_array(t2);
3470 return info1->type == info2->type &&
3471 info1->index_type == info2->index_type &&
3472 info1->nelems == info2->nelems;
3476 * Check structural compatibility of two ARRAYs, ignoring referenced type
3477 * IDs. This check is performed during type graph equivalence check and
3478 * referenced types equivalence is checked separately.
3480 static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
3482 if (!btf_equal_common(t1, t2))
3485 return btf_array(t1)->nelems == btf_array(t2)->nelems;
3489 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
3490 * under assumption that they were already resolved to canonical type IDs and
3491 * are not going to change.
3493 static long btf_hash_fnproto(struct btf_type *t)
3495 const struct btf_param *member = btf_params(t);
3496 __u16 vlen = btf_vlen(t);
3497 long h = btf_hash_common(t);
3500 for (i = 0; i < vlen; i++) {
3501 h = hash_combine(h, member->name_off);
3502 h = hash_combine(h, member->type);
3509 * Check exact equality of two FUNC_PROTOs, taking into account referenced
3510 * type IDs, under assumption that they were already resolved to canonical
3511 * type IDs and are not going to change.
3512 * This function is called during reference types deduplication to compare
3513 * FUNC_PROTO to potential canonical representative.
3515 static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
3517 const struct btf_param *m1, *m2;
3521 if (!btf_equal_common(t1, t2))
3524 vlen = btf_vlen(t1);
3525 m1 = btf_params(t1);
3526 m2 = btf_params(t2);
3527 for (i = 0; i < vlen; i++) {
3528 if (m1->name_off != m2->name_off || m1->type != m2->type)
3537 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
3538 * IDs. This check is performed during type graph equivalence check and
3539 * referenced types equivalence is checked separately.
3541 static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
3543 const struct btf_param *m1, *m2;
3547 /* skip return type ID */
3548 if (t1->name_off != t2->name_off || t1->info != t2->info)
3551 vlen = btf_vlen(t1);
3552 m1 = btf_params(t1);
3553 m2 = btf_params(t2);
3554 for (i = 0; i < vlen; i++) {
3555 if (m1->name_off != m2->name_off)
3564 * Deduplicate primitive types, that can't reference other types, by calculating
3565 * their type signature hash and comparing them with any possible canonical
3566 * candidate. If no canonical candidate matches, type itself is marked as
3567 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
3569 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
3571 struct btf_type *t = btf_type_by_id(d->btf, type_id);
3572 struct hashmap_entry *hash_entry;
3573 struct btf_type *cand;
3574 /* if we don't find equivalent type, then we are canonical */
3575 __u32 new_id = type_id;
3579 switch (btf_kind(t)) {
3580 case BTF_KIND_CONST:
3581 case BTF_KIND_VOLATILE:
3582 case BTF_KIND_RESTRICT:
3584 case BTF_KIND_TYPEDEF:
3585 case BTF_KIND_ARRAY:
3586 case BTF_KIND_STRUCT:
3587 case BTF_KIND_UNION:
3589 case BTF_KIND_FUNC_PROTO:
3591 case BTF_KIND_DATASEC:
3595 h = btf_hash_int(t);
3596 for_each_dedup_cand(d, hash_entry, h) {
3597 cand_id = (__u32)(long)hash_entry->value;
3598 cand = btf_type_by_id(d->btf, cand_id);
3599 if (btf_equal_int(t, cand)) {
3607 h = btf_hash_enum(t);
3608 for_each_dedup_cand(d, hash_entry, h) {
3609 cand_id = (__u32)(long)hash_entry->value;
3610 cand = btf_type_by_id(d->btf, cand_id);
3611 if (btf_equal_enum(t, cand)) {
3615 if (d->opts.dont_resolve_fwds)
3617 if (btf_compat_enum(t, cand)) {
3618 if (btf_is_enum_fwd(t)) {
3619 /* resolve fwd to full enum */
3623 /* resolve canonical enum fwd to full enum */
3624 d->map[cand_id] = type_id;
3630 h = btf_hash_common(t);
3631 for_each_dedup_cand(d, hash_entry, h) {
3632 cand_id = (__u32)(long)hash_entry->value;
3633 cand = btf_type_by_id(d->btf, cand_id);
3634 if (btf_equal_common(t, cand)) {
3645 d->map[type_id] = new_id;
3646 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
3652 static int btf_dedup_prim_types(struct btf_dedup *d)
3656 for (i = 1; i <= d->btf->nr_types; i++) {
3657 err = btf_dedup_prim_type(d, i);
3665 * Check whether type is already mapped into canonical one (could be to itself).
3667 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
3669 return d->map[type_id] <= BTF_MAX_NR_TYPES;
3673 * Resolve type ID into its canonical type ID, if any; otherwise return original
3674 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
3675 * STRUCT/UNION link and resolve it into canonical type ID as well.
3677 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
3679 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3680 type_id = d->map[type_id];
3685 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
3688 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
3690 __u32 orig_type_id = type_id;
3692 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3695 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3696 type_id = d->map[type_id];
3698 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3701 return orig_type_id;
3705 static inline __u16 btf_fwd_kind(struct btf_type *t)
3707 return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
3711 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
3712 * call it "candidate graph" in this description for brevity) to a type graph
3713 * formed by (potential) canonical struct/union ("canonical graph" for brevity
3714 * here, though keep in mind that not all types in canonical graph are
3715 * necessarily canonical representatives themselves, some of them might be
3716 * duplicates or its uniqueness might not have been established yet).
3718 * - >0, if type graphs are equivalent;
3719 * - 0, if not equivalent;
3722 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
3723 * equivalence of BTF types at each step. If at any point BTF types in candidate
3724 * and canonical graphs are not compatible structurally, whole graphs are
3725 * incompatible. If types are structurally equivalent (i.e., all information
3726 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
3727 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
3728 * If a type references other types, then those referenced types are checked
3729 * for equivalence recursively.
3731 * During DFS traversal, if we find that for current `canon_id` type we
3732 * already have some mapping in hypothetical map, we check for two possible
3734 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
3735 * happen when type graphs have cycles. In this case we assume those two
3736 * types are equivalent.
3737 * - `canon_id` is mapped to different type. This is contradiction in our
3738 * hypothetical mapping, because same graph in canonical graph corresponds
3739 * to two different types in candidate graph, which for equivalent type
3740 * graphs shouldn't happen. This condition terminates equivalence check
3741 * with negative result.
3743 * If type graphs traversal exhausts types to check and find no contradiction,
3744 * then type graphs are equivalent.
3746 * When checking types for equivalence, there is one special case: FWD types.
3747 * If FWD type resolution is allowed and one of the types (either from canonical
3748 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
3749 * flag) and their names match, hypothetical mapping is updated to point from
3750 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
3751 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
3753 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
3754 * if there are two exactly named (or anonymous) structs/unions that are
3755 * compatible structurally, one of which has FWD field, while other is concrete
3756 * STRUCT/UNION, but according to C sources they are different structs/unions
3757 * that are referencing different types with the same name. This is extremely
3758 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
3759 * this logic is causing problems.
3761 * Doing FWD resolution means that both candidate and/or canonical graphs can
3762 * consists of portions of the graph that come from multiple compilation units.
3763 * This is due to the fact that types within single compilation unit are always
3764 * deduplicated and FWDs are already resolved, if referenced struct/union
3765 * definiton is available. So, if we had unresolved FWD and found corresponding
3766 * STRUCT/UNION, they will be from different compilation units. This
3767 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
3768 * type graph will likely have at least two different BTF types that describe
3769 * same type (e.g., most probably there will be two different BTF types for the
3770 * same 'int' primitive type) and could even have "overlapping" parts of type
3771 * graph that describe same subset of types.
3773 * This in turn means that our assumption that each type in canonical graph
3774 * must correspond to exactly one type in candidate graph might not hold
3775 * anymore and will make it harder to detect contradictions using hypothetical
3776 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
3777 * resolution only in canonical graph. FWDs in candidate graphs are never
3778 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
3780 * - Both types in canonical and candidate graphs are FWDs. If they are
3781 * structurally equivalent, then they can either be both resolved to the
3782 * same STRUCT/UNION or not resolved at all. In both cases they are
3783 * equivalent and there is no need to resolve FWD on candidate side.
3784 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
3785 * so nothing to resolve as well, algorithm will check equivalence anyway.
3786 * - Type in canonical graph is FWD, while type in candidate is concrete
3787 * STRUCT/UNION. In this case candidate graph comes from single compilation
3788 * unit, so there is exactly one BTF type for each unique C type. After
3789 * resolving FWD into STRUCT/UNION, there might be more than one BTF type
3790 * in canonical graph mapping to single BTF type in candidate graph, but
3791 * because hypothetical mapping maps from canonical to candidate types, it's
3792 * alright, and we still maintain the property of having single `canon_id`
3793 * mapping to single `cand_id` (there could be two different `canon_id`
3794 * mapped to the same `cand_id`, but it's not contradictory).
3795 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
3796 * graph is FWD. In this case we are just going to check compatibility of
3797 * STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
3798 * assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
3799 * a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
3800 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
3803 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
3806 struct btf_type *cand_type;
3807 struct btf_type *canon_type;
3808 __u32 hypot_type_id;
3813 /* if both resolve to the same canonical, they must be equivalent */
3814 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
3817 canon_id = resolve_fwd_id(d, canon_id);
3819 hypot_type_id = d->hypot_map[canon_id];
3820 if (hypot_type_id <= BTF_MAX_NR_TYPES)
3821 return hypot_type_id == cand_id;
3823 if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
3826 cand_type = btf_type_by_id(d->btf, cand_id);
3827 canon_type = btf_type_by_id(d->btf, canon_id);
3828 cand_kind = btf_kind(cand_type);
3829 canon_kind = btf_kind(canon_type);
3831 if (cand_type->name_off != canon_type->name_off)
3834 /* FWD <--> STRUCT/UNION equivalence check, if enabled */
3835 if (!d->opts.dont_resolve_fwds
3836 && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
3837 && cand_kind != canon_kind) {
3841 if (cand_kind == BTF_KIND_FWD) {
3842 real_kind = canon_kind;
3843 fwd_kind = btf_fwd_kind(cand_type);
3845 real_kind = cand_kind;
3846 fwd_kind = btf_fwd_kind(canon_type);
3848 return fwd_kind == real_kind;
3851 if (cand_kind != canon_kind)
3854 switch (cand_kind) {
3856 return btf_equal_int(cand_type, canon_type);
3859 if (d->opts.dont_resolve_fwds)
3860 return btf_equal_enum(cand_type, canon_type);
3862 return btf_compat_enum(cand_type, canon_type);
3865 return btf_equal_common(cand_type, canon_type);
3867 case BTF_KIND_CONST:
3868 case BTF_KIND_VOLATILE:
3869 case BTF_KIND_RESTRICT:
3871 case BTF_KIND_TYPEDEF:
3873 if (cand_type->info != canon_type->info)
3875 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
3877 case BTF_KIND_ARRAY: {
3878 const struct btf_array *cand_arr, *canon_arr;
3880 if (!btf_compat_array(cand_type, canon_type))
3882 cand_arr = btf_array(cand_type);
3883 canon_arr = btf_array(canon_type);
3884 eq = btf_dedup_is_equiv(d,
3885 cand_arr->index_type, canon_arr->index_type);
3888 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
3891 case BTF_KIND_STRUCT:
3892 case BTF_KIND_UNION: {
3893 const struct btf_member *cand_m, *canon_m;
3896 if (!btf_shallow_equal_struct(cand_type, canon_type))
3898 vlen = btf_vlen(cand_type);
3899 cand_m = btf_members(cand_type);
3900 canon_m = btf_members(canon_type);
3901 for (i = 0; i < vlen; i++) {
3902 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
3912 case BTF_KIND_FUNC_PROTO: {
3913 const struct btf_param *cand_p, *canon_p;
3916 if (!btf_compat_fnproto(cand_type, canon_type))
3918 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
3921 vlen = btf_vlen(cand_type);
3922 cand_p = btf_params(cand_type);
3923 canon_p = btf_params(canon_type);
3924 for (i = 0; i < vlen; i++) {
3925 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
3941 * Use hypothetical mapping, produced by successful type graph equivalence
3942 * check, to augment existing struct/union canonical mapping, where possible.
3944 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
3945 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
3946 * it doesn't matter if FWD type was part of canonical graph or candidate one,
3947 * we are recording the mapping anyway. As opposed to carefulness required
3948 * for struct/union correspondence mapping (described below), for FWD resolution
3949 * it's not important, as by the time that FWD type (reference type) will be
3950 * deduplicated all structs/unions will be deduped already anyway.
3952 * Recording STRUCT/UNION mapping is purely a performance optimization and is
3953 * not required for correctness. It needs to be done carefully to ensure that
3954 * struct/union from candidate's type graph is not mapped into corresponding
3955 * struct/union from canonical type graph that itself hasn't been resolved into
3956 * canonical representative. The only guarantee we have is that canonical
3957 * struct/union was determined as canonical and that won't change. But any
3958 * types referenced through that struct/union fields could have been not yet
3959 * resolved, so in case like that it's too early to establish any kind of
3960 * correspondence between structs/unions.
3962 * No canonical correspondence is derived for primitive types (they are already
3963 * deduplicated completely already anyway) or reference types (they rely on
3964 * stability of struct/union canonical relationship for equivalence checks).
3966 static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
3968 __u32 cand_type_id, targ_type_id;
3969 __u16 t_kind, c_kind;
3973 for (i = 0; i < d->hypot_cnt; i++) {
3974 cand_type_id = d->hypot_list[i];
3975 targ_type_id = d->hypot_map[cand_type_id];
3976 t_id = resolve_type_id(d, targ_type_id);
3977 c_id = resolve_type_id(d, cand_type_id);
3978 t_kind = btf_kind(btf__type_by_id(d->btf, t_id));
3979 c_kind = btf_kind(btf__type_by_id(d->btf, c_id));
3981 * Resolve FWD into STRUCT/UNION.
3982 * It's ok to resolve FWD into STRUCT/UNION that's not yet
3983 * mapped to canonical representative (as opposed to
3984 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
3985 * eventually that struct is going to be mapped and all resolved
3986 * FWDs will automatically resolve to correct canonical
3987 * representative. This will happen before ref type deduping,
3988 * which critically depends on stability of these mapping. This
3989 * stability is not a requirement for STRUCT/UNION equivalence
3992 if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
3993 d->map[c_id] = t_id;
3994 else if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
3995 d->map[t_id] = c_id;
3997 if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
3998 c_kind != BTF_KIND_FWD &&
3999 is_type_mapped(d, c_id) &&
4000 !is_type_mapped(d, t_id)) {
4002 * as a perf optimization, we can map struct/union
4003 * that's part of type graph we just verified for
4004 * equivalence. We can do that for struct/union that has
4005 * canonical representative only, though.
4007 d->map[t_id] = c_id;
4013 * Deduplicate struct/union types.
4015 * For each struct/union type its type signature hash is calculated, taking
4016 * into account type's name, size, number, order and names of fields, but
4017 * ignoring type ID's referenced from fields, because they might not be deduped
4018 * completely until after reference types deduplication phase. This type hash
4019 * is used to iterate over all potential canonical types, sharing same hash.
4020 * For each canonical candidate we check whether type graphs that they form
4021 * (through referenced types in fields and so on) are equivalent using algorithm
4022 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
4023 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
4024 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
4025 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
4026 * potentially map other structs/unions to their canonical representatives,
4027 * if such relationship hasn't yet been established. This speeds up algorithm
4028 * by eliminating some of the duplicate work.
4030 * If no matching canonical representative was found, struct/union is marked
4031 * as canonical for itself and is added into btf_dedup->dedup_table hash map
4032 * for further look ups.
4034 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
4036 struct btf_type *cand_type, *t;
4037 struct hashmap_entry *hash_entry;
4038 /* if we don't find equivalent type, then we are canonical */
4039 __u32 new_id = type_id;
4043 /* already deduped or is in process of deduping (loop detected) */
4044 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4047 t = btf_type_by_id(d->btf, type_id);
4050 if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4053 h = btf_hash_struct(t);
4054 for_each_dedup_cand(d, hash_entry, h) {
4055 __u32 cand_id = (__u32)(long)hash_entry->value;
4059 * Even though btf_dedup_is_equiv() checks for
4060 * btf_shallow_equal_struct() internally when checking two
4061 * structs (unions) for equivalence, we need to guard here
4062 * from picking matching FWD type as a dedup candidate.
4063 * This can happen due to hash collision. In such case just
4064 * relying on btf_dedup_is_equiv() would lead to potentially
4065 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
4066 * FWD and compatible STRUCT/UNION are considered equivalent.
4068 cand_type = btf_type_by_id(d->btf, cand_id);
4069 if (!btf_shallow_equal_struct(t, cand_type))
4072 btf_dedup_clear_hypot_map(d);
4073 eq = btf_dedup_is_equiv(d, type_id, cand_id);
4079 btf_dedup_merge_hypot_map(d);
4083 d->map[type_id] = new_id;
4084 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4090 static int btf_dedup_struct_types(struct btf_dedup *d)
4094 for (i = 1; i <= d->btf->nr_types; i++) {
4095 err = btf_dedup_struct_type(d, i);
4103 * Deduplicate reference type.
4105 * Once all primitive and struct/union types got deduplicated, we can easily
4106 * deduplicate all other (reference) BTF types. This is done in two steps:
4108 * 1. Resolve all referenced type IDs into their canonical type IDs. This
4109 * resolution can be done either immediately for primitive or struct/union types
4110 * (because they were deduped in previous two phases) or recursively for
4111 * reference types. Recursion will always terminate at either primitive or
4112 * struct/union type, at which point we can "unwind" chain of reference types
4113 * one by one. There is no danger of encountering cycles because in C type
4114 * system the only way to form type cycle is through struct/union, so any chain
4115 * of reference types, even those taking part in a type cycle, will inevitably
4116 * reach struct/union at some point.
4118 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
4119 * becomes "stable", in the sense that no further deduplication will cause
4120 * any changes to it. With that, it's now possible to calculate type's signature
4121 * hash (this time taking into account referenced type IDs) and loop over all
4122 * potential canonical representatives. If no match was found, current type
4123 * will become canonical representative of itself and will be added into
4124 * btf_dedup->dedup_table as another possible canonical representative.
4126 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
4128 struct hashmap_entry *hash_entry;
4129 __u32 new_id = type_id, cand_id;
4130 struct btf_type *t, *cand;
4131 /* if we don't find equivalent type, then we are representative type */
4135 if (d->map[type_id] == BTF_IN_PROGRESS_ID)
4137 if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4138 return resolve_type_id(d, type_id);
4140 t = btf_type_by_id(d->btf, type_id);
4141 d->map[type_id] = BTF_IN_PROGRESS_ID;
4143 switch (btf_kind(t)) {
4144 case BTF_KIND_CONST:
4145 case BTF_KIND_VOLATILE:
4146 case BTF_KIND_RESTRICT:
4148 case BTF_KIND_TYPEDEF:
4150 ref_type_id = btf_dedup_ref_type(d, t->type);
4151 if (ref_type_id < 0)
4153 t->type = ref_type_id;
4155 h = btf_hash_common(t);
4156 for_each_dedup_cand(d, hash_entry, h) {
4157 cand_id = (__u32)(long)hash_entry->value;
4158 cand = btf_type_by_id(d->btf, cand_id);
4159 if (btf_equal_common(t, cand)) {
4166 case BTF_KIND_ARRAY: {
4167 struct btf_array *info = btf_array(t);
4169 ref_type_id = btf_dedup_ref_type(d, info->type);
4170 if (ref_type_id < 0)
4172 info->type = ref_type_id;
4174 ref_type_id = btf_dedup_ref_type(d, info->index_type);
4175 if (ref_type_id < 0)
4177 info->index_type = ref_type_id;
4179 h = btf_hash_array(t);
4180 for_each_dedup_cand(d, hash_entry, h) {
4181 cand_id = (__u32)(long)hash_entry->value;
4182 cand = btf_type_by_id(d->btf, cand_id);
4183 if (btf_equal_array(t, cand)) {
4191 case BTF_KIND_FUNC_PROTO: {
4192 struct btf_param *param;
4196 ref_type_id = btf_dedup_ref_type(d, t->type);
4197 if (ref_type_id < 0)
4199 t->type = ref_type_id;
4202 param = btf_params(t);
4203 for (i = 0; i < vlen; i++) {
4204 ref_type_id = btf_dedup_ref_type(d, param->type);
4205 if (ref_type_id < 0)
4207 param->type = ref_type_id;
4211 h = btf_hash_fnproto(t);
4212 for_each_dedup_cand(d, hash_entry, h) {
4213 cand_id = (__u32)(long)hash_entry->value;
4214 cand = btf_type_by_id(d->btf, cand_id);
4215 if (btf_equal_fnproto(t, cand)) {
4227 d->map[type_id] = new_id;
4228 if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4234 static int btf_dedup_ref_types(struct btf_dedup *d)
4238 for (i = 1; i <= d->btf->nr_types; i++) {
4239 err = btf_dedup_ref_type(d, i);
4243 /* we won't need d->dedup_table anymore */
4244 hashmap__free(d->dedup_table);
4245 d->dedup_table = NULL;
4252 * After we established for each type its corresponding canonical representative
4253 * type, we now can eliminate types that are not canonical and leave only
4254 * canonical ones layed out sequentially in memory by copying them over
4255 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
4256 * a map from original type ID to a new compacted type ID, which will be used
4257 * during next phase to "fix up" type IDs, referenced from struct/union and
4260 static int btf_dedup_compact_types(struct btf_dedup *d)
4263 __u32 next_type_id = 1;
4267 /* we are going to reuse hypot_map to store compaction remapping */
4268 d->hypot_map[0] = 0;
4269 for (i = 1; i <= d->btf->nr_types; i++)
4270 d->hypot_map[i] = BTF_UNPROCESSED_ID;
4272 p = d->btf->types_data;
4274 for (i = 1; i <= d->btf->nr_types; i++) {
4278 len = btf_type_size(btf__type_by_id(d->btf, i));
4282 memmove(p, btf__type_by_id(d->btf, i), len);
4283 d->hypot_map[i] = next_type_id;
4284 d->btf->type_offs[next_type_id] = p - d->btf->types_data;
4289 /* shrink struct btf's internal types index and update btf_header */
4290 d->btf->nr_types = next_type_id - 1;
4291 d->btf->type_offs_cap = d->btf->nr_types + 1;
4292 d->btf->hdr->type_len = p - d->btf->types_data;
4293 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
4297 d->btf->type_offs = new_offs;
4298 d->btf->hdr->str_off = d->btf->hdr->type_len;
4299 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
4304 * Figure out final (deduplicated and compacted) type ID for provided original
4305 * `type_id` by first resolving it into corresponding canonical type ID and
4306 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
4307 * which is populated during compaction phase.
4309 static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
4311 __u32 resolved_type_id, new_type_id;
4313 resolved_type_id = resolve_type_id(d, type_id);
4314 new_type_id = d->hypot_map[resolved_type_id];
4315 if (new_type_id > BTF_MAX_NR_TYPES)
4321 * Remap referenced type IDs into deduped type IDs.
4323 * After BTF types are deduplicated and compacted, their final type IDs may
4324 * differ from original ones. The map from original to a corresponding
4325 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
4326 * compaction phase. During remapping phase we are rewriting all type IDs
4327 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
4328 * their final deduped type IDs.
4330 static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
4332 struct btf_type *t = btf_type_by_id(d->btf, type_id);
4335 switch (btf_kind(t)) {
4341 case BTF_KIND_CONST:
4342 case BTF_KIND_VOLATILE:
4343 case BTF_KIND_RESTRICT:
4345 case BTF_KIND_TYPEDEF:
4348 r = btf_dedup_remap_type_id(d, t->type);
4354 case BTF_KIND_ARRAY: {
4355 struct btf_array *arr_info = btf_array(t);
4357 r = btf_dedup_remap_type_id(d, arr_info->type);
4361 r = btf_dedup_remap_type_id(d, arr_info->index_type);
4364 arr_info->index_type = r;
4368 case BTF_KIND_STRUCT:
4369 case BTF_KIND_UNION: {
4370 struct btf_member *member = btf_members(t);
4371 __u16 vlen = btf_vlen(t);
4373 for (i = 0; i < vlen; i++) {
4374 r = btf_dedup_remap_type_id(d, member->type);
4383 case BTF_KIND_FUNC_PROTO: {
4384 struct btf_param *param = btf_params(t);
4385 __u16 vlen = btf_vlen(t);
4387 r = btf_dedup_remap_type_id(d, t->type);
4392 for (i = 0; i < vlen; i++) {
4393 r = btf_dedup_remap_type_id(d, param->type);
4402 case BTF_KIND_DATASEC: {
4403 struct btf_var_secinfo *var = btf_var_secinfos(t);
4404 __u16 vlen = btf_vlen(t);
4406 for (i = 0; i < vlen; i++) {
4407 r = btf_dedup_remap_type_id(d, var->type);
4423 static int btf_dedup_remap_types(struct btf_dedup *d)
4427 for (i = 1; i <= d->btf->nr_types; i++) {
4428 r = btf_dedup_remap_type(d, i);
4436 * Probe few well-known locations for vmlinux kernel image and try to load BTF
4437 * data out of it to use for target BTF.
4439 struct btf *libbpf_find_kernel_btf(void)
4442 const char *path_fmt;
4445 /* try canonical vmlinux BTF through sysfs first */
4446 { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
4447 /* fall back to trying to find vmlinux ELF on disk otherwise */
4448 { "/boot/vmlinux-%1$s" },
4449 { "/lib/modules/%1$s/vmlinux-%1$s" },
4450 { "/lib/modules/%1$s/build/vmlinux" },
4451 { "/usr/lib/modules/%1$s/kernel/vmlinux" },
4452 { "/usr/lib/debug/boot/vmlinux-%1$s" },
4453 { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
4454 { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
4456 char path[PATH_MAX + 1];
4463 for (i = 0; i < ARRAY_SIZE(locations); i++) {
4464 snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
4466 if (access(path, R_OK))
4469 if (locations[i].raw_btf)
4470 btf = btf__parse_raw(path);
4472 btf = btf__parse_elf(path, NULL);
4474 pr_debug("loading kernel BTF '%s': %ld\n",
4475 path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
4482 pr_warn("failed to find valid kernel BTF\n");
4483 return ERR_PTR(-ESRCH);