1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2018 Facebook */
13 #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
14 #define max(a, b) ((a) > (b) ? (a) : (b))
15 #define min(a, b) ((a) < (b) ? (a) : (b))
17 #define BTF_MAX_NR_TYPES 65535
19 #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
20 ((k) == BTF_KIND_VOLATILE) || \
21 ((k) == BTF_KIND_CONST) || \
22 ((k) == BTF_KIND_RESTRICT))
24 static struct btf_type btf_void;
28 struct btf_header *hdr;
31 struct btf_type **types;
42 * info points to a deep copy of the individual info section
43 * (e.g. func_info and line_info) from the .BTF.ext.
44 * It does not include the __u32 rec_size.
52 struct btf_ext_info func_info;
53 struct btf_ext_info line_info;
56 struct btf_ext_info_sec {
59 /* Followed by num_info * record_size number of bytes */
63 /* The minimum bpf_func_info checked by the loader */
64 struct bpf_func_info_min {
69 /* The minimum bpf_line_info checked by the loader */
70 struct bpf_line_info_min {
77 static inline __u64 ptr_to_u64(const void *ptr)
79 return (__u64) (unsigned long) ptr;
82 static int btf_add_type(struct btf *btf, struct btf_type *t)
84 if (btf->types_size - btf->nr_types < 2) {
85 struct btf_type **new_types;
86 __u32 expand_by, new_size;
88 if (btf->types_size == BTF_MAX_NR_TYPES)
91 expand_by = max(btf->types_size >> 2, 16);
92 new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
94 new_types = realloc(btf->types, sizeof(*new_types) * new_size);
98 if (btf->nr_types == 0)
99 new_types[0] = &btf_void;
101 btf->types = new_types;
102 btf->types_size = new_size;
105 btf->types[++(btf->nr_types)] = t;
110 static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
112 const struct btf_header *hdr = btf->hdr;
115 if (btf->data_size < sizeof(struct btf_header)) {
116 elog("BTF header not found\n");
120 if (hdr->magic != BTF_MAGIC) {
121 elog("Invalid BTF magic:%x\n", hdr->magic);
125 if (hdr->version != BTF_VERSION) {
126 elog("Unsupported BTF version:%u\n", hdr->version);
131 elog("Unsupported BTF flags:%x\n", hdr->flags);
135 meta_left = btf->data_size - sizeof(*hdr);
137 elog("BTF has no data\n");
141 if (meta_left < hdr->type_off) {
142 elog("Invalid BTF type section offset:%u\n", hdr->type_off);
146 if (meta_left < hdr->str_off) {
147 elog("Invalid BTF string section offset:%u\n", hdr->str_off);
151 if (hdr->type_off >= hdr->str_off) {
152 elog("BTF type section offset >= string section offset. No type?\n");
156 if (hdr->type_off & 0x02) {
157 elog("BTF type section is not aligned to 4 bytes\n");
161 btf->nohdr_data = btf->hdr + 1;
166 static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
168 const struct btf_header *hdr = btf->hdr;
169 const char *start = btf->nohdr_data + hdr->str_off;
170 const char *end = start + btf->hdr->str_len;
172 if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
173 start[0] || end[-1]) {
174 elog("Invalid BTF string section\n");
178 btf->strings = start;
183 static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
185 struct btf_header *hdr = btf->hdr;
186 void *nohdr_data = btf->nohdr_data;
187 void *next_type = nohdr_data + hdr->type_off;
188 void *end_type = nohdr_data + hdr->str_off;
190 while (next_type < end_type) {
191 struct btf_type *t = next_type;
192 __u16 vlen = BTF_INFO_VLEN(t->info);
195 next_type += sizeof(*t);
196 switch (BTF_INFO_KIND(t->info)) {
198 next_type += sizeof(int);
201 next_type += sizeof(struct btf_array);
203 case BTF_KIND_STRUCT:
205 next_type += vlen * sizeof(struct btf_member);
208 next_type += vlen * sizeof(struct btf_enum);
210 case BTF_KIND_FUNC_PROTO:
211 next_type += vlen * sizeof(struct btf_param);
214 case BTF_KIND_TYPEDEF:
217 case BTF_KIND_VOLATILE:
219 case BTF_KIND_RESTRICT:
222 elog("Unsupported BTF_KIND:%u\n",
223 BTF_INFO_KIND(t->info));
227 err = btf_add_type(btf, t);
235 const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
237 if (type_id > btf->nr_types)
240 return btf->types[type_id];
243 static bool btf_type_is_void(const struct btf_type *t)
245 return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
248 static bool btf_type_is_void_or_null(const struct btf_type *t)
250 return !t || btf_type_is_void(t);
253 static __s64 btf_type_size(const struct btf_type *t)
255 switch (BTF_INFO_KIND(t->info)) {
257 case BTF_KIND_STRUCT:
262 return sizeof(void *);
268 #define MAX_RESOLVE_DEPTH 32
270 __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
272 const struct btf_array *array;
273 const struct btf_type *t;
278 t = btf__type_by_id(btf, type_id);
279 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
281 size = btf_type_size(t);
285 switch (BTF_INFO_KIND(t->info)) {
286 case BTF_KIND_TYPEDEF:
287 case BTF_KIND_VOLATILE:
289 case BTF_KIND_RESTRICT:
293 array = (const struct btf_array *)(t + 1);
294 if (nelems && array->nelems > UINT32_MAX / nelems)
296 nelems *= array->nelems;
297 type_id = array->type;
303 t = btf__type_by_id(btf, type_id);
309 if (nelems && size > UINT32_MAX / nelems)
312 return nelems * size;
315 int btf__resolve_type(const struct btf *btf, __u32 type_id)
317 const struct btf_type *t;
320 t = btf__type_by_id(btf, type_id);
321 while (depth < MAX_RESOLVE_DEPTH &&
322 !btf_type_is_void_or_null(t) &&
323 IS_MODIFIER(BTF_INFO_KIND(t->info))) {
325 t = btf__type_by_id(btf, type_id);
329 if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
335 __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
339 if (!strcmp(type_name, "void"))
342 for (i = 1; i <= btf->nr_types; i++) {
343 const struct btf_type *t = btf->types[i];
344 const char *name = btf__name_by_offset(btf, t->name_off);
346 if (name && !strcmp(type_name, name))
353 void btf__free(struct btf *btf)
366 struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log)
368 __u32 log_buf_size = 0;
369 char *log_buf = NULL;
373 btf = calloc(1, sizeof(struct btf));
375 return ERR_PTR(-ENOMEM);
380 log_buf = malloc(BPF_LOG_BUF_SIZE);
386 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 elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
404 if (log_buf && *log_buf)
405 elog("%s\n", log_buf);
409 err = btf_parse_hdr(btf, err_log);
413 err = btf_parse_str_sec(btf, err_log);
417 err = btf_parse_type_sec(btf, err_log);
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, NULL);
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,
518 btf_print_fn_t err_log)
520 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
521 const struct btf_ext_info_sec *sinfo;
522 struct btf_ext_info *ext_info;
523 __u32 info_left, record_size;
524 /* The start of the info sec (including the __u32 record_size). */
527 /* data and data_size do not include btf_ext_header from now on */
528 data = data + hdr->hdr_len;
529 data_size -= hdr->hdr_len;
531 if (ext_sec->off & 0x03) {
532 elog(".BTF.ext %s section is not aligned to 4 bytes\n",
537 if (data_size < ext_sec->off ||
538 ext_sec->len > data_size - ext_sec->off) {
539 elog("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
540 ext_sec->desc, ext_sec->off, ext_sec->len);
544 info = data + ext_sec->off;
545 info_left = ext_sec->len;
547 /* At least a record size */
548 if (info_left < sizeof(__u32)) {
549 elog(".BTF.ext %s record size not found\n", ext_sec->desc);
553 /* The record size needs to meet the minimum standard */
554 record_size = *(__u32 *)info;
555 if (record_size < ext_sec->min_rec_size ||
556 record_size & 0x03) {
557 elog("%s section in .BTF.ext has invalid record size %u\n",
558 ext_sec->desc, record_size);
562 sinfo = info + sizeof(__u32);
563 info_left -= sizeof(__u32);
565 /* If no records, return failure now so .BTF.ext won't be used. */
567 elog("%s section in .BTF.ext has no records", ext_sec->desc);
572 unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
573 __u64 total_record_size;
576 if (info_left < sec_hdrlen) {
577 elog("%s section header is not found in .BTF.ext\n",
582 num_records = sinfo->num_info;
583 if (num_records == 0) {
584 elog("%s section has incorrect num_records in .BTF.ext\n",
589 total_record_size = sec_hdrlen +
590 (__u64)num_records * record_size;
591 if (info_left < total_record_size) {
592 elog("%s section has incorrect num_records in .BTF.ext\n",
597 info_left -= total_record_size;
598 sinfo = (void *)sinfo + total_record_size;
601 ext_info = ext_sec->ext_info;
602 ext_info->len = ext_sec->len - sizeof(__u32);
603 ext_info->rec_size = record_size;
604 ext_info->info = malloc(ext_info->len);
607 memcpy(ext_info->info, info + sizeof(__u32), ext_info->len);
612 static int btf_ext_copy_func_info(struct btf_ext *btf_ext,
613 __u8 *data, __u32 data_size,
614 btf_print_fn_t err_log)
616 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
617 struct btf_ext_sec_copy_param param = {
618 .off = hdr->func_info_off,
619 .len = hdr->func_info_len,
620 .min_rec_size = sizeof(struct bpf_func_info_min),
621 .ext_info = &btf_ext->func_info,
625 return btf_ext_copy_info(btf_ext, data, data_size, ¶m, err_log);
628 static int btf_ext_copy_line_info(struct btf_ext *btf_ext,
629 __u8 *data, __u32 data_size,
630 btf_print_fn_t err_log)
632 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
633 struct btf_ext_sec_copy_param param = {
634 .off = hdr->line_info_off,
635 .len = hdr->line_info_len,
636 .min_rec_size = sizeof(struct bpf_line_info_min),
637 .ext_info = &btf_ext->line_info,
641 return btf_ext_copy_info(btf_ext, data, data_size, ¶m, err_log);
644 static int btf_ext_parse_hdr(__u8 *data, __u32 data_size,
645 btf_print_fn_t err_log)
647 const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
649 if (data_size < offsetof(struct btf_ext_header, func_info_off) ||
650 data_size < hdr->hdr_len) {
651 elog("BTF.ext header not found");
655 if (hdr->magic != BTF_MAGIC) {
656 elog("Invalid BTF.ext magic:%x\n", hdr->magic);
660 if (hdr->version != BTF_VERSION) {
661 elog("Unsupported BTF.ext version:%u\n", hdr->version);
666 elog("Unsupported BTF.ext flags:%x\n", hdr->flags);
670 if (data_size == hdr->hdr_len) {
671 elog("BTF.ext has no data\n");
678 void btf_ext__free(struct btf_ext *btf_ext)
683 free(btf_ext->func_info.info);
684 free(btf_ext->line_info.info);
688 struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log)
690 struct btf_ext *btf_ext;
693 err = btf_ext_parse_hdr(data, size, err_log);
697 btf_ext = calloc(1, sizeof(struct btf_ext));
699 return ERR_PTR(-ENOMEM);
701 err = btf_ext_copy_func_info(btf_ext, data, size, err_log);
703 btf_ext__free(btf_ext);
707 err = btf_ext_copy_line_info(btf_ext, data, size, err_log);
709 btf_ext__free(btf_ext);
716 static int btf_ext_reloc_info(const struct btf *btf,
717 const struct btf_ext_info *ext_info,
718 const char *sec_name, __u32 insns_cnt,
719 void **info, __u32 *cnt)
721 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
722 __u32 i, record_size, existing_len, records_len;
723 struct btf_ext_info_sec *sinfo;
724 const char *info_sec_name;
728 record_size = ext_info->rec_size;
729 sinfo = ext_info->info;
730 remain_len = ext_info->len;
731 while (remain_len > 0) {
732 records_len = sinfo->num_info * record_size;
733 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
734 if (strcmp(info_sec_name, sec_name)) {
735 remain_len -= sec_hdrlen + records_len;
736 sinfo = (void *)sinfo + sec_hdrlen + records_len;
740 existing_len = (*cnt) * record_size;
741 data = realloc(*info, existing_len + records_len);
745 memcpy(data + existing_len, sinfo->data, records_len);
746 /* adjust insn_off only, the rest data will be passed
749 for (i = 0; i < sinfo->num_info; i++) {
752 insn_off = data + existing_len + (i * record_size);
753 *insn_off = *insn_off / sizeof(struct bpf_insn) +
757 *cnt += sinfo->num_info;
764 int btf_ext__reloc_func_info(const struct btf *btf, const struct btf_ext *btf_ext,
765 const char *sec_name, __u32 insns_cnt,
766 void **func_info, __u32 *cnt)
768 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
769 insns_cnt, func_info, cnt);
772 int btf_ext__reloc_line_info(const struct btf *btf, const struct btf_ext *btf_ext,
773 const char *sec_name, __u32 insns_cnt,
774 void **line_info, __u32 *cnt)
776 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
777 insns_cnt, line_info, cnt);
780 __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
782 return btf_ext->func_info.rec_size;
785 __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
787 return btf_ext->line_info.rec_size;