1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2022 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7
8 /// @file
9 ///
10 /// This file contains the definitions of the entry points to
11 /// de-serialize an instance of @ref abigail::corpus from a file in
12 /// elf format, containing dwarf information.
13
14 #include "abg-internal.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <libgen.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include <elfutils/libdwfl.h>
23 #include <dwarf.h>
24 #include <algorithm>
25 #include <cmath>
26 #include <cstring>
27 #include <deque>
28 #include <list>
29 #include <memory>
30 #include <ostream>
31 #include <sstream>
32 #include <stack>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <map>
36
37 #include "abg-ir-priv.h"
38 #include "abg-suppression-priv.h"
39 #include "abg-corpus-priv.h"
40 #include "abg-symtab-reader.h"
41
42 // <headers defining libabigail's API go under here>
43 ABG_BEGIN_EXPORT_DECLARATIONS
44
45 #include "abg-dwarf-reader.h"
46 #include "abg-elf-based-reader.h"
47 #include "abg-sptr-utils.h"
48 #include "abg-tools-utils.h"
49 #include "abg-elf-helpers.h"
50
51 ABG_END_EXPORT_DECLARATIONS
52 // </headers defining libabigail's API>
53
54 #ifndef UINT64_MAX
55 #define UINT64_MAX 0xffffffffffffffff
56 #endif
57
58 using std::string;
59
60 namespace abigail
61 {
62
63 using std::cerr;
64
65 /// The namespace for the DWARF reader.
66 namespace dwarf
67 {
68
69 using std::dynamic_pointer_cast;
70 using std::static_pointer_cast;
71 using std::unordered_map;
72 using std::unordered_set;
73 using std::stack;
74 using std::deque;
75 using std::list;
76 using std::map;
77 using abg_compat::optional;
78
79 using namespace elf_helpers; // TODO: avoid using namespace
80
81 /// Where a DIE comes from. For instance, a DIE can come from the main
82 /// debug info section, the alternate debug info section or from the
83 /// type unit section.
84 enum die_source
85 {
86 NO_DEBUG_INFO_DIE_SOURCE,
87 PRIMARY_DEBUG_INFO_DIE_SOURCE,
88 ALT_DEBUG_INFO_DIE_SOURCE,
89 TYPE_UNIT_DIE_SOURCE,
90 NUMBER_OF_DIE_SOURCES, // This one must always be the latest
91 // enumerator
92 };
93
94
95 /// A convenience typedef for a vector of Dwarf_Off.
96 typedef vector<Dwarf_Off> dwarf_offsets_type;
97
98 /// Convenience typedef for a map which key is the offset of a dwarf
99 /// die and which value is the corresponding artefact.
100 typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
101
102 /// Convenience typedef for a map which key is the offset of a dwarf
103 /// die, (given by dwarf_dieoffset()) and which value is the
104 /// corresponding class_decl.
105 typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
106
107 /// Convenience typedef for a map which key is the offset of a dwarf
108 /// die, (given by dwarf_dieoffset()) and which value is the
109 /// corresponding class_or_union_sptr.
110 typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
111
112 /// Convenience typedef for a map which key the offset of a dwarf die
113 /// and which value is the corresponding function_decl.
114 typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
115
116 /// Convenience typedef for a map which key is the offset of a dwarf
117 /// die and which value is the corresponding function_type.
118 typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
119
120 /// Convenience typedef for a map which key is the offset of a
121 /// DW_TAG_compile_unit and the value is the corresponding @ref
122 /// translation_unit_sptr.
123 typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
124
125 /// Convenience typedef for a map which key is the offset of a DIE and
126 /// the value is the corresponding qualified name of the DIE.
127 typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
128
129 /// Convenience typedef for a map which is an interned_string and
130 /// which value is a vector of offsets.
131 typedef unordered_map<interned_string,
132 dwarf_offsets_type,
133 hash_interned_string>
134 istring_dwarf_offsets_map_type;
135
136 /// A hasher for a pair of Dwarf_Off. This is used as a hasher for
137 /// the type @ref dwarf_offset_pair_set_type.
138 struct dwarf_offset_pair_hash
139 {
140 size_t
operator ()abigail::dwarf::dwarf_offset_pair_hash141 operator()(const std::pair<Dwarf_Off, Dwarf_Off>& p) const
142 {return abigail::hashing::combine_hashes(p.first, p.second);}
143 };// end struct dwarf_offset_pair_hash
144
145 typedef unordered_set<std::pair<Dwarf_Off,
146 Dwarf_Off>,
147 dwarf_offset_pair_hash> dwarf_offset_pair_set_type;
148
149 /// An abstraction of a DIE offset that also encapsulate the source of
150 /// the DIE.
151 struct offset_type
152 {
153 die_source source_;
154 Dwarf_Off offset_;
155
offset_typeabigail::dwarf::offset_type156 offset_type()
157 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
158 offset_(0)
159 {}
160
offset_typeabigail::dwarf::offset_type161 offset_type(die_source source, Dwarf_Off offset)
162 : source_(source),
163 offset_(offset)
164 {}
165
offset_typeabigail::dwarf::offset_type166 offset_type(Dwarf_Off offset)
167 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
168 offset_(offset)
169 {}
170
operator ==abigail::dwarf::offset_type171 bool operator==(const offset_type& o) const
172 {return source_ == o.source_ && offset_ == o.offset_;}
173
operator Dwarf_Offabigail::dwarf::offset_type174 operator Dwarf_Off() const
175 {return offset_;}
176 }; // end struct offset_type
177
178 /// A convenience typedef for a pair of offset_type.
179 typedef std::pair<offset_type, offset_type> offset_pair_type;
180
181 /// A hasher for an instance of offset_type.
182 struct offset_hash
183 {
184 size_t
operator ()abigail::dwarf::offset_hash185 operator()(const offset_type& p) const
186 {return abigail::hashing::combine_hashes(p.source_, p.offset_);}
187 };// end struct offset_hash
188
189 /// A hasher for a pair of offset_type. This is used as a hasher for
190 /// the type @ref offset_pair_set_type, for instance.
191 struct offset_pair_hash
192 {
193 size_t
operator ()abigail::dwarf::offset_pair_hash194 operator()(const std::pair<offset_type, offset_type>& p) const
195 {
196 size_t h1 = abigail::hashing::combine_hashes(p.first.source_,
197 p.first.offset_);
198 size_t h2 = abigail::hashing::combine_hashes(p.second.source_,
199 p.second.offset_);
200 return abigail::hashing::combine_hashes(h1, h2);
201 }
202 };// end struct offset_pair_hash
203
204 /// A convenience typedef for an unordered set of DIE offsets.
205 typedef unordered_set<offset_type, offset_hash> offset_set_type;
206
207 ///A convenience typedef for an unordered set of pairs of offset_type.
208 typedef unordered_set<std::pair<offset_type,
209 offset_type>,
210 offset_pair_hash> offset_pair_set_type;
211
212 /// A convenience typedef for a vector of pairs of offset_type.
213 typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
214
215 /// A convenience typedef for an unordered map that associates a pair
216 /// of offset_type to a vector of pairs offset_type.
217 typedef unordered_map<std::pair<offset_type, offset_type>,
218 offset_pair_vector_type,
219 offset_pair_hash> offset_pair_vect_map_type;
220
221 /// A convenience typedef for an unordered_map that associates a pair
222 /// of offset_type to a set of pairs of offset_type.
223 typedef unordered_map<std::pair<offset_type, offset_type>,
224 offset_pair_set_type,
225 offset_pair_hash> offset_pair_set_map_type;
226
227 /// A convenience typedef for a vector of pairs of offset_type.
228 typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
229
230 class reader;
231
232 static translation_unit_sptr
233 build_translation_unit_and_add_to_ir(reader& rdr,
234 Dwarf_Die* die,
235 char address_size);
236
237 static void
238 maybe_propagate_canonical_type(const reader& rdr,
239 const Dwarf_Die* l,
240 const Dwarf_Die* r);
241
242 static void
243 propagate_canonical_type(const reader& rdr,
244 const Dwarf_Die* l,
245 const Dwarf_Die* r);
246
247 /// Convenience typedef for a shared pointer to an
248 /// addr_elf_symbol_sptr_map_type.
249 typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
250
251 /// Convenience typedef for a map that associates an @ref
252 /// interned_string to a @ref function_type_sptr.
253 typedef unordered_map<interned_string,
254 function_type_sptr,
255 hash_interned_string> istring_fn_type_map_type;
256
257 /// Convenience typedef for a stack containing the scopes up to the
258 /// current point in the abigail Internal Representation (aka IR) tree
259 /// that is being built.
260 typedef stack<scope_decl*> scope_stack_type;
261
262 /// Convenience typedef for a map which key is a dwarf offset. The
263 /// value is also a dwarf offset.
264 typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
265
266 /// Convenience typedef for a map which key is a string and which
267 /// value is a vector of smart pointer to a class.
268 typedef unordered_map<string, classes_type> string_classes_map;
269
270 /// Convenience typedef for a map which key is a string and which
271 /// value is a vector of smart pointer to a enum.
272 typedef unordered_map<string, enums_type> string_enums_map;
273
274 /// The abstraction of the place where a partial unit has been
275 /// imported. This is what the DW_TAG_imported_unit DIE expresses.
276 ///
277 /// This type thus contains:
278 /// - the offset to which the partial unit is imported
279 /// - the offset of the imported partial unit.
280 /// - the offset of the imported partial unit.
281 struct imported_unit_point
282 {
283 Dwarf_Off offset_of_import;
284 // The boolean below is true iff the imported unit comes from the
285 // alternate debug info file.
286 die_source imported_unit_die_source;
287 Dwarf_Off imported_unit_die_off;
288 Dwarf_Off imported_unit_cu_off;
289 Dwarf_Off imported_unit_child_off;
290
291 /// Default constructor for @ref the type imported_unit_point.
imported_unit_pointabigail::dwarf::imported_unit_point292 imported_unit_point()
293 : offset_of_import(),
294 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
295 imported_unit_die_off(),
296 imported_unit_cu_off(),
297 imported_unit_child_off()
298 {}
299
300 /// Constructor of @ref the type imported_unit_point.
301 ///
302 /// @param import_off the offset of the point at which the unit has
303 /// been imported.
imported_unit_pointabigail::dwarf::imported_unit_point304 imported_unit_point(Dwarf_Off import_off)
305 : offset_of_import(import_off),
306 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
307 imported_unit_die_off(),
308 imported_unit_cu_off(),
309 imported_unit_child_off()
310 {}
311
312 /// Constructor of @ref the type imported_unit_point.
313 ///
314 /// @param import_off the offset of the point at which the unit has
315 /// been imported.
316 ///
317 /// @param from where the imported DIE comes from.
318 ///
319 /// @param imported_die the die of the unit that has been imported.
imported_unit_pointabigail::dwarf::imported_unit_point320 imported_unit_point(Dwarf_Off import_off,
321 const Dwarf_Die& imported_die,
322 die_source from)
323 : offset_of_import(import_off),
324 imported_unit_die_source(from),
325 imported_unit_die_off(dwarf_dieoffset
326 (const_cast<Dwarf_Die*>(&imported_die))),
327 imported_unit_cu_off(),
328 imported_unit_child_off()
329 {
330 Dwarf_Die imported_unit_child;
331
332 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
333 &imported_unit_child) == 0);
334
335 imported_unit_child_off =
336 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
337
338 Dwarf_Die cu_die_memory;
339 Dwarf_Die *cu_die;
340
341 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
342 &cu_die_memory, 0, 0);
343 imported_unit_cu_off = dwarf_dieoffset(cu_die);
344 }
345 }; // struct imported_unit_point
346
347 /// Convenience typedef for a vector of @ref imported_unit_point.
348 typedef vector<imported_unit_point> imported_unit_points_type;
349
350 /// Convenience typedef for a vector of @ref imported_unit_point.
351 typedef unordered_map<Dwarf_Off, imported_unit_points_type>
352 tu_die_imported_unit_points_map_type;
353
354 /// "Less than" operator for instances of @ref imported_unit_point
355 /// type.
356 ///
357 /// @param the left hand side operand of the "Less than" operator.
358 ///
359 /// @param the right hand side operand of the "Less than" operator.
360 ///
361 /// @return true iff @p l is less than @p r.
362 static bool
operator <(const imported_unit_point & l,const imported_unit_point & r)363 operator<(const imported_unit_point& l, const imported_unit_point& r)
364 {return l.offset_of_import < r.offset_of_import;}
365
366 static bool
367 get_parent_die(const reader& rdr,
368 const Dwarf_Die* die,
369 Dwarf_Die& parent_die,
370 size_t where_offset);
371
372 static bool
373 get_scope_die(const reader& rdr,
374 const Dwarf_Die* die,
375 size_t where_offset,
376 Dwarf_Die& scope_die);
377
378 static bool
379 die_is_anonymous(const Dwarf_Die* die);
380
381 static bool
382 die_is_type(const Dwarf_Die* die);
383
384 static bool
385 die_is_decl(const Dwarf_Die* die);
386
387 static bool
388 die_is_declaration_only(Dwarf_Die* die);
389
390 static bool
391 die_is_variable_decl(const Dwarf_Die *die);
392
393 static bool
394 die_is_function_decl(const Dwarf_Die *die);
395
396 static bool
397 die_has_size_attribute(const Dwarf_Die *die);
398
399 static bool
400 die_has_no_child(const Dwarf_Die *die);
401
402 static bool
403 die_is_namespace(const Dwarf_Die* die);
404
405 static bool
406 die_is_unspecified(Dwarf_Die* die);
407
408 static bool
409 die_is_void_type(Dwarf_Die* die);
410
411 static bool
412 die_is_pointer_type(const Dwarf_Die* die);
413
414 static bool
415 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
416
417 static bool
418 die_is_reference_type(const Dwarf_Die* die);
419
420 static bool
421 die_is_pointer_or_reference_type(const Dwarf_Die* die);
422
423 static bool
424 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
425
426 static bool
427 die_is_class_type(const Dwarf_Die* die);
428
429 static bool
430 die_is_qualified_type(const Dwarf_Die* die);
431
432 static bool
433 die_is_function_type(const Dwarf_Die *die);
434
435 static bool
436 die_has_object_pointer(const Dwarf_Die* die,
437 Dwarf_Die& object_pointer);
438
439 static bool
440 die_has_children(const Dwarf_Die* die);
441
442 static bool
443 die_this_pointer_from_object_pointer(Dwarf_Die* die,
444 Dwarf_Die& this_pointer);
445
446 static bool
447 die_this_pointer_is_const(Dwarf_Die* die);
448
449 static bool
450 die_object_pointer_is_for_const_method(Dwarf_Die* die);
451
452 static bool
453 is_type_die_to_be_canonicalized(const Dwarf_Die *die);
454
455 static bool
456 die_is_at_class_scope(const reader& rdr,
457 const Dwarf_Die* die,
458 size_t where_offset,
459 Dwarf_Die& class_scope_die);
460 static bool
461 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
462 size_t expr_len,
463 int64_t& value,
464 bool& is_tls_address);
465
466 static translation_unit::language
467 dwarf_language_to_tu_language(size_t l);
468
469 static bool
470 die_unsigned_constant_attribute(const Dwarf_Die* die,
471 unsigned attr_name,
472 uint64_t& cst);
473
474 static bool
475 die_signed_constant_attribute(const Dwarf_Die*die,
476 unsigned attr_name,
477 int64_t& cst);
478
479 static bool
480 die_constant_attribute(const Dwarf_Die *die,
481 unsigned attr_name,
482 bool is_signed,
483 array_type_def::subrange_type::bound_value &value);
484
485 static bool
486 form_is_DW_FORM_strx(unsigned form);
487
488 static bool
489 form_is_DW_FORM_line_strp(unsigned form);
490
491 static bool
492 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
493
494 static string
495 die_name(const Dwarf_Die* die);
496
497 static location
498 die_location(const reader& rdr, const Dwarf_Die* die);
499
500 static bool
501 die_location_address(Dwarf_Die* die,
502 Dwarf_Addr& address,
503 bool& is_tls_address);
504
505 static bool
506 die_die_attribute(const Dwarf_Die* die,
507 unsigned attr_name,
508 Dwarf_Die& result,
509 bool recursively = true);
510
511 static string
512 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
513
514 static string
515 build_internal_anonymous_die_name(const string &base_name,
516 size_t anonymous_type_index);
517
518 static string
519 get_internal_anonymous_die_name(Dwarf_Die *die,
520 size_t anonymous_type_index);
521
522 static string
523 die_qualified_type_name(const reader& rdr,
524 const Dwarf_Die* die,
525 size_t where);
526
527 static string
528 die_qualified_decl_name(const reader& rdr,
529 const Dwarf_Die* die,
530 size_t where);
531
532 static string
533 die_qualified_name(const reader& rdr,
534 const Dwarf_Die* die,
535 size_t where);
536
537 static bool
538 die_qualified_type_name_empty(const reader& rdr,
539 const Dwarf_Die* die, size_t where,
540 string &qualified_name);
541
542 static void
543 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
544 const Dwarf_Die* die,
545 size_t where_offset,
546 bool pretty_print,
547 string &return_type_name,
548 string &class_name,
549 vector<string>& parm_names,
550 bool& is_const,
551 bool& is_static);
552
553 static string
554 die_function_signature(const reader& rdr,
555 const Dwarf_Die *die,
556 size_t where_offset);
557
558 static bool
559 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
560
561 static bool
562 die_function_type_is_method_type(const reader& rdr,
563 const Dwarf_Die *die,
564 size_t where_offset,
565 Dwarf_Die& object_pointer_die,
566 Dwarf_Die& class_die,
567 bool& is_static);
568
569 static string
570 die_pretty_print_type(reader& rdr,
571 const Dwarf_Die* die,
572 size_t where_offset);
573
574 static string
575 die_pretty_print_decl(reader& rdr,
576 const Dwarf_Die* die,
577 size_t where_offset);
578
579 static string
580 die_pretty_print(reader& rdr,
581 const Dwarf_Die* die,
582 size_t where_offset);
583
584 static void
585 maybe_canonicalize_type(const type_base_sptr& t,
586 reader& rdr);
587
588 static uint64_t
589 get_default_array_lower_bound(translation_unit::language l);
590
591 static bool
592 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
593 Dwarf_Off,
594 imported_unit_points_type::const_iterator&);
595
596 static array_type_def::subrange_sptr
597 build_subrange_type(reader& rdr,
598 const Dwarf_Die* die,
599 size_t where_offset,
600 bool associate_type_to_die = true);
601
602 static void
603 build_subranges_from_array_type_die(reader& rdr,
604 const Dwarf_Die* die,
605 array_type_def::subranges_type& subranges,
606 size_t where_offset,
607 bool associate_type_to_die = true);
608
609 static comparison_result
610 compare_dies(const reader& rdr,
611 const Dwarf_Die *l, const Dwarf_Die *r,
612 bool update_canonical_dies_on_the_fly);
613
614 static bool
615 compare_dies_during_canonicalization(reader& rdr,
616 const Dwarf_Die *l, const Dwarf_Die *r,
617 bool update_canonical_dies_on_the_fly);
618
619 static bool
620 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
621
622 /// Compare a symbol name against another name, possibly demangling
623 /// the symbol_name before performing the comparison.
624 ///
625 /// @param symbol_name the symbol_name to take in account.
626 ///
627 /// @param name the second name to take in account.
628 ///
629 /// @param demangle if true, demangle @p symbol_name and compare the
630 /// result of the demangling with @p name.
631 ///
632 /// @return true iff symbol_name equals name.
633 static bool
compare_symbol_name(const string & symbol_name,const string & name,bool demangle)634 compare_symbol_name(const string& symbol_name,
635 const string& name,
636 bool demangle)
637 {
638 if (demangle)
639 {
640 string m = demangle_cplus_mangled_name(symbol_name);
641 return m == name;
642 }
643 return symbol_name == name;
644 }
645
646 /// Lookup a symbol using the SysV ELF hash table.
647 ///
648 /// Note that this function hasn't been tested. So it hasn't been
649 /// debugged yet. IOW, it is not known to work. Or rather, it's
650 /// almost like it's surely doesn't work ;-)
651 ///
652 /// Use it at your own risks. :-)
653 ///
654 ///@parm env the environment we are operating from.
655 ///
656 /// @param elf_handle the elf_handle to use.
657 ///
658 /// @param sym_name the symbol name to look for.
659 ///
660 /// @param ht_index the index (in the section headers table) of the
661 /// hash table section to use.
662 ///
663 /// @param sym_tab_index the index (in the section headers table) of
664 /// the symbol table to use.
665 ///
666 /// @param demangle if true, demangle @p sym_name before comparing it
667 /// to names from the symbol table.
668 ///
669 /// @param syms_found a vector of symbols found with the name @p
670 /// sym_name. table.
671 static bool
lookup_symbol_from_sysv_hash_tab(const environment & env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)672 lookup_symbol_from_sysv_hash_tab(const environment& env,
673 Elf* elf_handle,
674 const string& sym_name,
675 size_t ht_index,
676 size_t sym_tab_index,
677 bool demangle,
678 vector<elf_symbol_sptr>& syms_found)
679 {
680 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
681 ABG_ASSERT(sym_tab_section);
682
683 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
684 ABG_ASSERT(sym_tab_data);
685
686 GElf_Shdr sheader_mem;
687 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
688 &sheader_mem);
689 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
690 ABG_ASSERT(hash_section);
691
692 // Poke at the different parts of the hash table and get them ready
693 // to be used.
694 unsigned long hash = elf_hash(sym_name.c_str());
695 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
696 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
697 size_t nb_buckets = ht_data[0];
698 size_t nb_chains = ht_data[1];
699
700 if (nb_buckets == 0)
701 // An empty hash table. Not sure if that is possible, but it
702 // would mean an empty table of exported symbols.
703 return false;
704
705 //size_t nb_chains = ht_data[1];
706 Elf32_Word* ht_buckets = &ht_data[2];
707 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
708
709 // Now do the real work.
710 size_t bucket = hash % nb_buckets;
711 size_t symbol_index = ht_buckets[bucket];
712
713 GElf_Sym symbol;
714 const char* sym_name_str;
715 size_t sym_size;
716 elf_symbol::type sym_type;
717 elf_symbol::binding sym_binding;
718 elf_symbol::visibility sym_visibility;
719 bool found = false;
720
721 do
722 {
723 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
724 sym_name_str = elf_strptr(elf_handle,
725 sym_tab_section_header->sh_link,
726 symbol.st_name);
727 if (sym_name_str
728 && compare_symbol_name(sym_name_str, sym_name, demangle))
729 {
730 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
731 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
732 sym_visibility =
733 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
734 sym_size = symbol.st_size;
735 elf_symbol::version ver;
736 if (get_version_for_symbol(elf_handle, symbol_index,
737 /*get_def_version=*/true, ver))
738 ABG_ASSERT(!ver.str().empty());
739 elf_symbol_sptr symbol_found =
740 elf_symbol::create(env,
741 symbol_index,
742 sym_size,
743 sym_name_str,
744 sym_type,
745 sym_binding,
746 symbol.st_shndx != SHN_UNDEF,
747 symbol.st_shndx == SHN_COMMON,
748 ver, sym_visibility);
749 syms_found.push_back(symbol_found);
750 found = true;
751 }
752 symbol_index = ht_chains[symbol_index];
753 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
754
755 return found;
756 }
757
758 /// Get the size of the elf class, in bytes.
759 ///
760 /// @param elf_handle the elf handle to use.
761 ///
762 /// @return the size computed.
763 static char
get_elf_class_size_in_bytes(Elf * elf_handle)764 get_elf_class_size_in_bytes(Elf* elf_handle)
765 {
766 char result = 0;
767 GElf_Ehdr hdr;
768
769 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
770 int c = hdr.e_ident[EI_CLASS];
771
772 switch (c)
773 {
774 case ELFCLASS32:
775 result = 4;
776 break;
777 case ELFCLASS64:
778 result = 8;
779 break;
780 default:
781 ABG_ASSERT_NOT_REACHED;
782 }
783
784 return result;
785 }
786
787 /// Get a given word of a bloom filter, referred to by the index of
788 /// the word.
789 ///
790 /// The bloom word size depends on the current elf class (32 bits for
791 /// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
792 /// abstracts that nicely.
793 ///
794 /// @param elf_handle the elf handle to use.
795 ///
796 /// @param bloom_filter the bloom filter to consider.
797 ///
798 /// @param index the index of the bloom filter to return.
799 ///
800 /// @return a 64 bits work containing the bloom word found at index @p
801 /// index. Note that if we are looking at an ELFCLASS32 binary, the 4
802 /// most significant bytes of the result are going to be zero.
803 static Elf64_Xword
bloom_word_at(Elf * elf_handle,Elf32_Word * bloom_filter,size_t index)804 bloom_word_at(Elf* elf_handle,
805 Elf32_Word* bloom_filter,
806 size_t index)
807 {
808 Elf64_Xword result = 0;
809 GElf_Ehdr h;
810 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
811 int c;
812 c = h.e_ident[EI_CLASS];
813
814 switch(c)
815 {
816 case ELFCLASS32:
817 result = bloom_filter[index];
818 break ;
819 case ELFCLASS64:
820 {
821 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
822 result = f[index];
823 }
824 break;
825 default:
826 abort();
827 }
828
829 return result;
830 }
831
832 /// The abstraction of the gnu elf hash table.
833 ///
834 /// The members of this struct are explained at
835 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
836 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
837 struct gnu_ht
838 {
839 size_t nb_buckets;
840 Elf32_Word* buckets;
841 Elf32_Word* chain;
842 size_t first_sym_index;
843 size_t bf_nwords;
844 size_t bf_size;
845 Elf32_Word* bloom_filter;
846 size_t shift;
847 size_t sym_count;
848 Elf_Scn* sym_tab_section;
849 GElf_Shdr sym_tab_section_header;
850
gnu_htabigail::dwarf::gnu_ht851 gnu_ht()
852 : nb_buckets(0),
853 buckets(0),
854 chain(0),
855 first_sym_index(0),
856 bf_nwords(0),
857 bf_size(0),
858 bloom_filter(0),
859 shift(0),
860 sym_count(0),
861 sym_tab_section(0)
862 {}
863 }; // end struct gnu_ht
864
865 /// Setup the members of the gnu hash table.
866 ///
867 /// @param elf_handle a handle on the elf file to use.
868 ///
869 /// @param ht_index the index (into the elf section headers table) of
870 /// the hash table section to use.
871 ///
872 /// @param sym_tab_index the index (into the elf section headers
873 /// table) of the symbol table the gnu hash table is about.
874 ///
875 /// @param ht the resulting hash table.
876 ///
877 /// @return true iff the hash table @ ht could be setup.
878 static bool
setup_gnu_ht(Elf * elf_handle,size_t ht_index,size_t sym_tab_index,gnu_ht & ht)879 setup_gnu_ht(Elf* elf_handle,
880 size_t ht_index,
881 size_t sym_tab_index,
882 gnu_ht& ht)
883 {
884 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
885 ABG_ASSERT(ht.sym_tab_section);
886 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
887 ht.sym_count =
888 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
889 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
890 ABG_ASSERT(hash_section);
891
892 // Poke at the different parts of the hash table and get them ready
893 // to be used.
894 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
895 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
896
897 ht.nb_buckets = ht_data[0];
898 if (ht.nb_buckets == 0)
899 // An empty hash table. Not sure if that is possible, but it
900 // would mean an empty table of exported symbols.
901 return false;
902 ht.first_sym_index = ht_data[1];
903 // The number of words used by the bloom filter. A size of a word
904 // is ELFCLASS.
905 ht.bf_nwords = ht_data[2];
906 // The shift used by the bloom filter code.
907 ht.shift = ht_data[3];
908 // The data of the bloom filter proper.
909 ht.bloom_filter = &ht_data[4];
910 // The size of the bloom filter in 4 bytes word. This is going to
911 // be used to index the 'bloom_filter' above, which is of type
912 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
913 // words.
914 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
915 // The buckets of the hash table.
916 ht.buckets = ht.bloom_filter + ht.bf_size;
917 // The chain of the hash table.
918 ht.chain = ht.buckets + ht.nb_buckets;
919
920 return true;
921 }
922
923 /// Look into the symbol tables of the underlying elf file and find
924 /// the symbol we are being asked.
925 ///
926 /// This function uses the GNU hash table for the symbol lookup.
927 ///
928 /// The reference of for the implementation of this function can be
929 /// found at:
930 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
931 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
932 ///
933 /// @param elf_handle the elf handle to use.
934 ///
935 /// @param sym_name the name of the symbol to look for.
936 ///
937 /// @param ht_index the index of the hash table header to use.
938 ///
939 /// @param sym_tab_index the index of the symbol table header to use
940 /// with this hash table.
941 ///
942 /// @param demangle if true, demangle @p sym_name.
943 ///
944 /// @param syms_found the vector of symbols found with the name @p
945 /// sym_name.
946 ///
947 /// @return true if a symbol was actually found.
948 static bool
lookup_symbol_from_gnu_hash_tab(const environment & env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)949 lookup_symbol_from_gnu_hash_tab(const environment& env,
950 Elf* elf_handle,
951 const string& sym_name,
952 size_t ht_index,
953 size_t sym_tab_index,
954 bool demangle,
955 vector<elf_symbol_sptr>& syms_found)
956 {
957 gnu_ht ht;
958 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
959 return false;
960
961 // Now do the real work.
962
963 // Compute bloom hashes (GNU hash and second bloom specific hashes).
964 size_t h1 = elf_gnu_hash(sym_name.c_str());
965 size_t h2 = h1 >> ht.shift;
966 // The size of one of the words used in the bloom
967 // filter, in bits.
968 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
969 int n = (h1 / c) % ht.bf_nwords;
970 // The bitmask of the bloom filter has a size of either 32-bits on
971 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
972 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
973 // type used here. When dealing with 32bits binaries, the upper
974 // bits of the bitmask will be zero anyway.
975 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
976
977 // Test if the symbol is *NOT* present in this ELF file.
978 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
979 return false;
980
981 size_t i = ht.buckets[h1 % ht.nb_buckets];
982 if (i == STN_UNDEF)
983 return false;
984
985 Elf32_Word stop_word, *stop_wordp;
986 elf_symbol::version ver;
987 GElf_Sym symbol;
988 const char* sym_name_str;
989 bool found = false;
990
991 elf_symbol::type sym_type;
992 elf_symbol::binding sym_binding;
993 elf_symbol::visibility sym_visibility;
994
995 // Let's walk the hash table and record the versions of all the
996 // symbols which name equal sym_name.
997 for (i = ht.buckets[h1 % ht.nb_buckets],
998 stop_wordp = &ht.chain[i - ht.first_sym_index];
999 i != STN_UNDEF
1000 && (stop_wordp
1001 < ht.chain + (ht.sym_count - ht.first_sym_index));
1002 ++i, ++stop_wordp)
1003 {
1004 stop_word = *stop_wordp;
1005 if ((stop_word & ~ 1)!= (h1 & ~1))
1006 // A given bucket can reference several hashes. Here we
1007 // stumbled across a hash value different from the one we are
1008 // looking for. Let's keep walking.
1009 continue;
1010
1011 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1012 i, &symbol));
1013 sym_name_str = elf_strptr(elf_handle,
1014 ht.sym_tab_section_header.sh_link,
1015 symbol.st_name);
1016 if (sym_name_str
1017 && compare_symbol_name(sym_name_str, sym_name, demangle))
1018 {
1019 // So we found a symbol (in the symbol table) that equals
1020 // sym_name. Now lets try to get its version and record it.
1021 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1022 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1023 sym_visibility =
1024 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1025
1026 if (get_version_for_symbol(elf_handle, i,
1027 /*get_def_version=*/true,
1028 ver))
1029 ABG_ASSERT(!ver.str().empty());
1030
1031 elf_symbol_sptr symbol_found =
1032 elf_symbol::create(env, i,
1033 symbol.st_size,
1034 sym_name_str,
1035 sym_type, sym_binding,
1036 symbol.st_shndx != SHN_UNDEF,
1037 symbol.st_shndx == SHN_COMMON,
1038 ver, sym_visibility);
1039 syms_found.push_back(symbol_found);
1040 found = true;
1041 }
1042
1043 if (stop_word & 1)
1044 // The last bit of the stop_word is 1. That means we need to
1045 // stop here. We reached the end of the chain of values
1046 // referenced by the hask bucket.
1047 break;
1048 }
1049 return found;
1050 }
1051
1052 /// Look into the symbol tables of the underlying elf file and find
1053 /// the symbol we are being asked.
1054 ///
1055 /// This function uses the elf hash table (be it the GNU hash table or
1056 /// the sysv hash table) for the symbol lookup.
1057 ///
1058 /// @param env the environment we are operating from.
1059 ///
1060 /// @param elf_handle the elf handle to use.
1061 ///
1062 /// @param ht_kind the kind of hash table to use. This is returned by
1063 /// the function function find_hash_table_section_index.
1064 ///
1065 /// @param ht_index the index (in the section headers table) of the
1066 /// hash table section to use.
1067 ///
1068 /// @param sym_tab_index the index (in section headers table) of the
1069 /// symbol table index to use with this hash table.
1070 ///
1071 /// @param symbol_name the name of the symbol to look for.
1072 ///
1073 /// @param demangle if true, demangle @p sym_name.
1074 ///
1075 /// @param syms_found the symbols that were actually found with the
1076 /// name @p symbol_name.
1077 ///
1078 /// @return true iff the function found the symbol from the elf hash
1079 /// table.
1080 static bool
lookup_symbol_from_elf_hash_tab(const environment & env,Elf * elf_handle,hash_table_kind ht_kind,size_t ht_index,size_t symtab_index,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1081 lookup_symbol_from_elf_hash_tab(const environment& env,
1082 Elf* elf_handle,
1083 hash_table_kind ht_kind,
1084 size_t ht_index,
1085 size_t symtab_index,
1086 const string& symbol_name,
1087 bool demangle,
1088 vector<elf_symbol_sptr>& syms_found)
1089 {
1090 if (elf_handle == 0 || symbol_name.empty())
1091 return false;
1092
1093 if (ht_kind == NO_HASH_TABLE_KIND)
1094 return false;
1095
1096 if (ht_kind == SYSV_HASH_TABLE_KIND)
1097 return lookup_symbol_from_sysv_hash_tab(env,
1098 elf_handle, symbol_name,
1099 ht_index,
1100 symtab_index,
1101 demangle,
1102 syms_found);
1103 else if (ht_kind == GNU_HASH_TABLE_KIND)
1104 return lookup_symbol_from_gnu_hash_tab(env,
1105 elf_handle, symbol_name,
1106 ht_index,
1107 symtab_index,
1108 demangle,
1109 syms_found);
1110 return false;
1111 }
1112
1113 /// Lookup a symbol from the symbol table directly.
1114 ///
1115 ///
1116 /// @param env the environment we are operating from.
1117 ///
1118 /// @param elf_handle the elf handle to use.
1119 ///
1120 /// @param sym_name the name of the symbol to look up.
1121 ///
1122 /// @param sym_tab_index the index (in the section headers table) of
1123 /// the symbol table section.
1124 ///
1125 /// @param demangle if true, demangle the names found in the symbol
1126 /// table before comparing them with @p sym_name.
1127 ///
1128 /// @param sym_name_found the actual name of the symbol found.
1129 ///
1130 /// @param sym_type the type of the symbol found.
1131 ///
1132 /// @param sym_binding the binding of the symbol found.
1133 ///
1134 /// @param sym_versions the versions of the symbol found.
1135 ///
1136 /// @return true iff the symbol was found.
1137 static bool
lookup_symbol_from_symtab(const environment & env,Elf * elf_handle,const string & sym_name,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)1138 lookup_symbol_from_symtab(const environment& env,
1139 Elf* elf_handle,
1140 const string& sym_name,
1141 size_t sym_tab_index,
1142 bool demangle,
1143 vector<elf_symbol_sptr>& syms_found)
1144 {
1145 // TODO: read all of the symbol table, store it in memory in a data
1146 // structure that associates each symbol with its versions and in
1147 // which lookups of a given symbol is fast.
1148 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1149 ABG_ASSERT(sym_tab_section);
1150
1151 GElf_Shdr header_mem;
1152 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1153 &header_mem);
1154
1155 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1156 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1157 GElf_Sym* sym;
1158 char* name_str = 0;
1159 elf_symbol::version ver;
1160 bool found = false;
1161
1162 for (size_t i = 0; i < symcount; ++i)
1163 {
1164 GElf_Sym sym_mem;
1165 sym = gelf_getsym(symtab, i, &sym_mem);
1166 name_str = elf_strptr(elf_handle,
1167 sym_tab_header->sh_link,
1168 sym->st_name);
1169
1170 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1171 {
1172 elf_symbol::type sym_type =
1173 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1174 elf_symbol::binding sym_binding =
1175 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1176 elf_symbol::visibility sym_visibility =
1177 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1178 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1179 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1180
1181 if (get_version_for_symbol(elf_handle, i,
1182 /*get_def_version=*/sym_is_defined,
1183 ver))
1184 ABG_ASSERT(!ver.str().empty());
1185 elf_symbol_sptr symbol_found =
1186 elf_symbol::create(env, i, sym->st_size,
1187 name_str, sym_type,
1188 sym_binding, sym_is_defined,
1189 sym_is_common, ver, sym_visibility);
1190 syms_found.push_back(symbol_found);
1191 found = true;
1192 }
1193 }
1194
1195 if (found)
1196 return true;
1197
1198 return false;
1199 }
1200
1201 /// Look into the symbol tables of the underlying elf file and see
1202 /// if we find a given symbol.
1203 ///
1204 /// @param env the environment we are operating from.
1205 ///
1206 /// @param symbol_name the name of the symbol to look for.
1207 ///
1208 /// @param demangle if true, try to demangle the symbol name found in
1209 /// the symbol table before comparing it to @p symbol_name.
1210 ///
1211 /// @param syms_found the list of symbols found, with the name @p
1212 /// symbol_name.
1213 ///
1214 /// @param sym_type this is set to the type of the symbol found. This
1215 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1216 /// STT_FUNC, STT_IFUNC, etc ...
1217 ///
1218 /// Note that this parameter is set iff the function returns true.
1219 ///
1220 /// @param sym_binding this is set to the binding of the symbol found.
1221 /// This is a standard elf.h value of the symbol binding kind, that
1222 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1223 ///
1224 /// @param symbol_versions the versions of the symbol @p symbol_name,
1225 /// if it was found.
1226 ///
1227 /// @return true iff a symbol with the name @p symbol_name was found.
1228 static bool
lookup_symbol_from_elf(const environment & env,Elf * elf_handle,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1229 lookup_symbol_from_elf(const environment& env,
1230 Elf* elf_handle,
1231 const string& symbol_name,
1232 bool demangle,
1233 vector<elf_symbol_sptr>& syms_found)
1234 {
1235 size_t hash_table_index = 0, symbol_table_index = 0;
1236 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1237
1238 if (!demangle)
1239 ht_kind = find_hash_table_section_index(elf_handle,
1240 hash_table_index,
1241 symbol_table_index);
1242
1243 if (ht_kind == NO_HASH_TABLE_KIND)
1244 {
1245 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1246 return false;
1247
1248 return lookup_symbol_from_symtab(env,
1249 elf_handle,
1250 symbol_name,
1251 symbol_table_index,
1252 demangle,
1253 syms_found);
1254 }
1255
1256 return lookup_symbol_from_elf_hash_tab(env,
1257 elf_handle,
1258 ht_kind,
1259 hash_table_index,
1260 symbol_table_index,
1261 symbol_name,
1262 demangle,
1263 syms_found);
1264 }
1265
1266 /// Look into the symbol tables of the underlying elf file and see if
1267 /// we find a given public (global or weak) symbol of function type.
1268 ///
1269 /// @param env the environment we are operating from.
1270 ///
1271 /// @param elf_handle the elf handle to use for the query.
1272 ///
1273 /// @param symbol_name the function symbol to look for.
1274 ///
1275 /// @param func_syms the vector of public functions symbols found, if
1276 /// any.
1277 ///
1278 /// @return true iff the symbol was found.
1279 static bool
lookup_public_function_symbol_from_elf(environment & env,Elf * elf_handle,const string & symbol_name,vector<elf_symbol_sptr> & func_syms)1280 lookup_public_function_symbol_from_elf(environment& env,
1281 Elf* elf_handle,
1282 const string& symbol_name,
1283 vector<elf_symbol_sptr>& func_syms)
1284 {
1285 vector<elf_symbol_sptr> syms_found;
1286 bool found = false;
1287
1288 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1289 /*demangle=*/false, syms_found))
1290 {
1291 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1292 i != syms_found.end();
1293 ++i)
1294 {
1295 elf_symbol::type type = (*i)->get_type();
1296 elf_symbol::binding binding = (*i)->get_binding();
1297
1298 if ((type == elf_symbol::FUNC_TYPE
1299 || type == elf_symbol::GNU_IFUNC_TYPE
1300 || type == elf_symbol::COMMON_TYPE)
1301 && (binding == elf_symbol::GLOBAL_BINDING
1302 || binding == elf_symbol::WEAK_BINDING))
1303 {
1304 func_syms.push_back(*i);
1305 found = true;
1306 }
1307 }
1308 }
1309
1310 return found;
1311 }
1312
1313 // ---------------------------------------
1314 // <location expression evaluation types>
1315 // ---------------------------------------
1316
1317 /// An abstraction of a value representing the result of the
1318 /// evaluation of a dwarf expression. This is abstraction represents
1319 /// a partial view on the possible values because we are only
1320 /// interested in extracting the latest and longuest constant
1321 /// sub-expression of a given dwarf expression.
1322 class expr_result
1323 {
1324 bool is_const_;
1325 int64_t const_value_;
1326
1327 public:
expr_result()1328 expr_result()
1329 : is_const_(true),
1330 const_value_(0)
1331 {}
1332
expr_result(bool is_const)1333 expr_result(bool is_const)
1334 : is_const_(is_const),
1335 const_value_(0)
1336 {}
1337
expr_result(int64_t v)1338 explicit expr_result(int64_t v)
1339 :is_const_(true),
1340 const_value_(v)
1341 {}
1342
1343 /// @return true if the value is a constant. Otherwise, return
1344 /// false, meaning the value represents a quantity for which we need
1345 /// inferior (a running program) state to determine the value.
1346 bool
is_const() const1347 is_const() const
1348 {return is_const_;}
1349
1350
1351 /// @param f a flag saying if the value is set to a constant or not.
1352 void
is_const(bool f)1353 is_const(bool f)
1354 {is_const_ = f;}
1355
1356 /// Get the current constant value iff this represents a
1357 /// constant.
1358 ///
1359 /// @param value the out parameter. Is set to the constant value of
1360 /// the @ref expr_result. This is set iff the function return true.
1361 ///
1362 ///@return true if this has a constant value, false otherwise.
1363 bool
const_value(int64_t & value)1364 const_value(int64_t& value)
1365 {
1366 if (is_const())
1367 {
1368 value = const_value_;
1369 return true;
1370 }
1371 return false;
1372 }
1373
1374 /// Getter of the constant value of the current @ref expr_result.
1375 ///
1376 /// Note that the current @ref expr_result must be constant,
1377 /// otherwise the current process is aborted.
1378 ///
1379 /// @return the constant value of the current @ref expr_result.
1380 int64_t
const_value() const1381 const_value() const
1382 {
1383 ABG_ASSERT(is_const());
1384 return const_value_;
1385 }
1386
operator int64_t() const1387 operator int64_t() const
1388 {return const_value();}
1389
1390 expr_result&
operator =(const int64_t v)1391 operator=(const int64_t v)
1392 {
1393 const_value_ = v;
1394 return *this;
1395 }
1396
1397 bool
operator ==(const expr_result & o) const1398 operator==(const expr_result& o) const
1399 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1400
1401 bool
operator >=(const expr_result & o) const1402 operator>=(const expr_result& o) const
1403 {return const_value_ >= o.const_value_;}
1404
1405 bool
operator <=(const expr_result & o) const1406 operator<=(const expr_result& o) const
1407 {return const_value_ <= o.const_value_;}
1408
1409 bool
operator >(const expr_result & o) const1410 operator>(const expr_result& o) const
1411 {return const_value_ > o.const_value_;}
1412
1413 bool
operator <(const expr_result & o) const1414 operator<(const expr_result& o) const
1415 {return const_value_ < o.const_value_;}
1416
1417 expr_result
operator +(const expr_result & v) const1418 operator+(const expr_result& v) const
1419 {
1420 expr_result r(*this);
1421 r.const_value_ += v.const_value_;
1422 r.is_const_ = r.is_const_ && v.is_const_;
1423 return r;
1424 }
1425
1426 expr_result&
operator +=(int64_t v)1427 operator+=(int64_t v)
1428 {
1429 const_value_ += v;
1430 return *this;
1431 }
1432
1433 expr_result
operator -(const expr_result & v) const1434 operator-(const expr_result& v) const
1435 {
1436 expr_result r(*this);
1437 r.const_value_ -= v.const_value_;
1438 r.is_const_ = r.is_const_ && v.is_const_;
1439 return r;
1440 }
1441
1442 expr_result
operator %(const expr_result & v) const1443 operator%(const expr_result& v) const
1444 {
1445 expr_result r(*this);
1446 r.const_value_ %= v.const_value_;
1447 r.is_const_ = r.is_const_ && v.is_const();
1448 return r;
1449 }
1450
1451 expr_result
operator *(const expr_result & v) const1452 operator*(const expr_result& v) const
1453 {
1454 expr_result r(*this);
1455 r.const_value_ *= v.const_value_;
1456 r.is_const_ = r.is_const_ && v.is_const();
1457 return r;
1458 }
1459
1460 expr_result
operator |(const expr_result & v) const1461 operator|(const expr_result& v) const
1462 {
1463 expr_result r(*this);
1464 r.const_value_ |= v.const_value_;
1465 r.is_const_ = r.is_const_ && v.is_const_;
1466 return r;
1467 }
1468
1469 expr_result
operator ^(const expr_result & v) const1470 operator^(const expr_result& v) const
1471 {
1472 expr_result r(*this);
1473 r.const_value_ ^= v.const_value_;
1474 r.is_const_ = r.is_const_ && v.is_const_;
1475 return r;
1476 }
1477
1478 expr_result
operator >>(const expr_result & v) const1479 operator>>(const expr_result& v) const
1480 {
1481 expr_result r(*this);
1482 r.const_value_ = r.const_value_ >> v.const_value_;
1483 r.is_const_ = r.is_const_ && v.is_const_;
1484 return r;
1485 }
1486
1487 expr_result
operator <<(const expr_result & v) const1488 operator<<(const expr_result& v) const
1489 {
1490 expr_result r(*this);
1491 r.const_value_ = r.const_value_ << v.const_value_;
1492 r.is_const_ = r.is_const_ && v.is_const_;
1493 return r;
1494 }
1495
1496 expr_result
operator ~() const1497 operator~() const
1498 {
1499 expr_result r(*this);
1500 r.const_value_ = ~r.const_value_;
1501 return r;
1502 }
1503
1504 expr_result
neg() const1505 neg() const
1506 {
1507 expr_result r(*this);
1508 r.const_value_ = -r.const_value_;
1509 return r;
1510 }
1511
1512 expr_result
abs() const1513 abs() const
1514 {
1515 expr_result r = *this;
1516 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1517 return r;
1518 }
1519
1520 expr_result
operator &(const expr_result & o)1521 operator&(const expr_result& o)
1522 {
1523 expr_result r(*this);
1524 r.const_value_ &= o.const_value_;
1525 r.is_const_ = r.is_const_ && o.is_const_;
1526 return r;
1527 }
1528
1529 expr_result
operator /(const expr_result & o)1530 operator/(const expr_result& o)
1531 {
1532 expr_result r(*this);
1533 r.is_const_ = r.is_const_ && o.is_const_;
1534 return r.const_value() / o.const_value();
1535 }
1536 };// class end expr_result;
1537
1538 /// A class that implements a stack of @ref expr_result, to be used in
1539 /// the engine evaluating DWARF expressions.
1540 class expr_result_stack_type
1541 {
1542 vector<expr_result> elems_;
1543
1544 public:
1545
expr_result_stack_type()1546 expr_result_stack_type()
1547 {elems_.reserve(4);}
1548
1549 expr_result&
operator [](unsigned i)1550 operator[](unsigned i)
1551 {
1552 unsigned s = elems_.size();
1553 ABG_ASSERT(s > i);
1554 return elems_[s - 1 -i];
1555 }
1556
1557 const expr_result&
operator [](unsigned i) const1558 operator[](unsigned i) const
1559 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1560
1561 unsigned
size() const1562 size() const
1563 {return elems_.size();}
1564
1565 vector<expr_result>::reverse_iterator
begin()1566 begin()
1567 {return elems_.rbegin();}
1568
1569 const vector<expr_result>::reverse_iterator
begin() const1570 begin() const
1571 {return const_cast<expr_result_stack_type*>(this)->begin();}
1572
1573 vector<expr_result>::reverse_iterator
end()1574 end()
1575 {return elems_.rend();}
1576
1577 const vector<expr_result>::reverse_iterator
end() const1578 end() const
1579 {return const_cast<expr_result_stack_type*>(this)->end();}
1580
1581 expr_result&
front()1582 front()
1583 {return elems_.back();}
1584
1585 const expr_result&
front() const1586 front() const
1587 {return const_cast<expr_result_stack_type*>(this)->front();}
1588
1589 void
push_front(expr_result e)1590 push_front(expr_result e)
1591 {elems_.push_back(e);}
1592
1593 expr_result
pop_front()1594 pop_front()
1595 {
1596 expr_result r = front();
1597 elems_.pop_back();
1598 return r;
1599 }
1600
1601 void
erase(vector<expr_result>::reverse_iterator i)1602 erase(vector<expr_result>::reverse_iterator i)
1603 {elems_.erase(--i.base());}
1604
1605 void
clear()1606 clear()
1607 {elems_.clear();}
1608 }; // end class expr_result_stack_type
1609
1610 /// Abstraction of the evaluation context of a dwarf expression.
1611 struct dwarf_expr_eval_context
1612 {
1613 expr_result accum;
1614 expr_result_stack_type stack;
1615 // Is set to true if the result of the expression that got evaluated
1616 // is a TLS address.
1617 bool set_tls_addr;
1618
dwarf_expr_eval_contextabigail::dwarf::dwarf_expr_eval_context1619 dwarf_expr_eval_context()
1620 : accum(/*is_const=*/false),
1621 set_tls_addr(false)
1622 {
1623 stack.push_front(expr_result(true));
1624 }
1625
1626 void
resetabigail::dwarf::dwarf_expr_eval_context1627 reset()
1628 {
1629 stack.clear();
1630 stack.push_front(expr_result(true));
1631 accum = expr_result(false);
1632 set_tls_addr = false;
1633 }
1634
1635 /// Set a flag to to tell that the result of the expression that got
1636 /// evaluated is a TLS address.
1637 ///
1638 /// @param f true iff the result of the expression that got
1639 /// evaluated is a TLS address, false otherwise.
1640 void
set_tls_addressabigail::dwarf::dwarf_expr_eval_context1641 set_tls_address(bool f)
1642 {set_tls_addr = f;}
1643
1644 /// Getter for the flag that tells if the result of the expression
1645 /// that got evaluated is a TLS address.
1646 ///
1647 /// @return true iff the result of the expression that got evaluated
1648 /// is a TLS address.
1649 bool
set_tls_addressabigail::dwarf::dwarf_expr_eval_context1650 set_tls_address() const
1651 {return set_tls_addr;}
1652
1653 expr_result
popabigail::dwarf::dwarf_expr_eval_context1654 pop()
1655 {
1656 expr_result r = stack.front();
1657 stack.pop_front();
1658 return r;
1659 }
1660
1661 void
pushabigail::dwarf::dwarf_expr_eval_context1662 push(const expr_result& v)
1663 {stack.push_front(v);}
1664 };//end class dwarf_expr_eval_context
1665
1666 // ---------------------------------------
1667 // </location expression evaluation types>
1668 // ---------------------------------------
1669
1670 class reader;
1671
1672 typedef shared_ptr<reader> reader_sptr;
1673
1674 /// The DWARF reader used to build the ABI corpus from debug info in
1675 /// DWARF format.
1676 ///
1677 /// This type is to be instanciated
1678 /// abigail::dwarf::reader::create().
1679 class reader : public elf_based_reader
1680 {
1681 public:
1682
1683 /// A set of containers that contains one container per kind of @ref
1684 /// die_source. This allows to associate DIEs to things, depending
1685 /// on the source of the DIE.
1686 template <typename ContainerType>
1687 class die_source_dependant_container_set
1688 {
1689 ContainerType primary_debug_info_container_;
1690 ContainerType alt_debug_info_container_;
1691 ContainerType type_unit_container_;
1692
1693 public:
1694
1695 /// Getter for the container associated to DIEs coming from a
1696 /// given @ref die_source.
1697 ///
1698 /// @param source the die_source for which we want the container.
1699 ///
1700 /// @return the container that associates DIEs coming from @p
1701 /// source to something.
1702 ContainerType&
get_container(die_source source)1703 get_container(die_source source)
1704 {
1705 ContainerType *result = 0;
1706 switch (source)
1707 {
1708 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1709 result = &primary_debug_info_container_;
1710 break;
1711 case ALT_DEBUG_INFO_DIE_SOURCE:
1712 result = &alt_debug_info_container_;
1713 break;
1714 case TYPE_UNIT_DIE_SOURCE:
1715 result = &type_unit_container_;
1716 break;
1717 case NO_DEBUG_INFO_DIE_SOURCE:
1718 case NUMBER_OF_DIE_SOURCES:
1719 ABG_ASSERT_NOT_REACHED;
1720 }
1721 return *result;
1722 }
1723
1724 /// Getter for the container associated to DIEs coming from a
1725 /// given @ref die_source.
1726 ///
1727 /// @param source the die_source for which we want the container.
1728 ///
1729 /// @return the container that associates DIEs coming from @p
1730 /// source to something.
1731 const ContainerType&
get_container(die_source source) const1732 get_container(die_source source) const
1733 {
1734 return const_cast<die_source_dependant_container_set*>(this)->
1735 get_container(source);
1736 }
1737
1738 /// Getter for the container associated to DIEs coming from the
1739 /// same source as a given DIE.
1740 ///
1741 /// @param rdr the DWARF reader to consider.
1742 ///
1743 /// @param die the DIE which should have the same source as the
1744 /// source of the container we want.
1745 ///
1746 /// @return the container that associates DIEs coming from the
1747 /// same source as @p die.
1748 ContainerType&
get_container(const reader & rdr,const Dwarf_Die * die)1749 get_container(const reader& rdr, const Dwarf_Die *die)
1750 {
1751 const die_source source = rdr.get_die_source(die);
1752 return get_container(source);
1753 }
1754
1755 /// Getter for the container associated to DIEs coming from the
1756 /// same source as a given DIE.
1757 ///
1758 /// @param rdr the DWARF reader to consider.
1759 ///
1760 /// @param die the DIE which should have the same source as the
1761 /// source of the container we want.
1762 ///
1763 /// @return the container that associates DIEs coming from the
1764 /// same source as @p die.
1765 const ContainerType&
get_container(const reader & rdr,const Dwarf_Die * die) const1766 get_container(const reader& rdr, const Dwarf_Die *die) const
1767 {
1768 return const_cast<die_source_dependant_container_set*>(this)->
1769 get_container(rdr, die);
1770 }
1771
1772 /// Clear the container set.
1773 void
clear()1774 clear()
1775 {
1776 primary_debug_info_container_.clear();
1777 alt_debug_info_container_.clear();
1778 type_unit_container_.clear();
1779 }
1780 }; // end die_dependant_container_set
1781
1782 unsigned short dwarf_version_;
1783 Dwarf_Die* cur_tu_die_;
1784 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1785 // A set of maps (one per kind of die source) that associates a decl
1786 // string representation with the DIEs (offsets) representing that
1787 // decl.
1788 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1789 decl_die_repr_die_offsets_maps_;
1790 // A set of maps (one per kind of die source) that associates a type
1791 // string representation with the DIEs (offsets) representing that
1792 // type.
1793 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1794 type_die_repr_die_offsets_maps_;
1795 mutable die_source_dependant_container_set<die_istring_map_type>
1796 die_qualified_name_maps_;
1797 mutable die_source_dependant_container_set<die_istring_map_type>
1798 die_pretty_repr_maps_;
1799 mutable die_source_dependant_container_set<die_istring_map_type>
1800 die_pretty_type_repr_maps_;
1801 // A set of maps (one per kind of die source) that associates the
1802 // offset of a decl die to its corresponding decl artifact.
1803 mutable die_source_dependant_container_set<die_artefact_map_type>
1804 decl_die_artefact_maps_;
1805 // A set of maps (one per kind of die source) that associates the
1806 // offset of a type die to its corresponding type artifact.
1807 mutable die_source_dependant_container_set<die_artefact_map_type>
1808 type_die_artefact_maps_;
1809 /// A set of vectors (one per kind of die source) that associates
1810 /// the offset of a type DIE to the offset of its canonical DIE.
1811 mutable die_source_dependant_container_set<offset_offset_map_type>
1812 canonical_type_die_offsets_;
1813 /// A set of vectors (one per kind of die source) that associates
1814 /// the offset of a decl DIE to the offset of its canonical DIE.
1815 mutable die_source_dependant_container_set<offset_offset_map_type>
1816 canonical_decl_die_offsets_;
1817 /// A map that associates a function type representations to
1818 /// function types, inside a translation unit.
1819 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1820 /// A map that associates a pair of DIE offsets to the result of the
1821 /// comparison of that pair.
1822 mutable std::unordered_map<std::pair<offset_type,offset_type>,
1823 abigail::ir::comparison_result,
1824 dwarf_offset_pair_hash> die_comparison_results_;
1825 // The set of types pair that have been canonical-type-propagated.
1826 mutable offset_pair_set_type propagated_types_;
1827 die_class_or_union_map_type die_wip_classes_map_;
1828 die_class_or_union_map_type alternate_die_wip_classes_map_;
1829 die_class_or_union_map_type type_unit_die_wip_classes_map_;
1830 die_function_type_map_type die_wip_function_types_map_;
1831 die_function_type_map_type alternate_die_wip_function_types_map_;
1832 die_function_type_map_type type_unit_die_wip_function_types_map_;
1833 die_function_decl_map_type die_function_with_no_symbol_map_;
1834 vector<type_base_sptr> types_to_canonicalize_;
1835 string_classes_map decl_only_classes_map_;
1836 string_enums_map decl_only_enums_map_;
1837 die_tu_map_type die_tu_map_;
1838 translation_unit_sptr cur_tu_;
1839 scope_decl_sptr nil_scope_;
1840 scope_stack_type scope_stack_;
1841 offset_offset_map_type primary_die_parent_map_;
1842 // A map that associates each tu die to a vector of unit import
1843 // points, in the main debug info
1844 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1845 // A map that associates each tu die to a vector of unit import
1846 // points, in the alternate debug info
1847 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1848 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1849 // A DIE -> parent map for DIEs coming from the alternate debug info
1850 // file.
1851 offset_offset_map_type alternate_die_parent_map_;
1852 offset_offset_map_type type_section_die_parent_map_;
1853 list<var_decl_sptr> var_decls_to_add_;
1854 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1855 bool debug_die_canonicalization_is_on_;
1856 bool use_canonical_die_comparison_;
1857 #endif
1858 mutable size_t compare_count_;
1859 mutable size_t canonical_propagated_count_;
1860 mutable size_t cancelled_propagation_count_;
1861 mutable optional<bool> leverage_dwarf_factorization_;
1862
1863 protected:
1864
1865 reader() = delete;
1866
1867 /// Constructor of reader.
1868 ///
1869 /// @param elf_path the path to the elf file the context is to be
1870 /// used for.
1871 ///
1872 /// @param debug_info_root_paths a vector of pointers to the path to
1873 /// the root directory under which the debug info is to be found for
1874 /// @p elf_path. Leave this empty if the debug info is not in a
1875 /// split file.
1876 ///
1877 /// @param environment the environment used by the current context.
1878 /// This environment contains resources needed by the DWARF reader and by
1879 /// the types and declarations that are to be created later. Note
1880 /// that ABI artifacts that are to be compared all need to be
1881 /// created within the same environment.
1882 ///
1883 /// Please also note that the life time of this environment object
1884 /// must be greater than the life time of the resulting @ref
1885 /// reader the context uses resources that are allocated in
1886 /// the environment.
1887 ///
1888 /// @param load_all_types if set to false only the types that are
1889 /// reachable from publicly exported declarations (of functions and
1890 /// variables) are read. If set to true then all types found in the
1891 /// debug information are loaded.
1892 ///
1893 /// @param linux_kernel_mode if set to true, then consider the special
1894 /// linux kernel symbol tables when determining if a symbol is
1895 /// exported or not.
reader(const string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)1896 reader(const string& elf_path,
1897 const vector<char**>& debug_info_root_paths,
1898 environment& environment,
1899 bool load_all_types,
1900 bool linux_kernel_mode)
1901 : elf_based_reader(elf_path,
1902 debug_info_root_paths,
1903 environment)
1904 {
1905 initialize(load_all_types, linux_kernel_mode);
1906 }
1907
1908 public:
1909
1910 /// Initializer of reader.
1911 ///
1912 /// Resets the reader so that it can be re-used to read another binary.
1913 ///
1914 /// @param load_all_types if set to false only the types that are
1915 /// reachable from publicly exported declarations (of functions and
1916 /// variables) are read. If set to true then all types found in the
1917 /// debug information are loaded.
1918 ///
1919 /// @param linux_kernel_mode if set to true, then consider the
1920 /// special linux kernel symbol tables when determining if a symbol
1921 /// is exported or not.
1922 void
initialize(bool load_all_types,bool linux_kernel_mode)1923 initialize(bool load_all_types, bool linux_kernel_mode)
1924 {
1925 dwarf_version_ = 0;
1926 cur_tu_die_ = 0;
1927 decl_die_repr_die_offsets_maps_.clear();
1928 type_die_repr_die_offsets_maps_.clear();
1929 die_qualified_name_maps_.clear();
1930 die_pretty_repr_maps_.clear();
1931 die_pretty_type_repr_maps_.clear();
1932 decl_die_artefact_maps_.clear();
1933 type_die_artefact_maps_.clear();
1934 canonical_type_die_offsets_.clear();
1935 canonical_decl_die_offsets_.clear();
1936 die_wip_classes_map_.clear();
1937 alternate_die_wip_classes_map_.clear();
1938 type_unit_die_wip_classes_map_.clear();
1939 die_wip_function_types_map_.clear();
1940 alternate_die_wip_function_types_map_.clear();
1941 type_unit_die_wip_function_types_map_.clear();
1942 die_function_with_no_symbol_map_.clear();
1943 types_to_canonicalize_.clear();
1944 decl_only_classes_map_.clear();
1945 die_tu_map_.clear();
1946 corpus().reset();
1947 corpus_group().reset();
1948 cur_tu_.reset();
1949 primary_die_parent_map_.clear();
1950 tu_die_imported_unit_points_map_.clear();
1951 alt_tu_die_imported_unit_points_map_.clear();
1952 type_units_tu_die_imported_unit_points_map_.clear();
1953 alternate_die_parent_map_.clear();
1954 type_section_die_parent_map_.clear();
1955 var_decls_to_add_.clear();
1956 clear_per_translation_unit_data();
1957 options().load_in_linux_kernel_mode = linux_kernel_mode;
1958 options().load_all_types = load_all_types;
1959 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1960 debug_die_canonicalization_is_on_ =
1961 environment->debug_die_canonicalization_is_on();
1962 use_canonical_die_comparison_ = true;
1963 #endif
1964 compare_count_ = 0;
1965 canonical_propagated_count_ = 0;
1966 cancelled_propagation_count_ = 0;
1967 load_in_linux_kernel_mode(linux_kernel_mode);
1968 }
1969
1970 /// Initializer of reader.
1971 ///
1972 /// Resets the reader so that it can be re-used to read another binary.
1973 ///
1974 /// @param elf_path the path to the new ELF file.
1975 ///
1976 /// @param debug_info_root_paths the vector of debug-info path to
1977 /// look for split debug info.
1978 ///
1979 /// @param load_all_types if set to false only the types that are
1980 /// reachable from publicly exported declarations (of functions and
1981 /// variables) are read. If set to true then all types found in the
1982 /// debug information are loaded.
1983 ///
1984 /// @param linux_kernel_mode if set to true, then consider the
1985 /// special linux kernel symbol tables when determining if a symbol
1986 /// is exported or not.
1987 void
initialize(const string & elf_path,const vector<char ** > & debug_info_root_paths,bool load_all_types,bool linux_kernel_mode)1988 initialize(const string& elf_path,
1989 const vector<char**>& debug_info_root_paths,
1990 bool load_all_types,
1991 bool linux_kernel_mode)
1992 {
1993 reset(elf_path, debug_info_root_paths);
1994 initialize(load_all_types, linux_kernel_mode);
1995 }
1996
1997 /// Create an instance of DWARF Reader.
1998 ///
1999 /// @param elf_path the path to the ELF file to read from.
2000 ///
2001 /// @param debug_info_root_paths a vector of paths where to look up
2002 /// split debug info files.
2003 ///
2004 /// @param environment the environment to be used by the reader.
2005 ///
2006 /// @param load_all_types if set to false only the types that are
2007 /// reachable from publicly exported declarations (of functions and
2008 /// variables) are read. If set to true then all types found in the
2009 /// debug information are loaded.
2010 ///
2011 /// @param linux_kernel_mode if set to true, then consider the
2012 /// special linux kernel symbol tables when determining if a symbol
2013 /// is exported or not.
2014 static dwarf::reader_sptr
create(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)2015 create(const std::string& elf_path,
2016 const vector<char**>& debug_info_root_paths,
2017 environment& environment,
2018 bool load_all_types,
2019 bool linux_kernel_mode)
2020 {
2021 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2022 environment, load_all_types,
2023 linux_kernel_mode));
2024 return result;
2025 }
2026
2027 /// Destructor of the @ref reader type.
~reader()2028 ~reader()
2029 {
2030 }
2031
2032 /// Read and analyze the ELF and DWARF information associated with
2033 /// the underlying ELF file and build an ABI corpus out of it.
2034 ///
2035 /// @param status output parameter. This is set to the status of
2036 /// the analysis of the debug info.
2037 ///
2038 /// @return the resulting ABI corpus.
2039 corpus_sptr
read_corpus(status & status)2040 read_corpus(status& status)
2041 {
2042 status = STATUS_UNKNOWN;
2043
2044 // Load the generic ELF parts of the corpus.
2045 elf::reader::read_corpus(status);
2046
2047 if ((status & STATUS_NO_SYMBOLS_FOUND)
2048 || !(status & STATUS_OK))
2049 // Either we couldn't find ELF symbols or something went badly
2050 // wrong. There is nothing we can do with this ELF file. Bail
2051 // out.
2052 return corpus_sptr();
2053
2054 // If we couldn't find debug info from the elf path, then say it.
2055 if (dwarf_debug_info() == nullptr)
2056 status |= STATUS_DEBUG_INFO_NOT_FOUND;
2057
2058 {
2059 string alt_di_path;
2060 if (refers_to_alt_debug_info(alt_di_path)
2061 && !alternate_dwarf_debug_info())
2062 status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
2063 }
2064
2065 if (// If debug info was found but not the required alternate debug
2066 // info ...
2067 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2068 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2069 // ... then we cannot handle the binary.
2070 return corpus_sptr();
2071
2072 // Read the variable and function descriptions from the debug info
2073 // we have, through the dwfl handle.
2074 corpus_sptr corp = read_debug_info_into_corpus();
2075
2076 status |= STATUS_OK;
2077
2078 return corp;
2079 }
2080
2081 /// Read an analyze the DWARF information.
2082 ///
2083 /// Construct an ABI corpus from it.
2084 ///
2085 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2086 ///
2087 /// @return the resulting ABI corpus.
2088 corpus_sptr
read_debug_info_into_corpus()2089 read_debug_info_into_corpus()
2090 {
2091 clear_per_corpus_data();
2092
2093 // First set some mundane properties of the corpus gathered from
2094 // ELF.
2095 corpus::origin origin = corpus()->get_origin();
2096 origin |= corpus::DWARF_ORIGIN;
2097 corpus()->set_origin(origin);
2098
2099 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2100 && !env().user_set_analyze_exported_interfaces_only())
2101 // So we are looking at the Linux Kernel and the user has not set
2102 // any particular option regarding the amount of types to analyse.
2103 // In that case, we need to only analyze types that are reachable
2104 // from exported interfaces otherwise we get such a massive amount
2105 // of type DIEs to look at that things are just too slow down the
2106 // road.
2107 env().analyze_exported_interfaces_only(true);
2108
2109 corpus()->set_soname(dt_soname());
2110 corpus()->set_needed(dt_needed());
2111 corpus()->set_architecture_name(elf_architecture());
2112 if (corpus_group_sptr group = corpus_group())
2113 group->add_corpus(corpus());
2114
2115 // Set symbols information to the corpus.
2116 corpus()->set_symtab(symtab());
2117
2118 // Get out now if no debug info is found.
2119 if (!dwarf_debug_info())
2120 return corpus();
2121
2122 uint8_t address_size = 0;
2123 size_t header_size = 0;
2124
2125 #ifdef WITH_DEBUG_SELF_COMPARISON
2126 if (env().self_comparison_debug_is_on())
2127 env().set_self_comparison_debug_input(corpus());
2128 #endif
2129
2130 // Walk all the DIEs of the debug info to build a DIE -> parent map
2131 // useful for get_die_parent() to work.
2132 {
2133 tools_utils::timer t;
2134 if (do_log())
2135 {
2136 cerr << "building die -> parent maps ...";
2137 t.start();
2138 }
2139
2140 build_die_parent_maps();
2141
2142 if (do_log())
2143 {
2144 t.stop();
2145 cerr << " DONE@" << corpus()->get_path()
2146 << ":"
2147 << t
2148 << "\n";
2149 }
2150 }
2151
2152 env().canonicalization_is_done(false);
2153
2154 {
2155 tools_utils::timer t;
2156 if (do_log())
2157 {
2158 cerr << "building the libabigail internal representation ...";
2159 t.start();
2160 }
2161 // And now walk all the DIEs again to build the libabigail IR.
2162 Dwarf_Half dwarf_vers = 0;
2163 for (Dwarf_Off offset = 0, next_offset = 0;
2164 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2165 offset, &next_offset, &header_size,
2166 &dwarf_vers, NULL, &address_size, NULL,
2167 NULL, NULL) == 0);
2168 offset = next_offset)
2169 {
2170 Dwarf_Off die_offset = offset + header_size;
2171 Dwarf_Die unit;
2172 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2173 die_offset, &unit)
2174 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2175 continue;
2176
2177 dwarf_version(dwarf_vers);
2178
2179 address_size *= 8;
2180
2181 // Build a translation_unit IR node from cu; note that cu must
2182 // be a DW_TAG_compile_unit die.
2183 translation_unit_sptr ir_node =
2184 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2185 ABG_ASSERT(ir_node);
2186 }
2187 if (do_log())
2188 {
2189 t.stop();
2190 cerr << " DONE@" << corpus()->get_path()
2191 << ":"
2192 << t
2193 << "\n";
2194
2195 cerr << "Number of aggregate types compared: "
2196 << compare_count_ << "\n"
2197 << "Number of canonical types propagated: "
2198 << canonical_propagated_count_ << "\n"
2199 << "Number of cancelled propagated canonical types:"
2200 << cancelled_propagation_count_ << "\n";
2201 }
2202 }
2203
2204 {
2205 tools_utils::timer t;
2206 if (do_log())
2207 {
2208 cerr << "resolving declaration only classes ...";
2209 t.start();
2210 }
2211 resolve_declaration_only_classes();
2212 if (do_log())
2213 {
2214 t.stop();
2215 cerr << " DONE@" << corpus()->get_path()
2216 << ":"
2217 << t
2218 <<"\n";
2219 }
2220 }
2221
2222 {
2223 tools_utils::timer t;
2224 if (do_log())
2225 {
2226 cerr << "resolving declaration only enums ...";
2227 t.start();
2228 }
2229 resolve_declaration_only_enums();
2230 if (do_log())
2231 {
2232 t.stop();
2233 cerr << " DONE@" << corpus()->get_path()
2234 << ":"
2235 << t
2236 <<"\n";
2237 }
2238 }
2239
2240 {
2241 tools_utils::timer t;
2242 if (do_log())
2243 {
2244 cerr << "fixing up functions with linkage name but "
2245 << "no advertised underlying symbols ....";
2246 t.start();
2247 }
2248 fixup_functions_with_no_symbols();
2249 if (do_log())
2250 {
2251 t.stop();
2252 cerr << " DONE@" << corpus()->get_path()
2253 <<":"
2254 << t
2255 <<"\n";
2256 }
2257 }
2258
2259 /// Now, look at the types that needs to be canonicalized after the
2260 /// translation has been constructed (which is just now) and
2261 /// canonicalize them.
2262 ///
2263 /// These types need to be constructed at the end of the translation
2264 /// unit reading phase because some types are modified by some DIEs
2265 /// even after the principal DIE describing the type has been read;
2266 /// this happens for clones of virtual destructors (for instance) or
2267 /// even for some static data members. We need to do that for types
2268 /// are in the alternate debug info section and for types that in
2269 /// the main debug info section.
2270 {
2271 tools_utils::timer t;
2272 if (do_log())
2273 {
2274 cerr << "perform late type canonicalizing ...\n";
2275 t.start();
2276 }
2277
2278 perform_late_type_canonicalizing();
2279 if (do_log())
2280 {
2281 t.stop();
2282 cerr << "late type canonicalizing DONE@"
2283 << corpus()->get_path()
2284 << ":"
2285 << t
2286 << "\n";
2287 }
2288 }
2289
2290 env().canonicalization_is_done(true);
2291
2292 {
2293 tools_utils::timer t;
2294 if (do_log())
2295 {
2296 cerr << "sort functions and variables ...";
2297 t.start();
2298 }
2299 corpus()->sort_functions();
2300 corpus()->sort_variables();
2301 if (do_log())
2302 {
2303 t.stop();
2304 cerr << " DONE@" << corpus()->get_path()
2305 << ":"
2306 << t
2307 <<" \n";
2308 }
2309 }
2310
2311 return corpus();
2312 }
2313
2314 /// Clear the data that is relevant only for the current translation
2315 /// unit being read. The rest of the data is relevant for the
2316 /// entire ABI corpus.
2317 void
clear_per_translation_unit_data()2318 clear_per_translation_unit_data()
2319 {
2320 while (!scope_stack().empty())
2321 scope_stack().pop();
2322 var_decls_to_re_add_to_tree().clear();
2323 per_tu_repr_to_fn_type_maps().clear();
2324 }
2325
2326 /// Clear the data that is relevant for the current corpus being
2327 /// read.
2328 void
clear_per_corpus_data()2329 clear_per_corpus_data()
2330 {
2331 die_qualified_name_maps_.clear();
2332 die_pretty_repr_maps_.clear();
2333 die_pretty_type_repr_maps_.clear();
2334 clear_types_to_canonicalize();
2335 }
2336
2337 /// Getter for the current environment.
2338 ///
2339 /// @return the current environment.
2340 environment&
env()2341 env()
2342 {return options().env;}
2343
2344 /// Getter for the current environment.
2345 ///
2346 /// @return the current environment.
2347 const environment&
env() const2348 env() const
2349 {return const_cast<reader*>(this)->env();}
2350
2351 /// Getter for the flag that tells us if we are dropping functions
2352 /// and variables that have undefined symbols.
2353 ///
2354 /// @return true iff we are dropping functions and variables that have
2355 /// undefined symbols.
2356 bool
drop_undefined_syms() const2357 drop_undefined_syms() const
2358 {return options().drop_undefined_syms;}
2359
2360 /// Setter for the flag that tells us if we are dropping functions
2361 /// and variables that have undefined symbols.
2362 ///
2363 /// @param f the new value of the flag.
2364 void
drop_undefined_syms(bool f)2365 drop_undefined_syms(bool f)
2366 {options().drop_undefined_syms = f;}
2367
2368 /// Getter of the DWARF version.
2369 unsigned short
dwarf_version() const2370 dwarf_version() const
2371 {return dwarf_version_;}
2372
2373 void
dwarf_version(unsigned short v)2374 dwarf_version(unsigned short v)
2375 {dwarf_version_ = v;}
2376
2377 /// Return the ELF descriptor used for DWARF access.
2378 ///
2379 /// This can be the same as reader::elf_handle() above, if the
2380 /// DWARF info is in the same ELF file as the one of the binary we
2381 /// are analizing. It is different if e.g, the debug info is split
2382 /// from the ELF file we are analizing.
2383 ///
2384 /// @return a pointer to the ELF descriptor used to access debug
2385 /// info.
2386 Elf*
dwarf_elf_handle() const2387 dwarf_elf_handle() const
2388 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2389
2390 /// Test if the debug information is in a separate ELF file wrt the
2391 /// main ELF file of the program (application or shared library) we
2392 /// are analizing.
2393 ///
2394 /// @return true if the debug information is in a separate ELF file
2395 /// compared to the main ELF file of the program (application or
2396 /// shared library) that we are looking at.
2397 bool
dwarf_is_splitted() const2398 dwarf_is_splitted() const
2399 {return dwarf_elf_handle() != elf_handle();}
2400
2401 /// Return the correct debug info, depending on the DIE source we
2402 /// are looking at.
2403 ///
2404 /// @param source the DIE source to consider.
2405 ///
2406 /// @return the right debug info, depending on @p source.
2407 const Dwarf*
dwarf_per_die_source(die_source source) const2408 dwarf_per_die_source(die_source source) const
2409 {
2410 const Dwarf *result = 0;
2411 switch(source)
2412 {
2413 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2414 case TYPE_UNIT_DIE_SOURCE:
2415 result = dwarf_debug_info();
2416 break;
2417 case ALT_DEBUG_INFO_DIE_SOURCE:
2418 result = alternate_dwarf_debug_info();
2419 break;
2420 case NO_DEBUG_INFO_DIE_SOURCE:
2421 case NUMBER_OF_DIE_SOURCES:
2422 ABG_ASSERT_NOT_REACHED;
2423 }
2424 return result;
2425 }
2426
2427 /// Return the path to the ELF path we are reading.
2428 ///
2429 /// @return the elf path.
2430 const string&
elf_path() const2431 elf_path() const
2432 {return corpus_path();}
2433
2434 const Dwarf_Die*
cur_tu_die() const2435 cur_tu_die() const
2436 {return cur_tu_die_;}
2437
2438 void
cur_tu_die(Dwarf_Die * cur_tu_die)2439 cur_tu_die(Dwarf_Die* cur_tu_die)
2440 {cur_tu_die_ = cur_tu_die;}
2441
2442 dwarf_expr_eval_context&
dwarf_expr_eval_ctxt() const2443 dwarf_expr_eval_ctxt() const
2444 {return dwarf_expr_eval_context_;}
2445
2446 /// Getter of the maps set that associates a representation of a
2447 /// decl DIE to a vector of offsets of DIEs having that representation.
2448 ///
2449 /// @return the maps set that associates a representation of a decl
2450 /// DIE to a vector of offsets of DIEs having that representation.
2451 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps() const2452 decl_die_repr_die_offsets_maps() const
2453 {return decl_die_repr_die_offsets_maps_;}
2454
2455 /// Getter of the maps set that associates a representation of a
2456 /// decl DIE to a vector of offsets of DIEs having that representation.
2457 ///
2458 /// @return the maps set that associates a representation of a decl
2459 /// DIE to a vector of offsets of DIEs having that representation.
2460 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps()2461 decl_die_repr_die_offsets_maps()
2462 {return decl_die_repr_die_offsets_maps_;}
2463
2464 /// Getter of the maps set that associate a representation of a type
2465 /// DIE to a vector of offsets of DIEs having that representation.
2466 ///
2467 /// @return the maps set that associate a representation of a type
2468 /// DIE to a vector of offsets of DIEs having that representation.
2469 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps() const2470 type_die_repr_die_offsets_maps() const
2471 {return type_die_repr_die_offsets_maps_;}
2472
2473 /// Getter of the maps set that associate a representation of a type
2474 /// DIE to a vector of offsets of DIEs having that representation.
2475 ///
2476 /// @return the maps set that associate a representation of a type
2477 /// DIE to a vector of offsets of DIEs having that representation.
2478 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps()2479 type_die_repr_die_offsets_maps()
2480 {return type_die_repr_die_offsets_maps_;}
2481
2482
2483 /// Compute the offset of the canonical DIE of a given DIE.
2484 ///
2485 /// @param die the DIE to consider.
2486 ///
2487 /// @param canonical_die_offset out parameter. This is set to the
2488 /// resulting canonical DIE that was computed.
2489 ///
2490 /// @param die_as_type if yes, it means @p die has to be considered
2491 /// as a type.
2492 void
compute_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off & canonical_die_offset,bool die_as_type) const2493 compute_canonical_die_offset(const Dwarf_Die *die,
2494 Dwarf_Off &canonical_die_offset,
2495 bool die_as_type) const
2496 {
2497 offset_offset_map_type &canonical_dies =
2498 die_as_type
2499 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2500 get_container(*this, die)
2501 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2502 get_container(*this, die);
2503
2504 Dwarf_Die canonical_die;
2505 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2506
2507 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2508 }
2509
2510 /// Compute (find) the canonical DIE of a given DIE.
2511 ///
2512 /// @param die the DIE to consider.
2513 ///
2514 /// @param canonical_dies the vector in which the canonical dies ar
2515 /// stored. The index of each element is the offset of the DIE we
2516 /// want the canonical DIE for. And the value of the element at
2517 /// that index is the canonical DIE offset we are looking for.
2518 ///
2519 /// @param canonical_die_offset out parameter. This is set to the
2520 /// resulting canonical DIE that was computed.
2521 ///
2522 /// @param die_as_type if yes, it means @p die has to be considered
2523 /// as a type.
2524 void
compute_canonical_die(const Dwarf_Die * die,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2525 compute_canonical_die(const Dwarf_Die *die,
2526 offset_offset_map_type& canonical_dies,
2527 Dwarf_Die &canonical_die,
2528 bool die_as_type) const
2529 {
2530 const die_source source = get_die_source(die);
2531
2532 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2533
2534 compute_canonical_die(die_offset, source,
2535 canonical_dies,
2536 canonical_die, die_as_type);
2537 }
2538
2539 /// Compute (find) the canonical DIE of a given DIE.
2540 ///
2541 /// @param die_offset the offset of the DIE to consider.
2542 ///
2543 /// @param source the source of the DIE to consider.
2544 ///
2545 /// @param canonical_dies the vector in which the canonical dies ar
2546 /// stored. The index of each element is the offset of the DIE we
2547 /// want the canonical DIE for. And the value of the element at
2548 /// that index is the canonical DIE offset we are looking for.
2549 ///
2550 /// @param canonical_die_offset out parameter. This is set to the
2551 /// resulting canonical DIE that was computed.
2552 ///
2553 /// @param die_as_type if yes, it means @p die has to be considered
2554 /// as a type.
2555 void
compute_canonical_die(Dwarf_Off die_offset,die_source source,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2556 compute_canonical_die(Dwarf_Off die_offset,
2557 die_source source,
2558 offset_offset_map_type& canonical_dies,
2559 Dwarf_Die &canonical_die,
2560 bool die_as_type) const
2561 {
2562 // The map that associates the string representation of 'die'
2563 // with a vector of offsets of potentially equivalent DIEs.
2564 istring_dwarf_offsets_map_type& map =
2565 die_as_type
2566 ? (const_cast<reader*>(this)->
2567 type_die_repr_die_offsets_maps().get_container(source))
2568 : (const_cast<reader*>(this)->
2569 decl_die_repr_die_offsets_maps().get_container(source));
2570
2571 Dwarf_Die die;
2572 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2573 die_offset, &die));
2574
2575 // The variable repr is the the string representation of 'die'.
2576 //
2577 // Even if die_as_type is true -- which means that 'die' is said
2578 // to be considered as a type -- we always consider a
2579 // DW_TAG_subprogram DIE as a decl here, as far as its string
2580 // representation is concerned.
2581 interned_string name =
2582 (die_as_type)
2583 ? get_die_pretty_type_representation(&die, /*where=*/0)
2584 : get_die_pretty_representation(&die, /*where=*/0);
2585
2586 Dwarf_Off canonical_die_offset = 0;
2587 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2588 if (i == map.end())
2589 {
2590 dwarf_offsets_type offsets;
2591 offsets.push_back(die_offset);
2592 map[name] = offsets;
2593 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2594 get_die_from_offset(source, die_offset, &canonical_die);
2595 return;
2596 }
2597
2598 Dwarf_Off cur_die_offset;
2599 Dwarf_Die potential_canonical_die;
2600 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2601 o != i->second.end();
2602 ++o)
2603 {
2604 cur_die_offset = *o;
2605 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2606 if (compare_dies(*this, &die, &potential_canonical_die,
2607 /*update_canonical_dies_on_the_fly=*/false))
2608 {
2609 canonical_die_offset = cur_die_offset;
2610 set_canonical_die_offset(canonical_dies, die_offset,
2611 canonical_die_offset);
2612 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2613 return;
2614 }
2615 }
2616
2617 canonical_die_offset = die_offset;
2618 i->second.push_back(die_offset);
2619 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2620 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2621 }
2622
2623 /// Getter of the canonical DIE of a given DIE.
2624 ///
2625 /// @param die the DIE to consider.
2626 ///
2627 /// @param canonical_die output parameter. Is set to the resuling
2628 /// canonical die, if this function returns true.
2629 ///
2630 /// @param where the offset of the logical DIE we are supposed to be
2631 /// calling this function from. If set to zero this means this is
2632 /// to be ignored.
2633 ///
2634 /// @param die_as_type if set to yes, it means @p die is to be
2635 /// considered as a type DIE.
2636 ///
2637 /// @return true iff a canonical DIE was found for @p die.
2638 bool
get_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type)2639 get_canonical_die(const Dwarf_Die *die,
2640 Dwarf_Die &canonical_die,
2641 size_t where,
2642 bool die_as_type)
2643 {
2644 const die_source source = get_die_source(die);
2645
2646 offset_offset_map_type &canonical_dies =
2647 die_as_type
2648 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2649 get_container(source)
2650 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2651 get_container(source);
2652
2653 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2654 if (Dwarf_Off canonical_die_offset =
2655 get_canonical_die_offset(canonical_dies, die_offset))
2656 {
2657 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2658 return true;
2659 }
2660
2661 // The map that associates the string representation of 'die'
2662 // with a vector of offsets of potentially equivalent DIEs.
2663 istring_dwarf_offsets_map_type& map =
2664 die_as_type
2665 ? (const_cast<reader*>(this)->
2666 type_die_repr_die_offsets_maps().get_container(*this, die))
2667 : (const_cast<reader*>(this)->
2668 decl_die_repr_die_offsets_maps().get_container(*this, die));
2669
2670 // The variable repr is the the string representation of 'die'.
2671 //
2672 // Even if die_as_type is true -- which means that 'die' is said
2673 // to be considered as a type -- we always consider a
2674 // DW_TAG_subprogram DIE as a decl here, as far as its string
2675 // representation is concerned.
2676 interned_string name =
2677 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2678 ? get_die_pretty_type_representation(die, where)
2679 : get_die_pretty_representation(die, where);
2680
2681 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2682 if (i == map.end())
2683 return false;
2684
2685 Dwarf_Off cur_die_offset;
2686 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2687 o != i->second.end();
2688 ++o)
2689 {
2690 cur_die_offset = *o;
2691 get_die_from_offset(source, cur_die_offset, &canonical_die);
2692 // compare die and canonical_die.
2693 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2694 die, &canonical_die,
2695 /*update_canonical_dies_on_the_fly=*/true))
2696 {
2697 set_canonical_die_offset(canonical_dies,
2698 die_offset,
2699 cur_die_offset);
2700 return true;
2701 }
2702 }
2703
2704 return false;
2705 }
2706
2707 /// Retrieve the canonical DIE of a given DIE.
2708 ///
2709 /// The canonical DIE is a DIE that is structurally equivalent to
2710 /// this one.
2711 ///
2712 /// Note that this function caches the canonical DIE that was
2713 /// computed. Subsequent invocations of this function on the same
2714 /// DIE return the same cached DIE.
2715 ///
2716 /// @param die the DIE to get a canonical type for.
2717 ///
2718 /// @param canonical_die the resulting canonical DIE.
2719 ///
2720 /// @param where the offset of the logical DIE we are supposed to be
2721 /// calling this function from. If set to zero this means this is
2722 /// to be ignored.
2723 ///
2724 /// @param die_as_type if true, consider DIE is a type.
2725 ///
2726 /// @return true if an *existing* canonical DIE was found.
2727 /// Otherwise, @p die is considered as being a canonical DIE for
2728 /// itself. @p canonical_die is thus set to the canonical die in
2729 /// either cases.
2730 bool
get_or_compute_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type) const2731 get_or_compute_canonical_die(const Dwarf_Die* die,
2732 Dwarf_Die& canonical_die,
2733 size_t where,
2734 bool die_as_type) const
2735 {
2736 const die_source source = get_die_source(die);
2737
2738 offset_offset_map_type &canonical_dies =
2739 die_as_type
2740 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2741 get_container(source)
2742 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2743 get_container(source);
2744
2745 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2746
2747 if (Dwarf_Off canonical_die_offset =
2748 get_canonical_die_offset(canonical_dies,
2749 initial_die_offset))
2750 {
2751 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2752 return true;
2753 }
2754
2755 if (!is_type_die_to_be_canonicalized(die))
2756 return false;
2757
2758 // The map that associates the string representation of 'die'
2759 // with a vector of offsets of potentially equivalent DIEs.
2760 istring_dwarf_offsets_map_type& map =
2761 die_as_type
2762 ? (const_cast<reader*>(this)->
2763 type_die_repr_die_offsets_maps().get_container(*this, die))
2764 : (const_cast<reader*>(this)->
2765 decl_die_repr_die_offsets_maps().get_container(*this, die));
2766
2767 // The variable repr is the the string representation of 'die'.
2768 //
2769 // Even if die_as_type is true -- which means that 'die' is said
2770 // to be considered as a type -- we always consider a
2771 // DW_TAG_subprogram DIE as a decl here, as far as its string
2772 // representation is concerned.
2773 interned_string name =
2774 (die_as_type)
2775 ? get_die_pretty_type_representation(die, where)
2776 : get_die_pretty_representation(die, where);
2777
2778 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2779 if (i == map.end())
2780 {
2781 dwarf_offsets_type offsets;
2782 offsets.push_back(initial_die_offset);
2783 map[name] = offsets;
2784 get_die_from_offset(source, initial_die_offset, &canonical_die);
2785 set_canonical_die_offset(canonical_dies,
2786 initial_die_offset,
2787 initial_die_offset);
2788 return false;
2789 }
2790
2791 // walk i->second without any iterator (using a while loop rather
2792 // than a for loop) because compare_dies might add new content to
2793 // the end of the i->second vector during the walking.
2794 dwarf_offsets_type::size_type n = 0, s = i->second.size();
2795 while (n < s)
2796 {
2797 Dwarf_Off die_offset = i->second[n];
2798 get_die_from_offset(source, die_offset, &canonical_die);
2799 // compare die and canonical_die.
2800 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2801 die, &canonical_die,
2802 /*update_canonical_dies_on_the_fly=*/true))
2803 {
2804 set_canonical_die_offset(canonical_dies,
2805 initial_die_offset,
2806 die_offset);
2807 return true;
2808 }
2809 ++n;
2810 }
2811
2812 // We didn't find a canonical DIE for 'die'. So let's consider
2813 // that it is its own canonical DIE.
2814 get_die_from_offset(source, initial_die_offset, &canonical_die);
2815 i->second.push_back(initial_die_offset);
2816 set_canonical_die_offset(canonical_dies,
2817 initial_die_offset,
2818 initial_die_offset);
2819
2820 return false;
2821 }
2822
2823 /// Get the source of the DIE.
2824 ///
2825 /// The function returns an enumerator value saying if the DIE comes
2826 /// from the .debug_info section of the primary debug info file, the
2827 /// .debug_info section of the alternate debug info file, or the
2828 /// .debug_types section.
2829 ///
2830 /// @param die the DIE to get the source of.
2831 ///
2832 /// @return the source of the DIE if it could be determined,
2833 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2834 die_source
get_die_source(const Dwarf_Die * die) const2835 get_die_source(const Dwarf_Die *die) const
2836 {
2837 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2838 ABG_ASSERT(die);
2839 ABG_ASSERT(get_die_source(*die, source));
2840 return source;
2841 }
2842
2843 /// Get the source of the DIE.
2844 ///
2845 /// The function returns an enumerator value saying if the DIE comes
2846 /// from the .debug_info section of the primary debug info file, the
2847 /// .debug_info section of the alternate debug info file, or the
2848 /// .debug_types section.
2849 ///
2850 /// @param die the DIE to get the source of.
2851 ///
2852 /// @param source out parameter. The function sets this parameter
2853 /// to the source of the DIE @p iff it returns true.
2854 ///
2855 /// @return true iff the source of the DIE could be determined and
2856 /// returned.
2857 bool
get_die_source(const Dwarf_Die & die,die_source & source) const2858 get_die_source(const Dwarf_Die &die, die_source &source) const
2859 {
2860 Dwarf_Die cu_die;
2861 Dwarf_Die cu_kind;
2862 uint8_t address_size = 0, offset_size = 0;
2863 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2864 &cu_die, &address_size,
2865 &offset_size))
2866 return false;
2867
2868 Dwarf_Half version = 0;
2869 Dwarf_Off abbrev_offset = 0;
2870 uint64_t type_signature = 0;
2871 Dwarf_Off type_offset = 0;
2872 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2873 &version, &abbrev_offset,
2874 &address_size, &offset_size,
2875 &type_signature, &type_offset))
2876 return false;
2877
2878 int tag = dwarf_tag(&cu_kind);
2879
2880 if (tag == DW_TAG_compile_unit
2881 || tag == DW_TAG_partial_unit)
2882 {
2883 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2884 if (dwarf_debug_info() == die_dwarf)
2885 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2886 else if (alternate_dwarf_debug_info() == die_dwarf)
2887 source = ALT_DEBUG_INFO_DIE_SOURCE;
2888 else
2889 ABG_ASSERT_NOT_REACHED;
2890 }
2891 else if (tag == DW_TAG_type_unit)
2892 source = TYPE_UNIT_DIE_SOURCE;
2893 else
2894 return false;
2895
2896 return true;
2897 }
2898
2899 /// Getter for the DIE designated by an offset.
2900 ///
2901 /// @param source the source of the DIE to get.
2902 ///
2903 /// @param offset the offset of the DIE to get.
2904 ///
2905 /// @param die the resulting DIE. The pointer has to point to an
2906 /// allocated memory region.
2907 void
get_die_from_offset(die_source source,Dwarf_Off offset,Dwarf_Die * die) const2908 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
2909 {
2910 if (source == TYPE_UNIT_DIE_SOURCE)
2911 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2912 offset, die));
2913 else
2914 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2915 offset, die));
2916 }
2917
2918 public:
2919
2920 /// Add an entry to the relevant die->decl map.
2921 ///
2922 /// @param die the DIE to add the the map.
2923 ///
2924 /// @param decl the decl to consider.
2925 ///
2926 /// @param where_offset where in the DIE stream we logically are.
2927 ///
2928 /// @param do_associate_by_repr if true then this function
2929 /// associates the representation string of @p die with the
2930 /// declaration @p decl, in a corpus-wide manner. That is, in the
2931 /// entire current corpus, there is going to be just one declaration
2932 /// associated with a DIE of the string representation of @p die.
2933 ///
2934 /// @param do_associate_by_repr_per_tu if true, then this function
2935 /// associates the representation string of @p die with the
2936 /// declaration @p decl in a translation unit wide manner. That is,
2937 /// in the entire current translation unit, there is going to be
2938 /// just one declaration associated with a DIE of the string
2939 /// representation of @p die.
2940 void
associate_die_to_decl(Dwarf_Die * die,decl_base_sptr decl,size_t where_offset,bool do_associate_by_repr=false)2941 associate_die_to_decl(Dwarf_Die* die,
2942 decl_base_sptr decl,
2943 size_t where_offset,
2944 bool do_associate_by_repr = false)
2945 {
2946 const die_source source = get_die_source(die);
2947
2948 die_artefact_map_type& m =
2949 decl_die_artefact_maps().get_container(source);
2950
2951 size_t die_offset;
2952 if (do_associate_by_repr)
2953 {
2954 Dwarf_Die equiv_die;
2955 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
2956 /*die_as_type=*/false))
2957 return;
2958 die_offset = dwarf_dieoffset(&equiv_die);
2959 }
2960 else
2961 die_offset = dwarf_dieoffset(die);
2962
2963 m[die_offset] = decl;
2964 }
2965
2966 /// Lookup the decl for a given DIE.
2967 ///
2968 /// The returned decl is either the decl of the DIE that as the
2969 /// exact offset @p die_offset
2970 /// die_offset, or
2971 /// give
2972 ///
2973 /// @param die_offset the offset of the DIE to consider.
2974 ///
2975 /// @param source where the DIE represented by @p die_offset comes
2976 /// from.
2977 ///
2978 /// Note that "alternate debug info sections" is a GNU extension as
2979 /// of DWARF4 and is described at
2980 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
2981 ///
2982 /// @return the resulting decl, or null if no decl is associated to
2983 /// the DIE represented by @p die_offset.
2984 decl_base_sptr
lookup_decl_from_die_offset(Dwarf_Off die_offset,die_source source)2985 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
2986 {
2987 decl_base_sptr result =
2988 is_decl(lookup_artifact_from_die_offset(die_offset, source,
2989 /*die_as_type=*/false));
2990
2991 return result;
2992 }
2993
2994 /// Get the qualified name of a given DIE.
2995 ///
2996 /// If the name of the DIE was already computed before just return
2997 /// that name from a cache. Otherwise, build the name, cache it and
2998 /// return it.
2999 ///
3000 /// @param die the DIE to consider.
3001 ///
3002 /// @param where_offset where in the DIE stream we logically are.
3003 ///
3004 /// @return the interned string representing the qualified name of
3005 /// @p die.
3006 interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset)3007 get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3008 {
3009 ABG_ASSERT(die);
3010 die_istring_map_type& map =
3011 die_qualified_name_maps_.get_container(*this, die);
3012
3013 size_t die_offset = dwarf_dieoffset(die);
3014 die_istring_map_type::const_iterator i = map.find(die_offset);
3015
3016 if (i == map.end())
3017 {
3018 reader& rdr = *const_cast<reader*>(this);
3019 string qualified_name = die_qualified_name(rdr, die, where_offset);
3020 interned_string istr = env().intern(qualified_name);
3021 map[die_offset] = istr;
3022 return istr;
3023 }
3024
3025 return i->second;
3026 }
3027
3028 /// Get the qualified name of a given DIE.
3029 ///
3030 /// If the name of the DIE was already computed before just return
3031 /// that name from a cache. Otherwise, build the name, cache it and
3032 /// return it.
3033 ///
3034 /// @param die the DIE to consider.
3035 ///
3036 /// @param where_offset where in the DIE stream we logically are.
3037 ///
3038 /// @return the interned string representing the qualified name of
3039 /// @p die.
3040 interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset) const3041 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3042 {
3043 return const_cast<reader*>(this)->
3044 get_die_qualified_name(die, where_offset);
3045 }
3046
3047 /// Get the qualified name of a given DIE which is considered to be
3048 /// the DIE for a type.
3049 ///
3050 /// For instance, for a DW_TAG_subprogram DIE, this function
3051 /// computes the name of the function *type* that corresponds to the
3052 /// function.
3053 ///
3054 /// If the name of the DIE was already computed before just return
3055 /// that name from a cache. Otherwise, build the name, cache it and
3056 /// return it.
3057 ///
3058 /// @param die the DIE to consider.
3059 ///
3060 /// @param where_offset where in the DIE stream we logically are.
3061 ///
3062 /// @return the interned string representing the qualified name of
3063 /// @p die.
3064 interned_string
get_die_qualified_type_name(const Dwarf_Die * die,size_t where_offset) const3065 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3066 {
3067 ABG_ASSERT(die);
3068
3069 // The name of the translation unit die is "".
3070 if (die == cur_tu_die())
3071 return env().intern("");
3072
3073 die_istring_map_type& map =
3074 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3075 die);
3076
3077 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3078 die_istring_map_type::const_iterator i =
3079 map.find(die_offset);
3080
3081 if (i == map.end())
3082 {
3083 reader& rdr = *const_cast<reader*>(this);
3084 string qualified_name;
3085 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3086 if ((tag == DW_TAG_structure_type
3087 || tag == DW_TAG_class_type
3088 || tag == DW_TAG_union_type)
3089 && die_is_anonymous(die))
3090 {
3091 location l = die_location(*this, die);
3092 qualified_name = l ? l.expand() : "noloc";
3093 qualified_name = "unnamed-at-" + qualified_name;
3094 }
3095 else
3096 qualified_name =
3097 die_qualified_type_name(rdr, die, where_offset);
3098
3099 interned_string istr = env().intern(qualified_name);
3100 map[die_offset] = istr;
3101 return istr;
3102 }
3103
3104 return i->second;
3105 }
3106
3107 /// Get the pretty representation of a DIE that represents a type.
3108 ///
3109 /// For instance, for the DW_TAG_subprogram, this function computes
3110 /// the pretty representation of the type of the function, not the
3111 /// pretty representation of the function declaration.
3112 ///
3113 /// Once the pretty representation is computed, it's stored in a
3114 /// cache. Subsequent invocations of this function on the same DIE
3115 /// will yield the cached name.
3116 ///
3117 /// @param die the DIE to consider.
3118 ///
3119 /// @param where_offset where in the DIE stream we logically are.
3120 ///
3121 /// @return the interned_string that represents the pretty
3122 /// representation.
3123 interned_string
get_die_pretty_type_representation(const Dwarf_Die * die,size_t where_offset) const3124 get_die_pretty_type_representation(const Dwarf_Die *die,
3125 size_t where_offset) const
3126 {
3127 ABG_ASSERT(die);
3128 die_istring_map_type& map =
3129 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3130 die);
3131
3132 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3133 die_istring_map_type::const_iterator i = map.find(die_offset);
3134
3135 if (i == map.end())
3136 {
3137 reader& rdr = *const_cast<reader*>(this);
3138 string pretty_representation =
3139 die_pretty_print_type(rdr, die, where_offset);
3140 interned_string istr = env().intern(pretty_representation);
3141 map[die_offset] = istr;
3142 return istr;
3143 }
3144
3145 return i->second;
3146 }
3147
3148 /// Get the pretty representation of a DIE.
3149 ///
3150 /// Once the pretty representation is computed, it's stored in a
3151 /// cache. Subsequent invocations of this function on the same DIE
3152 /// will yield the cached name.
3153 ///
3154 /// @param die the DIE to consider.
3155 ///
3156 /// @param where_offset where in the DIE stream we logically are.
3157 ///
3158 /// @return the interned_string that represents the pretty
3159 /// representation.
3160 interned_string
get_die_pretty_representation(const Dwarf_Die * die,size_t where_offset) const3161 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3162 {
3163 ABG_ASSERT(die);
3164
3165 die_istring_map_type& map =
3166 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3167 die);
3168
3169 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3170 die_istring_map_type::const_iterator i = map.find(die_offset);
3171
3172 if (i == map.end())
3173 {
3174 reader& rdr = *const_cast<reader*>(this);
3175 string pretty_representation =
3176 die_pretty_print(rdr, die, where_offset);
3177 interned_string istr = env().intern(pretty_representation);
3178 map[die_offset] = istr;
3179 return istr;
3180 }
3181
3182 return i->second;
3183 }
3184
3185 /// Lookup the artifact that was built to represent a type that has
3186 /// the same pretty representation as the type denoted by a given
3187 /// DIE.
3188 ///
3189 /// Note that the DIE must have previously been associated with the
3190 /// artifact using the functions associate_die_to_decl or
3191 /// associate_die_to_type.
3192 ///
3193 /// Also, note that the scope of the lookup is the current ABI
3194 /// corpus.
3195 ///
3196 /// @param die the DIE to consider.
3197 ///
3198 /// @param where_offset where in the DIE stream we logically are.
3199 ///
3200 /// @return the type artifact found.
3201 type_or_decl_base_sptr
lookup_type_artifact_from_die(Dwarf_Die * die) const3202 lookup_type_artifact_from_die(Dwarf_Die *die) const
3203 {
3204 type_or_decl_base_sptr artifact =
3205 lookup_artifact_from_die(die, /*type_as_die=*/true);
3206 if (function_decl_sptr fn = is_function_decl(artifact))
3207 return fn->get_type();
3208 return artifact;
3209 }
3210
3211 /// Lookup the artifact that was built to represent a type or a
3212 /// declaration that has the same pretty representation as the type
3213 /// denoted by a given DIE.
3214 ///
3215 /// Note that the DIE must have previously been associated with the
3216 /// artifact using the functions associate_die_to_decl or
3217 /// associate_die_to_type.
3218 ///
3219 /// Also, note that the scope of the lookup is the current ABI
3220 /// corpus.
3221 ///
3222 /// @param die the DIE to consider.
3223 ///
3224 /// @param where_offset where in the DIE stream we logically are.
3225 ///
3226 /// @param die_as_type if true, it means the DIE is to be considered
3227 /// as a type.
3228 ///
3229 /// @return the artifact found.
3230 type_or_decl_base_sptr
lookup_artifact_from_die(const Dwarf_Die * die,bool die_as_type=false) const3231 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3232 {
3233 Dwarf_Die equiv_die;
3234 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3235 return type_or_decl_base_sptr();
3236
3237 const die_artefact_map_type& m =
3238 die_as_type
3239 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3240 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3241
3242 size_t die_offset = dwarf_dieoffset(&equiv_die);
3243 die_artefact_map_type::const_iterator i = m.find(die_offset);
3244
3245 if (i == m.end())
3246 return type_or_decl_base_sptr();
3247 return i->second;
3248 }
3249
3250 /// Lookup the artifact that was built to represent a type or a
3251 /// declaration that has the same pretty representation as the type
3252 /// denoted by the offset of a given DIE.
3253 ///
3254 /// Note that the DIE must have previously been associated with the
3255 /// artifact using either associate_die_to_decl or
3256 /// associate_die_to_type.
3257 ///
3258 /// Also, note that the scope of the lookup is the current ABI
3259 /// corpus.
3260 ///
3261 /// @param die the DIE to consider.
3262 ///
3263 /// @param where_offset where in the DIE stream we logically are.
3264 ///
3265 /// @param die_as_type if true, it means the DIE is to be considered
3266 /// as a type.
3267 ///
3268 /// @return the artifact found.
3269 type_or_decl_base_sptr
lookup_artifact_from_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type=false) const3270 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3271 die_source source,
3272 bool die_as_type = false) const
3273 {
3274 const die_artefact_map_type& m =
3275 die_as_type
3276 ? type_die_artefact_maps().get_container(source)
3277 : decl_die_artefact_maps().get_container(source);
3278
3279 die_artefact_map_type::const_iterator i = m.find(die_offset);
3280 if (i == m.end())
3281 return type_or_decl_base_sptr();
3282 return i->second;
3283 }
3284
3285 /// Get the language used to generate a given DIE.
3286 ///
3287 /// @param die the DIE to consider.
3288 ///
3289 /// @param lang the resulting language.
3290 ///
3291 /// @return true iff the language of the DIE was found.
3292 bool
get_die_language(const Dwarf_Die * die,translation_unit::language & lang) const3293 get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3294 {
3295 Dwarf_Die cu_die;
3296 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3297
3298 uint64_t l = 0;
3299 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3300 return false;
3301
3302 lang = dwarf_language_to_tu_language(l);
3303 return true;
3304 }
3305
3306 /// Test if a given DIE originates from a program written in the C
3307 /// language.
3308 ///
3309 /// @param die the DIE to consider.
3310 ///
3311 /// @return true iff @p die originates from a program in the C
3312 /// language.
3313 bool
die_is_in_c(const Dwarf_Die * die) const3314 die_is_in_c(const Dwarf_Die *die) const
3315 {
3316 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3317 if (!get_die_language(die, l))
3318 return false;
3319 return is_c_language(l);
3320 }
3321
3322 /// Test if a given DIE originates from a program written in the C++
3323 /// language.
3324 ///
3325 /// @param die the DIE to consider.
3326 ///
3327 /// @return true iff @p die originates from a program in the C++
3328 /// language.
3329 bool
die_is_in_cplus_plus(const Dwarf_Die * die) const3330 die_is_in_cplus_plus(const Dwarf_Die *die) const
3331 {
3332 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3333 if (!get_die_language(die, l))
3334 return false;
3335 return is_cplus_plus_language(l);
3336 }
3337
3338 /// Test if a given DIE originates from a program written either in
3339 /// C or C++.
3340 ///
3341 /// @param die the DIE to consider.
3342 ///
3343 /// @return true iff @p die originates from a program written either in
3344 /// C or C++.
3345 bool
die_is_in_c_or_cplusplus(const Dwarf_Die * die) const3346 die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3347 {
3348 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3349 if (!get_die_language(die, l))
3350 return false;
3351 return (is_cplus_plus_language(l) || is_c_language(l));
3352 }
3353
3354 /// Check if we can assume the One Definition Rule[1] to be relevant
3355 /// for the current translation unit.
3356 ///
3357 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3358 ///
3359 /// At the moment this returns true if the current translation unit
3360 /// is in C++ language. In that case, it's relevant to assume that
3361 /// we use optimizations based on the ODR.
3362 bool
odr_is_relevant() const3363 odr_is_relevant() const
3364 {return odr_is_relevant(cur_transl_unit()->get_language());}
3365
3366 /// Check if we can assume the One Definition Rule[1] to be relevant
3367 /// for a given language.
3368 ///
3369 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3370 ///
3371 /// At the moment this returns true if the language considered
3372 /// is C++, Java or Ada.
3373 bool
odr_is_relevant(translation_unit::language l) const3374 odr_is_relevant(translation_unit::language l) const
3375 {
3376 return (is_cplus_plus_language(l)
3377 || is_java_language(l)
3378 || is_ada_language(l));
3379 }
3380
3381 /// Check if we can assume the One Definition Rule to be relevant
3382 /// for a given DIE.
3383 ///
3384 /// @param die the DIE to consider.
3385 ///
3386 /// @return true if the ODR is relevant for @p die.
3387 bool
odr_is_relevant(Dwarf_Off die_offset,die_source source) const3388 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3389 {
3390 Dwarf_Die die;
3391 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3392 die_offset, &die));
3393 return odr_is_relevant(&die);
3394 }
3395
3396 /// Check if we can assume the One Definition Rule to be relevant
3397 /// for a given DIE.
3398 ///
3399 /// @param die the DIE to consider.
3400 ///
3401 /// @return true if the ODR is relevant for @p die.
3402 bool
odr_is_relevant(const Dwarf_Die * die) const3403 odr_is_relevant(const Dwarf_Die *die) const
3404 {
3405 translation_unit::language lang;
3406 if (!get_die_language(die, lang))
3407 return odr_is_relevant();
3408
3409 return odr_is_relevant(lang);
3410 }
3411
3412 /// Getter for the maps set that associates a decl DIE offset to an
3413 /// artifact.
3414 ///
3415 /// @return the maps set that associates a decl DIE offset to an
3416 /// artifact.
3417 die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps()3418 decl_die_artefact_maps()
3419 {return decl_die_artefact_maps_;}
3420
3421 /// Getter for the maps set that associates a decl DIE offset to an
3422 /// artifact.
3423 ///
3424 /// @return the maps set that associates a decl DIE offset to an
3425 /// artifact.
3426 const die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps() const3427 decl_die_artefact_maps() const
3428 {return decl_die_artefact_maps_;}
3429
3430 /// Getter for the maps set that associates a type DIE offset to an
3431 /// artifact.
3432 ///
3433 /// @return the maps set that associates a type DIE offset to an
3434 /// artifact.
3435 die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps()3436 type_die_artefact_maps()
3437 {return type_die_artefact_maps_;}
3438
3439 /// Getter for the maps set that associates a type DIE offset to an
3440 /// artifact.
3441 ///
3442 /// @return the maps set that associates a type DIE offset to an
3443 /// artifact.
3444 const die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps() const3445 type_die_artefact_maps() const
3446 {return type_die_artefact_maps_;}
3447
3448 /// Getter of the maps that associates function type representations
3449 /// to function types, inside a translation unit.
3450 ///
3451 /// @return the maps that associates function type representations
3452 /// to function types, inside a translation unit.
3453 istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps()3454 per_tu_repr_to_fn_type_maps()
3455 {return per_tu_repr_to_fn_type_maps_;}
3456
3457 /// Getter of the maps that associates function type representations
3458 /// to function types, inside a translation unit.
3459 ///
3460 /// @return the maps that associates function type representations
3461 /// to function types, inside a translation unit.
3462 const istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps() const3463 per_tu_repr_to_fn_type_maps() const
3464 {return per_tu_repr_to_fn_type_maps_;}
3465
3466 /// Associate the representation of a function type DIE to a given
3467 /// function type, inside the current translation unit.
3468 ///
3469 /// @param die the DIE to associate to the function type, using its
3470 /// representation.
3471 ///
3472 /// @param fn_type the function type to associate to @p die.
3473 void
associate_die_repr_to_fn_type_per_tu(const Dwarf_Die * die,const function_type_sptr & fn_type)3474 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3475 const function_type_sptr &fn_type)
3476 {
3477 if (!die_is_function_type(die))
3478 return;
3479
3480 interned_string repr =
3481 get_die_pretty_type_representation(die, /*where=*/0);
3482 ABG_ASSERT(!repr.empty());
3483
3484 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3485 }
3486
3487 /// Lookup the function type associated to a given function type
3488 /// DIE, in the current translation unit.
3489 ///
3490 /// @param die the DIE of function type to consider.
3491 ///
3492 /// @return the @ref function_type_sptr associated to @p die, or nil
3493 /// of no function_type is associated to @p die.
3494 function_type_sptr
lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die * die)3495 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3496 {
3497 if (!die_is_function_type(die))
3498 return function_type_sptr();
3499
3500 interned_string repr =
3501 get_die_pretty_representation(die, /*where=*/0);
3502 ABG_ASSERT(!repr.empty());
3503
3504 istring_fn_type_map_type::const_iterator i =
3505 per_tu_repr_to_fn_type_maps().find(repr);
3506
3507 if (i == per_tu_repr_to_fn_type_maps().end())
3508 return function_type_sptr();
3509
3510 return i->second;
3511 }
3512
3513 /// Set the canonical DIE offset of a given DIE.
3514 ///
3515 /// @param canonical_dies the vector that holds canonical DIEs.
3516 ///
3517 /// @param die_offset the offset of the DIE to set the canonical DIE
3518 /// for.
3519 ///
3520 /// @param canonical_die_offset the canonical DIE offset to
3521 /// associate to @p die_offset.
3522 void
set_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset,Dwarf_Off canonical_die_offset) const3523 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3524 Dwarf_Off die_offset,
3525 Dwarf_Off canonical_die_offset) const
3526 {
3527 canonical_dies[die_offset] = canonical_die_offset;}
3528
3529 /// Set the canonical DIE offset of a given DIE.
3530 ///
3531 ///
3532 /// @param die_offset the offset of the DIE to set the canonical DIE
3533 /// for.
3534 ///
3535 /// @param source the source of the DIE denoted by @p die_offset.
3536 ///
3537 /// @param canonical_die_offset the canonical DIE offset to
3538 /// associate to @p die_offset.
3539 ///
3540 /// @param die_as_type if true, it means that @p die_offset has to
3541 /// be considered as a type.
3542 void
set_canonical_die_offset(Dwarf_Off die_offset,die_source source,Dwarf_Off canonical_die_offset,bool die_as_type) const3543 set_canonical_die_offset(Dwarf_Off die_offset,
3544 die_source source,
3545 Dwarf_Off canonical_die_offset,
3546 bool die_as_type) const
3547 {
3548 offset_offset_map_type &canonical_dies =
3549 die_as_type
3550 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3551 get_container(source)
3552 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3553 get_container(source);
3554
3555 set_canonical_die_offset(canonical_dies,
3556 die_offset,
3557 canonical_die_offset);
3558 }
3559
3560 /// Set the canonical DIE offset of a given DIE.
3561 ///
3562 ///
3563 /// @param die the DIE to set the canonical DIE for.
3564 ///
3565 /// @param canonical_die_offset the canonical DIE offset to
3566 /// associate to @p die_offset.
3567 ///
3568 /// @param die_as_type if true, it means that @p die has to be
3569 /// considered as a type.
3570 void
set_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off canonical_die_offset,bool die_as_type) const3571 set_canonical_die_offset(const Dwarf_Die *die,
3572 Dwarf_Off canonical_die_offset,
3573 bool die_as_type) const
3574 {
3575 const die_source source = get_die_source(die);
3576
3577 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3578
3579 set_canonical_die_offset(die_offset, source,
3580 canonical_die_offset,
3581 die_as_type);
3582 }
3583
3584 /// Get the canonical DIE offset of a given DIE.
3585 ///
3586 /// @param canonical_dies the vector that contains canonical DIES.
3587 ///
3588 /// @param die_offset the offset of the DIE to consider.
3589 ///
3590 /// @return the canonical of the DIE denoted by @p die_offset, or
3591 /// zero if no canonical DIE was found.
3592 Dwarf_Off
get_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset) const3593 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3594 Dwarf_Off die_offset) const
3595 {
3596 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3597 if (it == canonical_dies.end())
3598 return 0;
3599 return it->second;
3600 }
3601
3602 /// Get the canonical DIE offset of a given DIE.
3603 ///
3604 /// @param die_offset the offset of the DIE to consider.
3605 ///
3606 /// @param source the source of the DIE denoted by @p die_offset.
3607 ///
3608 /// @param die_as_type if true, it means that @p is to be considered
3609 /// as a type DIE.
3610 ///
3611 /// @return the canonical of the DIE denoted by @p die_offset, or
3612 /// zero if no canonical DIE was found.
3613 Dwarf_Off
get_canonical_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type) const3614 get_canonical_die_offset(Dwarf_Off die_offset,
3615 die_source source,
3616 bool die_as_type) const
3617 {
3618 offset_offset_map_type &canonical_dies =
3619 die_as_type
3620 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3621 get_container(source)
3622 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3623 get_container(source);
3624
3625 return get_canonical_die_offset(canonical_dies, die_offset);
3626 }
3627
3628 /// Erase the canonical type of a given DIE.
3629 ///
3630 /// @param die_offset the offset of the DIE to consider.
3631 ///
3632 /// @param source the source of the canonical type.
3633 ///
3634 /// @param die_as_type if true, it means that @p is to be considered
3635 /// as a type DIE.
3636 ///
3637 /// @return the canonical of the DIE denoted by @p die_offset, or
3638 /// zero if no canonical DIE was found and erased..
3639 bool
erase_canonical_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type) const3640 erase_canonical_die_offset(Dwarf_Off die_offset,
3641 die_source source,
3642 bool die_as_type) const
3643 {
3644 offset_offset_map_type &canonical_dies =
3645 die_as_type
3646 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3647 get_container(source)
3648 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3649 get_container(source);
3650
3651 return canonical_dies.erase(die_offset);
3652 }
3653
3654
3655 /// Associate a DIE (representing a type) to the type that it
3656 /// represents.
3657 ///
3658 /// @param die the DIE to consider.
3659 ///
3660 /// @param type the type to associate the DIE to.
3661 ///
3662 /// @param where_offset where in the DIE stream we logically are.
3663 void
associate_die_to_type(const Dwarf_Die * die,type_base_sptr type,size_t where)3664 associate_die_to_type(const Dwarf_Die *die,
3665 type_base_sptr type,
3666 size_t where)
3667 {
3668 if (!type)
3669 return;
3670
3671 Dwarf_Die equiv_die;
3672 if (!get_or_compute_canonical_die(die, equiv_die, where,
3673 /*die_as_type=*/true))
3674 return;
3675
3676 die_artefact_map_type& m =
3677 type_die_artefact_maps().get_container(*this, &equiv_die);
3678
3679 size_t die_offset = dwarf_dieoffset(&equiv_die);
3680 m[die_offset] = type;
3681 }
3682
3683 /// Lookup the type associated to a given DIE.
3684 ///
3685 /// Note that the DIE must have been associated to type by a
3686 /// previous invocation of the function
3687 /// reader::associate_die_to_type().
3688 ///
3689 /// @param die the DIE to consider.
3690 ///
3691 /// @return the type associated to the DIE or NULL if no type is
3692 /// associated to the DIE.
3693 type_base_sptr
lookup_type_from_die(const Dwarf_Die * die) const3694 lookup_type_from_die(const Dwarf_Die* die) const
3695 {
3696 type_or_decl_base_sptr artifact =
3697 lookup_artifact_from_die(die, /*die_as_type=*/true);
3698 if (function_decl_sptr fn = is_function_decl(artifact))
3699 return fn->get_type();
3700 return is_type(artifact);
3701 }
3702
3703 /// Lookup the type associated to a DIE at a given offset, from a
3704 /// given source.
3705 ///
3706 /// Note that the DIE must have been associated to type by a
3707 /// previous invocation of the function
3708 /// reader::associate_die_to_type().
3709 ///
3710 /// @param die_offset the offset of the DIE to consider.
3711 ///
3712 /// @param source the source of the DIE to consider.
3713 ///
3714 /// @return the type associated to the DIE or NULL if no type is
3715 /// associated to the DIE.
3716 type_base_sptr
lookup_type_from_die_offset(size_t die_offset,die_source source) const3717 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3718 {
3719 type_base_sptr result;
3720 const die_artefact_map_type& m =
3721 type_die_artefact_maps().get_container(source);
3722 die_artefact_map_type::const_iterator i = m.find(die_offset);
3723 if (i != m.end())
3724 {
3725 if (function_decl_sptr fn = is_function_decl(i->second))
3726 return fn->get_type();
3727 result = is_type(i->second);
3728 }
3729
3730 if (!result)
3731 {
3732 // Maybe we are looking for a class type being constructed?
3733 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3734 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3735
3736 if (i != m.end())
3737 result = i->second;
3738 }
3739
3740 if (!result)
3741 {
3742 // Maybe we are looking for a function type being constructed?
3743 const die_function_type_map_type& m =
3744 die_wip_function_types_map(source);
3745 die_function_type_map_type::const_iterator i = m.find(die_offset);
3746
3747 if (i != m.end())
3748 result = i->second;
3749 }
3750
3751 return result;
3752 }
3753
3754 /// Getter of a map that associates a die that represents a
3755 /// class/struct with the declaration of the class, while the class
3756 /// is being constructed.
3757 ///
3758 /// @param source where the DIE is from.
3759 ///
3760 /// @return the map that associates a DIE to the class that is being
3761 /// built.
3762 const die_class_or_union_map_type&
die_wip_classes_map(die_source source) const3763 die_wip_classes_map(die_source source) const
3764 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3765
3766 /// Getter of a map that associates a die that represents a
3767 /// class/struct with the declaration of the class, while the class
3768 /// is being constructed.
3769 ///
3770 /// @param source where the DIE comes from.
3771 ///
3772 /// @return the map that associates a DIE to the class that is being
3773 /// built.
3774 die_class_or_union_map_type&
die_wip_classes_map(die_source source)3775 die_wip_classes_map(die_source source)
3776 {
3777 switch (source)
3778 {
3779 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3780 break;
3781 case ALT_DEBUG_INFO_DIE_SOURCE:
3782 return alternate_die_wip_classes_map_;
3783 case TYPE_UNIT_DIE_SOURCE:
3784 return type_unit_die_wip_classes_map_;
3785 case NO_DEBUG_INFO_DIE_SOURCE:
3786 case NUMBER_OF_DIE_SOURCES:
3787 ABG_ASSERT_NOT_REACHED;
3788 }
3789 return die_wip_classes_map_;
3790 }
3791
3792 /// Getter for a map that associates a die (that represents a
3793 /// function type) whith a function type, while the function type is
3794 /// being constructed (WIP == work in progress).
3795 ///
3796 /// @param source where the DIE comes from.n
3797 ///
3798 /// @return the map of wip function types.
3799 const die_function_type_map_type&
die_wip_function_types_map(die_source source) const3800 die_wip_function_types_map(die_source source) const
3801 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3802
3803 /// Getter for a map that associates a die (that represents a
3804 /// function type) whith a function type, while the function type is
3805 /// being constructed (WIP == work in progress).
3806 ///
3807 /// @param source where DIEs of the map come from.
3808 ///
3809 /// @return the map of wip function types.
3810 die_function_type_map_type&
die_wip_function_types_map(die_source source)3811 die_wip_function_types_map(die_source source)
3812 {
3813 switch (source)
3814 {
3815 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3816 break;
3817 case ALT_DEBUG_INFO_DIE_SOURCE:
3818 return alternate_die_wip_function_types_map_;
3819 case TYPE_UNIT_DIE_SOURCE:
3820 return type_unit_die_wip_function_types_map_;
3821 case NO_DEBUG_INFO_DIE_SOURCE:
3822 case NUMBER_OF_DIE_SOURCES:
3823 ABG_ASSERT_NOT_REACHED;
3824 }
3825 return die_wip_function_types_map_;
3826 }
3827
3828 /// Getter for a map that associates a die with a function decl
3829 /// which has a linkage name but no elf symbol yet.
3830 ///
3831 /// This is to fixup function decls with linkage names, but with no
3832 /// link to their underlying elf symbol. There are some DIEs like
3833 /// that in DWARF sometimes, especially when the compiler optimizes
3834 /// stuff aggressively.
3835 die_function_decl_map_type&
die_function_decl_with_no_symbol_map()3836 die_function_decl_with_no_symbol_map()
3837 {return die_function_with_no_symbol_map_;}
3838
3839 /// Return true iff a given offset is for the DIE of a class that is
3840 /// being built, but that is not fully built yet. WIP == "work in
3841 /// progress".
3842 ///
3843 /// @param offset the DIE offset to consider.
3844 ///
3845 /// @param source where the DIE of the map come from.
3846 ///
3847 /// @return true iff @p offset is the offset of the DIE of a class
3848 /// that is being currently built.
3849 bool
is_wip_class_die_offset(Dwarf_Off offset,die_source source) const3850 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3851 {
3852 die_class_or_union_map_type::const_iterator i =
3853 die_wip_classes_map(source).find(offset);
3854 return (i != die_wip_classes_map(source).end());
3855 }
3856
3857 /// Return true iff a given offset is for the DIE of a function type
3858 /// that is being built at the moment, but is not fully built yet.
3859 /// WIP == work in progress.
3860 ///
3861 /// @param offset DIE offset to consider.
3862 ///
3863 /// @param source where the DIE comes from.
3864 ///
3865 /// @return true iff @p offset is the offset of the DIE of a
3866 /// function type that is being currently built.
3867 bool
is_wip_function_type_die_offset(Dwarf_Off offset,die_source source) const3868 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3869 {
3870 die_function_type_map_type::const_iterator i =
3871 die_wip_function_types_map(source).find(offset);
3872 return (i != die_wip_function_types_map(source).end());
3873 }
3874
3875 /// Getter for the map of declaration-only classes that are to be
3876 /// resolved to their definition classes by the end of the corpus
3877 /// loading.
3878 ///
3879 /// @return a map of string -> vector of classes where the key is
3880 /// the fully qualified name of the class and the value is the
3881 /// vector of declaration-only class.
3882 const string_classes_map&
declaration_only_classes() const3883 declaration_only_classes() const
3884 {return decl_only_classes_map_;}
3885
3886 /// Getter for the map of declaration-only classes that are to be
3887 /// resolved to their definition classes by the end of the corpus
3888 /// loading.
3889 ///
3890 /// @return a map of string -> vector of classes where the key is
3891 /// the fully qualified name of the class and the value is the
3892 /// vector of declaration-only class.
3893 string_classes_map&
declaration_only_classes()3894 declaration_only_classes()
3895 {return decl_only_classes_map_;}
3896
3897 /// If a given class is a declaration-only class then stash it on
3898 /// the side so that at the end of the corpus reading we can resolve
3899 /// it to its definition.
3900 ///
3901 /// @param klass the class to consider.
3902 void
maybe_schedule_declaration_only_class_for_resolution(class_decl_sptr & klass)3903 maybe_schedule_declaration_only_class_for_resolution(class_decl_sptr& klass)
3904 {
3905 if (klass->get_is_declaration_only()
3906 && klass->get_definition_of_declaration() == 0)
3907 {
3908 string qn = klass->get_qualified_name();
3909 string_classes_map::iterator record =
3910 declaration_only_classes().find(qn);
3911 if (record == declaration_only_classes().end())
3912 declaration_only_classes()[qn].push_back(klass);
3913 else
3914 record->second.push_back(klass);
3915 }
3916 }
3917
3918 /// Test if a given declaration-only class has been scheduled for
3919 /// resolution to a defined class.
3920 ///
3921 /// @param klass the class to consider for the test.
3922 ///
3923 /// @return true iff @p klass is a declaration-only class and if
3924 /// it's been scheduled for resolution to a defined class.
3925 bool
is_decl_only_class_scheduled_for_resolution(class_decl_sptr & klass)3926 is_decl_only_class_scheduled_for_resolution(class_decl_sptr& klass)
3927 {
3928 if (klass->get_is_declaration_only())
3929 return (declaration_only_classes().find(klass->get_qualified_name())
3930 != declaration_only_classes().end());
3931
3932 return false;
3933 }
3934
3935 /// Compare two ABI artifacts in a context which canonicalization
3936 /// has not be done yet.
3937 ///
3938 /// @param l the left-hand-side operand of the comparison
3939 ///
3940 /// @param r the right-hand-side operand of the comparison.
3941 ///
3942 /// @return true if @p l equals @p r.
3943 bool
compare_before_canonicalisation(const type_or_decl_base_sptr & l,const type_or_decl_base_sptr & r)3944 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
3945 const type_or_decl_base_sptr &r)
3946 {
3947 if (!l || !r)
3948 return !!l == !!r;
3949
3950 const environment& e = l->get_environment();
3951 ABG_ASSERT(!e.canonicalization_is_done());
3952
3953 e.priv_->allow_type_comparison_results_caching(true);
3954 bool s0 = e.decl_only_class_equals_definition();
3955 e.decl_only_class_equals_definition(true);
3956 bool equal = l == r;
3957 e.decl_only_class_equals_definition(s0);
3958 e.priv_->clear_type_comparison_results_cache();
3959 e.priv_->allow_type_comparison_results_caching(false);
3960 return equal;
3961 }
3962
3963 /// Walk the declaration-only classes that have been found during
3964 /// the building of the corpus and resolve them to their definitions.
3965 void
resolve_declaration_only_classes()3966 resolve_declaration_only_classes()
3967 {
3968 vector<string> resolved_classes;
3969
3970 for (string_classes_map::iterator i =
3971 declaration_only_classes().begin();
3972 i != declaration_only_classes().end();
3973 ++i)
3974 {
3975 bool to_resolve = false;
3976 for (classes_type::iterator j = i->second.begin();
3977 j != i->second.end();
3978 ++j)
3979 if ((*j)->get_is_declaration_only()
3980 && ((*j)->get_definition_of_declaration() == 0))
3981 to_resolve = true;
3982
3983 if (!to_resolve)
3984 {
3985 resolved_classes.push_back(i->first);
3986 continue;
3987 }
3988
3989 // Now, for each decl-only class that have the current name
3990 // 'i->first', let's try to poke at the fully defined class
3991 // that is defined in the same translation unit as the
3992 // declaration.
3993 //
3994 // If we find one class (defined in the TU of the declaration)
3995 // that defines the declaration, then the declaration can be
3996 // resolved to that class.
3997 //
3998 // If no defining class is found in the TU of the declaration,
3999 // then there are possibly three cases to consider:
4000 //
4001 // 1/ There is exactly one class that defines the
4002 // declaration and that class is defined in another TU. In
4003 // this case, the declaration is resolved to that
4004 // definition.
4005 //
4006 // 2/ There are more than one class that define that
4007 // declaration and none of them is defined in the TU of the
4008 // declaration. If those classes are all different, then
4009 // the declaration is left unresolved.
4010 //
4011 // 3/ No class defines the declaration. In this case, the
4012 // declaration is left unresoved.
4013
4014 // So get the classes that might define the current
4015 // declarations which name is i->first.
4016 const type_base_wptrs_type *classes =
4017 lookup_class_types(i->first, *corpus());
4018 if (!classes)
4019 continue;
4020
4021 // This is a map that associates the translation unit path to
4022 // the class (that potentially defines the declarations that
4023 // we consider) that are defined in that translation unit. It
4024 // should stay ordered by using the TU path as key to ensure
4025 // stability of the order of classe definitions in ABIXML
4026 // output.
4027 map<string, class_decl_sptr> per_tu_class_map;
4028 for (type_base_wptrs_type::const_iterator c = classes->begin();
4029 c != classes->end();
4030 ++c)
4031 {
4032 class_decl_sptr klass = is_class_type(type_base_sptr(*c));
4033 ABG_ASSERT(klass);
4034
4035 klass = is_class_type(look_through_decl_only_class(klass));
4036 if (klass->get_is_declaration_only())
4037 continue;
4038
4039 string tu_path = klass->get_translation_unit()->get_absolute_path();
4040 if (tu_path.empty())
4041 continue;
4042
4043 // Build a map that associates the translation unit path
4044 // to the class (that potentially defines the declarations
4045 // that we consider) that are defined in that translation unit.
4046 per_tu_class_map[tu_path] = klass;
4047 }
4048
4049 if (!per_tu_class_map.empty())
4050 {
4051 // Walk the declarations to resolve and resolve them
4052 // either to the definitions that are in the same TU as
4053 // the declaration, or to the definition found elsewhere,
4054 // if there is only one such definition.
4055 for (classes_type::iterator j = i->second.begin();
4056 j != i->second.end();
4057 ++j)
4058 {
4059 if ((*j)->get_is_declaration_only()
4060 && ((*j)->get_definition_of_declaration() == 0))
4061 {
4062 string tu_path =
4063 (*j)->get_translation_unit()->get_absolute_path();
4064 map<string, class_decl_sptr>::const_iterator e =
4065 per_tu_class_map.find(tu_path);
4066 if (e != per_tu_class_map.end())
4067 (*j)->set_definition_of_declaration(e->second);
4068 else if (per_tu_class_map.size() == 1)
4069 (*j)->set_definition_of_declaration
4070 (per_tu_class_map.begin()->second);
4071 else
4072 {
4073 // We are in case where there are more than
4074 // one definition for the declaration. Let's
4075 // see if they are all equal. If they are,
4076 // then the declaration resolves to the
4077 // definition. Otherwise, we are in the case
4078 // 3/ described above.
4079 map<string,
4080 class_decl_sptr>::const_iterator it;
4081 class_decl_sptr first_class =
4082 per_tu_class_map.begin()->second;
4083 bool all_class_definitions_are_equal = true;
4084 for (it = per_tu_class_map.begin();
4085 it != per_tu_class_map.end();
4086 ++it)
4087 {
4088 if (it == per_tu_class_map.begin())
4089 continue;
4090 else
4091 {
4092 if (!compare_before_canonicalisation(it->second,
4093 first_class))
4094 {
4095 all_class_definitions_are_equal = false;
4096 break;
4097 }
4098 }
4099 }
4100 if (all_class_definitions_are_equal)
4101 (*j)->set_definition_of_declaration(first_class);
4102 }
4103 }
4104 }
4105 resolved_classes.push_back(i->first);
4106 }
4107 }
4108
4109 size_t num_decl_only_classes = declaration_only_classes().size(),
4110 num_resolved = resolved_classes.size();
4111 if (show_stats())
4112 cerr << "resolved " << num_resolved
4113 << " class declarations out of "
4114 << num_decl_only_classes
4115 << "\n";
4116
4117 for (vector<string>::const_iterator i = resolved_classes.begin();
4118 i != resolved_classes.end();
4119 ++i)
4120 declaration_only_classes().erase(*i);
4121
4122 if (show_stats() && !declaration_only_classes().empty())
4123 {
4124 cerr << "Here are the "
4125 << num_decl_only_classes - num_resolved
4126 << " unresolved class declarations:\n";
4127 for (string_classes_map::iterator i = declaration_only_classes().begin();
4128 i != declaration_only_classes().end();
4129 ++i)
4130 cerr << " " << i->first << "\n";
4131 }
4132 }
4133
4134 /// Getter for the map of declaration-only enums that are to be
4135 /// resolved to their definition enums by the end of the corpus
4136 /// loading.
4137 ///
4138 /// @return a map of string -> vector of enums where the key is
4139 /// the fully qualified name of the enum and the value is the
4140 /// vector of declaration-only enum.
4141 const string_enums_map&
declaration_only_enums() const4142 declaration_only_enums() const
4143 {return decl_only_enums_map_;}
4144
4145 /// Getter for the map of declaration-only enums that are to be
4146 /// resolved to their definition enums by the end of the corpus
4147 /// loading.
4148 ///
4149 /// @return a map of string -> vector of enums where the key is
4150 /// the fully qualified name of the enum and the value is the
4151 /// vector of declaration-only enum.
4152 string_enums_map&
declaration_only_enums()4153 declaration_only_enums()
4154 {return decl_only_enums_map_;}
4155
4156 /// If a given enum is a declaration-only enum then stash it on
4157 /// the side so that at the end of the corpus reading we can resolve
4158 /// it to its definition.
4159 ///
4160 /// @param enom the enum to consider.
4161 void
maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr & enom)4162 maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr& enom)
4163 {
4164 if (enom->get_is_declaration_only()
4165 && enom->get_definition_of_declaration() == 0)
4166 {
4167 string qn = enom->get_qualified_name();
4168 string_enums_map::iterator record =
4169 declaration_only_enums().find(qn);
4170 if (record == declaration_only_enums().end())
4171 declaration_only_enums()[qn].push_back(enom);
4172 else
4173 record->second.push_back(enom);
4174 }
4175 }
4176
4177 /// Test if a given declaration-only enum has been scheduled for
4178 /// resolution to a defined enum.
4179 ///
4180 /// @param enom the enum to consider for the test.
4181 ///
4182 /// @return true iff @p enom is a declaration-only enum and if
4183 /// it's been scheduled for resolution to a defined enum.
4184 bool
is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr & enom)4185 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4186 {
4187 if (enom->get_is_declaration_only())
4188 return (declaration_only_enums().find(enom->get_qualified_name())
4189 != declaration_only_enums().end());
4190
4191 return false;
4192 }
4193
4194 /// Walk the declaration-only enums that have been found during
4195 /// the building of the corpus and resolve them to their definitions.
4196 ///
4197 /// TODO: Do away with this function by factorizing it with
4198 /// resolve_declaration_only_classes. All declaration-only decls
4199 /// could be handled the same way as declaration-only-ness is a
4200 /// property of abigail::ir::decl_base now.
4201 void
resolve_declaration_only_enums()4202 resolve_declaration_only_enums()
4203 {
4204 vector<string> resolved_enums;
4205
4206 for (string_enums_map::iterator i =
4207 declaration_only_enums().begin();
4208 i != declaration_only_enums().end();
4209 ++i)
4210 {
4211 bool to_resolve = false;
4212 for (enums_type::iterator j = i->second.begin();
4213 j != i->second.end();
4214 ++j)
4215 if ((*j)->get_is_declaration_only()
4216 && ((*j)->get_definition_of_declaration() == 0))
4217 to_resolve = true;
4218
4219 if (!to_resolve)
4220 {
4221 resolved_enums.push_back(i->first);
4222 continue;
4223 }
4224
4225 // Now, for each decl-only enum that have the current name
4226 // 'i->first', let's try to poke at the fully defined enum
4227 // that is defined in the same translation unit as the
4228 // declaration.
4229 //
4230 // If we find one enum (defined in the TU of the declaration)
4231 // that defines the declaration, then the declaration can be
4232 // resolved to that enum.
4233 //
4234 // If no defining enum is found in the TU of the declaration,
4235 // then there are possibly three cases to consider:
4236 //
4237 // 1/ There is exactly one enum that defines the
4238 // declaration and that enum is defined in another TU. In
4239 // this case, the declaration is resolved to that
4240 // definition.
4241 //
4242 // 2/ There are more than one enum that define that
4243 // declaration and none of them is defined in the TU of the
4244 // declaration. In this case, the declaration is left
4245 // unresolved.
4246 //
4247 // 3/ No enum defines the declaration. In this case, the
4248 // declaration is left unresoved.
4249
4250 // So get the enums that might define the current
4251 // declarations which name is i->first.
4252 const type_base_wptrs_type *enums =
4253 lookup_enum_types(i->first, *corpus());
4254 if (!enums)
4255 continue;
4256
4257 // This is a map that associates the translation unit path to
4258 // the enum (that potentially defines the declarations that
4259 // we consider) that are defined in that translation unit. It
4260 // should stay ordered by using the TU path as key to ensure
4261 // stability of the order of enum definitions in ABIXML
4262 // output.
4263 map<string, enum_type_decl_sptr> per_tu_enum_map;
4264 for (type_base_wptrs_type::const_iterator c = enums->begin();
4265 c != enums->end();
4266 ++c)
4267 {
4268 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4269 ABG_ASSERT(enom);
4270
4271 enom = is_enum_type(look_through_decl_only_enum(enom));
4272 if (enom->get_is_declaration_only())
4273 continue;
4274
4275 string tu_path = enom->get_translation_unit()->get_absolute_path();
4276 if (tu_path.empty())
4277 continue;
4278
4279 // Build a map that associates the translation unit path
4280 // to the enum (that potentially defines the declarations
4281 // that we consider) that are defined in that translation unit.
4282 per_tu_enum_map[tu_path] = enom;
4283 }
4284
4285 if (!per_tu_enum_map.empty())
4286 {
4287 // Walk the declarations to resolve and resolve them
4288 // either to the definitions that are in the same TU as
4289 // the declaration, or to the definition found elsewhere,
4290 // if there is only one such definition.
4291 for (enums_type::iterator j = i->second.begin();
4292 j != i->second.end();
4293 ++j)
4294 {
4295 if ((*j)->get_is_declaration_only()
4296 && ((*j)->get_definition_of_declaration() == 0))
4297 {
4298 string tu_path =
4299 (*j)->get_translation_unit()->get_absolute_path();
4300 map<string, enum_type_decl_sptr>::const_iterator e =
4301 per_tu_enum_map.find(tu_path);
4302 if (e != per_tu_enum_map.end())
4303 (*j)->set_definition_of_declaration(e->second);
4304 else if (per_tu_enum_map.size() == 1)
4305 (*j)->set_definition_of_declaration
4306 (per_tu_enum_map.begin()->second);
4307 else
4308 {
4309 // We are in case where there are more than
4310 // one definition for the declaration. Let's
4311 // see if they are all equal. If they are,
4312 // then the declaration resolves to the
4313 // definition. Otherwise, we are in the case
4314 // 3/ described above.
4315 map<string,
4316 enum_type_decl_sptr>::const_iterator it;
4317 enum_type_decl_sptr first_enum =
4318 per_tu_enum_map.begin()->second;
4319 bool all_enum_definitions_are_equal = true;
4320 for (it = per_tu_enum_map.begin();
4321 it != per_tu_enum_map.end();
4322 ++it)
4323 {
4324 if (it == per_tu_enum_map.begin())
4325 continue;
4326 else
4327 {
4328 if (!compare_before_canonicalisation(it->second,
4329 first_enum))
4330 {
4331 all_enum_definitions_are_equal = false;
4332 break;
4333 }
4334 }
4335 }
4336 if (all_enum_definitions_are_equal)
4337 (*j)->set_definition_of_declaration(first_enum);
4338 }
4339 }
4340 }
4341 resolved_enums.push_back(i->first);
4342 }
4343 }
4344
4345 size_t num_decl_only_enums = declaration_only_enums().size(),
4346 num_resolved = resolved_enums.size();
4347 if (show_stats())
4348 cerr << "resolved " << num_resolved
4349 << " enum declarations out of "
4350 << num_decl_only_enums
4351 << "\n";
4352
4353 for (vector<string>::const_iterator i = resolved_enums.begin();
4354 i != resolved_enums.end();
4355 ++i)
4356 declaration_only_enums().erase(*i);
4357
4358 if (show_stats() && !declaration_only_enums().empty())
4359 {
4360 cerr << "Here are the "
4361 << num_decl_only_enums - num_resolved
4362 << " unresolved enum declarations:\n";
4363 for (string_enums_map::iterator i = declaration_only_enums().begin();
4364 i != declaration_only_enums().end();
4365 ++i)
4366 cerr << " " << i->first << "\n";
4367 }
4368 }
4369
4370 /// Test if a symbol belongs to a function of the current ABI
4371 /// corpus.
4372 ///
4373 /// This is a sub-routine of fixup_functions_with_no_symbols.
4374 ///
4375 /// @param fn the function symbol to consider.
4376 ///
4377 /// @returnt true if @p fn belongs to a function of the current ABI
4378 /// corpus.
4379 bool
symbol_already_belongs_to_a_function(elf_symbol_sptr & fn)4380 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4381 {
4382 corpus_sptr corp = corpus();
4383 if (!corp)
4384 return false;
4385
4386 string id = fn->get_id_string();
4387
4388 const vector<function_decl*> *fns = corp->lookup_functions(id);
4389 if (!fns)
4390 return false;
4391
4392 for (vector<function_decl*>::const_iterator i = fns->begin();
4393 i != fns->end();
4394 ++i)
4395 {
4396 function_decl* f = *i;
4397 ABG_ASSERT(f);
4398 if (f->get_symbol())
4399 return true;
4400 }
4401 return false;
4402 }
4403
4404 /// Some functions described by DWARF may have their linkage name
4405 /// set, but no link to their actual underlying elf symbol. When
4406 /// these are virtual member functions, comparing the enclosing type
4407 /// against another one which has its underlying symbol properly set
4408 /// might lead to spurious type changes.
4409 ///
4410 /// If the corpus contains a symbol with the same name as the
4411 /// linkage name of the function, then set up the link between the
4412 /// function and its underlying symbol.
4413 ///
4414 /// Note that for the moment, only virtual member functions are
4415 /// fixed up like this. This is because they really are the only
4416 /// fuctions of functions that can affect types (in spurious ways).
4417 void
fixup_functions_with_no_symbols()4418 fixup_functions_with_no_symbols()
4419 {
4420 corpus_sptr corp = corpus();
4421 if (!corp)
4422 return;
4423
4424 die_function_decl_map_type &fns_with_no_symbol =
4425 die_function_decl_with_no_symbol_map();
4426
4427 if (do_log())
4428 cerr << fns_with_no_symbol.size()
4429 << " functions to fixup, potentially\n";
4430
4431 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4432 i != fns_with_no_symbol.end();
4433 ++i)
4434 if (elf_symbol_sptr sym =
4435 corp->lookup_function_symbol(i->second->get_linkage_name()))
4436 {
4437 // So i->second is a virtual member function that was
4438 // previously scheduled to be set a function symbol.
4439 //
4440 // But if it appears that it now has a symbol already set,
4441 // then do not set a symbol to it again.
4442 //
4443 // Or if it appears that another virtual member function
4444 // from the current ABI Corpus, with the same linkage
4445 // (mangled) name has already been set a symbol, then do not
4446 // set a symbol to this function either. Otherwise, there
4447 // will be two virtual member functions with the same symbol
4448 // in the class and that leads to spurious hard-to-debug
4449 // change reports later down the road.
4450 if (i->second->get_symbol()
4451 || symbol_already_belongs_to_a_function(sym))
4452 continue;
4453
4454 ABG_ASSERT(is_member_function(i->second));
4455 ABG_ASSERT(get_member_function_is_virtual(i->second));
4456 i->second->set_symbol(sym);
4457 // The function_decl now has an associated (public) ELF symbol so
4458 // it ought to be advertised as being public.
4459 i->second->set_is_in_public_symbol_table(true);
4460 // Add the function to the set of exported decls of the
4461 // current corpus.
4462 maybe_add_fn_to_exported_decls(i->second.get());
4463 if (do_log())
4464 cerr << "fixed up '"
4465 << i->second->get_pretty_representation()
4466 << "' with symbol '"
4467 << sym->get_id_string()
4468 << "'\n";
4469 }
4470
4471 fns_with_no_symbol.clear();
4472 }
4473
4474 /// Return a reference to the vector containing the types created
4475 /// during the binary analysis but that are not tied to a given
4476 /// DWARF DIE.
4477 ///
4478 /// @return reference to the vector containing the types created
4479 /// during the binary analysis but that are not tied to a given
4480 /// DWARF DIE.
4481 const vector<type_base_sptr>&
types_to_canonicalize() const4482 types_to_canonicalize() const
4483 {return types_to_canonicalize_;}
4484
4485 /// Clear the containers holding types to canonicalize.
4486 void
clear_types_to_canonicalize()4487 clear_types_to_canonicalize()
4488 {
4489 types_to_canonicalize_.clear();
4490 }
4491
4492 /// Types that were created but not tied to a particular DIE, must
4493 /// be scheduled for late canonicalization using this method.
4494 ///
4495 /// @param t the type to schedule for late canonicalization.
4496 void
schedule_type_for_late_canonicalization(const type_base_sptr & t)4497 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4498 {
4499 types_to_canonicalize_.push_back(t);
4500 }
4501
4502 /// Canonicalize types which DIE offsets are stored in vectors on
4503 /// the side. This is a sub-routine of
4504 /// reader::perform_late_type_canonicalizing().
4505 ///
4506 /// @param source where the DIE of the types to canonicalize are
4507 /// from.
4508 void
canonicalize_types_scheduled()4509 canonicalize_types_scheduled()
4510 {
4511 tools_utils::timer cn_timer;
4512 if (do_log())
4513 {
4514 cerr << "going to canonicalize types";
4515 corpus_sptr c = corpus();
4516 if (c)
4517 cerr << " of corpus " << corpus()->get_path();
4518 cn_timer.start();
4519 }
4520
4521 if (!types_to_canonicalize().empty())
4522 {
4523 tools_utils::timer single_type_cn_timer;
4524 size_t total = types_to_canonicalize().size();
4525 if (do_log())
4526 cerr << total << " Types to canonicalize\n";
4527 size_t i = 1;
4528 for (vector<type_base_sptr>::const_iterator it =
4529 types_to_canonicalize().begin();
4530 it != types_to_canonicalize().end();
4531 ++it, ++i)
4532 {
4533 if (do_log())
4534 {
4535 cerr << "canonicalizing type "
4536 << get_pretty_representation(*it, false)
4537 << " [" << i << "/" << total << "]";
4538 if (corpus_sptr c = corpus())
4539 cerr << "@" << c->get_path();
4540 cerr << " ...";
4541 single_type_cn_timer.start();
4542 }
4543 canonicalize(*it);
4544 if (do_log())
4545 {
4546 single_type_cn_timer.stop();
4547 cerr << "DONE:"
4548 << single_type_cn_timer
4549 << "\n";
4550 }
4551 }
4552 }
4553
4554 if (do_log())
4555 {
4556 cn_timer.stop();
4557 cerr << "finished canonicalizing types";
4558 corpus_sptr c = corpus();
4559 if (c)
4560 cerr << " of corpus " << corpus()->get_path();
4561 cerr << ": (" << cn_timer << ")\n";
4562 }
4563 }
4564
4565 /// Compute the number of canonicalized and missed types in the late
4566 /// canonicalization phase.
4567 ///
4568 /// @param source where the DIEs of the canonicalized types are
4569 /// from.
4570 ///
4571 /// @param canonicalized the number of types that got canonicalized
4572 /// is added to the value already present in this parameter.
4573 ///
4574 /// @param missed the number of types scheduled for late
4575 /// canonicalization and which couldn't be canonicalized (for a
4576 /// reason) is added to the value already present in this parameter.
4577 void
add_late_canonicalized_types_stats(size_t & canonicalized,size_t & missed) const4578 add_late_canonicalized_types_stats(size_t& canonicalized,
4579 size_t& missed) const
4580 {
4581 for (auto t : types_to_canonicalize())
4582 {
4583 if (t->get_canonical_type())
4584 ++canonicalized;
4585 else
4586 ++missed;
4587 }
4588 }
4589
4590 // Look at the types that need to be canonicalized after the
4591 // translation unit has been constructed and canonicalize them.
4592 void
perform_late_type_canonicalizing()4593 perform_late_type_canonicalizing()
4594 {
4595 canonicalize_types_scheduled();
4596
4597 if (show_stats())
4598 {
4599 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4600 add_late_canonicalized_types_stats(num_canonicalized,
4601 num_missed);
4602 total = num_canonicalized + num_missed;
4603 cerr << "binary: "
4604 << elf_path()
4605 << "\n";
4606 cerr << " # late canonicalized types: "
4607 << num_canonicalized;
4608 if (total)
4609 cerr << " (" << num_canonicalized * 100 / total << "%)";
4610 cerr << "\n"
4611 << " # missed canonicalization opportunities: "
4612 << num_missed;
4613 if (total)
4614 cerr << " (" << num_missed * 100 / total << "%)";
4615 cerr << "\n";
4616 }
4617
4618 }
4619
4620 const die_tu_map_type&
die_tu_map() const4621 die_tu_map() const
4622 {return die_tu_map_;}
4623
4624 die_tu_map_type&
die_tu_map()4625 die_tu_map()
4626 {return die_tu_map_;}
4627
4628 /// Getter for the map that associates a translation unit DIE to the
4629 /// vector of imported unit points that it contains.
4630 ///
4631 /// @param source where the DIEs are from.
4632 ///
4633 /// @return the map.
4634 const tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source) const4635 tu_die_imported_unit_points_map(die_source source) const
4636 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4637
4638 /// Getter for the map that associates a translation unit DIE to the
4639 /// vector of imported unit points that it contains.
4640 ///
4641 /// @param source where the DIEs are from.
4642 ///
4643 /// @return the map.
4644 tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source)4645 tu_die_imported_unit_points_map(die_source source)
4646 {
4647 switch (source)
4648 {
4649 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4650 break;
4651 case ALT_DEBUG_INFO_DIE_SOURCE:
4652 return alt_tu_die_imported_unit_points_map_;
4653 case TYPE_UNIT_DIE_SOURCE:
4654 return type_units_tu_die_imported_unit_points_map_;
4655 case NO_DEBUG_INFO_DIE_SOURCE:
4656 case NUMBER_OF_DIE_SOURCES:
4657 // We cannot reach this point.
4658 ABG_ASSERT_NOT_REACHED;
4659 }
4660 return tu_die_imported_unit_points_map_;
4661 }
4662
4663 /// Reset the current corpus being constructed.
4664 ///
4665 /// This actually deletes the current corpus being constructed.
4666 void
reset_corpus()4667 reset_corpus()
4668 {corpus().reset();}
4669
4670 /// Get the map that associates each DIE to its parent DIE. This is
4671 /// for DIEs coming from the main debug info sections.
4672 ///
4673 /// @param source where the DIEs in the map come from.
4674 ///
4675 /// @return the DIE -> parent map.
4676 const offset_offset_map_type&
die_parent_map(die_source source) const4677 die_parent_map(die_source source) const
4678 {return const_cast<reader*>(this)->die_parent_map(source);}
4679
4680 /// Get the map that associates each DIE to its parent DIE. This is
4681 /// for DIEs coming from the main debug info sections.
4682 ///
4683 /// @param source where the DIEs in the map come from.
4684 ///
4685 /// @return the DIE -> parent map.
4686 offset_offset_map_type&
die_parent_map(die_source source)4687 die_parent_map(die_source source)
4688 {
4689 switch (source)
4690 {
4691 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4692 break;
4693 case ALT_DEBUG_INFO_DIE_SOURCE:
4694 return alternate_die_parent_map_;
4695 case TYPE_UNIT_DIE_SOURCE:
4696 return type_section_die_parent_map();
4697 case NO_DEBUG_INFO_DIE_SOURCE:
4698 case NUMBER_OF_DIE_SOURCES:
4699 ABG_ASSERT_NOT_REACHED;
4700 }
4701 return primary_die_parent_map_;
4702 }
4703
4704 const offset_offset_map_type&
type_section_die_parent_map() const4705 type_section_die_parent_map() const
4706 {return type_section_die_parent_map_;}
4707
4708 offset_offset_map_type&
type_section_die_parent_map()4709 type_section_die_parent_map()
4710 {return type_section_die_parent_map_;}
4711
4712 /// Getter of the current translation unit.
4713 ///
4714 /// @return the current translation unit being constructed.
4715 const translation_unit_sptr&
cur_transl_unit() const4716 cur_transl_unit() const
4717 {return cur_tu_;}
4718
4719 /// Getter of the current translation unit.
4720 ///
4721 /// @return the current translation unit being constructed.
4722 translation_unit_sptr&
cur_transl_unit()4723 cur_transl_unit()
4724 {return cur_tu_;}
4725
4726 /// Setter of the current translation unit.
4727 ///
4728 /// @param tu the current translation unit being constructed.
4729 void
cur_transl_unit(translation_unit_sptr tu)4730 cur_transl_unit(translation_unit_sptr tu)
4731 {
4732 if (tu)
4733 cur_tu_ = tu;
4734 }
4735
4736 /// Return the global scope of the current translation unit.
4737 ///
4738 /// @return the global scope of the current translation unit.
4739 const scope_decl_sptr&
global_scope() const4740 global_scope() const
4741 {return cur_transl_unit()->get_global_scope();}
4742
4743 /// Return a scope that is nil.
4744 ///
4745 /// @return a scope that is nil.
4746 const scope_decl_sptr&
nil_scope() const4747 nil_scope() const
4748 {return nil_scope_;}
4749
4750 const scope_stack_type&
scope_stack() const4751 scope_stack() const
4752 {return scope_stack_;}
4753
4754 scope_stack_type&
scope_stack()4755 scope_stack()
4756 {return scope_stack_;}
4757
4758 scope_decl*
current_scope()4759 current_scope()
4760 {
4761 if (scope_stack().empty())
4762 {
4763 if (cur_transl_unit())
4764 scope_stack().push(cur_transl_unit()->get_global_scope().get());
4765 }
4766 return scope_stack().top();
4767 }
4768
4769 list<var_decl_sptr>&
var_decls_to_re_add_to_tree()4770 var_decls_to_re_add_to_tree()
4771 {return var_decls_to_add_;}
4772
4773 /// The section containing the symbol table from the current ELF
4774 /// file.
4775 ///
4776 /// Note that after it's first invocation, this function caches the
4777 /// symbol table that it found. Subsequent invocations just return
4778 /// the cached symbol table section.
4779 ///
4780 /// @return the symbol table section if found
4781 Elf_Scn*
find_symbol_table_section() const4782 find_symbol_table_section() const
4783 {return const_cast<Elf_Scn*>(abigail::elf::reader::find_symbol_table_section());}
4784
4785 /// Lookup an elf symbol, referred to by its index, from the .symtab
4786 /// section.
4787 ///
4788 /// The resulting symbol returned is an instance of a GElf_Sym, from
4789 /// the libelf library.
4790 ///
4791 /// @param symbol_index the index of the symbol to look up.
4792 ///
4793 /// @param elf_sym out parameter. This is set to the resulting ELF
4794 /// symbol iff the function returns TRUE, meaning the symbol was
4795 /// found.
4796 ///
4797 /// @return TRUE iff the symbol was found.
4798 bool
lookup_native_elf_symbol_from_index(size_t symbol_index,GElf_Sym & elf_sym)4799 lookup_native_elf_symbol_from_index(size_t symbol_index, GElf_Sym &elf_sym)
4800 {
4801 Elf_Scn* symtab_section = find_symbol_table_section();
4802 if (!symtab_section)
4803 return false;
4804
4805 Elf_Data* symtab = elf_getdata(symtab_section, 0);
4806 ABG_ASSERT(symtab);
4807
4808 if (!gelf_getsym(symtab, symbol_index, &elf_sym))
4809 return false;
4810
4811 return true;
4812 }
4813
4814 /// Test if a DIE represents a decl (function or variable) that has
4815 /// a symbol that is exported, whatever that means. This is
4816 /// supposed to work for Linux Kernel binaries as well.
4817 ///
4818 /// This is useful to limit the amount of DIEs taken into account to
4819 /// the strict limit of what an ABI actually means. Limiting the
4820 /// volume of DIEs analyzed this way is an important optimization to
4821 /// keep big binaries "manageable" by libabigail.
4822 ///
4823 /// @param DIE the die to consider.
4824 bool
is_decl_die_with_exported_symbol(const Dwarf_Die * die)4825 is_decl_die_with_exported_symbol(const Dwarf_Die *die)
4826 {
4827 if (!die || !die_is_decl(die))
4828 return false;
4829
4830 bool result = false, address_found = false, symbol_is_exported = false;;
4831 Dwarf_Addr decl_symbol_address = 0;
4832
4833 if (die_is_variable_decl(die))
4834 {
4835 if ((address_found = get_variable_address(die, decl_symbol_address)))
4836 symbol_is_exported =
4837 !!variable_symbol_is_exported(decl_symbol_address);
4838 }
4839 else if (die_is_function_decl(die))
4840 {
4841 if ((address_found = get_function_address(die, decl_symbol_address)))
4842 symbol_is_exported =
4843 !!function_symbol_is_exported(decl_symbol_address);
4844 }
4845
4846 if (address_found)
4847 result = symbol_is_exported;
4848
4849 return result;
4850 }
4851
4852 /// This is a sub-routine of maybe_adjust_fn_sym_address and
4853 /// maybe_adjust_var_sym_address.
4854 ///
4855 /// Given an address that we got by looking at some debug
4856 /// information (e.g, a symbol's address referred to by a DWARF
4857 /// TAG), If the ELF file we are interested in is a shared library
4858 /// or an executable, then adjust the address to be coherent with
4859 /// where the executable (or shared library) is loaded. That way,
4860 /// the address can be used to look for symbols in the executable or
4861 /// shared library.
4862 ///
4863 /// @return the adjusted address, or the same address as @p addr if
4864 /// it didn't need any adjustment.
4865 Dwarf_Addr
maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const4866 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4867 {
4868 if (addr == 0)
4869 return addr;
4870
4871 GElf_Ehdr eh_mem;
4872 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4873
4874 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4875 {
4876 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4877 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4878 dwarf_elf_load_address));
4879 ABG_ASSERT(get_binary_load_address(elf_handle(),
4880 elf_load_address));
4881 if (dwarf_is_splitted()
4882 && (dwarf_elf_load_address != elf_load_address))
4883 // This means that in theory the DWARF and the executable are
4884 // not loaded at the same address. And addr is meaningful
4885 // only in the context of the DWARF.
4886 //
4887 // So let's transform addr into an offset relative to where
4888 // the DWARF is loaded, and let's add that relative offset
4889 // to the load address of the executable. That way, addr
4890 // becomes meaningful in the context of the executable and
4891 // can thus be used to compare against the address of
4892 // symbols of the executable, for instance.
4893 addr = addr - dwarf_elf_load_address + elf_load_address;
4894 }
4895
4896 return addr;
4897 }
4898
4899 /// For a relocatable (*.o) elf file, this function expects an
4900 /// absolute address, representing a function symbol. It then
4901 /// extracts the address of the .text section from the symbol
4902 /// absolute address to get the relative address of the function
4903 /// from the beginning of the .text section.
4904 ///
4905 /// For executable or shared library, this function expects an
4906 /// address of a function symbol that was retrieved by looking at a
4907 /// DWARF "file". The function thus adjusts the address to make it
4908 /// be meaningful in the context of the ELF file.
4909 ///
4910 /// In both cases, the address can then be compared against the
4911 /// st_value field of a function symbol from the ELF file.
4912 ///
4913 /// @param addr an adress for a function symbol that was retrieved
4914 /// from a DWARF file.
4915 ///
4916 /// @return the (possibly) adjusted address, or just @p addr if no
4917 /// adjustment took place.
4918 Dwarf_Addr
maybe_adjust_fn_sym_address(Dwarf_Addr addr) const4919 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4920 {
4921 if (addr == 0)
4922 return addr;
4923
4924 Elf* elf = elf_handle();
4925 GElf_Ehdr eh_mem;
4926 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4927
4928 if (elf_header->e_type == ET_REL)
4929 // We are looking at a relocatable file. In this case, we don't
4930 // do anything because:
4931 //
4932 // 1/ the addresses from DWARF are absolute (relative to the
4933 // beginning of the relocatable file)
4934 //
4935 // 2/ The ELF symbol addresses that we store in our lookup
4936 // tables are translated from section-related to absolute as
4937 // well. So we don't have anything to do at this point for
4938 // ET_REL files.
4939 ;
4940 else
4941 addr = maybe_adjust_address_for_exec_or_dyn(addr);
4942
4943 return addr;
4944 }
4945
4946 /// For a relocatable (*.o) elf file, this function expects an
4947 /// absolute address, representing a global variable symbol. It
4948 /// then extracts the address of the {.data,.data1,.rodata,.bss}
4949 /// section from the symbol absolute address to get the relative
4950 /// address of the variable from the beginning of the data section.
4951 ///
4952 /// For executable or shared library, this function expects an
4953 /// address of a variable symbol that was retrieved by looking at a
4954 /// DWARF "file". The function thus adjusts the address to make it
4955 /// be meaningful in the context of the ELF file.
4956 ///
4957 /// In both cases, the address can then be compared against the
4958 /// st_value field of a function symbol from the ELF file.
4959 ///
4960 /// @param addr an address for a global variable symbol that was
4961 /// retrieved from a DWARF file.
4962 ///
4963 /// @return the (possibly) adjusted address, or just @p addr if no
4964 /// adjustment took place.
4965 Dwarf_Addr
maybe_adjust_var_sym_address(Dwarf_Addr addr) const4966 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
4967 {
4968 Elf* elf = elf_handle();
4969 GElf_Ehdr eh_mem;
4970 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4971
4972 if (elf_header->e_type == ET_REL)
4973 // We are looking at a relocatable file. In this case, we don't
4974 // do anything because:
4975 //
4976 // 1/ the addresses from DWARF are absolute (relative to the
4977 // beginning of the relocatable file)
4978 //
4979 // 2/ The ELF symbol addresses that we store in our lookup
4980 // tables are translated from section-related to absolute as
4981 // well. So we don't have anything to do at this point for
4982 // ET_REL files.
4983 ;
4984 else
4985 addr = maybe_adjust_address_for_exec_or_dyn(addr);
4986
4987 return addr;
4988 }
4989
4990 /// Get the first exported function address in the set of addresses
4991 /// referred to by the DW_AT_ranges attribute of a given DIE.
4992 ///
4993 /// @param die the DIE we are considering.
4994 ///
4995 /// @param address output parameter. This is set to the first
4996 /// address found in the sequence pointed to by the DW_AT_ranges
4997 /// attribute found on the DIE @p die, iff the function returns
4998 /// true. Otherwise, no value is set into this output parameter.
4999 ///
5000 /// @return true iff the DIE @p die does have a DW_AT_ranges
5001 /// attribute and an address of an exported function was found in
5002 /// its sequence value.
5003 bool
get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die * die,Dwarf_Addr & address) const5004 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5005 Dwarf_Addr& address) const
5006 {
5007 Dwarf_Addr base;
5008 Dwarf_Addr end_addr;
5009 ptrdiff_t offset = 0;
5010
5011 do
5012 {
5013 Dwarf_Addr addr = 0, fn_addr = 0;
5014 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5015 {
5016 fn_addr = maybe_adjust_fn_sym_address(addr);
5017 if (function_symbol_is_exported(fn_addr))
5018 {
5019 address = fn_addr;
5020 return true;
5021 }
5022 }
5023 } while (offset > 0);
5024 return false;
5025 }
5026
5027 /// Get the address of the function.
5028 ///
5029 /// The address of the function is considered to be the value of the
5030 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5031 /// only) to not point to an absolute address anymore, but rather to
5032 /// the address of the function inside the .text segment.
5033 ///
5034 /// @param function_die the die of the function to consider.
5035 ///
5036 /// @param address the resulting address iff the function returns
5037 /// true.
5038 ///
5039 /// @return true if the function address was found.
5040 bool
get_function_address(const Dwarf_Die * function_die,Dwarf_Addr & address) const5041 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5042 {
5043 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5044 DW_AT_low_pc, address))
5045 // So no DW_AT_low_pc was found. Let's see if the function DIE
5046 // has got a DW_AT_ranges attribute instead. If it does, the
5047 // first address of the set of addresses represented by the
5048 // value of that DW_AT_ranges represents the function (symbol)
5049 // address we are looking for.
5050 if (!get_first_exported_fn_address_from_DW_AT_ranges
5051 (const_cast<Dwarf_Die*>(function_die),
5052 address))
5053 return false;
5054
5055 address = maybe_adjust_fn_sym_address(address);
5056 return true;
5057 }
5058
5059 /// Get the address of the global variable.
5060 ///
5061 /// The address of the global variable is considered to be the value
5062 /// of the DW_AT_location attribute, possibly adjusted (in
5063 /// relocatable files only) to not point to an absolute address
5064 /// anymore, but rather to the address of the global variable inside
5065 /// the data segment.
5066 ///
5067 /// @param variable_die the die of the function to consider.
5068 ///
5069 /// @param address the resulting address iff this function returns
5070 /// true.
5071 ///
5072 /// @return true if the variable address was found.
5073 bool
get_variable_address(const Dwarf_Die * variable_die,Dwarf_Addr & address) const5074 get_variable_address(const Dwarf_Die* variable_die,
5075 Dwarf_Addr& address) const
5076 {
5077 bool is_tls_address = false;
5078 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5079 address, is_tls_address))
5080 return false;
5081 if (!is_tls_address)
5082 address = maybe_adjust_var_sym_address(address);
5083 return true;
5084 }
5085
5086 /// Getter of the exported decls builder object.
5087 ///
5088 /// @return the exported decls builder.
5089 corpus::exported_decls_builder*
exported_decls_builder()5090 exported_decls_builder()
5091 {return corpus()->get_exported_decls_builder().get();}
5092
5093 /// Getter of the "load_all_types" flag. This flag tells if all the
5094 /// types (including those not reachable by public declarations) are
5095 /// to be read and represented in the final ABI corpus.
5096 ///
5097 /// @return the load_all_types flag.
5098 bool
load_all_types() const5099 load_all_types() const
5100 {return options().load_all_types;}
5101
5102 /// Setter of the "load_all_types" flag. This flag tells if all the
5103 /// types (including those not reachable by public declarations) are
5104 /// to be read and represented in the final ABI corpus.
5105 ///
5106 /// @param f the new load_all_types flag.
5107 void
load_all_types(bool f)5108 load_all_types(bool f)
5109 {options().load_all_types = f;}
5110
5111 bool
load_in_linux_kernel_mode() const5112 load_in_linux_kernel_mode() const
5113 {return options().load_in_linux_kernel_mode;}
5114
5115 void
load_in_linux_kernel_mode(bool f)5116 load_in_linux_kernel_mode(bool f)
5117 {options().load_in_linux_kernel_mode = f;}
5118
5119 /// Test if it's allowed to assume that the DWARF debug info has
5120 /// been factorized (for instance, with the DWZ tool) so that if two
5121 /// type DIEs originating from the .gnu_debugaltlink section have
5122 /// different offsets, they represent different types.
5123 ///
5124 /// @return true iff we can assume that the DWARF debug info has
5125 /// been factorized.
5126 bool
leverage_dwarf_factorization() const5127 leverage_dwarf_factorization() const
5128 {
5129 if (!leverage_dwarf_factorization_.has_value())
5130 {
5131 if (options().leverage_dwarf_factorization
5132 && elf_helpers::find_section_by_name(elf_handle(),
5133 ".gnu_debugaltlink"))
5134 leverage_dwarf_factorization_ = true;
5135 else
5136 leverage_dwarf_factorization_ = false;
5137 }
5138 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5139
5140 return *leverage_dwarf_factorization_;
5141 }
5142 /// Getter of the "show_stats" flag.
5143 ///
5144 /// This flag tells if we should emit statistics about various
5145 /// internal stuff.
5146 ///
5147 /// @return the value of the flag.
5148 bool
show_stats() const5149 show_stats() const
5150 {return options().show_stats;}
5151
5152 /// Setter of the "show_stats" flag.
5153 ///
5154 /// This flag tells if we should emit statistics about various
5155 /// internal stuff.
5156 ///
5157 /// @param f the value of the flag.
5158 void
show_stats(bool f)5159 show_stats(bool f)
5160 {options().show_stats = f;}
5161
5162 /// Getter of the "do_log" flag.
5163 ///
5164 /// This flag tells if we should log about various internal
5165 /// details.
5166 ///
5167 /// return the "do_log" flag.
5168 bool
do_log() const5169 do_log() const
5170 {return options().do_log;}
5171
5172 /// Setter of the "do_log" flag.
5173 ///
5174 /// This flag tells if we should log about various internal details.
5175 ///
5176 /// @param f the new value of the flag.
5177 void
do_log(bool f)5178 do_log(bool f)
5179 {options().do_log = f;}
5180
5181 /// Walk the DIEs under a given die and for each child, populate the
5182 /// die -> parent map to record the child -> parent relationship
5183 /// that
5184 /// exists between the child and the given die.
5185 ///
5186 /// The function also builds the vector of places where units are
5187 /// imported.
5188 ///
5189 /// This is done recursively as for each child DIE, this function
5190 /// walks its children as well.
5191 ///
5192 /// @param die the DIE whose children to walk recursively.
5193 ///
5194 /// @param source where the DIE @p die comes from.
5195 ///
5196 /// @param imported_units a vector containing all the offsets of the
5197 /// points where unit have been imported, under @p die.
5198 void
build_die_parent_relations_under(Dwarf_Die * die,die_source source,imported_unit_points_type & imported_units)5199 build_die_parent_relations_under(Dwarf_Die* die,
5200 die_source source,
5201 imported_unit_points_type & imported_units)
5202 {
5203 if (!die)
5204 return;
5205
5206 offset_offset_map_type& parent_of = die_parent_map(source);
5207
5208 Dwarf_Die child;
5209 if (dwarf_child(die, &child) != 0)
5210 return;
5211
5212 do
5213 {
5214 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5215 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5216 {
5217 Dwarf_Die imported_unit;
5218 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5219 // If the imported_unit has a sub-tree, let's record
5220 // this point at which the sub-tree is imported into
5221 // the current debug info.
5222 //
5223 // Otherwise, if the imported_unit has no sub-tree,
5224 // there is no point in recording where a non-existent
5225 // sub-tree is being imported.
5226 //
5227 // Note that the imported_unit_points_type type below
5228 // expects the imported_unit to have a sub-tree.
5229 && die_has_children(&imported_unit))
5230 {
5231 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5232 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5233 imported_units.push_back
5234 (imported_unit_point(dwarf_dieoffset(&child),
5235 imported_unit,
5236 imported_unit_die_source));
5237 }
5238 }
5239 build_die_parent_relations_under(&child, source, imported_units);
5240 }
5241 while (dwarf_siblingof(&child, &child) == 0);
5242
5243 }
5244
5245 /// Determine if we do have to build a DIE -> parent map, depending
5246 /// on a given language.
5247 ///
5248 /// Some languages like C++, Ada etc, do have the concept of
5249 /// namespace and yet, the DIE data structure doesn't provide us
5250 /// with a way to get the parent namespace of a given DIE. So for
5251 /// those languages, we need to build a DIE -> parent map so that we
5252 /// can get the namespace DIE (or more generally the scope DIE) of a given
5253 /// DIE as we need it.
5254 ///
5255 /// But then some more basic languages like C or assembly don't have
5256 /// that need.
5257 ///
5258 /// This function, depending on the language, tells us if we need to
5259 /// build the DIE -> parent map or not.
5260 ///
5261 /// @param lang the language to consider.
5262 ///
5263 /// @return true iff we need to build the DIE -> parent map for this
5264 /// language.
5265 bool
do_we_build_die_parent_maps(translation_unit::language lang)5266 do_we_build_die_parent_maps(translation_unit::language lang)
5267 {
5268 if (is_c_language(lang))
5269 return false;
5270
5271 switch (lang)
5272 {
5273 case translation_unit::LANG_UNKNOWN:
5274 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5275 case translation_unit::LANG_Mips_Assembler:
5276 #endif
5277 return false;
5278 default:
5279 break;
5280 }
5281 return true;
5282 }
5283
5284 /// Walk all the DIEs accessible in the debug info (and in the
5285 /// alternate debug info as well) and build maps representing the
5286 /// relationship DIE -> parent. That is, make it so that we can get
5287 /// the parent for a given DIE.
5288 ///
5289 /// Note that the goal of this map is to be able to get the parent
5290 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5291 /// when we get a DIE of a type, and we want to build an internal
5292 /// representation for it, we need to get its fully qualified name.
5293 /// For that, we need to know what is the parent DIE of that type
5294 /// DIE, so that we can know what the namespace of that type is.
5295 ///
5296 /// Note that as the C language doesn't have namespaces (all types
5297 /// are defined in the same global namespace), this function doesn't
5298 /// build the DIE -> parent map if the current translation unit
5299 /// comes from C. This saves time on big C ELF files with a lot of
5300 /// DIEs.
5301 void
build_die_parent_maps()5302 build_die_parent_maps()
5303 {
5304 bool we_do_have_to_build_die_parent_map = false;
5305 uint8_t address_size = 0;
5306 size_t header_size = 0;
5307 // Get the DIE of the current translation unit, look at it to get
5308 // its language. If that language is in C, then all types are in
5309 // the global namespace so we don't need to build the DIE ->
5310 // parent map. So we dont build it in that case.
5311 for (Dwarf_Off offset = 0, next_offset = 0;
5312 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5313 offset, &next_offset, &header_size,
5314 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5315 offset = next_offset)
5316 {
5317 Dwarf_Off die_offset = offset + header_size;
5318 Dwarf_Die cu;
5319 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5320 die_offset, &cu))
5321 continue;
5322
5323 uint64_t l = 0;
5324 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5325 translation_unit::language lang = dwarf_language_to_tu_language(l);
5326 if (do_we_build_die_parent_maps(lang))
5327 we_do_have_to_build_die_parent_map = true;
5328 }
5329
5330 if (!we_do_have_to_build_die_parent_map)
5331 return;
5332
5333 // Build the DIE -> parent relation for DIEs coming from the
5334 // .debug_info section in the alternate debug info file.
5335 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5336 for (Dwarf_Off offset = 0, next_offset = 0;
5337 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5338 offset, &next_offset, &header_size,
5339 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5340 offset = next_offset)
5341 {
5342 Dwarf_Off die_offset = offset + header_size;
5343 Dwarf_Die cu;
5344 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5345 die_offset, &cu))
5346 continue;
5347 cur_tu_die(&cu);
5348
5349 imported_unit_points_type& imported_units =
5350 tu_die_imported_unit_points_map(source)[die_offset] =
5351 imported_unit_points_type();
5352 build_die_parent_relations_under(&cu, source, imported_units);
5353 }
5354
5355 // Build the DIE -> parent relation for DIEs coming from the
5356 // .debug_info section of the main debug info file.
5357 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5358 address_size = 0;
5359 header_size = 0;
5360 for (Dwarf_Off offset = 0, next_offset = 0;
5361 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5362 offset, &next_offset, &header_size,
5363 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5364 offset = next_offset)
5365 {
5366 Dwarf_Off die_offset = offset + header_size;
5367 Dwarf_Die cu;
5368 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5369 die_offset, &cu))
5370 continue;
5371 cur_tu_die(&cu);
5372 imported_unit_points_type& imported_units =
5373 tu_die_imported_unit_points_map(source)[die_offset] =
5374 imported_unit_points_type();
5375 build_die_parent_relations_under(&cu, source, imported_units);
5376 }
5377
5378 // Build the DIE -> parent relation for DIEs coming from the
5379 // .debug_types section.
5380 source = TYPE_UNIT_DIE_SOURCE;
5381 address_size = 0;
5382 header_size = 0;
5383 uint64_t type_signature = 0;
5384 Dwarf_Off type_offset;
5385 for (Dwarf_Off offset = 0, next_offset = 0;
5386 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5387 offset, &next_offset, &header_size,
5388 NULL, NULL, &address_size, NULL,
5389 &type_signature, &type_offset) == 0);
5390 offset = next_offset)
5391 {
5392 Dwarf_Off die_offset = offset + header_size;
5393 Dwarf_Die cu;
5394
5395 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5396 die_offset, &cu))
5397 continue;
5398 cur_tu_die(&cu);
5399 imported_unit_points_type& imported_units =
5400 tu_die_imported_unit_points_map(source)[die_offset] =
5401 imported_unit_points_type();
5402 build_die_parent_relations_under(&cu, source, imported_units);
5403 }
5404 }
5405 };// end class reader.
5406
5407 /// The type of the aggregates being compared during a DIE comparison.
5408 ///
5409 /// This encapsulates the stack of aggregates being compared at any
5410 /// single point.
5411 ///
5412 /// This is useful to detect "comparison cycles" and thus avoid the
5413 /// resulting infinite loops.
5414 ///
5415 /// This is also useful for implementing a very important optimization
5416 /// that takes place during the canonicalization
5417 struct offset_pairs_stack_type
5418 {
5419 // The DWARF DWARF reader that is useful for so many things.
5420 const reader& rdr_;
5421 // The set of types that are being compared. This is to speed up
5422 // searches.
5423 offset_pair_set_type set_;
5424 // The stack of types that are being compared. The top of the
5425 // stack is the back of the vector.
5426 offset_pair_vector_type vect_;
5427 // A map that associates a redundant type pair to the vector of
5428 // types that depends on it.
5429 offset_pair_vect_map_type redundant_types_;
5430 // A map that associates a dependant type to the vector of redundant
5431 // types it depends on.
5432 offset_pair_vect_map_type dependant_types_;
5433
offset_pairs_stack_typeabigail::dwarf::offset_pairs_stack_type5434 offset_pairs_stack_type(const reader& rdr)
5435 : rdr_ (rdr)
5436 {}
5437
5438 /// Add a pair of types being compared to the stack of aggregates
5439 /// being compared.
5440 ///
5441 /// @param p the pair of offsets of the type DIEs to consider.
5442 void
addabigail::dwarf::offset_pairs_stack_type5443 add(const offset_pair_type& p)
5444 {
5445 set_.insert(p);
5446 vect_.push_back(p);
5447 }
5448
5449 /// Erase a pair of types being compared from the stack of
5450 /// aggregates being compared.
5451 ///
5452 /// @param p the pair of offsets of the type DIEs to consider.
5453 ///
5454 /// @return true iff @p was found and erased from the stack.
5455 bool
eraseabigail::dwarf::offset_pairs_stack_type5456 erase(const offset_pair_type& p)
5457 {
5458 if (set_.erase(p))
5459 {
5460 offset_pair_vector_type::iterator i;
5461
5462 for (i = vect_.begin();i < vect_.end(); ++i)
5463 if (*i == p)
5464 break;
5465
5466 if (i != vect_.end())
5467 vect_.erase(i);
5468
5469 return true;
5470 }
5471
5472 return false;
5473 }
5474
5475 /// Test if a pair of type DIEs is part of the stack of type DIEs
5476 /// being compared.
5477 ///
5478 /// @param p the pair of offsets of the type DIEs to consider.
5479 ///
5480 /// @return true iff @p was found in the stack of types being
5481 /// compared.
5482 bool
containsabigail::dwarf::offset_pairs_stack_type5483 contains(const offset_pair_type &p) const
5484 {
5485 if (set_.find(p) == set_.end())
5486 return false;
5487 return true;
5488 }
5489
5490 /// Get the set of comparison pair that depends on a given
5491 /// comparison pair.
5492 ///
5493 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5494 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5495 /// words, the pair T appears in the comparison stack BEFORE the
5496 /// pair P.
5497 ///
5498 /// So, this function returns the vector of comparison pairs that
5499 /// appear in the comparison stack AFTER a given comparison pair.
5500 ///
5501 /// @param p the comparison pair to consider.
5502 ///
5503 /// @param pairs out parameter. This is filled with the comparison
5504 /// pairs that depend on @p, iff the function returns true.
5505 ///
5506 /// @return true iff comparison pairs depending on @p have been
5507 /// found and collected in @pairs.
5508 bool
get_pairs_that_depend_onabigail::dwarf::offset_pairs_stack_type5509 get_pairs_that_depend_on(const offset_pair_type& p,
5510 offset_pair_vector_type& pairs) const
5511 {
5512 bool result = false;
5513 if (!contains(p))
5514 return result;
5515
5516 // First, get an iterator on the position of 'p'.
5517 offset_pair_vector_type::const_iterator i;
5518 for (i = vect_.begin(); i != vect_.end(); ++i)
5519 if (*i == p)
5520 break;
5521
5522 if (i == vect_.end())
5523 return result;
5524
5525 // Then, harvest all the comparison pairs that come after the
5526 // position of 'p'.
5527 for (++i; i != vect_.end(); ++i)
5528 {
5529 pairs.push_back(*i);
5530 result = true;
5531 }
5532
5533 return result;
5534 }
5535
5536 /// Record the fact that a set of comparison pairs depends on a
5537 /// given comparison pair.
5538 ///
5539 /// Set a map that associates each dependant comparison pair to the
5540 /// pair it depends on.
5541 ///
5542 /// @param p the comparison pair that the set depends on.
5543 ///
5544 /// @param dependant_types the set of types that depends on @p.
5545 void
record_dependant_typesabigail::dwarf::offset_pairs_stack_type5546 record_dependant_types(const offset_pair_type& p,
5547 const offset_pair_vector_type& dependant_types)
5548 {
5549 for (auto type_pair : dependant_types)
5550 dependant_types_[type_pair].push_back(p);
5551 }
5552
5553 /// Record a comparison pair as being redundant.
5554 ///
5555 ///
5556 /// @param p the comparison pair to record as redundant.
5557 void
record_redundant_type_die_pairabigail::dwarf::offset_pairs_stack_type5558 record_redundant_type_die_pair(const offset_pair_type& p)
5559 {
5560 offset_pair_vector_type dependant_types;
5561 get_pairs_that_depend_on(p, dependant_types);
5562
5563 // First, record the relationship "p -> [pairs that depend on p]".
5564 auto it = redundant_types_.find(p);
5565 if (it == redundant_types_.end())
5566 {
5567 auto entry = std::make_pair(p, dependant_types);
5568 redundant_types_.insert(entry);
5569 }
5570 else
5571 it->second.insert(it->second.end(),
5572 dependant_types.begin(),
5573 dependant_types.end());
5574
5575 // For each dependant type pair, record the association:
5576 // dependant_pair --> [vect of redundant types]
5577 record_dependant_types(p, dependant_types);
5578 }
5579
5580 /// Test if a given pair has been detected as redundant.
5581 ///
5582 /// @param p the pair of DIEs to consider.
5583 ///
5584 /// @return iff @p is redundant.
5585 bool
is_redundantabigail::dwarf::offset_pairs_stack_type5586 is_redundant(const offset_pair_type& p)
5587 {
5588 auto i = redundant_types_.find(p);
5589 if (i != redundant_types_.end())
5590 return true;
5591 return false;
5592 }
5593
5594 /// Test if a given pair is dependant on at least a redundant type.
5595 ///
5596 /// @param p the pair to consider.
5597 ///
5598 /// @return true iff @p depends on a redundant type.
5599 bool
depends_on_redundant_typesabigail::dwarf::offset_pairs_stack_type5600 depends_on_redundant_types(const offset_pair_type& p)
5601 {
5602 auto i = dependant_types_.find(p);
5603 if (i == dependant_types_.end())
5604 return false;
5605 return true;
5606 }
5607
5608 /// Remove a redundant pair from the system.
5609 ///
5610 /// This needs updating the system to also remove the dependant
5611 /// types that depend on the redundant pair (if they depend only on
5612 /// that redundant pair).
5613 ///
5614 /// @param p the pair to consider.
5615 ///
5616 /// @param erase_canonical_die_offset if true then erase the cached
5617 /// comparison results for the redundant pair and its dependant
5618 /// types.
5619 void
erase_redundant_type_pair_entryabigail::dwarf::offset_pairs_stack_type5620 erase_redundant_type_pair_entry(const offset_pair_type& p,
5621 bool erase_cached_results = false)
5622 {
5623 // First, update the dependant types that depend on the redundant
5624 // type pair
5625 auto redundant_type = redundant_types_.find(p);
5626 if (redundant_type != redundant_types_.end())
5627 {
5628 for (auto dependant_type : redundant_type->second)
5629 {
5630 // Each dependant_type depends on the redundant type 'p',
5631 // among others.
5632 auto dependant_types_it = dependant_types_.find(dependant_type);
5633 ABG_ASSERT(dependant_types_it != dependant_types_.end());
5634 // Erase the redundant type 'p' from the redundant types
5635 // that dependant_type depends on.
5636 {
5637 auto i = dependant_types_it->second.begin();
5638 for (; i!= dependant_types_it->second.end();++i)
5639 if (*i == p)
5640 break;
5641 if (i != dependant_types_it->second.end())
5642 dependant_types_it->second.erase(i);
5643 }
5644 // If the dependant type itself doesn't depend on ANY
5645 // redundant type anymore, then remove the depend type
5646 // from the map of the dependant types.
5647 if (dependant_types_it->second.empty())
5648 {
5649 if (erase_cached_results)
5650 rdr_.die_comparison_results_.erase(dependant_type);
5651 dependant_types_.erase(dependant_types_it);
5652 }
5653 }
5654 }
5655 if (erase_cached_results)
5656 rdr_.die_comparison_results_.erase(p);
5657 redundant_types_.erase(p);
5658 }
5659
5660 /// If a comparison pair has been detected as redundant, stop
5661 /// tracking it as well as its dependant pairs. That will
5662 /// essentially make it impossible to reset/cancel the canonical
5663 /// propagated types for those depdant pairs, but will also save
5664 /// ressources.
5665 ///
5666 /// @param p the comparison pair to consider.
5667 void
confirm_canonical_propagated_typeabigail::dwarf::offset_pairs_stack_type5668 confirm_canonical_propagated_type(const offset_pair_type& p)
5669 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5670
5671 /// Walk the types that depend on a comparison pair and cancel their
5672 /// canonical-propagate-type, that means remove their canonical
5673 /// types and mark them as not being canonically-propagated. Also,
5674 /// erase their cached comparison results that was likely set to
5675 /// COMPARISON_RESULT_UNKNOWN.
5676 ///
5677 /// @param p the pair to consider.
5678 void
cancel_canonical_propagated_typeabigail::dwarf::offset_pairs_stack_type5679 cancel_canonical_propagated_type(const offset_pair_type& p)
5680 {
5681 offset_pair_set_type dependant_types;
5682 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5683 for (auto dependant_type : dependant_types)
5684 {
5685 // If this dependant type was canonical-type-propagated then
5686 // erase that canonical type.
5687 if (rdr_.propagated_types_.find(dependant_type)
5688 != rdr_.propagated_types_.end())
5689 {
5690 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5691 dependant_type.first.source_,
5692 /*die_as_type=*/true);
5693 rdr_.propagated_types_.erase(dependant_type);
5694 rdr_.cancelled_propagation_count_++;
5695 }
5696 // Update the cached result. We know the comparison result
5697 // must now be different.
5698 auto comp_result_it = rdr_.die_comparison_results_.find(p);
5699 if (comp_result_it != rdr_.die_comparison_results_.end())
5700 {
5701 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_UNKNOWN);
5702 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5703 }
5704 }
5705
5706 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5707 {
5708 rdr_.erase_canonical_die_offset(p.first.offset_,
5709 p.first.source_,
5710 /*die_as_type=*/true);
5711 rdr_.propagated_types_.erase(p);
5712 rdr_.cancelled_propagation_count_++;
5713 }
5714 }
5715
5716 /// Get the set of comparison pairs that depend on a given pair.
5717 ///
5718 /// @param p the pair to consider.
5719 ///
5720 /// @param result this is set to the pairs that depend on @p, iff
5721 /// the function returned true.
5722 ///
5723 /// @param transitive_closure if set to true, the transitive closure
5724 /// of the @result is set to it.
5725 ///
5726 /// @return true iff @result could be filled with the dependant
5727 /// types.
5728 bool
get_dependant_typesabigail::dwarf::offset_pairs_stack_type5729 get_dependant_types(const offset_pair_type& p,
5730 offset_pair_set_type& result,
5731 bool transitive_closure = false)
5732 {
5733 auto i = redundant_types_.find(p);
5734 if (i != redundant_types_.end())
5735 {
5736 for (auto dependant_type : i->second)
5737 if (result.find(dependant_type) == result.end())
5738 {
5739 result.insert(dependant_type);
5740 if (transitive_closure)
5741 get_dependant_types(p, result, /*transitive_closure=*/true);
5742 }
5743 return true;
5744 }
5745 return false;
5746 }
5747 }; // end struct offset_pairs_stack_type
5748
5749 static type_or_decl_base_sptr
5750 build_ir_node_from_die(reader& rdr,
5751 Dwarf_Die* die,
5752 scope_decl* scope,
5753 bool called_from_public_decl,
5754 size_t where_offset,
5755 bool is_declaration_only = true,
5756 bool is_required_decl_spec = false);
5757
5758 static type_or_decl_base_sptr
5759 build_ir_node_from_die(reader& rdr,
5760 Dwarf_Die* die,
5761 bool called_from_public_decl,
5762 size_t where_offset);
5763
5764 static class_decl_sptr
5765 add_or_update_class_type(reader& rdr,
5766 Dwarf_Die* die,
5767 scope_decl* scope,
5768 bool is_struct,
5769 class_decl_sptr klass,
5770 bool called_from_public_decl,
5771 size_t where_offset,
5772 bool is_declaration_only);
5773
5774 static union_decl_sptr
5775 add_or_update_union_type(reader& rdr,
5776 Dwarf_Die* die,
5777 scope_decl* scope,
5778 union_decl_sptr union_type,
5779 bool called_from_public_decl,
5780 size_t where_offset,
5781 bool is_declaration_only);
5782
5783 static decl_base_sptr
5784 build_ir_node_for_void_type(reader& rdr);
5785
5786 static decl_base_sptr
5787 build_ir_node_for_variadic_parameter_type(reader &rdr);
5788
5789 static function_decl_sptr
5790 build_function_decl(reader& rdr,
5791 Dwarf_Die* die,
5792 size_t where_offset,
5793 function_decl_sptr fn);
5794
5795 static bool
5796 function_is_suppressed(const reader& rdr,
5797 const scope_decl* scope,
5798 Dwarf_Die *function_die,
5799 bool is_declaration_only);
5800
5801 static function_decl_sptr
5802 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5803 scope_decl *scope,
5804 Dwarf_Die *die,
5805 size_t where_offset,
5806 bool is_declaration_only,
5807 function_decl_sptr f);
5808
5809 static var_decl_sptr
5810 build_var_decl(reader& rdr,
5811 Dwarf_Die *die,
5812 size_t where_offset,
5813 var_decl_sptr result = var_decl_sptr());
5814
5815 static var_decl_sptr
5816 build_or_get_var_decl_if_not_suppressed(reader& rdr,
5817 scope_decl *scope,
5818 Dwarf_Die *die,
5819 size_t where_offset,
5820 var_decl_sptr res = var_decl_sptr(),
5821 bool is_required_decl_spec = false);
5822 static bool
5823 variable_is_suppressed(const reader& rdr,
5824 const scope_decl* scope,
5825 Dwarf_Die *variable_die,
5826 bool is_required_decl_spec = false);
5827
5828 static void
5829 finish_member_function_reading(Dwarf_Die* die,
5830 const function_decl_sptr& f,
5831 const class_or_union_sptr klass,
5832 reader& rdr);
5833
5834 /// Test if a given DIE is anonymous
5835 ///
5836 /// @param die the DIE to consider.
5837 ///
5838 /// @return true iff @p die is anonymous.
5839 static bool
die_is_anonymous(const Dwarf_Die * die)5840 die_is_anonymous(const Dwarf_Die* die)
5841 {
5842 Dwarf_Attribute attr;
5843 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5844 return true;
5845 return false;
5846 }
5847
5848 /// Get the value of an attribute that is supposed to be a string, or
5849 /// an empty string if the attribute could not be found.
5850 ///
5851 /// @param die the DIE to get the attribute value from.
5852 ///
5853 /// @param attr_name the attribute name. Must come from dwarf.h and
5854 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5855 ///
5856 /// @return the string representing the value of the attribute, or an
5857 /// empty string if no string attribute could be found.
5858 static string
die_string_attribute(const Dwarf_Die * die,unsigned attr_name)5859 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5860 {
5861 if (!die)
5862 return "";
5863
5864 Dwarf_Attribute attr;
5865 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5866 return "";
5867
5868 const char* str = dwarf_formstring(&attr);
5869 return str ? str : "";
5870 }
5871
5872 /// Get the value of an attribute that is supposed to be a string, or
5873 /// an empty string if the attribute could not be found.
5874 ///
5875 /// @param die the DIE to get the attribute value from.
5876 ///
5877 /// @param attr_name the attribute name. Must come from dwarf.h and
5878 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5879 ///
5880 /// @return the char* representing the value of the attribute, or an
5881 /// empty string if no string attribute could be found.
5882 static const char*
die_char_str_attribute(const Dwarf_Die * die,unsigned attr_name)5883 die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
5884 {
5885 if (!die)
5886 return nullptr;
5887
5888 Dwarf_Attribute attr;
5889 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5890 return nullptr;
5891
5892 const char* str = dwarf_formstring(&attr);
5893 return str;
5894 }
5895
5896 /// Get the value of an attribute that is supposed to be an unsigned
5897 /// constant.
5898 ///
5899 /// @param die the DIE to read the information from.
5900 ///
5901 /// @param attr_name the DW_AT_* name of the attribute. Must come
5902 /// from dwarf.h and be an enumerator representing an attribute like,
5903 /// e.g, DW_AT_decl_line.
5904 ///
5905 ///@param cst the output parameter that is set to the value of the
5906 /// attribute @p attr_name. This parameter is set iff the function
5907 /// return true.
5908 ///
5909 /// @return true if there was an attribute of the name @p attr_name
5910 /// and with a value that is a constant, false otherwise.
5911 static bool
die_unsigned_constant_attribute(const Dwarf_Die * die,unsigned attr_name,uint64_t & cst)5912 die_unsigned_constant_attribute(const Dwarf_Die* die,
5913 unsigned attr_name,
5914 uint64_t& cst)
5915 {
5916 if (!die)
5917 return false;
5918
5919 Dwarf_Attribute attr;
5920 Dwarf_Word result = 0;
5921 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5922 || dwarf_formudata(&attr, &result))
5923 return false;
5924
5925 cst = result;
5926 return true;
5927 }
5928
5929 /// Read a signed constant value from a given attribute.
5930 ///
5931 /// The signed constant expected must be of constant form.
5932 ///
5933 /// @param die the DIE to get the attribute from.
5934 ///
5935 /// @param attr_name the attribute name.
5936 ///
5937 /// @param cst the resulting signed constant read.
5938 ///
5939 /// @return true iff a signed constant attribute of the name @p
5940 /// attr_name was found on the DIE @p die.
5941 static bool
die_signed_constant_attribute(const Dwarf_Die * die,unsigned attr_name,int64_t & cst)5942 die_signed_constant_attribute(const Dwarf_Die *die,
5943 unsigned attr_name,
5944 int64_t& cst)
5945 {
5946 if (!die)
5947 return false;
5948
5949 Dwarf_Attribute attr;
5950 Dwarf_Sword result = 0;
5951 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5952 || dwarf_formsdata(&attr, &result))
5953 return false;
5954
5955 cst = result;
5956 return true;
5957 }
5958
5959 /// Read the value of a constant attribute that is either signed or
5960 /// unsigned into a array_type_def::subrange_type::bound_value value.
5961 ///
5962 /// The bound_value instance will capture the actual signedness of the
5963 /// read attribute.
5964 ///
5965 /// @param die the DIE from which to read the value of the attribute.
5966 ///
5967 /// @param attr_name the attribute name to consider.
5968 ///
5969 /// @param is_signed true if the attribute value has to read as
5970 /// signed.
5971 ///
5972 /// @param value the resulting value read from attribute @p attr_name
5973 /// on DIE @p die.
5974 ///
5975 /// @return true iff DIE @p die has an attribute named @p attr_name
5976 /// with a constant value.
5977 static bool
die_constant_attribute(const Dwarf_Die * die,unsigned attr_name,bool is_signed,array_type_def::subrange_type::bound_value & value)5978 die_constant_attribute(const Dwarf_Die *die,
5979 unsigned attr_name,
5980 bool is_signed,
5981 array_type_def::subrange_type::bound_value &value)
5982 {
5983 if (!is_signed)
5984 {
5985 uint64_t l = 0;
5986 if (!die_unsigned_constant_attribute(die, attr_name, l))
5987 return false;
5988 value.set_unsigned(l);
5989 }
5990 else
5991 {
5992 int64_t l = 0;
5993 if (!die_signed_constant_attribute(die, attr_name, l))
5994 return false;
5995 value.set_signed(l);
5996 }
5997 return true;
5998 }
5999
6000 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
6001 ///
6002 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6003 /// enum in dwarf.h so we have to use an unsigned int for the form,
6004 /// grrr.
6005 ///
6006 /// @param form the form to consider.
6007 ///
6008 /// @return true iff @p form is DW_FORM_strx{1,4}.
6009 static bool
form_is_DW_FORM_strx(unsigned form)6010 form_is_DW_FORM_strx(unsigned form)
6011 {
6012 if (form)
6013 {
6014 #if defined HAVE_DW_FORM_strx1 \
6015 && defined HAVE_DW_FORM_strx2 \
6016 && defined HAVE_DW_FORM_strx3 \
6017 && defined HAVE_DW_FORM_strx4
6018 if (form == DW_FORM_strx1
6019 || form == DW_FORM_strx2
6020 || form == DW_FORM_strx3
6021 ||form == DW_FORM_strx4)
6022 return true;
6023 #endif
6024 }
6025 return false;
6026 }
6027
6028 /// Test if a given DWARF form is DW_FORM_line_strp.
6029 ///
6030 /// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6031 /// enum in dwarf.h so we have to use an unsigned int for the form,
6032 /// grrr.
6033 ///
6034 /// @param form the form to consider.
6035 ///
6036 /// @return true iff @p form is DW_FORM_line_strp.
6037 static bool
form_is_DW_FORM_line_strp(unsigned form)6038 form_is_DW_FORM_line_strp(unsigned form)
6039 {
6040 if (form)
6041 {
6042 #if defined HAVE_DW_FORM_line_strp
6043 if (form == DW_FORM_line_strp)
6044 return true;
6045 #endif
6046 }
6047 return false;
6048 }
6049
6050 /// Get the value of a DIE attribute; that value is meant to be a
6051 /// flag.
6052 ///
6053 /// @param die the DIE to get the attribute from.
6054 ///
6055 /// @param attr_name the DW_AT_* name of the attribute. Must come
6056 /// from dwarf.h and be an enumerator representing an attribute like,
6057 /// e.g, DW_AT_external.
6058 ///
6059 /// @param flag the output parameter to store the flag value into.
6060 /// This is set iff the function returns true.
6061 ///
6062 /// @param recursively if true, the function looks through the
6063 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6064 /// all the way down to the initial DIE that is cloned and look on
6065 /// that DIE to see if it has the @p attr_name attribute.
6066 ///
6067 /// @return true if the DIE has a flag attribute named @p attr_name,
6068 /// false otherwise.
6069 static bool
die_flag_attribute(const Dwarf_Die * die,unsigned attr_name,bool & flag,bool recursively=true)6070 die_flag_attribute(const Dwarf_Die* die,
6071 unsigned attr_name,
6072 bool& flag,
6073 bool recursively = true)
6074 {
6075 Dwarf_Attribute attr;
6076 if (recursively
6077 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6078 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6079 return false;
6080
6081 bool f = false;
6082 if (dwarf_formflag(&attr, &f))
6083 return false;
6084
6085 flag = f;
6086 return true;
6087 }
6088
6089 /// Get the mangled name from a given DIE.
6090 ///
6091 /// @param die the DIE to read the mangled name from.
6092 ///
6093 /// @return the mangled name if it's present in the DIE, or just an
6094 /// empty string if it's not.
6095 static string
die_linkage_name(const Dwarf_Die * die)6096 die_linkage_name(const Dwarf_Die* die)
6097 {
6098 if (!die)
6099 return "";
6100
6101 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6102 if (linkage_name.empty())
6103 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6104 return linkage_name;
6105 }
6106
6107 /// Get the file path that is the value of the DW_AT_decl_file
6108 /// attribute on a given DIE, if the DIE is a decl DIE having that
6109 /// attribute.
6110 ///
6111 /// @param die the DIE to consider.
6112 ///
6113 /// @return a string containing the file path that is the logical
6114 /// value of the DW_AT_decl_file attribute. If the DIE @p die
6115 /// doesn't have a DW_AT_decl_file attribute, then the return value is
6116 /// just an empty string.
6117 static string
die_decl_file_attribute(const Dwarf_Die * die)6118 die_decl_file_attribute(const Dwarf_Die* die)
6119 {
6120 if (!die)
6121 return "";
6122
6123 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6124
6125 return str ? str : "";
6126 }
6127
6128 /// Get the value of an attribute which value is supposed to be a
6129 /// reference to a DIE.
6130 ///
6131 /// @param die the DIE to read the value from.
6132 ///
6133 /// @param attr_name the DW_AT_* attribute name to read.
6134 ///
6135 /// @param result the DIE resulting from reading the attribute value.
6136 /// This is set iff the function returns true.
6137 ///
6138 /// @param recursively if true, the function looks through the
6139 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6140 /// all the way down to the initial DIE that is cloned and look on
6141 /// that DIE to see if it has the @p attr_name attribute.
6142 ///
6143 /// @return true if the DIE @p die contains an attribute named @p
6144 /// attr_name that is a DIE reference, false otherwise.
6145 static bool
die_die_attribute(const Dwarf_Die * die,unsigned attr_name,Dwarf_Die & result,bool recursively)6146 die_die_attribute(const Dwarf_Die* die,
6147 unsigned attr_name,
6148 Dwarf_Die& result,
6149 bool recursively)
6150 {
6151 Dwarf_Attribute attr;
6152 if (recursively
6153 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6154 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6155 return false;
6156
6157 return dwarf_formref_die(&attr, &result);
6158 }
6159
6160 /// Read and return an addresss class attribute from a given DIE.
6161 ///
6162 /// @param die the DIE to consider.
6163 ///
6164 /// @param attr_name the name of the address class attribute to read
6165 /// the value from.
6166 ///
6167 /// @param the resulting address.
6168 ///
6169 /// @return true iff the attribute could be read, was of the expected
6170 /// address class and could thus be translated into the @p result.
6171 static bool
die_address_attribute(Dwarf_Die * die,unsigned attr_name,Dwarf_Addr & result)6172 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6173 {
6174 Dwarf_Attribute attr;
6175 if (!dwarf_attr_integrate(die, attr_name, &attr))
6176 return false;
6177 return dwarf_formaddr(&attr, &result) == 0;
6178 }
6179
6180 /// Returns the source location associated with a decl DIE.
6181 ///
6182 /// @param rdr the @ref reader to use.
6183 ///
6184 /// @param die the DIE the read the source location from.
6185 ///
6186 /// @return the location associated with @p die.
6187 static location
die_location(const reader & rdr,const Dwarf_Die * die)6188 die_location(const reader& rdr, const Dwarf_Die* die)
6189 {
6190 if (!die)
6191 return location();
6192
6193 string file = die_decl_file_attribute(die);
6194 uint64_t line = 0;
6195 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6196
6197 if (!file.empty() && line != 0)
6198 {
6199 translation_unit_sptr tu = rdr.cur_transl_unit();
6200 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6201 return l;
6202 }
6203 return location();
6204 }
6205
6206 /// Return a copy of the name of a DIE.
6207 ///
6208 /// @param die the DIE to consider.
6209 ///
6210 /// @return a copy of the name of the DIE.
6211 static string
die_name(const Dwarf_Die * die)6212 die_name(const Dwarf_Die* die)
6213 {
6214 string name = die_string_attribute(die, DW_AT_name);
6215 return name;
6216 }
6217
6218 /// Return the location, the name and the mangled name of a given DIE.
6219 ///
6220 /// @param rdr the DWARF reader to use.
6221 ///
6222 /// @param die the DIE to read location and names from.
6223 ///
6224 /// @param loc the location output parameter to set.
6225 ///
6226 /// @param name the name output parameter to set.
6227 ///
6228 /// @param linkage_name the linkage_name output parameter to set.
6229 static void
die_loc_and_name(const reader & rdr,Dwarf_Die * die,location & loc,string & name,string & linkage_name)6230 die_loc_and_name(const reader& rdr,
6231 Dwarf_Die* die,
6232 location& loc,
6233 string& name,
6234 string& linkage_name)
6235 {
6236 loc = die_location(rdr, die);
6237 name = die_name(die);
6238 linkage_name = die_linkage_name(die);
6239 }
6240
6241 /// Get the size of a (type) DIE as the value for the parameter
6242 /// DW_AT_byte_size or DW_AT_bit_size.
6243 ///
6244 /// @param die the DIE to read the information from.
6245 ///
6246 /// @param size the resulting size in bits. This is set iff the
6247 /// function return true.
6248 ///
6249 /// @return true if the size attribute was found.
6250 static bool
die_size_in_bits(const Dwarf_Die * die,uint64_t & size)6251 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6252 {
6253 if (!die)
6254 return false;
6255
6256 uint64_t byte_size = 0, bit_size = 0;
6257
6258 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6259 {
6260 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6261 return false;
6262 }
6263 else
6264 bit_size = byte_size * 8;
6265
6266 size = bit_size;
6267
6268 return true;
6269 }
6270
6271 /// Get the access specifier (from the DW_AT_accessibility attribute
6272 /// value) of a given DIE.
6273 ///
6274 /// @param die the DIE to consider.
6275 ///
6276 /// @param access the resulting access. This is set iff the function
6277 /// returns true.
6278 ///
6279 /// @return bool if the DIE contains the DW_AT_accessibility die.
6280 static bool
die_access_specifier(Dwarf_Die * die,access_specifier & access)6281 die_access_specifier(Dwarf_Die * die, access_specifier& access)
6282 {
6283 if (!die)
6284 return false;
6285
6286 uint64_t a = 0;
6287 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6288 return false;
6289
6290 access_specifier result = private_access;
6291
6292 switch (a)
6293 {
6294 case private_access:
6295 result = private_access;
6296 break;
6297
6298 case protected_access:
6299 result = protected_access;
6300 break;
6301
6302 case public_access:
6303 result = public_access;
6304 break;
6305
6306 default:
6307 break;
6308 }
6309
6310 access = result;
6311 return true;
6312 }
6313
6314 /// Test whether a given DIE represents a decl that is public. That
6315 /// is, one with the DW_AT_external attribute set.
6316 ///
6317 /// @param die the DIE to consider for testing.
6318 ///
6319 /// @return true if a DW_AT_external attribute is present and its
6320 /// value is set to the true; return false otherwise.
6321 static bool
die_is_public_decl(const Dwarf_Die * die)6322 die_is_public_decl(const Dwarf_Die* die)
6323 {
6324 if (!die)
6325 return false;
6326 bool is_public = false;
6327
6328 // If this is a DW_TAG_subprogram DIE, look for the
6329 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6330 // then it's public. In all other cases, this should return false.
6331
6332 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6333 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6334 die_flag_attribute(die, DW_AT_external, is_public);
6335 else if (tag == DW_TAG_namespace)
6336 {
6337 string name = die_name(die);
6338 is_public = !name.empty();
6339 }
6340
6341 return is_public;
6342 }
6343
6344 /// Test if a DIE is effectively public.
6345 ///
6346 /// This is meant to return true when either the DIE is public or when
6347 /// it's a variable DIE that is at (global) namespace level.
6348 ///
6349 /// @return true iff either the DIE is public or is a variable DIE
6350 /// that is at (global) namespace level.
6351 static bool
die_is_effectively_public_decl(const reader & rdr,const Dwarf_Die * die)6352 die_is_effectively_public_decl(const reader& rdr,
6353 const Dwarf_Die* die)
6354 {
6355 if (die_is_public_decl(die))
6356 return true;
6357
6358 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6359 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6360 {
6361 // The DIE is a variable.
6362 Dwarf_Die parent_die;
6363 size_t where_offset = 0;
6364 if (!get_parent_die(rdr, die, parent_die, where_offset))
6365 return false;
6366
6367 tag = dwarf_tag(&parent_die);
6368 if (tag == DW_TAG_compile_unit
6369 || tag == DW_TAG_partial_unit
6370 || tag == DW_TAG_type_unit)
6371 // The DIE is at global scope.
6372 return true;
6373
6374 if (tag == DW_TAG_namespace)
6375 {
6376 string name = die_name(&parent_die);
6377 if (name.empty())
6378 // The DIE at unnamed namespace scope, so it's not public.
6379 return false;
6380 // The DIE is at namespace scope.
6381 return true;
6382 }
6383 }
6384 return false;
6385 }
6386
6387 /// Test whether a given DIE represents a declaration-only DIE.
6388 ///
6389 /// That is, if the DIE has the DW_AT_declaration flag set.
6390 ///
6391 /// @param die the DIE to consider.
6392 //
6393 /// @return true if a DW_AT_declaration is present, false otherwise.
6394 static bool
die_is_declaration_only(Dwarf_Die * die)6395 die_is_declaration_only(Dwarf_Die* die)
6396 {
6397 bool is_declaration = false;
6398 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6399 if (is_declaration && !die_has_size_attribute(die))
6400 return true;
6401 return false;
6402 }
6403
6404 /// Test if a DIE is for a function decl.
6405 ///
6406 /// @param die the DIE to consider.
6407 ///
6408 /// @return true iff @p die represents a function decl.
6409 static bool
die_is_function_decl(const Dwarf_Die * die)6410 die_is_function_decl(const Dwarf_Die *die)
6411 {
6412 if (!die)
6413 return false;
6414
6415 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6416 if (tag == DW_TAG_subprogram)
6417 return true;
6418 return false;
6419 }
6420
6421 /// Test if a DIE is for a variable decl.
6422 ///
6423 /// @param die the DIE to consider.
6424 ///
6425 /// @return true iff @p die represents a variable decl.
6426 static bool
die_is_variable_decl(const Dwarf_Die * die)6427 die_is_variable_decl(const Dwarf_Die *die)
6428 {
6429 if (!die)
6430 return false;
6431
6432 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6433 if (tag == DW_TAG_variable)
6434 return true;
6435 return false;
6436 }
6437
6438 /// Test if a DIE has size attribute.
6439 ///
6440 /// @param die the DIE to consider.
6441 ///
6442 /// @return true if the DIE has a size attribute.
6443 static bool
die_has_size_attribute(const Dwarf_Die * die)6444 die_has_size_attribute(const Dwarf_Die *die)
6445 {
6446 uint64_t s;
6447 if (die_size_in_bits(die, s))
6448 return true;
6449 return false;
6450 }
6451
6452 /// Test that a DIE has no child DIE.
6453 ///
6454 /// @param die the DIE to consider.
6455 ///
6456 /// @return true iff @p die has no child DIE.
6457 static bool
die_has_no_child(const Dwarf_Die * die)6458 die_has_no_child(const Dwarf_Die *die)
6459 {
6460 if (!die)
6461 return true;
6462
6463 Dwarf_Die child;
6464 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6465 return false;
6466 return true;
6467 }
6468
6469 /// Test whether a given DIE represents a declaration-only DIE.
6470 ///
6471 /// That is, if the DIE has the DW_AT_declaration flag set.
6472 ///
6473 /// @param die the DIE to consider.
6474 //
6475 /// @return true if a DW_AT_declaration is present, false otherwise.
6476 static bool
die_is_declaration_only(const Dwarf_Die * die)6477 die_is_declaration_only(const Dwarf_Die* die)
6478 {return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6479
6480 /// Tests whether a given DIE is artificial.
6481 ///
6482 /// @param die the test to test for.
6483 ///
6484 /// @return true if the DIE is artificial, false otherwise.
6485 static bool
die_is_artificial(Dwarf_Die * die)6486 die_is_artificial(Dwarf_Die* die)
6487 {
6488 bool is_artificial;
6489 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6490 }
6491
6492 ///@return true if a tag represents a type, false otherwise.
6493 ///
6494 ///@param tag the tag to consider.
6495 static bool
is_type_tag(unsigned tag)6496 is_type_tag(unsigned tag)
6497 {
6498 bool result = false;
6499
6500 switch (tag)
6501 {
6502 case DW_TAG_array_type:
6503 case DW_TAG_class_type:
6504 case DW_TAG_enumeration_type:
6505 case DW_TAG_pointer_type:
6506 case DW_TAG_reference_type:
6507 case DW_TAG_string_type:
6508 case DW_TAG_structure_type:
6509 case DW_TAG_subroutine_type:
6510 case DW_TAG_typedef:
6511 case DW_TAG_union_type:
6512 case DW_TAG_ptr_to_member_type:
6513 case DW_TAG_set_type:
6514 case DW_TAG_subrange_type:
6515 case DW_TAG_base_type:
6516 case DW_TAG_const_type:
6517 case DW_TAG_file_type:
6518 case DW_TAG_packed_type:
6519 case DW_TAG_thrown_type:
6520 case DW_TAG_volatile_type:
6521 case DW_TAG_restrict_type:
6522 case DW_TAG_interface_type:
6523 case DW_TAG_unspecified_type:
6524 case DW_TAG_shared_type:
6525 case DW_TAG_rvalue_reference_type:
6526 case DW_TAG_coarray_type:
6527 case DW_TAG_atomic_type:
6528 case DW_TAG_immutable_type:
6529 result = true;
6530 break;
6531
6532 default:
6533 result = false;
6534 break;
6535 }
6536
6537 return result;
6538 }
6539
6540 /// Test if a given DIE is a type whose canonical type is to be
6541 /// propagated during DIE canonicalization
6542 ///
6543 /// This is a sub-routine of compare_dies.
6544 ///
6545 /// @param tag the tag of the DIE to consider.
6546 ///
6547 /// @return true iff the DIE of tag @p tag is can see its canonical
6548 /// type be propagated during the type comparison that happens during
6549 /// DIE canonicalization.
6550 static bool
is_canon_type_to_be_propagated_tag(unsigned tag)6551 is_canon_type_to_be_propagated_tag(unsigned tag)
6552 {
6553 bool result = false;
6554
6555 switch (tag)
6556 {
6557 case DW_TAG_class_type:
6558 case DW_TAG_structure_type:
6559 case DW_TAG_union_type:
6560 case DW_TAG_subroutine_type:
6561 case DW_TAG_subprogram:
6562 result = true;
6563 break;
6564
6565 default:
6566 result = false;
6567 break;
6568 }
6569
6570 return result;
6571 }
6572
6573 /// Test if a given kind of DIE ought to have its comparison result
6574 /// cached by compare_dies, so that subsequent invocations of
6575 /// compare_dies can be faster.
6576 ///
6577 /// @param tag the tag of the DIE to consider.
6578 ///
6579 /// @return true iff DIEs of the tag @p tag ought to have its
6580 /// comparison results cached.
6581 static bool
type_comparison_result_to_be_cached(unsigned tag)6582 type_comparison_result_to_be_cached(unsigned tag)
6583 {
6584 bool r = false;
6585 switch (tag)
6586 {
6587 case DW_TAG_class_type:
6588 case DW_TAG_structure_type:
6589 case DW_TAG_union_type:
6590 case DW_TAG_subroutine_type:
6591 case DW_TAG_subprogram:
6592 r = true;
6593 break;
6594
6595 default:
6596 r = false;
6597 break;
6598 }
6599 return r;
6600 }
6601
6602 /// Cache the result of comparing to type DIEs.
6603 ///
6604 /// @param rdr the context to consider.
6605 ///
6606 /// @param tag the tag of the DIEs to consider.
6607 ///
6608 /// @param p the offsets of the pair of DIEs being compared.
6609 ///
6610 /// @param result the comparison result to be cached.
6611 static bool
maybe_cache_type_comparison_result(const reader & rdr,int tag,const offset_pair_type & p,comparison_result result)6612 maybe_cache_type_comparison_result(const reader& rdr,
6613 int tag,
6614 const offset_pair_type& p,
6615 comparison_result result)
6616 {
6617 if (!type_comparison_result_to_be_cached(tag)
6618 || (result != COMPARISON_RESULT_EQUAL
6619 && result != COMPARISON_RESULT_DIFFERENT))
6620 return false;
6621
6622 rdr.die_comparison_results_[p] = result;
6623
6624 return true;
6625
6626 }
6627
6628 /// Get the cached result of the comparison of a pair of DIEs.
6629 ///
6630 /// @param rdr the context to consider.
6631 ///
6632 /// @param tag the tag of the pair of DIEs to consider.
6633 ///
6634 /// @param p the offsets of the pair of DIEs to consider.
6635 ///
6636 /// @param result out parameter set to the cached result of the
6637 /// comparison of @p p if it has been found.
6638 ///
6639 /// @return true iff a cached result for the comparisonof @p has been
6640 /// found and set into @p result.
6641 static bool
get_cached_type_comparison_result(const reader & rdr,const offset_pair_type & p,comparison_result & result)6642 get_cached_type_comparison_result(const reader& rdr,
6643 const offset_pair_type& p,
6644 comparison_result& result)
6645 {
6646 auto i = rdr.die_comparison_results_.find(p);
6647 if (i != rdr.die_comparison_results_.end())
6648 {
6649 result = i->second;
6650 return true;
6651 }
6652 return false;
6653 }
6654
6655 /// Get the cached result of the comparison of a pair of DIEs, if the
6656 /// kind of DIEs ought to have its comparison results cached.
6657 ///
6658 /// @param rdr the context to consider.
6659 ///
6660 /// @param tag the tag of the pair of DIEs to consider.
6661 ///
6662 /// @param p the offsets of the pair of DIEs to consider.
6663 ///
6664 /// @param result out parameter set to the cached result of the
6665 /// comparison of @p p if it has been found.
6666 ///
6667 /// @return true iff a cached result for the comparisonof @p has been
6668 /// found and set into @p result.
6669 static bool
maybe_get_cached_type_comparison_result(const reader & rdr,int tag,const offset_pair_type & p,comparison_result & result)6670 maybe_get_cached_type_comparison_result(const reader& rdr,
6671 int tag,
6672 const offset_pair_type& p,
6673 comparison_result& result)
6674 {
6675 if (type_comparison_result_to_be_cached(tag))
6676 {
6677 // Types of this kind might have their comparison result cached
6678 // when they are not canonicalized. So let's see if we have a
6679 // cached comparison result.
6680 if (get_cached_type_comparison_result(rdr, p, result))
6681 return true;
6682 }
6683 return false;
6684 }
6685
6686 /// Test if a given DIE is to be canonicalized.
6687 ///
6688 /// @param die the DIE to consider.
6689 ///
6690 /// @return true iff @p die is to be canonicalized.
6691 static bool
is_type_die_to_be_canonicalized(const Dwarf_Die * die)6692 is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6693 {
6694 bool result = false;
6695 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6696
6697 if (!is_type_tag(tag))
6698 return false;
6699
6700 switch (tag)
6701 {
6702 case DW_TAG_class_type:
6703 case DW_TAG_structure_type:
6704 case DW_TAG_union_type:
6705 result = !die_is_declaration_only(die);
6706 break;
6707
6708 case DW_TAG_subroutine_type:
6709 case DW_TAG_subprogram:
6710 case DW_TAG_array_type:
6711 result = true;
6712
6713 default:
6714 break;
6715 }
6716
6717 return result;
6718 }
6719
6720 /// Test if a DIE tag represents a declaration.
6721 ///
6722 /// @param tag the DWARF tag to consider.
6723 ///
6724 /// @return true iff @p tag is for a declaration.
6725 static bool
is_decl_tag(unsigned tag)6726 is_decl_tag(unsigned tag)
6727 {
6728 switch (tag)
6729 {
6730 case DW_TAG_formal_parameter:
6731 case DW_TAG_imported_declaration:
6732 case DW_TAG_member:
6733 case DW_TAG_unspecified_parameters:
6734 case DW_TAG_subprogram:
6735 case DW_TAG_variable:
6736 case DW_TAG_namespace:
6737 case DW_TAG_GNU_template_template_param:
6738 case DW_TAG_GNU_template_parameter_pack:
6739 case DW_TAG_GNU_formal_parameter_pack:
6740 return true;
6741 }
6742 return false;
6743 }
6744
6745 /// Test if a DIE represents a type DIE.
6746 ///
6747 /// @param die the DIE to consider.
6748 ///
6749 /// @return true if @p die represents a type, false otherwise.
6750 static bool
die_is_type(const Dwarf_Die * die)6751 die_is_type(const Dwarf_Die* die)
6752 {
6753 if (!die)
6754 return false;
6755 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6756 }
6757
6758 /// Test if a DIE represents a declaration.
6759 ///
6760 /// @param die the DIE to consider.
6761 ///
6762 /// @return true if @p die represents a decl, false otherwise.
6763 static bool
die_is_decl(const Dwarf_Die * die)6764 die_is_decl(const Dwarf_Die* die)
6765 {
6766 if (!die)
6767 return false;
6768 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6769 }
6770
6771 /// Test if a DIE represents a namespace.
6772 ///
6773 /// @param die the DIE to consider.
6774 ///
6775 /// @return true if @p die represents a namespace, false otherwise.
6776 static bool
die_is_namespace(const Dwarf_Die * die)6777 die_is_namespace(const Dwarf_Die* die)
6778 {
6779 if (!die)
6780 return false;
6781 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6782 }
6783
6784 /// Test if a DIE has tag DW_TAG_unspecified_type.
6785 ///
6786 /// @param die the DIE to consider.
6787 ///
6788 /// @return true if @p die has tag DW_TAG_unspecified_type.
6789 static bool
die_is_unspecified(Dwarf_Die * die)6790 die_is_unspecified(Dwarf_Die* die)
6791 {
6792 if (!die)
6793 return false;
6794 return (dwarf_tag(die) == DW_TAG_unspecified_type);
6795 }
6796
6797 /// Test if a DIE represents a void type.
6798 ///
6799 /// @param die the DIE to consider.
6800 ///
6801 /// @return true if @p die represents a void type, false otherwise.
6802 static bool
die_is_void_type(Dwarf_Die * die)6803 die_is_void_type(Dwarf_Die* die)
6804 {
6805 if (!die || dwarf_tag(die) != DW_TAG_base_type)
6806 return false;
6807
6808 string name = die_name(die);
6809 if (name == "void")
6810 return true;
6811
6812 return false;
6813 }
6814
6815 /// Test if a DIE represents a pointer type.
6816 ///
6817 /// @param die the die to consider.
6818 ///
6819 /// @return true iff @p die represents a pointer type.
6820 static bool
die_is_pointer_type(const Dwarf_Die * die)6821 die_is_pointer_type(const Dwarf_Die* die)
6822 {
6823 if (!die)
6824 return false;
6825
6826 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6827 if (tag == DW_TAG_pointer_type)
6828 return true;
6829
6830 return false;
6831 }
6832
6833 /// Test if a DIE is for a pointer, reference or qualified type to
6834 /// anonymous class or struct.
6835 ///
6836 /// @param die the DIE to consider.
6837 ///
6838 /// @return true iff @p is for a pointer, reference or qualified type
6839 /// to anonymous class or struct.
6840 static bool
pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die * die)6841 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
6842 {
6843 if (!die_is_pointer_or_reference_type(die)
6844 && !die_is_qualified_type(die))
6845 return false;
6846
6847 Dwarf_Die underlying_type_die;
6848 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
6849 return false;
6850
6851 if (!die_is_class_type(&underlying_type_die))
6852 return false;
6853
6854 string name = die_name(&underlying_type_die);
6855
6856 return name.empty();
6857 }
6858
6859 /// Test if a DIE represents a reference type.
6860 ///
6861 /// @param die the die to consider.
6862 ///
6863 /// @return true iff @p die represents a reference type.
6864 static bool
die_is_reference_type(const Dwarf_Die * die)6865 die_is_reference_type(const Dwarf_Die* die)
6866 {
6867 if (!die)
6868 return false;
6869
6870 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6871 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
6872 return true;
6873
6874 return false;
6875 }
6876
6877 /// Test if a DIE represents an array type.
6878 ///
6879 /// @param die the die to consider.
6880 ///
6881 /// @return true iff @p die represents an array type.
6882 static bool
die_is_array_type(const Dwarf_Die * die)6883 die_is_array_type(const Dwarf_Die* die)
6884 {
6885 if (!die)
6886 return false;
6887
6888 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6889 if (tag == DW_TAG_array_type)
6890 return true;
6891
6892 return false;
6893 }
6894
6895 /// Test if a DIE represents a pointer, reference or array type.
6896 ///
6897 /// @param die the die to consider.
6898 ///
6899 /// @return true iff @p die represents a pointer or reference type.
6900 static bool
die_is_pointer_or_reference_type(const Dwarf_Die * die)6901 die_is_pointer_or_reference_type(const Dwarf_Die* die)
6902 {return (die_is_pointer_type(die)
6903 || die_is_reference_type(die)
6904 || die_is_array_type(die));}
6905
6906 /// Test if a DIE represents a pointer, a reference or a typedef type.
6907 ///
6908 /// @param die the die to consider.
6909 ///
6910 /// @return true iff @p die represents a pointer, a reference or a
6911 /// typedef type.
6912 static bool
die_is_pointer_reference_or_typedef_type(const Dwarf_Die * die)6913 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
6914 {return (die_is_pointer_or_reference_type(die)
6915 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
6916
6917 /// Test if a DIE represents a class type.
6918 ///
6919 /// @param die the die to consider.
6920 ///
6921 /// @return true iff @p die represents a class type.
6922 static bool
die_is_class_type(const Dwarf_Die * die)6923 die_is_class_type(const Dwarf_Die* die)
6924 {
6925 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6926
6927 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
6928 return true;
6929
6930 return false;
6931 }
6932
6933 /// Test if a DIE is for a qualified type.
6934 ///
6935 /// @param die the DIE to consider.
6936 ///
6937 /// @return true iff @p die is for a qualified type.
6938 static bool
die_is_qualified_type(const Dwarf_Die * die)6939 die_is_qualified_type(const Dwarf_Die* die)
6940 {
6941 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6942 if (tag == DW_TAG_const_type
6943 || tag == DW_TAG_volatile_type
6944 || tag == DW_TAG_restrict_type)
6945 return true;
6946
6947 return false;
6948 }
6949
6950 /// Test if a DIE is for a function type.
6951 ///
6952 /// @param die the DIE to consider.
6953 ///
6954 /// @return true iff @p die is for a function type.
6955 static bool
die_is_function_type(const Dwarf_Die * die)6956 die_is_function_type(const Dwarf_Die *die)
6957 {
6958 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6959 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
6960 return true;
6961
6962 return false;
6963 }
6964
6965 /// Test if a DIE for a function pointer or member function has an
6966 /// DW_AT_object_pointer attribute.
6967 ///
6968 /// @param die the DIE to consider.
6969 ///
6970 /// @param object_pointer out parameter. It's set to the DIE for the
6971 /// object pointer iff the function returns true.
6972 ///
6973 /// @return true iff the DIE @p die has an object pointer. In that
6974 /// case, the parameter @p object_pointer is set to the DIE of that
6975 /// object pointer.
6976 static bool
die_has_object_pointer(const Dwarf_Die * die,Dwarf_Die & object_pointer)6977 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
6978 {
6979 if (!die)
6980 return false;
6981
6982 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
6983 return true;
6984
6985 return false;
6986 }
6987
6988 /// Test if a DIE has children DIEs.
6989 ///
6990 /// @param die the DIE to consider.
6991 ///
6992 /// @return true iff @p DIE has at least one child node.
6993 static bool
die_has_children(const Dwarf_Die * die)6994 die_has_children(const Dwarf_Die* die)
6995 {
6996 if (!die)
6997 return false;
6998
6999 Dwarf_Die child;
7000 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7001 return true;
7002
7003 return false;
7004 }
7005
7006 /// When given the object pointer DIE of a function type or member
7007 /// function DIE, this function returns the "this" pointer that points
7008 /// to the associated class.
7009 ///
7010 /// @param die the DIE of the object pointer of the function or member
7011 /// function to consider.
7012 ///
7013 /// @param this_pointer_die out parameter. This is set to the DIE of
7014 /// the "this" pointer iff the function returns true.
7015 ///
7016 /// @return true iff the function found the "this" pointer from the
7017 /// object pointer DIE @p die. In that case, the parameter @p
7018 /// this_pointer_die is set to the DIE of that "this" pointer.
7019 static bool
die_this_pointer_from_object_pointer(Dwarf_Die * die,Dwarf_Die & this_pointer_die)7020 die_this_pointer_from_object_pointer(Dwarf_Die* die,
7021 Dwarf_Die& this_pointer_die)
7022 {
7023 ABG_ASSERT(die);
7024 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7025
7026 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7027 return true;
7028
7029 return false;
7030 }
7031
7032 /// Test if a given "this" pointer that points to a particular class
7033 /// type is for a const class or not. If it's for a const class, then
7034 /// it means the function type or the member function associated to
7035 /// that "this" pointer is const.
7036 ///
7037 /// @param die the DIE of the "this" pointer to consider.
7038 ///
7039 /// @return true iff @p die points to a const class type.
7040 static bool
die_this_pointer_is_const(Dwarf_Die * die)7041 die_this_pointer_is_const(Dwarf_Die* die)
7042 {
7043 ABG_ASSERT(die);
7044
7045 if (dwarf_tag(die) == DW_TAG_pointer_type)
7046 {
7047 Dwarf_Die pointed_to_type_die;
7048 if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7049 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7050 return true;
7051 }
7052
7053 return false;
7054 }
7055
7056 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
7057 /// attribute) points to a const implicit class and so is for a const
7058 /// method or or a const member function type.
7059 ///
7060 /// @param die the DIE of the object pointer to consider.
7061 ///
7062 /// @return true iff the object pointer represented by @p die is for a
7063 /// a const method or const member function type.
7064 static bool
die_object_pointer_is_for_const_method(Dwarf_Die * die)7065 die_object_pointer_is_for_const_method(Dwarf_Die* die)
7066 {
7067 ABG_ASSERT(die);
7068 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7069
7070 Dwarf_Die this_pointer_die;
7071 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7072 if (die_this_pointer_is_const(&this_pointer_die))
7073 return true;
7074
7075 return false;
7076 }
7077
7078 /// Test if a DIE represents an entity that is at class scope.
7079 ///
7080 /// @param rdr the DWARF reader to use.
7081 ///
7082 /// @param die the DIE to consider.
7083 ///
7084 /// @param where_offset where we are logically at in the DIE stream.
7085 ///
7086 /// @param class_scope_die out parameter. Set to the DIE of the
7087 /// containing class iff @p die happens to be at class scope; that is,
7088 /// iff the function returns true.
7089 ///
7090 /// @return true iff @p die is at class scope. In that case, @p
7091 /// class_scope_die is set to the DIE of the class that contains @p
7092 /// die.
7093 static bool
die_is_at_class_scope(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & class_scope_die)7094 die_is_at_class_scope(const reader& rdr,
7095 const Dwarf_Die* die,
7096 size_t where_offset,
7097 Dwarf_Die& class_scope_die)
7098 {
7099 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7100 return false;
7101
7102 int tag = dwarf_tag(&class_scope_die);
7103
7104 return (tag == DW_TAG_structure_type
7105 || tag == DW_TAG_class_type
7106 || tag == DW_TAG_union_type);
7107 }
7108
7109 /// Return the leaf object under a pointer, reference or qualified
7110 /// type DIE.
7111 ///
7112 /// @param die the DIE of the type to consider.
7113 ///
7114 /// @param peeled_die out parameter. Set to the DIE of the leaf
7115 /// object iff the function actually peeled anything.
7116 ///
7117 /// @return true upon successful completion.
7118 static bool
die_peel_qual_ptr(Dwarf_Die * die,Dwarf_Die & peeled_die)7119 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7120 {
7121 if (!die)
7122 return false;
7123
7124 int tag = dwarf_tag(die);
7125
7126 if (tag == DW_TAG_const_type
7127 || tag == DW_TAG_volatile_type
7128 || tag == DW_TAG_restrict_type
7129 || tag == DW_TAG_pointer_type
7130 || tag == DW_TAG_reference_type
7131 || tag == DW_TAG_rvalue_reference_type)
7132 {
7133 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7134 return false;
7135 }
7136 else
7137 return false;
7138
7139 memcpy(&peeled_die, die, sizeof(peeled_die));
7140
7141 while (tag == DW_TAG_const_type
7142 || tag == DW_TAG_volatile_type
7143 || tag == DW_TAG_restrict_type
7144 || tag == DW_TAG_pointer_type
7145 || tag == DW_TAG_reference_type
7146 || tag == DW_TAG_rvalue_reference_type)
7147 {
7148 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7149 break;
7150 tag = dwarf_tag(&peeled_die);
7151 }
7152
7153 return true;
7154 }
7155
7156 /// Return the leaf object under a typedef type DIE.
7157 ///
7158 /// @param die the DIE of the type to consider.
7159 ///
7160 /// @param peeled_die out parameter. Set to the DIE of the leaf
7161 /// object iff the function actually peeled anything.
7162 ///
7163 /// @return true upon successful completion.
7164 static bool
die_peel_typedef(Dwarf_Die * die,Dwarf_Die & peeled_die)7165 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7166 {
7167 if (!die)
7168 return false;
7169
7170 int tag = dwarf_tag(die);
7171
7172 memcpy(&peeled_die, die, sizeof(peeled_die));
7173
7174 if (tag == DW_TAG_typedef)
7175 {
7176 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7177 return false;
7178 }
7179 else
7180 return false;
7181
7182 while (tag == DW_TAG_typedef)
7183 {
7184 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7185 break;
7186 tag = dwarf_tag(&peeled_die);
7187 }
7188
7189 return true;
7190
7191 }
7192
7193 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7194 ///
7195 /// @param die the DIE to consider.
7196 ///
7197 /// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7198 /// iff the function returned true.
7199 ///
7200 /// @return true iff the function could peel @p die.
7201 static bool
die_peel_pointer_and_typedef(const Dwarf_Die * die,Dwarf_Die & peeled_die)7202 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7203 {
7204 if (!die)
7205 return false;
7206
7207 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7208
7209 if (tag == DW_TAG_pointer_type
7210 || tag == DW_TAG_reference_type
7211 || tag == DW_TAG_rvalue_reference_type
7212 || tag == DW_TAG_typedef)
7213 {
7214 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7215 return false;
7216 }
7217 else
7218 return false;
7219
7220 while (tag == DW_TAG_pointer_type
7221 || tag == DW_TAG_reference_type
7222 || tag == DW_TAG_rvalue_reference_type
7223 || tag == DW_TAG_typedef)
7224 {
7225 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7226 break;
7227 tag = dwarf_tag(&peeled_die);
7228 }
7229 return true;
7230 }
7231
7232 /// Test if a DIE for a function type represents a method type.
7233 ///
7234 /// @param rdr the DWARF reader.
7235 ///
7236 /// @param die the DIE to consider.
7237 ///
7238 /// @param where_offset where we logically are in the stream of DIEs.
7239 ///
7240 /// @param object_pointer_die out parameter. This is set by the
7241 /// function to the DIE that refers to the formal function parameter
7242 /// which holds the implicit "this" pointer of the method. That die
7243 /// is called the object pointer DIE. This is set iff the function
7244 ///
7245 /// @param class_die out parameter. This is set by the function to
7246 /// the DIE that represents the class of the method type. This is set
7247 /// iff the function returns true.
7248 ///
7249 /// @param is_static out parameter. This is set to true by the
7250 /// function if @p die is a static method. This is set iff the
7251 /// function returns true.
7252 ///
7253 /// @return true iff @p die is a DIE for a method type.
7254 static bool
die_function_type_is_method_type(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & object_pointer_die,Dwarf_Die & class_die,bool & is_static)7255 die_function_type_is_method_type(const reader& rdr,
7256 const Dwarf_Die *die,
7257 size_t where_offset,
7258 Dwarf_Die& object_pointer_die,
7259 Dwarf_Die& class_die,
7260 bool& is_static)
7261 {
7262 if (!die)
7263 return false;
7264
7265 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7266 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7267
7268 bool has_object_pointer = false;
7269 is_static = false;
7270 if (tag == DW_TAG_subprogram)
7271 {
7272 Dwarf_Die spec_or_origin_die;
7273 if (die_die_attribute(die, DW_AT_specification,
7274 spec_or_origin_die)
7275 || die_die_attribute(die, DW_AT_abstract_origin,
7276 spec_or_origin_die))
7277 {
7278 if (die_has_object_pointer(&spec_or_origin_die,
7279 object_pointer_die))
7280 has_object_pointer = true;
7281 else
7282 {
7283 if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7284 where_offset, class_die))
7285 is_static = true;
7286 else
7287 return false;
7288 }
7289 }
7290 else
7291 {
7292 if (die_has_object_pointer(die, object_pointer_die))
7293 has_object_pointer = true;
7294 else
7295 {
7296 if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7297 is_static = true;
7298 else
7299 return false;
7300 }
7301 }
7302 }
7303 else
7304 {
7305 if (die_has_object_pointer(die, object_pointer_die))
7306 has_object_pointer = true;
7307 else
7308 return false;
7309 }
7310
7311 if (!is_static)
7312 {
7313 ABG_ASSERT(has_object_pointer);
7314 // The object pointer die points to a DW_TAG_formal_parameter which
7315 // is the "this" parameter. The type of the "this" parameter is a
7316 // pointer. Let's get that pointer type.
7317 Dwarf_Die this_type_die;
7318 if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7319 return false;
7320
7321 // So the class type is the type pointed to by the type of the "this"
7322 // parameter.
7323 if (!die_peel_qual_ptr(&this_type_die, class_die))
7324 return false;
7325
7326 // And make we return a class type, rather than a typedef to a
7327 // class.
7328 die_peel_typedef(&class_die, class_die);
7329 }
7330
7331 return true;
7332 }
7333
7334 enum virtuality
7335 {
7336 VIRTUALITY_NOT_VIRTUAL,
7337 VIRTUALITY_VIRTUAL,
7338 VIRTUALITY_PURE_VIRTUAL
7339 };
7340
7341 /// Get the virtual-ness of a given DIE, that is, the value of the
7342 /// DW_AT_virtuality attribute.
7343 ///
7344 /// @param die the DIE to read from.
7345 ///
7346 /// @param virt the resulting virtuality attribute. This is set iff
7347 /// the function returns true.
7348 ///
7349 /// @return true if the virtual-ness could be determined.
7350 static bool
die_virtuality(const Dwarf_Die * die,virtuality & virt)7351 die_virtuality(const Dwarf_Die* die, virtuality& virt)
7352 {
7353 if (!die)
7354 return false;
7355
7356 uint64_t v = 0;
7357 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7358
7359 if (v == DW_VIRTUALITY_virtual)
7360 virt = VIRTUALITY_VIRTUAL;
7361 else if (v == DW_VIRTUALITY_pure_virtual)
7362 virt = VIRTUALITY_PURE_VIRTUAL;
7363 else
7364 virt = VIRTUALITY_NOT_VIRTUAL;
7365
7366 return true;
7367 }
7368
7369 /// Test whether the DIE represent either a virtual base or function.
7370 ///
7371 /// @param die the DIE to consider.
7372 ///
7373 /// @return bool if the DIE represents a virtual base or function,
7374 /// false othersise.
7375 static bool
die_is_virtual(const Dwarf_Die * die)7376 die_is_virtual(const Dwarf_Die* die)
7377 {
7378 virtuality v;
7379 if (!die_virtuality(die, v))
7380 return false;
7381
7382 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7383 }
7384
7385 /// Test if the DIE represents an entity that was declared inlined.
7386 ///
7387 /// @param die the DIE to test for.
7388 ///
7389 /// @return true if the DIE represents an entity that was declared
7390 /// inlined.
7391 static bool
die_is_declared_inline(Dwarf_Die * die)7392 die_is_declared_inline(Dwarf_Die* die)
7393 {
7394 uint64_t inline_value = 0;
7395 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7396 return false;
7397 return inline_value == DW_INL_declared_inlined;
7398 }
7399
7400 /// Compare two DWARF strings using the most accurate (and slowest)
7401 /// method possible.
7402 ///
7403 /// @param l the DIE that carries the first string to consider, as an
7404 /// attribute value.
7405 ///
7406 /// @param attr_name the name of the attribute which value is the
7407 /// string to compare.
7408 ///
7409 /// @return true iff the string carried by @p l equals the one carried
7410 /// by @p r.
7411 static bool
slowly_compare_strings(const Dwarf_Die * l,const Dwarf_Die * r,unsigned attr_name)7412 slowly_compare_strings(const Dwarf_Die *l,
7413 const Dwarf_Die *r,
7414 unsigned attr_name)
7415 {
7416 const char *l_str = die_char_str_attribute(l, attr_name),
7417 *r_str = die_char_str_attribute(r, attr_name);
7418 if (!l_str && !r_str)
7419 return true;
7420 return l_str && r_str && !strcmp(l_str, r_str);
7421 }
7422
7423 /// This function is a fast routine (optimization) to compare the
7424 /// values of two string attributes of two DIEs.
7425 ///
7426 /// @param l the first DIE to consider.
7427 ///
7428 /// @param r the second DIE to consider.
7429 ///
7430 /// @param attr_name the name of the attribute to compare, on the two
7431 /// DIEs above.
7432 ///
7433 /// @param result out parameter. This is set to the result of the
7434 /// comparison. If the value of attribute @p attr_name on DIE @p l
7435 /// equals the value of attribute @p attr_name on DIE @p r, then the
7436 /// the argument of this parameter is set to true. Otherwise, it's
7437 /// set to false. Note that the argument of this parameter is set iff
7438 /// the function returned true.
7439 ///
7440 /// @return true iff the comparison could be performed. There are
7441 /// cases in which the comparison cannot be performed. For instance,
7442 /// if one of the DIEs does not have the attribute @p attr_name. In
7443 /// any case, if this function returns true, then the parameter @p
7444 /// result is set to the result of the comparison.
7445 static bool
compare_dies_string_attribute_value(const Dwarf_Die * l,const Dwarf_Die * r,unsigned attr_name,bool & result)7446 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7447 unsigned attr_name,
7448 bool &result)
7449 {
7450 Dwarf_Attribute l_attr, r_attr;
7451 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7452 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7453 return false;
7454
7455 ABG_ASSERT(l_attr.form == DW_FORM_strp
7456 || l_attr.form == DW_FORM_string
7457 || l_attr.form == DW_FORM_GNU_strp_alt
7458 || form_is_DW_FORM_strx(l_attr.form)
7459 || form_is_DW_FORM_line_strp(l_attr.form));
7460
7461 ABG_ASSERT(r_attr.form == DW_FORM_strp
7462 || r_attr.form == DW_FORM_string
7463 || r_attr.form == DW_FORM_GNU_strp_alt
7464 || form_is_DW_FORM_strx(r_attr.form)
7465 || form_is_DW_FORM_line_strp(r_attr.form));
7466
7467 if ((l_attr.form == DW_FORM_strp
7468 && r_attr.form == DW_FORM_strp)
7469 || (l_attr.form == DW_FORM_GNU_strp_alt
7470 && r_attr.form == DW_FORM_GNU_strp_alt)
7471 || (form_is_DW_FORM_strx(l_attr.form)
7472 && form_is_DW_FORM_strx(r_attr.form))
7473 || (form_is_DW_FORM_line_strp(l_attr.form)
7474 && form_is_DW_FORM_line_strp(r_attr.form)))
7475 {
7476 // So these string attributes are actually pointers into a
7477 // string table. The string table is most likely de-duplicated
7478 // so comparing the *values* of the pointers should be enough.
7479 //
7480 // This is the fast path.
7481 if (l_attr.valp == r_attr.valp)
7482 {
7483 #if WITH_DEBUG_TYPE_CANONICALIZATION
7484 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7485 #endif
7486 result = true;
7487 return true;
7488 }
7489 }
7490
7491 // If we reached this point it means we couldn't use the fast path
7492 // because the string atttributes are strings that are "inline" in
7493 // the debug info section. Let's just compare them the slow and
7494 // obvious way.
7495 result = slowly_compare_strings(l, r, attr_name);
7496 return true;
7497 }
7498
7499 /// Compare the file path of the compilation units (aka CUs)
7500 /// associated to two DIEs.
7501 ///
7502 /// If the DIEs are for pointers or typedefs, this function also
7503 /// compares the file paths of the CUs of the leaf DIEs (underlying
7504 /// DIEs of the pointer or the typedef).
7505 ///
7506 /// @param l the first type DIE to consider.
7507 ///
7508 /// @param r the second type DIE to consider.
7509 ///
7510 /// @return true iff the file paths of the DIEs of the two types are
7511 /// equal.
7512 static bool
compare_dies_cu_decl_file(const Dwarf_Die * l,const Dwarf_Die * r,bool & result)7513 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7514 {
7515 Dwarf_Die l_cu, r_cu;
7516 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7517 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7518 return false;
7519
7520 bool compared =
7521 compare_dies_string_attribute_value(&l_cu, &r_cu,
7522 DW_AT_name,
7523 result);
7524 if (compared && result)
7525 {
7526 Dwarf_Die peeled_l, peeled_r;
7527 if (die_is_pointer_reference_or_typedef_type(l)
7528 && die_is_pointer_reference_or_typedef_type(r)
7529 && die_peel_pointer_and_typedef(l, peeled_l)
7530 && die_peel_pointer_and_typedef(r, peeled_r))
7531 {
7532 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7533 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7534 return false;
7535 compared =
7536 compare_dies_string_attribute_value(&l_cu, &r_cu,
7537 DW_AT_name,
7538 result);
7539 }
7540 }
7541
7542 return compared;
7543 }
7544
7545 // -----------------------------------
7546 // <location expression evaluation>
7547 // -----------------------------------
7548
7549 /// Get the value of a given DIE attribute, knowing that it must be a
7550 /// location expression.
7551 ///
7552 /// @param die the DIE to read the attribute from.
7553 ///
7554 /// @param attr_name the name of the attribute to read the value for.
7555 ///
7556 /// @param expr the pointer to allocate and fill with the resulting
7557 /// array of operators + operands forming a dwarf expression. This is
7558 /// set iff the function returns true.
7559 ///
7560 /// @param expr_len the length of the resulting dwarf expression.
7561 /// This is set iff the function returns true.
7562 ///
7563 /// @return true if the attribute exists and has a non-empty dwarf expression
7564 /// as value. In that case the expr and expr_len arguments are set to the
7565 /// resulting dwarf expression.
7566 static bool
die_location_expr(const Dwarf_Die * die,unsigned attr_name,Dwarf_Op ** expr,size_t * expr_len)7567 die_location_expr(const Dwarf_Die* die,
7568 unsigned attr_name,
7569 Dwarf_Op** expr,
7570 size_t* expr_len)
7571 {
7572 if (!die)
7573 return false;
7574
7575 Dwarf_Attribute attr;
7576 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7577 return false;
7578
7579 size_t len = 0;
7580 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7581
7582 // Ignore location expressions where reading them succeeded but
7583 // their length is 0.
7584 result &= len > 0;
7585
7586 if (result)
7587 *expr_len = len;
7588
7589 return result;
7590 }
7591
7592 /// If the current operation in the dwarf expression represents a push
7593 /// of a constant value onto the dwarf expr virtual machine (aka
7594 /// DEVM), perform the operation and update the DEVM.
7595 ///
7596 /// If the result of the operation is a constant, update the DEVM
7597 /// accumulator with its value. Otherwise, the DEVM accumulator is
7598 /// left with its previous value.
7599 ///
7600 /// @param ops the array of the dwarf expression operations to consider.
7601 ///
7602 /// @param ops_len the lengths of @p ops array above.
7603 ///
7604 /// @param index the index of the operation to interpret, in @p ops.
7605 ///
7606 /// @param next_index the index of the operation to interpret at the
7607 /// next step, after this function completed and returned. This is
7608 /// set an output parameter that is set iff the function returns true.
7609 ///
7610 /// @param ctxt the DEVM evaluation context.
7611 ///
7612 /// @return true if the current operation actually pushes a constant
7613 /// value onto the DEVM stack, false otherwise.
7614 static bool
op_pushes_constant_value(Dwarf_Op * ops,size_t ops_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)7615 op_pushes_constant_value(Dwarf_Op* ops,
7616 size_t ops_len,
7617 size_t index,
7618 size_t& next_index,
7619 dwarf_expr_eval_context& ctxt)
7620 {
7621 ABG_ASSERT(index < ops_len);
7622
7623 Dwarf_Op& op = ops[index];
7624 int64_t value = 0;
7625
7626 switch (op.atom)
7627 {
7628 case DW_OP_addr:
7629 value = ops[index].number;
7630 break;
7631
7632 case DW_OP_const1u:
7633 case DW_OP_const1s:
7634 case DW_OP_const2u:
7635 case DW_OP_const2s:
7636 case DW_OP_const4u:
7637 case DW_OP_const4s:
7638 case DW_OP_const8u:
7639 case DW_OP_const8s:
7640 case DW_OP_constu:
7641 case DW_OP_consts:
7642 value = ops[index].number;
7643 break;
7644
7645 case DW_OP_lit0:
7646 value = 0;
7647 break;
7648 case DW_OP_lit1:
7649 value = 1;
7650 break;
7651 case DW_OP_lit2:
7652 value = 2;
7653 break;
7654 case DW_OP_lit3:
7655 value = 3;
7656 break;
7657 case DW_OP_lit4:
7658 value = 4;
7659 break;
7660 case DW_OP_lit5:
7661 value = 5;
7662 break;
7663 case DW_OP_lit6:
7664 value = 6;
7665 break;
7666 case DW_OP_lit7:
7667 value = 7;
7668 break;
7669 case DW_OP_lit8:
7670 value = 8;
7671 break;
7672 case DW_OP_lit9:
7673 value = 9;
7674 break;
7675 case DW_OP_lit10:
7676 value = 10;
7677 break;
7678 case DW_OP_lit11:
7679 value = 11;
7680 break;
7681 case DW_OP_lit12:
7682 value = 12;
7683 break;
7684 case DW_OP_lit13:
7685 value = 13;
7686 break;
7687 case DW_OP_lit14:
7688 value = 14;
7689 break;
7690 case DW_OP_lit15:
7691 value = 15;
7692 break;
7693 case DW_OP_lit16:
7694 value = 16;
7695 break;
7696 case DW_OP_lit17:
7697 value = 17;
7698 break;
7699 case DW_OP_lit18:
7700 value = 18;
7701 break;
7702 case DW_OP_lit19:
7703 value = 19;
7704 break;
7705 case DW_OP_lit20:
7706 value = 20;
7707 break;
7708 case DW_OP_lit21:
7709 value = 21;
7710 break;
7711 case DW_OP_lit22:
7712 value = 22;
7713 break;
7714 case DW_OP_lit23:
7715 value = 23;
7716 break;
7717 case DW_OP_lit24:
7718 value = 24;
7719 break;
7720 case DW_OP_lit25:
7721 value = 25;
7722 break;
7723 case DW_OP_lit26:
7724 value = 26;
7725 break;
7726 case DW_OP_lit27:
7727 value = 27;
7728 break;
7729 case DW_OP_lit28:
7730 value = 28;
7731 break;
7732 case DW_OP_lit29:
7733 value = 29;
7734 break;
7735 case DW_OP_lit30:
7736 value = 30;
7737 break;
7738 case DW_OP_lit31:
7739 value = 31;
7740 break;
7741
7742 default:
7743 return false;
7744 }
7745
7746 expr_result r(value);
7747 ctxt.push(r);
7748 ctxt.accum = r;
7749 next_index = index + 1;
7750
7751 return true;
7752 }
7753
7754 /// If the current operation in the dwarf expression represents a push
7755 /// of a non-constant value onto the dwarf expr virtual machine (aka
7756 /// DEVM), perform the operation and update the DEVM. A non-constant
7757 /// is namely a quantity for which we need inferior (a running program
7758 /// image) state to know the exact value.
7759 ///
7760 /// Upon successful completion, as the result of the operation is a
7761 /// non-constant the DEVM accumulator value is left to its state as of
7762 /// before the invocation of this function.
7763 ///
7764 /// @param ops the array of the dwarf expression operations to consider.
7765 ///
7766 /// @param ops_len the lengths of @p ops array above.
7767 ///
7768 /// @param index the index of the operation to interpret, in @p ops.
7769 ///
7770 /// @param next_index the index of the operation to interpret at the
7771 /// next step, after this function completed and returned. This is
7772 /// set an output parameter that is set iff the function returns true.
7773 ///
7774 /// @param ctxt the DEVM evaluation context.
7775 ///
7776 /// @return true if the current operation actually pushes a
7777 /// non-constant value onto the DEVM stack, false otherwise.
7778 static bool
op_pushes_non_constant_value(Dwarf_Op * ops,size_t ops_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)7779 op_pushes_non_constant_value(Dwarf_Op* ops,
7780 size_t ops_len,
7781 size_t index,
7782 size_t& next_index,
7783 dwarf_expr_eval_context& ctxt)
7784 {
7785 ABG_ASSERT(index < ops_len);
7786 Dwarf_Op& op = ops[index];
7787
7788 switch (op.atom)
7789 {
7790 case DW_OP_reg0:
7791 case DW_OP_reg1:
7792 case DW_OP_reg2:
7793 case DW_OP_reg3:
7794 case DW_OP_reg4:
7795 case DW_OP_reg5:
7796 case DW_OP_reg6:
7797 case DW_OP_reg7:
7798 case DW_OP_reg8:
7799 case DW_OP_reg9:
7800 case DW_OP_reg10:
7801 case DW_OP_reg11:
7802 case DW_OP_reg12:
7803 case DW_OP_reg13:
7804 case DW_OP_reg14:
7805 case DW_OP_reg15:
7806 case DW_OP_reg16:
7807 case DW_OP_reg17:
7808 case DW_OP_reg18:
7809 case DW_OP_reg19:
7810 case DW_OP_reg20:
7811 case DW_OP_reg21:
7812 case DW_OP_reg22:
7813 case DW_OP_reg23:
7814 case DW_OP_reg24:
7815 case DW_OP_reg25:
7816 case DW_OP_reg26:
7817 case DW_OP_reg27:
7818 case DW_OP_reg28:
7819 case DW_OP_reg29:
7820 case DW_OP_reg30:
7821 case DW_OP_reg31:
7822 next_index = index + 1;
7823 break;
7824
7825 case DW_OP_breg0:
7826 case DW_OP_breg1:
7827 case DW_OP_breg2:
7828 case DW_OP_breg3:
7829 case DW_OP_breg4:
7830 case DW_OP_breg5:
7831 case DW_OP_breg6:
7832 case DW_OP_breg7:
7833 case DW_OP_breg8:
7834 case DW_OP_breg9:
7835 case DW_OP_breg10:
7836 case DW_OP_breg11:
7837 case DW_OP_breg12:
7838 case DW_OP_breg13:
7839 case DW_OP_breg14:
7840 case DW_OP_breg15:
7841 case DW_OP_breg16:
7842 case DW_OP_breg17:
7843 case DW_OP_breg18:
7844 case DW_OP_breg19:
7845 case DW_OP_breg20:
7846 case DW_OP_breg21:
7847 case DW_OP_breg22:
7848 case DW_OP_breg23:
7849 case DW_OP_breg24:
7850 case DW_OP_breg25:
7851 case DW_OP_breg26:
7852 case DW_OP_breg27:
7853 case DW_OP_breg28:
7854 case DW_OP_breg29:
7855 case DW_OP_breg30:
7856 case DW_OP_breg31:
7857 next_index = index + 1;
7858 break;
7859
7860 case DW_OP_regx:
7861 next_index = index + 2;
7862 break;
7863
7864 case DW_OP_fbreg:
7865 next_index = index + 1;
7866 break;
7867
7868 case DW_OP_bregx:
7869 next_index = index + 1;
7870 break;
7871
7872 default:
7873 return false;
7874 }
7875
7876 expr_result r(false);
7877 ctxt.push(r);
7878
7879 return true;
7880 }
7881
7882 /// If the current operation in the dwarf expression represents a
7883 /// manipulation of the stack of the DWARF Expression Virtual Machine
7884 /// (aka DEVM), this function performs the operation and updates the
7885 /// state of the DEVM. If the result of the operation represents a
7886 /// constant value, then the accumulator of the DEVM is set to that
7887 /// result's value, Otherwise, the DEVM accumulator is left with its
7888 /// previous value.
7889 ///
7890 /// @param expr the array of the dwarf expression operations to consider.
7891 ///
7892 /// @param expr_len the lengths of @p ops array above.
7893 ///
7894 /// @param index the index of the operation to interpret, in @p ops.
7895 ///
7896 /// @param next_index the index of the operation to interpret at the
7897 /// next step, after this function completed and returned. This is
7898 /// set an output parameter that is set iff the function returns true.
7899 ///
7900 /// @param ctxt the DEVM evaluation context.
7901 ///
7902 /// @return true if the current operation actually manipulates the
7903 /// DEVM stack, false otherwise.
7904 static bool
op_manipulates_stack(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)7905 op_manipulates_stack(Dwarf_Op* expr,
7906 size_t expr_len,
7907 size_t index,
7908 size_t& next_index,
7909 dwarf_expr_eval_context& ctxt)
7910 {
7911 Dwarf_Op& op = expr[index];
7912 expr_result v;
7913
7914 switch (op.atom)
7915 {
7916 case DW_OP_dup:
7917 v = ctxt.stack.front();
7918 ctxt.push(v);
7919 break;
7920
7921 case DW_OP_drop:
7922 v = ctxt.stack.front();
7923 ctxt.pop();
7924 break;
7925
7926 case DW_OP_over:
7927 ABG_ASSERT(ctxt.stack.size() > 1);
7928 v = ctxt.stack[1];
7929 ctxt.push(v);
7930 break;
7931
7932 case DW_OP_pick:
7933 ABG_ASSERT(index + 1 < expr_len);
7934 v = op.number;
7935 ctxt.push(v);
7936 break;
7937
7938 case DW_OP_swap:
7939 ABG_ASSERT(ctxt.stack.size() > 1);
7940 v = ctxt.stack[1];
7941 ctxt.stack.erase(ctxt.stack.begin() + 1);
7942 ctxt.push(v);
7943 break;
7944
7945 case DW_OP_rot:
7946 ABG_ASSERT(ctxt.stack.size() > 2);
7947 v = ctxt.stack[2];
7948 ctxt.stack.erase(ctxt.stack.begin() + 2);
7949 ctxt.push(v);
7950 break;
7951
7952 case DW_OP_deref:
7953 case DW_OP_deref_size:
7954 ABG_ASSERT(ctxt.stack.size() > 0);
7955 ctxt.pop();
7956 v.is_const(false);
7957 ctxt.push(v);
7958 break;
7959
7960 case DW_OP_xderef:
7961 case DW_OP_xderef_size:
7962 ABG_ASSERT(ctxt.stack.size() > 1);
7963 ctxt.pop();
7964 ctxt.pop();
7965 v.is_const(false);
7966 ctxt.push(v);
7967 break;
7968
7969 case DW_OP_push_object_address:
7970 v.is_const(false);
7971 ctxt.push(v);
7972 break;
7973
7974 case DW_OP_form_tls_address:
7975 case DW_OP_GNU_push_tls_address:
7976 ABG_ASSERT(ctxt.stack.size() > 0);
7977 v = ctxt.pop();
7978 if (op.atom == DW_OP_form_tls_address)
7979 v.is_const(false);
7980 ctxt.push(v);
7981 break;
7982
7983 case DW_OP_call_frame_cfa:
7984 v.is_const(false);
7985 ctxt.push(v);
7986 break;
7987
7988 default:
7989 return false;
7990 }
7991
7992 if (v.is_const())
7993 ctxt.accum = v;
7994
7995 if (op.atom == DW_OP_form_tls_address
7996 || op.atom == DW_OP_GNU_push_tls_address)
7997 ctxt.set_tls_address(true);
7998 else
7999 ctxt.set_tls_address(false);
8000
8001 next_index = index + 1;
8002
8003 return true;
8004 }
8005
8006 /// If the current operation in the dwarf expression represents a push
8007 /// of an arithmetic or logic operation onto the dwarf expr virtual
8008 /// machine (aka DEVM), perform the operation and update the DEVM.
8009 ///
8010 /// If the result of the operation is a constant, update the DEVM
8011 /// accumulator with its value. Otherwise, the DEVM accumulator is
8012 /// left with its previous value.
8013 ///
8014 /// @param expr the array of the dwarf expression operations to consider.
8015 ///
8016 /// @param expr_len the lengths of @p expr array above.
8017 ///
8018 /// @param index the index of the operation to interpret, in @p expr.
8019 ///
8020 /// @param next_index the index of the operation to interpret at the
8021 /// next step, after this function completed and returned. This is
8022 /// set an output parameter that is set iff the function returns true.
8023 ///
8024 /// @param ctxt the DEVM evaluation context.
8025 ///
8026 /// @return true if the current operation actually represent an
8027 /// arithmetic or logic operation.
8028 static bool
op_is_arith_logic(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)8029 op_is_arith_logic(Dwarf_Op* expr,
8030 size_t expr_len,
8031 size_t index,
8032 size_t& next_index,
8033 dwarf_expr_eval_context& ctxt)
8034 {
8035 ABG_ASSERT(index < expr_len);
8036
8037 Dwarf_Op& op = expr[index];
8038 expr_result val1, val2;
8039
8040 switch (op.atom)
8041 {
8042 case DW_OP_abs:
8043 val1 = ctxt.pop();
8044 val1 = val1.abs();
8045 ctxt.push(val1);
8046 break;
8047
8048 case DW_OP_and:
8049 ABG_ASSERT(ctxt.stack.size() > 1);
8050 val1 = ctxt.pop();
8051 val2 = ctxt.pop();
8052 ctxt.push(val1 & val2);
8053 break;
8054
8055 case DW_OP_div:
8056 val1 = ctxt.pop();
8057 val2 = ctxt.pop();
8058 if (!val1.is_const())
8059 val1 = 1;
8060 ctxt.push(val2 / val1);
8061 break;
8062
8063 case DW_OP_minus:
8064 val1 = ctxt.pop();
8065 val2 = ctxt.pop();
8066 ctxt.push(val2 - val1);
8067 break;
8068
8069 case DW_OP_mod:
8070 val1 = ctxt.pop();
8071 val2 = ctxt.pop();
8072 ctxt.push(val2 % val1);
8073 break;
8074
8075 case DW_OP_mul:
8076 val1 = ctxt.pop();
8077 val2 = ctxt.pop();
8078 ctxt.push(val2 * val1);
8079 break;
8080
8081 case DW_OP_neg:
8082 val1 = ctxt.pop();
8083 ctxt.push(-val1);
8084 break;
8085
8086 case DW_OP_not:
8087 val1 = ctxt.pop();
8088 ctxt.push(~val1);
8089 break;
8090
8091 case DW_OP_or:
8092 val1 = ctxt.pop();
8093 val2 = ctxt.pop();
8094 ctxt.push(val1 | val2);
8095 break;
8096
8097 case DW_OP_plus:
8098 val1 = ctxt.pop();
8099 val2 = ctxt.pop();
8100 ctxt.push(val2 + val1);
8101 break;
8102
8103 case DW_OP_plus_uconst:
8104 val1 = ctxt.pop();
8105 val1 += op.number;
8106 ctxt.push(val1);
8107 break;
8108
8109 case DW_OP_shl:
8110 val1 = ctxt.pop();
8111 val2 = ctxt.pop();
8112 ctxt.push(val2 << val1);
8113 break;
8114
8115 case DW_OP_shr:
8116 case DW_OP_shra:
8117 val1 = ctxt.pop();
8118 val2 = ctxt.pop();
8119 ctxt.push(val2 >> val1);
8120 break;
8121
8122 case DW_OP_xor:
8123 val1 = ctxt.pop();
8124 val2 = ctxt.pop();
8125 ctxt.push(val2 ^ val1);
8126 break;
8127
8128 default:
8129 return false;
8130 }
8131
8132 if (ctxt.stack.front().is_const())
8133 ctxt.accum = ctxt.stack.front();
8134
8135 next_index = index + 1;
8136 return true;
8137 }
8138
8139 /// If the current operation in the dwarf expression represents a push
8140 /// of a control flow operation onto the dwarf expr virtual machine
8141 /// (aka DEVM), perform the operation and update the DEVM.
8142 ///
8143 /// If the result of the operation is a constant, update the DEVM
8144 /// accumulator with its value. Otherwise, the DEVM accumulator is
8145 /// left with its previous value.
8146 ///
8147 /// @param expr the array of the dwarf expression operations to consider.
8148 ///
8149 /// @param expr_len the lengths of @p expr array above.
8150 ///
8151 /// @param index the index of the operation to interpret, in @p expr.
8152 ///
8153 /// @param next_index the index of the operation to interpret at the
8154 /// next step, after this function completed and returned. This is
8155 /// set an output parameter that is set iff the function returns true.
8156 ///
8157 /// @param ctxt the DEVM evaluation context.
8158 ///
8159 /// @return true if the current operation actually represents a
8160 /// control flow operation, false otherwise.
8161 static bool
op_is_control_flow(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)8162 op_is_control_flow(Dwarf_Op* expr,
8163 size_t expr_len,
8164 size_t index,
8165 size_t& next_index,
8166 dwarf_expr_eval_context& ctxt)
8167 {
8168 ABG_ASSERT(index < expr_len);
8169
8170 Dwarf_Op& op = expr[index];
8171 expr_result val1, val2;
8172
8173 switch (op.atom)
8174 {
8175 case DW_OP_eq:
8176 case DW_OP_ge:
8177 case DW_OP_gt:
8178 case DW_OP_le:
8179 case DW_OP_lt:
8180 case DW_OP_ne:
8181 {
8182 bool value = true;
8183 val1 = ctxt.pop();
8184 val2 = ctxt.pop();
8185 if (op.atom == DW_OP_eq)
8186 value = val2 == val1;
8187 else if (op.atom == DW_OP_ge)
8188 value = val2 >= val1;
8189 else if (op.atom == DW_OP_gt)
8190 value = val2 > val1;
8191 else if (op.atom == DW_OP_le)
8192 value = val2 <= val1;
8193 else if (op.atom == DW_OP_lt)
8194 value = val2 < val1;
8195 else if (op.atom == DW_OP_ne)
8196 value = val2 != val1;
8197
8198 val1 = value ? 1 : 0;
8199 ctxt.push(val1);
8200 }
8201 break;
8202
8203 case DW_OP_skip:
8204 if (op.number > 0)
8205 index += op.number - 1;
8206 break;
8207
8208 case DW_OP_bra:
8209 val1 = ctxt.pop();
8210 if (val1 != 0)
8211 index += val1.const_value() - 1;
8212 break;
8213
8214 case DW_OP_call2:
8215 case DW_OP_call4:
8216 case DW_OP_call_ref:
8217 case DW_OP_nop:
8218 break;
8219
8220 default:
8221 return false;
8222 }
8223
8224 if (ctxt.stack.front().is_const())
8225 ctxt.accum = ctxt.stack.front();
8226
8227 next_index = index + 1;
8228 return true;
8229 }
8230
8231 /// This function quickly evaluates a DWARF expression that is a
8232 /// constant.
8233 ///
8234 /// This is a "fast path" function that quickly evaluates a DWARF
8235 /// expression that is only made of a DW_OP_plus_uconst operator.
8236 ///
8237 /// This is a sub-routine of die_member_offset.
8238 ///
8239 /// @param expr the DWARF expression to evaluate.
8240 ///
8241 /// @param expr_len the length of the expression @p expr.
8242 ///
8243 /// @param value out parameter. This is set to the result of the
8244 /// evaluation of @p expr, iff this function returns true.
8245 ///
8246 /// @return true iff the evaluation of @p expr went OK.
8247 static bool
eval_quickly(Dwarf_Op * expr,uint64_t expr_len,int64_t & value)8248 eval_quickly(Dwarf_Op* expr,
8249 uint64_t expr_len,
8250 int64_t& value)
8251 {
8252 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8253 {
8254 value = expr[0].number;
8255 return true;
8256 }
8257 return false;
8258 }
8259
8260 /// Evaluate the value of the last sub-expression that is a constant,
8261 /// inside a given DWARF expression.
8262 ///
8263 /// @param expr the DWARF expression to consider.
8264 ///
8265 /// @param expr_len the length of the expression to consider.
8266 ///
8267 /// @param value the resulting value of the last constant
8268 /// sub-expression of the DWARF expression. This is set iff the
8269 /// function returns true.
8270 ///
8271 /// @param is_tls_address out parameter. This is set to true iff
8272 /// the resulting value of the evaluation is a TLS (thread local
8273 /// storage) address.
8274 ///
8275 /// @param eval_ctxt the evaluation context to (re)use. Note that
8276 /// this function initializes this context before using it.
8277 ///
8278 /// @return true if the function could find a constant sub-expression
8279 /// to evaluate, false otherwise.
8280 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,size_t expr_len,int64_t & value,bool & is_tls_address,dwarf_expr_eval_context & eval_ctxt)8281 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8282 size_t expr_len,
8283 int64_t& value,
8284 bool& is_tls_address,
8285 dwarf_expr_eval_context &eval_ctxt)
8286 {
8287 // Reset the evaluation context before evaluating the constant sub
8288 // expression contained in the DWARF expression 'expr'.
8289 eval_ctxt.reset();
8290
8291 size_t index = 0, next_index = 0;
8292 do
8293 {
8294 if (op_is_arith_logic(expr, expr_len, index,
8295 next_index, eval_ctxt)
8296 || op_pushes_constant_value(expr, expr_len, index,
8297 next_index, eval_ctxt)
8298 || op_manipulates_stack(expr, expr_len, index,
8299 next_index, eval_ctxt)
8300 || op_pushes_non_constant_value(expr, expr_len, index,
8301 next_index, eval_ctxt)
8302 || op_is_control_flow(expr, expr_len, index,
8303 next_index, eval_ctxt))
8304 ;
8305 else
8306 next_index = index + 1;
8307
8308 ABG_ASSERT(next_index > index);
8309 index = next_index;
8310 } while (index < expr_len);
8311
8312 is_tls_address = eval_ctxt.set_tls_address();
8313 if (eval_ctxt.accum.is_const())
8314 {
8315 value = eval_ctxt.accum;
8316 return true;
8317 }
8318 return false;
8319 }
8320
8321 /// Evaluate the value of the last sub-expression that is a constant,
8322 /// inside a given DWARF expression.
8323 ///
8324 /// @param expr the DWARF expression to consider.
8325 ///
8326 /// @param expr_len the length of the expression to consider.
8327 ///
8328 /// @param value the resulting value of the last constant
8329 /// sub-expression of the DWARF expression. This is set iff the
8330 /// function returns true.
8331 ///
8332 /// @return true if the function could find a constant sub-expression
8333 /// to evaluate, false otherwise.
8334 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,size_t expr_len,int64_t & value,bool & is_tls_address)8335 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8336 size_t expr_len,
8337 int64_t& value,
8338 bool& is_tls_address)
8339 {
8340 dwarf_expr_eval_context eval_ctxt;
8341 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8342 is_tls_address, eval_ctxt);
8343 }
8344
8345 // -----------------------------------
8346 // </location expression evaluation>
8347 // -----------------------------------
8348
8349 /// Convert a DW_AT_bit_offset attribute value into the same value as
8350 /// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8351 ///
8352 /// On big endian machines, the value of the DW_AT_bit_offset
8353 /// attribute + 8 * the value of the DW_AT_data_member_location
8354 /// attribute is the same as the value of the DW_AT_data_bit_offset
8355 /// attribute.
8356 ///
8357 /// On little endian machines however, the situation is different.
8358 /// The DW_AT_bit_offset value for a bit field is the number of bits
8359 /// to the left of the most significant bit of the bit field, within
8360 /// the integer value at DW_AT_data_member_location.
8361 ///
8362 /// The DW_AT_data_bit_offset offset value is the number of bits to
8363 /// the right of the least significant bit of the bit field, again
8364 /// relative to the containing integer value.
8365 ///
8366 /// In other words, DW_AT_data_bit_offset is what everybody would
8367 /// instinctively think of as being the "offset of the bit field". 8 *
8368 /// DW_AT_data_member_location + DW_AT_bit_offset however is very
8369 /// counter-intuitive on little endian machines.
8370 ///
8371 /// This function thus reads the value of a DW_AT_bit_offset property
8372 /// of a DIE and converts it into what the DW_AT_data_bit_offset would
8373 /// have been if it was present, ignoring the contribution of
8374 /// DW_AT_data_member_location.
8375 ///
8376 /// Note that DW_AT_bit_offset has been made obsolete starting from
8377 /// DWARF5 (for GCC; Clang still emits it).
8378 ///
8379 /// If you like coffee and it's not too late, now might be a good time
8380 /// to have a coffee break. Otherwise if it's late at night, you
8381 /// might want to consider an herbal tea break. Then come back to
8382 /// read this.
8383 ///
8384 ///
8385 /// In what follows, the bit fields are all contained within the first
8386 /// whole int of the struct, so DW_AT_data_member_location is 0.
8387 ///
8388 /// Okay, to have a better idea of what DW_AT_bit_offset and
8389 /// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8390 /// have bit fields data members defined as:
8391 ///
8392 /// struct S
8393 /// {
8394 /// int j:5;
8395 /// int k:6;
8396 /// int m:5;
8397 /// int n:8;
8398 /// };
8399 ///
8400 /// The below wonderful (at least!) ASCII art sketch describes the
8401 /// layout of the bitfields of 'struct S' on a little endian machine.
8402 /// You need to read the sketch from the bottom-up.
8403 ///
8404 /// So please scroll down to its bottom. Note how the 32 bits integer
8405 /// word containing the bit fields is laid out with its least
8406 /// significant bit starting on the right hand side, at index 0.
8407 ///
8408 /// Then slowly scroll up starting from there, and take the time to
8409 /// read each line and see how the bit fields are laid out and what
8410 /// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8411 /// the bit fields.
8412 ///
8413 /// DW_AT_bit_offset(n)
8414 /// < - - - - - - >
8415 /// | | n |
8416 /// ^ ^< - - - - >^
8417 /// DW_AT_data_bit_offset(n)
8418 /// < - - - - - - - - - - - - - - - >
8419 /// | |
8420 /// ^ ^
8421 /// DW_AT_bit_offset(m)
8422 /// <--------------------------------->
8423 /// | | m |
8424 /// ^ ^< - >^
8425 /// DW_AT_data_bit_offset(m)
8426 /// < - - - - - - - - - - >
8427 /// | |
8428 /// ^ ^
8429 /// DW_AT_bit_offset(k)
8430 /// <-------------------------------------------->
8431 /// | | k |
8432 /// ^ ^< - - >^
8433 /// DW_AT_data_bit_offset(k)
8434 /// < - - - - >
8435 /// | |
8436 /// ^ ^
8437 /// DW_AT_bit_offset(j)
8438 /// <-------------------------------------------------------->
8439 /// | |
8440 /// ^ ^
8441 /// n m k j
8442 /// < - - - - - - > < - - - > < - - - - > < - - - >
8443 ///
8444 /// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8445 /// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8446 /// 31 27 23 16 15 11 10 6 5 4 0
8447 ///
8448 /// So, the different bit fields all fit in one 32 bits word, assuming
8449 /// the bit fields are tightly packed.
8450 ///
8451 /// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8452 /// on this little endian machine and let's see how it relates to
8453 /// DW_AT_data_bit_offset of j.
8454 ///
8455 /// DW_AT_bit_offset(j) would be equal to the number of bits from the
8456 /// left of the 32 bits word (i.e from bit number 31) to the most
8457 /// significant bit of the j bit field (i.e, bit number 4). Thus:
8458 ///
8459 /// DW_AT_bit_offset(j) =
8460 /// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8461 ///
8462 /// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8463 /// 32 bits word (i.e, bit number 0) to the lest significant bit of
8464 /// the 'j' bit field (ie, bit number 0). Thus:
8465 ///
8466 /// DW_AT_data_bit_offset(j) = 0.
8467 ///
8468 /// More generally, we can notice that:
8469 ///
8470 /// sizeof_in_bits(int) =
8471 /// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8472 ///
8473 /// It follows that:
8474 ///
8475 /// DW_AT_data_bit_offset(j) =
8476 /// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8477 ///
8478 /// Thus:
8479 ///
8480 /// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8481 ///
8482 /// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8483 /// from the right hand side of the word. It is what we would
8484 /// intuitively think it is. DW_AT_bit_offset however is super
8485 /// counter-intuitive, pfff.
8486 ///
8487 /// Anyway, this general equation holds true for all bit fields.
8488 ///
8489 /// Similarly, it follows that:
8490 ///
8491 /// DW_AT_bit_offset(k) =
8492 /// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8493 ///
8494 /// Thus:
8495 /// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8496 ///
8497 ///
8498 /// Likewise:
8499 ///
8500 /// DW_AT_bit_offset(m) =
8501 /// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8502 ///
8503 ///
8504 /// Thus:
8505 /// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8506 ///
8507 /// And:
8508 ///
8509 ///
8510 /// Lastly:
8511 ///
8512 /// DW_AT_bit_offset(n) =
8513 /// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8514 ///
8515 /// Thus:
8516 /// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8517 ///
8518 /// Luckily, the body of the function is much smaller than this
8519 /// comment. Enjoy!
8520 ///
8521 /// @param die the DIE to consider.
8522 ///
8523 /// @param is_big_endian this is true iff the machine we are looking at
8524 /// is big endian.
8525 ///
8526 /// @param offset this is the output parameter into which the value of
8527 /// the DW_AT_bit_offset is put, converted as if it was the value of
8528 /// the DW_AT_data_bit_offset parameter, less the contribution of
8529 /// DW_AT_data_member_location. This parameter is set iff the
8530 /// function returns true.
8531 ///
8532 /// @return true if DW_AT_bit_offset was found on @p die.
8533 static bool
read_and_convert_DW_at_bit_offset(const Dwarf_Die * die,bool is_big_endian,uint64_t & offset)8534 read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8535 bool is_big_endian,
8536 uint64_t &offset)
8537 {
8538 uint64_t off = 0;
8539 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8540 return false;
8541
8542 if (is_big_endian)
8543 {
8544 offset = off;
8545 return true;
8546 }
8547
8548 // Okay, we are looking at a little endian machine. We need to
8549 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8550 // have been. To understand this, you really need to read the
8551 // preliminary comment of this function.
8552 uint64_t containing_anonymous_object_size = 0;
8553 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8554 containing_anonymous_object_size));
8555 containing_anonymous_object_size *= 8;
8556
8557 uint64_t bitfield_size = 0;
8558 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8559 bitfield_size));
8560
8561 // As noted in the the preliminary comment of this function if we
8562 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8563 // its DW_AT_bit_offset value, the equation is:
8564 //
8565 // DW_AT_data_bit_offset(k) =
8566 // sizeof_in_bits(containing_anonymous_object_size)
8567 // - DW_AT_data_bit_offset(k)
8568 // - sizeof_in_bits(k)
8569 offset = containing_anonymous_object_size - off - bitfield_size;
8570
8571 return true;
8572 }
8573
8574 /// Get the value of the DW_AT_data_member_location of the given DIE
8575 /// attribute as an constant.
8576 ///
8577 /// @param die the DIE to read the attribute from.
8578 ///
8579 /// @param offset the attribute as a constant value. This is set iff
8580 /// the function returns true.
8581 ///
8582 /// @return true if the attribute exists and has a constant value. In
8583 /// that case the offset is set to the value.
8584 static bool
die_constant_data_member_location(const Dwarf_Die * die,int64_t & offset)8585 die_constant_data_member_location(const Dwarf_Die *die,
8586 int64_t& offset)
8587 {
8588 if (!die)
8589 return false;
8590
8591 Dwarf_Attribute attr;
8592 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8593 DW_AT_data_member_location,
8594 &attr))
8595 return false;
8596
8597 Dwarf_Word val;
8598 if (dwarf_formudata(&attr, &val) != 0)
8599 return false;
8600
8601 offset = val;
8602 return true;
8603 }
8604
8605 /// Get the offset of a struct/class member as represented by the
8606 /// value of the DW_AT_data_member_location attribute.
8607 ///
8608 /// There is a huge gotcha in here. The value of the
8609 /// DW_AT_data_member_location is not necessarily a constant that one
8610 /// would just read and be done with it. Rather, it can be a DWARF
8611 /// expression that one has to interpret. In general, the offset can
8612 /// be given by the DW_AT_data_bit_offset or by the
8613 /// DW_AT_data_member_location attribute and optionally the
8614 /// DW_AT_bit_offset attribute. The bit offset attributes are
8615 /// always simple constants, but the DW_AT_data_member_location
8616 /// attribute is a DWARF location expression.
8617 ///
8618 /// When it's the DW_AT_data_member_location that is present,
8619 /// there are three cases to possibly take into account:
8620 ///
8621 /// 1/ The offset in the vtable where the offset of a virtual base
8622 /// can be found, aka vptr offset. Given the address of a
8623 /// given object O, the vptr offset for B is given by the
8624 /// (DWARF) expression:
8625 ///
8626 /// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8627 ///
8628 /// where VIRTUAL_OFFSET is a constant value; In this case,
8629 /// this function returns the constant VIRTUAL_OFFSET, as this
8630 /// is enough to detect changes in a given virtual base
8631 /// relative to the other virtual bases.
8632 ///
8633 /// 2/ The offset of a regular data member. Given the address of
8634 /// a struct object named O, the memory location for a
8635 /// particular data member is given by the (DWARF) expression:
8636 ///
8637 /// address(O) + OFFSET
8638 ///
8639 /// where OFFSET is a constant. In this case, this function
8640 /// returns the OFFSET constant.
8641 ///
8642 /// 3/ The offset of a virtual member function in the virtual
8643 /// pointer. The DWARF expression is a constant that designates
8644 /// the offset of the function in the vtable. In this case this
8645 /// function returns that constant.
8646 ///
8647 /// @param rdr the DWARF reader to consider.
8648 ///
8649 /// @param die the DIE to read the information from.
8650 ///
8651 /// @param offset the resulting constant offset, in bits. This
8652 /// argument is set iff the function returns true.
8653 static bool
die_member_offset(const reader & rdr,const Dwarf_Die * die,int64_t & offset)8654 die_member_offset(const reader& rdr,
8655 const Dwarf_Die* die,
8656 int64_t& offset)
8657 {
8658 Dwarf_Op* expr = NULL;
8659 size_t expr_len = 0;
8660 uint64_t bit_offset = 0;
8661
8662 // First let's see if the DW_AT_data_bit_offset attribute is
8663 // present.
8664 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8665 {
8666 offset = bit_offset;
8667 return true;
8668 }
8669
8670 // First try to read DW_AT_data_member_location as a plain constant.
8671 // We do this because the generic method using die_location_expr
8672 // might hit a bug in elfutils libdw dwarf_location_expression only
8673 // fixed in elfutils 0.184+. The bug only triggers if the attribute
8674 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8675 // handle all constants here because that is more consistent (and
8676 // slightly faster in the general case where the attribute isn't a
8677 // full DWARF expression).
8678 if (!die_constant_data_member_location(die, offset))
8679 {
8680 // Otherwise, let's see if the DW_AT_data_member_location
8681 // attribute and, optionally, the DW_AT_bit_offset attributes
8682 // are present.
8683 if (!die_location_expr(die, DW_AT_data_member_location,
8684 &expr, &expr_len))
8685 return false;
8686
8687 // The DW_AT_data_member_location attribute is present. Let's
8688 // evaluate it and get its constant sub-expression and return
8689 // that one.
8690 if (!eval_quickly(expr, expr_len, offset))
8691 {
8692 bool is_tls_address = false;
8693 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8694 offset, is_tls_address,
8695 rdr.dwarf_expr_eval_ctxt()))
8696 return false;
8697 }
8698 }
8699 offset *= 8;
8700
8701 // On little endian machines, we need to convert the
8702 // DW_AT_bit_offset attribute into a relative offset to 8 *
8703 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8704 // would be if it were used instead.
8705 //
8706 // In other words, before adding it to 8 *
8707 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8708 // converted into a human-understandable form that represents the
8709 // offset of the bitfield data member it describes. For details
8710 // about the conversion, please read the extensive comments of
8711 // read_and_convert_DW_at_bit_offset.
8712 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
8713 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8714 offset += bit_offset;
8715
8716 return true;
8717 }
8718
8719 /// Read the value of the DW_AT_location attribute from a DIE,
8720 /// evaluate the resulting DWARF expression and, if it's a constant
8721 /// expression, return it.
8722 ///
8723 /// @param die the DIE to consider.
8724 ///
8725 /// @param address the resulting constant address. This is set iff
8726 /// the function returns true.
8727 ///
8728 /// @return true iff the whole sequence of action described above
8729 /// could be completed normally.
8730 static bool
die_location_address(Dwarf_Die * die,Dwarf_Addr & address,bool & is_tls_address)8731 die_location_address(Dwarf_Die* die,
8732 Dwarf_Addr& address,
8733 bool& is_tls_address)
8734 {
8735 Dwarf_Op* expr = NULL;
8736 size_t expr_len = 0;
8737
8738 is_tls_address = false;
8739
8740 if (!die)
8741 return false;
8742
8743 Dwarf_Attribute attr;
8744 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8745 return false;
8746
8747 if (dwarf_getlocation(&attr, &expr, &expr_len))
8748 return false;
8749 // Ignore location expressions where reading them succeeded but
8750 // their length is 0.
8751 if (expr_len == 0)
8752 return false;
8753
8754 Dwarf_Attribute result;
8755 if (!dwarf_getlocation_attr(&attr, expr, &result))
8756 // A location that has been interpreted as an address.
8757 return !dwarf_formaddr(&result, &address);
8758
8759 // Just get the address out of the number field.
8760 address = expr->number;
8761 return true;
8762 }
8763
8764 /// Return the index of a function in its virtual table. That is,
8765 /// return the value of the DW_AT_vtable_elem_location attribute.
8766 ///
8767 /// @param die the DIE of the function to consider.
8768 ///
8769 /// @param vindex the resulting index. This is set iff the function
8770 /// returns true.
8771 ///
8772 /// @return true if the DIE has a DW_AT_vtable_elem_location
8773 /// attribute.
8774 static bool
die_virtual_function_index(Dwarf_Die * die,int64_t & vindex)8775 die_virtual_function_index(Dwarf_Die* die,
8776 int64_t& vindex)
8777 {
8778 if (!die)
8779 return false;
8780
8781 Dwarf_Op* expr = NULL;
8782 size_t expr_len = 0;
8783 if (!die_location_expr(die, DW_AT_vtable_elem_location,
8784 &expr, &expr_len))
8785 return false;
8786
8787 int64_t i = 0;
8788 bool is_tls_addr = false;
8789 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
8790 return false;
8791
8792 vindex = i;
8793 return true;
8794 }
8795
8796 /// Test if a given DIE represents an anonymous type.
8797 ///
8798 /// Anonymous types we are interested in are classes, unions and
8799 /// enumerations.
8800 ///
8801 /// @param die the DIE to consider.
8802 ///
8803 /// @return true iff @p die represents an anonymous type.
8804 bool
is_anonymous_type_die(Dwarf_Die * die)8805 is_anonymous_type_die(Dwarf_Die *die)
8806 {
8807 int tag = dwarf_tag(die);
8808
8809 if (tag == DW_TAG_class_type
8810 || tag == DW_TAG_structure_type
8811 || tag == DW_TAG_union_type
8812 || tag == DW_TAG_enumeration_type)
8813 return die_is_anonymous(die);
8814
8815 return false;
8816 }
8817
8818 /// Return the base of the internal name to represent an anonymous
8819 /// type.
8820 ///
8821 /// Typically, anonymous enums would be named
8822 /// __anonymous_enum__<number>, anonymous struct or classes would be
8823 /// named __anonymous_struct__<number> and anonymous unions would be
8824 /// named __anonymous_union__<number>. The first part of these
8825 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
8826 /// the base name. This function returns that base name, depending on
8827 /// the kind of type DIE we are looking at.
8828 ///
8829 /// @param die the type DIE to look at. This function expects a type
8830 /// DIE with an empty DW_AT_name property value (anonymous).
8831 ///
8832 /// @return a string representing the base of the internal anonymous
8833 /// name.
8834 static string
get_internal_anonymous_die_prefix_name(const Dwarf_Die * die)8835 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
8836 {
8837 ABG_ASSERT(die_is_type(die));
8838 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
8839
8840 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8841 string type_name;
8842 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
8843 type_name = tools_utils::get_anonymous_struct_internal_name_prefix();
8844 else if (tag == DW_TAG_union_type)
8845 type_name = tools_utils::get_anonymous_union_internal_name_prefix();
8846 else if (tag == DW_TAG_enumeration_type)
8847 type_name = tools_utils::get_anonymous_enum_internal_name_prefix();
8848
8849 return type_name;
8850 }
8851
8852 /// Build a full internal anonymous type name.
8853 ///
8854 /// @param base_name this is the base name as returned by the function
8855 /// @ref get_internal_anonymous_die_prefix_name.
8856 ///
8857 /// @param anonymous_type_index this is the index of the anonymous
8858 /// type in its scope. That is, if there are more than one anonymous
8859 /// types of a given kind in a scope, this index is what tells them
8860 /// appart, starting from 0.
8861 ///
8862 /// @return the built string, which is a concatenation of @p base_name
8863 /// and @p anonymous_type_index.
8864 static string
build_internal_anonymous_die_name(const string & base_name,size_t anonymous_type_index)8865 build_internal_anonymous_die_name(const string &base_name,
8866 size_t anonymous_type_index)
8867 {
8868 string name = base_name;
8869 if (anonymous_type_index && !base_name.empty())
8870 {
8871 std::ostringstream o;
8872 o << base_name << anonymous_type_index;
8873 name = o.str();
8874 }
8875 return name;
8876 }
8877
8878
8879 /// Build a full internal anonymous type name.
8880 ///
8881 /// @param die the DIE representing the anonymous type to consider.
8882 ///
8883 /// @param anonymous_type_index the index of the anonymous type
8884 /// represented by @p DIE, in its scope. That is, if there are
8885 /// several different anonymous types of the same kind as @p die, this
8886 /// index is what tells them appart.
8887 ///
8888 /// @return the internal name of the anonymous type represented by @p
8889 /// DIE.
8890 static string
get_internal_anonymous_die_name(Dwarf_Die * die,size_t anonymous_type_index)8891 get_internal_anonymous_die_name(Dwarf_Die *die,
8892 size_t anonymous_type_index)
8893 {
8894 string name = get_internal_anonymous_die_prefix_name(die);
8895 name = build_internal_anonymous_die_name(name, anonymous_type_index);
8896 return name;
8897 }
8898
8899 // ------------------------------------
8900 // <DIE pretty printer>
8901 // ------------------------------------
8902
8903 /// Compute the qualified name of a DIE that represents a type.
8904 ///
8905 /// For instance, if the DIE tag is DW_TAG_subprogram then this
8906 /// function computes the name of the function *type*.
8907 ///
8908 /// @param rdr the DWARF reader.
8909 ///
8910 /// @param die the DIE to consider.
8911 ///
8912 /// @param where_offset where in the are logically are in the DIE
8913 /// stream.
8914 ///
8915 /// @return a copy of the qualified name of the type.
8916 static string
die_qualified_type_name(const reader & rdr,const Dwarf_Die * die,size_t where_offset)8917 die_qualified_type_name(const reader& rdr,
8918 const Dwarf_Die* die,
8919 size_t where_offset)
8920 {
8921 if (!die)
8922 return "";
8923
8924 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
8925 if (tag == DW_TAG_compile_unit
8926 || tag == DW_TAG_partial_unit
8927 || tag == DW_TAG_type_unit)
8928 return "";
8929
8930 string name = die_name(die);
8931
8932 Dwarf_Die scope_die;
8933 if (!get_scope_die(rdr, die, where_offset, scope_die))
8934 return "";
8935
8936 string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
8937 bool colon_colon = die_is_type(die) || die_is_namespace(die);
8938 string separator = colon_colon ? "::" : ".";
8939
8940 string repr;
8941
8942 switch (tag)
8943 {
8944 case DW_TAG_unspecified_type:
8945 break;
8946
8947 case DW_TAG_base_type:
8948 {
8949 abigail::ir::integral_type int_type;
8950 if (parse_integral_type(name, int_type))
8951 repr = int_type;
8952 else
8953 repr = name;
8954 }
8955 break;
8956
8957 case DW_TAG_typedef:
8958 case DW_TAG_enumeration_type:
8959 case DW_TAG_structure_type:
8960 case DW_TAG_class_type:
8961 case DW_TAG_union_type:
8962 {
8963 if (name.empty())
8964 // TODO: handle cases where there are more than one
8965 // anonymous type of the same kind in the same scope. In
8966 // that case, their name must be built with the function
8967 // get_internal_anonymous_die_name or something of the same
8968 // kind.
8969 name = get_internal_anonymous_die_prefix_name(die);
8970
8971 ABG_ASSERT(!name.empty());
8972 repr = parent_name.empty() ? name : parent_name + separator + name;
8973 }
8974 break;
8975
8976 case DW_TAG_const_type:
8977 case DW_TAG_volatile_type:
8978 case DW_TAG_restrict_type:
8979 {
8980 Dwarf_Die underlying_type_die;
8981 bool has_underlying_type_die =
8982 die_die_attribute(die, DW_AT_type, underlying_type_die);
8983
8984 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
8985 break;
8986
8987 if (tag == DW_TAG_const_type)
8988 {
8989 if (has_underlying_type_die
8990 && die_is_reference_type(&underlying_type_die))
8991 // A reference is always const. So, to lower false
8992 // positive reports in diff computations, we consider a
8993 // const reference just as a reference. But we need to
8994 // keep the qualified-ness of the type. So we introduce
8995 // a 'no-op' qualifier here. Please remember that this
8996 // has to be kept in sync with what is done in
8997 // get_name_of_qualified_type. So if you change this
8998 // here, you have to change that code there too.
8999 repr = "";
9000 else if (!has_underlying_type_die
9001 || die_is_void_type(&underlying_type_die))
9002 {
9003 repr = "void";
9004 break;
9005 }
9006 else
9007 repr = "const";
9008 }
9009 else if (tag == DW_TAG_volatile_type)
9010 repr = "volatile";
9011 else if (tag == DW_TAG_restrict_type)
9012 repr = "restrict";
9013 else
9014 ABG_ASSERT_NOT_REACHED;
9015
9016 string underlying_type_repr;
9017 if (has_underlying_type_die)
9018 underlying_type_repr =
9019 die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9020 else
9021 underlying_type_repr = "void";
9022
9023 if (underlying_type_repr.empty())
9024 repr.clear();
9025 else
9026 {
9027 if (has_underlying_type_die
9028 && die_is_pointer_or_reference_type(&underlying_type_die))
9029 repr = underlying_type_repr + " " + repr;
9030 else
9031 repr += " " + underlying_type_repr;
9032 }
9033 }
9034 break;
9035
9036 case DW_TAG_pointer_type:
9037 case DW_TAG_reference_type:
9038 case DW_TAG_rvalue_reference_type:
9039 {
9040 Dwarf_Die pointed_to_type_die;
9041 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9042 {
9043 if (tag == DW_TAG_pointer_type)
9044 repr = "void*";
9045 break;
9046 }
9047
9048 if (die_is_unspecified(&pointed_to_type_die))
9049 break;
9050
9051 string pointed_type_repr =
9052 die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9053
9054 repr = pointed_type_repr;
9055 if (repr.empty())
9056 break;
9057
9058 if (tag == DW_TAG_pointer_type)
9059 repr += "*";
9060 else if (tag == DW_TAG_reference_type)
9061 repr += "&";
9062 else if (tag == DW_TAG_rvalue_reference_type)
9063 repr += "&&";
9064 else
9065 ABG_ASSERT_NOT_REACHED;
9066 }
9067 break;
9068
9069 case DW_TAG_subrange_type:
9070 {
9071 // In Ada, this one can be generated on its own, that is, not
9072 // as a sub-type of an array. So we need to support it on its
9073 // own. Note that when it's emitted as the sub-type of an
9074 // array like in C and C++, this is handled differently, for
9075 // now. But we try to make this usable by other languages
9076 // that are not Ada, even if we modelled it after Ada.
9077
9078 // So we build a subrange type for the sole purpose of using
9079 // the ::as_string() method of that type. So we don't add
9080 // that type to the current type tree being built.
9081 array_type_def::subrange_sptr s =
9082 build_subrange_type(const_cast<reader&>(rdr),
9083 die, where_offset,
9084 /*associate_die_to_type=*/false);
9085 repr += s->as_string();
9086 break;
9087 }
9088
9089 case DW_TAG_array_type:
9090 {
9091 Dwarf_Die element_type_die;
9092 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9093 break;
9094 string element_type_name =
9095 die_qualified_type_name(rdr, &element_type_die, where_offset);
9096 if (element_type_name.empty())
9097 break;
9098
9099 array_type_def::subranges_type subranges;
9100 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9101 die, subranges, where_offset,
9102 /*associate_type_to_die=*/false);
9103
9104 repr = element_type_name;
9105 repr += array_type_def::subrange_type::vector_as_string(subranges);
9106 }
9107 break;
9108
9109 case DW_TAG_subroutine_type:
9110 case DW_TAG_subprogram:
9111 {
9112 string return_type_name;
9113 string class_name;
9114 vector<string> parm_names;
9115 bool is_const = false;
9116 bool is_static = false;
9117
9118 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9119 /*pretty_print=*/true,
9120 return_type_name, class_name,
9121 parm_names, is_const,
9122 is_static);
9123 if (return_type_name.empty())
9124 return_type_name = "void";
9125
9126 repr = return_type_name;
9127
9128 if (!class_name.empty())
9129 {
9130 // This is a method, so print the class name.
9131 repr += " (" + class_name + "::*)";
9132 }
9133
9134 // Now parameters.
9135 repr += " (";
9136 for (vector<string>::const_iterator i = parm_names.begin();
9137 i != parm_names.end();
9138 ++i)
9139 {
9140 if (i != parm_names.begin())
9141 repr += ", ";
9142 repr += *i;
9143 }
9144 repr += ")";
9145
9146 }
9147 break;
9148
9149 case DW_TAG_string_type:
9150 case DW_TAG_ptr_to_member_type:
9151 case DW_TAG_set_type:
9152 case DW_TAG_file_type:
9153 case DW_TAG_packed_type:
9154 case DW_TAG_thrown_type:
9155 case DW_TAG_interface_type:
9156 case DW_TAG_shared_type:
9157 break;
9158 }
9159
9160 return repr;
9161 }
9162
9163 /// Compute the qualified name of a decl represented by a given DIE.
9164 ///
9165 /// For instance, for a DIE of tag DW_TAG_subprogram this function
9166 /// computes the signature of the function *declaration*.
9167 ///
9168 /// @param rdr the DWARF reader.
9169 ///
9170 /// @param die the DIE to consider.
9171 ///
9172 /// @param where_offset where we are logically at in the DIE stream.
9173 ///
9174 /// @return a copy of the computed name.
9175 static string
die_qualified_decl_name(const reader & rdr,const Dwarf_Die * die,size_t where_offset)9176 die_qualified_decl_name(const reader& rdr,
9177 const Dwarf_Die* die,
9178 size_t where_offset)
9179 {
9180 if (!die || !die_is_decl(die))
9181 return "";
9182
9183 string name = die_name(die);
9184
9185 Dwarf_Die scope_die;
9186 if (!get_scope_die(rdr, die, where_offset, scope_die))
9187 return "";
9188
9189 string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9190 string separator = "::";
9191
9192 string repr;
9193
9194 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9195 switch (tag)
9196 {
9197 case DW_TAG_namespace:
9198 case DW_TAG_member:
9199 case DW_TAG_variable:
9200 repr = scope_name.empty() ? name : scope_name + separator + name;
9201 break;
9202 case DW_TAG_subprogram:
9203 repr = die_function_signature(rdr, die, where_offset);
9204 break;
9205
9206 case DW_TAG_unspecified_parameters:
9207 repr = "...";
9208 break;
9209
9210 case DW_TAG_formal_parameter:
9211 case DW_TAG_imported_declaration:
9212 case DW_TAG_GNU_template_template_param:
9213 case DW_TAG_GNU_template_parameter_pack:
9214 case DW_TAG_GNU_formal_parameter_pack:
9215 break;
9216 }
9217 return repr;
9218 }
9219
9220 /// Compute the qualified name of the artifact represented by a given
9221 /// DIE.
9222 ///
9223 /// If the DIE represents a type, then the function computes the name
9224 /// of the type. Otherwise, if the DIE represents a decl then the
9225 /// function computes the name of the decl. Note that a DIE of tag
9226 /// DW_TAG_subprogram is going to be considered as a "type" -- just
9227 /// like if it was a DW_TAG_subroutine_type.
9228 ///
9229 /// @param rdr the DWARF reader.
9230 ///
9231 /// @param die the DIE to consider.
9232 ///
9233 /// @param where_offset where we are logically at in the DIE stream.
9234 ///
9235 /// @return a copy of the computed name.
9236 static string
die_qualified_name(const reader & rdr,const Dwarf_Die * die,size_t where)9237 die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9238 {
9239 if (die_is_type(die))
9240 return die_qualified_type_name(rdr, die, where);
9241 else if (die_is_decl(die))
9242 return die_qualified_decl_name(rdr, die, where);
9243 return "";
9244 }
9245
9246 /// Test if the qualified name of a given type should be empty.
9247 ///
9248 /// The reason why the name of a DIE with a given tag would be empty
9249 /// is that libabigail's internal representation doesn't yet support
9250 /// that tag; or if the DIE's qualified name is built from names of
9251 /// sub-types DIEs whose tags are not yet supported.
9252 ///
9253 /// @param rdr the DWARF reader.
9254 ///
9255 /// @param die the DIE to consider.
9256 ///
9257 /// @param where where we are logically at, in the DIE stream.
9258 ///
9259 /// @param qualified_name the qualified name of the DIE. This is set
9260 /// only iff the function returns false.
9261 ///
9262 /// @return true if the qualified name of the DIE is empty.
9263 static bool
die_qualified_type_name_empty(const reader & rdr,const Dwarf_Die * die,size_t where,string & qualified_name)9264 die_qualified_type_name_empty(const reader& rdr,
9265 const Dwarf_Die* die,
9266 size_t where, string &qualified_name)
9267 {
9268 if (!die)
9269 return true;
9270
9271 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9272
9273 string qname;
9274 if (tag == DW_TAG_typedef
9275 || tag == DW_TAG_pointer_type
9276 || tag == DW_TAG_reference_type
9277 || tag == DW_TAG_rvalue_reference_type
9278 || tag == DW_TAG_array_type
9279 || tag == DW_TAG_const_type
9280 || tag == DW_TAG_volatile_type
9281 || tag == DW_TAG_restrict_type)
9282 {
9283 Dwarf_Die underlying_type_die;
9284 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9285 {
9286 string name =
9287 die_qualified_type_name(rdr, &underlying_type_die, where);
9288 if (name.empty())
9289 return true;
9290 }
9291 }
9292 else
9293 {
9294 string name = die_qualified_type_name(rdr, die, where);
9295 if (name.empty())
9296 return true;
9297 }
9298
9299 qname = die_qualified_type_name(rdr, die, where);
9300 if (qname.empty())
9301 return true;
9302
9303 qualified_name = qname;
9304 return false;
9305 }
9306
9307 /// Given the DIE that represents a function type, compute the names
9308 /// of the following properties the function's type:
9309 ///
9310 /// - return type
9311 /// - enclosing class (if the function is a member function)
9312 /// - function parameter types
9313 ///
9314 /// When the function we are looking at is a member function, it also
9315 /// tells if it's const.
9316 ///
9317 /// @param rdr the DWARF reader.
9318 ///
9319 /// @param die the DIE of the function or function type we are looking
9320 /// at.
9321 ///
9322 /// @param where_offset where we are logically at in the DIE stream.
9323 ///
9324 /// @param pretty_print if set to yes, the type names are going to be
9325 /// pretty-printed names; otherwise, they are just qualified type
9326 /// names.
9327 ///
9328 /// @param return_type_name out parameter. This contains the name of
9329 /// the return type of the function.
9330 ///
9331 /// @param class_name out parameter. If the function is a member
9332 /// function, this contains the name of the enclosing class.
9333 ///
9334 /// @param parm_names out parameter. This vector is set to the names
9335 /// of the types of the parameters of the function.
9336 ///
9337 /// @param is_const out parameter. If the function is a member
9338 /// function, this is set to true iff the member function is const.
9339 ///
9340 /// @param is_static out parameter. If the function is a static
9341 /// member function, then this is set to true.
9342 static void
die_return_and_parm_names_from_fn_type_die(const reader & rdr,const Dwarf_Die * die,size_t where_offset,bool pretty_print,string & return_type_name,string & class_name,vector<string> & parm_names,bool & is_const,bool & is_static)9343 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9344 const Dwarf_Die* die,
9345 size_t where_offset,
9346 bool pretty_print,
9347 string &return_type_name,
9348 string &class_name,
9349 vector<string>& parm_names,
9350 bool& is_const,
9351 bool& is_static)
9352 {
9353 Dwarf_Die child;
9354 Dwarf_Die ret_type_die;
9355 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9356 return_type_name = "void";
9357 else
9358 return_type_name =
9359 pretty_print
9360 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9361 : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9362
9363 if (return_type_name.empty())
9364 return_type_name = "void";
9365
9366 Dwarf_Die object_pointer_die, class_die;
9367 bool is_method_type =
9368 die_function_type_is_method_type(rdr, die, where_offset,
9369 object_pointer_die,
9370 class_die, is_static);
9371
9372 is_const = false;
9373 if (is_method_type)
9374 {
9375 class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9376
9377 Dwarf_Die this_pointer_die;
9378 Dwarf_Die pointed_to_die;
9379 if (!is_static
9380 && die_die_attribute(&object_pointer_die, DW_AT_type,
9381 this_pointer_die))
9382 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9383 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9384 is_const = true;
9385
9386 string fn_name = die_name(die);
9387 string non_qualified_class_name = die_name(&class_die);
9388 bool is_ctor = fn_name == non_qualified_class_name;
9389 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9390
9391 if (is_ctor || is_dtor)
9392 return_type_name.clear();
9393 }
9394
9395 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9396 do
9397 {
9398 int child_tag = dwarf_tag(&child);
9399 if (child_tag == DW_TAG_formal_parameter)
9400 {
9401 Dwarf_Die parm_type_die;
9402 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9403 continue;
9404 string qualified_name =
9405 pretty_print
9406 ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9407 : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9408
9409 if (qualified_name.empty())
9410 continue;
9411 parm_names.push_back(qualified_name);
9412 }
9413 else if (child_tag == DW_TAG_unspecified_parameters)
9414 {
9415 // This is a variadic function parameter.
9416 parm_names.push_back("variadic parameter type");
9417 // After a DW_TAG_unspecified_parameters tag, we shouldn't
9418 // keep reading for parameters. The
9419 // unspecified_parameters TAG should be the last parameter
9420 // that we record. For instance, if there are multiple
9421 // DW_TAG_unspecified_parameters DIEs then we should care
9422 // only for the first one.
9423 break;
9424 }
9425 }
9426 while (dwarf_siblingof(&child, &child) == 0);
9427
9428 if (class_name.empty())
9429 {
9430 Dwarf_Die parent_die;
9431 if (get_parent_die(rdr, die, parent_die, where_offset))
9432 {
9433 if (die_is_class_type(&parent_die))
9434 class_name =
9435 rdr.get_die_qualified_type_name(&parent_die, where_offset);
9436 }
9437 }
9438 }
9439
9440 /// This computes the signature of the a function declaration
9441 /// represented by a DIE.
9442 ///
9443 /// @param rdr the DWARF reader.
9444 ///
9445 /// @param fn_die the DIE of the function to consider.
9446 ///
9447 /// @param where_offset where we are logically at in the stream of
9448 /// DIEs.
9449 ///
9450 /// @return a copy of the computed function signature string.
9451 static string
die_function_signature(const reader & rdr,const Dwarf_Die * fn_die,size_t where_offset)9452 die_function_signature(const reader& rdr,
9453 const Dwarf_Die *fn_die,
9454 size_t where_offset)
9455 {
9456
9457 translation_unit::language lang;
9458 bool has_lang = false;
9459 if ((has_lang = rdr.get_die_language(fn_die, lang)))
9460 {
9461 // In a binary originating from the C language, it's OK to use
9462 // the linkage name of the function as a key for the map which
9463 // is meant to reduce the number of DIE comparisons involved
9464 // during DIE canonicalization computation.
9465 if (is_c_language(lang))
9466 {
9467 string fn_name = die_linkage_name(fn_die);
9468 if (fn_name.empty())
9469 fn_name = die_name(fn_die);
9470 return fn_name;
9471 }
9472 }
9473
9474 // TODO: When we can structurally compare DIEs originating from C++
9475 // as well, we can use the linkage name of functions in C++ too, to
9476 // reduce the number of comparisons involved during DIE
9477 // canonicalization.
9478
9479 string return_type_name;
9480 Dwarf_Die ret_type_die;
9481 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9482 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9483 where_offset);
9484
9485 if (return_type_name.empty())
9486 return_type_name = "void";
9487
9488 Dwarf_Die scope_die;
9489 string scope_name;
9490 if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9491 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9492 string fn_name = die_name(fn_die);
9493 if (!scope_name.empty())
9494 fn_name = scope_name + "::" + fn_name;
9495
9496 string class_name;
9497 vector<string> parm_names;
9498 bool is_const = false;
9499 bool is_static = false;
9500
9501 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9502 /*pretty_print=*/false,
9503 return_type_name, class_name,
9504 parm_names, is_const, is_static);
9505
9506 bool is_virtual = die_is_virtual(fn_die);
9507
9508 string repr = class_name.empty() ? "function" : "method";
9509 if (is_virtual)
9510 repr += " virtual";
9511
9512 if (!return_type_name.empty())
9513 repr += " " + return_type_name;
9514
9515 repr += " " + fn_name;
9516
9517 // Now parameters.
9518 repr += "(";
9519 bool some_parm_emitted = false;
9520 for (vector<string>::const_iterator i = parm_names.begin();
9521 i != parm_names.end();
9522 ++i)
9523 {
9524 if (i != parm_names.begin())
9525 {
9526 if (some_parm_emitted)
9527 repr += ", ";
9528 }
9529 else
9530 if (!is_static && !class_name.empty())
9531 // We are printing a non-static method name, skip the implicit "this"
9532 // parameter type.
9533 continue;
9534 repr += *i;
9535 some_parm_emitted = true;
9536 }
9537 repr += ")";
9538
9539 if (is_const)
9540 {
9541 ABG_ASSERT(!class_name.empty());
9542 repr += " const";
9543 }
9544
9545 return repr;
9546 }
9547
9548 /// Return a pretty string representation of a type, for internal purposes.
9549 ///
9550 /// By internal purpose, we mean things like key-ing types for lookup
9551 /// purposes and so on.
9552 ///
9553 /// Note that this function is also used to pretty print functions.
9554 /// For functions, it prints the *type* of the function.
9555 ///
9556 /// @param rdr the context to use.
9557 ///
9558 /// @param the DIE of the type to pretty print.
9559 ///
9560 /// @param where_offset where we logically are placed when calling
9561 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9562 /// entries.
9563 ///
9564 /// @return the resulting pretty representation.
9565 static string
die_pretty_print_type(reader & rdr,const Dwarf_Die * die,size_t where_offset)9566 die_pretty_print_type(reader& rdr,
9567 const Dwarf_Die* die,
9568 size_t where_offset)
9569 {
9570 if (!die
9571 || (!die_is_type(die)
9572 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9573 return "";
9574
9575 string repr;
9576
9577 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9578 switch (tag)
9579 {
9580 case DW_TAG_string_type:
9581 // For now, we won't try to go get the actual representation of
9582 // the string because this would make things more complicated;
9583 // for that we'd need to interpret some location expressions to
9584 // get the length of the string. And for dynamically allocated
9585 // strings, the result of the location expression evaluation
9586 // might not even be a constant. So at the moment I consider
9587 // this to be a lot of hassle for no great return. Until proven
9588 // otherwise, of course.
9589 repr = "string type";
9590
9591 case DW_TAG_unspecified_type:
9592 case DW_TAG_ptr_to_member_type:
9593 break;
9594
9595 case DW_TAG_namespace:
9596 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9597 break;
9598
9599 case DW_TAG_base_type:
9600 repr = rdr.get_die_qualified_type_name(die, where_offset);
9601 break;
9602
9603 case DW_TAG_typedef:
9604 {
9605 string qualified_name;
9606 if (!die_qualified_type_name_empty(rdr, die,
9607 where_offset,
9608 qualified_name))
9609 repr = "typedef " + qualified_name;
9610 }
9611 break;
9612
9613 case DW_TAG_const_type:
9614 case DW_TAG_volatile_type:
9615 case DW_TAG_restrict_type:
9616 case DW_TAG_pointer_type:
9617 case DW_TAG_reference_type:
9618 case DW_TAG_rvalue_reference_type:
9619 repr = rdr.get_die_qualified_type_name(die, where_offset);
9620 break;
9621
9622 case DW_TAG_enumeration_type:
9623 {
9624 string qualified_name =
9625 rdr.get_die_qualified_type_name(die, where_offset);
9626 repr = "enum " + qualified_name;
9627 }
9628 break;
9629
9630 case DW_TAG_structure_type:
9631 case DW_TAG_class_type:
9632 {
9633 string qualified_name =
9634 rdr.get_die_qualified_type_name(die, where_offset);
9635 repr = "class " + qualified_name;
9636 }
9637 break;
9638
9639 case DW_TAG_union_type:
9640 {
9641 string qualified_name =
9642 rdr.get_die_qualified_type_name(die, where_offset);
9643 repr = "union " + qualified_name;
9644 }
9645 break;
9646
9647 case DW_TAG_array_type:
9648 {
9649 Dwarf_Die element_type_die;
9650 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9651 break;
9652 string element_type_name =
9653 rdr.get_die_qualified_type_name(&element_type_die, where_offset);
9654 if (element_type_name.empty())
9655 break;
9656
9657 array_type_def::subranges_type subranges;
9658 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
9659 /*associate_type_to_die=*/false);
9660
9661 repr = element_type_name;
9662 repr += array_type_def::subrange_type::vector_as_string(subranges);
9663 }
9664 break;
9665
9666 case DW_TAG_subrange_type:
9667 {
9668 // So this can be generated by Ada, on its own; that is, not
9669 // as a subtype of an array. In that case we need to handle
9670 // it properly.
9671
9672 // For now, we consider that the pretty printed name of the
9673 // subrange type is its name. We might need something more
9674 // advance, should the needs of the users get more
9675 // complicated.
9676 repr += die_qualified_type_name(rdr, die, where_offset);
9677 }
9678 break;
9679
9680 case DW_TAG_subroutine_type:
9681 case DW_TAG_subprogram:
9682 {
9683 string return_type_name;
9684 string class_name;
9685 vector<string> parm_names;
9686 bool is_const = false;
9687 bool is_static = false;
9688
9689 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9690 /*pretty_print=*/true,
9691 return_type_name, class_name,
9692 parm_names, is_const,
9693 is_static);
9694 if (class_name.empty())
9695 repr = "function type";
9696 else
9697 repr = "method type";
9698 repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
9699 }
9700 break;
9701
9702 case DW_TAG_set_type:
9703 case DW_TAG_file_type:
9704 case DW_TAG_packed_type:
9705 case DW_TAG_thrown_type:
9706 case DW_TAG_interface_type:
9707 case DW_TAG_shared_type:
9708 ABG_ASSERT_NOT_REACHED;
9709 }
9710
9711 return repr;
9712 }
9713
9714 /// Return a pretty string representation of a declaration, for
9715 /// internal purposes.
9716 ///
9717 /// By internal purpose, we mean things like key-ing declarations for
9718 /// lookup purposes and so on.
9719 ///
9720 /// Note that this function is also used to pretty print functions.
9721 /// For functions, it prints the signature of the function.
9722 ///
9723 /// @param rdr the context to use.
9724 ///
9725 /// @param the DIE of the declaration to pretty print.
9726 ///
9727 /// @param where_offset where we logically are placed when calling
9728 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9729 /// entries.
9730 ///
9731 /// @return the resulting pretty representation.
9732 static string
die_pretty_print_decl(reader & rdr,const Dwarf_Die * die,size_t where_offset)9733 die_pretty_print_decl(reader& rdr,
9734 const Dwarf_Die* die,
9735 size_t where_offset)
9736 {
9737 if (!die || !die_is_decl(die))
9738 return "";
9739
9740 string repr;
9741
9742 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9743 switch (tag)
9744 {
9745 case DW_TAG_namespace:
9746 repr = "namespace " + die_qualified_name(rdr, die, where_offset);
9747 break;
9748
9749 case DW_TAG_member:
9750 case DW_TAG_variable:
9751 {
9752 string type_repr = "void";
9753 Dwarf_Die type_die;
9754 if (die_die_attribute(die, DW_AT_type, type_die))
9755 type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
9756 repr = die_qualified_name(rdr, die, where_offset);
9757 if (!repr.empty())
9758 repr = type_repr + " " + repr;
9759 }
9760 break;
9761
9762 case DW_TAG_subprogram:
9763 repr = die_function_signature(rdr, die, where_offset);
9764 break;
9765
9766 default:
9767 break;
9768 }
9769 return repr;
9770 }
9771
9772 /// Compute the pretty printed representation of an artifact
9773 /// represented by a DIE.
9774 ///
9775 /// If the DIE is a type, compute the its pretty representation as a
9776 /// type; otherwise, if it's a declaration, compute its pretty
9777 /// representation as a declaration. Note for For instance, that a
9778 /// DW_TAG_subprogram DIE is going to be represented as a function
9779 /// *type*.
9780 ///
9781 /// @param rdr the DWARF reader.
9782 ///
9783 /// @param die the DIE to consider.
9784 ///
9785 /// @param where_offset we in the DIE stream we are logically at.
9786 ///
9787 /// @return a copy of the pretty printed artifact.
9788 static string
die_pretty_print(reader & rdr,const Dwarf_Die * die,size_t where_offset)9789 die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
9790 {
9791 if (die_is_type(die))
9792 return die_pretty_print_type(rdr, die, where_offset);
9793 else if (die_is_decl(die))
9794 return die_pretty_print_decl(rdr, die, where_offset);
9795 return "";
9796 }
9797
9798 // -----------------------------------
9799 // </die pretty printer>
9800 // -----------------------------------
9801
9802
9803 // ----------------------------------
9804 // <die comparison engine>
9805 // ---------------------------------
9806
9807 /// Compares two decls DIEs
9808 ///
9809 /// This works only for DIEs emitted by the C language.
9810 ///
9811 /// This implementation doesn't yet support namespaces.
9812 ///
9813 /// This is a subroutine of compare_dies.
9814 ///
9815 /// @return true iff @p l equals @p r.
9816 static bool
compare_as_decl_dies(const Dwarf_Die * l,const Dwarf_Die * r)9817 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
9818 {
9819 ABG_ASSERT(l && r);
9820
9821 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
9822 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
9823 if (l_tag != r_tag)
9824 return false;
9825
9826 bool result = false;
9827
9828 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
9829 {
9830 // Fast path for functions and global variables.
9831 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
9832 result)
9833 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
9834 result))
9835 {
9836 if (!result)
9837 return false;
9838 }
9839
9840 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
9841 result))
9842 {
9843 if (!result)
9844 return false;
9845 }
9846 return true;
9847 }
9848
9849 // Fast path for types.
9850 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
9851 result))
9852 return result;
9853 return true;
9854 }
9855
9856 /// Test if at least one of two ODR-relevant DIEs is decl-only.
9857 ///
9858 /// @param rdr the DWARF reader to consider.
9859 ///
9860 /// @param l the first type DIE to consider.
9861 ///
9862 /// @param r the second type DIE to consider.
9863 ///
9864 /// @return true iff either @p l or @p r is decl-only and both are
9865 /// ODR-relevant.
9866 static bool
at_least_one_decl_only_among_odr_relevant_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)9867 at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
9868 const Dwarf_Die *l,
9869 const Dwarf_Die *r)
9870 {
9871 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
9872 return false;
9873
9874 if ((die_is_declaration_only(l) && die_has_no_child(l))
9875 || (die_is_declaration_only(r) && die_has_no_child(r)))
9876 return true;
9877 return false;
9878 }
9879
9880 /// Compares two type DIEs
9881 ///
9882 /// This is a subroutine of compare_dies.
9883 ///
9884 /// Note that this function doesn't look at the name of the DIEs.
9885 /// Naming is taken into account by the function compare_as_decl_dies.
9886 ///
9887 /// If the two DIEs are from a translation unit that is subject to the
9888 /// ONE Definition Rule, then the function considers that if one DIE
9889 /// is a declaration, then it's equivalent to the second. In that
9890 /// case, the sizes of the two DIEs are not compared. This is so that
9891 /// a declaration of a type compares equal to the definition of the
9892 /// type.
9893 ///
9894 /// @param rdr the DWARF reader to consider.
9895 ///
9896 /// @param l the left operand of the comparison operator.
9897 ///
9898 /// @param r the right operand of the comparison operator.
9899 ///
9900 /// @return true iff @p l equals @p r.
9901 static bool
compare_as_type_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)9902 compare_as_type_dies(const reader& rdr,
9903 const Dwarf_Die *l,
9904 const Dwarf_Die *r)
9905 {
9906 ABG_ASSERT(l && r);
9907 ABG_ASSERT(die_is_type(l));
9908 ABG_ASSERT(die_is_type(r));
9909
9910 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
9911 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
9912 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
9913 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
9914 // For now, we cannot compare DW_TAG_string_type because of its
9915 // string_length attribute that is a location descriptor that is
9916 // not necessarily a constant. So it's super hard to evaluate it
9917 // in a libabigail context. So for now, we just say that all
9918 // DW_TAG_string_type DIEs are different, by default.
9919 return false;
9920
9921 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
9922 // A declaration of a type compares equal to the definition of the
9923 // type.
9924 return true;
9925
9926 uint64_t l_size = 0, r_size = 0;
9927 die_size_in_bits(l, l_size);
9928 die_size_in_bits(r, r_size);
9929
9930 return l_size == r_size;
9931 }
9932
9933 /// Compare two DIEs as decls (looking as their names etc) and as
9934 /// types (looking at their size etc).
9935 ///
9936 /// @param rdr the DWARF reader to consider.
9937 ///
9938 /// @param l the first DIE to consider.
9939 ///
9940 /// @param r the second DIE to consider.
9941 ///
9942 /// @return TRUE iff @p l equals @p r as far as naming and size is
9943 /// concerned.
9944 static bool
compare_as_decl_and_type_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)9945 compare_as_decl_and_type_dies(const reader &rdr,
9946 const Dwarf_Die *l,
9947 const Dwarf_Die *r)
9948 {
9949 if (!compare_as_decl_dies(l, r)
9950 || !compare_as_type_dies(rdr, l, r))
9951 return false;
9952
9953 return true;
9954 }
9955
9956 /// Test if two DIEs representing function declarations have the same
9957 /// linkage name, and thus are considered equal if they are C or C++,
9958 /// because the two DIEs represent functions in the same binary.
9959 ///
9960 /// If the DIEs don't have a linkage name, the function compares their
9961 /// name. But in that case, the caller of the function must know that
9962 /// in C++ for instance, that doesn't imply that the two functions are
9963 /// equal.
9964 ///
9965 /// @param rdr the @ref reader to consider.
9966 ///
9967 /// @param l the first function DIE to consider.
9968 ///
9969 /// @param r the second function DIE to consider.
9970 ///
9971 /// @return true iff the function represented by @p l have the same
9972 /// linkage name as the function represented by @p r.
9973 static bool
fn_die_equal_by_linkage_name(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)9974 fn_die_equal_by_linkage_name(const reader &rdr,
9975 const Dwarf_Die *l,
9976 const Dwarf_Die *r)
9977 {
9978 if (!!l != !!r)
9979 return false;
9980
9981 if (!l)
9982 return false;
9983
9984 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
9985 ABG_ASSERT(tag == DW_TAG_subprogram);
9986 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
9987 ABG_ASSERT(tag == DW_TAG_subprogram);
9988
9989 string lname = die_name(l), rname = die_name(r);
9990 string llinkage_name = die_linkage_name(l),
9991 rlinkage_name = die_linkage_name(r);
9992
9993 if (rdr.die_is_in_c_or_cplusplus(l)
9994 && rdr.die_is_in_c_or_cplusplus(r))
9995 {
9996 if (!llinkage_name.empty() && !rlinkage_name.empty())
9997 return llinkage_name == rlinkage_name;
9998 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
9999 return false;
10000 else
10001 return lname == rname;
10002 }
10003
10004 return (!llinkage_name.empty()
10005 && !rlinkage_name.empty()
10006 && llinkage_name == rlinkage_name);
10007 }
10008
10009 /// Compare two DIEs in the context of DIE canonicalization.
10010 ///
10011 /// If DIE canonicalization is on, the function compares the DIEs
10012 /// canonically and structurally. The two types of comparison should
10013 /// be equal, of course.
10014 ///
10015 /// @param rdr the DWARF reader.
10016 ///
10017 /// @param l_offset the offset of the first canonical DIE to compare.
10018 ///
10019 /// @param r_offset the offset of the second canonical DIE to compare.
10020 ///
10021 /// @param l_die_source the source of the DIE denoted by the offset @p
10022 /// l_offset.
10023 ///
10024 /// @param r_die_source the source of the DIE denoted by the offset @p
10025 /// r_offset.
10026 ///
10027 /// @param l_has_canonical_die_offset output parameter. Is set to
10028 /// true if @p l_offset has a canonical DIE.
10029 ///
10030 /// @param r_has_canonical_die_offset output parameter. Is set to
10031 /// true if @p r_offset has a canonical DIE.
10032 ///
10033 /// @param l_canonical_die_offset output parameter. If @p
10034 /// l_has_canonical_die_offset is set to true, then this parameter is
10035 /// set to the offset of the canonical DIE of the DIE designated by @p
10036 /// l_offset.
10037 static bool
try_canonical_die_comparison(const reader & rdr,Dwarf_Off l_offset,Dwarf_Off r_offset,die_source l_die_source,die_source r_die_source,bool & l_has_canonical_die_offset,bool & r_has_canonical_die_offset,Dwarf_Off & l_canonical_die_offset,Dwarf_Off & r_canonical_die_offset,bool & result)10038 try_canonical_die_comparison(const reader& rdr,
10039 Dwarf_Off l_offset, Dwarf_Off r_offset,
10040 die_source l_die_source, die_source r_die_source,
10041 bool& l_has_canonical_die_offset,
10042 bool& r_has_canonical_die_offset,
10043 Dwarf_Off& l_canonical_die_offset,
10044 Dwarf_Off& r_canonical_die_offset,
10045 bool& result)
10046 {
10047 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10048 if (rdr.debug_die_canonicalization_is_on_
10049 && !rdr.use_canonical_die_comparison_)
10050 return false;
10051 #endif
10052
10053
10054 l_has_canonical_die_offset =
10055 (l_canonical_die_offset =
10056 rdr.get_canonical_die_offset(l_offset, l_die_source,
10057 /*die_as_type=*/true));
10058
10059 r_has_canonical_die_offset =
10060 (r_canonical_die_offset =
10061 rdr.get_canonical_die_offset(r_offset, r_die_source,
10062 /*die_as_type=*/true));
10063
10064 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10065 {
10066 result = (l_canonical_die_offset == r_canonical_die_offset);
10067 return true;
10068 }
10069
10070 return false;
10071 }
10072
10073 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10074 /// This function is called whenever a DIE comparison fails.
10075 ///
10076 /// This function is intended for debugging purposes. The idea is for
10077 /// hackers to set a breakpoint on this function so that they can
10078 /// discover why exactly the comparison failed. They then can execute
10079 /// the program from compare_dies_during_canonicalization, for
10080 /// instance.
10081 ///
10082 /// @param @l the left-hand side of the DIE comparison.
10083 ///
10084 /// @param @r the right-hand side of the DIE comparison.
10085 static void
notify_die_comparison_failed(const Dwarf_Die *,const Dwarf_Die *)10086 notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10087 {
10088 }
10089
10090 #define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10091 notify_die_comparison_failed(l, r)
10092 #else
10093 #define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10094 #endif
10095
10096 /// A macro used to return from DIE comparison routines.
10097 ///
10098 /// If the return value is false, the macro invokes the
10099 /// notify_die_comparison_failed signalling function before returning.
10100 /// That way, hackers willing to learn more about why the comparison
10101 /// routine returned "false" can just set a breakpoint on
10102 /// notify_die_comparison_failed and execute the program from
10103 /// compare_dies_during_canonicalization, for instance.
10104 ///
10105 /// @param value the value to return from the DIE comparison routines.
10106 #define ABG_RETURN(value) \
10107 do \
10108 { \
10109 if ((value) == COMPARISON_RESULT_DIFFERENT) \
10110 { \
10111 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10112 } \
10113 return return_comparison_result(l, r, dies_being_compared, \
10114 value, aggregates_being_compared, \
10115 update_canonical_dies_on_the_fly); \
10116 } \
10117 while(false)
10118
10119 /// A macro used to return the "false" boolean from DIE comparison
10120 /// routines.
10121 ///
10122 /// As the return value is false, the macro invokes the
10123 /// notify_die_comparison_failed signalling function before returning.
10124 ///
10125 /// @param value the value to return from the DIE comparison routines.
10126 #define ABG_RETURN_FALSE \
10127 do \
10128 { \
10129 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10130 return return_comparison_result(l, r, dies_being_compared, \
10131 COMPARISON_RESULT_DIFFERENT, \
10132 aggregates_being_compared, \
10133 update_canonical_dies_on_the_fly); \
10134 } while(false)
10135
10136 /// A macro to set the 'result' variable to 'false'.
10137 ///
10138 /// The macro invokes the notify_die_comparison_failed function so
10139 /// that the hacker can set a debugging breakpoint on
10140 /// notify_die_comparison_failed to know where a DIE comparison failed
10141 /// during compare_dies_during_canonicalization for instance.
10142 ///
10143 /// @param result the 'result' variable to set.
10144 ///
10145 /// @param l the first DIE of the comparison operation.
10146 ///
10147 /// @param r the second DIE of the comparison operation.
10148 #define SET_RESULT_TO_FALSE(result, l , r) \
10149 do \
10150 { \
10151 result = COMPARISON_RESULT_DIFFERENT; \
10152 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10153 } while(false)
10154
10155 /// A macro to set the 'result' variable to a given value.
10156 ///
10157 /// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10158 /// invokes the notify_die_comparison_failed function so that the
10159 /// hacker can set a debugging breakpoint on
10160 /// notify_die_comparison_failed to know where a DIE comparison failed
10161 /// during compare_dies_during_canonicalization for instance.
10162 ///
10163 /// @param result the 'result' variable to set.
10164 ///
10165 /// @param l the first DIE of the comparison operation.
10166 ///
10167 /// @param r the second DIE of the comparison operation.
10168 #define SET_RESULT_TO(result, value, l , r) \
10169 do \
10170 { \
10171 result = (value); \
10172 if (result == COMPARISON_RESULT_DIFFERENT) \
10173 { \
10174 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10175 } \
10176 } while(false)
10177
10178 #define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10179 do \
10180 { \
10181 if (aggregates_being_compared.contains(dies_being_compared)) \
10182 { \
10183 result = COMPARISON_RESULT_CYCLE_DETECTED; \
10184 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10185 ABG_RETURN(result); \
10186 } \
10187 } \
10188 while(false)
10189
10190 /// Get the next member sibling of a given class or union member DIE.
10191 ///
10192 /// @param die the DIE to consider.
10193 ///
10194 /// @param member out parameter. This is set to the next member
10195 /// sibling, iff the function returns TRUE.
10196 ///
10197 /// @return TRUE iff the function set @p member to the next member
10198 /// sibling DIE.
10199 static bool
get_next_member_sibling_die(const Dwarf_Die * die,Dwarf_Die * member)10200 get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10201 {
10202 if (!die)
10203 return false;
10204
10205 bool found_member = false;
10206 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10207 member) == 0);
10208 found_member;
10209 found_member = (dwarf_siblingof(member, member) == 0))
10210 {
10211 int tag = dwarf_tag(member);
10212 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10213 break;
10214 }
10215
10216 return found_member;
10217 }
10218
10219 /// Get the first child DIE of a class/struct/union DIE that is a
10220 /// member DIE.
10221 ///
10222 /// @param die the DIE to consider.
10223 ///
10224 /// @param child out parameter. This is set to the first child DIE of
10225 /// @p iff this function returns TRUE.
10226 ///
10227 /// @return TRUE iff @p child is set to the first child DIE of @p die
10228 /// that is a member DIE.
10229 static bool
get_member_child_die(const Dwarf_Die * die,Dwarf_Die * child)10230 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10231 {
10232 if (!die)
10233 return false;
10234
10235 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10236 ABG_ASSERT(tag == DW_TAG_structure_type
10237 || tag == DW_TAG_union_type
10238 || tag == DW_TAG_class_type);
10239
10240 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10241 child) == 0);
10242
10243 if (!found_child)
10244 return false;
10245
10246 tag = dwarf_tag(child);
10247
10248 if (!(tag == DW_TAG_member
10249 || tag == DW_TAG_inheritance
10250 || tag == DW_TAG_subprogram))
10251 found_child = get_next_member_sibling_die(child, child);
10252
10253 return found_child;
10254 }
10255
10256 /// This is a sub-routine of return_comparison_result.
10257 ///
10258 /// Propagate the canonical type of a the right-hand-side DIE to the
10259 /// lef-hand-side DIE. This is a optimization that is done when the
10260 /// two DIEs compare equal.
10261 ///
10262 /// If the right-hand-side DIE is not canonicalized, the function
10263 /// performs its canonicalization.
10264 ///
10265 /// This optimization is performed only if
10266 /// is_canon_type_to_be_propagated_tag returns true.
10267 ///
10268 /// @param rdr the current context to consider.
10269 ///
10270 /// @param l the left-hand-side DIE of the comparison. It's going to
10271 /// receive the canonical type of the other DIE.
10272 ///
10273 /// @param r the right-hand-side DIE of the comparison. Its canonical
10274 /// type is propagated to @p l.
10275 static void
maybe_propagate_canonical_type(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10276 maybe_propagate_canonical_type(const reader& rdr,
10277 const Dwarf_Die* l,
10278 const Dwarf_Die* r)
10279 {
10280 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10281 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10282
10283 if (l_tag != r_tag)
10284 return;
10285
10286 if (is_canon_type_to_be_propagated_tag(l_tag))
10287 propagate_canonical_type(rdr, l, r);
10288 }
10289
10290 /// Propagate the canonical type of a the right-hand-side DIE to the
10291 /// left-hand-side DIE. This is a optimization that is done when the
10292 /// two DIEs compare equal.
10293 ///
10294 /// If the right-hand-side DIE is not canonicalized, the function
10295 /// performs its canonicalization.
10296 ///
10297 /// @param rdr the current context to consider.
10298 ///
10299 /// @param l the left-hand-side DIE of the comparison. It's going to
10300 /// receive the canonical type of the other DIE.
10301 ///
10302 /// @param r the right-hand-side DIE of the comparison. Its canonical
10303 /// type is propagated to @p l.
10304 static void
propagate_canonical_type(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10305 propagate_canonical_type(const reader& rdr,
10306 const Dwarf_Die* l,
10307 const Dwarf_Die* r)
10308 {
10309 ABG_ASSERT(l && r);
10310
10311 // If 'l' has no canonical DIE and if 'r' has one, then propagage
10312 // the canonical DIE of 'r' to 'l'.
10313 //
10314 // In case 'r' has no canonical DIE, then compute it, and then
10315 // propagate that canonical DIE to 'r'.
10316 const die_source l_source = rdr.get_die_source(l);
10317 const die_source r_source = rdr.get_die_source(r);
10318
10319 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10320 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10321 bool l_has_canonical_die_offset = false;
10322 bool r_has_canonical_die_offset = false;
10323 Dwarf_Off l_canonical_die_offset = 0;
10324 Dwarf_Off r_canonical_die_offset = 0;
10325
10326 l_has_canonical_die_offset =
10327 (l_canonical_die_offset =
10328 rdr.get_canonical_die_offset(l_offset, l_source,
10329 /*die_as_type=*/true));
10330
10331 r_has_canonical_die_offset =
10332 (r_canonical_die_offset =
10333 rdr.get_canonical_die_offset(r_offset, r_source,
10334 /*die_as_type=*/true));
10335
10336
10337 if (!l_has_canonical_die_offset
10338 // A DIE can be equivalent only to another DIE of the same
10339 // source.
10340 && l_source == r_source)
10341 {
10342 if (!r_has_canonical_die_offset)
10343 rdr.compute_canonical_die_offset(r, r_canonical_die_offset,
10344 /*die_as_type=*/true);
10345 ABG_ASSERT(r_canonical_die_offset);
10346 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10347 /*die_as_type=*/true);
10348 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10349 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10350 rdr.canonical_propagated_count_++;
10351 }
10352 }
10353
10354 /// This function does the book keeping of comparison pairs necessary
10355 /// to handle
10356 ///
10357 /// * the detection of cycles during the comparison of aggregate
10358 /// types, in conjuction with the macro
10359 /// RETURN_IF_COMPARISON_CYCLE_DETECTED
10360 ///
10361 /// * the handling of the canonical type propagation optimisation
10362 /// to speed-up type canonicalization.
10363 ///
10364 ///
10365 /// Note that this function is essentially a sub-routine of
10366 /// compare_dies.
10367 ///
10368 /// @param l the left-hand-side DIE being compared.
10369 ///
10370 /// @param r the right-hand-side DIE being compared.
10371 ///
10372 /// @param cur_dies the pair of die offsets of l and r. This is
10373 /// redundant as it can been computed from @p l and @p r. However,
10374 /// getting it as an argument is an optimization to avoid computing it
10375 /// over and over again, given how often this function is invoked from
10376 /// compare_dies.
10377 ///
10378 /// @param return the result of comparing @p l against @p r.
10379 ///
10380 /// @param comparison_stack the stack of pair of type DIEs being
10381 /// compared.
10382 ///
10383 /// @param do_propagate_canonical_type if true then the function
10384 /// performs canonical DIEs propagation, meaning that if @p l equals
10385 /// @p r and if @p r has a canonical type, then the canonical type of
10386 /// @p l is set to the canonical type of @p r.
10387 static comparison_result
return_comparison_result(const Dwarf_Die * l,const Dwarf_Die * r,const offset_pair_type & cur_dies,comparison_result result,offset_pairs_stack_type & comparison_stack,bool do_propagate_canonical_type=true)10388 return_comparison_result(const Dwarf_Die* l,
10389 const Dwarf_Die* r,
10390 const offset_pair_type& cur_dies,
10391 comparison_result result,
10392 offset_pairs_stack_type& comparison_stack,
10393 bool do_propagate_canonical_type = true)
10394 {
10395 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10396
10397 if (result == COMPARISON_RESULT_EQUAL)
10398 {
10399 // The result comparing the two types is "true", basically. So
10400 // let's propagate the canonical type of r onto l, so that we
10401 // don't need to compute the canonical type of r.
10402 if (do_propagate_canonical_type)
10403 {
10404 // Propagate canonical type.
10405 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10406
10407 // TODO: do we need to confirm any tentative canonical
10408 // propagation?
10409 }
10410 }
10411 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10412 {
10413 // So upon detection of the comparison cycle, compare_dies
10414 // returned early with the comparison result
10415 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10416 // carry on with the comparison of all the OTHER sub-types of
10417 // the redundant type. If they all compare equal, then it means
10418 // the redundant type pair compared equal. Otherwise, it
10419 // compared different.
10420 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10421 // Let's fall through to let the end of this function set the
10422 // result to COMPARISON_RESULT_UNKNOWN;
10423 }
10424 else if (result == COMPARISON_RESULT_UNKNOWN)
10425 {
10426 // Here is an introductory comment describing what we are going
10427 // to do in this case where the result of the comparison of the
10428 // current pair of type is not "false", basically.
10429 //
10430 // This means that we don't yet know what the result of
10431 // comparing these two types is, because one of the sub-types of
10432 // the types being compared is "redundant", meaning it appears
10433 // more than once in the comparison stack, so if we were to
10434 // naively try to carry on with the comparison member-wise, we'd
10435 // end up with an endless loop, a.k.a "comparison cycle".
10436 //
10437 // If the current type pair is redundant then:
10438 //
10439 // * This is a redundant type that has just been fully
10440 // compared. In that case, all the types that depend on
10441 // this redundant type and that have been tentatively
10442 // canonical-type-propagated must see their canonical types
10443 // "confirmed". This means that this type is going to be
10444 // considered as not being redundant anymore, meaning all
10445 // the types that depend on it must be updated as not being
10446 // dependant on it anymore, and the type itsef must be
10447 // removed from the map of redundant types.
10448 //
10449 // After the type's canonical-type-propagation is confirmed,
10450 // the result of its comparison must also be changed into
10451 // COMPARISON_RESULT_EQUAL.
10452 //
10453 // After that, If the current type depends on a redundant type,
10454 // then propagate its canonical type AND track it as having its
10455 // type being canonical-type-propagated.
10456 //
10457 // If the current type is not redundant however, then it must be
10458 // dependant on a redundant type. If it's not dependant on a
10459 // redundant type, then it must be of those types which
10460 // comparisons are not tracked for cycle, probably because they
10461 // are not aggregates. Otherwise, ABORT to understand why. I
10462 // believe this should not happen. In any case, after that
10463 // safety check is passed, we just need to return at this point.
10464
10465 if (comparison_stack.is_redundant(cur_dies)
10466 && comparison_stack.vect_.back() == cur_dies)
10467 {
10468 // We are in the case described above of a redundant type
10469 // that has been fully compared.
10470 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10471 comparison_stack.confirm_canonical_propagated_type(cur_dies);
10472
10473 result = COMPARISON_RESULT_EQUAL;
10474 }
10475 else if (is_canon_type_to_be_propagated_tag(l_tag)
10476 && comparison_stack.vect_.back() == cur_dies)
10477 {
10478 // The current type is not redundant. So, as described in
10479 // the introductory comment above, it must be dependant on a
10480 // redundant type.
10481 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10482 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10483 // Then pass through.
10484 }
10485 }
10486 else if (result == COMPARISON_RESULT_DIFFERENT)
10487 {
10488 // Here is an introductory comment describing what we are going
10489 // to do in this case where the result of the comparison of the
10490 // current pair of type is "false", basically.
10491 //
10492 // If the type pair {l,r} is redundant then cancel the
10493 // canonical-type-propagation of all the dependant pairs that
10494 // depends on this redundant {l, r}. This means walk the types
10495 // that depends on {l, r} and cancel their
10496 // canonical-propagate-type, that means remove their canonical
10497 // types and mark them as not being canonically-propagated.
10498 // Also, erase their cached comparison results that was likely
10499 // set to COMPARISON_RESULT_UNKNOWN.
10500 //
10501 // Also, update the cached result for this pair, that was likely
10502 // to be COMPARISON_RESULT_UNKNOWN.
10503 if (comparison_stack.is_redundant(cur_dies)
10504 && comparison_stack.vect_.back() == cur_dies)
10505 comparison_stack.cancel_canonical_propagated_type(cur_dies);
10506 }
10507 else
10508 {
10509 // We should never reach here.
10510 ABG_ASSERT_NOT_REACHED;
10511 }
10512
10513 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10514 result = COMPARISON_RESULT_UNKNOWN;
10515 else if (is_canon_type_to_be_propagated_tag(l_tag)
10516 && !comparison_stack.vect_.empty()
10517 && comparison_stack.vect_.back() == cur_dies)
10518 //Finally pop the pair types being compared from comparison_stack
10519 //iff {l,r} is on the top of the stack. If it's not, then it means
10520 //we are looking at a type that was detected as a being redundant
10521 //and thus hasn't been pushed to the stack yet gain.
10522 comparison_stack.erase(cur_dies);
10523
10524 maybe_cache_type_comparison_result(comparison_stack.rdr_,
10525 l_tag, cur_dies, result);
10526
10527 return result;
10528 }
10529
10530 /// Compare two DIEs emitted by a C compiler.
10531 ///
10532 /// @param rdr the DWARF reader used to load the DWARF information.
10533 ///
10534 /// @param l the left-hand-side argument of this comparison operator.
10535 ///
10536 /// @param r the righ-hand-side argument of this comparison operator.
10537 ///
10538 /// @param aggregates_being_compared this holds the names of the set
10539 /// of aggregates being compared. It's used by the comparison
10540 /// function to avoid recursing infinitely when faced with types
10541 /// referencing themselves through pointers or references. By
10542 /// default, just pass an empty instance of @ref istring_set_type to
10543 /// it.
10544 ///
10545 /// @param update_canonical_dies_on_the_fly if true, when two
10546 /// sub-types compare equal (during the comparison of @p l and @p r)
10547 /// update their canonical type. That way, two types of the same name
10548 /// are structurally compared to each other only once. So the
10549 /// non-linear structural comparison of two types of the same name
10550 /// only happen once.
10551 ///
10552 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10553 static comparison_result
compare_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,offset_pairs_stack_type & aggregates_being_compared,bool update_canonical_dies_on_the_fly)10554 compare_dies(const reader& rdr,
10555 const Dwarf_Die *l, const Dwarf_Die *r,
10556 offset_pairs_stack_type& aggregates_being_compared,
10557 bool update_canonical_dies_on_the_fly)
10558 {
10559 ABG_ASSERT(l);
10560 ABG_ASSERT(r);
10561
10562 const die_source l_die_source = rdr.get_die_source(l);
10563 const die_source r_die_source = rdr.get_die_source(r);
10564
10565 offset_type l_offset =
10566 {
10567 l_die_source,
10568 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10569 };
10570
10571 offset_type r_offset =
10572 {
10573 r_die_source,
10574 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10575 };
10576
10577 offset_pair_type dies_being_compared(l_offset, r_offset);
10578
10579 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10580 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10581
10582 if (l_tag != r_tag)
10583 ABG_RETURN_FALSE;
10584
10585 if (l_offset == r_offset)
10586 return COMPARISON_RESULT_EQUAL;
10587
10588 if (rdr.leverage_dwarf_factorization()
10589 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10590 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10591 if (l_offset != r_offset)
10592 return COMPARISON_RESULT_DIFFERENT;
10593
10594 comparison_result result = COMPARISON_RESULT_EQUAL;
10595 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10596 dies_being_compared,
10597 result))
10598 return result;
10599
10600 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10601 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10602
10603 // If 'l' and 'r' already have canonical DIEs, then just compare the
10604 // offsets of their canonical DIEs.
10605 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10606 {
10607 bool canonical_compare_result = false;
10608 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10609 l_die_source, r_die_source,
10610 l_has_canonical_die_offset,
10611 r_has_canonical_die_offset,
10612 l_canonical_die_offset,
10613 r_canonical_die_offset,
10614 canonical_compare_result))
10615 {
10616 comparison_result result;
10617 SET_RESULT_TO(result,
10618 (canonical_compare_result
10619 ? COMPARISON_RESULT_EQUAL
10620 : COMPARISON_RESULT_DIFFERENT),
10621 l, r);
10622 return result;
10623 }
10624 }
10625
10626
10627
10628 switch (l_tag)
10629 {
10630 case DW_TAG_base_type:
10631 case DW_TAG_string_type:
10632 case DW_TAG_unspecified_type:
10633 if (!compare_as_decl_and_type_dies(rdr, l, r))
10634 SET_RESULT_TO_FALSE(result, l, r);
10635 break;
10636
10637 case DW_TAG_typedef:
10638 case DW_TAG_pointer_type:
10639 case DW_TAG_reference_type:
10640 case DW_TAG_rvalue_reference_type:
10641 case DW_TAG_const_type:
10642 case DW_TAG_volatile_type:
10643 case DW_TAG_restrict_type:
10644 {
10645 if (!compare_as_type_dies(rdr, l, r))
10646 {
10647 SET_RESULT_TO_FALSE(result, l, r);
10648 break;
10649 }
10650
10651 bool from_the_same_tu = false;
10652 if (!pointer_or_qual_die_of_anonymous_class_type(l)
10653 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10654 && from_the_same_tu)
10655 {
10656 // These two typedefs, pointer, reference, or qualified
10657 // types have the same name and are defined in the same TU.
10658 // They thus ought to be the same.
10659 //
10660 // Note that pointers, reference or qualified types to
10661 // anonymous types are not taking into account here because
10662 // those always need to be structurally compared.
10663 SET_RESULT_TO_FALSE(result, l, r);
10664 break;
10665 }
10666 }
10667
10668 {
10669 // No fancy optimization in this case. We need to
10670 // structurally compare the two DIEs.
10671 Dwarf_Die lu_type_die, ru_type_die;
10672 bool lu_is_void, ru_is_void;
10673
10674 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10675 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10676
10677 if (lu_is_void && ru_is_void)
10678 result = COMPARISON_RESULT_EQUAL;
10679 else if (lu_is_void != ru_is_void)
10680 SET_RESULT_TO_FALSE(result, l, r);
10681 else
10682 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
10683 aggregates_being_compared,
10684 update_canonical_dies_on_the_fly);
10685 }
10686 break;
10687
10688 case DW_TAG_enumeration_type:
10689 if (!compare_as_decl_and_type_dies(rdr, l, r))
10690 SET_RESULT_TO_FALSE(result, l, r);
10691 else
10692 {
10693 // Walk the enumerators.
10694 Dwarf_Die l_enumtor, r_enumtor;
10695 bool found_l_enumtor = true, found_r_enumtor = true;
10696
10697 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10698 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10699 &l_enumtor) == 0,
10700 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10701 &r_enumtor) == 0;
10702 found_l_enumtor && found_r_enumtor;
10703 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
10704 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
10705 {
10706 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
10707 if ( l_tag != r_tag)
10708 {
10709 SET_RESULT_TO_FALSE(result, l, r);
10710 break;
10711 }
10712
10713 if (l_tag != DW_TAG_enumerator)
10714 continue;
10715
10716 uint64_t l_val = 0, r_val = 0;
10717 die_unsigned_constant_attribute(&l_enumtor,
10718 DW_AT_const_value,
10719 l_val);
10720 die_unsigned_constant_attribute(&r_enumtor,
10721 DW_AT_const_value,
10722 r_val);
10723 if (l_val != r_val)
10724 {
10725 SET_RESULT_TO_FALSE(result, l, r);
10726 break;
10727 }
10728 }
10729 if (found_l_enumtor != found_r_enumtor )
10730 SET_RESULT_TO_FALSE(result, l, r);
10731 }
10732 break;
10733
10734 case DW_TAG_structure_type:
10735 case DW_TAG_union_type:
10736 case DW_TAG_class_type:
10737 {
10738 RETURN_IF_COMPARISON_CYCLE_DETECTED;
10739
10740 rdr.compare_count_++;
10741
10742 if (!compare_as_decl_and_type_dies(rdr, l, r))
10743 SET_RESULT_TO_FALSE(result, l, r);
10744 else if (rdr.options().assume_odr_for_cplusplus
10745 && rdr.odr_is_relevant(l)
10746 && rdr.odr_is_relevant(r)
10747 && !die_is_anonymous(l)
10748 && !die_is_anonymous(r))
10749 result = COMPARISON_RESULT_EQUAL;
10750 else
10751 {
10752 aggregates_being_compared.add(dies_being_compared);
10753
10754 Dwarf_Die l_member, r_member;
10755 bool found_l_member = true, found_r_member = true;
10756
10757 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10758 for (found_l_member = get_member_child_die(l, &l_member),
10759 found_r_member = get_member_child_die(r, &r_member);
10760 found_l_member && found_r_member;
10761 found_l_member = get_next_member_sibling_die(&l_member,
10762 &l_member),
10763 found_r_member = get_next_member_sibling_die(&r_member,
10764 &r_member))
10765 {
10766 int l_tag = dwarf_tag(&l_member),
10767 r_tag = dwarf_tag(&r_member);
10768
10769 if (l_tag != r_tag)
10770 {
10771 SET_RESULT_TO_FALSE(result, l, r);
10772 break;
10773 }
10774
10775 ABG_ASSERT(l_tag == DW_TAG_member
10776 || l_tag == DW_TAG_variable
10777 || l_tag == DW_TAG_inheritance
10778 || l_tag == DW_TAG_subprogram);
10779
10780 comparison_result local_result =
10781 compare_dies(rdr, &l_member, &r_member,
10782 aggregates_being_compared,
10783 update_canonical_dies_on_the_fly);
10784
10785 if (local_result == COMPARISON_RESULT_UNKNOWN)
10786 // Note that if the result of comparing any
10787 // sub-type is COMPARISON_RESULT_EQUAL, just
10788 // because we have at least one sub-type's
10789 // comparison being COMPARISON_RESULT_UNKNOWN
10790 // means that the comparison of this type will
10791 // return COMPARISON_RESULT_UNKNOWN to show
10792 // callers that this type (and all the types that
10793 // depend on it) depends on a redundant type
10794 result = local_result;
10795
10796 if (local_result == COMPARISON_RESULT_DIFFERENT)
10797 {
10798 SET_RESULT_TO_FALSE(result, l, r);
10799 break;
10800 }
10801 }
10802 if (found_l_member != found_r_member)
10803 {
10804 SET_RESULT_TO_FALSE(result, l, r);
10805 break;
10806 }
10807 }
10808 }
10809 break;
10810
10811 case DW_TAG_array_type:
10812 {
10813 RETURN_IF_COMPARISON_CYCLE_DETECTED;
10814
10815 aggregates_being_compared.add(dies_being_compared);
10816
10817 rdr.compare_count_++;
10818
10819 Dwarf_Die l_child, r_child;
10820 bool found_l_child, found_r_child;
10821 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
10822 &l_child) == 0,
10823 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
10824 &r_child) == 0;
10825 found_l_child && found_r_child;
10826 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
10827 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
10828 {
10829 int l_child_tag = dwarf_tag(&l_child),
10830 r_child_tag = dwarf_tag(&r_child);
10831 if (l_child_tag == DW_TAG_subrange_type
10832 || r_child_tag == DW_TAG_subrange_type)
10833 {
10834 result = compare_dies(rdr, &l_child, &r_child,
10835 aggregates_being_compared,
10836 update_canonical_dies_on_the_fly);
10837 if (!result)
10838 {
10839 SET_RESULT_TO_FALSE(result, l, r);
10840 break;
10841 }
10842 }
10843 }
10844 if (found_l_child != found_r_child)
10845 SET_RESULT_TO_FALSE(result, l, r);
10846 // Compare the types of the elements of the array.
10847 Dwarf_Die ltype_die, rtype_die;
10848 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
10849 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
10850 ABG_ASSERT(found_ltype && found_rtype);
10851
10852 result = compare_dies(rdr, <ype_die, &rtype_die,
10853 aggregates_being_compared,
10854 update_canonical_dies_on_the_fly);
10855 if (!result)
10856 ABG_RETURN_FALSE;
10857 }
10858 break;
10859
10860 case DW_TAG_subrange_type:
10861 {
10862 uint64_t l_lower_bound = 0, r_lower_bound = 0,
10863 l_upper_bound = 0, r_upper_bound = 0;
10864 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
10865 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
10866 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
10867 l_upper_bound))
10868 {
10869 uint64_t l_count = 0;
10870 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
10871 {
10872 l_upper_bound = l_lower_bound + l_count;
10873 if (l_upper_bound)
10874 --l_upper_bound;
10875 }
10876 }
10877 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
10878 r_upper_bound))
10879 {
10880 uint64_t r_count = 0;
10881 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
10882 {
10883 r_upper_bound = r_lower_bound + r_count;
10884 if (r_upper_bound)
10885 --r_upper_bound;
10886 }
10887 }
10888
10889 if ((l_lower_bound != r_lower_bound)
10890 || (l_upper_bound != r_upper_bound))
10891 SET_RESULT_TO_FALSE(result, l, r);
10892 }
10893 break;
10894
10895 case DW_TAG_subroutine_type:
10896 case DW_TAG_subprogram:
10897 {
10898 RETURN_IF_COMPARISON_CYCLE_DETECTED;
10899
10900 aggregates_being_compared.add(dies_being_compared);
10901
10902 rdr.compare_count_++;
10903
10904 if (l_tag == DW_TAG_subprogram
10905 && !fn_die_equal_by_linkage_name(rdr, l, r))
10906 {
10907 SET_RESULT_TO_FALSE(result, l, r);
10908 break;
10909 }
10910 else if (l_tag == DW_TAG_subprogram
10911 && rdr.die_is_in_c(l) && rdr.die_is_in_c(r)
10912 /*&& fn_die_equal_by_linkage_name(rdr, l, r)*/)
10913 {
10914 result = COMPARISON_RESULT_EQUAL;
10915 break;
10916 }
10917 else if (!rdr.die_is_in_c(l) && !rdr.die_is_in_c(r))
10918 {
10919 // In C, we cannot have two different functions with the
10920 // same linkage name in a given binary. But here we are
10921 // looking at DIEs that don't originate from C. So we
10922 // need to compare return types and parameter types.
10923 Dwarf_Die l_return_type, r_return_type;
10924 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
10925 l_return_type);
10926 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
10927 r_return_type);
10928 if (l_return_type_is_void != r_return_type_is_void
10929 || (!l_return_type_is_void
10930 && !compare_dies(rdr,
10931 &l_return_type, &r_return_type,
10932 aggregates_being_compared,
10933 update_canonical_dies_on_the_fly)))
10934 SET_RESULT_TO_FALSE(result, l, r);
10935 else
10936 {
10937 Dwarf_Die l_child, r_child;
10938 bool found_l_child, found_r_child;
10939 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
10940 &l_child) == 0,
10941 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
10942 &r_child) == 0;
10943 found_l_child && found_r_child;
10944 found_l_child = dwarf_siblingof(&l_child,
10945 &l_child) == 0,
10946 found_r_child = dwarf_siblingof(&r_child,
10947 &r_child)==0)
10948 {
10949 int l_child_tag = dwarf_tag(&l_child);
10950 int r_child_tag = dwarf_tag(&r_child);
10951 comparison_result local_result =
10952 COMPARISON_RESULT_EQUAL;
10953 if (l_child_tag != r_child_tag)
10954 local_result = COMPARISON_RESULT_DIFFERENT;
10955 if (l_child_tag == DW_TAG_formal_parameter)
10956 local_result =
10957 compare_dies(rdr, &l_child, &r_child,
10958 aggregates_being_compared,
10959 update_canonical_dies_on_the_fly);
10960 if (local_result == COMPARISON_RESULT_DIFFERENT)
10961 {
10962 result = local_result;
10963 SET_RESULT_TO_FALSE(result, l, r);
10964 break;
10965 }
10966 if (local_result == COMPARISON_RESULT_UNKNOWN)
10967 // Note that if the result of comparing any
10968 // sub-type is COMPARISON_RESULT_EQUAL, just
10969 // because we have at least one sub-type's
10970 // comparison being COMPARISON_RESULT_UNKNOWN
10971 // means that the comparison of this type will
10972 // return COMPARISON_RESULT_UNKNOWN to show
10973 // callers that this type (and all the types
10974 // that depend on it) depends on a redundant
10975 // type and so, can't be
10976 // canonical-type-propagated.
10977 result = local_result;
10978 }
10979 if (found_l_child != found_r_child)
10980 {
10981 SET_RESULT_TO_FALSE(result, l, r);
10982 break;
10983 }
10984 }
10985 }
10986 }
10987 break;
10988
10989 case DW_TAG_formal_parameter:
10990 {
10991 Dwarf_Die l_type, r_type;
10992 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
10993 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
10994 if (l_type_is_void != r_type_is_void)
10995 SET_RESULT_TO_FALSE(result, l, r);
10996 else if (!l_type_is_void)
10997 {
10998 comparison_result local_result =
10999 compare_dies(rdr, &l_type, &r_type,
11000 aggregates_being_compared,
11001 update_canonical_dies_on_the_fly);
11002 SET_RESULT_TO(result, local_result, l, r);
11003 }
11004 }
11005 break;
11006
11007 case DW_TAG_variable:
11008 case DW_TAG_member:
11009 if (compare_as_decl_dies(l, r))
11010 {
11011 // Compare the offsets of the data members
11012 if (l_tag == DW_TAG_member)
11013 {
11014 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11015 die_member_offset(rdr, l, l_offset_in_bits);
11016 die_member_offset(rdr, r, r_offset_in_bits);
11017 if (l_offset_in_bits != r_offset_in_bits)
11018 SET_RESULT_TO_FALSE(result, l, r);
11019 }
11020 if (result)
11021 {
11022 // Compare the types of the data members or variables.
11023 Dwarf_Die l_type, r_type;
11024 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11025 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11026 comparison_result local_result =
11027 compare_dies(rdr, &l_type, &r_type,
11028 aggregates_being_compared,
11029 update_canonical_dies_on_the_fly);
11030 SET_RESULT_TO(result, local_result, l, r);
11031 }
11032 }
11033 else
11034 SET_RESULT_TO_FALSE(result, l, r);
11035 break;
11036
11037 case DW_TAG_inheritance:
11038 {
11039 Dwarf_Die l_type, r_type;
11040 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11041 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11042 result = compare_dies(rdr, &l_type, &r_type,
11043 aggregates_being_compared,
11044 update_canonical_dies_on_the_fly);
11045 if (!result)
11046 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11047
11048 uint64_t l_a = 0, r_a = 0;
11049 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11050 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11051 if (l_a != r_a)
11052 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11053
11054 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11055 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11056 if (l_a != r_a)
11057 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11058
11059 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11060 die_member_offset(rdr, l, l_offset_in_bits);
11061 die_member_offset(rdr, r, r_offset_in_bits);
11062 if (l_offset_in_bits != r_offset_in_bits)
11063 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11064 }
11065 break;
11066
11067 case DW_TAG_ptr_to_member_type:
11068 {
11069 bool comp_result = false;
11070 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11071 if (!comp_result)
11072 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11073
11074 Dwarf_Die l_type, r_type;
11075 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11076 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11077 result = compare_dies(rdr, &l_type, &r_type,
11078 aggregates_being_compared,
11079 update_canonical_dies_on_the_fly);
11080 if (!result)
11081 ABG_RETURN(result);
11082
11083 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11084 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11085 result = compare_dies(rdr, &l_type, &r_type,
11086 aggregates_being_compared,
11087 update_canonical_dies_on_the_fly);
11088 if (!result)
11089 ABG_RETURN(result);
11090 }
11091 break;
11092
11093 case DW_TAG_enumerator:
11094 case DW_TAG_packed_type:
11095 case DW_TAG_set_type:
11096 case DW_TAG_file_type:
11097 case DW_TAG_thrown_type:
11098 case DW_TAG_interface_type:
11099 case DW_TAG_shared_type:
11100 case DW_TAG_compile_unit:
11101 case DW_TAG_namespace:
11102 case DW_TAG_module:
11103 case DW_TAG_constant:
11104 case DW_TAG_partial_unit:
11105 case DW_TAG_imported_unit:
11106 case DW_TAG_dwarf_procedure:
11107 case DW_TAG_imported_declaration:
11108 case DW_TAG_entry_point:
11109 case DW_TAG_label:
11110 case DW_TAG_lexical_block:
11111 case DW_TAG_unspecified_parameters:
11112 case DW_TAG_variant:
11113 case DW_TAG_common_block:
11114 case DW_TAG_common_inclusion:
11115 case DW_TAG_inlined_subroutine:
11116 case DW_TAG_with_stmt:
11117 case DW_TAG_access_declaration:
11118 case DW_TAG_catch_block:
11119 case DW_TAG_friend:
11120 case DW_TAG_namelist:
11121 case DW_TAG_namelist_item:
11122 case DW_TAG_template_type_parameter:
11123 case DW_TAG_template_value_parameter:
11124 case DW_TAG_try_block:
11125 case DW_TAG_variant_part:
11126 case DW_TAG_imported_module:
11127 case DW_TAG_condition:
11128 case DW_TAG_type_unit:
11129 case DW_TAG_template_alias:
11130 case DW_TAG_lo_user:
11131 case DW_TAG_MIPS_loop:
11132 case DW_TAG_format_label:
11133 case DW_TAG_function_template:
11134 case DW_TAG_class_template:
11135 case DW_TAG_GNU_BINCL:
11136 case DW_TAG_GNU_EINCL:
11137 case DW_TAG_GNU_template_template_param:
11138 case DW_TAG_GNU_template_parameter_pack:
11139 case DW_TAG_GNU_formal_parameter_pack:
11140 case DW_TAG_GNU_call_site:
11141 case DW_TAG_GNU_call_site_parameter:
11142 case DW_TAG_hi_user:
11143 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11144 if (rdr.debug_die_canonicalization_is_on_)
11145 ABG_ASSERT_NOT_REACHED;
11146 #endif
11147 ABG_ASSERT_NOT_REACHED;
11148 break;
11149 }
11150
11151 ABG_RETURN(result);
11152 }
11153
11154 /// Compare two DIEs emitted by a C compiler.
11155 ///
11156 /// @param rdr the DWARF reader used to load the DWARF information.
11157 ///
11158 /// @param l the left-hand-side argument of this comparison operator.
11159 ///
11160 /// @param r the righ-hand-side argument of this comparison operator.
11161 ///
11162 /// @param update_canonical_dies_on_the_fly if yes, then this function
11163 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11164 /// comparing l and r. This helps in making so that sub-type DIEs of
11165 /// 'l' and 'r' are compared structurally only once. This is how we
11166 /// turn this exponential comparison problem into a problem that is a
11167 /// closer to a linear one.
11168 ///
11169 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11170 static comparison_result
compare_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,bool update_canonical_dies_on_the_fly)11171 compare_dies(const reader& rdr,
11172 const Dwarf_Die *l,
11173 const Dwarf_Die *r,
11174 bool update_canonical_dies_on_the_fly)
11175 {
11176 offset_pairs_stack_type aggregates_being_compared(rdr);
11177 return compare_dies(rdr, l, r, aggregates_being_compared,
11178 update_canonical_dies_on_the_fly);
11179 }
11180
11181 /// Compare two DIEs for the purpose of canonicalization.
11182 ///
11183 /// This is a sub-routine of reader::get_canonical_die.
11184 ///
11185 /// When DIE canonicalization debugging is on, this function performs
11186 /// both structural and canonical comparison. It expects that both
11187 /// comparison yield the same result.
11188 ///
11189 /// @param rdr the DWARF reader.
11190 ///
11191 /// @param l the left-hand-side comparison operand DIE.
11192 ///
11193 /// @param r the right-hand-side comparison operand DIE.
11194 ///
11195 /// @param update_canonical_dies_on_the_fly if true, then some
11196 /// aggregate DIEs will see their canonical types propagated.
11197 ///
11198 /// @return true iff @p l equals @p r.
11199 static bool
compare_dies_during_canonicalization(reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,bool update_canonical_dies_on_the_fly)11200 compare_dies_during_canonicalization(reader& rdr,
11201 const Dwarf_Die *l,
11202 const Dwarf_Die *r,
11203 bool update_canonical_dies_on_the_fly)
11204 {
11205 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11206 if (rdr.debug_die_canonicalization_is_on_)
11207 {
11208 bool canonical_equality = false, structural_equality = false;
11209 rdr.use_canonical_die_comparison_ = false;
11210 structural_equality = compare_dies(rdr, l, r,
11211 /*update_canonical_dies_on_the_fly=*/false);
11212 rdr.use_canonical_die_comparison_ = true;
11213 canonical_equality = compare_dies(rdr, l, r,
11214 update_canonical_dies_on_the_fly);
11215 if (canonical_equality != structural_equality)
11216 {
11217 std::cerr << "structural & canonical equality different for DIEs: "
11218 << std::hex
11219 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11220 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11221 << std::dec
11222 << ", repr: '"
11223 << rdr.get_die_pretty_type_representation(l, 0)
11224 << "'"
11225 << std::endl;
11226 ABG_ASSERT_NOT_REACHED;
11227 }
11228 return structural_equality;
11229 }
11230 #endif
11231 return compare_dies(rdr, l, r,
11232 update_canonical_dies_on_the_fly);
11233 }
11234
11235 // ----------------------------------
11236 // </die comparison engine>
11237 // ---------------------------------
11238
11239 /// Get the point where a DW_AT_import DIE is used to import a given
11240 /// (unit) DIE, between two DIEs.
11241 ///
11242 /// @param rdr the dwarf reader to consider.
11243 ///
11244 /// @param partial_unit_offset the imported unit for which we want to
11245 /// know the insertion point. This is usually a partial unit (with
11246 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11247 /// so.
11248 ///
11249 /// @param first_die_offset the offset of the DIE from which this
11250 /// function starts looking for the import point of
11251 /// @partial_unit_offset. Note that this offset is excluded from the
11252 /// set of potential solutions.
11253 ///
11254 /// @param first_die_cu_offset the offset of the (compilation) unit
11255 /// that @p first_die_cu_offset belongs to.
11256 ///
11257 /// @param source where the DIE of first_die_cu_offset unit comes
11258 /// from.
11259 ///
11260 /// @param last_die_offset the offset of the last DIE of the up to
11261 /// which this function looks for the import point of @p
11262 /// partial_unit_offset. Note that this offset is excluded from the
11263 /// set of potential solutions.
11264 ///
11265 /// @param imported_point_offset. The resulting
11266 /// imported_point_offset. Note that if the imported DIE @p
11267 /// partial_unit_offset is not found between @p first_die_offset and
11268 /// @p last_die_offset, this parameter is left untouched by this
11269 /// function.
11270 ///
11271 /// @return true iff an imported unit is found between @p
11272 /// first_die_offset and @p last_die_offset.
11273 static bool
find_import_unit_point_between_dies(const reader & rdr,size_t partial_unit_offset,Dwarf_Off first_die_offset,Dwarf_Off first_die_cu_offset,die_source source,size_t last_die_offset,size_t & imported_point_offset)11274 find_import_unit_point_between_dies(const reader& rdr,
11275 size_t partial_unit_offset,
11276 Dwarf_Off first_die_offset,
11277 Dwarf_Off first_die_cu_offset,
11278 die_source source,
11279 size_t last_die_offset,
11280 size_t& imported_point_offset)
11281 {
11282 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11283 rdr.tu_die_imported_unit_points_map(source);
11284
11285 tu_die_imported_unit_points_map_type::const_iterator iter =
11286 tu_die_imported_unit_points_map.find(first_die_cu_offset);
11287
11288 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11289
11290 const imported_unit_points_type& imported_unit_points = iter->second;
11291 if (imported_unit_points.empty())
11292 return false;
11293
11294 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11295 imported_unit_points_type::const_iterator e = imported_unit_points.end();
11296
11297 find_lower_bound_in_imported_unit_points(imported_unit_points,
11298 first_die_offset,
11299 b);
11300
11301 if (last_die_offset != static_cast<size_t>(-1))
11302 find_lower_bound_in_imported_unit_points(imported_unit_points,
11303 last_die_offset,
11304 e);
11305
11306 if (e != imported_unit_points.end())
11307 {
11308 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11309 if (i->imported_unit_die_off == partial_unit_offset)
11310 {
11311 imported_point_offset = i->offset_of_import ;
11312 return true;
11313 }
11314
11315 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11316 {
11317 if (find_import_unit_point_between_dies(rdr,
11318 partial_unit_offset,
11319 i->imported_unit_child_off,
11320 i->imported_unit_cu_off,
11321 i->imported_unit_die_source,
11322 /*(Dwarf_Off)*/-1,
11323 imported_point_offset))
11324 return true;
11325 }
11326 }
11327 else
11328 {
11329 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11330 if (i->imported_unit_die_off == partial_unit_offset)
11331 {
11332 imported_point_offset = i->offset_of_import ;
11333 return true;
11334 }
11335
11336 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11337 {
11338 if (find_import_unit_point_between_dies(rdr,
11339 partial_unit_offset,
11340 i->imported_unit_child_off,
11341 i->imported_unit_cu_off,
11342 i->imported_unit_die_source,
11343 /*(Dwarf_Off)*/-1,
11344 imported_point_offset))
11345 return true;
11346 }
11347 }
11348
11349 return false;
11350 }
11351
11352 /// In the current translation unit, get the last point where a
11353 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
11354 /// given DIE is found. That given DIE is called the limit DIE.
11355 ///
11356 /// Said otherwise, this function returns the last import point of a
11357 /// unit, before a limit.
11358 ///
11359 /// @param rdr the dwarf reader to consider.
11360 ///
11361 /// @param partial_unit_offset the imported unit for which we want to
11362 /// know the insertion point of. This is usually a partial unit (with
11363 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11364 /// so.
11365 ///
11366 /// @param where_offset the offset of the limit DIE.
11367 ///
11368 /// @param imported_point_offset. The resulting imported_point_offset.
11369 /// Note that if the imported DIE @p partial_unit_offset is not found
11370 /// before @p die_offset, this is set to the last @p
11371 /// partial_unit_offset found under @p parent_die.
11372 ///
11373 /// @return true iff an imported unit is found before @p die_offset.
11374 /// Note that if an imported unit is found after @p die_offset then @p
11375 /// imported_point_offset is set and the function return false.
11376 static bool
find_import_unit_point_before_die(const reader & rdr,size_t partial_unit_offset,size_t where_offset,size_t & imported_point_offset)11377 find_import_unit_point_before_die(const reader& rdr,
11378 size_t partial_unit_offset,
11379 size_t where_offset,
11380 size_t& imported_point_offset)
11381 {
11382 size_t import_point_offset = 0;
11383 Dwarf_Die first_die_of_tu;
11384
11385 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11386 &first_die_of_tu) != 0)
11387 return false;
11388
11389 Dwarf_Die cu_die_memory;
11390 Dwarf_Die *cu_die;
11391
11392 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11393 &cu_die_memory, 0, 0);
11394
11395 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11396 dwarf_dieoffset(&first_die_of_tu),
11397 dwarf_dieoffset(cu_die),
11398 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11399 where_offset,
11400 import_point_offset))
11401 {
11402 imported_point_offset = import_point_offset;
11403 return true;
11404 }
11405
11406 if (import_point_offset)
11407 {
11408 imported_point_offset = import_point_offset;
11409 return true;
11410 }
11411
11412 return false;
11413 }
11414
11415 /// Return the parent DIE for a given DIE.
11416 ///
11417 /// Note that the function build_die_parent_map() must have been
11418 /// called before this one can work. This function either succeeds or
11419 /// aborts the current process.
11420 ///
11421 /// @param rdr the DWARF reader to consider.
11422 ///
11423 /// @param die the DIE for which we want the parent.
11424 ///
11425 /// @param parent_die the output parameter set to the parent die of
11426 /// @p die. Its memory must be allocated and handled by the caller.
11427 ///
11428 /// @param where_offset the offset of the DIE where we are "logically"
11429 /// positionned at, in the DIE tree. This is useful when @p die is
11430 /// e.g, DW_TAG_partial_unit that can be included in several places in
11431 /// the DIE tree.
11432 ///
11433 /// @return true if the function could get a parent DIE, false
11434 /// otherwise.
11435 static bool
get_parent_die(const reader & rdr,const Dwarf_Die * die,Dwarf_Die & parent_die,size_t where_offset)11436 get_parent_die(const reader& rdr,
11437 const Dwarf_Die* die,
11438 Dwarf_Die& parent_die,
11439 size_t where_offset)
11440 {
11441 ABG_ASSERT(rdr.dwarf_debug_info());
11442
11443 const die_source source = rdr.get_die_source(die);
11444
11445 const offset_offset_map_type& m = rdr.die_parent_map(source);
11446 offset_offset_map_type::const_iterator i =
11447 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11448
11449 if (i == m.end())
11450 return false;
11451
11452 switch (source)
11453 {
11454 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11455 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11456 i->second, &parent_die));
11457 break;
11458 case ALT_DEBUG_INFO_DIE_SOURCE:
11459 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11460 i->second, &parent_die));
11461 break;
11462 case TYPE_UNIT_DIE_SOURCE:
11463 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11464 i->second, &parent_die));
11465 break;
11466 case NO_DEBUG_INFO_DIE_SOURCE:
11467 case NUMBER_OF_DIE_SOURCES:
11468 ABG_ASSERT_NOT_REACHED;
11469 }
11470
11471 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11472 {
11473 if (where_offset == 0)
11474 {
11475 parent_die = *rdr.cur_tu_die();
11476 return true;
11477 }
11478 size_t import_point_offset = 0;
11479 bool found =
11480 find_import_unit_point_before_die(rdr,
11481 dwarf_dieoffset(&parent_die),
11482 where_offset,
11483 import_point_offset);
11484 if (!found)
11485 // It looks like parent_die (which comes from the alternate
11486 // debug info file) hasn't been imported into this TU. So,
11487 // Let's assume its logical parent is the DIE of the current
11488 // TU.
11489 parent_die = *rdr.cur_tu_die();
11490 else
11491 {
11492 ABG_ASSERT(import_point_offset);
11493 Dwarf_Die import_point_die;
11494 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11495 import_point_offset,
11496 &import_point_die));
11497 return get_parent_die(rdr, &import_point_die,
11498 parent_die, where_offset);
11499 }
11500 }
11501
11502 return true;
11503 }
11504
11505 /// Get the DIE representing the scope of a given DIE.
11506 ///
11507 /// Please note that when the DIE we are looking at has a
11508 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11509 /// DIE is the parent DIE of the DIE referred to by that attribute.
11510 /// This is the only case where a scope DIE is different from the
11511 /// parent DIE of a given DIE.
11512 ///
11513 /// Also note that if the current translation unit is from C, then
11514 /// this returns the global scope.
11515 ///
11516 /// @param rdr the DWARF reader to use.
11517 ///
11518 /// @param die the DIE to consider.
11519 ///
11520 /// @param where_offset where we are logically at in the DIE stream.
11521 ///
11522 /// @param scope_die out parameter. This is set to the resulting
11523 /// scope DIE iff the function returns true.
11524 static bool
get_scope_die(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & scope_die)11525 get_scope_die(const reader& rdr,
11526 const Dwarf_Die* die,
11527 size_t where_offset,
11528 Dwarf_Die& scope_die)
11529 {
11530 if (is_c_language(rdr.cur_transl_unit()->get_language()))
11531 {
11532 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11533 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11534 }
11535
11536 Dwarf_Die logical_parent_die;
11537 if (die_die_attribute(die, DW_AT_specification,
11538 logical_parent_die, false)
11539 || die_die_attribute(die, DW_AT_abstract_origin,
11540 logical_parent_die, false))
11541 return get_scope_die(rdr, &logical_parent_die, where_offset, scope_die);
11542
11543 if (!get_parent_die(rdr, die, scope_die, where_offset))
11544 return false;
11545
11546 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11547 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11548 || dwarf_tag(&scope_die) == DW_TAG_array_type)
11549 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11550
11551 return true;
11552 }
11553
11554 /// Return the abigail IR node representing the scope of a given DIE.
11555 ///
11556 /// Note that it is the logical scope that is returned. That is, if
11557 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11558 /// attribute, it's the scope of the referred-to DIE (via these
11559 /// attributes) that is returned.
11560 ///
11561 /// Also note that if the current translation unit is from C, then
11562 /// this returns the global scope.
11563 ///
11564 /// @param rdr the dwarf reader to use.
11565 ///
11566 /// @param die the DIE to get the scope for.
11567 ///
11568 /// @param called_from_public_decl is true if this function has been
11569 /// initially called within the context of a public decl.
11570 ///
11571 /// @param where_offset the offset of the DIE where we are "logically"
11572 /// positionned at, in the DIE tree. This is useful when @p die is
11573 /// e.g, DW_TAG_partial_unit that can be included in several places in
11574 /// the DIE tree.
11575 static scope_decl_sptr
get_scope_for_die(reader & rdr,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)11576 get_scope_for_die(reader& rdr,
11577 Dwarf_Die* die,
11578 bool called_for_public_decl,
11579 size_t where_offset)
11580 {
11581 const die_source source_of_die = rdr.get_die_source(die);
11582
11583 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11584 rdr.get_die_language(die, die_lang);
11585 if (is_c_language(die_lang)
11586 || rdr.die_parent_map(source_of_die).empty())
11587 {
11588 // In units for the C languages all decls belong to the global
11589 // namespace. This is generally the case if Libabigail
11590 // determined that no DIE -> parent map was needed.
11591 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11592 return rdr.global_scope();
11593 }
11594
11595 Dwarf_Die cloned_die;
11596 if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
11597 || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
11598 return get_scope_for_die(rdr, &cloned_die,
11599 called_for_public_decl,
11600 where_offset);
11601
11602 Dwarf_Die parent_die;
11603
11604 if (!get_parent_die(rdr, die, parent_die, where_offset))
11605 return rdr.nil_scope();
11606
11607 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11608 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11609 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11610 {
11611 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11612 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11613 {
11614 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11615 || source_of_die == TYPE_UNIT_DIE_SOURCE);
11616 return rdr.cur_transl_unit()->get_global_scope();
11617 }
11618
11619 // For top level DIEs like DW_TAG_compile_unit, we just want to
11620 // return the global scope for the corresponding translation
11621 // unit. This must have been set by
11622 // build_translation_unit_and_add_to_ir if we already started to
11623 // build the translation unit of parent_die. Otherwise, just
11624 // return the global scope of the current translation unit.
11625 die_tu_map_type::const_iterator i =
11626 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
11627 if (i != rdr.die_tu_map().end())
11628 return i->second->get_global_scope();
11629 return rdr.cur_transl_unit()->get_global_scope();
11630 }
11631
11632 scope_decl_sptr s;
11633 type_or_decl_base_sptr d;
11634 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11635 || dwarf_tag(&parent_die) == DW_TAG_array_type
11636 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
11637 // this is an entity defined in a scope that is a function.
11638 // Normally, I would say that this should be dropped. But I have
11639 // seen a case where a typedef DIE needed by a function parameter
11640 // was defined right before the parameter, under the scope of the
11641 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
11642 // the function parm too. So for that case, let's say that the
11643 // scope is the scope of the function itself. Note that this is
11644 // an error of the DWARF emitter. We should never see this DIE in
11645 // this context.
11646 {
11647 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
11648 called_for_public_decl,
11649 where_offset);
11650 if (is_anonymous_type_die(die))
11651 // For anonymous type that have nothing to do in a function or
11652 // array type context, let's put it in the containing
11653 // namespace. That is, do not let it be in a containing class
11654 // or union where it has nothing to do.
11655 while (is_class_or_union_type(s))
11656 {
11657 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
11658 return rdr.nil_scope();
11659 s = get_scope_for_die(rdr, &parent_die,
11660 called_for_public_decl,
11661 where_offset);
11662 }
11663 return s;
11664 }
11665 else
11666 d = build_ir_node_from_die(rdr, &parent_die,
11667 called_for_public_decl,
11668 where_offset);
11669 s = dynamic_pointer_cast<scope_decl>(d);
11670 if (!s)
11671 // this is an entity defined in someting that is not a scope.
11672 // Let's drop it.
11673 return rdr.nil_scope();
11674
11675 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11676 if (cl && cl->get_is_declaration_only())
11677 {
11678 scope_decl_sptr scop =
11679 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
11680 if (scop)
11681 s = scop;
11682 else
11683 s = cl;
11684 }
11685 return s;
11686 }
11687
11688 /// Convert a DWARF constant representing the value of the
11689 /// DW_AT_language property into the translation_unit::language
11690 /// enumerator.
11691 ///
11692 /// @param l the DWARF constant to convert.
11693 ///
11694 /// @return the resulting translation_unit::language enumerator.
11695 static translation_unit::language
dwarf_language_to_tu_language(size_t l)11696 dwarf_language_to_tu_language(size_t l)
11697 {
11698 switch (l)
11699 {
11700 case DW_LANG_C89:
11701 return translation_unit::LANG_C89;
11702 case DW_LANG_C:
11703 return translation_unit::LANG_C;
11704 case DW_LANG_Ada83:
11705 return translation_unit::LANG_Ada83;
11706 case DW_LANG_C_plus_plus:
11707 return translation_unit::LANG_C_plus_plus;
11708 case DW_LANG_Cobol74:
11709 return translation_unit::LANG_Cobol74;
11710 case DW_LANG_Cobol85:
11711 return translation_unit::LANG_Cobol85;
11712 case DW_LANG_Fortran77:
11713 return translation_unit::LANG_Fortran77;
11714 case DW_LANG_Fortran90:
11715 return translation_unit::LANG_Fortran90;
11716 case DW_LANG_Pascal83:
11717 return translation_unit::LANG_Pascal83;
11718 case DW_LANG_Modula2:
11719 return translation_unit::LANG_Modula2;
11720 case DW_LANG_Java:
11721 return translation_unit::LANG_Java;
11722 case DW_LANG_C99:
11723 return translation_unit::LANG_C99;
11724 case DW_LANG_Ada95:
11725 return translation_unit::LANG_Ada95;
11726 case DW_LANG_Fortran95:
11727 return translation_unit::LANG_Fortran95;
11728 case DW_LANG_PLI:
11729 return translation_unit::LANG_PLI;
11730 case DW_LANG_ObjC:
11731 return translation_unit::LANG_ObjC;
11732 case DW_LANG_ObjC_plus_plus:
11733 return translation_unit::LANG_ObjC_plus_plus;
11734
11735 #ifdef HAVE_DW_LANG_Rust_enumerator
11736 case DW_LANG_Rust:
11737 return translation_unit::LANG_Rust;
11738 #endif
11739
11740 #ifdef HAVE_DW_LANG_UPC_enumerator
11741 case DW_LANG_UPC:
11742 return translation_unit::LANG_UPC;
11743 #endif
11744
11745 #ifdef HAVE_DW_LANG_D_enumerator
11746 case DW_LANG_D:
11747 return translation_unit::LANG_D;
11748 #endif
11749
11750 #ifdef HAVE_DW_LANG_Python_enumerator
11751 case DW_LANG_Python:
11752 return translation_unit::LANG_Python;
11753 #endif
11754
11755 #ifdef HAVE_DW_LANG_Go_enumerator
11756 case DW_LANG_Go:
11757 return translation_unit::LANG_Go;
11758 #endif
11759
11760 #ifdef HAVE_DW_LANG_C11_enumerator
11761 case DW_LANG_C11:
11762 return translation_unit::LANG_C11;
11763 #endif
11764
11765 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
11766 case DW_LANG_C_plus_plus_03:
11767 return translation_unit::LANG_C_plus_plus_03;
11768 #endif
11769
11770 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
11771 case DW_LANG_C_plus_plus_11:
11772 return translation_unit::LANG_C_plus_plus_11;
11773 #endif
11774
11775 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
11776 case DW_LANG_C_plus_plus_14:
11777 return translation_unit::LANG_C_plus_plus_14;
11778 #endif
11779
11780 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
11781 case DW_LANG_Mips_Assembler:
11782 return translation_unit::LANG_Mips_Assembler;
11783 #endif
11784
11785 default:
11786 return translation_unit::LANG_UNKNOWN;
11787 }
11788 }
11789
11790 /// Get the default array lower bound value as defined by the DWARF
11791 /// specification, version 4, depending on the language of the
11792 /// translation unit.
11793 ///
11794 /// @param l the language of the translation unit.
11795 ///
11796 /// @return the default array lower bound value.
11797 static uint64_t
get_default_array_lower_bound(translation_unit::language l)11798 get_default_array_lower_bound(translation_unit::language l)
11799 {
11800 int value = 0;
11801 switch (l)
11802 {
11803 case translation_unit::LANG_UNKNOWN:
11804 value = 0;
11805 break;
11806 case translation_unit::LANG_Cobol74:
11807 case translation_unit::LANG_Cobol85:
11808 value = 1;
11809 break;
11810 case translation_unit::LANG_C89:
11811 case translation_unit::LANG_C99:
11812 case translation_unit::LANG_C11:
11813 case translation_unit::LANG_C:
11814 case translation_unit::LANG_C_plus_plus_03:
11815 case translation_unit::LANG_C_plus_plus_11:
11816 case translation_unit::LANG_C_plus_plus_14:
11817 case translation_unit::LANG_C_plus_plus:
11818 case translation_unit::LANG_ObjC:
11819 case translation_unit::LANG_ObjC_plus_plus:
11820 case translation_unit::LANG_Rust:
11821 value = 0;
11822 break;
11823 case translation_unit::LANG_Fortran77:
11824 case translation_unit::LANG_Fortran90:
11825 case translation_unit::LANG_Fortran95:
11826 case translation_unit::LANG_Ada83:
11827 case translation_unit::LANG_Ada95:
11828 case translation_unit::LANG_Pascal83:
11829 case translation_unit::LANG_Modula2:
11830 value = 1;
11831 break;
11832 case translation_unit::LANG_Java:
11833 value = 0;
11834 break;
11835 case translation_unit::LANG_PLI:
11836 value = 1;
11837 break;
11838 case translation_unit::LANG_UPC:
11839 case translation_unit::LANG_D:
11840 case translation_unit::LANG_Python:
11841 case translation_unit::LANG_Go:
11842 case translation_unit::LANG_Mips_Assembler:
11843 value = 0;
11844 break;
11845 }
11846
11847 return value;
11848 }
11849
11850 /// For a given offset, find the lower bound of a sorted vector of
11851 /// imported unit point offset.
11852 ///
11853 /// The lower bound is the smallest point (the point with the smallest
11854 /// offset) which is the greater than a given offset.
11855 ///
11856 /// @param imported_unit_points_type the sorted vector of imported
11857 /// unit points.
11858 ///
11859 /// @param val the offset to consider when looking for the lower
11860 /// bound.
11861 ///
11862 /// @param r an iterator to the lower bound found. This parameter is
11863 /// set iff the function returns true.
11864 ///
11865 /// @return true iff the lower bound has been found.
11866 static bool
find_lower_bound_in_imported_unit_points(const imported_unit_points_type & p,Dwarf_Off val,imported_unit_points_type::const_iterator & r)11867 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
11868 Dwarf_Off val,
11869 imported_unit_points_type::const_iterator& r)
11870 {
11871 imported_unit_point v(val);
11872 imported_unit_points_type::const_iterator result =
11873 std::lower_bound(p.begin(), p.end(), v);
11874
11875 bool is_ok = result != p.end();
11876
11877 if (is_ok)
11878 r = result;
11879
11880 return is_ok;
11881 }
11882
11883 /// Given a DW_TAG_compile_unit, build and return the corresponding
11884 /// abigail::translation_unit ir node. Note that this function
11885 /// recursively reads the children dies of the current DIE and
11886 /// populates the resulting translation unit.
11887 ///
11888 /// @param rdr the DWARF reader to use.
11889 ///
11890 /// @param die the DW_TAG_compile_unit DIE to consider.
11891 ///
11892 /// @param address_size the size of the addresses expressed in this
11893 /// translation unit in general.
11894 ///
11895 /// @return a pointer to the resulting translation_unit.
11896 static translation_unit_sptr
build_translation_unit_and_add_to_ir(reader & rdr,Dwarf_Die * die,char address_size)11897 build_translation_unit_and_add_to_ir(reader& rdr,
11898 Dwarf_Die* die,
11899 char address_size)
11900 {
11901 translation_unit_sptr result;
11902
11903 if (!die)
11904 return result;
11905 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
11906
11907 // Clear the part of the context that is dependent on the translation
11908 // unit we are reading.
11909 rdr.clear_per_translation_unit_data();
11910
11911 rdr.cur_tu_die(die);
11912
11913 string path = die_string_attribute(die, DW_AT_name);
11914 if (path == "<artificial>")
11915 {
11916 // This is a file artificially generated by the compiler, so its
11917 // name is '<artificial>'. As we want all different translation
11918 // units to have unique path names, let's suffix this path name
11919 // with its die offset.
11920 std::ostringstream o;
11921 o << path << "-" << std::hex << dwarf_dieoffset(die);
11922 path = o.str();
11923 }
11924 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
11925
11926 // See if the same translation unit exits already in the current
11927 // corpus. Sometimes, the same translation unit can be present
11928 // several times in the same debug info. The content of the
11929 // different instances of the translation unit are different. So to
11930 // represent that, we are going to re-use the same translation
11931 // unit. That is, it's going to be the union of all the translation
11932 // units of the same path.
11933 {
11934 const string& abs_path =
11935 compilation_dir.empty() ? path : compilation_dir + "/" + path;
11936 result = rdr.corpus()->find_translation_unit(abs_path);
11937 }
11938
11939 if (!result)
11940 {
11941 result.reset(new translation_unit(rdr.env(),
11942 path,
11943 address_size));
11944 result->set_compilation_dir_path(compilation_dir);
11945 rdr.corpus()->add(result);
11946 uint64_t l = 0;
11947 die_unsigned_constant_attribute(die, DW_AT_language, l);
11948 result->set_language(dwarf_language_to_tu_language(l));
11949 }
11950
11951 rdr.cur_transl_unit(result);
11952 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
11953
11954 Dwarf_Die child;
11955 if (dwarf_child(die, &child) != 0)
11956 return result;
11957
11958 result->set_is_constructed(false);
11959
11960 do
11961 // Analyze all the DIEs we encounter unless we are asked to only
11962 // analyze exported interfaces and the types reachables from them.
11963 if (!rdr.env().analyze_exported_interfaces_only()
11964 || rdr.is_decl_die_with_exported_symbol(&child))
11965 build_ir_node_from_die(rdr, &child,
11966 die_is_public_decl(&child),
11967 dwarf_dieoffset(&child));
11968 while (dwarf_siblingof(&child, &child) == 0);
11969
11970 if (!rdr.var_decls_to_re_add_to_tree().empty())
11971 for (list<var_decl_sptr>::const_iterator v =
11972 rdr.var_decls_to_re_add_to_tree().begin();
11973 v != rdr.var_decls_to_re_add_to_tree().end();
11974 ++v)
11975 {
11976 if (is_member_decl(*v))
11977 continue;
11978
11979 ABG_ASSERT((*v)->get_scope());
11980 string demangled_name =
11981 demangle_cplus_mangled_name((*v)->get_linkage_name());
11982 if (!demangled_name.empty())
11983 {
11984 std::list<string> fqn_comps;
11985 fqn_to_components(demangled_name, fqn_comps);
11986 string mem_name = fqn_comps.back();
11987 fqn_comps.pop_back();
11988 class_decl_sptr class_type;
11989 string ty_name;
11990 if (!fqn_comps.empty())
11991 {
11992 ty_name = components_to_type_name(fqn_comps);
11993 class_type =
11994 lookup_class_type(ty_name, *rdr.cur_transl_unit());
11995 }
11996 if (class_type)
11997 {
11998 // So we are seeing a member variable for which there
11999 // is a global variable definition DIE not having a
12000 // reference attribute pointing back to the member
12001 // variable declaration DIE. Thus remove the global
12002 // variable definition from its current non-class
12003 // scope ...
12004 decl_base_sptr d;
12005 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12006 // This is the data member with the same name in cl.
12007 // We just need to flag it as static.
12008 ;
12009 else
12010 {
12011 // In this case there is no data member with the
12012 // same name in cl already. Let's add it there then
12013 // ...
12014 remove_decl_from_scope(*v);
12015 d = add_decl_to_scope(*v, class_type);
12016 }
12017
12018 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12019 // Let's flag the data member as static.
12020 set_member_is_static(d, true);
12021 }
12022 }
12023 }
12024 rdr.var_decls_to_re_add_to_tree().clear();
12025
12026 result->set_is_constructed(true);
12027
12028 return result;
12029 }
12030
12031 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12032 /// DW_TAG_module (for fortran) DIE.
12033 ///
12034 /// Note that this function connects the DW_TAG_namespace to the IR
12035 /// being currently created, reads the children of the DIE and
12036 /// connects them to the IR as well.
12037 ///
12038 /// @param rdr the DWARF reader to use.
12039 ///
12040 /// @param die the DIE to read from. Must be either DW_TAG_namespace
12041 /// or DW_TAG_module.
12042 ///
12043 /// @param where_offset the offset of the DIE where we are "logically"
12044 /// positionned at, in the DIE tree. This is useful when @p die is
12045 /// e.g, DW_TAG_partial_unit that can be included in several places in
12046 /// the DIE tree.
12047 ///
12048 /// @return the resulting @ref abigail::namespace_decl or NULL if it
12049 /// couldn't be created.
12050 static namespace_decl_sptr
build_namespace_decl_and_add_to_ir(reader & rdr,Dwarf_Die * die,size_t where_offset)12051 build_namespace_decl_and_add_to_ir(reader& rdr,
12052 Dwarf_Die* die,
12053 size_t where_offset)
12054 {
12055 namespace_decl_sptr result;
12056
12057 if (!die)
12058 return result;
12059
12060 unsigned tag = dwarf_tag(die);
12061 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12062 return result;
12063
12064 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12065 /*called_for_public_decl=*/false,
12066 where_offset);
12067
12068 string name, linkage_name;
12069 location loc;
12070 die_loc_and_name(rdr, die, loc, name, linkage_name);
12071
12072 result.reset(new namespace_decl(rdr.env(), name, loc));
12073 add_decl_to_scope(result, scope.get());
12074 rdr.associate_die_to_decl(die, result, where_offset);
12075
12076 Dwarf_Die child;
12077 if (dwarf_child(die, &child) != 0)
12078 return result;
12079
12080 rdr.scope_stack().push(result.get());
12081 do
12082 build_ir_node_from_die(rdr, &child,
12083 // If this namespace DIE is private
12084 // (anonymous) then all its content is
12085 // considered private. Otherwise, its
12086 // public decls are considered public.
12087 /*called_from_public_decl=*/
12088 die_is_public_decl(die) && die_is_public_decl(&child),
12089 where_offset);
12090 while (dwarf_siblingof(&child, &child) == 0);
12091 rdr.scope_stack().pop();
12092
12093 return result;
12094 }
12095
12096 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12097 ///
12098 /// @param rdr the DWARF reader to use.
12099 ///
12100 /// @param die the DW_TAG_base_type to consider.
12101 ///
12102 /// @param where_offset where we are logically at in the DIE stream.
12103 ///
12104 /// @return the resulting decl_base_sptr.
12105 static type_decl_sptr
build_type_decl(reader & rdr,Dwarf_Die * die,size_t where_offset)12106 build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12107 {
12108 type_decl_sptr result;
12109
12110 if (!die)
12111 return result;
12112 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12113
12114 uint64_t byte_size = 0, bit_size = 0;
12115 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12116 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12117 return result;
12118
12119 if (bit_size == 0 && byte_size != 0)
12120 // Update the bit size.
12121 bit_size = byte_size * 8;
12122
12123 string type_name, linkage_name;
12124 location loc;
12125 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12126
12127 if (byte_size == 0)
12128 {
12129 // The size of the type is zero, that must mean that we are
12130 // looking at the definition of the void type.
12131 if (type_name == "void")
12132 result = is_type_decl(build_ir_node_for_void_type(rdr));
12133 else
12134 // A type of size zero that is not void? Hmmh, I am not sure
12135 // what that means. Return nil for now.
12136 return result;
12137 }
12138
12139 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12140 {
12141 string normalized_type_name = type_name;
12142 integral_type int_type;
12143 if (parse_integral_type(type_name, int_type))
12144 normalized_type_name = int_type.to_string();
12145 result = lookup_basic_type(normalized_type_name, *corp);
12146 }
12147
12148 if (!result)
12149 if (corpus_sptr corp = rdr.corpus())
12150 result = lookup_basic_type(type_name, *corp);
12151 if (!result)
12152 result.reset(new type_decl(rdr.env(), type_name, bit_size,
12153 /*alignment=*/0, loc, linkage_name));
12154 rdr.associate_die_to_type(die, result, where_offset);
12155 return result;
12156 }
12157
12158 /// Construct the type that is to be used as the underlying type of an
12159 /// enum.
12160 ///
12161 /// @param rdr the DWARF reader to use.
12162 ///
12163 /// @param enum_name the name of the enum that this type is going to
12164 /// be the underlying type of.
12165 ///
12166 /// @param enum_size the size of the enum.
12167 ///
12168 /// @param is_anonymous whether the underlying type is anonymous or
12169 /// not. By default, this should be set to true as before c++11 (and
12170 /// in C), it's almost the case.
12171 static type_decl_sptr
build_enum_underlying_type(reader & rdr,string enum_name,uint64_t enum_size,bool is_anonymous=true)12172 build_enum_underlying_type(reader& rdr,
12173 string enum_name,
12174 uint64_t enum_size,
12175 bool is_anonymous = true)
12176 {
12177 string underlying_type_name =
12178 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12179 enum_size);
12180
12181 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12182 enum_size, enum_size, location()));
12183 result->set_is_anonymous(is_anonymous);
12184 result->set_is_artificial(true);
12185 translation_unit_sptr tu = rdr.cur_transl_unit();
12186 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12187 result = dynamic_pointer_cast<type_decl>(d);
12188 ABG_ASSERT(result);
12189 canonicalize(result);
12190 return result;
12191 }
12192
12193 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12194 ///
12195 /// @param rdr the DWARF reader to use.
12196 ///
12197 /// @param die the DIE to read from.
12198 ///
12199 /// @param scope the scope of the final enum. Note that this function
12200 /// does *NOT* add the built type to this scope. The scope is just so
12201 /// that the function knows how to name anonymous enums.
12202 ///
12203 /// @param is_declaration_only is true if the DIE denoted by @p die is
12204 /// a declaration-only DIE.
12205 ///
12206 /// @return the built enum_type_decl or NULL if it could not be built.
12207 static enum_type_decl_sptr
build_enum_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,size_t where_offset,bool is_declaration_only)12208 build_enum_type(reader& rdr,
12209 Dwarf_Die* die,
12210 scope_decl* scope,
12211 size_t where_offset,
12212 bool is_declaration_only)
12213 {
12214 enum_type_decl_sptr result;
12215 if (!die)
12216 return result;
12217
12218 unsigned tag = dwarf_tag(die);
12219 if (tag != DW_TAG_enumeration_type)
12220 return result;
12221
12222 string name, linkage_name;
12223 location loc;
12224 die_loc_and_name(rdr, die, loc, name, linkage_name);
12225
12226 bool is_anonymous = false;
12227 // If the enum is anonymous, let's give it a name.
12228 if (name.empty())
12229 {
12230 name = get_internal_anonymous_die_prefix_name(die);
12231 ABG_ASSERT(!name.empty());
12232 // But we remember that the type is anonymous.
12233 is_anonymous = true;
12234
12235 if (size_t s = scope->get_num_anonymous_member_enums())
12236 name = build_internal_anonymous_die_name(name, s);
12237 }
12238
12239 bool use_odr = rdr.odr_is_relevant(die);
12240 // If the type has location, then associate it to its
12241 // representation. This way, all occurences of types with the same
12242 // representation (name) and location can be later detected as being
12243 // for the same type.
12244
12245 if (!is_anonymous)
12246 {
12247 if (use_odr)
12248 {
12249 if (enum_type_decl_sptr pre_existing_enum =
12250 is_enum_type(rdr.lookup_artifact_from_die(die)))
12251 result = pre_existing_enum;
12252 }
12253 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12254 {
12255 if (loc)
12256 result = lookup_enum_type_per_location(loc.expand(), *corp);
12257 }
12258 else if (loc)
12259 {
12260 if (enum_type_decl_sptr pre_existing_enum =
12261 is_enum_type(rdr.lookup_artifact_from_die(die)))
12262 if (pre_existing_enum->get_location() == loc)
12263 result = pre_existing_enum;
12264 }
12265
12266 if (result)
12267 {
12268 rdr.associate_die_to_type(die, result, where_offset);
12269 return result;
12270 }
12271 }
12272 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12273 // we can look them up?
12274
12275 uint64_t size = 0;
12276 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12277 size *= 8;
12278 bool is_artificial = die_is_artificial(die);
12279
12280 // for now we consider that underlying types of enums are all anonymous
12281 bool enum_underlying_type_is_anonymous= true;
12282
12283 enum_type_decl::enumerators enms;
12284 Dwarf_Die child;
12285 if (dwarf_child(die, &child) == 0)
12286 {
12287 do
12288 {
12289 if (dwarf_tag(&child) != DW_TAG_enumerator)
12290 continue;
12291
12292 string n, m;
12293 location l;
12294 die_loc_and_name(rdr, &child, l, n, m);
12295 uint64_t val = 0;
12296 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12297 enms.push_back(enum_type_decl::enumerator(n, val));
12298 }
12299 while (dwarf_siblingof(&child, &child) == 0);
12300 }
12301
12302 // DWARF up to version 4 (at least) doesn't seem to carry the
12303 // underlying type, so let's create an artificial one here, which
12304 // sole purpose is to be passed to the constructor of the
12305 // enum_type_decl type.
12306 type_decl_sptr t =
12307 build_enum_underlying_type(rdr, name, size,
12308 enum_underlying_type_is_anonymous);
12309 t->set_is_declaration_only(is_declaration_only);
12310
12311 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12312 result->set_is_anonymous(is_anonymous);
12313 result->set_is_declaration_only(is_declaration_only);
12314 result->set_is_artificial(is_artificial);
12315 rdr.associate_die_to_type(die, result, where_offset);
12316
12317 rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12318
12319 return result;
12320 }
12321
12322 /// Once a function_decl has been built and added to a class as a
12323 /// member function, this function updates the information of the
12324 /// function_decl concerning the properties of its relationship with
12325 /// the member class. That is, it updates properties like
12326 /// virtualness, access, constness, cdtorness, etc ...
12327 ///
12328 /// @param die the DIE of the function_decl that has been just built.
12329 ///
12330 /// @param f the function_decl that has just been built from @p die.
12331 ///
12332 /// @param klass the @ref class_or_union that @p f belongs to.
12333 ///
12334 /// @param rdr the context used to read the ELF/DWARF information.
12335 static void
finish_member_function_reading(Dwarf_Die * die,const function_decl_sptr & f,const class_or_union_sptr klass,reader & rdr)12336 finish_member_function_reading(Dwarf_Die* die,
12337 const function_decl_sptr& f,
12338 const class_or_union_sptr klass,
12339 reader& rdr)
12340 {
12341 ABG_ASSERT(klass);
12342
12343 method_decl_sptr m = is_method_decl(f);
12344 ABG_ASSERT(m);
12345
12346 method_type_sptr method_t = is_method_type(m->get_type());
12347 ABG_ASSERT(method_t);
12348
12349 bool is_ctor = (f->get_name() == klass->get_name());
12350 bool is_dtor = (!f->get_name().empty()
12351 && static_cast<string>(f->get_name())[0] == '~');
12352 bool is_virtual = die_is_virtual(die);
12353 int64_t vindex = -1;
12354 if (is_virtual)
12355 die_virtual_function_index(die, vindex);
12356 access_specifier access = public_access;
12357 if (class_decl_sptr c = is_class_type(klass))
12358 if (!c->is_struct())
12359 access = private_access;
12360 die_access_specifier(die, access);
12361
12362 bool is_static = false;
12363 {
12364 // Let's see if the first parameter is a pointer to an instance of
12365 // the same class type as the current class and has a
12366 // DW_AT_artificial attribute flag set. We are not looking at
12367 // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12368 // emitted in GCC 4_4, which was already DWARF 3.
12369 function_decl::parameter_sptr first_parm;
12370 if (!f->get_parameters().empty())
12371 first_parm = f->get_parameters()[0];
12372
12373 bool is_artificial = first_parm && first_parm->get_is_artificial();
12374 type_base_sptr this_ptr_type, other_klass;
12375
12376 if (is_artificial)
12377 this_ptr_type = first_parm->get_type();
12378
12379 // Sometimes, the type of the "this" pointer is "const class_type* const".
12380 //
12381 // Meaning that the "this pointer" itself is const qualified. So
12382 // let's get the underlying underlying non-qualified pointer.
12383 if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12384 this_ptr_type = q->get_underlying_type();
12385
12386 // Now, get the pointed-to type.
12387 if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12388 other_klass = p->get_pointed_to_type();
12389
12390 // Sometimes, other_klass can be qualified; e.g, volatile. In
12391 // that case, let's get the unqualified version of other_klass.
12392 if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12393 other_klass = q->get_underlying_type();
12394
12395 if (other_klass
12396 && get_type_name(other_klass) == klass->get_qualified_name())
12397 ;
12398 else
12399 is_static = true;
12400
12401 if (is_static)
12402 {
12403 // If we are looking at a DWARF version that is high enough
12404 // for the DW_AT_object_pointer attribute to be present, let's
12405 // see if it's present. If it is, then the current member
12406 // function is not static.
12407 Dwarf_Die object_pointer_die;
12408 if (die_has_object_pointer(die, object_pointer_die))
12409 is_static = false;
12410 }
12411 }
12412 set_member_access_specifier(m, access);
12413 if (vindex != -1)
12414 set_member_function_vtable_offset(m, vindex);
12415 if (is_virtual)
12416 set_member_function_is_virtual(m, is_virtual);
12417 set_member_is_static(m, is_static);
12418 set_member_function_is_ctor(m, is_ctor);
12419 set_member_function_is_dtor(m, is_dtor);
12420 set_member_function_is_const(m, method_t->get_is_const());
12421
12422 ABG_ASSERT(is_member_function(m));
12423
12424 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12425 {
12426 // This is a virtual member function which has a linkage name
12427 // but has no underlying symbol set.
12428 //
12429 // The underlying elf symbol to set to this function can show up
12430 // later in the DWARF input or it can be that, because of some
12431 // compiler optimization, the relation between this function and
12432 // its underlying elf symbol is simply not emitted in the DWARF.
12433 //
12434 // Let's thus schedule this function for a later fixup pass
12435 // (performed by
12436 // reader::fixup_functions_with_no_symbols()) that will
12437 // set its underlying symbol.
12438 //
12439 // Note that if the underying symbol is encountered later in the
12440 // DWARF input, then the part of build_function_decl() that
12441 // updates the function to set its underlying symbol will
12442 // de-schedule this function wrt fixup pass.
12443 Dwarf_Off die_offset = dwarf_dieoffset(die);
12444 die_function_decl_map_type &fns_with_no_symbol =
12445 rdr.die_function_decl_with_no_symbol_map();
12446 die_function_decl_map_type::const_iterator i =
12447 fns_with_no_symbol.find(die_offset);
12448 if (i == fns_with_no_symbol.end())
12449 fns_with_no_symbol[die_offset] = f;
12450 }
12451
12452 }
12453
12454 /// If a function DIE has attributes which have not yet been read and
12455 /// added to the internal representation that represents that function
12456 /// then read those extra attributes and update the internal
12457 /// representation.
12458 ///
12459 /// @param rdr the DWARF reader to use.
12460 ///
12461 /// @param die the function DIE to consider.
12462 ///
12463 /// @param where_offset where we logical are, currently, in the stream
12464 /// of DIEs. If you don't know what this is, you can just set it to zero.
12465 ///
12466 /// @param existing_fn the representation of the function to update.
12467 ///
12468 /// @return the updated function representation.
12469 static function_decl_sptr
maybe_finish_function_decl_reading(reader & rdr,Dwarf_Die * die,size_t where_offset,const function_decl_sptr & existing_fn)12470 maybe_finish_function_decl_reading(reader& rdr,
12471 Dwarf_Die* die,
12472 size_t where_offset,
12473 const function_decl_sptr& existing_fn)
12474 {
12475 function_decl_sptr result = build_function_decl(rdr, die,
12476 where_offset,
12477 existing_fn);
12478
12479 return result;
12480 }
12481
12482 /// Lookup a class or a typedef with a given qualified name in the
12483 /// corpus that a given scope belongs to.
12484 ///
12485 /// @param scope the scope to consider.
12486 ///
12487 /// @param type_name the qualified name of the type to look for.
12488 ///
12489 /// @return the typedef or class type found.
12490 static type_base_sptr
lookup_class_or_typedef_from_corpus(scope_decl * scope,const string & type_name)12491 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12492 {
12493 string qname = build_qualified_name(scope, type_name);
12494 corpus* corp = scope->get_corpus();
12495 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12496 return result;
12497 }
12498
12499 /// Lookup a class of typedef type from the current corpus being
12500 /// constructed.
12501 ///
12502 /// The type being looked for has the same name as a given DIE.
12503 ///
12504 /// @param rdr the DWARF reader to use.
12505 ///
12506 /// @param die the DIE which has the same name as the type we are
12507 /// looking for.
12508 ///
12509 /// @param called_for_public_decl whether this function is being
12510 /// called from a a publicly defined declaration.
12511 ///
12512 /// @param where_offset where we are logically at in the DIE stream.
12513 ///
12514 /// @return the type found.
12515 static type_base_sptr
lookup_class_or_typedef_from_corpus(reader & rdr,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)12516 lookup_class_or_typedef_from_corpus(reader& rdr,
12517 Dwarf_Die* die,
12518 bool called_for_public_decl,
12519 size_t where_offset)
12520 {
12521 if (!die)
12522 return class_decl_sptr();
12523
12524 string class_name = die_string_attribute(die, DW_AT_name);
12525 if (class_name.empty())
12526 return class_decl_sptr();
12527
12528 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12529 called_for_public_decl,
12530 where_offset);
12531 if (scope)
12532 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12533
12534 return type_base_sptr();
12535 }
12536
12537 /// Lookup a class, typedef or enum type with a given qualified name
12538 /// in the corpus that a given scope belongs to.
12539 ///
12540 /// @param scope the scope to consider.
12541 ///
12542 /// @param type_name the qualified name of the type to look for.
12543 ///
12544 /// @return the typedef, enum or class type found.
12545 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(scope_decl * scope,const string & type_name)12546 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12547 const string& type_name)
12548 {
12549 string qname = build_qualified_name(scope, type_name);
12550 corpus* corp = scope->get_corpus();
12551 type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12552 return result;
12553 }
12554
12555 /// Lookup a class, typedef or enum type in a given scope, in the
12556 /// corpus that scope belongs to.
12557 ///
12558 /// @param die the DIE of the class, typedef or enum to lookup.
12559 ///
12560 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
12561 /// type, this is the index of that anonymous type in its scope, in
12562 /// case there are several anonymous types of the same kind in that
12563 /// scope.
12564 ///
12565 /// @param scope the scope in which to look the type for.
12566 ///
12567 /// @return the typedef, enum or class type found.
12568 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die * die,size_t anonymous_member_type_idx,scope_decl * scope)12569 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12570 size_t anonymous_member_type_idx,
12571 scope_decl* scope)
12572 {
12573 if (!die)
12574 return class_decl_sptr();
12575
12576 string type_name = die_string_attribute(die, DW_AT_name);
12577 if (is_anonymous_type_die(die))
12578 type_name =
12579 get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12580
12581 if (type_name.empty())
12582 return class_decl_sptr();
12583
12584 return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12585 }
12586
12587
12588 /// Test if a DIE represents a function that is a member of a given
12589 /// class type.
12590 ///
12591 /// @param rdr the DWARF reader.
12592 ///
12593 /// @param function_die the DIE of the function to consider.
12594 ///
12595 /// @param class_type the class type to consider.
12596 ///
12597 /// @param where_offset where we are logically at in the DIE stream.
12598 ///
12599 /// @return the method declaration corresponding to the member
12600 /// function of @p class_type, iff @p function_die is for a member
12601 /// function of @p class_type.
12602 static method_decl_sptr
is_function_for_die_a_member_of_class(reader & rdr,Dwarf_Die * function_die,const class_or_union_sptr & class_type)12603 is_function_for_die_a_member_of_class(reader& rdr,
12604 Dwarf_Die* function_die,
12605 const class_or_union_sptr& class_type)
12606 {
12607 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
12608
12609 if (!artifact)
12610 return method_decl_sptr();
12611
12612 method_decl_sptr method = is_method_decl(artifact);
12613 method_type_sptr method_type;
12614
12615 if (method)
12616 method_type = method->get_type();
12617 else
12618 method_type = is_method_type(artifact);
12619 ABG_ASSERT(method_type);
12620
12621 class_or_union_sptr method_class = method_type->get_class_type();
12622 ABG_ASSERT(method_class);
12623
12624 string method_class_name = method_class->get_qualified_name(),
12625 class_type_name = class_type->get_qualified_name();
12626
12627 if (method_class_name == class_type_name)
12628 {
12629 //ABG_ASSERT(class_type.get() == method_class.get());
12630 return method;
12631 }
12632
12633 return method_decl_sptr();
12634 }
12635
12636 /// If a given function DIE represents an existing member function of
12637 /// a given class, then update that member function with new
12638 /// properties present in the DIE. Otherwise, if the DIE represents a
12639 /// new member function that is not already present in the class then
12640 /// add that new member function to the class.
12641 ///
12642 /// @param rdr the DWARF reader.
12643 ///
12644 /// @param function_die the DIE of the potential member function to
12645 /// consider.
12646 ///
12647 /// @param class_type the class type to consider.
12648 ///
12649 /// @param called_from_public_decl is true iff this function was
12650 /// called from a publicly defined and exported declaration.
12651 ///
12652 /// @param where_offset where we are logically at in the DIE stream.
12653 ///
12654 /// @return the method decl representing the member function.
12655 static method_decl_sptr
add_or_update_member_function(reader & rdr,Dwarf_Die * function_die,const class_or_union_sptr & class_type,bool called_from_public_decl,size_t where_offset)12656 add_or_update_member_function(reader& rdr,
12657 Dwarf_Die* function_die,
12658 const class_or_union_sptr& class_type,
12659 bool called_from_public_decl,
12660 size_t where_offset)
12661 {
12662 method_decl_sptr method =
12663 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
12664
12665 if (!method)
12666 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
12667 class_type.get(),
12668 called_from_public_decl,
12669 where_offset));
12670 if (!method)
12671 return method_decl_sptr();
12672
12673 finish_member_function_reading(function_die,
12674 is_function_decl(method),
12675 class_type, rdr);
12676 return method;
12677 }
12678
12679 /// Build a an IR node for class type from a DW_TAG_structure_type or
12680 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
12681 /// currently built.
12682 ///
12683 /// If the represents class type that already exists, then update the
12684 /// existing class type with the new properties found in the DIE.
12685 ///
12686 /// It meanst that this function can also update an existing
12687 /// class_decl node with data members, member functions and other
12688 /// properties coming from the DIE.
12689 ///
12690 /// @param rdr the DWARF reader to consider.
12691 ///
12692 /// @param die the DIE to read information from. Must be either a
12693 /// DW_TAG_structure_type or a DW_TAG_class_type.
12694 ///
12695 /// @param scope a pointer to the scope_decl* under which this class
12696 /// is to be added to.
12697 ///
12698 /// @param is_struct whether the class was declared as a struct.
12699 ///
12700 /// @param klass if non-null, this is a klass to append the members
12701 /// to. Otherwise, this function just builds the class from scratch.
12702 ///
12703 /// @param called_from_public_decl set to true if this class is being
12704 /// called from a "Public declaration like vars or public symbols".
12705 ///
12706 /// @param where_offset the offset of the DIE where we are "logically"
12707 /// positionned at, in the DIE tree. This is useful when @p die is
12708 /// e.g, DW_TAG_partial_unit that can be included in several places in
12709 /// the DIE tree.
12710 ///
12711 /// @param is_declaration_only is true if the DIE denoted by @p die is
12712 /// a declaration-only DIE.
12713 ///
12714 /// @return the resulting class_type.
12715 static class_decl_sptr
add_or_update_class_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,bool is_struct,class_decl_sptr klass,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)12716 add_or_update_class_type(reader& rdr,
12717 Dwarf_Die* die,
12718 scope_decl* scope,
12719 bool is_struct,
12720 class_decl_sptr klass,
12721 bool called_from_public_decl,
12722 size_t where_offset,
12723 bool is_declaration_only)
12724 {
12725 class_decl_sptr result;
12726 if (!die)
12727 return result;
12728
12729 const die_source source = rdr.get_die_source(die);
12730
12731 unsigned tag = dwarf_tag(die);
12732
12733 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
12734 return result;
12735
12736 {
12737 die_class_or_union_map_type::const_iterator i =
12738 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12739 if (i != rdr.die_wip_classes_map(source).end())
12740 {
12741 class_decl_sptr class_type = is_class_type(i->second);
12742 ABG_ASSERT(class_type);
12743 return class_type;
12744 }
12745 }
12746
12747 string name, linkage_name;
12748 location loc;
12749 die_loc_and_name(rdr, die, loc, name, linkage_name);
12750
12751 bool is_anonymous = false;
12752 if (name.empty())
12753 {
12754 // So we are looking at an anonymous struct. Let's
12755 // give it a name.
12756 name = get_internal_anonymous_die_prefix_name(die);
12757 ABG_ASSERT(!name.empty());
12758 // But we remember that the type is anonymous.
12759 is_anonymous = true;
12760
12761 if (size_t s = scope->get_num_anonymous_member_classes())
12762 name = build_internal_anonymous_die_name(name, s);
12763 }
12764
12765 if (!is_anonymous)
12766 {
12767 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12768 {
12769 if (loc)
12770 // TODO: if there is only one class defined in the corpus
12771 // for this location, then re-use it. But if there are
12772 // more than one, then do not re-use it, for now.
12773 result = lookup_class_type_per_location(loc.expand(), *corp);
12774 else
12775 // TODO: if there is just one class for that name defined,
12776 // then re-use it. Otherwise, don't.
12777 result = lookup_class_type(name, *corp);
12778 if (result
12779 // If we are seeing a declaration of a definition we
12780 // already had, or if we are seing a type with the same
12781 // declaration-only-ness that we had before, then keep
12782 // the one we already had.
12783 && (result->get_is_declaration_only() == is_declaration_only
12784 || (!result->get_is_declaration_only()
12785 && is_declaration_only)))
12786 {
12787 rdr.associate_die_to_type(die, result, where_offset);
12788 return result;
12789 }
12790 else
12791 // We might be seeing the definition of a declaration we
12792 // already had. In that case, keep the definition and
12793 // drop the declaration.
12794 result.reset();
12795 }
12796 }
12797
12798 // If we've already seen the same class as 'die', then let's re-use
12799 // that one, unless it's an anonymous class. We can't really safely
12800 // re-use anonymous classes as they have no name, by construction.
12801 // What we can do, rather, is to reuse the typedef that name them,
12802 // when they do have a naming typedef.
12803 if (!is_anonymous)
12804 if (class_decl_sptr pre_existing_class =
12805 is_class_type(rdr.lookup_type_artifact_from_die(die)))
12806 klass = pre_existing_class;
12807
12808 uint64_t size = 0;
12809 die_size_in_bits(die, size);
12810 bool is_artificial = die_is_artificial(die);
12811
12812 Dwarf_Die child;
12813 bool has_child = (dwarf_child(die, &child) == 0);
12814
12815 decl_base_sptr res;
12816 if (klass)
12817 {
12818 res = result = klass;
12819 if (has_child && klass->get_is_declaration_only()
12820 && klass->get_definition_of_declaration())
12821 res = result = is_class_type(klass->get_definition_of_declaration());
12822 if (loc)
12823 result->set_location(loc);
12824 }
12825 else
12826 {
12827 result.reset(new class_decl(rdr.env(), name, size,
12828 /*alignment=*/0, is_struct, loc,
12829 decl_base::VISIBILITY_DEFAULT,
12830 is_anonymous));
12831
12832 result->set_is_declaration_only(is_declaration_only);
12833
12834 res = add_decl_to_scope(result, scope);
12835 result = dynamic_pointer_cast<class_decl>(res);
12836 ABG_ASSERT(result);
12837 }
12838
12839 if (!klass || klass->get_is_declaration_only())
12840 if (size != result->get_size_in_bits())
12841 result->set_size_in_bits(size);
12842
12843 if (klass)
12844 // We are amending a class that was built before. So let's check
12845 // if we need to amend its "declaration-only-ness" status.
12846 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
12847 // The size of the class doesn't match its
12848 // 'declaration-only-ness". We might have a non-zero sized
12849 // class which is declaration-only, or a zero sized class that
12850 // is not declaration-only. Let's set the declaration-only-ness
12851 // according to what we are instructed to.
12852 //
12853 // Note however that there are binaries out there emitted by
12854 // compilers (Clang, in C++) emit declarations-only classes that
12855 // have non-zero size. So we must honor these too. That is why
12856 // we are not forcing the declaration-only-ness to false when a
12857 // class has non-zero size. An example of such binary is
12858 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
12859 result->set_is_declaration_only(is_declaration_only);
12860
12861 // If a non-decl-only class has children node and is advertized as
12862 // having a non-zero size let's trust that.
12863 if (!result->get_is_declaration_only() && has_child)
12864 if (result->get_size_in_bits() == 0 && size != 0)
12865 result->set_size_in_bits(size);
12866
12867 result->set_is_artificial(is_artificial);
12868
12869 rdr.associate_die_to_type(die, result, where_offset);
12870
12871 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
12872
12873 if (!has_child)
12874 // TODO: set the access specifier for the declaration-only class
12875 // here.
12876 return result;
12877
12878 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
12879
12880 bool is_incomplete_type = false;
12881 if (is_declaration_only && size == 0 && has_child)
12882 // this is an incomplete DWARF type as defined by [5.7.1]
12883 //
12884 // An incomplete structure, union or class type is represented by
12885 // a structure, union or class entry that does not have a byte
12886 // size attribute and that has a DW_AT_declaration attribute.
12887 //
12888 // Let's consider that it's thus a decl-only class, likely
12889 // referred to by a pointer. If we later encounter a definition
12890 // for this decl-only class type, then this decl-only class will
12891 // be resolved to it by the code in
12892 // reader::resolve_declaration_only_classes.
12893 is_incomplete_type = true;
12894
12895 scope_decl_sptr scop =
12896 dynamic_pointer_cast<scope_decl>(res);
12897 ABG_ASSERT(scop);
12898 rdr.scope_stack().push(scop.get());
12899
12900 if (has_child && !is_incomplete_type)
12901 {
12902 int anonymous_member_class_index = -1;
12903 int anonymous_member_union_index = -1;
12904 int anonymous_member_enum_index = -1;
12905
12906 do
12907 {
12908 tag = dwarf_tag(&child);
12909
12910 // Handle base classes.
12911 if (tag == DW_TAG_inheritance)
12912 {
12913 result->set_is_declaration_only(false);
12914
12915 Dwarf_Die type_die;
12916 if (!die_die_attribute(&child, DW_AT_type, type_die))
12917 continue;
12918
12919 type_base_sptr base_type;
12920 if (!(base_type =
12921 lookup_class_or_typedef_from_corpus(rdr, &type_die,
12922 called_from_public_decl,
12923 where_offset)))
12924 {
12925 base_type =
12926 is_type(build_ir_node_from_die(rdr, &type_die,
12927 called_from_public_decl,
12928 where_offset));
12929 }
12930 // Sometimes base_type can be a typedef. Let's make
12931 // sure that typedef is compatible with a class type.
12932 class_decl_sptr b = is_compatible_with_class_type(base_type);
12933 if (!b)
12934 continue;
12935
12936 access_specifier access =
12937 is_struct
12938 ? public_access
12939 : private_access;
12940
12941 die_access_specifier(&child, access);
12942
12943 bool is_virt= die_is_virtual(&child);
12944 int64_t offset = 0;
12945 bool is_offset_present =
12946 die_member_offset(rdr, &child, offset);
12947
12948 class_decl::base_spec_sptr base(new class_decl::base_spec
12949 (b, access,
12950 is_offset_present ? offset : -1,
12951 is_virt));
12952 if (b->get_is_declaration_only())
12953 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
12954 if (result->find_base_class(b->get_qualified_name()))
12955 continue;
12956 result->add_base_specifier(base);
12957 }
12958 // Handle data members.
12959 else if (tag == DW_TAG_member
12960 || tag == DW_TAG_variable)
12961 {
12962 Dwarf_Die type_die;
12963 if (!die_die_attribute(&child, DW_AT_type, type_die))
12964 continue;
12965
12966 string n, m;
12967 location loc;
12968 die_loc_and_name(rdr, &child, loc, n, m);
12969 /// For now, we skip the hidden vtable pointer.
12970 /// Currently, we're looking for a member starting with
12971 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
12972 /// use as a name for the hidden vtable pointer.
12973 if (n.substr(0, 5) == "_vptr"
12974 && !std::isalnum(n.at(5))
12975 && n.at(5) != '_')
12976 continue;
12977
12978 // If the variable is already a member of this class,
12979 // move on. If it's an anonymous data member, we need
12980 // to handle it differently. We'll do that later below.
12981 if (!n.empty() && lookup_var_decl_in_scope(n, result))
12982 continue;
12983
12984 int64_t offset_in_bits = 0;
12985 bool is_laid_out = die_member_offset(rdr, &child,
12986 offset_in_bits);
12987 // For now, is_static == !is_laid_out. When we have
12988 // templates, we'll try to be more specific. For now,
12989 // this approximation should do OK.
12990 bool is_static = !is_laid_out;
12991
12992 if (is_static && variable_is_suppressed(rdr,
12993 result.get(),
12994 &child))
12995 continue;
12996
12997 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
12998 called_from_public_decl,
12999 where_offset));
13000 type_base_sptr t = is_type(ty);
13001 if (!t)
13002 continue;
13003
13004 // The call to build_ir_node_from_die above could have
13005 // triggered the adding of a data member named 'n' into
13006 // result. So let's check again if the variable is
13007 // already a member of this class. Here again, if it's
13008 // an anonymous data member, we need to handle it
13009 // differently. We'll do that later below.
13010 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13011 continue;
13012
13013 if (!is_static)
13014 // We have a non-static data member. So this class
13015 // cannot be a declaration-only class anymore, even if
13016 // some DWARF emitters might consider it otherwise.
13017 result->set_is_declaration_only(false);
13018 access_specifier access =
13019 is_struct
13020 ? public_access
13021 : private_access;
13022
13023 die_access_specifier(&child, access);
13024
13025 var_decl_sptr dm(new var_decl(n, t, loc, m));
13026 if (n.empty() && result->find_data_member(dm))
13027 // dm is an anonymous data member that was already
13028 // present in the current class so let's not add it.
13029 continue;
13030 result->add_data_member(dm, access, is_laid_out,
13031 is_static, offset_in_bits);
13032 ABG_ASSERT(has_scope(dm));
13033 rdr.associate_die_to_decl(&child, dm, where_offset,
13034 /*associate_by_repr=*/false);
13035 }
13036 // Handle member functions;
13037 else if (tag == DW_TAG_subprogram)
13038 {
13039 decl_base_sptr r =
13040 add_or_update_member_function(rdr, &child, result,
13041 called_from_public_decl,
13042 where_offset);
13043 if (function_decl_sptr f = is_function_decl(r))
13044 rdr.associate_die_to_decl(&child, f, where_offset,
13045 /*associate_by_repr=*/true);
13046 }
13047 // Handle member types
13048 else if (die_is_type(&child))
13049 {
13050 // Track the anonymous type index in the current
13051 // scope. Look for what this means by reading the
13052 // comment of the function
13053 // build_internal_anonymous_die_name.
13054 int anonymous_member_type_index = 0;
13055 if (is_anonymous_type_die(&child))
13056 {
13057 // Update the anonymous type index.
13058 if (die_is_class_type(&child))
13059 anonymous_member_type_index =
13060 ++anonymous_member_class_index;
13061 else if (dwarf_tag(&child) == DW_TAG_union_type)
13062 anonymous_member_type_index =
13063 ++anonymous_member_union_index;
13064 else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13065 anonymous_member_type_index =
13066 ++anonymous_member_enum_index;
13067 }
13068 // if the type is not already a member of this class,
13069 // then add it to the class.
13070 if ((is_anonymous_type_die(&child)
13071 && !lookup_class_typedef_or_enum_type_from_corpus
13072 (&child, anonymous_member_type_index, result.get()))
13073 || !result->find_member_type(die_name(&child)))
13074 build_ir_node_from_die(rdr, &child, result.get(),
13075 called_from_public_decl,
13076 where_offset);
13077 }
13078 } while (dwarf_siblingof(&child, &child) == 0);
13079 }
13080
13081 rdr.scope_stack().pop();
13082
13083 {
13084 die_class_or_union_map_type::const_iterator i =
13085 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13086 if (i != rdr.die_wip_classes_map(source).end())
13087 {
13088 if (is_member_type(i->second))
13089 set_member_access_specifier(res,
13090 get_member_access_specifier(i->second));
13091 rdr.die_wip_classes_map(source).erase(i);
13092 }
13093 }
13094
13095 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13096 return result;
13097 }
13098
13099 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
13100 ///
13101 /// @param rdr the DWARF reader to use.
13102 ///
13103 /// @param die the DIE to read from.
13104 ///
13105 /// @param scope the scope the resulting @ref union_decl belongs to.
13106 ///
13107 /// @param union_type if this parameter is non-nil, then this function
13108 /// updates the @ref union_decl that it points to, rather than
13109 /// creating a new @ref union_decl.
13110 ///
13111 /// @param called_from_public_decl is true if this function has been
13112 /// initially called within the context of a public decl.
13113 ///
13114 /// @param where_offset the offset of the DIE where we are "logically"
13115 /// positionned at, in the DIE tree. This is useful when @p die is
13116 /// e.g, DW_TAG_partial_unit that can be included in several places in
13117 /// the DIE tree.
13118 ///
13119 /// @param is_declaration_only is true if the DIE denoted by @p die is
13120 /// a declaration-only DIE.
13121 ///
13122 /// @return the resulting @ref union_decl type.
13123 static union_decl_sptr
add_or_update_union_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,union_decl_sptr union_type,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)13124 add_or_update_union_type(reader& rdr,
13125 Dwarf_Die* die,
13126 scope_decl* scope,
13127 union_decl_sptr union_type,
13128 bool called_from_public_decl,
13129 size_t where_offset,
13130 bool is_declaration_only)
13131 {
13132 union_decl_sptr result;
13133 if (!die)
13134 return result;
13135
13136 unsigned tag = dwarf_tag(die);
13137
13138 if (tag != DW_TAG_union_type)
13139 return result;
13140
13141 const die_source source = rdr.get_die_source(die);
13142 {
13143 die_class_or_union_map_type::const_iterator i =
13144 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13145 if (i != rdr.die_wip_classes_map(source).end())
13146 {
13147 union_decl_sptr u = is_union_type(i->second);
13148 ABG_ASSERT(u);
13149 return u;
13150 }
13151 }
13152
13153 string name, linkage_name;
13154 location loc;
13155 die_loc_and_name(rdr, die, loc, name, linkage_name);
13156
13157 bool is_anonymous = false;
13158 if (name.empty())
13159 {
13160 // So we are looking at an anonymous union. Let's give it a
13161 // name.
13162 name = get_internal_anonymous_die_prefix_name(die);
13163 ABG_ASSERT(!name.empty());
13164 // But we remember that the type is anonymous.
13165 is_anonymous = true;
13166
13167 if (size_t s = scope->get_num_anonymous_member_unions())
13168 name = build_internal_anonymous_die_name(name, s);
13169 }
13170
13171 // If the type has location, then associate it to its
13172 // representation. This way, all occurences of types with the same
13173 // representation (name) and location can be later detected as being
13174 // for the same type.
13175
13176 if (!is_anonymous)
13177 {
13178 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13179 {
13180 if (loc)
13181 result = lookup_union_type_per_location(loc.expand(), *corp);
13182 else
13183 result = lookup_union_type(name, *corp);
13184
13185 if (result)
13186 {
13187 rdr.associate_die_to_type(die, result, where_offset);
13188 return result;
13189 }
13190 }
13191 }
13192
13193 // if we've already seen a union with the same union as 'die' then
13194 // let's re-use that one. We can't really safely re-use anonymous
13195 // unions as they have no name, by construction. What we can do,
13196 // rather, is to reuse the typedef that name them, when they do have
13197 // a naming typedef.
13198 if (!is_anonymous)
13199 if (union_decl_sptr pre_existing_union =
13200 is_union_type(rdr.lookup_artifact_from_die(die)))
13201 union_type = pre_existing_union;
13202
13203 uint64_t size = 0;
13204 die_size_in_bits(die, size);
13205 bool is_artificial = die_is_artificial(die);
13206
13207 if (union_type)
13208 {
13209 result = union_type;
13210 result->set_location(loc);
13211 }
13212 else
13213 {
13214 result.reset(new union_decl(rdr.env(), name, size, loc,
13215 decl_base::VISIBILITY_DEFAULT,
13216 is_anonymous));
13217 if (is_declaration_only)
13218 result->set_is_declaration_only(true);
13219 result = is_union_type(add_decl_to_scope(result, scope));
13220 ABG_ASSERT(result);
13221 }
13222
13223 if (size)
13224 {
13225 result->set_size_in_bits(size);
13226 result->set_is_declaration_only(false);
13227 }
13228
13229 result->set_is_artificial(is_artificial);
13230
13231 rdr.associate_die_to_type(die, result, where_offset);
13232
13233 // TODO: maybe schedule declaration-only union for result like we do
13234 // for classes:
13235 // rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13236
13237 Dwarf_Die child;
13238 bool has_child = (dwarf_child(die, &child) == 0);
13239 if (!has_child)
13240 return result;
13241
13242 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13243
13244 scope_decl_sptr scop =
13245 dynamic_pointer_cast<scope_decl>(result);
13246 ABG_ASSERT(scop);
13247 rdr.scope_stack().push(scop.get());
13248
13249 if (has_child)
13250 {
13251 do
13252 {
13253 tag = dwarf_tag(&child);
13254 // Handle data members.
13255 if (tag == DW_TAG_member || tag == DW_TAG_variable)
13256 {
13257 Dwarf_Die type_die;
13258 if (!die_die_attribute(&child, DW_AT_type, type_die))
13259 continue;
13260
13261 string n, m;
13262 location loc;
13263 die_loc_and_name(rdr, &child, loc, n, m);
13264
13265 // Because we can be updating an existing union, let's
13266 // make sure we don't already have a member of the same
13267 // name. Anonymous member are handled a bit later below
13268 // so let's not consider them here.
13269 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13270 continue;
13271
13272 ssize_t offset_in_bits = 0;
13273 decl_base_sptr ty =
13274 is_decl(build_ir_node_from_die(rdr, &type_die,
13275 called_from_public_decl,
13276 where_offset));
13277 type_base_sptr t = is_type(ty);
13278 if (!t)
13279 continue;
13280
13281 // We have a non-static data member. So this union
13282 // cannot be a declaration-only union anymore, even if
13283 // some DWARF emitters might consider it otherwise.
13284 result->set_is_declaration_only(false);
13285 access_specifier access = public_access;
13286
13287 die_access_specifier(&child, access);
13288
13289 var_decl_sptr dm(new var_decl(n, t, loc, m));
13290 // If dm is an anonymous data member, let's make sure
13291 // the current union doesn't already have it as a data
13292 // member.
13293 if (n.empty() && result->find_data_member(dm))
13294 continue;
13295
13296 result->add_data_member(dm, access, /*is_laid_out=*/true,
13297 /*is_static=*/false,
13298 offset_in_bits);
13299 ABG_ASSERT(has_scope(dm));
13300 rdr.associate_die_to_decl(&child, dm, where_offset,
13301 /*associate_by_repr=*/false);
13302 }
13303 // Handle member functions;
13304 else if (tag == DW_TAG_subprogram)
13305 {
13306 decl_base_sptr r =
13307 is_decl(build_ir_node_from_die(rdr, &child,
13308 result.get(),
13309 called_from_public_decl,
13310 where_offset));
13311 if (!r)
13312 continue;
13313
13314 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13315 ABG_ASSERT(f);
13316
13317 finish_member_function_reading(&child, f, result, rdr);
13318
13319 rdr.associate_die_to_decl(&child, f, where_offset,
13320 /*associate_by_repr=*/false);
13321 }
13322 // Handle member types
13323 else if (die_is_type(&child))
13324 decl_base_sptr td =
13325 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13326 called_from_public_decl,
13327 where_offset));
13328 } while (dwarf_siblingof(&child, &child) == 0);
13329 }
13330
13331 rdr.scope_stack().pop();
13332
13333 {
13334 die_class_or_union_map_type::const_iterator i =
13335 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13336 if (i != rdr.die_wip_classes_map(source).end())
13337 {
13338 if (is_member_type(i->second))
13339 set_member_access_specifier(result,
13340 get_member_access_specifier(i->second));
13341 rdr.die_wip_classes_map(source).erase(i);
13342 }
13343 }
13344
13345 return result;
13346 }
13347
13348 /// build a qualified type from a DW_TAG_const_type,
13349 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13350 ///
13351 /// @param rdr the DWARF reader to consider.
13352 ///
13353 /// @param die the input DIE to read from.
13354 ///
13355 /// @param called_from_public_decl true if this function was called
13356 /// from a context where either a public function or a public variable
13357 /// is being built.
13358 ///
13359 /// @param where_offset the offset of the DIE where we are "logically"
13360 /// positionned at, in the DIE tree. This is useful when @p die is
13361 /// e.g, DW_TAG_partial_unit that can be included in several places in
13362 /// the DIE tree.
13363 ///
13364 /// @return the resulting qualified_type_def.
13365 static type_base_sptr
build_qualified_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13366 build_qualified_type(reader& rdr,
13367 Dwarf_Die* die,
13368 bool called_from_public_decl,
13369 size_t where_offset)
13370 {
13371 type_base_sptr result;
13372 if (!die)
13373 return result;
13374
13375 unsigned tag = dwarf_tag(die);
13376
13377 if (tag != DW_TAG_const_type
13378 && tag != DW_TAG_volatile_type
13379 && tag != DW_TAG_restrict_type)
13380 return result;
13381
13382 Dwarf_Die underlying_type_die;
13383 decl_base_sptr utype_decl;
13384 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13385 // So, if no DW_AT_type is present, then this means (if we are
13386 // looking at a debug info emitted by GCC) that we are looking
13387 // at a qualified void type.
13388 utype_decl = build_ir_node_for_void_type(rdr);
13389
13390 if (!utype_decl)
13391 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13392 called_from_public_decl,
13393 where_offset));
13394 if (!utype_decl)
13395 return result;
13396
13397 // The call to build_ir_node_from_die() could have triggered the
13398 // creation of the type for this DIE. In that case, just return it.
13399 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13400 {
13401 result = t;
13402 rdr.associate_die_to_type(die, result, where_offset);
13403 return result;
13404 }
13405
13406 type_base_sptr utype = is_type(utype_decl);
13407 ABG_ASSERT(utype);
13408
13409 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13410 if (tag == DW_TAG_const_type)
13411 qual |= qualified_type_def::CV_CONST;
13412 else if (tag == DW_TAG_volatile_type)
13413 qual |= qualified_type_def::CV_VOLATILE;
13414 else if (tag == DW_TAG_restrict_type)
13415 qual |= qualified_type_def::CV_RESTRICT;
13416 else
13417 ABG_ASSERT_NOT_REACHED;
13418
13419 if (!result)
13420 result.reset(new qualified_type_def(utype, qual, location()));
13421
13422 rdr.associate_die_to_type(die, result, where_offset);
13423
13424 return result;
13425 }
13426
13427 /// Walk a tree of typedef of qualified arrays and schedule all type
13428 /// nodes for canonicalization.
13429 ///
13430 /// This is to be used after an array tree has been cloned. In that
13431 /// case, the newly cloned type nodes have to be scheduled for
13432 /// canonicalization.
13433 ///
13434 /// This is a subroutine of maybe_strip_qualification.
13435 ///
13436 /// @param t the type node to be scheduled for canonicalization.
13437 ///
13438 /// @param rdr the DWARF reader to use.
13439 static void
schedule_array_tree_for_late_canonicalization(const type_base_sptr & t,reader & rdr)13440 schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13441 reader &rdr)
13442 {
13443 if (typedef_decl_sptr type = is_typedef(t))
13444 {
13445 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13446 rdr);
13447 rdr.schedule_type_for_late_canonicalization(t);
13448 }
13449 else if (qualified_type_def_sptr type = is_qualified_type(t))
13450 {
13451 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13452 rdr);
13453 rdr.schedule_type_for_late_canonicalization(t);
13454 }
13455 else if (array_type_def_sptr type = is_array_type(t))
13456 {
13457 for (vector<array_type_def::subrange_sptr>::const_iterator i =
13458 type->get_subranges().begin();
13459 i != type->get_subranges().end();
13460 ++i)
13461 {
13462 if (!(*i)->get_scope())
13463 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13464 rdr.schedule_type_for_late_canonicalization(*i);
13465
13466 }
13467 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13468 rdr);
13469 rdr.schedule_type_for_late_canonicalization(type);
13470 }
13471 }
13472
13473 /// Strip qualification from a qualified type, when it makes sense.
13474 ///
13475 /// DWARF constructs "const reference". This is redundant because a
13476 /// reference is always const. The issue is these redundant types then
13477 /// leak into the IR and make for bad diagnostics.
13478 ///
13479 /// This function thus strips the const qualifier from the type in
13480 /// that case. It might contain code to strip other cases like this
13481 /// in the future.
13482 ///
13483 /// @param t the type to strip const qualification from.
13484 ///
13485 /// @param rdr the @ref reader to use.
13486 ///
13487 /// @return the stripped type or just return @p t.
13488 static decl_base_sptr
maybe_strip_qualification(const qualified_type_def_sptr t,reader & rdr)13489 maybe_strip_qualification(const qualified_type_def_sptr t,
13490 reader &rdr)
13491 {
13492 if (!t)
13493 return t;
13494
13495 decl_base_sptr result = t;
13496 type_base_sptr u = t->get_underlying_type();
13497
13498 strip_redundant_quals_from_underyling_types(t);
13499 result = strip_useless_const_qualification(t);
13500 if (result.get() != t.get())
13501 return result;
13502
13503 if (is_array_type(u) || is_typedef_of_array(u))
13504 {
13505 array_type_def_sptr array;
13506 scope_decl * scope = 0;
13507 if ((array = is_array_type(u)))
13508 {
13509 scope = array->get_scope();
13510 ABG_ASSERT(scope);
13511 array = is_array_type(clone_array_tree(array));
13512 schedule_array_tree_for_late_canonicalization(array, rdr);
13513 add_decl_to_scope(array, scope);
13514 t->set_underlying_type(array);
13515 u = t->get_underlying_type();
13516 }
13517 else if (is_typedef_of_array(u))
13518 {
13519 scope = is_decl(u)->get_scope();
13520 ABG_ASSERT(scope);
13521 typedef_decl_sptr typdef =
13522 is_typedef(clone_array_tree(is_typedef(u)));
13523 schedule_array_tree_for_late_canonicalization(typdef, rdr);
13524 ABG_ASSERT(typdef);
13525 add_decl_to_scope(typdef, scope);
13526 t->set_underlying_type(typdef);
13527 u = t->get_underlying_type();
13528 array = is_typedef_of_array(u);
13529 }
13530 else
13531 ABG_ASSERT_NOT_REACHED;
13532
13533 ABG_ASSERT(array);
13534 // We should not be editing types that are already canonicalized.
13535 ABG_ASSERT(!array->get_canonical_type());
13536 type_base_sptr element_type = array->get_element_type();
13537
13538 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13539 {
13540 // We should not be editing types that are already canonicalized.
13541 ABG_ASSERT(!qualified->get_canonical_type());
13542 qualified_type_def::CV quals = qualified->get_cv_quals();
13543 quals |= t->get_cv_quals();
13544 qualified->set_cv_quals(quals);
13545 strip_redundant_quals_from_underyling_types(qualified);
13546 result = is_decl(u);
13547 }
13548 else
13549 {
13550 qualified_type_def_sptr qual_type
13551 (new qualified_type_def(element_type,
13552 t->get_cv_quals(),
13553 t->get_location()));
13554 strip_redundant_quals_from_underyling_types(qual_type);
13555 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13556 array->set_element_type(qual_type);
13557 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13558 result = is_decl(u);
13559 }
13560 }
13561
13562 return result;
13563 }
13564
13565 /// Build a pointer type from a DW_TAG_pointer_type DIE.
13566 ///
13567 /// @param rdr the DWARF reader to consider.
13568 ///
13569 /// @param die the DIE to read information from.
13570 ///
13571 /// @param called_from_public_decl true if this function was called
13572 /// from a context where either a public function or a public variable
13573 /// is being built.
13574 ///
13575 /// @param where_offset the offset of the DIE where we are "logically"
13576 /// positionned at, in the DIE tree. This is useful when @p die is
13577 /// e.g, DW_TAG_partial_unit that can be included in several places in
13578 /// the DIE tree.
13579 ///
13580 /// @return the resulting pointer to pointer_type_def.
13581 static pointer_type_def_sptr
build_pointer_type_def(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13582 build_pointer_type_def(reader& rdr,
13583 Dwarf_Die* die,
13584 bool called_from_public_decl,
13585 size_t where_offset)
13586 {
13587 pointer_type_def_sptr result;
13588
13589 if (!die)
13590 return result;
13591
13592 unsigned tag = dwarf_tag(die);
13593 if (tag != DW_TAG_pointer_type)
13594 return result;
13595
13596 type_or_decl_base_sptr utype_decl;
13597 Dwarf_Die underlying_type_die;
13598 bool has_underlying_type_die = false;
13599 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13600 // If the DW_AT_type attribute is missing, that means we are
13601 // looking at a pointer to "void".
13602 utype_decl = build_ir_node_for_void_type(rdr);
13603 else
13604 has_underlying_type_die = true;
13605
13606 if (!utype_decl && has_underlying_type_die)
13607 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
13608 called_from_public_decl,
13609 where_offset);
13610 if (!utype_decl)
13611 return result;
13612
13613 // The call to build_ir_node_from_die() could have triggered the
13614 // creation of the type for this DIE. In that case, just return it.
13615 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13616 {
13617 result = is_pointer_type(t);
13618 ABG_ASSERT(result);
13619 return result;
13620 }
13621
13622 type_base_sptr utype = is_type(utype_decl);
13623 ABG_ASSERT(utype);
13624
13625 // if the DIE for the pointer type doesn't have a byte_size
13626 // attribute then we assume the size of the pointer is the address
13627 // size of the current translation unit.
13628 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13629 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13630 // The size as expressed by DW_AT_byte_size is in byte, so let's
13631 // convert it to bits.
13632 size *= 8;
13633
13634 // And the size of the pointer must be the same as the address size
13635 // of the current translation unit.
13636 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13637
13638 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13639 ABG_ASSERT(result->get_pointed_to_type());
13640
13641 rdr.associate_die_to_type(die, result, where_offset);
13642 return result;
13643 }
13644
13645 /// Build a reference type from either a DW_TAG_reference_type or
13646 /// DW_TAG_rvalue_reference_type DIE.
13647 ///
13648 /// @param rdr the DWARF reader to consider.
13649 ///
13650 /// @param die the DIE to read from.
13651 ///
13652 /// @param called_from_public_decl true if this function was called
13653 /// from a context where either a public function or a public variable
13654 /// is being built.
13655 ///
13656 /// @param where_offset the offset of the DIE where we are "logically"
13657 /// positionned at, in the DIE tree. This is useful when @p die is
13658 /// e.g, DW_TAG_partial_unit that can be included in several places in
13659 /// the DIE tree.
13660 ///
13661 /// @return a pointer to the resulting reference_type_def.
13662 static reference_type_def_sptr
build_reference_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13663 build_reference_type(reader& rdr,
13664 Dwarf_Die* die,
13665 bool called_from_public_decl,
13666 size_t where_offset)
13667 {
13668 reference_type_def_sptr result;
13669
13670 if (!die)
13671 return result;
13672
13673 unsigned tag = dwarf_tag(die);
13674 if (tag != DW_TAG_reference_type
13675 && tag != DW_TAG_rvalue_reference_type)
13676 return result;
13677
13678 Dwarf_Die underlying_type_die;
13679 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13680 return result;
13681
13682 type_or_decl_base_sptr utype_decl =
13683 build_ir_node_from_die(rdr, &underlying_type_die,
13684 called_from_public_decl,
13685 where_offset);
13686 if (!utype_decl)
13687 return result;
13688
13689 // The call to build_ir_node_from_die() could have triggered the
13690 // creation of the type for this DIE. In that case, just return it.
13691 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13692 {
13693 result = is_reference_type(t);
13694 ABG_ASSERT(result);
13695 return result;
13696 }
13697
13698 type_base_sptr utype = is_type(utype_decl);
13699 ABG_ASSERT(utype);
13700
13701 // if the DIE for the reference type doesn't have a byte_size
13702 // attribute then we assume the size of the reference is the address
13703 // size of the current translation unit.
13704 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13705 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13706 size *= 8;
13707
13708 // And the size of the pointer must be the same as the address size
13709 // of the current translation unit.
13710 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13711
13712 bool is_lvalue = tag == DW_TAG_reference_type;
13713
13714 result.reset(new reference_type_def(utype, is_lvalue, size,
13715 /*alignment=*/0,
13716 location()));
13717 if (corpus_sptr corp = rdr.corpus())
13718 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
13719 result = t;
13720 rdr.associate_die_to_type(die, result, where_offset);
13721 return result;
13722 }
13723
13724 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13725 ///
13726 /// @param rdr the DWARF reader to consider.
13727 ///
13728 /// @param die the DIE to read from.
13729 ///
13730 /// @param is_method points to a class or union declaration iff we're
13731 /// building the type for a method. This is the enclosing class or
13732 /// union of the method.
13733 ///
13734 /// @param where_offset the offset of the DIE where we are "logically"
13735 /// positioned at, in the DIE tree. This is useful when @p die is
13736 /// e.g, DW_TAG_partial_unit that can be included in several places in
13737 /// the DIE tree.
13738 ///
13739 /// @return a pointer to the resulting function_type_sptr.
13740 static function_type_sptr
build_function_type(reader & rdr,Dwarf_Die * die,class_or_union_sptr is_method,size_t where_offset)13741 build_function_type(reader& rdr,
13742 Dwarf_Die* die,
13743 class_or_union_sptr is_method,
13744 size_t where_offset)
13745 {
13746 function_type_sptr result;
13747
13748 if (!die)
13749 return result;
13750
13751 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
13752 || dwarf_tag(die) == DW_TAG_subprogram);
13753
13754 const die_source source = rdr.get_die_source(die);
13755
13756 {
13757 size_t off = dwarf_dieoffset(die);
13758 auto i = rdr.die_wip_function_types_map(source).find(off);
13759 if (i != rdr.die_wip_function_types_map(source).end())
13760 {
13761 function_type_sptr fn_type = is_function_type(i->second);
13762 ABG_ASSERT(fn_type);
13763 return fn_type;
13764 }
13765 }
13766
13767 decl_base_sptr type_decl;
13768
13769 translation_unit_sptr tu = rdr.cur_transl_unit();
13770 ABG_ASSERT(tu);
13771
13772 /// If, inside the current translation unit, we've already seen a
13773 /// function type with the same text representation, then reuse that
13774 /// one instead.
13775 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
13776 {
13777 result = is_function_type(t);
13778 ABG_ASSERT(result);
13779 rdr.associate_die_to_type(die, result, where_offset);
13780 return result;
13781 }
13782
13783 bool odr_is_relevant = rdr.odr_is_relevant(die);
13784 if (odr_is_relevant)
13785 {
13786 // So we can rely on the One Definition Rule to say that if
13787 // several different function types have the same name (or
13788 // rather, representation) across the entire binary, then they
13789 // ought to designate the same function type. So let's ensure
13790 // that if we've already seen a function type with the same
13791 // representation as the function type 'die', then it's the same
13792 // type as the one denoted by 'die'.
13793 if (function_type_sptr fn_type =
13794 is_function_type(rdr.lookup_type_artifact_from_die(die)))
13795 {
13796 rdr.associate_die_to_type(die, fn_type, where_offset);
13797 return fn_type;
13798 }
13799 }
13800
13801 // Let's look at the DIE to detect if it's the DIE for a method
13802 // (type). If it is, we can deduce the name of its enclosing class
13803 // and if it's a static or const.
13804 bool is_const = false;
13805 bool is_static = false;
13806 Dwarf_Die object_pointer_die;
13807 Dwarf_Die class_type_die;
13808 bool has_this_parm_die =
13809 die_function_type_is_method_type(rdr, die, where_offset,
13810 object_pointer_die,
13811 class_type_die,
13812 is_static);
13813 if (has_this_parm_die)
13814 {
13815 // The function (type) has a "this" parameter DIE. It means it's
13816 // a member function DIE.
13817 if (!is_static)
13818 if (die_object_pointer_is_for_const_method(&object_pointer_die))
13819 is_const = true;
13820
13821 if (!is_method)
13822 {
13823 // We were initially called as if the function represented
13824 // by DIE was *NOT* a member function. But now we know it's
13825 // a member function. Let's take that into account.
13826 class_or_union_sptr klass_type =
13827 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
13828 /*called_from_pub_decl=*/true,
13829 where_offset));
13830 ABG_ASSERT(klass_type);
13831 is_method = klass_type;
13832 }
13833 }
13834
13835 // Let's create the type early and record it as being for the DIE
13836 // 'die'. This way, when building the sub-type triggers the
13837 // creation of a type matching the same 'die', then we'll reuse this
13838 // one.
13839
13840 result.reset(is_method
13841 ? new method_type(is_method, is_const,
13842 tu->get_address_size(),
13843 /*alignment=*/0)
13844 : new function_type(rdr.env(), tu->get_address_size(),
13845 /*alignment=*/0));
13846 rdr.associate_die_to_type(die, result, where_offset);
13847 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
13848
13849 type_base_sptr return_type;
13850 Dwarf_Die ret_type_die;
13851 if (die_die_attribute(die, DW_AT_type, ret_type_die))
13852 return_type =
13853 is_type(build_ir_node_from_die(rdr, &ret_type_die,
13854 /*called_from_public_decl=*/true,
13855 where_offset));
13856 if (!return_type)
13857 return_type = is_type(build_ir_node_for_void_type(rdr));
13858 result->set_return_type(return_type);
13859
13860 Dwarf_Die child;
13861 function_decl::parameters function_parms;
13862
13863 if (dwarf_child(die, &child) == 0)
13864 do
13865 {
13866 int child_tag = dwarf_tag(&child);
13867 if (child_tag == DW_TAG_formal_parameter)
13868 {
13869 // This is a "normal" function parameter.
13870 string name, linkage_name;
13871 location loc;
13872 die_loc_and_name(rdr, &child, loc, name, linkage_name);
13873 if (!tools_utils::string_is_ascii_identifier(name))
13874 // Sometimes, bogus compiler emit names that are
13875 // non-ascii garbage. Let's just ditch that for now.
13876 name.clear();
13877 bool is_artificial = die_is_artificial(&child);
13878 type_base_sptr parm_type;
13879 Dwarf_Die parm_type_die;
13880 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
13881 parm_type =
13882 is_type(build_ir_node_from_die(rdr, &parm_type_die,
13883 /*called_from_public_decl=*/true,
13884 where_offset));
13885 if (!parm_type)
13886 continue;
13887 function_decl::parameter_sptr p
13888 (new function_decl::parameter(parm_type, name, loc,
13889 /*variadic_marker=*/false,
13890 is_artificial));
13891 function_parms.push_back(p);
13892 }
13893 else if (child_tag == DW_TAG_unspecified_parameters)
13894 {
13895 // This is a variadic function parameter.
13896 bool is_artificial = die_is_artificial(&child);
13897
13898 type_base_sptr parm_type =
13899 is_type(build_ir_node_for_variadic_parameter_type(rdr));
13900 function_decl::parameter_sptr p
13901 (new function_decl::parameter(parm_type,
13902 /*name=*/"",
13903 location(),
13904 /*variadic_marker=*/true,
13905 is_artificial));
13906 function_parms.push_back(p);
13907 // After a DW_TAG_unspecified_parameters tag, we shouldn't
13908 // keep reading for parameters. The
13909 // unspecified_parameters TAG should be the last parameter
13910 // that we record. For instance, if there are multiple
13911 // DW_TAG_unspecified_parameters DIEs then we should care
13912 // only for the first one.
13913 break;
13914 }
13915 }
13916 while (dwarf_siblingof(&child, &child) == 0);
13917
13918 result->set_parameters(function_parms);
13919
13920 tu->bind_function_type_life_time(result);
13921
13922 result->set_is_artificial(true);
13923
13924 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
13925
13926 {
13927 die_function_type_map_type::const_iterator i =
13928 rdr.die_wip_function_types_map(source).
13929 find(dwarf_dieoffset(die));
13930 if (i != rdr.die_wip_function_types_map(source).end())
13931 rdr.die_wip_function_types_map(source).erase(i);
13932 }
13933
13934 maybe_canonicalize_type(result, rdr);
13935 return result;
13936 }
13937
13938 /// Build a subrange type from a DW_TAG_subrange_type.
13939 ///
13940 /// @param rdr the DWARF reader to consider.
13941 ///
13942 /// @param die the DIE to read from.
13943 ///
13944 /// @param where_offset the offset of the DIE where we are "logically"
13945 /// positionned at in the DIE tree. This is useful when @p die is
13946 /// e,g, DW_TAG_partial_unit that can be included in several places in
13947 /// the DIE tree.
13948 ///
13949 /// @param associate_die_to_type if this is true then the resulting
13950 /// type is associated to the @p die, so that next time when the
13951 /// system looks up the type associated to it, the current resulting
13952 /// type is returned. If false, then no association is done and the
13953 /// resulting type can be destroyed right after. This can be useful
13954 /// when the sole purpose of building the @ref
13955 /// array_type_def::subrange_type is to use some of its method like,
13956 /// e.g, its name pretty printing methods.
13957 ///
13958 /// @return the newly built instance of @ref
13959 /// array_type_def::subrange_type, or nil if no type could be built.
13960 static array_type_def::subrange_sptr
build_subrange_type(reader & rdr,const Dwarf_Die * die,size_t where_offset,bool associate_type_to_die)13961 build_subrange_type(reader& rdr,
13962 const Dwarf_Die* die,
13963 size_t where_offset,
13964 bool associate_type_to_die)
13965 {
13966 array_type_def::subrange_sptr result;
13967
13968 if (!die)
13969 return result;
13970
13971 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
13972 if (tag != DW_TAG_subrange_type)
13973 return result;
13974
13975 string name = die_name(die);
13976
13977 // load the underlying type.
13978 Dwarf_Die underlying_type_die;
13979 type_base_sptr underlying_type;
13980 /* Unless there is an underlying type which says differently. */
13981 bool is_signed = false;
13982 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
13983 underlying_type =
13984 is_type(build_ir_node_from_die(rdr,
13985 &underlying_type_die,
13986 /*called_from_public_decl=*/true,
13987 where_offset));
13988
13989 if (underlying_type)
13990 {
13991 uint64_t ate;
13992 if (die_unsigned_constant_attribute (&underlying_type_die,
13993 DW_AT_encoding,
13994 ate))
13995 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
13996 }
13997
13998 translation_unit::language language = rdr.cur_transl_unit()->get_language();
13999 array_type_def::subrange_type::bound_value lower_bound =
14000 get_default_array_lower_bound(language);
14001 array_type_def::subrange_type::bound_value upper_bound;
14002 uint64_t count = 0;
14003 bool is_infinite = false;
14004
14005 // The DWARF 4 specifications says, in [5.11 Subrange
14006 // Type Entries]:
14007 //
14008 // The subrange entry may have the attributes
14009 // DW_AT_lower_bound and DW_AT_upper_bound to
14010 // specify, respectively, the lower and upper bound
14011 // values of the subrange.
14012 //
14013 // So let's look for DW_AT_lower_bound first.
14014 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14015
14016 // Then, DW_AT_upper_bound.
14017 if (!die_constant_attribute(die, DW_AT_upper_bound, is_signed, upper_bound))
14018 {
14019 // The DWARF 4 spec says, in [5.11 Subrange Type
14020 // Entries]:
14021 //
14022 // The DW_AT_upper_bound attribute may be replaced
14023 // by a DW_AT_count attribute, whose value
14024 // describes the number of elements in the
14025 // subrange rather than the value of the last
14026 // element."
14027 //
14028 // So, as DW_AT_upper_bound is not present in this
14029 // case, let's see if there is a DW_AT_count.
14030 die_unsigned_constant_attribute(die, DW_AT_count, count);
14031
14032 // We can deduce the upper_bound from the
14033 // lower_bound and the number of elements of the
14034 // array:
14035 if (int64_t u = lower_bound.get_signed_value() + count)
14036 upper_bound = u - 1;
14037
14038 if (upper_bound.get_unsigned_value() == 0 && count == 0)
14039 // No upper_bound nor count was present on the DIE, this means
14040 // the array is considered to have an infinite (or rather not
14041 // known) size.
14042 is_infinite = true;
14043 }
14044
14045 if (UINT64_MAX == upper_bound.get_unsigned_value())
14046 {
14047 // If the upper_bound size is the max of the integer value, then
14048 // it most certainly means infinite size.
14049 is_infinite = true;
14050 upper_bound.set_unsigned(0);
14051 }
14052
14053 result.reset
14054 (new array_type_def::subrange_type(rdr.env(),
14055 name,
14056 lower_bound,
14057 upper_bound,
14058 location()));
14059 result->is_infinite(is_infinite);
14060
14061 if (underlying_type)
14062 result->set_underlying_type(underlying_type);
14063
14064 ABG_ASSERT(result->is_infinite()
14065 || (result->get_length() ==
14066 (uint64_t) (result->get_upper_bound()
14067 - result->get_lower_bound() + 1)));
14068
14069 if (associate_type_to_die)
14070 rdr.associate_die_to_type(die, result, where_offset);
14071
14072 return result;
14073 }
14074
14075 /// Build the sub-ranges of an array type.
14076 ///
14077 /// This is a sub-routine of build_array_type().
14078 ///
14079 /// @param rdr the context to read from.
14080 ///
14081 /// @param die the DIE of tag DW_TAG_array_type which contains
14082 /// children DIEs that represent the sub-ranges.
14083 ///
14084 /// @param subranges out parameter. This is set to the sub-ranges
14085 /// that are built from @p die.
14086 ///
14087 /// @param where_offset the offset of the DIE where we are "logically"
14088 /// positioned at, in the DIE tree. This is useful when @p die is
14089 /// e.g, DW_TAG_partial_unit that can be included in several places in
14090 /// the DIE tree.
14091 static void
build_subranges_from_array_type_die(reader & rdr,const Dwarf_Die * die,array_type_def::subranges_type & subranges,size_t where_offset,bool associate_type_to_die)14092 build_subranges_from_array_type_die(reader& rdr,
14093 const Dwarf_Die* die,
14094 array_type_def::subranges_type& subranges,
14095 size_t where_offset,
14096 bool associate_type_to_die)
14097 {
14098 Dwarf_Die child;
14099
14100 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14101 {
14102 do
14103 {
14104 int child_tag = dwarf_tag(&child);
14105 if (child_tag == DW_TAG_subrange_type)
14106 {
14107 array_type_def::subrange_sptr s;
14108 if (associate_type_to_die)
14109 {
14110 // We are being called to create the type, add it to
14111 // the current type graph and associate it to the
14112 // DIE it's been created from.
14113 type_or_decl_base_sptr t =
14114 build_ir_node_from_die(rdr, &child,
14115 /*called_from_public_decl=*/true,
14116 where_offset);
14117 s = is_subrange_type(t);
14118 }
14119 else
14120 // We are being called to create the type but *NOT*
14121 // add it to the current tyupe tree, *NOR* associate
14122 // it to the DIE it's been created from.
14123 s = build_subrange_type(rdr, &child,
14124 where_offset,
14125 /*associate_type_to_die=*/false);
14126 if (s)
14127 subranges.push_back(s);
14128 }
14129 }
14130 while (dwarf_siblingof(&child, &child) == 0);
14131 }
14132 }
14133
14134 /// Build an array type from a DW_TAG_array_type DIE.
14135 ///
14136 /// @param rdr the DWARF reader to consider.
14137 ///
14138 /// @param die the DIE to read from.
14139 ///
14140 /// @param called_from_public_decl true if this function was called
14141 /// from a context where either a public function or a public variable
14142 /// is being built.
14143 ///
14144 /// @param where_offset the offset of the DIE where we are "logically"
14145 /// positioned at, in the DIE tree. This is useful when @p die is
14146 /// e.g, DW_TAG_partial_unit that can be included in several places in
14147 /// the DIE tree.
14148 ///
14149 /// @return a pointer to the resulting array_type_def.
14150 static array_type_def_sptr
build_array_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)14151 build_array_type(reader& rdr,
14152 Dwarf_Die* die,
14153 bool called_from_public_decl,
14154 size_t where_offset)
14155 {
14156 array_type_def_sptr result;
14157
14158 if (!die)
14159 return result;
14160
14161 unsigned tag = dwarf_tag(die);
14162 if (tag != DW_TAG_array_type)
14163 return result;
14164
14165 decl_base_sptr type_decl;
14166 Dwarf_Die type_die;
14167
14168 if (die_die_attribute(die, DW_AT_type, type_die))
14169 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14170 called_from_public_decl,
14171 where_offset));
14172 if (!type_decl)
14173 return result;
14174
14175 // The call to build_ir_node_from_die() could have triggered the
14176 // creation of the type for this DIE. In that case, just return it.
14177 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14178 {
14179 result = is_array_type(t);
14180 ABG_ASSERT(result);
14181 return result;
14182 }
14183
14184 type_base_sptr type = is_type(type_decl);
14185 ABG_ASSERT(type);
14186
14187 array_type_def::subranges_type subranges;
14188
14189 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14190
14191 result.reset(new array_type_def(type, subranges, location()));
14192
14193 return result;
14194 }
14195
14196 /// Create a typedef_decl from a DW_TAG_typedef DIE.
14197 ///
14198 /// @param rdr the DWARF reader to consider.
14199 ///
14200 /// @param die the DIE to read from.
14201 ///
14202 /// @param called_from_public_decl true if this function was called
14203 /// from a context where either a public function or a public variable
14204 /// is being built.
14205 ///
14206 /// @param where_offset the offset of the DIE where we are "logically"
14207 /// positionned at, in the DIE tree. This is useful when @p die is
14208 /// e.g, DW_TAG_partial_unit that can be included in several places in
14209 /// the DIE tree.
14210 ///
14211 /// @return the newly created typedef_decl.
14212 static typedef_decl_sptr
build_typedef_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)14213 build_typedef_type(reader& rdr,
14214 Dwarf_Die* die,
14215 bool called_from_public_decl,
14216 size_t where_offset)
14217 {
14218 typedef_decl_sptr result;
14219
14220 if (!die)
14221 return result;
14222
14223 unsigned tag = dwarf_tag(die);
14224 if (tag != DW_TAG_typedef)
14225 return result;
14226
14227 string name, linkage_name;
14228 location loc;
14229 die_loc_and_name(rdr, die, loc, name, linkage_name);
14230
14231 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14232 if (loc)
14233 result = lookup_typedef_type_per_location(loc.expand(), *corp);
14234
14235 if (!result)
14236 {
14237 type_base_sptr utype;
14238 Dwarf_Die underlying_type_die;
14239 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14240 // A typedef DIE with no underlying type means a typedef to
14241 // void type.
14242 utype = rdr.env().get_void_type();
14243
14244 if (!utype)
14245 utype =
14246 is_type(build_ir_node_from_die(rdr,
14247 &underlying_type_die,
14248 called_from_public_decl,
14249 where_offset));
14250 if (!utype)
14251 return result;
14252
14253 ABG_ASSERT(utype);
14254 result.reset(new typedef_decl(name, utype, loc, linkage_name));
14255
14256 if ((is_class_or_union_type(utype) || is_enum_type(utype))
14257 && is_anonymous_type(utype))
14258 {
14259 // This is a naming typedef for an enum or a class. Let's
14260 // mark the underlying decl as such.
14261 decl_base_sptr decl = is_decl(utype);
14262 ABG_ASSERT(decl);
14263 decl->set_naming_typedef(result);
14264 }
14265 }
14266
14267 rdr.associate_die_to_type(die, result, where_offset);
14268
14269 return result;
14270 }
14271
14272 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14273 /// denoted by the DIE is not suppressed by a suppression
14274 /// specification associated to the current DWARF reader.
14275 ///
14276 /// Note that if a member variable declaration with the same name as
14277 /// the name of the DIE we are looking at exists, this function returns
14278 /// that existing variable declaration.
14279 ///
14280 /// @param rdr the DWARF reader to use.
14281 ///
14282 /// @param die the DIE representing the variable we are looking at.
14283 ///
14284 /// @param where_offset the offset of the DIE where we are "logically"
14285 /// positionned at, in the DIE tree. This is useful when @p die is
14286 /// e.g, DW_TAG_partial_unit that can be included in several places in
14287 /// the DIE tree.
14288 ///
14289 /// @param result if this is set to an existing var_decl, this means
14290 /// that the function will append the new properties it sees on @p die
14291 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14292 /// new var_decl is going to be allocated and returned.
14293 ///
14294 /// @param is_required_decl_spec this is true iff the variable to
14295 /// build is referred to as being the specification of another
14296 /// variable.
14297 ///
14298 /// @return a pointer to the newly created var_decl. If the var_decl
14299 /// could not be built, this function returns NULL.
14300 static var_decl_sptr
build_or_get_var_decl_if_not_suppressed(reader & rdr,scope_decl * scope,Dwarf_Die * die,size_t where_offset,var_decl_sptr result,bool is_required_decl_spec)14301 build_or_get_var_decl_if_not_suppressed(reader& rdr,
14302 scope_decl *scope,
14303 Dwarf_Die *die,
14304 size_t where_offset,
14305 var_decl_sptr result,
14306 bool is_required_decl_spec)
14307 {
14308 var_decl_sptr var;
14309 if (variable_is_suppressed(rdr, scope, die, is_required_decl_spec))
14310 return var;
14311
14312 if (class_decl* class_type = is_class_type(scope))
14313 {
14314 string var_name = die_name(die);
14315 if (!var_name.empty())
14316 if ((var = class_type->find_data_member(var_name)))
14317 return var;
14318 }
14319 var = build_var_decl(rdr, die, where_offset, result);
14320 return var;
14321 }
14322
14323 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
14324 ///
14325 /// @param rdr the DWARF reader to use.
14326 ///
14327 /// @param die the DIE representing the variable we are looking at.
14328 ///
14329 /// @param where_offset the offset of the DIE where we are "logically"
14330 /// positionned at, in the DIE tree. This is useful when @p die is
14331 /// e.g, DW_TAG_partial_unit that can be included in several places in
14332 /// the DIE tree.
14333 ///
14334 /// @param result if this is set to an existing var_decl, this means
14335 /// that the function will append the new properties it sees on @p die
14336 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14337 /// new var_decl is going to be allocated and returned.
14338 ///
14339 /// @return a pointer to the newly created var_decl. If the var_decl
14340 /// could not be built, this function returns NULL.
14341 static var_decl_sptr
build_var_decl(reader & rdr,Dwarf_Die * die,size_t where_offset,var_decl_sptr result)14342 build_var_decl(reader& rdr,
14343 Dwarf_Die *die,
14344 size_t where_offset,
14345 var_decl_sptr result)
14346 {
14347 if (!die)
14348 return result;
14349
14350 int tag = dwarf_tag(die);
14351 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14352
14353 if (!die_is_public_decl(die))
14354 return result;
14355
14356 type_base_sptr type;
14357 Dwarf_Die type_die;
14358 if (die_die_attribute(die, DW_AT_type, type_die))
14359 {
14360 decl_base_sptr ty =
14361 is_decl(build_ir_node_from_die(rdr, &type_die,
14362 /*called_from_public_decl=*/true,
14363 where_offset));
14364 if (!ty)
14365 return result;
14366 type = is_type(ty);
14367 ABG_ASSERT(type);
14368 }
14369
14370 if (!type)
14371 return result;
14372
14373 string name, linkage_name;
14374 location loc;
14375 die_loc_and_name(rdr, die, loc, name, linkage_name);
14376
14377 if (!result)
14378 result.reset(new var_decl(name, type, loc, linkage_name));
14379 else
14380 {
14381 // We were called to append properties that might have been
14382 // missing from the first version of the variable. And usually
14383 // that missing property is the mangled name.
14384 if (!linkage_name.empty())
14385 result->set_linkage_name(linkage_name);
14386 }
14387
14388 // Check if a variable symbol with this name is exported by the elf
14389 // binary. If it is, then set the symbol of the variable, if it's
14390 // not set already.
14391 if (!result->get_symbol())
14392 {
14393 elf_symbol_sptr var_sym;
14394 Dwarf_Addr var_addr;
14395
14396 if (rdr.get_variable_address(die, var_addr))
14397 {
14398 rdr.symtab()->
14399 update_main_symbol(var_addr,
14400 result->get_linkage_name().empty()
14401 ? result->get_name()
14402 : result->get_linkage_name());
14403 var_sym = rdr.variable_symbol_is_exported(var_addr);
14404 }
14405
14406 if (var_sym)
14407 {
14408 result->set_symbol(var_sym);
14409 // If the linkage name is not set or is wrong, set it to
14410 // the name of the underlying symbol.
14411 string linkage_name = result->get_linkage_name();
14412 if (linkage_name.empty()
14413 || !var_sym->get_alias_from_name(linkage_name))
14414 result->set_linkage_name(var_sym->get_name());
14415 result->set_is_in_public_symbol_table(true);
14416 }
14417 }
14418
14419 return result;
14420 }
14421
14422 /// Test if a given function denoted by its DIE and its scope is
14423 /// suppressed by any of the suppression specifications associated to
14424 /// a given context of ELF/DWARF reading.
14425 ///
14426 /// Note that a non-member function which symbol is not exported is
14427 /// also suppressed.
14428 ///
14429 /// @param rdr the ELF/DWARF reading content of interest.
14430 ///
14431 /// @param scope of the scope of the function.
14432 ///
14433 /// @param function_die the DIE representing the function.
14434 ///
14435 /// @param is_declaration_only is true if the DIE denoted by @p die is
14436 /// a declaration-only DIE.
14437 ///
14438 /// @return true iff @p function_die is suppressed by at least one
14439 /// suppression specification attached to the @p rdr.
14440 static bool
function_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * function_die,bool is_declaration_only)14441 function_is_suppressed(const reader& rdr,
14442 const scope_decl* scope,
14443 Dwarf_Die *function_die,
14444 bool is_declaration_only)
14445 {
14446 if (function_die == 0
14447 || dwarf_tag(function_die) != DW_TAG_subprogram)
14448 return false;
14449
14450 string fname = die_string_attribute(function_die, DW_AT_name);
14451 string flinkage_name = die_linkage_name(function_die);
14452 if (flinkage_name.empty() && rdr.die_is_in_c(function_die))
14453 flinkage_name = fname;
14454 string qualified_name = build_qualified_name(scope, fname);
14455
14456 // A non-member non-static function which symbol is not exported is
14457 // suppressed.
14458 //
14459 // Note that if the non-member non-static function has an undefined
14460 // symbol, by default, it's not suppressed. Unless we are asked to
14461 // drop undefined symbols too.
14462 if (!is_class_type(scope)
14463 && (!is_declaration_only || rdr.drop_undefined_syms()))
14464 {
14465 Dwarf_Addr fn_addr;
14466 if (!rdr.get_function_address(function_die, fn_addr))
14467 return true;
14468
14469 elf_symbol_sptr symbol =
14470 rdr.function_symbol_is_exported(fn_addr);
14471 if (!symbol)
14472 return true;
14473 if (!symbol->is_suppressed())
14474 return false;
14475
14476 // Since there is only one symbol in DWARF associated with an elf_symbol,
14477 // we can assume this is the main symbol then. Otherwise the main hinting
14478 // did not work as expected.
14479 ABG_ASSERT(symbol->is_main_symbol());
14480 if (symbol->has_aliases())
14481 for (elf_symbol_sptr a = symbol->get_next_alias();
14482 !a->is_main_symbol(); a = a->get_next_alias())
14483 if (!a->is_suppressed())
14484 return false;
14485 }
14486
14487 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
14488 /*require_drop_property=*/true);
14489 }
14490
14491 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
14492 /// function denoted by the DIE is not suppressed by a suppression
14493 /// specification associated to the current DWARF reader.
14494 ///
14495 /// Note that if a member function declaration with the same signature
14496 /// (pretty representation) as one of the DIE we are looking at
14497 /// exists, this function returns that existing function declaration.
14498 /// Similarly, if there is already a constructed member function with
14499 /// the same linkage name as the one on the DIE, this function returns
14500 /// that member function.
14501 ///
14502 /// Also note that the function_decl IR returned by this function must
14503 /// be passed to finish_member_function_reading because several
14504 /// properties from the DIE are actually read by that function, and
14505 /// the corresponding properties on the function_decl IR are updated
14506 /// accordingly. This is done to support "updating" a function_decl
14507 /// IR with properties scathered across several DIEs.
14508 ///
14509 /// @param rdr the DWARF reader to use.
14510 ///
14511 /// @param scope the scope of the function we are looking at.
14512 ///
14513 /// @param fn_die the DIE representing the function we are looking at.
14514 ///
14515 /// @param where_offset the offset of the DIE where we are "logically"
14516 /// positionned at, in the DIE tree. This is useful when @p die is
14517 /// e.g, DW_TAG_partial_unit that can be included in several places in
14518 /// the DIE tree.
14519 ///
14520 /// @param is_declaration_only is true if the DIE denoted by @p fn_die
14521 /// is a declaration-only DIE.
14522 ///
14523 /// @param result if this is set to an existing function_decl, this
14524 /// means that the function will append the new properties it sees on
14525 /// @p fn_die to that exising function_decl. Otherwise, if this
14526 /// parameter is NULL, a new function_decl is going to be allocated
14527 /// and returned.
14528 ///
14529 /// @return a pointer to the newly created var_decl. If the var_decl
14530 /// could not be built, this function returns NULL.
14531 static function_decl_sptr
build_or_get_fn_decl_if_not_suppressed(reader & rdr,scope_decl * scope,Dwarf_Die * fn_die,size_t where_offset,bool is_declaration_only,function_decl_sptr result)14532 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
14533 scope_decl *scope,
14534 Dwarf_Die *fn_die,
14535 size_t where_offset,
14536 bool is_declaration_only,
14537 function_decl_sptr result)
14538 {
14539 function_decl_sptr fn;
14540 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
14541 return fn;
14542
14543 string name = die_name(fn_die);
14544 string linkage_name = die_linkage_name(fn_die);
14545 bool is_dtor = !name.empty() && name[0]== '~';
14546 bool is_virtual = false;
14547 if (is_dtor)
14548 {
14549 Dwarf_Attribute attr;
14550 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
14551 DW_AT_vtable_elem_location,
14552 &attr))
14553 is_virtual = true;
14554 }
14555
14556
14557 // If we've already built an IR for a function with the same
14558 // signature (from another DIE), reuse it, unless that function is a
14559 // virtual C++ destructor. Several virtual C++ destructors with the
14560 // same signature can be implemented by several different ELF
14561 // symbols. So re-using C++ destructors like that can lead to us
14562 // missing some destructors.
14563 if (!result && (!(is_dtor && is_virtual)))
14564 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
14565 {
14566 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
14567 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
14568 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
14569 return fn;
14570 }
14571
14572 // If a member function with the same linkage name as the one
14573 // carried by the DIE already exists, then return it.
14574 if (class_decl* klass = is_class_type(scope))
14575 {
14576 string linkage_name = die_linkage_name(fn_die);
14577 fn = klass->find_member_function_sptr(linkage_name);
14578 if (fn)
14579 // We found a member function that has the same signature.
14580 // Let's mark it for update.
14581 result = fn;
14582 }
14583
14584 if (!fn || !fn->get_symbol())
14585 // We haven't yet been able to construct a function IR, or, we
14586 // have one 'partial' function IR that doesn't have any associated
14587 // symbol yet. Note that in the later case, a function IR without
14588 // any associated symbol will be dropped on the floor by
14589 // potential_member_fn_should_be_dropped. So let's build or a new
14590 // function IR or complete the existing partial IR.
14591 fn = build_function_decl(rdr, fn_die, where_offset, result);
14592
14593 return fn;
14594 }
14595
14596 /// Test if a given variable denoted by its DIE and its scope is
14597 /// suppressed by any of the suppression specifications associated to
14598 /// a given context of ELF/DWARF reading.
14599 ///
14600 /// @param rdr the ELF/DWARF reading content of interest.
14601 ///
14602 /// @param scope of the scope of the variable.
14603 ///
14604 /// @param variable_die the DIE representing the variable.
14605 ///
14606 /// @param is_required_decl_spec if true, means that the @p
14607 /// variable_die being considered is for a variable decl that is a
14608 /// specification for a concrete variable being built.
14609 ///
14610 /// @return true iff @p variable_die is suppressed by at least one
14611 /// suppression specification attached to the @p rdr.
14612 static bool
variable_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * variable_die,bool is_required_decl_spec)14613 variable_is_suppressed(const reader& rdr,
14614 const scope_decl* scope,
14615 Dwarf_Die *variable_die,
14616 bool is_required_decl_spec)
14617 {
14618 if (variable_die == 0
14619 || (dwarf_tag(variable_die) != DW_TAG_variable
14620 && dwarf_tag(variable_die) != DW_TAG_member))
14621 return false;
14622
14623 string name = die_string_attribute(variable_die, DW_AT_name);
14624 string linkage_name = die_linkage_name(variable_die);
14625 if (linkage_name.empty() && rdr.die_is_in_c(variable_die))
14626 linkage_name = name;
14627 string qualified_name = build_qualified_name(scope, name);
14628
14629 // If a non member variable that is a declaration (has no defined
14630 // and exported symbol) and is not the specification of another
14631 // concrete variable, then it's suppressed. This is a size
14632 // optimization; it removes useless declaration-only variables from
14633 // the IR.
14634 if (!is_class_type(scope) && !is_required_decl_spec)
14635 {
14636 Dwarf_Addr var_addr = 0;
14637 if (!rdr.get_variable_address(variable_die, var_addr))
14638 return true;
14639
14640 elf_symbol_sptr symbol =
14641 rdr.variable_symbol_is_exported(var_addr);
14642 if (!symbol)
14643 return true;
14644 if (!symbol->is_suppressed())
14645 return false;
14646
14647 // Since there is only one symbol in DWARF associated with an elf_symbol,
14648 // we can assume this is the main symbol then. Otherwise the main hinting
14649 // did not work as expected.
14650 ABG_ASSERT(symbol->is_main_symbol());
14651 if (symbol->has_aliases())
14652 for (elf_symbol_sptr a = symbol->get_next_alias();
14653 !a->is_main_symbol(); a = a->get_next_alias())
14654 if (!a->is_suppressed())
14655 return false;
14656 }
14657
14658 return suppr::is_variable_suppressed(rdr,
14659 qualified_name,
14660 linkage_name,
14661 /*require_drop_property=*/true);
14662 }
14663
14664 /// Test if a type (designated by a given DIE) in a given scope is
14665 /// suppressed by the suppression specifications that are associated
14666 /// to a given DWARF reader.
14667 ///
14668 /// @param rdr the DWARF reader to consider.
14669 ///
14670 /// @param scope of the scope of the type DIE to consider.
14671 ///
14672 /// @param type_die the DIE that designates the type to consider.
14673 ///
14674 /// @param type_is_private out parameter. If this function returns
14675 /// true (the type @p type_die is suppressed) and if the type was
14676 /// suppressed because it's private then this parameter is set to
14677 /// true.
14678 ///
14679 /// @return true iff the type designated by the DIE @p type_die, in
14680 /// the scope @p scope is suppressed by at the suppression
14681 /// specifications associated to the current DWARF reader.
14682 static bool
type_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * type_die,bool & type_is_private)14683 type_is_suppressed(const reader& rdr,
14684 const scope_decl* scope,
14685 Dwarf_Die *type_die,
14686 bool &type_is_private)
14687 {
14688 if (type_die == 0
14689 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
14690 && dwarf_tag(type_die) != DW_TAG_class_type
14691 && dwarf_tag(type_die) != DW_TAG_structure_type
14692 && dwarf_tag(type_die) != DW_TAG_union_type))
14693 return false;
14694
14695 string type_name, linkage_name;
14696 location type_location;
14697 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
14698 string qualified_name = build_qualified_name(scope, type_name);
14699
14700 return suppr::is_type_suppressed(rdr,
14701 qualified_name,
14702 type_location,
14703 type_is_private,
14704 /*require_drop_property=*/true);
14705 }
14706
14707 /// Test if a type (designated by a given DIE) in a given scope is
14708 /// suppressed by the suppression specifications that are associated
14709 /// to a given DWARF reader.
14710 ///
14711 /// @param rdr the DWARF reader to consider.
14712 ///
14713 /// @param scope of the scope of the type DIE to consider.
14714 ///
14715 /// @param type_die the DIE that designates the type to consider.
14716 ///
14717 /// @return true iff the type designated by the DIE @p type_die, in
14718 /// the scope @p scope is suppressed by at the suppression
14719 /// specifications associated to the current DWARF reader.
14720 static bool
type_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * type_die)14721 type_is_suppressed(const reader& rdr,
14722 const scope_decl* scope,
14723 Dwarf_Die *type_die)
14724 {
14725 bool type_is_private = false;
14726 return type_is_suppressed(rdr, scope, type_die, type_is_private);
14727 }
14728
14729 /// Get the opaque version of a type that was suppressed because it's
14730 /// a private type.
14731 ///
14732 /// The opaque version version of the type is just a declared-only
14733 /// version of the type (class, union or enum type) denoted by @p
14734 /// type_die.
14735 ///
14736 /// @param rdr the DWARF reader in use.
14737 ///
14738 /// @param scope the scope of the type die we are looking at.
14739 ///
14740 /// @param type_die the type DIE we are looking at.
14741 ///
14742 /// @param where_offset the offset of the DIE where we are "logically"
14743 /// positionned at, in the DIE tree. This is useful when @p die is
14744 /// e.g, DW_TAG_partial_unit that can be included in several places in
14745 /// the DIE tree.
14746 ///
14747 /// @return the opaque version of the type denoted by @p type_die or
14748 /// nil if no opaque version was found.
14749 static type_or_decl_base_sptr
get_opaque_version_of_type(reader & rdr,scope_decl * scope,Dwarf_Die * type_die,size_t where_offset)14750 get_opaque_version_of_type(reader &rdr,
14751 scope_decl *scope,
14752 Dwarf_Die *type_die,
14753 size_t where_offset)
14754 {
14755 type_or_decl_base_sptr result;
14756
14757 if (type_die == 0)
14758 return result;
14759
14760 unsigned tag = dwarf_tag(type_die);
14761 if (tag != DW_TAG_class_type
14762 && tag != DW_TAG_structure_type
14763 && tag != DW_TAG_union_type
14764 && tag != DW_TAG_enumeration_type)
14765 return result;
14766
14767 string type_name, linkage_name;
14768 location type_location;
14769 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
14770 if (!type_location)
14771 return result;
14772
14773 string qualified_name = build_qualified_name(scope, type_name);
14774
14775 //
14776 // TODO: also handle declaration-only unions. To do that, we mostly
14777 // need to adapt add_or_update_union_type to make it schedule
14778 // declaration-only unions for resolution too.
14779 //
14780 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
14781 {
14782 string_classes_map::const_iterator i =
14783 rdr.declaration_only_classes().find(qualified_name);
14784 if (i != rdr.declaration_only_classes().end())
14785 result = i->second.back();
14786
14787 if (!result)
14788 {
14789 // So we didn't find any pre-existing forward-declared-only
14790 // class for the class definition that we could return as an
14791 // opaque type. So let's build one.
14792 //
14793 // TODO: we need to be able to do this for unions too!
14794 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
14795 /*alignment=*/0, /*size=*/0,
14796 tag == DW_TAG_structure_type,
14797 type_location,
14798 decl_base::VISIBILITY_DEFAULT));
14799 klass->set_is_declaration_only(true);
14800 klass->set_is_artificial(die_is_artificial(type_die));
14801 add_decl_to_scope(klass, scope);
14802 rdr.associate_die_to_type(type_die, klass, where_offset);
14803 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
14804 result = klass;
14805 }
14806 }
14807
14808 if (tag == DW_TAG_enumeration_type)
14809 {
14810 string_enums_map::const_iterator i =
14811 rdr.declaration_only_enums().find(qualified_name);
14812 if (i != rdr.declaration_only_enums().end())
14813 result = i->second.back();
14814
14815 if (!result)
14816 {
14817 uint64_t size = 0;
14818 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
14819 size *= 8;
14820 type_decl_sptr underlying_type =
14821 build_enum_underlying_type(rdr, type_name, size,
14822 /*anonymous=*/true);
14823 enum_type_decl::enumerators enumeratorz;
14824 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
14825 type_location,
14826 underlying_type,
14827 enumeratorz,
14828 linkage_name));
14829 enum_type->set_is_artificial(die_is_artificial(type_die));
14830 add_decl_to_scope(enum_type, scope);
14831 result = enum_type;
14832 }
14833 }
14834
14835 return result;
14836 }
14837
14838 /// Create a function symbol with a given name.
14839 ///
14840 /// @param sym_name the name of the symbol to create.
14841 ///
14842 /// @param env the environment to create the symbol in.
14843 ///
14844 /// @return the newly created symbol.
14845 elf_symbol_sptr
create_default_fn_sym(const string & sym_name,const environment & env)14846 create_default_fn_sym(const string& sym_name, const environment& env)
14847 {
14848 elf_symbol::version ver;
14849 elf_symbol_sptr result =
14850 elf_symbol::create(env,
14851 /*symbol index=*/ 0,
14852 /*symbol size=*/ 0,
14853 sym_name,
14854 /*symbol type=*/ elf_symbol::FUNC_TYPE,
14855 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
14856 /*symbol is defined=*/ true,
14857 /*symbol is common=*/ false,
14858 /*symbol version=*/ ver,
14859 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
14860 return result;
14861 }
14862
14863 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
14864 ///
14865 /// @param rdr the DWARF reader to use
14866 ///
14867 /// @param die the DW_TAG_subprogram DIE to read from.
14868 ///
14869 /// @param where_offset the offset of the DIE where we are "logically"
14870 /// positionned at, in the DIE tree. This is useful when @p die is
14871 /// e.g, DW_TAG_partial_unit that can be included in several places in
14872 /// the DIE tree.
14873 ///
14874 /// @param called_for_public_decl this is set to true if the function
14875 /// was called for a public (function) decl.
14876 static function_decl_sptr
build_function_decl(reader & rdr,Dwarf_Die * die,size_t where_offset,function_decl_sptr fn)14877 build_function_decl(reader& rdr,
14878 Dwarf_Die* die,
14879 size_t where_offset,
14880 function_decl_sptr fn)
14881 {
14882 function_decl_sptr result = fn;
14883 if (!die)
14884 return result;
14885 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
14886
14887 if (!die_is_public_decl(die))
14888 return result;
14889
14890 translation_unit_sptr tu = rdr.cur_transl_unit();
14891 ABG_ASSERT(tu);
14892
14893 string fname, flinkage_name;
14894 location floc;
14895 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
14896
14897 size_t is_inline = die_is_declared_inline(die);
14898 class_or_union_sptr is_method =
14899 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
14900
14901 if (result)
14902 {
14903 // Add the properties that might have been missing from the
14904 // first declaration of the function. For now, it usually is
14905 // the mangled name that goes missing in the first declarations.
14906 //
14907 // Also note that if 'fn' has just been cloned, the current
14908 // linkage name (of the current DIE) might be different from the
14909 // linkage name of 'fn'. In that case, update the linkage name
14910 // of 'fn' too.
14911 if (!flinkage_name.empty()
14912 && result->get_linkage_name() != flinkage_name)
14913 result->set_linkage_name(flinkage_name);
14914 if (floc)
14915 if (!result->get_location())
14916 result->set_location(floc);
14917 }
14918 else
14919 {
14920 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
14921 where_offset));
14922 if (!fn_type)
14923 return result;
14924
14925 maybe_canonicalize_type(fn_type, rdr);
14926
14927 result.reset(is_method
14928 ? new method_decl(fname, fn_type,
14929 is_inline, floc,
14930 flinkage_name)
14931 : new function_decl(fname, fn_type,
14932 is_inline, floc,
14933 flinkage_name));
14934 }
14935
14936 // Set the symbol of the function. If the linkage name is not set
14937 // or is wrong, set it to the name of the underlying symbol.
14938 if (!result->get_symbol())
14939 {
14940 elf_symbol_sptr fn_sym;
14941 Dwarf_Addr fn_addr;
14942 if (rdr.get_function_address(die, fn_addr))
14943 {
14944 rdr.symtab()->
14945 update_main_symbol(fn_addr,
14946 result->get_linkage_name().empty()
14947 ? result->get_name()
14948 : result->get_linkage_name());
14949 fn_sym = rdr.function_symbol_is_exported(fn_addr);
14950 }
14951
14952 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
14953 {
14954 result->set_symbol(fn_sym);
14955 string linkage_name = result->get_linkage_name();
14956 if (linkage_name.empty())
14957 result->set_linkage_name(fn_sym->get_name());
14958 result->set_is_in_public_symbol_table(true);
14959 }
14960 }
14961
14962 rdr.associate_die_to_type(die, result->get_type(), where_offset);
14963
14964 size_t die_offset = dwarf_dieoffset(die);
14965
14966 if (fn
14967 && is_member_function(fn)
14968 && get_member_function_is_virtual(fn)
14969 && !result->get_linkage_name().empty())
14970 // This function is a virtual member function which has its
14971 // linkage name *and* and has its underlying symbol correctly set.
14972 // It thus doesn't need any fixup related to elf symbol. So
14973 // remove it from the set of virtual member functions with linkage
14974 // names and no elf symbol that need to be fixed up.
14975 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
14976 return result;
14977 }
14978
14979 /// Canonicalize a type if it's suitable for early canonicalizing, or,
14980 /// if it's not, schedule it for late canonicalization, after the
14981 /// debug info of the current translation unit has been fully read.
14982 ///
14983 /// A (composite) type is deemed suitable for early canonicalizing iff
14984 /// all of its sub-types are canonicalized themselve. Non composite
14985 /// types are always deemed suitable for early canonicalization.
14986 ///
14987 /// Note that this function knows how to deal with anonymous classes,
14988 /// structs and enums, unlike the overload below:
14989 ///
14990 /// @param t the type DIE to consider for canonicalization.
14991 ///
14992 /// @param rdr the @ref reader to use.
14993 static void
maybe_canonicalize_type(const type_base_sptr & t,reader & rdr)14994 maybe_canonicalize_type(const type_base_sptr& t,
14995 reader& rdr)
14996 {
14997 if (!t)
14998 return;
14999
15000 type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15001 if (is_class_type(peeled_type)
15002 || is_union_type(peeled_type)
15003 || is_function_type(peeled_type)
15004 || is_array_type(peeled_type)
15005 || is_qualified_type(peeled_type)
15006 || is_enum_type(peeled_type)
15007 ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15008 // We delay canonicalization of classes/unions or typedef,
15009 // pointers, references and array to classes/unions. This is
15010 // because the (underlying) class might not be finished yet and we
15011 // might not be able to able detect it here (thinking about
15012 // classes that are work-in-progress, or classes that might be
15013 // later amended by some DWARF construct). So we err on the safe
15014 // side. We also delay canonicalization for array and qualified
15015 // types because they can be edited (in particular by
15016 // maybe_strip_qualification) after they are initially built.
15017 rdr.schedule_type_for_late_canonicalization(t);
15018 else if (type_has_non_canonicalized_subtype(t))
15019 rdr.schedule_type_for_late_canonicalization(t);
15020 else
15021 canonicalize(t);
15022 }
15023
15024 /// If a given decl is a member type declaration, set its access
15025 /// specifier from the DIE that represents it.
15026 ///
15027 /// @param member_type_declaration the member type declaration to
15028 /// consider.
15029 static void
maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,Dwarf_Die * die)15030 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15031 Dwarf_Die* die)
15032 {
15033 if (is_type(member_type_declaration)
15034 && is_member_decl(member_type_declaration))
15035 {
15036 class_or_union* scope =
15037 is_class_or_union_type(member_type_declaration->get_scope());
15038 ABG_ASSERT(scope);
15039
15040 access_specifier access = public_access;
15041 if (class_decl* cl = is_class_type(scope))
15042 if (!cl->is_struct())
15043 access = private_access;
15044
15045 die_access_specifier(die, access);
15046 set_member_access_specifier(member_type_declaration, access);
15047 }
15048 }
15049
15050 /// This function tests if a given function which might be intented to
15051 /// be added to a class scope (to become a member function) should be
15052 /// dropped on the floor instead and not be added to the class.
15053 ///
15054 /// This is a subroutine of build_ir_node_from_die.
15055 ///
15056 /// @param fn the function to consider.
15057 ///
15058 /// @param scope the scope the function is intended to be added
15059 /// to. This might be of class type or not.
15060 ///
15061 /// @param fn_die the DWARF die of @p fn.
15062 ///
15063 /// @return true iff @p fn should be dropped on the floor.
15064 static bool
potential_member_fn_should_be_dropped(const function_decl_sptr & fn,Dwarf_Die * fn_die)15065 potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15066 Dwarf_Die *fn_die)
15067 {
15068 if (!fn || fn->get_scope())
15069 return false;
15070
15071 if (// A function that is not virtual ...
15072 !die_is_virtual(fn_die)
15073 // ... has a linkage name ...
15074 && !fn->get_linkage_name().empty()
15075 // .. and yet has no ELF symbol associated ...
15076 && !fn->get_symbol())
15077 // Should not be added to its class scope.
15078 //
15079 // Why would it? It's not part of the ABI anyway, as it doesn't
15080 // have any ELF symbol associated and is not a virtual member
15081 // function. It just constitutes bloat in the IR and might even
15082 // induce spurious change reports down the road.
15083 return true;
15084
15085 return false;
15086 }
15087
15088 /// Build an IR node from a given DIE and add the node to the current
15089 /// IR being build and held in the DWARF reader. Doing that is called
15090 /// "emitting an IR node for the DIE".
15091 ///
15092 /// @param rdr the DWARF reader.
15093 ///
15094 /// @param die the DIE to consider.
15095 ///
15096 /// @param scope the scope under which the resulting IR node has to be
15097 /// added.
15098 ///
15099 /// @param called_from_public_decl set to yes if this function is
15100 /// called from the functions used to build a public decl (functions
15101 /// and variables). In that case, this function accepts building IR
15102 /// nodes representing types. Otherwise, this function only creates
15103 /// IR nodes representing public decls (functions and variables).
15104 /// This is done to avoid emitting IR nodes for types that are not
15105 /// referenced by public functions or variables.
15106 ///
15107 /// @param where_offset the offset of the DIE where we are "logically"
15108 /// positionned at, in the DIE tree. This is useful when @p die is
15109 /// e.g, DW_TAG_partial_unit that can be included in several places in
15110 /// the DIE tree.
15111 ///
15112 /// @param is_required_decl_spec if true, it means the ir node to
15113 /// build is for a decl that is a specification for another decl that
15114 /// is concrete. If you don't know what this is, set it to false.
15115 ///
15116 /// @param is_declaration_only is true if the DIE denoted by @p die is
15117 /// a declaration-only DIE.
15118 ///
15119 /// @return the resulting IR node.
15120 static type_or_decl_base_sptr
build_ir_node_from_die(reader & rdr,Dwarf_Die * die,scope_decl * scope,bool called_from_public_decl,size_t where_offset,bool is_declaration_only,bool is_required_decl_spec)15121 build_ir_node_from_die(reader& rdr,
15122 Dwarf_Die* die,
15123 scope_decl* scope,
15124 bool called_from_public_decl,
15125 size_t where_offset,
15126 bool is_declaration_only,
15127 bool is_required_decl_spec)
15128 {
15129 type_or_decl_base_sptr result;
15130
15131 if (!die || !scope)
15132 return result;
15133
15134 int tag = dwarf_tag(die);
15135
15136 if (!called_from_public_decl)
15137 {
15138 if (rdr.load_all_types() && die_is_type(die))
15139 /* We were instructed to load debug info for all types,
15140 included those that are not reachable from a public
15141 declaration. So load the debug info for this type. */;
15142 else if (tag != DW_TAG_subprogram
15143 && tag != DW_TAG_variable
15144 && tag != DW_TAG_member
15145 && tag != DW_TAG_namespace)
15146 return result;
15147 }
15148
15149 const die_source source_of_die = rdr.get_die_source(die);
15150
15151 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15152 source_of_die)))
15153 {
15154 if (rdr.load_all_types())
15155 if (called_from_public_decl)
15156 if (type_base_sptr t = is_type(result))
15157 if (corpus *abi_corpus = scope->get_corpus())
15158 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15159
15160 return result;
15161 }
15162
15163 // This is *the* bit of code that ensures we have the right notion
15164 // of "declared" at any point in a DIE chain formed from
15165 // DW_AT_abstract_origin and DW_AT_specification links. There should
15166 // be no other callers of die_is_declaration_only.
15167 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15168
15169 switch (tag)
15170 {
15171 // Type DIEs we support.
15172 case DW_TAG_base_type:
15173 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15174 {
15175 result =
15176 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15177 canonicalize(t);
15178 }
15179 break;
15180
15181 case DW_TAG_typedef:
15182 {
15183 typedef_decl_sptr t;
15184 t = is_typedef(scope->find_member_type(die_name(die)));
15185
15186 if (!t)
15187 t = build_typedef_type(rdr, die,
15188 called_from_public_decl,
15189 where_offset);
15190
15191 result = add_decl_to_scope(t, scope);
15192 if (result)
15193 {
15194 maybe_set_member_type_access_specifier(is_decl(result), die);
15195 maybe_canonicalize_type(t, rdr);
15196 }
15197 }
15198 break;
15199
15200 case DW_TAG_pointer_type:
15201 {
15202 pointer_type_def_sptr p =
15203 build_pointer_type_def(rdr, die,
15204 called_from_public_decl,
15205 where_offset);
15206 if (p)
15207 {
15208 result =
15209 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15210 ABG_ASSERT(result->get_translation_unit());
15211 maybe_canonicalize_type(p, rdr);
15212 }
15213 }
15214 break;
15215
15216 case DW_TAG_reference_type:
15217 case DW_TAG_rvalue_reference_type:
15218 {
15219 reference_type_def_sptr r =
15220 build_reference_type(rdr, die,
15221 called_from_public_decl,
15222 where_offset);
15223 if (r)
15224 {
15225 result =
15226 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15227
15228 rdr.associate_die_to_type(die, r, where_offset);
15229 maybe_canonicalize_type(r, rdr);
15230 }
15231 }
15232 break;
15233
15234 case DW_TAG_const_type:
15235 case DW_TAG_volatile_type:
15236 case DW_TAG_restrict_type:
15237 {
15238 type_base_sptr q =
15239 build_qualified_type(rdr, die,
15240 called_from_public_decl,
15241 where_offset);
15242 if (q)
15243 {
15244 // Strip some potentially redundant type qualifiers from
15245 // the qualified type we just built.
15246 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15247 rdr);
15248 if (!d)
15249 d = get_type_declaration(q);
15250 ABG_ASSERT(d);
15251 type_base_sptr ty = is_type(d);
15252 // Associate the die to type ty again because 'ty'might be
15253 // different from 'q', because 'ty' is 'q' possibly
15254 // stripped from some redundant type qualifier.
15255 rdr.associate_die_to_type(die, ty, where_offset);
15256 result =
15257 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15258 maybe_canonicalize_type(is_type(result), rdr);
15259 }
15260 }
15261 break;
15262
15263 case DW_TAG_enumeration_type:
15264 {
15265 bool type_is_private = false;
15266 bool type_suppressed =
15267 type_is_suppressed(rdr, scope, die, type_is_private);
15268 if (type_suppressed && type_is_private)
15269 {
15270 // The type is suppressed because it's private. If other
15271 // non-suppressed and declaration-only instances of this
15272 // type exist in the current corpus, then it means those
15273 // non-suppressed instances are opaque versions of the
15274 // suppressed private type. Lets return one of these opaque
15275 // types then.
15276 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15277 maybe_canonicalize_type(is_type(result), rdr);
15278 }
15279 else if (!type_suppressed)
15280 {
15281 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15282 where_offset,
15283 is_declaration_only);
15284 result = add_decl_to_scope(e, scope);
15285 if (result)
15286 {
15287 maybe_set_member_type_access_specifier(is_decl(result), die);
15288 maybe_canonicalize_type(is_type(result), rdr);
15289 }
15290 }
15291 }
15292 break;
15293
15294 case DW_TAG_class_type:
15295 case DW_TAG_structure_type:
15296 {
15297 bool type_is_private = false;
15298 bool type_suppressed=
15299 type_is_suppressed(rdr, scope, die, type_is_private);
15300
15301 if (type_suppressed && type_is_private)
15302 {
15303 // The type is suppressed because it's private. If other
15304 // non-suppressed and declaration-only instances of this
15305 // type exist in the current corpus, then it means those
15306 // non-suppressed instances are opaque versions of the
15307 // suppressed private type. Lets return one of these opaque
15308 // types then.
15309 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15310 maybe_canonicalize_type(is_type(result), rdr);
15311 }
15312 else if (!type_suppressed)
15313 {
15314 Dwarf_Die spec_die;
15315 scope_decl_sptr scop;
15316 class_decl_sptr klass;
15317 if (die_die_attribute(die, DW_AT_specification, spec_die))
15318 {
15319 scope_decl_sptr skope =
15320 get_scope_for_die(rdr, &spec_die,
15321 called_from_public_decl,
15322 where_offset);
15323 ABG_ASSERT(skope);
15324 decl_base_sptr cl =
15325 is_decl(build_ir_node_from_die(rdr, &spec_die,
15326 skope.get(),
15327 called_from_public_decl,
15328 where_offset,
15329 is_declaration_only,
15330 /*is_required_decl_spec=*/false));
15331 ABG_ASSERT(cl);
15332 klass = dynamic_pointer_cast<class_decl>(cl);
15333 ABG_ASSERT(klass);
15334
15335 klass =
15336 add_or_update_class_type(rdr, die,
15337 skope.get(),
15338 tag == DW_TAG_structure_type,
15339 klass,
15340 called_from_public_decl,
15341 where_offset,
15342 is_declaration_only);
15343 }
15344 else
15345 klass =
15346 add_or_update_class_type(rdr, die, scope,
15347 tag == DW_TAG_structure_type,
15348 class_decl_sptr(),
15349 called_from_public_decl,
15350 where_offset,
15351 is_declaration_only);
15352 result = klass;
15353 if (klass)
15354 {
15355 maybe_set_member_type_access_specifier(klass, die);
15356 maybe_canonicalize_type(klass, rdr);
15357 }
15358 }
15359 }
15360 break;
15361 case DW_TAG_union_type:
15362 if (!type_is_suppressed(rdr, scope, die))
15363 {
15364 union_decl_sptr union_type =
15365 add_or_update_union_type(rdr, die, scope,
15366 union_decl_sptr(),
15367 called_from_public_decl,
15368 where_offset,
15369 is_declaration_only);
15370 if (union_type)
15371 {
15372 maybe_set_member_type_access_specifier(union_type, die);
15373 maybe_canonicalize_type(union_type, rdr);
15374 }
15375 result = union_type;
15376 }
15377 break;
15378 case DW_TAG_string_type:
15379 break;
15380 case DW_TAG_subroutine_type:
15381 {
15382 function_type_sptr f = build_function_type(rdr, die,
15383 class_decl_sptr(),
15384 where_offset);
15385 if (f)
15386 {
15387 result = f;
15388 result->set_is_artificial(false);
15389 maybe_canonicalize_type(f, rdr);
15390 }
15391 }
15392 break;
15393 case DW_TAG_array_type:
15394 {
15395 array_type_def_sptr a = build_array_type(rdr,
15396 die,
15397 called_from_public_decl,
15398 where_offset);
15399 if (a)
15400 {
15401 result =
15402 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
15403 rdr.associate_die_to_type(die, a, where_offset);
15404 maybe_canonicalize_type(a, rdr);
15405 }
15406 break;
15407 }
15408 case DW_TAG_subrange_type:
15409 {
15410 // If we got here, this means the subrange type is a "free
15411 // form" defined in the global namespace of the current
15412 // translation unit, like what is found in Ada.
15413 array_type_def::subrange_sptr s =
15414 build_subrange_type(rdr, die, where_offset);
15415 if (s)
15416 {
15417 result =
15418 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
15419 rdr.associate_die_to_type(die, s, where_offset);
15420 maybe_canonicalize_type(s, rdr);
15421 }
15422 }
15423 break;
15424 case DW_TAG_packed_type:
15425 break;
15426 case DW_TAG_set_type:
15427 break;
15428 case DW_TAG_file_type:
15429 break;
15430 case DW_TAG_ptr_to_member_type:
15431 break;
15432 case DW_TAG_thrown_type:
15433 break;
15434 case DW_TAG_interface_type:
15435 break;
15436 case DW_TAG_unspecified_type:
15437 break;
15438 case DW_TAG_shared_type:
15439 break;
15440
15441 case DW_TAG_compile_unit:
15442 // We shouldn't reach this point b/c this should be handled by
15443 // build_translation_unit.
15444 ABG_ASSERT_NOT_REACHED;
15445
15446 case DW_TAG_namespace:
15447 case DW_TAG_module:
15448 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
15449 break;
15450
15451 case DW_TAG_variable:
15452 case DW_TAG_member:
15453 {
15454 Dwarf_Die spec_die;
15455 bool var_is_cloned = false;
15456
15457 if (tag == DW_TAG_member)
15458 ABG_ASSERT(!is_c_language(rdr.cur_transl_unit()->get_language()));
15459
15460 if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15461 || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15462 spec_die, false)))
15463 {
15464 scope_decl_sptr spec_scope =
15465 get_scope_for_die(rdr, &spec_die,
15466 /*called_from_public_decl=*/
15467 die_is_effectively_public_decl(rdr, die),
15468 where_offset);
15469 if (spec_scope)
15470 {
15471 decl_base_sptr d =
15472 is_decl(build_ir_node_from_die(rdr, &spec_die,
15473 spec_scope.get(),
15474 called_from_public_decl,
15475 where_offset,
15476 is_declaration_only,
15477 /*is_required_decl_spec=*/true));
15478 if (d)
15479 {
15480 var_decl_sptr m =
15481 dynamic_pointer_cast<var_decl>(d);
15482 if (var_is_cloned)
15483 m = m->clone();
15484 m = build_var_decl(rdr, die, where_offset, m);
15485 if (is_data_member(m))
15486 {
15487 set_member_is_static(m, true);
15488 rdr.associate_die_to_decl(die, m, where_offset,
15489 /*associate_by_repr=*/false);
15490 }
15491 else
15492 {
15493 ABG_ASSERT(has_scope(m));
15494 rdr.var_decls_to_re_add_to_tree().push_back(m);
15495 }
15496 ABG_ASSERT(m->get_scope());
15497 rdr.maybe_add_var_to_exported_decls(m.get());
15498 result = m;
15499 }
15500 }
15501 }
15502 else if (var_decl_sptr v =
15503 build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
15504 where_offset,
15505 /*result=*/var_decl_sptr(),
15506 is_required_decl_spec))
15507 {
15508 result = add_decl_to_scope(v, scope);
15509 ABG_ASSERT(is_decl(result)->get_scope());
15510 v = dynamic_pointer_cast<var_decl>(result);
15511 ABG_ASSERT(v);
15512 ABG_ASSERT(v->get_scope());
15513 rdr.var_decls_to_re_add_to_tree().push_back(v);
15514 rdr.maybe_add_var_to_exported_decls(v.get());
15515 }
15516 }
15517 break;
15518
15519 case DW_TAG_subprogram:
15520 {
15521 Dwarf_Die spec_die;
15522 Dwarf_Die abstract_origin_die;
15523 Dwarf_Die *interface_die = 0, *origin_die = 0;
15524 scope_decl_sptr interface_scope;
15525 if (die_is_artificial(die))
15526 break;
15527
15528 function_decl_sptr fn;
15529 bool has_spec = die_die_attribute(die, DW_AT_specification,
15530 spec_die, true);
15531 bool has_abstract_origin =
15532 die_die_attribute(die, DW_AT_abstract_origin,
15533 abstract_origin_die, true);
15534 if (has_spec || has_abstract_origin)
15535 {
15536 interface_die =
15537 has_spec
15538 ? &spec_die
15539 : &abstract_origin_die;
15540 origin_die =
15541 has_abstract_origin
15542 ? &abstract_origin_die
15543 : &spec_die;
15544
15545 string linkage_name = die_linkage_name(die);
15546 string spec_linkage_name = die_linkage_name(interface_die);
15547
15548 interface_scope = get_scope_for_die(rdr, interface_die,
15549 called_from_public_decl,
15550 where_offset);
15551 if (interface_scope)
15552 {
15553 decl_base_sptr d;
15554 class_decl_sptr c = is_class_type(interface_scope);
15555 if (c && !linkage_name.empty())
15556 d = c->find_member_function_sptr(linkage_name);
15557
15558 if (!d)
15559 d = is_decl(build_ir_node_from_die(rdr,
15560 origin_die,
15561 interface_scope.get(),
15562 called_from_public_decl,
15563 where_offset,
15564 is_declaration_only,
15565 /*is_required_decl_spec=*/true));
15566 if (d)
15567 {
15568 fn = dynamic_pointer_cast<function_decl>(d);
15569 if (has_abstract_origin
15570 && (linkage_name != spec_linkage_name))
15571 // The current DIE has 'd' as abstract orign,
15572 // and has a linkage name that is different
15573 // from from the linkage name of 'd'. That
15574 // means, the current DIE represents a clone
15575 // of 'd'.
15576 fn = fn->clone();
15577 }
15578 }
15579 }
15580 rdr.scope_stack().push(scope);
15581
15582 scope_decl* logical_scope =
15583 interface_scope
15584 ? interface_scope.get()
15585 : scope;
15586
15587 result = build_or_get_fn_decl_if_not_suppressed(rdr, logical_scope,
15588 die, where_offset,
15589 is_declaration_only,
15590 fn);
15591
15592 if (result && !fn)
15593 {
15594 if (potential_member_fn_should_be_dropped(is_function_decl(result),
15595 die)
15596 && !is_required_decl_spec)
15597 {
15598 result.reset();
15599 break;
15600 }
15601 result = add_decl_to_scope(is_decl(result), logical_scope);
15602 }
15603
15604 fn = is_function_decl(result);
15605 if (fn && is_member_function(fn))
15606 {
15607 class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
15608 sptr_utils::noop_deleter());
15609 ABG_ASSERT(klass);
15610 finish_member_function_reading(die, fn, klass, rdr);
15611 }
15612
15613 if (fn)
15614 {
15615 rdr.maybe_add_fn_to_exported_decls(fn.get());
15616 rdr.associate_die_to_decl(die, fn, where_offset,
15617 /*associate_by_repr=*/false);
15618 maybe_canonicalize_type(fn->get_type(), rdr);
15619 }
15620
15621 rdr.scope_stack().pop();
15622 }
15623 break;
15624
15625 case DW_TAG_formal_parameter:
15626 // We should not read this case as it should have been dealt
15627 // with by build_function_decl above.
15628 ABG_ASSERT_NOT_REACHED;
15629
15630 case DW_TAG_constant:
15631 break;
15632 case DW_TAG_enumerator:
15633 break;
15634
15635 case DW_TAG_partial_unit:
15636 case DW_TAG_imported_unit:
15637 // For now, the DIEs under these are read lazily when they are
15638 // referenced by a public decl DIE that is under a
15639 // DW_TAG_compile_unit, so we shouldn't get here.
15640 ABG_ASSERT_NOT_REACHED;
15641
15642 // Other declaration we don't really intend to support yet.
15643 case DW_TAG_dwarf_procedure:
15644 case DW_TAG_imported_declaration:
15645 case DW_TAG_entry_point:
15646 case DW_TAG_label:
15647 case DW_TAG_lexical_block:
15648 case DW_TAG_unspecified_parameters:
15649 case DW_TAG_variant:
15650 case DW_TAG_common_block:
15651 case DW_TAG_common_inclusion:
15652 case DW_TAG_inheritance:
15653 case DW_TAG_inlined_subroutine:
15654 case DW_TAG_with_stmt:
15655 case DW_TAG_access_declaration:
15656 case DW_TAG_catch_block:
15657 case DW_TAG_friend:
15658 case DW_TAG_namelist:
15659 case DW_TAG_namelist_item:
15660 case DW_TAG_template_type_parameter:
15661 case DW_TAG_template_value_parameter:
15662 case DW_TAG_try_block:
15663 case DW_TAG_variant_part:
15664 case DW_TAG_imported_module:
15665 case DW_TAG_condition:
15666 case DW_TAG_type_unit:
15667 case DW_TAG_template_alias:
15668 case DW_TAG_lo_user:
15669 case DW_TAG_MIPS_loop:
15670 case DW_TAG_format_label:
15671 case DW_TAG_function_template:
15672 case DW_TAG_class_template:
15673 case DW_TAG_GNU_BINCL:
15674 case DW_TAG_GNU_EINCL:
15675 case DW_TAG_GNU_template_template_param:
15676 case DW_TAG_GNU_template_parameter_pack:
15677 case DW_TAG_GNU_formal_parameter_pack:
15678 case DW_TAG_GNU_call_site:
15679 case DW_TAG_GNU_call_site_parameter:
15680 case DW_TAG_hi_user:
15681 default:
15682 break;
15683 }
15684
15685 if (result && tag != DW_TAG_subroutine_type)
15686 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
15687 /*associate_by_repr=*/false);
15688
15689 if (result)
15690 if (rdr.load_all_types())
15691 if (called_from_public_decl)
15692 if (type_base_sptr t = is_type(result))
15693 if (corpus *abi_corpus = scope->get_corpus())
15694 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15695
15696 return result;
15697 }
15698
15699 /// Build the IR node for a void type.
15700 ///
15701 /// @param rdr the DWARF reader to use.
15702 ///
15703 /// @return the void type node.
15704 static decl_base_sptr
build_ir_node_for_void_type(reader & rdr)15705 build_ir_node_for_void_type(reader& rdr)
15706 {
15707 const environment& env = rdr.env();
15708
15709 type_base_sptr t = env.get_void_type();
15710 decl_base_sptr type_declaration = get_type_declaration(t);
15711 if (!has_scope(type_declaration))
15712 add_decl_to_scope(type_declaration,
15713 rdr.cur_transl_unit()->get_global_scope());
15714 canonicalize(t);
15715 return type_declaration;
15716 }
15717
15718 /// Build the IR node for a variadic parameter type.
15719 ///
15720 /// @param rdr the DWARF reader to use.
15721 ///
15722 /// @return the variadic parameter type.
15723 static decl_base_sptr
build_ir_node_for_variadic_parameter_type(reader & rdr)15724 build_ir_node_for_variadic_parameter_type(reader &rdr)
15725 {
15726
15727 const environment& env = rdr.env();
15728
15729 type_base_sptr t = env.get_variadic_parameter_type();
15730 decl_base_sptr type_declaration = get_type_declaration(t);
15731 if (!has_scope(type_declaration))
15732 add_decl_to_scope(type_declaration,
15733 rdr.cur_transl_unit()->get_global_scope());
15734 canonicalize(t);
15735 return type_declaration;
15736 }
15737
15738 /// Build an IR node from a given DIE and add the node to the current
15739 /// IR being build and held in the DWARF reader. Doing that is called
15740 /// "emitting an IR node for the DIE".
15741 ///
15742 /// @param rdr the DWARF reader.
15743 ///
15744 /// @param die the DIE to consider.
15745 ///
15746 /// @param called_from_public_decl set to yes if this function is
15747 /// called from the functions used to build a public decl (functions
15748 /// and variables). In that case, this function accepts building IR
15749 /// nodes representing types. Otherwise, this function only creates
15750 /// IR nodes representing public decls (functions and variables).
15751 /// This is done to avoid emitting IR nodes for types that are not
15752 /// referenced by public functions or variables.
15753 ///
15754 /// @param where_offset the offset of the DIE where we are "logically"
15755 /// positionned at, in the DIE tree. This is useful when @p die is
15756 /// e.g, DW_TAG_partial_unit that can be included in several places in
15757 /// the DIE tree.
15758 ///
15759 /// @return the resulting IR node.
15760 static type_or_decl_base_sptr
build_ir_node_from_die(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)15761 build_ir_node_from_die(reader& rdr,
15762 Dwarf_Die* die,
15763 bool called_from_public_decl,
15764 size_t where_offset)
15765 {
15766 if (!die)
15767 return decl_base_sptr();
15768
15769 if (is_c_language(rdr.cur_transl_unit()->get_language()))
15770 {
15771 const scope_decl_sptr& scop = rdr.global_scope();
15772 return build_ir_node_from_die(rdr, die, scop.get(),
15773 called_from_public_decl,
15774 where_offset,
15775 true);
15776 }
15777
15778 // Normaly, a decl that is meant to be external has a DW_AT_external
15779 // set. But then some compilers fail to always emit that flag. For
15780 // instance, for static data members, some compilers won't emit the
15781 // DW_AT_external. In that case, we assume that if the variable is
15782 // at global or named namespace scope, then we can assume it's
15783 // external. If the variable doesn't have any ELF symbol associated
15784 // to it, it'll be dropped on the floor anyway. Those variable
15785 // decls are considered as being "effectively public".
15786 bool consider_as_called_from_public_decl =
15787 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
15788 scope_decl_sptr scope = get_scope_for_die(rdr, die,
15789 consider_as_called_from_public_decl,
15790 where_offset);
15791 return build_ir_node_from_die(rdr, die, scope.get(),
15792 called_from_public_decl,
15793 where_offset,
15794 true);
15795 }
15796
15797 /// Create a dwarf::reader.
15798 ///
15799 /// @param elf_path the path to the elf file the reader is to be used
15800 /// for.
15801 ///
15802 /// @param debug_info_root_paths a vector to the paths to the
15803 /// directories under which the debug info is to be found for @p
15804 /// elf_path. Pass an empty vector if the debug info is not in a
15805 /// split file.
15806 ///
15807 /// @param environment the environment used by the current context.
15808 /// This environment contains resources needed by the DWARF reader and by
15809 /// the types and declarations that are to be created later. Note
15810 /// that ABI artifacts that are to be compared all need to be created
15811 /// within the same environment.
15812 ///
15813 /// Please also note that the life time of this environment object
15814 /// must be greater than the life time of the resulting @ref
15815 /// reader the context uses resources that are allocated in the
15816 /// environment.
15817 ///
15818 /// @param load_all_types if set to false only the types that are
15819 /// reachable from publicly exported declarations (of functions and
15820 /// variables) are read. If set to true then all types found in the
15821 /// debug information are loaded.
15822 ///
15823 /// @param linux_kernel_mode if set to true, then consider the special
15824 /// linux kernel symbol tables when determining if a symbol is
15825 /// exported or not.
15826 ///
15827 /// @return a smart pointer to the resulting dwarf::reader.
15828 elf_based_reader_sptr
create_reader(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)15829 create_reader(const std::string& elf_path,
15830 const vector<char**>& debug_info_root_paths,
15831 environment& environment,
15832 bool load_all_types,
15833 bool linux_kernel_mode)
15834 {
15835
15836 reader_sptr r = reader::create(elf_path,
15837 debug_info_root_paths,
15838 environment,
15839 load_all_types,
15840 linux_kernel_mode);
15841 return static_pointer_cast<elf_based_reader>(r);
15842 }
15843
15844 /// Re-initialize a reader so that it can re-used to read
15845 /// another binary.
15846 ///
15847 /// @param rdr the context to re-initialize.
15848 ///
15849 /// @param elf_path the path to the elf file the context is to be used
15850 /// for.
15851 ///
15852 /// @param debug_info_root_path a pointer to the path to the root
15853 /// directory under which the debug info is to be found for @p
15854 /// elf_path. Leave this to NULL if the debug info is not in a split
15855 /// file.
15856 ///
15857 /// @param environment the environment used by the current context.
15858 /// This environment contains resources needed by the DWARF reader and by
15859 /// the types and declarations that are to be created later. Note
15860 /// that ABI artifacts that are to be compared all need to be created
15861 /// within the same environment.
15862 ///
15863 /// Please also note that the life time of this environment object
15864 /// must be greater than the life time of the resulting @ref
15865 /// reader the context uses resources that are allocated in the
15866 /// environment.
15867 ///
15868 /// @param load_all_types if set to false only the types that are
15869 /// reachable from publicly exported declarations (of functions and
15870 /// variables) are read. If set to true then all types found in the
15871 /// debug information are loaded.
15872 ///
15873 /// @param linux_kernel_mode if set to true, then consider the special
15874 /// linux kernel symbol tables when determining if a symbol is
15875 /// exported or not.
15876 ///
15877 /// @return a smart pointer to the resulting dwarf::reader.
15878 void
reset_reader(elf_based_reader & rdr,const std::string & elf_path,const vector<char ** > & debug_info_root_path,bool read_all_types,bool linux_kernel_mode)15879 reset_reader(elf_based_reader& rdr,
15880 const std::string& elf_path,
15881 const vector<char**>&debug_info_root_path,
15882 bool read_all_types,
15883 bool linux_kernel_mode)
15884 {
15885 reader& r = dynamic_cast<reader&>(rdr);
15886 r.initialize(elf_path, debug_info_root_path,
15887 read_all_types, linux_kernel_mode);
15888 }
15889
15890 /// Read all @ref abigail::translation_unit possible from the debug info
15891 /// accessible from an elf file, stuff them into a libabigail ABI
15892 /// Corpus and return it.
15893 ///
15894 /// @param elf_path the path to the elf file.
15895 ///
15896 /// @param debug_info_root_paths a vector of pointers to root paths
15897 /// under which to look for the debug info of the elf files that are
15898 /// later handled by the Dwfl. This for cases where the debug info is
15899 /// split into a different file from the binary we want to inspect.
15900 /// On Red Hat compatible systems, this root path is usually
15901 /// /usr/lib/debug by default. If this argument is set to NULL, then
15902 /// "./debug" and /usr/lib/debug will be searched for sub-directories
15903 /// containing the debug info file.
15904 ///
15905 /// @param environment the environment used by the current context.
15906 /// This environment contains resources needed by the DWARF reader and by
15907 /// the types and declarations that are to be created later. Note
15908 /// that ABI artifacts that are to be compared all need to be created
15909 /// within the same environment. Also, the lifetime of the
15910 /// environment must be greater than the lifetime of the resulting
15911 /// corpus because the corpus uses resources that are allocated in the
15912 /// environment.
15913 ///
15914 /// @param load_all_types if set to false only the types that are
15915 /// reachable from publicly exported declarations (of functions and
15916 /// variables) are read. If set to true then all types found in the
15917 /// debug information are loaded.
15918 ///
15919 /// @param resulting_corp a pointer to the resulting abigail::corpus.
15920 ///
15921 /// @return the resulting status.
15922 corpus_sptr
read_corpus_from_elf(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,fe_iface::status & status)15923 read_corpus_from_elf(const std::string& elf_path,
15924 const vector<char**>& debug_info_root_paths,
15925 environment& environment,
15926 bool load_all_types,
15927 fe_iface::status& status)
15928 {
15929 elf_based_reader_sptr rdr =
15930 dwarf::reader::create(elf_path, debug_info_root_paths,
15931 environment, load_all_types,
15932 /*linux_kernel_mode=*/false);
15933
15934 return rdr->read_corpus(status);
15935 }
15936
15937 /// Look into the symbol tables of a given elf file and see if we find
15938 /// a given symbol.
15939 ///
15940 /// @param env the environment we are operating from.
15941 ///
15942 /// @param elf_path the path to the elf file to consider.
15943 ///
15944 /// @param symbol_name the name of the symbol to look for.
15945 ///
15946 /// @param demangle if true, try to demangle the symbol name found in
15947 /// the symbol table.
15948 ///
15949 /// @param syms the vector of symbols found with the name @p symbol_name.
15950 ///
15951 /// @return true iff the symbol was found among the publicly exported
15952 /// symbols of the ELF file.
15953 bool
lookup_symbol_from_elf(const environment & env,const string & elf_path,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms)15954 lookup_symbol_from_elf(const environment& env,
15955 const string& elf_path,
15956 const string& symbol_name,
15957 bool demangle,
15958 vector<elf_symbol_sptr>& syms)
15959
15960 {
15961 if (elf_version(EV_CURRENT) == EV_NONE)
15962 return false;
15963
15964 int fd = open(elf_path.c_str(), O_RDONLY);
15965 if (fd < 0)
15966 return false;
15967
15968 struct stat s;
15969 if (fstat(fd, &s))
15970 return false;
15971
15972 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
15973 if (elf == 0)
15974 return false;
15975
15976 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
15977 demangle, syms);
15978 elf_end(elf);
15979 close(fd);
15980
15981 return value;
15982 }
15983
15984 /// Look into the symbol tables of an elf file to see if a public
15985 /// function of a given name is found.
15986 ///
15987 /// @param env the environment we are operating from.
15988 ///
15989 /// @param elf_path the path to the elf file to consider.
15990 ///
15991 /// @param symbol_name the name of the function to look for.
15992 ///
15993 /// @param syms the vector of public function symbols found with the
15994 /// name @p symname.
15995 ///
15996 /// @return true iff a function with symbol name @p symbol_name is
15997 /// found.
15998 bool
lookup_public_function_symbol_from_elf(environment & env,const string & path,const string & symname,vector<elf_symbol_sptr> & syms)15999 lookup_public_function_symbol_from_elf(environment& env,
16000 const string& path,
16001 const string& symname,
16002 vector<elf_symbol_sptr>& syms)
16003 {
16004 if (elf_version(EV_CURRENT) == EV_NONE)
16005 return false;
16006
16007 int fd = open(path.c_str(), O_RDONLY);
16008 if (fd < 0)
16009 return false;
16010
16011 struct stat s;
16012 if (fstat(fd, &s))
16013 return false;
16014
16015 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16016 if (elf == 0)
16017 return false;
16018
16019 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16020 elf_end(elf);
16021 close(fd);
16022
16023 return value;
16024 }
16025
16026 }// end namespace dwarf
16027
16028 }// end namespace abigail
16029