Lines Matching +full:self +full:- +full:describing
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
35 /* raw BTF data in non-native endianness */
47 * +--------------------------------+
49 * +--------------------------------+
53 * types_data-+ |
54 * strs_data------------+
65 * +----------+ +---------+ +-----------+
67 * +----------+ +---------+ +-----------+
71 * types_data----+ |
72 * strset__data(strs_set)-----+
74 * +----------+---------+-----------+
76 * raw_data----->+----------+---------+-----------+
81 size_t types_data_cap; /* used size stored in hdr->type_len */
84 * type_offs[0] corresponds to the first non-VOID type:
85 * - for base BTF it's type [1];
86 * - for split BTF it's the first non-base BTF type.
91 * - doesn't include special [0] void type;
92 * - for split BTF counts number of types added on top of base BTF.
100 * - for base BTF it's equal to 1;
101 * - for split BTF it's equal to biggest type ID of base BTF plus 1.
105 * - for base BTF it's equal to 0;
106 * - for split BTF it's equal to total size of base BTF's string section size.
110 /* only one of strs_data or strs_set can be non-NULL, depending on
172 memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz); in libbpf_add_mem()
189 p = libbpf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt); in libbpf_ensure_mem()
191 return -ENOMEM; in libbpf_ensure_mem()
198 return libbpf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32), in btf_add_type_offs_mem()
199 btf->nr_types, BTF_MAX_NR_TYPES, add_cnt); in btf_add_type_offs_mem()
208 return -ENOMEM; in btf_add_type_idx_entry()
216 h->magic = bswap_16(h->magic); in btf_bswap_hdr()
217 h->hdr_len = bswap_32(h->hdr_len); in btf_bswap_hdr()
218 h->type_off = bswap_32(h->type_off); in btf_bswap_hdr()
219 h->type_len = bswap_32(h->type_len); in btf_bswap_hdr()
220 h->str_off = bswap_32(h->str_off); in btf_bswap_hdr()
221 h->str_len = bswap_32(h->str_len); in btf_bswap_hdr()
226 struct btf_header *hdr = btf->hdr; in btf_parse_hdr()
229 if (btf->raw_size < sizeof(struct btf_header)) { in btf_parse_hdr()
231 return -EINVAL; in btf_parse_hdr()
234 if (hdr->magic == bswap_16(BTF_MAGIC)) { in btf_parse_hdr()
235 btf->swapped_endian = true; in btf_parse_hdr()
236 if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) { in btf_parse_hdr()
237 pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n", in btf_parse_hdr()
238 bswap_32(hdr->hdr_len)); in btf_parse_hdr()
239 return -ENOTSUP; in btf_parse_hdr()
242 } else if (hdr->magic != BTF_MAGIC) { in btf_parse_hdr()
243 pr_debug("Invalid BTF magic: %x\n", hdr->magic); in btf_parse_hdr()
244 return -EINVAL; in btf_parse_hdr()
247 if (btf->raw_size < hdr->hdr_len) { in btf_parse_hdr()
249 hdr->hdr_len, btf->raw_size); in btf_parse_hdr()
250 return -EINVAL; in btf_parse_hdr()
253 meta_left = btf->raw_size - hdr->hdr_len; in btf_parse_hdr()
254 if (meta_left < (long long)hdr->str_off + hdr->str_len) { in btf_parse_hdr()
255 pr_debug("Invalid BTF total size: %u\n", btf->raw_size); in btf_parse_hdr()
256 return -EINVAL; in btf_parse_hdr()
259 if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) { in btf_parse_hdr()
261 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len); in btf_parse_hdr()
262 return -EINVAL; in btf_parse_hdr()
265 if (hdr->type_off % 4) { in btf_parse_hdr()
267 return -EINVAL; in btf_parse_hdr()
275 const struct btf_header *hdr = btf->hdr; in btf_parse_str_sec()
276 const char *start = btf->strs_data; in btf_parse_str_sec()
277 const char *end = start + btf->hdr->str_len; in btf_parse_str_sec()
279 if (btf->base_btf && hdr->str_len == 0) in btf_parse_str_sec()
281 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) { in btf_parse_str_sec()
283 return -EINVAL; in btf_parse_str_sec()
285 if (!btf->base_btf && start[0]) { in btf_parse_str_sec()
287 return -EINVAL; in btf_parse_str_sec()
329 return -EINVAL; in btf_type_size()
335 t->name_off = bswap_32(t->name_off); in btf_bswap_type_base()
336 t->info = bswap_32(t->info); in btf_bswap_type_base()
337 t->type = bswap_32(t->type); in btf_bswap_type_base()
367 e->name_off = bswap_32(e->name_off); in btf_bswap_type_rest()
368 e->val = bswap_32(e->val); in btf_bswap_type_rest()
373 e64->name_off = bswap_32(e64->name_off); in btf_bswap_type_rest()
374 e64->val_lo32 = bswap_32(e64->val_lo32); in btf_bswap_type_rest()
375 e64->val_hi32 = bswap_32(e64->val_hi32); in btf_bswap_type_rest()
380 a->type = bswap_32(a->type); in btf_bswap_type_rest()
381 a->index_type = bswap_32(a->index_type); in btf_bswap_type_rest()
382 a->nelems = bswap_32(a->nelems); in btf_bswap_type_rest()
387 m->name_off = bswap_32(m->name_off); in btf_bswap_type_rest()
388 m->type = bswap_32(m->type); in btf_bswap_type_rest()
389 m->offset = bswap_32(m->offset); in btf_bswap_type_rest()
394 p->name_off = bswap_32(p->name_off); in btf_bswap_type_rest()
395 p->type = bswap_32(p->type); in btf_bswap_type_rest()
399 btf_var(t)->linkage = bswap_32(btf_var(t)->linkage); in btf_bswap_type_rest()
403 v->type = bswap_32(v->type); in btf_bswap_type_rest()
404 v->offset = bswap_32(v->offset); in btf_bswap_type_rest()
405 v->size = bswap_32(v->size); in btf_bswap_type_rest()
409 btf_decl_tag(t)->component_idx = bswap_32(btf_decl_tag(t)->component_idx); in btf_bswap_type_rest()
413 return -EINVAL; in btf_bswap_type_rest()
419 struct btf_header *hdr = btf->hdr; in btf_parse_type_sec()
420 void *next_type = btf->types_data; in btf_parse_type_sec()
421 void *end_type = next_type + hdr->type_len; in btf_parse_type_sec()
425 if (btf->swapped_endian) in btf_parse_type_sec()
432 pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types); in btf_parse_type_sec()
433 return -EINVAL; in btf_parse_type_sec()
436 if (btf->swapped_endian && btf_bswap_type_rest(next_type)) in btf_parse_type_sec()
437 return -EINVAL; in btf_parse_type_sec()
439 err = btf_add_type_idx_entry(btf, next_type - btf->types_data); in btf_parse_type_sec()
444 btf->nr_types++; in btf_parse_type_sec()
449 return -EINVAL; in btf_parse_type_sec()
462 return -EINVAL; in btf_validate_str()
475 return -EINVAL; in btf_validate_id()
486 err = btf_validate_str(btf, t->name_off, "type name", id); in btf_validate_type()
504 err = btf_validate_id(btf, t->type, id); in btf_validate_type()
511 err = btf_validate_id(btf, a->type, id); in btf_validate_type()
512 err = err ?: btf_validate_id(btf, a->index_type, id); in btf_validate_type()
523 err = btf_validate_str(btf, m->name_off, "field name", id); in btf_validate_type()
524 err = err ?: btf_validate_id(btf, m->type, id); in btf_validate_type()
535 err = btf_validate_str(btf, m->name_off, "enum name", id); in btf_validate_type()
546 err = btf_validate_str(btf, m->name_off, "enum name", id); in btf_validate_type()
555 err = btf_validate_id(btf, t->type, id); in btf_validate_type()
558 ft = btf__type_by_id(btf, t->type); in btf_validate_type()
560 pr_warn("btf: type [%u]: referenced type [%u] is not FUNC_PROTO\n", id, t->type); in btf_validate_type()
561 return -EINVAL; in btf_validate_type()
570 err = btf_validate_str(btf, m->name_off, "param name", id); in btf_validate_type()
571 err = err ?: btf_validate_id(btf, m->type, id); in btf_validate_type()
582 err = btf_validate_id(btf, m->type, id); in btf_validate_type()
590 return -EINVAL; in btf_validate_type()
605 for (i = btf->start_id; i < n; i++) { in btf_sanity_check()
616 return btf->start_id + btf->nr_types; in btf__type_cnt()
621 return btf->base_btf; in btf__base_btf()
624 /* internal helper returning non-const pointer to a type */
629 if (type_id < btf->start_id) in btf_type_by_id()
630 return btf_type_by_id(btf->base_btf, type_id); in btf_type_by_id()
631 return btf->types_data + btf->type_offs[type_id - btf->start_id]; in btf_type_by_id()
636 if (type_id >= btf->start_id + btf->nr_types) in btf__type_by_id()
660 if (btf->base_btf && btf->base_btf->ptr_sz > 0) in determine_ptr_size()
661 return btf->base_btf->ptr_sz; in determine_ptr_size()
669 if (t->size != 4 && t->size != 8) in determine_ptr_size()
672 name = btf__name_by_offset(btf, t->name_off); in determine_ptr_size()
678 return t->size; in determine_ptr_size()
682 return -1; in determine_ptr_size()
687 if (!btf->ptr_sz) in btf_ptr_sz()
688 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); in btf_ptr_sz()
689 return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz; in btf_ptr_sz()
702 if (!btf->ptr_sz) in btf__pointer_size()
703 ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf); in btf__pointer_size()
705 if (btf->ptr_sz < 0) in btf__pointer_size()
709 return btf->ptr_sz; in btf__pointer_size()
718 return libbpf_err(-EINVAL); in btf__set_pointer_size()
719 btf->ptr_sz = ptr_sz; in btf__set_pointer_size()
737 return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; in btf__endianness()
739 return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; in btf__endianness()
745 return libbpf_err(-EINVAL); in btf__set_endianness()
747 btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); in btf__set_endianness()
748 if (!btf->swapped_endian) { in btf__set_endianness()
749 free(btf->raw_data_swapped); in btf__set_endianness()
750 btf->raw_data_swapped = NULL; in btf__set_endianness()
772 __s64 size = -1; in btf__resolve_size()
785 size = t->size; in btf__resolve_size()
797 type_id = t->type; in btf__resolve_size()
801 if (nelems && array->nelems > UINT32_MAX / nelems) in btf__resolve_size()
802 return libbpf_err(-E2BIG); in btf__resolve_size()
803 nelems *= array->nelems; in btf__resolve_size()
804 type_id = array->type; in btf__resolve_size()
807 return libbpf_err(-EINVAL); in btf__resolve_size()
815 return libbpf_err(-EINVAL); in btf__resolve_size()
817 return libbpf_err(-E2BIG); in btf__resolve_size()
832 return min(btf_ptr_sz(btf), (size_t)t->size); in btf__align_of()
840 return btf__align_of(btf, t->type); in btf__align_of()
842 return btf__align_of(btf, btf_array(t)->type); in btf__align_of()
850 align = btf__align_of(btf, m->type); in btf__align_of()
859 (m->offset % (8 * align)) != 0) in btf__align_of()
866 if ((t->size % max_align) != 0) in btf__align_of()
886 type_id = t->type; in btf__resolve_type()
892 return libbpf_err(-EINVAL); in btf__resolve_type()
906 const char *name = btf__name_by_offset(btf, t->name_off); in btf__find_by_name()
912 return libbpf_err(-ENOENT); in btf__find_by_name()
929 name = btf__name_by_offset(btf, t->name_off); in btf_find_by_name_kind()
934 return libbpf_err(-ENOENT); in btf_find_by_name_kind()
940 return btf_find_by_name_kind(btf, btf->start_id, type_name, kind); in btf__find_by_name_kind_own()
951 return (void *)btf->hdr != btf->raw_data; in btf_is_modifiable()
959 if (btf->fd >= 0) in btf__free()
960 close(btf->fd); in btf__free()
964 * in-memory representation for header, types, and strings in btf__free()
969 free(btf->hdr); in btf__free()
970 free(btf->types_data); in btf__free()
971 strset__free(btf->strs_set); in btf__free()
973 free(btf->raw_data); in btf__free()
974 free(btf->raw_data_swapped); in btf__free()
975 free(btf->type_offs); in btf__free()
976 if (btf->owns_base) in btf__free()
977 btf__free(btf->base_btf); in btf__free()
987 return ERR_PTR(-ENOMEM); in btf_new_empty()
989 btf->nr_types = 0; in btf_new_empty()
990 btf->start_id = 1; in btf_new_empty()
991 btf->start_str_off = 0; in btf_new_empty()
992 btf->fd = -1; in btf_new_empty()
993 btf->ptr_sz = sizeof(void *); in btf_new_empty()
994 btf->swapped_endian = false; in btf_new_empty()
997 btf->base_btf = base_btf; in btf_new_empty()
998 btf->start_id = btf__type_cnt(base_btf); in btf_new_empty()
999 btf->start_str_off = base_btf->hdr->str_len; in btf_new_empty()
1000 btf->swapped_endian = base_btf->swapped_endian; in btf_new_empty()
1004 btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1); in btf_new_empty()
1005 btf->raw_data = calloc(1, btf->raw_size); in btf_new_empty()
1006 if (!btf->raw_data) { in btf_new_empty()
1008 return ERR_PTR(-ENOMEM); in btf_new_empty()
1011 btf->hdr = btf->raw_data; in btf_new_empty()
1012 btf->hdr->hdr_len = sizeof(struct btf_header); in btf_new_empty()
1013 btf->hdr->magic = BTF_MAGIC; in btf_new_empty()
1014 btf->hdr->version = BTF_VERSION; in btf_new_empty()
1016 btf->types_data = btf->raw_data + btf->hdr->hdr_len; in btf_new_empty()
1017 btf->strs_data = btf->raw_data + btf->hdr->hdr_len; in btf_new_empty()
1018 btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */ in btf_new_empty()
1040 return ERR_PTR(-ENOMEM); in btf_new()
1042 btf->nr_types = 0; in btf_new()
1043 btf->start_id = 1; in btf_new()
1044 btf->start_str_off = 0; in btf_new()
1045 btf->fd = -1; in btf_new()
1048 btf->base_btf = base_btf; in btf_new()
1049 btf->start_id = btf__type_cnt(base_btf); in btf_new()
1050 btf->start_str_off = base_btf->hdr->str_len; in btf_new()
1053 btf->raw_data = malloc(size); in btf_new()
1054 if (!btf->raw_data) { in btf_new()
1055 err = -ENOMEM; in btf_new()
1058 memcpy(btf->raw_data, data, size); in btf_new()
1059 btf->raw_size = size; in btf_new()
1061 btf->hdr = btf->raw_data; in btf_new()
1066 btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off; in btf_new()
1067 btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off; in btf_new()
1143 field = &secs->btf_data; in btf_find_elf_sections()
1145 field = &secs->btf_ext_data; in btf_find_elf_sections()
1147 field = &secs->btf_base_data; in btf_find_elf_sections()
1163 return -LIBBPF_ERRNO__FORMAT; in btf_find_elf_sections()
1172 int err = 0, fd = -1; in btf_parse_elf()
1177 return ERR_PTR(-LIBBPF_ERRNO__LIBELF); in btf_parse_elf()
1182 err = -errno; in btf_parse_elf()
1189 err = -LIBBPF_ERRNO__FORMAT; in btf_parse_elf()
1200 err = -ENODATA; in btf_parse_elf()
1205 dist_base_btf = btf_new(secs.btf_base_data->d_buf, secs.btf_base_data->d_size, in btf_parse_elf()
1214 btf = btf_new(secs.btf_data->d_buf, secs.btf_data->d_size, in btf_parse_elf()
1229 btf->owns_base = true; in btf_parse_elf()
1244 *btf_ext = btf_ext__new(secs.btf_ext_data->d_buf, secs.btf_ext_data->d_size); in btf_parse_elf()
1289 err = -errno; in btf_parse_raw()
1295 err = -EIO; in btf_parse_raw()
1300 err = -EPROTO; in btf_parse_raw()
1306 err = -errno; in btf_parse_raw()
1311 err = -errno; in btf_parse_raw()
1316 err = -errno; in btf_parse_raw()
1320 /* pre-alloc memory and read all of BTF data */ in btf_parse_raw()
1323 err = -ENOMEM; in btf_parse_raw()
1327 err = -EIO; in btf_parse_raw()
1363 if (err != -EPROTO) in btf_parse()
1390 if (btf->fd >= 0) in btf_load_into_kernel()
1391 return libbpf_err(-EEXIST); in btf_load_into_kernel()
1393 return libbpf_err(-EINVAL); in btf_load_into_kernel()
1398 err = -ENOMEM; in btf_load_into_kernel()
1401 btf->raw_size = raw_size; in btf_load_into_kernel()
1402 btf->raw_data = raw_data; in btf_load_into_kernel()
1407 * retry, using either auto-allocated or custom log_buf. This way in btf_load_into_kernel()
1408 * non-NULL custom log_buf provides a buffer just in case, but hopes in btf_load_into_kernel()
1420 err = -ENOMEM; in btf_load_into_kernel()
1436 btf->fd = bpf_btf_load(raw_data, raw_size, &opts); in btf_load_into_kernel()
1437 if (btf->fd < 0) { in btf_load_into_kernel()
1449 err = -errno; in btf_load_into_kernel()
1453 pr_warn("-- BEGIN BTF LOAD LOG ---\n%s\n-- END BTF LOAD LOG --\n", buf); in btf_load_into_kernel()
1468 return btf->fd; in btf__fd()
1473 btf->fd = fd; in btf__set_fd()
1478 return btf->strs_data ? btf->strs_data : strset__data(btf->strs_set); in btf_strs_data()
1483 struct btf_header *hdr = btf->hdr; in btf_get_raw_data()
1489 data = swap_endian ? btf->raw_data_swapped : btf->raw_data; in btf_get_raw_data()
1491 *size = btf->raw_size; in btf_get_raw_data()
1495 data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len; in btf_get_raw_data()
1501 memcpy(p, hdr, hdr->hdr_len); in btf_get_raw_data()
1504 p += hdr->hdr_len; in btf_get_raw_data()
1506 memcpy(p, btf->types_data, hdr->type_len); in btf_get_raw_data()
1508 for (i = 0; i < btf->nr_types; i++) { in btf_get_raw_data()
1509 t = p + btf->type_offs[i]; in btf_get_raw_data()
1510 /* btf_bswap_type_rest() relies on native t->info, so in btf_get_raw_data()
1519 p += hdr->type_len; in btf_get_raw_data()
1521 memcpy(p, btf_strs_data(btf), hdr->str_len); in btf_get_raw_data()
1522 p += hdr->str_len; in btf_get_raw_data()
1537 data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian); in btf__raw_data()
1541 btf->raw_size = data_sz; in btf__raw_data()
1542 if (btf->swapped_endian) in btf__raw_data()
1543 btf->raw_data_swapped = data; in btf__raw_data()
1545 btf->raw_data = data; in btf__raw_data()
1555 if (offset < btf->start_str_off) in btf__str_by_offset()
1556 return btf__str_by_offset(btf->base_btf, offset); in btf__str_by_offset()
1557 else if (offset - btf->start_str_off < btf->hdr->str_len) in btf__str_by_offset()
1558 return btf_strs_data(btf) + (offset - btf->start_str_off); in btf__str_by_offset()
1578 * let's start with a sane default - 4KiB here - and resize it only if in btf_get_from_fd()
1584 return ERR_PTR(-ENOMEM); in btf_get_from_fd()
1597 btf = ERR_PTR(-ENOMEM); in btf_get_from_fd()
1611 btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG); in btf_get_from_fd()
1629 return libbpf_err_ptr(-errno); in btf__load_from_kernel_by_id_split()
1644 if (btf->raw_data) { in btf_invalidate_raw_data()
1645 free(btf->raw_data); in btf_invalidate_raw_data()
1646 btf->raw_data = NULL; in btf_invalidate_raw_data()
1648 if (btf->raw_data_swapped) { in btf_invalidate_raw_data()
1649 free(btf->raw_data_swapped); in btf_invalidate_raw_data()
1650 btf->raw_data_swapped = NULL; in btf_invalidate_raw_data()
1662 int err = -ENOMEM; in btf_ensure_modifiable()
1671 hdr = malloc(btf->hdr->hdr_len); in btf_ensure_modifiable()
1672 types = malloc(btf->hdr->type_len); in btf_ensure_modifiable()
1676 memcpy(hdr, btf->hdr, btf->hdr->hdr_len); in btf_ensure_modifiable()
1677 memcpy(types, btf->types_data, btf->hdr->type_len); in btf_ensure_modifiable()
1680 set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr->str_len); in btf_ensure_modifiable()
1687 btf->hdr = hdr; in btf_ensure_modifiable()
1688 btf->types_data = types; in btf_ensure_modifiable()
1689 btf->types_data_cap = btf->hdr->type_len; in btf_ensure_modifiable()
1690 btf->strs_data = NULL; in btf_ensure_modifiable()
1691 btf->strs_set = set; in btf_ensure_modifiable()
1695 if (btf->hdr->str_len == 0) in btf_ensure_modifiable()
1696 btf->strs_deduped = true; in btf_ensure_modifiable()
1697 if (!btf->base_btf && btf->hdr->str_len == 1) in btf_ensure_modifiable()
1698 btf->strs_deduped = true; in btf_ensure_modifiable()
1714 * - >0 offset into string section, if string is found;
1715 * - -ENOENT, if string is not in the string section;
1716 * - <0, on any other error.
1722 if (btf->base_btf) { in btf__find_str()
1723 off = btf__find_str(btf->base_btf, s); in btf__find_str()
1724 if (off != -ENOENT) in btf__find_str()
1730 return libbpf_err(-ENOMEM); in btf__find_str()
1732 off = strset__find_str(btf->strs_set, s); in btf__find_str()
1736 return btf->start_str_off + off; in btf__find_str()
1741 * - > 0 offset into string section, on success;
1742 * - < 0, on error.
1748 if (btf->base_btf) { in btf__add_str()
1749 off = btf__find_str(btf->base_btf, s); in btf__add_str()
1750 if (off != -ENOENT) in btf__add_str()
1755 return libbpf_err(-ENOMEM); in btf__add_str()
1757 off = strset__add_str(btf->strs_set, s); in btf__add_str()
1761 btf->hdr->str_len = strset__data_size(btf->strs_set); in btf__add_str()
1763 return btf->start_str_off + off; in btf__add_str()
1768 return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1, in btf_add_type_mem()
1769 btf->hdr->type_len, UINT_MAX, add_sz); in btf_add_type_mem()
1774 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t)); in btf_type_inc_vlen()
1781 err = btf_add_type_idx_entry(btf, btf->hdr->type_len); in btf_commit_type()
1785 btf->hdr->type_len += data_sz; in btf_commit_type()
1786 btf->hdr->str_off += data_sz; in btf_commit_type()
1787 btf->nr_types++; in btf_commit_type()
1788 return btf->start_id + btf->nr_types - 1; in btf_commit_type()
1805 if (p->str_off_map && in btf_rewrite_str()
1806 hashmap__find(p->str_off_map, *str_off, &mapped_off)) { in btf_rewrite_str()
1811 off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off)); in btf_rewrite_str()
1818 if (p->str_off_map) { in btf_rewrite_str()
1819 err = hashmap__append(p->str_off_map, *str_off, off); in btf_rewrite_str()
1840 if (btf_ensure_modifiable(p->dst)) in btf_add_type()
1841 return libbpf_err(-ENOMEM); in btf_add_type()
1843 t = btf_add_type_mem(p->dst, sz); in btf_add_type()
1845 return libbpf_err(-ENOMEM); in btf_add_type()
1859 return btf_commit_type(p->dst, sz); in btf_add_type()
1880 if (src_btf->base_btf) in btf__add_btf()
1881 return libbpf_err(-ENOTSUP); in btf__add_btf()
1885 return libbpf_err(-ENOMEM); in btf__add_btf()
1890 old_strs_len = btf->hdr->str_len; in btf__add_btf()
1892 data_sz = src_btf->hdr->type_len; in btf__add_btf()
1893 cnt = btf__type_cnt(src_btf) - 1; in btf__add_btf()
1895 /* pre-allocate enough memory for new types */ in btf__add_btf()
1898 return libbpf_err(-ENOMEM); in btf__add_btf()
1900 /* pre-allocate enough memory for type offset index for new types */ in btf__add_btf()
1903 return libbpf_err(-ENOMEM); in btf__add_btf()
1908 return libbpf_err(-ENOMEM); in btf__add_btf()
1911 memcpy(t, src_btf->types_data, data_sz); in btf__add_btf()
1925 *off = t - btf->types_data; in btf__add_btf()
1947 * btf->start_id + btf->nr_types - 1 is the type ID offset we should in btf__add_btf()
1950 *type_id += btf->start_id + btf->nr_types - 1; in btf__add_btf()
1961 * pre-allocated, but it would not be available for querying. But now in btf__add_btf()
1966 btf->hdr->type_len += data_sz; in btf__add_btf()
1967 btf->hdr->str_off += data_sz; in btf__add_btf()
1968 btf->nr_types += cnt; in btf__add_btf()
1973 return btf->start_id + btf->nr_types - cnt; in btf__add_btf()
1978 memset(btf->types_data + btf->hdr->type_len, 0, data_sz); in btf__add_btf()
1979 memset(btf->strs_data + old_strs_len, 0, btf->hdr->str_len - old_strs_len); in btf__add_btf()
1984 btf->hdr->str_len = old_strs_len; in btf__add_btf()
1993 * - *name* - non-empty, non-NULL type name;
1994 * - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
1995 * - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
1997 * - >0, type ID of newly added BTF type;
1998 * - <0, on error.
2005 /* non-empty name */ in btf__add_int()
2007 return libbpf_err(-EINVAL); in btf__add_int()
2009 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16) in btf__add_int()
2010 return libbpf_err(-EINVAL); in btf__add_int()
2012 return libbpf_err(-EINVAL); in btf__add_int()
2016 return libbpf_err(-ENOMEM); in btf__add_int()
2021 return libbpf_err(-ENOMEM); in btf__add_int()
2031 t->name_off = name_off; in btf__add_int()
2032 t->info = btf_type_info(BTF_KIND_INT, 0, 0); in btf__add_int()
2033 t->size = byte_sz; in btf__add_int()
2042 * - *name* - non-empty, non-NULL type name;
2043 * - *sz* - size of the type, in bytes;
2045 * - >0, type ID of newly added BTF type;
2046 * - <0, on error.
2053 /* non-empty name */ in btf__add_float()
2055 return libbpf_err(-EINVAL); in btf__add_float()
2060 return libbpf_err(-EINVAL); in btf__add_float()
2063 return libbpf_err(-ENOMEM); in btf__add_float()
2068 return libbpf_err(-ENOMEM); in btf__add_float()
2074 t->name_off = name_off; in btf__add_float()
2075 t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0); in btf__add_float()
2076 t->size = byte_sz; in btf__add_float()
2088 return -EINVAL; in validate_type_id()
2099 return libbpf_err(-EINVAL); in btf_add_ref_kind()
2102 return libbpf_err(-ENOMEM); in btf_add_ref_kind()
2107 return libbpf_err(-ENOMEM); in btf_add_ref_kind()
2115 t->name_off = name_off; in btf_add_ref_kind()
2116 t->info = btf_type_info(kind, 0, 0); in btf_add_ref_kind()
2117 t->type = ref_type_id; in btf_add_ref_kind()
2124 * - *ref_type_id* - referenced type ID, it might not exist yet;
2126 * - >0, type ID of newly added BTF type;
2127 * - <0, on error.
2136 * - *index_type_id* - type ID of the type describing array index;
2137 * - *elem_type_id* - type ID of the type describing array element;
2138 * - *nr_elems* - the size of the array;
2140 * - >0, type ID of newly added BTF type;
2141 * - <0, on error.
2150 return libbpf_err(-EINVAL); in btf__add_array()
2153 return libbpf_err(-ENOMEM); in btf__add_array()
2158 return libbpf_err(-ENOMEM); in btf__add_array()
2160 t->name_off = 0; in btf__add_array()
2161 t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0); in btf__add_array()
2162 t->size = 0; in btf__add_array()
2165 a->type = elem_type_id; in btf__add_array()
2166 a->index_type = index_type_id; in btf__add_array()
2167 a->nelems = nr_elems; in btf__add_array()
2179 return libbpf_err(-ENOMEM); in btf_add_composite()
2184 return libbpf_err(-ENOMEM); in btf_add_composite()
2195 t->name_off = name_off; in btf_add_composite()
2196 t->info = btf_type_info(kind, 0, 0); in btf_add_composite()
2197 t->size = bytes_sz; in btf_add_composite()
2204 * - *name* - name of the struct, can be NULL or empty for anonymous structs;
2205 * - *byte_sz* - size of the struct, in bytes;
2211 * - >0, type ID of newly added BTF type;
2212 * - <0, on error.
2221 * - *name* - name of the union, can be NULL or empty for anonymous union;
2222 * - *byte_sz* - size of the union, in bytes;
2229 * - >0, type ID of newly added BTF type;
2230 * - <0, on error.
2239 return btf_type_by_id(btf, btf__type_cnt(btf) - 1); in btf_last_type()
2244 * - *name* - name of the field, can be NULL or empty for anonymous field;
2245 * - *type_id* - type ID for the type describing field type;
2246 * - *bit_offset* - bit offset of the start of the field within struct/union;
2247 * - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
2249 * - 0, on success;
2250 * - <0, on error.
2261 if (btf->nr_types == 0) in btf__add_field()
2262 return libbpf_err(-EINVAL); in btf__add_field()
2265 return libbpf_err(-EINVAL); in btf__add_field()
2268 return libbpf_err(-EINVAL); in btf__add_field()
2269 /* best-effort bit field offset/size enforcement */ in btf__add_field()
2272 return libbpf_err(-EINVAL); in btf__add_field()
2276 return libbpf_err(-EINVAL); in btf__add_field()
2280 return libbpf_err(-ENOMEM); in btf__add_field()
2285 return libbpf_err(-ENOMEM); in btf__add_field()
2293 m->name_off = name_off; in btf__add_field()
2294 m->type = type_id; in btf__add_field()
2295 m->offset = bit_offset | (bit_size << 24); in btf__add_field()
2300 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t)); in btf__add_field()
2302 btf->hdr->type_len += sz; in btf__add_field()
2303 btf->hdr->str_off += sz; in btf__add_field()
2314 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8) in btf_add_enum_common()
2315 return libbpf_err(-EINVAL); in btf_add_enum_common()
2318 return libbpf_err(-ENOMEM); in btf_add_enum_common()
2323 return libbpf_err(-ENOMEM); in btf_add_enum_common()
2332 t->name_off = name_off; in btf_add_enum_common()
2333 t->info = btf_type_info(kind, 0, is_signed); in btf_add_enum_common()
2334 t->size = byte_sz; in btf_add_enum_common()
2341 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2342 * - *byte_sz* - size of the enum, in bytes.
2349 * - >0, type ID of newly added BTF type;
2350 * - <0, on error.
2363 * - *name* - name of the enumerator value, can't be NULL or empty;
2364 * - *value* - integer value corresponding to enum value *name*;
2366 * - 0, on success;
2367 * - <0, on error.
2376 if (btf->nr_types == 0) in btf__add_enum_value()
2377 return libbpf_err(-EINVAL); in btf__add_enum_value()
2380 return libbpf_err(-EINVAL); in btf__add_enum_value()
2382 /* non-empty name */ in btf__add_enum_value()
2384 return libbpf_err(-EINVAL); in btf__add_enum_value()
2386 return libbpf_err(-E2BIG); in btf__add_enum_value()
2390 return libbpf_err(-ENOMEM); in btf__add_enum_value()
2395 return libbpf_err(-ENOMEM); in btf__add_enum_value()
2401 v->name_off = name_off; in btf__add_enum_value()
2402 v->val = value; in btf__add_enum_value()
2410 t->info = btf_type_info(btf_kind(t), btf_vlen(t), true); in btf__add_enum_value()
2412 btf->hdr->type_len += sz; in btf__add_enum_value()
2413 btf->hdr->str_off += sz; in btf__add_enum_value()
2419 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2420 * - *byte_sz* - size of the enum, in bytes.
2421 * - *is_signed* - whether the enum values are signed or not;
2428 * - >0, type ID of newly added BTF type;
2429 * - <0, on error.
2440 * - *name* - name of the enumerator value, can't be NULL or empty;
2441 * - *value* - integer value corresponding to enum value *name*;
2443 * - 0, on success;
2444 * - <0, on error.
2453 if (btf->nr_types == 0) in btf__add_enum64_value()
2454 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2457 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2459 /* non-empty name */ in btf__add_enum64_value()
2461 return libbpf_err(-EINVAL); in btf__add_enum64_value()
2465 return libbpf_err(-ENOMEM); in btf__add_enum64_value()
2470 return libbpf_err(-ENOMEM); in btf__add_enum64_value()
2476 v->name_off = name_off; in btf__add_enum64_value()
2477 v->val_lo32 = (__u32)value; in btf__add_enum64_value()
2478 v->val_hi32 = value >> 32; in btf__add_enum64_value()
2484 btf->hdr->type_len += sz; in btf__add_enum64_value()
2485 btf->hdr->str_off += sz; in btf__add_enum64_value()
2491 * - *name*, non-empty/non-NULL name;
2492 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2495 * - >0, type ID of newly added BTF type;
2496 * - <0, on error.
2501 return libbpf_err(-EINVAL); in btf__add_fwd()
2513 t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION); in btf__add_fwd()
2518 * values; we also assume a standard 4-byte size for it in btf__add_fwd()
2522 return libbpf_err(-EINVAL); in btf__add_fwd()
2528 * - *name*, non-empty/non-NULL name;
2529 * - *ref_type_id* - referenced type ID, it might not exist yet;
2531 * - >0, type ID of newly added BTF type;
2532 * - <0, on error.
2537 return libbpf_err(-EINVAL); in btf__add_typedef()
2544 * - *ref_type_id* - referenced type ID, it might not exist yet;
2546 * - >0, type ID of newly added BTF type;
2547 * - <0, on error.
2556 * - *ref_type_id* - referenced type ID, it might not exist yet;
2558 * - >0, type ID of newly added BTF type;
2559 * - <0, on error.
2568 * - *ref_type_id* - referenced type ID, it might not exist yet;
2570 * - >0, type ID of newly added BTF type;
2571 * - <0, on error.
2580 * - *value*, non-empty/non-NULL tag value;
2581 * - *ref_type_id* - referenced type ID, it might not exist yet;
2583 * - >0, type ID of newly added BTF type;
2584 * - <0, on error.
2589 return libbpf_err(-EINVAL); in btf__add_type_tag()
2596 * - *name*, non-empty/non-NULL name;
2597 * - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
2599 * - >0, type ID of newly added BTF type;
2600 * - <0, on error.
2608 return libbpf_err(-EINVAL); in btf__add_func()
2611 return libbpf_err(-EINVAL); in btf__add_func()
2617 t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0); in btf__add_func()
2624 * - *ret_type_id* - type ID for return result of a function.
2631 * - >0, type ID of newly added BTF type;
2632 * - <0, on error.
2640 return libbpf_err(-EINVAL); in btf__add_func_proto()
2643 return libbpf_err(-ENOMEM); in btf__add_func_proto()
2648 return libbpf_err(-ENOMEM); in btf__add_func_proto()
2653 t->name_off = 0; in btf__add_func_proto()
2654 t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0); in btf__add_func_proto()
2655 t->type = ret_type_id; in btf__add_func_proto()
2662 * - *name* - parameter name, can be NULL or empty;
2663 * - *type_id* - type ID describing the type of the parameter.
2665 * - 0, on success;
2666 * - <0, on error.
2675 return libbpf_err(-EINVAL); in btf__add_func_param()
2678 if (btf->nr_types == 0) in btf__add_func_param()
2679 return libbpf_err(-EINVAL); in btf__add_func_param()
2682 return libbpf_err(-EINVAL); in btf__add_func_param()
2686 return libbpf_err(-ENOMEM); in btf__add_func_param()
2691 return libbpf_err(-ENOMEM); in btf__add_func_param()
2699 p->name_off = name_off; in btf__add_func_param()
2700 p->type = type_id; in btf__add_func_param()
2706 btf->hdr->type_len += sz; in btf__add_func_param()
2707 btf->hdr->str_off += sz; in btf__add_func_param()
2713 * - *name* - non-empty/non-NULL name;
2714 * - *linkage* - variable linkage, one of BTF_VAR_STATIC,
2716 * - *type_id* - type ID of the type describing the type of the variable.
2718 * - >0, type ID of newly added BTF type;
2719 * - <0, on error.
2727 /* non-empty name */ in btf__add_var()
2729 return libbpf_err(-EINVAL); in btf__add_var()
2732 return libbpf_err(-EINVAL); in btf__add_var()
2734 return libbpf_err(-EINVAL); in btf__add_var()
2738 return libbpf_err(-ENOMEM); in btf__add_var()
2743 return libbpf_err(-ENOMEM); in btf__add_var()
2749 t->name_off = name_off; in btf__add_var()
2750 t->info = btf_type_info(BTF_KIND_VAR, 0, 0); in btf__add_var()
2751 t->type = type_id; in btf__add_var()
2754 v->linkage = linkage; in btf__add_var()
2761 * - *name* - non-empty/non-NULL name;
2762 * - *byte_sz* - data section size, in bytes.
2768 * - >0, type ID of newly added BTF type;
2769 * - <0, on error.
2776 /* non-empty name */ in btf__add_datasec()
2778 return libbpf_err(-EINVAL); in btf__add_datasec()
2781 return libbpf_err(-ENOMEM); in btf__add_datasec()
2786 return libbpf_err(-ENOMEM); in btf__add_datasec()
2793 t->name_off = name_off; in btf__add_datasec()
2794 t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0); in btf__add_datasec()
2795 t->size = byte_sz; in btf__add_datasec()
2802 * - *var_type_id* - type ID, describing type of the variable;
2803 * - *offset* - variable offset within data section, in bytes;
2804 * - *byte_sz* - variable size, in bytes.
2807 * - 0, on success;
2808 * - <0, on error.
2817 if (btf->nr_types == 0) in btf__add_datasec_var_info()
2818 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2821 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2824 return libbpf_err(-EINVAL); in btf__add_datasec_var_info()
2828 return libbpf_err(-ENOMEM); in btf__add_datasec_var_info()
2833 return libbpf_err(-ENOMEM); in btf__add_datasec_var_info()
2835 v->type = var_type_id; in btf__add_datasec_var_info()
2836 v->offset = offset; in btf__add_datasec_var_info()
2837 v->size = byte_sz; in btf__add_datasec_var_info()
2843 btf->hdr->type_len += sz; in btf__add_datasec_var_info()
2844 btf->hdr->str_off += sz; in btf__add_datasec_var_info()
2850 * - *value* - non-empty/non-NULL string;
2851 * - *ref_type_id* - referenced type ID, it might not exist yet;
2852 * - *component_idx* - -1 for tagging reference type, otherwise struct/union
2855 * - >0, type ID of newly added BTF type;
2856 * - <0, on error.
2864 if (!value || !value[0] || component_idx < -1) in btf__add_decl_tag()
2865 return libbpf_err(-EINVAL); in btf__add_decl_tag()
2868 return libbpf_err(-EINVAL); in btf__add_decl_tag()
2871 return libbpf_err(-ENOMEM); in btf__add_decl_tag()
2876 return libbpf_err(-ENOMEM); in btf__add_decl_tag()
2882 t->name_off = value_off; in btf__add_decl_tag()
2883 t->info = btf_type_info(BTF_KIND_DECL_TAG, 0, false); in btf__add_decl_tag()
2884 t->type = ref_type_id; in btf__add_decl_tag()
2885 btf_decl_tag(t)->component_idx = component_idx; in btf__add_decl_tag()
2900 * - validate subsection structure and elements
2901 * - save info subsection start and sizing details in struct btf_ext
2902 * - endian-independent operation, for calling before byte-swapping
2914 if (ext_sec->len == 0) in btf_ext_parse_sec_info()
2917 if (ext_sec->off & 0x03) { in btf_ext_parse_sec_info()
2919 ext_sec->desc); in btf_ext_parse_sec_info()
2920 return -EINVAL; in btf_ext_parse_sec_info()
2924 info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off; in btf_ext_parse_sec_info()
2925 info_left = ext_sec->len; in btf_ext_parse_sec_info()
2927 if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) { in btf_ext_parse_sec_info()
2929 ext_sec->desc, ext_sec->off, ext_sec->len); in btf_ext_parse_sec_info()
2930 return -EINVAL; in btf_ext_parse_sec_info()
2935 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc); in btf_ext_parse_sec_info()
2936 return -EINVAL; in btf_ext_parse_sec_info()
2940 * handling non-native endianness data, the exact standard so as in btf_ext_parse_sec_info()
2941 * to allow safe byte-swapping. in btf_ext_parse_sec_info()
2944 if (record_size < ext_sec->min_rec_size || in btf_ext_parse_sec_info()
2945 (!is_native && record_size != ext_sec->min_rec_size) || in btf_ext_parse_sec_info()
2948 ext_sec->desc, record_size); in btf_ext_parse_sec_info()
2949 return -EINVAL; in btf_ext_parse_sec_info()
2953 info_left -= sizeof(__u32); in btf_ext_parse_sec_info()
2957 pr_debug("%s section in .BTF.ext has no records\n", ext_sec->desc); in btf_ext_parse_sec_info()
2958 return -EINVAL; in btf_ext_parse_sec_info()
2968 ext_sec->desc); in btf_ext_parse_sec_info()
2969 return -EINVAL; in btf_ext_parse_sec_info()
2972 num_records = is_native ? sinfo->num_info : bswap_32(sinfo->num_info); in btf_ext_parse_sec_info()
2975 ext_sec->desc); in btf_ext_parse_sec_info()
2976 return -EINVAL; in btf_ext_parse_sec_info()
2982 ext_sec->desc); in btf_ext_parse_sec_info()
2983 return -EINVAL; in btf_ext_parse_sec_info()
2986 info_left -= total_record_size; in btf_ext_parse_sec_info()
2991 ext_info = ext_sec->ext_info; in btf_ext_parse_sec_info()
2992 ext_info->len = ext_sec->len - sizeof(__u32); in btf_ext_parse_sec_info()
2993 ext_info->rec_size = record_size; in btf_ext_parse_sec_info()
2994 ext_info->info = info + sizeof(__u32); in btf_ext_parse_sec_info()
2995 ext_info->sec_cnt = sec_cnt; in btf_ext_parse_sec_info()
3004 .off = btf_ext->hdr->func_info_off, in btf_ext_parse_info()
3005 .len = btf_ext->hdr->func_info_len, in btf_ext_parse_info()
3007 .ext_info = &btf_ext->func_info, in btf_ext_parse_info()
3011 .off = btf_ext->hdr->line_info_off, in btf_ext_parse_info()
3012 .len = btf_ext->hdr->line_info_len, in btf_ext_parse_info()
3014 .ext_info = &btf_ext->line_info, in btf_ext_parse_info()
3018 .off = btf_ext->hdr->core_relo_off, in btf_ext_parse_info()
3019 .len = btf_ext->hdr->core_relo_len, in btf_ext_parse_info()
3021 .ext_info = &btf_ext->core_relo_info, in btf_ext_parse_info()
3034 if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) in btf_ext_parse_info()
3044 /* Swap byte-order of BTF.ext header with any endianness */
3047 bool is_native = h->magic == BTF_MAGIC; in btf_ext_bswap_hdr()
3050 hdr_len = is_native ? h->hdr_len : bswap_32(h->hdr_len); in btf_ext_bswap_hdr()
3052 h->magic = bswap_16(h->magic); in btf_ext_bswap_hdr()
3053 h->hdr_len = bswap_32(h->hdr_len); in btf_ext_bswap_hdr()
3054 h->func_info_off = bswap_32(h->func_info_off); in btf_ext_bswap_hdr()
3055 h->func_info_len = bswap_32(h->func_info_len); in btf_ext_bswap_hdr()
3056 h->line_info_off = bswap_32(h->line_info_off); in btf_ext_bswap_hdr()
3057 h->line_info_len = bswap_32(h->line_info_len); in btf_ext_bswap_hdr()
3062 h->core_relo_off = bswap_32(h->core_relo_off); in btf_ext_bswap_hdr()
3063 h->core_relo_len = bswap_32(h->core_relo_len); in btf_ext_bswap_hdr()
3066 /* Swap byte-order of generic info subsection */
3081 info_left = len - sizeof(__u32); in btf_ext_bswap_info_sec()
3087 num_recs = is_native ? sec->num_info : bswap_32(sec->num_info); in btf_ext_bswap_info_sec()
3088 sec->sec_name_off = bswap_32(sec->sec_name_off); in btf_ext_bswap_info_sec()
3089 sec->num_info = bswap_32(sec->num_info); in btf_ext_bswap_info_sec()
3090 p = sec->data; /* info rec #1 */ in btf_ext_bswap_info_sec()
3094 info_left -= sec_hdrlen + (__u64)rec_size * num_recs; in btf_ext_bswap_info_sec()
3099 * Swap byte-order of all info data in a BTF.ext section
3100 * - requires BTF.ext hdr in native endianness
3104 const bool is_native = btf_ext->swapped_endian; in btf_ext_bswap_info()
3108 /* Swap func_info subsection byte-order */ in btf_ext_bswap_info()
3109 info = data + h->hdr_len + h->func_info_off; in btf_ext_bswap_info()
3110 btf_ext_bswap_info_sec(info, h->func_info_len, is_native, in btf_ext_bswap_info()
3113 /* Swap line_info subsection byte-order */ in btf_ext_bswap_info()
3114 info = data + h->hdr_len + h->line_info_off; in btf_ext_bswap_info()
3115 btf_ext_bswap_info_sec(info, h->line_info_len, is_native, in btf_ext_bswap_info()
3118 /* Swap core_relo subsection byte-order (if present) */ in btf_ext_bswap_info()
3119 if (h->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) in btf_ext_bswap_info()
3122 info = data + h->hdr_len + h->core_relo_off; in btf_ext_bswap_info()
3123 btf_ext_bswap_info_sec(info, h->core_relo_len, is_native, in btf_ext_bswap_info()
3130 __u32 hdr_len, data_size = btf_ext->data_size; in btf_ext_parse()
3131 struct btf_ext_header *hdr = btf_ext->hdr; in btf_ext_parse()
3137 return -EINVAL; in btf_ext_parse()
3140 hdr_len = hdr->hdr_len; in btf_ext_parse()
3141 if (hdr->magic == bswap_16(BTF_MAGIC)) { in btf_ext_parse()
3144 } else if (hdr->magic != BTF_MAGIC) { in btf_ext_parse()
3145 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic); in btf_ext_parse()
3146 return -EINVAL; in btf_ext_parse()
3150 if (hdr->version != 1) { in btf_ext_parse()
3151 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version); in btf_ext_parse()
3152 return -ENOTSUP; in btf_ext_parse()
3155 if (hdr->flags) { in btf_ext_parse()
3156 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags); in btf_ext_parse()
3157 return -ENOTSUP; in btf_ext_parse()
3162 return -EINVAL; in btf_ext_parse()
3165 return -EINVAL; in btf_ext_parse()
3171 return -EINVAL; in btf_ext_parse()
3174 /* Keep hdr native byte-order in memory for introspection */ in btf_ext_parse()
3176 btf_ext_bswap_hdr(btf_ext->hdr); in btf_ext_parse()
3183 /* Keep infos native byte-order in memory for introspection */ in btf_ext_parse()
3185 btf_ext_bswap_info(btf_ext, btf_ext->data); in btf_ext_parse()
3188 * Set btf_ext->swapped_endian only after all header and info data has in btf_ext_parse()
3190 * in native byte-order when called. in btf_ext_parse()
3192 btf_ext->swapped_endian = swapped_endian; in btf_ext_parse()
3200 free(btf_ext->func_info.sec_idxs); in btf_ext__free()
3201 free(btf_ext->line_info.sec_idxs); in btf_ext__free()
3202 free(btf_ext->core_relo_info.sec_idxs); in btf_ext__free()
3203 free(btf_ext->data); in btf_ext__free()
3204 free(btf_ext->data_swapped); in btf_ext__free()
3215 return libbpf_err_ptr(-ENOMEM); in btf_ext__new()
3217 btf_ext->data_size = size; in btf_ext__new()
3218 btf_ext->data = malloc(size); in btf_ext__new()
3219 if (!btf_ext->data) { in btf_ext__new()
3220 err = -ENOMEM; in btf_ext__new()
3223 memcpy(btf_ext->data, data, size); in btf_ext__new()
3239 const __u32 data_sz = btf_ext->data_size; in btf_ext_raw_data()
3244 return btf_ext->data; in btf_ext_raw_data()
3245 else if (btf_ext->data_swapped) in btf_ext_raw_data()
3246 return btf_ext->data_swapped; in btf_ext_raw_data()
3252 memcpy(data, btf_ext->data, data_sz); in btf_ext_raw_data()
3256 btf_ext->data_swapped = data; in btf_ext_raw_data()
3264 data = btf_ext_raw_data(btf_ext, btf_ext->swapped_endian); in btf_ext__raw_data()
3268 *size = btf_ext->data_size; in btf_ext__raw_data()
3278 return btf_ext->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN; in btf_ext__endianness()
3280 return btf_ext->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN; in btf_ext__endianness()
3286 return libbpf_err(-EINVAL); in btf_ext__set_endianness()
3288 btf_ext->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN); in btf_ext__set_endianness()
3290 if (!btf_ext->swapped_endian) { in btf_ext__set_endianness()
3291 free(btf_ext->data_swapped); in btf_ext__set_endianness()
3292 btf_ext->data_swapped = NULL; in btf_ext__set_endianness()
3315 * memory in-place with deduplicated types and strings without any loss of
3325 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
3353 * struct A* self;
3367 * struct B* self;
3382 * single CU with complete type information describing structs `S`, `A`, and
3398 * struct A* self;
3403 * struct B* self;
3433 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
3437 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
3454 return libbpf_err(-EINVAL); in btf__dedup()
3459 return libbpf_err(-EINVAL); in btf__dedup()
3463 err = -ENOMEM; in btf__dedup()
3513 #define BTF_UNPROCESSED_ID ((__u32)-1)
3514 #define BTF_IN_PROGRESS_ID ((__u32)-2)
3517 /* .BTF section to be deduped in-place */
3558 hashmap__for_each_key_entry(d->dedup_table, node, hash)
3562 return hashmap__append(d->dedup_table, hash, type_id); in btf_dedup_table_add()
3568 if (d->hypot_cnt == d->hypot_cap) { in btf_dedup_hypot_map_add()
3571 d->hypot_cap += max((size_t)16, d->hypot_cap / 2); in btf_dedup_hypot_map_add()
3572 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32)); in btf_dedup_hypot_map_add()
3574 return -ENOMEM; in btf_dedup_hypot_map_add()
3575 d->hypot_list = new_list; in btf_dedup_hypot_map_add()
3577 d->hypot_list[d->hypot_cnt++] = from_id; in btf_dedup_hypot_map_add()
3578 d->hypot_map[from_id] = to_id; in btf_dedup_hypot_map_add()
3586 for (i = 0; i < d->hypot_cnt; i++) in btf_dedup_clear_hypot_map()
3587 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; in btf_dedup_clear_hypot_map()
3588 d->hypot_cnt = 0; in btf_dedup_clear_hypot_map()
3589 d->hypot_adjust_canon = false; in btf_dedup_clear_hypot_map()
3594 hashmap__free(d->dedup_table); in btf_dedup_free()
3595 d->dedup_table = NULL; in btf_dedup_free()
3597 free(d->map); in btf_dedup_free()
3598 d->map = NULL; in btf_dedup_free()
3600 free(d->hypot_map); in btf_dedup_free()
3601 d->hypot_map = NULL; in btf_dedup_free()
3603 free(d->hypot_list); in btf_dedup_free()
3604 d->hypot_list = NULL; in btf_dedup_free()
3631 return ERR_PTR(-ENOMEM); in btf_dedup_new()
3636 d->btf = btf; in btf_dedup_new()
3637 d->btf_ext = OPTS_GET(opts, btf_ext, NULL); in btf_dedup_new()
3639 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL); in btf_dedup_new()
3640 if (IS_ERR(d->dedup_table)) { in btf_dedup_new()
3641 err = PTR_ERR(d->dedup_table); in btf_dedup_new()
3642 d->dedup_table = NULL; in btf_dedup_new()
3647 d->map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3648 if (!d->map) { in btf_dedup_new()
3649 err = -ENOMEM; in btf_dedup_new()
3653 d->map[0] = 0; in btf_dedup_new()
3655 struct btf_type *t = btf_type_by_id(d->btf, i); in btf_dedup_new()
3657 /* VAR and DATASEC are never deduped and are self-canonical */ in btf_dedup_new()
3659 d->map[i] = i; in btf_dedup_new()
3661 d->map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3664 d->hypot_map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3665 if (!d->hypot_map) { in btf_dedup_new()
3666 err = -ENOMEM; in btf_dedup_new()
3670 d->hypot_map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3689 for (i = 0; i < d->btf->nr_types; i++) { in btf_for_each_str_off()
3691 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_for_each_str_off()
3705 if (!d->btf_ext) in btf_for_each_str_off()
3708 r = btf_ext_visit_str_offs(d->btf_ext, fn, ctx); in btf_for_each_str_off()
3723 if (str_off == 0 || str_off < d->btf->start_str_off) in strs_dedup_remap_str_off()
3726 s = btf__str_by_offset(d->btf, str_off); in strs_dedup_remap_str_off()
3727 if (d->btf->base_btf) { in strs_dedup_remap_str_off()
3728 err = btf__find_str(d->btf->base_btf, s); in strs_dedup_remap_str_off()
3733 if (err != -ENOENT) in strs_dedup_remap_str_off()
3737 off = strset__add_str(d->strs_set, s); in strs_dedup_remap_str_off()
3741 *str_off_ptr = d->btf->start_str_off + off; in strs_dedup_remap_str_off()
3760 if (d->btf->strs_deduped) in btf_dedup_strings()
3763 d->strs_set = strset__new(BTF_MAX_STR_OFFSET, NULL, 0); in btf_dedup_strings()
3764 if (IS_ERR(d->strs_set)) { in btf_dedup_strings()
3765 err = PTR_ERR(d->strs_set); in btf_dedup_strings()
3769 if (!d->btf->base_btf) { in btf_dedup_strings()
3773 err = strset__add_str(d->strs_set, ""); in btf_dedup_strings()
3784 strset__free(d->btf->strs_set); in btf_dedup_strings()
3785 d->btf->hdr->str_len = strset__data_size(d->strs_set); in btf_dedup_strings()
3786 d->btf->strs_set = d->strs_set; in btf_dedup_strings()
3787 d->strs_set = NULL; in btf_dedup_strings()
3788 d->btf->strs_deduped = true; in btf_dedup_strings()
3792 strset__free(d->strs_set); in btf_dedup_strings()
3793 d->strs_set = NULL; in btf_dedup_strings()
3802 h = hash_combine(0, t->name_off); in btf_hash_common()
3803 h = hash_combine(h, t->info); in btf_hash_common()
3804 h = hash_combine(h, t->size); in btf_hash_common()
3810 return t1->name_off == t2->name_off && in btf_equal_common()
3811 t1->info == t2->info && in btf_equal_common()
3812 t1->size == t2->size; in btf_equal_common()
3844 h = hash_combine(0, t->name_off); in btf_hash_enum()
3858 if (m1->name_off != m2->name_off || m1->val != m2->val) in btf_equal_enum_members()
3876 if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 || in btf_equal_enum64_members()
3877 m1->val_hi32 != m2->val_hi32) in btf_equal_enum64_members()
3908 * - skip comparing vlen because it is zero for forward declarations; in btf_compat_enum()
3909 * - skip comparing size to allow enum forward declarations in btf_compat_enum()
3911 * - skip comparing kind for the same reason. in btf_compat_enum()
3913 return t1->name_off == t2->name_off && in btf_compat_enum()
3930 h = hash_combine(h, member->name_off); in btf_hash_struct()
3931 h = hash_combine(h, member->offset); in btf_hash_struct()
3956 if (m1->name_off != m2->name_off || m1->offset != m2->offset) in btf_shallow_equal_struct()
3974 h = hash_combine(h, info->type); in btf_hash_array()
3975 h = hash_combine(h, info->index_type); in btf_hash_array()
3976 h = hash_combine(h, info->nelems); in btf_hash_array()
3996 return info1->type == info2->type && in btf_equal_array()
3997 info1->index_type == info2->index_type && in btf_equal_array()
3998 info1->nelems == info2->nelems; in btf_equal_array()
4011 return btf_array(t1)->nelems == btf_array(t2)->nelems; in btf_compat_array()
4027 h = hash_combine(h, member->name_off); in btf_hash_fnproto()
4028 h = hash_combine(h, member->type); in btf_hash_fnproto()
4054 if (m1->name_off != m2->name_off || m1->type != m2->type) in btf_equal_fnproto()
4074 if (t1->name_off != t2->name_off || t1->info != t2->info) in btf_compat_fnproto()
4081 if (m1->name_off != m2->name_off) in btf_compat_fnproto()
4099 if (!d->btf->base_btf) in btf_dedup_prep()
4102 for (type_id = 1; type_id < d->btf->start_id; type_id++) { in btf_dedup_prep()
4103 t = btf_type_by_id(d->btf, type_id); in btf_dedup_prep()
4105 /* all base BTF types are self-canonical by definition */ in btf_dedup_prep()
4106 d->map[type_id] = type_id; in btf_dedup_prep()
4144 return -EINVAL; in btf_dedup_prep()
4147 return -ENOMEM; in btf_dedup_prep()
4157 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
4161 struct btf_type *t = btf_type_by_id(d->btf, type_id); in btf_dedup_prim_type()
4189 cand_id = hash_entry->value; in btf_dedup_prim_type()
4190 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4202 cand_id = hash_entry->value; in btf_dedup_prim_type()
4203 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4215 d->map[cand_id] = type_id; in btf_dedup_prim_type()
4224 cand_id = hash_entry->value; in btf_dedup_prim_type()
4225 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
4234 return -EINVAL; in btf_dedup_prim_type()
4237 d->map[type_id] = new_id; in btf_dedup_prim_type()
4239 return -ENOMEM; in btf_dedup_prim_type()
4248 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_prim_types()
4249 err = btf_dedup_prim_type(d, d->btf->start_id + i); in btf_dedup_prim_types()
4261 return d->map[type_id] <= BTF_MAX_NR_TYPES; in is_type_mapped()
4271 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_type_id()
4272 type_id = d->map[type_id]; in resolve_type_id()
4284 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
4287 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_fwd_id()
4288 type_id = d->map[type_id]; in resolve_fwd_id()
4290 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
4307 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_arrays()
4308 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_arrays()
4322 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_structs()
4323 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_structs()
4334 if (m1->type != m2->type && in btf_dedup_identical_structs()
4335 !btf_dedup_identical_arrays(d, m1->type, m2->type) && in btf_dedup_identical_structs()
4336 !btf_dedup_identical_structs(d, m1->type, m2->type)) in btf_dedup_identical_structs()
4350 * - >0, if type graphs are equivalent;
4351 * - 0, if not equivalent;
4352 * - <0, on error.
4354 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
4359 * a `cand_id` is recoded in hypothetical mapping (`btf_dedup->hypot_map`).
4366 * - `canon_id` is mapped to exactly the same type as `cand_id`. This will
4369 * - `canon_id` is mapped to different type. This is contradiction in our
4383 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
4408 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
4412 * - Both types in canonical and candidate graphs are FWDs. If they are
4416 * - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
4418 * - Type in canonical graph is FWD, while type in candidate is concrete
4427 * - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
4432 * turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
4451 hypot_type_id = d->hypot_map[canon_id]; in btf_dedup_is_equiv()
4459 * types sub-graph has a consistent and deduped-by-compiler in btf_dedup_is_equiv()
4470 * complicated and requires a many-to-many equivalence mapping. in btf_dedup_is_equiv()
4478 return -ENOMEM; in btf_dedup_is_equiv()
4480 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_is_equiv()
4481 canon_type = btf_type_by_id(d->btf, canon_id); in btf_dedup_is_equiv()
4485 if (cand_type->name_off != canon_type->name_off) in btf_dedup_is_equiv()
4488 /* FWD <--> STRUCT/UNION equivalence check, if enabled */ in btf_dedup_is_equiv()
4501 if (fwd_kind == real_kind && canon_id < d->btf->start_id) in btf_dedup_is_equiv()
4502 d->hypot_adjust_canon = true; in btf_dedup_is_equiv()
4529 if (cand_type->info != canon_type->info) in btf_dedup_is_equiv()
4531 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
4540 eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type); in btf_dedup_is_equiv()
4543 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); in btf_dedup_is_equiv()
4557 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); in btf_dedup_is_equiv()
4573 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
4580 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); in btf_dedup_is_equiv()
4590 return -EINVAL; in btf_dedup_is_equiv()
4600 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
4628 for (i = 0; i < d->hypot_cnt; i++) { in btf_dedup_merge_hypot_map()
4629 canon_type_id = d->hypot_list[i]; in btf_dedup_merge_hypot_map()
4630 targ_type_id = d->hypot_map[canon_type_id]; in btf_dedup_merge_hypot_map()
4633 t_kind = btf_kind(btf__type_by_id(d->btf, t_id)); in btf_dedup_merge_hypot_map()
4634 c_kind = btf_kind(btf__type_by_id(d->btf, c_id)); in btf_dedup_merge_hypot_map()
4639 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because in btf_dedup_merge_hypot_map()
4656 d->map[c_id] = t_id; in btf_dedup_merge_hypot_map()
4663 if (d->hypot_adjust_canon) in btf_dedup_merge_hypot_map()
4667 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4679 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4696 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
4697 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
4703 * as canonical for itself and is added into btf_dedup->dedup_table hash map
4716 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_struct_type()
4719 t = btf_type_by_id(d->btf, type_id); in btf_dedup_struct_type()
4727 __u32 cand_id = hash_entry->value; in btf_dedup_struct_type()
4737 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because in btf_dedup_struct_type()
4740 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_struct_type()
4751 if (d->hypot_adjust_canon) /* not really equivalent */ in btf_dedup_struct_type()
4757 d->map[type_id] = new_id; in btf_dedup_struct_type()
4759 return -ENOMEM; in btf_dedup_struct_type()
4768 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_struct_types()
4769 err = btf_dedup_struct_type(d, d->btf->start_id + i); in btf_dedup_struct_types()
4798 * btf_dedup->dedup_table as another possible canonical representative.
4809 if (d->map[type_id] == BTF_IN_PROGRESS_ID) in btf_dedup_ref_type()
4810 return -ELOOP; in btf_dedup_ref_type()
4811 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_ref_type()
4814 t = btf_type_by_id(d->btf, type_id); in btf_dedup_ref_type()
4815 d->map[type_id] = BTF_IN_PROGRESS_ID; in btf_dedup_ref_type()
4825 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4828 t->type = ref_type_id; in btf_dedup_ref_type()
4832 cand_id = hash_entry->value; in btf_dedup_ref_type()
4833 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4842 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4845 t->type = ref_type_id; in btf_dedup_ref_type()
4849 cand_id = hash_entry->value; in btf_dedup_ref_type()
4850 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4861 ref_type_id = btf_dedup_ref_type(d, info->type); in btf_dedup_ref_type()
4864 info->type = ref_type_id; in btf_dedup_ref_type()
4866 ref_type_id = btf_dedup_ref_type(d, info->index_type); in btf_dedup_ref_type()
4869 info->index_type = ref_type_id; in btf_dedup_ref_type()
4873 cand_id = hash_entry->value; in btf_dedup_ref_type()
4874 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4888 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4891 t->type = ref_type_id; in btf_dedup_ref_type()
4896 ref_type_id = btf_dedup_ref_type(d, param->type); in btf_dedup_ref_type()
4899 param->type = ref_type_id; in btf_dedup_ref_type()
4905 cand_id = hash_entry->value; in btf_dedup_ref_type()
4906 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4916 return -EINVAL; in btf_dedup_ref_type()
4919 d->map[type_id] = new_id; in btf_dedup_ref_type()
4921 return -ENOMEM; in btf_dedup_ref_type()
4930 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_ref_types()
4931 err = btf_dedup_ref_type(d, d->btf->start_id + i); in btf_dedup_ref_types()
4935 /* we won't need d->dedup_table anymore */ in btf_dedup_ref_types()
4936 hashmap__free(d->dedup_table); in btf_dedup_ref_types()
4937 d->dedup_table = NULL; in btf_dedup_ref_types()
4948 __u32 nr_types = btf__type_cnt(d->btf); in btf_dedup_fill_unique_names_map()
4959 t = btf_type_by_id(d->btf, type_id); in btf_dedup_fill_unique_names_map()
4965 /* Skip non-canonical types */ in btf_dedup_fill_unique_names_map()
4966 if (type_id != d->map[type_id]) in btf_dedup_fill_unique_names_map()
4969 err = hashmap__add(names_map, t->name_off, type_id); in btf_dedup_fill_unique_names_map()
4970 if (err == -EEXIST) in btf_dedup_fill_unique_names_map()
4971 err = hashmap__set(names_map, t->name_off, 0, NULL, NULL); in btf_dedup_fill_unique_names_map()
4982 struct btf_type *t = btf_type_by_id(d->btf, type_id); in btf_dedup_resolve_fwd()
4992 if (type_id != d->map[type_id]) in btf_dedup_resolve_fwd()
4995 if (!hashmap__find(names_map, t->name_off, &cand_id)) in btf_dedup_resolve_fwd()
5002 cand_t = btf_type_by_id(d->btf, cand_id); in btf_dedup_resolve_fwd()
5008 d->map[type_id] = cand_id; in btf_dedup_resolve_fwd()
5058 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_resolve_fwds()
5059 err = btf_dedup_resolve_fwd(d, names_map, d->btf->start_id + i); in btf_dedup_resolve_fwds()
5075 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
5083 __u32 next_type_id = d->btf->start_id; in btf_dedup_compact_types()
5089 d->hypot_map[0] = 0; in btf_dedup_compact_types()
5091 for (id = 1; id < d->btf->start_id; id++) in btf_dedup_compact_types()
5092 d->hypot_map[id] = id; in btf_dedup_compact_types()
5093 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) in btf_dedup_compact_types()
5094 d->hypot_map[id] = BTF_UNPROCESSED_ID; in btf_dedup_compact_types()
5096 p = d->btf->types_data; in btf_dedup_compact_types()
5098 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) { in btf_dedup_compact_types()
5099 if (d->map[id] != id) in btf_dedup_compact_types()
5102 t = btf__type_by_id(d->btf, id); in btf_dedup_compact_types()
5108 d->hypot_map[id] = next_type_id; in btf_dedup_compact_types()
5109 d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data; in btf_dedup_compact_types()
5115 d->btf->nr_types = next_type_id - d->btf->start_id; in btf_dedup_compact_types()
5116 d->btf->type_offs_cap = d->btf->nr_types; in btf_dedup_compact_types()
5117 d->btf->hdr->type_len = p - d->btf->types_data; in btf_dedup_compact_types()
5118 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap, in btf_dedup_compact_types()
5120 if (d->btf->type_offs_cap && !new_offs) in btf_dedup_compact_types()
5121 return -ENOMEM; in btf_dedup_compact_types()
5122 d->btf->type_offs = new_offs; in btf_dedup_compact_types()
5123 d->btf->hdr->str_off = d->btf->hdr->type_len; in btf_dedup_compact_types()
5124 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len; in btf_dedup_compact_types()
5131 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
5140 new_type_id = d->hypot_map[resolved_type_id]; in btf_dedup_remap_type_id()
5142 return -EINVAL; in btf_dedup_remap_type_id()
5153 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
5162 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_remap_types()
5163 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_dedup_remap_types()
5175 new_id = d->hypot_map[resolved_id]; in btf_dedup_remap_types()
5177 return -EINVAL; in btf_dedup_remap_types()
5183 if (!d->btf_ext) in btf_dedup_remap_types()
5186 r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d); in btf_dedup_remap_types()
5194 * Probe few well-known locations for vmlinux kernel image and try to load BTF
5202 "/boot/vmlinux-%1$s", in btf__load_vmlinux_btf()
5203 "/lib/modules/%1$s/vmlinux-%1$s", in btf__load_vmlinux_btf()
5206 "/usr/lib/debug/boot/vmlinux-%1$s", in btf__load_vmlinux_btf()
5207 "/usr/lib/debug/boot/vmlinux-%1$s.debug", in btf__load_vmlinux_btf()
5222 err = -errno; in btf__load_vmlinux_btf()
5249 return libbpf_err_ptr(-ESRCH); in btf__load_vmlinux_btf()
5268 seg = &btf_ext->func_info; in btf_ext_visit_type_ids()
5273 err = visit(&rec->type_id, ctx); in btf_ext_visit_type_ids()
5279 seg = &btf_ext->core_relo_info; in btf_ext_visit_type_ids()
5284 err = visit(&rec->type_id, ctx); in btf_ext_visit_type_ids()
5299 seg = &btf_ext->func_info; in btf_ext_visit_str_offs()
5301 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5306 seg = &btf_ext->line_info; in btf_ext_visit_str_offs()
5310 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5315 err = visit(&rec->file_name_off, ctx); in btf_ext_visit_str_offs()
5318 err = visit(&rec->line_off, ctx); in btf_ext_visit_str_offs()
5324 seg = &btf_ext->core_relo_info; in btf_ext_visit_str_offs()
5328 err = visit(&sec->sec_name_off, ctx); in btf_ext_visit_str_offs()
5333 err = visit(&rec->access_str_off, ctx); in btf_ext_visit_str_offs()
5352 struct btf_type *split_t = btf_type_by_id(dist->pipe.src, i); in btf_add_distilled_type_ids()
5366 if (*id >= dist->split_start_id) in btf_add_distilled_type_ids()
5369 if (dist->id_map[*id] > 0) in btf_add_distilled_type_ids()
5375 base_t = btf_type_by_id(dist->pipe.src, *id); in btf_add_distilled_type_ids()
5392 dist->id_map[*id] = *id; in btf_add_distilled_type_ids()
5397 return -EINVAL; in btf_add_distilled_type_ids()
5406 if (btf_is_composite(base_t) && base_t->name_off) in btf_add_distilled_type_ids()
5417 bool adding_to_base = dist->pipe.dst->start_id == 1; in btf_add_distilled_types()
5418 int id = btf__type_cnt(dist->pipe.dst); in btf_add_distilled_types()
5426 for (i = 1; i < dist->split_start_id; i++) { in btf_add_distilled_types()
5430 if (!dist->id_map[i]) in btf_add_distilled_types()
5432 t = btf_type_by_id(dist->pipe.src, i); in btf_add_distilled_types()
5434 name = btf__name_by_offset(dist->pipe.src, t->name_off); in btf_add_distilled_types()
5443 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5447 /* Named struct/union are added to base as 0-vlen in btf_add_distilled_types()
5449 * are added to split BTF as-is. in btf_add_distilled_types()
5452 if (!t->name_off) in btf_add_distilled_types()
5454 err = btf_add_composite(dist->pipe.dst, kind, name, t->size); in btf_add_distilled_types()
5456 if (t->name_off) in btf_add_distilled_types()
5458 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5464 * enum; relocation will match with appropriately-named in btf_add_distilled_types()
5467 * Anonymous enums are added to split BTF as-is. in btf_add_distilled_types()
5470 if (!t->name_off) in btf_add_distilled_types()
5472 err = btf__add_enum(dist->pipe.dst, name, t->size); in btf_add_distilled_types()
5474 if (t->name_off) in btf_add_distilled_types()
5476 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5490 err = btf_add_type(&dist->pipe, t); in btf_add_distilled_types()
5495 return -EINVAL; in btf_add_distilled_types()
5500 dist->id_map[i] = id++; in btf_add_distilled_types()
5512 struct btf_type *t = btf_type_by_id(dist->pipe.dst, i); in btf_update_distilled_type_ids()
5521 if (dist->id_map[*id]) in btf_update_distilled_type_ids()
5522 *id = dist->id_map[*id]; in btf_update_distilled_type_ids()
5523 else if (*id >= dist->split_start_id) in btf_update_distilled_type_ids()
5524 *id -= dist->diff_id; in btf_update_distilled_type_ids()
5542 * We return newly-created split BTF where the split BTF refers to a newly-created
5558 return libbpf_err(-EINVAL); in btf__distill_base()
5562 return libbpf_err(-ENOMEM); in btf__distill_base()
5568 err = -ENOMEM; in btf__distill_base()
5575 err = -ENOMEM; in btf__distill_base()
5579 dist.split_start_str = old_base->hdr->str_len; in btf__distill_base()
5585 for (i = src_btf->start_id; i < n; i++) { in btf__distill_base()
5604 err = -errno; in btf__distill_base()
5609 for (i = src_btf->start_id; i < n; i++) { in btf__distill_base()
5623 dist.diff_id = dist.split_start_id - btf__type_cnt(new_base); in btf__distill_base()
5648 return btf->hdr; in btf_header()
5653 btf->base_btf = (struct btf *)base_btf; in btf_set_base_btf()
5654 btf->start_id = btf__type_cnt(base_btf); in btf_set_base_btf()
5655 btf->start_str_off = base_btf->hdr->str_len; in btf_set_base_btf()
5663 btf->owns_base = false; in btf__relocate()