1 /* ctfdump.c: CTF dumper.
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
27 static void *tag__alloc(const size_t size
)
29 struct tag
*tag
= zalloc(size
);
37 static int ctf__load_ftype(struct ctf
*ctf
, struct ftype
*proto
, struct cu
*cu
, uint16_t tag
,
38 uint16_t type
, uint16_t vlen
, uint16_t *args
, long id
)
41 proto
->tag
.type
= type
;
42 INIT_LIST_HEAD(&proto
->parms
);
43 INIT_LIST_HEAD(&proto
->template_type_params
);
44 INIT_LIST_HEAD(&proto
->template_value_params
);
45 proto
->template_parameter_pack
= NULL
;
46 proto
->formal_parameter_pack
= NULL
;
49 for (i
= 0; i
< vlen
; i
++) {
50 uint16_t type
= ctf__get16(ctf
, &args
[i
]);
53 proto
->unspec_parms
= 1;
55 struct parameter
*p
= tag__alloc(sizeof(*p
));
58 goto out_free_parameters
;
59 p
->tag
.tag
= DW_TAG_formal_parameter
;
60 p
->tag
.type
= ctf__get16(ctf
, &args
[i
]);
61 ftype__add_parameter(proto
, p
);
65 vlen
*= sizeof(*args
);
67 /* Round up to next multiple of 4 to maintain
76 cu__add_tag(ctf
->priv
, &proto
->tag
, &type_id
);
78 cu__add_tag_with_id(ctf
->priv
, &proto
->tag
, id
);
83 ftype__delete(proto
, cu
);
87 static struct function
*function__new(uint16_t **ptr
, GElf_Sym
*sym
,
88 struct ctf
*ctf
, struct cu
*cu
)
90 struct function
*func
= tag__alloc(sizeof(*func
));
93 func
->lexblock
.ip
.addr
= elf_sym__value(sym
);
94 func
->lexblock
.size
= elf_sym__size(sym
);
95 func
->name
= elf_sym__name(sym
, ctf
->symtab
);
96 func
->vtable_entry
= -1;
97 func
->external
= elf_sym__bind(sym
) == STB_GLOBAL
;
98 INIT_LIST_HEAD(&func
->vtable_node
);
99 INIT_LIST_HEAD(&func
->tool_node
);
100 INIT_LIST_HEAD(&func
->lexblock
.tags
);
102 uint16_t val
= ctf__get16(ctf
, *ptr
);
103 uint16_t tag
= CTF_GET_KIND(val
);
104 uint16_t vlen
= CTF_GET_VLEN(val
);
108 if (tag
!= CTF_TYPE_KIND_FUNC
) {
110 "%s: Expected function type, got %u\n",
114 uint16_t type
= ctf__get16(ctf
, *ptr
);
115 long id
= -1; /* FIXME: not needed for funcs... */
119 if (ctf__load_ftype(ctf
, &func
->proto
, cu
, DW_TAG_subprogram
,
120 type
, vlen
, *ptr
, id
) < 0)
123 * Round up to next multiple of 4 to maintain 32-bit alignment.
136 static int ctf__load_funcs(struct ctf
*ctf
, struct cu
*cu
)
138 struct ctf_header
*hp
= ctf__get_buffer(ctf
);
139 uint16_t *func_ptr
= (ctf__get_buffer(ctf
) + sizeof(*hp
) +
140 ctf__get32(ctf
, &hp
->ctf_func_off
));
144 ctf__for_each_symtab_function(ctf
, idx
, sym
)
145 if (function__new(&func_ptr
, &sym
, ctf
, cu
) == NULL
)
151 static struct base_type
*base_type__new(const char *name
, uint32_t attrs
,
152 uint8_t float_type
, size_t size
)
154 struct base_type
*bt
= tag__alloc(sizeof(*bt
));
159 bt
->is_signed
= attrs
& CTF_TYPE_INT_SIGNED
;
160 bt
->is_bool
= attrs
& CTF_TYPE_INT_BOOL
;
161 bt
->is_varargs
= attrs
& CTF_TYPE_INT_VARARGS
;
162 bt
->name_has_encoding
= false;
163 bt
->float_type
= float_type
;
164 INIT_LIST_HEAD(&bt
->node
);
169 static void type__init(struct type
*type
, uint16_t tag
, const char *name
, size_t size
)
172 INIT_LIST_HEAD(&type
->namespace.tags
);
174 type
->namespace.tag
.tag
= tag
;
175 type
->namespace.name
= name
;
176 type
->template_parameter_pack
= NULL
;
179 static struct type
*type__new(uint16_t tag
, const char *name
, size_t size
)
181 struct type
*type
= tag__alloc(sizeof(*type
));
184 type__init(type
, tag
, name
, size
);
189 static struct class *class__new(const char *name
, size_t size
)
191 struct class *class = tag__alloc(sizeof(*class));
194 type__init(&class->type
, DW_TAG_structure_type
, name
, size
);
195 INIT_LIST_HEAD(&class->vtable
);
201 static int create_new_base_type(struct ctf
*ctf
, void *ptr
,
202 struct ctf_full_type
*tp
, uint32_t id
)
205 uint32_t eval
= ctf__get32(ctf
, enc
);
206 uint32_t attrs
= CTF_TYPE_INT_ATTRS(eval
);
207 uint32_t name
= ctf__get32(ctf
, &tp
->base
.ctf_name
);
208 struct base_type
*base
= base_type__new(ctf__string(ctf
, name
), attrs
, 0,
209 CTF_TYPE_INT_BITS(eval
));
213 base
->tag
.tag
= DW_TAG_base_type
;
214 cu__add_tag_with_id(ctf
->priv
, &base
->tag
, id
);
219 static int create_new_base_type_float(struct ctf
*ctf
, void *ptr
,
220 struct ctf_full_type
*tp
,
223 uint32_t name
= ctf__get32(ctf
, &tp
->base
.ctf_name
);
224 uint32_t *enc
= ptr
, eval
= ctf__get32(ctf
, enc
);
225 struct base_type
*base
= base_type__new(ctf__string(ctf
, name
), 0, eval
,
226 CTF_TYPE_FP_BITS(eval
));
230 base
->tag
.tag
= DW_TAG_base_type
;
231 cu__add_tag_with_id(ctf
->priv
, &base
->tag
, id
);
236 static int create_new_array(struct ctf
*ctf
, void *ptr
, uint32_t id
)
238 struct ctf_array
*ap
= ptr
;
239 struct array_type
*array
= tag__alloc(sizeof(*array
));
244 /* FIXME: where to get the number of dimensions?
245 * it it flattened? */
246 array
->dimensions
= 1;
247 array
->nr_entries
= malloc(sizeof(uint32_t));
249 if (array
->nr_entries
== NULL
) {
254 array
->nr_entries
[0] = ctf__get32(ctf
, &ap
->ctf_array_nelems
);
255 array
->tag
.tag
= DW_TAG_array_type
;
256 array
->tag
.type
= ctf__get16(ctf
, &ap
->ctf_array_type
);
258 cu__add_tag_with_id(ctf
->priv
, &array
->tag
, id
);
263 static int create_new_subroutine_type(struct ctf
*ctf
, void *ptr
,
264 int vlen
, struct ctf_full_type
*tp
,
265 uint32_t id
, struct cu
*cu
)
267 uint16_t *args
= ptr
;
268 unsigned int type
= ctf__get16(ctf
, &tp
->base
.ctf_type
);
269 struct ftype
*proto
= tag__alloc(sizeof(*proto
));
274 vlen
= ctf__load_ftype(ctf
, proto
, cu
, DW_TAG_subroutine_type
,
275 type
, vlen
, args
, id
);
276 return vlen
< 0 ? -ENOMEM
: vlen
;
279 static int create_full_members(struct ctf
*ctf
, void *ptr
,
280 int vlen
, struct type
*class)
282 struct ctf_full_member
*mp
= ptr
;
285 for (i
= 0; i
< vlen
; i
++) {
286 struct class_member
*member
= zalloc(sizeof(*member
));
291 member
->tag
.tag
= DW_TAG_member
;
292 member
->tag
.type
= ctf__get16(ctf
, &mp
[i
].ctf_member_type
);
293 member
->name
= ctf__string(ctf
, ctf__get32(ctf
, &mp
[i
].ctf_member_name
));
294 member
->bit_offset
= (ctf__get32(ctf
, &mp
[i
].ctf_member_offset_high
) << 16) |
295 ctf__get32(ctf
, &mp
[i
].ctf_member_offset_low
);
296 /* sizes and offsets will be corrected at class__fixup_ctf_bitfields */
297 type__add_member(class, member
);
303 static int create_short_members(struct ctf
*ctf
, void *ptr
,
304 int vlen
, struct type
*class)
306 struct ctf_short_member
*mp
= ptr
;
309 for (i
= 0; i
< vlen
; i
++) {
310 struct class_member
*member
= zalloc(sizeof(*member
));
315 member
->tag
.tag
= DW_TAG_member
;
316 member
->tag
.type
= ctf__get16(ctf
, &mp
[i
].ctf_member_type
);
317 member
->name
= ctf__string(ctf
, ctf__get32(ctf
, &mp
[i
].ctf_member_name
));
318 member
->bit_offset
= ctf__get16(ctf
, &mp
[i
].ctf_member_offset
);
319 /* sizes and offsets will be corrected at class__fixup_ctf_bitfields */
321 type__add_member(class, member
);
327 static int create_new_class(struct ctf
*ctf
, void *ptr
,
328 int vlen
, struct ctf_full_type
*tp
,
329 uint64_t size
, uint32_t id
, struct cu
*cu
)
332 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &tp
->base
.ctf_name
));
333 struct class *class = class__new(name
, size
);
335 if (size
>= CTF_SHORT_MEMBER_LIMIT
) {
336 member_size
= create_full_members(ctf
, ptr
, vlen
, &class->type
);
338 member_size
= create_short_members(ctf
, ptr
, vlen
, &class->type
);
344 cu__add_tag_with_id(ctf
->priv
, &class->type
.namespace.tag
, id
);
346 return (vlen
* member_size
);
348 class__delete(class, cu
);
352 static int create_new_union(struct ctf
*ctf
, void *ptr
,
353 int vlen
, struct ctf_full_type
*tp
,
354 uint64_t size
, uint32_t id
, struct cu
*cu
)
357 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &tp
->base
.ctf_name
));
358 struct type
*un
= type__new(DW_TAG_union_type
, name
, size
);
360 if (size
>= CTF_SHORT_MEMBER_LIMIT
) {
361 member_size
= create_full_members(ctf
, ptr
, vlen
, un
);
363 member_size
= create_short_members(ctf
, ptr
, vlen
, un
);
369 cu__add_tag_with_id(ctf
->priv
, &un
->namespace.tag
, id
);
371 return (vlen
* member_size
);
373 type__delete(un
, cu
);
377 static struct enumerator
*enumerator__new(const char *name
, uint32_t value
)
379 struct enumerator
*en
= tag__alloc(sizeof(*en
));
384 en
->tag
.tag
= DW_TAG_enumerator
;
390 static int create_new_enumeration(struct ctf
*ctf
, void *ptr
,
391 int vlen
, struct ctf_full_type
*tp
,
392 uint16_t size
, uint32_t id
, struct cu
*cu
)
394 struct ctf_enum
*ep
= ptr
;
396 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &tp
->base
.ctf_name
));
397 struct type
*enumeration
= type__new(DW_TAG_enumeration_type
, name
, size
?: (sizeof(int) * 8));
399 if (enumeration
== NULL
)
402 for (i
= 0; i
< vlen
; i
++) {
403 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &ep
[i
].ctf_enum_name
));
404 uint32_t value
= ctf__get32(ctf
, &ep
[i
].ctf_enum_val
);
405 struct enumerator
*enumerator
= enumerator__new(name
, value
);
407 if (enumerator
== NULL
)
410 enumeration__add(enumeration
, enumerator
);
413 cu__add_tag_with_id(ctf
->priv
, &enumeration
->namespace.tag
, id
);
415 return (vlen
* sizeof(*ep
));
417 enumeration__delete(enumeration
, cu
);
421 static int create_new_forward_decl(struct ctf
*ctf
, struct ctf_full_type
*tp
,
422 uint64_t size
, uint32_t id
)
424 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &tp
->base
.ctf_name
));
425 struct class *fwd
= class__new(name
, size
);
429 fwd
->type
.declaration
= 1;
430 cu__add_tag_with_id(ctf
->priv
, &fwd
->type
.namespace.tag
, id
);
434 static int create_new_typedef(struct ctf
*ctf
, struct ctf_full_type
*tp
,
435 uint64_t size
, uint32_t id
)
437 const char *name
= ctf__string(ctf
, ctf__get32(ctf
, &tp
->base
.ctf_name
));
438 unsigned int type_id
= ctf__get16(ctf
, &tp
->base
.ctf_type
);
439 struct type
*type
= type__new(DW_TAG_typedef
, name
, size
);
444 type
->namespace.tag
.type
= type_id
;
445 cu__add_tag_with_id(ctf
->priv
, &type
->namespace.tag
, id
);
450 static int create_new_tag(struct ctf
*ctf
, int type
,
451 struct ctf_full_type
*tp
, uint32_t id
)
453 unsigned int type_id
= ctf__get16(ctf
, &tp
->base
.ctf_type
);
454 struct tag
*tag
= zalloc(sizeof(*tag
));
460 case CTF_TYPE_KIND_CONST
: tag
->tag
= DW_TAG_const_type
; break;
461 case CTF_TYPE_KIND_PTR
: tag
->tag
= DW_TAG_pointer_type
; break;
462 case CTF_TYPE_KIND_RESTRICT
: tag
->tag
= DW_TAG_restrict_type
; break;
463 case CTF_TYPE_KIND_VOLATILE
: tag
->tag
= DW_TAG_volatile_type
; break;
466 printf("%s: unknown type %d\n\n", __func__
, type
);
471 cu__add_tag_with_id(ctf
->priv
, tag
, id
);
476 static int ctf__load_types(struct ctf
*ctf
, struct cu
*cu
)
478 void *ctf_buffer
= ctf__get_buffer(ctf
);
479 struct ctf_header
*hp
= ctf_buffer
;
480 void *ctf_contents
= ctf_buffer
+ sizeof(*hp
),
481 *type_section
= (ctf_contents
+ ctf__get32(ctf
, &hp
->ctf_type_off
)),
482 *strings_section
= (ctf_contents
+ ctf__get32(ctf
, &hp
->ctf_str_off
));
483 struct ctf_full_type
*type_ptr
= type_section
,
484 *end
= strings_section
;
485 uint32_t type_index
= 0x0001;
487 if (hp
->ctf_parent_name
|| hp
->ctf_parent_label
)
488 type_index
+= 0x8000;
490 while (type_ptr
< end
) {
491 uint16_t val
= ctf__get16(ctf
, &type_ptr
->base
.ctf_info
);
492 uint16_t type
= CTF_GET_KIND(val
);
493 int vlen
= CTF_GET_VLEN(val
);
494 void *ptr
= type_ptr
;
495 uint16_t base_size
= ctf__get16(ctf
, &type_ptr
->base
.ctf_size
);
496 uint64_t size
= base_size
;
498 if (base_size
== 0xffff) {
499 size
= ctf__get32(ctf
, &type_ptr
->ctf_size_high
);
501 size
|= ctf__get32(ctf
, &type_ptr
->ctf_size_low
);
502 ptr
+= sizeof(struct ctf_full_type
);
504 ptr
+= sizeof(struct ctf_short_type
);
506 if (type
== CTF_TYPE_KIND_INT
) {
507 vlen
= create_new_base_type(ctf
, ptr
, type_ptr
, type_index
);
508 } else if (type
== CTF_TYPE_KIND_FLT
) {
509 vlen
= create_new_base_type_float(ctf
, ptr
, type_ptr
, type_index
);
510 } else if (type
== CTF_TYPE_KIND_ARR
) {
511 vlen
= create_new_array(ctf
, ptr
, type_index
);
512 } else if (type
== CTF_TYPE_KIND_FUNC
) {
513 vlen
= create_new_subroutine_type(ctf
, ptr
, vlen
, type_ptr
, type_index
, cu
);
514 } else if (type
== CTF_TYPE_KIND_STR
) {
515 vlen
= create_new_class(ctf
, ptr
,
516 vlen
, type_ptr
, size
, type_index
, cu
);
517 } else if (type
== CTF_TYPE_KIND_UNION
) {
518 vlen
= create_new_union(ctf
, ptr
,
519 vlen
, type_ptr
, size
, type_index
, cu
);
520 } else if (type
== CTF_TYPE_KIND_ENUM
) {
521 vlen
= create_new_enumeration(ctf
, ptr
, vlen
, type_ptr
,
522 size
, type_index
, cu
);
523 } else if (type
== CTF_TYPE_KIND_FWD
) {
524 vlen
= create_new_forward_decl(ctf
, type_ptr
, size
, type_index
);
525 } else if (type
== CTF_TYPE_KIND_TYPDEF
) {
526 vlen
= create_new_typedef(ctf
, type_ptr
, size
, type_index
);
527 } else if (type
== CTF_TYPE_KIND_VOLATILE
||
528 type
== CTF_TYPE_KIND_PTR
||
529 type
== CTF_TYPE_KIND_CONST
||
530 type
== CTF_TYPE_KIND_RESTRICT
) {
531 vlen
= create_new_tag(ctf
, type
, type_ptr
, type_index
);
532 } else if (type
== CTF_TYPE_KIND_UNKN
) {
533 cu__table_nullify_type_entry(ctf
->priv
, type_index
);
535 "CTF: idx: %d, off: %zd, root: %s Unknown\n",
536 type_index
, ((void *)type_ptr
) - type_section
,
537 CTF_ISROOT(val
) ? "yes" : "no");
545 type_ptr
= ptr
+ vlen
;
551 static struct variable
*variable__new(uint16_t type
, GElf_Sym
*sym
,
554 struct variable
*var
= tag__alloc(sizeof(*var
));
557 var
->scope
= VSCOPE_GLOBAL
;
558 var
->ip
.addr
= elf_sym__value(sym
);
559 var
->name
= ctf
->symtab
->symstrs
->d_buf
+ sym
->st_name
;
560 var
->external
= elf_sym__bind(sym
) == STB_GLOBAL
;
561 var
->ip
.tag
.tag
= DW_TAG_variable
;
562 var
->ip
.tag
.type
= type
;
563 uint32_t id
; /* FIXME: not needed for variables... */
564 cu__add_tag(ctf
->priv
, &var
->ip
.tag
, &id
);
570 static int ctf__load_objects(struct ctf
*ctf
)
572 struct ctf_header
*hp
= ctf__get_buffer(ctf
);
573 uint16_t *objp
= (ctf__get_buffer(ctf
) + sizeof(*hp
) +
574 ctf__get32(ctf
, &hp
->ctf_object_off
));
578 ctf__for_each_symtab_object(ctf
, idx
, sym
) {
579 const uint16_t type
= *objp
;
581 * Discard void objects, probably was an object
582 * we didn't found DWARF info for when encoding.
584 if (type
&& variable__new(type
, &sym
, ctf
) == NULL
)
592 static int ctf__load_sections(struct ctf
*ctf
, struct cu
*cu
)
594 int err
= ctf__load_symtab(ctf
);
598 err
= ctf__load_funcs(ctf
, cu
);
600 err
= ctf__load_types(ctf
, cu
);
602 err
= ctf__load_objects(ctf
);
607 static int class__fixup_ctf_bitfields(struct tag
*tag
, struct cu
*cu
)
609 struct class_member
*pos
;
610 struct type
*tag_type
= tag__type(tag
);
612 type__for_each_data_member(tag_type
, pos
) {
613 struct tag
*type
= tag__strip_typedefs_and_modifiers(&pos
->tag
, cu
);
615 if (type
== NULL
) /* FIXME: C++ CTF... */
618 pos
->bitfield_offset
= 0;
619 pos
->bitfield_size
= 0;
620 pos
->byte_offset
= pos
->bit_offset
/ 8;
622 uint16_t type_bit_size
;
623 size_t integral_bit_size
;
626 case DW_TAG_enumeration_type
:
627 type_bit_size
= tag__type(type
)->size
;
628 /* Best we can do to check if this is a packed enum */
629 if (is_power_of_2(type_bit_size
))
630 integral_bit_size
= roundup(type_bit_size
, 8);
632 integral_bit_size
= sizeof(int) * 8;
634 case DW_TAG_base_type
: {
635 struct base_type
*bt
= tag__base_type(type
);
637 type_bit_size
= bt
->bit_size
;
638 integral_bit_size
= base_type__name_to_size(bt
, cu
);
639 if (integral_bit_size
== 0)
640 fprintf(stderr
, "%s: unknown base type name \"%s\"!\n",
641 __func__
, base_type__name(bt
, name
, sizeof(name
)));
645 pos
->byte_size
= tag__size(type
, cu
);
646 pos
->bit_size
= pos
->byte_size
* 8;
651 * XXX: integral_bit_size can be zero if base_type__name_to_size doesn't
652 * know about the base_type name, so one has to add there when
653 * such base_type isn't found. pahole will put zero on the
654 * struct output so it should be easy to spot the name when
655 * such unlikely thing happens.
657 pos
->byte_size
= integral_bit_size
/ 8;
659 if (integral_bit_size
== 0 || type_bit_size
== integral_bit_size
) {
660 pos
->bit_size
= integral_bit_size
;
664 pos
->bitfield_offset
= pos
->bit_offset
% integral_bit_size
;
665 pos
->bitfield_size
= type_bit_size
;
666 pos
->bit_size
= type_bit_size
;
667 pos
->byte_offset
= (((pos
->bit_offset
/ integral_bit_size
) *
668 integral_bit_size
) / 8);
674 static int cu__fixup_ctf_bitfields(struct cu
*cu
)
679 list_for_each_entry(pos
, &cu
->tags
, node
)
680 if (tag__is_struct(pos
) || tag__is_union(pos
)) {
681 err
= class__fixup_ctf_bitfields(pos
, cu
);
689 static void ctf__cu_delete(struct cu
*cu
)
691 ctf__delete(cu
->priv
);
695 struct debug_fmt_ops ctf__ops
;
697 int ctf__load_file(struct cus
*cus
, struct conf_load
*conf
,
698 const char *filename
)
701 struct ctf
*state
= ctf__new(filename
, NULL
);
706 struct cu
*cu
= cu__new(filename
, state
->wordsize
, NULL
, 0, filename
, false);
710 cu
->language
= LANG_C
;
711 cu
->uses_global_strings
= false;
712 cu
->little_endian
= state
->ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
;
713 cu
->dfops
= &ctf__ops
;
716 if (ctf__load(state
) != 0)
719 err
= ctf__load_sections(state
, cu
);
726 err
= cu__fixup_ctf_bitfields(cu
);
728 * The app stole this cu, possibly deleting it,
731 if (conf
&& conf
->steal
&& conf
->steal(cu
, conf
))
738 struct debug_fmt_ops ctf__ops
= {
740 .load_file
= ctf__load_file
,
741 .cu__delete
= ctf__cu_delete
,