Lines Matching +full:three +full:- +full:conversion +full:- +full:cycles
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
34 /* raw BTF data in non-native endianness */
46 * +--------------------------------+
48 * +--------------------------------+
52 * types_data-+ |
53 * strs_data------------+
57 * representation is broken up into three independently allocated
64 * +----------+ +---------+ +-----------+
66 * +----------+ +---------+ +-----------+
70 * types_data----+ |
71 * strset__data(strs_set)-----+
73 * +----------+---------+-----------+
75 * raw_data----->+----------+---------+-----------+
80 size_t types_data_cap; /* used size stored in hdr->type_len */
83 * type_offs[0] corresponds to the first non-VOID type:
84 * - for base BTF it's type [1];
85 * - for split BTF it's the first non-base BTF type.
90 * - doesn't include special [0] void type;
91 * - for split BTF counts number of types added on top of base BTF.
99 * - for base BTF it's equal to 1;
100 * - for split BTF it's equal to biggest type ID of base BTF plus 1.
104 * - for base BTF it's equal to 0;
105 * - for split BTF it's equal to total size of base BTF's string section size.
109 /* only one of strs_data or strs_set can be non-NULL, depending on
171 memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz); in libbpf_add_mem()
188 p = libbpf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt); in libbpf_ensure_mem()
190 return -ENOMEM; in libbpf_ensure_mem()
197 return libbpf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32), in btf_add_type_offs_mem()
198 btf->nr_types, BTF_MAX_NR_TYPES, add_cnt); in btf_add_type_offs_mem()
207 return -ENOMEM; in btf_add_type_idx_entry()
215 h->magic = bswap_16(h->magic); in btf_bswap_hdr()
216 h->hdr_len = bswap_32(h->hdr_len); in btf_bswap_hdr()
217 h->type_off = bswap_32(h->type_off); in btf_bswap_hdr()
218 h->type_len = bswap_32(h->type_len); in btf_bswap_hdr()
219 h->str_off = bswap_32(h->str_off); in btf_bswap_hdr()
220 h->str_len = bswap_32(h->str_len); in btf_bswap_hdr()
225 struct btf_header *hdr = btf->hdr; in btf_parse_hdr()
228 if (btf->raw_size < sizeof(struct btf_header)) { in btf_parse_hdr()
230 return -EINVAL; in btf_parse_hdr()
233 if (hdr->magic == bswap_16(BTF_MAGIC)) { in btf_parse_hdr()
234 btf->swapped_endian = true; in btf_parse_hdr()
235 if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) { in btf_parse_hdr()
236 pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n", in btf_parse_hdr()
237 bswap_32(hdr->hdr_len)); in btf_parse_hdr()
238 return -ENOTSUP; in btf_parse_hdr()
241 } else if (hdr->magic != BTF_MAGIC) { in btf_parse_hdr()
242 pr_debug("Invalid BTF magic: %x\n", hdr->magic); in btf_parse_hdr()
243 return -EINVAL; in btf_parse_hdr()
246 if (btf->raw_size < hdr->hdr_len) { in btf_parse_hdr()
248 hdr->hdr_len, btf->raw_size); in btf_parse_hdr()
249 return -EINVAL; in btf_parse_hdr()
252 meta_left = btf->raw_size - hdr->hdr_len; in btf_parse_hdr()
253 if (meta_left < (long long)hdr->str_off + hdr->str_len) { in btf_parse_hdr()
254 pr_debug("Invalid BTF total size: %u\n", btf->raw_size); in btf_parse_hdr()
255 return -EINVAL; in btf_parse_hdr()
258 if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) { in btf_parse_hdr()
260 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len); in btf_parse_hdr()
261 return -EINVAL; in btf_parse_hdr()
264 if (hdr->type_off % 4) { in btf_parse_hdr()
266 return -EINVAL; in btf_parse_hdr()
274 const struct btf_header *hdr = btf->hdr; in btf_parse_str_sec()
275 const char *start = btf->strs_data; in btf_parse_str_sec()
276 const char *end = start + btf->hdr->str_len; in btf_parse_str_sec()
278 if (btf->base_btf && hdr->str_len == 0) in btf_parse_str_sec()
280 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) { in btf_parse_str_sec()
282 return -EINVAL; in btf_parse_str_sec()
284 if (!btf->base_btf && start[0]) { in btf_parse_str_sec()
286 return -EINVAL; in btf_parse_str_sec()
328 return -EINVAL; in btf_type_size()
334 t->name_off = bswap_32(t->name_off); in btf_bswap_type_base()
335 t->info = bswap_32(t->info); in btf_bswap_type_base()
336 t->type = bswap_32(t->type); in btf_bswap_type_base()
366 e->name_off = bswap_32(e->name_off); in btf_bswap_type_rest()
367 e->val = bswap_32(e->val); in btf_bswap_type_rest()
372 e64->name_off = bswap_32(e64->name_off); in btf_bswap_type_rest()
373 e64->val_lo32 = bswap_32(e64->val_lo32); in btf_bswap_type_rest()
374 e64->val_hi32 = bswap_32(e64->val_hi32); in btf_bswap_type_rest()
379 a->type = bswap_32(a->type); in btf_bswap_type_rest()
380 a->index_type = bswap_32(a->index_type); in btf_bswap_type_rest()
381 a->nelems = bswap_32(a->nelems); in btf_bswap_type_rest()
386 m->name_off = bswap_32(m->name_off); in btf_bswap_type_rest()
387 m->type = bswap_32(m->type); in btf_bswap_type_rest()
388 m->offset = bswap_32(m->offset); in btf_bswap_type_rest()
393 p->name_off = bswap_32(p->name_off); in btf_bswap_type_rest()
394 p->type = bswap_32(p->type); in btf_bswap_type_rest()
398 btf_var(t)->linkage = bswap_32(btf_var(t)->linkage); in btf_bswap_type_rest()
402 v->type = bswap_32(v->type); in btf_bswap_type_rest()
403 v->offset = bswap_32(v->offset); in btf_bswap_type_rest()
404 v->size = bswap_32(v->size); in btf_bswap_type_rest()
408 btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx); in btf_bswap_type_rest()
412 return -EINVAL; in btf_bswap_type_rest()
418 struct btf_header *hdr = btf->hdr; in btf_parse_type_sec()
419 void *next_type = btf->types_data; in btf_parse_type_sec()
420 void *end_type = next_type + hdr->type_len; in btf_parse_type_sec()
424 if (btf->swapped_endian) in btf_parse_type_sec()
431 pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types); in btf_parse_type_sec()
432 return -EINVAL; in btf_parse_type_sec()
435 if (btf->swapped_endian && btf_bswap_type_rest(next_type)) in btf_parse_type_sec()
436 return -EINVAL; in btf_parse_type_sec()
438 err = btf_add_type_idx_entry(btf, next_type - btf->types_data); in btf_parse_type_sec()
443 btf->nr_types++; in btf_parse_type_sec()
448 return -EINVAL; in btf_parse_type_sec()
461 return -EINVAL; in btf_validate_str()
474 return -EINVAL; in btf_validate_id()
485 err = btf_validate_str(btf, t->name_off, "type name", id); in btf_validate_type()
503 err = btf_validate_id(btf, t->type, id); in btf_validate_type()
510 err = btf_validate_id(btf, a->type, id); in btf_validate_type()
511 err = err ?: btf_validate_id(btf, a->index_type, id); in btf_validate_type()
522 err = btf_validate_str(btf, m->name_off, "field name", id); in btf_validate_type()
523 err = err ?: btf_validate_id(btf, m->type, id); in btf_validate_type()
534 err = btf_validate_str(btf, m->name_off, "enum name", id); in btf_validate_type()
545 err = btf_validate_str(btf, m->name_off, "enum name", id); in btf_validate_type()
554 err = btf_validate_id(btf, t->type, id); in btf_validate_type()
557 ft = btf__type_by_id(btf, t->type); in btf_validate_type()
559 pr_warn("btf: type [%u]: referenced type [%u] is not FUNC_PROTO\n", id, t->type); in btf_validate_type()
560 return -EINVAL; in btf_validate_type()
569 err = btf_validate_str(btf, m->name_off, "param name", id); in btf_validate_type()
570 err = err ?: btf_validate_id(btf, m->type, id); in btf_validate_type()
581 err = btf_validate_id(btf, m->type, id); in btf_validate_type()
589 return -EINVAL; in btf_validate_type()
604 for (i = btf->start_id; i < n; i++) { in btf_sanity_check()
615 return btf->start_id + btf->nr_types; in btf__type_cnt()
620 return btf->base_btf; in btf__base_btf()
623 /* internal helper returning non-const pointer to a type */
628 if (type_id < btf->start_id) in btf_type_by_id()
629 return btf_type_by_id(btf->base_btf, type_id); in btf_type_by_id()
630 return btf->types_data + btf->type_offs[type_id - btf->start_id]; in btf_type_by_id()
635 if (type_id >= btf->start_id + btf->nr_types) in btf__type_by_id()
659 if (btf->base_btf && btf->base_btf->ptr_sz > 0) in determine_ptr_size()
660 return btf->base_btf->ptr_sz; in determine_ptr_size()
668 if (t->size != 4 && t->size != 8) in determine_ptr_size()
671 name = btf__name_by_offset(btf, t->name_off); in determine_ptr_size()
677 return t->size; in determine_ptr_size()
681 return -1; in determine_ptr_size()
686 if (!btf->ptr_sz) in btf_ptr_sz()
687 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); in btf_ptr_sz()
688 return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz; in btf_ptr_sz()
701 if (!btf->ptr_sz) in btf__pointer_size()
702 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); in btf__pointer_size()
704 if (btf->ptr_sz < 0) in btf__pointer_size()
708 return btf->ptr_sz; in btf__pointer_size()
717 return libbpf_err(-EINVAL); in btf__set_pointer_size()
718 btf->ptr_sz = ptr_sz; in btf__set_pointer_size()
736 return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; in btf__endianness()
738 return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; in btf__endianness()
744 return libbpf_err(-EINVAL); in btf__set_endianness()
746 btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); in btf__set_endianness()
747 if (!btf->swapped_endian) { in btf__set_endianness()
748 free(btf->raw_data_swapped); in btf__set_endianness()
749 btf->raw_data_swapped = NULL; in btf__set_endianness()
771 __s64 size = -1; in btf__resolve_size()
784 size = t->size; in btf__resolve_size()
796 type_id = t->type; in btf__resolve_size()
800 if (nelems && array->nelems > UINT32_MAX / nelems) in btf__resolve_size()
801 return libbpf_err(-E2BIG); in btf__resolve_size()
802 nelems *= array->nelems; in btf__resolve_size()
803 type_id = array->type; in btf__resolve_size()
806 return libbpf_err(-EINVAL); in btf__resolve_size()
814 return libbpf_err(-EINVAL); in btf__resolve_size()
816 return libbpf_err(-E2BIG); in btf__resolve_size()
831 return min(btf_ptr_sz(btf), (size_t)t->size); in btf__align_of()
839 return btf__align_of(btf, t->type); in btf__align_of()
841 return btf__align_of(btf, btf_array(t)->type); in btf__align_of()
849 align = btf__align_of(btf, m->type); in btf__align_of()
858 (m->offset % (8 * align)) != 0) in btf__align_of()
865 if ((t->size % max_align) != 0) in btf__align_of()
885 type_id = t->type; in btf__resolve_type()
891 return libbpf_err(-EINVAL); in btf__resolve_type()
905 const char *name = btf__name_by_offset(btf, t->name_off); in btf__find_by_name()
911 return libbpf_err(-ENOENT); in btf__find_by_name()
928 name = btf__name_by_offset(btf, t->name_off); in btf_find_by_name_kind()
933 return libbpf_err(-ENOENT); in btf_find_by_name_kind()
939 return btf_find_by_name_kind(btf, btf->start_id, type_name, kind); in btf__find_by_name_kind_own()
950 return (void *)btf->hdr != btf->raw_data; in btf_is_modifiable()
958 if (btf->fd >= 0) in btf__free()
959 close(btf->fd); in btf__free()
963 * in-memory representation for header, types, and strings in btf__free()
968 free(btf->hdr); in btf__free()
969 free(btf->types_data); in btf__free()
970 strset__free(btf->strs_set); in btf__free()
972 free(btf->raw_data); in btf__free()
973 free(btf->raw_data_swapped); in btf__free()
974 free(btf->type_offs); in btf__free()
975 if (btf->owns_base) in btf__free()
976 btf__free(btf->base_btf); in btf__free()
986 return ERR_PTR(-ENOMEM); in btf_new_empty()
988 btf->nr_types = 0; in btf_new_empty()
989 btf->start_id = 1; in btf_new_empty()
990 btf->start_str_off = 0; in btf_new_empty()
991 btf->fd = -1; in btf_new_empty()
992 btf->ptr_sz = sizeof(void *); in btf_new_empty()
993 btf->swapped_endian = false; in btf_new_empty()
996 btf->base_btf = base_btf; in btf_new_empty()
997 btf->start_id = btf__type_cnt(base_btf); in btf_new_empty()
998 btf->start_str_off = base_btf->hdr->str_len; in btf_new_empty()
999 btf->swapped_endian = base_btf->swapped_endian; in btf_new_empty()
1003 btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1); in btf_new_empty()
1004 btf->raw_data = calloc(1, btf->raw_size); in btf_new_empty()
1005 if (!btf->raw_data) { in btf_new_empty()
1007 return ERR_PTR(-ENOMEM); in btf_new_empty()
1010 btf->hdr = btf->raw_data; in btf_new_empty()
1011 btf->hdr->hdr_len = sizeof(struct btf_header); in btf_new_empty()
1012 btf->hdr->magic = BTF_MAGIC; in btf_new_empty()
1013 btf->hdr->version = BTF_VERSION; in btf_new_empty()
1015 btf->types_data = btf->raw_data + btf->hdr->hdr_len; in btf_new_empty()
1016 btf->strs_data = btf->raw_data + btf->hdr->hdr_len; in btf_new_empty()
1017 btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */ in btf_new_empty()
1039 return ERR_PTR(-ENOMEM); in btf_new()
1041 btf->nr_types = 0; in btf_new()
1042 btf->start_id = 1; in btf_new()
1043 btf->start_str_off = 0; in btf_new()
1044 btf->fd = -1; in btf_new()
1047 btf->base_btf = base_btf; in btf_new()
1048 btf->start_id = btf__type_cnt(base_btf); in btf_new()
1049 btf->start_str_off = base_btf->hdr->str_len; in btf_new()
1052 btf->raw_data = malloc(size); in btf_new()
1053 if (!btf->raw_data) { in btf_new()
1054 err = -ENOMEM; in btf_new()
1057 memcpy(btf->raw_data, data, size); in btf_new()
1058 btf->raw_size = size; in btf_new()
1060 btf->hdr = btf->raw_data; in btf_new()
1065 btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off; in btf_new()
1066 btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off; in btf_new()
1142 field = &secs->btf_data; in btf_find_elf_sections()
1144 field = &secs->btf_ext_data; in btf_find_elf_sections()
1146 field = &secs->btf_base_data; in btf_find_elf_sections()
1162 return -LIBBPF_ERRNO__FORMAT; in btf_find_elf_sections()
1171 int err = 0, fd = -1; in btf_parse_elf()
1176 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); in btf_parse_elf()
1181 err = -errno; in btf_parse_elf()
1198 err = -ENODATA; in btf_parse_elf()
1203 dist_base_btf = btf_new(secs.btf_base_data->d_buf, secs.btf_base_data->d_size, in btf_parse_elf()
1212 btf = btf_new(secs.btf_data->d_buf, secs.btf_data->d_size, in btf_parse_elf()
1227 btf->owns_base = true; in btf_parse_elf()
1242 *btf_ext = btf_ext__new(secs.btf_ext_data->d_buf, secs.btf_ext_data->d_size); in btf_parse_elf()
1287 err = -errno; in btf_parse_raw()
1293 err = -EIO; in btf_parse_raw()
1298 err = -EPROTO; in btf_parse_raw()
1304 err = -errno; in btf_parse_raw()
1309 err = -errno; in btf_parse_raw()
1314 err = -errno; in btf_parse_raw()
1318 /* pre-alloc memory and read all of BTF data */ in btf_parse_raw()
1321 err = -ENOMEM; in btf_parse_raw()
1325 err = -EIO; in btf_parse_raw()
1361 if (err != -EPROTO) in btf_parse()
1388 if (btf->fd >= 0) in btf_load_into_kernel()
1389 return libbpf_err(-EEXIST); in btf_load_into_kernel()
1391 return libbpf_err(-EINVAL); in btf_load_into_kernel()
1396 err = -ENOMEM; in btf_load_into_kernel()
1399 btf->raw_size = raw_size; in btf_load_into_kernel()
1400 btf->raw_data = raw_data; in btf_load_into_kernel()
1405 * retry, using either auto-allocated or custom log_buf. This way in btf_load_into_kernel()
1406 * non-NULL custom log_buf provides a buffer just in case, but hopes in btf_load_into_kernel()
1418 err = -ENOMEM; in btf_load_into_kernel()
1434 btf->fd = bpf_btf_load(raw_data, raw_size, &opts); in btf_load_into_kernel()
1435 if (btf->fd < 0) { in btf_load_into_kernel()
1447 err = -errno; in btf_load_into_kernel()
1451 pr_warn("-- BEGIN BTF LOAD LOG ---\n%s\n-- END BTF LOAD LOG --\n", buf); in btf_load_into_kernel()
1466 return btf->fd; in btf__fd()
1471 btf->fd = fd; in btf__set_fd()
1476 return btf->strs_data ? btf->strs_data : strset__data(btf->strs_set); in btf_strs_data()
1481 struct btf_header *hdr = btf->hdr; in btf_get_raw_data()
1487 data = swap_endian ? btf->raw_data_swapped : btf->raw_data; in btf_get_raw_data()
1489 *size = btf->raw_size; in btf_get_raw_data()
1493 data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len; in btf_get_raw_data()
1499 memcpy(p, hdr, hdr->hdr_len); in btf_get_raw_data()
1502 p += hdr->hdr_len; in btf_get_raw_data()
1504 memcpy(p, btf->types_data, hdr->type_len); in btf_get_raw_data()
1506 for (i = 0; i < btf->nr_types; i++) { in btf_get_raw_data()
1507 t = p + btf->type_offs[i]; in btf_get_raw_data()
1508 /* btf_bswap_type_rest() relies on native t->info, so in btf_get_raw_data()
1517 p += hdr->type_len; in btf_get_raw_data()
1519 memcpy(p, btf_strs_data(btf), hdr->str_len); in btf_get_raw_data()
1520 p += hdr->str_len; in btf_get_raw_data()
1535 data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian); in btf__raw_data()
1539 btf->raw_size = data_sz; in btf__raw_data()
1540 if (btf->swapped_endian) in btf__raw_data()
1541 btf->raw_data_swapped = data; in btf__raw_data()
1543 btf->raw_data = data; in btf__raw_data()
1553 if (offset < btf->start_str_off) in btf__str_by_offset()
1554 return btf__str_by_offset(btf->base_btf, offset); in btf__str_by_offset()
1555 else if (offset - btf->start_str_off < btf->hdr->str_len) in btf__str_by_offset()
1556 return btf_strs_data(btf) + (offset - btf->start_str_off); in btf__str_by_offset()
1576 * let's start with a sane default - 4KiB here - and resize it only if in btf_get_from_fd()
1582 return ERR_PTR(-ENOMEM); in btf_get_from_fd()
1595 btf = ERR_PTR(-ENOMEM); in btf_get_from_fd()
1609 btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG); in btf_get_from_fd()
1627 return libbpf_err_ptr(-errno); in btf__load_from_kernel_by_id_split()
1642 if (btf->raw_data) { in btf_invalidate_raw_data()
1643 free(btf->raw_data); in btf_invalidate_raw_data()
1644 btf->raw_data = NULL; in btf_invalidate_raw_data()
1646 if (btf->raw_data_swapped) { in btf_invalidate_raw_data()
1647 free(btf->raw_data_swapped); in btf_invalidate_raw_data()
1648 btf->raw_data_swapped = NULL; in btf_invalidate_raw_data()
1652 /* Ensure BTF is ready to be modified (by splitting into a three memory
1660 int err = -ENOMEM; in btf_ensure_modifiable()
1668 /* split raw data into three memory regions */ in btf_ensure_modifiable()
1669 hdr = malloc(btf->hdr->hdr_len); in btf_ensure_modifiable()
1670 types = malloc(btf->hdr->type_len); in btf_ensure_modifiable()
1674 memcpy(hdr, btf->hdr, btf->hdr->hdr_len); in btf_ensure_modifiable()
1675 memcpy(types, btf->types_data, btf->hdr->type_len); in btf_ensure_modifiable()
1678 set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr->str_len); in btf_ensure_modifiable()
1685 btf->hdr = hdr; in btf_ensure_modifiable()
1686 btf->types_data = types; in btf_ensure_modifiable()
1687 btf->types_data_cap = btf->hdr->type_len; in btf_ensure_modifiable()
1688 btf->strs_data = NULL; in btf_ensure_modifiable()
1689 btf->strs_set = set; in btf_ensure_modifiable()
1693 if (btf->hdr->str_len == 0) in btf_ensure_modifiable()
1694 btf->strs_deduped = true; in btf_ensure_modifiable()
1695 if (!btf->base_btf && btf->hdr->str_len == 1) in btf_ensure_modifiable()
1696 btf->strs_deduped = true; in btf_ensure_modifiable()
1712 * - >0 offset into string section, if string is found;
1713 * - -ENOENT, if string is not in the string section;
1714 * - <0, on any other error.
1720 if (btf->base_btf) { in btf__find_str()
1721 off = btf__find_str(btf->base_btf, s); in btf__find_str()
1722 if (off != -ENOENT) in btf__find_str()
1728 return libbpf_err(-ENOMEM); in btf__find_str()
1730 off = strset__find_str(btf->strs_set, s); in btf__find_str()
1734 return btf->start_str_off + off; in btf__find_str()
1739 * - > 0 offset into string section, on success;
1740 * - < 0, on error.
1746 if (btf->base_btf) { in btf__add_str()
1747 off = btf__find_str(btf->base_btf, s); in btf__add_str()
1748 if (off != -ENOENT) in btf__add_str()
1753 return libbpf_err(-ENOMEM); in btf__add_str()
1755 off = strset__add_str(btf->strs_set, s); in btf__add_str()
1759 btf->hdr->str_len = strset__data_size(btf->strs_set); in btf__add_str()
1761 return btf->start_str_off + off; in btf__add_str()
1766 return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1, in btf_add_type_mem()
1767 btf->hdr->type_len, UINT_MAX, add_sz); in btf_add_type_mem()
1772 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t)); in btf_type_inc_vlen()
1779 err = btf_add_type_idx_entry(btf, btf->hdr->type_len); in btf_commit_type()
1783 btf->hdr->type_len += data_sz; in btf_commit_type()
1784 btf->hdr->str_off += data_sz; in btf_commit_type()
1785 btf->nr_types++; in btf_commit_type()
1786 return btf->start_id + btf->nr_types - 1; in btf_commit_type()
1803 if (p->str_off_map && in btf_rewrite_str()
1804 hashmap__find(p->str_off_map, *str_off, &mapped_off)) { in btf_rewrite_str()
1809 off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off)); in btf_rewrite_str()
1816 if (p->str_off_map) { in btf_rewrite_str()
1817 err = hashmap__append(p->str_off_map, *str_off, off); in btf_rewrite_str()
1838 if (btf_ensure_modifiable(p->dst)) in btf_add_type()
1839 return libbpf_err(-ENOMEM); in btf_add_type()
1841 t = btf_add_type_mem(p->dst, sz); in btf_add_type()
1843 return libbpf_err(-ENOMEM); in btf_add_type()
1857 return btf_commit_type(p->dst, sz); in btf_add_type()
1878 if (src_btf->base_btf) in btf__add_btf()
1879 return libbpf_err(-ENOTSUP); in btf__add_btf()
1883 return libbpf_err(-ENOMEM); in btf__add_btf()
1888 old_strs_len = btf->hdr->str_len; in btf__add_btf()
1890 data_sz = src_btf->hdr->type_len; in btf__add_btf()
1891 cnt = btf__type_cnt(src_btf) - 1; in btf__add_btf()
1893 /* pre-allocate enough memory for new types */ in btf__add_btf()
1896 return libbpf_err(-ENOMEM); in btf__add_btf()
1898 /* pre-allocate enough memory for type offset index for new types */ in btf__add_btf()
1901 return libbpf_err(-ENOMEM); in btf__add_btf()
1906 return libbpf_err(-ENOMEM); in btf__add_btf()
1909 memcpy(t, src_btf->types_data, data_sz); in btf__add_btf()
1923 *off = t - btf->types_data; in btf__add_btf()
1945 * btf->start_id + btf->nr_types - 1 is the type ID offset we should in btf__add_btf()
1948 *type_id += btf->start_id + btf->nr_types - 1; in btf__add_btf()
1959 * pre-allocated, but it would not be available for querying. But now in btf__add_btf()
1964 btf->hdr->type_len += data_sz; in btf__add_btf()
1965 btf->hdr->str_off += data_sz; in btf__add_btf()
1966 btf->nr_types += cnt; in btf__add_btf()
1971 return btf->start_id + btf->nr_types - cnt; in btf__add_btf()
1976 memset(btf->types_data + btf->hdr->type_len, 0, data_sz); in btf__add_btf()
1977 memset(btf->strs_data + old_strs_len, 0, btf->hdr->str_len - old_strs_len); in btf__add_btf()
1982 btf->hdr->str_len = old_strs_len; in btf__add_btf()
1991 * - *name* - non-empty, non-NULL type name;
1992 * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
1993 * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
1995 * - >0, type ID of newly added BTF type;
1996 * - <0, on error.
2003 /* non-empty name */ in btf__add_int()
2005 return libbpf_err(-EINVAL); in btf__add_int()
2007 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16) in btf__add_int()
2008 return libbpf_err(-EINVAL); in btf__add_int()
2010 return libbpf_err(-EINVAL); in btf__add_int()
2014 return libbpf_err(-ENOMEM); in btf__add_int()
2019 return libbpf_err(-ENOMEM); in btf__add_int()
2029 t->name_off = name_off; in btf__add_int()
2030 t->info = btf_type_info(BTF_KIND_INT, 0, 0); in btf__add_int()
2031 t->size = byte_sz; in btf__add_int()
2040 * - *name* - non-empty, non-NULL type name;
2041 * - *sz* - size of the type, in bytes;
2043 * - >0, type ID of newly added BTF type;
2044 * - <0, on error.
2051 /* non-empty name */ in btf__add_float()
2053 return libbpf_err(-EINVAL); in btf__add_float()
2058 return libbpf_err(-EINVAL); in btf__add_float()
2061 return libbpf_err(-ENOMEM); in btf__add_float()
2066 return libbpf_err(-ENOMEM); in btf__add_float()
2072 t->name_off = name_off; in btf__add_float()
2073 t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0); in btf__add_float()
2074 t->size = byte_sz; in btf__add_float()
2086 return -EINVAL; in validate_type_id()
2097 return libbpf_err(-EINVAL); in btf_add_ref_kind()
2100 return libbpf_err(-ENOMEM); in btf_add_ref_kind()
2105 return libbpf_err(-ENOMEM); in btf_add_ref_kind()
2113 t->name_off = name_off; in btf_add_ref_kind()
2114 t->info = btf_type_info(kind, 0, 0); in btf_add_ref_kind()
2115 t->type = ref_type_id; in btf_add_ref_kind()
2122 * - *ref_type_id* - referenced type ID, it might not exist yet;
2124 * - >0, type ID of newly added BTF type;
2125 * - <0, on error.
2134 * - *index_type_id* - type ID of the type describing array index;
2135 * - *elem_type_id* - type ID of the type describing array element;
2136 * - *nr_elems* - the size of the array;
2138 * - >0, type ID of newly added BTF type;
2139 * - <0, on error.
2148 return libbpf_err(-EINVAL); in btf__add_array()
2151 return libbpf_err(-ENOMEM); in btf__add_array()
2156 return libbpf_err(-ENOMEM); in btf__add_array()
2158 t->name_off = 0; in btf__add_array()
2159 t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0); in btf__add_array()
2160 t->size = 0; in btf__add_array()
2163 a->type = elem_type_id; in btf__add_array()
2164 a->index_type = index_type_id; in btf__add_array()
2165 a->nelems = nr_elems; in btf__add_array()
2177 return libbpf_err(-ENOMEM); in btf_add_composite()
2182 return libbpf_err(-ENOMEM); in btf_add_composite()
2193 t->name_off = name_off; in btf_add_composite()
2194 t->info = btf_type_info(kind, 0, 0); in btf_add_composite()
2195 t->size = bytes_sz; in btf_add_composite()
2202 * - *name* - name of the struct, can be NULL or empty for anonymous structs;
2203 * - *byte_sz* - size of the struct, in bytes;
2209 * - >0, type ID of newly added BTF type;
2210 * - <0, on error.
2219 * - *name* - name of the union, can be NULL or empty for anonymous union;
2220 * - *byte_sz* - size of the union, in bytes;
2227 * - >0, type ID of newly added BTF type;
2228 * - <0, on error.
2237 return btf_type_by_id(btf, btf__type_cnt(btf) - 1); in btf_last_type()
2242 * - *name* - name of the field, can be NULL or empty for anonymous field;
2243 * - *type_id* - type ID for the type describing field type;
2244 * - *bit_offset* - bit offset of the start of the field within struct/union;
2245 * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
2247 * - 0, on success;
2248 * - <0, on error.
2259 if (btf->nr_types == 0) in btf__add_field()
2260 return libbpf_err(-EINVAL); in btf__add_field()
2263 return libbpf_err(-EINVAL); in btf__add_field()
2266 return libbpf_err(-EINVAL); in btf__add_field()
2267 /* best-effort bit field offset/size enforcement */ in btf__add_field()
2270 return libbpf_err(-EINVAL); in btf__add_field()
2274 return libbpf_err(-EINVAL); in btf__add_field()
2278 return libbpf_err(-ENOMEM); in btf__add_field()
2283 return libbpf_err(-ENOMEM); in btf__add_field()
2291 m->name_off = name_off; in btf__add_field()
2292 m->type = type_id; in btf__add_field()
2293 m->offset = bit_offset | (bit_size << 24); in btf__add_field()
2298 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t)); in btf__add_field()
2300 btf->hdr->type_len += sz; in btf__add_field()
2301 btf->hdr->str_off += sz; in btf__add_field()
2312 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8) in btf_add_enum_common()
2313 return libbpf_err(-EINVAL); in btf_add_enum_common()
2316 return libbpf_err(-ENOMEM); in btf_add_enum_common()
2321 return libbpf_err(-ENOMEM); in btf_add_enum_common()
2330 t->name_off = name_off; in btf_add_enum_common()
2331 t->info = btf_type_info(kind, 0, is_signed); in btf_add_enum_common()
2332 t->size = byte_sz; in btf_add_enum_common()
2339 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2340 * - *byte_sz* - size of the enum, in bytes.
2347 * - >0, type ID of newly added BTF type;
2348 * - <0, on error.
2361 * - *name* - name of the enumerator value, can't be NULL or empty;
2362 * - *value* - integer value corresponding to enum value *name*;
2364 * - 0, on success;
2365 * - <0, on error.
2374 if (btf->nr_types == 0) in btf__add_enum_value()
2375 return libbpf_err(-EINVAL); in btf__add_enum_value()
2378 return libbpf_err(-EINVAL); in btf__add_enum_value()
2380 /* non-empty name */ in btf__add_enum_value()
2382 return libbpf_err(-EINVAL); in btf__add_enum_value()
2384 return libbpf_err(-E2BIG); in btf__add_enum_value()
2388 return libbpf_err(-ENOMEM); in btf__add_enum_value()
2393 return libbpf_err(-ENOMEM); in btf__add_enum_value()
2399 v->name_off = name_off; in btf__add_enum_value()
2400 v->val = value; in btf__add_enum_value()
2408 t->info = btf_type_info(btf_kind(t), btf_vlen(t), true); in btf__add_enum_value()
2410 btf->hdr->type_len += sz; in btf__add_enum_value()
2411 btf->hdr->str_off += sz; in btf__add_enum_value()
2417 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2418 * - *byte_sz* - size of the enum, in bytes.
2419 * - *is_signed* - whether the enum values are signed or not;
2426 * - >0, type ID of newly added BTF type;
2427 * - <0, on error.
2438 * - *name* - name of the enumerator value, can't be NULL or empty;
2439 * - *value* - integer value corresponding to enum value *name*;
2441 * - 0, on success;
2442 * - <0, on error.
2451 if (btf->nr_types == 0) in btf__add_enum64_value()
2452 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2455 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2457 /* non-empty name */ in btf__add_enum64_value()
2459 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2463 return libbpf_err(-ENOMEM); in btf__add_enum64_value()
2468 return libbpf_err(-ENOMEM); in btf__add_enum64_value()
2474 v->name_off = name_off; in btf__add_enum64_value()
2475 v->val_lo32 = (__u32)value; in btf__add_enum64_value()
2476 v->val_hi32 = value >> 32; in btf__add_enum64_value()
2482 btf->hdr->type_len += sz; in btf__add_enum64_value()
2483 btf->hdr->str_off += sz; in btf__add_enum64_value()
2489 * - *name*, non-empty/non-NULL name;
2490 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2493 * - >0, type ID of newly added BTF type;
2494 * - <0, on error.
2499 return libbpf_err(-EINVAL); in btf__add_fwd()
2511 t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION); in btf__add_fwd()
2516 * values; we also assume a standard 4-byte size for it in btf__add_fwd()
2520 return libbpf_err(-EINVAL); in btf__add_fwd()
2526 * - *name*, non-empty/non-NULL name;
2527 * - *ref_type_id* - referenced type ID, it might not exist yet;
2529 * - >0, type ID of newly added BTF type;
2530 * - <0, on error.
2535 return libbpf_err(-EINVAL); in btf__add_typedef()
2542 * - *ref_type_id* - referenced type ID, it might not exist yet;
2544 * - >0, type ID of newly added BTF type;
2545 * - <0, on error.
2554 * - *ref_type_id* - referenced type ID, it might not exist yet;
2556 * - >0, type ID of newly added BTF type;
2557 * - <0, on error.
2566 * - *ref_type_id* - referenced type ID, it might not exist yet;
2568 * - >0, type ID of newly added BTF type;
2569 * - <0, on error.
2578 * - *value*, non-empty/non-NULL tag value;
2579 * - *ref_type_id* - referenced type ID, it might not exist yet;
2581 * - >0, type ID of newly added BTF type;
2582 * - <0, on error.
2587 return libbpf_err(-EINVAL); in btf__add_type_tag()
2594 * - *name*, non-empty/non-NULL name;
2595 * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
2597 * - >0, type ID of newly added BTF type;
2598 * - <0, on error.
2606 return libbpf_err(-EINVAL); in btf__add_func()
2609 return libbpf_err(-EINVAL); in btf__add_func()
2615 t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0); in btf__add_func()
2622 * - *ret_type_id* - type ID for return result of a function.
2629 * - >0, type ID of newly added BTF type;
2630 * - <0, on error.
2638 return libbpf_err(-EINVAL); in btf__add_func_proto()
2641 return libbpf_err(-ENOMEM); in btf__add_func_proto()
2646 return libbpf_err(-ENOMEM); in btf__add_func_proto()
2651 t->name_off = 0; in btf__add_func_proto()
2652 t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0); in btf__add_func_proto()
2653 t->type = ret_type_id; in btf__add_func_proto()
2660 * - *name* - parameter name, can be NULL or empty;
2661 * - *type_id* - type ID describing the type of the parameter.
2663 * - 0, on success;
2664 * - <0, on error.
2673 return libbpf_err(-EINVAL); in btf__add_func_param()
2676 if (btf->nr_types == 0) in btf__add_func_param()
2677 return libbpf_err(-EINVAL); in btf__add_func_param()
2680 return libbpf_err(-EINVAL); in btf__add_func_param()
2684 return libbpf_err(-ENOMEM); in btf__add_func_param()
2689 return libbpf_err(-ENOMEM); in btf__add_func_param()
2697 p->name_off = name_off; in btf__add_func_param()
2698 p->type = type_id; in btf__add_func_param()
2704 btf->hdr->type_len += sz; in btf__add_func_param()
2705 btf->hdr->str_off += sz; in btf__add_func_param()
2711 * - *name* - non-empty/non-NULL name;
2712 * - *linkage* - variable linkage, one of BTF_VAR_STATIC,
2714 * - *type_id* - type ID of the type describing the type of the variable.
2716 * - >0, type ID of newly added BTF type;
2717 * - <0, on error.
2725 /* non-empty name */ in btf__add_var()
2727 return libbpf_err(-EINVAL); in btf__add_var()
2730 return libbpf_err(-EINVAL); in btf__add_var()
2732 return libbpf_err(-EINVAL); in btf__add_var()
2736 return libbpf_err(-ENOMEM); in btf__add_var()
2741 return libbpf_err(-ENOMEM); in btf__add_var()
2747 t->name_off = name_off; in btf__add_var()
2748 t->info = btf_type_info(BTF_KIND_VAR, 0, 0); in btf__add_var()
2749 t->type = type_id; in btf__add_var()
2752 v->linkage = linkage; in btf__add_var()
2759 * - *name* - non-empty/non-NULL name;
2760 * - *byte_sz* - data section size, in bytes.
2766 * - >0, type ID of newly added BTF type;
2767 * - <0, on error.
2774 /* non-empty name */ in btf__add_datasec()
2776 return libbpf_err(-EINVAL); in btf__add_datasec()
2779 return libbpf_err(-ENOMEM); in btf__add_datasec()
2784 return libbpf_err(-ENOMEM); in btf__add_datasec()
2791 t->name_off = name_off; in btf__add_datasec()
2792 t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0); in btf__add_datasec()
2793 t->size = byte_sz; in btf__add_datasec()
2800 * - *var_type_id* - type ID, describing type of the variable;
2801 * - *offset* - variable offset within data section, in bytes;
2802 * - *byte_sz* - variable size, in bytes.
2805 * - 0, on success;
2806 * - <0, on error.
2815 if (btf->nr_types == 0) in btf__add_datasec_var_info()
2816 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2819 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2822 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2826 return libbpf_err(-ENOMEM); in btf__add_datasec_var_info()
2831 return libbpf_err(-ENOMEM); in btf__add_datasec_var_info()
2833 v->type = var_type_id; in btf__add_datasec_var_info()
2834 v->offset = offset; in btf__add_datasec_var_info()
2835 v->size = byte_sz; in btf__add_datasec_var_info()
2841 btf->hdr->type_len += sz; in btf__add_datasec_var_info()
2842 btf->hdr->str_off += sz; in btf__add_datasec_var_info()
2848 * - *value* - non-empty/non-NULL string;
2849 * - *ref_type_id* - referenced type ID, it might not exist yet;
2850 * - *component_idx* - -1 for tagging reference type, otherwise struct/union
2853 * - >0, type ID of newly added BTF type;
2854 * - <0, on error.
2862 if (!value || !value[0] || component_idx < -1) in btf__add_decl_tag()
2863 return libbpf_err(-EINVAL); in btf__add_decl_tag()
2866 return libbpf_err(-EINVAL); in btf__add_decl_tag()
2869 return libbpf_err(-ENOMEM); in btf__add_decl_tag()
2874 return libbpf_err(-ENOMEM); in btf__add_decl_tag()
2880 t->name_off = value_off; in btf__add_decl_tag()
2881 t->info = btf_type_info(BTF_KIND_DECL_TAG, 0, false); in btf__add_decl_tag()
2882 t->type = ref_type_id; in btf__add_decl_tag()
2883 btf_decl_tag(t)->component_idx = component_idx; in btf__add_decl_tag()
2906 if (ext_sec->len == 0) in btf_ext_setup_info()
2909 if (ext_sec->off & 0x03) { in btf_ext_setup_info()
2911 ext_sec->desc); in btf_ext_setup_info()
2912 return -EINVAL; in btf_ext_setup_info()
2915 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off; in btf_ext_setup_info()
2916 info_left = ext_sec->len; in btf_ext_setup_info()
2918 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) { in btf_ext_setup_info()
2920 ext_sec->desc, ext_sec->off, ext_sec->len); in btf_ext_setup_info()
2921 return -EINVAL; in btf_ext_setup_info()
2926 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc); in btf_ext_setup_info()
2927 return -EINVAL; in btf_ext_setup_info()
2932 if (record_size < ext_sec->min_rec_size || in btf_ext_setup_info()
2935 ext_sec->desc, record_size); in btf_ext_setup_info()
2936 return -EINVAL; in btf_ext_setup_info()
2940 info_left -= sizeof(__u32); in btf_ext_setup_info()
2944 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc); in btf_ext_setup_info()
2945 return -EINVAL; in btf_ext_setup_info()
2955 ext_sec->desc); in btf_ext_setup_info()
2956 return -EINVAL; in btf_ext_setup_info()
2959 num_records = sinfo->num_info; in btf_ext_setup_info()
2962 ext_sec->desc); in btf_ext_setup_info()
2963 return -EINVAL; in btf_ext_setup_info()
2969 ext_sec->desc); in btf_ext_setup_info()
2970 return -EINVAL; in btf_ext_setup_info()
2973 info_left -= total_record_size; in btf_ext_setup_info()
2978 ext_info = ext_sec->ext_info; in btf_ext_setup_info()
2979 ext_info->len = ext_sec->len - sizeof(__u32); in btf_ext_setup_info()
2980 ext_info->rec_size = record_size; in btf_ext_setup_info()
2981 ext_info->info = info + sizeof(__u32); in btf_ext_setup_info()
2982 ext_info->sec_cnt = sec_cnt; in btf_ext_setup_info()
2990 .off = btf_ext->hdr->func_info_off, in btf_ext_setup_func_info()
2991 .len = btf_ext->hdr->func_info_len, in btf_ext_setup_func_info()
2993 .ext_info = &btf_ext->func_info, in btf_ext_setup_func_info()
3003 .off = btf_ext->hdr->line_info_off, in btf_ext_setup_line_info()
3004 .len = btf_ext->hdr->line_info_len, in btf_ext_setup_line_info()
3006 .ext_info = &btf_ext->line_info, in btf_ext_setup_line_info()
3016 .off = btf_ext->hdr->core_relo_off, in btf_ext_setup_core_relos()
3017 .len = btf_ext->hdr->core_relo_len, in btf_ext_setup_core_relos()
3019 .ext_info = &btf_ext->core_relo_info, in btf_ext_setup_core_relos()
3031 data_size < hdr->hdr_len) { in btf_ext_parse_hdr()
3033 return -EINVAL; in btf_ext_parse_hdr()
3036 if (hdr->magic == bswap_16(BTF_MAGIC)) { in btf_ext_parse_hdr()
3037 pr_warn("BTF.ext in non-native endianness is not supported\n"); in btf_ext_parse_hdr()
3038 return -ENOTSUP; in btf_ext_parse_hdr()
3039 } else if (hdr->magic != BTF_MAGIC) { in btf_ext_parse_hdr()
3040 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); in btf_ext_parse_hdr()
3041 return -EINVAL; in btf_ext_parse_hdr()
3044 if (hdr->version != BTF_VERSION) { in btf_ext_parse_hdr()
3045 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); in btf_ext_parse_hdr()
3046 return -ENOTSUP; in btf_ext_parse_hdr()
3049 if (hdr->flags) { in btf_ext_parse_hdr()
3050 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags); in btf_ext_parse_hdr()
3051 return -ENOTSUP; in btf_ext_parse_hdr()
3054 if (data_size == hdr->hdr_len) { in btf_ext_parse_hdr()
3056 return -EINVAL; in btf_ext_parse_hdr()
3066 free(btf_ext->func_info.sec_idxs); in btf_ext__free()
3067 free(btf_ext->line_info.sec_idxs); in btf_ext__free()
3068 free(btf_ext->core_relo_info.sec_idxs); in btf_ext__free()
3069 free(btf_ext->data); in btf_ext__free()
3080 return libbpf_err_ptr(-ENOMEM); in btf_ext__new()
3082 btf_ext->data_size = size; in btf_ext__new()
3083 btf_ext->data = malloc(size); in btf_ext__new()
3084 if (!btf_ext->data) { in btf_ext__new()
3085 err = -ENOMEM; in btf_ext__new()
3088 memcpy(btf_ext->data, data, size); in btf_ext__new()
3090 err = btf_ext_parse_hdr(btf_ext->data, size); in btf_ext__new()
3094 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) { in btf_ext__new()
3095 err = -EINVAL; in btf_ext__new()
3107 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) in btf_ext__new()
3125 *size = btf_ext->data_size; in btf_ext__raw_data()
3126 return btf_ext->data; in btf_ext__raw_data()
3151 * memory in-place with deduplicated types and strings without any loss of
3161 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
3166 * BTF type information is typically emitted either as a result of conversion
3222 * types, in general, can form graphs containing cycles, not just DAGs.
3269 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
3273 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
3290 return libbpf_err(-EINVAL); in btf__dedup()
3295 return libbpf_err(-EINVAL); in btf__dedup()
3299 err = -ENOMEM; in btf__dedup()
3349 #define BTF_UNPROCESSED_ID ((__u32)-1)
3350 #define BTF_IN_PROGRESS_ID ((__u32)-2)
3353 /* .BTF section to be deduped in-place */
3394 hashmap__for_each_key_entry(d->dedup_table, node, hash)
3398 return hashmap__append(d->dedup_table, hash, type_id); in btf_dedup_table_add()
3404 if (d->hypot_cnt == d->hypot_cap) { in btf_dedup_hypot_map_add()
3407 d->hypot_cap += max((size_t)16, d->hypot_cap / 2); in btf_dedup_hypot_map_add()
3408 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32)); in btf_dedup_hypot_map_add()
3410 return -ENOMEM; in btf_dedup_hypot_map_add()
3411 d->hypot_list = new_list; in btf_dedup_hypot_map_add()
3413 d->hypot_list[d->hypot_cnt++] = from_id; in btf_dedup_hypot_map_add()
3414 d->hypot_map[from_id] = to_id; in btf_dedup_hypot_map_add()
3422 for (i = 0; i < d->hypot_cnt; i++) in btf_dedup_clear_hypot_map()
3423 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; in btf_dedup_clear_hypot_map()
3424 d->hypot_cnt = 0; in btf_dedup_clear_hypot_map()
3425 d->hypot_adjust_canon = false; in btf_dedup_clear_hypot_map()
3430 hashmap__free(d->dedup_table); in btf_dedup_free()
3431 d->dedup_table = NULL; in btf_dedup_free()
3433 free(d->map); in btf_dedup_free()
3434 d->map = NULL; in btf_dedup_free()
3436 free(d->hypot_map); in btf_dedup_free()
3437 d->hypot_map = NULL; in btf_dedup_free()
3439 free(d->hypot_list); in btf_dedup_free()
3440 d->hypot_list = NULL; in btf_dedup_free()
3467 return ERR_PTR(-ENOMEM); in btf_dedup_new()
3472 d->btf = btf; in btf_dedup_new()
3473 d->btf_ext = OPTS_GET(opts, btf_ext, NULL); in btf_dedup_new()
3475 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL); in btf_dedup_new()
3476 if (IS_ERR(d->dedup_table)) { in btf_dedup_new()
3477 err = PTR_ERR(d->dedup_table); in btf_dedup_new()
3478 d->dedup_table = NULL; in btf_dedup_new()
3483 d->map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3484 if (!d->map) { in btf_dedup_new()
3485 err = -ENOMEM; in btf_dedup_new()
3489 d->map[0] = 0; in btf_dedup_new()
3491 struct btf_type *t = btf_type_by_id(d->btf, i); in btf_dedup_new()
3493 /* VAR and DATASEC are never deduped and are self-canonical */ in btf_dedup_new()
3495 d->map[i] = i; in btf_dedup_new()
3497 d->map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3500 d->hypot_map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3501 if (!d->hypot_map) { in btf_dedup_new()
3502 err = -ENOMEM; in btf_dedup_new()
3506 d->hypot_map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3525 for (i = 0; i < d->btf->nr_types; i++) { in btf_for_each_str_off()
3527 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_for_each_str_off()
3541 if (!d->btf_ext) in btf_for_each_str_off()
3544 r = btf_ext_visit_str_offs(d->btf_ext, fn, ctx); in btf_for_each_str_off()
3559 if (str_off == 0 || str_off < d->btf->start_str_off) in strs_dedup_remap_str_off()
3562 s = btf__str_by_offset(d->btf, str_off); in strs_dedup_remap_str_off()
3563 if (d->btf->base_btf) { in strs_dedup_remap_str_off()
3564 err = btf__find_str(d->btf->base_btf, s); in strs_dedup_remap_str_off()
3569 if (err != -ENOENT) in strs_dedup_remap_str_off()
3573 off = strset__add_str(d->strs_set, s); in strs_dedup_remap_str_off()
3577 *str_off_ptr = d->btf->start_str_off + off; in strs_dedup_remap_str_off()
3596 if (d->btf->strs_deduped) in btf_dedup_strings()
3599 d->strs_set = strset__new(BTF_MAX_STR_OFFSET, NULL, 0); in btf_dedup_strings()
3600 if (IS_ERR(d->strs_set)) { in btf_dedup_strings()
3601 err = PTR_ERR(d->strs_set); in btf_dedup_strings()
3605 if (!d->btf->base_btf) { in btf_dedup_strings()
3609 err = strset__add_str(d->strs_set, ""); in btf_dedup_strings()
3620 strset__free(d->btf->strs_set); in btf_dedup_strings()
3621 d->btf->hdr->str_len = strset__data_size(d->strs_set); in btf_dedup_strings()
3622 d->btf->strs_set = d->strs_set; in btf_dedup_strings()
3623 d->strs_set = NULL; in btf_dedup_strings()
3624 d->btf->strs_deduped = true; in btf_dedup_strings()
3628 strset__free(d->strs_set); in btf_dedup_strings()
3629 d->strs_set = NULL; in btf_dedup_strings()
3638 h = hash_combine(0, t->name_off); in btf_hash_common()
3639 h = hash_combine(h, t->info); in btf_hash_common()
3640 h = hash_combine(h, t->size); in btf_hash_common()
3646 return t1->name_off == t2->name_off && in btf_equal_common()
3647 t1->info == t2->info && in btf_equal_common()
3648 t1->size == t2->size; in btf_equal_common()
3680 h = hash_combine(0, t->name_off); in btf_hash_enum()
3694 if (m1->name_off != m2->name_off || m1->val != m2->val) in btf_equal_enum_members()
3712 if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 || in btf_equal_enum64_members()
3713 m1->val_hi32 != m2->val_hi32) in btf_equal_enum64_members()
3744 * - skip comparing vlen because it is zero for forward declarations; in btf_compat_enum()
3745 * - skip comparing size to allow enum forward declarations in btf_compat_enum()
3747 * - skip comparing kind for the same reason. in btf_compat_enum()
3749 return t1->name_off == t2->name_off && in btf_compat_enum()
3766 h = hash_combine(h, member->name_off); in btf_hash_struct()
3767 h = hash_combine(h, member->offset); in btf_hash_struct()
3792 if (m1->name_off != m2->name_off || m1->offset != m2->offset) in btf_shallow_equal_struct()
3810 h = hash_combine(h, info->type); in btf_hash_array()
3811 h = hash_combine(h, info->index_type); in btf_hash_array()
3812 h = hash_combine(h, info->nelems); in btf_hash_array()
3832 return info1->type == info2->type && in btf_equal_array()
3833 info1->index_type == info2->index_type && in btf_equal_array()
3834 info1->nelems == info2->nelems; in btf_equal_array()
3847 return btf_array(t1)->nelems == btf_array(t2)->nelems; in btf_compat_array()
3863 h = hash_combine(h, member->name_off); in btf_hash_fnproto()
3864 h = hash_combine(h, member->type); in btf_hash_fnproto()
3890 if (m1->name_off != m2->name_off || m1->type != m2->type) in btf_equal_fnproto()
3910 if (t1->name_off != t2->name_off || t1->info != t2->info) in btf_compat_fnproto()
3917 if (m1->name_off != m2->name_off) in btf_compat_fnproto()
3935 if (!d->btf->base_btf) in btf_dedup_prep()
3938 for (type_id = 1; type_id < d->btf->start_id; type_id++) { in btf_dedup_prep()
3939 t = btf_type_by_id(d->btf, type_id); in btf_dedup_prep()
3941 /* all base BTF types are self-canonical by definition */ in btf_dedup_prep()
3942 d->map[type_id] = type_id; in btf_dedup_prep()
3980 return -EINVAL; in btf_dedup_prep()
3983 return -ENOMEM; in btf_dedup_prep()
3993 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
3997 struct btf_type *t = btf_type_by_id(d->btf, type_id); in btf_dedup_prim_type()
4025 cand_id = hash_entry->value; in btf_dedup_prim_type()
4026 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4038 cand_id = hash_entry->value; in btf_dedup_prim_type()
4039 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4051 d->map[cand_id] = type_id; in btf_dedup_prim_type()
4060 cand_id = hash_entry->value; in btf_dedup_prim_type()
4061 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4070 return -EINVAL; in btf_dedup_prim_type()
4073 d->map[type_id] = new_id; in btf_dedup_prim_type()
4075 return -ENOMEM; in btf_dedup_prim_type()
4084 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_prim_types()
4085 err = btf_dedup_prim_type(d, d->btf->start_id + i); in btf_dedup_prim_types()
4097 return d->map[type_id] <= BTF_MAX_NR_TYPES; in is_type_mapped()
4107 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_type_id()
4108 type_id = d->map[type_id]; in resolve_type_id()
4120 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
4123 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_fwd_id()
4124 type_id = d->map[type_id]; in resolve_fwd_id()
4126 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
4143 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_arrays()
4144 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_arrays()
4158 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_structs()
4159 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_structs()
4170 if (m1->type != m2->type && in btf_dedup_identical_structs()
4171 !btf_dedup_identical_arrays(d, m1->type, m2->type) && in btf_dedup_identical_structs()
4172 !btf_dedup_identical_structs(d, m1->type, m2->type)) in btf_dedup_identical_structs()
4186 * - >0, if type graphs are equivalent;
4187 * - 0, if not equivalent;
4188 * - <0, on error.
4190 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
4195 * a `cand_id` is recoded in hypothetical mapping (`btf_dedup->hypot_map`).
4202 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
4203 * happen when type graphs have cycles. In this case we assume those two
4205 * - `canon_id` is mapped to different type. This is contradiction in our
4219 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
4244 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
4248 * - Both types in canonical and candidate graphs are FWDs. If they are
4252 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
4254 * - Type in canonical graph is FWD, while type in candidate is concrete
4263 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
4268 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
4287 hypot_type_id = d->hypot_map[canon_id]; in btf_dedup_is_equiv()
4295 * types sub-graph has a consistent and deduped-by-compiler in btf_dedup_is_equiv()
4306 * complicated and requires a many-to-many equivalence mapping. in btf_dedup_is_equiv()
4314 return -ENOMEM; in btf_dedup_is_equiv()
4316 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_is_equiv()
4317 canon_type = btf_type_by_id(d->btf, canon_id); in btf_dedup_is_equiv()
4321 if (cand_type->name_off != canon_type->name_off) in btf_dedup_is_equiv()
4324 /* FWD <--> STRUCT/UNION equivalence check, if enabled */ in btf_dedup_is_equiv()
4337 if (fwd_kind == real_kind && canon_id < d->btf->start_id) in btf_dedup_is_equiv()
4338 d->hypot_adjust_canon = true; in btf_dedup_is_equiv()
4365 if (cand_type->info != canon_type->info) in btf_dedup_is_equiv()
4367 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
4376 eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type); in btf_dedup_is_equiv()
4379 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); in btf_dedup_is_equiv()
4393 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); in btf_dedup_is_equiv()
4409 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
4416 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); in btf_dedup_is_equiv()
4426 return -EINVAL; in btf_dedup_is_equiv()
4436 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
4464 for (i = 0; i < d->hypot_cnt; i++) { in btf_dedup_merge_hypot_map()
4465 canon_type_id = d->hypot_list[i]; in btf_dedup_merge_hypot_map()
4466 targ_type_id = d->hypot_map[canon_type_id]; in btf_dedup_merge_hypot_map()
4469 t_kind = btf_kind(btf__type_by_id(d->btf, t_id)); in btf_dedup_merge_hypot_map()
4470 c_kind = btf_kind(btf__type_by_id(d->btf, c_id)); in btf_dedup_merge_hypot_map()
4475 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because in btf_dedup_merge_hypot_map()
4492 d->map[c_id] = t_id; in btf_dedup_merge_hypot_map()
4499 if (d->hypot_adjust_canon) in btf_dedup_merge_hypot_map()
4503 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4515 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4532 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
4533 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
4539 * as canonical for itself and is added into btf_dedup->dedup_table hash map
4552 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_struct_type()
4555 t = btf_type_by_id(d->btf, type_id); in btf_dedup_struct_type()
4563 __u32 cand_id = hash_entry->value; in btf_dedup_struct_type()
4573 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because in btf_dedup_struct_type()
4576 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_struct_type()
4587 if (d->hypot_adjust_canon) /* not really equivalent */ in btf_dedup_struct_type()
4593 d->map[type_id] = new_id; in btf_dedup_struct_type()
4595 return -ENOMEM; in btf_dedup_struct_type()
4604 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_struct_types()
4605 err = btf_dedup_struct_type(d, d->btf->start_id + i); in btf_dedup_struct_types()
4623 * one by one. There is no danger of encountering cycles because in C type
4634 * btf_dedup->dedup_table as another possible canonical representative.
4645 if (d->map[type_id] == BTF_IN_PROGRESS_ID) in btf_dedup_ref_type()
4646 return -ELOOP; in btf_dedup_ref_type()
4647 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_ref_type()
4650 t = btf_type_by_id(d->btf, type_id); in btf_dedup_ref_type()
4651 d->map[type_id] = BTF_IN_PROGRESS_ID; in btf_dedup_ref_type()
4661 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4664 t->type = ref_type_id; in btf_dedup_ref_type()
4668 cand_id = hash_entry->value; in btf_dedup_ref_type()
4669 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4678 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4681 t->type = ref_type_id; in btf_dedup_ref_type()
4685 cand_id = hash_entry->value; in btf_dedup_ref_type()
4686 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4697 ref_type_id = btf_dedup_ref_type(d, info->type); in btf_dedup_ref_type()
4700 info->type = ref_type_id; in btf_dedup_ref_type()
4702 ref_type_id = btf_dedup_ref_type(d, info->index_type); in btf_dedup_ref_type()
4705 info->index_type = ref_type_id; in btf_dedup_ref_type()
4709 cand_id = hash_entry->value; in btf_dedup_ref_type()
4710 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4724 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4727 t->type = ref_type_id; in btf_dedup_ref_type()
4732 ref_type_id = btf_dedup_ref_type(d, param->type); in btf_dedup_ref_type()
4735 param->type = ref_type_id; in btf_dedup_ref_type()
4741 cand_id = hash_entry->value; in btf_dedup_ref_type()
4742 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4752 return -EINVAL; in btf_dedup_ref_type()
4755 d->map[type_id] = new_id; in btf_dedup_ref_type()
4757 return -ENOMEM; in btf_dedup_ref_type()
4766 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_ref_types()
4767 err = btf_dedup_ref_type(d, d->btf->start_id + i); in btf_dedup_ref_types()
4771 /* we won't need d->dedup_table anymore */ in btf_dedup_ref_types()
4772 hashmap__free(d->dedup_table); in btf_dedup_ref_types()
4773 d->dedup_table = NULL; in btf_dedup_ref_types()
4784 __u32 nr_types = btf__type_cnt(d->btf); in btf_dedup_fill_unique_names_map()
4795 t = btf_type_by_id(d->btf, type_id); in btf_dedup_fill_unique_names_map()
4801 /* Skip non-canonical types */ in btf_dedup_fill_unique_names_map()
4802 if (type_id != d->map[type_id]) in btf_dedup_fill_unique_names_map()
4805 err = hashmap__add(names_map, t->name_off, type_id); in btf_dedup_fill_unique_names_map()
4806 if (err == -EEXIST) in btf_dedup_fill_unique_names_map()
4807 err = hashmap__set(names_map, t->name_off, 0, NULL, NULL); in btf_dedup_fill_unique_names_map()
4818 struct btf_type *t = btf_type_by_id(d->btf, type_id); in btf_dedup_resolve_fwd()
4828 if (type_id != d->map[type_id]) in btf_dedup_resolve_fwd()
4831 if (!hashmap__find(names_map, t->name_off, &cand_id)) in btf_dedup_resolve_fwd()
4838 cand_t = btf_type_by_id(d->btf, cand_id); in btf_dedup_resolve_fwd()
4844 d->map[type_id] = cand_id; in btf_dedup_resolve_fwd()
4894 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_resolve_fwds()
4895 err = btf_dedup_resolve_fwd(d, names_map, d->btf->start_id + i); in btf_dedup_resolve_fwds()
4911 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
4919 __u32 next_type_id = d->btf->start_id; in btf_dedup_compact_types()
4925 d->hypot_map[0] = 0; in btf_dedup_compact_types()
4927 for (id = 1; id < d->btf->start_id; id++) in btf_dedup_compact_types()
4928 d->hypot_map[id] = id; in btf_dedup_compact_types()
4929 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) in btf_dedup_compact_types()
4930 d->hypot_map[id] = BTF_UNPROCESSED_ID; in btf_dedup_compact_types()
4932 p = d->btf->types_data; in btf_dedup_compact_types()
4934 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) { in btf_dedup_compact_types()
4935 if (d->map[id] != id) in btf_dedup_compact_types()
4938 t = btf__type_by_id(d->btf, id); in btf_dedup_compact_types()
4944 d->hypot_map[id] = next_type_id; in btf_dedup_compact_types()
4945 d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data; in btf_dedup_compact_types()
4951 d->btf->nr_types = next_type_id - d->btf->start_id; in btf_dedup_compact_types()
4952 d->btf->type_offs_cap = d->btf->nr_types; in btf_dedup_compact_types()
4953 d->btf->hdr->type_len = p - d->btf->types_data; in btf_dedup_compact_types()
4954 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap, in btf_dedup_compact_types()
4956 if (d->btf->type_offs_cap && !new_offs) in btf_dedup_compact_types()
4957 return -ENOMEM; in btf_dedup_compact_types()
4958 d->btf->type_offs = new_offs; in btf_dedup_compact_types()
4959 d->btf->hdr->str_off = d->btf->hdr->type_len; in btf_dedup_compact_types()
4960 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len; in btf_dedup_compact_types()
4967 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
4976 new_type_id = d->hypot_map[resolved_type_id]; in btf_dedup_remap_type_id()
4978 return -EINVAL; in btf_dedup_remap_type_id()
4989 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
4998 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_remap_types()
4999 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_dedup_remap_types()
5011 new_id = d->hypot_map[resolved_id]; in btf_dedup_remap_types()
5013 return -EINVAL; in btf_dedup_remap_types()
5019 if (!d->btf_ext) in btf_dedup_remap_types()
5022 r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d); in btf_dedup_remap_types()
5030 * Probe few well-known locations for vmlinux kernel image and try to load BTF
5038 "/boot/vmlinux-%1$s", in btf__load_vmlinux_btf()
5039 "/lib/modules/%1$s/vmlinux-%1$s", in btf__load_vmlinux_btf()
5042 "/usr/lib/debug/boot/vmlinux-%1$s", in btf__load_vmlinux_btf()
5043 "/usr/lib/debug/boot/vmlinux-%1$s.debug", in btf__load_vmlinux_btf()
5058 err = -errno; in btf__load_vmlinux_btf()
5084 return libbpf_err_ptr(-ESRCH); in btf__load_vmlinux_btf()
5103 seg = &btf_ext->func_info; in btf_ext_visit_type_ids()
5108 err = visit(&rec->type_id, ctx); in btf_ext_visit_type_ids()
5114 seg = &btf_ext->core_relo_info; in btf_ext_visit_type_ids()
5119 err = visit(&rec->type_id, ctx); in btf_ext_visit_type_ids()
5134 seg = &btf_ext->func_info; in btf_ext_visit_str_offs()
5136 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5141 seg = &btf_ext->line_info; in btf_ext_visit_str_offs()
5145 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5150 err = visit(&rec->file_name_off, ctx); in btf_ext_visit_str_offs()
5153 err = visit(&rec->line_off, ctx); in btf_ext_visit_str_offs()
5159 seg = &btf_ext->core_relo_info; in btf_ext_visit_str_offs()
5163 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5168 err = visit(&rec->access_str_off, ctx); in btf_ext_visit_str_offs()
5187 struct btf_type *split_t = btf_type_by_id(dist->pipe.src, i); in btf_add_distilled_type_ids()
5201 if (*id >= dist->split_start_id) in btf_add_distilled_type_ids()
5204 if (dist->id_map[*id] > 0) in btf_add_distilled_type_ids()
5210 base_t = btf_type_by_id(dist->pipe.src, *id); in btf_add_distilled_type_ids()
5227 dist->id_map[*id] = *id; in btf_add_distilled_type_ids()
5232 return -EINVAL; in btf_add_distilled_type_ids()
5241 if (btf_is_composite(base_t) && base_t->name_off) in btf_add_distilled_type_ids()
5252 bool adding_to_base = dist->pipe.dst->start_id == 1; in btf_add_distilled_types()
5253 int id = btf__type_cnt(dist->pipe.dst); in btf_add_distilled_types()
5261 for (i = 1; i < dist->split_start_id; i++) { in btf_add_distilled_types()
5265 if (!dist->id_map[i]) in btf_add_distilled_types()
5267 t = btf_type_by_id(dist->pipe.src, i); in btf_add_distilled_types()
5269 name = btf__name_by_offset(dist->pipe.src, t->name_off); in btf_add_distilled_types()
5278 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5282 /* Named struct/union are added to base as 0-vlen in btf_add_distilled_types()
5284 * are added to split BTF as-is. in btf_add_distilled_types()
5287 if (!t->name_off) in btf_add_distilled_types()
5289 err = btf_add_composite(dist->pipe.dst, kind, name, t->size); in btf_add_distilled_types()
5291 if (t->name_off) in btf_add_distilled_types()
5293 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5299 * enum; relocation will match with appropriately-named in btf_add_distilled_types()
5302 * Anonymous enums are added to split BTF as-is. in btf_add_distilled_types()
5305 if (!t->name_off) in btf_add_distilled_types()
5307 err = btf__add_enum(dist->pipe.dst, name, t->size); in btf_add_distilled_types()
5309 if (t->name_off) in btf_add_distilled_types()
5311 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5325 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5330 return -EINVAL; in btf_add_distilled_types()
5335 dist->id_map[i] = id++; in btf_add_distilled_types()
5347 struct btf_type *t = btf_type_by_id(dist->pipe.dst, i); in btf_update_distilled_type_ids()
5356 if (dist->id_map[*id]) in btf_update_distilled_type_ids()
5357 *id = dist->id_map[*id]; in btf_update_distilled_type_ids()
5358 else if (*id >= dist->split_start_id) in btf_update_distilled_type_ids()
5359 *id -= dist->diff_id; in btf_update_distilled_type_ids()
5377 * We return newly-created split BTF where the split BTF refers to a newly-created
5393 return libbpf_err(-EINVAL); in btf__distill_base()
5397 return libbpf_err(-ENOMEM); in btf__distill_base()
5403 err = -ENOMEM; in btf__distill_base()
5410 err = -ENOMEM; in btf__distill_base()
5414 dist.split_start_str = old_base->hdr->str_len; in btf__distill_base()
5420 for (i = src_btf->start_id; i < n; i++) { in btf__distill_base()
5439 err = -errno; in btf__distill_base()
5444 for (i = src_btf->start_id; i < n; i++) { in btf__distill_base()
5458 dist.diff_id = dist.split_start_id - btf__type_cnt(new_base); in btf__distill_base()
5483 return btf->hdr; in btf_header()
5488 btf->base_btf = (struct btf *)base_btf; in btf_set_base_btf()
5489 btf->start_id = btf__type_cnt(base_btf); in btf_set_base_btf()
5490 btf->start_str_off = base_btf->hdr->str_len; in btf_set_base_btf()
5498 btf->owns_base = false; in btf__relocate()