1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
13 #include "libbpf_util.h"
15 #define max(a, b) ((a) > (b) ? (a) : (b))
16 #define min(a, b) ((a) < (b) ? (a) : (b))
18 #define BTF_MAX_NR_TYPES 65535
20 #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
21 ((k) == BTF_KIND_VOLATILE) || \
22 ((k) == BTF_KIND_CONST) || \
23 ((k) == BTF_KIND_RESTRICT))
25 static struct btf_type btf_void;
29 struct btf_header *hdr;
32 struct btf_type **types;
43 * info points to a deep copy of the individual info section
44 * (e.g. func_info and line_info) from the .BTF.ext.
45 * It does not include the __u32 rec_size.
53 struct btf_ext_info func_info;
54 struct btf_ext_info line_info;
57 struct btf_ext_info_sec {
60 /* Followed by num_info * record_size number of bytes */
64 /* The minimum bpf_func_info checked by the loader */
65 struct bpf_func_info_min {
70 /* The minimum bpf_line_info checked by the loader */
71 struct bpf_line_info_min {
78 static inline __u64 ptr_to_u64(const void *ptr)
80 return (__u64) (unsigned long) ptr;
83 static int btf_add_type(struct btf *btf, struct btf_type *t)
85 if (btf->types_size - btf->nr_types < 2) {
86 struct btf_type **new_types;
87 __u32 expand_by, new_size;
89 if (btf->types_size == BTF_MAX_NR_TYPES)
92 expand_by = max(btf->types_size >> 2, 16);
93 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
95 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
99 if (btf->nr_types == 0)
100 new_types[0] = &btf_void;
102 btf->types = new_types;
103 btf->types_size = new_size;
106 btf->types[++(btf->nr_types)] = t;
111 static int btf_parse_hdr(struct btf *btf)
113 const struct btf_header *hdr = btf->hdr;
116 if (btf->data_size < sizeof(struct btf_header)) {
117 pr_debug("BTF header not found\n");
121 if (hdr->magic != BTF_MAGIC) {
122 pr_debug("Invalid BTF magic:%x\n", hdr->magic);
126 if (hdr->version != BTF_VERSION) {
127 pr_debug("Unsupported BTF version:%u\n", hdr->version);
132 pr_debug("Unsupported BTF flags:%x\n", hdr->flags);
136 meta_left = btf->data_size - sizeof(*hdr);
138 pr_debug("BTF has no data\n");
142 if (meta_left < hdr->type_off) {
143 pr_debug("Invalid BTF type section offset:%u\n", hdr->type_off);
147 if (meta_left < hdr->str_off) {
148 pr_debug("Invalid BTF string section offset:%u\n", hdr->str_off);
152 if (hdr->type_off >= hdr->str_off) {
153 pr_debug("BTF type section offset >= string section offset. No type?\n");
157 if (hdr->type_off & 0x02) {
158 pr_debug("BTF type section is not aligned to 4 bytes\n");
162 btf->nohdr_data = btf->hdr + 1;
167 static int btf_parse_str_sec(struct btf *btf)
169 const struct btf_header *hdr = btf->hdr;
170 const char *start = btf->nohdr_data + hdr->str_off;
171 const char *end = start + btf->hdr->str_len;
173 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
174 start[0] || end[-1]) {
175 pr_debug("Invalid BTF string section\n");
179 btf->strings = start;
184 static int btf_parse_type_sec(struct btf *btf)
186 struct btf_header *hdr = btf->hdr;
187 void *nohdr_data = btf->nohdr_data;
188 void *next_type = nohdr_data + hdr->type_off;
189 void *end_type = nohdr_data + hdr->str_off;
191 while (next_type < end_type) {
192 struct btf_type *t = next_type;
193 __u16 vlen = BTF_INFO_VLEN(t->info);
196 next_type += sizeof(*t);
197 switch (BTF_INFO_KIND(t->info)) {
199 next_type += sizeof(int);
202 next_type += sizeof(struct btf_array);
204 case BTF_KIND_STRUCT:
206 next_type += vlen * sizeof(struct btf_member);
209 next_type += vlen * sizeof(struct btf_enum);
211 case BTF_KIND_FUNC_PROTO:
212 next_type += vlen * sizeof(struct btf_param);
215 case BTF_KIND_TYPEDEF:
218 case BTF_KIND_VOLATILE:
220 case BTF_KIND_RESTRICT:
223 pr_debug("Unsupported BTF_KIND:%u\n",
224 BTF_INFO_KIND(t->info));
228 err = btf_add_type(btf, t);
236 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
238 if (type_id > btf->nr_types)
241 return btf->types[type_id];
244 static bool btf_type_is_void(const struct btf_type *t)
246 return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
249 static bool btf_type_is_void_or_null(const struct btf_type *t)
251 return !t || btf_type_is_void(t);
254 static __s64 btf_type_size(const struct btf_type *t)
256 switch (BTF_INFO_KIND(t->info)) {
258 case BTF_KIND_STRUCT:
263 return sizeof(void *);
269 #define MAX_RESOLVE_DEPTH 32
271 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
273 const struct btf_array *array;
274 const struct btf_type *t;
279 t = btf__type_by_id(btf, type_id);
280 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
282 size = btf_type_size(t);
286 switch (BTF_INFO_KIND(t->info)) {
287 case BTF_KIND_TYPEDEF:
288 case BTF_KIND_VOLATILE:
290 case BTF_KIND_RESTRICT:
294 array = (const struct btf_array *)(t + 1);
295 if (nelems && array->nelems > UINT32_MAX / nelems)
297 nelems *= array->nelems;
298 type_id = array->type;
304 t = btf__type_by_id(btf, type_id);
310 if (nelems && size > UINT32_MAX / nelems)
313 return nelems * size;
316 int btf__resolve_type(const struct btf *btf, __u32 type_id)
318 const struct btf_type *t;
321 t = btf__type_by_id(btf, type_id);
322 while (depth < MAX_RESOLVE_DEPTH &&
323 !btf_type_is_void_or_null(t) &&
324 IS_MODIFIER(BTF_INFO_KIND(t->info))) {
326 t = btf__type_by_id(btf, type_id);
330 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
336 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
340 if (!strcmp(type_name, "void"))
343 for (i = 1; i <= btf->nr_types; i++) {
344 const struct btf_type *t = btf->types[i];
345 const char *name = btf__name_by_offset(btf, t->name_off);
347 if (name && !strcmp(type_name, name))
354 void btf__free(struct btf *btf)
367 struct btf *btf__new(__u8 *data, __u32 size)
369 __u32 log_buf_size = 0;
370 char *log_buf = NULL;
374 btf = calloc(1, sizeof(struct btf));
376 return ERR_PTR(-ENOMEM);
380 log_buf = malloc(BPF_LOG_BUF_SIZE);
387 log_buf_size = BPF_LOG_BUF_SIZE;
389 btf->data = malloc(size);
395 memcpy(btf->data, data, size);
396 btf->data_size = size;
398 btf->fd = bpf_load_btf(btf->data, btf->data_size,
399 log_buf, log_buf_size, false);
403 pr_warning("Error loading BTF: %s(%d)\n", strerror(errno), errno);
404 if (log_buf && *log_buf)
405 pr_warning("%s\n", log_buf);
409 err = btf_parse_hdr(btf);
413 err = btf_parse_str_sec(btf);
417 err = btf_parse_type_sec(btf);
430 int btf__fd(const struct btf *btf)
435 const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
437 if (offset < btf->hdr->str_len)
438 return &btf->strings[offset];
443 int btf__get_from_id(__u32 id, struct btf **btf)
445 struct bpf_btf_info btf_info = { 0 };
446 __u32 len = sizeof(btf_info);
454 btf_fd = bpf_btf_get_fd_by_id(id);
458 /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
459 * let's start with a sane default - 4KiB here - and resize it only if
460 * bpf_obj_get_info_by_fd() needs a bigger buffer.
462 btf_info.btf_size = 4096;
463 last_size = btf_info.btf_size;
464 ptr = malloc(last_size);
470 bzero(ptr, last_size);
471 btf_info.btf = ptr_to_u64(ptr);
472 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
474 if (!err && btf_info.btf_size > last_size) {
477 last_size = btf_info.btf_size;
478 temp_ptr = realloc(ptr, last_size);
484 bzero(ptr, last_size);
485 btf_info.btf = ptr_to_u64(ptr);
486 err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
489 if (err || btf_info.btf_size > last_size) {
494 *btf = btf__new((__u8 *)(long)btf_info.btf, btf_info.btf_size);
507 struct btf_ext_sec_copy_param {
511 struct btf_ext_info *ext_info;
515 static int btf_ext_copy_info(struct btf_ext *btf_ext,
516 __u8 *data, __u32 data_size,
517 struct btf_ext_sec_copy_param *ext_sec)
519 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
520 const struct btf_ext_info_sec *sinfo;
521 struct btf_ext_info *ext_info;
522 __u32 info_left, record_size;
523 /* The start of the info sec (including the __u32 record_size). */
526 /* data and data_size do not include btf_ext_header from now on */
527 data = data + hdr->hdr_len;
528 data_size -= hdr->hdr_len;
530 if (ext_sec->off & 0x03) {
531 pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
536 if (data_size < ext_sec->off ||
537 ext_sec->len > data_size - ext_sec->off) {
538 pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
539 ext_sec->desc, ext_sec->off, ext_sec->len);
543 info = data + ext_sec->off;
544 info_left = ext_sec->len;
546 /* At least a record size */
547 if (info_left < sizeof(__u32)) {
548 pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
552 /* The record size needs to meet the minimum standard */
553 record_size = *(__u32 *)info;
554 if (record_size < ext_sec->min_rec_size ||
555 record_size & 0x03) {
556 pr_debug("%s section in .BTF.ext has invalid record size %u\n",
557 ext_sec->desc, record_size);
561 sinfo = info + sizeof(__u32);
562 info_left -= sizeof(__u32);
564 /* If no records, return failure now so .BTF.ext won't be used. */
566 pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
571 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
572 __u64 total_record_size;
575 if (info_left < sec_hdrlen) {
576 pr_debug("%s section header is not found in .BTF.ext\n",
581 num_records = sinfo->num_info;
582 if (num_records == 0) {
583 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
588 total_record_size = sec_hdrlen +
589 (__u64)num_records * record_size;
590 if (info_left < total_record_size) {
591 pr_debug("%s section has incorrect num_records in .BTF.ext\n",
596 info_left -= total_record_size;
597 sinfo = (void *)sinfo + total_record_size;
600 ext_info = ext_sec->ext_info;
601 ext_info->len = ext_sec->len - sizeof(__u32);
602 ext_info->rec_size = record_size;
603 ext_info->info = malloc(ext_info->len);
606 memcpy(ext_info->info, info + sizeof(__u32), ext_info->len);
611 static int btf_ext_copy_func_info(struct btf_ext *btf_ext,
612 __u8 *data, __u32 data_size)
614 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
615 struct btf_ext_sec_copy_param param = {
616 .off = hdr->func_info_off,
617 .len = hdr->func_info_len,
618 .min_rec_size = sizeof(struct bpf_func_info_min),
619 .ext_info = &btf_ext->func_info,
623 return btf_ext_copy_info(btf_ext, data, data_size, ¶m);
626 static int btf_ext_copy_line_info(struct btf_ext *btf_ext,
627 __u8 *data, __u32 data_size)
629 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
630 struct btf_ext_sec_copy_param param = {
631 .off = hdr->line_info_off,
632 .len = hdr->line_info_len,
633 .min_rec_size = sizeof(struct bpf_line_info_min),
634 .ext_info = &btf_ext->line_info,
638 return btf_ext_copy_info(btf_ext, data, data_size, ¶m);
641 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
643 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
645 if (data_size < offsetof(struct btf_ext_header, func_info_off) ||
646 data_size < hdr->hdr_len) {
647 pr_debug("BTF.ext header not found");
651 if (hdr->magic != BTF_MAGIC) {
652 pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
656 if (hdr->version != BTF_VERSION) {
657 pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
662 pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
666 if (data_size == hdr->hdr_len) {
667 pr_debug("BTF.ext has no data\n");
674 void btf_ext__free(struct btf_ext *btf_ext)
679 free(btf_ext->func_info.info);
680 free(btf_ext->line_info.info);
684 struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
686 struct btf_ext *btf_ext;
689 err = btf_ext_parse_hdr(data, size);
693 btf_ext = calloc(1, sizeof(struct btf_ext));
695 return ERR_PTR(-ENOMEM);
697 err = btf_ext_copy_func_info(btf_ext, data, size);
699 btf_ext__free(btf_ext);
703 err = btf_ext_copy_line_info(btf_ext, data, size);
705 btf_ext__free(btf_ext);
712 static int btf_ext_reloc_info(const struct btf *btf,
713 const struct btf_ext_info *ext_info,
714 const char *sec_name, __u32 insns_cnt,
715 void **info, __u32 *cnt)
717 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
718 __u32 i, record_size, existing_len, records_len;
719 struct btf_ext_info_sec *sinfo;
720 const char *info_sec_name;
724 record_size = ext_info->rec_size;
725 sinfo = ext_info->info;
726 remain_len = ext_info->len;
727 while (remain_len > 0) {
728 records_len = sinfo->num_info * record_size;
729 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
730 if (strcmp(info_sec_name, sec_name)) {
731 remain_len -= sec_hdrlen + records_len;
732 sinfo = (void *)sinfo + sec_hdrlen + records_len;
736 existing_len = (*cnt) * record_size;
737 data = realloc(*info, existing_len + records_len);
741 memcpy(data + existing_len, sinfo->data, records_len);
742 /* adjust insn_off only, the rest data will be passed
745 for (i = 0; i < sinfo->num_info; i++) {
748 insn_off = data + existing_len + (i * record_size);
749 *insn_off = *insn_off / sizeof(struct bpf_insn) +
753 *cnt += sinfo->num_info;
760 int btf_ext__reloc_func_info(const struct btf *btf, const struct btf_ext *btf_ext,
761 const char *sec_name, __u32 insns_cnt,
762 void **func_info, __u32 *cnt)
764 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
765 insns_cnt, func_info, cnt);
768 int btf_ext__reloc_line_info(const struct btf *btf, const struct btf_ext *btf_ext,
769 const char *sec_name, __u32 insns_cnt,
770 void **line_info, __u32 *cnt)
772 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
773 insns_cnt, line_info, cnt);
776 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
778 return btf_ext->func_info.rec_size;
781 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
783 return btf_ext->line_info.rec_size;