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>
11 #include <sys/types.h>
23 #include <linux/btf.h>
25 #include <bpf/libbpf.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
);
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
);
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
++) {
63 proto
->unspec_parms
= 1;
65 struct parameter
*p
= tag__alloc(sizeof(*p
));
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
);
80 ftype__delete(proto
, cu
);
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
));
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
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
);
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
));
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
);
120 static void type__init(struct type
*type
, uint32_t tag
, const char *name
, size_t size
)
123 INIT_LIST_HEAD(&type
->namespace.tags
);
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
));
135 type__init(type
, tag
, name
, size
);
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
;
146 type__init(&class->type
, tag
, name
, size
);
147 INIT_LIST_HEAD(&class->vtable
);
153 static struct variable
*variable__new(const char *name
, uint32_t linkage
)
155 struct variable
*var
= tag__alloc(sizeof(*var
));
158 var
->external
= linkage
== BTF_VAR_GLOBAL_ALLOCATED
;
160 var
->ip
.tag
.tag
= DW_TAG_variable
;
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
));
175 base
->tag
.tag
= DW_TAG_base_type
;
176 cu__add_tag_with_id(cu
, &base
->tag
, id
);
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);
189 base
->tag
.tag
= DW_TAG_base_type
;
190 cu__add_tag_with_id(cu
, &base
->tag
, id
);
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
));
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
) {
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
);
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
));
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
);
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
);
254 cu__add_tag_with_id(cu
, &class->type
.namespace.tag
, id
);
258 class__delete(class, cu
);
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
);
270 cu__add_tag_with_id(cu
, &un
->namespace.tag
, id
);
274 type__delete(un
, cu
);
278 static struct enumerator
*enumerator__new(const char *name
, uint64_t value
)
280 struct enumerator
*en
= tag__alloc(sizeof(*en
));
285 en
->tag
.tag
= DW_TAG_enumerator
;
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
)
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
)
316 enumeration__add(enumeration
, enumerator
);
319 cu__add_tag_with_id(cu
, &enumeration
->namespace.tag
, id
);
323 enumeration__delete(enumeration
, cu
);
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
));
334 en
->value
= value
; // Value is already 64-bit, as this is used with DWARF as well
335 en
->tag
.tag
= DW_TAG_enumerator
;
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
)
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
)
362 enumeration__add(enumeration
, enumerator
);
365 cu__add_tag_with_id(cu
, &enumeration
->namespace.tag
, id
);
369 enumeration__delete(enumeration
, cu
);
373 static int create_new_enumeration64(struct cu
*cu __maybe_unused
, const struct btf_type
*tp __maybe_unused
, uint32_t id __maybe_unused
)
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
));
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
));
395 fwd
->type
.declaration
= 1;
396 cu__add_tag_with_id(cu
, &fwd
->type
.namespace.tag
, id
);
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);
407 type
->namespace.tag
.type
= tp
->type
;
408 cu__add_tag_with_id(cu
, &type
->namespace.tag
, id
);
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
);
421 var
->ip
.tag
.type
= tp
->type
;
422 cu__add_tag_with_id(cu
, &var
->ip
.tag
, id
);
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
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
));
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;
452 fprintf(stderr
, "%s: Unknown type %d\n\n", __func__
, type
);
456 tag
->type
= tp
->type
;
457 cu__add_tag_with_id(cu
, tag
, id
);
462 static struct attributes
*attributes__realloc(struct attributes
*attributes
, const char *value
)
464 struct attributes
*result
;
468 cnt
= attributes
? attributes
->cnt
: 0;
469 sz
= sizeof(*attributes
) + (cnt
+ 1) * sizeof(*attributes
->values
);
470 result
= realloc(attributes
, sz
);
475 result
->values
[cnt
] = value
;
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
;
486 tag
= cu__function(cu
, tp
->type
);
489 tag
= cu__tag(cu
, tp
->type
);
492 fprintf(stderr
, "WARNING: BTF_KIND_DECL_TAG for unknown BTF id %d\n", tp
->type
);
496 const char *attribute
= cu__btf_str(cu
, tp
->name_off
);
497 tmp
= attributes__realloc(tag
->attributes
, attribute
);
501 tag
->attributes
= tmp
;
506 static int btf__load_types(struct btf
*btf
, struct cu
*cu
)
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
);
517 err
= create_new_int_type(cu
, type_ptr
, type_index
);
520 err
= create_new_array(cu
, type_ptr
, type_index
);
522 case BTF_KIND_STRUCT
:
523 err
= create_new_class(cu
, type_ptr
, type_index
);
526 err
= create_new_union(cu
, type_ptr
, type_index
);
529 err
= create_new_enumeration(cu
, type_ptr
, type_index
);
531 case BTF_KIND_ENUM64
:
532 err
= create_new_enumeration64(cu
, type_ptr
, type_index
);
535 err
= create_new_forward_decl(cu
, type_ptr
, type_index
);
537 case BTF_KIND_TYPEDEF
:
538 err
= create_new_typedef(cu
, type_ptr
, type_index
);
541 err
= create_new_variable(cu
, type_ptr
, type_index
);
543 case BTF_KIND_DATASEC
:
544 err
= create_new_datasec(cu
, type_ptr
, type_index
);
546 case BTF_KIND_VOLATILE
:
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
);
559 cu__table_nullify_type_entry(cu
, type_index
);
560 fprintf(stderr
, "BTF: idx: %d, Unknown kind %d\n", type_index
, type
);
564 case BTF_KIND_FUNC_PROTO
:
565 err
= create_new_subroutine_type(cu
, type_ptr
, type_index
);
568 // BTF_KIND_FUNC corresponding to a defined subprogram.
569 err
= create_new_function(cu
, type_ptr
, type_index
);
572 err
= create_new_float_type(cu
, type_ptr
, type_index
);
574 case BTF_KIND_DECL_TAG
:
575 err
= process_decl_tag(cu
, type_ptr
);
578 fprintf(stderr
, "BTF: idx: %d, Unknown kind %d\n", type_index
, type
);
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
;
605 if (byte_offset
% 2 == 0) {
606 /* Find the power of 2 immediately higher than
609 alignment
= 1 << (8 * sizeof(offset_delta
) -
610 __builtin_clz(offset_delta
));
616 /* Natural alignment, nothing to do */
617 if (alignment
<= natural_alignment
|| alignment
== 1)
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
624 else if (alignment
< cacheline_size
&&
625 cacheline_size
% alignment
== 0 &&
626 byte_offset
% cacheline_size
== 0)
627 alignment
= cacheline_size
;
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... */
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) {
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
;
667 pos
->byte_offset
= pos
->bit_offset
/ 8;
671 pos
->alignment
= class__infer_alignment(conf
,
673 tag__natural_alignment(type
, cu
),
675 smallest_offset
= pos
->byte_offset
+ pos
->byte_size
;
678 tag_type
->alignment
= class__infer_alignment(conf
,
680 tag__natural_alignment(tag
, cu
),
686 static int cu__fixup_btf_bitfields(const struct conf_load
*conf
, struct cu
*cu
)
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
);
701 static void btf__cu_delete(struct cu
*cu
)
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
)
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);
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
);
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
);
743 err
= cu__fixup_btf_bitfields(conf
, cu
);
745 * The app stole this cu, possibly deleting it,
748 if (conf
&& conf
->steal
&& conf
->steal(cu
, conf
))
755 cu__delete(cu
); // will call btf__free(cu->priv);
759 struct debug_fmt_ops btf__ops
= {
761 .load_file
= cus__load_btf
,
762 .cu__delete
= btf__cu_delete
,