1 /*
2 SPDX-License-Identifier: GPL-2.0-only
3
4 Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>
5 */
6
7 #include <assert.h>
8 #include <dirent.h>
9 #include <dwarf.h>
10 #include <elfutils/libdwfl.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <fnmatch.h>
14 #include <libelf.h>
15 #include <obstack.h>
16 #include <search.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include "config.h"
23 #include "list.h"
24 #include "dwarves.h"
25 #include "dutil.h"
26 #include "strings.h"
27 #include "hash.h"
28
29 #define fprintf
30
31 struct strings *strings;
32
33 #ifndef DW_AT_GNU_vector
34 #define DW_AT_GNU_vector 0x2107
35 #endif
36
37 #ifndef DW_TAG_GNU_call_site
38 #define DW_TAG_GNU_call_site 0x4109
39 #define DW_TAG_GNU_call_site_parameter 0x410a
40 #endif
41
42 #define hashtags__fn(key) hash_64(key, HASHTAGS__BITS)
43
44 bool no_bitfield_type_recode = true;
45
__tag__print_not_supported(uint32_t tag,const char * func)46 static void __tag__print_not_supported(uint32_t tag, const char *func)
47 {
48 static bool dwarf_tags_warned[DW_TAG_GNU_call_site_parameter + 64];
49
50 if (tag < sizeof(dwarf_tags_warned)) {
51 if (dwarf_tags_warned[tag])
52 return;
53 dwarf_tags_warned[tag] = true;
54 }
55
56 fprintf(stderr, "%s: tag not supported %#x (%s)!\n", func,
57 tag, dwarf_tag_name(tag));
58 }
59
60 #define tag__print_not_supported(tag) \
61 __tag__print_not_supported(tag, __func__)
62
63 struct dwarf_off_ref {
64 unsigned int from_types : 1;
65 Dwarf_Off off;
66 };
67
68 typedef struct dwarf_off_ref dwarf_off_ref;
69
70 struct dwarf_tag {
71 struct hlist_node hash_node;
72 dwarf_off_ref type;
73 Dwarf_Off id;
74 union {
75 dwarf_off_ref abstract_origin;
76 dwarf_off_ref containing_type;
77 };
78 struct tag *tag;
79 uint32_t small_id;
80 strings_t decl_file;
81 uint16_t decl_line;
82 };
83
dwarf_tag__spec(struct dwarf_tag * dtag)84 static dwarf_off_ref dwarf_tag__spec(struct dwarf_tag *dtag)
85 {
86 return *(dwarf_off_ref *)(dtag + 1);
87 }
88
dwarf_tag__set_spec(struct dwarf_tag * dtag,dwarf_off_ref spec)89 static void dwarf_tag__set_spec(struct dwarf_tag *dtag, dwarf_off_ref spec)
90 {
91 *(dwarf_off_ref *)(dtag + 1) = spec;
92 }
93
94 #define HASHTAGS__BITS 8
95 #define HASHTAGS__SIZE (1UL << HASHTAGS__BITS)
96
97 #define obstack_chunk_alloc malloc
98 #define obstack_chunk_free free
99
obstack_zalloc(struct obstack * obstack,size_t size)100 static void *obstack_zalloc(struct obstack *obstack, size_t size)
101 {
102 void *o = obstack_alloc(obstack, size);
103
104 if (o)
105 memset(o, 0, size);
106 return o;
107 }
108
109 struct dwarf_cu {
110 struct hlist_head hash_tags[HASHTAGS__SIZE];
111 struct hlist_head hash_types[HASHTAGS__SIZE];
112 struct obstack obstack;
113 struct cu *cu;
114 struct dwarf_cu *type_unit;
115 };
116
dwarf_cu__init(struct dwarf_cu * dcu)117 static void dwarf_cu__init(struct dwarf_cu *dcu)
118 {
119 unsigned int i;
120 for (i = 0; i < HASHTAGS__SIZE; ++i) {
121 INIT_HLIST_HEAD(&dcu->hash_tags[i]);
122 INIT_HLIST_HEAD(&dcu->hash_types[i]);
123 }
124 obstack_init(&dcu->obstack);
125 dcu->type_unit = NULL;
126 }
127
hashtags__hash(struct hlist_head * hashtable,struct dwarf_tag * dtag)128 static void hashtags__hash(struct hlist_head *hashtable,
129 struct dwarf_tag *dtag)
130 {
131 struct hlist_head *head = hashtable + hashtags__fn(dtag->id);
132 hlist_add_head(&dtag->hash_node, head);
133 }
134
hashtags__find(const struct hlist_head * hashtable,const Dwarf_Off id)135 static struct dwarf_tag *hashtags__find(const struct hlist_head *hashtable,
136 const Dwarf_Off id)
137 {
138 if (id == 0)
139 return NULL;
140
141 struct dwarf_tag *tpos;
142 struct hlist_node *pos;
143 uint16_t bucket = hashtags__fn(id);
144 const struct hlist_head *head = hashtable + bucket;
145
146 hlist_for_each_entry(tpos, pos, head, hash_node) {
147 if (tpos->id == id)
148 return tpos;
149 }
150
151 return NULL;
152 }
153
cu__hash(struct cu * cu,struct tag * tag)154 static void cu__hash(struct cu *cu, struct tag *tag)
155 {
156 struct dwarf_cu *dcu = cu->priv;
157 struct hlist_head *hashtable = tag__is_tag_type(tag) ?
158 dcu->hash_types :
159 dcu->hash_tags;
160 hashtags__hash(hashtable, tag->priv);
161 }
162
dwarf_cu__find_tag_by_ref(const struct dwarf_cu * cu,const struct dwarf_off_ref * ref)163 static struct dwarf_tag *dwarf_cu__find_tag_by_ref(const struct dwarf_cu *cu,
164 const struct dwarf_off_ref *ref)
165 {
166 if (cu == NULL)
167 return NULL;
168 if (ref->from_types) {
169 return NULL;
170 }
171 return hashtags__find(cu->hash_tags, ref->off);
172 }
173
dwarf_cu__find_type_by_ref(const struct dwarf_cu * dcu,const struct dwarf_off_ref * ref)174 static struct dwarf_tag *dwarf_cu__find_type_by_ref(const struct dwarf_cu *dcu,
175 const struct dwarf_off_ref *ref)
176 {
177 if (dcu == NULL)
178 return NULL;
179 if (ref->from_types) {
180 dcu = dcu->type_unit;
181 if (dcu == NULL) {
182 return NULL;
183 }
184 }
185 return hashtags__find(dcu->hash_types, ref->off);
186 }
187
188 extern struct strings *strings;
189
memdup(const void * src,size_t len,struct cu * cu)190 static void *memdup(const void *src, size_t len, struct cu *cu)
191 {
192 void *s = obstack_alloc(&cu->obstack, len);
193 if (s != NULL)
194 memcpy(s, src, len);
195 return s;
196 }
197
198 /* Number decoding macros. See 7.6 Variable Length Data. */
199
200 #define get_uleb128_step(var, addr, nth, break) \
201 __b = *(addr)++; \
202 var |= (uintmax_t) (__b & 0x7f) << (nth * 7); \
203 if ((__b & 0x80) == 0) \
204 break
205
206 #define get_uleb128_rest_return(var, i, addrp) \
207 do { \
208 for (; i < 10; ++i) { \
209 get_uleb128_step(var, *addrp, i, \
210 return var); \
211 } \
212 /* Other implementations set VALUE to UINT_MAX in this \
213 case. So we better do this as well. */ \
214 return UINT64_MAX; \
215 } while (0)
216
__libdw_get_uleb128(uint64_t acc,uint32_t i,const uint8_t ** addrp)217 static uint64_t __libdw_get_uleb128(uint64_t acc, uint32_t i,
218 const uint8_t **addrp)
219 {
220 uint8_t __b;
221 get_uleb128_rest_return (acc, i, addrp);
222 }
223
224 #define get_uleb128(var, addr) \
225 do { \
226 uint8_t __b; \
227 var = 0; \
228 get_uleb128_step(var, addr, 0, break); \
229 var = __libdw_get_uleb128 (var, 1, &(addr)); \
230 } while (0)
231
attr_numeric(Dwarf_Die * die,uint32_t name)232 static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
233 {
234 Dwarf_Attribute attr;
235 uint32_t form;
236
237 if (dwarf_attr(die, name, &attr) == NULL)
238 return 0;
239
240 form = dwarf_whatform(&attr);
241
242 switch (form) {
243 case DW_FORM_addr: {
244 Dwarf_Addr addr;
245 if (dwarf_formaddr(&attr, &addr) == 0)
246 return addr;
247 }
248 break;
249 case DW_FORM_data1:
250 case DW_FORM_data2:
251 case DW_FORM_data4:
252 case DW_FORM_data8:
253 case DW_FORM_sdata:
254 case DW_FORM_udata: {
255 Dwarf_Word value;
256 if (dwarf_formudata(&attr, &value) == 0)
257 return value;
258 }
259 break;
260 case DW_FORM_flag:
261 case DW_FORM_flag_present: {
262 bool value;
263 if (dwarf_formflag(&attr, &value) == 0)
264 return value;
265 }
266 break;
267 default:
268 fprintf(stderr, "DW_AT_<0x%x>=0x%x\n", name, form);
269 break;
270 }
271
272 return 0;
273 }
274
dwarf_expr(const uint8_t * expr,uint32_t len __unused)275 static uint64_t dwarf_expr(const uint8_t *expr, uint32_t len __unused)
276 {
277 /* Common case: offset from start of the class */
278 if (expr[0] == DW_OP_plus_uconst ||
279 expr[0] == DW_OP_constu) {
280 uint64_t result;
281 ++expr;
282 get_uleb128(result, expr);
283 return result;
284 }
285
286 fprintf(stderr, "%s: unhandled %#x DW_OP_ operation\n",
287 __func__, *expr);
288 return UINT64_MAX;
289 }
290
attr_offset(Dwarf_Die * die,const uint32_t name)291 static Dwarf_Off attr_offset(Dwarf_Die *die, const uint32_t name)
292 {
293 Dwarf_Attribute attr;
294 Dwarf_Block block;
295
296 if (dwarf_attr(die, name, &attr) == NULL)
297 return 0;
298
299 switch (dwarf_whatform(&attr)) {
300 case DW_FORM_data1:
301 case DW_FORM_data2:
302 case DW_FORM_data4:
303 case DW_FORM_data8:
304 case DW_FORM_sdata:
305 case DW_FORM_udata: {
306 Dwarf_Word value;
307 if (dwarf_formudata(&attr, &value) == 0)
308 return value;
309 break;
310 }
311 default:
312 if (dwarf_formblock(&attr, &block) == 0)
313 return dwarf_expr(block.data, block.length);
314 }
315
316 return 0;
317 }
318
attr_string(Dwarf_Die * die,uint32_t name)319 static const char *attr_string(Dwarf_Die *die, uint32_t name)
320 {
321 Dwarf_Attribute attr;
322 if (dwarf_attr(die, name, &attr) != NULL)
323 return dwarf_formstring(&attr);
324 return NULL;
325 }
326
attr_type(Dwarf_Die * die,uint32_t attr_name)327 static struct dwarf_off_ref attr_type(Dwarf_Die *die, uint32_t attr_name)
328 {
329 Dwarf_Attribute attr;
330 struct dwarf_off_ref ref;
331 if (dwarf_attr(die, attr_name, &attr) != NULL) {
332 Dwarf_Die type_die;
333 if (dwarf_formref_die(&attr, &type_die) != NULL) {
334 ref.from_types = attr.form == DW_FORM_ref_sig8;
335 ref.off = dwarf_dieoffset(&type_die);
336 return ref;
337 }
338 }
339 memset(&ref, 0, sizeof(ref));
340 return ref;
341 }
342
attr_location(Dwarf_Die * die,Dwarf_Op ** expr,size_t * exprlen)343 static int attr_location(Dwarf_Die *die, Dwarf_Op **expr, size_t *exprlen)
344 {
345 Dwarf_Attribute attr;
346 if (dwarf_attr(die, DW_AT_location, &attr) != NULL) {
347 if (dwarf_getlocation(&attr, expr, exprlen) == 0)
348 return 0;
349 }
350
351 return 1;
352 }
353
__tag__alloc(struct dwarf_cu * dcu,size_t size,bool spec)354 static void *__tag__alloc(struct dwarf_cu *dcu, size_t size, bool spec)
355 {
356 struct dwarf_tag *dtag = obstack_zalloc(&dcu->obstack,
357 (sizeof(*dtag) +
358 (spec ? sizeof(dwarf_off_ref) : 0)));
359 if (dtag == NULL)
360 return NULL;
361
362 struct tag *tag = obstack_zalloc(&dcu->cu->obstack, size);
363
364 if (tag == NULL)
365 return NULL;
366
367 dtag->tag = tag;
368 tag->priv = dtag;
369 tag->type = 0;
370 tag->top_level = 0;
371
372 return tag;
373 }
374
tag__alloc(struct cu * cu,size_t size)375 static void *tag__alloc(struct cu *cu, size_t size)
376 {
377 return __tag__alloc(cu->priv, size, false);
378 }
379
tag__alloc_with_spec(struct cu * cu,size_t size)380 static void *tag__alloc_with_spec(struct cu *cu, size_t size)
381 {
382 return __tag__alloc(cu->priv, size, true);
383 }
384
tag__init(struct tag * tag,struct cu * cu,Dwarf_Die * die)385 static void tag__init(struct tag *tag, struct cu *cu, Dwarf_Die *die)
386 {
387 struct dwarf_tag *dtag = tag->priv;
388
389 tag->tag = dwarf_tag(die);
390
391 dtag->id = dwarf_dieoffset(die);
392
393 if (tag->tag == DW_TAG_imported_module ||
394 tag->tag == DW_TAG_imported_declaration)
395 dtag->type = attr_type(die, DW_AT_import);
396 else
397 dtag->type = attr_type(die, DW_AT_type);
398
399 dtag->abstract_origin = attr_type(die, DW_AT_abstract_origin);
400 tag->recursivity_level = 0;
401
402 if (cu->extra_dbg_info) {
403 int32_t decl_line;
404 const char *decl_file = dwarf_decl_file(die);
405 static const char *last_decl_file;
406 static uint32_t last_decl_file_idx;
407
408 if (decl_file != last_decl_file) {
409 last_decl_file_idx = strings__add(strings, decl_file);
410 last_decl_file = decl_file;
411 }
412
413 dtag->decl_file = last_decl_file_idx;
414 dwarf_decl_line(die, &decl_line);
415 dtag->decl_line = decl_line;
416 }
417
418 INIT_LIST_HEAD(&tag->node);
419 }
420
tag__new(Dwarf_Die * die,struct cu * cu)421 static struct tag *tag__new(Dwarf_Die *die, struct cu *cu)
422 {
423 struct tag *tag = tag__alloc(cu, sizeof(*tag));
424
425 if (tag != NULL)
426 tag__init(tag, cu, die);
427
428 return tag;
429 }
430
ptr_to_member_type__new(Dwarf_Die * die,struct cu * cu)431 static struct ptr_to_member_type *ptr_to_member_type__new(Dwarf_Die *die,
432 struct cu *cu)
433 {
434 struct ptr_to_member_type *ptr = tag__alloc(cu, sizeof(*ptr));
435
436 if (ptr != NULL) {
437 tag__init(&ptr->tag, cu, die);
438 struct dwarf_tag *dtag = ptr->tag.priv;
439 dtag->containing_type = attr_type(die, DW_AT_containing_type);
440 }
441
442 return ptr;
443 }
444
base_type__new(Dwarf_Die * die,struct cu * cu)445 static struct base_type *base_type__new(Dwarf_Die *die, struct cu *cu)
446 {
447 struct base_type *bt = tag__alloc(cu, sizeof(*bt));
448
449 if (bt != NULL) {
450 tag__init(&bt->tag, cu, die);
451 bt->name = strings__add(strings, attr_string(die, DW_AT_name));
452 bt->bit_size = attr_numeric(die, DW_AT_byte_size) * 8;
453 uint64_t encoding = attr_numeric(die, DW_AT_encoding);
454 bt->is_bool = encoding == DW_ATE_boolean;
455 bt->is_signed = encoding == DW_ATE_signed;
456 bt->is_varargs = false;
457 bt->name_has_encoding = true;
458 }
459
460 return bt;
461 }
462
array_type__new(Dwarf_Die * die,struct cu * cu)463 static struct array_type *array_type__new(Dwarf_Die *die, struct cu *cu)
464 {
465 struct array_type *at = tag__alloc(cu, sizeof(*at));
466
467 if (at != NULL) {
468 tag__init(&at->tag, cu, die);
469 at->dimensions = 0;
470 at->nr_entries = NULL;
471 at->is_vector = dwarf_hasattr(die, DW_AT_GNU_vector);
472 }
473
474 return at;
475 }
476
namespace__init(struct namespace * namespace,Dwarf_Die * die,struct cu * cu)477 static void namespace__init(struct namespace *namespace, Dwarf_Die *die,
478 struct cu *cu)
479 {
480 tag__init(&namespace->tag, cu, die);
481 INIT_LIST_HEAD(&namespace->tags);
482 namespace->sname = 0;
483 namespace->name = strings__add(strings, attr_string(die, DW_AT_name));
484 namespace->nr_tags = 0;
485 namespace->shared_tags = 0;
486 }
487
namespace__new(Dwarf_Die * die,struct cu * cu)488 static struct namespace *namespace__new(Dwarf_Die *die, struct cu *cu)
489 {
490 struct namespace *namespace = tag__alloc(cu, sizeof(*namespace));
491
492 if (namespace != NULL)
493 namespace__init(namespace, die, cu);
494
495 return namespace;
496 }
497
type__init(struct type * type,Dwarf_Die * die,struct cu * cu)498 static void type__init(struct type *type, Dwarf_Die *die, struct cu *cu)
499 {
500 namespace__init(&type->namespace, die, cu);
501 INIT_LIST_HEAD(&type->node);
502 type->size = attr_numeric(die, DW_AT_byte_size);
503 type->alignment = attr_numeric(die, DW_AT_alignment);
504 type->declaration = attr_numeric(die, DW_AT_declaration);
505 dwarf_tag__set_spec(type->namespace.tag.priv,
506 attr_type(die, DW_AT_specification));
507 type->definition_emitted = 0;
508 type->fwd_decl_emitted = 0;
509 type->resized = 0;
510 type->nr_members = 0;
511 type->nr_static_members = 0;
512 }
513
type__new(Dwarf_Die * die,struct cu * cu)514 static struct type *type__new(Dwarf_Die *die, struct cu *cu)
515 {
516 struct type *type = tag__alloc_with_spec(cu, sizeof(*type));
517
518 if (type != NULL)
519 type__init(type, die, cu);
520
521 return type;
522 }
523
enumerator__new(Dwarf_Die * die,struct cu * cu)524 static struct enumerator *enumerator__new(Dwarf_Die *die, struct cu *cu)
525 {
526 struct enumerator *enumerator = tag__alloc(cu, sizeof(*enumerator));
527
528 if (enumerator != NULL) {
529 tag__init(&enumerator->tag, cu, die);
530 enumerator->name = strings__add(strings, attr_string(die, DW_AT_name));
531 enumerator->value = attr_numeric(die, DW_AT_const_value);
532 }
533
534 return enumerator;
535 }
536
dwarf__location(Dwarf_Die * die,uint64_t * addr,struct location * location)537 static enum vscope dwarf__location(Dwarf_Die *die, uint64_t *addr, struct location *location)
538 {
539 enum vscope scope = VSCOPE_UNKNOWN;
540
541 if (attr_location(die, &location->expr, &location->exprlen) != 0)
542 scope = VSCOPE_OPTIMIZED;
543 else if (location->exprlen != 0) {
544 Dwarf_Op *expr = location->expr;
545 switch (expr->atom) {
546 case DW_OP_addr:
547 scope = VSCOPE_GLOBAL;
548 *addr = expr[0].number;
549 break;
550 case DW_OP_reg1 ... DW_OP_reg31:
551 case DW_OP_breg0 ... DW_OP_breg31:
552 scope = VSCOPE_REGISTER; break;
553 case DW_OP_fbreg:
554 scope = VSCOPE_LOCAL; break;
555 }
556 }
557
558 return scope;
559 }
560
variable__scope(const struct dw_variable * var)561 enum vscope variable__scope(const struct dw_variable *var)
562 {
563 return var->scope;
564 }
565
variable__scope_str(const struct dw_variable * var)566 const char *variable__scope_str(const struct dw_variable *var)
567 {
568 switch (var->scope) {
569 case VSCOPE_LOCAL: return "local";
570 case VSCOPE_GLOBAL: return "global";
571 case VSCOPE_REGISTER: return "register";
572 case VSCOPE_OPTIMIZED: return "optimized";
573 default: break;
574 };
575
576 return "unknown";
577 }
578
variable__new(Dwarf_Die * die,struct cu * cu)579 static struct dw_variable *variable__new(Dwarf_Die *die, struct cu *cu)
580 {
581 struct dw_variable *var = tag__alloc(cu, sizeof(*var));
582
583 if (var != NULL) {
584 tag__init(&var->ip.tag, cu, die);
585 var->name = strings__add(strings, attr_string(die, DW_AT_name));
586 /* variable is visible outside of its enclosing cu */
587 var->external = dwarf_hasattr(die, DW_AT_external);
588 /* non-defining declaration of an object */
589 var->declaration = dwarf_hasattr(die, DW_AT_declaration);
590 var->scope = VSCOPE_UNKNOWN;
591 var->ip.addr = 0;
592 if (!var->declaration && cu->has_addr_info)
593 var->scope = dwarf__location(die, &var->ip.addr, &var->location);
594 }
595
596 return var;
597 }
598
tag__recode_dwarf_bitfield(struct tag * tag,struct cu * cu,uint16_t bit_size)599 static int tag__recode_dwarf_bitfield(struct tag *tag, struct cu *cu, uint16_t bit_size)
600 {
601 int id;
602 type_id_t short_id;
603 struct tag *recoded;
604 /* in all the cases the name is at the same offset */
605 strings_t name = tag__namespace(tag)->name;
606
607 switch (tag->tag) {
608 case DW_TAG_typedef: {
609 const struct dwarf_tag *dtag = tag->priv;
610 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv,
611 &dtag->type);
612 struct tag *type = dtype->tag;
613
614 id = tag__recode_dwarf_bitfield(type, cu, bit_size);
615 if (id < 0)
616 return id;
617
618 struct type *new_typedef = obstack_zalloc(&cu->obstack,
619 sizeof(*new_typedef));
620 if (new_typedef == NULL)
621 return -ENOMEM;
622
623 recoded = (struct tag *)new_typedef;
624 recoded->tag = DW_TAG_typedef;
625 recoded->type = id;
626 new_typedef->namespace.name = tag__namespace(tag)->name;
627 }
628 break;
629
630 case DW_TAG_const_type:
631 case DW_TAG_volatile_type: {
632 const struct dwarf_tag *dtag = tag->priv;
633 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
634 struct tag *type = dtype->tag;
635
636 id = tag__recode_dwarf_bitfield(type, cu, bit_size);
637 if (id == tag->type)
638 return id;
639
640 recoded = obstack_zalloc(&cu->obstack, sizeof(*recoded));
641 if (recoded == NULL)
642 return -ENOMEM;
643
644 recoded->tag = DW_TAG_volatile_type;
645 recoded->type = id;
646 }
647 break;
648
649 case DW_TAG_base_type:
650 /*
651 * Here we must search on the final, core cu, not on
652 * the dwarf_cu as in dwarf there are no such things
653 * as base_types of less than 8 bits, etc.
654 */
655 recoded = cu__find_base_type_by_sname_and_size(cu, name, bit_size, &short_id);
656 if (recoded != NULL)
657 return short_id;
658
659 struct base_type *new_bt = obstack_zalloc(&cu->obstack,
660 sizeof(*new_bt));
661 if (new_bt == NULL)
662 return -ENOMEM;
663
664 recoded = (struct tag *)new_bt;
665 recoded->tag = DW_TAG_base_type;
666 recoded->top_level = 1;
667 new_bt->name = name;
668 new_bt->bit_size = bit_size;
669 break;
670
671 case DW_TAG_enumeration_type:
672 /*
673 * Here we must search on the final, core cu, not on
674 * the dwarf_cu as in dwarf there are no such things
675 * as enumeration_types of less than 8 bits, etc.
676 */
677 recoded = cu__find_enumeration_by_sname_and_size(cu, name, bit_size, &short_id);
678 if (recoded != NULL)
679 return short_id;
680
681 struct type *alias = tag__type(tag);
682 struct type *new_enum = obstack_zalloc(&cu->obstack, sizeof(*new_enum));
683 if (new_enum == NULL)
684 return -ENOMEM;
685
686 recoded = (struct tag *)new_enum;
687 recoded->tag = DW_TAG_enumeration_type;
688 recoded->top_level = 1;
689 new_enum->nr_members = alias->nr_members;
690 /*
691 * Share the tags
692 */
693 new_enum->namespace.tags.next = &alias->namespace.tags;
694 new_enum->namespace.shared_tags = 1;
695 new_enum->namespace.name = name;
696 new_enum->size = bit_size;
697 break;
698 default:
699 fprintf(stderr, "%s: tag=%s, name=%s, bit_size=%d\n",
700 __func__, dwarf_tag_name(tag->tag),
701 strings__ptr(strings, name), bit_size);
702 return -EINVAL;
703 }
704
705 uint32_t new_id;
706 if (cu__add_tag(cu, recoded, &new_id) == 0)
707 return new_id;
708
709 obstack_free(&cu->obstack, recoded);
710 return -ENOMEM;
711 }
712
class_member__dwarf_recode_bitfield(struct class_member * member,struct cu * cu)713 int class_member__dwarf_recode_bitfield(struct class_member *member,
714 struct cu *cu)
715 {
716 struct dwarf_tag *dtag = member->tag.priv;
717 struct dwarf_tag *type = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
718 int recoded_type_id;
719
720 if (type == NULL)
721 return -ENOENT;
722
723 recoded_type_id = tag__recode_dwarf_bitfield(type->tag, cu, member->bitfield_size);
724 if (recoded_type_id < 0)
725 return recoded_type_id;
726
727 member->tag.type = recoded_type_id;
728 return 0;
729 }
730
class_member__new(Dwarf_Die * die,struct cu * cu,bool in_union)731 static struct class_member *class_member__new(Dwarf_Die *die, struct cu *cu,
732 bool in_union)
733 {
734 struct class_member *member = tag__alloc(cu, sizeof(*member));
735
736 if (member != NULL) {
737 tag__init(&member->tag, cu, die);
738 member->name = strings__add(strings, attr_string(die, DW_AT_name));
739 member->is_static = !in_union && !dwarf_hasattr(die, DW_AT_data_member_location);
740 member->const_value = attr_numeric(die, DW_AT_const_value);
741 member->alignment = attr_numeric(die, DW_AT_alignment);
742 member->byte_offset = attr_offset(die, DW_AT_data_member_location);
743 /*
744 * Bit offset calculated here is valid only for byte-aligned
745 * fields. For bitfields on little-endian archs we need to
746 * adjust them taking into account byte size of the field,
747 * which might not be yet known. So we'll re-calculate bit
748 * offset later, in class_member__cache_byte_size.
749 */
750 member->bit_offset = member->byte_offset * 8;
751 /*
752 * If DW_AT_byte_size is not present, byte size will be
753 * determined later in class_member__cache_byte_size using
754 * base integer/enum type
755 */
756 member->byte_size = attr_numeric(die, DW_AT_byte_size);
757 member->bitfield_offset = attr_numeric(die, DW_AT_bit_offset);
758 member->bitfield_size = attr_numeric(die, DW_AT_bit_size);
759 member->bit_hole = 0;
760 member->bitfield_end = 0;
761 member->visited = 0;
762 member->accessibility = attr_numeric(die, DW_AT_accessibility);
763 member->virtuality = attr_numeric(die, DW_AT_virtuality);
764 member->hole = 0;
765 }
766
767 return member;
768 }
769
parameter__new(Dwarf_Die * die,struct cu * cu)770 static struct parameter *parameter__new(Dwarf_Die *die, struct cu *cu)
771 {
772 struct parameter *parm = tag__alloc(cu, sizeof(*parm));
773
774 if (parm != NULL) {
775 tag__init(&parm->tag, cu, die);
776 parm->name = strings__add(strings, attr_string(die, DW_AT_name));
777 }
778
779 return parm;
780 }
781
inline_expansion__new(Dwarf_Die * die,struct cu * cu)782 static struct inline_expansion *inline_expansion__new(Dwarf_Die *die,
783 struct cu *cu)
784 {
785 struct inline_expansion *exp = tag__alloc(cu, sizeof(*exp));
786
787 if (exp != NULL) {
788 struct dwarf_tag *dtag = exp->ip.tag.priv;
789
790 tag__init(&exp->ip.tag, cu, die);
791 dtag->decl_file =
792 strings__add(strings, attr_string(die, DW_AT_call_file));
793 dtag->decl_line = attr_numeric(die, DW_AT_call_line);
794 dtag->type = attr_type(die, DW_AT_abstract_origin);
795 exp->ip.addr = 0;
796 exp->high_pc = 0;
797
798 if (!cu->has_addr_info)
799 goto out;
800
801 if (dwarf_lowpc(die, &exp->ip.addr))
802 exp->ip.addr = 0;
803 if (dwarf_lowpc(die, &exp->high_pc))
804 exp->high_pc = 0;
805
806 exp->size = exp->high_pc - exp->ip.addr;
807 if (exp->size == 0) {
808 Dwarf_Addr base, start;
809 ptrdiff_t offset = 0;
810
811 while (1) {
812 offset = dwarf_ranges(die, offset, &base, &start,
813 &exp->high_pc);
814 start = (unsigned long)start;
815 exp->high_pc = (unsigned long)exp->high_pc;
816 if (offset <= 0)
817 break;
818 exp->size += exp->high_pc - start;
819 if (exp->ip.addr == 0)
820 exp->ip.addr = start;
821 }
822 }
823 }
824 out:
825 return exp;
826 }
827
label__new(Dwarf_Die * die,struct cu * cu)828 static struct label *label__new(Dwarf_Die *die, struct cu *cu)
829 {
830 struct label *label = tag__alloc(cu, sizeof(*label));
831
832 if (label != NULL) {
833 tag__init(&label->ip.tag, cu, die);
834 label->name = strings__add(strings, attr_string(die, DW_AT_name));
835 if (!cu->has_addr_info || dwarf_lowpc(die, &label->ip.addr))
836 label->ip.addr = 0;
837 }
838
839 return label;
840 }
841
class__new(Dwarf_Die * die,struct cu * cu)842 static struct class *class__new(Dwarf_Die *die, struct cu *cu)
843 {
844 struct class *class = tag__alloc_with_spec(cu, sizeof(*class));
845
846 if (class != NULL) {
847 type__init(&class->type, die, cu);
848 INIT_LIST_HEAD(&class->vtable);
849 class->nr_vtable_entries =
850 class->nr_holes =
851 class->nr_bit_holes =
852 class->padding =
853 class->bit_padding = 0;
854 class->priv = NULL;
855 }
856
857 return class;
858 }
859
lexblock__init(struct lexblock * block,struct cu * cu,Dwarf_Die * die)860 static void lexblock__init(struct lexblock *block, struct cu *cu,
861 Dwarf_Die *die)
862 {
863 Dwarf_Off high_pc;
864
865 if (!cu->has_addr_info || dwarf_lowpc(die, &block->ip.addr)) {
866 block->ip.addr = 0;
867 block->size = 0;
868 } else if (dwarf_highpc(die, &high_pc))
869 block->size = 0;
870 else
871 block->size = high_pc - block->ip.addr;
872
873 INIT_LIST_HEAD(&block->tags);
874
875 block->size_inline_expansions =
876 block->nr_inline_expansions =
877 block->nr_labels =
878 block->nr_lexblocks =
879 block->nr_variables = 0;
880 }
881
lexblock__new(Dwarf_Die * die,struct cu * cu)882 static struct lexblock *lexblock__new(Dwarf_Die *die, struct cu *cu)
883 {
884 struct lexblock *block = tag__alloc(cu, sizeof(*block));
885
886 if (block != NULL) {
887 tag__init(&block->ip.tag, cu, die);
888 lexblock__init(block, cu, die);
889 }
890
891 return block;
892 }
893
ftype__init(struct ftype * ftype,Dwarf_Die * die,struct cu * cu)894 static void ftype__init(struct ftype *ftype, Dwarf_Die *die, struct cu *cu)
895 {
896 const uint16_t tag = dwarf_tag(die);
897 assert(tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type);
898
899 tag__init(&ftype->tag, cu, die);
900 INIT_LIST_HEAD(&ftype->parms);
901 ftype->nr_parms = 0;
902 ftype->unspec_parms = 0;
903 }
904
ftype__new(Dwarf_Die * die,struct cu * cu)905 static struct ftype *ftype__new(Dwarf_Die *die, struct cu *cu)
906 {
907 struct ftype *ftype = tag__alloc(cu, sizeof(*ftype));
908
909 if (ftype != NULL)
910 ftype__init(ftype, die, cu);
911
912 return ftype;
913 }
914
function__new(Dwarf_Die * die,struct cu * cu)915 static struct function *function__new(Dwarf_Die *die, struct cu *cu)
916 {
917 struct function *func = tag__alloc_with_spec(cu, sizeof(*func));
918
919 if (func != NULL) {
920 ftype__init(&func->proto, die, cu);
921 lexblock__init(&func->lexblock, cu, die);
922 func->name = strings__add(strings, attr_string(die, DW_AT_name));
923 func->linkage_name = strings__add(strings, attr_string(die, DW_AT_MIPS_linkage_name));
924 func->inlined = attr_numeric(die, DW_AT_inline);
925 func->declaration = dwarf_hasattr(die, DW_AT_declaration);
926 func->external = dwarf_hasattr(die, DW_AT_external);
927 func->abstract_origin = dwarf_hasattr(die, DW_AT_abstract_origin);
928 dwarf_tag__set_spec(func->proto.tag.priv,
929 attr_type(die, DW_AT_specification));
930 func->accessibility = attr_numeric(die, DW_AT_accessibility);
931 func->virtuality = attr_numeric(die, DW_AT_virtuality);
932 INIT_LIST_HEAD(&func->vtable_node);
933 INIT_LIST_HEAD(&func->tool_node);
934 func->vtable_entry = -1;
935 if (dwarf_hasattr(die, DW_AT_vtable_elem_location))
936 func->vtable_entry = attr_offset(die, DW_AT_vtable_elem_location);
937 func->cu_total_size_inline_expansions = 0;
938 func->cu_total_nr_inline_expansions = 0;
939 func->priv = NULL;
940 }
941
942 return func;
943 }
944
attr_upper_bound(Dwarf_Die * die)945 static uint64_t attr_upper_bound(Dwarf_Die *die)
946 {
947 Dwarf_Attribute attr;
948
949 if (dwarf_attr(die, DW_AT_upper_bound, &attr) != NULL) {
950 Dwarf_Word num;
951
952 if (dwarf_formudata(&attr, &num) == 0) {
953 return (uintmax_t)num + 1;
954 }
955 } else if (dwarf_attr(die, DW_AT_count, &attr) != NULL) {
956 Dwarf_Word num;
957
958 if (dwarf_formudata(&attr, &num) == 0) {
959 return (uintmax_t)num;
960 }
961 }
962
963 return 0;
964 }
965
__cu__tag_not_handled(Dwarf_Die * die,const char * fn)966 static void __cu__tag_not_handled(Dwarf_Die *die, const char *fn)
967 {
968 uint32_t tag = dwarf_tag(die);
969
970 fprintf(stderr, "%s: DW_TAG_%s (%#x) @ <%#llx> not handled!\n",
971 fn, dwarf_tag_name(tag), tag,
972 (unsigned long long)dwarf_dieoffset(die));
973 }
974
975 static struct tag unsupported_tag;
976
977 #define cu__tag_not_handled(die) __cu__tag_not_handled(die, __FUNCTION__)
978
979 static struct tag *__die__process_tag(Dwarf_Die *die, struct cu *cu,
980 int toplevel, const char *fn);
981
982 #define die__process_tag(die, cu, toplevel) \
983 __die__process_tag(die, cu, toplevel, __FUNCTION__)
984
die__create_new_tag(Dwarf_Die * die,struct cu * cu)985 static struct tag *die__create_new_tag(Dwarf_Die *die, struct cu *cu)
986 {
987 struct tag *tag = tag__new(die, cu);
988
989 if (tag != NULL) {
990 if (dwarf_haschildren(die))
991 fprintf(stderr, "%s: %s WITH children!\n", __func__,
992 dwarf_tag_name(tag->tag));
993 }
994
995 return tag;
996 }
997
die__create_new_ptr_to_member_type(Dwarf_Die * die,struct cu * cu)998 static struct tag *die__create_new_ptr_to_member_type(Dwarf_Die *die,
999 struct cu *cu)
1000 {
1001 struct ptr_to_member_type *ptr = ptr_to_member_type__new(die, cu);
1002
1003 return ptr ? &ptr->tag : NULL;
1004 }
1005
1006 static int die__process_class(Dwarf_Die *die,
1007 struct type *class, struct cu *cu);
1008
die__create_new_class(Dwarf_Die * die,struct cu * cu)1009 static struct tag *die__create_new_class(Dwarf_Die *die, struct cu *cu)
1010 {
1011 Dwarf_Die child;
1012 struct class *class = class__new(die, cu);
1013
1014 if (class != NULL &&
1015 dwarf_haschildren(die) != 0 &&
1016 dwarf_child(die, &child) == 0) {
1017 if (die__process_class(&child, &class->type, cu) != 0) {
1018 class__delete(class, cu);
1019 class = NULL;
1020 }
1021 }
1022
1023 return class ? &class->type.namespace.tag : NULL;
1024 }
1025
1026 static int die__process_namespace(Dwarf_Die *die, struct namespace *namespace,
1027 struct cu *cu);
1028
die__create_new_namespace(Dwarf_Die * die,struct cu * cu)1029 static struct tag *die__create_new_namespace(Dwarf_Die *die, struct cu *cu)
1030 {
1031 Dwarf_Die child;
1032 struct namespace *namespace = namespace__new(die, cu);
1033
1034 if (namespace != NULL &&
1035 dwarf_haschildren(die) != 0 &&
1036 dwarf_child(die, &child) == 0) {
1037 if (die__process_namespace(&child, namespace, cu) != 0) {
1038 namespace__delete(namespace, cu);
1039 namespace = NULL;
1040 }
1041 }
1042
1043 return namespace ? &namespace->tag : NULL;
1044 }
1045
die__create_new_union(Dwarf_Die * die,struct cu * cu)1046 static struct tag *die__create_new_union(Dwarf_Die *die, struct cu *cu)
1047 {
1048 Dwarf_Die child;
1049 struct type *utype = type__new(die, cu);
1050
1051 if (utype != NULL &&
1052 dwarf_haschildren(die) != 0 &&
1053 dwarf_child(die, &child) == 0) {
1054 if (die__process_class(&child, utype, cu) != 0) {
1055 type__delete(utype, cu);
1056 utype = NULL;
1057 }
1058 }
1059
1060 return utype ? &utype->namespace.tag : NULL;
1061 }
1062
die__create_new_base_type(Dwarf_Die * die,struct cu * cu)1063 static struct tag *die__create_new_base_type(Dwarf_Die *die, struct cu *cu)
1064 {
1065 struct base_type *base = base_type__new(die, cu);
1066
1067 if (base == NULL)
1068 return NULL;
1069
1070 if (dwarf_haschildren(die))
1071 fprintf(stderr, "%s: DW_TAG_base_type WITH children!\n",
1072 __func__);
1073
1074 return &base->tag;
1075 }
1076
die__create_new_typedef(Dwarf_Die * die,struct cu * cu)1077 static struct tag *die__create_new_typedef(Dwarf_Die *die, struct cu *cu)
1078 {
1079 struct type *tdef = type__new(die, cu);
1080
1081 if (tdef == NULL)
1082 return NULL;
1083
1084 if (dwarf_haschildren(die)) {
1085 struct dwarf_tag *dtag = tdef->namespace.tag.priv;
1086 fprintf(stderr, "%s: DW_TAG_typedef %llx WITH children!\n",
1087 __func__, (unsigned long long)dtag->id);
1088 }
1089
1090 return &tdef->namespace.tag;
1091 }
1092
die__create_new_array(Dwarf_Die * die,struct cu * cu)1093 static struct tag *die__create_new_array(Dwarf_Die *die, struct cu *cu)
1094 {
1095 Dwarf_Die child;
1096 /* "64 dimensions will be enough for everybody." acme, 2006 */
1097 const uint8_t max_dimensions = 64;
1098 uint32_t nr_entries[max_dimensions];
1099 struct array_type *array = array_type__new(die, cu);
1100
1101 if (array == NULL)
1102 return NULL;
1103
1104 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1105 return &array->tag;
1106
1107 die = &child;
1108 do {
1109 if (dwarf_tag(die) == DW_TAG_subrange_type) {
1110 nr_entries[array->dimensions++] = attr_upper_bound(die);
1111 if (array->dimensions == max_dimensions) {
1112 fprintf(stderr, "%s: only %u dimensions are "
1113 "supported!\n",
1114 __FUNCTION__, max_dimensions);
1115 break;
1116 }
1117 } else
1118 cu__tag_not_handled(die);
1119 } while (dwarf_siblingof(die, die) == 0);
1120
1121 array->nr_entries = memdup(nr_entries,
1122 array->dimensions * sizeof(uint32_t), cu);
1123 if (array->nr_entries == NULL)
1124 goto out_free;
1125
1126 return &array->tag;
1127 out_free:
1128 obstack_free(&cu->obstack, array);
1129 return NULL;
1130 }
1131
die__create_new_parameter(Dwarf_Die * die,struct ftype * ftype,struct lexblock * lexblock,struct cu * cu)1132 static struct tag *die__create_new_parameter(Dwarf_Die *die,
1133 struct ftype *ftype,
1134 struct lexblock *lexblock,
1135 struct cu *cu)
1136 {
1137 struct parameter *parm = parameter__new(die, cu);
1138
1139 if (parm == NULL)
1140 return NULL;
1141
1142 if (ftype != NULL)
1143 ftype__add_parameter(ftype, parm);
1144 else {
1145 /*
1146 * DW_TAG_formal_parameters on a non DW_TAG_subprogram nor
1147 * DW_TAG_subroutine_type tag happens sometimes, likely due to
1148 * compiler optimizing away a inline expansion (at least this
1149 * was observed in some cases, such as in the Linux kernel
1150 * current_kernel_time function circa 2.6.20-rc5), keep it in
1151 * the lexblock tag list because it can be referenced as an
1152 * DW_AT_abstract_origin in another DW_TAG_formal_parameter.
1153 */
1154 lexblock__add_tag(lexblock, &parm->tag);
1155 }
1156
1157 return &parm->tag;
1158 }
1159
die__create_new_label(Dwarf_Die * die,struct lexblock * lexblock,struct cu * cu)1160 static struct tag *die__create_new_label(Dwarf_Die *die,
1161 struct lexblock *lexblock,
1162 struct cu *cu)
1163 {
1164 struct label *label = label__new(die, cu);
1165
1166 if (label == NULL)
1167 return NULL;
1168
1169 lexblock__add_label(lexblock, label);
1170 return &label->ip.tag;
1171 }
1172
die__create_new_variable(Dwarf_Die * die,struct cu * cu)1173 static struct tag *die__create_new_variable(Dwarf_Die *die, struct cu *cu)
1174 {
1175 struct dw_variable *var = variable__new(die, cu);
1176
1177 return var ? &var->ip.tag : NULL;
1178 }
1179
die__create_new_subroutine_type(Dwarf_Die * die,struct cu * cu)1180 static struct tag *die__create_new_subroutine_type(Dwarf_Die *die,
1181 struct cu *cu)
1182 {
1183 Dwarf_Die child;
1184 struct ftype *ftype = ftype__new(die, cu);
1185 struct tag *tag;
1186
1187 if (ftype == NULL)
1188 return NULL;
1189
1190 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1191 goto out;
1192
1193 die = &child;
1194 do {
1195 uint32_t id;
1196
1197 switch (dwarf_tag(die)) {
1198 case DW_TAG_formal_parameter:
1199 tag = die__create_new_parameter(die, ftype, NULL, cu);
1200 break;
1201 case DW_TAG_unspecified_parameters:
1202 ftype->unspec_parms = 1;
1203 continue;
1204 default:
1205 tag = die__process_tag(die, cu, 0);
1206 if (tag == NULL)
1207 goto out_delete;
1208
1209 if (cu__add_tag(cu, tag, &id) < 0)
1210 goto out_delete_tag;
1211
1212 goto hash;
1213 }
1214
1215 if (tag == NULL)
1216 goto out_delete;
1217
1218 if (cu__table_add_tag(cu, tag, &id) < 0)
1219 goto out_delete_tag;
1220 hash:
1221 cu__hash(cu, tag);
1222 struct dwarf_tag *dtag = tag->priv;
1223 dtag->small_id = id;
1224 } while (dwarf_siblingof(die, die) == 0);
1225 out:
1226 return &ftype->tag;
1227 out_delete_tag:
1228 tag__delete(tag, cu);
1229 out_delete:
1230 ftype__delete(ftype, cu);
1231 return NULL;
1232 }
1233
die__create_new_enumeration(Dwarf_Die * die,struct cu * cu)1234 static struct tag *die__create_new_enumeration(Dwarf_Die *die, struct cu *cu)
1235 {
1236 Dwarf_Die child;
1237 struct type *enumeration = type__new(die, cu);
1238
1239 if (enumeration == NULL)
1240 return NULL;
1241
1242 if (enumeration->size == 0)
1243 enumeration->size = sizeof(int) * 8;
1244 else
1245 enumeration->size *= 8;
1246
1247 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0) {
1248 /* Seen on libQtCore.so.4.3.4.debug,
1249 * class QAbstractFileEngineIterator, enum EntryInfoType */
1250 goto out;
1251 }
1252
1253 die = &child;
1254 do {
1255 struct enumerator *enumerator;
1256
1257 if (dwarf_tag(die) != DW_TAG_enumerator) {
1258 cu__tag_not_handled(die);
1259 continue;
1260 }
1261 enumerator = enumerator__new(die, cu);
1262 if (enumerator == NULL)
1263 goto out_delete;
1264
1265 enumeration__add(enumeration, enumerator);
1266 } while (dwarf_siblingof(die, die) == 0);
1267 out:
1268 return &enumeration->namespace.tag;
1269 out_delete:
1270 enumeration__delete(enumeration, cu);
1271 return NULL;
1272 }
1273
die__process_class(Dwarf_Die * die,struct type * class,struct cu * cu)1274 static int die__process_class(Dwarf_Die *die, struct type *class,
1275 struct cu *cu)
1276 {
1277 const bool is_union = tag__is_union(&class->namespace.tag);
1278
1279 do {
1280 switch (dwarf_tag(die)) {
1281 #ifdef STB_GNU_UNIQUE
1282 case DW_TAG_GNU_formal_parameter_pack:
1283 case DW_TAG_GNU_template_parameter_pack:
1284 case DW_TAG_GNU_template_template_param:
1285 #endif
1286 case DW_TAG_template_type_parameter:
1287 case DW_TAG_template_value_parameter:
1288 /*
1289 * FIXME: probably we'll have to attach this as a list of
1290 * template parameters to use at class__fprintf time...
1291 *
1292 * See:
1293 * https://gcc.gnu.org/wiki/TemplateParmsDwarf
1294 */
1295 tag__print_not_supported(dwarf_tag(die));
1296 continue;
1297 case DW_TAG_inheritance:
1298 case DW_TAG_member: {
1299 struct class_member *member = class_member__new(die, cu, is_union);
1300
1301 if (member == NULL)
1302 return -ENOMEM;
1303
1304 if (cu__is_c_plus_plus(cu)) {
1305 uint32_t id;
1306
1307 if (cu__table_add_tag(cu, &member->tag, &id) < 0) {
1308 class_member__delete(member, cu);
1309 return -ENOMEM;
1310 }
1311
1312 struct dwarf_tag *dtag = member->tag.priv;
1313 dtag->small_id = id;
1314 }
1315
1316 type__add_member(class, member);
1317 cu__hash(cu, &member->tag);
1318 }
1319 continue;
1320 default: {
1321 struct tag *tag = die__process_tag(die, cu, 0);
1322
1323 if (tag == NULL)
1324 return -ENOMEM;
1325
1326 uint32_t id;
1327
1328 if (cu__table_add_tag(cu, tag, &id) < 0) {
1329 tag__delete(tag, cu);
1330 return -ENOMEM;
1331 }
1332
1333 struct dwarf_tag *dtag = tag->priv;
1334 dtag->small_id = id;
1335
1336 namespace__add_tag(&class->namespace, tag);
1337 cu__hash(cu, tag);
1338 if (tag__is_function(tag)) {
1339 struct function *fself = tag__function(tag);
1340
1341 if (fself->vtable_entry != -1)
1342 class__add_vtable_entry(type__class(class), fself);
1343 }
1344 continue;
1345 }
1346 }
1347 } while (dwarf_siblingof(die, die) == 0);
1348
1349 return 0;
1350 }
1351
die__process_namespace(Dwarf_Die * die,struct namespace * namespace,struct cu * cu)1352 static int die__process_namespace(Dwarf_Die *die, struct namespace *namespace,
1353 struct cu *cu)
1354 {
1355 struct tag *tag;
1356 do {
1357 tag = die__process_tag(die, cu, 0);
1358 if (tag == NULL)
1359 goto out_enomem;
1360
1361 uint32_t id;
1362 if (cu__table_add_tag(cu, tag, &id) < 0)
1363 goto out_delete_tag;
1364
1365 struct dwarf_tag *dtag = tag->priv;
1366 dtag->small_id = id;
1367
1368 namespace__add_tag(namespace, tag);
1369 cu__hash(cu, tag);
1370 } while (dwarf_siblingof(die, die) == 0);
1371
1372 return 0;
1373 out_delete_tag:
1374 tag__delete(tag, cu);
1375 out_enomem:
1376 return -ENOMEM;
1377 }
1378
1379 static int die__process_function(Dwarf_Die *die, struct ftype *ftype,
1380 struct lexblock *lexblock, struct cu *cu);
1381
die__create_new_lexblock(Dwarf_Die * die,struct cu * cu,struct lexblock * father)1382 static int die__create_new_lexblock(Dwarf_Die *die,
1383 struct cu *cu, struct lexblock *father)
1384 {
1385 struct lexblock *lexblock = lexblock__new(die, cu);
1386
1387 if (lexblock != NULL) {
1388 if (die__process_function(die, NULL, lexblock, cu) != 0)
1389 goto out_delete;
1390 }
1391 if (father != NULL)
1392 lexblock__add_lexblock(father, lexblock);
1393 return 0;
1394 out_delete:
1395 lexblock__delete(lexblock, cu);
1396 return -ENOMEM;
1397 }
1398
1399 static struct tag *die__create_new_inline_expansion(Dwarf_Die *die,
1400 struct lexblock *lexblock,
1401 struct cu *cu);
1402
die__process_inline_expansion(Dwarf_Die * die,struct lexblock * lexblock,struct cu * cu)1403 static int die__process_inline_expansion(Dwarf_Die *die, struct lexblock *lexblock, struct cu *cu)
1404 {
1405 Dwarf_Die child;
1406 struct tag *tag;
1407
1408 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1409 return 0;
1410
1411 die = &child;
1412 do {
1413 uint32_t id;
1414
1415 switch (dwarf_tag(die)) {
1416 case DW_TAG_GNU_call_site:
1417 case DW_TAG_GNU_call_site_parameter:
1418 /*
1419 * FIXME: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
1420 * and write proper support.
1421 *
1422 * From a quick read there is not much we can use in
1423 * the existing dwarves tools, so just stop warning the user,
1424 * developers will find these notes if wanting to use in a
1425 * new tool.
1426 */
1427 continue;
1428 case DW_TAG_lexical_block:
1429 if (die__create_new_lexblock(die, cu, lexblock) != 0)
1430 goto out_enomem;
1431 continue;
1432 case DW_TAG_formal_parameter:
1433 /*
1434 * FIXME:
1435 * So far DW_TAG_inline_routine had just an
1436 * abstract origin, but starting with
1437 * /usr/lib/openoffice.org/basis3.0/program/libdbalx.so
1438 * I realized it really has to be handled as a
1439 * DW_TAG_function... Lets just get the types
1440 * for 1.8, then fix this properly.
1441 *
1442 * cu__tag_not_handled(die);
1443 */
1444 continue;
1445 case DW_TAG_inlined_subroutine:
1446 tag = die__create_new_inline_expansion(die, lexblock, cu);
1447 break;
1448 case DW_TAG_label:
1449 tag = die__create_new_label(die, lexblock, cu);
1450 break;
1451 default:
1452 tag = die__process_tag(die, cu, 0);
1453 if (tag == NULL)
1454 goto out_enomem;
1455
1456 if (tag == &unsupported_tag)
1457 continue;
1458
1459 if (cu__add_tag(cu, tag, &id) < 0)
1460 goto out_delete_tag;
1461 goto hash;
1462 }
1463
1464 if (tag == NULL)
1465 goto out_enomem;
1466
1467 if (cu__table_add_tag(cu, tag, &id) < 0)
1468 goto out_delete_tag;
1469 hash:
1470 cu__hash(cu, tag);
1471 struct dwarf_tag *dtag = tag->priv;
1472 dtag->small_id = id;
1473 } while (dwarf_siblingof(die, die) == 0);
1474
1475 return 0;
1476 out_delete_tag:
1477 tag__delete(tag, cu);
1478 out_enomem:
1479 return -ENOMEM;
1480 }
1481
die__create_new_inline_expansion(Dwarf_Die * die,struct lexblock * lexblock,struct cu * cu)1482 static struct tag *die__create_new_inline_expansion(Dwarf_Die *die,
1483 struct lexblock *lexblock,
1484 struct cu *cu)
1485 {
1486 struct inline_expansion *exp = inline_expansion__new(die, cu);
1487
1488 if (exp == NULL)
1489 return NULL;
1490
1491 if (die__process_inline_expansion(die, lexblock, cu) != 0) {
1492 obstack_free(&cu->obstack, exp);
1493 return NULL;
1494 }
1495
1496 if (lexblock != NULL)
1497 lexblock__add_inline_expansion(lexblock, exp);
1498 return &exp->ip.tag;
1499 }
1500
die__process_function(Dwarf_Die * die,struct ftype * ftype,struct lexblock * lexblock,struct cu * cu)1501 static int die__process_function(Dwarf_Die *die, struct ftype *ftype,
1502 struct lexblock *lexblock, struct cu *cu)
1503 {
1504 Dwarf_Die child;
1505 struct tag *tag;
1506
1507 if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
1508 return 0;
1509
1510 die = &child;
1511 do {
1512 uint32_t id;
1513
1514 switch (dwarf_tag(die)) {
1515 case DW_TAG_GNU_call_site:
1516 case DW_TAG_GNU_call_site_parameter:
1517 /*
1518 * XXX: read http://www.dwarfstd.org/ShowIssue.php?issue=100909.2&type=open
1519 * and write proper support.
1520 *
1521 * From a quick read there is not much we can use in
1522 * the existing dwarves tools, so just stop warning the user,
1523 * developers will find these notes if wanting to use in a
1524 * new tool.
1525 */
1526 continue;
1527 case DW_TAG_dwarf_procedure:
1528 /*
1529 * Ignore it, just scope expressions, that we have no use for (so far).
1530 */
1531 continue;
1532 #ifdef STB_GNU_UNIQUE
1533 case DW_TAG_GNU_formal_parameter_pack:
1534 case DW_TAG_GNU_template_parameter_pack:
1535 case DW_TAG_GNU_template_template_param:
1536 #endif
1537 case DW_TAG_template_type_parameter:
1538 case DW_TAG_template_value_parameter:
1539 /* FIXME: probably we'll have to attach this as a list of
1540 * template parameters to use at class__fprintf time...
1541 * See die__process_class */
1542 tag__print_not_supported(dwarf_tag(die));
1543 continue;
1544 case DW_TAG_formal_parameter:
1545 tag = die__create_new_parameter(die, ftype, lexblock, cu);
1546 break;
1547 case DW_TAG_variable:
1548 tag = die__create_new_variable(die, cu);
1549 if (tag == NULL)
1550 goto out_enomem;
1551 lexblock__add_variable(lexblock, tag__variable(tag));
1552 break;
1553 case DW_TAG_unspecified_parameters:
1554 if (ftype != NULL)
1555 ftype->unspec_parms = 1;
1556 continue;
1557 case DW_TAG_label:
1558 tag = die__create_new_label(die, lexblock, cu);
1559 break;
1560 case DW_TAG_inlined_subroutine:
1561 tag = die__create_new_inline_expansion(die, lexblock, cu);
1562 break;
1563 case DW_TAG_lexical_block:
1564 if (die__create_new_lexblock(die, cu, lexblock) != 0)
1565 goto out_enomem;
1566 continue;
1567 default:
1568 tag = die__process_tag(die, cu, 0);
1569
1570 if (tag == NULL)
1571 goto out_enomem;
1572
1573 if (tag == &unsupported_tag)
1574 continue;
1575
1576 if (cu__add_tag(cu, tag, &id) < 0)
1577 goto out_delete_tag;
1578
1579 goto hash;
1580 }
1581
1582 if (tag == NULL)
1583 goto out_enomem;
1584
1585 if (cu__table_add_tag(cu, tag, &id) < 0)
1586 goto out_delete_tag;
1587 hash:
1588 cu__hash(cu, tag);
1589 struct dwarf_tag *dtag = tag->priv;
1590 dtag->small_id = id;
1591 } while (dwarf_siblingof(die, die) == 0);
1592
1593 return 0;
1594 out_delete_tag:
1595 tag__delete(tag, cu);
1596 out_enomem:
1597 return -ENOMEM;
1598 }
1599
die__create_new_function(Dwarf_Die * die,struct cu * cu)1600 static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu)
1601 {
1602 struct function *function = function__new(die, cu);
1603
1604 if (function != NULL &&
1605 die__process_function(die, &function->proto,
1606 &function->lexblock, cu) != 0) {
1607 function__delete(function, cu);
1608 function = NULL;
1609 }
1610
1611 return function ? &function->proto.tag : NULL;
1612 }
1613
__die__process_tag(Dwarf_Die * die,struct cu * cu,int top_level,const char * fn)1614 static struct tag *__die__process_tag(Dwarf_Die *die, struct cu *cu,
1615 int top_level, const char *fn)
1616 {
1617 struct tag *tag;
1618
1619 switch (dwarf_tag(die)) {
1620 case DW_TAG_array_type:
1621 tag = die__create_new_array(die, cu); break;
1622 case DW_TAG_base_type:
1623 tag = die__create_new_base_type(die, cu); break;
1624 case DW_TAG_const_type:
1625 case DW_TAG_imported_declaration:
1626 case DW_TAG_imported_module:
1627 case DW_TAG_pointer_type:
1628 case DW_TAG_reference_type:
1629 case DW_TAG_restrict_type:
1630 case DW_TAG_unspecified_type:
1631 case DW_TAG_volatile_type:
1632 tag = die__create_new_tag(die, cu); break;
1633 case DW_TAG_ptr_to_member_type:
1634 tag = die__create_new_ptr_to_member_type(die, cu); break;
1635 case DW_TAG_enumeration_type:
1636 tag = die__create_new_enumeration(die, cu); break;
1637 case DW_TAG_namespace:
1638 tag = die__create_new_namespace(die, cu); break;
1639 case DW_TAG_class_type:
1640 case DW_TAG_interface_type:
1641 case DW_TAG_structure_type:
1642 tag = die__create_new_class(die, cu); break;
1643 case DW_TAG_subprogram:
1644 tag = die__create_new_function(die, cu); break;
1645 case DW_TAG_subroutine_type:
1646 tag = die__create_new_subroutine_type(die, cu); break;
1647 case DW_TAG_rvalue_reference_type:
1648 case DW_TAG_typedef:
1649 tag = die__create_new_typedef(die, cu); break;
1650 case DW_TAG_union_type:
1651 tag = die__create_new_union(die, cu); break;
1652 case DW_TAG_variable:
1653 tag = die__create_new_variable(die, cu); break;
1654 default:
1655 __cu__tag_not_handled(die, fn);
1656 /* fall thru */
1657 case DW_TAG_dwarf_procedure:
1658 /*
1659 * Ignore it, just scope expressions, that we have no use for (so far).
1660 */
1661 tag = &unsupported_tag;
1662 break;
1663 }
1664
1665 if (tag != NULL)
1666 tag->top_level = top_level;
1667
1668 return tag;
1669 }
1670
die__process_unit(Dwarf_Die * die,struct cu * cu)1671 static int die__process_unit(Dwarf_Die *die, struct cu *cu)
1672 {
1673 do {
1674 struct tag *tag = die__process_tag(die, cu, 1);
1675 if (tag == NULL)
1676 return -ENOMEM;
1677
1678 if (tag == &unsupported_tag)
1679 continue;
1680
1681 uint32_t id;
1682 cu__add_tag(cu, tag, &id);
1683 cu__hash(cu, tag);
1684 struct dwarf_tag *dtag = tag->priv;
1685 dtag->small_id = id;
1686 } while (dwarf_siblingof(die, die) == 0);
1687
1688 return 0;
1689 }
1690
__tag__print_type_not_found(struct tag * tag,const char * func)1691 static void __tag__print_type_not_found(struct tag *tag, const char *func)
1692 {
1693 struct dwarf_tag *dtag = tag->priv;
1694 fprintf(stderr, "%s: couldn't find %#llx type for %#llx (%s)!\n", func,
1695 (unsigned long long)dtag->type.off, (unsigned long long)dtag->id,
1696 dwarf_tag_name(tag->tag));
1697 }
1698
1699 #define tag__print_type_not_found(tag) \
1700 __tag__print_type_not_found(tag, __func__)
1701
1702 static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu);
1703
namespace__recode_dwarf_types(struct tag * tag,struct cu * cu)1704 static int namespace__recode_dwarf_types(struct tag *tag, struct cu *cu)
1705 {
1706 struct tag *pos;
1707 struct dwarf_cu *dcu = cu->priv;
1708 struct namespace *ns = tag__namespace(tag);
1709
1710 namespace__for_each_tag(ns, pos) {
1711 struct dwarf_tag *dtype;
1712 struct dwarf_tag *dpos = pos->priv;
1713
1714 if (tag__has_namespace(pos)) {
1715 if (namespace__recode_dwarf_types(pos, cu))
1716 return -1;
1717 continue;
1718 }
1719
1720 switch (pos->tag) {
1721 case DW_TAG_member: {
1722 struct class_member *member = tag__class_member(pos);
1723 /*
1724 * We may need to recode the type, possibly creating a
1725 * suitably sized new base_type
1726 */
1727 if (member->bitfield_size != 0 && !no_bitfield_type_recode) {
1728 if (class_member__dwarf_recode_bitfield(member, cu))
1729 return -1;
1730 continue;
1731 }
1732 }
1733 break;
1734 case DW_TAG_subroutine_type:
1735 case DW_TAG_subprogram:
1736 ftype__recode_dwarf_types(pos, cu);
1737 break;
1738 case DW_TAG_imported_module:
1739 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->type);
1740 goto check_type;
1741 /* Can be for both types and non types */
1742 case DW_TAG_imported_declaration:
1743 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->type);
1744 if (dtype != NULL)
1745 goto next;
1746 goto find_type;
1747 }
1748
1749 if (dpos->type.off == 0) /* void */
1750 continue;
1751 find_type:
1752 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
1753 check_type:
1754 if (dtype == NULL) {
1755 tag__print_type_not_found(pos);
1756 continue;
1757 }
1758 next:
1759 pos->type = dtype->small_id;
1760 }
1761 return 0;
1762 }
1763
type__recode_dwarf_specification(struct tag * tag,struct cu * cu)1764 static void type__recode_dwarf_specification(struct tag *tag, struct cu *cu)
1765 {
1766 struct dwarf_tag *dtype;
1767 struct type *t = tag__type(tag);
1768 dwarf_off_ref specification = dwarf_tag__spec(tag->priv);
1769
1770 if (t->namespace.name != 0 || specification.off == 0)
1771 return;
1772
1773 dtype = dwarf_cu__find_type_by_ref(cu->priv, &specification);
1774 if (dtype != NULL)
1775 t->namespace.name = tag__namespace(dtype->tag)->name;
1776 else {
1777 struct dwarf_tag *dtag = tag->priv;
1778
1779 fprintf(stderr,
1780 "%s: couldn't find name for "
1781 "class %#llx, specification=%#llx\n", __func__,
1782 (unsigned long long)dtag->id,
1783 (unsigned long long)specification.off);
1784 }
1785 }
1786
__tag__print_abstract_origin_not_found(struct tag * tag,const char * func)1787 static void __tag__print_abstract_origin_not_found(struct tag *tag,
1788 const char *func)
1789 {
1790 struct dwarf_tag *dtag = tag->priv;
1791 fprintf(stderr,
1792 "%s: couldn't find %#llx abstract_origin for %#llx (%s)!\n",
1793 func, (unsigned long long)dtag->abstract_origin.off,
1794 (unsigned long long)dtag->id,
1795 dwarf_tag_name(tag->tag));
1796 }
1797
1798 #define tag__print_abstract_origin_not_found(tag ) \
1799 __tag__print_abstract_origin_not_found(tag, __func__)
1800
ftype__recode_dwarf_types(struct tag * tag,struct cu * cu)1801 static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu)
1802 {
1803 struct parameter *pos;
1804 struct dwarf_cu *dcu = cu->priv;
1805 struct ftype *type = tag__ftype(tag);
1806
1807 ftype__for_each_parameter(type, pos) {
1808 struct dwarf_tag *dpos = pos->tag.priv;
1809 struct dwarf_tag *dtype;
1810
1811 if (dpos->type.off == 0) {
1812 if (dpos->abstract_origin.off == 0) {
1813 /* Function without parameters */
1814 pos->tag.type = 0;
1815 continue;
1816 }
1817 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->abstract_origin);
1818 if (dtype == NULL) {
1819 tag__print_abstract_origin_not_found(&pos->tag);
1820 continue;
1821 }
1822 pos->name = tag__parameter(dtype->tag)->name;
1823 pos->tag.type = dtype->tag->type;
1824 continue;
1825 }
1826
1827 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
1828 if (dtype == NULL) {
1829 tag__print_type_not_found(&pos->tag);
1830 continue;
1831 }
1832 pos->tag.type = dtype->small_id;
1833 }
1834 }
1835
lexblock__recode_dwarf_types(struct lexblock * tag,struct cu * cu)1836 static void lexblock__recode_dwarf_types(struct lexblock *tag, struct cu *cu)
1837 {
1838 struct tag *pos;
1839 struct dwarf_cu *dcu = cu->priv;
1840
1841 list_for_each_entry(pos, &tag->tags, node) {
1842 struct dwarf_tag *dpos = pos->priv;
1843 struct dwarf_tag *dtype;
1844
1845 switch (pos->tag) {
1846 case DW_TAG_lexical_block:
1847 lexblock__recode_dwarf_types(tag__lexblock(pos), cu);
1848 continue;
1849 case DW_TAG_inlined_subroutine:
1850 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->type);
1851 if (dtype == NULL) {
1852 tag__print_type_not_found(pos);
1853 continue;
1854 }
1855 ftype__recode_dwarf_types(dtype->tag, cu);
1856 continue;
1857
1858 case DW_TAG_formal_parameter:
1859 if (dpos->type.off != 0)
1860 break;
1861
1862 struct parameter *fp = tag__parameter(pos);
1863 dtype = dwarf_cu__find_tag_by_ref(dcu,
1864 &dpos->abstract_origin);
1865 if (dtype == NULL) {
1866 tag__print_abstract_origin_not_found(pos);
1867 continue;
1868 }
1869 fp->name = tag__parameter(dtype->tag)->name;
1870 pos->type = dtype->tag->type;
1871 continue;
1872
1873 case DW_TAG_variable:
1874 if (dpos->type.off != 0)
1875 break;
1876
1877 struct dw_variable *var = tag__variable(pos);
1878
1879 if (dpos->abstract_origin.off == 0) {
1880 /*
1881 * DW_TAG_variable completely empty was
1882 * found on libQtGui.so.4.3.4.debug
1883 * <3><d6ea1>: Abbrev Number: 164 (DW_TAG_variable)
1884 */
1885 continue;
1886 }
1887
1888 dtype = dwarf_cu__find_tag_by_ref(dcu,
1889 &dpos->abstract_origin);
1890 if (dtype == NULL) {
1891 tag__print_abstract_origin_not_found(pos);
1892 continue;
1893 }
1894 var->name = tag__variable(dtype->tag)->name;
1895 pos->type = dtype->tag->type;
1896 continue;
1897
1898 case DW_TAG_label: {
1899 struct label *l = tag__label(pos);
1900
1901 if (dpos->abstract_origin.off == 0)
1902 continue;
1903
1904 dtype = dwarf_cu__find_tag_by_ref(dcu, &dpos->abstract_origin);
1905 if (dtype != NULL)
1906 l->name = tag__label(dtype->tag)->name;
1907 else
1908 tag__print_abstract_origin_not_found(pos);
1909 }
1910 continue;
1911 }
1912
1913 dtype = dwarf_cu__find_type_by_ref(dcu, &dpos->type);
1914 if (dtype == NULL) {
1915 tag__print_type_not_found(pos);
1916 continue;
1917 }
1918 pos->type = dtype->small_id;
1919 }
1920 }
1921
tag__recode_dwarf_type(struct tag * tag,struct cu * cu)1922 static int tag__recode_dwarf_type(struct tag *tag, struct cu *cu)
1923 {
1924 struct dwarf_tag *dtag = tag->priv;
1925 struct dwarf_tag *dtype;
1926
1927 /* Check if this is an already recoded bitfield */
1928 if (dtag == NULL)
1929 return 0;
1930
1931 if (tag__is_type(tag))
1932 type__recode_dwarf_specification(tag, cu);
1933
1934 if (tag__has_namespace(tag))
1935 return namespace__recode_dwarf_types(tag, cu);
1936
1937 switch (tag->tag) {
1938 case DW_TAG_subprogram: {
1939 struct function *fn = tag__function(tag);
1940
1941 if (fn->name == 0) {
1942 dwarf_off_ref specification = dwarf_tag__spec(dtag);
1943 if (dtag->abstract_origin.off == 0 &&
1944 specification.off == 0) {
1945 /*
1946 * Found on libQtGui.so.4.3.4.debug
1947 * <3><1423de>: Abbrev Number: 209 (DW_TAG_subprogram)
1948 * <1423e0> DW_AT_declaration : 1
1949 */
1950 return 0;
1951 }
1952 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &dtag->abstract_origin);
1953 if (dtype == NULL)
1954 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &specification);
1955 if (dtype != NULL)
1956 fn->name = tag__function(dtype->tag)->name;
1957 else {
1958 fprintf(stderr,
1959 "%s: couldn't find name for "
1960 "function %#llx, abstract_origin=%#llx,"
1961 " specification=%#llx\n", __func__,
1962 (unsigned long long)dtag->id,
1963 (unsigned long long)dtag->abstract_origin.off,
1964 (unsigned long long)specification.off);
1965 }
1966 }
1967 lexblock__recode_dwarf_types(&fn->lexblock, cu);
1968 }
1969 /* Fall thru */
1970
1971 case DW_TAG_subroutine_type:
1972 ftype__recode_dwarf_types(tag, cu);
1973 /* Fall thru, for the function return type */
1974 break;
1975
1976 case DW_TAG_lexical_block:
1977 lexblock__recode_dwarf_types(tag__lexblock(tag), cu);
1978 return 0;
1979
1980 case DW_TAG_ptr_to_member_type: {
1981 struct ptr_to_member_type *pt = tag__ptr_to_member_type(tag);
1982
1983 dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->containing_type);
1984 if (dtype != NULL)
1985 pt->containing_type = dtype->small_id;
1986 else {
1987 fprintf(stderr,
1988 "%s: couldn't find type for "
1989 "containing_type %#llx, containing_type=%#llx\n",
1990 __func__,
1991 (unsigned long long)dtag->id,
1992 (unsigned long long)dtag->containing_type.off);
1993 }
1994 }
1995 break;
1996
1997 case DW_TAG_namespace:
1998 return namespace__recode_dwarf_types(tag, cu);
1999 /* Damn, DW_TAG_inlined_subroutine is an special case
2000 as dwarf_tag->id is in fact an abtract origin, i.e. must be
2001 looked up in the tags_table, not in the types_table.
2002 The others also point to routines, so are in tags_table */
2003 case DW_TAG_inlined_subroutine:
2004 case DW_TAG_imported_module:
2005 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &dtag->type);
2006 goto check_type;
2007 /* Can be for both types and non types */
2008 case DW_TAG_imported_declaration:
2009 dtype = dwarf_cu__find_tag_by_ref(cu->priv, &dtag->type);
2010 if (dtype != NULL)
2011 goto out;
2012 goto find_type;
2013 }
2014
2015 if (dtag->type.off == 0) {
2016 tag->type = 0; /* void */
2017 return 0;
2018 }
2019
2020 find_type:
2021 dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
2022 check_type:
2023 if (dtype == NULL) {
2024 tag__print_type_not_found(tag);
2025 return 0;
2026 }
2027 out:
2028 tag->type = dtype->small_id;
2029 return 0;
2030 }
2031
cu__recode_dwarf_types_table(struct cu * cu,struct ptr_table * pt,uint32_t i)2032 static int cu__recode_dwarf_types_table(struct cu *cu,
2033 struct ptr_table *pt,
2034 uint32_t i)
2035 {
2036 for (; i < pt->nr_entries; ++i) {
2037 struct tag *tag = pt->entries[i];
2038
2039 if (tag != NULL) /* void, see cu__new */
2040 if (tag__recode_dwarf_type(tag, cu))
2041 return -1;
2042 }
2043 return 0;
2044 }
2045
cu__recode_dwarf_types(struct cu * cu)2046 static int cu__recode_dwarf_types(struct cu *cu)
2047 {
2048 if (cu__recode_dwarf_types_table(cu, &cu->types_table, 1) ||
2049 cu__recode_dwarf_types_table(cu, &cu->tags_table, 0) ||
2050 cu__recode_dwarf_types_table(cu, &cu->functions_table, 0))
2051 return -1;
2052 return 0;
2053 }
2054
dwarf_tag__decl_file(const struct tag * tag,const struct cu * cu)2055 static const char *dwarf_tag__decl_file(const struct tag *tag,
2056 const struct cu *cu)
2057 {
2058 struct dwarf_tag *dtag = tag->priv;
2059 return cu->extra_dbg_info ?
2060 strings__ptr(strings, dtag->decl_file) : NULL;
2061 }
2062
dwarf_tag__decl_line(const struct tag * tag,const struct cu * cu)2063 static uint32_t dwarf_tag__decl_line(const struct tag *tag,
2064 const struct cu *cu)
2065 {
2066 struct dwarf_tag *dtag = tag->priv;
2067 return cu->extra_dbg_info ? dtag->decl_line : 0;
2068 }
2069
dwarf_tag__orig_id(const struct tag * tag,const struct cu * cu)2070 static unsigned long long dwarf_tag__orig_id(const struct tag *tag,
2071 const struct cu *cu)
2072 {
2073 struct dwarf_tag *dtag = tag->priv;
2074 return cu->extra_dbg_info ? dtag->id : 0;
2075 }
2076
dwarf__strings_ptr(const struct cu * cu __unused,strings_t s)2077 static const char *dwarf__strings_ptr(const struct cu *cu __unused,
2078 strings_t s)
2079 {
2080 return strings__ptr(strings, s);
2081 }
2082
2083 struct debug_fmt_ops dwarf__ops;
2084
die__process(Dwarf_Die * die,struct cu * cu)2085 static int die__process(Dwarf_Die *die, struct cu *cu)
2086 {
2087 Dwarf_Die child;
2088 const uint16_t tag = dwarf_tag(die);
2089
2090 if (tag != DW_TAG_compile_unit && tag != DW_TAG_type_unit && tag != DW_TAG_partial_unit) {
2091 fprintf(stderr, "%s: DW_TAG_compile_unit, DW_TAG_type_unit or DW_TAG_partial_unit expected got %s!\n",
2092 __FUNCTION__, dwarf_tag_name(tag));
2093 return -EINVAL;
2094 }
2095
2096 cu->language = attr_numeric(die, DW_AT_language);
2097
2098 if (dwarf_child(die, &child) == 0) {
2099 int err = die__process_unit(&child, cu);
2100 if (err)
2101 return err;
2102 }
2103
2104 if (dwarf_siblingof(die, die) == 0)
2105 fprintf(stderr, "%s: got %s unexpected tag after "
2106 "DW_TAG_compile_unit!\n",
2107 __FUNCTION__, dwarf_tag_name(tag));
2108
2109 return 0;
2110 }
2111
die__process_and_recode(Dwarf_Die * die,struct cu * cu)2112 static int die__process_and_recode(Dwarf_Die *die, struct cu *cu)
2113 {
2114 int ret = die__process(die, cu);
2115 if (ret != 0)
2116 return ret;
2117 return cu__recode_dwarf_types(cu);
2118 }
2119
class_member__cache_byte_size(struct tag * tag,struct cu * cu,void * cookie)2120 static int class_member__cache_byte_size(struct tag *tag, struct cu *cu,
2121 void *cookie)
2122 {
2123 struct class_member *member = tag__class_member(tag);
2124 struct conf_load *conf_load = cookie;
2125
2126 if (tag__is_class_member(tag)) {
2127 if (member->is_static)
2128 return 0;
2129 } else if (tag->tag != DW_TAG_inheritance) {
2130 return 0;
2131 }
2132
2133 if (member->bitfield_size == 0) {
2134 member->byte_size = tag__size(tag, cu);
2135 member->bit_size = member->byte_size * 8;
2136 return 0;
2137 }
2138
2139 /*
2140 * Try to figure out byte size, if it's not directly provided in DWARF
2141 */
2142 if (member->byte_size == 0) {
2143 struct tag *type = tag__strip_typedefs_and_modifiers(&member->tag, cu);
2144 member->byte_size = tag__size(type, cu);
2145 if (member->byte_size == 0) {
2146 int bit_size;
2147 if (tag__is_enumeration(type)) {
2148 bit_size = tag__type(type)->size;
2149 } else {
2150 struct base_type *bt = tag__base_type(type);
2151 bit_size = bt->bit_size ? bt->bit_size : base_type__name_to_size(bt, cu);
2152 }
2153 member->byte_size = (bit_size + 7) / 8 * 8;
2154 }
2155 }
2156 member->bit_size = member->byte_size * 8;
2157
2158 /*
2159 * XXX: after all the attemps to determine byte size, we might still
2160 * be unsuccessful, because base_type__name_to_size doesn't know about
2161 * the base_type name, so one has to add there when such base_type
2162 * isn't found. pahole will put zero on the struct output so it should
2163 * be easy to spot the name when such unlikely thing happens.
2164 */
2165 if (member->byte_size == 0) {
2166 member->bitfield_offset = 0;
2167 return 0;
2168 }
2169
2170 /*
2171 * For little-endian architectures, DWARF data emitted by gcc/clang
2172 * specifies bitfield offset as an offset from the highest-order bit
2173 * of an underlying integral type (e.g., int) to a highest-order bit
2174 * of a bitfield. E.g., for bitfield taking first 5 bits of int-backed
2175 * bitfield, bit offset will be 27 (sizeof(int) - 0 offset - 5 bit
2176 * size), which is very counter-intuitive and isn't a natural
2177 * extension of byte offset, which on little-endian points to
2178 * lowest-order byte. So here we re-adjust bitfield offset to be an
2179 * offset from lowest-order bit of underlying integral type to
2180 * a lowest-order bit of a bitfield. This makes bitfield offset
2181 * a natural extension of byte offset for bitfields and is uniform
2182 * with how big-endian bit offsets work.
2183 */
2184 if (cu->little_endian) {
2185 member->bitfield_offset = member->bit_size - member->bitfield_offset - member->bitfield_size;
2186 }
2187 member->bit_offset = member->byte_offset * 8 + member->bitfield_offset;
2188
2189 /* make sure bitfield offset is non-negative */
2190 if (member->bitfield_offset < 0) {
2191 member->bitfield_offset += member->bit_size;
2192 member->byte_offset -= member->byte_size;
2193 member->bit_offset = member->byte_offset * 8 + member->bitfield_offset;
2194 }
2195 /* align on underlying base type natural alignment boundary */
2196 member->bitfield_offset += (member->byte_offset % member->byte_size) * 8;
2197 member->byte_offset = member->bit_offset / member->bit_size * member->bit_size / 8;
2198 if (member->bitfield_offset >= member->bit_size) {
2199 member->bitfield_offset -= member->bit_size;
2200 member->byte_offset += member->byte_size;
2201 }
2202
2203 if (conf_load && conf_load->fixup_silly_bitfields &&
2204 member->byte_size == 8 * member->bitfield_size) {
2205 member->bitfield_size = 0;
2206 member->bitfield_offset = 0;
2207 }
2208
2209 return 0;
2210 }
2211
finalize_cu(struct cus * cus,struct cu * cu,struct dwarf_cu * dcu,struct conf_load * conf)2212 static int finalize_cu(struct cus *cus, struct cu *cu, struct dwarf_cu *dcu,
2213 struct conf_load *conf)
2214 {
2215 base_type_name_to_size_table__init(strings);
2216 cu__for_all_tags(cu, class_member__cache_byte_size, conf);
2217 if (conf && conf->steal) {
2218 return conf->steal(cu, conf);
2219 }
2220 return LSK__KEEPIT;
2221 }
2222
finalize_cu_immediately(struct cus * cus,struct cu * cu,struct dwarf_cu * dcu,struct conf_load * conf)2223 static int finalize_cu_immediately(struct cus *cus, struct cu *cu,
2224 struct dwarf_cu *dcu,
2225 struct conf_load *conf)
2226 {
2227 int lsk = finalize_cu(cus, cu, dcu, conf);
2228 switch (lsk) {
2229 case LSK__DELETE:
2230 cu__delete(cu);
2231 break;
2232 case LSK__STOP_LOADING:
2233 break;
2234 case LSK__KEEPIT:
2235 if (!cu->extra_dbg_info)
2236 obstack_free(&dcu->obstack, NULL);
2237 cus__add(cus, cu);
2238 break;
2239 }
2240 return lsk;
2241 }
2242
cus__load_debug_types(struct cus * cus,struct conf_load * conf,Dwfl_Module * mod,Dwarf * dw,Elf * elf,const char * filename,const unsigned char * build_id,int build_id_len,struct cu ** cup,struct dwarf_cu * dcup)2243 static int cus__load_debug_types(struct cus *cus, struct conf_load *conf,
2244 Dwfl_Module *mod, Dwarf *dw, Elf *elf,
2245 const char *filename,
2246 const unsigned char *build_id,
2247 int build_id_len,
2248 struct cu **cup, struct dwarf_cu *dcup)
2249 {
2250 Dwarf_Off off = 0, noff, type_off;
2251 size_t cuhl;
2252 uint8_t pointer_size, offset_size;
2253 uint64_t signature;
2254
2255 *cup = NULL;
2256
2257 while (dwarf_next_unit(dw, off, &noff, &cuhl, NULL, NULL, &pointer_size,
2258 &offset_size, &signature, &type_off)
2259 == 0) {
2260
2261 if (*cup == NULL) {
2262 struct cu *cu;
2263
2264 cu = cu__new("", pointer_size, build_id,
2265 build_id_len, filename);
2266 if (cu == NULL) {
2267 return DWARF_CB_ABORT;
2268 }
2269
2270 cu->uses_global_strings = true;
2271 cu->elf = elf;
2272 cu->dwfl = mod;
2273 cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0;
2274 cu->has_addr_info = conf ? conf->get_addr_info : 0;
2275
2276 GElf_Ehdr ehdr;
2277 if (gelf_getehdr(elf, &ehdr) == NULL) {
2278 return DWARF_CB_ABORT;
2279 }
2280 cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB;
2281
2282 dwarf_cu__init(dcup);
2283 dcup->cu = cu;
2284 /* Funny hack. */
2285 dcup->type_unit = dcup;
2286 cu->priv = dcup;
2287 cu->dfops = &dwarf__ops;
2288
2289 *cup = cu;
2290 }
2291
2292 Dwarf_Die die_mem;
2293 Dwarf_Die *cu_die = dwarf_offdie_types(dw, off + cuhl,
2294 &die_mem);
2295
2296 if (die__process(cu_die, *cup) != 0)
2297 return DWARF_CB_ABORT;
2298
2299 off = noff;
2300 }
2301
2302 if (*cup != NULL && cu__recode_dwarf_types(*cup) != 0)
2303 return DWARF_CB_ABORT;
2304
2305 return 0;
2306 }
2307
cus__load_module(struct cus * cus,struct conf_load * conf,Dwfl_Module * mod,Dwarf * dw,Elf * elf,const char * filename)2308 static int cus__load_module(struct cus *cus, struct conf_load *conf,
2309 Dwfl_Module *mod, Dwarf *dw, Elf *elf,
2310 const char *filename)
2311 {
2312 Dwarf_Off off = 0, noff;
2313 size_t cuhl;
2314 GElf_Addr vaddr;
2315 const unsigned char *build_id = NULL;
2316 uint8_t pointer_size, offset_size;
2317
2318 #ifdef HAVE_DWFL_MODULE_BUILD_ID
2319 int build_id_len = dwfl_module_build_id(mod, &build_id, &vaddr);
2320 #else
2321 int build_id_len = 0;
2322 #endif
2323
2324 struct cu *type_cu;
2325 struct dwarf_cu type_dcu;
2326 int type_lsk = LSK__KEEPIT;
2327
2328 int res = cus__load_debug_types(cus, conf, mod, dw, elf, filename,
2329 build_id, build_id_len,
2330 &type_cu, &type_dcu);
2331 if (res != 0) {
2332 return res;
2333 }
2334
2335 if (type_cu != NULL) {
2336 type_lsk = finalize_cu(cus, type_cu, &type_dcu, conf);
2337 if (type_lsk == LSK__KEEPIT) {
2338 cus__add(cus, type_cu);
2339 }
2340 }
2341
2342 while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size,
2343 &offset_size) == 0) {
2344 Dwarf_Die die_mem;
2345 Dwarf_Die *cu_die = dwarf_offdie(dw, off + cuhl, &die_mem);
2346
2347 /*
2348 * DW_AT_name in DW_TAG_compile_unit can be NULL, first
2349 * seen in:
2350 * /usr/libexec/gcc/x86_64-redhat-linux/4.3.2/ecj1.debug
2351 */
2352 const char *name = attr_string(cu_die, DW_AT_name);
2353 struct cu *cu = cu__new(name ?: "", pointer_size,
2354 build_id, build_id_len, filename);
2355 if (cu == NULL)
2356 return DWARF_CB_ABORT;
2357 cu->uses_global_strings = true;
2358 cu->elf = elf;
2359 cu->dwfl = mod;
2360 cu->extra_dbg_info = conf ? conf->extra_dbg_info : 0;
2361 cu->has_addr_info = conf ? conf->get_addr_info : 0;
2362
2363 GElf_Ehdr ehdr;
2364 if (gelf_getehdr(elf, &ehdr) == NULL) {
2365 return DWARF_CB_ABORT;
2366 }
2367 cu->little_endian = ehdr.e_ident[EI_DATA] == ELFDATA2LSB;
2368
2369 struct dwarf_cu dcu;
2370
2371 dwarf_cu__init(&dcu);
2372 dcu.cu = cu;
2373 dcu.type_unit = type_cu ? &type_dcu : NULL;
2374 cu->priv = &dcu;
2375 cu->dfops = &dwarf__ops;
2376
2377 if (die__process_and_recode(cu_die, cu) != 0)
2378 return DWARF_CB_ABORT;
2379
2380 if (finalize_cu_immediately(cus, cu, &dcu, conf)
2381 == LSK__STOP_LOADING)
2382 return DWARF_CB_ABORT;
2383
2384 off = noff;
2385 }
2386
2387 if (type_lsk == LSK__DELETE)
2388 cu__delete(type_cu);
2389
2390 return DWARF_CB_OK;
2391 }
2392
2393 struct process_dwflmod_parms {
2394 struct cus *cus;
2395 struct conf_load *conf;
2396 const char *filename;
2397 uint32_t nr_dwarf_sections_found;
2398 };
2399
cus__process_dwflmod(Dwfl_Module * dwflmod,void ** userdata __unused,const char * name __unused,Dwarf_Addr base __unused,void * arg)2400 static int cus__process_dwflmod(Dwfl_Module *dwflmod,
2401 void **userdata __unused,
2402 const char *name __unused,
2403 Dwarf_Addr base __unused,
2404 void *arg)
2405 {
2406 struct process_dwflmod_parms *parms = arg;
2407 struct cus *cus = parms->cus;
2408
2409 GElf_Addr dwflbias;
2410 /*
2411 * Does the relocation and saves the elf for later processing
2412 * by the stealer, such as pahole_stealer, so that it don't
2413 * have to create another Elf instance just to do things like
2414 * reading this ELF file symtab to do CTF encoding of the
2415 * DW_TAG_suprogram tags (functions).
2416 */
2417 Elf *elf = dwfl_module_getelf(dwflmod, &dwflbias);
2418
2419 Dwarf_Addr dwbias;
2420 Dwarf *dw = dwfl_module_getdwarf(dwflmod, &dwbias);
2421
2422 int err = DWARF_CB_OK;
2423 if (dw != NULL) {
2424 ++parms->nr_dwarf_sections_found;
2425 err = cus__load_module(cus, parms->conf, dwflmod, dw, elf,
2426 parms->filename);
2427 }
2428 /*
2429 * XXX We will fall back to try finding other debugging
2430 * formats (CTF), so no point in telling this to the user
2431 * Use for debugging.
2432 * else
2433 * fprintf(stderr,
2434 * "%s: can't get debug context descriptor: %s\n",
2435 * __func__, dwfl_errmsg(-1));
2436 */
2437
2438 return err;
2439 }
2440
cus__process_file(struct cus * cus,struct conf_load * conf,int fd,const char * filename)2441 static int cus__process_file(struct cus *cus, struct conf_load *conf, int fd,
2442 const char *filename)
2443 {
2444 /* Duplicate an fd for dwfl_report_offline to swallow. */
2445 int dwfl_fd = dup(fd);
2446
2447 if (dwfl_fd < 0)
2448 return -1;
2449
2450 /*
2451 * Use libdwfl in a trivial way to open the libdw handle for us.
2452 * This takes care of applying relocations to DWARF data in ET_REL
2453 * files.
2454 */
2455
2456 static const Dwfl_Callbacks callbacks = {
2457 .section_address = dwfl_offline_section_address,
2458 .find_debuginfo = dwfl_standard_find_debuginfo,
2459 /* We use this table for core files too. */
2460 .find_elf = dwfl_build_id_find_elf,
2461 };
2462
2463 Dwfl *dwfl = dwfl_begin(&callbacks);
2464
2465 if (dwfl_report_offline(dwfl, filename, filename, dwfl_fd) == NULL)
2466 return -1;
2467
2468 dwfl_report_end(dwfl, NULL, NULL);
2469
2470 struct process_dwflmod_parms parms = {
2471 .cus = cus,
2472 .conf = conf,
2473 .filename = filename,
2474 .nr_dwarf_sections_found = 0,
2475 };
2476
2477 /* Process the one or more modules gleaned from this file. */
2478 dwfl_getmodules(dwfl, cus__process_dwflmod, &parms, 0);
2479 dwfl_end(dwfl);
2480 return parms.nr_dwarf_sections_found ? 0 : -1;
2481 }
2482
dwarf__load_file(struct cus * cus,struct conf_load * conf,const char * filename)2483 static int dwarf__load_file(struct cus *cus, struct conf_load *conf,
2484 const char *filename)
2485 {
2486 int fd, err;
2487
2488 elf_version(EV_CURRENT);
2489
2490 fd = open(filename, O_RDONLY);
2491
2492 if (fd == -1)
2493 return -1;
2494
2495 err = cus__process_file(cus, conf, fd, filename);
2496 close(fd);
2497
2498 return err;
2499 }
2500
dwarf__init(void)2501 static int dwarf__init(void)
2502 {
2503 strings = strings__new();
2504 return strings != NULL ? 0 : -ENOMEM;
2505 }
2506
dwarf__exit(void)2507 static void dwarf__exit(void)
2508 {
2509 strings__delete(strings);
2510 strings = NULL;
2511 }
2512
2513 struct debug_fmt_ops dwarf__ops = {
2514 .name = "dwarf",
2515 .init = dwarf__init,
2516 .exit = dwarf__exit,
2517 .load_file = dwarf__load_file,
2518 .strings__ptr = dwarf__strings_ptr,
2519 .tag__decl_file = dwarf_tag__decl_file,
2520 .tag__decl_line = dwarf_tag__decl_line,
2521 .tag__orig_id = dwarf_tag__orig_id,
2522 .has_alignment_info = true,
2523 };
2524