pahole: Describe expected use of 'default' in the man page
[dwarves.git] / btf_loader.c
blob57d6d007257ea2e3d7cd82b72a98b5d3ed91f2a8
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);
57 for (i = 0; i < vlen; ++i, param++) {
58 if (param->type == 0)
59 proto->unspec_parms = 1;
60 else {
61 struct parameter *p = tag__alloc(sizeof(*p));
63 if (p == NULL)
64 goto out_free_parameters;
65 p->tag.tag = DW_TAG_formal_parameter;
66 p->tag.type = param->type;
67 p->name = cu__btf_str(cu, param->name_off);
68 ftype__add_parameter(proto, p);
72 cu__add_tag_with_id(cu, &proto->tag, id);
74 return 0;
75 out_free_parameters:
76 ftype__delete(proto);
77 return -ENOMEM;
80 static int create_new_function(struct cu *cu, const struct btf_type *tp, uint32_t id)
82 struct function *func = tag__alloc(sizeof(*func));
84 if (func == NULL)
85 return -ENOMEM;
87 // for BTF this is not really the type of the return of the function,
88 // but the prototype, the return type is the one in type_id
89 func->btf = 1;
90 func->proto.tag.tag = DW_TAG_subprogram;
91 func->proto.tag.type = tp->type;
92 func->name = cu__btf_str(cu, tp->name_off);
93 INIT_LIST_HEAD(&func->lexblock.tags);
94 cu__add_tag_with_id(cu, &func->proto.tag, id);
96 return 0;
99 static struct base_type *base_type__new(const char *name, uint32_t attrs,
100 uint8_t float_type, size_t size)
102 struct base_type *bt = tag__alloc(sizeof(*bt));
104 if (bt != NULL) {
105 bt->name = name;
106 bt->bit_size = size;
107 bt->is_signed = attrs & BTF_INT_SIGNED;
108 bt->is_bool = attrs & BTF_INT_BOOL;
109 bt->name_has_encoding = false;
110 bt->float_type = float_type;
111 INIT_LIST_HEAD(&bt->node);
113 return bt;
116 static void type__init(struct type *type, uint32_t tag, const char *name, size_t size)
118 __type__init(type);
119 INIT_LIST_HEAD(&type->namespace.tags);
120 type->size = size;
121 type->namespace.tag.tag = tag;
122 type->namespace.name = name;
125 static struct type *type__new(uint16_t tag, const char *name, size_t size)
127 struct type *type = tag__alloc(sizeof(*type));
129 if (type != NULL)
130 type__init(type, tag, name, size);
132 return type;
135 static struct class *class__new(const char *name, size_t size, bool is_union)
137 struct class *class = tag__alloc(sizeof(*class));
138 uint32_t tag = is_union ? DW_TAG_union_type : DW_TAG_structure_type;
140 if (class != NULL) {
141 type__init(&class->type, tag, name, size);
142 INIT_LIST_HEAD(&class->vtable);
145 return class;
148 static struct variable *variable__new(const char *name, uint32_t linkage)
150 struct variable *var = tag__alloc(sizeof(*var));
152 if (var != NULL) {
153 var->external = linkage == BTF_VAR_GLOBAL_ALLOCATED;
154 var->name = name;
155 var->ip.tag.tag = DW_TAG_variable;
158 return var;
161 static int create_new_int_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
163 uint32_t attrs = btf_int_encoding(tp);
164 const char *name = cu__btf_str(cu, tp->name_off);
165 struct base_type *base = base_type__new(name, attrs, 0, btf_int_bits(tp));
167 if (base == NULL)
168 return -ENOMEM;
170 base->tag.tag = DW_TAG_base_type;
171 cu__add_tag_with_id(cu, &base->tag, id);
173 return 0;
176 static int create_new_float_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
178 const char *name = cu__btf_str(cu, tp->name_off);
179 struct base_type *base = base_type__new(name, 0, BT_FP_SINGLE, tp->size * 8);
181 if (base == NULL)
182 return -ENOMEM;
184 base->tag.tag = DW_TAG_base_type;
185 cu__add_tag_with_id(cu, &base->tag, id);
187 return 0;
190 static int create_new_array(struct cu *cu, const struct btf_type *tp, uint32_t id)
192 struct btf_array *ap = btf_array(tp);
193 struct array_type *array = tag__alloc(sizeof(*array));
195 if (array == NULL)
196 return -ENOMEM;
198 /* FIXME: where to get the number of dimensions?
199 * it it flattened? */
200 array->dimensions = 1;
201 array->nr_entries = malloc(sizeof(uint32_t));
203 if (array->nr_entries == NULL) {
204 free(array);
205 return -ENOMEM;
208 array->nr_entries[0] = ap->nelems;
209 array->tag.tag = DW_TAG_array_type;
210 array->tag.type = ap->type;
212 cu__add_tag_with_id(cu, &array->tag, id);
214 return 0;
217 static int create_members(struct cu *cu, const struct btf_type *tp, struct type *class)
219 struct btf_member *mp = btf_members(tp);
220 int i, vlen = btf_vlen(tp);
222 for (i = 0; i < vlen; i++) {
223 struct class_member *member = zalloc(sizeof(*member));
225 if (member == NULL)
226 return -ENOMEM;
228 member->tag.tag = DW_TAG_member;
229 member->tag.type = mp[i].type;
230 member->name = cu__btf_str(cu, mp[i].name_off);
231 member->bit_offset = btf_member_bit_offset(tp, i);
232 member->bitfield_size = btf_member_bitfield_size(tp, i);
233 member->byte_offset = member->bit_offset / 8;
234 /* sizes and offsets will be corrected at class__fixup_btf_bitfields */
235 type__add_member(class, member);
238 return 0;
241 static int create_new_class(struct cu *cu, const struct btf_type *tp, uint32_t id)
243 struct class *class = class__new(cu__btf_str(cu, tp->name_off), tp->size, false);
244 int member_size = create_members(cu, tp, &class->type);
246 if (member_size < 0)
247 goto out_free;
249 cu__add_tag_with_id(cu, &class->type.namespace.tag, id);
251 return 0;
252 out_free:
253 class__delete(class);
254 return -ENOMEM;
257 static int create_new_union(struct cu *cu, const struct btf_type *tp, uint32_t id)
259 struct type *un = type__new(DW_TAG_union_type, cu__btf_str(cu, tp->name_off), tp->size);
260 int member_size = create_members(cu, tp, un);
262 if (member_size < 0)
263 goto out_free;
265 cu__add_tag_with_id(cu, &un->namespace.tag, id);
267 return 0;
268 out_free:
269 type__delete(un);
270 return -ENOMEM;
273 static struct enumerator *enumerator__new(const char *name, uint64_t value)
275 struct enumerator *en = tag__alloc(sizeof(*en));
277 if (en != NULL) {
278 en->name = name;
279 en->value = value;
280 en->tag.tag = DW_TAG_enumerator;
283 return en;
286 static int create_new_enumeration(struct cu *cu, const struct btf_type *tp, uint32_t id)
288 struct btf_enum *ep = btf_enum(tp);
289 uint16_t i, vlen = btf_vlen(tp);
290 struct type *enumeration = type__new(DW_TAG_enumeration_type,
291 cu__btf_str(cu, tp->name_off),
292 tp->size ? tp->size * 8 : (sizeof(int) * 8));
294 if (enumeration == NULL)
295 return -ENOMEM;
297 enumeration->is_signed_enum = !!btf_kflag(tp);
299 for (i = 0; i < vlen; i++) {
300 const char *name = cu__btf_str(cu, ep[i].name_off);
301 uint64_t value = ep[i].val;
303 if (!enumeration->is_signed_enum)
304 value = (uint32_t)ep[i].val;
306 struct enumerator *enumerator = enumerator__new(name, value);
308 if (enumerator == NULL)
309 goto out_free;
311 enumeration__add(enumeration, enumerator);
314 cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
316 return 0;
317 out_free:
318 enumeration__delete(enumeration);
319 return -ENOMEM;
322 #if LIBBPF_MAJOR_VERSION >= 1
323 static struct enumerator *enumerator__new64(const char *name, uint64_t value)
325 struct enumerator *en = tag__alloc(sizeof(*en));
327 if (en != NULL) {
328 en->name = name;
329 en->value = value; // Value is already 64-bit, as this is used with DWARF as well
330 en->tag.tag = DW_TAG_enumerator;
333 return en;
336 static int create_new_enumeration64(struct cu *cu, const struct btf_type *tp, uint32_t id)
338 struct btf_enum64 *ep = btf_enum64(tp);
339 uint16_t i, vlen = btf_vlen(tp);
340 struct type *enumeration = type__new(DW_TAG_enumeration_type,
341 cu__btf_str(cu, tp->name_off),
342 tp->size ? tp->size * 8 : (sizeof(int) * 8));
344 if (enumeration == NULL)
345 return -ENOMEM;
347 enumeration->is_signed_enum = !!btf_kflag(tp);
349 for (i = 0; i < vlen; i++) {
350 const char *name = cu__btf_str(cu, ep[i].name_off);
351 uint64_t value = btf_enum64_value(&ep[i]);
352 struct enumerator *enumerator = enumerator__new64(name, value);
354 if (enumerator == NULL)
355 goto out_free;
357 enumeration__add(enumeration, enumerator);
360 cu__add_tag_with_id(cu, &enumeration->namespace.tag, id);
362 return 0;
363 out_free:
364 enumeration__delete(enumeration);
365 return -ENOMEM;
367 #else
368 static int create_new_enumeration64(struct cu *cu __maybe_unused, const struct btf_type *tp __maybe_unused, uint32_t id __maybe_unused)
370 return -ENOTSUP;
372 #endif
374 static int create_new_subroutine_type(struct cu *cu, const struct btf_type *tp, uint32_t id)
376 struct ftype *proto = tag__alloc(sizeof(*proto));
378 if (proto == NULL)
379 return -ENOMEM;
381 return cu__load_ftype(cu, proto, DW_TAG_subroutine_type, tp, id);
384 static int create_new_forward_decl(struct cu *cu, const struct btf_type *tp, uint32_t id)
386 struct class *fwd = class__new(cu__btf_str(cu, tp->name_off), 0, btf_kflag(tp));
388 if (fwd == NULL)
389 return -ENOMEM;
390 fwd->type.declaration = 1;
391 cu__add_tag_with_id(cu, &fwd->type.namespace.tag, id);
392 return 0;
395 static int create_new_typedef(struct cu *cu, const struct btf_type *tp, uint32_t id)
397 struct type *type = type__new(DW_TAG_typedef, cu__btf_str(cu, tp->name_off), 0);
399 if (type == NULL)
400 return -ENOMEM;
402 type->namespace.tag.type = tp->type;
403 cu__add_tag_with_id(cu, &type->namespace.tag, id);
405 return 0;
408 static int create_new_variable(struct cu *cu, const struct btf_type *tp, uint32_t id)
410 struct btf_var *bvar = btf_var(tp);
411 struct variable *var = variable__new(cu__btf_str(cu, tp->name_off), bvar->linkage);
413 if (var == NULL)
414 return -ENOMEM;
416 var->ip.tag.type = tp->type;
417 cu__add_tag_with_id(cu, &var->ip.tag, id);
418 return 0;
421 static int create_new_datasec(struct cu *cu __maybe_unused, const struct btf_type *tp __maybe_unused, uint32_t id __maybe_unused)
423 //cu__add_tag_with_id(cu, &datasec->tag, id);
426 * FIXME: this will not be used to reconstruct some original C code,
427 * its about runtime placement of variables so just ignore this for now
429 return 0;
432 static int create_new_tag(struct cu *cu, int type, const struct btf_type *tp, uint32_t id)
434 struct tag *tag = zalloc(sizeof(*tag));
436 if (tag == NULL)
437 return -ENOMEM;
439 switch (type) {
440 case BTF_KIND_CONST: tag->tag = DW_TAG_const_type; break;
441 case BTF_KIND_PTR: tag->tag = DW_TAG_pointer_type; break;
442 case BTF_KIND_RESTRICT: tag->tag = DW_TAG_restrict_type; break;
443 case BTF_KIND_VOLATILE: tag->tag = DW_TAG_volatile_type; break;
444 case BTF_KIND_TYPE_TAG: tag->tag = DW_TAG_LLVM_annotation; break;
445 default:
446 free(tag);
447 printf("%s: Unknown type %d\n\n", __func__, type);
448 return 0;
451 tag->type = tp->type;
452 cu__add_tag_with_id(cu, tag, id);
454 return 0;
457 static int btf__load_types(struct btf *btf, struct cu *cu)
459 uint32_t type_index;
460 int err;
462 for (type_index = 1; type_index < btf__type_cnt(btf); type_index++) {
463 const struct btf_type *type_ptr = btf__type_by_id(btf, type_index);
464 uint32_t type = btf_kind(type_ptr);
466 switch (type) {
467 case BTF_KIND_INT:
468 err = create_new_int_type(cu, type_ptr, type_index);
469 break;
470 case BTF_KIND_ARRAY:
471 err = create_new_array(cu, type_ptr, type_index);
472 break;
473 case BTF_KIND_STRUCT:
474 err = create_new_class(cu, type_ptr, type_index);
475 break;
476 case BTF_KIND_UNION:
477 err = create_new_union(cu, type_ptr, type_index);
478 break;
479 case BTF_KIND_ENUM:
480 err = create_new_enumeration(cu, type_ptr, type_index);
481 break;
482 case BTF_KIND_ENUM64:
483 err = create_new_enumeration64(cu, type_ptr, type_index);
484 break;
485 case BTF_KIND_FWD:
486 err = create_new_forward_decl(cu, type_ptr, type_index);
487 break;
488 case BTF_KIND_TYPEDEF:
489 err = create_new_typedef(cu, type_ptr, type_index);
490 break;
491 case BTF_KIND_VAR:
492 err = create_new_variable(cu, type_ptr, type_index);
493 break;
494 case BTF_KIND_DATASEC:
495 err = create_new_datasec(cu, type_ptr, type_index);
496 break;
497 case BTF_KIND_VOLATILE:
498 case BTF_KIND_PTR:
499 case BTF_KIND_CONST:
500 case BTF_KIND_RESTRICT:
501 /* For type tag it's a bit of a lie.
502 * In DWARF it is encoded as a child tag of whatever type it
503 * applies to. Here we load it as a standalone tag with a pointer
504 * to a next type only to have a valid ID in the types table.
506 case BTF_KIND_TYPE_TAG:
507 err = create_new_tag(cu, type, type_ptr, type_index);
508 break;
509 case BTF_KIND_UNKN:
510 cu__table_nullify_type_entry(cu, type_index);
511 fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
512 fflush(stderr);
513 err = 0;
514 break;
515 case BTF_KIND_FUNC_PROTO:
516 err = create_new_subroutine_type(cu, type_ptr, type_index);
517 break;
518 case BTF_KIND_FUNC:
519 // BTF_KIND_FUNC corresponding to a defined subprogram.
520 err = create_new_function(cu, type_ptr, type_index);
521 break;
522 case BTF_KIND_FLOAT:
523 err = create_new_float_type(cu, type_ptr, type_index);
524 break;
525 default:
526 fprintf(stderr, "BTF: idx: %d, Unknown kind %d\n", type_index, type);
527 fflush(stderr);
528 err = 0;
529 break;
532 if (err < 0)
533 return err;
535 return 0;
538 static int btf__load_sections(struct btf *btf, struct cu *cu)
540 return btf__load_types(btf, cu);
543 static uint32_t class__infer_alignment(const struct conf_load *conf,
544 uint32_t byte_offset,
545 uint32_t natural_alignment,
546 uint32_t smallest_offset)
548 uint16_t cacheline_size = conf->conf_fprintf->cacheline_size;
549 uint32_t alignment = 0;
550 uint32_t offset_delta = byte_offset - smallest_offset;
552 if (offset_delta) {
553 if (byte_offset % 2 == 0) {
554 /* Find the power of 2 immediately higher than
555 * offset_delta
557 alignment = 1 << (8 * sizeof(offset_delta) -
558 __builtin_clz(offset_delta));
559 } else {
560 alignment = 0;
564 /* Natural alignment, nothing to do */
565 if (alignment <= natural_alignment || alignment == 1)
566 alignment = 0;
567 /* If the offset is compatible with being aligned on the cacheline size
568 * and this would only result in increasing the alignment, use the
569 * cacheline size as it is safe and quite likely to be what was in the
570 * source.
572 else if (alignment < cacheline_size &&
573 cacheline_size % alignment == 0 &&
574 byte_offset % cacheline_size == 0)
575 alignment = cacheline_size;
577 return alignment;
580 static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *tag, struct cu *cu)
582 struct class_member *pos;
583 struct type *tag_type = tag__type(tag);
584 uint32_t smallest_offset = 0;
586 type__for_each_data_member(tag_type, pos) {
587 struct tag *type = tag__strip_typedefs_and_modifiers(&pos->tag, cu);
589 if (type == NULL) /* FIXME: C++ BTF... */
590 continue;
592 pos->bitfield_offset = 0;
593 pos->byte_size = tag__size(type, cu);
594 pos->bit_size = pos->byte_size * 8;
596 /* if BTF data is incorrect and has size == 0, skip field,
597 * instead of crashing */
598 if (pos->byte_size == 0) {
599 continue;
602 /* bitfield fixup is needed for enums and base types only */
603 if (type->tag == DW_TAG_base_type || type->tag == DW_TAG_enumeration_type) {
604 if (pos->bitfield_size) {
605 /* bitfields seem to be always aligned, no matter the packing */
606 pos->byte_offset = pos->bit_offset / pos->bit_size * pos->bit_size / 8;
607 pos->bitfield_offset = pos->bit_offset - pos->byte_offset * 8;
608 /* re-adjust bitfield offset if it is negative */
609 if (pos->bitfield_offset < 0) {
610 pos->bitfield_offset += pos->bit_size;
611 pos->byte_offset -= pos->byte_size;
612 pos->bit_offset = pos->byte_offset * 8 + pos->bitfield_offset;
614 } else {
615 pos->byte_offset = pos->bit_offset / 8;
619 pos->alignment = class__infer_alignment(conf,
620 pos->byte_offset,
621 tag__natural_alignment(type, cu),
622 smallest_offset);
623 smallest_offset = pos->byte_offset + pos->byte_size;
626 tag_type->alignment = class__infer_alignment(conf,
627 tag_type->size,
628 tag__natural_alignment(tag, cu),
629 smallest_offset);
631 return 0;
634 static int cu__fixup_btf_bitfields(const struct conf_load *conf, struct cu *cu)
636 int err = 0;
637 struct tag *pos;
639 list_for_each_entry(pos, &cu->tags, node)
640 if (tag__is_struct(pos) || tag__is_union(pos)) {
641 err = class__fixup_btf_bitfields(conf, pos, cu);
642 if (err)
643 break;
646 return err;
649 static void btf__cu_delete(struct cu *cu)
651 btf__free(cu->priv);
652 cu->priv = NULL;
655 static int libbpf_log(enum libbpf_print_level level __maybe_unused, const char *format, va_list args)
657 return vfprintf(stderr, format, args);
660 struct debug_fmt_ops btf__ops;
662 static int cus__load_btf(struct cus *cus, struct conf_load *conf, const char *filename)
664 int err = -1;
666 // Pass a zero for addr_size, we'll get it after we load via btf__pointer_size()
667 struct cu *cu = cu__new(filename, 0, NULL, 0, filename, false);
668 if (cu == NULL)
669 return -1;
671 cu->language = LANG_C;
672 cu->uses_global_strings = false;
673 cu->dfops = &btf__ops;
675 libbpf_set_print(libbpf_log);
677 struct btf *btf = btf__parse_split(filename, conf->base_btf);
679 err = libbpf_get_error(btf);
680 if (err)
681 goto out_free;
683 cu->priv = btf;
684 cu->little_endian = btf__endianness(btf) == BTF_LITTLE_ENDIAN;
685 cu->addr_size = btf__pointer_size(btf);
687 err = btf__load_sections(btf, cu);
688 if (err != 0)
689 goto out_free;
691 err = cu__fixup_btf_bitfields(conf, cu);
693 * The app stole this cu, possibly deleting it,
694 * so forget about it
696 if (conf && conf->steal && conf->steal(cu, conf, NULL))
697 return 0;
699 cus__add(cus, cu);
700 return err;
702 out_free:
703 cu__delete(cu); // will call btf__free(cu->priv);
704 return err;
707 struct debug_fmt_ops btf__ops = {
708 .name = "btf",
709 .load_file = cus__load_btf,
710 .cu__delete = btf__cu_delete,