dwarf_loader: Use libdw__lock for dwarf_getlocation(s)
[dwarves.git] / dwarf_loader.c
blob598fde4425c979abcf61d75ae538bd7637928d73
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, unsigned long long offset)
74 static bool dwarf_tags_warned[DW_TAG_GNU_call_site_parameter + 64];
76 if (tag < sizeof(dwarf_tags_warned)) {
77 if (dwarf_tags_warned[tag])
78 return;
79 dwarf_tags_warned[tag] = true;
82 fprintf(stderr, "%s: tag not supported %#x (%s) at <%llx>!\n", func,
83 tag, dwarf_tag_name(tag), offset);
86 #define tag__print_not_supported(die) \
87 __tag__print_not_supported(dwarf_tag(die), __func__, dwarf_dieoffset(die))
89 static void __cu__tag_not_handled(const struct cu *cu, Dwarf_Die *die, const char *fn)
91 uint32_t tag = dwarf_tag(die);
93 fprintf(stderr, "%s: DW_TAG_%s (%#x) @ <%#llx> not handled in a %s CU!\n",
94 fn, dwarf_tag_name(tag), tag,
95 (unsigned long long)dwarf_dieoffset(die), lang__int2str(cu->language));
98 static struct tag unsupported_tag;
100 #define cu__tag_not_handled(cu, die) __cu__tag_not_handled(cu, die, __FUNCTION__)
102 struct dwarf_tag {
103 struct hlist_node hash_node;
104 Dwarf_Off type;
105 Dwarf_Off id;
106 union {
107 Dwarf_Off abstract_origin;
108 Dwarf_Off containing_type;
110 Dwarf_Off specification;
111 struct {
112 bool type:1;
113 bool abstract_origin:1;
114 bool containing_type:1;
115 bool specification:1;
116 } from_types_section;
117 uint16_t decl_line;
118 uint32_t small_id;
119 const char *decl_file;
122 static inline struct tag *dtag__tag(struct dwarf_tag *dtag)
124 return (struct tag *)(dtag + 1);
127 static inline struct dwarf_tag *tag__dwarf(const struct tag *tag)
129 return ((struct dwarf_tag *)tag) - 1;
132 struct dwarf_cu {
133 struct hlist_head *hash_tags;
134 struct hlist_head *hash_types;
135 struct dwarf_tag *last_type_lookup;
136 struct cu *cu;
137 struct dwarf_cu *type_unit;
140 static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
142 static struct dwarf_tag sentinel_dtag = { .id = ULLONG_MAX, };
143 uint64_t hashtags_size = 1UL << hashtags__bits;
145 dcu->cu = cu;
147 dcu->hash_tags = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
148 if (!dcu->hash_tags)
149 return -ENOMEM;
151 dcu->hash_types = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
152 if (!dcu->hash_types) {
153 cu__free(cu, dcu->hash_tags);
154 return -ENOMEM;
157 unsigned int i;
158 for (i = 0; i < hashtags_size; ++i) {
159 INIT_HLIST_HEAD(&dcu->hash_tags[i]);
160 INIT_HLIST_HEAD(&dcu->hash_types[i]);
162 dcu->type_unit = NULL;
163 // To avoid a per-lookup check against NULL in dwarf_cu__find_type_by_ref()
164 dcu->last_type_lookup = &sentinel_dtag;
165 return 0;
168 static struct dwarf_cu *dwarf_cu__new(struct cu *cu)
170 struct dwarf_cu *dwarf_cu = cu__zalloc(cu, sizeof(*dwarf_cu));
172 if (dwarf_cu != NULL && dwarf_cu__init(dwarf_cu, cu) != 0) {
173 cu__free(cu, dwarf_cu);
174 dwarf_cu = NULL;
177 return dwarf_cu;
180 static void dwarf_cu__delete(struct cu *cu)
182 if (cu == NULL || cu->priv == NULL)
183 return;
185 struct dwarf_cu *dcu = cu->priv;
187 // dcu->hash_tags & dcu->hash_types are on cu->obstack
188 cu__free(cu, dcu);
189 cu->priv = NULL;
192 static void __tag__print_type_not_found(struct tag *tag, const char *func)
194 struct dwarf_tag *dtag = tag__dwarf(tag);
195 fprintf(stderr, "%s: couldn't find %#llx type for %#llx (%s)!\n", func,
196 (unsigned long long)dtag->type, (unsigned long long)dtag->id,
197 dwarf_tag_name(tag->tag));
200 #define tag__print_type_not_found(tag) \
201 __tag__print_type_not_found(tag, __func__)
203 static void hashtags__hash(struct hlist_head *hashtable,
204 struct dwarf_tag *dtag)
206 struct hlist_head *head = hashtable + hashtags__fn(dtag->id);
207 hlist_add_head(&dtag->hash_node, head);
210 static struct dwarf_tag *hashtags__find(const struct hlist_head *hashtable,
211 const Dwarf_Off id)
213 if (id == 0)
214 return NULL;
216 struct dwarf_tag *tpos;
217 struct hlist_node *pos;
218 uint32_t bucket = hashtags__fn(id);
219 const struct hlist_head *head = hashtable + bucket;
221 hlist_for_each_entry(tpos, pos, head, hash_node) {
222 if (tpos->id == id)
223 return tpos;
226 return NULL;
229 static void cu__hash(struct cu *cu, struct tag *tag)
231 struct dwarf_cu *dcu = cu->priv;
232 struct hlist_head *hashtable = tag__is_tag_type(tag) ?
233 dcu->hash_types :
234 dcu->hash_tags;
235 hashtags__hash(hashtable, tag__dwarf(tag));
238 static struct dwarf_tag *__dwarf_cu__find_tag_by_ref(const struct dwarf_cu *cu,
239 const Dwarf_Off ref, bool from_types)
241 if (cu == NULL)
242 return NULL;
243 if (from_types) {
244 return NULL;
246 return hashtags__find(cu->hash_tags, ref);
249 #define dwarf_cu__find_tag_by_ref(cu, dtag, field) \
250 __dwarf_cu__find_tag_by_ref(cu, dtag->field, dtag->from_types_section.field)
252 static struct dwarf_tag *__dwarf_cu__find_type_by_ref(struct dwarf_cu *dcu,
253 const Dwarf_Off ref, bool from_types)
255 if (dcu == NULL)
256 return NULL;
257 if (from_types) {
258 dcu = dcu->type_unit;
259 if (dcu == NULL) {
260 return NULL;
264 if (dcu->last_type_lookup->id == ref)
265 return dcu->last_type_lookup;
267 struct dwarf_tag *dtag = hashtags__find(dcu->hash_types, ref);
269 if (dtag)
270 dcu->last_type_lookup = dtag;
272 return dtag;
275 #define dwarf_cu__find_type_by_ref(dcu, dtag, field) \
276 __dwarf_cu__find_type_by_ref(dcu, dtag->field, dtag->from_types_section.field)
278 static void *memdup(const void *src, size_t len, struct cu *cu)
280 void *s = cu__malloc(cu, len);
281 if (s != NULL)
282 memcpy(s, src, len);
283 return s;
286 /* Number decoding macros. See 7.6 Variable Length Data. */
288 #define get_uleb128_step(var, addr, nth, break) \
289 __b = *(addr)++; \
290 var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
291 if ((__b & 0x80) == 0) \
292 break
294 #define get_uleb128_rest_return(var, i, addrp) \
295 do { \
296 for (; i < 10; ++i) { \
297 get_uleb128_step(var, *addrp, i, \
298 return var); \
300 /* Other implementations set VALUE to UINT_MAX in this \
301 case. So we better do this as well. */ \
302 return UINT64_MAX; \
303 } while (0)
305 static uint64_t __libdw_get_uleb128(uint64_t acc, uint32_t i,
306 const uint8_t **addrp)
308 uint8_t __b;
309 get_uleb128_rest_return (acc, i, addrp);
312 #define get_uleb128(var, addr) \
313 do { \
314 uint8_t __b; \
315 var = 0; \
316 get_uleb128_step(var, addr, 0, break); \
317 var = __libdw_get_uleb128 (var, 1, &(addr)); \
318 } while (0)
320 static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
322 Dwarf_Attribute attr;
323 uint32_t form;
325 if (dwarf_attr(die, name, &attr) == NULL)
326 return 0;
328 form = dwarf_whatform(&attr);
330 switch (form) {
331 case DW_FORM_addr: {
332 Dwarf_Addr addr;
333 if (dwarf_formaddr(&attr, &addr) == 0)
334 return addr;
336 break;
337 case DW_FORM_implicit_const:
338 case DW_FORM_data1:
339 case DW_FORM_data2:
340 case DW_FORM_data4:
341 case DW_FORM_data8:
342 case DW_FORM_sdata:
343 case DW_FORM_udata: {
344 Dwarf_Word value;
345 if (dwarf_formudata(&attr, &value) == 0)
346 return value;
348 break;
349 case DW_FORM_flag:
350 case DW_FORM_flag_present: {
351 bool value;
352 if (dwarf_formflag(&attr, &value) == 0)
353 return value;
355 break;
356 default:
357 fprintf(stderr, "DW_AT_<0x%x>=0x%x\n", name, form);
358 break;
361 return 0;
364 static uint64_t attr_alignment(Dwarf_Die *die, struct conf_load *conf)
366 return conf->ignore_alignment_attr ? 0 : attr_numeric(die, DW_AT_alignment);
369 static uint64_t dwarf_expr(const uint8_t *expr, uint32_t len __maybe_unused)
371 /* Common case: offset from start of the class */
372 if (expr[0] == DW_OP_plus_uconst ||
373 expr[0] == DW_OP_constu) {
374 uint64_t result;
375 ++expr;
376 get_uleb128(result, expr);
377 return result;
380 fprintf(stderr, "%s: unhandled %#x DW_OP_ operation\n",
381 __func__, *expr);
382 return UINT64_MAX;
385 static Dwarf_Off __attr_offset(Dwarf_Attribute *attr)
387 Dwarf_Block block;
389 switch (dwarf_whatform(attr)) {
390 case DW_FORM_implicit_const:
391 case DW_FORM_data1:
392 case DW_FORM_data2:
393 case DW_FORM_data4:
394 case DW_FORM_data8:
395 case DW_FORM_sdata:
396 case DW_FORM_udata: {
397 Dwarf_Word value;
398 if (dwarf_formudata(attr, &value) == 0)
399 return value;
400 break;
402 default:
403 if (dwarf_formblock(attr, &block) == 0)
404 return dwarf_expr(block.data, block.length);
407 return 0;
410 static Dwarf_Off attr_offset(Dwarf_Die *die, const uint32_t name)
412 Dwarf_Attribute attr;
414 if (dwarf_attr(die, name, &attr) == NULL)
415 return 0;
417 return __attr_offset(&attr);
420 static const char *attr_string(Dwarf_Die *die, uint32_t name, struct conf_load *conf __maybe_unused)
422 const char *str = NULL;
423 Dwarf_Attribute attr;
425 if (dwarf_attr(die, name, &attr) != NULL) {
426 str = dwarf_formstring(&attr);
428 if (conf && conf->kabi_prefix && str && strncmp(str, conf->kabi_prefix, conf->kabi_prefix_len) == 0)
429 return conf->kabi_prefix;
432 return str;
435 static bool attr_type(Dwarf_Die *die, uint32_t attr_name, Dwarf_Off *offset)
437 Dwarf_Attribute attr;
439 if (dwarf_attr(die, attr_name, &attr) != NULL) {
440 Dwarf_Die type_die;
441 if (dwarf_formref_die(&attr, &type_die) != NULL) {
442 *offset = dwarf_dieoffset(&type_die);
443 return attr.form == DW_FORM_ref_sig8;
446 *offset = 0;
447 return 0;
450 static int attr_location(Dwarf_Die *die, Dwarf_Op **expr, size_t *exprlen)
452 Dwarf_Attribute attr;
453 int ret = 1;
455 if (dwarf_attr(die, DW_AT_location, &attr) != NULL) {
456 /* use libdw__lock as dwarf_getlocation(s) has concurrency
457 * issues when libdw is not compiled with experimental
458 * --enable-thread-safety
460 pthread_mutex_lock(&libdw__lock);
461 if (dwarf_getlocation(&attr, expr, exprlen) == 0) {
462 /* DW_OP_addrx needs additional lookup for real addr. */
463 if (*exprlen != 0 && expr[0]->atom == DW_OP_addrx) {
464 Dwarf_Attribute addr_attr;
465 dwarf_getlocation_attr(&attr, expr[0], &addr_attr);
467 Dwarf_Addr address;
468 dwarf_formaddr (&addr_attr, &address);
470 expr[0]->number = address;
472 ret = 0;
474 pthread_mutex_unlock(&libdw__lock);
477 return ret;
480 /* The struct dwarf_tag has a fixed size while the 'struct tag' is just the base
481 * class for all DWARF DW_TAG_ tags, so we must have the fixed part first
482 * to be able to derive it from the one that has multiple sizes.
484 static void *tag__alloc(struct cu *cu, size_t size)
486 struct dwarf_tag *dtag = cu__zalloc(cu, sizeof(*dtag) + size);
488 return dtag ? dtag__tag(dtag) : NULL;
491 static void tag__free(struct tag *tag, struct cu *cu)
493 struct dwarf_tag *dtag = tag__dwarf(tag);
495 cu__free(cu, dtag);
498 #define dwarf_tag__set_attr_type(dtag, field, die, attr_name) \
499 dtag->from_types_section.field = attr_type(die, attr_name, &dtag->field)
501 static void tag__init(struct tag *tag, struct cu *cu, Dwarf_Die *die)
503 struct dwarf_tag *dtag = tag__dwarf(tag);
505 tag->tag = dwarf_tag(die);
507 dtag->id = dwarf_dieoffset(die);
509 if (tag->tag == DW_TAG_imported_module || tag->tag == DW_TAG_imported_declaration)
510 dwarf_tag__set_attr_type(dtag, type, die, DW_AT_import);
511 else
512 dwarf_tag__set_attr_type(dtag, type, die, DW_AT_type);
514 dwarf_tag__set_attr_type(dtag, abstract_origin, die, DW_AT_abstract_origin);
515 tag->recursivity_level = 0;
516 tag->attribute = NULL;
518 if (cu->extra_dbg_info) {
519 pthread_mutex_lock(&libdw__lock);
521 int32_t decl_line;
522 const char *decl_file = dwarf_decl_file(die);
523 static const char *last_decl_file, *last_decl_file_ptr;
525 if (decl_file != last_decl_file_ptr) {
526 last_decl_file = decl_file ? strdup(decl_file) : NULL;
527 last_decl_file_ptr = decl_file;
530 dtag->decl_file = last_decl_file;
531 dwarf_decl_line(die, &decl_line);
532 dtag->decl_line = decl_line;
534 pthread_mutex_unlock(&libdw__lock);
537 INIT_LIST_HEAD(&tag->node);
540 static struct tag *tag__new(Dwarf_Die *die, struct cu *cu)
542 struct tag *tag = tag__alloc(cu, sizeof(*tag));
544 if (tag != NULL)
545 tag__init(tag, cu, die);
547 return tag;
550 static void tag__set_spec(struct tag *tag, Dwarf_Die *die)
552 struct dwarf_tag *dtag = tag__dwarf(tag);
553 dwarf_tag__set_attr_type(dtag, specification, die, DW_AT_specification);
556 static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
557 struct cu *cu)
559 struct ptr_to_member_type *ptr = tag__alloc(cu, sizeof(*ptr));
561 if (ptr != NULL) {
562 tag__init(&ptr->tag, cu, die);
563 struct dwarf_tag *dtag = tag__dwarf(&ptr->tag);
564 dwarf_tag__set_attr_type(dtag, containing_type, die, DW_AT_containing_type);
567 return ptr;
570 static uint8_t encoding_to_float_type(uint64_t encoding)
572 switch (encoding) {
573 case DW_ATE_complex_float: return BT_FP_CMPLX;
574 case DW_ATE_float: return BT_FP_SINGLE;
575 case DW_ATE_imaginary_float: return BT_FP_IMGRY;
576 default: return 0;
580 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
582 struct base_type *bt = tag__alloc(cu, sizeof(*bt));
584 if (bt != NULL) {
585 tag__init(&bt->tag, cu, die);
586 bt->name = attr_string(die, DW_AT_name, conf);
587 bt->bit_size = attr_numeric(die, DW_AT_byte_size) * 8;
588 uint64_t encoding = attr_numeric(die, DW_AT_encoding);
589 bt->is_bool = encoding == DW_ATE_boolean;
590 bt->is_signed = (encoding == DW_ATE_signed) || (encoding == DW_ATE_signed_char);
591 bt->is_varargs = false;
592 bt->name_has_encoding = true;
593 bt->float_type = encoding_to_float_type(encoding);
594 INIT_LIST_HEAD(&bt->node);
597 return bt;
600 static struct array_type *array_type__new(Dwarf_Die *die, struct cu *cu)
602 struct array_type *at = tag__alloc(cu, sizeof(*at));
604 if (at != NULL) {
605 tag__init(&at->tag, cu, die);
606 at->dimensions = 0;
607 at->nr_entries = NULL;
608 at->is_vector = dwarf_hasattr(die, DW_AT_GNU_vector);
611 return at;
614 static struct string_type *string_type__new(Dwarf_Die *die, struct cu *cu)
616 struct string_type *st = tag__alloc(cu, sizeof(*st));
618 if (st != NULL) {
619 tag__init(&st->tag, cu, die);
620 st->nr_entries = attr_numeric(die, DW_AT_byte_size);
621 if (st->nr_entries == 0)
622 st->nr_entries = 1;
625 return st;
628 static void namespace__init(struct namespace *namespace, Dwarf_Die *die,
629 struct cu *cu, struct conf_load *conf)
631 tag__init(&namespace->tag, cu, die);
632 INIT_LIST_HEAD(&namespace->tags);
633 INIT_LIST_HEAD(&namespace->annots);
634 namespace->name = attr_string(die, DW_AT_name, conf);
635 namespace->tag.shared_tags = 0;
638 static struct namespace *namespace__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
640 struct namespace *namespace = tag__alloc(cu, sizeof(*namespace));
642 if (namespace != NULL)
643 namespace__init(namespace, die, cu, conf);
645 return namespace;
648 static void type__init(struct type *type, Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
650 namespace__init(&type->namespace, die, cu, conf);
651 __type__init(type);
652 type->size = attr_numeric(die, DW_AT_byte_size);
653 type->alignment = attr_alignment(die, conf);
654 type->declaration = attr_numeric(die, DW_AT_declaration);
655 tag__set_spec(&type->namespace.tag, die);
656 type->definition_emitted = 0;
657 type->fwd_decl_emitted = 0;
658 type->resized = 0;
659 type->nr_members = 0;
660 type->nr_static_members = 0;
661 type->is_signed_enum = 0;
663 Dwarf_Attribute attr;
664 if (dwarf_attr(die, DW_AT_type, &attr) != NULL) {
665 Dwarf_Die type_die;
666 if (dwarf_formref_die(&attr, &type_die) != NULL) {
667 uint64_t encoding = attr_numeric(&type_die, DW_AT_encoding);
669 if (encoding == DW_ATE_signed || encoding == DW_ATE_signed_char)
670 type->is_signed_enum = 1;
675 static struct type *type__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
677 struct type *type = tag__alloc(cu, sizeof(*type));
679 if (type != NULL)
680 type__init(type, die, cu, conf);
682 return type;
685 static struct enumerator *enumerator__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
687 struct enumerator *enumerator = tag__alloc(cu, sizeof(*enumerator));
689 if (enumerator != NULL) {
690 tag__init(&enumerator->tag, cu, die);
691 enumerator->name = attr_string(die, DW_AT_name, conf);
692 enumerator->value = attr_numeric(die, DW_AT_const_value);
695 return enumerator;
698 static enum vscope dwarf__location(Dwarf_Die *die, uint64_t *addr, struct location *location)
700 enum vscope scope = VSCOPE_UNKNOWN;
702 if (attr_location(die, &location->expr, &location->exprlen) != 0)
703 scope = VSCOPE_OPTIMIZED;
704 else if (location->exprlen != 0) {
705 Dwarf_Op *expr = location->expr;
706 switch (expr->atom) {
707 case DW_OP_addr:
708 case DW_OP_addrx:
709 scope = VSCOPE_GLOBAL;
710 *addr = expr[0].number;
711 break;
712 case DW_OP_reg1 ... DW_OP_reg31:
713 case DW_OP_breg0 ... DW_OP_breg31:
714 scope = VSCOPE_REGISTER; break;
715 case DW_OP_fbreg:
716 scope = VSCOPE_LOCAL; break;
720 return scope;
723 enum vscope variable__scope(const struct variable *var)
725 return var->scope;
728 const char *variable__scope_str(const struct variable *var)
730 switch (var->scope) {
731 case VSCOPE_LOCAL: return "local";
732 case VSCOPE_GLOBAL: return "global";
733 case VSCOPE_REGISTER: return "register";
734 case VSCOPE_OPTIMIZED: return "optimized";
735 default: break;
738 return "unknown";
741 static struct variable *variable__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf, int top_level)
743 bool has_specification = dwarf_hasattr(die, DW_AT_specification);
744 struct variable *var = tag__alloc(cu, sizeof(*var));
746 if (var != NULL) {
747 tag__init(&var->ip.tag, cu, die);
748 var->name = attr_string(die, DW_AT_name, conf);
749 /* variable is visible outside of its enclosing cu */
750 var->external = dwarf_hasattr(die, DW_AT_external);
751 /* non-defining declaration of an object */
752 var->declaration = dwarf_hasattr(die, DW_AT_declaration);
753 var->has_specification = has_specification;
754 var->artificial = dwarf_hasattr(die, DW_AT_artificial);
755 var->top_level = top_level;
756 var->scope = VSCOPE_UNKNOWN;
757 INIT_LIST_HEAD(&var->annots);
758 var->ip.addr = 0;
759 if (!var->declaration && cu->has_addr_info)
760 var->scope = dwarf__location(die, &var->ip.addr, &var->location);
761 if (has_specification) {
762 tag__set_spec(&var->ip.tag, die);
766 return var;
769 static struct constant *constant__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
771 struct constant *constant = tag__alloc(cu, sizeof(*constant));
773 if (constant != NULL) {
774 tag__init(&constant->tag, cu, die);
775 constant->name = attr_string(die, DW_AT_name, conf);
776 constant->value = attr_numeric(die, DW_AT_const_value);
779 return constant;
782 static int tag__recode_dwarf_bitfield(struct tag *tag, struct cu *cu, uint16_t bit_size)
784 int id;
785 type_id_t short_id;
786 struct tag *recoded;
787 /* in all the cases the name is at the same offset */
788 const char *name = namespace__name(tag__namespace(tag));
790 switch (tag->tag) {
791 case DW_TAG_typedef: {
792 const struct dwarf_tag *dtag = tag__dwarf(tag);
793 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, dtag, type);
795 if (dtype == NULL) {
796 tag__print_type_not_found(tag);
797 return -ENOENT;
800 struct tag *type = dtag__tag(dtype);
802 id = tag__recode_dwarf_bitfield(type, cu, bit_size);
803 if (id < 0)
804 return id;
806 struct type *new_typedef = cu__tag_alloc(cu, sizeof(*new_typedef));
807 if (new_typedef == NULL)
808 return -ENOMEM;
810 recoded = (struct tag *)new_typedef;
811 recoded->tag = DW_TAG_typedef;
812 recoded->type = id;
813 new_typedef->namespace.name = tag__namespace(tag)->name;
815 break;
817 case DW_TAG_const_type:
818 case DW_TAG_volatile_type:
819 case DW_TAG_atomic_type: {
820 const struct dwarf_tag *dtag = tag__dwarf(tag);
821 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, dtag, type);
823 if (dtype == NULL) {
824 tag__print_type_not_found(tag);
825 return -ENOENT;
828 struct tag *type = dtag__tag(dtype);
830 id = tag__recode_dwarf_bitfield(type, cu, bit_size);
831 if (id >= 0 && (uint32_t)id == tag->type)
832 return id;
834 recoded = cu__tag_alloc(cu, sizeof(*recoded));
835 if (recoded == NULL)
836 return -ENOMEM;
838 recoded->tag = DW_TAG_volatile_type;
839 recoded->type = id;
841 break;
843 case DW_TAG_base_type:
845 * Here we must search on the final, core cu, not on
846 * the dwarf_cu as in dwarf there are no such things
847 * as base_types of less than 8 bits, etc.
849 recoded = cu__find_base_type_by_name_and_size(cu, name, bit_size, &short_id);
850 if (recoded != NULL)
851 return short_id;
853 struct base_type *new_bt = cu__tag_alloc(cu, sizeof(*new_bt));
854 if (new_bt == NULL)
855 return -ENOMEM;
857 recoded = (struct tag *)new_bt;
858 recoded->tag = DW_TAG_base_type;
859 recoded->top_level = 1;
860 new_bt->name = strdup(name);
861 new_bt->bit_size = bit_size;
862 break;
864 case DW_TAG_enumeration_type:
866 * Here we must search on the final, core cu, not on
867 * the dwarf_cu as in dwarf there are no such things
868 * as enumeration_types of less than 8 bits, etc.
870 recoded = cu__find_enumeration_by_name_and_size(cu, name, bit_size, &short_id);
871 if (recoded != NULL)
872 return short_id;
874 struct type *alias = tag__type(tag);
875 struct type *new_enum = cu__tag_alloc(cu, sizeof(*new_enum));
876 if (new_enum == NULL)
877 return -ENOMEM;
879 recoded = (struct tag *)new_enum;
880 recoded->tag = DW_TAG_enumeration_type;
881 recoded->top_level = 1;
882 new_enum->nr_members = alias->nr_members;
884 * Share the tags
886 new_enum->namespace.tags.next = &alias->namespace.tags;
887 new_enum->namespace.tag.shared_tags = 1;
888 new_enum->namespace.name = strdup(name);
889 new_enum->size = bit_size;
890 break;
891 default:
892 fprintf(stderr, "%s: tag=%s, name=%s, bit_size=%d\n",
893 __func__, dwarf_tag_name(tag->tag),
894 name, bit_size);
895 return -EINVAL;
898 uint32_t new_id;
899 if (cu__add_tag(cu, recoded, &new_id) == 0)
900 return new_id;
902 cu__free(cu, recoded);
903 return -ENOMEM;
906 static int add_llvm_annotation(Dwarf_Die *die, int component_idx, struct conf_load *conf,
907 struct list_head *head)
909 struct llvm_annotation *annot;
910 const char *name;
912 if (conf->skip_encoding_btf_decl_tag)
913 return 0;
915 /* Only handle btf_decl_tag annotation for now. */
916 name = attr_string(die, DW_AT_name, conf);
917 if (strcmp(name, "btf_decl_tag") != 0)
918 return 0;
920 annot = zalloc(sizeof(*annot));
921 if (!annot)
922 return -ENOMEM;
924 annot->value = attr_string(die, DW_AT_const_value, conf);
925 annot->component_idx = component_idx;
926 list_add_tail(&annot->node, head);
927 return 0;
930 static int add_child_llvm_annotations(Dwarf_Die *die, int component_idx,
931 struct conf_load *conf, struct list_head *head)
933 Dwarf_Die child;
934 int ret;
936 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
937 return 0;
939 die = &child;
940 do {
941 if (dwarf_tag(die) == DW_TAG_LLVM_annotation) {
942 ret = add_llvm_annotation(die, component_idx, conf, head);
943 if (ret)
944 return ret;
946 } while (dwarf_siblingof(die, die) == 0);
948 return 0;
951 int class_member__dwarf_recode_bitfield(struct class_member *member,
952 struct cu *cu)
954 struct dwarf_tag *dtag = tag__dwarf(&member->tag);
955 struct dwarf_tag *type = dwarf_cu__find_type_by_ref(cu->priv, dtag, type);
956 int recoded_type_id;
958 if (type == NULL)
959 return -ENOENT;
961 recoded_type_id = tag__recode_dwarf_bitfield(dtag__tag(type), cu, member->bitfield_size);
962 if (recoded_type_id < 0)
963 return recoded_type_id;
965 member->tag.type = recoded_type_id;
966 return 0;
969 static struct class_member *class_member__new(Dwarf_Die *die, struct cu *cu,
970 bool in_union, struct conf_load *conf)
972 struct class_member *member = tag__alloc(cu, sizeof(*member));
974 if (member != NULL) {
975 tag__init(&member->tag, cu, die);
976 member->name = attr_string(die, DW_AT_name, conf);
977 member->alignment = attr_alignment(die, conf);
979 Dwarf_Attribute attr;
981 member->has_bit_offset = dwarf_attr(die, DW_AT_data_bit_offset, &attr) != NULL;
983 if (member->has_bit_offset) {
984 member->bit_offset = __attr_offset(&attr);
985 // byte_offset and bitfield_offset will be recalculated later, when
986 // we discover the size of this bitfield base type.
987 } else {
988 if (dwarf_attr(die, DW_AT_data_member_location, &attr) != NULL) {
989 member->byte_offset = __attr_offset(&attr);
990 } else {
991 member->is_static = !in_union;
995 * Bit offset calculated here is valid only for byte-aligned
996 * fields. For bitfields on little-endian archs we need to
997 * adjust them taking into account byte size of the field,
998 * which might not be yet known. So we'll re-calculate bit
999 * offset later, in class_member__cache_byte_size.
1001 member->bit_offset = member->byte_offset * 8;
1002 member->bitfield_offset = attr_numeric(die, DW_AT_bit_offset);
1006 * If DW_AT_byte_size is not present, byte size will be
1007 * determined later in class_member__cache_byte_size using
1008 * base integer/enum type
1010 member->byte_size = attr_numeric(die, DW_AT_byte_size);
1011 member->bitfield_size = attr_numeric(die, DW_AT_bit_size);
1012 member->bit_hole = 0;
1013 member->bitfield_end = 0;
1014 member->visited = 0;
1016 if (!cu__is_c(cu)) {
1017 member->accessibility = attr_numeric(die, DW_AT_accessibility);
1018 member->const_value = attr_numeric(die, DW_AT_const_value);
1019 member->virtuality = attr_numeric(die, DW_AT_virtuality);
1021 member->hole = 0;
1024 return member;
1027 /* How many function parameters are passed via registers? Used below in
1028 * determining if an argument has been optimized out or if it is simply
1029 * an argument > cu__nr_register_params(). Making cu__nr_register_params()
1030 * return 0 allows unsupported architectures to skip tagging optimized-out
1031 * values.
1033 static int arch__nr_register_params(const GElf_Ehdr *ehdr)
1035 switch (ehdr->e_machine) {
1036 case EM_S390: return 5;
1037 case EM_SPARC:
1038 case EM_SPARCV9:
1039 case EM_X86_64: return 6;
1040 case EM_AARCH64:
1041 case EM_ARC:
1042 case EM_ARM:
1043 case EM_MIPS:
1044 case EM_PPC:
1045 case EM_PPC64:
1046 case EM_RISCV: return 8;
1047 default: break;
1050 return 0;
1053 /* map from parameter index (0 for first, ...) to expected DW_OP_reg.
1054 * This will allow us to identify cases where optimized-out parameters
1055 * interfere with expectations about register contents on function
1056 * entry.
1058 static void arch__set_register_params(const GElf_Ehdr *ehdr, struct cu *cu)
1060 memset(cu->register_params, -1, sizeof(cu->register_params));
1062 switch (ehdr->e_machine) {
1063 case EM_S390:
1064 /* https://github.com/IBM/s390x-abi/releases/download/v1.6/lzsabi_s390x.pdf */
1065 cu->register_params[0] = DW_OP_reg2; // %r2
1066 cu->register_params[1] = DW_OP_reg3; // %r3
1067 cu->register_params[2] = DW_OP_reg4; // %r4
1068 cu->register_params[3] = DW_OP_reg5; // %r5
1069 cu->register_params[4] = DW_OP_reg6; // %r6
1070 return;
1071 case EM_X86_64:
1072 /* //en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI */
1073 cu->register_params[0] = DW_OP_reg5; // %rdi
1074 cu->register_params[1] = DW_OP_reg4; // %rsi
1075 cu->register_params[2] = DW_OP_reg1; // %rdx
1076 cu->register_params[3] = DW_OP_reg2; // %rcx
1077 cu->register_params[4] = DW_OP_reg8; // %r8
1078 cu->register_params[5] = DW_OP_reg9; // %r9
1079 return;
1080 case EM_ARM:
1081 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#machine-registers */
1082 case EM_AARCH64:
1083 /* https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
1084 cu->register_params[0] = DW_OP_reg0;
1085 cu->register_params[1] = DW_OP_reg1;
1086 cu->register_params[2] = DW_OP_reg2;
1087 cu->register_params[3] = DW_OP_reg3;
1088 cu->register_params[4] = DW_OP_reg4;
1089 cu->register_params[5] = DW_OP_reg5;
1090 cu->register_params[6] = DW_OP_reg6;
1091 cu->register_params[7] = DW_OP_reg7;
1092 return;
1093 default:
1094 return;
1098 static struct template_type_param *template_type_param__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1100 struct template_type_param *ttparm = tag__alloc(cu, sizeof(*ttparm));
1102 if (ttparm != NULL) {
1103 tag__init(&ttparm->tag, cu, die);
1104 ttparm->name = attr_string(die, DW_AT_name, conf);
1107 return ttparm;
1110 static struct template_value_param *template_value_param__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1112 struct template_value_param *tvparm = tag__alloc(cu, sizeof(*tvparm));
1114 if (tvparm != NULL) {
1115 tag__init(&tvparm->tag, cu, die);
1116 tvparm->name = attr_string(die, DW_AT_name, conf);
1117 tvparm->const_value = attr_numeric(die, DW_AT_const_value);
1118 tvparm->default_value = attr_numeric(die, DW_AT_default_value);
1121 return tvparm;
1124 static int template_parameter_pack__load_params(struct template_parameter_pack *pack, Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1126 Dwarf_Die child;
1128 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1129 return 0;
1131 die = &child;
1132 do {
1133 if (dwarf_tag(die) != DW_TAG_template_type_parameter) {
1134 cu__tag_not_handled(cu, die);
1135 continue;
1138 struct template_type_param *param = template_type_param__new(die, cu, conf);
1140 if (param == NULL)
1141 return -1;
1143 template_parameter_pack__add(pack, param);
1144 } while (dwarf_siblingof(die, die) == 0);
1146 return 0;
1149 static struct template_parameter_pack *template_parameter_pack__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1151 struct template_parameter_pack *pack = tag__alloc(cu, sizeof(*pack));
1153 if (pack != NULL) {
1154 tag__init(&pack->tag, cu, die);
1156 pack->name = attr_string(die, DW_AT_name, conf);
1157 INIT_LIST_HEAD(&pack->params);
1159 if (template_parameter_pack__load_params(pack, die, cu, conf)) {
1160 template_parameter_pack__delete(pack, cu);
1161 pack = NULL;
1165 return pack;
1168 /* Returns number of locations found or negative value for errors. */
1169 static ptrdiff_t __dwarf_getlocations(Dwarf_Attribute *attr,
1170 ptrdiff_t offset, Dwarf_Addr *basep,
1171 Dwarf_Addr *startp, Dwarf_Addr *endp,
1172 Dwarf_Op **expr, size_t *exprlen)
1174 int ret;
1176 #if _ELFUTILS_PREREQ(0, 157)
1177 ret = dwarf_getlocations(attr, offset, basep, startp, endp, expr, exprlen);
1178 #else
1179 if (offset == 0) {
1180 ret = dwarf_getlocation(attr, expr, exprlen);
1181 if (ret == 0)
1182 ret = 1;
1184 #endif
1185 return ret;
1188 /* For DW_AT_location 'attr':
1189 * - if first location is DW_OP_regXX with expected number, return the register;
1190 * otherwise save the register for later return
1191 * - if location DW_OP_entry_value(DW_OP_regXX) with expected number is in the
1192 * list, return the register; otherwise save register for later return
1193 * - otherwise if no register was found for locations, return -1.
1195 static int parameter__reg(Dwarf_Attribute *attr, int expected_reg)
1197 Dwarf_Addr base, start, end;
1198 Dwarf_Op *expr, *entry_ops;
1199 Dwarf_Attribute entry_attr;
1200 size_t exprlen, entry_len;
1201 ptrdiff_t offset = 0;
1202 int loc_num = -1;
1203 int ret = -1;
1205 /* use libdw__lock as dwarf_getlocation(s) has concurrency issues
1206 * when libdw is not compiled with experimental --enable-thread-safety
1208 pthread_mutex_lock(&libdw__lock);
1209 while ((offset = __dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &exprlen)) > 0) {
1210 loc_num++;
1212 /* Convert expression list (XX DW_OP_stack_value) -> (XX).
1213 * DW_OP_stack_value instructs interpreter to pop current value from
1214 * DWARF expression evaluation stack, and thus is not important here.
1216 if (exprlen > 1 && expr[exprlen - 1].atom == DW_OP_stack_value)
1217 exprlen--;
1219 if (exprlen != 1)
1220 continue;
1222 switch (expr->atom) {
1223 /* match DW_OP_regXX at first location */
1224 case DW_OP_reg0 ... DW_OP_reg31:
1225 if (loc_num != 0)
1226 break;
1227 ret = expr->atom;
1228 if (ret == expected_reg)
1229 goto out;
1230 break;
1231 /* match DW_OP_entry_value(DW_OP_regXX) at any location */
1232 case DW_OP_entry_value:
1233 case DW_OP_GNU_entry_value:
1234 if (dwarf_getlocation_attr(attr, expr, &entry_attr) == 0 &&
1235 dwarf_getlocation(&entry_attr, &entry_ops, &entry_len) == 0 &&
1236 entry_len == 1) {
1237 ret = entry_ops->atom;
1238 if (ret == expected_reg)
1239 goto out;
1241 break;
1244 out:
1245 pthread_mutex_unlock(&libdw__lock);
1246 return ret;
1249 static struct parameter *parameter__new(Dwarf_Die *die, struct cu *cu,
1250 struct conf_load *conf, int param_idx)
1252 struct parameter *parm = tag__alloc(cu, sizeof(*parm));
1254 if (parm != NULL) {
1255 bool has_const_value;
1256 Dwarf_Attribute attr;
1258 tag__init(&parm->tag, cu, die);
1259 parm->name = attr_string(die, DW_AT_name, conf);
1261 if (param_idx >= cu->nr_register_params || param_idx < 0)
1262 return parm;
1263 /* Parameters which use DW_AT_abstract_origin to point at
1264 * the original parameter definition (with no name in the DIE)
1265 * are the result of later DWARF generation during compilation
1266 * so often better take into account if arguments were
1267 * optimized out.
1269 * By checking that locations for parameters that are expected
1270 * to be passed as registers are actually passed as registers,
1271 * we can spot optimized-out parameters.
1273 * It can also be the case that a parameter DIE has
1274 * a constant value attribute reflecting optimization or
1275 * has no location attribute.
1277 * From the DWARF spec:
1279 * "4.1.10
1281 * A DW_AT_const_value attribute for an entry describing a
1282 * variable or formal parameter whose value is constant and not
1283 * represented by an object in the address space of the program,
1284 * or an entry describing a named constant. (Note
1285 * that such an entry does not have a location attribute.)"
1287 * So we can also use the absence of a location for a parameter
1288 * as evidence it has been optimized out. This info will
1289 * need to be shared between a parameter and any abstract
1290 * origin references however, since gcc can have location
1291 * information in the parameter that refers back to the original
1292 * via abstract origin, so we need to share location presence
1293 * between these parameter representations. See
1294 * ftype__recode_dwarf_types() below for how this is handled.
1296 has_const_value = dwarf_attr(die, DW_AT_const_value, &attr) != NULL;
1297 parm->has_loc = dwarf_attr(die, DW_AT_location, &attr) != NULL;
1299 if (parm->has_loc) {
1300 int expected_reg = cu->register_params[param_idx];
1301 int actual_reg = parameter__reg(&attr, expected_reg);
1303 if (actual_reg < 0)
1304 parm->optimized = 1;
1305 else if (expected_reg >= 0 && expected_reg != actual_reg)
1306 /* mark parameters that use an unexpected
1307 * register to hold a parameter; these will
1308 * be problematic for users of BTF as they
1309 * violate expectations about register
1310 * contents.
1312 parm->unexpected_reg = 1;
1313 } else if (has_const_value) {
1314 parm->optimized = 1;
1318 return parm;
1321 static int formal_parameter_pack__load_params(struct formal_parameter_pack *pack, Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1323 Dwarf_Die child;
1325 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1326 return 0;
1328 die = &child;
1329 do {
1330 if (dwarf_tag(die) != DW_TAG_formal_parameter) {
1331 cu__tag_not_handled(cu, die);
1332 continue;
1335 struct parameter *param = parameter__new(die, cu, conf, -1);
1337 if (param == NULL)
1338 return -1;
1340 formal_parameter_pack__add(pack, param);
1341 } while (dwarf_siblingof(die, die) == 0);
1343 return 0;
1346 static struct formal_parameter_pack *formal_parameter_pack__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1348 struct formal_parameter_pack *pack = tag__alloc(cu, sizeof(*pack));
1350 if (pack != NULL) {
1351 tag__init(&pack->tag, cu, die);
1353 INIT_LIST_HEAD(&pack->params);
1355 if (formal_parameter_pack__load_params(pack, die, cu, conf)) {
1356 formal_parameter_pack__delete(pack, cu);
1357 pack = NULL;
1361 return pack;
1364 static struct inline_expansion *inline_expansion__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1366 struct inline_expansion *exp = tag__alloc(cu, sizeof(*exp));
1368 if (exp != NULL) {
1369 struct dwarf_tag *dtag = tag__dwarf(&exp->ip.tag);
1371 tag__init(&exp->ip.tag, cu, die);
1372 dtag->decl_file = attr_string(die, DW_AT_call_file, conf);
1373 dtag->decl_line = attr_numeric(die, DW_AT_call_line);
1374 dwarf_tag__set_attr_type(dtag, type, die, DW_AT_abstract_origin);
1375 exp->ip.addr = 0;
1376 exp->high_pc = 0;
1378 if (!cu->has_addr_info)
1379 goto out;
1381 if (dwarf_lowpc(die, &exp->ip.addr))
1382 exp->ip.addr = 0;
1383 if (dwarf_lowpc(die, &exp->high_pc))
1384 exp->high_pc = 0;
1386 exp->size = exp->high_pc - exp->ip.addr;
1387 if (exp->size == 0) {
1388 Dwarf_Addr base, start;
1389 ptrdiff_t offset = 0;
1391 while (1) {
1392 offset = dwarf_ranges(die, offset, &base, &start,
1393 &exp->high_pc);
1394 start = (unsigned long)start;
1395 exp->high_pc = (unsigned long)exp->high_pc;
1396 if (offset <= 0)
1397 break;
1398 exp->size += exp->high_pc - start;
1399 if (exp->ip.addr == 0)
1400 exp->ip.addr = start;
1404 out:
1405 return exp;
1408 static struct label *label__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1410 struct label *label = tag__alloc(cu, sizeof(*label));
1412 if (label != NULL) {
1413 tag__init(&label->ip.tag, cu, die);
1414 label->name = attr_string(die, DW_AT_name, conf);
1415 if (!cu->has_addr_info || dwarf_lowpc(die, &label->ip.addr))
1416 label->ip.addr = 0;
1419 return label;
1422 static struct class *class__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1424 struct class *class = tag__alloc(cu, sizeof(*class));
1426 if (class != NULL) {
1427 type__init(&class->type, die, cu, conf);
1428 INIT_LIST_HEAD(&class->vtable);
1429 class->nr_vtable_entries =
1430 class->nr_holes =
1431 class->nr_bit_holes =
1432 class->padding =
1433 class->bit_padding = 0;
1434 class->priv = NULL;
1437 return class;
1440 static void lexblock__init(struct lexblock *block, struct cu *cu,
1441 Dwarf_Die *die)
1443 Dwarf_Off high_pc;
1445 if (!cu->has_addr_info || dwarf_lowpc(die, &block->ip.addr)) {
1446 block->ip.addr = 0;
1447 block->size = 0;
1448 } else if (dwarf_highpc(die, &high_pc))
1449 block->size = 0;
1450 else
1451 block->size = high_pc - block->ip.addr;
1453 INIT_LIST_HEAD(&block->tags);
1455 block->size_inline_expansions =
1456 block->nr_inline_expansions =
1457 block->nr_labels =
1458 block->nr_lexblocks =
1459 block->nr_variables = 0;
1462 static struct lexblock *lexblock__new(Dwarf_Die *die, struct cu *cu)
1464 struct lexblock *block = tag__alloc(cu, sizeof(*block));
1466 if (block != NULL) {
1467 tag__init(&block->ip.tag, cu, die);
1468 lexblock__init(block, cu, die);
1471 return block;
1474 static void ftype__init(struct ftype *ftype, Dwarf_Die *die, struct cu *cu)
1476 #ifndef NDEBUG
1477 const uint16_t tag = dwarf_tag(die);
1478 assert(tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type);
1479 #endif
1480 tag__init(&ftype->tag, cu, die);
1481 ftype->byte_size = attr_numeric(die, DW_AT_byte_size);
1482 INIT_LIST_HEAD(&ftype->parms);
1483 INIT_LIST_HEAD(&ftype->template_type_params);
1484 INIT_LIST_HEAD(&ftype->template_value_params);
1485 ftype->nr_parms = 0;
1486 ftype->unspec_parms = 0;
1487 ftype->template_parameter_pack = NULL;
1490 static struct ftype *ftype__new(Dwarf_Die *die, struct cu *cu)
1492 struct ftype *ftype = tag__alloc(cu, sizeof(*ftype));
1494 if (ftype != NULL)
1495 ftype__init(ftype, die, cu);
1497 return ftype;
1500 static struct function *function__new(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1502 struct function *func = tag__alloc(cu, sizeof(*func));
1504 if (func != NULL) {
1505 ftype__init(&func->proto, die, cu);
1506 lexblock__init(&func->lexblock, cu, die);
1507 func->name = attr_string(die, DW_AT_name, conf);
1508 func->linkage_name = attr_string(die, DW_AT_MIPS_linkage_name, conf);
1509 func->inlined = attr_numeric(die, DW_AT_inline);
1510 func->declaration = dwarf_hasattr(die, DW_AT_declaration);
1511 func->external = dwarf_hasattr(die, DW_AT_external);
1512 func->abstract_origin = dwarf_hasattr(die, DW_AT_abstract_origin);
1513 tag__set_spec(&func->proto.tag, die);
1514 func->accessibility = attr_numeric(die, DW_AT_accessibility);
1515 func->virtuality = attr_numeric(die, DW_AT_virtuality);
1516 INIT_LIST_HEAD(&func->vtable_node);
1517 INIT_LIST_HEAD(&func->annots);
1518 INIT_LIST_HEAD(&func->tool_node);
1519 func->vtable_entry = -1;
1520 if (dwarf_hasattr(die, DW_AT_vtable_elem_location))
1521 func->vtable_entry = attr_offset(die, DW_AT_vtable_elem_location);
1522 func->cu_total_size_inline_expansions = 0;
1523 func->cu_total_nr_inline_expansions = 0;
1524 func->priv = NULL;
1527 return func;
1530 static uint64_t attr_upper_bound(Dwarf_Die *die)
1532 Dwarf_Attribute attr;
1534 if (dwarf_attr(die, DW_AT_upper_bound, &attr) != NULL) {
1535 Dwarf_Word num;
1537 if (dwarf_formudata(&attr, &num) == 0) {
1538 return (uintmax_t)num + 1;
1540 } else if (dwarf_attr(die, DW_AT_count, &attr) != NULL) {
1541 Dwarf_Word num;
1543 if (dwarf_formudata(&attr, &num) == 0) {
1544 return (uintmax_t)num;
1548 return 0;
1551 static struct tag *__die__process_tag(Dwarf_Die *die, struct cu *cu,
1552 int toplevel, const char *fn, struct conf_load *conf);
1554 #define die__process_tag(die, cu, toplevel, conf_load) \
1555 __die__process_tag(die, cu, toplevel, __FUNCTION__, conf_load)
1557 static struct tag *die__create_new_tag(Dwarf_Die *die, struct cu *cu)
1559 struct tag *tag = tag__new(die, cu);
1561 if (tag != NULL) {
1562 if (dwarf_haschildren(die))
1563 fprintf(stderr, "%s: %s WITH children!\n", __func__,
1564 dwarf_tag_name(tag->tag));
1567 return tag;
1570 static struct btf_type_tag_ptr_type *die__create_new_btf_type_tag_ptr_type(Dwarf_Die *die, struct cu *cu)
1572 struct btf_type_tag_ptr_type *tag;
1574 tag = tag__alloc(cu, sizeof(struct btf_type_tag_ptr_type));
1575 if (tag == NULL)
1576 return NULL;
1578 tag__init(&tag->tag, cu, die);
1579 tag->tag.has_btf_type_tag = true;
1580 INIT_LIST_HEAD(&tag->tags);
1581 return tag;
1584 static struct btf_type_tag_type *die__create_new_btf_type_tag_type(Dwarf_Die *die, struct cu *cu,
1585 struct conf_load *conf)
1587 struct btf_type_tag_type *tag;
1589 tag = tag__alloc(cu, sizeof(struct btf_type_tag_type));
1590 if (tag == NULL)
1591 return NULL;
1593 tag__init(&tag->tag, cu, die);
1594 tag->value = attr_string(die, DW_AT_const_value, conf);
1595 return tag;
1598 static struct tag *die__create_new_pointer_tag(Dwarf_Die *die, struct cu *cu,
1599 struct conf_load *conf)
1601 struct btf_type_tag_ptr_type *tag = NULL;
1602 struct btf_type_tag_type *annot;
1603 Dwarf_Die *cdie, child;
1604 const char *name;
1605 uint32_t id;
1607 /* If no child tags or skipping btf_type_tag encoding, just create a new tag
1608 * and return
1610 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0 ||
1611 conf->skip_encoding_btf_type_tag)
1612 return tag__new(die, cu);
1614 /* Otherwise, check DW_TAG_LLVM_annotation child tags */
1615 cdie = &child;
1616 do {
1617 if (dwarf_tag(cdie) != DW_TAG_LLVM_annotation)
1618 continue;
1620 /* Only check btf_type_tag annotations */
1621 name = attr_string(cdie, DW_AT_name, conf);
1622 if (strcmp(name, "btf_type_tag") != 0)
1623 continue;
1625 if (tag == NULL) {
1626 /* Create a btf_type_tag_ptr type. */
1627 tag = die__create_new_btf_type_tag_ptr_type(die, cu);
1628 if (!tag)
1629 return NULL;
1632 /* Create a btf_type_tag type for this annotation. */
1633 annot = die__create_new_btf_type_tag_type(cdie, cu, conf);
1634 if (annot == NULL)
1635 return NULL;
1637 if (cu__table_add_tag(cu, &annot->tag, &id) < 0)
1638 return NULL;
1640 struct dwarf_tag *dtag = tag__dwarf(&annot->tag);
1641 dtag->small_id = id;
1642 cu__hash(cu, &annot->tag);
1644 /* For a list of DW_TAG_LLVM_annotation like tag1 -> tag2 -> tag3,
1645 * the tag->tags contains tag3 -> tag2 -> tag1.
1647 list_add(&annot->node, &tag->tags);
1648 } while (dwarf_siblingof(cdie, cdie) == 0);
1650 return tag ? &tag->tag : tag__new(die, cu);
1653 static struct tag *die__create_new_ptr_to_member_type(Dwarf_Die *die,
1654 struct cu *cu)
1656 struct ptr_to_member_type *ptr = ptr_to_member_type__new(die, cu);
1658 return ptr ? &ptr->tag : NULL;
1661 static int die__process_class(Dwarf_Die *die,
1662 struct type *class, struct cu *cu, struct conf_load *conf);
1664 static struct tag *die__create_new_class(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1666 Dwarf_Die child;
1667 struct class *class = class__new(die, cu, conf);
1669 if (class != NULL &&
1670 dwarf_haschildren(die) != 0 &&
1671 dwarf_child(die, &child) == 0) {
1672 if (die__process_class(&child, &class->type, cu, conf) != 0) {
1673 class__delete(class, cu);
1674 class = NULL;
1678 return class ? &class->type.namespace.tag : NULL;
1681 static int die__process_namespace(Dwarf_Die *die, struct namespace *namespace,
1682 struct cu *cu, struct conf_load *conf);
1684 static struct tag *die__create_new_namespace(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1686 Dwarf_Die child;
1687 struct namespace *namespace = namespace__new(die, cu, conf);
1689 if (namespace != NULL &&
1690 dwarf_haschildren(die) != 0 &&
1691 dwarf_child(die, &child) == 0) {
1692 if (die__process_namespace(&child, namespace, cu, conf) != 0) {
1693 namespace__delete(namespace, cu);
1694 namespace = NULL;
1698 return namespace ? &namespace->tag : NULL;
1701 static struct tag *die__create_new_union(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1703 Dwarf_Die child;
1704 struct type *utype = type__new(die, cu, conf);
1706 if (utype != NULL &&
1707 dwarf_haschildren(die) != 0 &&
1708 dwarf_child(die, &child) == 0) {
1709 if (die__process_class(&child, utype, cu, conf) != 0) {
1710 type__delete(utype, cu);
1711 utype = NULL;
1715 return utype ? &utype->namespace.tag : NULL;
1718 static struct tag *die__create_new_base_type(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1720 struct base_type *base = base_type__new(die, cu, conf);
1722 if (base == NULL)
1723 return NULL;
1725 if (dwarf_haschildren(die))
1726 fprintf(stderr, "%s: DW_TAG_base_type WITH children!\n",
1727 __func__);
1729 return &base->tag;
1732 static struct tag *die__create_new_typedef(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1734 struct type *tdef = type__new(die, cu, conf);
1736 if (tdef == NULL)
1737 return NULL;
1739 if (add_child_llvm_annotations(die, -1, conf, &tdef->namespace.annots))
1740 return NULL;
1742 return &tdef->namespace.tag;
1745 static struct tag *die__create_new_array(Dwarf_Die *die, struct cu *cu)
1747 Dwarf_Die child;
1748 /* "64 dimensions will be enough for everybody." acme, 2006 */
1749 const uint8_t max_dimensions = 64;
1750 uint32_t nr_entries[max_dimensions];
1751 struct array_type *array = array_type__new(die, cu);
1753 if (array == NULL)
1754 return NULL;
1756 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1757 return &array->tag;
1759 die = &child;
1760 do {
1761 if (dwarf_tag(die) == DW_TAG_subrange_type) {
1762 nr_entries[array->dimensions++] = attr_upper_bound(die);
1763 if (array->dimensions == max_dimensions) {
1764 fprintf(stderr, "%s: only %u dimensions are "
1765 "supported!\n",
1766 __FUNCTION__, max_dimensions);
1767 break;
1769 } else
1770 cu__tag_not_handled(cu, die);
1771 } while (dwarf_siblingof(die, die) == 0);
1773 array->nr_entries = memdup(nr_entries,
1774 array->dimensions * sizeof(uint32_t), cu);
1775 if (array->nr_entries == NULL)
1776 goto out_free;
1778 return &array->tag;
1779 out_free:
1780 tag__free(&array->tag, cu);
1781 return NULL;
1784 static struct tag *die__create_new_string_type(Dwarf_Die *die, struct cu *cu)
1786 struct string_type *string = string_type__new(die, cu);
1788 if (string == NULL)
1789 return NULL;
1791 return &string->tag;
1794 static struct tag *die__create_new_parameter(Dwarf_Die *die,
1795 struct ftype *ftype,
1796 struct lexblock *lexblock,
1797 struct cu *cu, struct conf_load *conf,
1798 int param_idx)
1800 struct parameter *parm = parameter__new(die, cu, conf, param_idx);
1802 if (parm == NULL)
1803 return NULL;
1805 if (ftype != NULL) {
1806 ftype__add_parameter(ftype, parm);
1807 if (param_idx >= 0) {
1808 if (add_child_llvm_annotations(die, param_idx, conf, &(tag__function(&ftype->tag)->annots)))
1809 return NULL;
1811 } else {
1813 * DW_TAG_formal_parameters on a non DW_TAG_subprogram nor
1814 * DW_TAG_subroutine_type tag happens sometimes, likely due to
1815 * compiler optimizing away a inline expansion (at least this
1816 * was observed in some cases, such as in the Linux kernel
1817 * current_kernel_time function circa 2.6.20-rc5), keep it in
1818 * the lexblock tag list because it can be referenced as an
1819 * DW_AT_abstract_origin in another DW_TAG_formal_parameter.
1821 lexblock__add_tag(lexblock, &parm->tag);
1824 return &parm->tag;
1827 static struct tag *die__create_new_label(Dwarf_Die *die,
1828 struct lexblock *lexblock,
1829 struct cu *cu, struct conf_load *conf)
1831 struct label *label = label__new(die, cu, conf);
1833 if (label == NULL)
1834 return NULL;
1836 if (lexblock != NULL) {
1837 // asm CUs have labels and they will be in the cu top level tag list
1838 // See die__process_unit()
1839 lexblock__add_label(lexblock, label);
1842 return &label->ip.tag;
1845 static struct tag *die__create_new_variable(Dwarf_Die *die, struct cu *cu, struct conf_load *conf, int top_level)
1847 struct variable *var = variable__new(die, cu, conf, top_level);
1849 if (var == NULL || add_child_llvm_annotations(die, -1, conf, &var->annots))
1850 return NULL;
1852 return &var->ip.tag;
1855 static struct tag *die__create_new_constant(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1857 struct constant *constant = constant__new(die, cu, conf);
1859 if (constant == NULL)
1860 return NULL;
1862 return &constant->tag;
1865 static struct tag *die__create_new_subroutine_type(Dwarf_Die *die,
1866 struct cu *cu, struct conf_load *conf)
1868 Dwarf_Die child;
1869 struct ftype *ftype = ftype__new(die, cu);
1870 struct tag *tag;
1872 if (ftype == NULL)
1873 return NULL;
1875 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1876 goto out;
1878 die = &child;
1879 do {
1880 uint32_t id;
1882 switch (dwarf_tag(die)) {
1883 case DW_TAG_subrange_type: // ADA stuff
1884 tag__print_not_supported(die);
1885 continue;
1886 case DW_TAG_formal_parameter:
1887 tag = die__create_new_parameter(die, ftype, NULL, cu, conf, -1);
1888 break;
1889 case DW_TAG_unspecified_parameters:
1890 ftype->unspec_parms = 1;
1891 continue;
1892 default:
1893 tag = die__process_tag(die, cu, 0, conf);
1894 if (tag == NULL)
1895 goto out_delete;
1897 if (tag == &unsupported_tag) {
1898 tag__print_not_supported(die);
1899 continue;
1902 if (cu__add_tag(cu, tag, &id) < 0)
1903 goto out_delete_tag;
1905 goto hash;
1908 if (tag == NULL)
1909 goto out_delete;
1911 if (cu__table_add_tag(cu, tag, &id) < 0)
1912 goto out_delete_tag;
1913 hash:
1914 cu__hash(cu, tag);
1915 struct dwarf_tag *dtag = tag__dwarf(tag);
1916 dtag->small_id = id;
1917 } while (dwarf_siblingof(die, die) == 0);
1918 out:
1919 return &ftype->tag;
1920 out_delete_tag:
1921 tag__delete(tag, cu);
1922 out_delete:
1923 ftype__delete(ftype, cu);
1924 return NULL;
1927 static struct tag *die__create_new_enumeration(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
1929 Dwarf_Die child;
1930 struct type *enumeration = type__new(die, cu, conf);
1932 if (enumeration == NULL)
1933 return NULL;
1935 if (enumeration->size == 0)
1936 enumeration->size = sizeof(int) * 8;
1937 else
1938 enumeration->size *= 8;
1940 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0) {
1941 /* Seen on libQtCore.so.4.3.4.debug,
1942 * class QAbstractFileEngineIterator, enum EntryInfoType */
1943 goto out;
1946 die = &child;
1947 do {
1948 struct enumerator *enumerator;
1950 if (dwarf_tag(die) != DW_TAG_enumerator) {
1951 cu__tag_not_handled(cu, die);
1952 continue;
1954 enumerator = enumerator__new(die, cu, conf);
1955 if (enumerator == NULL)
1956 goto out_delete;
1958 enumeration__add(enumeration, enumerator);
1959 cu__hash(cu, &enumerator->tag);
1960 } while (dwarf_siblingof(die, die) == 0);
1961 out:
1962 return &enumeration->namespace.tag;
1963 out_delete:
1964 enumeration__delete(enumeration, cu);
1965 return NULL;
1968 static int die__process_class(Dwarf_Die *die, struct type *class,
1969 struct cu *cu, struct conf_load *conf)
1971 const bool is_union = tag__is_union(&class->namespace.tag);
1972 int member_idx = 0;
1974 do {
1975 switch (dwarf_tag(die)) {
1976 #ifdef STB_GNU_UNIQUE
1977 case DW_TAG_GNU_template_parameter_pack:
1978 class->template_parameter_pack = template_parameter_pack__new(die, cu, conf);
1980 if (class->template_parameter_pack == NULL)
1981 return -ENOMEM;
1983 continue;
1984 case DW_TAG_GNU_formal_parameter_pack:
1985 case DW_TAG_GNU_template_template_param:
1986 #endif
1987 case DW_TAG_subrange_type: // XXX: ADA stuff, its a type tho, will have other entries referencing it...
1988 case DW_TAG_variant_part: // XXX: Rust stuff
1989 tag__print_not_supported(die);
1990 continue;
1991 case DW_TAG_template_type_parameter: {
1992 struct template_type_param *ttparm = template_type_param__new(die, cu, conf);
1994 if (ttparm == NULL)
1995 return -ENOMEM;
1997 type__add_template_type_param(class, ttparm);
1998 continue;
2000 case DW_TAG_template_value_parameter: {
2002 * FIXME: probably we'll have to attach this as a list of
2003 * template parameters to use at class__fprintf time...
2005 * See:
2006 * https://gcc.gnu.org/wiki/TemplateParmsDwarf
2008 struct template_value_param *tvparm = template_value_param__new(die, cu, conf);
2010 if (tvparm == NULL)
2011 return -ENOMEM;
2013 type__add_template_value_param(class, tvparm);
2014 continue;
2016 case DW_TAG_inheritance:
2017 case DW_TAG_member: {
2018 struct class_member *member = class_member__new(die, cu, is_union, conf);
2020 if (member == NULL)
2021 return -ENOMEM;
2023 if (cu__is_c_plus_plus(cu)) {
2024 uint32_t id;
2026 if (cu__table_add_tag(cu, &member->tag, &id) < 0) {
2027 class_member__delete(member, cu);
2028 return -ENOMEM;
2031 struct dwarf_tag *dtag = tag__dwarf(&member->tag);
2032 dtag->small_id = id;
2035 type__add_member(class, member);
2036 cu__hash(cu, &member->tag);
2037 if (add_child_llvm_annotations(die, member_idx, conf, &class->namespace.annots))
2038 return -ENOMEM;
2039 member_idx++;
2041 continue;
2042 case DW_TAG_LLVM_annotation:
2043 if (add_llvm_annotation(die, -1, conf, &class->namespace.annots))
2044 return -ENOMEM;
2045 continue;
2046 default: {
2047 struct tag *tag = die__process_tag(die, cu, 0, conf);
2049 if (tag == NULL)
2050 return -ENOMEM;
2052 if (tag == &unsupported_tag) {
2053 tag__print_not_supported(die);
2054 continue;
2057 uint32_t id;
2059 if (cu__table_add_tag(cu, tag, &id) < 0) {
2060 tag__delete(tag, cu);
2061 return -ENOMEM;
2064 struct dwarf_tag *dtag = tag__dwarf(tag);
2065 dtag->small_id = id;
2067 namespace__add_tag(&class->namespace, tag);
2068 cu__hash(cu, tag);
2069 if (tag__is_function(tag)) {
2070 struct function *fself = tag__function(tag);
2072 if (fself->vtable_entry != -1)
2073 class__add_vtable_entry(type__class(class), fself);
2075 continue;
2078 } while (dwarf_siblingof(die, die) == 0);
2080 return 0;
2083 static int die__process_namespace(Dwarf_Die *die, struct namespace *namespace,
2084 struct cu *cu, struct conf_load *conf)
2086 struct tag *tag;
2087 do {
2088 tag = die__process_tag(die, cu, 0, conf);
2089 if (tag == NULL)
2090 goto out_enomem;
2092 if (tag == &unsupported_tag) {
2093 tag__print_not_supported(die);
2094 continue;
2097 uint32_t id;
2098 if (cu__table_add_tag(cu, tag, &id) < 0)
2099 goto out_delete_tag;
2101 struct dwarf_tag *dtag = tag__dwarf(tag);
2102 dtag->small_id = id;
2104 namespace__add_tag(namespace, tag);
2105 cu__hash(cu, tag);
2106 } while (dwarf_siblingof(die, die) == 0);
2108 return 0;
2109 out_delete_tag:
2110 tag__delete(tag, cu);
2111 out_enomem:
2112 return -ENOMEM;
2115 static int die__process_function(Dwarf_Die *die, struct ftype *ftype,
2116 struct lexblock *lexblock, struct cu *cu, struct conf_load *conf);
2118 static int die__create_new_lexblock(Dwarf_Die *die,
2119 struct cu *cu, struct lexblock *father, struct conf_load *conf)
2121 struct lexblock *lexblock = lexblock__new(die, cu);
2123 if (lexblock != NULL) {
2124 if (die__process_function(die, NULL, lexblock, cu, conf) != 0)
2125 goto out_delete;
2127 if (father != NULL)
2128 lexblock__add_lexblock(father, lexblock);
2129 return 0;
2130 out_delete:
2131 lexblock__delete(lexblock, cu);
2132 return -ENOMEM;
2135 static struct tag *die__create_new_inline_expansion(Dwarf_Die *die,
2136 struct lexblock *lexblock,
2137 struct cu *cu, struct conf_load *conf);
2139 static int die__process_inline_expansion(Dwarf_Die *die, struct lexblock *lexblock, struct cu *cu, struct conf_load *conf)
2141 Dwarf_Die child;
2142 struct tag *tag;
2144 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
2145 return 0;
2147 die = &child;
2148 do {
2149 uint32_t id;
2151 switch (dwarf_tag(die)) {
2152 case DW_TAG_call_site:
2153 case DW_TAG_call_site_parameter:
2154 case DW_TAG_GNU_call_site:
2155 case DW_TAG_GNU_call_site_parameter:
2157 * FIXME: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
2158 * and write proper support.
2160 * From a quick read there is not much we can use in
2161 * the existing dwarves tools, so just stop warning the user,
2162 * developers will find these notes if wanting to use in a
2163 * new tool.
2165 continue;
2166 case DW_TAG_lexical_block:
2167 if (die__create_new_lexblock(die, cu, lexblock, conf) != 0)
2168 goto out_enomem;
2169 continue;
2170 case DW_TAG_formal_parameter:
2172 * FIXME:
2173 * So far DW_TAG_inline_routine had just an
2174 * abstract origin, but starting with
2175 * /usr/lib/openoffice.org/basis3.0/program/libdbalx.so
2176 * I realized it really has to be handled as a
2177 * DW_TAG_function... Lets just get the types
2178 * for 1.8, then fix this properly.
2180 * cu__tag_not_handled(cu, die);
2182 continue;
2183 case DW_TAG_inlined_subroutine:
2184 tag = die__create_new_inline_expansion(die, lexblock, cu, conf);
2185 break;
2186 case DW_TAG_label:
2187 if (conf->ignore_labels)
2188 continue;
2189 tag = die__create_new_label(die, lexblock, cu, conf);
2190 break;
2191 default:
2192 tag = die__process_tag(die, cu, 0, conf);
2193 if (tag == NULL)
2194 goto out_enomem;
2196 if (tag == &unsupported_tag) {
2197 tag__print_not_supported(die);
2198 continue;
2201 if (cu__add_tag(cu, tag, &id) < 0)
2202 goto out_delete_tag;
2203 goto hash;
2206 if (tag == NULL)
2207 goto out_enomem;
2209 if (cu__table_add_tag(cu, tag, &id) < 0)
2210 goto out_delete_tag;
2211 hash:
2212 cu__hash(cu, tag);
2213 struct dwarf_tag *dtag = tag__dwarf(tag);
2214 dtag->small_id = id;
2215 } while (dwarf_siblingof(die, die) == 0);
2217 return 0;
2218 out_delete_tag:
2219 tag__delete(tag, cu);
2220 out_enomem:
2221 return -ENOMEM;
2224 static struct tag *die__create_new_inline_expansion(Dwarf_Die *die,
2225 struct lexblock *lexblock,
2226 struct cu *cu, struct conf_load *conf)
2228 struct inline_expansion *exp = inline_expansion__new(die, cu, conf);
2230 if (exp == NULL)
2231 return NULL;
2233 if (die__process_inline_expansion(die, lexblock, cu, conf) != 0) {
2234 tag__free(&exp->ip.tag, cu);
2235 return NULL;
2238 if (lexblock != NULL)
2239 lexblock__add_inline_expansion(lexblock, exp);
2240 return &exp->ip.tag;
2243 static int die__process_function(Dwarf_Die *die, struct ftype *ftype,
2244 struct lexblock *lexblock, struct cu *cu, struct conf_load *conf)
2246 int param_idx = 0;
2247 Dwarf_Die child;
2248 struct tag *tag;
2250 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
2251 return 0;
2253 die = &child;
2254 do {
2255 uint32_t id;
2257 switch (dwarf_tag(die)) {
2258 case DW_TAG_call_site:
2259 case DW_TAG_call_site_parameter:
2260 case DW_TAG_GNU_call_site:
2261 case DW_TAG_GNU_call_site_parameter:
2263 * XXX: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
2264 * and write proper support.
2266 * From a quick read there is not much we can use in
2267 * the existing dwarves tools, so just stop warning the user,
2268 * developers will find these notes if wanting to use in a
2269 * new tool.
2271 continue;
2272 case DW_TAG_dwarf_procedure:
2274 * Ignore it, just scope expressions, that we have no use for (so far).
2276 continue;
2277 #ifdef STB_GNU_UNIQUE
2278 case DW_TAG_GNU_template_parameter_pack:
2279 ftype->template_parameter_pack = template_parameter_pack__new(die, cu, conf);
2281 if (ftype->template_parameter_pack == NULL)
2282 return -ENOMEM;
2284 continue;
2285 case DW_TAG_GNU_formal_parameter_pack:
2286 ftype->formal_parameter_pack = formal_parameter_pack__new(die, cu, conf);
2288 if (ftype->formal_parameter_pack == NULL)
2289 return -ENOMEM;
2291 continue;
2292 case DW_TAG_GNU_template_template_param:
2293 #endif
2294 tag__print_not_supported(die);
2295 continue;
2296 case DW_TAG_template_type_parameter: {
2297 struct template_type_param *ttparm = template_type_param__new(die, cu, conf);
2299 if (ttparm == NULL)
2300 return -ENOMEM;
2302 ftype__add_template_type_param(ftype, ttparm);
2303 continue;
2305 case DW_TAG_template_value_parameter: {
2306 /* FIXME: probably we'll have to attach this as a list of
2307 * template parameters to use at class__fprintf time...
2308 * See die__process_class */
2309 struct template_value_param *tvparm = template_value_param__new(die, cu, conf);
2311 if (tvparm == NULL)
2312 return -ENOMEM;
2314 ftype__add_template_value_param(ftype, tvparm);
2315 continue;
2317 case DW_TAG_formal_parameter:
2318 tag = die__create_new_parameter(die, ftype, lexblock, cu, conf, param_idx++);
2319 break;
2320 case DW_TAG_variable:
2321 tag = die__create_new_variable(die, cu, conf, 0);
2322 if (tag == NULL)
2323 goto out_enomem;
2324 lexblock__add_variable(lexblock, tag__variable(tag));
2325 break;
2326 case DW_TAG_unspecified_parameters:
2327 if (ftype != NULL)
2328 ftype->unspec_parms = 1;
2329 continue;
2330 case DW_TAG_label:
2331 if (conf->ignore_labels)
2332 continue;
2333 tag = die__create_new_label(die, lexblock, cu, conf);
2334 break;
2335 case DW_TAG_inlined_subroutine:
2336 if (conf->ignore_inline_expansions)
2337 continue;
2338 tag = die__create_new_inline_expansion(die, lexblock, cu, conf);
2339 break;
2340 case DW_TAG_lexical_block:
2341 // lexblocks can contain types that are then referenced from outside.
2342 // Thus we can't ignore them without more surgery, i.e. by adding code
2343 // to just process types inside lexblocks, leave this for later.
2344 if (die__create_new_lexblock(die, cu, lexblock, conf) != 0)
2345 goto out_enomem;
2346 continue;
2347 case DW_TAG_LLVM_annotation:
2348 if (add_llvm_annotation(die, -1, conf, &(tag__function(&ftype->tag)->annots)))
2349 goto out_enomem;
2350 continue;
2351 default:
2352 tag = die__process_tag(die, cu, 0, conf);
2354 if (tag == NULL)
2355 goto out_enomem;
2357 if (tag == &unsupported_tag) {
2358 tag__print_not_supported(die);
2359 continue;
2362 if (cu__add_tag(cu, tag, &id) < 0)
2363 goto out_delete_tag;
2365 goto hash;
2368 if (tag == NULL)
2369 goto out_enomem;
2371 if (cu__table_add_tag(cu, tag, &id) < 0)
2372 goto out_delete_tag;
2373 hash:
2374 cu__hash(cu, tag);
2375 struct dwarf_tag *dtag = tag__dwarf(tag);
2376 dtag->small_id = id;
2377 } while (dwarf_siblingof(die, die) == 0);
2379 return 0;
2380 out_delete_tag:
2381 tag__delete(tag, cu);
2382 out_enomem:
2383 return -ENOMEM;
2386 static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
2388 struct function *function = function__new(die, cu, conf);
2390 if (function != NULL &&
2391 die__process_function(die, &function->proto, &function->lexblock, cu, conf) != 0) {
2392 function__delete(function, cu);
2393 function = NULL;
2396 return function ? &function->proto.tag : NULL;
2399 static struct tag *__die__process_tag(Dwarf_Die *die, struct cu *cu,
2400 int top_level, const char *fn, struct conf_load *conf)
2402 struct tag *tag;
2404 switch (dwarf_tag(die)) {
2405 case DW_TAG_imported_unit:
2406 return NULL; // We don't support imported units yet, so to avoid segfaults
2407 case DW_TAG_array_type:
2408 tag = die__create_new_array(die, cu); break;
2409 case DW_TAG_string_type: // FORTRAN stuff, looks like an array
2410 tag = die__create_new_string_type(die, cu); break;
2411 case DW_TAG_base_type:
2412 tag = die__create_new_base_type(die, cu, conf); break;
2413 case DW_TAG_const_type:
2414 case DW_TAG_imported_declaration:
2415 case DW_TAG_imported_module:
2416 case DW_TAG_reference_type:
2417 case DW_TAG_restrict_type:
2418 case DW_TAG_volatile_type:
2419 case DW_TAG_atomic_type:
2420 tag = die__create_new_tag(die, cu); break;
2421 case DW_TAG_unspecified_type:
2422 tag = die__create_new_tag(die, cu); break;
2423 case DW_TAG_pointer_type:
2424 tag = die__create_new_pointer_tag(die, cu, conf); break;
2425 case DW_TAG_ptr_to_member_type:
2426 tag = die__create_new_ptr_to_member_type(die, cu); break;
2427 case DW_TAG_enumeration_type:
2428 tag = die__create_new_enumeration(die, cu, conf); break;
2429 case DW_TAG_namespace:
2430 tag = die__create_new_namespace(die, cu, conf); break;
2431 case DW_TAG_class_type:
2432 case DW_TAG_interface_type:
2433 case DW_TAG_structure_type:
2434 tag = die__create_new_class(die, cu, conf); break;
2435 case DW_TAG_subprogram:
2436 tag = die__create_new_function(die, cu, conf); break;
2437 case DW_TAG_subroutine_type:
2438 tag = die__create_new_subroutine_type(die, cu, conf); break;
2439 case DW_TAG_rvalue_reference_type:
2440 case DW_TAG_typedef:
2441 tag = die__create_new_typedef(die, cu, conf); break;
2442 case DW_TAG_union_type:
2443 tag = die__create_new_union(die, cu, conf); break;
2444 case DW_TAG_variable:
2445 tag = die__create_new_variable(die, cu, conf, top_level); break;
2446 case DW_TAG_constant: // First seen in a Go CU
2447 tag = die__create_new_constant(die, cu, conf); break;
2448 default:
2449 __cu__tag_not_handled(cu, die, fn);
2450 /* fall thru */
2451 case DW_TAG_dwarf_procedure:
2453 * Ignore it, just scope expressions, that we have no use for (so far).
2455 tag = &unsupported_tag;
2456 break;
2457 case DW_TAG_label:
2458 if (conf->ignore_labels)
2459 tag = &unsupported_tag; // callers will assume conf->ignore_labels is true
2460 else // We can have labels in asm CUs, no lexblock
2461 tag = die__create_new_label(die, NULL, cu, conf);
2462 break;
2465 if (tag != NULL)
2466 tag->top_level = top_level;
2468 return tag;
2471 static int die__process_unit(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
2473 do {
2474 struct tag *tag = die__process_tag(die, cu, 1, conf);
2475 if (tag == NULL)
2476 return -ENOMEM;
2478 if (tag == &unsupported_tag) {
2479 // XXX special case DW_TAG_dwarf_procedure, appears when looking at a recent ~/bin/perf
2480 // Investigate later how to properly support this...
2481 if (dwarf_tag(die) != DW_TAG_dwarf_procedure &&
2482 dwarf_tag(die) != DW_TAG_label) // conf->ignore_labels == true, see die__process_tag()
2483 tag__print_not_supported(die);
2484 continue;
2487 uint32_t id = 0;
2488 /* There is no BTF representation for unspecified types.
2489 * Currently we want such types to be represented as `void`
2490 * (and thus skip BTF encoding).
2492 * As BTF encoding is skipped, such types must not be added to type table,
2493 * otherwise an ID for a type would be allocated and we would be forced
2494 * to put something in BTF at this ID.
2495 * Thus avoid `cu__add_tag()` call for such types.
2497 * On the other hand, there might be references to this type from other
2498 * tags, so `dwarf_cu__find_tag_by_ref()` must return something.
2499 * Thus call `cu__hash()` for such types.
2501 * Note, that small_id of zero would be assigned to unspecified type entry.
2503 if (tag->tag != DW_TAG_unspecified_type)
2504 cu__add_tag(cu, tag, &id);
2505 cu__hash(cu, tag);
2506 struct dwarf_tag *dtag = tag__dwarf(tag);
2507 dtag->small_id = id;
2508 } while (dwarf_siblingof(die, die) == 0);
2510 return 0;
2513 static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu);
2515 static int namespace__recode_dwarf_types(struct tag *tag, struct cu *cu)
2517 struct tag *pos;
2518 struct dwarf_cu *dcu = cu->priv;
2519 struct namespace *ns = tag__namespace(tag);
2521 namespace__for_each_tag(ns, pos) {
2522 struct dwarf_tag *dtype;
2523 struct dwarf_tag *dpos = tag__dwarf(pos);
2525 if (tag__has_namespace(pos)) {
2526 if (namespace__recode_dwarf_types(pos, cu))
2527 return -1;
2528 continue;
2531 switch (pos->tag) {
2532 case DW_TAG_member: {
2533 struct class_member *member = tag__class_member(pos);
2535 * We may need to recode the type, possibly creating a
2536 * suitably sized new base_type
2538 if (member->bitfield_size != 0 && !no_bitfield_type_recode) {
2539 if (class_member__dwarf_recode_bitfield(member, cu))
2540 return -1;
2541 continue;
2544 break;
2545 case DW_TAG_subroutine_type:
2546 case DW_TAG_subprogram:
2547 ftype__recode_dwarf_types(pos, cu);
2548 break;
2549 case DW_TAG_imported_module:
2550 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, type);
2551 goto check_type;
2552 /* Can be for both types and non types */
2553 case DW_TAG_imported_declaration:
2554 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, type);
2555 if (dtype != NULL)
2556 goto next;
2557 goto find_type;
2560 if (dpos->type == 0) /* void */
2561 continue;
2562 find_type:
2563 dtype = dwarf_cu__find_type_by_ref(dcu, dpos, type);
2564 check_type:
2565 if (dtype == NULL) {
2566 tag__print_type_not_found(pos);
2567 continue;
2569 next:
2570 pos->type = dtype->small_id;
2572 return 0;
2575 static void type__recode_dwarf_specification(struct tag *tag, struct cu *cu)
2577 struct dwarf_tag *dtype;
2578 struct type *t = tag__type(tag);
2579 struct dwarf_tag *dtag = tag__dwarf(tag);
2581 if (t->namespace.name != 0 || dtag->specification == 0)
2582 return;
2584 dtype = dwarf_cu__find_type_by_ref(cu->priv, dtag, specification);
2585 if (dtype != NULL)
2586 t->namespace.name = tag__namespace(dtag__tag(dtype))->name;
2587 else {
2588 fprintf(stderr,
2589 "%s: couldn't find name for "
2590 "class %#llx, specification=%#llx\n", __func__,
2591 (unsigned long long)dtag->id,
2592 (unsigned long long)dtag->specification);
2596 static void __tag__print_abstract_origin_not_found(struct tag *tag,
2597 const char *func, int line)
2599 struct dwarf_tag *dtag = tag__dwarf(tag);
2600 fprintf(stderr,
2601 "%s(%d): couldn't find %#llx abstract_origin for %#llx (%s)!\n",
2602 func, line, (unsigned long long)dtag->abstract_origin,
2603 (unsigned long long)dtag->id,
2604 dwarf_tag_name(tag->tag));
2607 #define tag__print_abstract_origin_not_found(tag) \
2608 __tag__print_abstract_origin_not_found(tag, __func__, __LINE__)
2610 static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu)
2612 struct parameter *pos;
2613 struct dwarf_cu *dcu = cu->priv;
2614 struct ftype *type = tag__ftype(tag);
2616 ftype__for_each_parameter(type, pos) {
2617 struct dwarf_tag *dpos = tag__dwarf(&pos->tag);
2618 struct parameter *opos;
2619 struct dwarf_tag *dtype;
2621 if (dpos->type == 0) {
2622 if (dpos->abstract_origin == 0) {
2623 /* Function without parameters */
2624 pos->tag.type = 0;
2625 continue;
2627 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, abstract_origin);
2628 if (dtype == NULL) {
2629 tag__print_abstract_origin_not_found(&pos->tag);
2630 continue;
2632 opos = tag__parameter(dtag__tag(dtype));
2633 pos->name = opos->name;
2634 pos->tag.type = dtag__tag(dtype)->type;
2635 /* share location information between parameter and
2636 * abstract origin; if neither have location, we will
2637 * mark the parameter as optimized out. Also share
2638 * info regarding unexpected register use for
2639 * parameters.
2641 if (pos->has_loc)
2642 opos->has_loc = pos->has_loc;
2644 if (pos->optimized)
2645 opos->optimized = pos->optimized;
2646 if (pos->unexpected_reg)
2647 opos->unexpected_reg = pos->unexpected_reg;
2648 continue;
2651 dtype = dwarf_cu__find_type_by_ref(dcu, dpos, type);
2652 if (dtype == NULL) {
2653 tag__print_type_not_found(&pos->tag);
2654 continue;
2656 pos->tag.type = dtype->small_id;
2660 static void lexblock__recode_dwarf_types(struct lexblock *tag, struct cu *cu)
2662 struct tag *pos;
2663 struct dwarf_cu *dcu = cu->priv;
2665 list_for_each_entry(pos, &tag->tags, node) {
2666 struct dwarf_tag *dpos = tag__dwarf(pos);
2667 struct dwarf_tag *dtype;
2669 switch (pos->tag) {
2670 case DW_TAG_lexical_block:
2671 lexblock__recode_dwarf_types(tag__lexblock(pos), cu);
2672 continue;
2673 case DW_TAG_inlined_subroutine:
2674 if (dpos->type != 0)
2675 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, type);
2676 else
2677 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, abstract_origin);
2678 if (dtype == NULL) {
2679 if (dpos->type != 0)
2680 tag__print_type_not_found(pos);
2681 else
2682 tag__print_abstract_origin_not_found(pos);
2683 continue;
2685 ftype__recode_dwarf_types(dtag__tag(dtype), cu);
2686 continue;
2688 case DW_TAG_formal_parameter:
2689 if (dpos->type != 0)
2690 break;
2692 struct parameter *fp = tag__parameter(pos);
2693 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, abstract_origin);
2694 if (dtype == NULL) {
2695 tag__print_abstract_origin_not_found(pos);
2696 continue;
2698 fp->name = tag__parameter(dtag__tag(dtype))->name;
2699 pos->type = dtag__tag(dtype)->type;
2700 continue;
2702 case DW_TAG_variable:
2703 if (dpos->type != 0)
2704 break;
2706 struct variable *var = tag__variable(pos);
2708 if (dpos->abstract_origin == 0) {
2710 * DW_TAG_variable completely empty was
2711 * found on libQtGui.so.4.3.4.debug
2712 * <3><d6ea1>: Abbrev Number: 164 (DW_TAG_variable)
2714 continue;
2717 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, abstract_origin);
2718 if (dtype == NULL) {
2719 tag__print_abstract_origin_not_found(pos);
2720 continue;
2722 var->name = tag__variable(dtag__tag(dtype))->name;
2723 pos->type = dtag__tag(dtype)->type;
2724 continue;
2726 case DW_TAG_label: {
2727 struct label *l = tag__label(pos);
2729 if (dpos->abstract_origin == 0)
2730 continue;
2732 dtype = dwarf_cu__find_tag_by_ref(dcu, dpos, abstract_origin);
2733 if (dtype != NULL)
2734 l->name = tag__label(dtag__tag(dtype))->name;
2735 else
2736 tag__print_abstract_origin_not_found(pos);
2738 continue;
2741 dtype = dwarf_cu__find_type_by_ref(dcu, dpos, type);
2742 if (dtype == NULL) {
2743 tag__print_type_not_found(pos);
2744 continue;
2746 pos->type = dtype->small_id;
2750 static void dwarf_cu__recode_btf_type_tag_ptr(struct btf_type_tag_ptr_type *tag,
2751 uint32_t pointee_type)
2753 struct btf_type_tag_type *annot;
2754 struct dwarf_tag *annot_dtag;
2755 struct tag *prev_tag;
2757 /* Given source like
2758 * int tag1 tag2 tag3 *p;
2759 * the tag->tags contains tag3 -> tag2 -> tag1, the final type chain looks like:
2760 * pointer -> tag3 -> tag2 -> tag1 -> pointee
2762 * Basically it means
2763 * - '*' applies to "int tag1 tag2 tag3"
2764 * - tag3 applies to "int tag1 tag2"
2765 * - tag2 applies to "int tag1"
2766 * - tag1 applies to "int"
2768 * This also makes final source code (format c) easier as we can do
2769 * emit for "tag3 -> tag2 -> tag1 -> int"
2770 * emit '*'
2772 * For 'tag3 -> tag2 -> tag1 -> int":
2773 * emit for "tag2 -> tag1 -> int"
2774 * emit tag3
2776 * Eventually we can get the source code like
2777 * int tag1 tag2 tag3 *p;
2778 * and this matches the user/kernel code.
2780 prev_tag = &tag->tag;
2781 list_for_each_entry(annot, &tag->tags, node) {
2782 annot_dtag = tag__dwarf(&annot->tag);
2783 prev_tag->type = annot_dtag->small_id;
2784 prev_tag = &annot->tag;
2786 prev_tag->type = pointee_type;
2789 static int tag__recode_dwarf_type(struct tag *tag, struct cu *cu)
2791 struct dwarf_tag *dtag = tag__dwarf(tag);
2792 struct dwarf_tag *dtype;
2794 /* Check if this is an already recoded bitfield */
2795 if (dtag == NULL)
2796 return 0;
2798 if (tag__is_type(tag))
2799 type__recode_dwarf_specification(tag, cu);
2801 if (tag__has_namespace(tag))
2802 return namespace__recode_dwarf_types(tag, cu);
2804 switch (tag->tag) {
2805 case DW_TAG_subprogram: {
2806 struct function *fn = tag__function(tag);
2808 if (fn->name == 0) {
2809 if (dtag->abstract_origin == 0 &&
2810 dtag->specification == 0) {
2812 * Found on libQtGui.so.4.3.4.debug
2813 * <3><1423de>: Abbrev Number: 209 (DW_TAG_subprogram)
2814 * <1423e0> DW_AT_declaration : 1
2816 return 0;
2818 dtype = dwarf_cu__find_tag_by_ref(cu->priv, dtag, abstract_origin);
2819 if (dtype == NULL)
2820 dtype = dwarf_cu__find_tag_by_ref(cu->priv, dtag, specification);
2821 if (dtype != NULL)
2822 fn->name = tag__function(dtag__tag(dtype))->name;
2823 else {
2824 fprintf(stderr,
2825 "%s: couldn't find name for "
2826 "function %#llx, abstract_origin=%#llx,"
2827 " specification=%#llx\n", __func__,
2828 (unsigned long long)dtag->id,
2829 (unsigned long long)dtag->abstract_origin,
2830 (unsigned long long)dtag->specification);
2833 lexblock__recode_dwarf_types(&fn->lexblock, cu);
2835 /* Fall thru */
2837 case DW_TAG_subroutine_type:
2838 ftype__recode_dwarf_types(tag, cu);
2839 /* Fall thru, for the function return type */
2840 break;
2842 case DW_TAG_lexical_block:
2843 lexblock__recode_dwarf_types(tag__lexblock(tag), cu);
2844 return 0;
2846 case DW_TAG_ptr_to_member_type: {
2847 struct ptr_to_member_type *pt = tag__ptr_to_member_type(tag);
2849 dtype = dwarf_cu__find_type_by_ref(cu->priv, dtag, containing_type);
2850 if (dtype != NULL)
2851 pt->containing_type = dtype->small_id;
2852 else {
2853 fprintf(stderr,
2854 "%s: couldn't find type for "
2855 "containing_type %#llx, containing_type=%#llx\n",
2856 __func__,
2857 (unsigned long long)dtag->id,
2858 (unsigned long long)dtag->containing_type);
2861 break;
2863 case DW_TAG_namespace:
2864 return namespace__recode_dwarf_types(tag, cu);
2865 /* Damn, DW_TAG_inlined_subroutine is an special case
2866 as dwarf_tag->id is in fact an abtract origin, i.e. must be
2867 looked up in the tags_table, not in the types_table.
2868 The others also point to routines, so are in tags_table */
2869 case DW_TAG_inlined_subroutine:
2870 case DW_TAG_imported_module:
2871 dtype = dwarf_cu__find_tag_by_ref(cu->priv, dtag, type);
2872 goto check_type;
2873 /* Can be for both types and non types */
2874 case DW_TAG_imported_declaration:
2875 dtype = dwarf_cu__find_tag_by_ref(cu->priv, dtag, type);
2876 if (dtype != NULL)
2877 goto out;
2878 goto find_type;
2879 case DW_TAG_variable: {
2880 struct variable *var = tag__variable(tag);
2882 if (var->has_specification) {
2883 if (dtag->specification) {
2884 dtype = dwarf_cu__find_tag_by_ref(cu->priv, dtag, specification);
2885 if (dtype)
2886 var->spec = tag__variable(dtag__tag(dtype));
2893 if (dtag->type == 0) {
2894 if (tag->tag != DW_TAG_pointer_type || !tag->has_btf_type_tag)
2895 tag->type = 0; /* void */
2896 else
2897 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag), 0);
2898 return 0;
2901 find_type:
2902 dtype = dwarf_cu__find_type_by_ref(cu->priv, dtag, type);
2903 check_type:
2904 if (dtype == NULL) {
2905 tag__print_type_not_found(tag);
2906 return 0;
2908 out:
2909 if (tag->tag != DW_TAG_pointer_type || !tag->has_btf_type_tag)
2910 tag->type = dtype->small_id;
2911 else
2912 dwarf_cu__recode_btf_type_tag_ptr(tag__btf_type_tag_ptr(tag), dtype->small_id);
2914 return 0;
2917 static bool param__is_struct(struct cu *cu, struct tag *tag)
2919 struct tag *type = cu__type(cu, tag->type);
2921 if (!type)
2922 return false;
2924 switch (type->tag) {
2925 case DW_TAG_structure_type:
2926 return true;
2927 case DW_TAG_const_type:
2928 case DW_TAG_typedef:
2929 /* handle "typedef struct", const parameter */
2930 return param__is_struct(cu, type);
2931 default:
2932 return false;
2936 static int cu__resolve_func_ret_types_optimized(struct cu *cu)
2938 struct ptr_table *pt = &cu->functions_table;
2939 uint32_t i;
2941 for (i = 0; i < pt->nr_entries; ++i) {
2942 struct tag *tag = pt->entries[i];
2943 struct parameter *pos;
2944 struct function *fn = tag__function(tag);
2945 bool has_unexpected_reg = false, has_struct_param = false;
2947 /* mark function as optimized if parameter is, or
2948 * if parameter does not have a location; at this
2949 * point location presence has been marked in
2950 * abstract origins for cases where a parameter
2951 * location is not stored in the original function
2952 * parameter tag.
2954 * Also mark functions which, due to optimization,
2955 * use an unexpected register for a parameter.
2956 * Exception is functions which have a struct
2957 * as a parameter, as multiple registers may
2958 * be used to represent it, throwing off register
2959 * to parameter mapping.
2961 ftype__for_each_parameter(&fn->proto, pos) {
2962 if (pos->optimized || !pos->has_loc)
2963 fn->proto.optimized_parms = 1;
2965 if (pos->unexpected_reg)
2966 has_unexpected_reg = true;
2968 if (has_unexpected_reg) {
2969 ftype__for_each_parameter(&fn->proto, pos) {
2970 has_struct_param = param__is_struct(cu, &pos->tag);
2971 if (has_struct_param)
2972 break;
2974 if (!has_struct_param)
2975 fn->proto.unexpected_reg = 1;
2978 if (tag == NULL || tag->type != 0)
2979 continue;
2981 if (!fn->abstract_origin)
2982 continue;
2984 struct dwarf_tag *dtag = tag__dwarf(tag);
2985 struct dwarf_tag *dfunc;
2986 dfunc = dwarf_cu__find_tag_by_ref(cu->priv, dtag, abstract_origin);
2987 if (dfunc == NULL) {
2988 tag__print_abstract_origin_not_found(tag);
2989 return -1;
2992 tag->type = dtag__tag(dfunc)->type;
2994 return 0;
2997 static int cu__recode_dwarf_types_table(struct cu *cu,
2998 struct ptr_table *pt,
2999 uint32_t i)
3001 for (; i < pt->nr_entries; ++i) {
3002 struct tag *tag = pt->entries[i];
3004 if (tag != NULL) /* void, see cu__new */
3005 if (tag__recode_dwarf_type(tag, cu))
3006 return -1;
3009 return 0;
3012 static int cu__recode_dwarf_types(struct cu *cu)
3014 if (cu__recode_dwarf_types_table(cu, &cu->types_table, 1) ||
3015 cu__recode_dwarf_types_table(cu, &cu->tags_table, 0) ||
3016 cu__recode_dwarf_types_table(cu, &cu->functions_table, 0))
3017 return -1;
3018 return 0;
3021 static const char *dwarf_tag__decl_file(const struct tag *tag,
3022 const struct cu *cu)
3024 struct dwarf_tag *dtag = tag__dwarf(tag);
3025 return cu->extra_dbg_info ? dtag->decl_file : NULL;
3028 static uint32_t dwarf_tag__decl_line(const struct tag *tag,
3029 const struct cu *cu)
3031 struct dwarf_tag *dtag = tag__dwarf(tag);
3032 return cu->extra_dbg_info ? dtag->decl_line : 0;
3035 static unsigned long long dwarf_tag__orig_id(const struct tag *tag,
3036 const struct cu *cu)
3038 struct dwarf_tag *dtag = tag__dwarf(tag);
3039 return cu->extra_dbg_info ? dtag->id : 0;
3042 struct debug_fmt_ops dwarf__ops;
3044 static int die__process(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
3046 Dwarf_Die child;
3047 const uint16_t tag = dwarf_tag(die);
3049 if (tag == DW_TAG_skeleton_unit) {
3050 static bool warned;
3052 if (!warned) {
3053 fprintf(stderr, "WARNING: DW_TAG_skeleton_unit used, please look for a .dwo file and use it instead.\n"
3054 " A future version of pahole will support do this automagically.\n");
3055 warned = true;
3057 return 0; // so that other units can be processed
3060 if (tag == DW_TAG_partial_unit) {
3061 static bool warned;
3063 if (!warned) {
3064 fprintf(stderr, "WARNING: DW_TAG_partial_unit used, some types will not be considered!\n"
3065 " Probably this was optimized using a tool like 'dwz'\n"
3066 " A future version of pahole will support this.\n");
3067 warned = true;
3069 return 0; // so that other units can be processed
3072 if (tag != DW_TAG_compile_unit && tag != DW_TAG_type_unit) {
3073 fprintf(stderr, "%s: DW_TAG_compile_unit, DW_TAG_type_unit, DW_TAG_partial_unit or DW_TAG_skeleton_unit expected got %s (0x%x) @ %llx!\n",
3074 __FUNCTION__, dwarf_tag_name(tag), tag, (unsigned long long)dwarf_dieoffset(die));
3075 return -EINVAL;
3078 cu->language = attr_numeric(die, DW_AT_language);
3080 if (conf->early_cu_filter)
3081 cu = conf->early_cu_filter(cu);
3084 * If we filtered this CU out, we still want to keep iterating, but
3085 * there's no need to walk the rest of the CU info.
3087 if (cu == NULL)
3088 return DWARF_CB_OK;
3090 if (dwarf_child(die, &child) == 0) {
3091 int err = die__process_unit(&child, cu, conf);
3092 if (err)
3093 return err;
3096 if (dwarf_siblingof(die, die) == 0)
3097 fprintf(stderr, "%s: got %s unexpected tag after "
3098 "DW_TAG_compile_unit!\n",
3099 __FUNCTION__, dwarf_tag_name(tag));
3101 return 0;
3104 static int die__process_and_recode(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
3106 int ret = die__process(die, cu, conf);
3107 if (ret != 0)
3108 return ret;
3109 ret = cu__recode_dwarf_types(cu);
3110 if (ret != 0)
3111 return ret;
3113 return cu__resolve_func_ret_types_optimized(cu);
3116 static int class_member__cache_byte_size(struct tag *tag, struct cu *cu,
3117 void *cookie)
3119 struct class_member *member = tag__class_member(tag);
3120 struct conf_load *conf_load = cookie;
3122 if (tag__is_class_member(tag)) {
3123 if (member->is_static)
3124 return 0;
3125 } else if (tag->tag != DW_TAG_inheritance) {
3126 return 0;
3129 if (member->bitfield_size == 0) {
3130 member->byte_size = tag__size(tag, cu);
3131 member->bit_size = member->byte_size * 8;
3132 return 0;
3136 * Try to figure out byte size, if it's not directly provided in DWARF
3138 if (member->byte_size == 0) {
3139 struct tag *type = tag__strip_typedefs_and_modifiers(&member->tag, cu);
3140 member->byte_size = tag__size(type, cu);
3141 if (member->byte_size == 0) {
3142 int bit_size;
3143 if (tag__is_enumeration(type)) {
3144 bit_size = tag__type(type)->size;
3145 } else {
3146 struct base_type *bt = tag__base_type(type);
3147 bit_size = bt->bit_size ? bt->bit_size : base_type__name_to_size(bt, cu);
3149 member->byte_size = (bit_size + 7) / 8 * 8;
3152 member->bit_size = member->byte_size * 8;
3155 * XXX: after all the attempts to determine byte size, we might still
3156 * be unsuccessful, because base_type__name_to_size doesn't know about
3157 * the base_type name, so one has to add there when such base_type
3158 * isn't found. pahole will put zero on the struct output so it should
3159 * be easy to spot the name when such unlikely thing happens.
3161 if (member->byte_size == 0) {
3162 member->bitfield_offset = 0;
3163 return 0;
3166 if (!member->has_bit_offset) {
3168 * For little-endian architectures, DWARF data emitted by gcc/clang
3169 * specifies bitfield offset as an offset from the highest-order bit
3170 * of an underlying integral type (e.g., int) to a highest-order bit
3171 * of a bitfield. E.g., for bitfield taking first 5 bits of int-backed
3172 * bitfield, bit offset will be 27 (sizeof(int) - 0 offset - 5 bit
3173 * size), which is very counter-intuitive and isn't a natural
3174 * extension of byte offset, which on little-endian points to
3175 * lowest-order byte. So here we re-adjust bitfield offset to be an
3176 * offset from lowest-order bit of underlying integral type to
3177 * a lowest-order bit of a bitfield. This makes bitfield offset
3178 * a natural extension of byte offset for bitfields and is uniform
3179 * with how big-endian bit offsets work.
3181 if (cu->little_endian)
3182 member->bitfield_offset = member->bit_size - member->bitfield_offset - member->bitfield_size;
3184 member->bit_offset = member->byte_offset * 8 + member->bitfield_offset;
3185 } else {
3186 // DWARF5 has DW_AT_data_bit_offset, offset in bits from the
3187 // start of the container type (struct, class, etc).
3188 member->byte_offset = member->bit_offset / 8;
3189 member->bitfield_offset = member->bit_offset - member->byte_offset * 8;
3192 /* make sure bitfield offset is non-negative */
3193 if (member->bitfield_offset < 0) {
3194 member->bitfield_offset += member->bit_size;
3195 member->byte_offset -= member->byte_size;
3196 member->bit_offset = member->byte_offset * 8 + member->bitfield_offset;
3198 /* align on underlying base type natural alignment boundary */
3199 member->bitfield_offset += (member->byte_offset % member->byte_size) * 8;
3200 member->byte_offset = member->bit_offset / member->bit_size * member->bit_size / 8;
3201 if (member->bitfield_offset >= member->bit_size) {
3202 member->bitfield_offset -= member->bit_size;
3203 member->byte_offset += member->byte_size;
3206 if (conf_load && conf_load->fixup_silly_bitfields &&
3207 member->byte_size == 8 * member->bitfield_size) {
3208 member->bitfield_size = 0;
3209 member->bitfield_offset = 0;
3212 return 0;
3215 static bool cu__language_reorders_offsets(const struct cu *cu)
3217 return cu->language == DW_LANG_Rust;
3220 static int type__sort_by_offset(struct tag *tag, struct cu *cu, void *cookie __maybe_unused)
3222 if (!tag__is_type(tag))
3223 return 0;
3225 struct type *type = tag__type(tag);
3226 struct class_member *current_member;
3228 // There may be more than DW_TAG_members entries in the type tags, so do a simple
3229 // bubble sort for now, so that the other non tags stay where they are.
3230 restart:
3231 type__for_each_data_member(type, current_member) {
3232 if (list_is_last(&current_member->tag.node, &type->namespace.tags))
3233 break;
3235 struct class_member *next_member = list_entry(current_member->tag.node.next, typeof(*current_member), tag.node);
3237 if (current_member->byte_offset <= next_member->byte_offset)
3238 continue;
3240 list_del(&current_member->tag.node);
3241 list_add(&current_member->tag.node, &next_member->tag.node);
3242 goto restart;
3245 return 0;
3248 static void cu__sort_types_by_offset(struct cu *cu, struct conf_load *conf)
3250 cu__for_all_tags(cu, type__sort_by_offset, conf);
3253 static int cu__finalize(struct cu *cu, struct cus *cus, struct conf_load *conf, void *thr_data)
3255 cu__for_all_tags(cu, class_member__cache_byte_size, conf);
3257 if (cu__language_reorders_offsets(cu))
3258 cu__sort_types_by_offset(cu, conf);
3260 cus__set_cu_state(cus, cu, CU__LOADED);
3262 if (conf && conf->steal) {
3263 return conf->steal(cu, conf, thr_data);
3265 return LSK__KEEPIT;
3268 static int cus__finalize(struct cus *cus, struct cu *cu, struct conf_load *conf, void *thr_data)
3270 int lsk = cu__finalize(cu, cus, conf, thr_data);
3271 switch (lsk) {
3272 case LSK__DELETE:
3273 cus__remove(cus, cu);
3274 cu__delete(cu);
3275 break;
3276 case LSK__STOP_LOADING:
3277 break;
3278 case LSK__KEEPIT:
3279 break;
3281 return lsk;
3284 static int cu__set_common(struct cu *cu, struct conf_load *conf,
3285 Dwfl_Module *mod, Elf *elf)
3287 cu->uses_global_strings = true;
3288 cu->elf = elf;
3289 cu->dwfl = mod;
3290 cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0;
3291 cu->has_addr_info = conf ? conf->get_addr_info : 0;
3293 GElf_Ehdr ehdr;
3294 if (gelf_getehdr(elf, &ehdr) == NULL)
3295 return DWARF_CB_ABORT;
3297 cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB;
3298 cu->nr_register_params = arch__nr_register_params(&ehdr);
3299 arch__set_register_params(&ehdr, cu);
3300 return 0;
3303 static int __cus__load_debug_types(struct cus *cus, struct conf_load *conf, Dwfl_Module *mod, Dwarf *dw, Elf *elf,
3304 const char *filename, const unsigned char *build_id,
3305 int build_id_len, struct cu **cup, struct dwarf_cu *dcup)
3307 Dwarf_Off off = 0, noff, type_off;
3308 size_t cuhl;
3309 uint8_t pointer_size, offset_size;
3310 uint64_t signature;
3312 *cup = NULL;
3314 while (dwarf_next_unit(dw, off, &noff, &cuhl, NULL, NULL, &pointer_size,
3315 &offset_size, &signature, &type_off)
3316 == 0) {
3318 if (*cup == NULL) {
3319 struct cu *cu;
3321 cu = cu__new("", pointer_size, build_id,
3322 build_id_len, filename, conf->use_obstack);
3323 if (cu == NULL ||
3324 cu__set_common(cu, conf, mod, elf) != 0) {
3325 cu__delete(cu);
3326 return DWARF_CB_ABORT;
3329 if (dwarf_cu__init(dcup, cu) != 0) {
3330 cu__delete(cu);
3331 return DWARF_CB_ABORT;
3333 dcup->cu = cu;
3334 /* Funny hack. */
3335 dcup->type_unit = dcup;
3336 cu->priv = dcup;
3337 cu->dfops = &dwarf__ops;
3339 *cup = cu;
3340 cus__add(cus, cu);
3343 Dwarf_Die die_mem;
3344 Dwarf_Die *cu_die = dwarf_offdie_types(dw, off + cuhl,
3345 &die_mem);
3347 if (die__process(cu_die, *cup, conf) != 0)
3348 return DWARF_CB_ABORT;
3350 off = noff;
3353 if (*cup != NULL && cu__recode_dwarf_types(*cup) != 0)
3354 return DWARF_CB_ABORT;
3356 return 0;
3359 /* Match the define in linux:include/linux/elfnote-lto.h */
3360 #define LINUX_ELFNOTE_LTO_INFO 0x101
3362 static bool cus__merging_cu(Dwarf *dw, Elf *elf)
3364 Elf_Scn *section = NULL;
3365 while ((section = elf_nextscn(elf, section)) != 0) {
3366 GElf_Shdr header;
3367 if (!gelf_getshdr(section, &header))
3368 continue;
3370 if (header.sh_type != SHT_NOTE)
3371 continue;
3373 Elf_Data *data = NULL;
3374 while ((data = elf_getdata(section, data)) != 0) {
3375 size_t name_off, desc_off, offset = 0;
3376 GElf_Nhdr hdr;
3377 while ((offset = gelf_getnote(data, offset, &hdr, &name_off, &desc_off)) != 0) {
3378 if (hdr.n_type != LINUX_ELFNOTE_LTO_INFO)
3379 continue;
3381 /* owner is Linux */
3382 if (strcmp((char *)data->d_buf + name_off, "Linux") != 0)
3383 continue;
3385 return *(int *)(data->d_buf + desc_off) != 0;
3390 Dwarf_Off off = 0, noff;
3391 size_t cuhl;
3393 while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) {
3394 Dwarf_Die die_mem;
3395 Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem);
3397 if (cu_die == NULL)
3398 break;
3400 Dwarf_Off offset = 0;
3401 while (true) {
3402 size_t length;
3403 Dwarf_Abbrev *abbrev = dwarf_getabbrev (cu_die, offset, &length);
3404 if (abbrev == NULL || abbrev == DWARF_END_ABBREV)
3405 break;
3407 size_t attrcnt;
3408 if (dwarf_getattrcnt (abbrev, &attrcnt) != 0)
3409 return false;
3411 unsigned int attr_num, attr_form;
3412 Dwarf_Off aboffset;
3413 size_t j;
3414 for (j = 0; j < attrcnt; ++j) {
3415 if (dwarf_getabbrevattr (abbrev, j, &attr_num, &attr_form,
3416 &aboffset))
3417 return false;
3418 if (attr_form == DW_FORM_ref_addr)
3419 return true;
3422 offset += length;
3425 off = noff;
3428 return false;
3431 struct dwarf_cus {
3432 struct cus *cus;
3433 struct conf_load *conf;
3434 Dwfl_Module *mod;
3435 Dwarf *dw;
3436 Elf *elf;
3437 const char *filename;
3438 Dwarf_Off off;
3439 const unsigned char *build_id;
3440 int build_id_len;
3441 int error;
3442 struct dwarf_cu *type_dcu;
3445 struct dwarf_thread {
3446 struct dwarf_cus *dcus;
3447 void *data;
3450 static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die *cu_die, uint8_t pointer_size)
3453 * DW_AT_name in DW_TAG_compile_unit can be NULL, first seen in:
3455 * /usr/libexec/gcc/x86_64-redhat-linux/4.3.2/ecj1.debug
3457 const char *name = attr_string(cu_die, DW_AT_name, dcus->conf);
3458 struct cu *cu = cu__new(name ?: "", pointer_size, dcus->build_id, dcus->build_id_len, dcus->filename, dcus->conf->use_obstack);
3459 if (cu == NULL || cu__set_common(cu, dcus->conf, dcus->mod, dcus->elf) != 0) {
3460 cu__delete(cu);
3461 return NULL;
3464 struct dwarf_cu *dcu = dwarf_cu__new(cu);
3466 if (dcu == NULL) {
3467 cu__delete(cu);
3468 return NULL;
3471 dcu->type_unit = dcus->type_dcu;
3472 cu->priv = dcu;
3473 cu->dfops = &dwarf__ops;
3475 return dcu;
3478 static int dwarf_cus__process_cu(struct dwarf_cus *dcus, Dwarf_Die *cu_die,
3479 struct cu *cu, void *thr_data)
3481 if (die__process_and_recode(cu_die, cu, dcus->conf) != 0 ||
3482 cus__finalize(dcus->cus, cu, dcus->conf, thr_data) == LSK__STOP_LOADING)
3483 return DWARF_CB_ABORT;
3485 return DWARF_CB_OK;
3488 static int dwarf_cus__create_and_process_cu(struct dwarf_cus *dcus, Dwarf_Die *cu_die, uint8_t pointer_size)
3490 struct dwarf_cu *dcu = dwarf_cus__create_cu(dcus, cu_die, pointer_size);
3492 if (dcu == NULL)
3493 return DWARF_CB_ABORT;
3495 cus__add(dcus->cus, dcu->cu);
3497 return dwarf_cus__process_cu(dcus, cu_die, dcu->cu, NULL);
3500 static int dwarf_cus__nextcu(struct dwarf_cus *dcus, struct dwarf_cu **dcu,
3501 Dwarf_Die *die_mem, Dwarf_Die **cu_die,
3502 uint8_t *pointer_size, uint8_t *offset_size)
3504 Dwarf_Off noff;
3505 size_t cuhl;
3506 int ret;
3508 cus__lock(dcus->cus);
3510 if (dcus->error) {
3511 ret = dcus->error;
3512 goto out_unlock;
3515 ret = dwarf_nextcu(dcus->dw, dcus->off, &noff, &cuhl, NULL, pointer_size, offset_size);
3516 if (ret == 0) {
3517 *cu_die = dwarf_offdie(dcus->dw, dcus->off + cuhl, die_mem);
3518 if (*cu_die != NULL)
3519 dcus->off = noff;
3522 if (ret == 0 && *cu_die != NULL) {
3523 *dcu = dwarf_cus__create_cu(dcus, *cu_die, *pointer_size);
3524 if (*dcu == NULL) {
3525 dcus->error = ENOMEM;
3526 ret = -1;
3527 goto out_unlock;
3529 // Do it here to keep all CUs in cus->cus in the same
3530 // order as in the DWARF file being loaded (e.g. vmlinux)
3531 __cus__add(dcus->cus, (*dcu)->cu);
3534 out_unlock:
3535 cus__unlock(dcus->cus);
3537 return ret;
3540 static void *dwarf_cus__process_cu_thread(void *arg)
3542 struct dwarf_thread *dthr = arg;
3543 struct dwarf_cus *dcus = dthr->dcus;
3544 uint8_t pointer_size, offset_size;
3545 Dwarf_Die die_mem, *cu_die;
3546 struct dwarf_cu *dcu;
3548 while (dwarf_cus__nextcu(dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size) == 0) {
3549 if (cu_die == NULL)
3550 break;
3552 if (dwarf_cus__process_cu(dcus, cu_die, dcu->cu, dthr->data) == DWARF_CB_ABORT)
3553 goto out_abort;
3556 if (dcus->conf->thread_exit &&
3557 dcus->conf->thread_exit(dcus->conf, dthr->data) != 0)
3558 goto out_abort;
3560 return (void *)DWARF_CB_OK;
3561 out_abort:
3562 return (void *)DWARF_CB_ABORT;
3565 static int dwarf_cus__threaded_process_cus(struct dwarf_cus *dcus)
3567 pthread_t threads[dcus->conf->nr_jobs];
3568 struct dwarf_thread dthr[dcus->conf->nr_jobs];
3569 void *thread_data[dcus->conf->nr_jobs];
3570 int res;
3571 int i;
3573 if (dcus->conf->threads_prepare) {
3574 res = dcus->conf->threads_prepare(dcus->conf, dcus->conf->nr_jobs, thread_data);
3575 if (res != 0)
3576 return res;
3577 } else {
3578 memset(thread_data, 0, sizeof(void *) * dcus->conf->nr_jobs);
3581 for (i = 0; i < dcus->conf->nr_jobs; ++i) {
3582 dthr[i].dcus = dcus;
3583 dthr[i].data = thread_data[i];
3585 dcus->error = pthread_create(&threads[i], NULL,
3586 dwarf_cus__process_cu_thread,
3587 &dthr[i]);
3588 if (dcus->error)
3589 goto out_join;
3592 dcus->error = 0;
3594 out_join:
3595 while (--i >= 0) {
3596 void *res;
3597 int err = pthread_join(threads[i], &res);
3599 if (err == 0 && res != NULL)
3600 dcus->error = (long)res;
3603 if (dcus->conf->threads_collect) {
3604 res = dcus->conf->threads_collect(dcus->conf, dcus->conf->nr_jobs,
3605 thread_data, dcus->error);
3606 if (dcus->error == 0)
3607 dcus->error = res;
3610 return dcus->error;
3613 static int __dwarf_cus__process_cus(struct dwarf_cus *dcus)
3615 uint8_t pointer_size, offset_size;
3616 Dwarf_Off noff;
3617 size_t cuhl;
3619 while (dwarf_nextcu(dcus->dw, dcus->off, &noff, &cuhl, NULL, &pointer_size, &offset_size) == 0) {
3620 Dwarf_Die die_mem;
3621 Dwarf_Die *cu_die = dwarf_offdie(dcus->dw, dcus->off + cuhl, &die_mem);
3623 if (cu_die == NULL)
3624 break;
3626 if (dwarf_cus__create_and_process_cu(dcus, cu_die, pointer_size) == DWARF_CB_ABORT)
3627 return DWARF_CB_ABORT;
3629 dcus->off = noff;
3632 return 0;
3635 static int dwarf_cus__process_cus(struct dwarf_cus *dcus)
3637 if (dcus->conf->nr_jobs > 1)
3638 return dwarf_cus__threaded_process_cus(dcus);
3640 return __dwarf_cus__process_cus(dcus);
3643 static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
3644 Dwfl_Module *mod, Dwarf *dw, Elf *elf,
3645 const char *filename,
3646 const unsigned char *build_id,
3647 int build_id_len,
3648 struct dwarf_cu *type_dcu)
3650 uint8_t pointer_size, offset_size;
3651 struct dwarf_cu *dcu = NULL;
3652 Dwarf_Off off = 0, noff;
3653 struct cu *cu = NULL;
3654 size_t cuhl;
3656 while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size,
3657 &offset_size) == 0) {
3658 Dwarf_Die die_mem;
3659 Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem);
3661 if (cu_die == NULL)
3662 break;
3664 if (cu == NULL) {
3665 cu = cu__new("", pointer_size, build_id, build_id_len,
3666 filename, conf->use_obstack);
3667 if (cu == NULL || cu__set_common(cu, conf, mod, elf) != 0)
3668 goto out_abort;
3670 dcu = zalloc(sizeof(*dcu));
3671 if (dcu == NULL)
3672 goto out_abort;
3674 /* Merged cu tends to need a lot more memory.
3675 * Let us start with max_hashtags__bits and
3676 * go down to find a proper hashtag bit value.
3678 uint32_t default_hbits = hashtags__bits;
3679 for (hashtags__bits = max_hashtags__bits;
3680 hashtags__bits >= default_hbits;
3681 hashtags__bits--) {
3682 if (dwarf_cu__init(dcu, cu) == 0)
3683 break;
3685 if (hashtags__bits < default_hbits)
3686 goto out_abort;
3688 dcu->cu = cu;
3689 dcu->type_unit = type_dcu;
3690 cu->priv = dcu;
3691 cu->dfops = &dwarf__ops;
3692 cu->language = attr_numeric(cu_die, DW_AT_language);
3693 cus__add(cus, cu);
3696 Dwarf_Die child;
3697 if (dwarf_child(cu_die, &child) == 0) {
3698 bool filtered = false;
3700 if (conf->early_cu_filter) {
3701 struct cu unmerged_cu = {
3702 .name = attr_string(cu_die, DW_AT_name, conf),
3703 .language = attr_numeric(cu_die, DW_AT_language),
3706 filtered = conf->early_cu_filter(&unmerged_cu) == NULL;
3709 if (!filtered && die__process_unit(&child, cu, conf) != 0)
3710 goto out_abort;
3713 off = noff;
3716 if (cu == NULL)
3717 return 0;
3719 /* process merged cu */
3720 if (cu__recode_dwarf_types(cu) != LSK__KEEPIT)
3721 goto out_abort;
3724 * for lto build, the function return type may not be
3725 * resolved due to the return type of a subprogram is
3726 * encoded in another subprogram through abstract_origin
3727 * tag. Let us visit all subprograms again to resolve this.
3729 if (cu__resolve_func_ret_types_optimized(cu) != LSK__KEEPIT)
3730 goto out_abort;
3732 if (cus__finalize(cus, cu, conf, NULL) == LSK__STOP_LOADING)
3733 goto out_abort;
3735 return 0;
3737 out_abort:
3738 dwarf_cu__delete(cu);
3739 cu__delete(cu);
3740 return DWARF_CB_ABORT;
3743 static int cus__load_module(struct cus *cus, struct conf_load *conf,
3744 Dwfl_Module *mod, Dwarf *dw, Elf *elf,
3745 const char *filename)
3747 const unsigned char *build_id = NULL;
3748 #ifdef HAVE_DWFL_MODULE_BUILD_ID
3749 GElf_Addr vaddr;
3750 int build_id_len = dwfl_module_build_id(mod, &build_id, &vaddr);
3751 #else
3752 int build_id_len = 0;
3753 #endif
3754 struct cu *type_cu;
3755 struct dwarf_cu type_dcu;
3756 int type_lsk = LSK__KEEPIT;
3758 int res = __cus__load_debug_types(cus, conf, mod, dw, elf, filename, build_id, build_id_len, &type_cu, &type_dcu);
3759 if (res != 0) {
3760 return res;
3763 if (type_cu != NULL) {
3764 type_lsk = cu__finalize(type_cu, cus, conf, NULL);
3765 if (type_lsk == LSK__DELETE) {
3766 cus__remove(cus, type_cu);
3770 if (cus__merging_cu(dw, elf)) {
3771 res = cus__merge_and_process_cu(cus, conf, mod, dw, elf, filename,
3772 build_id, build_id_len,
3773 type_cu ? &type_dcu : NULL);
3774 } else {
3775 struct dwarf_cus dcus = {
3776 .off = 0,
3777 .cus = cus,
3778 .conf = conf,
3779 .mod = mod,
3780 .dw = dw,
3781 .elf = elf,
3782 .filename = filename,
3783 .type_dcu = type_cu ? &type_dcu : NULL,
3784 .build_id = build_id,
3785 .build_id_len = build_id_len,
3787 res = dwarf_cus__process_cus(&dcus);
3790 if (res)
3791 return res;
3793 if (type_lsk == LSK__DELETE)
3794 cu__delete(type_cu);
3796 return DWARF_CB_OK;
3799 struct process_dwflmod_parms {
3800 struct cus *cus;
3801 struct conf_load *conf;
3802 const char *filename;
3803 uint32_t nr_dwarf_sections_found;
3806 static int cus__process_dwflmod(Dwfl_Module *dwflmod,
3807 void **userdata __maybe_unused,
3808 const char *name __maybe_unused,
3809 Dwarf_Addr base __maybe_unused,
3810 void *arg)
3812 struct process_dwflmod_parms *parms = arg;
3813 struct cus *cus = parms->cus;
3815 GElf_Addr dwflbias;
3817 * Does the relocation and saves the elf for later processing
3818 * by the stealer, such as pahole_stealer, so that it don't
3819 * have to create another Elf instance just to do things like
3820 * reading this ELF file symtab to do CTF encoding of the
3821 * DW_TAG_suprogram tags (functions).
3823 Elf *elf = dwfl_module_getelf(dwflmod, &dwflbias);
3825 Dwarf_Addr dwbias;
3826 Dwarf *dw = dwfl_module_getdwarf(dwflmod, &dwbias);
3828 int err = DWARF_CB_OK;
3829 if (dw != NULL) {
3830 ++parms->nr_dwarf_sections_found;
3831 err = cus__load_module(cus, parms->conf, dwflmod, dw, elf,
3832 parms->filename);
3835 * XXX We will fall back to try finding other debugging
3836 * formats (CTF), so no point in telling this to the user
3837 * Use for debugging.
3838 * else
3839 * fprintf(stderr,
3840 * "%s: can't get debug context descriptor: %s\n",
3841 * __func__, dwfl_errmsg(-1));
3844 return err;
3847 static void dwarf_loader__exit(struct cus *cus)
3849 Dwfl *dwfl = cus__priv(cus);
3851 if (dwfl) {
3852 dwfl_end(dwfl);
3853 cus__set_priv(cus, NULL);
3857 static int cus__process_file(struct cus *cus, struct conf_load *conf, int fd,
3858 const char *filename)
3860 /* Duplicate an fd for dwfl_report_offline to swallow. */
3861 int dwfl_fd = dup(fd);
3863 if (dwfl_fd < 0)
3864 return -1;
3867 * Use libdwfl in a trivial way to open the libdw handle for us.
3868 * This takes care of applying relocations to DWARF data in ET_REL
3869 * files.
3872 static const Dwfl_Callbacks callbacks = {
3873 .section_address = dwfl_offline_section_address,
3874 .find_debuginfo = dwfl_standard_find_debuginfo,
3875 /* We use this table for core files too. */
3876 .find_elf = dwfl_build_id_find_elf,
3879 Dwfl *dwfl = dwfl_begin(&callbacks);
3881 cus__set_priv(cus, dwfl);
3882 cus__set_loader_exit(cus, dwarf_loader__exit);
3884 if (dwfl_report_offline(dwfl, filename, filename, dwfl_fd) == NULL)
3885 return -1;
3887 dwfl_report_end(dwfl, NULL, NULL);
3889 struct process_dwflmod_parms parms = {
3890 .cus = cus,
3891 .conf = conf,
3892 .filename = filename,
3893 .nr_dwarf_sections_found = 0,
3896 /* Process the one or more modules gleaned from this file. */
3897 int err = dwfl_getmodules(dwfl, cus__process_dwflmod, &parms, 0);
3898 if (err < 0)
3899 return -1;
3901 // We can't call dwfl_end(dwfl) here, as we keep pointers to strings
3902 // allocated by libdw that will be freed at dwfl_end(), so leave this for
3903 // cus__delete().
3904 return parms.nr_dwarf_sections_found ? 0 : -1;
3907 static int dwarf__load_file(struct cus *cus, struct conf_load *conf,
3908 const char *filename)
3910 int fd, err;
3912 if (conf->max_hashtable_bits != 0) {
3913 if (conf->max_hashtable_bits > 31)
3914 return -E2BIG;
3916 max_hashtags__bits = conf->max_hashtable_bits;
3919 if (conf->hashtable_bits != 0) {
3920 if (conf->hashtable_bits > max_hashtags__bits)
3921 return -E2BIG;
3923 hashtags__bits = conf->hashtable_bits;
3924 } else if (hashtags__bits > max_hashtags__bits)
3925 return -EINVAL;
3927 elf_version(EV_CURRENT);
3929 fd = open(filename, O_RDONLY);
3931 if (fd == -1)
3932 return -1;
3934 err = cus__process_file(cus, conf, fd, filename);
3935 close(fd);
3937 return err;
3940 struct debug_fmt_ops dwarf__ops = {
3941 .name = "dwarf",
3942 .load_file = dwarf__load_file,
3943 .tag__decl_file = dwarf_tag__decl_file,
3944 .tag__decl_line = dwarf_tag__decl_line,
3945 .tag__orig_id = dwarf_tag__orig_id,
3946 .cu__delete = dwarf_cu__delete,
3947 .tag__alloc = tag__alloc,
3948 .tag__free = tag__free,
3949 .has_alignment_info = true,