2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>
10 #include <elfutils/libdwfl.h>
11 #include <elfutils/version.h>
30 #ifndef DW_AT_alignment
31 #define DW_AT_alignment 0x88
34 #ifndef DW_AT_GNU_vector
35 #define DW_AT_GNU_vector 0x2107
38 #ifndef DW_TAG_GNU_call_site
39 #define DW_TAG_GNU_call_site 0x4109
40 #define DW_TAG_GNU_call_site_parameter 0x410a
43 #ifndef DW_TAG_call_site
44 #define DW_TAG_call_site 0x48
45 #define DW_TAG_call_site_parameter 0x49
48 #ifndef DW_FORM_implicit_const
49 #define DW_FORM_implicit_const 0x21
53 #define DW_OP_addrx 0xa1
60 static pthread_mutex_t libdw__lock
= PTHREAD_MUTEX_INITIALIZER
;
62 static uint32_t hashtags__bits
= 12;
63 static uint32_t max_hashtags__bits
= 21;
65 static uint32_t hashtags__fn(Dwarf_Off key
)
67 return hash_64(key
, hashtags__bits
);
70 bool no_bitfield_type_recode
= true;
72 static void __tag__print_not_supported(uint32_t tag
, const char *func
, unsigned long long offset
)
74 static bool dwarf_tags_warned
[DW_TAG_GNU_call_site_parameter
+ 64];
76 if (tag
< sizeof(dwarf_tags_warned
)) {
77 if (dwarf_tags_warned
[tag
])
79 dwarf_tags_warned
[tag
] = true;
82 fprintf(stderr
, "%s: tag not supported %#x (%s) at <%llx>!\n", func
,
83 tag
, dwarf_tag_name(tag
), offset
);
86 #define tag__print_not_supported(die) \
87 __tag__print_not_supported(dwarf_tag(die), __func__, dwarf_dieoffset(die))
89 static void __cu__tag_not_handled(const struct cu
*cu
, Dwarf_Die
*die
, const char *fn
)
91 uint32_t tag
= dwarf_tag(die
);
93 fprintf(stderr
, "%s: DW_TAG_%s (%#x) @ <%#llx> not handled in a %s CU!\n",
94 fn
, dwarf_tag_name(tag
), tag
,
95 (unsigned long long)dwarf_dieoffset(die
), lang__int2str(cu
->language
));
98 static struct tag unsupported_tag
;
100 #define cu__tag_not_handled(cu, die) __cu__tag_not_handled(cu, die, __FUNCTION__)
103 struct hlist_node hash_node
;
107 Dwarf_Off abstract_origin
;
108 Dwarf_Off containing_type
;
110 Dwarf_Off specification
;
113 bool abstract_origin
:1;
114 bool containing_type
:1;
115 bool specification
:1;
116 } from_types_section
;
119 const char *decl_file
;
122 static inline struct tag
*dtag__tag(struct dwarf_tag
*dtag
)
124 return (struct tag
*)(dtag
+ 1);
127 static inline struct dwarf_tag
*tag__dwarf(const struct tag
*tag
)
129 return ((struct dwarf_tag
*)tag
) - 1;
133 struct hlist_head
*hash_tags
;
134 struct hlist_head
*hash_types
;
135 struct dwarf_tag
*last_type_lookup
;
137 struct dwarf_cu
*type_unit
;
140 static int dwarf_cu__init(struct dwarf_cu
*dcu
, struct cu
*cu
)
142 static struct dwarf_tag sentinel_dtag
= { .id
= ULLONG_MAX
, };
143 uint64_t hashtags_size
= 1UL << hashtags__bits
;
147 dcu
->hash_tags
= cu__malloc(cu
, sizeof(struct hlist_head
) * hashtags_size
);
151 dcu
->hash_types
= cu__malloc(cu
, sizeof(struct hlist_head
) * hashtags_size
);
152 if (!dcu
->hash_types
) {
153 cu__free(cu
, dcu
->hash_tags
);
158 for (i
= 0; i
< hashtags_size
; ++i
) {
159 INIT_HLIST_HEAD(&dcu
->hash_tags
[i
]);
160 INIT_HLIST_HEAD(&dcu
->hash_types
[i
]);
162 dcu
->type_unit
= NULL
;
163 // To avoid a per-lookup check against NULL in dwarf_cu__find_type_by_ref()
164 dcu
->last_type_lookup
= &sentinel_dtag
;
168 static struct dwarf_cu
*dwarf_cu__new(struct cu
*cu
)
170 struct dwarf_cu
*dwarf_cu
= cu__zalloc(cu
, sizeof(*dwarf_cu
));
172 if (dwarf_cu
!= NULL
&& dwarf_cu__init(dwarf_cu
, cu
) != 0) {
173 cu__free(cu
, dwarf_cu
);
180 static void dwarf_cu__delete(struct cu
*cu
)
182 if (cu
== NULL
|| cu
->priv
== NULL
)
185 struct dwarf_cu
*dcu
= cu
->priv
;
187 // dcu->hash_tags & dcu->hash_types are on cu->obstack
192 static void __tag__print_type_not_found(struct tag
*tag
, const char *func
)
194 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
195 fprintf(stderr
, "%s: couldn't find %#llx type for %#llx (%s)!\n", func
,
196 (unsigned long long)dtag
->type
, (unsigned long long)dtag
->id
,
197 dwarf_tag_name(tag
->tag
));
200 #define tag__print_type_not_found(tag) \
201 __tag__print_type_not_found(tag, __func__)
203 static void hashtags__hash(struct hlist_head
*hashtable
,
204 struct dwarf_tag
*dtag
)
206 struct hlist_head
*head
= hashtable
+ hashtags__fn(dtag
->id
);
207 hlist_add_head(&dtag
->hash_node
, head
);
210 static struct dwarf_tag
*hashtags__find(const struct hlist_head
*hashtable
,
216 struct dwarf_tag
*tpos
;
217 struct hlist_node
*pos
;
218 uint32_t bucket
= hashtags__fn(id
);
219 const struct hlist_head
*head
= hashtable
+ bucket
;
221 hlist_for_each_entry(tpos
, pos
, head
, hash_node
) {
229 static void cu__hash(struct cu
*cu
, struct tag
*tag
)
231 struct dwarf_cu
*dcu
= cu
->priv
;
232 struct hlist_head
*hashtable
= tag__is_tag_type(tag
) ?
235 hashtags__hash(hashtable
, tag__dwarf(tag
));
238 static struct dwarf_tag
*__dwarf_cu__find_tag_by_ref(const struct dwarf_cu
*cu
,
239 const Dwarf_Off ref
, bool from_types
)
246 return hashtags__find(cu
->hash_tags
, ref
);
249 #define dwarf_cu__find_tag_by_ref(cu, dtag, field) \
250 __dwarf_cu__find_tag_by_ref(cu, dtag->field, dtag->from_types_section.field)
252 static struct dwarf_tag
*__dwarf_cu__find_type_by_ref(struct dwarf_cu
*dcu
,
253 const Dwarf_Off ref
, bool from_types
)
258 dcu
= dcu
->type_unit
;
264 if (dcu
->last_type_lookup
->id
== ref
)
265 return dcu
->last_type_lookup
;
267 struct dwarf_tag
*dtag
= hashtags__find(dcu
->hash_types
, ref
);
270 dcu
->last_type_lookup
= dtag
;
275 #define dwarf_cu__find_type_by_ref(dcu, dtag, field) \
276 __dwarf_cu__find_type_by_ref(dcu, dtag->field, dtag->from_types_section.field)
278 static void *memdup(const void *src
, size_t len
, struct cu
*cu
)
280 void *s
= cu__malloc(cu
, len
);
286 /* Number decoding macros. See 7.6 Variable Length Data. */
288 #define get_uleb128_step(var, addr, nth, break) \
290 var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
291 if ((__b & 0x80) == 0) \
294 #define get_uleb128_rest_return(var, i, addrp) \
296 for (; i < 10; ++i) { \
297 get_uleb128_step(var, *addrp, i, \
300 /* Other implementations set VALUE to UINT_MAX in this \
301 case. So we better do this as well. */ \
305 static uint64_t __libdw_get_uleb128(uint64_t acc
, uint32_t i
,
306 const uint8_t **addrp
)
309 get_uleb128_rest_return (acc
, i
, addrp
);
312 #define get_uleb128(var, addr) \
316 get_uleb128_step(var, addr, 0, break); \
317 var = __libdw_get_uleb128 (var, 1, &(addr)); \
320 static uint64_t attr_numeric(Dwarf_Die
*die
, uint32_t name
)
322 Dwarf_Attribute attr
;
325 if (dwarf_attr(die
, name
, &attr
) == NULL
)
328 form
= dwarf_whatform(&attr
);
333 if (dwarf_formaddr(&attr
, &addr
) == 0)
337 case DW_FORM_implicit_const
:
343 case DW_FORM_udata
: {
345 if (dwarf_formudata(&attr
, &value
) == 0)
350 case DW_FORM_flag_present
: {
352 if (dwarf_formflag(&attr
, &value
) == 0)
357 fprintf(stderr
, "DW_AT_<0x%x>=0x%x\n", name
, form
);
364 static uint64_t attr_alignment(Dwarf_Die
*die
, struct conf_load
*conf
)
366 return conf
->ignore_alignment_attr
? 0 : attr_numeric(die
, DW_AT_alignment
);
369 static uint64_t dwarf_expr(const uint8_t *expr
, uint32_t len __maybe_unused
)
371 /* Common case: offset from start of the class */
372 if (expr
[0] == DW_OP_plus_uconst
||
373 expr
[0] == DW_OP_constu
) {
376 get_uleb128(result
, expr
);
380 fprintf(stderr
, "%s: unhandled %#x DW_OP_ operation\n",
385 static Dwarf_Off
__attr_offset(Dwarf_Attribute
*attr
)
389 switch (dwarf_whatform(attr
)) {
390 case DW_FORM_implicit_const
:
396 case DW_FORM_udata
: {
398 if (dwarf_formudata(attr
, &value
) == 0)
403 if (dwarf_formblock(attr
, &block
) == 0)
404 return dwarf_expr(block
.data
, block
.length
);
410 static Dwarf_Off
attr_offset(Dwarf_Die
*die
, const uint32_t name
)
412 Dwarf_Attribute attr
;
414 if (dwarf_attr(die
, name
, &attr
) == NULL
)
417 return __attr_offset(&attr
);
420 static const char *attr_string(Dwarf_Die
*die
, uint32_t name
, struct conf_load
*conf __maybe_unused
)
422 const char *str
= NULL
;
423 Dwarf_Attribute attr
;
425 if (dwarf_attr(die
, name
, &attr
) != NULL
) {
426 str
= dwarf_formstring(&attr
);
428 if (conf
&& conf
->kabi_prefix
&& str
&& strncmp(str
, conf
->kabi_prefix
, conf
->kabi_prefix_len
) == 0)
429 return conf
->kabi_prefix
;
435 static bool attr_type(Dwarf_Die
*die
, uint32_t attr_name
, Dwarf_Off
*offset
)
437 Dwarf_Attribute attr
;
439 if (dwarf_attr(die
, attr_name
, &attr
) != NULL
) {
441 if (dwarf_formref_die(&attr
, &type_die
) != NULL
) {
442 *offset
= dwarf_dieoffset(&type_die
);
443 return attr
.form
== DW_FORM_ref_sig8
;
450 static int attr_location(Dwarf_Die
*die
, Dwarf_Op
**expr
, size_t *exprlen
)
452 Dwarf_Attribute attr
;
455 if (dwarf_attr(die
, DW_AT_location
, &attr
) != NULL
) {
456 /* use libdw__lock as dwarf_getlocation(s) has concurrency
457 * issues when libdw is not compiled with experimental
458 * --enable-thread-safety
460 pthread_mutex_lock(&libdw__lock
);
461 if (dwarf_getlocation(&attr
, expr
, exprlen
) == 0) {
462 /* DW_OP_addrx needs additional lookup for real addr. */
463 if (*exprlen
!= 0 && expr
[0]->atom
== DW_OP_addrx
) {
464 Dwarf_Attribute addr_attr
;
465 dwarf_getlocation_attr(&attr
, expr
[0], &addr_attr
);
468 dwarf_formaddr (&addr_attr
, &address
);
470 expr
[0]->number
= address
;
474 pthread_mutex_unlock(&libdw__lock
);
480 /* The struct dwarf_tag has a fixed size while the 'struct tag' is just the base
481 * class for all DWARF DW_TAG_ tags, so we must have the fixed part first
482 * to be able to derive it from the one that has multiple sizes.
484 static void *tag__alloc(struct cu
*cu
, size_t size
)
486 struct dwarf_tag
*dtag
= cu__zalloc(cu
, sizeof(*dtag
) + size
);
488 return dtag
? dtag__tag(dtag
) : NULL
;
491 static void tag__free(struct tag
*tag
, struct cu
*cu
)
493 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
498 #define dwarf_tag__set_attr_type(dtag, field, die, attr_name) \
499 dtag->from_types_section.field = attr_type(die, attr_name, &dtag->field)
501 static void tag__init(struct tag
*tag
, struct cu
*cu
, Dwarf_Die
*die
)
503 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
505 tag
->tag
= dwarf_tag(die
);
507 dtag
->id
= dwarf_dieoffset(die
);
509 if (tag
->tag
== DW_TAG_imported_module
|| tag
->tag
== DW_TAG_imported_declaration
)
510 dwarf_tag__set_attr_type(dtag
, type
, die
, DW_AT_import
);
512 dwarf_tag__set_attr_type(dtag
, type
, die
, DW_AT_type
);
514 dwarf_tag__set_attr_type(dtag
, abstract_origin
, die
, DW_AT_abstract_origin
);
515 tag
->recursivity_level
= 0;
516 tag
->attribute
= NULL
;
518 if (cu
->extra_dbg_info
) {
519 pthread_mutex_lock(&libdw__lock
);
522 const char *decl_file
= dwarf_decl_file(die
);
523 static const char *last_decl_file
, *last_decl_file_ptr
;
525 if (decl_file
!= last_decl_file_ptr
) {
526 last_decl_file
= decl_file
? strdup(decl_file
) : NULL
;
527 last_decl_file_ptr
= decl_file
;
530 dtag
->decl_file
= last_decl_file
;
531 dwarf_decl_line(die
, &decl_line
);
532 dtag
->decl_line
= decl_line
;
534 pthread_mutex_unlock(&libdw__lock
);
537 INIT_LIST_HEAD(&tag
->node
);
540 static struct tag
*tag__new(Dwarf_Die
*die
, struct cu
*cu
)
542 struct tag
*tag
= tag__alloc(cu
, sizeof(*tag
));
545 tag__init(tag
, cu
, die
);
550 static void tag__set_spec(struct tag
*tag
, Dwarf_Die
*die
)
552 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
553 dwarf_tag__set_attr_type(dtag
, specification
, die
, DW_AT_specification
);
556 static struct ptr_to_member_type
*ptr_to_member_type__new(Dwarf_Die
*die
,
559 struct ptr_to_member_type
*ptr
= tag__alloc(cu
, sizeof(*ptr
));
562 tag__init(&ptr
->tag
, cu
, die
);
563 struct dwarf_tag
*dtag
= tag__dwarf(&ptr
->tag
);
564 dwarf_tag__set_attr_type(dtag
, containing_type
, die
, DW_AT_containing_type
);
570 static uint8_t encoding_to_float_type(uint64_t encoding
)
573 case DW_ATE_complex_float
: return BT_FP_CMPLX
;
574 case DW_ATE_float
: return BT_FP_SINGLE
;
575 case DW_ATE_imaginary_float
: return BT_FP_IMGRY
;
580 static struct base_type
*base_type__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
582 struct base_type
*bt
= tag__alloc(cu
, sizeof(*bt
));
585 tag__init(&bt
->tag
, cu
, die
);
586 bt
->name
= attr_string(die
, DW_AT_name
, conf
);
587 bt
->bit_size
= attr_numeric(die
, DW_AT_byte_size
) * 8;
588 uint64_t encoding
= attr_numeric(die
, DW_AT_encoding
);
589 bt
->is_bool
= encoding
== DW_ATE_boolean
;
590 bt
->is_signed
= (encoding
== DW_ATE_signed
) || (encoding
== DW_ATE_signed_char
);
591 bt
->is_varargs
= false;
592 bt
->name_has_encoding
= true;
593 bt
->float_type
= encoding_to_float_type(encoding
);
594 INIT_LIST_HEAD(&bt
->node
);
600 static struct array_type
*array_type__new(Dwarf_Die
*die
, struct cu
*cu
)
602 struct array_type
*at
= tag__alloc(cu
, sizeof(*at
));
605 tag__init(&at
->tag
, cu
, die
);
607 at
->nr_entries
= NULL
;
608 at
->is_vector
= dwarf_hasattr(die
, DW_AT_GNU_vector
);
614 static struct string_type
*string_type__new(Dwarf_Die
*die
, struct cu
*cu
)
616 struct string_type
*st
= tag__alloc(cu
, sizeof(*st
));
619 tag__init(&st
->tag
, cu
, die
);
620 st
->nr_entries
= attr_numeric(die
, DW_AT_byte_size
);
621 if (st
->nr_entries
== 0)
628 static void namespace__init(struct namespace *namespace, Dwarf_Die
*die
,
629 struct cu
*cu
, struct conf_load
*conf
)
631 tag__init(&namespace->tag
, cu
, die
);
632 INIT_LIST_HEAD(&namespace->tags
);
633 INIT_LIST_HEAD(&namespace->annots
);
634 namespace->name
= attr_string(die
, DW_AT_name
, conf
);
635 namespace->tag
.shared_tags
= 0;
638 static struct namespace *namespace__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
640 struct namespace *namespace = tag__alloc(cu
, sizeof(*namespace));
642 if (namespace != NULL
)
643 namespace__init(namespace, die
, cu
, conf
);
648 static void type__init(struct type
*type
, Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
650 namespace__init(&type
->namespace, die
, cu
, conf
);
652 type
->size
= attr_numeric(die
, DW_AT_byte_size
);
653 type
->alignment
= attr_alignment(die
, conf
);
654 type
->declaration
= attr_numeric(die
, DW_AT_declaration
);
655 tag__set_spec(&type
->namespace.tag
, die
);
656 type
->definition_emitted
= 0;
657 type
->fwd_decl_emitted
= 0;
659 type
->nr_members
= 0;
660 type
->nr_static_members
= 0;
661 type
->is_signed_enum
= 0;
663 Dwarf_Attribute attr
;
664 if (dwarf_attr(die
, DW_AT_type
, &attr
) != NULL
) {
666 if (dwarf_formref_die(&attr
, &type_die
) != NULL
) {
667 uint64_t encoding
= attr_numeric(&type_die
, DW_AT_encoding
);
669 if (encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
)
670 type
->is_signed_enum
= 1;
675 static struct type
*type__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
677 struct type
*type
= tag__alloc(cu
, sizeof(*type
));
680 type__init(type
, die
, cu
, conf
);
685 static struct enumerator
*enumerator__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
687 struct enumerator
*enumerator
= tag__alloc(cu
, sizeof(*enumerator
));
689 if (enumerator
!= NULL
) {
690 tag__init(&enumerator
->tag
, cu
, die
);
691 enumerator
->name
= attr_string(die
, DW_AT_name
, conf
);
692 enumerator
->value
= attr_numeric(die
, DW_AT_const_value
);
698 static enum vscope
dwarf__location(Dwarf_Die
*die
, uint64_t *addr
, struct location
*location
)
700 enum vscope scope
= VSCOPE_UNKNOWN
;
702 if (attr_location(die
, &location
->expr
, &location
->exprlen
) != 0)
703 scope
= VSCOPE_OPTIMIZED
;
704 else if (location
->exprlen
!= 0) {
705 Dwarf_Op
*expr
= location
->expr
;
706 switch (expr
->atom
) {
709 scope
= VSCOPE_GLOBAL
;
710 *addr
= expr
[0].number
;
712 case DW_OP_reg1
... DW_OP_reg31
:
713 case DW_OP_breg0
... DW_OP_breg31
:
714 scope
= VSCOPE_REGISTER
; break;
716 scope
= VSCOPE_LOCAL
; break;
723 enum vscope
variable__scope(const struct variable
*var
)
728 const char *variable__scope_str(const struct variable
*var
)
730 switch (var
->scope
) {
731 case VSCOPE_LOCAL
: return "local";
732 case VSCOPE_GLOBAL
: return "global";
733 case VSCOPE_REGISTER
: return "register";
734 case VSCOPE_OPTIMIZED
: return "optimized";
741 static struct variable
*variable__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
, int top_level
)
743 bool has_specification
= dwarf_hasattr(die
, DW_AT_specification
);
744 struct variable
*var
= tag__alloc(cu
, sizeof(*var
));
747 tag__init(&var
->ip
.tag
, cu
, die
);
748 var
->name
= attr_string(die
, DW_AT_name
, conf
);
749 /* variable is visible outside of its enclosing cu */
750 var
->external
= dwarf_hasattr(die
, DW_AT_external
);
751 /* non-defining declaration of an object */
752 var
->declaration
= dwarf_hasattr(die
, DW_AT_declaration
);
753 var
->has_specification
= has_specification
;
754 var
->artificial
= dwarf_hasattr(die
, DW_AT_artificial
);
755 var
->top_level
= top_level
;
756 var
->scope
= VSCOPE_UNKNOWN
;
757 INIT_LIST_HEAD(&var
->annots
);
759 if (!var
->declaration
&& cu
->has_addr_info
)
760 var
->scope
= dwarf__location(die
, &var
->ip
.addr
, &var
->location
);
761 if (has_specification
) {
762 tag__set_spec(&var
->ip
.tag
, die
);
769 static struct constant
*constant__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
771 struct constant
*constant
= tag__alloc(cu
, sizeof(*constant
));
773 if (constant
!= NULL
) {
774 tag__init(&constant
->tag
, cu
, die
);
775 constant
->name
= attr_string(die
, DW_AT_name
, conf
);
776 constant
->value
= attr_numeric(die
, DW_AT_const_value
);
782 static int tag__recode_dwarf_bitfield(struct tag
*tag
, struct cu
*cu
, uint16_t bit_size
)
787 /* in all the cases the name is at the same offset */
788 const char *name
= namespace__name(tag__namespace(tag
));
791 case DW_TAG_typedef
: {
792 const struct dwarf_tag
*dtag
= tag__dwarf(tag
);
793 struct dwarf_tag
*dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, type
);
796 tag__print_type_not_found(tag
);
800 struct tag
*type
= dtag__tag(dtype
);
802 id
= tag__recode_dwarf_bitfield(type
, cu
, bit_size
);
806 struct type
*new_typedef
= cu__tag_alloc(cu
, sizeof(*new_typedef
));
807 if (new_typedef
== NULL
)
810 recoded
= (struct tag
*)new_typedef
;
811 recoded
->tag
= DW_TAG_typedef
;
813 new_typedef
->namespace.name
= tag__namespace(tag
)->name
;
817 case DW_TAG_const_type
:
818 case DW_TAG_volatile_type
:
819 case DW_TAG_atomic_type
: {
820 const struct dwarf_tag
*dtag
= tag__dwarf(tag
);
821 struct dwarf_tag
*dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, type
);
824 tag__print_type_not_found(tag
);
828 struct tag
*type
= dtag__tag(dtype
);
830 id
= tag__recode_dwarf_bitfield(type
, cu
, bit_size
);
831 if (id
>= 0 && (uint32_t)id
== tag
->type
)
834 recoded
= cu__tag_alloc(cu
, sizeof(*recoded
));
838 recoded
->tag
= DW_TAG_volatile_type
;
843 case DW_TAG_base_type
:
845 * Here we must search on the final, core cu, not on
846 * the dwarf_cu as in dwarf there are no such things
847 * as base_types of less than 8 bits, etc.
849 recoded
= cu__find_base_type_by_name_and_size(cu
, name
, bit_size
, &short_id
);
853 struct base_type
*new_bt
= cu__tag_alloc(cu
, sizeof(*new_bt
));
857 recoded
= (struct tag
*)new_bt
;
858 recoded
->tag
= DW_TAG_base_type
;
859 recoded
->top_level
= 1;
860 new_bt
->name
= strdup(name
);
861 new_bt
->bit_size
= bit_size
;
864 case DW_TAG_enumeration_type
:
866 * Here we must search on the final, core cu, not on
867 * the dwarf_cu as in dwarf there are no such things
868 * as enumeration_types of less than 8 bits, etc.
870 recoded
= cu__find_enumeration_by_name_and_size(cu
, name
, bit_size
, &short_id
);
874 struct type
*alias
= tag__type(tag
);
875 struct type
*new_enum
= cu__tag_alloc(cu
, sizeof(*new_enum
));
876 if (new_enum
== NULL
)
879 recoded
= (struct tag
*)new_enum
;
880 recoded
->tag
= DW_TAG_enumeration_type
;
881 recoded
->top_level
= 1;
882 new_enum
->nr_members
= alias
->nr_members
;
886 new_enum
->namespace.tags
.next
= &alias
->namespace.tags
;
887 new_enum
->namespace.tag
.shared_tags
= 1;
888 new_enum
->namespace.name
= strdup(name
);
889 new_enum
->size
= bit_size
;
892 fprintf(stderr
, "%s: tag=%s, name=%s, bit_size=%d\n",
893 __func__
, dwarf_tag_name(tag
->tag
),
899 if (cu__add_tag(cu
, recoded
, &new_id
) == 0)
902 cu__free(cu
, recoded
);
906 static int add_llvm_annotation(Dwarf_Die
*die
, int component_idx
, struct conf_load
*conf
,
907 struct list_head
*head
)
909 struct llvm_annotation
*annot
;
912 if (conf
->skip_encoding_btf_decl_tag
)
915 /* Only handle btf_decl_tag annotation for now. */
916 name
= attr_string(die
, DW_AT_name
, conf
);
917 if (strcmp(name
, "btf_decl_tag") != 0)
920 annot
= zalloc(sizeof(*annot
));
924 annot
->value
= attr_string(die
, DW_AT_const_value
, conf
);
925 annot
->component_idx
= component_idx
;
926 list_add_tail(&annot
->node
, head
);
930 static int add_child_llvm_annotations(Dwarf_Die
*die
, int component_idx
,
931 struct conf_load
*conf
, struct list_head
*head
)
936 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
941 if (dwarf_tag(die
) == DW_TAG_LLVM_annotation
) {
942 ret
= add_llvm_annotation(die
, component_idx
, conf
, head
);
946 } while (dwarf_siblingof(die
, die
) == 0);
951 int class_member__dwarf_recode_bitfield(struct class_member
*member
,
954 struct dwarf_tag
*dtag
= tag__dwarf(&member
->tag
);
955 struct dwarf_tag
*type
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, type
);
961 recoded_type_id
= tag__recode_dwarf_bitfield(dtag__tag(type
), cu
, member
->bitfield_size
);
962 if (recoded_type_id
< 0)
963 return recoded_type_id
;
965 member
->tag
.type
= recoded_type_id
;
969 static struct class_member
*class_member__new(Dwarf_Die
*die
, struct cu
*cu
,
970 bool in_union
, struct conf_load
*conf
)
972 struct class_member
*member
= tag__alloc(cu
, sizeof(*member
));
974 if (member
!= NULL
) {
975 tag__init(&member
->tag
, cu
, die
);
976 member
->name
= attr_string(die
, DW_AT_name
, conf
);
977 member
->alignment
= attr_alignment(die
, conf
);
979 Dwarf_Attribute attr
;
981 member
->has_bit_offset
= dwarf_attr(die
, DW_AT_data_bit_offset
, &attr
) != NULL
;
983 if (member
->has_bit_offset
) {
984 member
->bit_offset
= __attr_offset(&attr
);
985 // byte_offset and bitfield_offset will be recalculated later, when
986 // we discover the size of this bitfield base type.
988 if (dwarf_attr(die
, DW_AT_data_member_location
, &attr
) != NULL
) {
989 member
->byte_offset
= __attr_offset(&attr
);
991 member
->is_static
= !in_union
;
995 * Bit offset calculated here is valid only for byte-aligned
996 * fields. For bitfields on little-endian archs we need to
997 * adjust them taking into account byte size of the field,
998 * which might not be yet known. So we'll re-calculate bit
999 * offset later, in class_member__cache_byte_size.
1001 member
->bit_offset
= member
->byte_offset
* 8;
1002 member
->bitfield_offset
= attr_numeric(die
, DW_AT_bit_offset
);
1006 * If DW_AT_byte_size is not present, byte size will be
1007 * determined later in class_member__cache_byte_size using
1008 * base integer/enum type
1010 member
->byte_size
= attr_numeric(die
, DW_AT_byte_size
);
1011 member
->bitfield_size
= attr_numeric(die
, DW_AT_bit_size
);
1012 member
->bit_hole
= 0;
1013 member
->bitfield_end
= 0;
1014 member
->visited
= 0;
1016 if (!cu__is_c(cu
)) {
1017 member
->accessibility
= attr_numeric(die
, DW_AT_accessibility
);
1018 member
->const_value
= attr_numeric(die
, DW_AT_const_value
);
1019 member
->virtuality
= attr_numeric(die
, DW_AT_virtuality
);
1027 /* How many function parameters are passed via registers? Used below in
1028 * determining if an argument has been optimized out or if it is simply
1029 * an argument > cu__nr_register_params(). Making cu__nr_register_params()
1030 * return 0 allows unsupported architectures to skip tagging optimized-out
1033 static int arch__nr_register_params(const GElf_Ehdr
*ehdr
)
1035 switch (ehdr
->e_machine
) {
1036 case EM_S390
: return 5;
1039 case EM_X86_64
: return 6;
1046 case EM_RISCV
: return 8;
1053 /* map from parameter index (0 for first, ...) to expected DW_OP_reg.
1054 * This will allow us to identify cases where optimized-out parameters
1055 * interfere with expectations about register contents on function
1058 static void arch__set_register_params(const GElf_Ehdr
*ehdr
, struct cu
*cu
)
1060 memset(cu
->register_params
, -1, sizeof(cu
->register_params
));
1062 switch (ehdr
->e_machine
) {
1064 /* https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf */
1065 cu
->register_params
[0] = DW_OP_reg2
; // %r2
1066 cu
->register_params
[1] = DW_OP_reg3
; // %r3
1067 cu
->register_params
[2] = DW_OP_reg4
; // %r4
1068 cu
->register_params
[3] = DW_OP_reg5
; // %r5
1069 cu
->register_params
[4] = DW_OP_reg6
; // %r6
1072 /* //en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI */
1073 cu
->register_params
[0] = DW_OP_reg5
; // %rdi
1074 cu
->register_params
[1] = DW_OP_reg4
; // %rsi
1075 cu
->register_params
[2] = DW_OP_reg1
; // %rdx
1076 cu
->register_params
[3] = DW_OP_reg2
; // %rcx
1077 cu
->register_params
[4] = DW_OP_reg8
; // %r8
1078 cu
->register_params
[5] = DW_OP_reg9
; // %r9
1081 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers */
1083 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
1084 cu
->register_params
[0] = DW_OP_reg0
;
1085 cu
->register_params
[1] = DW_OP_reg1
;
1086 cu
->register_params
[2] = DW_OP_reg2
;
1087 cu
->register_params
[3] = DW_OP_reg3
;
1088 cu
->register_params
[4] = DW_OP_reg4
;
1089 cu
->register_params
[5] = DW_OP_reg5
;
1090 cu
->register_params
[6] = DW_OP_reg6
;
1091 cu
->register_params
[7] = DW_OP_reg7
;
1098 static struct template_type_param
*template_type_param__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1100 struct template_type_param
*ttparm
= tag__alloc(cu
, sizeof(*ttparm
));
1102 if (ttparm
!= NULL
) {
1103 tag__init(&ttparm
->tag
, cu
, die
);
1104 ttparm
->name
= attr_string(die
, DW_AT_name
, conf
);
1110 static struct template_value_param
*template_value_param__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1112 struct template_value_param
*tvparm
= tag__alloc(cu
, sizeof(*tvparm
));
1114 if (tvparm
!= NULL
) {
1115 tag__init(&tvparm
->tag
, cu
, die
);
1116 tvparm
->name
= attr_string(die
, DW_AT_name
, conf
);
1117 tvparm
->const_value
= attr_numeric(die
, DW_AT_const_value
);
1118 tvparm
->default_value
= attr_numeric(die
, DW_AT_default_value
);
1124 static int template_parameter_pack__load_params(struct template_parameter_pack
*pack
, Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1128 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1133 if (dwarf_tag(die
) != DW_TAG_template_type_parameter
) {
1134 cu__tag_not_handled(cu
, die
);
1138 struct template_type_param
*param
= template_type_param__new(die
, cu
, conf
);
1143 template_parameter_pack__add(pack
, param
);
1144 } while (dwarf_siblingof(die
, die
) == 0);
1149 static struct template_parameter_pack
*template_parameter_pack__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1151 struct template_parameter_pack
*pack
= tag__alloc(cu
, sizeof(*pack
));
1154 tag__init(&pack
->tag
, cu
, die
);
1156 pack
->name
= attr_string(die
, DW_AT_name
, conf
);
1157 INIT_LIST_HEAD(&pack
->params
);
1159 if (template_parameter_pack__load_params(pack
, die
, cu
, conf
)) {
1160 template_parameter_pack__delete(pack
, cu
);
1168 /* Returns number of locations found or negative value for errors. */
1169 static ptrdiff_t __dwarf_getlocations(Dwarf_Attribute
*attr
,
1170 ptrdiff_t offset
, Dwarf_Addr
*basep
,
1171 Dwarf_Addr
*startp
, Dwarf_Addr
*endp
,
1172 Dwarf_Op
**expr
, size_t *exprlen
)
1176 #if _ELFUTILS_PREREQ(0, 157)
1177 ret
= dwarf_getlocations(attr
, offset
, basep
, startp
, endp
, expr
, exprlen
);
1180 ret
= dwarf_getlocation(attr
, expr
, exprlen
);
1188 /* For DW_AT_location 'attr':
1189 * - if first location is DW_OP_regXX with expected number, return the register;
1190 * otherwise save the register for later return
1191 * - if location DW_OP_entry_value(DW_OP_regXX) with expected number is in the
1192 * list, return the register; otherwise save register for later return
1193 * - otherwise if no register was found for locations, return -1.
1195 static int parameter__reg(Dwarf_Attribute
*attr
, int expected_reg
)
1197 Dwarf_Addr base
, start
, end
;
1198 Dwarf_Op
*expr
, *entry_ops
;
1199 Dwarf_Attribute entry_attr
;
1200 size_t exprlen
, entry_len
;
1201 ptrdiff_t offset
= 0;
1205 /* use libdw__lock as dwarf_getlocation(s) has concurrency issues
1206 * when libdw is not compiled with experimental --enable-thread-safety
1208 pthread_mutex_lock(&libdw__lock
);
1209 while ((offset
= __dwarf_getlocations(attr
, offset
, &base
, &start
, &end
, &expr
, &exprlen
)) > 0) {
1212 /* Convert expression list (XX DW_OP_stack_value) -> (XX).
1213 * DW_OP_stack_value instructs interpreter to pop current value from
1214 * DWARF expression evaluation stack, and thus is not important here.
1216 if (exprlen
> 1 && expr
[exprlen
- 1].atom
== DW_OP_stack_value
)
1222 switch (expr
->atom
) {
1223 /* match DW_OP_regXX at first location */
1224 case DW_OP_reg0
... DW_OP_reg31
:
1228 if (ret
== expected_reg
)
1231 /* match DW_OP_entry_value(DW_OP_regXX) at any location */
1232 case DW_OP_entry_value
:
1233 case DW_OP_GNU_entry_value
:
1234 if (dwarf_getlocation_attr(attr
, expr
, &entry_attr
) == 0 &&
1235 dwarf_getlocation(&entry_attr
, &entry_ops
, &entry_len
) == 0 &&
1237 ret
= entry_ops
->atom
;
1238 if (ret
== expected_reg
)
1245 pthread_mutex_unlock(&libdw__lock
);
1249 static struct parameter
*parameter__new(Dwarf_Die
*die
, struct cu
*cu
,
1250 struct conf_load
*conf
, int param_idx
)
1252 struct parameter
*parm
= tag__alloc(cu
, sizeof(*parm
));
1255 bool has_const_value
;
1256 Dwarf_Attribute attr
;
1258 tag__init(&parm
->tag
, cu
, die
);
1259 parm
->name
= attr_string(die
, DW_AT_name
, conf
);
1261 if (param_idx
>= cu
->nr_register_params
|| param_idx
< 0)
1263 /* Parameters which use DW_AT_abstract_origin to point at
1264 * the original parameter definition (with no name in the DIE)
1265 * are the result of later DWARF generation during compilation
1266 * so often better take into account if arguments were
1269 * By checking that locations for parameters that are expected
1270 * to be passed as registers are actually passed as registers,
1271 * we can spot optimized-out parameters.
1273 * It can also be the case that a parameter DIE has
1274 * a constant value attribute reflecting optimization or
1275 * has no location attribute.
1277 * From the DWARF spec:
1281 * A DW_AT_const_value attribute for an entry describing a
1282 * variable or formal parameter whose value is constant and not
1283 * represented by an object in the address space of the program,
1284 * or an entry describing a named constant. (Note
1285 * that such an entry does not have a location attribute.)"
1287 * So we can also use the absence of a location for a parameter
1288 * as evidence it has been optimized out. This info will
1289 * need to be shared between a parameter and any abstract
1290 * origin references however, since gcc can have location
1291 * information in the parameter that refers back to the original
1292 * via abstract origin, so we need to share location presence
1293 * between these parameter representations. See
1294 * ftype__recode_dwarf_types() below for how this is handled.
1296 has_const_value
= dwarf_attr(die
, DW_AT_const_value
, &attr
) != NULL
;
1297 parm
->has_loc
= dwarf_attr(die
, DW_AT_location
, &attr
) != NULL
;
1299 if (parm
->has_loc
) {
1300 int expected_reg
= cu
->register_params
[param_idx
];
1301 int actual_reg
= parameter__reg(&attr
, expected_reg
);
1304 parm
->optimized
= 1;
1305 else if (expected_reg
>= 0 && expected_reg
!= actual_reg
)
1306 /* mark parameters that use an unexpected
1307 * register to hold a parameter; these will
1308 * be problematic for users of BTF as they
1309 * violate expectations about register
1312 parm
->unexpected_reg
= 1;
1313 } else if (has_const_value
) {
1314 parm
->optimized
= 1;
1321 static int formal_parameter_pack__load_params(struct formal_parameter_pack
*pack
, Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1325 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1330 if (dwarf_tag(die
) != DW_TAG_formal_parameter
) {
1331 cu__tag_not_handled(cu
, die
);
1335 struct parameter
*param
= parameter__new(die
, cu
, conf
, -1);
1340 formal_parameter_pack__add(pack
, param
);
1341 } while (dwarf_siblingof(die
, die
) == 0);
1346 static struct formal_parameter_pack
*formal_parameter_pack__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1348 struct formal_parameter_pack
*pack
= tag__alloc(cu
, sizeof(*pack
));
1351 tag__init(&pack
->tag
, cu
, die
);
1353 INIT_LIST_HEAD(&pack
->params
);
1355 if (formal_parameter_pack__load_params(pack
, die
, cu
, conf
)) {
1356 formal_parameter_pack__delete(pack
, cu
);
1364 static struct inline_expansion
*inline_expansion__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1366 struct inline_expansion
*exp
= tag__alloc(cu
, sizeof(*exp
));
1369 struct dwarf_tag
*dtag
= tag__dwarf(&exp
->ip
.tag
);
1371 tag__init(&exp
->ip
.tag
, cu
, die
);
1372 dtag
->decl_file
= attr_string(die
, DW_AT_call_file
, conf
);
1373 dtag
->decl_line
= attr_numeric(die
, DW_AT_call_line
);
1374 dwarf_tag__set_attr_type(dtag
, type
, die
, DW_AT_abstract_origin
);
1378 if (!cu
->has_addr_info
)
1381 if (dwarf_lowpc(die
, &exp
->ip
.addr
))
1383 if (dwarf_lowpc(die
, &exp
->high_pc
))
1386 exp
->size
= exp
->high_pc
- exp
->ip
.addr
;
1387 if (exp
->size
== 0) {
1388 Dwarf_Addr base
, start
;
1389 ptrdiff_t offset
= 0;
1392 offset
= dwarf_ranges(die
, offset
, &base
, &start
,
1394 start
= (unsigned long)start
;
1395 exp
->high_pc
= (unsigned long)exp
->high_pc
;
1398 exp
->size
+= exp
->high_pc
- start
;
1399 if (exp
->ip
.addr
== 0)
1400 exp
->ip
.addr
= start
;
1408 static struct label
*label__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1410 struct label
*label
= tag__alloc(cu
, sizeof(*label
));
1412 if (label
!= NULL
) {
1413 tag__init(&label
->ip
.tag
, cu
, die
);
1414 label
->name
= attr_string(die
, DW_AT_name
, conf
);
1415 if (!cu
->has_addr_info
|| dwarf_lowpc(die
, &label
->ip
.addr
))
1422 static struct class *class__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1424 struct class *class = tag__alloc(cu
, sizeof(*class));
1426 if (class != NULL
) {
1427 type__init(&class->type
, die
, cu
, conf
);
1428 INIT_LIST_HEAD(&class->vtable
);
1429 class->nr_vtable_entries
=
1431 class->nr_bit_holes
=
1433 class->bit_padding
= 0;
1440 static void lexblock__init(struct lexblock
*block
, struct cu
*cu
,
1445 if (!cu
->has_addr_info
|| dwarf_lowpc(die
, &block
->ip
.addr
)) {
1448 } else if (dwarf_highpc(die
, &high_pc
))
1451 block
->size
= high_pc
- block
->ip
.addr
;
1453 INIT_LIST_HEAD(&block
->tags
);
1455 block
->size_inline_expansions
=
1456 block
->nr_inline_expansions
=
1458 block
->nr_lexblocks
=
1459 block
->nr_variables
= 0;
1462 static struct lexblock
*lexblock__new(Dwarf_Die
*die
, struct cu
*cu
)
1464 struct lexblock
*block
= tag__alloc(cu
, sizeof(*block
));
1466 if (block
!= NULL
) {
1467 tag__init(&block
->ip
.tag
, cu
, die
);
1468 lexblock__init(block
, cu
, die
);
1474 static void ftype__init(struct ftype
*ftype
, Dwarf_Die
*die
, struct cu
*cu
)
1477 const uint16_t tag
= dwarf_tag(die
);
1478 assert(tag
== DW_TAG_subprogram
|| tag
== DW_TAG_subroutine_type
);
1480 tag__init(&ftype
->tag
, cu
, die
);
1481 ftype
->byte_size
= attr_numeric(die
, DW_AT_byte_size
);
1482 INIT_LIST_HEAD(&ftype
->parms
);
1483 INIT_LIST_HEAD(&ftype
->template_type_params
);
1484 INIT_LIST_HEAD(&ftype
->template_value_params
);
1485 ftype
->nr_parms
= 0;
1486 ftype
->unspec_parms
= 0;
1487 ftype
->template_parameter_pack
= NULL
;
1490 static struct ftype
*ftype__new(Dwarf_Die
*die
, struct cu
*cu
)
1492 struct ftype
*ftype
= tag__alloc(cu
, sizeof(*ftype
));
1495 ftype__init(ftype
, die
, cu
);
1500 static struct function
*function__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1502 struct function
*func
= tag__alloc(cu
, sizeof(*func
));
1505 ftype__init(&func
->proto
, die
, cu
);
1506 lexblock__init(&func
->lexblock
, cu
, die
);
1507 func
->name
= attr_string(die
, DW_AT_name
, conf
);
1508 func
->linkage_name
= attr_string(die
, DW_AT_MIPS_linkage_name
, conf
);
1509 func
->inlined
= attr_numeric(die
, DW_AT_inline
);
1510 func
->declaration
= dwarf_hasattr(die
, DW_AT_declaration
);
1511 func
->external
= dwarf_hasattr(die
, DW_AT_external
);
1512 func
->abstract_origin
= dwarf_hasattr(die
, DW_AT_abstract_origin
);
1513 tag__set_spec(&func
->proto
.tag
, die
);
1514 func
->accessibility
= attr_numeric(die
, DW_AT_accessibility
);
1515 func
->virtuality
= attr_numeric(die
, DW_AT_virtuality
);
1516 INIT_LIST_HEAD(&func
->vtable_node
);
1517 INIT_LIST_HEAD(&func
->annots
);
1518 INIT_LIST_HEAD(&func
->tool_node
);
1519 func
->vtable_entry
= -1;
1520 if (dwarf_hasattr(die
, DW_AT_vtable_elem_location
))
1521 func
->vtable_entry
= attr_offset(die
, DW_AT_vtable_elem_location
);
1522 func
->cu_total_size_inline_expansions
= 0;
1523 func
->cu_total_nr_inline_expansions
= 0;
1530 static uint64_t attr_upper_bound(Dwarf_Die
*die
)
1532 Dwarf_Attribute attr
;
1534 if (dwarf_attr(die
, DW_AT_upper_bound
, &attr
) != NULL
) {
1537 if (dwarf_formudata(&attr
, &num
) == 0) {
1538 return (uintmax_t)num
+ 1;
1540 } else if (dwarf_attr(die
, DW_AT_count
, &attr
) != NULL
) {
1543 if (dwarf_formudata(&attr
, &num
) == 0) {
1544 return (uintmax_t)num
;
1551 static struct tag
*__die__process_tag(Dwarf_Die
*die
, struct cu
*cu
,
1552 int toplevel
, const char *fn
, struct conf_load
*conf
);
1554 #define die__process_tag(die, cu, toplevel, conf_load) \
1555 __die__process_tag(die, cu, toplevel, __FUNCTION__, conf_load)
1557 static struct tag
*die__create_new_tag(Dwarf_Die
*die
, struct cu
*cu
)
1559 struct tag
*tag
= tag__new(die
, cu
);
1562 if (dwarf_haschildren(die
))
1563 fprintf(stderr
, "%s: %s WITH children!\n", __func__
,
1564 dwarf_tag_name(tag
->tag
));
1570 static struct btf_type_tag_ptr_type
*die__create_new_btf_type_tag_ptr_type(Dwarf_Die
*die
, struct cu
*cu
)
1572 struct btf_type_tag_ptr_type
*tag
;
1574 tag
= tag__alloc(cu
, sizeof(struct btf_type_tag_ptr_type
));
1578 tag__init(&tag
->tag
, cu
, die
);
1579 tag
->tag
.has_btf_type_tag
= true;
1580 INIT_LIST_HEAD(&tag
->tags
);
1584 static struct btf_type_tag_type
*die__create_new_btf_type_tag_type(Dwarf_Die
*die
, struct cu
*cu
,
1585 struct conf_load
*conf
)
1587 struct btf_type_tag_type
*tag
;
1589 tag
= tag__alloc(cu
, sizeof(struct btf_type_tag_type
));
1593 tag__init(&tag
->tag
, cu
, die
);
1594 tag
->value
= attr_string(die
, DW_AT_const_value
, conf
);
1598 static struct tag
*die__create_new_pointer_tag(Dwarf_Die
*die
, struct cu
*cu
,
1599 struct conf_load
*conf
)
1601 struct btf_type_tag_ptr_type
*tag
= NULL
;
1602 struct btf_type_tag_type
*annot
;
1603 Dwarf_Die
*cdie
, child
;
1607 /* If no child tags or skipping btf_type_tag encoding, just create a new tag
1610 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0 ||
1611 conf
->skip_encoding_btf_type_tag
)
1612 return tag__new(die
, cu
);
1614 /* Otherwise, check DW_TAG_LLVM_annotation child tags */
1617 if (dwarf_tag(cdie
) != DW_TAG_LLVM_annotation
)
1620 /* Only check btf_type_tag annotations */
1621 name
= attr_string(cdie
, DW_AT_name
, conf
);
1622 if (strcmp(name
, "btf_type_tag") != 0)
1626 /* Create a btf_type_tag_ptr type. */
1627 tag
= die__create_new_btf_type_tag_ptr_type(die
, cu
);
1632 /* Create a btf_type_tag type for this annotation. */
1633 annot
= die__create_new_btf_type_tag_type(cdie
, cu
, conf
);
1637 if (cu__table_add_tag(cu
, &annot
->tag
, &id
) < 0)
1640 struct dwarf_tag
*dtag
= tag__dwarf(&annot
->tag
);
1641 dtag
->small_id
= id
;
1642 cu__hash(cu
, &annot
->tag
);
1644 /* For a list of DW_TAG_LLVM_annotation like tag1 -> tag2 -> tag3,
1645 * the tag->tags contains tag3 -> tag2 -> tag1.
1647 list_add(&annot
->node
, &tag
->tags
);
1648 } while (dwarf_siblingof(cdie
, cdie
) == 0);
1650 return tag
? &tag
->tag
: tag__new(die
, cu
);
1653 static struct tag
*die__create_new_ptr_to_member_type(Dwarf_Die
*die
,
1656 struct ptr_to_member_type
*ptr
= ptr_to_member_type__new(die
, cu
);
1658 return ptr
? &ptr
->tag
: NULL
;
1661 static int die__process_class(Dwarf_Die
*die
,
1662 struct type
*class, struct cu
*cu
, struct conf_load
*conf
);
1664 static struct tag
*die__create_new_class(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1667 struct class *class = class__new(die
, cu
, conf
);
1669 if (class != NULL
&&
1670 dwarf_haschildren(die
) != 0 &&
1671 dwarf_child(die
, &child
) == 0) {
1672 if (die__process_class(&child
, &class->type
, cu
, conf
) != 0) {
1673 class__delete(class, cu
);
1678 return class ? &class->type
.namespace.tag
: NULL
;
1681 static int die__process_namespace(Dwarf_Die
*die
, struct namespace *namespace,
1682 struct cu
*cu
, struct conf_load
*conf
);
1684 static struct tag
*die__create_new_namespace(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1687 struct namespace *namespace = namespace__new(die
, cu
, conf
);
1689 if (namespace != NULL
&&
1690 dwarf_haschildren(die
) != 0 &&
1691 dwarf_child(die
, &child
) == 0) {
1692 if (die__process_namespace(&child
, namespace, cu
, conf
) != 0) {
1693 namespace__delete(namespace, cu
);
1698 return namespace ? &namespace->tag
: NULL
;
1701 static struct tag
*die__create_new_union(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1704 struct type
*utype
= type__new(die
, cu
, conf
);
1706 if (utype
!= NULL
&&
1707 dwarf_haschildren(die
) != 0 &&
1708 dwarf_child(die
, &child
) == 0) {
1709 if (die__process_class(&child
, utype
, cu
, conf
) != 0) {
1710 type__delete(utype
, cu
);
1715 return utype
? &utype
->namespace.tag
: NULL
;
1718 static struct tag
*die__create_new_base_type(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1720 struct base_type
*base
= base_type__new(die
, cu
, conf
);
1725 if (dwarf_haschildren(die
))
1726 fprintf(stderr
, "%s: DW_TAG_base_type WITH children!\n",
1732 static struct tag
*die__create_new_typedef(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1734 struct type
*tdef
= type__new(die
, cu
, conf
);
1739 if (add_child_llvm_annotations(die
, -1, conf
, &tdef
->namespace.annots
))
1742 return &tdef
->namespace.tag
;
1745 static struct tag
*die__create_new_array(Dwarf_Die
*die
, struct cu
*cu
)
1748 /* "64 dimensions will be enough for everybody." acme, 2006 */
1749 const uint8_t max_dimensions
= 64;
1750 uint32_t nr_entries
[max_dimensions
];
1751 struct array_type
*array
= array_type__new(die
, cu
);
1756 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1761 if (dwarf_tag(die
) == DW_TAG_subrange_type
) {
1762 nr_entries
[array
->dimensions
++] = attr_upper_bound(die
);
1763 if (array
->dimensions
== max_dimensions
) {
1764 fprintf(stderr
, "%s: only %u dimensions are "
1766 __FUNCTION__
, max_dimensions
);
1770 cu__tag_not_handled(cu
, die
);
1771 } while (dwarf_siblingof(die
, die
) == 0);
1773 array
->nr_entries
= memdup(nr_entries
,
1774 array
->dimensions
* sizeof(uint32_t), cu
);
1775 if (array
->nr_entries
== NULL
)
1780 tag__free(&array
->tag
, cu
);
1784 static struct tag
*die__create_new_string_type(Dwarf_Die
*die
, struct cu
*cu
)
1786 struct string_type
*string
= string_type__new(die
, cu
);
1791 return &string
->tag
;
1794 static struct tag
*die__create_new_parameter(Dwarf_Die
*die
,
1795 struct ftype
*ftype
,
1796 struct lexblock
*lexblock
,
1797 struct cu
*cu
, struct conf_load
*conf
,
1800 struct parameter
*parm
= parameter__new(die
, cu
, conf
, param_idx
);
1805 if (ftype
!= NULL
) {
1806 ftype__add_parameter(ftype
, parm
);
1807 if (param_idx
>= 0) {
1808 if (add_child_llvm_annotations(die
, param_idx
, conf
, &(tag__function(&ftype
->tag
)->annots
)))
1813 * DW_TAG_formal_parameters on a non DW_TAG_subprogram nor
1814 * DW_TAG_subroutine_type tag happens sometimes, likely due to
1815 * compiler optimizing away a inline expansion (at least this
1816 * was observed in some cases, such as in the Linux kernel
1817 * current_kernel_time function circa 2.6.20-rc5), keep it in
1818 * the lexblock tag list because it can be referenced as an
1819 * DW_AT_abstract_origin in another DW_TAG_formal_parameter.
1821 lexblock__add_tag(lexblock
, &parm
->tag
);
1827 static struct tag
*die__create_new_label(Dwarf_Die
*die
,
1828 struct lexblock
*lexblock
,
1829 struct cu
*cu
, struct conf_load
*conf
)
1831 struct label
*label
= label__new(die
, cu
, conf
);
1836 if (lexblock
!= NULL
) {
1837 // asm CUs have labels and they will be in the cu top level tag list
1838 // See die__process_unit()
1839 lexblock__add_label(lexblock
, label
);
1842 return &label
->ip
.tag
;
1845 static struct tag
*die__create_new_variable(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
, int top_level
)
1847 struct variable
*var
= variable__new(die
, cu
, conf
, top_level
);
1849 if (var
== NULL
|| add_child_llvm_annotations(die
, -1, conf
, &var
->annots
))
1852 return &var
->ip
.tag
;
1855 static struct tag
*die__create_new_constant(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1857 struct constant
*constant
= constant__new(die
, cu
, conf
);
1859 if (constant
== NULL
)
1862 return &constant
->tag
;
1865 static struct tag
*die__create_new_subroutine_type(Dwarf_Die
*die
,
1866 struct cu
*cu
, struct conf_load
*conf
)
1869 struct ftype
*ftype
= ftype__new(die
, cu
);
1875 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1882 switch (dwarf_tag(die
)) {
1883 case DW_TAG_subrange_type
: // ADA stuff
1884 tag__print_not_supported(die
);
1886 case DW_TAG_formal_parameter
:
1887 tag
= die__create_new_parameter(die
, ftype
, NULL
, cu
, conf
, -1);
1889 case DW_TAG_unspecified_parameters
:
1890 ftype
->unspec_parms
= 1;
1893 tag
= die__process_tag(die
, cu
, 0, conf
);
1897 if (tag
== &unsupported_tag
) {
1898 tag__print_not_supported(die
);
1902 if (cu__add_tag(cu
, tag
, &id
) < 0)
1903 goto out_delete_tag
;
1911 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
1912 goto out_delete_tag
;
1915 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
1916 dtag
->small_id
= id
;
1917 } while (dwarf_siblingof(die
, die
) == 0);
1921 tag__delete(tag
, cu
);
1923 ftype__delete(ftype
, cu
);
1927 static struct tag
*die__create_new_enumeration(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1930 struct type
*enumeration
= type__new(die
, cu
, conf
);
1932 if (enumeration
== NULL
)
1935 if (enumeration
->size
== 0)
1936 enumeration
->size
= sizeof(int) * 8;
1938 enumeration
->size
*= 8;
1940 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0) {
1941 /* Seen on libQtCore.so.4.3.4.debug,
1942 * class QAbstractFileEngineIterator, enum EntryInfoType */
1948 struct enumerator
*enumerator
;
1950 if (dwarf_tag(die
) != DW_TAG_enumerator
) {
1951 cu__tag_not_handled(cu
, die
);
1954 enumerator
= enumerator__new(die
, cu
, conf
);
1955 if (enumerator
== NULL
)
1958 enumeration__add(enumeration
, enumerator
);
1959 cu__hash(cu
, &enumerator
->tag
);
1960 } while (dwarf_siblingof(die
, die
) == 0);
1962 return &enumeration
->namespace.tag
;
1964 enumeration__delete(enumeration
, cu
);
1968 static int die__process_class(Dwarf_Die
*die
, struct type
*class,
1969 struct cu
*cu
, struct conf_load
*conf
)
1971 const bool is_union
= tag__is_union(&class->namespace.tag
);
1975 switch (dwarf_tag(die
)) {
1976 #ifdef STB_GNU_UNIQUE
1977 case DW_TAG_GNU_template_parameter_pack
:
1978 class->template_parameter_pack
= template_parameter_pack__new(die
, cu
, conf
);
1980 if (class->template_parameter_pack
== NULL
)
1984 case DW_TAG_GNU_formal_parameter_pack
:
1985 case DW_TAG_GNU_template_template_param
:
1987 case DW_TAG_subrange_type
: // XXX: ADA stuff, its a type tho, will have other entries referencing it...
1988 case DW_TAG_variant_part
: // XXX: Rust stuff
1989 tag__print_not_supported(die
);
1991 case DW_TAG_template_type_parameter
: {
1992 struct template_type_param
*ttparm
= template_type_param__new(die
, cu
, conf
);
1997 type__add_template_type_param(class, ttparm
);
2000 case DW_TAG_template_value_parameter
: {
2002 * FIXME: probably we'll have to attach this as a list of
2003 * template parameters to use at class__fprintf time...
2006 * https://gcc.gnu.org/wiki/TemplateParmsDwarf
2008 struct template_value_param
*tvparm
= template_value_param__new(die
, cu
, conf
);
2013 type__add_template_value_param(class, tvparm
);
2016 case DW_TAG_inheritance
:
2017 case DW_TAG_member
: {
2018 struct class_member
*member
= class_member__new(die
, cu
, is_union
, conf
);
2023 if (cu__is_c_plus_plus(cu
)) {
2026 if (cu__table_add_tag(cu
, &member
->tag
, &id
) < 0) {
2027 class_member__delete(member
, cu
);
2031 struct dwarf_tag
*dtag
= tag__dwarf(&member
->tag
);
2032 dtag
->small_id
= id
;
2035 type__add_member(class, member
);
2036 cu__hash(cu
, &member
->tag
);
2037 if (add_child_llvm_annotations(die
, member_idx
, conf
, &class->namespace.annots
))
2042 case DW_TAG_LLVM_annotation
:
2043 if (add_llvm_annotation(die
, -1, conf
, &class->namespace.annots
))
2047 struct tag
*tag
= die__process_tag(die
, cu
, 0, conf
);
2052 if (tag
== &unsupported_tag
) {
2053 tag__print_not_supported(die
);
2059 if (cu__table_add_tag(cu
, tag
, &id
) < 0) {
2060 tag__delete(tag
, cu
);
2064 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2065 dtag
->small_id
= id
;
2067 namespace__add_tag(&class->namespace, tag
);
2069 if (tag__is_function(tag
)) {
2070 struct function
*fself
= tag__function(tag
);
2072 if (fself
->vtable_entry
!= -1)
2073 class__add_vtable_entry(type__class(class), fself
);
2078 } while (dwarf_siblingof(die
, die
) == 0);
2083 static int die__process_namespace(Dwarf_Die
*die
, struct namespace *namespace,
2084 struct cu
*cu
, struct conf_load
*conf
)
2088 tag
= die__process_tag(die
, cu
, 0, conf
);
2092 if (tag
== &unsupported_tag
) {
2093 tag__print_not_supported(die
);
2098 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
2099 goto out_delete_tag
;
2101 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2102 dtag
->small_id
= id
;
2104 namespace__add_tag(namespace, tag
);
2106 } while (dwarf_siblingof(die
, die
) == 0);
2110 tag__delete(tag
, cu
);
2115 static int die__process_function(Dwarf_Die
*die
, struct ftype
*ftype
,
2116 struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
);
2118 static int die__create_new_lexblock(Dwarf_Die
*die
,
2119 struct cu
*cu
, struct lexblock
*father
, struct conf_load
*conf
)
2121 struct lexblock
*lexblock
= lexblock__new(die
, cu
);
2123 if (lexblock
!= NULL
) {
2124 if (die__process_function(die
, NULL
, lexblock
, cu
, conf
) != 0)
2128 lexblock__add_lexblock(father
, lexblock
);
2131 lexblock__delete(lexblock
, cu
);
2135 static struct tag
*die__create_new_inline_expansion(Dwarf_Die
*die
,
2136 struct lexblock
*lexblock
,
2137 struct cu
*cu
, struct conf_load
*conf
);
2139 static int die__process_inline_expansion(Dwarf_Die
*die
, struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
)
2144 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
2151 switch (dwarf_tag(die
)) {
2152 case DW_TAG_call_site
:
2153 case DW_TAG_call_site_parameter
:
2154 case DW_TAG_GNU_call_site
:
2155 case DW_TAG_GNU_call_site_parameter
:
2157 * FIXME: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
2158 * and write proper support.
2160 * From a quick read there is not much we can use in
2161 * the existing dwarves tools, so just stop warning the user,
2162 * developers will find these notes if wanting to use in a
2166 case DW_TAG_lexical_block
:
2167 if (die__create_new_lexblock(die
, cu
, lexblock
, conf
) != 0)
2170 case DW_TAG_formal_parameter
:
2173 * So far DW_TAG_inline_routine had just an
2174 * abstract origin, but starting with
2175 * /usr/lib/openoffice.org/basis3.0/program/libdbalx.so
2176 * I realized it really has to be handled as a
2177 * DW_TAG_function... Lets just get the types
2178 * for 1.8, then fix this properly.
2180 * cu__tag_not_handled(cu, die);
2183 case DW_TAG_inlined_subroutine
:
2184 tag
= die__create_new_inline_expansion(die
, lexblock
, cu
, conf
);
2187 if (conf
->ignore_labels
)
2189 tag
= die__create_new_label(die
, lexblock
, cu
, conf
);
2192 tag
= die__process_tag(die
, cu
, 0, conf
);
2196 if (tag
== &unsupported_tag
) {
2197 tag__print_not_supported(die
);
2201 if (cu__add_tag(cu
, tag
, &id
) < 0)
2202 goto out_delete_tag
;
2209 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
2210 goto out_delete_tag
;
2213 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2214 dtag
->small_id
= id
;
2215 } while (dwarf_siblingof(die
, die
) == 0);
2219 tag__delete(tag
, cu
);
2224 static struct tag
*die__create_new_inline_expansion(Dwarf_Die
*die
,
2225 struct lexblock
*lexblock
,
2226 struct cu
*cu
, struct conf_load
*conf
)
2228 struct inline_expansion
*exp
= inline_expansion__new(die
, cu
, conf
);
2233 if (die__process_inline_expansion(die
, lexblock
, cu
, conf
) != 0) {
2234 tag__free(&exp
->ip
.tag
, cu
);
2238 if (lexblock
!= NULL
)
2239 lexblock__add_inline_expansion(lexblock
, exp
);
2240 return &exp
->ip
.tag
;
2243 static int die__process_function(Dwarf_Die
*die
, struct ftype
*ftype
,
2244 struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
)
2250 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
2257 switch (dwarf_tag(die
)) {
2258 case DW_TAG_call_site
:
2259 case DW_TAG_call_site_parameter
:
2260 case DW_TAG_GNU_call_site
:
2261 case DW_TAG_GNU_call_site_parameter
:
2263 * XXX: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
2264 * and write proper support.
2266 * From a quick read there is not much we can use in
2267 * the existing dwarves tools, so just stop warning the user,
2268 * developers will find these notes if wanting to use in a
2272 case DW_TAG_dwarf_procedure
:
2274 * Ignore it, just scope expressions, that we have no use for (so far).
2277 #ifdef STB_GNU_UNIQUE
2278 case DW_TAG_GNU_template_parameter_pack
:
2279 ftype
->template_parameter_pack
= template_parameter_pack__new(die
, cu
, conf
);
2281 if (ftype
->template_parameter_pack
== NULL
)
2285 case DW_TAG_GNU_formal_parameter_pack
:
2286 ftype
->formal_parameter_pack
= formal_parameter_pack__new(die
, cu
, conf
);
2288 if (ftype
->formal_parameter_pack
== NULL
)
2292 case DW_TAG_GNU_template_template_param
:
2294 tag__print_not_supported(die
);
2296 case DW_TAG_template_type_parameter
: {
2297 struct template_type_param
*ttparm
= template_type_param__new(die
, cu
, conf
);
2302 ftype__add_template_type_param(ftype
, ttparm
);
2305 case DW_TAG_template_value_parameter
: {
2306 /* FIXME: probably we'll have to attach this as a list of
2307 * template parameters to use at class__fprintf time...
2308 * See die__process_class */
2309 struct template_value_param
*tvparm
= template_value_param__new(die
, cu
, conf
);
2314 ftype__add_template_value_param(ftype
, tvparm
);
2317 case DW_TAG_formal_parameter
:
2318 tag
= die__create_new_parameter(die
, ftype
, lexblock
, cu
, conf
, param_idx
++);
2320 case DW_TAG_variable
:
2321 tag
= die__create_new_variable(die
, cu
, conf
, 0);
2324 lexblock__add_variable(lexblock
, tag__variable(tag
));
2326 case DW_TAG_unspecified_parameters
:
2328 ftype
->unspec_parms
= 1;
2331 if (conf
->ignore_labels
)
2333 tag
= die__create_new_label(die
, lexblock
, cu
, conf
);
2335 case DW_TAG_inlined_subroutine
:
2336 if (conf
->ignore_inline_expansions
)
2338 tag
= die__create_new_inline_expansion(die
, lexblock
, cu
, conf
);
2340 case DW_TAG_lexical_block
:
2341 // lexblocks can contain types that are then referenced from outside.
2342 // Thus we can't ignore them without more surgery, i.e. by adding code
2343 // to just process types inside lexblocks, leave this for later.
2344 if (die__create_new_lexblock(die
, cu
, lexblock
, conf
) != 0)
2347 case DW_TAG_LLVM_annotation
:
2348 if (add_llvm_annotation(die
, -1, conf
, &(tag__function(&ftype
->tag
)->annots
)))
2352 tag
= die__process_tag(die
, cu
, 0, conf
);
2357 if (tag
== &unsupported_tag
) {
2358 tag__print_not_supported(die
);
2362 if (cu__add_tag(cu
, tag
, &id
) < 0)
2363 goto out_delete_tag
;
2371 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
2372 goto out_delete_tag
;
2375 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2376 dtag
->small_id
= id
;
2377 } while (dwarf_siblingof(die
, die
) == 0);
2381 tag__delete(tag
, cu
);
2386 static struct tag
*die__create_new_function(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2388 struct function
*function
= function__new(die
, cu
, conf
);
2390 if (function
!= NULL
&&
2391 die__process_function(die
, &function
->proto
, &function
->lexblock
, cu
, conf
) != 0) {
2392 function__delete(function
, cu
);
2396 return function
? &function
->proto
.tag
: NULL
;
2399 static struct tag
*__die__process_tag(Dwarf_Die
*die
, struct cu
*cu
,
2400 int top_level
, const char *fn
, struct conf_load
*conf
)
2404 switch (dwarf_tag(die
)) {
2405 case DW_TAG_imported_unit
:
2406 return NULL
; // We don't support imported units yet, so to avoid segfaults
2407 case DW_TAG_array_type
:
2408 tag
= die__create_new_array(die
, cu
); break;
2409 case DW_TAG_string_type
: // FORTRAN stuff, looks like an array
2410 tag
= die__create_new_string_type(die
, cu
); break;
2411 case DW_TAG_base_type
:
2412 tag
= die__create_new_base_type(die
, cu
, conf
); break;
2413 case DW_TAG_const_type
:
2414 case DW_TAG_imported_declaration
:
2415 case DW_TAG_imported_module
:
2416 case DW_TAG_reference_type
:
2417 case DW_TAG_restrict_type
:
2418 case DW_TAG_volatile_type
:
2419 case DW_TAG_atomic_type
:
2420 tag
= die__create_new_tag(die
, cu
); break;
2421 case DW_TAG_unspecified_type
:
2422 tag
= die__create_new_tag(die
, cu
); break;
2423 case DW_TAG_pointer_type
:
2424 tag
= die__create_new_pointer_tag(die
, cu
, conf
); break;
2425 case DW_TAG_ptr_to_member_type
:
2426 tag
= die__create_new_ptr_to_member_type(die
, cu
); break;
2427 case DW_TAG_enumeration_type
:
2428 tag
= die__create_new_enumeration(die
, cu
, conf
); break;
2429 case DW_TAG_namespace
:
2430 tag
= die__create_new_namespace(die
, cu
, conf
); break;
2431 case DW_TAG_class_type
:
2432 case DW_TAG_interface_type
:
2433 case DW_TAG_structure_type
:
2434 tag
= die__create_new_class(die
, cu
, conf
); break;
2435 case DW_TAG_subprogram
:
2436 tag
= die__create_new_function(die
, cu
, conf
); break;
2437 case DW_TAG_subroutine_type
:
2438 tag
= die__create_new_subroutine_type(die
, cu
, conf
); break;
2439 case DW_TAG_rvalue_reference_type
:
2440 case DW_TAG_typedef
:
2441 tag
= die__create_new_typedef(die
, cu
, conf
); break;
2442 case DW_TAG_union_type
:
2443 tag
= die__create_new_union(die
, cu
, conf
); break;
2444 case DW_TAG_variable
:
2445 tag
= die__create_new_variable(die
, cu
, conf
, top_level
); break;
2446 case DW_TAG_constant
: // First seen in a Go CU
2447 tag
= die__create_new_constant(die
, cu
, conf
); break;
2449 __cu__tag_not_handled(cu
, die
, fn
);
2451 case DW_TAG_dwarf_procedure
:
2453 * Ignore it, just scope expressions, that we have no use for (so far).
2455 tag
= &unsupported_tag
;
2458 if (conf
->ignore_labels
)
2459 tag
= &unsupported_tag
; // callers will assume conf->ignore_labels is true
2460 else // We can have labels in asm CUs, no lexblock
2461 tag
= die__create_new_label(die
, NULL
, cu
, conf
);
2466 tag
->top_level
= top_level
;
2471 static int die__process_unit(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2474 struct tag
*tag
= die__process_tag(die
, cu
, 1, conf
);
2478 if (tag
== &unsupported_tag
) {
2479 // XXX special case DW_TAG_dwarf_procedure, appears when looking at a recent ~/bin/perf
2480 // Investigate later how to properly support this...
2481 if (dwarf_tag(die
) != DW_TAG_dwarf_procedure
&&
2482 dwarf_tag(die
) != DW_TAG_label
) // conf->ignore_labels == true, see die__process_tag()
2483 tag__print_not_supported(die
);
2488 /* There is no BTF representation for unspecified types.
2489 * Currently we want such types to be represented as `void`
2490 * (and thus skip BTF encoding).
2492 * As BTF encoding is skipped, such types must not be added to type table,
2493 * otherwise an ID for a type would be allocated and we would be forced
2494 * to put something in BTF at this ID.
2495 * Thus avoid `cu__add_tag()` call for such types.
2497 * On the other hand, there might be references to this type from other
2498 * tags, so `dwarf_cu__find_tag_by_ref()` must return something.
2499 * Thus call `cu__hash()` for such types.
2501 * Note, that small_id of zero would be assigned to unspecified type entry.
2503 if (tag
->tag
!= DW_TAG_unspecified_type
)
2504 cu__add_tag(cu
, tag
, &id
);
2506 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2507 dtag
->small_id
= id
;
2508 } while (dwarf_siblingof(die
, die
) == 0);
2513 static void ftype__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
);
2515 static int namespace__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
)
2518 struct dwarf_cu
*dcu
= cu
->priv
;
2519 struct namespace *ns
= tag__namespace(tag
);
2521 namespace__for_each_tag(ns
, pos
) {
2522 struct dwarf_tag
*dtype
;
2523 struct dwarf_tag
*dpos
= tag__dwarf(pos
);
2525 if (tag__has_namespace(pos
)) {
2526 if (namespace__recode_dwarf_types(pos
, cu
))
2532 case DW_TAG_member
: {
2533 struct class_member
*member
= tag__class_member(pos
);
2535 * We may need to recode the type, possibly creating a
2536 * suitably sized new base_type
2538 if (member
->bitfield_size
!= 0 && !no_bitfield_type_recode
) {
2539 if (class_member__dwarf_recode_bitfield(member
, cu
))
2545 case DW_TAG_subroutine_type
:
2546 case DW_TAG_subprogram
:
2547 ftype__recode_dwarf_types(pos
, cu
);
2549 case DW_TAG_imported_module
:
2550 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, type
);
2552 /* Can be for both types and non types */
2553 case DW_TAG_imported_declaration
:
2554 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, type
);
2560 if (dpos
->type
== 0) /* void */
2563 dtype
= dwarf_cu__find_type_by_ref(dcu
, dpos
, type
);
2565 if (dtype
== NULL
) {
2566 tag__print_type_not_found(pos
);
2570 pos
->type
= dtype
->small_id
;
2575 static void type__recode_dwarf_specification(struct tag
*tag
, struct cu
*cu
)
2577 struct dwarf_tag
*dtype
;
2578 struct type
*t
= tag__type(tag
);
2579 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2581 if (t
->namespace.name
!= 0 || dtag
->specification
== 0)
2584 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, specification
);
2586 t
->namespace.name
= tag__namespace(dtag__tag(dtype
))->name
;
2589 "%s: couldn't find name for "
2590 "class %#llx, specification=%#llx\n", __func__
,
2591 (unsigned long long)dtag
->id
,
2592 (unsigned long long)dtag
->specification
);
2596 static void __tag__print_abstract_origin_not_found(struct tag
*tag
,
2597 const char *func
, int line
)
2599 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2601 "%s(%d): couldn't find %#llx abstract_origin for %#llx (%s)!\n",
2602 func
, line
, (unsigned long long)dtag
->abstract_origin
,
2603 (unsigned long long)dtag
->id
,
2604 dwarf_tag_name(tag
->tag
));
2607 #define tag__print_abstract_origin_not_found(tag) \
2608 __tag__print_abstract_origin_not_found(tag, __func__, __LINE__)
2610 static void ftype__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
)
2612 struct parameter
*pos
;
2613 struct dwarf_cu
*dcu
= cu
->priv
;
2614 struct ftype
*type
= tag__ftype(tag
);
2616 ftype__for_each_parameter(type
, pos
) {
2617 struct dwarf_tag
*dpos
= tag__dwarf(&pos
->tag
);
2618 struct parameter
*opos
;
2619 struct dwarf_tag
*dtype
;
2621 if (dpos
->type
== 0) {
2622 if (dpos
->abstract_origin
== 0) {
2623 /* Function without parameters */
2627 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, abstract_origin
);
2628 if (dtype
== NULL
) {
2629 tag__print_abstract_origin_not_found(&pos
->tag
);
2632 opos
= tag__parameter(dtag__tag(dtype
));
2633 pos
->name
= opos
->name
;
2634 pos
->tag
.type
= dtag__tag(dtype
)->type
;
2635 /* share location information between parameter and
2636 * abstract origin; if neither have location, we will
2637 * mark the parameter as optimized out. Also share
2638 * info regarding unexpected register use for
2642 opos
->has_loc
= pos
->has_loc
;
2645 opos
->optimized
= pos
->optimized
;
2646 if (pos
->unexpected_reg
)
2647 opos
->unexpected_reg
= pos
->unexpected_reg
;
2651 dtype
= dwarf_cu__find_type_by_ref(dcu
, dpos
, type
);
2652 if (dtype
== NULL
) {
2653 tag__print_type_not_found(&pos
->tag
);
2656 pos
->tag
.type
= dtype
->small_id
;
2660 static void lexblock__recode_dwarf_types(struct lexblock
*tag
, struct cu
*cu
)
2663 struct dwarf_cu
*dcu
= cu
->priv
;
2665 list_for_each_entry(pos
, &tag
->tags
, node
) {
2666 struct dwarf_tag
*dpos
= tag__dwarf(pos
);
2667 struct dwarf_tag
*dtype
;
2670 case DW_TAG_lexical_block
:
2671 lexblock__recode_dwarf_types(tag__lexblock(pos
), cu
);
2673 case DW_TAG_inlined_subroutine
:
2674 if (dpos
->type
!= 0)
2675 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, type
);
2677 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, abstract_origin
);
2678 if (dtype
== NULL
) {
2679 if (dpos
->type
!= 0)
2680 tag__print_type_not_found(pos
);
2682 tag__print_abstract_origin_not_found(pos
);
2685 ftype__recode_dwarf_types(dtag__tag(dtype
), cu
);
2688 case DW_TAG_formal_parameter
:
2689 if (dpos
->type
!= 0)
2692 struct parameter
*fp
= tag__parameter(pos
);
2693 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, abstract_origin
);
2694 if (dtype
== NULL
) {
2695 tag__print_abstract_origin_not_found(pos
);
2698 fp
->name
= tag__parameter(dtag__tag(dtype
))->name
;
2699 pos
->type
= dtag__tag(dtype
)->type
;
2702 case DW_TAG_variable
:
2703 if (dpos
->type
!= 0)
2706 struct variable
*var
= tag__variable(pos
);
2708 if (dpos
->abstract_origin
== 0) {
2710 * DW_TAG_variable completely empty was
2711 * found on libQtGui.so.4.3.4.debug
2712 * <3><d6ea1>: Abbrev Number: 164 (DW_TAG_variable)
2717 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, abstract_origin
);
2718 if (dtype
== NULL
) {
2719 tag__print_abstract_origin_not_found(pos
);
2722 var
->name
= tag__variable(dtag__tag(dtype
))->name
;
2723 pos
->type
= dtag__tag(dtype
)->type
;
2726 case DW_TAG_label
: {
2727 struct label
*l
= tag__label(pos
);
2729 if (dpos
->abstract_origin
== 0)
2732 dtype
= dwarf_cu__find_tag_by_ref(dcu
, dpos
, abstract_origin
);
2734 l
->name
= tag__label(dtag__tag(dtype
))->name
;
2736 tag__print_abstract_origin_not_found(pos
);
2741 dtype
= dwarf_cu__find_type_by_ref(dcu
, dpos
, type
);
2742 if (dtype
== NULL
) {
2743 tag__print_type_not_found(pos
);
2746 pos
->type
= dtype
->small_id
;
2750 static void dwarf_cu__recode_btf_type_tag_ptr(struct btf_type_tag_ptr_type
*tag
,
2751 uint32_t pointee_type
)
2753 struct btf_type_tag_type
*annot
;
2754 struct dwarf_tag
*annot_dtag
;
2755 struct tag
*prev_tag
;
2757 /* Given source like
2758 * int tag1 tag2 tag3 *p;
2759 * the tag->tags contains tag3 -> tag2 -> tag1, the final type chain looks like:
2760 * pointer -> tag3 -> tag2 -> tag1 -> pointee
2762 * Basically it means
2763 * - '*' applies to "int tag1 tag2 tag3"
2764 * - tag3 applies to "int tag1 tag2"
2765 * - tag2 applies to "int tag1"
2766 * - tag1 applies to "int"
2768 * This also makes final source code (format c) easier as we can do
2769 * emit for "tag3 -> tag2 -> tag1 -> int"
2772 * For 'tag3 -> tag2 -> tag1 -> int":
2773 * emit for "tag2 -> tag1 -> int"
2776 * Eventually we can get the source code like
2777 * int tag1 tag2 tag3 *p;
2778 * and this matches the user/kernel code.
2780 prev_tag
= &tag
->tag
;
2781 list_for_each_entry(annot
, &tag
->tags
, node
) {
2782 annot_dtag
= tag__dwarf(&annot
->tag
);
2783 prev_tag
->type
= annot_dtag
->small_id
;
2784 prev_tag
= &annot
->tag
;
2786 prev_tag
->type
= pointee_type
;
2789 static int tag__recode_dwarf_type(struct tag
*tag
, struct cu
*cu
)
2791 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2792 struct dwarf_tag
*dtype
;
2794 /* Check if this is an already recoded bitfield */
2798 if (tag__is_type(tag
))
2799 type__recode_dwarf_specification(tag
, cu
);
2801 if (tag__has_namespace(tag
))
2802 return namespace__recode_dwarf_types(tag
, cu
);
2805 case DW_TAG_subprogram
: {
2806 struct function
*fn
= tag__function(tag
);
2808 if (fn
->name
== 0) {
2809 if (dtag
->abstract_origin
== 0 &&
2810 dtag
->specification
== 0) {
2812 * Found on libQtGui.so.4.3.4.debug
2813 * <3><1423de>: Abbrev Number: 209 (DW_TAG_subprogram)
2814 * <1423e0> DW_AT_declaration : 1
2818 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, abstract_origin
);
2820 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, specification
);
2822 fn
->name
= tag__function(dtag__tag(dtype
))->name
;
2825 "%s: couldn't find name for "
2826 "function %#llx, abstract_origin=%#llx,"
2827 " specification=%#llx\n", __func__
,
2828 (unsigned long long)dtag
->id
,
2829 (unsigned long long)dtag
->abstract_origin
,
2830 (unsigned long long)dtag
->specification
);
2833 lexblock__recode_dwarf_types(&fn
->lexblock
, cu
);
2837 case DW_TAG_subroutine_type
:
2838 ftype__recode_dwarf_types(tag
, cu
);
2839 /* Fall thru, for the function return type */
2842 case DW_TAG_lexical_block
:
2843 lexblock__recode_dwarf_types(tag__lexblock(tag
), cu
);
2846 case DW_TAG_ptr_to_member_type
: {
2847 struct ptr_to_member_type
*pt
= tag__ptr_to_member_type(tag
);
2849 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, containing_type
);
2851 pt
->containing_type
= dtype
->small_id
;
2854 "%s: couldn't find type for "
2855 "containing_type %#llx, containing_type=%#llx\n",
2857 (unsigned long long)dtag
->id
,
2858 (unsigned long long)dtag
->containing_type
);
2863 case DW_TAG_namespace
:
2864 return namespace__recode_dwarf_types(tag
, cu
);
2865 /* Damn, DW_TAG_inlined_subroutine is an special case
2866 as dwarf_tag->id is in fact an abtract origin, i.e. must be
2867 looked up in the tags_table, not in the types_table.
2868 The others also point to routines, so are in tags_table */
2869 case DW_TAG_inlined_subroutine
:
2870 case DW_TAG_imported_module
:
2871 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, type
);
2873 /* Can be for both types and non types */
2874 case DW_TAG_imported_declaration
:
2875 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, type
);
2879 case DW_TAG_variable
: {
2880 struct variable
*var
= tag__variable(tag
);
2882 if (var
->has_specification
) {
2883 if (dtag
->specification
) {
2884 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, specification
);
2886 var
->spec
= tag__variable(dtag__tag(dtype
));
2893 if (dtag
->type
== 0) {
2894 if (tag
->tag
!= DW_TAG_pointer_type
|| !tag
->has_btf_type_tag
)
2895 tag
->type
= 0; /* void */
2897 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag
), 0);
2902 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, dtag
, type
);
2904 if (dtype
== NULL
) {
2905 tag__print_type_not_found(tag
);
2909 if (tag
->tag
!= DW_TAG_pointer_type
|| !tag
->has_btf_type_tag
)
2910 tag
->type
= dtype
->small_id
;
2912 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag
), dtype
->small_id
);
2917 static bool param__is_struct(struct cu
*cu
, struct tag
*tag
)
2919 struct tag
*type
= cu__type(cu
, tag
->type
);
2924 switch (type
->tag
) {
2925 case DW_TAG_structure_type
:
2927 case DW_TAG_const_type
:
2928 case DW_TAG_typedef
:
2929 /* handle "typedef struct", const parameter */
2930 return param__is_struct(cu
, type
);
2936 static int cu__resolve_func_ret_types_optimized(struct cu
*cu
)
2938 struct ptr_table
*pt
= &cu
->functions_table
;
2941 for (i
= 0; i
< pt
->nr_entries
; ++i
) {
2942 struct tag
*tag
= pt
->entries
[i
];
2943 struct parameter
*pos
;
2944 struct function
*fn
= tag__function(tag
);
2945 bool has_unexpected_reg
= false, has_struct_param
= false;
2947 /* mark function as optimized if parameter is, or
2948 * if parameter does not have a location; at this
2949 * point location presence has been marked in
2950 * abstract origins for cases where a parameter
2951 * location is not stored in the original function
2954 * Also mark functions which, due to optimization,
2955 * use an unexpected register for a parameter.
2956 * Exception is functions which have a struct
2957 * as a parameter, as multiple registers may
2958 * be used to represent it, throwing off register
2959 * to parameter mapping.
2961 ftype__for_each_parameter(&fn
->proto
, pos
) {
2962 if (pos
->optimized
|| !pos
->has_loc
)
2963 fn
->proto
.optimized_parms
= 1;
2965 if (pos
->unexpected_reg
)
2966 has_unexpected_reg
= true;
2968 if (has_unexpected_reg
) {
2969 ftype__for_each_parameter(&fn
->proto
, pos
) {
2970 has_struct_param
= param__is_struct(cu
, &pos
->tag
);
2971 if (has_struct_param
)
2974 if (!has_struct_param
)
2975 fn
->proto
.unexpected_reg
= 1;
2978 if (tag
== NULL
|| tag
->type
!= 0)
2981 if (!fn
->abstract_origin
)
2984 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
2985 struct dwarf_tag
*dfunc
;
2986 dfunc
= dwarf_cu__find_tag_by_ref(cu
->priv
, dtag
, abstract_origin
);
2987 if (dfunc
== NULL
) {
2988 tag__print_abstract_origin_not_found(tag
);
2992 tag
->type
= dtag__tag(dfunc
)->type
;
2997 static int cu__recode_dwarf_types_table(struct cu
*cu
,
2998 struct ptr_table
*pt
,
3001 for (; i
< pt
->nr_entries
; ++i
) {
3002 struct tag
*tag
= pt
->entries
[i
];
3004 if (tag
!= NULL
) /* void, see cu__new */
3005 if (tag__recode_dwarf_type(tag
, cu
))
3012 static int cu__recode_dwarf_types(struct cu
*cu
)
3014 if (cu__recode_dwarf_types_table(cu
, &cu
->types_table
, 1) ||
3015 cu__recode_dwarf_types_table(cu
, &cu
->tags_table
, 0) ||
3016 cu__recode_dwarf_types_table(cu
, &cu
->functions_table
, 0))
3021 static const char *dwarf_tag__decl_file(const struct tag
*tag
,
3022 const struct cu
*cu
)
3024 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
3025 return cu
->extra_dbg_info
? dtag
->decl_file
: NULL
;
3028 static uint32_t dwarf_tag__decl_line(const struct tag
*tag
,
3029 const struct cu
*cu
)
3031 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
3032 return cu
->extra_dbg_info
? dtag
->decl_line
: 0;
3035 static unsigned long long dwarf_tag__orig_id(const struct tag
*tag
,
3036 const struct cu
*cu
)
3038 struct dwarf_tag
*dtag
= tag__dwarf(tag
);
3039 return cu
->extra_dbg_info
? dtag
->id
: 0;
3042 struct debug_fmt_ops dwarf__ops
;
3044 static int die__process(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
3047 const uint16_t tag
= dwarf_tag(die
);
3049 if (tag
== DW_TAG_skeleton_unit
) {
3053 fprintf(stderr
, "WARNING: DW_TAG_skeleton_unit used, please look for a .dwo file and use it instead.\n"
3054 " A future version of pahole will support do this automagically.\n");
3057 return 0; // so that other units can be processed
3060 if (tag
== DW_TAG_partial_unit
) {
3064 fprintf(stderr
, "WARNING: DW_TAG_partial_unit used, some types will not be considered!\n"
3065 " Probably this was optimized using a tool like 'dwz'\n"
3066 " A future version of pahole will support this.\n");
3069 return 0; // so that other units can be processed
3072 if (tag
!= DW_TAG_compile_unit
&& tag
!= DW_TAG_type_unit
) {
3073 fprintf(stderr
, "%s: DW_TAG_compile_unit, DW_TAG_type_unit, DW_TAG_partial_unit or DW_TAG_skeleton_unit expected got %s (0x%x) @ %llx!\n",
3074 __FUNCTION__
, dwarf_tag_name(tag
), tag
, (unsigned long long)dwarf_dieoffset(die
));
3078 cu
->language
= attr_numeric(die
, DW_AT_language
);
3080 if (conf
->early_cu_filter
)
3081 cu
= conf
->early_cu_filter(cu
);
3084 * If we filtered this CU out, we still want to keep iterating, but
3085 * there's no need to walk the rest of the CU info.
3090 if (dwarf_child(die
, &child
) == 0) {
3091 int err
= die__process_unit(&child
, cu
, conf
);
3096 if (dwarf_siblingof(die
, die
) == 0)
3097 fprintf(stderr
, "%s: got %s unexpected tag after "
3098 "DW_TAG_compile_unit!\n",
3099 __FUNCTION__
, dwarf_tag_name(tag
));
3104 static int die__process_and_recode(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
3106 int ret
= die__process(die
, cu
, conf
);
3109 ret
= cu__recode_dwarf_types(cu
);
3113 return cu__resolve_func_ret_types_optimized(cu
);
3116 static int class_member__cache_byte_size(struct tag
*tag
, struct cu
*cu
,
3119 struct class_member
*member
= tag__class_member(tag
);
3120 struct conf_load
*conf_load
= cookie
;
3122 if (tag__is_class_member(tag
)) {
3123 if (member
->is_static
)
3125 } else if (tag
->tag
!= DW_TAG_inheritance
) {
3129 if (member
->bitfield_size
== 0) {
3130 member
->byte_size
= tag__size(tag
, cu
);
3131 member
->bit_size
= member
->byte_size
* 8;
3136 * Try to figure out byte size, if it's not directly provided in DWARF
3138 if (member
->byte_size
== 0) {
3139 struct tag
*type
= tag__strip_typedefs_and_modifiers(&member
->tag
, cu
);
3140 member
->byte_size
= tag__size(type
, cu
);
3141 if (member
->byte_size
== 0) {
3143 if (tag__is_enumeration(type
)) {
3144 bit_size
= tag__type(type
)->size
;
3146 struct base_type
*bt
= tag__base_type(type
);
3147 bit_size
= bt
->bit_size
? bt
->bit_size
: base_type__name_to_size(bt
, cu
);
3149 member
->byte_size
= (bit_size
+ 7) / 8 * 8;
3152 member
->bit_size
= member
->byte_size
* 8;
3155 * XXX: after all the attempts to determine byte size, we might still
3156 * be unsuccessful, because base_type__name_to_size doesn't know about
3157 * the base_type name, so one has to add there when such base_type
3158 * isn't found. pahole will put zero on the struct output so it should
3159 * be easy to spot the name when such unlikely thing happens.
3161 if (member
->byte_size
== 0) {
3162 member
->bitfield_offset
= 0;
3166 if (!member
->has_bit_offset
) {
3168 * For little-endian architectures, DWARF data emitted by gcc/clang
3169 * specifies bitfield offset as an offset from the highest-order bit
3170 * of an underlying integral type (e.g., int) to a highest-order bit
3171 * of a bitfield. E.g., for bitfield taking first 5 bits of int-backed
3172 * bitfield, bit offset will be 27 (sizeof(int) - 0 offset - 5 bit
3173 * size), which is very counter-intuitive and isn't a natural
3174 * extension of byte offset, which on little-endian points to
3175 * lowest-order byte. So here we re-adjust bitfield offset to be an
3176 * offset from lowest-order bit of underlying integral type to
3177 * a lowest-order bit of a bitfield. This makes bitfield offset
3178 * a natural extension of byte offset for bitfields and is uniform
3179 * with how big-endian bit offsets work.
3181 if (cu
->little_endian
)
3182 member
->bitfield_offset
= member
->bit_size
- member
->bitfield_offset
- member
->bitfield_size
;
3184 member
->bit_offset
= member
->byte_offset
* 8 + member
->bitfield_offset
;
3186 // DWARF5 has DW_AT_data_bit_offset, offset in bits from the
3187 // start of the container type (struct, class, etc).
3188 member
->byte_offset
= member
->bit_offset
/ 8;
3189 member
->bitfield_offset
= member
->bit_offset
- member
->byte_offset
* 8;
3192 /* make sure bitfield offset is non-negative */
3193 if (member
->bitfield_offset
< 0) {
3194 member
->bitfield_offset
+= member
->bit_size
;
3195 member
->byte_offset
-= member
->byte_size
;
3196 member
->bit_offset
= member
->byte_offset
* 8 + member
->bitfield_offset
;
3198 /* align on underlying base type natural alignment boundary */
3199 member
->bitfield_offset
+= (member
->byte_offset
% member
->byte_size
) * 8;
3200 member
->byte_offset
= member
->bit_offset
/ member
->bit_size
* member
->bit_size
/ 8;
3201 if (member
->bitfield_offset
>= member
->bit_size
) {
3202 member
->bitfield_offset
-= member
->bit_size
;
3203 member
->byte_offset
+= member
->byte_size
;
3206 if (conf_load
&& conf_load
->fixup_silly_bitfields
&&
3207 member
->byte_size
== 8 * member
->bitfield_size
) {
3208 member
->bitfield_size
= 0;
3209 member
->bitfield_offset
= 0;
3215 static bool cu__language_reorders_offsets(const struct cu
*cu
)
3217 return cu
->language
== DW_LANG_Rust
;
3220 static int type__sort_by_offset(struct tag
*tag
, struct cu
*cu
, void *cookie __maybe_unused
)
3222 if (!tag__is_type(tag
))
3225 struct type
*type
= tag__type(tag
);
3226 struct class_member
*current_member
;
3228 // There may be more than DW_TAG_members entries in the type tags, so do a simple
3229 // bubble sort for now, so that the other non tags stay where they are.
3231 type__for_each_data_member(type
, current_member
) {
3232 if (list_is_last(¤t_member
->tag
.node
, &type
->namespace.tags
))
3235 struct class_member
*next_member
= list_entry(current_member
->tag
.node
.next
, typeof(*current_member
), tag
.node
);
3237 if (current_member
->byte_offset
<= next_member
->byte_offset
)
3240 list_del(¤t_member
->tag
.node
);
3241 list_add(¤t_member
->tag
.node
, &next_member
->tag
.node
);
3248 static void cu__sort_types_by_offset(struct cu
*cu
, struct conf_load
*conf
)
3250 cu__for_all_tags(cu
, type__sort_by_offset
, conf
);
3253 static int cu__finalize(struct cu
*cu
, struct cus
*cus
, struct conf_load
*conf
, void *thr_data
)
3255 cu__for_all_tags(cu
, class_member__cache_byte_size
, conf
);
3257 if (cu__language_reorders_offsets(cu
))
3258 cu__sort_types_by_offset(cu
, conf
);
3260 cus__set_cu_state(cus
, cu
, CU__LOADED
);
3262 if (conf
&& conf
->steal
) {
3263 return conf
->steal(cu
, conf
, thr_data
);
3268 static int cus__finalize(struct cus
*cus
, struct cu
*cu
, struct conf_load
*conf
, void *thr_data
)
3270 int lsk
= cu__finalize(cu
, cus
, conf
, thr_data
);
3273 cus__remove(cus
, cu
);
3276 case LSK__STOP_LOADING
:
3284 static int cu__set_common(struct cu
*cu
, struct conf_load
*conf
,
3285 Dwfl_Module
*mod
, Elf
*elf
)
3287 cu
->uses_global_strings
= true;
3290 cu
->extra_dbg_info
= conf
? conf
->extra_dbg_info
: 0;
3291 cu
->has_addr_info
= conf
? conf
->get_addr_info
: 0;
3294 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
3295 return DWARF_CB_ABORT
;
3297 cu
->little_endian
= ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
;
3298 cu
->nr_register_params
= arch__nr_register_params(&ehdr
);
3299 arch__set_register_params(&ehdr
, cu
);
3303 static int __cus__load_debug_types(struct cus
*cus
, struct conf_load
*conf
, Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3304 const char *filename
, const unsigned char *build_id
,
3305 int build_id_len
, struct cu
**cup
, struct dwarf_cu
*dcup
)
3307 Dwarf_Off off
= 0, noff
, type_off
;
3309 uint8_t pointer_size
, offset_size
;
3314 while (dwarf_next_unit(dw
, off
, &noff
, &cuhl
, NULL
, NULL
, &pointer_size
,
3315 &offset_size
, &signature
, &type_off
)
3321 cu
= cu__new("", pointer_size
, build_id
,
3322 build_id_len
, filename
, conf
->use_obstack
);
3324 cu__set_common(cu
, conf
, mod
, elf
) != 0) {
3326 return DWARF_CB_ABORT
;
3329 if (dwarf_cu__init(dcup
, cu
) != 0) {
3331 return DWARF_CB_ABORT
;
3335 dcup
->type_unit
= dcup
;
3337 cu
->dfops
= &dwarf__ops
;
3344 Dwarf_Die
*cu_die
= dwarf_offdie_types(dw
, off
+ cuhl
,
3347 if (die__process(cu_die
, *cup
, conf
) != 0)
3348 return DWARF_CB_ABORT
;
3353 if (*cup
!= NULL
&& cu__recode_dwarf_types(*cup
) != 0)
3354 return DWARF_CB_ABORT
;
3359 /* Match the define in linux:include/linux/elfnote-lto.h */
3360 #define LINUX_ELFNOTE_LTO_INFO 0x101
3362 static bool cus__merging_cu(Dwarf
*dw
, Elf
*elf
)
3364 Elf_Scn
*section
= NULL
;
3365 while ((section
= elf_nextscn(elf
, section
)) != 0) {
3367 if (!gelf_getshdr(section
, &header
))
3370 if (header
.sh_type
!= SHT_NOTE
)
3373 Elf_Data
*data
= NULL
;
3374 while ((data
= elf_getdata(section
, data
)) != 0) {
3375 size_t name_off
, desc_off
, offset
= 0;
3377 while ((offset
= gelf_getnote(data
, offset
, &hdr
, &name_off
, &desc_off
)) != 0) {
3378 if (hdr
.n_type
!= LINUX_ELFNOTE_LTO_INFO
)
3381 /* owner is Linux */
3382 if (strcmp((char *)data
->d_buf
+ name_off
, "Linux") != 0)
3385 return *(int *)(data
->d_buf
+ desc_off
) != 0;
3390 Dwarf_Off off
= 0, noff
;
3393 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0) {
3395 Dwarf_Die
*cu_die
= dwarf_offdie(dw
, off
+ cuhl
, &die_mem
);
3400 Dwarf_Off offset
= 0;
3403 Dwarf_Abbrev
*abbrev
= dwarf_getabbrev (cu_die
, offset
, &length
);
3404 if (abbrev
== NULL
|| abbrev
== DWARF_END_ABBREV
)
3408 if (dwarf_getattrcnt (abbrev
, &attrcnt
) != 0)
3411 unsigned int attr_num
, attr_form
;
3414 for (j
= 0; j
< attrcnt
; ++j
) {
3415 if (dwarf_getabbrevattr (abbrev
, j
, &attr_num
, &attr_form
,
3418 if (attr_form
== DW_FORM_ref_addr
)
3433 struct conf_load
*conf
;
3437 const char *filename
;
3439 const unsigned char *build_id
;
3442 struct dwarf_cu
*type_dcu
;
3445 struct dwarf_thread
{
3446 struct dwarf_cus
*dcus
;
3450 static struct dwarf_cu
*dwarf_cus__create_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
, uint8_t pointer_size
)
3453 * DW_AT_name in DW_TAG_compile_unit can be NULL, first seen in:
3455 * /usr/libexec/gcc/x86_64-redhat-linux/4.3.2/ecj1.debug
3457 const char *name
= attr_string(cu_die
, DW_AT_name
, dcus
->conf
);
3458 struct cu
*cu
= cu__new(name
?: "", pointer_size
, dcus
->build_id
, dcus
->build_id_len
, dcus
->filename
, dcus
->conf
->use_obstack
);
3459 if (cu
== NULL
|| cu__set_common(cu
, dcus
->conf
, dcus
->mod
, dcus
->elf
) != 0) {
3464 struct dwarf_cu
*dcu
= dwarf_cu__new(cu
);
3471 dcu
->type_unit
= dcus
->type_dcu
;
3473 cu
->dfops
= &dwarf__ops
;
3478 static int dwarf_cus__process_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
,
3479 struct cu
*cu
, void *thr_data
)
3481 if (die__process_and_recode(cu_die
, cu
, dcus
->conf
) != 0 ||
3482 cus__finalize(dcus
->cus
, cu
, dcus
->conf
, thr_data
) == LSK__STOP_LOADING
)
3483 return DWARF_CB_ABORT
;
3488 static int dwarf_cus__create_and_process_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
, uint8_t pointer_size
)
3490 struct dwarf_cu
*dcu
= dwarf_cus__create_cu(dcus
, cu_die
, pointer_size
);
3493 return DWARF_CB_ABORT
;
3495 cus__add(dcus
->cus
, dcu
->cu
);
3497 return dwarf_cus__process_cu(dcus
, cu_die
, dcu
->cu
, NULL
);
3500 static int dwarf_cus__nextcu(struct dwarf_cus
*dcus
, struct dwarf_cu
**dcu
,
3501 Dwarf_Die
*die_mem
, Dwarf_Die
**cu_die
,
3502 uint8_t *pointer_size
, uint8_t *offset_size
)
3508 cus__lock(dcus
->cus
);
3515 ret
= dwarf_nextcu(dcus
->dw
, dcus
->off
, &noff
, &cuhl
, NULL
, pointer_size
, offset_size
);
3517 *cu_die
= dwarf_offdie(dcus
->dw
, dcus
->off
+ cuhl
, die_mem
);
3518 if (*cu_die
!= NULL
)
3522 if (ret
== 0 && *cu_die
!= NULL
) {
3523 *dcu
= dwarf_cus__create_cu(dcus
, *cu_die
, *pointer_size
);
3525 dcus
->error
= ENOMEM
;
3529 // Do it here to keep all CUs in cus->cus in the same
3530 // order as in the DWARF file being loaded (e.g. vmlinux)
3531 __cus__add(dcus
->cus
, (*dcu
)->cu
);
3535 cus__unlock(dcus
->cus
);
3540 static void *dwarf_cus__process_cu_thread(void *arg
)
3542 struct dwarf_thread
*dthr
= arg
;
3543 struct dwarf_cus
*dcus
= dthr
->dcus
;
3544 uint8_t pointer_size
, offset_size
;
3545 Dwarf_Die die_mem
, *cu_die
;
3546 struct dwarf_cu
*dcu
;
3548 while (dwarf_cus__nextcu(dcus
, &dcu
, &die_mem
, &cu_die
, &pointer_size
, &offset_size
) == 0) {
3552 if (dwarf_cus__process_cu(dcus
, cu_die
, dcu
->cu
, dthr
->data
) == DWARF_CB_ABORT
)
3556 if (dcus
->conf
->thread_exit
&&
3557 dcus
->conf
->thread_exit(dcus
->conf
, dthr
->data
) != 0)
3560 return (void *)DWARF_CB_OK
;
3562 return (void *)DWARF_CB_ABORT
;
3565 static int dwarf_cus__threaded_process_cus(struct dwarf_cus
*dcus
)
3567 pthread_t threads
[dcus
->conf
->nr_jobs
];
3568 struct dwarf_thread dthr
[dcus
->conf
->nr_jobs
];
3569 void *thread_data
[dcus
->conf
->nr_jobs
];
3573 if (dcus
->conf
->threads_prepare
) {
3574 res
= dcus
->conf
->threads_prepare(dcus
->conf
, dcus
->conf
->nr_jobs
, thread_data
);
3578 memset(thread_data
, 0, sizeof(void *) * dcus
->conf
->nr_jobs
);
3581 for (i
= 0; i
< dcus
->conf
->nr_jobs
; ++i
) {
3582 dthr
[i
].dcus
= dcus
;
3583 dthr
[i
].data
= thread_data
[i
];
3585 dcus
->error
= pthread_create(&threads
[i
], NULL
,
3586 dwarf_cus__process_cu_thread
,
3597 int err
= pthread_join(threads
[i
], &res
);
3599 if (err
== 0 && res
!= NULL
)
3600 dcus
->error
= (long)res
;
3603 if (dcus
->conf
->threads_collect
) {
3604 res
= dcus
->conf
->threads_collect(dcus
->conf
, dcus
->conf
->nr_jobs
,
3605 thread_data
, dcus
->error
);
3606 if (dcus
->error
== 0)
3613 static int __dwarf_cus__process_cus(struct dwarf_cus
*dcus
)
3615 uint8_t pointer_size
, offset_size
;
3619 while (dwarf_nextcu(dcus
->dw
, dcus
->off
, &noff
, &cuhl
, NULL
, &pointer_size
, &offset_size
) == 0) {
3621 Dwarf_Die
*cu_die
= dwarf_offdie(dcus
->dw
, dcus
->off
+ cuhl
, &die_mem
);
3626 if (dwarf_cus__create_and_process_cu(dcus
, cu_die
, pointer_size
) == DWARF_CB_ABORT
)
3627 return DWARF_CB_ABORT
;
3635 static int dwarf_cus__process_cus(struct dwarf_cus
*dcus
)
3637 if (dcus
->conf
->nr_jobs
> 1)
3638 return dwarf_cus__threaded_process_cus(dcus
);
3640 return __dwarf_cus__process_cus(dcus
);
3643 static int cus__merge_and_process_cu(struct cus
*cus
, struct conf_load
*conf
,
3644 Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3645 const char *filename
,
3646 const unsigned char *build_id
,
3648 struct dwarf_cu
*type_dcu
)
3650 uint8_t pointer_size
, offset_size
;
3651 struct dwarf_cu
*dcu
= NULL
;
3652 Dwarf_Off off
= 0, noff
;
3653 struct cu
*cu
= NULL
;
3656 while (dwarf_nextcu(dw
, off
, &noff
, &cuhl
, NULL
, &pointer_size
,
3657 &offset_size
) == 0) {
3659 Dwarf_Die
*cu_die
= dwarf_offdie(dw
, off
+ cuhl
, &die_mem
);
3665 cu
= cu__new("", pointer_size
, build_id
, build_id_len
,
3666 filename
, conf
->use_obstack
);
3667 if (cu
== NULL
|| cu__set_common(cu
, conf
, mod
, elf
) != 0)
3670 dcu
= zalloc(sizeof(*dcu
));
3674 /* Merged cu tends to need a lot more memory.
3675 * Let us start with max_hashtags__bits and
3676 * go down to find a proper hashtag bit value.
3678 uint32_t default_hbits
= hashtags__bits
;
3679 for (hashtags__bits
= max_hashtags__bits
;
3680 hashtags__bits
>= default_hbits
;
3682 if (dwarf_cu__init(dcu
, cu
) == 0)
3685 if (hashtags__bits
< default_hbits
)
3689 dcu
->type_unit
= type_dcu
;
3691 cu
->dfops
= &dwarf__ops
;
3692 cu
->language
= attr_numeric(cu_die
, DW_AT_language
);
3697 if (dwarf_child(cu_die
, &child
) == 0) {
3698 bool filtered
= false;
3700 if (conf
->early_cu_filter
) {
3701 struct cu unmerged_cu
= {
3702 .name
= attr_string(cu_die
, DW_AT_name
, conf
),
3703 .language
= attr_numeric(cu_die
, DW_AT_language
),
3706 filtered
= conf
->early_cu_filter(&unmerged_cu
) == NULL
;
3709 if (!filtered
&& die__process_unit(&child
, cu
, conf
) != 0)
3719 /* process merged cu */
3720 if (cu__recode_dwarf_types(cu
) != LSK__KEEPIT
)
3724 * for lto build, the function return type may not be
3725 * resolved due to the return type of a subprogram is
3726 * encoded in another subprogram through abstract_origin
3727 * tag. Let us visit all subprograms again to resolve this.
3729 if (cu__resolve_func_ret_types_optimized(cu
) != LSK__KEEPIT
)
3732 if (cus__finalize(cus
, cu
, conf
, NULL
) == LSK__STOP_LOADING
)
3738 dwarf_cu__delete(cu
);
3740 return DWARF_CB_ABORT
;
3743 static int cus__load_module(struct cus
*cus
, struct conf_load
*conf
,
3744 Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3745 const char *filename
)
3747 const unsigned char *build_id
= NULL
;
3748 #ifdef HAVE_DWFL_MODULE_BUILD_ID
3750 int build_id_len
= dwfl_module_build_id(mod
, &build_id
, &vaddr
);
3752 int build_id_len
= 0;
3755 struct dwarf_cu type_dcu
;
3756 int type_lsk
= LSK__KEEPIT
;
3758 int res
= __cus__load_debug_types(cus
, conf
, mod
, dw
, elf
, filename
, build_id
, build_id_len
, &type_cu
, &type_dcu
);
3763 if (type_cu
!= NULL
) {
3764 type_lsk
= cu__finalize(type_cu
, cus
, conf
, NULL
);
3765 if (type_lsk
== LSK__DELETE
) {
3766 cus__remove(cus
, type_cu
);
3770 if (cus__merging_cu(dw
, elf
)) {
3771 res
= cus__merge_and_process_cu(cus
, conf
, mod
, dw
, elf
, filename
,
3772 build_id
, build_id_len
,
3773 type_cu
? &type_dcu
: NULL
);
3775 struct dwarf_cus dcus
= {
3782 .filename
= filename
,
3783 .type_dcu
= type_cu
? &type_dcu
: NULL
,
3784 .build_id
= build_id
,
3785 .build_id_len
= build_id_len
,
3787 res
= dwarf_cus__process_cus(&dcus
);
3793 if (type_lsk
== LSK__DELETE
)
3794 cu__delete(type_cu
);
3799 struct process_dwflmod_parms
{
3801 struct conf_load
*conf
;
3802 const char *filename
;
3803 uint32_t nr_dwarf_sections_found
;
3806 static int cus__process_dwflmod(Dwfl_Module
*dwflmod
,
3807 void **userdata __maybe_unused
,
3808 const char *name __maybe_unused
,
3809 Dwarf_Addr base __maybe_unused
,
3812 struct process_dwflmod_parms
*parms
= arg
;
3813 struct cus
*cus
= parms
->cus
;
3817 * Does the relocation and saves the elf for later processing
3818 * by the stealer, such as pahole_stealer, so that it don't
3819 * have to create another Elf instance just to do things like
3820 * reading this ELF file symtab to do CTF encoding of the
3821 * DW_TAG_suprogram tags (functions).
3823 Elf
*elf
= dwfl_module_getelf(dwflmod
, &dwflbias
);
3826 Dwarf
*dw
= dwfl_module_getdwarf(dwflmod
, &dwbias
);
3828 int err
= DWARF_CB_OK
;
3830 ++parms
->nr_dwarf_sections_found
;
3831 err
= cus__load_module(cus
, parms
->conf
, dwflmod
, dw
, elf
,
3835 * XXX We will fall back to try finding other debugging
3836 * formats (CTF), so no point in telling this to the user
3837 * Use for debugging.
3840 * "%s: can't get debug context descriptor: %s\n",
3841 * __func__, dwfl_errmsg(-1));
3847 static void dwarf_loader__exit(struct cus
*cus
)
3849 Dwfl
*dwfl
= cus__priv(cus
);
3853 cus__set_priv(cus
, NULL
);
3857 static int cus__process_file(struct cus
*cus
, struct conf_load
*conf
, int fd
,
3858 const char *filename
)
3860 /* Duplicate an fd for dwfl_report_offline to swallow. */
3861 int dwfl_fd
= dup(fd
);
3867 * Use libdwfl in a trivial way to open the libdw handle for us.
3868 * This takes care of applying relocations to DWARF data in ET_REL
3872 static const Dwfl_Callbacks callbacks
= {
3873 .section_address
= dwfl_offline_section_address
,
3874 .find_debuginfo
= dwfl_standard_find_debuginfo
,
3875 /* We use this table for core files too. */
3876 .find_elf
= dwfl_build_id_find_elf
,
3879 Dwfl
*dwfl
= dwfl_begin(&callbacks
);
3881 cus__set_priv(cus
, dwfl
);
3882 cus__set_loader_exit(cus
, dwarf_loader__exit
);
3884 if (dwfl_report_offline(dwfl
, filename
, filename
, dwfl_fd
) == NULL
)
3887 dwfl_report_end(dwfl
, NULL
, NULL
);
3889 struct process_dwflmod_parms parms
= {
3892 .filename
= filename
,
3893 .nr_dwarf_sections_found
= 0,
3896 /* Process the one or more modules gleaned from this file. */
3897 int err
= dwfl_getmodules(dwfl
, cus__process_dwflmod
, &parms
, 0);
3901 // We can't call dwfl_end(dwfl) here, as we keep pointers to strings
3902 // allocated by libdw that will be freed at dwfl_end(), so leave this for
3904 return parms
.nr_dwarf_sections_found
? 0 : -1;
3907 static int dwarf__load_file(struct cus
*cus
, struct conf_load
*conf
,
3908 const char *filename
)
3912 if (conf
->max_hashtable_bits
!= 0) {
3913 if (conf
->max_hashtable_bits
> 31)
3916 max_hashtags__bits
= conf
->max_hashtable_bits
;
3919 if (conf
->hashtable_bits
!= 0) {
3920 if (conf
->hashtable_bits
> max_hashtags__bits
)
3923 hashtags__bits
= conf
->hashtable_bits
;
3924 } else if (hashtags__bits
> max_hashtags__bits
)
3927 elf_version(EV_CURRENT
);
3929 fd
= open(filename
, O_RDONLY
);
3934 err
= cus__process_file(cus
, conf
, fd
, filename
);
3940 struct debug_fmt_ops dwarf__ops
= {
3942 .load_file
= dwarf__load_file
,
3943 .tag__decl_file
= dwarf_tag__decl_file
,
3944 .tag__decl_line
= dwarf_tag__decl_line
,
3945 .tag__orig_id
= dwarf_tag__orig_id
,
3946 .cu__delete
= dwarf_cu__delete
,
3947 .tag__alloc
= tag__alloc
,
3948 .tag__free
= tag__free
,
3949 .has_alignment_info
= true,