Prep 1.29
[dwarves.git] / btf_loader.c
blobf4f9f65289b5acac8bea938119624e9eadeb5fb6
1 /*
2 * btf_loader.c
4 * Copyright (C) 2018 Arnaldo Carvalho de Melo <acme@kernel.org>
6 * Based on ctf_loader.c that, in turn, was based on ctfdump.c: CTF dumper.
8 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
9 */
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <malloc.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <libgen.h>
23 #include <linux/btf.h>
24 #include <bpf/btf.h>
25 #include <bpf/libbpf.h>
26 #include <zlib.h>
28 #include <gelf.h>
30 #include "dutil.h"
31 #include "dwarves.h"
33 static const char *cu__btf_str(struct cu *cu, uint32_t offset)
35 return offset ? btf__str_by_offset(cu->priv, offset) : NULL;
38 static void *tag__alloc(const size_t size)
40 struct tag *tag = zalloc(size);
42 if (tag != NULL)
43 tag->top_level = 1;
45 return tag;
48 static int cu__load_ftype(struct cu *cu, struct ftype *proto, uint32_t tag, const struct btf_type *tp, uint32_t id)
50 const struct btf_param *param = btf_params(tp);
51 int i, vlen = btf_vlen(tp);
53 proto->tag.tag = tag;
54 proto->tag.type = tp->type;
55 INIT_LIST_HEAD(&proto->parms);
56 INIT_LIST_HEAD(&proto->template_type_params);
57 INIT_LIST_HEAD(&proto->template_value_params);
58 proto->template_parameter_pack = NULL;
59 proto->formal_parameter_pack = NULL;
61 for (i = 0; i < vlen; ++i, param++) {
62 if (param->type == 0)
63 proto->unspec_parms = 1;
64 else {
65 struct parameter *p = tag__alloc(sizeof(*p));
67 if (p == NULL)
68 goto out_free_parameters;
69 p->tag.tag = DW_TAG_formal_parameter;
70 p->tag.type = param->type;
71 p->name = cu__btf_str(cu, param->name_off);
72 ftype__add_parameter(proto, p);
76 cu__add_tag_with_id(cu, &proto->tag, id);
78 return 0;
79 out_free_parameters:
80 ftype__delete(proto, cu);
81 return -ENOMEM;
84 static int create_new_function(struct cu *cu, const struct btf_type *tp, uint32_t id)
86 struct function *func = tag__alloc(sizeof(*func));
88 if (func == NULL)
89 return -ENOMEM;
91 // for BTF this is not really the type of the return of the function,
92 // but the prototype, the return type is the one in type_id
93 func->btf = 1;
94 func->proto.tag.tag = DW_TAG_subprogram;
95 func->proto.tag.type = tp->type;
96 func->name = cu__btf_str(cu, tp->name_off);
97 INIT_LIST_HEAD(&func->lexblock.tags);
98 cu__add_tag_with_id(cu, &func->proto.tag, id);
100 return 0;
103 static struct base_type *base_type__new(const char *name, uint32_t attrs,
104 uint8_t float_type, size_t size)
106 struct base_type *bt = tag__alloc(sizeof(*bt));
108 if (bt != NULL) {
109 bt->name = name;
110 bt->bit_size = size;
111 bt->is_signed = attrs & BTF_INT_SIGNED;
112 bt->is_bool = attrs & BTF_INT_BOOL;
113 bt->name_has_encoding = false;
114 bt->float_type = float_type;
115 INIT_LIST_HEAD(&bt->node);
117 return bt;
120 static void type__init(struct type *type, uint32_t tag, const char *name, size_t size)
122 __type__init(type);
123 INIT_LIST_HEAD(&type->namespace.tags);
124 type->size = size;
125 type->namespace.tag.tag = tag;
126 type->namespace.name = name;
127 type->template_parameter_pack = NULL;
130 static struct type *type__new(uint16_t tag, const char *name, size_t size)
132 struct type *type = tag__alloc(sizeof(*type));
134 if (type != NULL)
135 type__init(type, tag, name, size);
137 return type;
140 static struct class *class__new(const char *name, size_t size, bool is_union)
142 struct class *class = tag__alloc(sizeof(*class));
143 uint32_t tag = is_union ? DW_TAG_union_type : DW_TAG_structure_type;
145 if (class != NULL) {
146 type__init(&class->type, tag, name, size);
147 INIT_LIST_HEAD(&class->vtable);
150 return class;
153 static struct variable *variable__new(const char *name, uint32_t linkage)
155 struct variable *var = tag__alloc(sizeof(*var));
157 if (var != NULL) {
158 var->external = linkage == BTF_VAR_GLOBAL_ALLOCATED;
159 var->name = name;
160 var->ip.tag.tag = DW_TAG_variable;
163 return var;
166 static int create_new_int_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
168 uint32_t attrs = btf_int_encoding(tp);
169 const char *name = cu__btf_str(cu, tp->name_off);
170 struct base_type *base = base_type__new(name, attrs, 0, btf_int_bits(tp));
172 if (base == NULL)
173 return -ENOMEM;
175 base->tag.tag = DW_TAG_base_type;
176 cu__add_tag_with_id(cu, &base->tag, id);
178 return 0;
181 static int create_new_float_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
183 const char *name = cu__btf_str(cu, tp->name_off);
184 struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
186 if (base == NULL)
187 return -ENOMEM;
189 base->tag.tag = DW_TAG_base_type;
190 cu__add_tag_with_id(cu, &base->tag, id);
192 return 0;
195 static int create_new_array(struct cu *cu, const struct btf_type *tp, uint32_t id)
197 struct btf_array *ap = btf_array(tp);
198 struct array_type *array = tag__alloc(sizeof(*array));
200 if (array == NULL)
201 return -ENOMEM;
203 /* FIXME: where to get the number of dimensions?
204 * it it flattened? */
205 array->dimensions = 1;
206 array->nr_entries = malloc(sizeof(uint32_t));
208 if (array->nr_entries == NULL) {
209 free(array);
210 return -ENOMEM;
213 array->nr_entries[0] = ap->nelems;
214 array->tag.tag = DW_TAG_array_type;
215 array->tag.type = ap->type;
217 cu__add_tag_with_id(cu, &array->tag, id);
219 return 0;
222 static int create_members(struct cu *cu, const struct btf_type *tp, struct type *class)
224 struct btf_member *mp = btf_members(tp);
225 int i, vlen = btf_vlen(tp);
227 for (i = 0; i < vlen; i++) {
228 struct class_member *member = zalloc(sizeof(*member));
230 if (member == NULL)
231 return -ENOMEM;
233 member->tag.tag = DW_TAG_member;
234 member->tag.type = mp[i].type;
235 member->name = cu__btf_str(cu, mp[i].name_off);
236 member->bit_offset = btf_member_bit_offset(tp, i);
237 member->bitfield_size = btf_member_bitfield_size(tp, i);
238 member->byte_offset = member->bit_offset / 8;
239 /* sizes and offsets will be corrected at class__fixup_btf_bitfields */
240 type__add_member(class, member);
243 return 0;
246 static int create_new_class(struct cu *cu, const struct btf_type *tp, uint32_t id)
248 struct class *class = class__new(cu__btf_str(cu, tp->name_off), tp->size, false);
249 int member_size = create_members(cu, tp, &class->type);
251 if (member_size < 0)
252 goto out_free;
254 cu__add_tag_with_id(cu, &class->type.namespace.tag, id);
256 return 0;
257 out_free:
258 class__delete(class, cu);
259 return -ENOMEM;
262 static int create_new_union(struct cu *cu, const struct btf_type *tp, uint32_t id)
264 struct type *un = type__new(DW_TAG_union_type, cu__btf_str(cu, tp->name_off), tp->size);
265 int member_size = create_members(cu, tp, un);
267 if (member_size < 0)
268 goto out_free;
270 cu__add_tag_with_id(cu, &un->namespace.tag, id);
272 return 0;
273 out_free:
274 type__delete(un, cu);
275 return -ENOMEM;
278 static struct enumerator *enumerator__new(const char *name, uint64_t value)
280 struct enumerator *en = tag__alloc(sizeof(*en));
282 if (en != NULL) {
283 en->name = name;
284 en->value = value;
285 en->tag.tag = DW_TAG_enumerator;
288 return en;
291 static int create_new_enumeration(struct cu *cu, const struct btf_type *tp, uint32_t id)
293 struct btf_enum *ep = btf_enum(tp);
294 uint16_t i, vlen = btf_vlen(tp);
295 struct type *enumeration = type__new(DW_TAG_enumeration_type,
296 cu__btf_str(cu, tp->name_off),
297 tp->size ? tp->size * 8 : (sizeof(int) * 8));
299 if (enumeration == NULL)
300 return -ENOMEM;
302 enumeration->is_signed_enum = !!btf_kflag(tp);
304 for (i = 0; i < vlen; i++) {
305 const char *name = cu__btf_str(cu, ep[i].name_off);
306 uint64_t value = ep[i].val;
308 if (!enumeration->is_signed_enum)
309 value = (uint32_t)ep[i].val;
311 struct enumerator *enumerator = enumerator__new(name, value);
313 if (enumerator == NULL)
314 goto out_free;
316 enumeration__add(enumeration, enumerator);
319 cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
321 return 0;
322 out_free:
323 enumeration__delete(enumeration, cu);
324 return -ENOMEM;
327 #if LIBBPF_MAJOR_VERSION >= 1
328 static struct enumerator *enumerator__new64(const char *name, uint64_t value)
330 struct enumerator *en = tag__alloc(sizeof(*en));
332 if (en != NULL) {
333 en->name = name;
334 en->value = value; // Value is already 64-bit, as this is used with DWARF as well
335 en->tag.tag = DW_TAG_enumerator;
338 return en;
341 static int create_new_enumeration64(struct cu *cu, const struct btf_type *tp, uint32_t id)
343 struct btf_enum64 *ep = btf_enum64(tp);
344 uint16_t i, vlen = btf_vlen(tp);
345 struct type *enumeration = type__new(DW_TAG_enumeration_type,
346 cu__btf_str(cu, tp->name_off),
347 tp->size ? tp->size * 8 : (sizeof(int) * 8));
349 if (enumeration == NULL)
350 return -ENOMEM;
352 enumeration->is_signed_enum = !!btf_kflag(tp);
354 for (i = 0; i < vlen; i++) {
355 const char *name = cu__btf_str(cu, ep[i].name_off);
356 uint64_t value = btf_enum64_value(&ep[i]);
357 struct enumerator *enumerator = enumerator__new64(name, value);
359 if (enumerator == NULL)
360 goto out_free;
362 enumeration__add(enumeration, enumerator);
365 cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
367 return 0;
368 out_free:
369 enumeration__delete(enumeration, cu);
370 return -ENOMEM;
372 #else
373 static int create_new_enumeration64(struct cu *cu __maybe_unused, const struct btf_type *tp __maybe_unused, uint32_t id __maybe_unused)
375 return -ENOTSUP;
377 #endif
379 static int create_new_subroutine_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
381 struct ftype *proto = tag__alloc(sizeof(*proto));
383 if (proto == NULL)
384 return -ENOMEM;
386 return cu__load_ftype(cu, proto, DW_TAG_subroutine_type, tp, id);
389 static int create_new_forward_decl(struct cu *cu, const struct btf_type *tp, uint32_t id)
391 struct class *fwd = class__new(cu__btf_str(cu, tp->name_off), 0, btf_kflag(tp));
393 if (fwd == NULL)
394 return -ENOMEM;
395 fwd->type.declaration = 1;
396 cu__add_tag_with_id(cu, &fwd->type.namespace.tag, id);
397 return 0;
400 static int create_new_typedef(struct cu *cu, const struct btf_type *tp, uint32_t id)
402 struct type *type = type__new(DW_TAG_typedef, cu__btf_str(cu, tp->name_off), 0);
404 if (type == NULL)
405 return -ENOMEM;
407 type->namespace.tag.type = tp->type;
408 cu__add_tag_with_id(cu, &type->namespace.tag, id);
410 return 0;
413 static int create_new_variable(struct cu *cu, const struct btf_type *tp, uint32_t id)
415 struct btf_var *bvar = btf_var(tp);
416 struct variable *var = variable__new(cu__btf_str(cu, tp->name_off), bvar->linkage);
418 if (var == NULL)
419 return -ENOMEM;
421 var->ip.tag.type = tp->type;
422 cu__add_tag_with_id(cu, &var->ip.tag, id);
423 return 0;
426 static int create_new_datasec(struct cu *cu __maybe_unused, const struct btf_type *tp __maybe_unused, uint32_t id __maybe_unused)
428 //cu__add_tag_with_id(cu, &datasec->tag, id);
431 * FIXME: this will not be used to reconstruct some original C code,
432 * its about runtime placement of variables so just ignore this for now
434 return 0;
437 static int create_new_tag(struct cu *cu, int type, const struct btf_type *tp, uint32_t id)
439 struct tag *tag = zalloc(sizeof(*tag));
441 if (tag == NULL)
442 return -ENOMEM;
444 switch (type) {
445 case BTF_KIND_CONST: tag->tag = DW_TAG_const_type; break;
446 case BTF_KIND_PTR: tag->tag = DW_TAG_pointer_type; break;
447 case BTF_KIND_RESTRICT: tag->tag = DW_TAG_restrict_type; break;
448 case BTF_KIND_VOLATILE: tag->tag = DW_TAG_volatile_type; break;
449 case BTF_KIND_TYPE_TAG: tag->tag = DW_TAG_LLVM_annotation; break;
450 default:
451 free(tag);
452 fprintf(stderr, "%s: Unknown type %d\n\n", __func__, type);
453 return 0;
456 tag->type = tp->type;
457 cu__add_tag_with_id(cu, tag, id);
459 return 0;
462 static struct attributes *attributes__realloc(struct attributes *attributes, const char *value)
464 struct attributes *result;
465 uint64_t cnt;
466 size_t sz;
468 cnt = attributes ? attributes->cnt : 0;
469 sz = sizeof(*attributes) + (cnt + 1) * sizeof(*attributes->values);
470 result = realloc(attributes, sz);
471 if (!result)
472 return NULL;
473 if (!attributes)
474 result->cnt = 0;
475 result->values[cnt] = value;
476 result->cnt++;
477 return result;
480 static int process_decl_tag(struct cu *cu, const struct btf_type *tp)
482 struct tag *tag = cu__type(cu, tp->type);
483 struct attributes *tmp;
485 if (tag == NULL)
486 tag = cu__function(cu, tp->type);
488 if (tag == NULL)
489 tag = cu__tag(cu, tp->type);
491 if (tag == NULL) {
492 fprintf(stderr, "WARNING: BTF_KIND_DECL_TAG for unknown BTF id %d\n", tp->type);
493 return 0;
496 const char *attribute = cu__btf_str(cu, tp->name_off);
497 tmp = attributes__realloc(tag->attributes, attribute);
498 if (!tmp)
499 return -ENOMEM;
501 tag->attributes = tmp;
503 return 0;
506 static int btf__load_types(struct btf *btf, struct cu *cu)
508 uint32_t type_index;
509 int err;
511 for (type_index = 1; type_index < btf__type_cnt(btf); type_index++) {
512 const struct btf_type *type_ptr = btf__type_by_id(btf, type_index);
513 uint32_t type = btf_kind(type_ptr);
515 switch (type) {
516 case BTF_KIND_INT:
517 err = create_new_int_type(cu, type_ptr, type_index);
518 break;
519 case BTF_KIND_ARRAY:
520 err = create_new_array(cu, type_ptr, type_index);
521 break;
522 case BTF_KIND_STRUCT:
523 err = create_new_class(cu, type_ptr, type_index);
524 break;
525 case BTF_KIND_UNION:
526 err = create_new_union(cu, type_ptr, type_index);
527 break;
528 case BTF_KIND_ENUM:
529 err = create_new_enumeration(cu, type_ptr, type_index);
530 break;
531 case BTF_KIND_ENUM64:
532 err = create_new_enumeration64(cu, type_ptr, type_index);
533 break;
534 case BTF_KIND_FWD:
535 err = create_new_forward_decl(cu, type_ptr, type_index);
536 break;
537 case BTF_KIND_TYPEDEF:
538 err = create_new_typedef(cu, type_ptr, type_index);
539 break;
540 case BTF_KIND_VAR:
541 err = create_new_variable(cu, type_ptr, type_index);
542 break;
543 case BTF_KIND_DATASEC:
544 err = create_new_datasec(cu, type_ptr, type_index);
545 break;
546 case BTF_KIND_VOLATILE:
547 case BTF_KIND_PTR:
548 case BTF_KIND_CONST:
549 case BTF_KIND_RESTRICT:
550 /* For type tag it's a bit of a lie.
551 * In DWARF it is encoded as a child tag of whatever type it
552 * applies to. Here we load it as a standalone tag with a pointer
553 * to a next type only to have a valid ID in the types table.
555 case BTF_KIND_TYPE_TAG:
556 err = create_new_tag(cu, type, type_ptr, type_index);
557 break;
558 case BTF_KIND_UNKN:
559 cu__table_nullify_type_entry(cu, type_index);
560 fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
561 fflush(stderr);
562 err = 0;
563 break;
564 case BTF_KIND_FUNC_PROTO:
565 err = create_new_subroutine_type(cu, type_ptr, type_index);
566 break;
567 case BTF_KIND_FUNC:
568 // BTF_KIND_FUNC corresponding to a defined subprogram.
569 err = create_new_function(cu, type_ptr, type_index);
570 break;
571 case BTF_KIND_FLOAT:
572 err = create_new_float_type(cu, type_ptr, type_index);
573 break;
574 case BTF_KIND_DECL_TAG:
575 err = process_decl_tag(cu, type_ptr);
576 break;
577 default:
578 fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
579 fflush(stderr);
580 err = 0;
581 break;
584 if (err < 0)
585 return err;
587 return 0;
590 static int btf__load_sections(struct btf *btf, struct cu *cu)
592 return btf__load_types(btf, cu);
595 static uint32_t class__infer_alignment(const struct conf_load *conf,
596 uint32_t byte_offset,
597 uint32_t natural_alignment,
598 uint32_t smallest_offset)
600 uint16_t cacheline_size = conf->conf_fprintf->cacheline_size;
601 uint32_t alignment = 0;
602 uint32_t offset_delta = byte_offset - smallest_offset;
604 if (offset_delta) {
605 if (byte_offset % 2 == 0) {
606 /* Find the power of 2 immediately higher than
607 * offset_delta
609 alignment = 1 << (8 * sizeof(offset_delta) -
610 __builtin_clz(offset_delta));
611 } else {
612 alignment = 0;
616 /* Natural alignment, nothing to do */
617 if (alignment <= natural_alignment || alignment == 1)
618 alignment = 0;
619 /* If the offset is compatible with being aligned on the cacheline size
620 * and this would only result in increasing the alignment, use the
621 * cacheline size as it is safe and quite likely to be what was in the
622 * source.
624 else if (alignment < cacheline_size &&
625 cacheline_size % alignment == 0 &&
626 byte_offset % cacheline_size == 0)
627 alignment = cacheline_size;
629 return alignment;
632 static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *tag, struct cu *cu)
634 struct class_member *pos;
635 struct type *tag_type = tag__type(tag);
636 uint32_t smallest_offset = 0;
638 type__for_each_data_member(tag_type, pos) {
639 struct tag *type = tag__strip_typedefs_and_modifiers(&pos->tag, cu);
641 if (type == NULL) /* FIXME: C++ BTF... */
642 continue;
644 pos->bitfield_offset = 0;
645 pos->byte_size = tag__size(type, cu);
646 pos->bit_size = pos->byte_size * 8;
648 /* if BTF data is incorrect and has size == 0, skip field,
649 * instead of crashing */
650 if (pos->byte_size == 0) {
651 continue;
654 /* bitfield fixup is needed for enums and base types only */
655 if (type->tag == DW_TAG_base_type || type->tag == DW_TAG_enumeration_type) {
656 if (pos->bitfield_size) {
657 /* bitfields seem to be always aligned, no matter the packing */
658 pos->byte_offset = pos->bit_offset / pos->bit_size * pos->bit_size / 8;
659 pos->bitfield_offset = pos->bit_offset - pos->byte_offset * 8;
660 /* re-adjust bitfield offset if it is negative */
661 if (pos->bitfield_offset < 0) {
662 pos->bitfield_offset += pos->bit_size;
663 pos->byte_offset -= pos->byte_size;
664 pos->bit_offset = pos->byte_offset * 8 + pos->bitfield_offset;
666 } else {
667 pos->byte_offset = pos->bit_offset / 8;
671 pos->alignment = class__infer_alignment(conf,
672 pos->byte_offset,
673 tag__natural_alignment(type, cu),
674 smallest_offset);
675 smallest_offset = pos->byte_offset + pos->byte_size;
678 tag_type->alignment = class__infer_alignment(conf,
679 tag_type->size,
680 tag__natural_alignment(tag, cu),
681 smallest_offset);
683 return 0;
686 static int cu__fixup_btf_bitfields(const struct conf_load *conf, struct cu *cu)
688 int err = 0;
689 struct tag *pos;
691 list_for_each_entry(pos, &cu->tags, node)
692 if (tag__is_struct(pos) || tag__is_union(pos)) {
693 err = class__fixup_btf_bitfields(conf, pos, cu);
694 if (err)
695 break;
698 return err;
701 static void btf__cu_delete(struct cu *cu)
703 btf__free(cu->priv);
704 cu->priv = NULL;
707 static int libbpf_log(enum libbpf_print_level level __maybe_unused, const char *format, va_list args)
709 return vfprintf(stderr, format, args);
712 struct debug_fmt_ops btf__ops;
714 static int cus__load_btf(struct cus *cus, struct conf_load *conf, const char *filename)
716 int err = -1;
718 // Pass a zero for addr_size, we'll get it after we load via btf__pointer_size()
719 struct cu *cu = cu__new(filename, 0, NULL, 0, filename, false);
720 if (cu == NULL)
721 return -1;
723 cu->language = LANG_C;
724 cu->uses_global_strings = false;
725 cu->dfops = &btf__ops;
727 libbpf_set_print(libbpf_log);
729 struct btf *btf = btf__parse_split(filename, conf->base_btf);
731 err = libbpf_get_error(btf);
732 if (err)
733 goto out_free;
735 cu->priv = btf;
736 cu->little_endian = btf__endianness(btf) == BTF_LITTLE_ENDIAN;
737 cu->addr_size = btf__pointer_size(btf);
739 err = btf__load_sections(btf, cu);
740 if (err != 0)
741 goto out_free;
743 err = cu__fixup_btf_bitfields(conf, cu);
745 * The app stole this cu, possibly deleting it,
746 * so forget about it
748 if (conf && conf->steal && conf->steal(cu, conf))
749 return 0;
751 cus__add(cus, cu);
752 return err;
754 out_free:
755 cu__delete(cu); // will call btf__free(cu->priv);
756 return err;
759 struct debug_fmt_ops btf__ops = {
760 .name = "btf",
761 .load_file = cus__load_btf,
762 .cu__delete = btf__cu_delete,