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
);
57 for (i
= 0; i
< vlen
; ++i
, param
++) {
59 proto
->unspec_parms
= 1;
61 struct parameter
*p
= tag__alloc(sizeof(*p
));
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
);
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
));
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
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
);
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
));
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
);
116 static void type__init(struct type
*type
, uint32_t tag
, const char *name
, size_t size
)
119 INIT_LIST_HEAD(&type
->namespace.tags
);
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
));
130 type__init(type
, tag
, name
, size
);
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
;
141 type__init(&class->type
, tag
, name
, size
);
142 INIT_LIST_HEAD(&class->vtable
);
148 static struct variable
*variable__new(const char *name
, uint32_t linkage
)
150 struct variable
*var
= tag__alloc(sizeof(*var
));
153 var
->external
= linkage
== BTF_VAR_GLOBAL_ALLOCATED
;
155 var
->ip
.tag
.tag
= DW_TAG_variable
;
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
));
170 base
->tag
.tag
= DW_TAG_base_type
;
171 cu__add_tag_with_id(cu
, &base
->tag
, id
);
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);
184 base
->tag
.tag
= DW_TAG_base_type
;
185 cu__add_tag_with_id(cu
, &base
->tag
, id
);
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
));
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
) {
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
);
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
));
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
);
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
);
249 cu__add_tag_with_id(cu
, &class->type
.namespace.tag
, id
);
253 class__delete(class);
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
);
265 cu__add_tag_with_id(cu
, &un
->namespace.tag
, id
);
273 static struct enumerator
*enumerator__new(const char *name
, uint64_t value
)
275 struct enumerator
*en
= tag__alloc(sizeof(*en
));
280 en
->tag
.tag
= DW_TAG_enumerator
;
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
)
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
)
311 enumeration__add(enumeration
, enumerator
);
314 cu__add_tag_with_id(cu
, &enumeration
->namespace.tag
, id
);
318 enumeration__delete(enumeration
);
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
));
329 en
->value
= value
; // Value is already 64-bit, as this is used with DWARF as well
330 en
->tag
.tag
= DW_TAG_enumerator
;
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
)
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
)
357 enumeration__add(enumeration
, enumerator
);
360 cu__add_tag_with_id(cu
, &enumeration
->namespace.tag
, id
);
364 enumeration__delete(enumeration
);
368 static int create_new_enumeration64(struct cu
*cu __maybe_unused
, const struct btf_type
*tp __maybe_unused
, uint32_t id __maybe_unused
)
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
));
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
));
390 fwd
->type
.declaration
= 1;
391 cu__add_tag_with_id(cu
, &fwd
->type
.namespace.tag
, id
);
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);
402 type
->namespace.tag
.type
= tp
->type
;
403 cu__add_tag_with_id(cu
, &type
->namespace.tag
, id
);
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
);
416 var
->ip
.tag
.type
= tp
->type
;
417 cu__add_tag_with_id(cu
, &var
->ip
.tag
, id
);
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
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
));
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;
447 printf("%s: Unknown type %d\n\n", __func__
, type
);
451 tag
->type
= tp
->type
;
452 cu__add_tag_with_id(cu
, tag
, id
);
457 static int btf__load_types(struct btf
*btf
, struct cu
*cu
)
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
);
468 err
= create_new_int_type(cu
, type_ptr
, type_index
);
471 err
= create_new_array(cu
, type_ptr
, type_index
);
473 case BTF_KIND_STRUCT
:
474 err
= create_new_class(cu
, type_ptr
, type_index
);
477 err
= create_new_union(cu
, type_ptr
, type_index
);
480 err
= create_new_enumeration(cu
, type_ptr
, type_index
);
482 case BTF_KIND_ENUM64
:
483 err
= create_new_enumeration64(cu
, type_ptr
, type_index
);
486 err
= create_new_forward_decl(cu
, type_ptr
, type_index
);
488 case BTF_KIND_TYPEDEF
:
489 err
= create_new_typedef(cu
, type_ptr
, type_index
);
492 err
= create_new_variable(cu
, type_ptr
, type_index
);
494 case BTF_KIND_DATASEC
:
495 err
= create_new_datasec(cu
, type_ptr
, type_index
);
497 case BTF_KIND_VOLATILE
:
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
);
510 cu__table_nullify_type_entry(cu
, type_index
);
511 fprintf(stderr
, "BTF: idx: %d, Unknown kind %d\n", type_index
, type
);
515 case BTF_KIND_FUNC_PROTO
:
516 err
= create_new_subroutine_type(cu
, type_ptr
, type_index
);
519 // BTF_KIND_FUNC corresponding to a defined subprogram.
520 err
= create_new_function(cu
, type_ptr
, type_index
);
523 err
= create_new_float_type(cu
, type_ptr
, type_index
);
526 fprintf(stderr
, "BTF: idx: %d, Unknown kind %d\n", type_index
, type
);
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
;
553 if (byte_offset
% 2 == 0) {
554 /* Find the power of 2 immediately higher than
557 alignment
= 1 << (8 * sizeof(offset_delta
) -
558 __builtin_clz(offset_delta
));
564 /* Natural alignment, nothing to do */
565 if (alignment
<= natural_alignment
|| alignment
== 1)
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
572 else if (alignment
< cacheline_size
&&
573 cacheline_size
% alignment
== 0 &&
574 byte_offset
% cacheline_size
== 0)
575 alignment
= cacheline_size
;
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... */
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) {
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
;
615 pos
->byte_offset
= pos
->bit_offset
/ 8;
619 pos
->alignment
= class__infer_alignment(conf
,
621 tag__natural_alignment(type
, cu
),
623 smallest_offset
= pos
->byte_offset
+ pos
->byte_size
;
626 tag_type
->alignment
= class__infer_alignment(conf
,
628 tag__natural_alignment(tag
, cu
),
634 static int cu__fixup_btf_bitfields(const struct conf_load
*conf
, struct cu
*cu
)
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
);
649 static void btf__cu_delete(struct cu
*cu
)
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
)
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);
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
);
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
);
691 err
= cu__fixup_btf_bitfields(conf
, cu
);
693 * The app stole this cu, possibly deleting it,
696 if (conf
&& conf
->steal
&& conf
->steal(cu
, conf
, NULL
))
703 cu__delete(cu
); // will call btf__free(cu->priv);
707 struct debug_fmt_ops btf__ops
= {
709 .load_file
= cus__load_btf
,
710 .cu__delete
= btf__cu_delete
,