pahole: Describe expected use of 'default' in the man page
[dwarves.git] / dwarf_loader.c
blobb15cf543fa9d7471b618fcd3e2a2b3a0170365b8
1 /*
2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>
5 */
7 #include <assert.h>
8 #include <dirent.h>
9 #include <dwarf.h>
10 #include <elfutils/libdwfl.h>
11 #include <elfutils/version.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <fnmatch.h>
15 #include <libelf.h>
16 #include <limits.h>
17 #include <pthread.h>
18 #include <search.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include "config.h"
25 #include "list.h"
26 #include "dwarves.h"
27 #include "dutil.h"
28 #include "hash.h"
30 #ifndef DW_AT_alignment
31 #define DW_AT_alignment 0x88
32 #endif
34 #ifndef DW_AT_GNU_vector
35 #define DW_AT_GNU_vector 0x2107
36 #endif
38 #ifndef DW_TAG_GNU_call_site
39 #define DW_TAG_GNU_call_site 0x4109
40 #define DW_TAG_GNU_call_site_parameter 0x410a
41 #endif
43 #ifndef DW_TAG_call_site
44 #define DW_TAG_call_site 0x48
45 #define DW_TAG_call_site_parameter 0x49
46 #endif
48 #ifndef DW_FORM_implicit_const
49 #define DW_FORM_implicit_const 0x21
50 #endif
52 #ifndef DW_OP_addrx
53 #define DW_OP_addrx 0xa1
54 #endif
56 #ifndef EM_RISCV
57 #define EM_RISCV 243
58 #endif
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])
78 return;
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;
91 Dwarf_Off off;
94 typedef struct dwarf_off_ref dwarf_off_ref;
96 struct dwarf_tag {
97 struct hlist_node hash_node;
98 dwarf_off_ref type;
99 Dwarf_Off id;
100 union {
101 dwarf_off_ref abstract_origin;
102 dwarf_off_ref containing_type;
104 struct tag *tag;
105 uint32_t small_id;
106 uint16_t decl_line;
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;
120 struct dwarf_cu {
121 struct hlist_head *hash_tags;
122 struct hlist_head *hash_types;
123 struct dwarf_tag *last_type_lookup;
124 struct cu *cu;
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;
133 dcu->cu = cu;
135 dcu->hash_tags = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
136 if (!dcu->hash_tags)
137 return -ENOMEM;
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);
142 return -ENOMEM;
145 unsigned int i;
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;
153 return 0;
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);
162 dwarf_cu = NULL;
165 return dwarf_cu;
168 static void dwarf_cu__delete(struct cu *cu)
170 if (cu == NULL || cu->priv == NULL)
171 return;
173 struct dwarf_cu *dcu = cu->priv;
175 // dcu->hash_tags & dcu->hash_types are on cu->obstack
176 cu__free(cu, dcu);
177 cu->priv = NULL;
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,
199 const Dwarf_Off id)
201 if (id == 0)
202 return NULL;
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) {
210 if (tpos->id == id)
211 return tpos;
214 return NULL;
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) ?
221 dcu->hash_types :
222 dcu->hash_tags;
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)
229 if (cu == NULL)
230 return NULL;
231 if (ref->from_types) {
232 return NULL;
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)
240 if (dcu == NULL)
241 return NULL;
242 if (ref->from_types) {
243 dcu = dcu->type_unit;
244 if (dcu == NULL) {
245 return NULL;
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);
254 if (dtag)
255 dcu->last_type_lookup = dtag;
257 return dtag;
260 static void *memdup(const void *src, size_t len, struct cu *cu)
262 void *s = cu__malloc(cu, len);
263 if (s != NULL)
264 memcpy(s, src, len);
265 return s;
268 /* Number decoding macros. See 7.6 Variable Length Data. */
270 #define get_uleb128_step(var, addr, nth, break) \
271 __b = *(addr)++; \
272 var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
273 if ((__b & 0x80) == 0) \
274 break
276 #define get_uleb128_rest_return(var, i, addrp) \
277 do { \
278 for (; i < 10; ++i) { \
279 get_uleb128_step(var, *addrp, i, \
280 return var); \
282 /* Other implementations set VALUE to UINT_MAX in this \
283 case. So we better do this as well. */ \
284 return UINT64_MAX; \
285 } while (0)
287 static uint64_t __libdw_get_uleb128(uint64_t acc, uint32_t i,
288 const uint8_t **addrp)
290 uint8_t __b;
291 get_uleb128_rest_return (acc, i, addrp);
294 #define get_uleb128(var, addr) \
295 do { \
296 uint8_t __b; \
297 var = 0; \
298 get_uleb128_step(var, addr, 0, break); \
299 var = __libdw_get_uleb128 (var, 1, &(addr)); \
300 } while (0)
302 static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
304 Dwarf_Attribute attr;
305 uint32_t form;
307 if (dwarf_attr(die, name, &attr) == NULL)
308 return 0;
310 form = dwarf_whatform(&attr);
312 switch (form) {
313 case DW_FORM_addr: {
314 Dwarf_Addr addr;
315 if (dwarf_formaddr(&attr, &addr) == 0)
316 return addr;
318 break;
319 case DW_FORM_implicit_const:
320 case DW_FORM_data1:
321 case DW_FORM_data2:
322 case DW_FORM_data4:
323 case DW_FORM_data8:
324 case DW_FORM_sdata:
325 case DW_FORM_udata: {
326 Dwarf_Word value;
327 if (dwarf_formudata(&attr, &value) == 0)
328 return value;
330 break;
331 case DW_FORM_flag:
332 case DW_FORM_flag_present: {
333 bool value;
334 if (dwarf_formflag(&attr, &value) == 0)
335 return value;
337 break;
338 default:
339 fprintf(stderr, "DW_AT_<0x%x>=0x%x\n", name, form);
340 break;
343 return 0;
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) {
356 uint64_t result;
357 ++expr;
358 get_uleb128(result, expr);
359 return result;
362 fprintf(stderr, "%s: unhandled %#x DW_OP_ operation\n",
363 __func__, *expr);
364 return UINT64_MAX;
367 static Dwarf_Off __attr_offset(Dwarf_Attribute *attr)
369 Dwarf_Block block;
371 switch (dwarf_whatform(attr)) {
372 case DW_FORM_implicit_const:
373 case DW_FORM_data1:
374 case DW_FORM_data2:
375 case DW_FORM_data4:
376 case DW_FORM_data8:
377 case DW_FORM_sdata:
378 case DW_FORM_udata: {
379 Dwarf_Word value;
380 if (dwarf_formudata(attr, &value) == 0)
381 return value;
382 break;
384 default:
385 if (dwarf_formblock(attr, &block) == 0)
386 return dwarf_expr(block.data, block.length);
389 return 0;
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)
397 return 0;
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;
414 return str;
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) {
422 Dwarf_Die type_die;
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);
426 return ref;
429 memset(&ref, 0, sizeof(ref));
430 return 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);
443 Dwarf_Addr address;
444 dwarf_formaddr (&addr_attr, &address);
446 expr[0]->number = address;
448 return 0;
452 return 1;
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)));
459 if (dtag == NULL)
460 return NULL;
462 struct tag *tag = cu__zalloc(dcu->cu, size);
464 if (tag == NULL)
465 return NULL;
467 dtag->tag = tag;
468 tag->priv = dtag;
469 tag->type = 0;
470 tag->top_level = 0;
472 return tag;
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);
496 else
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);
505 int32_t decl_line;
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));
528 if (tag != NULL)
529 tag__init(tag, cu, die);
531 return tag;
534 static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
535 struct cu *cu)
537 struct ptr_to_member_type *ptr = tag__alloc(cu, sizeof(*ptr));
539 if (ptr != NULL) {
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);
545 return ptr;
548 static uint8_t encoding_to_float_type(uint64_t encoding)
550 switch (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;
554 default: return 0;
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));
562 if (bt != NULL) {
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);
575 return bt;
578 static struct array_type *array_type__new(Dwarf_Die *die, struct cu *cu)
580 struct array_type *at = tag__alloc(cu, sizeof(*at));
582 if (at != NULL) {
583 tag__init(&at->tag, cu, die);
584 at->dimensions = 0;
585 at->nr_entries = NULL;
586 at->is_vector = dwarf_hasattr(die, DW_AT_GNU_vector);
589 return at;
592 static struct string_type *string_type__new(Dwarf_Die *die, struct cu *cu)
594 struct string_type *st = tag__alloc(cu, sizeof(*st));
596 if (st != NULL) {
597 tag__init(&st->tag, cu, die);
598 st->nr_entries = attr_numeric(die, DW_AT_byte_size);
599 if (st->nr_entries == 0)
600 st->nr_entries = 1;
603 return st;
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);
624 return namespace;
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);
630 __type__init(type);
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;
638 type->resized = 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) {
645 Dwarf_Die type_die;
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));
659 if (type != NULL)
660 type__init(type, die, cu, conf);
662 return type;
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);
675 return enumerator;
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) {
687 case DW_OP_addr:
688 case DW_OP_addrx:
689 scope = VSCOPE_GLOBAL;
690 *addr = expr[0].number;
691 break;
692 case DW_OP_reg1 ... DW_OP_reg31:
693 case DW_OP_breg0 ... DW_OP_breg31:
694 scope = VSCOPE_REGISTER; break;
695 case DW_OP_fbreg:
696 scope = VSCOPE_LOCAL; break;
700 return scope;
703 enum vscope variable__scope(const struct variable *var)
705 return var->scope;
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";
715 default: break;
718 return "unknown";
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));
729 } else {
730 var = tag__alloc(cu, sizeof(*var));
733 if (var != NULL) {
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);
743 var->ip.addr = 0;
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));
752 return var;
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);
765 return constant;
768 static int tag__recode_dwarf_bitfield(struct tag *tag, struct cu *cu, uint16_t bit_size)
770 int id;
771 type_id_t short_id;
772 struct tag *recoded;
773 /* in all the cases the name is at the same offset */
774 const char *name = namespace__name(tag__namespace(tag));
776 switch (tag->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);
781 if (dtype == NULL) {
782 tag__print_type_not_found(tag);
783 return -ENOENT;
786 struct tag *type = dtype->tag;
788 id = tag__recode_dwarf_bitfield(type, cu, bit_size);
789 if (id < 0)
790 return id;
792 struct type *new_typedef = cu__zalloc(cu, sizeof(*new_typedef));
793 if (new_typedef == NULL)
794 return -ENOMEM;
796 recoded = (struct tag *)new_typedef;
797 recoded->tag = DW_TAG_typedef;
798 recoded->type = id;
799 new_typedef->namespace.name = tag__namespace(tag)->name;
801 break;
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);
809 if (dtype == NULL) {
810 tag__print_type_not_found(tag);
811 return -ENOENT;
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)
818 return id;
820 recoded = cu__zalloc(cu, sizeof(*recoded));
821 if (recoded == NULL)
822 return -ENOMEM;
824 recoded->tag = DW_TAG_volatile_type;
825 recoded->type = id;
827 break;
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);
836 if (recoded != NULL)
837 return short_id;
839 struct base_type *new_bt = cu__zalloc(cu, sizeof(*new_bt));
840 if (new_bt == NULL)
841 return -ENOMEM;
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;
848 break;
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);
857 if (recoded != NULL)
858 return 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)
863 return -ENOMEM;
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;
870 * Share the tags
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;
876 break;
877 default:
878 fprintf(stderr, "%s: tag=%s, name=%s, bit_size=%d\n",
879 __func__, dwarf_tag_name(tag->tag),
880 name, bit_size);
881 return -EINVAL;
884 uint32_t new_id;
885 if (cu__add_tag(cu, recoded, &new_id) == 0)
886 return new_id;
888 free(recoded);
889 return -ENOMEM;
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;
896 const char *name;
898 if (conf->skip_encoding_btf_decl_tag)
899 return 0;
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)
904 return 0;
906 annot = zalloc(sizeof(*annot));
907 if (!annot)
908 return -ENOMEM;
910 annot->value = attr_string(die, DW_AT_const_value, conf);
911 annot->component_idx = component_idx;
912 list_add_tail(&annot->node, head);
913 return 0;
916 static int add_child_llvm_annotations(Dwarf_Die *die, int component_idx,
917 struct conf_load *conf, struct list_head *head)
919 Dwarf_Die child;
920 int ret;
922 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
923 return 0;
925 die = &child;
926 do {
927 if (dwarf_tag(die) == DW_TAG_LLVM_annotation) {
928 ret = add_llvm_annotation(die, component_idx, conf, head);
929 if (ret)
930 return ret;
932 } while (dwarf_siblingof(die, die) == 0);
934 return 0;
937 int class_member__dwarf_recode_bitfield(struct class_member *member,
938 struct cu *cu)
940 struct dwarf_tag *dtag = member->tag.priv;
941 struct dwarf_tag *type = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
942 int recoded_type_id;
944 if (type == NULL)
945 return -ENOENT;
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;
952 return 0;
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.
973 } else {
974 if (dwarf_attr(die, DW_AT_data_member_location, &attr) != NULL) {
975 member->byte_offset = __attr_offset(&attr);
976 } else {
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);
1007 member->hole = 0;
1010 return member;
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
1017 * values.
1019 static int arch__nr_register_params(const GElf_Ehdr *ehdr)
1021 switch (ehdr->e_machine) {
1022 case EM_S390: return 5;
1023 case EM_SPARC:
1024 case EM_SPARCV9:
1025 case EM_X86_64: return 6;
1026 case EM_AARCH64:
1027 case EM_ARC:
1028 case EM_ARM:
1029 case EM_MIPS:
1030 case EM_PPC:
1031 case EM_PPC64:
1032 case EM_RISCV: return 8;
1033 default: break;
1036 return 0;
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
1042 * entry.
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) {
1049 case EM_S390:
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
1056 return;
1057 case EM_X86_64:
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
1065 return;
1066 case EM_ARM:
1067 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers */
1068 case EM_AARCH64:
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;
1078 return;
1079 default:
1080 return;
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));
1089 if (parm != NULL) {
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)
1099 return parm;
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
1104 * optimized out.
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:
1116 * "4.1.10
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 &&
1142 #else
1143 dwarf_getlocation(&attr, &loc.expr, &loc.exprlen) == 0 &&
1144 #endif
1145 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
1155 * contents.
1157 if (expected_reg >= 0 && expected_reg != expr->atom)
1158 parm->unexpected_reg = 1;
1159 break;
1160 default:
1161 parm->optimized = 1;
1162 break;
1164 } else if (has_const_value) {
1165 parm->optimized = 1;
1169 return parm;
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));
1176 if (exp != NULL) {
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);
1183 exp->ip.addr = 0;
1184 exp->high_pc = 0;
1186 if (!cu->has_addr_info)
1187 goto out;
1189 if (dwarf_lowpc(die, &exp->ip.addr))
1190 exp->ip.addr = 0;
1191 if (dwarf_lowpc(die, &exp->high_pc))
1192 exp->high_pc = 0;
1194 exp->size = exp->high_pc - exp->ip.addr;
1195 if (exp->size == 0) {
1196 Dwarf_Addr base, start;
1197 ptrdiff_t offset = 0;
1199 while (1) {
1200 offset = dwarf_ranges(die, offset, &base, &start,
1201 &exp->high_pc);
1202 start = (unsigned long)start;
1203 exp->high_pc = (unsigned long)exp->high_pc;
1204 if (offset <= 0)
1205 break;
1206 exp->size += exp->high_pc - start;
1207 if (exp->ip.addr == 0)
1208 exp->ip.addr = start;
1212 out:
1213 return exp;
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))
1224 label->ip.addr = 0;
1227 return label;
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 =
1238 class->nr_holes =
1239 class->nr_bit_holes =
1240 class->padding =
1241 class->bit_padding = 0;
1242 class->priv = NULL;
1245 return class;
1248 static void lexblock__init(struct lexblock *block, struct cu *cu,
1249 Dwarf_Die *die)
1251 Dwarf_Off high_pc;
1253 if (!cu->has_addr_info || dwarf_lowpc(die, &block->ip.addr)) {
1254 block->ip.addr = 0;
1255 block->size = 0;
1256 } else if (dwarf_highpc(die, &high_pc))
1257 block->size = 0;
1258 else
1259 block->size = high_pc - block->ip.addr;
1261 INIT_LIST_HEAD(&block->tags);
1263 block->size_inline_expansions =
1264 block->nr_inline_expansions =
1265 block->nr_labels =
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);
1279 return block;
1282 static void ftype__init(struct ftype *ftype, Dwarf_Die *die, struct cu *cu)
1284 #ifndef NDEBUG
1285 const uint16_t tag = dwarf_tag(die);
1286 assert(tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type);
1287 #endif
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));
1299 if (ftype != NULL)
1300 ftype__init(ftype, die, cu);
1302 return ftype;
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));
1309 if (func != NULL) {
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;
1330 func->priv = NULL;
1333 return func;
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) {
1341 Dwarf_Word num;
1343 if (dwarf_formudata(&attr, &num) == 0) {
1344 return (uintmax_t)num + 1;
1346 } else if (dwarf_attr(die, DW_AT_count, &attr) != NULL) {
1347 Dwarf_Word num;
1349 if (dwarf_formudata(&attr, &num) == 0) {
1350 return (uintmax_t)num;
1354 return 0;
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);
1380 if (tag != NULL) {
1381 if (dwarf_haschildren(die))
1382 fprintf(stderr, "%s: %s WITH children!\n", __func__,
1383 dwarf_tag_name(tag->tag));
1386 return 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));
1394 if (tag == NULL)
1395 return NULL;
1397 tag__init(&tag->tag, cu, die);
1398 tag->tag.has_btf_type_tag = true;
1399 INIT_LIST_HEAD(&tag->tags);
1400 return tag;
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));
1409 if (tag == NULL)
1410 return NULL;
1412 tag__init(&tag->tag, cu, die);
1413 tag->value = attr_string(die, DW_AT_const_value, conf);
1414 return tag;
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;
1423 const char *name;
1424 uint32_t id;
1426 /* If no child tags or skipping btf_type_tag encoding, just create a new tag
1427 * and return
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 */
1434 cdie = &child;
1435 do {
1436 if (dwarf_tag(cdie) != DW_TAG_LLVM_annotation)
1437 continue;
1439 /* Only check btf_type_tag annotations */
1440 name = attr_string(cdie, DW_AT_name, conf);
1441 if (strcmp(name, "btf_type_tag") != 0)
1442 continue;
1444 if (tag == NULL) {
1445 /* Create a btf_type_tag_ptr type. */
1446 tag = die__create_new_btf_type_tag_ptr_type(die, cu);
1447 if (!tag)
1448 return NULL;
1451 /* Create a btf_type_tag type for this annotation. */
1452 annot = die__create_new_btf_type_tag_type(cdie, cu, conf);
1453 if (annot == NULL)
1454 return NULL;
1456 if (cu__table_add_tag(cu, &annot->tag, &id) < 0)
1457 return NULL;
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,
1473 struct cu *cu)
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)
1485 Dwarf_Die child;
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);
1493 class = NULL;
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)
1505 Dwarf_Die child;
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);
1513 namespace = NULL;
1517 return namespace ? &namespace->tag : NULL;
1520 static struct tag *die__create_new_union(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1522 Dwarf_Die child;
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);
1530 utype = NULL;
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);
1541 if (base == NULL)
1542 return NULL;
1544 if (dwarf_haschildren(die))
1545 fprintf(stderr, "%s: DW_TAG_base_type WITH children!\n",
1546 __func__);
1548 return &base->tag;
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);
1555 if (tdef == NULL)
1556 return NULL;
1558 if (add_child_llvm_annotations(die, -1, conf, &tdef->namespace.annots))
1559 return NULL;
1561 return &tdef->namespace.tag;
1564 static struct tag *die__create_new_array(Dwarf_Die *die, struct cu *cu)
1566 Dwarf_Die child;
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);
1572 if (array == NULL)
1573 return NULL;
1575 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1576 return &array->tag;
1578 die = &child;
1579 do {
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 "
1584 "supported!\n",
1585 __FUNCTION__, max_dimensions);
1586 break;
1588 } else
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)
1595 goto out_free;
1597 return &array->tag;
1598 out_free:
1599 free(array);
1600 return 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);
1607 if (string == NULL)
1608 return NULL;
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,
1617 int param_idx)
1619 struct parameter *parm = parameter__new(die, cu, conf, param_idx);
1621 if (parm == NULL)
1622 return NULL;
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)))
1628 return NULL;
1630 } else {
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);
1643 return &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);
1652 if (label == NULL)
1653 return NULL;
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))
1669 return NULL;
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)
1679 return 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)
1687 Dwarf_Die child;
1688 struct ftype *ftype = ftype__new(die, cu);
1689 struct tag *tag;
1691 if (ftype == NULL)
1692 return NULL;
1694 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1695 goto out;
1697 die = &child;
1698 do {
1699 uint32_t id;
1701 switch (dwarf_tag(die)) {
1702 case DW_TAG_subrange_type: // ADA stuff
1703 tag__print_not_supported(dwarf_tag(die));
1704 continue;
1705 case DW_TAG_formal_parameter:
1706 tag = die__create_new_parameter(die, ftype, NULL, cu, conf, -1);
1707 break;
1708 case DW_TAG_unspecified_parameters:
1709 ftype->unspec_parms = 1;
1710 continue;
1711 default:
1712 tag = die__process_tag(die, cu, 0, conf);
1713 if (tag == NULL)
1714 goto out_delete;
1716 if (tag == &unsupported_tag) {
1717 tag__print_not_supported(dwarf_tag(die));
1718 continue;
1721 if (cu__add_tag(cu, tag, &id) < 0)
1722 goto out_delete_tag;
1724 goto hash;
1727 if (tag == NULL)
1728 goto out_delete;
1730 if (cu__table_add_tag(cu, tag, &id) < 0)
1731 goto out_delete_tag;
1732 hash:
1733 cu__hash(cu, tag);
1734 struct dwarf_tag *dtag = tag->priv;
1735 dtag->small_id = id;
1736 } while (dwarf_siblingof(die, die) == 0);
1737 out:
1738 return &ftype->tag;
1739 out_delete_tag:
1740 tag__delete(tag);
1741 out_delete:
1742 ftype__delete(ftype);
1743 return NULL;
1746 static struct tag *die__create_new_enumeration(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1748 Dwarf_Die child;
1749 struct type *enumeration = type__new(die, cu, conf);
1751 if (enumeration == NULL)
1752 return NULL;
1754 if (enumeration->size == 0)
1755 enumeration->size = sizeof(int) * 8;
1756 else
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 */
1762 goto out;
1765 die = &child;
1766 do {
1767 struct enumerator *enumerator;
1769 if (dwarf_tag(die) != DW_TAG_enumerator) {
1770 cu__tag_not_handled(die);
1771 continue;
1773 enumerator = enumerator__new(die, cu, conf);
1774 if (enumerator == NULL)
1775 goto out_delete;
1777 enumeration__add(enumeration, enumerator);
1778 } while (dwarf_siblingof(die, die) == 0);
1779 out:
1780 return &enumeration->namespace.tag;
1781 out_delete:
1782 enumeration__delete(enumeration);
1783 return NULL;
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);
1790 int member_idx = 0;
1792 do {
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:
1800 #endif
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...
1807 * See:
1808 * https://gcc.gnu.org/wiki/TemplateParmsDwarf
1810 tag__print_not_supported(dwarf_tag(die));
1811 continue;
1812 case DW_TAG_inheritance:
1813 case DW_TAG_member: {
1814 struct class_member *member = class_member__new(die, cu, is_union, conf);
1816 if (member == NULL)
1817 return -ENOMEM;
1819 if (cu__is_c_plus_plus(cu)) {
1820 uint32_t id;
1822 if (cu__table_add_tag(cu, &member->tag, &id) < 0) {
1823 class_member__delete(member);
1824 return -ENOMEM;
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))
1834 return -ENOMEM;
1835 member_idx++;
1837 continue;
1838 case DW_TAG_LLVM_annotation:
1839 if (add_llvm_annotation(die, -1, conf, &class->namespace.annots))
1840 return -ENOMEM;
1841 continue;
1842 default: {
1843 struct tag *tag = die__process_tag(die, cu, 0, conf);
1845 if (tag == NULL)
1846 return -ENOMEM;
1848 if (tag == &unsupported_tag) {
1849 tag__print_not_supported(dwarf_tag(die));
1850 continue;
1853 uint32_t id;
1855 if (cu__table_add_tag(cu, tag, &id) < 0) {
1856 tag__delete(tag);
1857 return -ENOMEM;
1860 struct dwarf_tag *dtag = tag->priv;
1861 dtag->small_id = id;
1863 namespace__add_tag(&class->namespace, tag);
1864 cu__hash(cu, 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);
1871 continue;
1874 } while (dwarf_siblingof(die, die) == 0);
1876 return 0;
1879 static int die__process_namespace(Dwarf_Die *die, struct namespace *namespace,
1880 struct cu *cu, struct conf_load *conf)
1882 struct tag *tag;
1883 do {
1884 tag = die__process_tag(die, cu, 0, conf);
1885 if (tag == NULL)
1886 goto out_enomem;
1888 if (tag == &unsupported_tag) {
1889 tag__print_not_supported(dwarf_tag(die));
1890 continue;
1893 uint32_t id;
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);
1901 cu__hash(cu, tag);
1902 } while (dwarf_siblingof(die, die) == 0);
1904 return 0;
1905 out_delete_tag:
1906 tag__delete(tag);
1907 out_enomem:
1908 return -ENOMEM;
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)
1921 goto out_delete;
1923 if (father != NULL)
1924 lexblock__add_lexblock(father, lexblock);
1925 return 0;
1926 out_delete:
1927 lexblock__delete(lexblock);
1928 return -ENOMEM;
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)
1937 Dwarf_Die child;
1938 struct tag *tag;
1940 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1941 return 0;
1943 die = &child;
1944 do {
1945 uint32_t id;
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
1959 * new tool.
1961 continue;
1962 case DW_TAG_lexical_block:
1963 if (die__create_new_lexblock(die, cu, lexblock, conf) != 0)
1964 goto out_enomem;
1965 continue;
1966 case DW_TAG_formal_parameter:
1968 * FIXME:
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);
1978 continue;
1979 case DW_TAG_inlined_subroutine:
1980 tag = die__create_new_inline_expansion(die, lexblock, cu, conf);
1981 break;
1982 case DW_TAG_label:
1983 if (conf->ignore_labels)
1984 continue;
1985 tag = die__create_new_label(die, lexblock, cu, conf);
1986 break;
1987 default:
1988 tag = die__process_tag(die, cu, 0, conf);
1989 if (tag == NULL)
1990 goto out_enomem;
1992 if (tag == &unsupported_tag) {
1993 tag__print_not_supported(dwarf_tag(die));
1994 continue;
1997 if (cu__add_tag(cu, tag, &id) < 0)
1998 goto out_delete_tag;
1999 goto hash;
2002 if (tag == NULL)
2003 goto out_enomem;
2005 if (cu__table_add_tag(cu, tag, &id) < 0)
2006 goto out_delete_tag;
2007 hash:
2008 cu__hash(cu, tag);
2009 struct dwarf_tag *dtag = tag->priv;
2010 dtag->small_id = id;
2011 } while (dwarf_siblingof(die, die) == 0);
2013 return 0;
2014 out_delete_tag:
2015 tag__delete(tag);
2016 out_enomem:
2017 return -ENOMEM;
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);
2026 if (exp == NULL)
2027 return NULL;
2029 if (die__process_inline_expansion(die, lexblock, cu, conf) != 0) {
2030 free(exp);
2031 return NULL;
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)
2042 int param_idx = 0;
2043 Dwarf_Die child;
2044 struct tag *tag;
2046 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
2047 return 0;
2049 die = &child;
2050 do {
2051 uint32_t id;
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
2065 * new tool.
2067 continue;
2068 case DW_TAG_dwarf_procedure:
2070 * Ignore it, just scope expressions, that we have no use for (so far).
2072 continue;
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:
2077 #endif
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));
2084 continue;
2085 case DW_TAG_formal_parameter:
2086 tag = die__create_new_parameter(die, ftype, lexblock, cu, conf, param_idx++);
2087 break;
2088 case DW_TAG_variable:
2089 tag = die__create_new_variable(die, cu, conf);
2090 if (tag == NULL)
2091 goto out_enomem;
2092 lexblock__add_variable(lexblock, tag__variable(tag));
2093 break;
2094 case DW_TAG_unspecified_parameters:
2095 if (ftype != NULL)
2096 ftype->unspec_parms = 1;
2097 continue;
2098 case DW_TAG_label:
2099 if (conf->ignore_labels)
2100 continue;
2101 tag = die__create_new_label(die, lexblock, cu, conf);
2102 break;
2103 case DW_TAG_inlined_subroutine:
2104 if (conf->ignore_inline_expansions)
2105 continue;
2106 tag = die__create_new_inline_expansion(die, lexblock, cu, conf);
2107 break;
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)
2113 goto out_enomem;
2114 continue;
2115 case DW_TAG_LLVM_annotation:
2116 if (add_llvm_annotation(die, -1, conf, &(tag__function(&ftype->tag)->annots)))
2117 goto out_enomem;
2118 continue;
2119 default:
2120 tag = die__process_tag(die, cu, 0, conf);
2122 if (tag == NULL)
2123 goto out_enomem;
2125 if (tag == &unsupported_tag) {
2126 tag__print_not_supported(dwarf_tag(die));
2127 continue;
2130 if (cu__add_tag(cu, tag, &id) < 0)
2131 goto out_delete_tag;
2133 goto hash;
2136 if (tag == NULL)
2137 goto out_enomem;
2139 if (cu__table_add_tag(cu, tag, &id) < 0)
2140 goto out_delete_tag;
2141 hash:
2142 cu__hash(cu, tag);
2143 struct dwarf_tag *dtag = tag->priv;
2144 dtag->small_id = id;
2145 } while (dwarf_siblingof(die, die) == 0);
2147 return 0;
2148 out_delete_tag:
2149 tag__delete(tag);
2150 out_enomem:
2151 return -ENOMEM;
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);
2161 function = NULL;
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)
2170 struct tag *tag;
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;
2216 default:
2217 __cu__tag_not_handled(die, fn);
2218 /* fall thru */
2219 case DW_TAG_dwarf_procedure:
2221 * Ignore it, just scope expressions, that we have no use for (so far).
2223 tag = &unsupported_tag;
2224 break;
2225 case DW_TAG_label:
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);
2230 break;
2233 if (tag != NULL)
2234 tag->top_level = top_level;
2236 return tag;
2239 static int die__process_unit(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
2241 do {
2242 struct tag *tag = die__process_tag(die, cu, 1, conf);
2243 if (tag == NULL)
2244 return -ENOMEM;
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));
2252 continue;
2255 uint32_t id = 0;
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);
2273 cu__hash(cu, tag);
2274 struct dwarf_tag *dtag = tag->priv;
2275 dtag->small_id = id;
2276 } while (dwarf_siblingof(die, die) == 0);
2278 return 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)
2285 struct tag *pos;
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))
2295 return -1;
2296 continue;
2299 switch (pos->tag) {
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))
2308 return -1;
2309 continue;
2312 break;
2313 case DW_TAG_subroutine_type:
2314 case DW_TAG_subprogram:
2315 ftype__recode_dwarf_types(pos, cu);
2316 break;
2317 case DW_TAG_imported_module:
2318 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->type);
2319 goto check_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);
2323 if (dtype != NULL)
2324 goto next;
2325 goto find_type;
2328 if (dpos->type.off == 0) /* void */
2329 continue;
2330 find_type:
2331 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
2332 check_type:
2333 if (dtype == NULL) {
2334 tag__print_type_not_found(pos);
2335 continue;
2337 next:
2338 pos->type = dtype->small_id;
2340 return 0;
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)
2350 return;
2352 dtype = dwarf_cu__find_type_by_ref(cu->priv, &specification);
2353 if (dtype != NULL)
2354 t->namespace.name = tag__namespace(dtype->tag)->name;
2355 else {
2356 struct dwarf_tag *dtag = tag->priv;
2358 fprintf(stderr,
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;
2370 fprintf(stderr,
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 */
2394 pos->tag.type = 0;
2395 continue;
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);
2400 continue;
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
2409 * parameters.
2411 if (pos->has_loc)
2412 opos->has_loc = pos->has_loc;
2414 if (pos->optimized)
2415 opos->optimized = pos->optimized;
2416 if (pos->unexpected_reg)
2417 opos->unexpected_reg = pos->unexpected_reg;
2418 continue;
2421 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
2422 if (dtype == NULL) {
2423 tag__print_type_not_found(&pos->tag);
2424 continue;
2426 pos->tag.type = dtype->small_id;
2430 static void lexblock__recode_dwarf_types(struct lexblock *tag, struct cu *cu)
2432 struct tag *pos;
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;
2439 switch (pos->tag) {
2440 case DW_TAG_lexical_block:
2441 lexblock__recode_dwarf_types(tag__lexblock(pos), cu);
2442 continue;
2443 case DW_TAG_inlined_subroutine:
2444 if (dpos->type.off != 0)
2445 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->type);
2446 else
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);
2451 else
2452 tag__print_abstract_origin_not_found(pos);
2453 continue;
2455 ftype__recode_dwarf_types(dtype->tag, cu);
2456 continue;
2458 case DW_TAG_formal_parameter:
2459 if (dpos->type.off != 0)
2460 break;
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);
2467 continue;
2469 fp->name = tag__parameter(dtype->tag)->name;
2470 pos->type = dtype->tag->type;
2471 continue;
2473 case DW_TAG_variable:
2474 if (dpos->type.off != 0)
2475 break;
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)
2485 continue;
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);
2492 continue;
2494 var->name = tag__variable(dtype->tag)->name;
2495 pos->type = dtype->tag->type;
2496 continue;
2498 case DW_TAG_label: {
2499 struct label *l = tag__label(pos);
2501 if (dpos->abstract_origin.off == 0)
2502 continue;
2504 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->abstract_origin);
2505 if (dtype != NULL)
2506 l->name = tag__label(dtype->tag)->name;
2507 else
2508 tag__print_abstract_origin_not_found(pos);
2510 continue;
2513 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
2514 if (dtype == NULL) {
2515 tag__print_type_not_found(pos);
2516 continue;
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"
2542 * emit '*'
2544 * For 'tag3 -> tag2 -> tag1 -> int":
2545 * emit for "tag2 -> tag1 -> int"
2546 * emit tag3
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 */
2567 if (dtag == NULL)
2568 return 0;
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);
2576 switch (tag->tag) {
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
2589 return 0;
2591 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &dtag->abstract_origin);
2592 if (dtype == NULL)
2593 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &specification);
2594 if (dtype != NULL)
2595 fn->name = tag__function(dtype->tag)->name;
2596 else {
2597 fprintf(stderr,
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);
2608 /* Fall thru */
2610 case DW_TAG_subroutine_type:
2611 ftype__recode_dwarf_types(tag, cu);
2612 /* Fall thru, for the function return type */
2613 break;
2615 case DW_TAG_lexical_block:
2616 lexblock__recode_dwarf_types(tag__lexblock(tag), cu);
2617 return 0;
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);
2623 if (dtype != NULL)
2624 pt->containing_type = dtype->small_id;
2625 else {
2626 fprintf(stderr,
2627 "%s: couldn't find type for "
2628 "containing_type %#llx, containing_type=%#llx\n",
2629 __func__,
2630 (unsigned long long)dtag->id,
2631 (unsigned long long)dtag->containing_type.off);
2634 break;
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);
2645 goto check_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);
2649 if (dtype != NULL)
2650 goto out;
2651 goto find_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,
2660 &specification);
2661 if (dtype)
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 */
2672 else
2673 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag), 0);
2674 return 0;
2677 find_type:
2678 dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
2679 check_type:
2680 if (dtype == NULL) {
2681 tag__print_type_not_found(tag);
2682 return 0;
2684 out:
2685 if (tag->tag != DW_TAG_pointer_type || !tag->has_btf_type_tag)
2686 tag->type = dtype->small_id;
2687 else
2688 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag), dtype->small_id);
2690 return 0;
2693 static bool param__is_struct(struct cu *cu, struct tag *tag)
2695 struct tag *type = cu__type(cu, tag->type);
2697 if (!type)
2698 return false;
2700 switch (type->tag) {
2701 case DW_TAG_structure_type:
2702 return true;
2703 case DW_TAG_const_type:
2704 case DW_TAG_typedef:
2705 /* handle "typedef struct", const parameter */
2706 return param__is_struct(cu, type);
2707 default:
2708 return false;
2712 static int cu__resolve_func_ret_types_optimized(struct cu *cu)
2714 struct ptr_table *pt = &cu->functions_table;
2715 uint32_t i;
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
2728 * parameter tag.
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)
2748 break;
2750 if (!has_struct_param)
2751 fn->proto.unexpected_reg = 1;
2754 if (tag == NULL || tag->type != 0)
2755 continue;
2757 if (!fn->abstract_origin)
2758 continue;
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);
2765 return -1;
2768 tag->type = dfunc->tag->type;
2770 return 0;
2773 static int cu__recode_dwarf_types_table(struct cu *cu,
2774 struct ptr_table *pt,
2775 uint32_t i)
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))
2782 return -1;
2785 return 0;
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))
2793 return -1;
2794 return 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)
2822 Dwarf_Die child;
2823 const uint16_t tag = dwarf_tag(die);
2825 if (tag == DW_TAG_skeleton_unit) {
2826 static bool warned;
2828 if (!warned) {
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");
2831 warned = true;
2833 return 0; // so that other units can be processed
2836 if (tag == DW_TAG_partial_unit) {
2837 static bool warned;
2839 if (!warned) {
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");
2843 warned = true;
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);
2851 return -EINVAL;
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);
2858 if (err)
2859 return err;
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));
2867 return 0;
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);
2873 if (ret != 0)
2874 return ret;
2875 ret = cu__recode_dwarf_types(cu);
2876 if (ret != 0)
2877 return ret;
2879 return cu__resolve_func_ret_types_optimized(cu);
2882 static int class_member__cache_byte_size(struct tag *tag, struct cu *cu,
2883 void *cookie)
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)
2890 return 0;
2891 } else if (tag->tag != DW_TAG_inheritance) {
2892 return 0;
2895 if (member->bitfield_size == 0) {
2896 member->byte_size = tag__size(tag, cu);
2897 member->bit_size = member->byte_size * 8;
2898 return 0;
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) {
2908 int bit_size;
2909 if (tag__is_enumeration(type)) {
2910 bit_size = tag__type(type)->size;
2911 } else {
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;
2929 return 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;
2951 } else {
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;
2978 return 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))
2989 return 0;
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.
2996 restart:
2997 type__for_each_data_member(type, current_member) {
2998 if (list_is_last(&current_member->tag.node, &type->namespace.tags))
2999 break;
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)
3004 continue;
3006 list_del(&current_member->tag.node);
3007 list_add(&current_member->tag.node, &next_member->tag.node);
3008 goto restart;
3011 return 0;
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);
3031 return LSK__KEEPIT;
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);
3037 switch (lsk) {
3038 case LSK__DELETE:
3039 cus__remove(cus, cu);
3040 cu__delete(cu);
3041 break;
3042 case LSK__STOP_LOADING:
3043 break;
3044 case LSK__KEEPIT:
3045 break;
3047 return lsk;
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;
3054 cu->elf = elf;
3055 cu->dwfl = mod;
3056 cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0;
3057 cu->has_addr_info = conf ? conf->get_addr_info : 0;
3059 GElf_Ehdr ehdr;
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);
3066 return 0;
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;
3074 size_t cuhl;
3075 uint8_t pointer_size, offset_size;
3076 uint64_t signature;
3078 *cup = NULL;
3080 while (dwarf_next_unit(dw, off, &noff, &cuhl, NULL, NULL, &pointer_size,
3081 &offset_size, &signature, &type_off)
3082 == 0) {
3084 if (*cup == NULL) {
3085 struct cu *cu;
3087 cu = cu__new("", pointer_size, build_id,
3088 build_id_len, filename, conf->use_obstack);
3089 if (cu == NULL ||
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;
3096 dcup->cu = cu;
3097 /* Funny hack. */
3098 dcup->type_unit = dcup;
3099 cu->priv = dcup;
3100 cu->dfops = &dwarf__ops;
3102 *cup = cu;
3103 cus__add(cus, cu);
3106 Dwarf_Die die_mem;
3107 Dwarf_Die *cu_die = dwarf_offdie_types(dw, off + cuhl,
3108 &die_mem);
3110 if (die__process(cu_die, *cup, conf) != 0)
3111 return DWARF_CB_ABORT;
3113 off = noff;
3116 if (*cup != NULL && cu__recode_dwarf_types(*cup) != 0)
3117 return DWARF_CB_ABORT;
3119 return 0;
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) {
3129 GElf_Shdr header;
3130 if (!gelf_getshdr(section, &header))
3131 continue;
3133 if (header.sh_type != SHT_NOTE)
3134 continue;
3136 Elf_Data *data = NULL;
3137 while ((data = elf_getdata(section, data)) != 0) {
3138 size_t name_off, desc_off, offset = 0;
3139 GElf_Nhdr hdr;
3140 while ((offset = gelf_getnote(data, offset, &hdr, &name_off, &desc_off)) != 0) {
3141 if (hdr.n_type != LINUX_ELFNOTE_LTO_INFO)
3142 continue;
3144 /* owner is Linux */
3145 if (strcmp((char *)data->d_buf + name_off, "Linux") != 0)
3146 continue;
3148 return *(int *)(data->d_buf + desc_off) != 0;
3153 Dwarf_Off off = 0, noff;
3154 size_t cuhl;
3156 while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) {
3157 Dwarf_Die die_mem;
3158 Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem);
3160 if (cu_die == NULL)
3161 break;
3163 Dwarf_Off offset = 0;
3164 while (true) {
3165 size_t length;
3166 Dwarf_Abbrev *abbrev = dwarf_getabbrev (cu_die, offset, &length);
3167 if (abbrev == NULL || abbrev == DWARF_END_ABBREV)
3168 break;
3170 size_t attrcnt;
3171 if (dwarf_getattrcnt (abbrev, &attrcnt) != 0)
3172 return false;
3174 unsigned int attr_num, attr_form;
3175 Dwarf_Off aboffset;
3176 size_t j;
3177 for (j = 0; j < attrcnt; ++j) {
3178 if (dwarf_getabbrevattr (abbrev, j, &attr_num, &attr_form,
3179 &aboffset))
3180 return false;
3181 if (attr_form == DW_FORM_ref_addr)
3182 return true;
3185 offset += length;
3188 off = noff;
3191 return false;
3194 struct dwarf_cus {
3195 struct cus *cus;
3196 struct conf_load *conf;
3197 Dwfl_Module *mod;
3198 Dwarf *dw;
3199 Elf *elf;
3200 const char *filename;
3201 Dwarf_Off off;
3202 const unsigned char *build_id;
3203 int build_id_len;
3204 int error;
3205 struct dwarf_cu *type_dcu;
3208 struct dwarf_thread {
3209 struct dwarf_cus *dcus;
3210 void *data;
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)
3223 return NULL;
3225 struct dwarf_cu *dcu = dwarf_cu__new(cu);
3227 if (dcu == NULL) {
3228 cu__delete(cu);
3229 return NULL;
3232 dcu->type_unit = dcus->type_dcu;
3233 cu->priv = dcu;
3234 cu->dfops = &dwarf__ops;
3236 return dcu;
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;
3246 return DWARF_CB_OK;
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);
3253 if (dcu == NULL)
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)
3265 Dwarf_Off noff;
3266 size_t cuhl;
3267 int ret;
3269 cus__lock(dcus->cus);
3271 if (dcus->error) {
3272 ret = dcus->error;
3273 goto out_unlock;
3276 ret = dwarf_nextcu(dcus->dw, dcus->off, &noff, &cuhl, NULL, pointer_size, offset_size);
3277 if (ret == 0) {
3278 *cu_die = dwarf_offdie(dcus->dw, dcus->off + cuhl, die_mem);
3279 if (*cu_die != NULL)
3280 dcus->off = noff;
3283 if (ret == 0 && *cu_die != NULL) {
3284 *dcu = dwarf_cus__create_cu(dcus, *cu_die, *pointer_size);
3285 if (*dcu == NULL) {
3286 dcus->error = ENOMEM;
3287 ret = -1;
3288 goto out_unlock;
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);
3295 out_unlock:
3296 cus__unlock(dcus->cus);
3298 return ret;
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) {
3310 if (cu_die == NULL)
3311 break;
3313 if (dwarf_cus__process_cu(dcus, cu_die, dcu->cu, dthr->data) == DWARF_CB_ABORT)
3314 goto out_abort;
3317 if (dcus->conf->thread_exit &&
3318 dcus->conf->thread_exit(dcus->conf, dthr->data) != 0)
3319 goto out_abort;
3321 return (void *)DWARF_CB_OK;
3322 out_abort:
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];
3331 int res;
3332 int i;
3334 if (dcus->conf->threads_prepare) {
3335 res = dcus->conf->threads_prepare(dcus->conf, dcus->conf->nr_jobs, thread_data);
3336 if (res != 0)
3337 return res;
3338 } else {
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,
3348 &dthr[i]);
3349 if (dcus->error)
3350 goto out_join;
3353 dcus->error = 0;
3355 out_join:
3356 while (--i >= 0) {
3357 void *res;
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)
3368 dcus->error = res;
3371 return dcus->error;
3374 static int __dwarf_cus__process_cus(struct dwarf_cus *dcus)
3376 uint8_t pointer_size, offset_size;
3377 Dwarf_Off noff;
3378 size_t cuhl;
3380 while (dwarf_nextcu(dcus->dw, dcus->off, &noff, &cuhl, NULL, &pointer_size, &offset_size) == 0) {
3381 Dwarf_Die die_mem;
3382 Dwarf_Die *cu_die = dwarf_offdie(dcus->dw, dcus->off + cuhl, &die_mem);
3384 if (cu_die == NULL)
3385 break;
3387 if (dwarf_cus__create_and_process_cu(dcus, cu_die, pointer_size) == DWARF_CB_ABORT)
3388 return DWARF_CB_ABORT;
3390 dcus->off = noff;
3393 return 0;
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,
3408 int build_id_len,
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;
3415 size_t cuhl;
3417 while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size,
3418 &offset_size) == 0) {
3419 Dwarf_Die die_mem;
3420 Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem);
3422 if (cu_die == NULL)
3423 break;
3425 if (cu == NULL) {
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)
3429 goto out_abort;
3431 dcu = zalloc(sizeof(*dcu));
3432 if (dcu == NULL)
3433 goto out_abort;
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;
3442 hashtags__bits--) {
3443 if (dwarf_cu__init(dcu, cu) == 0)
3444 break;
3446 if (hashtags__bits < default_hbits)
3447 goto out_abort;
3449 dcu->cu = cu;
3450 dcu->type_unit = type_dcu;
3451 cu->priv = dcu;
3452 cu->dfops = &dwarf__ops;
3453 cu->language = attr_numeric(cu_die, DW_AT_language);
3456 Dwarf_Die child;
3457 if (dwarf_child(cu_die, &child) == 0) {
3458 if (die__process_unit(&child, cu, conf) != 0)
3459 goto out_abort;
3462 off = noff;
3465 if (cu == NULL)
3466 return 0;
3468 /* process merged cu */
3469 if (cu__recode_dwarf_types(cu) != LSK__KEEPIT)
3470 goto out_abort;
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)
3479 goto out_abort;
3481 if (cus__finalize(cus, cu, conf, NULL) == LSK__STOP_LOADING)
3482 goto out_abort;
3484 return 0;
3486 out_abort:
3487 dwarf_cu__delete(cu);
3488 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
3498 GElf_Addr vaddr;
3499 int build_id_len = dwfl_module_build_id(mod, &build_id, &vaddr);
3500 #else
3501 int build_id_len = 0;
3502 #endif
3503 struct cu *type_cu;
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);
3508 if (res != 0) {
3509 return res;
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);
3523 } else {
3524 struct dwarf_cus dcus = {
3525 .off = 0,
3526 .cus = cus,
3527 .conf = conf,
3528 .mod = mod,
3529 .dw = dw,
3530 .elf = elf,
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);
3539 if (res)
3540 return res;
3542 if (type_lsk == LSK__DELETE)
3543 cu__delete(type_cu);
3545 return DWARF_CB_OK;
3548 struct process_dwflmod_parms {
3549 struct cus *cus;
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,
3559 void *arg)
3561 struct process_dwflmod_parms *parms = arg;
3562 struct cus *cus = parms->cus;
3564 GElf_Addr dwflbias;
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);
3574 Dwarf_Addr dwbias;
3575 Dwarf *dw = dwfl_module_getdwarf(dwflmod, &dwbias);
3577 int err = DWARF_CB_OK;
3578 if (dw != NULL) {
3579 ++parms->nr_dwarf_sections_found;
3580 err = cus__load_module(cus, parms->conf, dwflmod, dw, elf,
3581 parms->filename);
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.
3587 * else
3588 * fprintf(stderr,
3589 * "%s: can't get debug context descriptor: %s\n",
3590 * __func__, dwfl_errmsg(-1));
3593 return err;
3596 static void dwarf_loader__exit(struct cus *cus)
3598 Dwfl *dwfl = cus__priv(cus);
3600 if (dwfl) {
3601 dwfl_end(dwfl);
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);
3612 if (dwfl_fd < 0)
3613 return -1;
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
3618 * files.
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)
3634 return -1;
3636 dwfl_report_end(dwfl, NULL, NULL);
3638 struct process_dwflmod_parms parms = {
3639 .cus = cus,
3640 .conf = conf,
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);
3647 if (err < 0)
3648 return -1;
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
3652 // cus__delete().
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)
3659 int fd, err;
3661 if (conf->max_hashtable_bits != 0) {
3662 if (conf->max_hashtable_bits > 31)
3663 return -E2BIG;
3665 max_hashtags__bits = conf->max_hashtable_bits;
3668 if (conf->hashtable_bits != 0) {
3669 if (conf->hashtable_bits > max_hashtags__bits)
3670 return -E2BIG;
3672 hashtags__bits = conf->hashtable_bits;
3673 } else if (hashtags__bits > max_hashtags__bits)
3674 return -EINVAL;
3676 elf_version(EV_CURRENT);
3678 fd = open(filename, O_RDONLY);
3680 if (fd == -1)
3681 return -1;
3683 err = cus__process_file(cus, conf, fd, filename);
3684 close(fd);
3686 return err;
3689 struct debug_fmt_ops dwarf__ops = {
3690 .name = "dwarf",
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,