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
)
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)!\n", func
,
83 tag
, dwarf_tag_name(tag
));
86 #define tag__print_not_supported(tag) \
87 __tag__print_not_supported(tag, __func__)
89 struct dwarf_off_ref
{
90 unsigned int from_types
: 1;
94 typedef struct dwarf_off_ref dwarf_off_ref
;
97 struct hlist_node hash_node
;
101 dwarf_off_ref abstract_origin
;
102 dwarf_off_ref containing_type
;
107 const char *decl_file
;
110 static dwarf_off_ref
dwarf_tag__spec(struct dwarf_tag
*dtag
)
112 return *(dwarf_off_ref
*)(dtag
+ 1);
115 static void dwarf_tag__set_spec(struct dwarf_tag
*dtag
, dwarf_off_ref spec
)
117 *(dwarf_off_ref
*)(dtag
+ 1) = spec
;
121 struct hlist_head
*hash_tags
;
122 struct hlist_head
*hash_types
;
123 struct dwarf_tag
*last_type_lookup
;
125 struct dwarf_cu
*type_unit
;
128 static int dwarf_cu__init(struct dwarf_cu
*dcu
, struct cu
*cu
)
130 static struct dwarf_tag sentinel_dtag
= { .id
= ULLONG_MAX
, };
131 uint64_t hashtags_size
= 1UL << hashtags__bits
;
135 dcu
->hash_tags
= cu__malloc(cu
, sizeof(struct hlist_head
) * hashtags_size
);
139 dcu
->hash_types
= cu__malloc(cu
, sizeof(struct hlist_head
) * hashtags_size
);
140 if (!dcu
->hash_types
) {
141 cu__free(cu
, dcu
->hash_tags
);
146 for (i
= 0; i
< hashtags_size
; ++i
) {
147 INIT_HLIST_HEAD(&dcu
->hash_tags
[i
]);
148 INIT_HLIST_HEAD(&dcu
->hash_types
[i
]);
150 dcu
->type_unit
= NULL
;
151 // To avoid a per-lookup check against NULL in dwarf_cu__find_type_by_ref()
152 dcu
->last_type_lookup
= &sentinel_dtag
;
156 static struct dwarf_cu
*dwarf_cu__new(struct cu
*cu
)
158 struct dwarf_cu
*dwarf_cu
= cu__zalloc(cu
, sizeof(*dwarf_cu
));
160 if (dwarf_cu
!= NULL
&& dwarf_cu__init(dwarf_cu
, cu
) != 0) {
161 cu__free(cu
, dwarf_cu
);
168 static void dwarf_cu__delete(struct cu
*cu
)
170 if (cu
== NULL
|| cu
->priv
== NULL
)
173 struct dwarf_cu
*dcu
= cu
->priv
;
175 // dcu->hash_tags & dcu->hash_types are on cu->obstack
180 static void __tag__print_type_not_found(struct tag
*tag
, const char *func
)
182 struct dwarf_tag
*dtag
= tag
->priv
;
183 fprintf(stderr
, "%s: couldn't find %#llx type for %#llx (%s)!\n", func
,
184 (unsigned long long)dtag
->type
.off
, (unsigned long long)dtag
->id
,
185 dwarf_tag_name(tag
->tag
));
188 #define tag__print_type_not_found(tag) \
189 __tag__print_type_not_found(tag, __func__)
191 static void hashtags__hash(struct hlist_head
*hashtable
,
192 struct dwarf_tag
*dtag
)
194 struct hlist_head
*head
= hashtable
+ hashtags__fn(dtag
->id
);
195 hlist_add_head(&dtag
->hash_node
, head
);
198 static struct dwarf_tag
*hashtags__find(const struct hlist_head
*hashtable
,
204 struct dwarf_tag
*tpos
;
205 struct hlist_node
*pos
;
206 uint32_t bucket
= hashtags__fn(id
);
207 const struct hlist_head
*head
= hashtable
+ bucket
;
209 hlist_for_each_entry(tpos
, pos
, head
, hash_node
) {
217 static void cu__hash(struct cu
*cu
, struct tag
*tag
)
219 struct dwarf_cu
*dcu
= cu
->priv
;
220 struct hlist_head
*hashtable
= tag__is_tag_type(tag
) ?
223 hashtags__hash(hashtable
, tag
->priv
);
226 static struct dwarf_tag
*dwarf_cu__find_tag_by_ref(const struct dwarf_cu
*cu
,
227 const struct dwarf_off_ref
*ref
)
231 if (ref
->from_types
) {
234 return hashtags__find(cu
->hash_tags
, ref
->off
);
237 static struct dwarf_tag
*dwarf_cu__find_type_by_ref(struct dwarf_cu
*dcu
,
238 const struct dwarf_off_ref
*ref
)
242 if (ref
->from_types
) {
243 dcu
= dcu
->type_unit
;
249 if (dcu
->last_type_lookup
->id
== ref
->off
)
250 return dcu
->last_type_lookup
;
252 struct dwarf_tag
*dtag
= hashtags__find(dcu
->hash_types
, ref
->off
);
255 dcu
->last_type_lookup
= dtag
;
260 static void *memdup(const void *src
, size_t len
, struct cu
*cu
)
262 void *s
= cu__malloc(cu
, len
);
268 /* Number decoding macros. See 7.6 Variable Length Data. */
270 #define get_uleb128_step(var, addr, nth, break) \
272 var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
273 if ((__b & 0x80) == 0) \
276 #define get_uleb128_rest_return(var, i, addrp) \
278 for (; i < 10; ++i) { \
279 get_uleb128_step(var, *addrp, i, \
282 /* Other implementations set VALUE to UINT_MAX in this \
283 case. So we better do this as well. */ \
287 static uint64_t __libdw_get_uleb128(uint64_t acc
, uint32_t i
,
288 const uint8_t **addrp
)
291 get_uleb128_rest_return (acc
, i
, addrp
);
294 #define get_uleb128(var, addr) \
298 get_uleb128_step(var, addr, 0, break); \
299 var = __libdw_get_uleb128 (var, 1, &(addr)); \
302 static uint64_t attr_numeric(Dwarf_Die
*die
, uint32_t name
)
304 Dwarf_Attribute attr
;
307 if (dwarf_attr(die
, name
, &attr
) == NULL
)
310 form
= dwarf_whatform(&attr
);
315 if (dwarf_formaddr(&attr
, &addr
) == 0)
319 case DW_FORM_implicit_const
:
325 case DW_FORM_udata
: {
327 if (dwarf_formudata(&attr
, &value
) == 0)
332 case DW_FORM_flag_present
: {
334 if (dwarf_formflag(&attr
, &value
) == 0)
339 fprintf(stderr
, "DW_AT_<0x%x>=0x%x\n", name
, form
);
346 static uint64_t attr_alignment(Dwarf_Die
*die
, struct conf_load
*conf
)
348 return conf
->ignore_alignment_attr
? 0 : attr_numeric(die
, DW_AT_alignment
);
351 static uint64_t dwarf_expr(const uint8_t *expr
, uint32_t len __maybe_unused
)
353 /* Common case: offset from start of the class */
354 if (expr
[0] == DW_OP_plus_uconst
||
355 expr
[0] == DW_OP_constu
) {
358 get_uleb128(result
, expr
);
362 fprintf(stderr
, "%s: unhandled %#x DW_OP_ operation\n",
367 static Dwarf_Off
__attr_offset(Dwarf_Attribute
*attr
)
371 switch (dwarf_whatform(attr
)) {
372 case DW_FORM_implicit_const
:
378 case DW_FORM_udata
: {
380 if (dwarf_formudata(attr
, &value
) == 0)
385 if (dwarf_formblock(attr
, &block
) == 0)
386 return dwarf_expr(block
.data
, block
.length
);
392 static Dwarf_Off
attr_offset(Dwarf_Die
*die
, const uint32_t name
)
394 Dwarf_Attribute attr
;
396 if (dwarf_attr(die
, name
, &attr
) == NULL
)
399 return __attr_offset(&attr
);
402 static const char *attr_string(Dwarf_Die
*die
, uint32_t name
, struct conf_load
*conf __maybe_unused
)
404 const char *str
= NULL
;
405 Dwarf_Attribute attr
;
407 if (dwarf_attr(die
, name
, &attr
) != NULL
) {
408 str
= dwarf_formstring(&attr
);
410 if (conf
&& conf
->kabi_prefix
&& str
&& strncmp(str
, conf
->kabi_prefix
, conf
->kabi_prefix_len
) == 0)
411 return conf
->kabi_prefix
;
417 static struct dwarf_off_ref
attr_type(Dwarf_Die
*die
, uint32_t attr_name
)
419 Dwarf_Attribute attr
;
420 struct dwarf_off_ref ref
;
421 if (dwarf_attr(die
, attr_name
, &attr
) != NULL
) {
423 if (dwarf_formref_die(&attr
, &type_die
) != NULL
) {
424 ref
.from_types
= attr
.form
== DW_FORM_ref_sig8
;
425 ref
.off
= dwarf_dieoffset(&type_die
);
429 memset(&ref
, 0, sizeof(ref
));
433 static int attr_location(Dwarf_Die
*die
, Dwarf_Op
**expr
, size_t *exprlen
)
435 Dwarf_Attribute attr
;
436 if (dwarf_attr(die
, DW_AT_location
, &attr
) != NULL
) {
437 if (dwarf_getlocation(&attr
, expr
, exprlen
) == 0) {
438 /* DW_OP_addrx needs additional lookup for real addr. */
439 if (*exprlen
!= 0 && expr
[0]->atom
== DW_OP_addrx
) {
440 Dwarf_Attribute addr_attr
;
441 dwarf_getlocation_attr(&attr
, expr
[0], &addr_attr
);
444 dwarf_formaddr (&addr_attr
, &address
);
446 expr
[0]->number
= address
;
455 static void *__tag__alloc(struct dwarf_cu
*dcu
, size_t size
, bool spec
)
457 struct dwarf_tag
*dtag
= cu__zalloc(dcu
->cu
, (sizeof(*dtag
) + (spec
? sizeof(dwarf_off_ref
) : 0)));
462 struct tag
*tag
= cu__zalloc(dcu
->cu
, size
);
475 static void *tag__alloc(struct cu
*cu
, size_t size
)
477 return __tag__alloc(cu
->priv
, size
, false);
480 static void *tag__alloc_with_spec(struct cu
*cu
, size_t size
)
482 return __tag__alloc(cu
->priv
, size
, true);
485 static void tag__init(struct tag
*tag
, struct cu
*cu
, Dwarf_Die
*die
)
487 struct dwarf_tag
*dtag
= tag
->priv
;
489 tag
->tag
= dwarf_tag(die
);
491 dtag
->id
= dwarf_dieoffset(die
);
493 if (tag
->tag
== DW_TAG_imported_module
||
494 tag
->tag
== DW_TAG_imported_declaration
)
495 dtag
->type
= attr_type(die
, DW_AT_import
);
497 dtag
->type
= attr_type(die
, DW_AT_type
);
499 dtag
->abstract_origin
= attr_type(die
, DW_AT_abstract_origin
);
500 tag
->recursivity_level
= 0;
502 if (cu
->extra_dbg_info
) {
503 pthread_mutex_lock(&libdw__lock
);
506 const char *decl_file
= dwarf_decl_file(die
);
507 static const char *last_decl_file
, *last_decl_file_ptr
;
509 if (decl_file
!= last_decl_file_ptr
) {
510 last_decl_file
= decl_file
? strdup(decl_file
) : NULL
;
511 last_decl_file_ptr
= decl_file
;
514 dtag
->decl_file
= last_decl_file
;
515 dwarf_decl_line(die
, &decl_line
);
516 dtag
->decl_line
= decl_line
;
518 pthread_mutex_unlock(&libdw__lock
);
521 INIT_LIST_HEAD(&tag
->node
);
524 static struct tag
*tag__new(Dwarf_Die
*die
, struct cu
*cu
)
526 struct tag
*tag
= tag__alloc(cu
, sizeof(*tag
));
529 tag__init(tag
, cu
, die
);
534 static struct ptr_to_member_type
*ptr_to_member_type__new(Dwarf_Die
*die
,
537 struct ptr_to_member_type
*ptr
= tag__alloc(cu
, sizeof(*ptr
));
540 tag__init(&ptr
->tag
, cu
, die
);
541 struct dwarf_tag
*dtag
= ptr
->tag
.priv
;
542 dtag
->containing_type
= attr_type(die
, DW_AT_containing_type
);
548 static uint8_t encoding_to_float_type(uint64_t encoding
)
551 case DW_ATE_complex_float
: return BT_FP_CMPLX
;
552 case DW_ATE_float
: return BT_FP_SINGLE
;
553 case DW_ATE_imaginary_float
: return BT_FP_IMGRY
;
558 static struct base_type
*base_type__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
560 struct base_type
*bt
= tag__alloc(cu
, sizeof(*bt
));
563 tag__init(&bt
->tag
, cu
, die
);
564 bt
->name
= attr_string(die
, DW_AT_name
, conf
);
565 bt
->bit_size
= attr_numeric(die
, DW_AT_byte_size
) * 8;
566 uint64_t encoding
= attr_numeric(die
, DW_AT_encoding
);
567 bt
->is_bool
= encoding
== DW_ATE_boolean
;
568 bt
->is_signed
= (encoding
== DW_ATE_signed
) || (encoding
== DW_ATE_signed_char
);
569 bt
->is_varargs
= false;
570 bt
->name_has_encoding
= true;
571 bt
->float_type
= encoding_to_float_type(encoding
);
572 INIT_LIST_HEAD(&bt
->node
);
578 static struct array_type
*array_type__new(Dwarf_Die
*die
, struct cu
*cu
)
580 struct array_type
*at
= tag__alloc(cu
, sizeof(*at
));
583 tag__init(&at
->tag
, cu
, die
);
585 at
->nr_entries
= NULL
;
586 at
->is_vector
= dwarf_hasattr(die
, DW_AT_GNU_vector
);
592 static struct string_type
*string_type__new(Dwarf_Die
*die
, struct cu
*cu
)
594 struct string_type
*st
= tag__alloc(cu
, sizeof(*st
));
597 tag__init(&st
->tag
, cu
, die
);
598 st
->nr_entries
= attr_numeric(die
, DW_AT_byte_size
);
599 if (st
->nr_entries
== 0)
606 static void namespace__init(struct namespace *namespace, Dwarf_Die
*die
,
607 struct cu
*cu
, struct conf_load
*conf
)
609 tag__init(&namespace->tag
, cu
, die
);
610 INIT_LIST_HEAD(&namespace->tags
);
611 INIT_LIST_HEAD(&namespace->annots
);
612 namespace->name
= attr_string(die
, DW_AT_name
, conf
);
613 namespace->nr_tags
= 0;
614 namespace->shared_tags
= 0;
617 static struct namespace *namespace__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
619 struct namespace *namespace = tag__alloc(cu
, sizeof(*namespace));
621 if (namespace != NULL
)
622 namespace__init(namespace, die
, cu
, conf
);
627 static void type__init(struct type
*type
, Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
629 namespace__init(&type
->namespace, die
, cu
, conf
);
631 type
->size
= attr_numeric(die
, DW_AT_byte_size
);
632 type
->alignment
= attr_alignment(die
, conf
);
633 type
->declaration
= attr_numeric(die
, DW_AT_declaration
);
634 dwarf_tag__set_spec(type
->namespace.tag
.priv
,
635 attr_type(die
, DW_AT_specification
));
636 type
->definition_emitted
= 0;
637 type
->fwd_decl_emitted
= 0;
639 type
->nr_members
= 0;
640 type
->nr_static_members
= 0;
641 type
->is_signed_enum
= 0;
643 Dwarf_Attribute attr
;
644 if (dwarf_attr(die
, DW_AT_type
, &attr
) != NULL
) {
646 if (dwarf_formref_die(&attr
, &type_die
) != NULL
) {
647 uint64_t encoding
= attr_numeric(&type_die
, DW_AT_encoding
);
649 if (encoding
== DW_ATE_signed
|| encoding
== DW_ATE_signed_char
)
650 type
->is_signed_enum
= 1;
655 static struct type
*type__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
657 struct type
*type
= tag__alloc_with_spec(cu
, sizeof(*type
));
660 type__init(type
, die
, cu
, conf
);
665 static struct enumerator
*enumerator__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
667 struct enumerator
*enumerator
= tag__alloc(cu
, sizeof(*enumerator
));
669 if (enumerator
!= NULL
) {
670 tag__init(&enumerator
->tag
, cu
, die
);
671 enumerator
->name
= attr_string(die
, DW_AT_name
, conf
);
672 enumerator
->value
= attr_numeric(die
, DW_AT_const_value
);
678 static enum vscope
dwarf__location(Dwarf_Die
*die
, uint64_t *addr
, struct location
*location
)
680 enum vscope scope
= VSCOPE_UNKNOWN
;
682 if (attr_location(die
, &location
->expr
, &location
->exprlen
) != 0)
683 scope
= VSCOPE_OPTIMIZED
;
684 else if (location
->exprlen
!= 0) {
685 Dwarf_Op
*expr
= location
->expr
;
686 switch (expr
->atom
) {
689 scope
= VSCOPE_GLOBAL
;
690 *addr
= expr
[0].number
;
692 case DW_OP_reg1
... DW_OP_reg31
:
693 case DW_OP_breg0
... DW_OP_breg31
:
694 scope
= VSCOPE_REGISTER
; break;
696 scope
= VSCOPE_LOCAL
; break;
703 enum vscope
variable__scope(const struct variable
*var
)
708 const char *variable__scope_str(const struct variable
*var
)
710 switch (var
->scope
) {
711 case VSCOPE_LOCAL
: return "local";
712 case VSCOPE_GLOBAL
: return "global";
713 case VSCOPE_REGISTER
: return "register";
714 case VSCOPE_OPTIMIZED
: return "optimized";
721 static struct variable
*variable__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
723 struct variable
*var
;
724 bool has_specification
;
726 has_specification
= dwarf_hasattr(die
, DW_AT_specification
);
727 if (has_specification
) {
728 var
= tag__alloc_with_spec(cu
, sizeof(*var
));
730 var
= tag__alloc(cu
, sizeof(*var
));
734 tag__init(&var
->ip
.tag
, cu
, die
);
735 var
->name
= attr_string(die
, DW_AT_name
, conf
);
736 /* variable is visible outside of its enclosing cu */
737 var
->external
= dwarf_hasattr(die
, DW_AT_external
);
738 /* non-defining declaration of an object */
739 var
->declaration
= dwarf_hasattr(die
, DW_AT_declaration
);
740 var
->has_specification
= has_specification
;
741 var
->scope
= VSCOPE_UNKNOWN
;
742 INIT_LIST_HEAD(&var
->annots
);
744 if (!var
->declaration
&& cu
->has_addr_info
)
745 var
->scope
= dwarf__location(die
, &var
->ip
.addr
, &var
->location
);
746 if (has_specification
) {
747 dwarf_tag__set_spec(var
->ip
.tag
.priv
,
748 attr_type(die
, DW_AT_specification
));
755 static struct constant
*constant__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
757 struct constant
*constant
= tag__alloc(cu
, sizeof(*constant
));
759 if (constant
!= NULL
) {
760 tag__init(&constant
->tag
, cu
, die
);
761 constant
->name
= attr_string(die
, DW_AT_name
, conf
);
762 constant
->value
= attr_numeric(die
, DW_AT_const_value
);
768 static int tag__recode_dwarf_bitfield(struct tag
*tag
, struct cu
*cu
, uint16_t bit_size
)
773 /* in all the cases the name is at the same offset */
774 const char *name
= namespace__name(tag__namespace(tag
));
777 case DW_TAG_typedef
: {
778 const struct dwarf_tag
*dtag
= tag
->priv
;
779 struct dwarf_tag
*dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, &dtag
->type
);
782 tag__print_type_not_found(tag
);
786 struct tag
*type
= dtype
->tag
;
788 id
= tag__recode_dwarf_bitfield(type
, cu
, bit_size
);
792 struct type
*new_typedef
= cu__zalloc(cu
, sizeof(*new_typedef
));
793 if (new_typedef
== NULL
)
796 recoded
= (struct tag
*)new_typedef
;
797 recoded
->tag
= DW_TAG_typedef
;
799 new_typedef
->namespace.name
= tag__namespace(tag
)->name
;
803 case DW_TAG_const_type
:
804 case DW_TAG_volatile_type
:
805 case DW_TAG_atomic_type
: {
806 const struct dwarf_tag
*dtag
= tag
->priv
;
807 struct dwarf_tag
*dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, &dtag
->type
);
810 tag__print_type_not_found(tag
);
814 struct tag
*type
= dtype
->tag
;
816 id
= tag__recode_dwarf_bitfield(type
, cu
, bit_size
);
817 if (id
>= 0 && (uint32_t)id
== tag
->type
)
820 recoded
= cu__zalloc(cu
, sizeof(*recoded
));
824 recoded
->tag
= DW_TAG_volatile_type
;
829 case DW_TAG_base_type
:
831 * Here we must search on the final, core cu, not on
832 * the dwarf_cu as in dwarf there are no such things
833 * as base_types of less than 8 bits, etc.
835 recoded
= cu__find_base_type_by_name_and_size(cu
, name
, bit_size
, &short_id
);
839 struct base_type
*new_bt
= cu__zalloc(cu
, sizeof(*new_bt
));
843 recoded
= (struct tag
*)new_bt
;
844 recoded
->tag
= DW_TAG_base_type
;
845 recoded
->top_level
= 1;
846 new_bt
->name
= strdup(name
);
847 new_bt
->bit_size
= bit_size
;
850 case DW_TAG_enumeration_type
:
852 * Here we must search on the final, core cu, not on
853 * the dwarf_cu as in dwarf there are no such things
854 * as enumeration_types of less than 8 bits, etc.
856 recoded
= cu__find_enumeration_by_name_and_size(cu
, name
, bit_size
, &short_id
);
860 struct type
*alias
= tag__type(tag
);
861 struct type
*new_enum
= cu__zalloc(cu
, sizeof(*new_enum
));
862 if (new_enum
== NULL
)
865 recoded
= (struct tag
*)new_enum
;
866 recoded
->tag
= DW_TAG_enumeration_type
;
867 recoded
->top_level
= 1;
868 new_enum
->nr_members
= alias
->nr_members
;
872 new_enum
->namespace.tags
.next
= &alias
->namespace.tags
;
873 new_enum
->namespace.shared_tags
= 1;
874 new_enum
->namespace.name
= strdup(name
);
875 new_enum
->size
= bit_size
;
878 fprintf(stderr
, "%s: tag=%s, name=%s, bit_size=%d\n",
879 __func__
, dwarf_tag_name(tag
->tag
),
885 if (cu__add_tag(cu
, recoded
, &new_id
) == 0)
892 static int add_llvm_annotation(Dwarf_Die
*die
, int component_idx
, struct conf_load
*conf
,
893 struct list_head
*head
)
895 struct llvm_annotation
*annot
;
898 if (conf
->skip_encoding_btf_decl_tag
)
901 /* Only handle btf_decl_tag annotation for now. */
902 name
= attr_string(die
, DW_AT_name
, conf
);
903 if (strcmp(name
, "btf_decl_tag") != 0)
906 annot
= zalloc(sizeof(*annot
));
910 annot
->value
= attr_string(die
, DW_AT_const_value
, conf
);
911 annot
->component_idx
= component_idx
;
912 list_add_tail(&annot
->node
, head
);
916 static int add_child_llvm_annotations(Dwarf_Die
*die
, int component_idx
,
917 struct conf_load
*conf
, struct list_head
*head
)
922 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
927 if (dwarf_tag(die
) == DW_TAG_LLVM_annotation
) {
928 ret
= add_llvm_annotation(die
, component_idx
, conf
, head
);
932 } while (dwarf_siblingof(die
, die
) == 0);
937 int class_member__dwarf_recode_bitfield(struct class_member
*member
,
940 struct dwarf_tag
*dtag
= member
->tag
.priv
;
941 struct dwarf_tag
*type
= dwarf_cu__find_type_by_ref(cu
->priv
, &dtag
->type
);
947 recoded_type_id
= tag__recode_dwarf_bitfield(type
->tag
, cu
, member
->bitfield_size
);
948 if (recoded_type_id
< 0)
949 return recoded_type_id
;
951 member
->tag
.type
= recoded_type_id
;
955 static struct class_member
*class_member__new(Dwarf_Die
*die
, struct cu
*cu
,
956 bool in_union
, struct conf_load
*conf
)
958 struct class_member
*member
= tag__alloc(cu
, sizeof(*member
));
960 if (member
!= NULL
) {
961 tag__init(&member
->tag
, cu
, die
);
962 member
->name
= attr_string(die
, DW_AT_name
, conf
);
963 member
->alignment
= attr_alignment(die
, conf
);
965 Dwarf_Attribute attr
;
967 member
->has_bit_offset
= dwarf_attr(die
, DW_AT_data_bit_offset
, &attr
) != NULL
;
969 if (member
->has_bit_offset
) {
970 member
->bit_offset
= __attr_offset(&attr
);
971 // byte_offset and bitfield_offset will be recalculated later, when
972 // we discover the size of this bitfield base type.
974 if (dwarf_attr(die
, DW_AT_data_member_location
, &attr
) != NULL
) {
975 member
->byte_offset
= __attr_offset(&attr
);
977 member
->is_static
= !in_union
;
981 * Bit offset calculated here is valid only for byte-aligned
982 * fields. For bitfields on little-endian archs we need to
983 * adjust them taking into account byte size of the field,
984 * which might not be yet known. So we'll re-calculate bit
985 * offset later, in class_member__cache_byte_size.
987 member
->bit_offset
= member
->byte_offset
* 8;
988 member
->bitfield_offset
= attr_numeric(die
, DW_AT_bit_offset
);
992 * If DW_AT_byte_size is not present, byte size will be
993 * determined later in class_member__cache_byte_size using
994 * base integer/enum type
996 member
->byte_size
= attr_numeric(die
, DW_AT_byte_size
);
997 member
->bitfield_size
= attr_numeric(die
, DW_AT_bit_size
);
998 member
->bit_hole
= 0;
999 member
->bitfield_end
= 0;
1000 member
->visited
= 0;
1002 if (!cu__is_c(cu
)) {
1003 member
->accessibility
= attr_numeric(die
, DW_AT_accessibility
);
1004 member
->const_value
= attr_numeric(die
, DW_AT_const_value
);
1005 member
->virtuality
= attr_numeric(die
, DW_AT_virtuality
);
1013 /* How many function parameters are passed via registers? Used below in
1014 * determining if an argument has been optimized out or if it is simply
1015 * an argument > cu__nr_register_params(). Making cu__nr_register_params()
1016 * return 0 allows unsupported architectures to skip tagging optimized-out
1019 static int arch__nr_register_params(const GElf_Ehdr
*ehdr
)
1021 switch (ehdr
->e_machine
) {
1022 case EM_S390
: return 5;
1025 case EM_X86_64
: return 6;
1032 case EM_RISCV
: return 8;
1039 /* map from parameter index (0 for first, ...) to expected DW_OP_reg.
1040 * This will allow us to identify cases where optimized-out parameters
1041 * interfere with expectations about register contents on function
1044 static void arch__set_register_params(const GElf_Ehdr
*ehdr
, struct cu
*cu
)
1046 memset(cu
->register_params
, -1, sizeof(cu
->register_params
));
1048 switch (ehdr
->e_machine
) {
1050 /* https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf */
1051 cu
->register_params
[0] = DW_OP_reg2
; // %r2
1052 cu
->register_params
[1] = DW_OP_reg3
; // %r3
1053 cu
->register_params
[2] = DW_OP_reg4
; // %r4
1054 cu
->register_params
[3] = DW_OP_reg5
; // %r5
1055 cu
->register_params
[4] = DW_OP_reg6
; // %r6
1058 /* //en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI */
1059 cu
->register_params
[0] = DW_OP_reg5
; // %rdi
1060 cu
->register_params
[1] = DW_OP_reg4
; // %rsi
1061 cu
->register_params
[2] = DW_OP_reg1
; // %rdx
1062 cu
->register_params
[3] = DW_OP_reg2
; // %rcx
1063 cu
->register_params
[4] = DW_OP_reg8
; // %r8
1064 cu
->register_params
[5] = DW_OP_reg9
; // %r9
1067 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers */
1069 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
1070 cu
->register_params
[0] = DW_OP_reg0
;
1071 cu
->register_params
[1] = DW_OP_reg1
;
1072 cu
->register_params
[2] = DW_OP_reg2
;
1073 cu
->register_params
[3] = DW_OP_reg3
;
1074 cu
->register_params
[4] = DW_OP_reg4
;
1075 cu
->register_params
[5] = DW_OP_reg5
;
1076 cu
->register_params
[6] = DW_OP_reg6
;
1077 cu
->register_params
[7] = DW_OP_reg7
;
1084 static struct parameter
*parameter__new(Dwarf_Die
*die
, struct cu
*cu
,
1085 struct conf_load
*conf
, int param_idx
)
1087 struct parameter
*parm
= tag__alloc(cu
, sizeof(*parm
));
1090 Dwarf_Addr base
, start
, end
;
1091 bool has_const_value
;
1092 Dwarf_Attribute attr
;
1093 struct location loc
;
1095 tag__init(&parm
->tag
, cu
, die
);
1096 parm
->name
= attr_string(die
, DW_AT_name
, conf
);
1098 if (param_idx
>= cu
->nr_register_params
|| param_idx
< 0)
1100 /* Parameters which use DW_AT_abstract_origin to point at
1101 * the original parameter definition (with no name in the DIE)
1102 * are the result of later DWARF generation during compilation
1103 * so often better take into account if arguments were
1106 * By checking that locations for parameters that are expected
1107 * to be passed as registers are actually passed as registers,
1108 * we can spot optimized-out parameters.
1110 * It can also be the case that a parameter DIE has
1111 * a constant value attribute reflecting optimization or
1112 * has no location attribute.
1114 * From the DWARF spec:
1118 * A DW_AT_const_value attribute for an entry describing a
1119 * variable or formal parameter whose value is constant and not
1120 * represented by an object in the address space of the program,
1121 * or an entry describing a named constant. (Note
1122 * that such an entry does not have a location attribute.)"
1124 * So we can also use the absence of a location for a parameter
1125 * as evidence it has been optimized out. This info will
1126 * need to be shared between a parameter and any abstract
1127 * origin references however, since gcc can have location
1128 * information in the parameter that refers back to the original
1129 * via abstract origin, so we need to share location presence
1130 * between these parameter representations. See
1131 * ftype__recode_dwarf_types() below for how this is handled.
1133 has_const_value
= dwarf_attr(die
, DW_AT_const_value
, &attr
) != NULL
;
1134 parm
->has_loc
= dwarf_attr(die
, DW_AT_location
, &attr
) != NULL
;
1135 /* dwarf_getlocations() handles location lists; here we are
1136 * only interested in the first expr.
1138 if (parm
->has_loc
&&
1139 #if _ELFUTILS_PREREQ(0, 157)
1140 dwarf_getlocations(&attr
, 0, &base
, &start
, &end
,
1141 &loc
.expr
, &loc
.exprlen
) > 0 &&
1143 dwarf_getlocation(&attr
, &loc
.expr
, &loc
.exprlen
) == 0 &&
1146 int expected_reg
= cu
->register_params
[param_idx
];
1147 Dwarf_Op
*expr
= loc
.expr
;
1149 switch (expr
->atom
) {
1150 case DW_OP_reg0
... DW_OP_reg31
:
1151 /* mark parameters that use an unexpected
1152 * register to hold a parameter; these will
1153 * be problematic for users of BTF as they
1154 * violate expectations about register
1157 if (expected_reg
>= 0 && expected_reg
!= expr
->atom
)
1158 parm
->unexpected_reg
= 1;
1161 parm
->optimized
= 1;
1164 } else if (has_const_value
) {
1165 parm
->optimized
= 1;
1172 static struct inline_expansion
*inline_expansion__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1174 struct inline_expansion
*exp
= tag__alloc(cu
, sizeof(*exp
));
1177 struct dwarf_tag
*dtag
= exp
->ip
.tag
.priv
;
1179 tag__init(&exp
->ip
.tag
, cu
, die
);
1180 dtag
->decl_file
= attr_string(die
, DW_AT_call_file
, conf
);
1181 dtag
->decl_line
= attr_numeric(die
, DW_AT_call_line
);
1182 dtag
->type
= attr_type(die
, DW_AT_abstract_origin
);
1186 if (!cu
->has_addr_info
)
1189 if (dwarf_lowpc(die
, &exp
->ip
.addr
))
1191 if (dwarf_lowpc(die
, &exp
->high_pc
))
1194 exp
->size
= exp
->high_pc
- exp
->ip
.addr
;
1195 if (exp
->size
== 0) {
1196 Dwarf_Addr base
, start
;
1197 ptrdiff_t offset
= 0;
1200 offset
= dwarf_ranges(die
, offset
, &base
, &start
,
1202 start
= (unsigned long)start
;
1203 exp
->high_pc
= (unsigned long)exp
->high_pc
;
1206 exp
->size
+= exp
->high_pc
- start
;
1207 if (exp
->ip
.addr
== 0)
1208 exp
->ip
.addr
= start
;
1216 static struct label
*label__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1218 struct label
*label
= tag__alloc(cu
, sizeof(*label
));
1220 if (label
!= NULL
) {
1221 tag__init(&label
->ip
.tag
, cu
, die
);
1222 label
->name
= attr_string(die
, DW_AT_name
, conf
);
1223 if (!cu
->has_addr_info
|| dwarf_lowpc(die
, &label
->ip
.addr
))
1230 static struct class *class__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1232 struct class *class = tag__alloc_with_spec(cu
, sizeof(*class));
1234 if (class != NULL
) {
1235 type__init(&class->type
, die
, cu
, conf
);
1236 INIT_LIST_HEAD(&class->vtable
);
1237 class->nr_vtable_entries
=
1239 class->nr_bit_holes
=
1241 class->bit_padding
= 0;
1248 static void lexblock__init(struct lexblock
*block
, struct cu
*cu
,
1253 if (!cu
->has_addr_info
|| dwarf_lowpc(die
, &block
->ip
.addr
)) {
1256 } else if (dwarf_highpc(die
, &high_pc
))
1259 block
->size
= high_pc
- block
->ip
.addr
;
1261 INIT_LIST_HEAD(&block
->tags
);
1263 block
->size_inline_expansions
=
1264 block
->nr_inline_expansions
=
1266 block
->nr_lexblocks
=
1267 block
->nr_variables
= 0;
1270 static struct lexblock
*lexblock__new(Dwarf_Die
*die
, struct cu
*cu
)
1272 struct lexblock
*block
= tag__alloc(cu
, sizeof(*block
));
1274 if (block
!= NULL
) {
1275 tag__init(&block
->ip
.tag
, cu
, die
);
1276 lexblock__init(block
, cu
, die
);
1282 static void ftype__init(struct ftype
*ftype
, Dwarf_Die
*die
, struct cu
*cu
)
1285 const uint16_t tag
= dwarf_tag(die
);
1286 assert(tag
== DW_TAG_subprogram
|| tag
== DW_TAG_subroutine_type
);
1288 tag__init(&ftype
->tag
, cu
, die
);
1289 ftype
->byte_size
= attr_numeric(die
, DW_AT_byte_size
);
1290 INIT_LIST_HEAD(&ftype
->parms
);
1291 ftype
->nr_parms
= 0;
1292 ftype
->unspec_parms
= 0;
1295 static struct ftype
*ftype__new(Dwarf_Die
*die
, struct cu
*cu
)
1297 struct ftype
*ftype
= tag__alloc(cu
, sizeof(*ftype
));
1300 ftype__init(ftype
, die
, cu
);
1305 static struct function
*function__new(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1307 struct function
*func
= tag__alloc_with_spec(cu
, sizeof(*func
));
1310 ftype__init(&func
->proto
, die
, cu
);
1311 lexblock__init(&func
->lexblock
, cu
, die
);
1312 func
->name
= attr_string(die
, DW_AT_name
, conf
);
1313 func
->linkage_name
= attr_string(die
, DW_AT_MIPS_linkage_name
, conf
);
1314 func
->inlined
= attr_numeric(die
, DW_AT_inline
);
1315 func
->declaration
= dwarf_hasattr(die
, DW_AT_declaration
);
1316 func
->external
= dwarf_hasattr(die
, DW_AT_external
);
1317 func
->abstract_origin
= dwarf_hasattr(die
, DW_AT_abstract_origin
);
1318 dwarf_tag__set_spec(func
->proto
.tag
.priv
,
1319 attr_type(die
, DW_AT_specification
));
1320 func
->accessibility
= attr_numeric(die
, DW_AT_accessibility
);
1321 func
->virtuality
= attr_numeric(die
, DW_AT_virtuality
);
1322 INIT_LIST_HEAD(&func
->vtable_node
);
1323 INIT_LIST_HEAD(&func
->annots
);
1324 INIT_LIST_HEAD(&func
->tool_node
);
1325 func
->vtable_entry
= -1;
1326 if (dwarf_hasattr(die
, DW_AT_vtable_elem_location
))
1327 func
->vtable_entry
= attr_offset(die
, DW_AT_vtable_elem_location
);
1328 func
->cu_total_size_inline_expansions
= 0;
1329 func
->cu_total_nr_inline_expansions
= 0;
1336 static uint64_t attr_upper_bound(Dwarf_Die
*die
)
1338 Dwarf_Attribute attr
;
1340 if (dwarf_attr(die
, DW_AT_upper_bound
, &attr
) != NULL
) {
1343 if (dwarf_formudata(&attr
, &num
) == 0) {
1344 return (uintmax_t)num
+ 1;
1346 } else if (dwarf_attr(die
, DW_AT_count
, &attr
) != NULL
) {
1349 if (dwarf_formudata(&attr
, &num
) == 0) {
1350 return (uintmax_t)num
;
1357 static void __cu__tag_not_handled(Dwarf_Die
*die
, const char *fn
)
1359 uint32_t tag
= dwarf_tag(die
);
1361 fprintf(stderr
, "%s: DW_TAG_%s (%#x) @ <%#llx> not handled!\n",
1362 fn
, dwarf_tag_name(tag
), tag
,
1363 (unsigned long long)dwarf_dieoffset(die
));
1366 static struct tag unsupported_tag
;
1368 #define cu__tag_not_handled(die) __cu__tag_not_handled(die, __FUNCTION__)
1370 static struct tag
*__die__process_tag(Dwarf_Die
*die
, struct cu
*cu
,
1371 int toplevel
, const char *fn
, struct conf_load
*conf
);
1373 #define die__process_tag(die, cu, toplevel, conf_load) \
1374 __die__process_tag(die, cu, toplevel, __FUNCTION__, conf_load)
1376 static struct tag
*die__create_new_tag(Dwarf_Die
*die
, struct cu
*cu
)
1378 struct tag
*tag
= tag__new(die
, cu
);
1381 if (dwarf_haschildren(die
))
1382 fprintf(stderr
, "%s: %s WITH children!\n", __func__
,
1383 dwarf_tag_name(tag
->tag
));
1389 static struct btf_type_tag_ptr_type
*die__create_new_btf_type_tag_ptr_type(Dwarf_Die
*die
, struct cu
*cu
)
1391 struct btf_type_tag_ptr_type
*tag
;
1393 tag
= tag__alloc_with_spec(cu
, sizeof(struct btf_type_tag_ptr_type
));
1397 tag__init(&tag
->tag
, cu
, die
);
1398 tag
->tag
.has_btf_type_tag
= true;
1399 INIT_LIST_HEAD(&tag
->tags
);
1403 static struct btf_type_tag_type
*die__create_new_btf_type_tag_type(Dwarf_Die
*die
, struct cu
*cu
,
1404 struct conf_load
*conf
)
1406 struct btf_type_tag_type
*tag
;
1408 tag
= tag__alloc_with_spec(cu
, sizeof(struct btf_type_tag_type
));
1412 tag__init(&tag
->tag
, cu
, die
);
1413 tag
->value
= attr_string(die
, DW_AT_const_value
, conf
);
1417 static struct tag
*die__create_new_pointer_tag(Dwarf_Die
*die
, struct cu
*cu
,
1418 struct conf_load
*conf
)
1420 struct btf_type_tag_ptr_type
*tag
= NULL
;
1421 struct btf_type_tag_type
*annot
;
1422 Dwarf_Die
*cdie
, child
;
1426 /* If no child tags or skipping btf_type_tag encoding, just create a new tag
1429 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0 ||
1430 conf
->skip_encoding_btf_type_tag
)
1431 return tag__new(die
, cu
);
1433 /* Otherwise, check DW_TAG_LLVM_annotation child tags */
1436 if (dwarf_tag(cdie
) != DW_TAG_LLVM_annotation
)
1439 /* Only check btf_type_tag annotations */
1440 name
= attr_string(cdie
, DW_AT_name
, conf
);
1441 if (strcmp(name
, "btf_type_tag") != 0)
1445 /* Create a btf_type_tag_ptr type. */
1446 tag
= die__create_new_btf_type_tag_ptr_type(die
, cu
);
1451 /* Create a btf_type_tag type for this annotation. */
1452 annot
= die__create_new_btf_type_tag_type(cdie
, cu
, conf
);
1456 if (cu__table_add_tag(cu
, &annot
->tag
, &id
) < 0)
1459 struct dwarf_tag
*dtag
= annot
->tag
.priv
;
1460 dtag
->small_id
= id
;
1461 cu__hash(cu
, &annot
->tag
);
1463 /* For a list of DW_TAG_LLVM_annotation like tag1 -> tag2 -> tag3,
1464 * the tag->tags contains tag3 -> tag2 -> tag1.
1466 list_add(&annot
->node
, &tag
->tags
);
1467 } while (dwarf_siblingof(cdie
, cdie
) == 0);
1469 return tag
? &tag
->tag
: tag__new(die
, cu
);
1472 static struct tag
*die__create_new_ptr_to_member_type(Dwarf_Die
*die
,
1475 struct ptr_to_member_type
*ptr
= ptr_to_member_type__new(die
, cu
);
1477 return ptr
? &ptr
->tag
: NULL
;
1480 static int die__process_class(Dwarf_Die
*die
,
1481 struct type
*class, struct cu
*cu
, struct conf_load
*conf
);
1483 static struct tag
*die__create_new_class(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1486 struct class *class = class__new(die
, cu
, conf
);
1488 if (class != NULL
&&
1489 dwarf_haschildren(die
) != 0 &&
1490 dwarf_child(die
, &child
) == 0) {
1491 if (die__process_class(&child
, &class->type
, cu
, conf
) != 0) {
1492 class__delete(class);
1497 return class ? &class->type
.namespace.tag
: NULL
;
1500 static int die__process_namespace(Dwarf_Die
*die
, struct namespace *namespace,
1501 struct cu
*cu
, struct conf_load
*conf
);
1503 static struct tag
*die__create_new_namespace(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1506 struct namespace *namespace = namespace__new(die
, cu
, conf
);
1508 if (namespace != NULL
&&
1509 dwarf_haschildren(die
) != 0 &&
1510 dwarf_child(die
, &child
) == 0) {
1511 if (die__process_namespace(&child
, namespace, cu
, conf
) != 0) {
1512 namespace__delete(namespace);
1517 return namespace ? &namespace->tag
: NULL
;
1520 static struct tag
*die__create_new_union(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1523 struct type
*utype
= type__new(die
, cu
, conf
);
1525 if (utype
!= NULL
&&
1526 dwarf_haschildren(die
) != 0 &&
1527 dwarf_child(die
, &child
) == 0) {
1528 if (die__process_class(&child
, utype
, cu
, conf
) != 0) {
1529 type__delete(utype
);
1534 return utype
? &utype
->namespace.tag
: NULL
;
1537 static struct tag
*die__create_new_base_type(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1539 struct base_type
*base
= base_type__new(die
, cu
, conf
);
1544 if (dwarf_haschildren(die
))
1545 fprintf(stderr
, "%s: DW_TAG_base_type WITH children!\n",
1551 static struct tag
*die__create_new_typedef(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1553 struct type
*tdef
= type__new(die
, cu
, conf
);
1558 if (add_child_llvm_annotations(die
, -1, conf
, &tdef
->namespace.annots
))
1561 return &tdef
->namespace.tag
;
1564 static struct tag
*die__create_new_array(Dwarf_Die
*die
, struct cu
*cu
)
1567 /* "64 dimensions will be enough for everybody." acme, 2006 */
1568 const uint8_t max_dimensions
= 64;
1569 uint32_t nr_entries
[max_dimensions
];
1570 struct array_type
*array
= array_type__new(die
, cu
);
1575 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1580 if (dwarf_tag(die
) == DW_TAG_subrange_type
) {
1581 nr_entries
[array
->dimensions
++] = attr_upper_bound(die
);
1582 if (array
->dimensions
== max_dimensions
) {
1583 fprintf(stderr
, "%s: only %u dimensions are "
1585 __FUNCTION__
, max_dimensions
);
1589 cu__tag_not_handled(die
);
1590 } while (dwarf_siblingof(die
, die
) == 0);
1592 array
->nr_entries
= memdup(nr_entries
,
1593 array
->dimensions
* sizeof(uint32_t), cu
);
1594 if (array
->nr_entries
== NULL
)
1603 static struct tag
*die__create_new_string_type(Dwarf_Die
*die
, struct cu
*cu
)
1605 struct string_type
*string
= string_type__new(die
, cu
);
1610 return &string
->tag
;
1613 static struct tag
*die__create_new_parameter(Dwarf_Die
*die
,
1614 struct ftype
*ftype
,
1615 struct lexblock
*lexblock
,
1616 struct cu
*cu
, struct conf_load
*conf
,
1619 struct parameter
*parm
= parameter__new(die
, cu
, conf
, param_idx
);
1624 if (ftype
!= NULL
) {
1625 ftype__add_parameter(ftype
, parm
);
1626 if (param_idx
>= 0) {
1627 if (add_child_llvm_annotations(die
, param_idx
, conf
, &(tag__function(&ftype
->tag
)->annots
)))
1632 * DW_TAG_formal_parameters on a non DW_TAG_subprogram nor
1633 * DW_TAG_subroutine_type tag happens sometimes, likely due to
1634 * compiler optimizing away a inline expansion (at least this
1635 * was observed in some cases, such as in the Linux kernel
1636 * current_kernel_time function circa 2.6.20-rc5), keep it in
1637 * the lexblock tag list because it can be referenced as an
1638 * DW_AT_abstract_origin in another DW_TAG_formal_parameter.
1640 lexblock__add_tag(lexblock
, &parm
->tag
);
1646 static struct tag
*die__create_new_label(Dwarf_Die
*die
,
1647 struct lexblock
*lexblock
,
1648 struct cu
*cu
, struct conf_load
*conf
)
1650 struct label
*label
= label__new(die
, cu
, conf
);
1655 if (lexblock
!= NULL
) {
1656 // asm CUs have labels and they will be in the cu top level tag list
1657 // See die__process_unit()
1658 lexblock__add_label(lexblock
, label
);
1661 return &label
->ip
.tag
;
1664 static struct tag
*die__create_new_variable(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1666 struct variable
*var
= variable__new(die
, cu
, conf
);
1668 if (var
== NULL
|| add_child_llvm_annotations(die
, -1, conf
, &var
->annots
))
1671 return &var
->ip
.tag
;
1674 static struct tag
*die__create_new_constant(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1676 struct constant
*constant
= constant__new(die
, cu
, conf
);
1678 if (constant
== NULL
)
1681 return &constant
->tag
;
1684 static struct tag
*die__create_new_subroutine_type(Dwarf_Die
*die
,
1685 struct cu
*cu
, struct conf_load
*conf
)
1688 struct ftype
*ftype
= ftype__new(die
, cu
);
1694 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1701 switch (dwarf_tag(die
)) {
1702 case DW_TAG_subrange_type
: // ADA stuff
1703 tag__print_not_supported(dwarf_tag(die
));
1705 case DW_TAG_formal_parameter
:
1706 tag
= die__create_new_parameter(die
, ftype
, NULL
, cu
, conf
, -1);
1708 case DW_TAG_unspecified_parameters
:
1709 ftype
->unspec_parms
= 1;
1712 tag
= die__process_tag(die
, cu
, 0, conf
);
1716 if (tag
== &unsupported_tag
) {
1717 tag__print_not_supported(dwarf_tag(die
));
1721 if (cu__add_tag(cu
, tag
, &id
) < 0)
1722 goto out_delete_tag
;
1730 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
1731 goto out_delete_tag
;
1734 struct dwarf_tag
*dtag
= tag
->priv
;
1735 dtag
->small_id
= id
;
1736 } while (dwarf_siblingof(die
, die
) == 0);
1742 ftype__delete(ftype
);
1746 static struct tag
*die__create_new_enumeration(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
1749 struct type
*enumeration
= type__new(die
, cu
, conf
);
1751 if (enumeration
== NULL
)
1754 if (enumeration
->size
== 0)
1755 enumeration
->size
= sizeof(int) * 8;
1757 enumeration
->size
*= 8;
1759 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0) {
1760 /* Seen on libQtCore.so.4.3.4.debug,
1761 * class QAbstractFileEngineIterator, enum EntryInfoType */
1767 struct enumerator
*enumerator
;
1769 if (dwarf_tag(die
) != DW_TAG_enumerator
) {
1770 cu__tag_not_handled(die
);
1773 enumerator
= enumerator__new(die
, cu
, conf
);
1774 if (enumerator
== NULL
)
1777 enumeration__add(enumeration
, enumerator
);
1778 } while (dwarf_siblingof(die
, die
) == 0);
1780 return &enumeration
->namespace.tag
;
1782 enumeration__delete(enumeration
);
1786 static int die__process_class(Dwarf_Die
*die
, struct type
*class,
1787 struct cu
*cu
, struct conf_load
*conf
)
1789 const bool is_union
= tag__is_union(&class->namespace.tag
);
1793 switch (dwarf_tag(die
)) {
1794 case DW_TAG_subrange_type
: // XXX: ADA stuff, its a type tho, will have other entries referencing it...
1795 case DW_TAG_variant_part
: // XXX: Rust stuff
1796 #ifdef STB_GNU_UNIQUE
1797 case DW_TAG_GNU_formal_parameter_pack
:
1798 case DW_TAG_GNU_template_parameter_pack
:
1799 case DW_TAG_GNU_template_template_param
:
1801 case DW_TAG_template_type_parameter
:
1802 case DW_TAG_template_value_parameter
:
1804 * FIXME: probably we'll have to attach this as a list of
1805 * template parameters to use at class__fprintf time...
1808 * https://gcc.gnu.org/wiki/TemplateParmsDwarf
1810 tag__print_not_supported(dwarf_tag(die
));
1812 case DW_TAG_inheritance
:
1813 case DW_TAG_member
: {
1814 struct class_member
*member
= class_member__new(die
, cu
, is_union
, conf
);
1819 if (cu__is_c_plus_plus(cu
)) {
1822 if (cu__table_add_tag(cu
, &member
->tag
, &id
) < 0) {
1823 class_member__delete(member
);
1827 struct dwarf_tag
*dtag
= member
->tag
.priv
;
1828 dtag
->small_id
= id
;
1831 type__add_member(class, member
);
1832 cu__hash(cu
, &member
->tag
);
1833 if (add_child_llvm_annotations(die
, member_idx
, conf
, &class->namespace.annots
))
1838 case DW_TAG_LLVM_annotation
:
1839 if (add_llvm_annotation(die
, -1, conf
, &class->namespace.annots
))
1843 struct tag
*tag
= die__process_tag(die
, cu
, 0, conf
);
1848 if (tag
== &unsupported_tag
) {
1849 tag__print_not_supported(dwarf_tag(die
));
1855 if (cu__table_add_tag(cu
, tag
, &id
) < 0) {
1860 struct dwarf_tag
*dtag
= tag
->priv
;
1861 dtag
->small_id
= id
;
1863 namespace__add_tag(&class->namespace, tag
);
1865 if (tag__is_function(tag
)) {
1866 struct function
*fself
= tag__function(tag
);
1868 if (fself
->vtable_entry
!= -1)
1869 class__add_vtable_entry(type__class(class), fself
);
1874 } while (dwarf_siblingof(die
, die
) == 0);
1879 static int die__process_namespace(Dwarf_Die
*die
, struct namespace *namespace,
1880 struct cu
*cu
, struct conf_load
*conf
)
1884 tag
= die__process_tag(die
, cu
, 0, conf
);
1888 if (tag
== &unsupported_tag
) {
1889 tag__print_not_supported(dwarf_tag(die
));
1894 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
1895 goto out_delete_tag
;
1897 struct dwarf_tag
*dtag
= tag
->priv
;
1898 dtag
->small_id
= id
;
1900 namespace__add_tag(namespace, tag
);
1902 } while (dwarf_siblingof(die
, die
) == 0);
1911 static int die__process_function(Dwarf_Die
*die
, struct ftype
*ftype
,
1912 struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
);
1914 static int die__create_new_lexblock(Dwarf_Die
*die
,
1915 struct cu
*cu
, struct lexblock
*father
, struct conf_load
*conf
)
1917 struct lexblock
*lexblock
= lexblock__new(die
, cu
);
1919 if (lexblock
!= NULL
) {
1920 if (die__process_function(die
, NULL
, lexblock
, cu
, conf
) != 0)
1924 lexblock__add_lexblock(father
, lexblock
);
1927 lexblock__delete(lexblock
);
1931 static struct tag
*die__create_new_inline_expansion(Dwarf_Die
*die
,
1932 struct lexblock
*lexblock
,
1933 struct cu
*cu
, struct conf_load
*conf
);
1935 static int die__process_inline_expansion(Dwarf_Die
*die
, struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
)
1940 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
1947 switch (dwarf_tag(die
)) {
1948 case DW_TAG_call_site
:
1949 case DW_TAG_call_site_parameter
:
1950 case DW_TAG_GNU_call_site
:
1951 case DW_TAG_GNU_call_site_parameter
:
1953 * FIXME: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
1954 * and write proper support.
1956 * From a quick read there is not much we can use in
1957 * the existing dwarves tools, so just stop warning the user,
1958 * developers will find these notes if wanting to use in a
1962 case DW_TAG_lexical_block
:
1963 if (die__create_new_lexblock(die
, cu
, lexblock
, conf
) != 0)
1966 case DW_TAG_formal_parameter
:
1969 * So far DW_TAG_inline_routine had just an
1970 * abstract origin, but starting with
1971 * /usr/lib/openoffice.org/basis3.0/program/libdbalx.so
1972 * I realized it really has to be handled as a
1973 * DW_TAG_function... Lets just get the types
1974 * for 1.8, then fix this properly.
1976 * cu__tag_not_handled(die);
1979 case DW_TAG_inlined_subroutine
:
1980 tag
= die__create_new_inline_expansion(die
, lexblock
, cu
, conf
);
1983 if (conf
->ignore_labels
)
1985 tag
= die__create_new_label(die
, lexblock
, cu
, conf
);
1988 tag
= die__process_tag(die
, cu
, 0, conf
);
1992 if (tag
== &unsupported_tag
) {
1993 tag__print_not_supported(dwarf_tag(die
));
1997 if (cu__add_tag(cu
, tag
, &id
) < 0)
1998 goto out_delete_tag
;
2005 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
2006 goto out_delete_tag
;
2009 struct dwarf_tag
*dtag
= tag
->priv
;
2010 dtag
->small_id
= id
;
2011 } while (dwarf_siblingof(die
, die
) == 0);
2020 static struct tag
*die__create_new_inline_expansion(Dwarf_Die
*die
,
2021 struct lexblock
*lexblock
,
2022 struct cu
*cu
, struct conf_load
*conf
)
2024 struct inline_expansion
*exp
= inline_expansion__new(die
, cu
, conf
);
2029 if (die__process_inline_expansion(die
, lexblock
, cu
, conf
) != 0) {
2034 if (lexblock
!= NULL
)
2035 lexblock__add_inline_expansion(lexblock
, exp
);
2036 return &exp
->ip
.tag
;
2039 static int die__process_function(Dwarf_Die
*die
, struct ftype
*ftype
,
2040 struct lexblock
*lexblock
, struct cu
*cu
, struct conf_load
*conf
)
2046 if (!dwarf_haschildren(die
) || dwarf_child(die
, &child
) != 0)
2053 switch (dwarf_tag(die
)) {
2054 case DW_TAG_call_site
:
2055 case DW_TAG_call_site_parameter
:
2056 case DW_TAG_GNU_call_site
:
2057 case DW_TAG_GNU_call_site_parameter
:
2059 * XXX: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
2060 * and write proper support.
2062 * From a quick read there is not much we can use in
2063 * the existing dwarves tools, so just stop warning the user,
2064 * developers will find these notes if wanting to use in a
2068 case DW_TAG_dwarf_procedure
:
2070 * Ignore it, just scope expressions, that we have no use for (so far).
2073 #ifdef STB_GNU_UNIQUE
2074 case DW_TAG_GNU_formal_parameter_pack
:
2075 case DW_TAG_GNU_template_parameter_pack
:
2076 case DW_TAG_GNU_template_template_param
:
2078 case DW_TAG_template_type_parameter
:
2079 case DW_TAG_template_value_parameter
:
2080 /* FIXME: probably we'll have to attach this as a list of
2081 * template parameters to use at class__fprintf time...
2082 * See die__process_class */
2083 tag__print_not_supported(dwarf_tag(die
));
2085 case DW_TAG_formal_parameter
:
2086 tag
= die__create_new_parameter(die
, ftype
, lexblock
, cu
, conf
, param_idx
++);
2088 case DW_TAG_variable
:
2089 tag
= die__create_new_variable(die
, cu
, conf
);
2092 lexblock__add_variable(lexblock
, tag__variable(tag
));
2094 case DW_TAG_unspecified_parameters
:
2096 ftype
->unspec_parms
= 1;
2099 if (conf
->ignore_labels
)
2101 tag
= die__create_new_label(die
, lexblock
, cu
, conf
);
2103 case DW_TAG_inlined_subroutine
:
2104 if (conf
->ignore_inline_expansions
)
2106 tag
= die__create_new_inline_expansion(die
, lexblock
, cu
, conf
);
2108 case DW_TAG_lexical_block
:
2109 // lexblocks can contain types that are then referenced from outside.
2110 // Thus we can't ignore them without more surgery, i.e. by adding code
2111 // to just process types inside lexblocks, leave this for later.
2112 if (die__create_new_lexblock(die
, cu
, lexblock
, conf
) != 0)
2115 case DW_TAG_LLVM_annotation
:
2116 if (add_llvm_annotation(die
, -1, conf
, &(tag__function(&ftype
->tag
)->annots
)))
2120 tag
= die__process_tag(die
, cu
, 0, conf
);
2125 if (tag
== &unsupported_tag
) {
2126 tag__print_not_supported(dwarf_tag(die
));
2130 if (cu__add_tag(cu
, tag
, &id
) < 0)
2131 goto out_delete_tag
;
2139 if (cu__table_add_tag(cu
, tag
, &id
) < 0)
2140 goto out_delete_tag
;
2143 struct dwarf_tag
*dtag
= tag
->priv
;
2144 dtag
->small_id
= id
;
2145 } while (dwarf_siblingof(die
, die
) == 0);
2154 static struct tag
*die__create_new_function(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2156 struct function
*function
= function__new(die
, cu
, conf
);
2158 if (function
!= NULL
&&
2159 die__process_function(die
, &function
->proto
, &function
->lexblock
, cu
, conf
) != 0) {
2160 function__delete(function
);
2164 return function
? &function
->proto
.tag
: NULL
;
2167 static struct tag
*__die__process_tag(Dwarf_Die
*die
, struct cu
*cu
,
2168 int top_level
, const char *fn
, struct conf_load
*conf
)
2172 switch (dwarf_tag(die
)) {
2173 case DW_TAG_imported_unit
:
2174 return NULL
; // We don't support imported units yet, so to avoid segfaults
2175 case DW_TAG_array_type
:
2176 tag
= die__create_new_array(die
, cu
); break;
2177 case DW_TAG_string_type
: // FORTRAN stuff, looks like an array
2178 tag
= die__create_new_string_type(die
, cu
); break;
2179 case DW_TAG_base_type
:
2180 tag
= die__create_new_base_type(die
, cu
, conf
); break;
2181 case DW_TAG_const_type
:
2182 case DW_TAG_imported_declaration
:
2183 case DW_TAG_imported_module
:
2184 case DW_TAG_reference_type
:
2185 case DW_TAG_restrict_type
:
2186 case DW_TAG_volatile_type
:
2187 case DW_TAG_atomic_type
:
2188 tag
= die__create_new_tag(die
, cu
); break;
2189 case DW_TAG_unspecified_type
:
2190 tag
= die__create_new_tag(die
, cu
); break;
2191 case DW_TAG_pointer_type
:
2192 tag
= die__create_new_pointer_tag(die
, cu
, conf
); break;
2193 case DW_TAG_ptr_to_member_type
:
2194 tag
= die__create_new_ptr_to_member_type(die
, cu
); break;
2195 case DW_TAG_enumeration_type
:
2196 tag
= die__create_new_enumeration(die
, cu
, conf
); break;
2197 case DW_TAG_namespace
:
2198 tag
= die__create_new_namespace(die
, cu
, conf
); break;
2199 case DW_TAG_class_type
:
2200 case DW_TAG_interface_type
:
2201 case DW_TAG_structure_type
:
2202 tag
= die__create_new_class(die
, cu
, conf
); break;
2203 case DW_TAG_subprogram
:
2204 tag
= die__create_new_function(die
, cu
, conf
); break;
2205 case DW_TAG_subroutine_type
:
2206 tag
= die__create_new_subroutine_type(die
, cu
, conf
); break;
2207 case DW_TAG_rvalue_reference_type
:
2208 case DW_TAG_typedef
:
2209 tag
= die__create_new_typedef(die
, cu
, conf
); break;
2210 case DW_TAG_union_type
:
2211 tag
= die__create_new_union(die
, cu
, conf
); break;
2212 case DW_TAG_variable
:
2213 tag
= die__create_new_variable(die
, cu
, conf
); break;
2214 case DW_TAG_constant
: // First seen in a Go CU
2215 tag
= die__create_new_constant(die
, cu
, conf
); break;
2217 __cu__tag_not_handled(die
, fn
);
2219 case DW_TAG_dwarf_procedure
:
2221 * Ignore it, just scope expressions, that we have no use for (so far).
2223 tag
= &unsupported_tag
;
2226 if (conf
->ignore_labels
)
2227 tag
= &unsupported_tag
; // callers will assume conf->ignore_labels is true
2228 else // We can have labels in asm CUs, no lexblock
2229 tag
= die__create_new_label(die
, NULL
, cu
, conf
);
2234 tag
->top_level
= top_level
;
2239 static int die__process_unit(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2242 struct tag
*tag
= die__process_tag(die
, cu
, 1, conf
);
2246 if (tag
== &unsupported_tag
) {
2247 // XXX special case DW_TAG_dwarf_procedure, appears when looking at a recent ~/bin/perf
2248 // Investigate later how to properly support this...
2249 if (dwarf_tag(die
) != DW_TAG_dwarf_procedure
&&
2250 dwarf_tag(die
) != DW_TAG_label
) // conf->ignore_labels == true, see die__process_tag()
2251 tag__print_not_supported(dwarf_tag(die
));
2256 /* There is no BTF representation for unspecified types.
2257 * Currently we want such types to be represented as `void`
2258 * (and thus skip BTF encoding).
2260 * As BTF encoding is skipped, such types must not be added to type table,
2261 * otherwise an ID for a type would be allocated and we would be forced
2262 * to put something in BTF at this ID.
2263 * Thus avoid `cu__add_tag()` call for such types.
2265 * On the other hand, there might be references to this type from other
2266 * tags, so `dwarf_cu__find_tag_by_ref()` must return something.
2267 * Thus call `cu__hash()` for such types.
2269 * Note, that small_id of zero would be assigned to unspecified type entry.
2271 if (tag
->tag
!= DW_TAG_unspecified_type
)
2272 cu__add_tag(cu
, tag
, &id
);
2274 struct dwarf_tag
*dtag
= tag
->priv
;
2275 dtag
->small_id
= id
;
2276 } while (dwarf_siblingof(die
, die
) == 0);
2281 static void ftype__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
);
2283 static int namespace__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
)
2286 struct dwarf_cu
*dcu
= cu
->priv
;
2287 struct namespace *ns
= tag__namespace(tag
);
2289 namespace__for_each_tag(ns
, pos
) {
2290 struct dwarf_tag
*dtype
;
2291 struct dwarf_tag
*dpos
= pos
->priv
;
2293 if (tag__has_namespace(pos
)) {
2294 if (namespace__recode_dwarf_types(pos
, cu
))
2300 case DW_TAG_member
: {
2301 struct class_member
*member
= tag__class_member(pos
);
2303 * We may need to recode the type, possibly creating a
2304 * suitably sized new base_type
2306 if (member
->bitfield_size
!= 0 && !no_bitfield_type_recode
) {
2307 if (class_member__dwarf_recode_bitfield(member
, cu
))
2313 case DW_TAG_subroutine_type
:
2314 case DW_TAG_subprogram
:
2315 ftype__recode_dwarf_types(pos
, cu
);
2317 case DW_TAG_imported_module
:
2318 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->type
);
2320 /* Can be for both types and non types */
2321 case DW_TAG_imported_declaration
:
2322 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->type
);
2328 if (dpos
->type
.off
== 0) /* void */
2331 dtype
= dwarf_cu__find_type_by_ref(dcu
, &dpos
->type
);
2333 if (dtype
== NULL
) {
2334 tag__print_type_not_found(pos
);
2338 pos
->type
= dtype
->small_id
;
2343 static void type__recode_dwarf_specification(struct tag
*tag
, struct cu
*cu
)
2345 struct dwarf_tag
*dtype
;
2346 struct type
*t
= tag__type(tag
);
2347 dwarf_off_ref specification
= dwarf_tag__spec(tag
->priv
);
2349 if (t
->namespace.name
!= 0 || specification
.off
== 0)
2352 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, &specification
);
2354 t
->namespace.name
= tag__namespace(dtype
->tag
)->name
;
2356 struct dwarf_tag
*dtag
= tag
->priv
;
2359 "%s: couldn't find name for "
2360 "class %#llx, specification=%#llx\n", __func__
,
2361 (unsigned long long)dtag
->id
,
2362 (unsigned long long)specification
.off
);
2366 static void __tag__print_abstract_origin_not_found(struct tag
*tag
,
2367 const char *func
, int line
)
2369 struct dwarf_tag
*dtag
= tag
->priv
;
2371 "%s(%d): couldn't find %#llx abstract_origin for %#llx (%s)!\n",
2372 func
, line
, (unsigned long long)dtag
->abstract_origin
.off
,
2373 (unsigned long long)dtag
->id
,
2374 dwarf_tag_name(tag
->tag
));
2377 #define tag__print_abstract_origin_not_found(tag) \
2378 __tag__print_abstract_origin_not_found(tag, __func__, __LINE__)
2380 static void ftype__recode_dwarf_types(struct tag
*tag
, struct cu
*cu
)
2382 struct parameter
*pos
;
2383 struct dwarf_cu
*dcu
= cu
->priv
;
2384 struct ftype
*type
= tag__ftype(tag
);
2386 ftype__for_each_parameter(type
, pos
) {
2387 struct dwarf_tag
*dpos
= pos
->tag
.priv
;
2388 struct parameter
*opos
;
2389 struct dwarf_tag
*dtype
;
2391 if (dpos
->type
.off
== 0) {
2392 if (dpos
->abstract_origin
.off
== 0) {
2393 /* Function without parameters */
2397 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->abstract_origin
);
2398 if (dtype
== NULL
) {
2399 tag__print_abstract_origin_not_found(&pos
->tag
);
2402 opos
= tag__parameter(dtype
->tag
);
2403 pos
->name
= opos
->name
;
2404 pos
->tag
.type
= dtype
->tag
->type
;
2405 /* share location information between parameter and
2406 * abstract origin; if neither have location, we will
2407 * mark the parameter as optimized out. Also share
2408 * info regarding unexpected register use for
2412 opos
->has_loc
= pos
->has_loc
;
2415 opos
->optimized
= pos
->optimized
;
2416 if (pos
->unexpected_reg
)
2417 opos
->unexpected_reg
= pos
->unexpected_reg
;
2421 dtype
= dwarf_cu__find_type_by_ref(dcu
, &dpos
->type
);
2422 if (dtype
== NULL
) {
2423 tag__print_type_not_found(&pos
->tag
);
2426 pos
->tag
.type
= dtype
->small_id
;
2430 static void lexblock__recode_dwarf_types(struct lexblock
*tag
, struct cu
*cu
)
2433 struct dwarf_cu
*dcu
= cu
->priv
;
2435 list_for_each_entry(pos
, &tag
->tags
, node
) {
2436 struct dwarf_tag
*dpos
= pos
->priv
;
2437 struct dwarf_tag
*dtype
;
2440 case DW_TAG_lexical_block
:
2441 lexblock__recode_dwarf_types(tag__lexblock(pos
), cu
);
2443 case DW_TAG_inlined_subroutine
:
2444 if (dpos
->type
.off
!= 0)
2445 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->type
);
2447 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->abstract_origin
);
2448 if (dtype
== NULL
) {
2449 if (dpos
->type
.off
!= 0)
2450 tag__print_type_not_found(pos
);
2452 tag__print_abstract_origin_not_found(pos
);
2455 ftype__recode_dwarf_types(dtype
->tag
, cu
);
2458 case DW_TAG_formal_parameter
:
2459 if (dpos
->type
.off
!= 0)
2462 struct parameter
*fp
= tag__parameter(pos
);
2463 dtype
= dwarf_cu__find_tag_by_ref(dcu
,
2464 &dpos
->abstract_origin
);
2465 if (dtype
== NULL
) {
2466 tag__print_abstract_origin_not_found(pos
);
2469 fp
->name
= tag__parameter(dtype
->tag
)->name
;
2470 pos
->type
= dtype
->tag
->type
;
2473 case DW_TAG_variable
:
2474 if (dpos
->type
.off
!= 0)
2477 struct variable
*var
= tag__variable(pos
);
2479 if (dpos
->abstract_origin
.off
== 0) {
2481 * DW_TAG_variable completely empty was
2482 * found on libQtGui.so.4.3.4.debug
2483 * <3><d6ea1>: Abbrev Number: 164 (DW_TAG_variable)
2488 dtype
= dwarf_cu__find_tag_by_ref(dcu
,
2489 &dpos
->abstract_origin
);
2490 if (dtype
== NULL
) {
2491 tag__print_abstract_origin_not_found(pos
);
2494 var
->name
= tag__variable(dtype
->tag
)->name
;
2495 pos
->type
= dtype
->tag
->type
;
2498 case DW_TAG_label
: {
2499 struct label
*l
= tag__label(pos
);
2501 if (dpos
->abstract_origin
.off
== 0)
2504 dtype
= dwarf_cu__find_tag_by_ref(dcu
, &dpos
->abstract_origin
);
2506 l
->name
= tag__label(dtype
->tag
)->name
;
2508 tag__print_abstract_origin_not_found(pos
);
2513 dtype
= dwarf_cu__find_type_by_ref(dcu
, &dpos
->type
);
2514 if (dtype
== NULL
) {
2515 tag__print_type_not_found(pos
);
2518 pos
->type
= dtype
->small_id
;
2522 static void dwarf_cu__recode_btf_type_tag_ptr(struct btf_type_tag_ptr_type
*tag
,
2523 uint32_t pointee_type
)
2525 struct btf_type_tag_type
*annot
;
2526 struct dwarf_tag
*annot_dtag
;
2527 struct tag
*prev_tag
;
2529 /* Given source like
2530 * int tag1 tag2 tag3 *p;
2531 * the tag->tags contains tag3 -> tag2 -> tag1, the final type chain looks like:
2532 * pointer -> tag3 -> tag2 -> tag1 -> pointee
2534 * Basically it means
2535 * - '*' applies to "int tag1 tag2 tag3"
2536 * - tag3 applies to "int tag1 tag2"
2537 * - tag2 applies to "int tag1"
2538 * - tag1 applies to "int"
2540 * This also makes final source code (format c) easier as we can do
2541 * emit for "tag3 -> tag2 -> tag1 -> int"
2544 * For 'tag3 -> tag2 -> tag1 -> int":
2545 * emit for "tag2 -> tag1 -> int"
2548 * Eventually we can get the source code like
2549 * int tag1 tag2 tag3 *p;
2550 * and this matches the user/kernel code.
2552 prev_tag
= &tag
->tag
;
2553 list_for_each_entry(annot
, &tag
->tags
, node
) {
2554 annot_dtag
= annot
->tag
.priv
;
2555 prev_tag
->type
= annot_dtag
->small_id
;
2556 prev_tag
= &annot
->tag
;
2558 prev_tag
->type
= pointee_type
;
2561 static int tag__recode_dwarf_type(struct tag
*tag
, struct cu
*cu
)
2563 struct dwarf_tag
*dtag
= tag
->priv
;
2564 struct dwarf_tag
*dtype
;
2566 /* Check if this is an already recoded bitfield */
2570 if (tag__is_type(tag
))
2571 type__recode_dwarf_specification(tag
, cu
);
2573 if (tag__has_namespace(tag
))
2574 return namespace__recode_dwarf_types(tag
, cu
);
2577 case DW_TAG_subprogram
: {
2578 struct function
*fn
= tag__function(tag
);
2580 if (fn
->name
== 0) {
2581 dwarf_off_ref specification
= dwarf_tag__spec(dtag
);
2582 if (dtag
->abstract_origin
.off
== 0 &&
2583 specification
.off
== 0) {
2585 * Found on libQtGui.so.4.3.4.debug
2586 * <3><1423de>: Abbrev Number: 209 (DW_TAG_subprogram)
2587 * <1423e0> DW_AT_declaration : 1
2591 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, &dtag
->abstract_origin
);
2593 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, &specification
);
2595 fn
->name
= tag__function(dtype
->tag
)->name
;
2598 "%s: couldn't find name for "
2599 "function %#llx, abstract_origin=%#llx,"
2600 " specification=%#llx\n", __func__
,
2601 (unsigned long long)dtag
->id
,
2602 (unsigned long long)dtag
->abstract_origin
.off
,
2603 (unsigned long long)specification
.off
);
2606 lexblock__recode_dwarf_types(&fn
->lexblock
, cu
);
2610 case DW_TAG_subroutine_type
:
2611 ftype__recode_dwarf_types(tag
, cu
);
2612 /* Fall thru, for the function return type */
2615 case DW_TAG_lexical_block
:
2616 lexblock__recode_dwarf_types(tag__lexblock(tag
), cu
);
2619 case DW_TAG_ptr_to_member_type
: {
2620 struct ptr_to_member_type
*pt
= tag__ptr_to_member_type(tag
);
2622 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, &dtag
->containing_type
);
2624 pt
->containing_type
= dtype
->small_id
;
2627 "%s: couldn't find type for "
2628 "containing_type %#llx, containing_type=%#llx\n",
2630 (unsigned long long)dtag
->id
,
2631 (unsigned long long)dtag
->containing_type
.off
);
2636 case DW_TAG_namespace
:
2637 return namespace__recode_dwarf_types(tag
, cu
);
2638 /* Damn, DW_TAG_inlined_subroutine is an special case
2639 as dwarf_tag->id is in fact an abtract origin, i.e. must be
2640 looked up in the tags_table, not in the types_table.
2641 The others also point to routines, so are in tags_table */
2642 case DW_TAG_inlined_subroutine
:
2643 case DW_TAG_imported_module
:
2644 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, &dtag
->type
);
2646 /* Can be for both types and non types */
2647 case DW_TAG_imported_declaration
:
2648 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
, &dtag
->type
);
2652 case DW_TAG_variable
: {
2653 struct variable
*var
= tag__variable(tag
);
2655 if (var
->has_specification
) {
2656 dwarf_off_ref specification
= dwarf_tag__spec(dtag
);
2658 if (specification
.off
) {
2659 dtype
= dwarf_cu__find_tag_by_ref(cu
->priv
,
2662 var
->spec
= tag__variable(dtype
->tag
);
2669 if (dtag
->type
.off
== 0) {
2670 if (tag
->tag
!= DW_TAG_pointer_type
|| !tag
->has_btf_type_tag
)
2671 tag
->type
= 0; /* void */
2673 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag
), 0);
2678 dtype
= dwarf_cu__find_type_by_ref(cu
->priv
, &dtag
->type
);
2680 if (dtype
== NULL
) {
2681 tag__print_type_not_found(tag
);
2685 if (tag
->tag
!= DW_TAG_pointer_type
|| !tag
->has_btf_type_tag
)
2686 tag
->type
= dtype
->small_id
;
2688 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag
), dtype
->small_id
);
2693 static bool param__is_struct(struct cu
*cu
, struct tag
*tag
)
2695 struct tag
*type
= cu__type(cu
, tag
->type
);
2700 switch (type
->tag
) {
2701 case DW_TAG_structure_type
:
2703 case DW_TAG_const_type
:
2704 case DW_TAG_typedef
:
2705 /* handle "typedef struct", const parameter */
2706 return param__is_struct(cu
, type
);
2712 static int cu__resolve_func_ret_types_optimized(struct cu
*cu
)
2714 struct ptr_table
*pt
= &cu
->functions_table
;
2717 for (i
= 0; i
< pt
->nr_entries
; ++i
) {
2718 struct tag
*tag
= pt
->entries
[i
];
2719 struct parameter
*pos
;
2720 struct function
*fn
= tag__function(tag
);
2721 bool has_unexpected_reg
= false, has_struct_param
= false;
2723 /* mark function as optimized if parameter is, or
2724 * if parameter does not have a location; at this
2725 * point location presence has been marked in
2726 * abstract origins for cases where a parameter
2727 * location is not stored in the original function
2730 * Also mark functions which, due to optimization,
2731 * use an unexpected register for a parameter.
2732 * Exception is functions which have a struct
2733 * as a parameter, as multiple registers may
2734 * be used to represent it, throwing off register
2735 * to parameter mapping.
2737 ftype__for_each_parameter(&fn
->proto
, pos
) {
2738 if (pos
->optimized
|| !pos
->has_loc
)
2739 fn
->proto
.optimized_parms
= 1;
2741 if (pos
->unexpected_reg
)
2742 has_unexpected_reg
= true;
2744 if (has_unexpected_reg
) {
2745 ftype__for_each_parameter(&fn
->proto
, pos
) {
2746 has_struct_param
= param__is_struct(cu
, &pos
->tag
);
2747 if (has_struct_param
)
2750 if (!has_struct_param
)
2751 fn
->proto
.unexpected_reg
= 1;
2754 if (tag
== NULL
|| tag
->type
!= 0)
2757 if (!fn
->abstract_origin
)
2760 struct dwarf_tag
*dtag
= tag
->priv
;
2761 struct dwarf_tag
*dfunc
;
2762 dfunc
= dwarf_cu__find_tag_by_ref(cu
->priv
, &dtag
->abstract_origin
);
2763 if (dfunc
== NULL
) {
2764 tag__print_abstract_origin_not_found(tag
);
2768 tag
->type
= dfunc
->tag
->type
;
2773 static int cu__recode_dwarf_types_table(struct cu
*cu
,
2774 struct ptr_table
*pt
,
2777 for (; i
< pt
->nr_entries
; ++i
) {
2778 struct tag
*tag
= pt
->entries
[i
];
2780 if (tag
!= NULL
) /* void, see cu__new */
2781 if (tag__recode_dwarf_type(tag
, cu
))
2788 static int cu__recode_dwarf_types(struct cu
*cu
)
2790 if (cu__recode_dwarf_types_table(cu
, &cu
->types_table
, 1) ||
2791 cu__recode_dwarf_types_table(cu
, &cu
->tags_table
, 0) ||
2792 cu__recode_dwarf_types_table(cu
, &cu
->functions_table
, 0))
2797 static const char *dwarf_tag__decl_file(const struct tag
*tag
,
2798 const struct cu
*cu
)
2800 struct dwarf_tag
*dtag
= tag
->priv
;
2801 return cu
->extra_dbg_info
? dtag
->decl_file
: NULL
;
2804 static uint32_t dwarf_tag__decl_line(const struct tag
*tag
,
2805 const struct cu
*cu
)
2807 struct dwarf_tag
*dtag
= tag
->priv
;
2808 return cu
->extra_dbg_info
? dtag
->decl_line
: 0;
2811 static unsigned long long dwarf_tag__orig_id(const struct tag
*tag
,
2812 const struct cu
*cu
)
2814 struct dwarf_tag
*dtag
= tag
->priv
;
2815 return cu
->extra_dbg_info
? dtag
->id
: 0;
2818 struct debug_fmt_ops dwarf__ops
;
2820 static int die__process(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2823 const uint16_t tag
= dwarf_tag(die
);
2825 if (tag
== DW_TAG_skeleton_unit
) {
2829 fprintf(stderr
, "WARNING: DW_TAG_skeleton_unit used, please look for a .dwo file and use it instead.\n"
2830 " A future version of pahole will support do this automagically.\n");
2833 return 0; // so that other units can be processed
2836 if (tag
== DW_TAG_partial_unit
) {
2840 fprintf(stderr
, "WARNING: DW_TAG_partial_unit used, some types will not be considered!\n"
2841 " Probably this was optimized using a tool like 'dwz'\n"
2842 " A future version of pahole will support this.\n");
2845 return 0; // so that other units can be processed
2848 if (tag
!= DW_TAG_compile_unit
&& tag
!= DW_TAG_type_unit
) {
2849 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)!\n",
2850 __FUNCTION__
, dwarf_tag_name(tag
), tag
);
2854 cu
->language
= attr_numeric(die
, DW_AT_language
);
2856 if (dwarf_child(die
, &child
) == 0) {
2857 int err
= die__process_unit(&child
, cu
, conf
);
2862 if (dwarf_siblingof(die
, die
) == 0)
2863 fprintf(stderr
, "%s: got %s unexpected tag after "
2864 "DW_TAG_compile_unit!\n",
2865 __FUNCTION__
, dwarf_tag_name(tag
));
2870 static int die__process_and_recode(Dwarf_Die
*die
, struct cu
*cu
, struct conf_load
*conf
)
2872 int ret
= die__process(die
, cu
, conf
);
2875 ret
= cu__recode_dwarf_types(cu
);
2879 return cu__resolve_func_ret_types_optimized(cu
);
2882 static int class_member__cache_byte_size(struct tag
*tag
, struct cu
*cu
,
2885 struct class_member
*member
= tag__class_member(tag
);
2886 struct conf_load
*conf_load
= cookie
;
2888 if (tag__is_class_member(tag
)) {
2889 if (member
->is_static
)
2891 } else if (tag
->tag
!= DW_TAG_inheritance
) {
2895 if (member
->bitfield_size
== 0) {
2896 member
->byte_size
= tag__size(tag
, cu
);
2897 member
->bit_size
= member
->byte_size
* 8;
2902 * Try to figure out byte size, if it's not directly provided in DWARF
2904 if (member
->byte_size
== 0) {
2905 struct tag
*type
= tag__strip_typedefs_and_modifiers(&member
->tag
, cu
);
2906 member
->byte_size
= tag__size(type
, cu
);
2907 if (member
->byte_size
== 0) {
2909 if (tag__is_enumeration(type
)) {
2910 bit_size
= tag__type(type
)->size
;
2912 struct base_type
*bt
= tag__base_type(type
);
2913 bit_size
= bt
->bit_size
? bt
->bit_size
: base_type__name_to_size(bt
, cu
);
2915 member
->byte_size
= (bit_size
+ 7) / 8 * 8;
2918 member
->bit_size
= member
->byte_size
* 8;
2921 * XXX: after all the attempts to determine byte size, we might still
2922 * be unsuccessful, because base_type__name_to_size doesn't know about
2923 * the base_type name, so one has to add there when such base_type
2924 * isn't found. pahole will put zero on the struct output so it should
2925 * be easy to spot the name when such unlikely thing happens.
2927 if (member
->byte_size
== 0) {
2928 member
->bitfield_offset
= 0;
2932 if (!member
->has_bit_offset
) {
2934 * For little-endian architectures, DWARF data emitted by gcc/clang
2935 * specifies bitfield offset as an offset from the highest-order bit
2936 * of an underlying integral type (e.g., int) to a highest-order bit
2937 * of a bitfield. E.g., for bitfield taking first 5 bits of int-backed
2938 * bitfield, bit offset will be 27 (sizeof(int) - 0 offset - 5 bit
2939 * size), which is very counter-intuitive and isn't a natural
2940 * extension of byte offset, which on little-endian points to
2941 * lowest-order byte. So here we re-adjust bitfield offset to be an
2942 * offset from lowest-order bit of underlying integral type to
2943 * a lowest-order bit of a bitfield. This makes bitfield offset
2944 * a natural extension of byte offset for bitfields and is uniform
2945 * with how big-endian bit offsets work.
2947 if (cu
->little_endian
)
2948 member
->bitfield_offset
= member
->bit_size
- member
->bitfield_offset
- member
->bitfield_size
;
2950 member
->bit_offset
= member
->byte_offset
* 8 + member
->bitfield_offset
;
2952 // DWARF5 has DW_AT_data_bit_offset, offset in bits from the
2953 // start of the container type (struct, class, etc).
2954 member
->byte_offset
= member
->bit_offset
/ 8;
2955 member
->bitfield_offset
= member
->bit_offset
- member
->byte_offset
* 8;
2958 /* make sure bitfield offset is non-negative */
2959 if (member
->bitfield_offset
< 0) {
2960 member
->bitfield_offset
+= member
->bit_size
;
2961 member
->byte_offset
-= member
->byte_size
;
2962 member
->bit_offset
= member
->byte_offset
* 8 + member
->bitfield_offset
;
2964 /* align on underlying base type natural alignment boundary */
2965 member
->bitfield_offset
+= (member
->byte_offset
% member
->byte_size
) * 8;
2966 member
->byte_offset
= member
->bit_offset
/ member
->bit_size
* member
->bit_size
/ 8;
2967 if (member
->bitfield_offset
>= member
->bit_size
) {
2968 member
->bitfield_offset
-= member
->bit_size
;
2969 member
->byte_offset
+= member
->byte_size
;
2972 if (conf_load
&& conf_load
->fixup_silly_bitfields
&&
2973 member
->byte_size
== 8 * member
->bitfield_size
) {
2974 member
->bitfield_size
= 0;
2975 member
->bitfield_offset
= 0;
2981 static bool cu__language_reorders_offsets(const struct cu
*cu
)
2983 return cu
->language
== DW_LANG_Rust
;
2986 static int type__sort_by_offset(struct tag
*tag
, struct cu
*cu
, void *cookie __maybe_unused
)
2988 if (!tag__is_type(tag
))
2991 struct type
*type
= tag__type(tag
);
2992 struct class_member
*current_member
;
2994 // There may be more than DW_TAG_members entries in the type tags, so do a simple
2995 // bubble sort for now, so that the other non tags stay where they are.
2997 type__for_each_data_member(type
, current_member
) {
2998 if (list_is_last(¤t_member
->tag
.node
, &type
->namespace.tags
))
3001 struct class_member
*next_member
= list_entry(current_member
->tag
.node
.next
, typeof(*current_member
), tag
.node
);
3003 if (current_member
->byte_offset
<= next_member
->byte_offset
)
3006 list_del(¤t_member
->tag
.node
);
3007 list_add(¤t_member
->tag
.node
, &next_member
->tag
.node
);
3014 static void cu__sort_types_by_offset(struct cu
*cu
, struct conf_load
*conf
)
3016 cu__for_all_tags(cu
, type__sort_by_offset
, conf
);
3019 static int cu__finalize(struct cu
*cu
, struct cus
*cus
, struct conf_load
*conf
, void *thr_data
)
3021 cu__for_all_tags(cu
, class_member__cache_byte_size
, conf
);
3023 if (cu__language_reorders_offsets(cu
))
3024 cu__sort_types_by_offset(cu
, conf
);
3026 cus__set_cu_state(cus
, cu
, CU__LOADED
);
3028 if (conf
&& conf
->steal
) {
3029 return conf
->steal(cu
, conf
, thr_data
);
3034 static int cus__finalize(struct cus
*cus
, struct cu
*cu
, struct conf_load
*conf
, void *thr_data
)
3036 int lsk
= cu__finalize(cu
, cus
, conf
, thr_data
);
3039 cus__remove(cus
, cu
);
3042 case LSK__STOP_LOADING
:
3050 static int cu__set_common(struct cu
*cu
, struct conf_load
*conf
,
3051 Dwfl_Module
*mod
, Elf
*elf
)
3053 cu
->uses_global_strings
= true;
3056 cu
->extra_dbg_info
= conf
? conf
->extra_dbg_info
: 0;
3057 cu
->has_addr_info
= conf
? conf
->get_addr_info
: 0;
3060 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
3061 return DWARF_CB_ABORT
;
3063 cu
->little_endian
= ehdr
.e_ident
[EI_DATA
] == ELFDATA2LSB
;
3064 cu
->nr_register_params
= arch__nr_register_params(&ehdr
);
3065 arch__set_register_params(&ehdr
, cu
);
3069 static int __cus__load_debug_types(struct cus
*cus
, struct conf_load
*conf
, Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3070 const char *filename
, const unsigned char *build_id
,
3071 int build_id_len
, struct cu
**cup
, struct dwarf_cu
*dcup
)
3073 Dwarf_Off off
= 0, noff
, type_off
;
3075 uint8_t pointer_size
, offset_size
;
3080 while (dwarf_next_unit(dw
, off
, &noff
, &cuhl
, NULL
, NULL
, &pointer_size
,
3081 &offset_size
, &signature
, &type_off
)
3087 cu
= cu__new("", pointer_size
, build_id
,
3088 build_id_len
, filename
, conf
->use_obstack
);
3090 cu__set_common(cu
, conf
, mod
, elf
) != 0) {
3091 return DWARF_CB_ABORT
;
3094 if (dwarf_cu__init(dcup
, cu
) != 0)
3095 return DWARF_CB_ABORT
;
3098 dcup
->type_unit
= dcup
;
3100 cu
->dfops
= &dwarf__ops
;
3107 Dwarf_Die
*cu_die
= dwarf_offdie_types(dw
, off
+ cuhl
,
3110 if (die__process(cu_die
, *cup
, conf
) != 0)
3111 return DWARF_CB_ABORT
;
3116 if (*cup
!= NULL
&& cu__recode_dwarf_types(*cup
) != 0)
3117 return DWARF_CB_ABORT
;
3122 /* Match the define in linux:include/linux/elfnote-lto.h */
3123 #define LINUX_ELFNOTE_LTO_INFO 0x101
3125 static bool cus__merging_cu(Dwarf
*dw
, Elf
*elf
)
3127 Elf_Scn
*section
= NULL
;
3128 while ((section
= elf_nextscn(elf
, section
)) != 0) {
3130 if (!gelf_getshdr(section
, &header
))
3133 if (header
.sh_type
!= SHT_NOTE
)
3136 Elf_Data
*data
= NULL
;
3137 while ((data
= elf_getdata(section
, data
)) != 0) {
3138 size_t name_off
, desc_off
, offset
= 0;
3140 while ((offset
= gelf_getnote(data
, offset
, &hdr
, &name_off
, &desc_off
)) != 0) {
3141 if (hdr
.n_type
!= LINUX_ELFNOTE_LTO_INFO
)
3144 /* owner is Linux */
3145 if (strcmp((char *)data
->d_buf
+ name_off
, "Linux") != 0)
3148 return *(int *)(data
->d_buf
+ desc_off
) != 0;
3153 Dwarf_Off off
= 0, noff
;
3156 while (dwarf_nextcu (dw
, off
, &noff
, &cuhl
, NULL
, NULL
, NULL
) == 0) {
3158 Dwarf_Die
*cu_die
= dwarf_offdie(dw
, off
+ cuhl
, &die_mem
);
3163 Dwarf_Off offset
= 0;
3166 Dwarf_Abbrev
*abbrev
= dwarf_getabbrev (cu_die
, offset
, &length
);
3167 if (abbrev
== NULL
|| abbrev
== DWARF_END_ABBREV
)
3171 if (dwarf_getattrcnt (abbrev
, &attrcnt
) != 0)
3174 unsigned int attr_num
, attr_form
;
3177 for (j
= 0; j
< attrcnt
; ++j
) {
3178 if (dwarf_getabbrevattr (abbrev
, j
, &attr_num
, &attr_form
,
3181 if (attr_form
== DW_FORM_ref_addr
)
3196 struct conf_load
*conf
;
3200 const char *filename
;
3202 const unsigned char *build_id
;
3205 struct dwarf_cu
*type_dcu
;
3208 struct dwarf_thread
{
3209 struct dwarf_cus
*dcus
;
3213 static struct dwarf_cu
*dwarf_cus__create_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
, uint8_t pointer_size
)
3216 * DW_AT_name in DW_TAG_compile_unit can be NULL, first seen in:
3218 * /usr/libexec/gcc/x86_64-redhat-linux/4.3.2/ecj1.debug
3220 const char *name
= attr_string(cu_die
, DW_AT_name
, dcus
->conf
);
3221 struct cu
*cu
= cu__new(name
?: "", pointer_size
, dcus
->build_id
, dcus
->build_id_len
, dcus
->filename
, dcus
->conf
->use_obstack
);
3222 if (cu
== NULL
|| cu__set_common(cu
, dcus
->conf
, dcus
->mod
, dcus
->elf
) != 0)
3225 struct dwarf_cu
*dcu
= dwarf_cu__new(cu
);
3232 dcu
->type_unit
= dcus
->type_dcu
;
3234 cu
->dfops
= &dwarf__ops
;
3239 static int dwarf_cus__process_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
,
3240 struct cu
*cu
, void *thr_data
)
3242 if (die__process_and_recode(cu_die
, cu
, dcus
->conf
) != 0 ||
3243 cus__finalize(dcus
->cus
, cu
, dcus
->conf
, thr_data
) == LSK__STOP_LOADING
)
3244 return DWARF_CB_ABORT
;
3249 static int dwarf_cus__create_and_process_cu(struct dwarf_cus
*dcus
, Dwarf_Die
*cu_die
, uint8_t pointer_size
)
3251 struct dwarf_cu
*dcu
= dwarf_cus__create_cu(dcus
, cu_die
, pointer_size
);
3254 return DWARF_CB_ABORT
;
3256 cus__add(dcus
->cus
, dcu
->cu
);
3258 return dwarf_cus__process_cu(dcus
, cu_die
, dcu
->cu
, NULL
);
3261 static int dwarf_cus__nextcu(struct dwarf_cus
*dcus
, struct dwarf_cu
**dcu
,
3262 Dwarf_Die
*die_mem
, Dwarf_Die
**cu_die
,
3263 uint8_t *pointer_size
, uint8_t *offset_size
)
3269 cus__lock(dcus
->cus
);
3276 ret
= dwarf_nextcu(dcus
->dw
, dcus
->off
, &noff
, &cuhl
, NULL
, pointer_size
, offset_size
);
3278 *cu_die
= dwarf_offdie(dcus
->dw
, dcus
->off
+ cuhl
, die_mem
);
3279 if (*cu_die
!= NULL
)
3283 if (ret
== 0 && *cu_die
!= NULL
) {
3284 *dcu
= dwarf_cus__create_cu(dcus
, *cu_die
, *pointer_size
);
3286 dcus
->error
= ENOMEM
;
3290 // Do it here to keep all CUs in cus->cus in the same
3291 // order as in the DWARF file being loaded (e.g. vmlinux)
3292 __cus__add(dcus
->cus
, (*dcu
)->cu
);
3296 cus__unlock(dcus
->cus
);
3301 static void *dwarf_cus__process_cu_thread(void *arg
)
3303 struct dwarf_thread
*dthr
= arg
;
3304 struct dwarf_cus
*dcus
= dthr
->dcus
;
3305 uint8_t pointer_size
, offset_size
;
3306 Dwarf_Die die_mem
, *cu_die
;
3307 struct dwarf_cu
*dcu
;
3309 while (dwarf_cus__nextcu(dcus
, &dcu
, &die_mem
, &cu_die
, &pointer_size
, &offset_size
) == 0) {
3313 if (dwarf_cus__process_cu(dcus
, cu_die
, dcu
->cu
, dthr
->data
) == DWARF_CB_ABORT
)
3317 if (dcus
->conf
->thread_exit
&&
3318 dcus
->conf
->thread_exit(dcus
->conf
, dthr
->data
) != 0)
3321 return (void *)DWARF_CB_OK
;
3323 return (void *)DWARF_CB_ABORT
;
3326 static int dwarf_cus__threaded_process_cus(struct dwarf_cus
*dcus
)
3328 pthread_t threads
[dcus
->conf
->nr_jobs
];
3329 struct dwarf_thread dthr
[dcus
->conf
->nr_jobs
];
3330 void *thread_data
[dcus
->conf
->nr_jobs
];
3334 if (dcus
->conf
->threads_prepare
) {
3335 res
= dcus
->conf
->threads_prepare(dcus
->conf
, dcus
->conf
->nr_jobs
, thread_data
);
3339 memset(thread_data
, 0, sizeof(void *) * dcus
->conf
->nr_jobs
);
3342 for (i
= 0; i
< dcus
->conf
->nr_jobs
; ++i
) {
3343 dthr
[i
].dcus
= dcus
;
3344 dthr
[i
].data
= thread_data
[i
];
3346 dcus
->error
= pthread_create(&threads
[i
], NULL
,
3347 dwarf_cus__process_cu_thread
,
3358 int err
= pthread_join(threads
[i
], &res
);
3360 if (err
== 0 && res
!= NULL
)
3361 dcus
->error
= (long)res
;
3364 if (dcus
->conf
->threads_collect
) {
3365 res
= dcus
->conf
->threads_collect(dcus
->conf
, dcus
->conf
->nr_jobs
,
3366 thread_data
, dcus
->error
);
3367 if (dcus
->error
== 0)
3374 static int __dwarf_cus__process_cus(struct dwarf_cus
*dcus
)
3376 uint8_t pointer_size
, offset_size
;
3380 while (dwarf_nextcu(dcus
->dw
, dcus
->off
, &noff
, &cuhl
, NULL
, &pointer_size
, &offset_size
) == 0) {
3382 Dwarf_Die
*cu_die
= dwarf_offdie(dcus
->dw
, dcus
->off
+ cuhl
, &die_mem
);
3387 if (dwarf_cus__create_and_process_cu(dcus
, cu_die
, pointer_size
) == DWARF_CB_ABORT
)
3388 return DWARF_CB_ABORT
;
3396 static int dwarf_cus__process_cus(struct dwarf_cus
*dcus
)
3398 if (dcus
->conf
->nr_jobs
> 1)
3399 return dwarf_cus__threaded_process_cus(dcus
);
3401 return __dwarf_cus__process_cus(dcus
);
3404 static int cus__merge_and_process_cu(struct cus
*cus
, struct conf_load
*conf
,
3405 Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3406 const char *filename
,
3407 const unsigned char *build_id
,
3409 struct dwarf_cu
*type_dcu
)
3411 uint8_t pointer_size
, offset_size
;
3412 struct dwarf_cu
*dcu
= NULL
;
3413 Dwarf_Off off
= 0, noff
;
3414 struct cu
*cu
= NULL
;
3417 while (dwarf_nextcu(dw
, off
, &noff
, &cuhl
, NULL
, &pointer_size
,
3418 &offset_size
) == 0) {
3420 Dwarf_Die
*cu_die
= dwarf_offdie(dw
, off
+ cuhl
, &die_mem
);
3426 cu
= cu__new("", pointer_size
, build_id
, build_id_len
,
3427 filename
, conf
->use_obstack
);
3428 if (cu
== NULL
|| cu__set_common(cu
, conf
, mod
, elf
) != 0)
3431 dcu
= zalloc(sizeof(*dcu
));
3435 /* Merged cu tends to need a lot more memory.
3436 * Let us start with max_hashtags__bits and
3437 * go down to find a proper hashtag bit value.
3439 uint32_t default_hbits
= hashtags__bits
;
3440 for (hashtags__bits
= max_hashtags__bits
;
3441 hashtags__bits
>= default_hbits
;
3443 if (dwarf_cu__init(dcu
, cu
) == 0)
3446 if (hashtags__bits
< default_hbits
)
3450 dcu
->type_unit
= type_dcu
;
3452 cu
->dfops
= &dwarf__ops
;
3453 cu
->language
= attr_numeric(cu_die
, DW_AT_language
);
3457 if (dwarf_child(cu_die
, &child
) == 0) {
3458 if (die__process_unit(&child
, cu
, conf
) != 0)
3468 /* process merged cu */
3469 if (cu__recode_dwarf_types(cu
) != LSK__KEEPIT
)
3473 * for lto build, the function return type may not be
3474 * resolved due to the return type of a subprogram is
3475 * encoded in another subprogram through abstract_origin
3476 * tag. Let us visit all subprograms again to resolve this.
3478 if (cu__resolve_func_ret_types_optimized(cu
) != LSK__KEEPIT
)
3481 if (cus__finalize(cus
, cu
, conf
, NULL
) == LSK__STOP_LOADING
)
3487 dwarf_cu__delete(cu
);
3489 return DWARF_CB_ABORT
;
3492 static int cus__load_module(struct cus
*cus
, struct conf_load
*conf
,
3493 Dwfl_Module
*mod
, Dwarf
*dw
, Elf
*elf
,
3494 const char *filename
)
3496 const unsigned char *build_id
= NULL
;
3497 #ifdef HAVE_DWFL_MODULE_BUILD_ID
3499 int build_id_len
= dwfl_module_build_id(mod
, &build_id
, &vaddr
);
3501 int build_id_len
= 0;
3504 struct dwarf_cu type_dcu
;
3505 int type_lsk
= LSK__KEEPIT
;
3507 int res
= __cus__load_debug_types(cus
, conf
, mod
, dw
, elf
, filename
, build_id
, build_id_len
, &type_cu
, &type_dcu
);
3512 if (type_cu
!= NULL
) {
3513 type_lsk
= cu__finalize(type_cu
, cus
, conf
, NULL
);
3514 if (type_lsk
== LSK__DELETE
) {
3515 cus__remove(cus
, type_cu
);
3519 if (cus__merging_cu(dw
, elf
)) {
3520 res
= cus__merge_and_process_cu(cus
, conf
, mod
, dw
, elf
, filename
,
3521 build_id
, build_id_len
,
3522 type_cu
? &type_dcu
: NULL
);
3524 struct dwarf_cus dcus
= {
3531 .filename
= filename
,
3532 .type_dcu
= type_cu
? &type_dcu
: NULL
,
3533 .build_id
= build_id
,
3534 .build_id_len
= build_id_len
,
3536 res
= dwarf_cus__process_cus(&dcus
);
3542 if (type_lsk
== LSK__DELETE
)
3543 cu__delete(type_cu
);
3548 struct process_dwflmod_parms
{
3550 struct conf_load
*conf
;
3551 const char *filename
;
3552 uint32_t nr_dwarf_sections_found
;
3555 static int cus__process_dwflmod(Dwfl_Module
*dwflmod
,
3556 void **userdata __maybe_unused
,
3557 const char *name __maybe_unused
,
3558 Dwarf_Addr base __maybe_unused
,
3561 struct process_dwflmod_parms
*parms
= arg
;
3562 struct cus
*cus
= parms
->cus
;
3566 * Does the relocation and saves the elf for later processing
3567 * by the stealer, such as pahole_stealer, so that it don't
3568 * have to create another Elf instance just to do things like
3569 * reading this ELF file symtab to do CTF encoding of the
3570 * DW_TAG_suprogram tags (functions).
3572 Elf
*elf
= dwfl_module_getelf(dwflmod
, &dwflbias
);
3575 Dwarf
*dw
= dwfl_module_getdwarf(dwflmod
, &dwbias
);
3577 int err
= DWARF_CB_OK
;
3579 ++parms
->nr_dwarf_sections_found
;
3580 err
= cus__load_module(cus
, parms
->conf
, dwflmod
, dw
, elf
,
3584 * XXX We will fall back to try finding other debugging
3585 * formats (CTF), so no point in telling this to the user
3586 * Use for debugging.
3589 * "%s: can't get debug context descriptor: %s\n",
3590 * __func__, dwfl_errmsg(-1));
3596 static void dwarf_loader__exit(struct cus
*cus
)
3598 Dwfl
*dwfl
= cus__priv(cus
);
3602 cus__set_priv(cus
, NULL
);
3606 static int cus__process_file(struct cus
*cus
, struct conf_load
*conf
, int fd
,
3607 const char *filename
)
3609 /* Duplicate an fd for dwfl_report_offline to swallow. */
3610 int dwfl_fd
= dup(fd
);
3616 * Use libdwfl in a trivial way to open the libdw handle for us.
3617 * This takes care of applying relocations to DWARF data in ET_REL
3621 static const Dwfl_Callbacks callbacks
= {
3622 .section_address
= dwfl_offline_section_address
,
3623 .find_debuginfo
= dwfl_standard_find_debuginfo
,
3624 /* We use this table for core files too. */
3625 .find_elf
= dwfl_build_id_find_elf
,
3628 Dwfl
*dwfl
= dwfl_begin(&callbacks
);
3630 cus__set_priv(cus
, dwfl
);
3631 cus__set_loader_exit(cus
, dwarf_loader__exit
);
3633 if (dwfl_report_offline(dwfl
, filename
, filename
, dwfl_fd
) == NULL
)
3636 dwfl_report_end(dwfl
, NULL
, NULL
);
3638 struct process_dwflmod_parms parms
= {
3641 .filename
= filename
,
3642 .nr_dwarf_sections_found
= 0,
3645 /* Process the one or more modules gleaned from this file. */
3646 int err
= dwfl_getmodules(dwfl
, cus__process_dwflmod
, &parms
, 0);
3650 // We can't call dwfl_end(dwfl) here, as we keep pointers to strings
3651 // allocated by libdw that will be freed at dwfl_end(), so leave this for
3653 return parms
.nr_dwarf_sections_found
? 0 : -1;
3656 static int dwarf__load_file(struct cus
*cus
, struct conf_load
*conf
,
3657 const char *filename
)
3661 if (conf
->max_hashtable_bits
!= 0) {
3662 if (conf
->max_hashtable_bits
> 31)
3665 max_hashtags__bits
= conf
->max_hashtable_bits
;
3668 if (conf
->hashtable_bits
!= 0) {
3669 if (conf
->hashtable_bits
> max_hashtags__bits
)
3672 hashtags__bits
= conf
->hashtable_bits
;
3673 } else if (hashtags__bits
> max_hashtags__bits
)
3676 elf_version(EV_CURRENT
);
3678 fd
= open(filename
, O_RDONLY
);
3683 err
= cus__process_file(cus
, conf
, fd
, filename
);
3689 struct debug_fmt_ops dwarf__ops
= {
3691 .load_file
= dwarf__load_file
,
3692 .tag__decl_file
= dwarf_tag__decl_file
,
3693 .tag__decl_line
= dwarf_tag__decl_line
,
3694 .tag__orig_id
= dwarf_tag__orig_id
,
3695 .cu__delete
= dwarf_cu__delete
,
3696 .has_alignment_info
= true,