libabigail
abg-dwarf-reader.cc
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 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;
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.
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,
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
141  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 
156  offset_type()
157  : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
158  offset_(0)
159  {}
160 
161  offset_type(die_source source, Dwarf_Off offset)
162  : source_(source),
163  offset_(offset)
164  {}
165 
166  offset_type(Dwarf_Off offset)
167  : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
168  offset_(offset)
169  {}
170 
171  bool operator==(const offset_type& o) const
172  {return source_ == o.source_ && offset_ == o.offset_;}
173 
174  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
185  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
194  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>,
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>,
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 
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,
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_or_union_sptr.
268 typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_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 class.
272 typedef unordered_map<string, classes_type> string_classes_map;
273 
274 /// Convenience typedef for a map which key is a string and which
275 /// value is a vector of smart pointer to a enum.
276 typedef unordered_map<string, enums_type> string_enums_map;
277 
278 /// The abstraction of the place where a partial unit has been
279 /// imported. This is what the DW_TAG_imported_unit DIE expresses.
280 ///
281 /// This type thus contains:
282 /// - the offset to which the partial unit is imported
283 /// - the offset of the imported partial unit.
284 /// - the offset of the imported partial unit.
285 struct imported_unit_point
286 {
287  Dwarf_Off offset_of_import;
288  // The boolean below is true iff the imported unit comes from the
289  // alternate debug info file.
290  die_source imported_unit_die_source;
291  Dwarf_Off imported_unit_die_off;
292  Dwarf_Off imported_unit_cu_off;
293  Dwarf_Off imported_unit_child_off;
294 
295  /// Default constructor for @ref the type imported_unit_point.
296  imported_unit_point()
297  : offset_of_import(),
298  imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
299  imported_unit_die_off(),
300  imported_unit_cu_off(),
301  imported_unit_child_off()
302  {}
303 
304  /// Constructor of @ref the type imported_unit_point.
305  ///
306  /// @param import_off the offset of the point at which the unit has
307  /// been imported.
308  imported_unit_point(Dwarf_Off import_off)
309  : offset_of_import(import_off),
310  imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
311  imported_unit_die_off(),
312  imported_unit_cu_off(),
313  imported_unit_child_off()
314  {}
315 
316  /// Constructor of @ref the type imported_unit_point.
317  ///
318  /// @param import_off the offset of the point at which the unit has
319  /// been imported.
320  ///
321  /// @param from where the imported DIE comes from.
322  ///
323  /// @param imported_die the die of the unit that has been imported.
324  imported_unit_point(Dwarf_Off import_off,
325  const Dwarf_Die& imported_die,
326  die_source from)
327  : offset_of_import(import_off),
328  imported_unit_die_source(from),
329  imported_unit_die_off(dwarf_dieoffset
330  (const_cast<Dwarf_Die*>(&imported_die))),
331  imported_unit_cu_off(),
332  imported_unit_child_off()
333  {
334  Dwarf_Die imported_unit_child;
335 
336  ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
337  &imported_unit_child) == 0);
338 
339  imported_unit_child_off =
340  dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
341 
342  Dwarf_Die cu_die_memory;
343  Dwarf_Die *cu_die;
344 
345  cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
346  &cu_die_memory, 0, 0);
347  imported_unit_cu_off = dwarf_dieoffset(cu_die);
348  }
349 }; // struct imported_unit_point
350 
351 /// Convenience typedef for a vector of @ref imported_unit_point.
352 typedef vector<imported_unit_point> imported_unit_points_type;
353 
354 /// Convenience typedef for a vector of @ref imported_unit_point.
355 typedef unordered_map<Dwarf_Off, imported_unit_points_type>
357 
358 /// "Less than" operator for instances of @ref imported_unit_point
359 /// type.
360 ///
361 /// @param the left hand side operand of the "Less than" operator.
362 ///
363 /// @param the right hand side operand of the "Less than" operator.
364 ///
365 /// @return true iff @p l is less than @p r.
366 static bool
367 operator<(const imported_unit_point& l, const imported_unit_point& r)
368 {return l.offset_of_import < r.offset_of_import;}
369 
370 static bool
371 get_parent_die(const reader& rdr,
372  const Dwarf_Die* die,
373  Dwarf_Die& parent_die,
374  size_t where_offset);
375 
376 static bool
377 get_scope_die(const reader& rdr,
378  const Dwarf_Die* die,
379  size_t where_offset,
380  Dwarf_Die& scope_die);
381 
382 static bool
383 die_is_anonymous(const Dwarf_Die* die);
384 
385 static bool
386 die_is_anonymous_data_member(const Dwarf_Die* die);
387 
388 static bool
389 die_is_type(const Dwarf_Die* die);
390 
391 static bool
392 die_is_decl(const Dwarf_Die* die);
393 
394 static bool
395 die_is_declaration_only(Dwarf_Die* die);
396 
397 static bool
398 die_is_variable_decl(const Dwarf_Die *die);
399 
400 static bool
401 die_is_function_decl(const Dwarf_Die *die);
402 
403 static bool
404 die_has_size_attribute(const Dwarf_Die *die);
405 
406 static bool
407 die_has_no_child(const Dwarf_Die *die);
408 
409 static bool
410 die_is_namespace(const Dwarf_Die* die);
411 
412 static bool
413 die_is_unspecified(Dwarf_Die* die);
414 
415 static bool
416 die_is_void_type(Dwarf_Die* die);
417 
418 static bool
419 die_is_pointer_type(const Dwarf_Die* die);
420 
421 static bool
422 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
423 
424 static bool
425 die_is_reference_type(const Dwarf_Die* die);
426 
427 static bool
428 die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
429 
430 static bool
431 die_is_pointer_or_reference_type(const Dwarf_Die* die);
432 
433 static bool
434 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
435 
436 static bool
437 die_is_class_type(const Dwarf_Die* die);
438 
439 static bool
440 die_is_qualified_type(const Dwarf_Die* die);
441 
442 static bool
443 die_is_function_type(const Dwarf_Die *die);
444 
445 static bool
446 die_has_object_pointer(const Dwarf_Die* die,
447  Dwarf_Die& object_pointer);
448 
449 static bool
450 die_has_children(const Dwarf_Die* die);
451 
452 static bool
453 die_this_pointer_from_object_pointer(Dwarf_Die* die,
454  Dwarf_Die& this_pointer);
455 
456 static bool
457 die_this_pointer_is_const(Dwarf_Die* die);
458 
459 static bool
460 die_object_pointer_is_for_const_method(Dwarf_Die* die);
461 
462 static bool
463 is_type_die_to_be_canonicalized(const Dwarf_Die *die);
464 
465 static bool
466 die_is_at_class_scope(const reader& rdr,
467  const Dwarf_Die* die,
468  size_t where_offset,
469  Dwarf_Die& class_scope_die);
470 static bool
471 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
472  size_t expr_len,
473  int64_t& value,
474  bool& is_tls_address);
475 
477 dwarf_language_to_tu_language(size_t l);
478 
479 static bool
480 die_unsigned_constant_attribute(const Dwarf_Die* die,
481  unsigned attr_name,
482  uint64_t& cst);
483 
484 static bool
485 die_signed_constant_attribute(const Dwarf_Die*die,
486  unsigned attr_name,
487  int64_t& cst);
488 
489 static bool
490 die_constant_attribute(const Dwarf_Die *die,
491  unsigned attr_name,
492  bool is_signed,
493  array_type_def::subrange_type::bound_value &value);
494 
495 static bool
496 die_member_offset(const reader& rdr,
497  const Dwarf_Die* die,
498  int64_t& offset);
499 
500 static bool
501 form_is_DW_FORM_strx(unsigned form);
502 
503 static bool
504 form_is_DW_FORM_line_strp(unsigned form);
505 
506 static bool
507 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
508 
509 static string
510 die_name(const Dwarf_Die* die);
511 
512 static location
513 die_location(const reader& rdr, const Dwarf_Die* die);
514 
515 static bool
516 die_location_address(Dwarf_Die* die,
517  Dwarf_Addr& address,
518  bool& is_tls_address);
519 
520 static bool
521 die_die_attribute(const Dwarf_Die* die,
522  unsigned attr_name,
523  Dwarf_Die& result,
524  bool recursively = true);
525 
526 static bool
527 subrange_die_indirect_bound_value(const Dwarf_Die *die,
528  unsigned attr_name,
529  array_type_def::subrange_type::bound_value& v,
530  bool& is_signed);
531 
532 static bool
533 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
534  unsigned attr_name,
535  Dwarf_Die& referenced_subrange);
536 static string
537 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
538 
539 static string
540 build_internal_anonymous_die_name(const string &base_name,
541  size_t anonymous_type_index);
542 
543 static string
544 get_internal_anonymous_die_name(Dwarf_Die *die,
545  size_t anonymous_type_index);
546 
547 static string
548 die_qualified_type_name(const reader& rdr,
549  const Dwarf_Die* die,
550  size_t where);
551 
552 static string
553 die_qualified_decl_name(const reader& rdr,
554  const Dwarf_Die* die,
555  size_t where);
556 
557 static string
558 die_qualified_name(const reader& rdr,
559  const Dwarf_Die* die,
560  size_t where);
561 
562 static bool
563 die_qualified_type_name_empty(const reader& rdr,
564  const Dwarf_Die* die, size_t where,
565  string &qualified_name);
566 
567 static void
568 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
569  const Dwarf_Die* die,
570  size_t where_offset,
571  bool pretty_print,
572  string &return_type_name,
573  string &class_name,
574  vector<string>& parm_names,
575  bool& is_const,
576  bool& is_static);
577 
578 static string
579 die_function_signature(const reader& rdr,
580  const Dwarf_Die *die,
581  size_t where_offset);
582 
583 static bool
584 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
585 
586 static bool
587 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
588 
589 static bool
590 die_function_type_is_method_type(const reader& rdr,
591  const Dwarf_Die *die,
592  size_t where_offset,
593  Dwarf_Die& object_pointer_die,
594  Dwarf_Die& class_die,
595  bool& is_static);
596 
597 static string
598 die_pretty_print_type(reader& rdr,
599  const Dwarf_Die* die,
600  size_t where_offset);
601 
602 static string
603 die_pretty_print_decl(reader& rdr,
604  const Dwarf_Die* die,
605  size_t where_offset);
606 
607 static string
608 die_pretty_print(reader& rdr,
609  const Dwarf_Die* die,
610  size_t where_offset);
611 
612 static void
613 maybe_canonicalize_type(const type_base_sptr& t,
614  reader& rdr);
615 
616 static uint64_t
617 get_default_array_lower_bound(translation_unit::language l);
618 
619 static bool
620 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
621  Dwarf_Off,
622  imported_unit_points_type::const_iterator&);
623 
625 build_subrange_type(reader& rdr,
626  const Dwarf_Die* die,
627  size_t where_offset,
628  bool associate_type_to_die = true);
629 
630 static void
631 build_subranges_from_array_type_die(reader& rdr,
632  const Dwarf_Die* die,
634  size_t where_offset,
635  bool associate_type_to_die = true);
636 
637 static comparison_result
638 compare_dies(const reader& rdr,
639  const Dwarf_Die *l, const Dwarf_Die *r,
640  bool update_canonical_dies_on_the_fly);
641 
642 static bool
643 compare_dies_during_canonicalization(reader& rdr,
644  const Dwarf_Die *l, const Dwarf_Die *r,
645  bool update_canonical_dies_on_the_fly);
646 
647 static bool
648 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
649 
650 /// Compare a symbol name against another name, possibly demangling
651 /// the symbol_name before performing the comparison.
652 ///
653 /// @param symbol_name the symbol_name to take in account.
654 ///
655 /// @param name the second name to take in account.
656 ///
657 /// @param demangle if true, demangle @p symbol_name and compare the
658 /// result of the demangling with @p name.
659 ///
660 /// @return true iff symbol_name equals name.
661 static bool
662 compare_symbol_name(const string& symbol_name,
663  const string& name,
664  bool demangle)
665 {
666  if (demangle)
667  {
668  string m = demangle_cplus_mangled_name(symbol_name);
669  return m == name;
670  }
671  return symbol_name == name;
672 }
673 
674 /// Lookup a symbol using the SysV ELF hash table.
675 ///
676 /// Note that this function hasn't been tested. So it hasn't been
677 /// debugged yet. IOW, it is not known to work. Or rather, it's
678 /// almost like it's surely doesn't work ;-)
679 ///
680 /// Use it at your own risks. :-)
681 ///
682 ///@parm env the environment we are operating from.
683 ///
684 /// @param elf_handle the elf_handle to use.
685 ///
686 /// @param sym_name the symbol name to look for.
687 ///
688 /// @param ht_index the index (in the section headers table) of the
689 /// hash table section to use.
690 ///
691 /// @param sym_tab_index the index (in the section headers table) of
692 /// the symbol table to use.
693 ///
694 /// @param demangle if true, demangle @p sym_name before comparing it
695 /// to names from the symbol table.
696 ///
697 /// @param syms_found a vector of symbols found with the name @p
698 /// sym_name. table.
699 static bool
700 lookup_symbol_from_sysv_hash_tab(const environment& env,
701  Elf* elf_handle,
702  const string& sym_name,
703  size_t ht_index,
704  size_t sym_tab_index,
705  bool demangle,
706  vector<elf_symbol_sptr>& syms_found)
707 {
708  Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
709  ABG_ASSERT(sym_tab_section);
710 
711  Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
712  ABG_ASSERT(sym_tab_data);
713 
714  GElf_Shdr sheader_mem;
715  GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
716  &sheader_mem);
717  Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
718  ABG_ASSERT(hash_section);
719 
720  // Poke at the different parts of the hash table and get them ready
721  // to be used.
722  unsigned long hash = elf_hash(sym_name.c_str());
723  Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
724  Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
725  size_t nb_buckets = ht_data[0];
726  size_t nb_chains = ht_data[1];
727 
728  if (nb_buckets == 0)
729  // An empty hash table. Not sure if that is possible, but it
730  // would mean an empty table of exported symbols.
731  return false;
732 
733  //size_t nb_chains = ht_data[1];
734  Elf32_Word* ht_buckets = &ht_data[2];
735  Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
736 
737  // Now do the real work.
738  size_t bucket = hash % nb_buckets;
739  size_t symbol_index = ht_buckets[bucket];
740 
741  GElf_Sym symbol;
742  const char* sym_name_str;
743  size_t sym_size;
744  elf_symbol::type sym_type;
745  elf_symbol::binding sym_binding;
746  elf_symbol::visibility sym_visibility;
747  bool found = false;
748 
749  do
750  {
751  ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
752  sym_name_str = elf_strptr(elf_handle,
753  sym_tab_section_header->sh_link,
754  symbol.st_name);
755  if (sym_name_str
756  && compare_symbol_name(sym_name_str, sym_name, demangle))
757  {
758  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
759  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
760  sym_visibility =
761  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
762  sym_size = symbol.st_size;
763  elf_symbol::version ver;
764  if (get_version_for_symbol(elf_handle, symbol_index,
765  /*get_def_version=*/true, ver))
766  ABG_ASSERT(!ver.str().empty());
767  elf_symbol_sptr symbol_found =
768  elf_symbol::create(env,
769  symbol_index,
770  sym_size,
771  sym_name_str,
772  sym_type,
773  sym_binding,
774  symbol.st_shndx != SHN_UNDEF,
775  symbol.st_shndx == SHN_COMMON,
776  ver, sym_visibility);
777  syms_found.push_back(symbol_found);
778  found = true;
779  }
780  symbol_index = ht_chains[symbol_index];
781  } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
782 
783  return found;
784 }
785 
786 /// Get the size of the elf class, in bytes.
787 ///
788 /// @param elf_handle the elf handle to use.
789 ///
790 /// @return the size computed.
791 static char
792 get_elf_class_size_in_bytes(Elf* elf_handle)
793 {
794  char result = 0;
795  GElf_Ehdr hdr;
796 
797  ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
798  int c = hdr.e_ident[EI_CLASS];
799 
800  switch (c)
801  {
802  case ELFCLASS32:
803  result = 4;
804  break;
805  case ELFCLASS64:
806  result = 8;
807  break;
808  default:
810  }
811 
812  return result;
813 }
814 
815 /// Get a given word of a bloom filter, referred to by the index of
816 /// the word.
817 ///
818 /// The bloom word size depends on the current elf class (32 bits for
819 /// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
820 /// abstracts that nicely.
821 ///
822 /// @param elf_handle the elf handle to use.
823 ///
824 /// @param bloom_filter the bloom filter to consider.
825 ///
826 /// @param index the index of the bloom filter to return.
827 ///
828 /// @return a 64 bits work containing the bloom word found at index @p
829 /// index. Note that if we are looking at an ELFCLASS32 binary, the 4
830 /// most significant bytes of the result are going to be zero.
831 static Elf64_Xword
832 bloom_word_at(Elf* elf_handle,
833  Elf32_Word* bloom_filter,
834  size_t index)
835 {
836  Elf64_Xword result = 0;
837  GElf_Ehdr h;
838  ABG_ASSERT(gelf_getehdr(elf_handle, &h));
839  int c;
840  c = h.e_ident[EI_CLASS];
841 
842  switch(c)
843  {
844  case ELFCLASS32:
845  result = bloom_filter[index];
846  break ;
847  case ELFCLASS64:
848  {
849  Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
850  result = f[index];
851  }
852  break;
853  default:
854  abort();
855  }
856 
857  return result;
858 }
859 
860 /// The abstraction of the gnu elf hash table.
861 ///
862 /// The members of this struct are explained at
863 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
864 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
865 struct gnu_ht
866 {
867  size_t nb_buckets;
868  Elf32_Word* buckets;
869  Elf32_Word* chain;
870  size_t first_sym_index;
871  size_t bf_nwords;
872  size_t bf_size;
873  Elf32_Word* bloom_filter;
874  size_t shift;
875  size_t sym_count;
876  Elf_Scn* sym_tab_section;
877  GElf_Shdr sym_tab_section_header;
878 
879  gnu_ht()
880  : nb_buckets(0),
881  buckets(0),
882  chain(0),
883  first_sym_index(0),
884  bf_nwords(0),
885  bf_size(0),
886  bloom_filter(0),
887  shift(0),
888  sym_count(0),
889  sym_tab_section(0)
890  {}
891 }; // end struct gnu_ht
892 
893 /// Setup the members of the gnu hash table.
894 ///
895 /// @param elf_handle a handle on the elf file to use.
896 ///
897 /// @param ht_index the index (into the elf section headers table) of
898 /// the hash table section to use.
899 ///
900 /// @param sym_tab_index the index (into the elf section headers
901 /// table) of the symbol table the gnu hash table is about.
902 ///
903 /// @param ht the resulting hash table.
904 ///
905 /// @return true iff the hash table @ ht could be setup.
906 static bool
907 setup_gnu_ht(Elf* elf_handle,
908  size_t ht_index,
909  size_t sym_tab_index,
910  gnu_ht& ht)
911 {
912  ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
913  ABG_ASSERT(ht.sym_tab_section);
914  ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
915  ht.sym_count =
916  ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
917  Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
918  ABG_ASSERT(hash_section);
919 
920  // Poke at the different parts of the hash table and get them ready
921  // to be used.
922  Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
923  Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
924 
925  ht.nb_buckets = ht_data[0];
926  if (ht.nb_buckets == 0)
927  // An empty hash table. Not sure if that is possible, but it
928  // would mean an empty table of exported symbols.
929  return false;
930  ht.first_sym_index = ht_data[1];
931  // The number of words used by the bloom filter. A size of a word
932  // is ELFCLASS.
933  ht.bf_nwords = ht_data[2];
934  // The shift used by the bloom filter code.
935  ht.shift = ht_data[3];
936  // The data of the bloom filter proper.
937  ht.bloom_filter = &ht_data[4];
938  // The size of the bloom filter in 4 bytes word. This is going to
939  // be used to index the 'bloom_filter' above, which is of type
940  // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
941  // words.
942  ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
943  // The buckets of the hash table.
944  ht.buckets = ht.bloom_filter + ht.bf_size;
945  // The chain of the hash table.
946  ht.chain = ht.buckets + ht.nb_buckets;
947 
948  return true;
949 }
950 
951 /// Look into the symbol tables of the underlying elf file and find
952 /// the symbol we are being asked.
953 ///
954 /// This function uses the GNU hash table for the symbol lookup.
955 ///
956 /// The reference of for the implementation of this function can be
957 /// found at:
958 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
959 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
960 ///
961 /// @param elf_handle the elf handle to use.
962 ///
963 /// @param sym_name the name of the symbol to look for.
964 ///
965 /// @param ht_index the index of the hash table header to use.
966 ///
967 /// @param sym_tab_index the index of the symbol table header to use
968 /// with this hash table.
969 ///
970 /// @param demangle if true, demangle @p sym_name.
971 ///
972 /// @param syms_found the vector of symbols found with the name @p
973 /// sym_name.
974 ///
975 /// @return true if a symbol was actually found.
976 static bool
977 lookup_symbol_from_gnu_hash_tab(const environment& env,
978  Elf* elf_handle,
979  const string& sym_name,
980  size_t ht_index,
981  size_t sym_tab_index,
982  bool demangle,
983  vector<elf_symbol_sptr>& syms_found)
984 {
985  gnu_ht ht;
986  if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
987  return false;
988 
989  // Now do the real work.
990 
991  // Compute bloom hashes (GNU hash and second bloom specific hashes).
992  size_t h1 = elf_gnu_hash(sym_name.c_str());
993  size_t h2 = h1 >> ht.shift;
994  // The size of one of the words used in the bloom
995  // filter, in bits.
996  int c = get_elf_class_size_in_bytes(elf_handle) * 8;
997  int n = (h1 / c) % ht.bf_nwords;
998  // The bitmask of the bloom filter has a size of either 32-bits on
999  // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1000  // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1001  // type used here. When dealing with 32bits binaries, the upper
1002  // bits of the bitmask will be zero anyway.
1003  Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1004 
1005  // Test if the symbol is *NOT* present in this ELF file.
1006  if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1007  return false;
1008 
1009  size_t i = ht.buckets[h1 % ht.nb_buckets];
1010  if (i == STN_UNDEF)
1011  return false;
1012 
1013  Elf32_Word stop_word, *stop_wordp;
1014  elf_symbol::version ver;
1015  GElf_Sym symbol;
1016  const char* sym_name_str;
1017  bool found = false;
1018 
1019  elf_symbol::type sym_type;
1020  elf_symbol::binding sym_binding;
1021  elf_symbol::visibility sym_visibility;
1022 
1023  // Let's walk the hash table and record the versions of all the
1024  // symbols which name equal sym_name.
1025  for (i = ht.buckets[h1 % ht.nb_buckets],
1026  stop_wordp = &ht.chain[i - ht.first_sym_index];
1027  i != STN_UNDEF
1028  && (stop_wordp
1029  < ht.chain + (ht.sym_count - ht.first_sym_index));
1030  ++i, ++stop_wordp)
1031  {
1032  stop_word = *stop_wordp;
1033  if ((stop_word & ~ 1)!= (h1 & ~1))
1034  // A given bucket can reference several hashes. Here we
1035  // stumbled across a hash value different from the one we are
1036  // looking for. Let's keep walking.
1037  continue;
1038 
1039  ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1040  i, &symbol));
1041  sym_name_str = elf_strptr(elf_handle,
1042  ht.sym_tab_section_header.sh_link,
1043  symbol.st_name);
1044  if (sym_name_str
1045  && compare_symbol_name(sym_name_str, sym_name, demangle))
1046  {
1047  // So we found a symbol (in the symbol table) that equals
1048  // sym_name. Now lets try to get its version and record it.
1049  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1050  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1051  sym_visibility =
1052  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1053 
1054  if (get_version_for_symbol(elf_handle, i,
1055  /*get_def_version=*/true,
1056  ver))
1057  ABG_ASSERT(!ver.str().empty());
1058 
1059  elf_symbol_sptr symbol_found =
1060  elf_symbol::create(env, i,
1061  symbol.st_size,
1062  sym_name_str,
1063  sym_type, sym_binding,
1064  symbol.st_shndx != SHN_UNDEF,
1065  symbol.st_shndx == SHN_COMMON,
1066  ver, sym_visibility);
1067  syms_found.push_back(symbol_found);
1068  found = true;
1069  }
1070 
1071  if (stop_word & 1)
1072  // The last bit of the stop_word is 1. That means we need to
1073  // stop here. We reached the end of the chain of values
1074  // referenced by the hask bucket.
1075  break;
1076  }
1077  return found;
1078 }
1079 
1080 /// Look into the symbol tables of the underlying elf file and find
1081 /// the symbol we are being asked.
1082 ///
1083 /// This function uses the elf hash table (be it the GNU hash table or
1084 /// the sysv hash table) for the symbol lookup.
1085 ///
1086 /// @param env the environment we are operating from.
1087 ///
1088 /// @param elf_handle the elf handle to use.
1089 ///
1090 /// @param ht_kind the kind of hash table to use. This is returned by
1091 /// the function function find_hash_table_section_index.
1092 ///
1093 /// @param ht_index the index (in the section headers table) of the
1094 /// hash table section to use.
1095 ///
1096 /// @param sym_tab_index the index (in section headers table) of the
1097 /// symbol table index to use with this hash table.
1098 ///
1099 /// @param symbol_name the name of the symbol to look for.
1100 ///
1101 /// @param demangle if true, demangle @p sym_name.
1102 ///
1103 /// @param syms_found the symbols that were actually found with the
1104 /// name @p symbol_name.
1105 ///
1106 /// @return true iff the function found the symbol from the elf hash
1107 /// table.
1108 static bool
1109 lookup_symbol_from_elf_hash_tab(const environment& env,
1110  Elf* elf_handle,
1111  hash_table_kind ht_kind,
1112  size_t ht_index,
1113  size_t symtab_index,
1114  const string& symbol_name,
1115  bool demangle,
1116  vector<elf_symbol_sptr>& syms_found)
1117 {
1118  if (elf_handle == 0 || symbol_name.empty())
1119  return false;
1120 
1121  if (ht_kind == NO_HASH_TABLE_KIND)
1122  return false;
1123 
1124  if (ht_kind == SYSV_HASH_TABLE_KIND)
1125  return lookup_symbol_from_sysv_hash_tab(env,
1126  elf_handle, symbol_name,
1127  ht_index,
1128  symtab_index,
1129  demangle,
1130  syms_found);
1131  else if (ht_kind == GNU_HASH_TABLE_KIND)
1132  return lookup_symbol_from_gnu_hash_tab(env,
1133  elf_handle, symbol_name,
1134  ht_index,
1135  symtab_index,
1136  demangle,
1137  syms_found);
1138  return false;
1139 }
1140 
1141 /// Lookup a symbol from the symbol table directly.
1142 ///
1143 ///
1144 /// @param env the environment we are operating from.
1145 ///
1146 /// @param elf_handle the elf handle to use.
1147 ///
1148 /// @param sym_name the name of the symbol to look up.
1149 ///
1150 /// @param sym_tab_index the index (in the section headers table) of
1151 /// the symbol table section.
1152 ///
1153 /// @param demangle if true, demangle the names found in the symbol
1154 /// table before comparing them with @p sym_name.
1155 ///
1156 /// @param sym_name_found the actual name of the symbol found.
1157 ///
1158 /// @param sym_type the type of the symbol found.
1159 ///
1160 /// @param sym_binding the binding of the symbol found.
1161 ///
1162 /// @param sym_versions the versions of the symbol found.
1163 ///
1164 /// @return true iff the symbol was found.
1165 static bool
1166 lookup_symbol_from_symtab(const environment& env,
1167  Elf* elf_handle,
1168  const string& sym_name,
1169  size_t sym_tab_index,
1170  bool demangle,
1171  vector<elf_symbol_sptr>& syms_found)
1172 {
1173  // TODO: read all of the symbol table, store it in memory in a data
1174  // structure that associates each symbol with its versions and in
1175  // which lookups of a given symbol is fast.
1176  Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1177  ABG_ASSERT(sym_tab_section);
1178 
1179  GElf_Shdr header_mem;
1180  GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1181  &header_mem);
1182 
1183  size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1184  Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1185  GElf_Sym* sym;
1186  char* name_str = 0;
1187  elf_symbol::version ver;
1188  bool found = false;
1189 
1190  for (size_t i = 0; i < symcount; ++i)
1191  {
1192  GElf_Sym sym_mem;
1193  sym = gelf_getsym(symtab, i, &sym_mem);
1194  name_str = elf_strptr(elf_handle,
1195  sym_tab_header->sh_link,
1196  sym->st_name);
1197 
1198  if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1199  {
1200  elf_symbol::type sym_type =
1201  stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1202  elf_symbol::binding sym_binding =
1203  stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1204  elf_symbol::visibility sym_visibility =
1205  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1206  bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1207  bool sym_is_common = sym->st_shndx == SHN_COMMON;
1208 
1209  if (get_version_for_symbol(elf_handle, i,
1210  /*get_def_version=*/sym_is_defined,
1211  ver))
1212  ABG_ASSERT(!ver.str().empty());
1213  elf_symbol_sptr symbol_found =
1214  elf_symbol::create(env, i, sym->st_size,
1215  name_str, sym_type,
1216  sym_binding, sym_is_defined,
1217  sym_is_common, ver, sym_visibility);
1218  syms_found.push_back(symbol_found);
1219  found = true;
1220  }
1221  }
1222 
1223  if (found)
1224  return true;
1225 
1226  return false;
1227 }
1228 
1229 /// Look into the symbol tables of the underlying elf file and see
1230 /// if we find a given symbol.
1231 ///
1232 /// @param env the environment we are operating from.
1233 ///
1234 /// @param symbol_name the name of the symbol to look for.
1235 ///
1236 /// @param demangle if true, try to demangle the symbol name found in
1237 /// the symbol table before comparing it to @p symbol_name.
1238 ///
1239 /// @param syms_found the list of symbols found, with the name @p
1240 /// symbol_name.
1241 ///
1242 /// @param sym_type this is set to the type of the symbol found. This
1243 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1244 /// STT_FUNC, STT_IFUNC, etc ...
1245 ///
1246 /// Note that this parameter is set iff the function returns true.
1247 ///
1248 /// @param sym_binding this is set to the binding of the symbol found.
1249 /// This is a standard elf.h value of the symbol binding kind, that
1250 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1251 ///
1252 /// @param symbol_versions the versions of the symbol @p symbol_name,
1253 /// if it was found.
1254 ///
1255 /// @return true iff a symbol with the name @p symbol_name was found.
1256 static bool
1257 lookup_symbol_from_elf(const environment& env,
1258  Elf* elf_handle,
1259  const string& symbol_name,
1260  bool demangle,
1261  vector<elf_symbol_sptr>& syms_found)
1262 {
1263  size_t hash_table_index = 0, symbol_table_index = 0;
1264  hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1265 
1266  if (!demangle)
1267  ht_kind = find_hash_table_section_index(elf_handle,
1268  hash_table_index,
1269  symbol_table_index);
1270 
1271  if (ht_kind == NO_HASH_TABLE_KIND)
1272  {
1273  if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1274  return false;
1275 
1276  return lookup_symbol_from_symtab(env,
1277  elf_handle,
1278  symbol_name,
1279  symbol_table_index,
1280  demangle,
1281  syms_found);
1282  }
1283 
1284  return lookup_symbol_from_elf_hash_tab(env,
1285  elf_handle,
1286  ht_kind,
1287  hash_table_index,
1288  symbol_table_index,
1289  symbol_name,
1290  demangle,
1291  syms_found);
1292 }
1293 
1294 /// Look into the symbol tables of the underlying elf file and see if
1295 /// we find a given public (global or weak) symbol of function type.
1296 ///
1297 /// @param env the environment we are operating from.
1298 ///
1299 /// @param elf_handle the elf handle to use for the query.
1300 ///
1301 /// @param symbol_name the function symbol to look for.
1302 ///
1303 /// @param func_syms the vector of public functions symbols found, if
1304 /// any.
1305 ///
1306 /// @return true iff the symbol was found.
1307 static bool
1308 lookup_public_function_symbol_from_elf(environment& env,
1309  Elf* elf_handle,
1310  const string& symbol_name,
1311  vector<elf_symbol_sptr>& func_syms)
1312 {
1313  vector<elf_symbol_sptr> syms_found;
1314  bool found = false;
1315 
1316  if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1317  /*demangle=*/false, syms_found))
1318  {
1319  for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1320  i != syms_found.end();
1321  ++i)
1322  {
1323  elf_symbol::type type = (*i)->get_type();
1324  elf_symbol::binding binding = (*i)->get_binding();
1325 
1326  if ((type == elf_symbol::FUNC_TYPE
1327  || type == elf_symbol::GNU_IFUNC_TYPE
1328  || type == elf_symbol::COMMON_TYPE)
1329  && (binding == elf_symbol::GLOBAL_BINDING
1330  || binding == elf_symbol::WEAK_BINDING))
1331  {
1332  func_syms.push_back(*i);
1333  found = true;
1334  }
1335  }
1336  }
1337 
1338  return found;
1339 }
1340 
1341 // ---------------------------------------
1342 // <location expression evaluation types>
1343 // ---------------------------------------
1344 
1345 /// An abstraction of a value representing the result of the
1346 /// evaluation of a dwarf expression. This is abstraction represents
1347 /// a partial view on the possible values because we are only
1348 /// interested in extracting the latest and longuest constant
1349 /// sub-expression of a given dwarf expression.
1350 class expr_result
1351 {
1352  bool is_const_;
1353  int64_t const_value_;
1354 
1355 public:
1356  expr_result()
1357  : is_const_(true),
1358  const_value_(0)
1359  {}
1360 
1361  expr_result(bool is_const)
1362  : is_const_(is_const),
1363  const_value_(0)
1364  {}
1365 
1366  explicit expr_result(int64_t v)
1367  :is_const_(true),
1368  const_value_(v)
1369  {}
1370 
1371  /// @return true if the value is a constant. Otherwise, return
1372  /// false, meaning the value represents a quantity for which we need
1373  /// inferior (a running program) state to determine the value.
1374  bool
1375  is_const() const
1376  {return is_const_;}
1377 
1378 
1379  /// @param f a flag saying if the value is set to a constant or not.
1380  void
1381  is_const(bool f)
1382  {is_const_ = f;}
1383 
1384  /// Get the current constant value iff this represents a
1385  /// constant.
1386  ///
1387  /// @param value the out parameter. Is set to the constant value of
1388  /// the @ref expr_result. This is set iff the function return true.
1389  ///
1390  ///@return true if this has a constant value, false otherwise.
1391  bool
1392  const_value(int64_t& value)
1393  {
1394  if (is_const())
1395  {
1396  value = const_value_;
1397  return true;
1398  }
1399  return false;
1400  }
1401 
1402  /// Getter of the constant value of the current @ref expr_result.
1403  ///
1404  /// Note that the current @ref expr_result must be constant,
1405  /// otherwise the current process is aborted.
1406  ///
1407  /// @return the constant value of the current @ref expr_result.
1408  int64_t
1409  const_value() const
1410  {
1411  ABG_ASSERT(is_const());
1412  return const_value_;
1413  }
1414 
1415  operator int64_t() const
1416  {return const_value();}
1417 
1418  expr_result&
1419  operator=(const int64_t v)
1420  {
1421  const_value_ = v;
1422  return *this;
1423  }
1424 
1425  bool
1426  operator==(const expr_result& o) const
1427  {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1428 
1429  bool
1430  operator>=(const expr_result& o) const
1431  {return const_value_ >= o.const_value_;}
1432 
1433  bool
1434  operator<=(const expr_result& o) const
1435  {return const_value_ <= o.const_value_;}
1436 
1437  bool
1438  operator>(const expr_result& o) const
1439  {return const_value_ > o.const_value_;}
1440 
1441  bool
1442  operator<(const expr_result& o) const
1443  {return const_value_ < o.const_value_;}
1444 
1445  expr_result
1446  operator+(const expr_result& v) const
1447  {
1448  expr_result r(*this);
1449  r.const_value_ += v.const_value_;
1450  r.is_const_ = r.is_const_ && v.is_const_;
1451  return r;
1452  }
1453 
1454  expr_result&
1455  operator+=(int64_t v)
1456  {
1457  const_value_ += v;
1458  return *this;
1459  }
1460 
1461  expr_result
1462  operator-(const expr_result& v) const
1463  {
1464  expr_result r(*this);
1465  r.const_value_ -= v.const_value_;
1466  r.is_const_ = r.is_const_ && v.is_const_;
1467  return r;
1468  }
1469 
1470  expr_result
1471  operator%(const expr_result& v) const
1472  {
1473  expr_result r(*this);
1474  r.const_value_ %= v.const_value_;
1475  r.is_const_ = r.is_const_ && v.is_const();
1476  return r;
1477  }
1478 
1479  expr_result
1480  operator*(const expr_result& v) const
1481  {
1482  expr_result r(*this);
1483  r.const_value_ *= v.const_value_;
1484  r.is_const_ = r.is_const_ && v.is_const();
1485  return r;
1486  }
1487 
1488  expr_result
1489  operator|(const expr_result& v) const
1490  {
1491  expr_result r(*this);
1492  r.const_value_ |= v.const_value_;
1493  r.is_const_ = r.is_const_ && v.is_const_;
1494  return r;
1495  }
1496 
1497  expr_result
1498  operator^(const expr_result& v) const
1499  {
1500  expr_result r(*this);
1501  r.const_value_ ^= v.const_value_;
1502  r.is_const_ = r.is_const_ && v.is_const_;
1503  return r;
1504  }
1505 
1506  expr_result
1507  operator>>(const expr_result& v) const
1508  {
1509  expr_result r(*this);
1510  r.const_value_ = r.const_value_ >> v.const_value_;
1511  r.is_const_ = r.is_const_ && v.is_const_;
1512  return r;
1513  }
1514 
1515  expr_result
1516  operator<<(const expr_result& v) const
1517  {
1518  expr_result r(*this);
1519  r.const_value_ = r.const_value_ << v.const_value_;
1520  r.is_const_ = r.is_const_ && v.is_const_;
1521  return r;
1522  }
1523 
1524  expr_result
1525  operator~() const
1526  {
1527  expr_result r(*this);
1528  r.const_value_ = ~r.const_value_;
1529  return r;
1530  }
1531 
1532  expr_result
1533  neg() const
1534  {
1535  expr_result r(*this);
1536  r.const_value_ = -r.const_value_;
1537  return r;
1538  }
1539 
1540  expr_result
1541  abs() const
1542  {
1543  expr_result r = *this;
1544  r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1545  return r;
1546  }
1547 
1548  expr_result
1549  operator&(const expr_result& o)
1550  {
1551  expr_result r(*this);
1552  r.const_value_ &= o.const_value_;
1553  r.is_const_ = r.is_const_ && o.is_const_;
1554  return r;
1555  }
1556 
1557  expr_result
1558  operator/(const expr_result& o)
1559  {
1560  expr_result r(*this);
1561  r.is_const_ = r.is_const_ && o.is_const_;
1562  return r.const_value() / o.const_value();
1563  }
1564 };// class end expr_result;
1565 
1566 /// A class that implements a stack of @ref expr_result, to be used in
1567 /// the engine evaluating DWARF expressions.
1568 class expr_result_stack_type
1569 {
1570  vector<expr_result> elems_;
1571 
1572 public:
1573 
1574  expr_result_stack_type()
1575  {elems_.reserve(4);}
1576 
1577  expr_result&
1578  operator[](unsigned i)
1579  {
1580  unsigned s = elems_.size();
1581  ABG_ASSERT(s > i);
1582  return elems_[s - 1 -i];
1583  }
1584 
1585  const expr_result&
1586  operator[](unsigned i) const
1587  {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1588 
1589  unsigned
1590  size() const
1591  {return elems_.size();}
1592 
1593  vector<expr_result>::reverse_iterator
1594  begin()
1595  {return elems_.rbegin();}
1596 
1597  const vector<expr_result>::reverse_iterator
1598  begin() const
1599  {return const_cast<expr_result_stack_type*>(this)->begin();}
1600 
1601  vector<expr_result>::reverse_iterator
1602  end()
1603  {return elems_.rend();}
1604 
1605  const vector<expr_result>::reverse_iterator
1606  end() const
1607  {return const_cast<expr_result_stack_type*>(this)->end();}
1608 
1609  expr_result&
1610  front()
1611  {return elems_.back();}
1612 
1613  const expr_result&
1614  front() const
1615  {return const_cast<expr_result_stack_type*>(this)->front();}
1616 
1617  void
1618  push_front(expr_result e)
1619  {elems_.push_back(e);}
1620 
1621  expr_result
1622  pop_front()
1623  {
1624  expr_result r = front();
1625  elems_.pop_back();
1626  return r;
1627  }
1628 
1629  void
1630  erase(vector<expr_result>::reverse_iterator i)
1631  {elems_.erase(--i.base());}
1632 
1633  void
1634  clear()
1635  {elems_.clear();}
1636 }; // end class expr_result_stack_type
1637 
1638 /// Abstraction of the evaluation context of a dwarf expression.
1639 struct dwarf_expr_eval_context
1640 {
1641  expr_result accum;
1642  expr_result_stack_type stack;
1643  // Is set to true if the result of the expression that got evaluated
1644  // is a TLS address.
1645  bool set_tls_addr;
1646 
1647  dwarf_expr_eval_context()
1648  : accum(/*is_const=*/false),
1649  set_tls_addr(false)
1650  {
1651  stack.push_front(expr_result(true));
1652  }
1653 
1654  void
1655  reset()
1656  {
1657  stack.clear();
1658  stack.push_front(expr_result(true));
1659  accum = expr_result(false);
1660  set_tls_addr = false;
1661  }
1662 
1663  /// Set a flag to to tell that the result of the expression that got
1664  /// evaluated is a TLS address.
1665  ///
1666  /// @param f true iff the result of the expression that got
1667  /// evaluated is a TLS address, false otherwise.
1668  void
1669  set_tls_address(bool f)
1670  {set_tls_addr = f;}
1671 
1672  /// Getter for the flag that tells if the result of the expression
1673  /// that got evaluated is a TLS address.
1674  ///
1675  /// @return true iff the result of the expression that got evaluated
1676  /// is a TLS address.
1677  bool
1678  set_tls_address() const
1679  {return set_tls_addr;}
1680 
1681  expr_result
1682  pop()
1683  {
1684  expr_result r = stack.front();
1685  stack.pop_front();
1686  return r;
1687  }
1688 
1689  void
1690  push(const expr_result& v)
1691  {stack.push_front(v);}
1692 };//end class dwarf_expr_eval_context
1693 
1694 // ---------------------------------------
1695 // </location expression evaluation types>
1696 // ---------------------------------------
1697 
1698 class reader;
1699 
1700 typedef shared_ptr<reader> reader_sptr;
1701 
1702 /// The DWARF reader used to build the ABI corpus from debug info in
1703 /// DWARF format.
1704 ///
1705 /// This type is to be instanciated
1706 /// abigail::dwarf::reader::create().
1707 class reader : public elf_based_reader
1708 {
1709 public:
1710 
1711  /// A set of containers that contains one container per kind of @ref
1712  /// die_source. This allows to associate DIEs to things, depending
1713  /// on the source of the DIE.
1714  template <typename ContainerType>
1715  class die_source_dependant_container_set
1716  {
1717  ContainerType primary_debug_info_container_;
1718  ContainerType alt_debug_info_container_;
1719  ContainerType type_unit_container_;
1720 
1721  public:
1722 
1723  /// Getter for the container associated to DIEs coming from a
1724  /// given @ref die_source.
1725  ///
1726  /// @param source the die_source for which we want the container.
1727  ///
1728  /// @return the container that associates DIEs coming from @p
1729  /// source to something.
1730  ContainerType&
1731  get_container(die_source source)
1732  {
1733  ContainerType *result = 0;
1734  switch (source)
1735  {
1736  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1737  result = &primary_debug_info_container_;
1738  break;
1739  case ALT_DEBUG_INFO_DIE_SOURCE:
1740  result = &alt_debug_info_container_;
1741  break;
1742  case TYPE_UNIT_DIE_SOURCE:
1743  result = &type_unit_container_;
1744  break;
1745  case NO_DEBUG_INFO_DIE_SOURCE:
1746  case NUMBER_OF_DIE_SOURCES:
1748  }
1749  return *result;
1750  }
1751 
1752  /// Getter for the container associated to DIEs coming from a
1753  /// given @ref die_source.
1754  ///
1755  /// @param source the die_source for which we want the container.
1756  ///
1757  /// @return the container that associates DIEs coming from @p
1758  /// source to something.
1759  const ContainerType&
1760  get_container(die_source source) const
1761  {
1762  return const_cast<die_source_dependant_container_set*>(this)->
1763  get_container(source);
1764  }
1765 
1766  /// Getter for the container associated to DIEs coming from the
1767  /// same source as a given DIE.
1768  ///
1769  /// @param rdr the DWARF reader to consider.
1770  ///
1771  /// @param die the DIE which should have the same source as the
1772  /// source of the container we want.
1773  ///
1774  /// @return the container that associates DIEs coming from the
1775  /// same source as @p die.
1776  ContainerType&
1777  get_container(const reader& rdr, const Dwarf_Die *die)
1778  {
1779  const die_source source = rdr.get_die_source(die);
1780  return get_container(source);
1781  }
1782 
1783  /// Getter for the container associated to DIEs coming from the
1784  /// same source as a given DIE.
1785  ///
1786  /// @param rdr the DWARF reader to consider.
1787  ///
1788  /// @param die the DIE which should have the same source as the
1789  /// source of the container we want.
1790  ///
1791  /// @return the container that associates DIEs coming from the
1792  /// same source as @p die.
1793  const ContainerType&
1794  get_container(const reader& rdr, const Dwarf_Die *die) const
1795  {
1796  return const_cast<die_source_dependant_container_set*>(this)->
1797  get_container(rdr, die);
1798  }
1799 
1800  /// Clear the container set.
1801  void
1802  clear()
1803  {
1804  primary_debug_info_container_.clear();
1805  alt_debug_info_container_.clear();
1806  type_unit_container_.clear();
1807  }
1808  }; // end die_dependant_container_set
1809 
1810  unsigned short dwarf_version_;
1811  Dwarf_Die* cur_tu_die_;
1812  mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1813  // A set of maps (one per kind of die source) that associates a decl
1814  // string representation with the DIEs (offsets) representing that
1815  // decl.
1816  mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1817  decl_die_repr_die_offsets_maps_;
1818  // A set of maps (one per kind of die source) that associates a type
1819  // string representation with the DIEs (offsets) representing that
1820  // type.
1821  mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1822  type_die_repr_die_offsets_maps_;
1823  mutable die_source_dependant_container_set<die_istring_map_type>
1824  die_qualified_name_maps_;
1825  mutable die_source_dependant_container_set<die_istring_map_type>
1826  die_pretty_repr_maps_;
1827  mutable die_source_dependant_container_set<die_istring_map_type>
1828  die_pretty_type_repr_maps_;
1829  // A set of maps (one per kind of die source) that associates the
1830  // offset of a decl die to its corresponding decl artifact.
1831  mutable die_source_dependant_container_set<die_artefact_map_type>
1832  decl_die_artefact_maps_;
1833  // A set of maps (one per kind of die source) that associates the
1834  // offset of a type die to its corresponding type artifact.
1835  mutable die_source_dependant_container_set<die_artefact_map_type>
1836  type_die_artefact_maps_;
1837  /// A set of vectors (one per kind of die source) that associates
1838  /// the offset of a type DIE to the offset of its canonical DIE.
1839  mutable die_source_dependant_container_set<offset_offset_map_type>
1840  canonical_type_die_offsets_;
1841  /// A set of vectors (one per kind of die source) that associates
1842  /// the offset of a decl DIE to the offset of its canonical DIE.
1843  mutable die_source_dependant_container_set<offset_offset_map_type>
1844  canonical_decl_die_offsets_;
1845  /// A map that associates a function type representations to
1846  /// function types, inside a translation unit.
1847  mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1848  /// A map that associates a pair of DIE offsets to the result of the
1849  /// comparison of that pair.
1850  mutable std::unordered_map<std::pair<offset_type,offset_type>,
1852  dwarf_offset_pair_hash> die_comparison_results_;
1853  // The set of types pair that have been canonical-type-propagated.
1854  mutable offset_pair_set_type propagated_types_;
1855  die_class_or_union_map_type die_wip_classes_map_;
1856  die_class_or_union_map_type alternate_die_wip_classes_map_;
1857  die_class_or_union_map_type type_unit_die_wip_classes_map_;
1858  die_function_type_map_type die_wip_function_types_map_;
1859  die_function_type_map_type alternate_die_wip_function_types_map_;
1860  die_function_type_map_type type_unit_die_wip_function_types_map_;
1861  die_function_decl_map_type die_function_with_no_symbol_map_;
1862  vector<type_base_sptr> types_to_canonicalize_;
1863  string_classes_or_unions_map decl_only_classes_map_;
1864  string_enums_map decl_only_enums_map_;
1865  die_tu_map_type die_tu_map_;
1866  translation_unit_sptr cur_tu_;
1867  scope_decl_sptr nil_scope_;
1868  scope_stack_type scope_stack_;
1869  offset_offset_map_type primary_die_parent_map_;
1870  // A map that associates each tu die to a vector of unit import
1871  // points, in the main debug info
1872  tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1873  // A map that associates each tu die to a vector of unit import
1874  // points, in the alternate debug info
1875  tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1876  tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1877  // A DIE -> parent map for DIEs coming from the alternate debug info
1878  // file.
1879  offset_offset_map_type alternate_die_parent_map_;
1880  offset_offset_map_type type_section_die_parent_map_;
1881  list<var_decl_sptr> var_decls_to_add_;
1882 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1883  bool debug_die_canonicalization_is_on_;
1884  bool use_canonical_die_comparison_;
1885 #endif
1886  mutable size_t compare_count_;
1887  mutable size_t canonical_propagated_count_;
1888  mutable size_t cancelled_propagation_count_;
1889  mutable optional<bool> leverage_dwarf_factorization_;
1890 
1891 protected:
1892 
1893  reader() = delete;
1894 
1895  /// Constructor of reader.
1896  ///
1897  /// @param elf_path the path to the elf file the context is to be
1898  /// used for.
1899  ///
1900  /// @param debug_info_root_paths a vector of pointers to the path to
1901  /// the root directory under which the debug info is to be found for
1902  /// @p elf_path. Leave this empty if the debug info is not in a
1903  /// split file.
1904  ///
1905  /// @param environment the environment used by the current context.
1906  /// This environment contains resources needed by the DWARF reader and by
1907  /// the types and declarations that are to be created later. Note
1908  /// that ABI artifacts that are to be compared all need to be
1909  /// created within the same environment.
1910  ///
1911  /// Please also note that the life time of this environment object
1912  /// must be greater than the life time of the resulting @ref
1913  /// reader the context uses resources that are allocated in
1914  /// the environment.
1915  ///
1916  /// @param load_all_types if set to false only the types that are
1917  /// reachable from publicly exported declarations (of functions and
1918  /// variables) are read. If set to true then all types found in the
1919  /// debug information are loaded.
1920  ///
1921  /// @param linux_kernel_mode if set to true, then consider the special
1922  /// linux kernel symbol tables when determining if a symbol is
1923  /// exported or not.
1924  reader(const string& elf_path,
1925  const vector<char**>& debug_info_root_paths,
1926  environment& environment,
1927  bool load_all_types,
1928  bool linux_kernel_mode)
1929  : elf_based_reader(elf_path,
1930  debug_info_root_paths,
1931  environment)
1932  {
1933  initialize(load_all_types, linux_kernel_mode);
1934  }
1935 
1936 public:
1937 
1938  /// Initializer of reader.
1939  ///
1940  /// Resets the reader so that it can be re-used to read another binary.
1941  ///
1942  /// @param load_all_types if set to false only the types that are
1943  /// reachable from publicly exported declarations (of functions and
1944  /// variables) are read. If set to true then all types found in the
1945  /// debug information are loaded.
1946  ///
1947  /// @param linux_kernel_mode if set to true, then consider the
1948  /// special linux kernel symbol tables when determining if a symbol
1949  /// is exported or not.
1950  void
1951  initialize(bool load_all_types, bool linux_kernel_mode)
1952  {
1953  dwarf_version_ = 0;
1954  cur_tu_die_ = 0;
1955  decl_die_repr_die_offsets_maps_.clear();
1956  type_die_repr_die_offsets_maps_.clear();
1957  die_qualified_name_maps_.clear();
1958  die_pretty_repr_maps_.clear();
1959  die_pretty_type_repr_maps_.clear();
1960  decl_die_artefact_maps_.clear();
1961  type_die_artefact_maps_.clear();
1962  canonical_type_die_offsets_.clear();
1963  canonical_decl_die_offsets_.clear();
1964  die_wip_classes_map_.clear();
1965  alternate_die_wip_classes_map_.clear();
1966  type_unit_die_wip_classes_map_.clear();
1967  die_wip_function_types_map_.clear();
1968  alternate_die_wip_function_types_map_.clear();
1969  type_unit_die_wip_function_types_map_.clear();
1970  die_function_with_no_symbol_map_.clear();
1971  types_to_canonicalize_.clear();
1972  decl_only_classes_map_.clear();
1973  die_tu_map_.clear();
1974  corpus().reset();
1975  corpus_group().reset();
1976  cur_tu_.reset();
1977  primary_die_parent_map_.clear();
1978  tu_die_imported_unit_points_map_.clear();
1979  alt_tu_die_imported_unit_points_map_.clear();
1980  type_units_tu_die_imported_unit_points_map_.clear();
1981  alternate_die_parent_map_.clear();
1982  type_section_die_parent_map_.clear();
1983  var_decls_to_add_.clear();
1984  clear_per_translation_unit_data();
1985  options().load_in_linux_kernel_mode = linux_kernel_mode;
1986  options().load_all_types = load_all_types;
1987 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1988  debug_die_canonicalization_is_on_ =
1989  env().debug_die_canonicalization_is_on();
1990  use_canonical_die_comparison_ = true;
1991 #endif
1992  compare_count_ = 0;
1993  canonical_propagated_count_ = 0;
1994  cancelled_propagation_count_ = 0;
1995  load_in_linux_kernel_mode(linux_kernel_mode);
1996  }
1997 
1998  /// Initializer of reader.
1999  ///
2000  /// Resets the reader so that it can be re-used to read another binary.
2001  ///
2002  /// @param elf_path the path to the new ELF file.
2003  ///
2004  /// @param debug_info_root_paths the vector of debug-info path to
2005  /// look for split debug info.
2006  ///
2007  /// @param load_all_types if set to false only the types that are
2008  /// reachable from publicly exported declarations (of functions and
2009  /// variables) are read. If set to true then all types found in the
2010  /// debug information are loaded.
2011  ///
2012  /// @param linux_kernel_mode if set to true, then consider the
2013  /// special linux kernel symbol tables when determining if a symbol
2014  /// is exported or not.
2015  void
2016  initialize(const string& elf_path,
2017  const vector<char**>& debug_info_root_paths,
2018  bool load_all_types,
2019  bool linux_kernel_mode)
2020  {
2021  reset(elf_path, debug_info_root_paths);
2022  initialize(load_all_types, linux_kernel_mode);
2023  }
2024 
2025  /// Create an instance of DWARF Reader.
2026  ///
2027  /// @param elf_path the path to the ELF file to read from.
2028  ///
2029  /// @param debug_info_root_paths a vector of paths where to look up
2030  /// split debug info files.
2031  ///
2032  /// @param environment the environment to be used by the reader.
2033  ///
2034  /// @param load_all_types if set to false only the types that are
2035  /// reachable from publicly exported declarations (of functions and
2036  /// variables) are read. If set to true then all types found in the
2037  /// debug information are loaded.
2038  ///
2039  /// @param linux_kernel_mode if set to true, then consider the
2040  /// special linux kernel symbol tables when determining if a symbol
2041  /// is exported or not.
2042  static dwarf::reader_sptr
2043  create(const std::string& elf_path,
2044  const vector<char**>& debug_info_root_paths,
2045  environment& environment,
2046  bool load_all_types,
2047  bool linux_kernel_mode)
2048  {
2049  reader_sptr result(new reader(elf_path, debug_info_root_paths,
2050  environment, load_all_types,
2051  linux_kernel_mode));
2052  return result;
2053  }
2054 
2055  /// Destructor of the @ref reader type.
2056  ~reader()
2057  {
2058  }
2059 
2060  /// Read and analyze the ELF and DWARF information associated with
2061  /// the underlying ELF file and build an ABI corpus out of it.
2062  ///
2063  /// @param status output parameter. This is set to the status of
2064  /// the analysis of the debug info.
2065  ///
2066  /// @return the resulting ABI corpus.
2067  corpus_sptr
2068  read_corpus(status& status)
2069  {
2070  status = STATUS_UNKNOWN;
2071 
2072  // Load the generic ELF parts of the corpus.
2073  elf::reader::read_corpus(status);
2074 
2075  if ((status & STATUS_NO_SYMBOLS_FOUND)
2076  || !(status & STATUS_OK))
2077  // Either we couldn't find ELF symbols or something went badly
2078  // wrong. There is nothing we can do with this ELF file. Bail
2079  // out.
2080  return corpus_sptr();
2081 
2082  // If we couldn't find debug info from the elf path, then say it.
2083  if (dwarf_debug_info() == nullptr)
2084  status |= STATUS_DEBUG_INFO_NOT_FOUND;
2085 
2086  {
2087  string alt_di_path;
2088  if (refers_to_alt_debug_info(alt_di_path)
2089  && !alternate_dwarf_debug_info())
2090  status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
2091  }
2092 
2093  if (// If debug info was found but not the required alternate debug
2094  // info ...
2095  ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2096  && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2097  // ... then we cannot handle the binary.
2098  return corpus_sptr();
2099 
2100  // Read the variable and function descriptions from the debug info
2101  // we have, through the dwfl handle.
2102  corpus_sptr corp = read_debug_info_into_corpus();
2103 
2104  status |= STATUS_OK;
2105 
2106  return corp;
2107  }
2108 
2109  /// Read an analyze the DWARF information.
2110  ///
2111  /// Construct an ABI corpus from it.
2112  ///
2113  /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2114  ///
2115  /// @return the resulting ABI corpus.
2116  corpus_sptr
2117  read_debug_info_into_corpus()
2118  {
2119  clear_per_corpus_data();
2120 
2121  // First set some mundane properties of the corpus gathered from
2122  // ELF.
2123  corpus::origin origin = corpus()->get_origin();
2124  origin |= corpus::DWARF_ORIGIN;
2125  corpus()->set_origin(origin);
2126 
2127  if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2128  && !env().user_set_analyze_exported_interfaces_only())
2129  // So we are looking at the Linux Kernel and the user has not set
2130  // any particular option regarding the amount of types to analyse.
2131  // In that case, we need to only analyze types that are reachable
2132  // from exported interfaces otherwise we get such a massive amount
2133  // of type DIEs to look at that things are just too slow down the
2134  // road.
2135  env().analyze_exported_interfaces_only(true);
2136 
2137  corpus()->set_soname(dt_soname());
2138  corpus()->set_needed(dt_needed());
2139  corpus()->set_architecture_name(elf_architecture());
2140  // Set symbols information to the corpus.
2141  corpus()->set_symtab(symtab());
2142 
2143  // Get out now if no debug info is found or if the symbol table is
2144  // empty.
2145  if (!dwarf_debug_info()
2146  || !corpus()->get_symtab()->has_symbols())
2147  return corpus();
2148 
2149  uint8_t address_size = 0;
2150  size_t header_size = 0;
2151 
2152 #ifdef WITH_DEBUG_SELF_COMPARISON
2153  if (env().self_comparison_debug_is_on())
2154  env().set_self_comparison_debug_input(corpus());
2155 #endif
2156 
2157  // Walk all the DIEs of the debug info to build a DIE -> parent map
2158  // useful for get_die_parent() to work.
2159  {
2160  tools_utils::timer t;
2161  if (do_log())
2162  {
2163  cerr << "building die -> parent maps ...";
2164  t.start();
2165  }
2166 
2167  build_die_parent_maps();
2168 
2169  if (do_log())
2170  {
2171  t.stop();
2172  cerr << " DONE@" << corpus()->get_path()
2173  << ":"
2174  << t
2175  << "\n";
2176  }
2177  }
2178 
2179  env().canonicalization_is_done(false);
2180 
2181  {
2182  tools_utils::timer t;
2183  if (do_log())
2184  {
2185  cerr << "building the libabigail internal representation ...";
2186  t.start();
2187  }
2188  // And now walk all the DIEs again to build the libabigail IR.
2189  Dwarf_Half dwarf_vers = 0;
2190  for (Dwarf_Off offset = 0, next_offset = 0;
2191  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2192  offset, &next_offset, &header_size,
2193  &dwarf_vers, NULL, &address_size, NULL,
2194  NULL, NULL) == 0);
2195  offset = next_offset)
2196  {
2197  Dwarf_Off die_offset = offset + header_size;
2198  Dwarf_Die unit;
2199  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2200  die_offset, &unit)
2201  || dwarf_tag(&unit) != DW_TAG_compile_unit)
2202  continue;
2203 
2204  dwarf_version(dwarf_vers);
2205 
2206  address_size *= 8;
2207 
2208  // Build a translation_unit IR node from cu; note that cu must
2209  // be a DW_TAG_compile_unit die.
2210  translation_unit_sptr ir_node =
2211  build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2212  ABG_ASSERT(ir_node);
2213  }
2214  if (do_log())
2215  {
2216  t.stop();
2217  cerr << " DONE@" << corpus()->get_path()
2218  << ":"
2219  << t
2220  << "\n";
2221 
2222  cerr << "Number of aggregate types compared: "
2223  << compare_count_ << "\n"
2224  << "Number of canonical types propagated: "
2225  << canonical_propagated_count_ << "\n"
2226  << "Number of cancelled propagated canonical types:"
2227  << cancelled_propagation_count_ << "\n";
2228  }
2229  }
2230 
2231  {
2232  tools_utils::timer t;
2233  if (do_log())
2234  {
2235  cerr << "resolving declaration only classes ...";
2236  t.start();
2237  }
2238  resolve_declaration_only_classes();
2239  if (do_log())
2240  {
2241  t.stop();
2242  cerr << " DONE@" << corpus()->get_path()
2243  << ":"
2244  << t
2245  <<"\n";
2246  }
2247  }
2248 
2249  {
2250  tools_utils::timer t;
2251  if (do_log())
2252  {
2253  cerr << "resolving declaration only enums ...";
2254  t.start();
2255  }
2256  resolve_declaration_only_enums();
2257  if (do_log())
2258  {
2259  t.stop();
2260  cerr << " DONE@" << corpus()->get_path()
2261  << ":"
2262  << t
2263  <<"\n";
2264  }
2265  }
2266 
2267  {
2268  tools_utils::timer t;
2269  if (do_log())
2270  {
2271  cerr << "fixing up functions with linkage name but "
2272  << "no advertised underlying symbols ....";
2273  t.start();
2274  }
2275  fixup_functions_with_no_symbols();
2276  if (do_log())
2277  {
2278  t.stop();
2279  cerr << " DONE@" << corpus()->get_path()
2280  <<":"
2281  << t
2282  <<"\n";
2283  }
2284  }
2285 
2286  /// Now, look at the types that needs to be canonicalized after the
2287  /// translation has been constructed (which is just now) and
2288  /// canonicalize them.
2289  ///
2290  /// These types need to be constructed at the end of the translation
2291  /// unit reading phase because some types are modified by some DIEs
2292  /// even after the principal DIE describing the type has been read;
2293  /// this happens for clones of virtual destructors (for instance) or
2294  /// even for some static data members. We need to do that for types
2295  /// are in the alternate debug info section and for types that in
2296  /// the main debug info section.
2297  {
2298  tools_utils::timer t;
2299  if (do_log())
2300  {
2301  cerr << "perform late type canonicalizing ...\n";
2302  t.start();
2303  }
2304 
2305  perform_late_type_canonicalizing();
2306  if (do_log())
2307  {
2308  t.stop();
2309  cerr << "late type canonicalizing DONE@"
2310  << corpus()->get_path()
2311  << ":"
2312  << t
2313  << "\n";
2314  }
2315  }
2316 
2317  env().canonicalization_is_done(true);
2318 
2319  {
2320  tools_utils::timer t;
2321  if (do_log())
2322  {
2323  cerr << "sort functions and variables ...";
2324  t.start();
2325  }
2326  corpus()->sort_functions();
2327  corpus()->sort_variables();
2328  if (do_log())
2329  {
2330  t.stop();
2331  cerr << " DONE@" << corpus()->get_path()
2332  << ":"
2333  << t
2334  <<" \n";
2335  }
2336  }
2337 
2338  return corpus();
2339  }
2340 
2341  /// Clear the data that is relevant only for the current translation
2342  /// unit being read. The rest of the data is relevant for the
2343  /// entire ABI corpus.
2344  void
2345  clear_per_translation_unit_data()
2346  {
2347  while (!scope_stack().empty())
2348  scope_stack().pop();
2349  var_decls_to_re_add_to_tree().clear();
2350  per_tu_repr_to_fn_type_maps().clear();
2351  }
2352 
2353  /// Clear the data that is relevant for the current corpus being
2354  /// read.
2355  void
2356  clear_per_corpus_data()
2357  {
2358  die_qualified_name_maps_.clear();
2359  die_pretty_repr_maps_.clear();
2360  die_pretty_type_repr_maps_.clear();
2361  clear_types_to_canonicalize();
2362  }
2363 
2364  /// Getter for the current environment.
2365  ///
2366  /// @return the current environment.
2367  environment&
2368  env()
2369  {return options().env;}
2370 
2371  /// Getter for the current environment.
2372  ///
2373  /// @return the current environment.
2374  const environment&
2375  env() const
2376  {return const_cast<reader*>(this)->env();}
2377 
2378  /// Getter for the flag that tells us if we are dropping functions
2379  /// and variables that have undefined symbols.
2380  ///
2381  /// @return true iff we are dropping functions and variables that have
2382  /// undefined symbols.
2383  bool
2384  drop_undefined_syms() const
2385  {return options().drop_undefined_syms;}
2386 
2387  /// Setter for the flag that tells us if we are dropping functions
2388  /// and variables that have undefined symbols.
2389  ///
2390  /// @param f the new value of the flag.
2391  void
2392  drop_undefined_syms(bool f)
2393  {options().drop_undefined_syms = f;}
2394 
2395  /// Getter of the DWARF version.
2396  unsigned short
2397  dwarf_version() const
2398  {return dwarf_version_;}
2399 
2400  void
2401  dwarf_version(unsigned short v)
2402  {dwarf_version_ = v;}
2403 
2404  /// Return the ELF descriptor used for DWARF access.
2405  ///
2406  /// This can be the same as reader::elf_handle() above, if the
2407  /// DWARF info is in the same ELF file as the one of the binary we
2408  /// are analizing. It is different if e.g, the debug info is split
2409  /// from the ELF file we are analizing.
2410  ///
2411  /// @return a pointer to the ELF descriptor used to access debug
2412  /// info.
2413  Elf*
2414  dwarf_elf_handle() const
2415  {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2416 
2417  /// Test if the debug information is in a separate ELF file wrt the
2418  /// main ELF file of the program (application or shared library) we
2419  /// are analizing.
2420  ///
2421  /// @return true if the debug information is in a separate ELF file
2422  /// compared to the main ELF file of the program (application or
2423  /// shared library) that we are looking at.
2424  bool
2425  dwarf_is_splitted() const
2426  {return dwarf_elf_handle() != elf_handle();}
2427 
2428  /// Return the correct debug info, depending on the DIE source we
2429  /// are looking at.
2430  ///
2431  /// @param source the DIE source to consider.
2432  ///
2433  /// @return the right debug info, depending on @p source.
2434  const Dwarf*
2435  dwarf_per_die_source(die_source source) const
2436  {
2437  const Dwarf *result = 0;
2438  switch(source)
2439  {
2440  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2441  case TYPE_UNIT_DIE_SOURCE:
2442  result = dwarf_debug_info();
2443  break;
2444  case ALT_DEBUG_INFO_DIE_SOURCE:
2445  result = alternate_dwarf_debug_info();
2446  break;
2447  case NO_DEBUG_INFO_DIE_SOURCE:
2448  case NUMBER_OF_DIE_SOURCES:
2450  }
2451  return result;
2452  }
2453 
2454  /// Return the path to the ELF path we are reading.
2455  ///
2456  /// @return the elf path.
2457  const string&
2458  elf_path() const
2459  {return corpus_path();}
2460 
2461  const Dwarf_Die*
2462  cur_tu_die() const
2463  {return cur_tu_die_;}
2464 
2465  void
2466  cur_tu_die(Dwarf_Die* cur_tu_die)
2467  {cur_tu_die_ = cur_tu_die;}
2468 
2469  dwarf_expr_eval_context&
2470  dwarf_expr_eval_ctxt() const
2471  {return dwarf_expr_eval_context_;}
2472 
2473  /// Getter of the maps set that associates a representation of a
2474  /// decl DIE to a vector of offsets of DIEs having that representation.
2475  ///
2476  /// @return the maps set that associates a representation of a decl
2477  /// DIE to a vector of offsets of DIEs having that representation.
2478  const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2479  decl_die_repr_die_offsets_maps() const
2480  {return decl_die_repr_die_offsets_maps_;}
2481 
2482  /// Getter of the maps set that associates a representation of a
2483  /// decl DIE to a vector of offsets of DIEs having that representation.
2484  ///
2485  /// @return the maps set that associates a representation of a decl
2486  /// DIE to a vector of offsets of DIEs having that representation.
2487  die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2488  decl_die_repr_die_offsets_maps()
2489  {return decl_die_repr_die_offsets_maps_;}
2490 
2491  /// Getter of the maps set that associate a representation of a type
2492  /// DIE to a vector of offsets of DIEs having that representation.
2493  ///
2494  /// @return the maps set that associate a representation of a type
2495  /// DIE to a vector of offsets of DIEs having that representation.
2496  const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2497  type_die_repr_die_offsets_maps() const
2498  {return type_die_repr_die_offsets_maps_;}
2499 
2500  /// Getter of the maps set that associate a representation of a type
2501  /// DIE to a vector of offsets of DIEs having that representation.
2502  ///
2503  /// @return the maps set that associate a representation of a type
2504  /// DIE to a vector of offsets of DIEs having that representation.
2505  die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2506  type_die_repr_die_offsets_maps()
2507  {return type_die_repr_die_offsets_maps_;}
2508 
2509 
2510  /// Compute the offset of the canonical DIE of a given DIE.
2511  ///
2512  /// @param die the DIE to consider.
2513  ///
2514  /// @param canonical_die_offset out parameter. This is set to the
2515  /// resulting canonical DIE that was computed.
2516  ///
2517  /// @param die_as_type if yes, it means @p die has to be considered
2518  /// as a type.
2519  void
2520  compute_canonical_die_offset(const Dwarf_Die *die,
2521  Dwarf_Off &canonical_die_offset,
2522  bool die_as_type) const
2523  {
2524  offset_offset_map_type &canonical_dies =
2525  die_as_type
2526  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2527  get_container(*this, die)
2528  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2529  get_container(*this, die);
2530 
2531  Dwarf_Die canonical_die;
2532  compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2533 
2534  canonical_die_offset = dwarf_dieoffset(&canonical_die);
2535  }
2536 
2537  /// Compute (find) the canonical DIE of a given DIE.
2538  ///
2539  /// @param die the DIE to consider.
2540  ///
2541  /// @param canonical_dies the vector in which the canonical dies ar
2542  /// stored. The index of each element is the offset of the DIE we
2543  /// want the canonical DIE for. And the value of the element at
2544  /// that index is the canonical DIE offset we are looking for.
2545  ///
2546  /// @param canonical_die_offset out parameter. This is set to the
2547  /// resulting canonical DIE that was computed.
2548  ///
2549  /// @param die_as_type if yes, it means @p die has to be considered
2550  /// as a type.
2551  void
2552  compute_canonical_die(const Dwarf_Die *die,
2553  offset_offset_map_type& canonical_dies,
2554  Dwarf_Die &canonical_die,
2555  bool die_as_type) const
2556  {
2557  const die_source source = get_die_source(die);
2558 
2559  Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2560 
2561  compute_canonical_die(die_offset, source,
2562  canonical_dies,
2563  canonical_die, die_as_type);
2564  }
2565 
2566  /// Compute (find) the canonical DIE of a given DIE.
2567  ///
2568  /// @param die_offset the offset of the DIE to consider.
2569  ///
2570  /// @param source the source of the DIE to consider.
2571  ///
2572  /// @param canonical_dies the vector in which the canonical dies ar
2573  /// stored. The index of each element is the offset of the DIE we
2574  /// want the canonical DIE for. And the value of the element at
2575  /// that index is the canonical DIE offset we are looking for.
2576  ///
2577  /// @param canonical_die_offset out parameter. This is set to the
2578  /// resulting canonical DIE that was computed.
2579  ///
2580  /// @param die_as_type if yes, it means @p die has to be considered
2581  /// as a type.
2582  void
2583  compute_canonical_die(Dwarf_Off die_offset,
2584  die_source source,
2585  offset_offset_map_type& canonical_dies,
2586  Dwarf_Die &canonical_die,
2587  bool die_as_type) const
2588  {
2589  // The map that associates the string representation of 'die'
2590  // with a vector of offsets of potentially equivalent DIEs.
2592  die_as_type
2593  ? (const_cast<reader*>(this)->
2594  type_die_repr_die_offsets_maps().get_container(source))
2595  : (const_cast<reader*>(this)->
2596  decl_die_repr_die_offsets_maps().get_container(source));
2597 
2598  Dwarf_Die die;
2599  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2600  die_offset, &die));
2601 
2602  // The variable repr is the the string representation of 'die'.
2603  //
2604  // Even if die_as_type is true -- which means that 'die' is said
2605  // to be considered as a type -- we always consider a
2606  // DW_TAG_subprogram DIE as a decl here, as far as its string
2607  // representation is concerned.
2608  interned_string name =
2609  (die_as_type)
2610  ? get_die_pretty_type_representation(&die, /*where=*/0)
2611  : get_die_pretty_representation(&die, /*where=*/0);
2612 
2613  Dwarf_Off canonical_die_offset = 0;
2614  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2615  if (i == map.end())
2616  {
2617  dwarf_offsets_type offsets;
2618  offsets.push_back(die_offset);
2619  map[name] = offsets;
2620  set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2621  get_die_from_offset(source, die_offset, &canonical_die);
2622  return;
2623  }
2624 
2625  Dwarf_Off cur_die_offset;
2626  Dwarf_Die potential_canonical_die;
2627  for (dwarf_offsets_type::const_iterator o = i->second.begin();
2628  o != i->second.end();
2629  ++o)
2630  {
2631  cur_die_offset = *o;
2632  get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2633  if (compare_dies(*this, &die, &potential_canonical_die,
2634  /*update_canonical_dies_on_the_fly=*/false))
2635  {
2636  canonical_die_offset = cur_die_offset;
2637  set_canonical_die_offset(canonical_dies, die_offset,
2638  canonical_die_offset);
2639  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2640  return;
2641  }
2642  }
2643 
2644  canonical_die_offset = die_offset;
2645  i->second.push_back(die_offset);
2646  set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2647  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2648  }
2649 
2650  /// Getter of the canonical DIE of a given DIE.
2651  ///
2652  /// @param die the DIE to consider.
2653  ///
2654  /// @param canonical_die output parameter. Is set to the resulting
2655  /// canonical die, if this function returns true.
2656  ///
2657  /// @param where the offset of the logical DIE we are supposed to be
2658  /// calling this function from. If set to zero this means this is
2659  /// to be ignored.
2660  ///
2661  /// @param die_as_type if set to yes, it means @p die is to be
2662  /// considered as a type DIE.
2663  ///
2664  /// @return true iff a canonical DIE was found for @p die.
2665  bool
2666  get_canonical_die(const Dwarf_Die *die,
2667  Dwarf_Die &canonical_die,
2668  size_t where,
2669  bool die_as_type)
2670  {
2671  const die_source source = get_die_source(die);
2672 
2673  offset_offset_map_type &canonical_dies =
2674  die_as_type
2675  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2676  get_container(source)
2677  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2678  get_container(source);
2679 
2680  Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2681  if (Dwarf_Off canonical_die_offset =
2682  get_canonical_die_offset(canonical_dies, die_offset))
2683  {
2684  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2685  return true;
2686  }
2687 
2688  // The map that associates the string representation of 'die'
2689  // with a vector of offsets of potentially equivalent DIEs.
2691  die_as_type
2692  ? (const_cast<reader*>(this)->
2693  type_die_repr_die_offsets_maps().get_container(*this, die))
2694  : (const_cast<reader*>(this)->
2695  decl_die_repr_die_offsets_maps().get_container(*this, die));
2696 
2697  // The variable repr is the the string representation of 'die'.
2698  //
2699  // Even if die_as_type is true -- which means that 'die' is said
2700  // to be considered as a type -- we always consider a
2701  // DW_TAG_subprogram DIE as a decl here, as far as its string
2702  // representation is concerned.
2703  interned_string name =
2704  (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2705  ? get_die_pretty_type_representation(die, where)
2706  : get_die_pretty_representation(die, where);
2707 
2708  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2709  if (i == map.end())
2710  return false;
2711 
2712  Dwarf_Off cur_die_offset;
2713  for (dwarf_offsets_type::const_iterator o = i->second.begin();
2714  o != i->second.end();
2715  ++o)
2716  {
2717  cur_die_offset = *o;
2718  get_die_from_offset(source, cur_die_offset, &canonical_die);
2719  // compare die and canonical_die.
2720  if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2721  die, &canonical_die,
2722  /*update_canonical_dies_on_the_fly=*/true))
2723  {
2724  set_canonical_die_offset(canonical_dies,
2725  die_offset,
2726  cur_die_offset);
2727  return true;
2728  }
2729  }
2730 
2731  return false;
2732  }
2733 
2734  /// Retrieve the canonical DIE of a given DIE.
2735  ///
2736  /// The canonical DIE is a DIE that is structurally equivalent to
2737  /// this one.
2738  ///
2739  /// Note that this function caches the canonical DIE that was
2740  /// computed. Subsequent invocations of this function on the same
2741  /// DIE return the same cached DIE.
2742  ///
2743  /// @param die the DIE to get a canonical type for.
2744  ///
2745  /// @param canonical_die the resulting canonical DIE.
2746  ///
2747  /// @param where the offset of the logical DIE we are supposed to be
2748  /// calling this function from. If set to zero this means this is
2749  /// to be ignored.
2750  ///
2751  /// @param die_as_type if true, consider DIE is a type.
2752  ///
2753  /// @return true if an *existing* canonical DIE was found.
2754  /// Otherwise, @p die is considered as being a canonical DIE for
2755  /// itself. @p canonical_die is thus set to the canonical die in
2756  /// either cases.
2757  bool
2758  get_or_compute_canonical_die(const Dwarf_Die* die,
2759  Dwarf_Die& canonical_die,
2760  size_t where,
2761  bool die_as_type) const
2762  {
2763  const die_source source = get_die_source(die);
2764 
2765  offset_offset_map_type &canonical_dies =
2766  die_as_type
2767  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2768  get_container(source)
2769  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2770  get_container(source);
2771 
2772  Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2773 
2774  if (Dwarf_Off canonical_die_offset =
2775  get_canonical_die_offset(canonical_dies,
2776  initial_die_offset))
2777  {
2778  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2779  return true;
2780  }
2781 
2782  if (!is_type_die_to_be_canonicalized(die))
2783  return false;
2784 
2785  // The map that associates the string representation of 'die'
2786  // with a vector of offsets of potentially equivalent DIEs.
2788  die_as_type
2789  ? (const_cast<reader*>(this)->
2790  type_die_repr_die_offsets_maps().get_container(*this, die))
2791  : (const_cast<reader*>(this)->
2792  decl_die_repr_die_offsets_maps().get_container(*this, die));
2793 
2794  // The variable repr is the the string representation of 'die'.
2795  //
2796  // Even if die_as_type is true -- which means that 'die' is said
2797  // to be considered as a type -- we always consider a
2798  // DW_TAG_subprogram DIE as a decl here, as far as its string
2799  // representation is concerned.
2800  interned_string name =
2801  (die_as_type)
2802  ? get_die_pretty_type_representation(die, where)
2803  : get_die_pretty_representation(die, where);
2804 
2805  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2806  if (i == map.end())
2807  {
2808  dwarf_offsets_type offsets;
2809  offsets.push_back(initial_die_offset);
2810  map[name] = offsets;
2811  get_die_from_offset(source, initial_die_offset, &canonical_die);
2812  set_canonical_die_offset(canonical_dies,
2813  initial_die_offset,
2814  initial_die_offset);
2815  return false;
2816  }
2817 
2818  // walk i->second without any iterator (using a while loop rather
2819  // than a for loop) because compare_dies might add new content to
2820  // the end of the i->second vector during the walking.
2821  dwarf_offsets_type::size_type n = 0, s = i->second.size();
2822  while (n < s)
2823  {
2824  Dwarf_Off die_offset = i->second[n];
2825  get_die_from_offset(source, die_offset, &canonical_die);
2826  // compare die and canonical_die.
2827  if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2828  die, &canonical_die,
2829  /*update_canonical_dies_on_the_fly=*/true))
2830  {
2831  set_canonical_die_offset(canonical_dies,
2832  initial_die_offset,
2833  die_offset);
2834  return true;
2835  }
2836  ++n;
2837  }
2838 
2839  // We didn't find a canonical DIE for 'die'. So let's consider
2840  // that it is its own canonical DIE.
2841  get_die_from_offset(source, initial_die_offset, &canonical_die);
2842  i->second.push_back(initial_die_offset);
2843  set_canonical_die_offset(canonical_dies,
2844  initial_die_offset,
2845  initial_die_offset);
2846 
2847  return false;
2848  }
2849 
2850  /// Get the source of the DIE.
2851  ///
2852  /// The function returns an enumerator value saying if the DIE comes
2853  /// from the .debug_info section of the primary debug info file, the
2854  /// .debug_info section of the alternate debug info file, or the
2855  /// .debug_types section.
2856  ///
2857  /// @param die the DIE to get the source of.
2858  ///
2859  /// @return the source of the DIE if it could be determined,
2860  /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2861  die_source
2862  get_die_source(const Dwarf_Die *die) const
2863  {
2864  die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2865  ABG_ASSERT(die);
2866  ABG_ASSERT(get_die_source(*die, source));
2867  return source;
2868  }
2869 
2870  /// Get the source of the DIE.
2871  ///
2872  /// The function returns an enumerator value saying if the DIE comes
2873  /// from the .debug_info section of the primary debug info file, the
2874  /// .debug_info section of the alternate debug info file, or the
2875  /// .debug_types section.
2876  ///
2877  /// @param die the DIE to get the source of.
2878  ///
2879  /// @param source out parameter. The function sets this parameter
2880  /// to the source of the DIE @p iff it returns true.
2881  ///
2882  /// @return true iff the source of the DIE could be determined and
2883  /// returned.
2884  bool
2885  get_die_source(const Dwarf_Die &die, die_source &source) const
2886  {
2887  Dwarf_Die cu_die;
2888  Dwarf_Die cu_kind;
2889  uint8_t address_size = 0, offset_size = 0;
2890  if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2891  &cu_die, &address_size,
2892  &offset_size))
2893  return false;
2894 
2895  Dwarf_Half version = 0;
2896  Dwarf_Off abbrev_offset = 0;
2897  uint64_t type_signature = 0;
2898  Dwarf_Off type_offset = 0;
2899  if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2900  &version, &abbrev_offset,
2901  &address_size, &offset_size,
2902  &type_signature, &type_offset))
2903  return false;
2904 
2905  int tag = dwarf_tag(&cu_kind);
2906 
2907  if (tag == DW_TAG_compile_unit
2908  || tag == DW_TAG_partial_unit)
2909  {
2910  const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2911  if (dwarf_debug_info() == die_dwarf)
2912  source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2913  else if (alternate_dwarf_debug_info() == die_dwarf)
2914  source = ALT_DEBUG_INFO_DIE_SOURCE;
2915  else
2917  }
2918  else if (tag == DW_TAG_type_unit)
2919  source = TYPE_UNIT_DIE_SOURCE;
2920  else
2921  return false;
2922 
2923  return true;
2924  }
2925 
2926  /// Getter for the DIE designated by an offset.
2927  ///
2928  /// @param source the source of the DIE to get.
2929  ///
2930  /// @param offset the offset of the DIE to get.
2931  ///
2932  /// @param die the resulting DIE. The pointer has to point to an
2933  /// allocated memory region.
2934  void
2935  get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
2936  {
2937  if (source == TYPE_UNIT_DIE_SOURCE)
2938  ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2939  offset, die));
2940  else
2941  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2942  offset, die));
2943  }
2944 
2945 public:
2946 
2947  /// Add an entry to the relevant die->decl map.
2948  ///
2949  /// @param die the DIE to add the the map.
2950  ///
2951  /// @param decl the decl to consider.
2952  ///
2953  /// @param where_offset where in the DIE stream we logically are.
2954  ///
2955  /// @param do_associate_by_repr if true then this function
2956  /// associates the representation string of @p die with the
2957  /// declaration @p decl, in a corpus-wide manner. That is, in the
2958  /// entire current corpus, there is going to be just one declaration
2959  /// associated with a DIE of the string representation of @p die.
2960  ///
2961  /// @param do_associate_by_repr_per_tu if true, then this function
2962  /// associates the representation string of @p die with the
2963  /// declaration @p decl in a translation unit wide manner. That is,
2964  /// in the entire current translation unit, there is going to be
2965  /// just one declaration associated with a DIE of the string
2966  /// representation of @p die.
2967  void
2968  associate_die_to_decl(Dwarf_Die* die,
2969  decl_base_sptr decl,
2970  size_t where_offset,
2971  bool do_associate_by_repr = false)
2972  {
2973  const die_source source = get_die_source(die);
2974 
2976  decl_die_artefact_maps().get_container(source);
2977 
2978  size_t die_offset;
2979  if (do_associate_by_repr)
2980  {
2981  Dwarf_Die equiv_die;
2982  if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
2983  /*die_as_type=*/false))
2984  return;
2985  die_offset = dwarf_dieoffset(&equiv_die);
2986  }
2987  else
2988  die_offset = dwarf_dieoffset(die);
2989 
2990  m[die_offset] = decl;
2991  }
2992 
2993  /// Lookup the decl for a given DIE.
2994  ///
2995  /// The returned decl is either the decl of the DIE that as the
2996  /// exact offset @p die_offset
2997  /// die_offset, or
2998  /// give
2999  ///
3000  /// @param die_offset the offset of the DIE to consider.
3001  ///
3002  /// @param source where the DIE represented by @p die_offset comes
3003  /// from.
3004  ///
3005  /// Note that "alternate debug info sections" is a GNU extension as
3006  /// of DWARF4 and is described at
3007  /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3008  ///
3009  /// @return the resulting decl, or null if no decl is associated to
3010  /// the DIE represented by @p die_offset.
3011  decl_base_sptr
3012  lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3013  {
3014  decl_base_sptr result =
3015  is_decl(lookup_artifact_from_die_offset(die_offset, source,
3016  /*die_as_type=*/false));
3017 
3018  return result;
3019  }
3020 
3021  /// Get the qualified name of a given DIE.
3022  ///
3023  /// If the name of the DIE was already computed before just return
3024  /// that name from a cache. Otherwise, build the name, cache it and
3025  /// return it.
3026  ///
3027  /// @param die the DIE to consider.
3028  ///
3029  /// @param where_offset where in the DIE stream we logically are.
3030  ///
3031  /// @return the interned string representing the qualified name of
3032  /// @p die.
3033  interned_string
3034  get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3035  {
3036  ABG_ASSERT(die);
3037  die_istring_map_type& map =
3038  die_qualified_name_maps_.get_container(*this, die);
3039 
3040  size_t die_offset = dwarf_dieoffset(die);
3041  die_istring_map_type::const_iterator i = map.find(die_offset);
3042 
3043  if (i == map.end())
3044  {
3045  reader& rdr = *const_cast<reader*>(this);
3046  string qualified_name = die_qualified_name(rdr, die, where_offset);
3047  interned_string istr = env().intern(qualified_name);
3048  map[die_offset] = istr;
3049  return istr;
3050  }
3051 
3052  return i->second;
3053  }
3054 
3055  /// Get the qualified name of a given DIE.
3056  ///
3057  /// If the name of the DIE was already computed before just return
3058  /// that name from a cache. Otherwise, build the name, cache it and
3059  /// return it.
3060  ///
3061  /// @param die the DIE to consider.
3062  ///
3063  /// @param where_offset where in the DIE stream we logically are.
3064  ///
3065  /// @return the interned string representing the qualified name of
3066  /// @p die.
3067  interned_string
3068  get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3069  {
3070  return const_cast<reader*>(this)->
3071  get_die_qualified_name(die, where_offset);
3072  }
3073 
3074  /// Get the qualified name of a given DIE which is considered to be
3075  /// the DIE for a type.
3076  ///
3077  /// For instance, for a DW_TAG_subprogram DIE, this function
3078  /// computes the name of the function *type* that corresponds to the
3079  /// function.
3080  ///
3081  /// If the name of the DIE was already computed before just return
3082  /// that name from a cache. Otherwise, build the name, cache it and
3083  /// return it.
3084  ///
3085  /// @param die the DIE to consider.
3086  ///
3087  /// @param where_offset where in the DIE stream we logically are.
3088  ///
3089  /// @return the interned string representing the qualified name of
3090  /// @p die.
3091  interned_string
3092  get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3093  {
3094  ABG_ASSERT(die);
3095 
3096  // The name of the translation unit die is "".
3097  if (die == cur_tu_die())
3098  return env().intern("");
3099 
3100  die_istring_map_type& map =
3101  die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3102  die);
3103 
3104  size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3105  die_istring_map_type::const_iterator i =
3106  map.find(die_offset);
3107 
3108  if (i == map.end())
3109  {
3110  reader& rdr = *const_cast<reader*>(this);
3111  string qualified_name;
3112  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3113  if ((tag == DW_TAG_structure_type
3114  || tag == DW_TAG_class_type
3115  || tag == DW_TAG_union_type)
3116  && die_is_anonymous(die))
3117  {
3118  location l = die_location(*this, die);
3119  qualified_name = l ? l.expand() : "noloc";
3120  qualified_name = "unnamed-at-" + qualified_name;
3121  }
3122  else
3123  qualified_name =
3124  die_qualified_type_name(rdr, die, where_offset);
3125 
3126  interned_string istr = env().intern(qualified_name);
3127  map[die_offset] = istr;
3128  return istr;
3129  }
3130 
3131  return i->second;
3132  }
3133 
3134  /// Get the pretty representation of a DIE that represents a type.
3135  ///
3136  /// For instance, for the DW_TAG_subprogram, this function computes
3137  /// the pretty representation of the type of the function, not the
3138  /// pretty representation of the function declaration.
3139  ///
3140  /// Once the pretty representation is computed, it's stored in a
3141  /// cache. Subsequent invocations of this function on the same DIE
3142  /// will yield the cached name.
3143  ///
3144  /// @param die the DIE to consider.
3145  ///
3146  /// @param where_offset where in the DIE stream we logically are.
3147  ///
3148  /// @return the interned_string that represents the pretty
3149  /// representation.
3150  interned_string
3151  get_die_pretty_type_representation(const Dwarf_Die *die,
3152  size_t where_offset) const
3153  {
3154  ABG_ASSERT(die);
3155  die_istring_map_type& map =
3156  die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3157  die);
3158 
3159  size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3160  die_istring_map_type::const_iterator i = map.find(die_offset);
3161 
3162  if (i == map.end())
3163  {
3164  reader& rdr = *const_cast<reader*>(this);
3165  string pretty_representation =
3166  die_pretty_print_type(rdr, die, where_offset);
3167  interned_string istr = env().intern(pretty_representation);
3168  map[die_offset] = istr;
3169  return istr;
3170  }
3171 
3172  return i->second;
3173  }
3174 
3175  /// Get the pretty representation of a DIE.
3176  ///
3177  /// Once the pretty representation is computed, it's stored in a
3178  /// cache. Subsequent invocations of this function on the same DIE
3179  /// will yield the cached name.
3180  ///
3181  /// @param die the DIE to consider.
3182  ///
3183  /// @param where_offset where in the DIE stream we logically are.
3184  ///
3185  /// @return the interned_string that represents the pretty
3186  /// representation.
3187  interned_string
3188  get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3189  {
3190  ABG_ASSERT(die);
3191 
3192  die_istring_map_type& map =
3193  die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3194  die);
3195 
3196  size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3197  die_istring_map_type::const_iterator i = map.find(die_offset);
3198 
3199  if (i == map.end())
3200  {
3201  reader& rdr = *const_cast<reader*>(this);
3202  string pretty_representation =
3203  die_pretty_print(rdr, die, where_offset);
3204  interned_string istr = env().intern(pretty_representation);
3205  map[die_offset] = istr;
3206  return istr;
3207  }
3208 
3209  return i->second;
3210  }
3211 
3212  /// Lookup the artifact that was built to represent a type that has
3213  /// the same pretty representation as the type denoted by a given
3214  /// DIE.
3215  ///
3216  /// Note that the DIE must have previously been associated with the
3217  /// artifact using the functions associate_die_to_decl or
3218  /// associate_die_to_type.
3219  ///
3220  /// Also, note that the scope of the lookup is the current ABI
3221  /// corpus.
3222  ///
3223  /// @param die the DIE to consider.
3224  ///
3225  /// @param where_offset where in the DIE stream we logically are.
3226  ///
3227  /// @return the type artifact found.
3229  lookup_type_artifact_from_die(Dwarf_Die *die) const
3230  {
3231  type_or_decl_base_sptr artifact =
3232  lookup_artifact_from_die(die, /*type_as_die=*/true);
3233  if (function_decl_sptr fn = is_function_decl(artifact))
3234  return fn->get_type();
3235  return artifact;
3236  }
3237 
3238  /// Lookup the artifact that was built to represent a type or a
3239  /// declaration that has the same pretty representation as the type
3240  /// denoted by a given DIE.
3241  ///
3242  /// Note that the DIE must have previously been associated with the
3243  /// artifact using the functions associate_die_to_decl or
3244  /// associate_die_to_type.
3245  ///
3246  /// Also, note that the scope of the lookup is the current ABI
3247  /// corpus.
3248  ///
3249  /// @param die the DIE to consider.
3250  ///
3251  /// @param where_offset where in the DIE stream we logically are.
3252  ///
3253  /// @param die_as_type if true, it means the DIE is to be considered
3254  /// as a type.
3255  ///
3256  /// @return the artifact found.
3258  lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3259  {
3260  Dwarf_Die equiv_die;
3261  if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3262  return type_or_decl_base_sptr();
3263 
3264  const die_artefact_map_type& m =
3265  die_as_type
3266  ? type_die_artefact_maps().get_container(*this, &equiv_die)
3267  : decl_die_artefact_maps().get_container(*this, &equiv_die);
3268 
3269  size_t die_offset = dwarf_dieoffset(&equiv_die);
3270  die_artefact_map_type::const_iterator i = m.find(die_offset);
3271 
3272  if (i == m.end())
3273  return type_or_decl_base_sptr();
3274  return i->second;
3275  }
3276 
3277  /// Lookup the artifact that was built to represent a type or a
3278  /// declaration that has the same pretty representation as the type
3279  /// denoted by the offset of a given DIE.
3280  ///
3281  /// Note that the DIE must have previously been associated with the
3282  /// artifact using either associate_die_to_decl or
3283  /// associate_die_to_type.
3284  ///
3285  /// Also, note that the scope of the lookup is the current ABI
3286  /// corpus.
3287  ///
3288  /// @param die the DIE to consider.
3289  ///
3290  /// @param where_offset where in the DIE stream we logically are.
3291  ///
3292  /// @param die_as_type if true, it means the DIE is to be considered
3293  /// as a type.
3294  ///
3295  /// @return the artifact found.
3297  lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3298  die_source source,
3299  bool die_as_type = false) const
3300  {
3301  const die_artefact_map_type& m =
3302  die_as_type
3303  ? type_die_artefact_maps().get_container(source)
3304  : decl_die_artefact_maps().get_container(source);
3305 
3306  die_artefact_map_type::const_iterator i = m.find(die_offset);
3307  if (i == m.end())
3308  return type_or_decl_base_sptr();
3309  return i->second;
3310  }
3311 
3312  /// Get the language used to generate a given DIE.
3313  ///
3314  /// @param die the DIE to consider.
3315  ///
3316  /// @param lang the resulting language.
3317  ///
3318  /// @return true iff the language of the DIE was found.
3319  bool
3320  get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3321  {
3322  Dwarf_Die cu_die;
3323  ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3324 
3325  uint64_t l = 0;
3326  if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3327  return false;
3328 
3329  lang = dwarf_language_to_tu_language(l);
3330  return true;
3331  }
3332 
3333  /// Test if a given DIE originates from a program written in the C
3334  /// language.
3335  ///
3336  /// @param die the DIE to consider.
3337  ///
3338  /// @return true iff @p die originates from a program in the C
3339  /// language.
3340  bool
3341  die_is_in_c(const Dwarf_Die *die) const
3342  {
3343  translation_unit::language l = translation_unit::LANG_UNKNOWN;
3344  if (!get_die_language(die, l))
3345  return false;
3346  return is_c_language(l);
3347  }
3348 
3349  /// Test if a given DIE originates from a program written in the C++
3350  /// language.
3351  ///
3352  /// @param die the DIE to consider.
3353  ///
3354  /// @return true iff @p die originates from a program in the C++
3355  /// language.
3356  bool
3357  die_is_in_cplus_plus(const Dwarf_Die *die) const
3358  {
3359  translation_unit::language l = translation_unit::LANG_UNKNOWN;
3360  if (!get_die_language(die, l))
3361  return false;
3362  return is_cplus_plus_language(l);
3363  }
3364 
3365  /// Test if a given DIE originates from a program written either in
3366  /// C or C++.
3367  ///
3368  /// @param die the DIE to consider.
3369  ///
3370  /// @return true iff @p die originates from a program written either in
3371  /// C or C++.
3372  bool
3373  die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3374  {
3375  translation_unit::language l = translation_unit::LANG_UNKNOWN;
3376  if (!get_die_language(die, l))
3377  return false;
3378  return (is_cplus_plus_language(l) || is_c_language(l));
3379  }
3380 
3381  /// Check if we can assume the One Definition Rule[1] to be relevant
3382  /// for the current translation unit.
3383  ///
3384  /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3385  ///
3386  /// At the moment this returns true if the current translation unit
3387  /// is in C++ language. In that case, it's relevant to assume that
3388  /// we use optimizations based on the ODR.
3389  bool
3390  odr_is_relevant() const
3391  {return odr_is_relevant(cur_transl_unit()->get_language());}
3392 
3393  /// Check if we can assume the One Definition Rule[1] to be relevant
3394  /// for a given language.
3395  ///
3396  /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3397  ///
3398  /// At the moment this returns true if the language considered
3399  /// is C++, Java or Ada.
3400  bool
3402  {
3403  return (is_cplus_plus_language(l)
3404  || is_java_language(l)
3405  || is_ada_language(l));
3406  }
3407 
3408  /// Check if we can assume the One Definition Rule to be relevant
3409  /// for a given DIE.
3410  ///
3411  /// @param die the DIE to consider.
3412  ///
3413  /// @return true if the ODR is relevant for @p die.
3414  bool
3415  odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3416  {
3417  Dwarf_Die die;
3418  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3419  die_offset, &die));
3420  return odr_is_relevant(&die);
3421  }
3422 
3423  /// Check if we can assume the One Definition Rule to be relevant
3424  /// for a given DIE.
3425  ///
3426  /// @param die the DIE to consider.
3427  ///
3428  /// @return true if the ODR is relevant for @p die.
3429  bool
3430  odr_is_relevant(const Dwarf_Die *die) const
3431  {
3433  if (!get_die_language(die, lang))
3434  return odr_is_relevant();
3435 
3436  return odr_is_relevant(lang);
3437  }
3438 
3439  /// Getter for the maps set that associates a decl DIE offset to an
3440  /// artifact.
3441  ///
3442  /// @return the maps set that associates a decl DIE offset to an
3443  /// artifact.
3444  die_source_dependant_container_set<die_artefact_map_type>&
3445  decl_die_artefact_maps()
3446  {return decl_die_artefact_maps_;}
3447 
3448  /// Getter for the maps set that associates a decl DIE offset to an
3449  /// artifact.
3450  ///
3451  /// @return the maps set that associates a decl DIE offset to an
3452  /// artifact.
3453  const die_source_dependant_container_set<die_artefact_map_type>&
3454  decl_die_artefact_maps() const
3455  {return decl_die_artefact_maps_;}
3456 
3457  /// Getter for the maps set that associates a type DIE offset to an
3458  /// artifact.
3459  ///
3460  /// @return the maps set that associates a type DIE offset to an
3461  /// artifact.
3462  die_source_dependant_container_set<die_artefact_map_type>&
3463  type_die_artefact_maps()
3464  {return type_die_artefact_maps_;}
3465 
3466  /// Getter for the maps set that associates a type DIE offset to an
3467  /// artifact.
3468  ///
3469  /// @return the maps set that associates a type DIE offset to an
3470  /// artifact.
3471  const die_source_dependant_container_set<die_artefact_map_type>&
3472  type_die_artefact_maps() const
3473  {return type_die_artefact_maps_;}
3474 
3475  /// Getter of the maps that associates function type representations
3476  /// to function types, inside a translation unit.
3477  ///
3478  /// @return the maps that associates function type representations
3479  /// to function types, inside a translation unit.
3481  per_tu_repr_to_fn_type_maps()
3482  {return per_tu_repr_to_fn_type_maps_;}
3483 
3484  /// Getter of the maps that associates function type representations
3485  /// to function types, inside a translation unit.
3486  ///
3487  /// @return the maps that associates function type representations
3488  /// to function types, inside a translation unit.
3490  per_tu_repr_to_fn_type_maps() const
3491  {return per_tu_repr_to_fn_type_maps_;}
3492 
3493  /// Associate the representation of a function type DIE to a given
3494  /// function type, inside the current translation unit.
3495  ///
3496  /// @param die the DIE to associate to the function type, using its
3497  /// representation.
3498  ///
3499  /// @param fn_type the function type to associate to @p die.
3500  void
3501  associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3502  const function_type_sptr &fn_type)
3503  {
3504  if (!die_is_function_type(die))
3505  return;
3506 
3507  interned_string repr =
3508  get_die_pretty_type_representation(die, /*where=*/0);
3509  ABG_ASSERT(!repr.empty());
3510 
3511  per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3512  }
3513 
3514  /// Lookup the function type associated to a given function type
3515  /// DIE, in the current translation unit.
3516  ///
3517  /// @param die the DIE of function type to consider.
3518  ///
3519  /// @return the @ref function_type_sptr associated to @p die, or nil
3520  /// of no function_type is associated to @p die.
3522  lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3523  {
3524  if (!die_is_function_type(die))
3525  return function_type_sptr();
3526 
3527  interned_string repr = die_name(die).empty() ?
3528  get_die_pretty_type_representation(die, /*where=*/0)
3529  : get_die_pretty_representation(die, /*where=*/0);
3530  ABG_ASSERT(!repr.empty());
3531 
3532  istring_fn_type_map_type::const_iterator i =
3533  per_tu_repr_to_fn_type_maps().find(repr);
3534 
3535  if (i == per_tu_repr_to_fn_type_maps().end())
3536  return function_type_sptr();
3537 
3538  return i->second;
3539  }
3540 
3541  /// Set the canonical DIE offset of a given DIE.
3542  ///
3543  /// @param canonical_dies the vector that holds canonical DIEs.
3544  ///
3545  /// @param die_offset the offset of the DIE to set the canonical DIE
3546  /// for.
3547  ///
3548  /// @param canonical_die_offset the canonical DIE offset to
3549  /// associate to @p die_offset.
3550  void
3551  set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3552  Dwarf_Off die_offset,
3553  Dwarf_Off canonical_die_offset) const
3554  {
3555  canonical_dies[die_offset] = canonical_die_offset;}
3556 
3557  /// Set the canonical DIE offset of a given DIE.
3558  ///
3559  ///
3560  /// @param die_offset the offset of the DIE to set the canonical DIE
3561  /// for.
3562  ///
3563  /// @param source the source of the DIE denoted by @p die_offset.
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_offset has to
3569  /// be considered as a type.
3570  void
3571  set_canonical_die_offset(Dwarf_Off die_offset,
3572  die_source source,
3573  Dwarf_Off canonical_die_offset,
3574  bool die_as_type) const
3575  {
3576  offset_offset_map_type &canonical_dies =
3577  die_as_type
3578  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3579  get_container(source)
3580  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3581  get_container(source);
3582 
3583  set_canonical_die_offset(canonical_dies,
3584  die_offset,
3585  canonical_die_offset);
3586  }
3587 
3588  /// Set the canonical DIE offset of a given DIE.
3589  ///
3590  ///
3591  /// @param die the DIE to set the canonical DIE for.
3592  ///
3593  /// @param canonical_die_offset the canonical DIE offset to
3594  /// associate to @p die_offset.
3595  ///
3596  /// @param die_as_type if true, it means that @p die has to be
3597  /// considered as a type.
3598  void
3599  set_canonical_die_offset(const Dwarf_Die *die,
3600  Dwarf_Off canonical_die_offset,
3601  bool die_as_type) const
3602  {
3603  const die_source source = get_die_source(die);
3604 
3605  Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3606 
3607  set_canonical_die_offset(die_offset, source,
3608  canonical_die_offset,
3609  die_as_type);
3610  }
3611 
3612  /// Get the canonical DIE offset of a given DIE.
3613  ///
3614  /// @param canonical_dies the vector that contains canonical DIES.
3615  ///
3616  /// @param die_offset the offset of the DIE to consider.
3617  ///
3618  /// @return the canonical of the DIE denoted by @p die_offset, or
3619  /// zero if no canonical DIE was found.
3620  Dwarf_Off
3621  get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3622  Dwarf_Off die_offset) const
3623  {
3624  offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3625  if (it == canonical_dies.end())
3626  return 0;
3627  return it->second;
3628  }
3629 
3630  /// Get the canonical DIE offset of a given DIE.
3631  ///
3632  /// @param die_offset the offset of the DIE to consider.
3633  ///
3634  /// @param source the source of the DIE denoted by @p die_offset.
3635  ///
3636  /// @param die_as_type if true, it means that @p is to be considered
3637  /// as a type DIE.
3638  ///
3639  /// @return the canonical of the DIE denoted by @p die_offset, or
3640  /// zero if no canonical DIE was found.
3641  Dwarf_Off
3642  get_canonical_die_offset(Dwarf_Off die_offset,
3643  die_source source,
3644  bool die_as_type) const
3645  {
3646  offset_offset_map_type &canonical_dies =
3647  die_as_type
3648  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3649  get_container(source)
3650  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3651  get_container(source);
3652 
3653  return get_canonical_die_offset(canonical_dies, die_offset);
3654  }
3655 
3656  /// Erase the canonical type of a given DIE.
3657  ///
3658  /// @param die_offset the offset of the DIE to consider.
3659  ///
3660  /// @param source the source of the canonical type.
3661  ///
3662  /// @param die_as_type if true, it means that @p is to be considered
3663  /// as a type DIE.
3664  ///
3665  /// @return the canonical of the DIE denoted by @p die_offset, or
3666  /// zero if no canonical DIE was found and erased..
3667  bool
3668  erase_canonical_die_offset(Dwarf_Off die_offset,
3669  die_source source,
3670  bool die_as_type) const
3671  {
3672  offset_offset_map_type &canonical_dies =
3673  die_as_type
3674  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3675  get_container(source)
3676  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3677  get_container(source);
3678 
3679  return canonical_dies.erase(die_offset);
3680  }
3681 
3682 
3683  /// Associate a DIE (representing a type) to the type that it
3684  /// represents.
3685  ///
3686  /// @param die the DIE to consider.
3687  ///
3688  /// @param type the type to associate the DIE to.
3689  ///
3690  /// @param where_offset where in the DIE stream we logically are.
3691  void
3692  associate_die_to_type(const Dwarf_Die *die,
3693  type_base_sptr type,
3694  size_t where)
3695  {
3696  if (!type)
3697  return;
3698 
3699  Dwarf_Die equiv_die;
3700  if (!get_or_compute_canonical_die(die, equiv_die, where,
3701  /*die_as_type=*/true))
3702  return;
3703 
3705  type_die_artefact_maps().get_container(*this, &equiv_die);
3706 
3707  size_t die_offset = dwarf_dieoffset(&equiv_die);
3708  m[die_offset] = type;
3709  }
3710 
3711  /// Lookup the type associated to a given DIE.
3712  ///
3713  /// Note that the DIE must have been associated to type by a
3714  /// previous invocation of the function
3715  /// reader::associate_die_to_type().
3716  ///
3717  /// @param die the DIE to consider.
3718  ///
3719  /// @return the type associated to the DIE or NULL if no type is
3720  /// associated to the DIE.
3721  type_base_sptr
3722  lookup_type_from_die(const Dwarf_Die* die) const
3723  {
3724  type_or_decl_base_sptr artifact =
3725  lookup_artifact_from_die(die, /*die_as_type=*/true);
3726  if (function_decl_sptr fn = is_function_decl(artifact))
3727  return fn->get_type();
3728  return is_type(artifact);
3729  }
3730 
3731  /// Lookup the type associated to a DIE at a given offset, from a
3732  /// given source.
3733  ///
3734  /// Note that the DIE must have been associated to type by a
3735  /// previous invocation of the function
3736  /// reader::associate_die_to_type().
3737  ///
3738  /// @param die_offset the offset of the DIE to consider.
3739  ///
3740  /// @param source the source of the DIE to consider.
3741  ///
3742  /// @return the type associated to the DIE or NULL if no type is
3743  /// associated to the DIE.
3744  type_base_sptr
3745  lookup_type_from_die_offset(size_t die_offset, die_source source) const
3746  {
3747  type_base_sptr result;
3748  const die_artefact_map_type& m =
3749  type_die_artefact_maps().get_container(source);
3750  die_artefact_map_type::const_iterator i = m.find(die_offset);
3751  if (i != m.end())
3752  {
3753  if (function_decl_sptr fn = is_function_decl(i->second))
3754  return fn->get_type();
3755  result = is_type(i->second);
3756  }
3757 
3758  if (!result)
3759  {
3760  // Maybe we are looking for a class type being constructed?
3761  const die_class_or_union_map_type& m = die_wip_classes_map(source);
3762  die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3763 
3764  if (i != m.end())
3765  result = i->second;
3766  }
3767 
3768  if (!result)
3769  {
3770  // Maybe we are looking for a function type being constructed?
3771  const die_function_type_map_type& m =
3772  die_wip_function_types_map(source);
3773  die_function_type_map_type::const_iterator i = m.find(die_offset);
3774 
3775  if (i != m.end())
3776  result = i->second;
3777  }
3778 
3779  return result;
3780  }
3781 
3782  /// Getter of a map that associates a die that represents a
3783  /// class/struct with the declaration of the class, while the class
3784  /// is being constructed.
3785  ///
3786  /// @param source where the DIE is from.
3787  ///
3788  /// @return the map that associates a DIE to the class that is being
3789  /// built.
3791  die_wip_classes_map(die_source source) const
3792  {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3793 
3794  /// Getter of a map that associates a die that represents a
3795  /// class/struct with the declaration of the class, while the class
3796  /// is being constructed.
3797  ///
3798  /// @param source where the DIE comes from.
3799  ///
3800  /// @return the map that associates a DIE to the class that is being
3801  /// built.
3803  die_wip_classes_map(die_source source)
3804  {
3805  switch (source)
3806  {
3807  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3808  break;
3809  case ALT_DEBUG_INFO_DIE_SOURCE:
3810  return alternate_die_wip_classes_map_;
3811  case TYPE_UNIT_DIE_SOURCE:
3812  return type_unit_die_wip_classes_map_;
3813  case NO_DEBUG_INFO_DIE_SOURCE:
3814  case NUMBER_OF_DIE_SOURCES:
3816  }
3817  return die_wip_classes_map_;
3818  }
3819 
3820  /// Getter for a map that associates a die (that represents a
3821  /// function type) whith a function type, while the function type is
3822  /// being constructed (WIP == work in progress).
3823  ///
3824  /// @param source where the DIE comes from.n
3825  ///
3826  /// @return the map of wip function types.
3828  die_wip_function_types_map(die_source source) const
3829  {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3830 
3831  /// Getter for a map that associates a die (that represents a
3832  /// function type) whith a function type, while the function type is
3833  /// being constructed (WIP == work in progress).
3834  ///
3835  /// @param source where DIEs of the map come from.
3836  ///
3837  /// @return the map of wip function types.
3839  die_wip_function_types_map(die_source source)
3840  {
3841  switch (source)
3842  {
3843  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3844  break;
3845  case ALT_DEBUG_INFO_DIE_SOURCE:
3846  return alternate_die_wip_function_types_map_;
3847  case TYPE_UNIT_DIE_SOURCE:
3848  return type_unit_die_wip_function_types_map_;
3849  case NO_DEBUG_INFO_DIE_SOURCE:
3850  case NUMBER_OF_DIE_SOURCES:
3852  }
3853  return die_wip_function_types_map_;
3854  }
3855 
3856  /// Getter for a map that associates a die with a function decl
3857  /// which has a linkage name but no elf symbol yet.
3858  ///
3859  /// This is to fixup function decls with linkage names, but with no
3860  /// link to their underlying elf symbol. There are some DIEs like
3861  /// that in DWARF sometimes, especially when the compiler optimizes
3862  /// stuff aggressively.
3864  die_function_decl_with_no_symbol_map()
3865  {return die_function_with_no_symbol_map_;}
3866 
3867  /// Return true iff a given offset is for the DIE of a class that is
3868  /// being built, but that is not fully built yet. WIP == "work in
3869  /// progress".
3870  ///
3871  /// @param offset the DIE offset to consider.
3872  ///
3873  /// @param source where the DIE of the map come from.
3874  ///
3875  /// @return true iff @p offset is the offset of the DIE of a class
3876  /// that is being currently built.
3877  bool
3878  is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3879  {
3880  die_class_or_union_map_type::const_iterator i =
3881  die_wip_classes_map(source).find(offset);
3882  return (i != die_wip_classes_map(source).end());
3883  }
3884 
3885  /// Return true iff a given offset is for the DIE of a function type
3886  /// that is being built at the moment, but is not fully built yet.
3887  /// WIP == work in progress.
3888  ///
3889  /// @param offset DIE offset to consider.
3890  ///
3891  /// @param source where the DIE comes from.
3892  ///
3893  /// @return true iff @p offset is the offset of the DIE of a
3894  /// function type that is being currently built.
3895  bool
3896  is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3897  {
3898  die_function_type_map_type::const_iterator i =
3899  die_wip_function_types_map(source).find(offset);
3900  return (i != die_wip_function_types_map(source).end());
3901  }
3902 
3903  /// Sometimes, a data member die can erroneously have an empty name as
3904  /// a result of a bug of the DWARF emitter.
3905  ///
3906  /// This is what happens in
3907  /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3908  ///
3909  /// In that case, this function constructs an artificial name for that
3910  /// data member. The pattern of the name is as follows:
3911  ///
3912  /// "unnamed-@-<location>".
3913  ///
3914  ///location is either the value of the data member location of the
3915  ///data member if it has one or concatenation of its source location
3916  ///if it has none. If no location can be calculated then the function
3917  ///returns the empty string.
3918  string
3919  build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3920  {
3921  string result;
3922  // Let's make sure we are looking at a data member with an empty
3923  // name ...
3924  if (!die
3925  || dwarf_tag(die) != DW_TAG_member
3926  || !die_name(die).empty())
3927  return result;
3928 
3929  // ... and yet, it's not an anonymous data member (aka unnamed
3930  // field) as described in
3931  // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3932  if (die_is_anonymous_data_member(die))
3933  return result;
3934 
3935  // If we come this far, it means we are looking at a buggy data
3936  // member with no name. Let's build a name for it so that it can be
3937  // addressed.
3938  int64_t offset_in_bits = 0;
3939  bool has_offset = die_member_offset(*this, die, offset_in_bits);
3940  location loc;
3941  if (!has_offset)
3942  {
3943  loc = die_location(*this, die);
3944  if (!loc)
3945  return result;
3946  }
3947 
3948  std::ostringstream o;
3949  o << "unnamed-dm-@-";
3950  if (has_offset)
3951  o << "offset-" << offset_in_bits << "bits";
3952  else
3953  o << "loc-" << loc.expand();
3954 
3955  return o.str();
3956  }
3957 
3958  /// Getter for the map of declaration-only classes that are to be
3959  /// resolved to their definition classes by the end of the corpus
3960  /// loading.
3961  ///
3962  /// @return a map of string -> vector of classes where the key is
3963  /// the fully qualified name of the class and the value is the
3964  /// vector of declaration-only class.
3966  declaration_only_classes() const
3967  {return decl_only_classes_map_;}
3968 
3969  /// Getter for the map of declaration-only classes that are to be
3970  /// resolved to their definition classes by the end of the corpus
3971  /// loading.
3972  ///
3973  /// @return a map of string -> vector of classes where the key is
3974  /// the fully qualified name of the class and the value is the
3975  /// vector of declaration-only class.
3977  declaration_only_classes()
3978  {return decl_only_classes_map_;}
3979 
3980  /// If a given class is a declaration-only class then stash it on
3981  /// the side so that at the end of the corpus reading we can resolve
3982  /// it to its definition.
3983  ///
3984  /// @param klass the class to consider.
3985  void
3986  maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
3987  {
3988  if (cou->get_is_declaration_only()
3989  && cou->get_definition_of_declaration() == 0)
3990  {
3991  string qn = cou->get_qualified_name();
3992  string_classes_or_unions_map::iterator record =
3993  declaration_only_classes().find(qn);
3994  if (record == declaration_only_classes().end())
3995  declaration_only_classes()[qn].push_back(cou);
3996  else
3997  record->second.push_back(cou);
3998  }
3999  }
4000 
4001  /// Test if a given declaration-only class has been scheduled for
4002  /// resolution to a defined class.
4003  ///
4004  /// @param klass the class to consider for the test.
4005  ///
4006  /// @return true iff @p klass is a declaration-only class and if
4007  /// it's been scheduled for resolution to a defined class.
4008  bool
4009  is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4010  {
4011  if (cou->get_is_declaration_only())
4012  return (declaration_only_classes().find(cou->get_qualified_name())
4013  != declaration_only_classes().end());
4014 
4015  return false;
4016  }
4017 
4018  /// Compare two ABI artifacts in a context which canonicalization
4019  /// has not be done yet.
4020  ///
4021  /// @param l the left-hand-side operand of the comparison
4022  ///
4023  /// @param r the right-hand-side operand of the comparison.
4024  ///
4025  /// @return true if @p l equals @p r.
4026  bool
4027  compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4028  const type_or_decl_base_sptr &r)
4029  {
4030  if (!l || !r)
4031  return !!l == !!r;
4032 
4033  const environment& e = l->get_environment();
4034  ABG_ASSERT(!e.canonicalization_is_done());
4035 
4036  e.priv_->allow_type_comparison_results_caching(true);
4037  bool s0 = e.decl_only_class_equals_definition();
4038  e.decl_only_class_equals_definition(true);
4039  bool equal = l == r;
4040  e.decl_only_class_equals_definition(s0);
4041  e.priv_->clear_type_comparison_results_cache();
4042  e.priv_->allow_type_comparison_results_caching(false);
4043  return equal;
4044  }
4045 
4046  /// Walk the declaration-only classes that have been found during
4047  /// the building of the corpus and resolve them to their definitions.
4048  void
4049  resolve_declaration_only_classes()
4050  {
4051  vector<string> resolved_classes;
4052 
4053  for (string_classes_or_unions_map::iterator i =
4054  declaration_only_classes().begin();
4055  i != declaration_only_classes().end();
4056  ++i)
4057  {
4058  bool to_resolve = false;
4059  for (classes_or_unions_type::iterator j = i->second.begin();
4060  j != i->second.end();
4061  ++j)
4062  if ((*j)->get_is_declaration_only()
4063  && ((*j)->get_definition_of_declaration() == 0))
4064  to_resolve = true;
4065 
4066  if (!to_resolve)
4067  {
4068  resolved_classes.push_back(i->first);
4069  continue;
4070  }
4071 
4072  // Now, for each decl-only class that have the current name
4073  // 'i->first', let's try to poke at the fully defined class
4074  // that is defined in the same translation unit as the
4075  // declaration.
4076  //
4077  // If we find one class (defined in the TU of the declaration)
4078  // that defines the declaration, then the declaration can be
4079  // resolved to that class.
4080  //
4081  // If no defining class is found in the TU of the declaration,
4082  // then there are possibly three cases to consider:
4083  //
4084  // 1/ There is exactly one class that defines the
4085  // declaration and that class is defined in another TU. In
4086  // this case, the declaration is resolved to that
4087  // definition.
4088  //
4089  // 2/ There are more than one class that define that
4090  // declaration and none of them is defined in the TU of the
4091  // declaration. If those classes are all different, then
4092  // the declaration is left unresolved.
4093  //
4094  // 3/ No class defines the declaration. In this case, the
4095  // declaration is left unresoved.
4096 
4097  // So get the classes that might define the current
4098  // declarations which name is i->first.
4099  const type_base_wptrs_type *classes =
4100  lookup_class_types(i->first, *corpus());
4101  if (!classes)
4102  classes = lookup_union_types(i->first, *corpus());
4103 
4104  if (!classes)
4105  continue;
4106 
4107  // This is a map that associates the translation unit path to
4108  // the class (that potentially defines the declarations that
4109  // we consider) that are defined in that translation unit. It
4110  // should stay ordered by using the TU path as key to ensure
4111  // stability of the order of classe definitions in ABIXML
4112  // output.
4113  map<string, class_or_union_sptr> per_tu_class_map;
4114  for (type_base_wptrs_type::const_iterator c = classes->begin();
4115  c != classes->end();
4116  ++c)
4117  {
4118  class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4119  ABG_ASSERT(klass);
4120 
4122  if (klass->get_is_declaration_only())
4123  continue;
4124 
4125  string tu_path = klass->get_translation_unit()->get_absolute_path();
4126  if (tu_path.empty())
4127  continue;
4128 
4129  // Build a map that associates the translation unit path
4130  // to the class (that potentially defines the declarations
4131  // that we consider) that are defined in that translation unit.
4132  per_tu_class_map[tu_path] = klass;
4133  }
4134 
4135  if (!per_tu_class_map.empty())
4136  {
4137  // Walk the declarations to resolve and resolve them
4138  // either to the definitions that are in the same TU as
4139  // the declaration, or to the definition found elsewhere,
4140  // if there is only one such definition.
4141  for (classes_or_unions_type::iterator j = i->second.begin();
4142  j != i->second.end();
4143  ++j)
4144  {
4145  if ((*j)->get_is_declaration_only()
4146  && ((*j)->get_definition_of_declaration() == 0))
4147  {
4148  string tu_path =
4149  (*j)->get_translation_unit()->get_absolute_path();
4150  map<string, class_or_union_sptr>::const_iterator e =
4151  per_tu_class_map.find(tu_path);
4152  if (e != per_tu_class_map.end())
4153  (*j)->set_definition_of_declaration(e->second);
4154  else if (per_tu_class_map.size() == 1)
4155  (*j)->set_definition_of_declaration
4156  (per_tu_class_map.begin()->second);
4157  else
4158  {
4159  // We are in case where there are more than
4160  // one definition for the declaration. Let's
4161  // see if they are all equal. If they are,
4162  // then the declaration resolves to the
4163  // definition. Otherwise, we are in the case
4164  // 3/ described above.
4165  map<string,
4166  class_or_union_sptr>::const_iterator it;
4167  class_or_union_sptr first_class =
4168  per_tu_class_map.begin()->second;
4169  bool all_class_definitions_are_equal = true;
4170  for (it = per_tu_class_map.begin();
4171  it != per_tu_class_map.end();
4172  ++it)
4173  {
4174  if (it == per_tu_class_map.begin())
4175  continue;
4176  else
4177  {
4178  if (!compare_before_canonicalisation(it->second,
4179  first_class))
4180  {
4181  all_class_definitions_are_equal = false;
4182  break;
4183  }
4184  }
4185  }
4186  if (all_class_definitions_are_equal)
4187  (*j)->set_definition_of_declaration(first_class);
4188  }
4189  }
4190  }
4191  resolved_classes.push_back(i->first);
4192  }
4193  }
4194 
4195  size_t num_decl_only_classes = declaration_only_classes().size(),
4196  num_resolved = resolved_classes.size();
4197  if (show_stats())
4198  cerr << "resolved " << num_resolved
4199  << " class declarations out of "
4200  << num_decl_only_classes
4201  << "\n";
4202 
4203  for (vector<string>::const_iterator i = resolved_classes.begin();
4204  i != resolved_classes.end();
4205  ++i)
4206  declaration_only_classes().erase(*i);
4207 
4208  if (show_stats() && !declaration_only_classes().empty())
4209  {
4210  cerr << "Here are the "
4211  << num_decl_only_classes - num_resolved
4212  << " unresolved class declarations:\n";
4213  for (string_classes_or_unions_map::iterator i =
4214  declaration_only_classes().begin();
4215  i != declaration_only_classes().end();
4216  ++i)
4217  cerr << " " << i->first << "\n";
4218  }
4219  }
4220 
4221  /// Getter for the map of declaration-only enums that are to be
4222  /// resolved to their definition enums by the end of the corpus
4223  /// loading.
4224  ///
4225  /// @return a map of string -> vector of enums where the key is
4226  /// the fully qualified name of the enum and the value is the
4227  /// vector of declaration-only enum.
4228  const string_enums_map&
4229  declaration_only_enums() const
4230  {return decl_only_enums_map_;}
4231 
4232  /// Getter for the map of declaration-only enums that are to be
4233  /// resolved to their definition enums by the end of the corpus
4234  /// loading.
4235  ///
4236  /// @return a map of string -> vector of enums where the key is
4237  /// the fully qualified name of the enum and the value is the
4238  /// vector of declaration-only enum.
4240  declaration_only_enums()
4241  {return decl_only_enums_map_;}
4242 
4243  /// If a given enum is a declaration-only enum then stash it on
4244  /// the side so that at the end of the corpus reading we can resolve
4245  /// it to its definition.
4246  ///
4247  /// @param enom the enum to consider.
4248  void
4249  maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr& enom)
4250  {
4251  if (enom->get_is_declaration_only()
4252  && enom->get_definition_of_declaration() == 0)
4253  {
4254  string qn = enom->get_qualified_name();
4255  string_enums_map::iterator record =
4256  declaration_only_enums().find(qn);
4257  if (record == declaration_only_enums().end())
4258  declaration_only_enums()[qn].push_back(enom);
4259  else
4260  record->second.push_back(enom);
4261  }
4262  }
4263 
4264  /// Test if a given declaration-only enum has been scheduled for
4265  /// resolution to a defined enum.
4266  ///
4267  /// @param enom the enum to consider for the test.
4268  ///
4269  /// @return true iff @p enom is a declaration-only enum and if
4270  /// it's been scheduled for resolution to a defined enum.
4271  bool
4272  is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4273  {
4274  if (enom->get_is_declaration_only())
4275  return (declaration_only_enums().find(enom->get_qualified_name())
4276  != declaration_only_enums().end());
4277 
4278  return false;
4279  }
4280 
4281  /// Walk the declaration-only enums that have been found during
4282  /// the building of the corpus and resolve them to their definitions.
4283  ///
4284  /// TODO: Do away with this function by factorizing it with
4285  /// resolve_declaration_only_classes. All declaration-only decls
4286  /// could be handled the same way as declaration-only-ness is a
4287  /// property of abigail::ir::decl_base now.
4288  void
4289  resolve_declaration_only_enums()
4290  {
4291  vector<string> resolved_enums;
4292 
4293  for (string_enums_map::iterator i =
4294  declaration_only_enums().begin();
4295  i != declaration_only_enums().end();
4296  ++i)
4297  {
4298  bool to_resolve = false;
4299  for (enums_type::iterator j = i->second.begin();
4300  j != i->second.end();
4301  ++j)
4302  if ((*j)->get_is_declaration_only()
4303  && ((*j)->get_definition_of_declaration() == 0))
4304  to_resolve = true;
4305 
4306  if (!to_resolve)
4307  {
4308  resolved_enums.push_back(i->first);
4309  continue;
4310  }
4311 
4312  // Now, for each decl-only enum that have the current name
4313  // 'i->first', let's try to poke at the fully defined enum
4314  // that is defined in the same translation unit as the
4315  // declaration.
4316  //
4317  // If we find one enum (defined in the TU of the declaration)
4318  // that defines the declaration, then the declaration can be
4319  // resolved to that enum.
4320  //
4321  // If no defining enum is found in the TU of the declaration,
4322  // then there are possibly three cases to consider:
4323  //
4324  // 1/ There is exactly one enum that defines the
4325  // declaration and that enum is defined in another TU. In
4326  // this case, the declaration is resolved to that
4327  // definition.
4328  //
4329  // 2/ There are more than one enum that define that
4330  // declaration and none of them is defined in the TU of the
4331  // declaration. In this case, the declaration is left
4332  // unresolved.
4333  //
4334  // 3/ No enum defines the declaration. In this case, the
4335  // declaration is left unresoved.
4336 
4337  // So get the enums that might define the current
4338  // declarations which name is i->first.
4339  const type_base_wptrs_type *enums =
4340  lookup_enum_types(i->first, *corpus());
4341  if (!enums)
4342  continue;
4343 
4344  // This is a map that associates the translation unit path to
4345  // the enum (that potentially defines the declarations that
4346  // we consider) that are defined in that translation unit. It
4347  // should stay ordered by using the TU path as key to ensure
4348  // stability of the order of enum definitions in ABIXML
4349  // output.
4350  map<string, enum_type_decl_sptr> per_tu_enum_map;
4351  for (type_base_wptrs_type::const_iterator c = enums->begin();
4352  c != enums->end();
4353  ++c)
4354  {
4355  enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4356  ABG_ASSERT(enom);
4357 
4359  if (enom->get_is_declaration_only())
4360  continue;
4361 
4362  string tu_path = enom->get_translation_unit()->get_absolute_path();
4363  if (tu_path.empty())
4364  continue;
4365 
4366  // Build a map that associates the translation unit path
4367  // to the enum (that potentially defines the declarations
4368  // that we consider) that are defined in that translation unit.
4369  per_tu_enum_map[tu_path] = enom;
4370  }
4371 
4372  if (!per_tu_enum_map.empty())
4373  {
4374  // Walk the declarations to resolve and resolve them
4375  // either to the definitions that are in the same TU as
4376  // the declaration, or to the definition found elsewhere,
4377  // if there is only one such definition.
4378  for (enums_type::iterator j = i->second.begin();
4379  j != i->second.end();
4380  ++j)
4381  {
4382  if ((*j)->get_is_declaration_only()
4383  && ((*j)->get_definition_of_declaration() == 0))
4384  {
4385  string tu_path =
4386  (*j)->get_translation_unit()->get_absolute_path();
4387  map<string, enum_type_decl_sptr>::const_iterator e =
4388  per_tu_enum_map.find(tu_path);
4389  if (e != per_tu_enum_map.end())
4390  (*j)->set_definition_of_declaration(e->second);
4391  else if (per_tu_enum_map.size() == 1)
4392  (*j)->set_definition_of_declaration
4393  (per_tu_enum_map.begin()->second);
4394  else
4395  {
4396  // We are in case where there are more than
4397  // one definition for the declaration. Let's
4398  // see if they are all equal. If they are,
4399  // then the declaration resolves to the
4400  // definition. Otherwise, we are in the case
4401  // 3/ described above.
4402  map<string,
4403  enum_type_decl_sptr>::const_iterator it;
4404  enum_type_decl_sptr first_enum =
4405  per_tu_enum_map.begin()->second;
4406  bool all_enum_definitions_are_equal = true;
4407  for (it = per_tu_enum_map.begin();
4408  it != per_tu_enum_map.end();
4409  ++it)
4410  {
4411  if (it == per_tu_enum_map.begin())
4412  continue;
4413  else
4414  {
4415  if (!compare_before_canonicalisation(it->second,
4416  first_enum))
4417  {
4418  all_enum_definitions_are_equal = false;
4419  break;
4420  }
4421  }
4422  }
4423  if (all_enum_definitions_are_equal)
4424  (*j)->set_definition_of_declaration(first_enum);
4425  }
4426  }
4427  }
4428  resolved_enums.push_back(i->first);
4429  }
4430  }
4431 
4432  size_t num_decl_only_enums = declaration_only_enums().size(),
4433  num_resolved = resolved_enums.size();
4434  if (show_stats())
4435  cerr << "resolved " << num_resolved
4436  << " enum declarations out of "
4437  << num_decl_only_enums
4438  << "\n";
4439 
4440  for (vector<string>::const_iterator i = resolved_enums.begin();
4441  i != resolved_enums.end();
4442  ++i)
4443  declaration_only_enums().erase(*i);
4444 
4445  if (show_stats() && !declaration_only_enums().empty())
4446  {
4447  cerr << "Here are the "
4448  << num_decl_only_enums - num_resolved
4449  << " unresolved enum declarations:\n";
4450  for (string_enums_map::iterator i = declaration_only_enums().begin();
4451  i != declaration_only_enums().end();
4452  ++i)
4453  cerr << " " << i->first << "\n";
4454  }
4455  }
4456 
4457  /// Test if a symbol belongs to a function of the current ABI
4458  /// corpus.
4459  ///
4460  /// This is a sub-routine of fixup_functions_with_no_symbols.
4461  ///
4462  /// @param fn the function symbol to consider.
4463  ///
4464  /// @returnt true if @p fn belongs to a function of the current ABI
4465  /// corpus.
4466  bool
4467  symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4468  {
4469  corpus_sptr corp = corpus();
4470  if (!corp)
4471  return false;
4472 
4473  string id = fn->get_id_string();
4474 
4475  const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4476  if (!fns)
4477  return false;
4478 
4479  for (auto f : *fns)
4480  if (f->get_symbol())
4481  return true;
4482 
4483  return false;
4484  }
4485 
4486  /// Some functions described by DWARF may have their linkage name
4487  /// set, but no link to their actual underlying elf symbol. When
4488  /// these are virtual member functions, comparing the enclosing type
4489  /// against another one which has its underlying symbol properly set
4490  /// might lead to spurious type changes.
4491  ///
4492  /// If the corpus contains a symbol with the same name as the
4493  /// linkage name of the function, then set up the link between the
4494  /// function and its underlying symbol.
4495  ///
4496  /// Note that for the moment, only virtual member functions are
4497  /// fixed up like this. This is because they really are the only
4498  /// fuctions of functions that can affect types (in spurious ways).
4499  void
4500  fixup_functions_with_no_symbols()
4501  {
4502  corpus_sptr corp = corpus();
4503  if (!corp)
4504  return;
4505 
4506  die_function_decl_map_type &fns_with_no_symbol =
4507  die_function_decl_with_no_symbol_map();
4508 
4509  if (do_log())
4510  cerr << fns_with_no_symbol.size()
4511  << " functions to fixup, potentially\n";
4512 
4513  for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4514  i != fns_with_no_symbol.end();
4515  ++i)
4516  if (elf_symbol_sptr sym =
4517  corp->lookup_function_symbol(i->second->get_linkage_name()))
4518  {
4519  // So i->second is a virtual member function that was
4520  // previously scheduled to be set a function symbol.
4521  //
4522  // But if it appears that it now has a symbol already set,
4523  // then do not set a symbol to it again.
4524  //
4525  // Or if it appears that another virtual member function
4526  // from the current ABI Corpus, with the same linkage
4527  // (mangled) name has already been set a symbol, then do not
4528  // set a symbol to this function either. Otherwise, there
4529  // will be two virtual member functions with the same symbol
4530  // in the class and that leads to spurious hard-to-debug
4531  // change reports later down the road.
4532  if (i->second->get_symbol()
4533  || symbol_already_belongs_to_a_function(sym))
4534  continue;
4535 
4536  ABG_ASSERT(is_member_function(i->second));
4538  i->second->set_symbol(sym);
4539  // The function_decl now has an associated (public) ELF symbol so
4540  // it ought to be advertised as being public.
4541  i->second->set_is_in_public_symbol_table(true);
4542  // Add the function to the set of exported decls of the
4543  // current corpus.
4544  maybe_add_fn_to_exported_decls(i->second.get());
4545  if (do_log())
4546  cerr << "fixed up '"
4547  << i->second->get_pretty_representation()
4548  << "' with symbol '"
4549  << sym->get_id_string()
4550  << "'\n";
4551  }
4552 
4553  fns_with_no_symbol.clear();
4554  }
4555 
4556  /// Return a reference to the vector containing the types created
4557  /// during the binary analysis but that are not tied to a given
4558  /// DWARF DIE.
4559  ///
4560  /// @return reference to the vector containing the types created
4561  /// during the binary analysis but that are not tied to a given
4562  /// DWARF DIE.
4563  const vector<type_base_sptr>&
4564  types_to_canonicalize() const
4565  {return types_to_canonicalize_;}
4566 
4567  /// Clear the containers holding types to canonicalize.
4568  void
4569  clear_types_to_canonicalize()
4570  {
4571  types_to_canonicalize_.clear();
4572  }
4573 
4574  /// Types that were created but not tied to a particular DIE, must
4575  /// be scheduled for late canonicalization using this method.
4576  ///
4577  /// @param t the type to schedule for late canonicalization.
4578  void
4579  schedule_type_for_late_canonicalization(const type_base_sptr &t)
4580  {
4581  types_to_canonicalize_.push_back(t);
4582  }
4583 
4584  /// Canonicalize types which DIE offsets are stored in vectors on
4585  /// the side. This is a sub-routine of
4586  /// reader::perform_late_type_canonicalizing().
4587  ///
4588  /// @param source where the DIE of the types to canonicalize are
4589  /// from.
4590  void
4591  canonicalize_types_scheduled()
4592  {
4593  tools_utils::timer cn_timer;
4594  if (do_log())
4595  {
4596  cerr << "going to canonicalize types";
4597  corpus_sptr c = corpus();
4598  if (c)
4599  cerr << " of corpus " << corpus()->get_path();
4600  cn_timer.start();
4601  }
4602 
4603  if (!types_to_canonicalize().empty())
4604  canonicalize_types(types_to_canonicalize().begin(),
4605  types_to_canonicalize().end(),
4606  [](const vector<type_base_sptr>::const_iterator& i)
4607  {return *i;});
4608 
4609  if (do_log())
4610  {
4611  cn_timer.stop();
4612  cerr << "finished canonicalizing types";
4613  corpus_sptr c = corpus();
4614  if (c)
4615  cerr << " of corpus " << corpus()->get_path();
4616  cerr << ": (" << cn_timer << ")\n";
4617  }
4618  }
4619 
4620  /// Compute the number of canonicalized and missed types in the late
4621  /// canonicalization phase.
4622  ///
4623  /// @param source where the DIEs of the canonicalized types are
4624  /// from.
4625  ///
4626  /// @param canonicalized the number of types that got canonicalized
4627  /// is added to the value already present in this parameter.
4628  ///
4629  /// @param missed the number of types scheduled for late
4630  /// canonicalization and which couldn't be canonicalized (for a
4631  /// reason) is added to the value already present in this parameter.
4632  void
4633  add_late_canonicalized_types_stats(size_t& canonicalized,
4634  size_t& missed) const
4635  {
4636  for (auto t : types_to_canonicalize())
4637  {
4638  if (t->get_canonical_type())
4639  ++canonicalized;
4640  else
4641  ++missed;
4642  }
4643  }
4644 
4645  // Look at the types that need to be canonicalized after the
4646  // translation unit has been constructed and canonicalize them.
4647  void
4648  perform_late_type_canonicalizing()
4649  {
4650  canonicalize_types_scheduled();
4651 
4652  if (show_stats())
4653  {
4654  size_t num_canonicalized = 0, num_missed = 0, total = 0;
4655  add_late_canonicalized_types_stats(num_canonicalized,
4656  num_missed);
4657  total = num_canonicalized + num_missed;
4658  cerr << "binary: "
4659  << elf_path()
4660  << "\n";
4661  cerr << " # late canonicalized types: "
4662  << num_canonicalized;
4663  if (total)
4664  cerr << " (" << num_canonicalized * 100 / total << "%)";
4665  cerr << "\n"
4666  << " # missed canonicalization opportunities: "
4667  << num_missed;
4668  if (total)
4669  cerr << " (" << num_missed * 100 / total << "%)";
4670  cerr << "\n";
4671  }
4672 
4673  }
4674 
4675  const die_tu_map_type&
4676  die_tu_map() const
4677  {return die_tu_map_;}
4678 
4680  die_tu_map()
4681  {return die_tu_map_;}
4682 
4683  /// Getter for the map that associates a translation unit DIE to the
4684  /// vector of imported unit points that it contains.
4685  ///
4686  /// @param source where the DIEs are from.
4687  ///
4688  /// @return the map.
4690  tu_die_imported_unit_points_map(die_source source) const
4691  {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4692 
4693  /// Getter for the map that associates a translation unit DIE to the
4694  /// vector of imported unit points that it contains.
4695  ///
4696  /// @param source where the DIEs are from.
4697  ///
4698  /// @return the map.
4700  tu_die_imported_unit_points_map(die_source source)
4701  {
4702  switch (source)
4703  {
4704  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4705  break;
4706  case ALT_DEBUG_INFO_DIE_SOURCE:
4707  return alt_tu_die_imported_unit_points_map_;
4708  case TYPE_UNIT_DIE_SOURCE:
4709  return type_units_tu_die_imported_unit_points_map_;
4710  case NO_DEBUG_INFO_DIE_SOURCE:
4711  case NUMBER_OF_DIE_SOURCES:
4712  // We cannot reach this point.
4714  }
4715  return tu_die_imported_unit_points_map_;
4716  }
4717 
4718  /// Reset the current corpus being constructed.
4719  ///
4720  /// This actually deletes the current corpus being constructed.
4721  void
4722  reset_corpus()
4723  {corpus().reset();}
4724 
4725  /// Get the map that associates each DIE to its parent DIE. This is
4726  /// for DIEs coming from the main debug info sections.
4727  ///
4728  /// @param source where the DIEs in the map come from.
4729  ///
4730  /// @return the DIE -> parent map.
4731  const offset_offset_map_type&
4732  die_parent_map(die_source source) const
4733  {return const_cast<reader*>(this)->die_parent_map(source);}
4734 
4735  /// Get the map that associates each DIE to its parent DIE. This is
4736  /// for DIEs coming from the main debug info sections.
4737  ///
4738  /// @param source where the DIEs in the map come from.
4739  ///
4740  /// @return the DIE -> parent map.
4742  die_parent_map(die_source source)
4743  {
4744  switch (source)
4745  {
4746  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4747  break;
4748  case ALT_DEBUG_INFO_DIE_SOURCE:
4749  return alternate_die_parent_map_;
4750  case TYPE_UNIT_DIE_SOURCE:
4751  return type_section_die_parent_map();
4752  case NO_DEBUG_INFO_DIE_SOURCE:
4753  case NUMBER_OF_DIE_SOURCES:
4755  }
4756  return primary_die_parent_map_;
4757  }
4758 
4759  const offset_offset_map_type&
4760  type_section_die_parent_map() const
4761  {return type_section_die_parent_map_;}
4762 
4764  type_section_die_parent_map()
4765  {return type_section_die_parent_map_;}
4766 
4767  /// Getter of the current translation unit.
4768  ///
4769  /// @return the current translation unit being constructed.
4770  const translation_unit_sptr&
4771  cur_transl_unit() const
4772  {return cur_tu_;}
4773 
4774  /// Getter of the current translation unit.
4775  ///
4776  /// @return the current translation unit being constructed.
4778  cur_transl_unit()
4779  {return cur_tu_;}
4780 
4781  /// Setter of the current translation unit.
4782  ///
4783  /// @param tu the current translation unit being constructed.
4784  void
4785  cur_transl_unit(translation_unit_sptr tu)
4786  {
4787  if (tu)
4788  cur_tu_ = tu;
4789  }
4790 
4791  /// Return the global scope of the current translation unit.
4792  ///
4793  /// @return the global scope of the current translation unit.
4794  const scope_decl_sptr&
4795  global_scope() const
4796  {return cur_transl_unit()->get_global_scope();}
4797 
4798  /// Return a scope that is nil.
4799  ///
4800  /// @return a scope that is nil.
4801  const scope_decl_sptr&
4802  nil_scope() const
4803  {return nil_scope_;}
4804 
4805  const scope_stack_type&
4806  scope_stack() const
4807  {return scope_stack_;}
4808 
4810  scope_stack()
4811  {return scope_stack_;}
4812 
4813  scope_decl*
4814  current_scope()
4815  {
4816  if (scope_stack().empty())
4817  {
4818  if (cur_transl_unit())
4819  scope_stack().push(cur_transl_unit()->get_global_scope().get());
4820  }
4821  return scope_stack().top();
4822  }
4823 
4824  list<var_decl_sptr>&
4825  var_decls_to_re_add_to_tree()
4826  {return var_decls_to_add_;}
4827 
4828  /// Test if a DIE represents a decl (function or variable) that has
4829  /// a symbol that is exported, whatever that means. This is
4830  /// supposed to work for Linux Kernel binaries as well.
4831  ///
4832  /// This is useful to limit the amount of DIEs taken into account to
4833  /// the strict limit of what an ABI actually means. Limiting the
4834  /// volume of DIEs analyzed this way is an important optimization to
4835  /// keep big binaries "manageable" by libabigail.
4836  ///
4837  /// @param DIE the die to consider.
4838  bool
4839  is_decl_die_with_exported_symbol(const Dwarf_Die *die)
4840  {
4841  if (!die || !die_is_decl(die))
4842  return false;
4843 
4844  bool result = false, address_found = false, symbol_is_exported = false;;
4845  Dwarf_Addr decl_symbol_address = 0;
4846 
4847  if (die_is_variable_decl(die))
4848  {
4849  if ((address_found = get_variable_address(die, decl_symbol_address)))
4850  symbol_is_exported =
4851  !!variable_symbol_is_exported(decl_symbol_address);
4852  }
4853  else if (die_is_function_decl(die))
4854  {
4855  if ((address_found = get_function_address(die, decl_symbol_address)))
4856  symbol_is_exported =
4857  !!function_symbol_is_exported(decl_symbol_address);
4858  }
4859 
4860  if (address_found)
4861  result = symbol_is_exported;
4862 
4863  return result;
4864  }
4865 
4866  /// This is a sub-routine of maybe_adjust_fn_sym_address and
4867  /// maybe_adjust_var_sym_address.
4868  ///
4869  /// Given an address that we got by looking at some debug
4870  /// information (e.g, a symbol's address referred to by a DWARF
4871  /// TAG), If the ELF file we are interested in is a shared library
4872  /// or an executable, then adjust the address to be coherent with
4873  /// where the executable (or shared library) is loaded. That way,
4874  /// the address can be used to look for symbols in the executable or
4875  /// shared library.
4876  ///
4877  /// @return the adjusted address, or the same address as @p addr if
4878  /// it didn't need any adjustment.
4879  Dwarf_Addr
4880  maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4881  {
4882  if (addr == 0)
4883  return addr;
4884 
4885  GElf_Ehdr eh_mem;
4886  GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4887 
4888  if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4889  {
4890  Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4891  ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4892  dwarf_elf_load_address));
4893  ABG_ASSERT(get_binary_load_address(elf_handle(),
4894  elf_load_address));
4895  if (dwarf_is_splitted()
4896  && (dwarf_elf_load_address != elf_load_address))
4897  // This means that in theory the DWARF and the executable are
4898  // not loaded at the same address. And addr is meaningful
4899  // only in the context of the DWARF.
4900  //
4901  // So let's transform addr into an offset relative to where
4902  // the DWARF is loaded, and let's add that relative offset
4903  // to the load address of the executable. That way, addr
4904  // becomes meaningful in the context of the executable and
4905  // can thus be used to compare against the address of
4906  // symbols of the executable, for instance.
4907  addr = addr - dwarf_elf_load_address + elf_load_address;
4908  }
4909 
4910  return addr;
4911  }
4912 
4913  /// For a relocatable (*.o) elf file, this function expects an
4914  /// absolute address, representing a function symbol. It then
4915  /// extracts the address of the .text section from the symbol
4916  /// absolute address to get the relative address of the function
4917  /// from the beginning of the .text section.
4918  ///
4919  /// For executable or shared library, this function expects an
4920  /// address of a function symbol that was retrieved by looking at a
4921  /// DWARF "file". The function thus adjusts the address to make it
4922  /// be meaningful in the context of the ELF file.
4923  ///
4924  /// In both cases, the address can then be compared against the
4925  /// st_value field of a function symbol from the ELF file.
4926  ///
4927  /// @param addr an adress for a function symbol that was retrieved
4928  /// from a DWARF file.
4929  ///
4930  /// @return the (possibly) adjusted address, or just @p addr if no
4931  /// adjustment took place.
4932  Dwarf_Addr
4933  maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4934  {
4935  if (addr == 0)
4936  return addr;
4937 
4938  Elf* elf = elf_handle();
4939  GElf_Ehdr eh_mem;
4940  GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4941 
4942  if (elf_header->e_type == ET_REL)
4943  // We are looking at a relocatable file. In this case, we don't
4944  // do anything because:
4945  //
4946  // 1/ the addresses from DWARF are absolute (relative to the
4947  // beginning of the relocatable file)
4948  //
4949  // 2/ The ELF symbol addresses that we store in our lookup
4950  // tables are translated from section-related to absolute as
4951  // well. So we don't have anything to do at this point for
4952  // ET_REL files.
4953  ;
4954  else
4955  addr = maybe_adjust_address_for_exec_or_dyn(addr);
4956 
4957  return addr;
4958  }
4959 
4960  /// For a relocatable (*.o) elf file, this function expects an
4961  /// absolute address, representing a global variable symbol. It
4962  /// then extracts the address of the {.data,.data1,.rodata,.bss}
4963  /// section from the symbol absolute address to get the relative
4964  /// address of the variable from the beginning of the data section.
4965  ///
4966  /// For executable or shared library, this function expects an
4967  /// address of a variable symbol that was retrieved by looking at a
4968  /// DWARF "file". The function thus adjusts the address to make it
4969  /// be meaningful in the context of the ELF file.
4970  ///
4971  /// In both cases, the address can then be compared against the
4972  /// st_value field of a function symbol from the ELF file.
4973  ///
4974  /// @param addr an address for a global variable symbol that was
4975  /// retrieved from a DWARF file.
4976  ///
4977  /// @return the (possibly) adjusted address, or just @p addr if no
4978  /// adjustment took place.
4979  Dwarf_Addr
4980  maybe_adjust_var_sym_address(Dwarf_Addr addr) const
4981  {
4982  Elf* elf = elf_handle();
4983  GElf_Ehdr eh_mem;
4984  GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4985 
4986  if (elf_header->e_type == ET_REL)
4987  // We are looking at a relocatable file. In this case, we don't
4988  // do anything because:
4989  //
4990  // 1/ the addresses from DWARF are absolute (relative to the
4991  // beginning of the relocatable file)
4992  //
4993  // 2/ The ELF symbol addresses that we store in our lookup
4994  // tables are translated from section-related to absolute as
4995  // well. So we don't have anything to do at this point for
4996  // ET_REL files.
4997  ;
4998  else
4999  addr = maybe_adjust_address_for_exec_or_dyn(addr);
5000 
5001  return addr;
5002  }
5003 
5004  /// Get the first exported function address in the set of addresses
5005  /// referred to by the DW_AT_ranges attribute of a given DIE.
5006  ///
5007  /// @param die the DIE we are considering.
5008  ///
5009  /// @param address output parameter. This is set to the first
5010  /// address found in the sequence pointed to by the DW_AT_ranges
5011  /// attribute found on the DIE @p die, iff the function returns
5012  /// true. Otherwise, no value is set into this output parameter.
5013  ///
5014  /// @return true iff the DIE @p die does have a DW_AT_ranges
5015  /// attribute and an address of an exported function was found in
5016  /// its sequence value.
5017  bool
5018  get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5019  Dwarf_Addr& address) const
5020  {
5021  Dwarf_Addr base;
5022  Dwarf_Addr end_addr;
5023  ptrdiff_t offset = 0;
5024 
5025  do
5026  {
5027  Dwarf_Addr addr = 0, fn_addr = 0;
5028  if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5029  {
5030  fn_addr = maybe_adjust_fn_sym_address(addr);
5031  if (function_symbol_is_exported(fn_addr))
5032  {
5033  address = fn_addr;
5034  return true;
5035  }
5036  }
5037  } while (offset > 0);
5038  return false;
5039  }
5040 
5041  /// Get the address of the function.
5042  ///
5043  /// The address of the function is considered to be the value of the
5044  /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5045  /// only) to not point to an absolute address anymore, but rather to
5046  /// the address of the function inside the .text segment.
5047  ///
5048  /// @param function_die the die of the function to consider.
5049  ///
5050  /// @param address the resulting address iff the function returns
5051  /// true.
5052  ///
5053  /// @return true if the function address was found.
5054  bool
5055  get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5056  {
5057  if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5058  DW_AT_low_pc, address))
5059  // So no DW_AT_low_pc was found. Let's see if the function DIE
5060  // has got a DW_AT_ranges attribute instead. If it does, the
5061  // first address of the set of addresses represented by the
5062  // value of that DW_AT_ranges represents the function (symbol)
5063  // address we are looking for.
5064  if (!get_first_exported_fn_address_from_DW_AT_ranges
5065  (const_cast<Dwarf_Die*>(function_die),
5066  address))
5067  return false;
5068 
5069  address = maybe_adjust_fn_sym_address(address);
5070  return true;
5071  }
5072 
5073  /// Get the address of the global variable.
5074  ///
5075  /// The address of the global variable is considered to be the value
5076  /// of the DW_AT_location attribute, possibly adjusted (in
5077  /// relocatable files only) to not point to an absolute address
5078  /// anymore, but rather to the address of the global variable inside
5079  /// the data segment.
5080  ///
5081  /// @param variable_die the die of the function to consider.
5082  ///
5083  /// @param address the resulting address iff this function returns
5084  /// true.
5085  ///
5086  /// @return true if the variable address was found.
5087  bool
5088  get_variable_address(const Dwarf_Die* variable_die,
5089  Dwarf_Addr& address) const
5090  {
5091  bool is_tls_address = false;
5092  if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5093  address, is_tls_address))
5094  return false;
5095  if (!is_tls_address)
5096  address = maybe_adjust_var_sym_address(address);
5097  return true;
5098  }
5099 
5100  /// Getter of the exported decls builder object.
5101  ///
5102  /// @return the exported decls builder.
5103  corpus::exported_decls_builder*
5104  exported_decls_builder()
5105  {return corpus()->get_exported_decls_builder().get();}
5106 
5107  /// Getter of the "load_all_types" flag. This flag tells if all the
5108  /// types (including those not reachable by public declarations) are
5109  /// to be read and represented in the final ABI corpus.
5110  ///
5111  /// @return the load_all_types flag.
5112  bool
5113  load_all_types() const
5114  {return options().load_all_types;}
5115 
5116  /// Setter of the "load_all_types" flag. This flag tells if all the
5117  /// types (including those not reachable by public declarations) are
5118  /// to be read and represented in the final ABI corpus.
5119  ///
5120  /// @param f the new load_all_types flag.
5121  void
5122  load_all_types(bool f)
5123  {options().load_all_types = f;}
5124 
5125  bool
5126  load_in_linux_kernel_mode() const
5127  {return options().load_in_linux_kernel_mode;}
5128 
5129  void
5130  load_in_linux_kernel_mode(bool f)
5131  {options().load_in_linux_kernel_mode = f;}
5132 
5133  /// Test if it's allowed to assume that the DWARF debug info has
5134  /// been factorized (for instance, with the DWZ tool) so that if two
5135  /// type DIEs originating from the .gnu_debugaltlink section have
5136  /// different offsets, they represent different types.
5137  ///
5138  /// @return true iff we can assume that the DWARF debug info has
5139  /// been factorized.
5140  bool
5141  leverage_dwarf_factorization() const
5142  {
5143  if (!leverage_dwarf_factorization_.has_value())
5144  {
5145  if (options().leverage_dwarf_factorization
5146  && elf_helpers::find_section_by_name(elf_handle(),
5147  ".gnu_debugaltlink"))
5148  leverage_dwarf_factorization_ = true;
5149  else
5150  leverage_dwarf_factorization_ = false;
5151  }
5152  ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5153 
5154  return *leverage_dwarf_factorization_;
5155  }
5156  /// Getter of the "show_stats" flag.
5157  ///
5158  /// This flag tells if we should emit statistics about various
5159  /// internal stuff.
5160  ///
5161  /// @return the value of the flag.
5162  bool
5163  show_stats() const
5164  {return options().show_stats;}
5165 
5166  /// Setter of the "show_stats" flag.
5167  ///
5168  /// This flag tells if we should emit statistics about various
5169  /// internal stuff.
5170  ///
5171  /// @param f the value of the flag.
5172  void
5173  show_stats(bool f)
5174  {options().show_stats = f;}
5175 
5176  /// Getter of the "do_log" flag.
5177  ///
5178  /// This flag tells if we should log about various internal
5179  /// details.
5180  ///
5181  /// return the "do_log" flag.
5182  bool
5183  do_log() const
5184  {return options().do_log;}
5185 
5186  /// Setter of the "do_log" flag.
5187  ///
5188  /// This flag tells if we should log about various internal details.
5189  ///
5190  /// @param f the new value of the flag.
5191  void
5192  do_log(bool f)
5193  {options().do_log = f;}
5194 
5195  /// Walk the DIEs under a given die and for each child, populate the
5196  /// die -> parent map to record the child -> parent relationship
5197  /// that
5198  /// exists between the child and the given die.
5199  ///
5200  /// The function also builds the vector of places where units are
5201  /// imported.
5202  ///
5203  /// This is done recursively as for each child DIE, this function
5204  /// walks its children as well.
5205  ///
5206  /// @param die the DIE whose children to walk recursively.
5207  ///
5208  /// @param source where the DIE @p die comes from.
5209  ///
5210  /// @param imported_units a vector containing all the offsets of the
5211  /// points where unit have been imported, under @p die.
5212  void
5213  build_die_parent_relations_under(Dwarf_Die* die,
5214  die_source source,
5215  imported_unit_points_type & imported_units)
5216  {
5217  if (!die)
5218  return;
5219 
5220  offset_offset_map_type& parent_of = die_parent_map(source);
5221 
5222  Dwarf_Die child;
5223  if (dwarf_child(die, &child) != 0)
5224  return;
5225 
5226  do
5227  {
5228  parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5229  if (dwarf_tag(&child) == DW_TAG_imported_unit)
5230  {
5231  Dwarf_Die imported_unit;
5232  if (die_die_attribute(&child, DW_AT_import, imported_unit)
5233  // If the imported_unit has a sub-tree, let's record
5234  // this point at which the sub-tree is imported into
5235  // the current debug info.
5236  //
5237  // Otherwise, if the imported_unit has no sub-tree,
5238  // there is no point in recording where a non-existent
5239  // sub-tree is being imported.
5240  //
5241  // Note that the imported_unit_points_type type below
5242  // expects the imported_unit to have a sub-tree.
5243  && die_has_children(&imported_unit))
5244  {
5245  die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5246  ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5247  imported_units.push_back
5248  (imported_unit_point(dwarf_dieoffset(&child),
5249  imported_unit,
5250  imported_unit_die_source));
5251  }
5252  }
5253  build_die_parent_relations_under(&child, source, imported_units);
5254  }
5255  while (dwarf_siblingof(&child, &child) == 0);
5256 
5257  }
5258 
5259  /// Determine if we do have to build a DIE -> parent map, depending
5260  /// on a given language.
5261  ///
5262  /// Some languages like C++, Ada etc, do have the concept of
5263  /// namespace and yet, the DIE data structure doesn't provide us
5264  /// with a way to get the parent namespace of a given DIE. So for
5265  /// those languages, we need to build a DIE -> parent map so that we
5266  /// can get the namespace DIE (or more generally the scope DIE) of a given
5267  /// DIE as we need it.
5268  ///
5269  /// But then some more basic languages like C or assembly don't have
5270  /// that need.
5271  ///
5272  /// This function, depending on the language, tells us if we need to
5273  /// build the DIE -> parent map or not.
5274  ///
5275  /// @param lang the language to consider.
5276  ///
5277  /// @return true iff we need to build the DIE -> parent map for this
5278  /// language.
5279  bool
5280  do_we_build_die_parent_maps(translation_unit::language lang)
5281  {
5282  if (is_c_language(lang))
5283  return false;
5284 
5285  switch (lang)
5286  {
5287  case translation_unit::LANG_UNKNOWN:
5288 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5289  case translation_unit::LANG_Mips_Assembler:
5290 #endif
5291  return false;
5292  default:
5293  break;
5294  }
5295  return true;
5296  }
5297 
5298  /// Walk all the DIEs accessible in the debug info (and in the
5299  /// alternate debug info as well) and build maps representing the
5300  /// relationship DIE -> parent. That is, make it so that we can get
5301  /// the parent for a given DIE.
5302  ///
5303  /// Note that the goal of this map is to be able to get the parent
5304  /// of a given DIE. This is to mainly to handle namespaces. For instance,
5305  /// when we get a DIE of a type, and we want to build an internal
5306  /// representation for it, we need to get its fully qualified name.
5307  /// For that, we need to know what is the parent DIE of that type
5308  /// DIE, so that we can know what the namespace of that type is.
5309  ///
5310  /// Note that as the C language doesn't have namespaces (all types
5311  /// are defined in the same global namespace), this function doesn't
5312  /// build the DIE -> parent map if the current translation unit
5313  /// comes from C. This saves time on big C ELF files with a lot of
5314  /// DIEs.
5315  void
5316  build_die_parent_maps()
5317  {
5318  bool we_do_have_to_build_die_parent_map = false;
5319  uint8_t address_size = 0;
5320  size_t header_size = 0;
5321  // Get the DIE of the current translation unit, look at it to get
5322  // its language. If that language is in C, then all types are in
5323  // the global namespace so we don't need to build the DIE ->
5324  // parent map. So we dont build it in that case.
5325  for (Dwarf_Off offset = 0, next_offset = 0;
5326  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5327  offset, &next_offset, &header_size,
5328  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5329  offset = next_offset)
5330  {
5331  Dwarf_Off die_offset = offset + header_size;
5332  Dwarf_Die cu;
5333  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5334  die_offset, &cu))
5335  continue;
5336 
5337  uint64_t l = 0;
5338  die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5339  translation_unit::language lang = dwarf_language_to_tu_language(l);
5340  if (do_we_build_die_parent_maps(lang))
5341  we_do_have_to_build_die_parent_map = true;
5342  }
5343 
5344  if (!we_do_have_to_build_die_parent_map)
5345  return;
5346 
5347  // Build the DIE -> parent relation for DIEs coming from the
5348  // .debug_info section in the alternate debug info file.
5349  die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5350  for (Dwarf_Off offset = 0, next_offset = 0;
5351  (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5352  offset, &next_offset, &header_size,
5353  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5354  offset = next_offset)
5355  {
5356  Dwarf_Off die_offset = offset + header_size;
5357  Dwarf_Die cu;
5358  if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5359  die_offset, &cu))
5360  continue;
5361  cur_tu_die(&cu);
5362 
5363  imported_unit_points_type& imported_units =
5364  tu_die_imported_unit_points_map(source)[die_offset] =
5366  build_die_parent_relations_under(&cu, source, imported_units);
5367  }
5368 
5369  // Build the DIE -> parent relation for DIEs coming from the
5370  // .debug_info section of the main debug info file.
5371  source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5372  address_size = 0;
5373  header_size = 0;
5374  for (Dwarf_Off offset = 0, next_offset = 0;
5375  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5376  offset, &next_offset, &header_size,
5377  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5378  offset = next_offset)
5379  {
5380  Dwarf_Off die_offset = offset + header_size;
5381  Dwarf_Die cu;
5382  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5383  die_offset, &cu))
5384  continue;
5385  cur_tu_die(&cu);
5386  imported_unit_points_type& imported_units =
5387  tu_die_imported_unit_points_map(source)[die_offset] =
5389  build_die_parent_relations_under(&cu, source, imported_units);
5390  }
5391 
5392  // Build the DIE -> parent relation for DIEs coming from the
5393  // .debug_types section.
5394  source = TYPE_UNIT_DIE_SOURCE;
5395  address_size = 0;
5396  header_size = 0;
5397  uint64_t type_signature = 0;
5398  Dwarf_Off type_offset;
5399  for (Dwarf_Off offset = 0, next_offset = 0;
5400  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5401  offset, &next_offset, &header_size,
5402  NULL, NULL, &address_size, NULL,
5403  &type_signature, &type_offset) == 0);
5404  offset = next_offset)
5405  {
5406  Dwarf_Off die_offset = offset + header_size;
5407  Dwarf_Die cu;
5408 
5409  if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5410  die_offset, &cu))
5411  continue;
5412  cur_tu_die(&cu);
5413  imported_unit_points_type& imported_units =
5414  tu_die_imported_unit_points_map(source)[die_offset] =
5416  build_die_parent_relations_under(&cu, source, imported_units);
5417  }
5418  }
5419 };// end class reader.
5420 
5421 /// The type of the aggregates being compared during a DIE comparison.
5422 ///
5423 /// This encapsulates the stack of aggregates being compared at any
5424 /// single point.
5425 ///
5426 /// This is useful to detect "comparison cycles" and thus avoid the
5427 /// resulting infinite loops.
5428 ///
5429 /// This is also useful for implementing a very important optimization
5430 /// that takes place during the canonicalization
5431 struct offset_pairs_stack_type
5432 {
5433  // The DWARF DWARF reader that is useful for so many things.
5434  const reader& rdr_;
5435  // The set of types that are being compared. This is to speed up
5436  // searches.
5437  offset_pair_set_type set_;
5438  // The stack of types that are being compared. The top of the
5439  // stack is the back of the vector.
5441  // A map that associates a redundant type pair to the vector of
5442  // types that depends on it.
5443  offset_pair_vect_map_type redundant_types_;
5444  // A map that associates a dependant type to the vector of redundant
5445  // types it depends on.
5446  offset_pair_vect_map_type dependant_types_;
5447 
5448  offset_pairs_stack_type(const reader& rdr)
5449  : rdr_ (rdr)
5450  {}
5451 
5452  /// Add a pair of types being compared to the stack of aggregates
5453  /// being compared.
5454  ///
5455  /// @param p the pair of offsets of the type DIEs to consider.
5456  void
5457  add(const offset_pair_type& p)
5458  {
5459  set_.insert(p);
5460  vect_.push_back(p);
5461  }
5462 
5463  /// Erase a pair of types being compared from the stack of
5464  /// aggregates being compared.
5465  ///
5466  /// @param p the pair of offsets of the type DIEs to consider.
5467  ///
5468  /// @return true iff @p was found and erased from the stack.
5469  bool
5470  erase(const offset_pair_type& p)
5471  {
5472  if (set_.erase(p))
5473  {
5474  offset_pair_vector_type::iterator i;
5475 
5476  for (i = vect_.begin();i < vect_.end(); ++i)
5477  if (*i == p)
5478  break;
5479 
5480  if (i != vect_.end())
5481  vect_.erase(i);
5482 
5483  return true;
5484  }
5485 
5486  return false;
5487  }
5488 
5489  /// Test if a pair of type DIEs is part of the stack of type DIEs
5490  /// being compared.
5491  ///
5492  /// @param p the pair of offsets of the type DIEs to consider.
5493  ///
5494  /// @return true iff @p was found in the stack of types being
5495  /// compared.
5496  bool
5497  contains(const offset_pair_type &p) const
5498  {
5499  if (set_.find(p) == set_.end())
5500  return false;
5501  return true;
5502  }
5503 
5504  /// Get the set of comparison pair that depends on a given
5505  /// comparison pair.
5506  ///
5507  /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5508  /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5509  /// words, the pair T appears in the comparison stack BEFORE the
5510  /// pair P.
5511  ///
5512  /// So, this function returns the vector of comparison pairs that
5513  /// appear in the comparison stack AFTER a given comparison pair.
5514  ///
5515  /// @param p the comparison pair to consider.
5516  ///
5517  /// @param pairs out parameter. This is filled with the comparison
5518  /// pairs that depend on @p, iff the function returns true.
5519  ///
5520  /// @return true iff comparison pairs depending on @p have been
5521  /// found and collected in @pairs.
5522  bool
5523  get_pairs_that_depend_on(const offset_pair_type& p,
5524  offset_pair_vector_type& pairs) const
5525  {
5526  bool result = false;
5527  if (!contains(p))
5528  return result;
5529 
5530  // First, get an iterator on the position of 'p'.
5531  offset_pair_vector_type::const_iterator i;
5532  for (i = vect_.begin(); i != vect_.end(); ++i)
5533  if (*i == p)
5534  break;
5535 
5536  if (i == vect_.end())
5537  return result;
5538 
5539  // Then, harvest all the comparison pairs that come after the
5540  // position of 'p'.
5541  for (++i; i != vect_.end(); ++i)
5542  {
5543  pairs.push_back(*i);
5544  result = true;
5545  }
5546 
5547  return result;
5548  }
5549 
5550  /// Record the fact that a set of comparison pairs depends on a
5551  /// given comparison pair.
5552  ///
5553  /// Set a map that associates each dependant comparison pair to the
5554  /// pair it depends on.
5555  ///
5556  /// @param p the comparison pair that the set depends on.
5557  ///
5558  /// @param dependant_types the set of types that depends on @p.
5559  void
5560  record_dependant_types(const offset_pair_type& p,
5561  const offset_pair_vector_type& dependant_types)
5562  {
5563  for (auto type_pair : dependant_types)
5564  dependant_types_[type_pair].push_back(p);
5565  }
5566 
5567  /// Record a comparison pair as being redundant.
5568  ///
5569  ///
5570  /// @param p the comparison pair to record as redundant.
5571  void
5572  record_redundant_type_die_pair(const offset_pair_type& p)
5573  {
5574  offset_pair_vector_type dependant_types;
5575  get_pairs_that_depend_on(p, dependant_types);
5576 
5577  // First, record the relationship "p -> [pairs that depend on p]".
5578  auto it = redundant_types_.find(p);
5579  if (it == redundant_types_.end())
5580  {
5581  auto entry = std::make_pair(p, dependant_types);
5582  redundant_types_.insert(entry);
5583  }
5584  else
5585  it->second.insert(it->second.end(),
5586  dependant_types.begin(),
5587  dependant_types.end());
5588 
5589  // For each dependant type pair, record the association:
5590  // dependant_pair --> [vect of redundant types]
5591  record_dependant_types(p, dependant_types);
5592  }
5593 
5594  /// Test if a given pair has been detected as redundant.
5595  ///
5596  /// @param p the pair of DIEs to consider.
5597  ///
5598  /// @return iff @p is redundant.
5599  bool
5600  is_redundant(const offset_pair_type& p)
5601  {
5602  auto i = redundant_types_.find(p);
5603  if (i != redundant_types_.end())
5604  return true;
5605  return false;
5606  }
5607 
5608  /// Test if a given pair is dependant on at least a redundant type.
5609  ///
5610  /// @param p the pair to consider.
5611  ///
5612  /// @return true iff @p depends on a redundant type.
5613  bool
5614  depends_on_redundant_types(const offset_pair_type& p)
5615  {
5616  auto i = dependant_types_.find(p);
5617  if (i == dependant_types_.end())
5618  return false;
5619  return true;
5620  }
5621 
5622  /// Remove a redundant pair from the system.
5623  ///
5624  /// This needs updating the system to also remove the dependant
5625  /// types that depend on the redundant pair (if they depend only on
5626  /// that redundant pair).
5627  ///
5628  /// @param p the pair to consider.
5629  ///
5630  /// @param erase_canonical_die_offset if true then erase the cached
5631  /// comparison results for the redundant pair and its dependant
5632  /// types.
5633  void
5634  erase_redundant_type_pair_entry(const offset_pair_type& p,
5635  bool erase_cached_results = false)
5636  {
5637  // First, update the dependant types that depend on the redundant
5638  // type pair
5639  auto redundant_type = redundant_types_.find(p);
5640  if (redundant_type != redundant_types_.end())
5641  {
5642  for (auto dependant_type : redundant_type->second)
5643  {
5644  // Each dependant_type depends on the redundant type 'p',
5645  // among others.
5646  auto dependant_types_it = dependant_types_.find(dependant_type);
5647  ABG_ASSERT(dependant_types_it != dependant_types_.end());
5648  // Erase the redundant type 'p' from the redundant types
5649  // that dependant_type depends on.
5650  {
5651  auto i = dependant_types_it->second.begin();
5652  for (; i!= dependant_types_it->second.end();++i)
5653  if (*i == p)
5654  break;
5655  if (i != dependant_types_it->second.end())
5656  dependant_types_it->second.erase(i);
5657  }
5658  // If the dependant type itself doesn't depend on ANY
5659  // redundant type anymore, then remove the depend type
5660  // from the map of the dependant types.
5661  if (dependant_types_it->second.empty())
5662  {
5663  if (erase_cached_results)
5664  rdr_.die_comparison_results_.erase(dependant_type);
5665  dependant_types_.erase(dependant_types_it);
5666  }
5667  }
5668  }
5669  if (erase_cached_results)
5670  rdr_.die_comparison_results_.erase(p);
5671  redundant_types_.erase(p);
5672  }
5673 
5674  /// If a comparison pair has been detected as redundant, stop
5675  /// tracking it as well as its dependant pairs. That will
5676  /// essentially make it impossible to reset/cancel the canonical
5677  /// propagated types for those depdant pairs, but will also save
5678  /// ressources.
5679  ///
5680  /// @param p the comparison pair to consider.
5681  void
5682  confirm_canonical_propagated_type(const offset_pair_type& p)
5683  {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5684 
5685  /// Walk the types that depend on a comparison pair and cancel their
5686  /// canonical-propagate-type, that means remove their canonical
5687  /// types and mark them as not being canonically-propagated. Also,
5688  /// erase their cached comparison results that was likely set to
5689  /// COMPARISON_RESULT_UNKNOWN.
5690  ///
5691  /// @param p the pair to consider.
5692  void
5693  cancel_canonical_propagated_type(const offset_pair_type& p)
5694  {
5695  offset_pair_set_type dependant_types;
5696  get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5697  for (auto dependant_type : dependant_types)
5698  {
5699  // If this dependant type was canonical-type-propagated then
5700  // erase that canonical type.
5701  if (rdr_.propagated_types_.find(dependant_type)
5702  != rdr_.propagated_types_.end())
5703  {
5704  rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5705  dependant_type.first.source_,
5706  /*die_as_type=*/true);
5707  rdr_.propagated_types_.erase(dependant_type);
5708  rdr_.cancelled_propagation_count_++;
5709  }
5710  // Update the cached result. We know the comparison result
5711  // must now be different.
5712  auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5713  if (comp_result_it != rdr_.die_comparison_results_.end())
5714  comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5715  }
5716 
5717  // Update the cached result of the root type to cancel too.
5718  auto comp_result_it = rdr_.die_comparison_results_.find(p);
5719  if (comp_result_it != rdr_.die_comparison_results_.end())
5720  {
5721  // At this point, the result of p is either
5722  // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5723  // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5724  // Make sure it's the cached result is now
5725  // COMPARISON_RESULT_DIFFERENT.
5726  if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5727  comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5728  ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5729  }
5730 
5731  if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5732  {
5733  rdr_.erase_canonical_die_offset(p.first.offset_,
5734  p.first.source_,
5735  /*die_as_type=*/true);
5736  rdr_.propagated_types_.erase(p);
5737  rdr_.cancelled_propagation_count_++;
5738  }
5739  }
5740 
5741  /// Get the set of comparison pairs that depend on a given pair.
5742  ///
5743  /// @param p the pair to consider.
5744  ///
5745  /// @param result this is set to the pairs that depend on @p, iff
5746  /// the function returned true.
5747  ///
5748  /// @param transitive_closure if set to true, the transitive closure
5749  /// of the @result is set to it.
5750  ///
5751  /// @return true iff @result could be filled with the dependant
5752  /// types.
5753  bool
5754  get_dependant_types(const offset_pair_type& p,
5755  offset_pair_set_type& result,
5756  bool transitive_closure = false)
5757  {
5758  auto i = redundant_types_.find(p);
5759  if (i != redundant_types_.end())
5760  {
5761  for (auto dependant_type : i->second)
5762  if (result.find(dependant_type) == result.end())
5763  {
5764  result.insert(dependant_type);
5765  if (transitive_closure)
5766  get_dependant_types(p, result, /*transitive_closure=*/true);
5767  }
5768  return true;
5769  }
5770  return false;
5771  }
5772 }; // end struct offset_pairs_stack_type
5773 
5775 build_ir_node_from_die(reader& rdr,
5776  Dwarf_Die* die,
5777  scope_decl* scope,
5778  bool called_from_public_decl,
5779  size_t where_offset,
5780  bool is_declaration_only = true,
5781  bool is_required_decl_spec = false);
5782 
5784 build_ir_node_from_die(reader& rdr,
5785  Dwarf_Die* die,
5786  bool called_from_public_decl,
5787  size_t where_offset);
5788 
5789 static class_decl_sptr
5790 add_or_update_class_type(reader& rdr,
5791  Dwarf_Die* die,
5792  scope_decl* scope,
5793  bool is_struct,
5794  class_decl_sptr klass,
5795  bool called_from_public_decl,
5796  size_t where_offset,
5797  bool is_declaration_only);
5798 
5799 static union_decl_sptr
5800 add_or_update_union_type(reader& rdr,
5801  Dwarf_Die* die,
5802  scope_decl* scope,
5803  union_decl_sptr union_type,
5804  bool called_from_public_decl,
5805  size_t where_offset,
5806  bool is_declaration_only);
5807 
5808 static decl_base_sptr
5809 build_ir_node_for_void_type(reader& rdr);
5810 
5811 static decl_base_sptr
5812 build_ir_node_for_variadic_parameter_type(reader &rdr);
5813 
5814 static function_decl_sptr
5815 build_function_decl(reader& rdr,
5816  Dwarf_Die* die,
5817  size_t where_offset,
5818  function_decl_sptr fn);
5819 
5820 static bool
5821 function_is_suppressed(const reader& rdr,
5822  const scope_decl* scope,
5823  Dwarf_Die *function_die,
5824  bool is_declaration_only);
5825 
5826 static function_decl_sptr
5827 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5828  scope_decl *scope,
5829  Dwarf_Die *die,
5830  size_t where_offset,
5831  bool is_declaration_only,
5832  function_decl_sptr f);
5833 
5834 static var_decl_sptr
5835 build_var_decl(reader& rdr,
5836  Dwarf_Die *die,
5837  size_t where_offset,
5838  var_decl_sptr result = var_decl_sptr());
5839 
5840 static var_decl_sptr
5841 build_or_get_var_decl_if_not_suppressed(reader& rdr,
5842  scope_decl *scope,
5843  Dwarf_Die *die,
5844  size_t where_offset,
5845  var_decl_sptr res = var_decl_sptr(),
5846  bool is_required_decl_spec = false);
5847 static bool
5848 variable_is_suppressed(const reader& rdr,
5849  const scope_decl* scope,
5850  Dwarf_Die *variable_die,
5851  bool is_required_decl_spec = false);
5852 
5853 static void
5854 finish_member_function_reading(Dwarf_Die* die,
5855  const function_decl_sptr& f,
5856  const class_or_union_sptr klass,
5857  reader& rdr);
5858 
5859 /// Test if a given DIE is anonymous
5860 ///
5861 /// @param die the DIE to consider.
5862 ///
5863 /// @return true iff @p die is anonymous.
5864 static bool
5865 die_is_anonymous(const Dwarf_Die* die)
5866 {
5867  Dwarf_Attribute attr;
5868  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5869  return true;
5870  return false;
5871 }
5872 
5873 /// Test if a DIE is an anonymous data member, aka, "unnamed field".
5874 ///
5875 /// Unnamed fields are specified at
5876 /// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5877 ///
5878 /// @param die the DIE to consider.
5879 ///
5880 /// @return true iff @p die is an anonymous data member.
5881 static bool
5882 die_is_anonymous_data_member(const Dwarf_Die* die)
5883 {
5884  if (!die
5885  || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5886  || !die_name(die).empty())
5887  return false;
5888 
5889  Dwarf_Die type_die;
5890  if (!die_die_attribute(die, DW_AT_type, type_die))
5891  return false;
5892 
5893  if (dwarf_tag(&type_die) != DW_TAG_structure_type
5894  && dwarf_tag(&type_die) != DW_TAG_union_type)
5895  return false;
5896 
5897  return true;
5898 }
5899 
5900 /// Get the value of an attribute that is supposed to be a string, or
5901 /// an empty string if the attribute could not be found.
5902 ///
5903 /// @param die the DIE to get the attribute value from.
5904 ///
5905 /// @param attr_name the attribute name. Must come from dwarf.h and
5906 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5907 ///
5908 /// @return the string representing the value of the attribute, or an
5909 /// empty string if no string attribute could be found.
5910 static string
5911 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5912 {
5913  if (!die)
5914  return "";
5915 
5916  Dwarf_Attribute attr;
5917  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5918  return "";
5919 
5920  const char* str = dwarf_formstring(&attr);
5921  return str ? str : "";
5922 }
5923 
5924 /// Get the value of an attribute that is supposed to be a string, or
5925 /// an empty string if the attribute could not be found.
5926 ///
5927 /// @param die the DIE to get the attribute value from.
5928 ///
5929 /// @param attr_name the attribute name. Must come from dwarf.h and
5930 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5931 ///
5932 /// @return the char* representing the value of the attribute, or an
5933 /// empty string if no string attribute could be found.
5934 static const char*
5935 die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
5936 {
5937  if (!die)
5938  return nullptr;
5939 
5940  Dwarf_Attribute attr;
5941  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5942  return nullptr;
5943 
5944  const char* str = dwarf_formstring(&attr);
5945  return str;
5946 }
5947 
5948 /// Get the value of an attribute that is supposed to be an unsigned
5949 /// constant.
5950 ///
5951 /// @param die the DIE to read the information from.
5952 ///
5953 /// @param attr_name the DW_AT_* name of the attribute. Must come
5954 /// from dwarf.h and be an enumerator representing an attribute like,
5955 /// e.g, DW_AT_decl_line.
5956 ///
5957 ///@param cst the output parameter that is set to the value of the
5958 /// attribute @p attr_name. This parameter is set iff the function
5959 /// return true.
5960 ///
5961 /// @return true if there was an attribute of the name @p attr_name
5962 /// and with a value that is a constant, false otherwise.
5963 static bool
5964 die_unsigned_constant_attribute(const Dwarf_Die* die,
5965  unsigned attr_name,
5966  uint64_t& cst)
5967 {
5968  if (!die)
5969  return false;
5970 
5971  Dwarf_Attribute attr;
5972  Dwarf_Word result = 0;
5973  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5974  || dwarf_formudata(&attr, &result))
5975  return false;
5976 
5977  cst = result;
5978  return true;
5979 }
5980 
5981 /// Read a signed constant value from a given attribute.
5982 ///
5983 /// The signed constant expected must be of constant form.
5984 ///
5985 /// @param die the DIE to get the attribute from.
5986 ///
5987 /// @param attr_name the attribute name.
5988 ///
5989 /// @param cst the resulting signed constant read.
5990 ///
5991 /// @return true iff a signed constant attribute of the name @p
5992 /// attr_name was found on the DIE @p die.
5993 static bool
5994 die_signed_constant_attribute(const Dwarf_Die *die,
5995  unsigned attr_name,
5996  int64_t& cst)
5997 {
5998  if (!die)
5999  return false;
6000 
6001  Dwarf_Attribute attr;
6002  Dwarf_Sword result = 0;
6003  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6004  || dwarf_formsdata(&attr, &result))
6005  return false;
6006 
6007  cst = result;
6008  return true;
6009 }
6010 
6011 /// Read the value of a constant attribute that is either signed or
6012 /// unsigned into a array_type_def::subrange_type::bound_value value.
6013 ///
6014 /// The bound_value instance will capture the actual signedness of the
6015 /// read attribute.
6016 ///
6017 /// @param die the DIE from which to read the value of the attribute.
6018 ///
6019 /// @param attr_name the attribute name to consider.
6020 ///
6021 /// @param is_signed true if the attribute value has to read as
6022 /// signed.
6023 ///
6024 /// @param value the resulting value read from attribute @p attr_name
6025 /// on DIE @p die.
6026 ///
6027 /// @return true iff DIE @p die has an attribute named @p attr_name
6028 /// with a constant value.
6029 static bool
6030 die_constant_attribute(const Dwarf_Die *die,
6031  unsigned attr_name,
6032  bool is_signed,
6033  array_type_def::subrange_type::bound_value &value)
6034 {
6035  if (!is_signed)
6036  {
6037  uint64_t l = 0;
6038  if (!die_unsigned_constant_attribute(die, attr_name, l))
6039  return false;
6040  value.set_unsigned(l);
6041  }
6042  else
6043  {
6044  int64_t l = 0;
6045  if (!die_signed_constant_attribute(die, attr_name, l))
6046  return false;
6047  value.set_signed(l);
6048  }
6049  return true;
6050 }
6051 
6052 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
6053 ///
6054 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6055 /// enum in dwarf.h so we have to use an unsigned int for the form,
6056 /// grrr.
6057 ///
6058 /// @param form the form to consider.
6059 ///
6060 /// @return true iff @p form is DW_FORM_strx{1,4}.
6061 static bool
6062 form_is_DW_FORM_strx(unsigned form)
6063 {
6064  if (form)
6065  {
6066 #if defined HAVE_DW_FORM_strx1 \
6067  && defined HAVE_DW_FORM_strx2 \
6068  && defined HAVE_DW_FORM_strx3 \
6069  && defined HAVE_DW_FORM_strx4
6070  if (form == DW_FORM_strx1
6071  || form == DW_FORM_strx2
6072  || form == DW_FORM_strx3
6073  ||form == DW_FORM_strx4)
6074  return true;
6075 #endif
6076  }
6077  return false;
6078 }
6079 
6080 /// Test if a given DWARF form is DW_FORM_line_strp.
6081 ///
6082 /// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6083 /// enum in dwarf.h so we have to use an unsigned int for the form,
6084 /// grrr.
6085 ///
6086 /// @param form the form to consider.
6087 ///
6088 /// @return true iff @p form is DW_FORM_line_strp.
6089 static bool
6090 form_is_DW_FORM_line_strp(unsigned form)
6091 {
6092  if (form)
6093  {
6094 #if defined HAVE_DW_FORM_line_strp
6095  if (form == DW_FORM_line_strp)
6096  return true;
6097 #endif
6098  }
6099  return false;
6100 }
6101 
6102 /// Get the value of a DIE attribute; that value is meant to be a
6103 /// flag.
6104 ///
6105 /// @param die the DIE to get the attribute from.
6106 ///
6107 /// @param attr_name the DW_AT_* name of the attribute. Must come
6108 /// from dwarf.h and be an enumerator representing an attribute like,
6109 /// e.g, DW_AT_external.
6110 ///
6111 /// @param flag the output parameter to store the flag value into.
6112 /// This is set iff the function returns true.
6113 ///
6114 /// @param recursively if true, the function looks through the
6115 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6116 /// all the way down to the initial DIE that is cloned and look on
6117 /// that DIE to see if it has the @p attr_name attribute.
6118 ///
6119 /// @return true if the DIE has a flag attribute named @p attr_name,
6120 /// false otherwise.
6121 static bool
6122 die_flag_attribute(const Dwarf_Die* die,
6123  unsigned attr_name,
6124  bool& flag,
6125  bool recursively = true)
6126 {
6127  Dwarf_Attribute attr;
6128  if (recursively
6129  ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6130  : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6131  return false;
6132 
6133  bool f = false;
6134  if (dwarf_formflag(&attr, &f))
6135  return false;
6136 
6137  flag = f;
6138  return true;
6139 }
6140 
6141 /// Get the mangled name from a given DIE.
6142 ///
6143 /// @param die the DIE to read the mangled name from.
6144 ///
6145 /// @return the mangled name if it's present in the DIE, or just an
6146 /// empty string if it's not.
6147 static string
6148 die_linkage_name(const Dwarf_Die* die)
6149 {
6150  if (!die)
6151  return "";
6152 
6153  string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6154  if (linkage_name.empty())
6155  linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6156  return linkage_name;
6157 }
6158 
6159 /// Get the file path that is the value of the DW_AT_decl_file
6160 /// attribute on a given DIE, if the DIE is a decl DIE having that
6161 /// attribute.
6162 ///
6163 /// @param die the DIE to consider.
6164 ///
6165 /// @return a string containing the file path that is the logical
6166 /// value of the DW_AT_decl_file attribute. If the DIE @p die
6167 /// doesn't have a DW_AT_decl_file attribute, then the return value is
6168 /// just an empty string.
6169 static string
6170 die_decl_file_attribute(const Dwarf_Die* die)
6171 {
6172  if (!die)
6173  return "";
6174 
6175  const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6176 
6177  return str ? str : "";
6178 }
6179 
6180 /// Get the value of an attribute which value is supposed to be a
6181 /// reference to a DIE.
6182 ///
6183 /// @param die the DIE to read the value from.
6184 ///
6185 /// @param attr_name the DW_AT_* attribute name to read.
6186 ///
6187 /// @param result the DIE resulting from reading the attribute value.
6188 /// This is set iff the function returns true.
6189 ///
6190 /// @param recursively if true, the function looks through the
6191 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6192 /// all the way down to the initial DIE that is cloned and look on
6193 /// that DIE to see if it has the @p attr_name attribute.
6194 ///
6195 /// @return true if the DIE @p die contains an attribute named @p
6196 /// attr_name that is a DIE reference, false otherwise.
6197 static bool
6198 die_die_attribute(const Dwarf_Die* die,
6199  unsigned attr_name,
6200  Dwarf_Die& result,
6201  bool recursively)
6202 {
6203  Dwarf_Attribute attr;
6204  if (recursively
6205  ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6206  : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6207  return false;
6208 
6209  return dwarf_formref_die(&attr, &result);
6210 }
6211 
6212 /// Test if a subrange DIE indirectly references another subrange DIE
6213 /// through a given attribute.
6214 ///
6215 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6216 /// attribute be a reference to either a data member or a variable
6217 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6218 /// DIE is said to be "indirectly referenced" by the former subrange
6219 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6220 /// the value we want for the DW_AT_upper_bound of the former.
6221 ///
6222 /// This function tests if the former subrange DIE does indirectly
6223 /// reference another subrange DIE through a given attribute (not
6224 /// necessarily DW_AT_upper_bound).
6225 ///
6226 /// @param die the DIE to consider. Note that It must be a
6227 /// DW_TAG_subrange_type.
6228 ///
6229 /// @param attr_name the name of the attribute to look through for the
6230 /// indirectly referenced subrange DIE.
6231 ///
6232 /// @param referenced_subrange if the function returns true, then the
6233 /// argument of this parameter is set to the indirectly referenced
6234 /// DW_TAG_subrange_type DIE.
6235 ///
6236 /// @return true iff @p DIE indirectly references a subrange DIE
6237 /// through the attribute @p attr_name.
6238 static bool
6239 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6240  unsigned attr_name,
6241  Dwarf_Die& referenced_subrange)
6242 {
6243  bool result = false;
6244 
6245  if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6246  return result;
6247 
6248  Dwarf_Die referenced_die;
6249  if (die_die_attribute(die, attr_name, referenced_die))
6250  {
6251  unsigned tag = dwarf_tag(&referenced_die);
6252  if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6253  {
6254  Dwarf_Die type_die;
6255  if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6256  {
6257  tag = dwarf_tag(&type_die);
6258  if (tag == DW_TAG_subrange_type)
6259  {
6260  memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6261  result = true;
6262  }
6263  }
6264  }
6265  }
6266  return result;
6267 }
6268 
6269 /// Return the bound value of subrange die by looking at an indirectly
6270 /// referenced subrange DIE.
6271 ///
6272 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6273 /// attribute be a reference to either a data member or a variable
6274 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6275 /// DIE is said to be "indirectly referenced" by the former subrange
6276 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6277 /// the value we want for the DW_AT_{lower,upper}_bound of the former.
6278 ///
6279 /// This function gets the DW_AT_{lower,upper}_bound value of a
6280 /// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6281 /// the indirectly referenced subrange type, if it exists.
6282 ///
6283 /// @param die the subrange DIE to consider.
6284 ///
6285 /// @param attr_name the name of the attribute to consider, typically,
6286 /// DW_AT_{lower,upper}_bound.
6287 ///
6288 /// @param v the found value, iff this function returned true.
6289 ///
6290 /// @param is_signed, this is set to true if @p v is signed. This
6291 /// parameter is set at all only if the function returns true.
6292 ///
6293 /// @return true iff the DW_AT_{lower,upper}_bound was found on the
6294 /// indirectly referenced subrange type.
6295 static bool
6296 subrange_die_indirect_bound_value(const Dwarf_Die *die,
6297  unsigned attr_name,
6298  array_type_def::subrange_type::bound_value& v,
6299  bool& is_signed)
6300 {
6301  bool result = false;
6302 
6303  if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6304  return result;
6305 
6306  Dwarf_Die subrange_die;
6307  if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6308  subrange_die))
6309  {
6310  if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6311  result = true;
6312  }
6313  return result;
6314 }
6315 
6316 /// Read and return an addresss class attribute from a given DIE.
6317 ///
6318 /// @param die the DIE to consider.
6319 ///
6320 /// @param attr_name the name of the address class attribute to read
6321 /// the value from.
6322 ///
6323 /// @param the resulting address.
6324 ///
6325 /// @return true iff the attribute could be read, was of the expected
6326 /// address class and could thus be translated into the @p result.
6327 static bool
6328 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6329 {
6330  Dwarf_Attribute attr;
6331  if (!dwarf_attr_integrate(die, attr_name, &attr))
6332  return false;
6333  return dwarf_formaddr(&attr, &result) == 0;
6334 }
6335 
6336 /// Returns the source location associated with a decl DIE.
6337 ///
6338 /// @param rdr the @ref reader to use.
6339 ///
6340 /// @param die the DIE the read the source location from.
6341 ///
6342 /// @return the location associated with @p die.
6343 static location
6344 die_location(const reader& rdr, const Dwarf_Die* die)
6345 {
6346  if (!die)
6347  return location();
6348 
6349  string file = die_decl_file_attribute(die);
6350  uint64_t line = 0;
6351  die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6352 
6353  if (!file.empty() && line != 0)
6354  {
6355  translation_unit_sptr tu = rdr.cur_transl_unit();
6356  location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6357  return l;
6358  }
6359  return location();
6360 }
6361 
6362 /// Return a copy of the name of a DIE.
6363 ///
6364 /// @param die the DIE to consider.
6365 ///
6366 /// @return a copy of the name of the DIE.
6367 static string
6368 die_name(const Dwarf_Die* die)
6369 {
6370  string name = die_string_attribute(die, DW_AT_name);
6371  return name;
6372 }
6373 
6374 /// Return the location, the name and the mangled name of a given DIE.
6375 ///
6376 /// @param rdr the DWARF reader to use.
6377 ///
6378 /// @param die the DIE to read location and names from.
6379 ///
6380 /// @param loc the location output parameter to set.
6381 ///
6382 /// @param name the name output parameter to set.
6383 ///
6384 /// @param linkage_name the linkage_name output parameter to set.
6385 static void
6386 die_loc_and_name(const reader& rdr,
6387  Dwarf_Die* die,
6388  location& loc,
6389  string& name,
6390  string& linkage_name)
6391 {
6392  loc = die_location(rdr, die);
6393  name = die_name(die);
6394  linkage_name = die_linkage_name(die);
6395 }
6396 
6397 /// Get the size of a (type) DIE as the value for the parameter
6398 /// DW_AT_byte_size or DW_AT_bit_size.
6399 ///
6400 /// @param die the DIE to read the information from.
6401 ///
6402 /// @param size the resulting size in bits. This is set iff the
6403 /// function return true.
6404 ///
6405 /// @return true if the size attribute was found.
6406 static bool
6407 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6408 {
6409  if (!die)
6410  return false;
6411 
6412  uint64_t byte_size = 0, bit_size = 0;
6413 
6414  if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6415  {
6416  if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6417  return false;
6418  }
6419  else
6420  bit_size = byte_size * 8;
6421 
6422  size = bit_size;
6423 
6424  return true;
6425 }
6426 
6427 /// Get the access specifier (from the DW_AT_accessibility attribute
6428 /// value) of a given DIE.
6429 ///
6430 /// @param die the DIE to consider.
6431 ///
6432 /// @param access the resulting access. This is set iff the function
6433 /// returns true.
6434 ///
6435 /// @return bool if the DIE contains the DW_AT_accessibility die.
6436 static bool
6437 die_access_specifier(Dwarf_Die * die, access_specifier& access)
6438 {
6439  if (!die)
6440  return false;
6441 
6442  uint64_t a = 0;
6443  if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6444  return false;
6445 
6446  access_specifier result = private_access;
6447 
6448  switch (a)
6449  {
6450  case private_access:
6451  result = private_access;
6452  break;
6453 
6454  case protected_access:
6455  result = protected_access;
6456  break;
6457 
6458  case public_access:
6459  result = public_access;
6460  break;
6461 
6462  default:
6463  break;
6464  }
6465 
6466  access = result;
6467  return true;
6468 }
6469 
6470 /// Test whether a given DIE represents a decl that is public. That
6471 /// is, one with the DW_AT_external attribute set.
6472 ///
6473 /// @param die the DIE to consider for testing.
6474 ///
6475 /// @return true if a DW_AT_external attribute is present and its
6476 /// value is set to the true; return false otherwise.
6477 static bool
6478 die_is_public_decl(const Dwarf_Die* die)
6479 {
6480  if (!die)
6481  return false;
6482  bool is_public = false;
6483 
6484  // If this is a DW_TAG_subprogram DIE, look for the
6485  // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6486  // then it's public. In all other cases, this should return false.
6487 
6488  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6489  if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6490  die_flag_attribute(die, DW_AT_external, is_public);
6491  else if (tag == DW_TAG_namespace)
6492  {
6493  string name = die_name(die);
6494  is_public = !name.empty();
6495  }
6496 
6497  return is_public;
6498 }
6499 
6500 /// Test if a DIE is effectively public.
6501 ///
6502 /// This is meant to return true when either the DIE is public or when
6503 /// it's a variable DIE that is at (global) namespace level.
6504 ///
6505 /// @return true iff either the DIE is public or is a variable DIE
6506 /// that is at (global) namespace level.
6507 static bool
6508 die_is_effectively_public_decl(const reader& rdr,
6509  const Dwarf_Die* die)
6510 {
6511  if (die_is_public_decl(die))
6512  return true;
6513 
6514  unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6515  if (tag == DW_TAG_variable || tag == DW_TAG_member)
6516  {
6517  // The DIE is a variable.
6518  Dwarf_Die parent_die;
6519  size_t where_offset = 0;
6520  if (!get_parent_die(rdr, die, parent_die, where_offset))
6521  return false;
6522 
6523  tag = dwarf_tag(&parent_die);
6524  if (tag == DW_TAG_compile_unit
6525  || tag == DW_TAG_partial_unit
6526  || tag == DW_TAG_type_unit)
6527  // The DIE is at global scope.
6528  return true;
6529 
6530  if (tag == DW_TAG_namespace)
6531  {
6532  string name = die_name(&parent_die);
6533  if (name.empty())
6534  // The DIE at unnamed namespace scope, so it's not public.
6535  return false;
6536  // The DIE is at namespace scope.
6537  return true;
6538  }
6539  }
6540  return false;
6541 }
6542 
6543 /// Test whether a given DIE represents a declaration-only DIE.
6544 ///
6545 /// That is, if the DIE has the DW_AT_declaration flag set.
6546 ///
6547 /// @param die the DIE to consider.
6548 //
6549 /// @return true if a DW_AT_declaration is present, false otherwise.
6550 static bool
6551 die_is_declaration_only(Dwarf_Die* die)
6552 {
6553  bool is_declaration = false;
6554  die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6555  if (is_declaration && !die_has_size_attribute(die))
6556  return true;
6557  return false;
6558 }
6559 
6560 /// Test if a DIE is for a function decl.
6561 ///
6562 /// @param die the DIE to consider.
6563 ///
6564 /// @return true iff @p die represents a function decl.
6565 static bool
6566 die_is_function_decl(const Dwarf_Die *die)
6567 {
6568  if (!die)
6569  return false;
6570 
6571  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6572  if (tag == DW_TAG_subprogram)
6573  return true;
6574  return false;
6575 }
6576 
6577 /// Test if a DIE is for a variable decl.
6578 ///
6579 /// @param die the DIE to consider.
6580 ///
6581 /// @return true iff @p die represents a variable decl.
6582 static bool
6583 die_is_variable_decl(const Dwarf_Die *die)
6584 {
6585  if (!die)
6586  return false;
6587 
6588  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6589  if (tag == DW_TAG_variable)
6590  return true;
6591  return false;
6592 }
6593 
6594 /// Test if a DIE has size attribute.
6595 ///
6596 /// @param die the DIE to consider.
6597 ///
6598 /// @return true if the DIE has a size attribute.
6599 static bool
6600 die_has_size_attribute(const Dwarf_Die *die)
6601 {
6602  uint64_t s;
6603  if (die_size_in_bits(die, s))
6604  return true;
6605  return false;
6606 }
6607 
6608 /// Test that a DIE has no child DIE.
6609 ///
6610 /// @param die the DIE to consider.
6611 ///
6612 /// @return true iff @p die has no child DIE.
6613 static bool
6614 die_has_no_child(const Dwarf_Die *die)
6615 {
6616  if (!die)
6617  return true;
6618 
6619  Dwarf_Die child;
6620  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6621  return false;
6622  return true;
6623 }
6624 
6625 /// Test whether a given DIE represents a declaration-only DIE.
6626 ///
6627 /// That is, if the DIE has the DW_AT_declaration flag set.
6628 ///
6629 /// @param die the DIE to consider.
6630 //
6631 /// @return true if a DW_AT_declaration is present, false otherwise.
6632 static bool
6633 die_is_declaration_only(const Dwarf_Die* die)
6634 {return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6635 
6636 /// Tests whether a given DIE is artificial.
6637 ///
6638 /// @param die the test to test for.
6639 ///
6640 /// @return true if the DIE is artificial, false otherwise.
6641 static bool
6642 die_is_artificial(Dwarf_Die* die)
6643 {
6644  bool is_artificial;
6645  return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6646 }
6647 
6648 ///@return true if a tag represents a type, false otherwise.
6649 ///
6650 ///@param tag the tag to consider.
6651 static bool
6652 is_type_tag(unsigned tag)
6653 {
6654  bool result = false;
6655 
6656  switch (tag)
6657  {
6658  case DW_TAG_array_type:
6659  case DW_TAG_class_type:
6660  case DW_TAG_enumeration_type:
6661  case DW_TAG_pointer_type:
6662  case DW_TAG_reference_type:
6663  case DW_TAG_string_type:
6664  case DW_TAG_structure_type:
6665  case DW_TAG_subroutine_type:
6666  case DW_TAG_typedef:
6667  case DW_TAG_union_type:
6668  case DW_TAG_ptr_to_member_type:
6669  case DW_TAG_set_type:
6670  case DW_TAG_subrange_type:
6671  case DW_TAG_base_type:
6672  case DW_TAG_const_type:
6673  case DW_TAG_file_type:
6674  case DW_TAG_packed_type:
6675  case DW_TAG_thrown_type:
6676  case DW_TAG_volatile_type:
6677  case DW_TAG_restrict_type:
6678  case DW_TAG_interface_type:
6679  case DW_TAG_unspecified_type:
6680  case DW_TAG_shared_type:
6681  case DW_TAG_rvalue_reference_type:
6682  case DW_TAG_coarray_type:
6683  case DW_TAG_atomic_type:
6684  case DW_TAG_immutable_type:
6685  result = true;
6686  break;
6687 
6688  default:
6689  result = false;
6690  break;
6691  }
6692 
6693  return result;
6694 }
6695 
6696 /// Test if a given DIE is a type whose canonical type is to be
6697 /// propagated during DIE canonicalization
6698 ///
6699 /// This is a sub-routine of compare_dies.
6700 ///
6701 /// @param tag the tag of the DIE to consider.
6702 ///
6703 /// @return true iff the DIE of tag @p tag is can see its canonical
6704 /// type be propagated during the type comparison that happens during
6705 /// DIE canonicalization.
6706 static bool
6707 is_canon_type_to_be_propagated_tag(unsigned tag)
6708 {
6709  bool result = false;
6710 
6711  switch (tag)
6712  {
6713  case DW_TAG_class_type:
6714  case DW_TAG_structure_type:
6715  case DW_TAG_union_type:
6716  case DW_TAG_subroutine_type:
6717  case DW_TAG_subprogram:
6718  result = true;
6719  break;
6720 
6721  default:
6722  result = false;
6723  break;
6724  }
6725 
6726  return result;
6727 }
6728 
6729 /// Test if a given kind of DIE ought to have its comparison result
6730 /// cached by compare_dies, so that subsequent invocations of
6731 /// compare_dies can be faster.
6732 ///
6733 /// @param tag the tag of the DIE to consider.
6734 ///
6735 /// @return true iff DIEs of the tag @p tag ought to have its
6736 /// comparison results cached.
6737 static bool
6738 type_comparison_result_to_be_cached(unsigned tag)
6739 {
6740  bool r = false;
6741  switch (tag)
6742  {
6743  case DW_TAG_class_type:
6744  case DW_TAG_structure_type:
6745  case DW_TAG_union_type:
6746  case DW_TAG_subroutine_type:
6747  case DW_TAG_subprogram:
6748  r = true;
6749  break;
6750 
6751  default:
6752  r = false;
6753  break;
6754  }
6755  return r;
6756 }
6757 
6758 /// Cache the result of comparing to type DIEs.
6759 ///
6760 /// @param rdr the context to consider.
6761 ///
6762 /// @param tag the tag of the DIEs to consider.
6763 ///
6764 /// @param p the offsets of the pair of DIEs being compared.
6765 ///
6766 /// @param result the comparison result to be cached.
6767 static bool
6768 maybe_cache_type_comparison_result(const reader& rdr,
6769  int tag,
6770  const offset_pair_type& p,
6771  comparison_result result)
6772 {
6773  if (!type_comparison_result_to_be_cached(tag)
6774  || (result != COMPARISON_RESULT_EQUAL
6775  && result != COMPARISON_RESULT_DIFFERENT))
6776  return false;
6777 
6778  rdr.die_comparison_results_[p] = result;
6779 
6780  return true;
6781 
6782 }
6783 
6784 /// Get the cached result of the comparison of a pair of DIEs.
6785 ///
6786 /// @param rdr the context to consider.
6787 ///
6788 /// @param tag the tag of the pair of DIEs to consider.
6789 ///
6790 /// @param p the offsets of the pair of DIEs to consider.
6791 ///
6792 /// @param result out parameter set to the cached result of the
6793 /// comparison of @p p if it has been found.
6794 ///
6795 /// @return true iff a cached result for the comparisonof @p has been
6796 /// found and set into @p result.
6797 static bool
6798 get_cached_type_comparison_result(const reader& rdr,
6799  const offset_pair_type& p,
6800  comparison_result& result)
6801 {
6802  auto i = rdr.die_comparison_results_.find(p);
6803  if (i != rdr.die_comparison_results_.end())
6804  {
6805  result = i->second;
6806  return true;
6807  }
6808  return false;
6809 }
6810 
6811 /// Get the cached result of the comparison of a pair of DIEs, if the
6812 /// kind of DIEs ought to have its comparison results cached.
6813 ///
6814 /// @param rdr the context to consider.
6815 ///
6816 /// @param tag the tag of the pair of DIEs to consider.
6817 ///
6818 /// @param p the offsets of the pair of DIEs to consider.
6819 ///
6820 /// @param result out parameter set to the cached result of the
6821 /// comparison of @p p if it has been found.
6822 ///
6823 /// @return true iff a cached result for the comparisonof @p has been
6824 /// found and set into @p result.
6825 static bool
6826 maybe_get_cached_type_comparison_result(const reader& rdr,
6827  int tag,
6828  const offset_pair_type& p,
6829  comparison_result& result)
6830 {
6831  if (type_comparison_result_to_be_cached(tag))
6832  {
6833  // Types of this kind might have their comparison result cached
6834  // when they are not canonicalized. So let's see if we have a
6835  // cached comparison result.
6836  if (get_cached_type_comparison_result(rdr, p, result))
6837  return true;
6838  }
6839  return false;
6840 }
6841 
6842 /// Test if a given DIE is to be canonicalized.
6843 ///
6844 /// @param die the DIE to consider.
6845 ///
6846 /// @return true iff @p die is to be canonicalized.
6847 static bool
6848 is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6849 {
6850  bool result = false;
6851  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6852 
6853  if (!is_type_tag(tag))
6854  return false;
6855 
6856  switch (tag)
6857  {
6858  case DW_TAG_class_type:
6859  case DW_TAG_structure_type:
6860  case DW_TAG_union_type:
6861  result = !die_is_declaration_only(die);
6862  break;
6863 
6864  case DW_TAG_subroutine_type:
6865  case DW_TAG_subprogram:
6866  case DW_TAG_array_type:
6867  result = true;
6868 
6869  default:
6870  break;
6871  }
6872 
6873  return result;
6874 }
6875 
6876 /// Test if a DIE tag represents a declaration.
6877 ///
6878 /// @param tag the DWARF tag to consider.
6879 ///
6880 /// @return true iff @p tag is for a declaration.
6881 static bool
6882 is_decl_tag(unsigned tag)
6883 {
6884  switch (tag)
6885  {
6886  case DW_TAG_formal_parameter:
6887  case DW_TAG_imported_declaration:
6888  case DW_TAG_member:
6889  case DW_TAG_unspecified_parameters:
6890  case DW_TAG_subprogram:
6891  case DW_TAG_variable:
6892  case DW_TAG_namespace:
6893  case DW_TAG_GNU_template_template_param:
6894  case DW_TAG_GNU_template_parameter_pack:
6895  case DW_TAG_GNU_formal_parameter_pack:
6896  return true;
6897  }
6898  return false;
6899 }
6900 
6901 /// Test if a DIE represents a type DIE.
6902 ///
6903 /// @param die the DIE to consider.
6904 ///
6905 /// @return true if @p die represents a type, false otherwise.
6906 static bool
6907 die_is_type(const Dwarf_Die* die)
6908 {
6909  if (!die)
6910  return false;
6911  return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6912 }
6913 
6914 /// Test if a DIE represents a declaration.
6915 ///
6916 /// @param die the DIE to consider.
6917 ///
6918 /// @return true if @p die represents a decl, false otherwise.
6919 static bool
6920 die_is_decl(const Dwarf_Die* die)
6921 {
6922  if (!die)
6923  return false;
6924  return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6925 }
6926 
6927 /// Test if a DIE represents a namespace.
6928 ///
6929 /// @param die the DIE to consider.
6930 ///
6931 /// @return true if @p die represents a namespace, false otherwise.
6932 static bool
6933 die_is_namespace(const Dwarf_Die* die)
6934 {
6935  if (!die)
6936  return false;
6937  return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6938 }
6939 
6940 /// Test if a DIE has tag DW_TAG_unspecified_type.
6941 ///
6942 /// @param die the DIE to consider.
6943 ///
6944 /// @return true if @p die has tag DW_TAG_unspecified_type.
6945 static bool
6946 die_is_unspecified(Dwarf_Die* die)
6947 {
6948  if (!die)
6949  return false;
6950  return (dwarf_tag(die) == DW_TAG_unspecified_type);
6951 }
6952 
6953 /// Test if a DIE represents a void type.
6954 ///
6955 /// @param die the DIE to consider.
6956 ///
6957 /// @return true if @p die represents a void type, false otherwise.
6958 static bool
6959 die_is_void_type(Dwarf_Die* die)
6960 {
6961  if (!die || dwarf_tag(die) != DW_TAG_base_type)
6962  return false;
6963 
6964  string name = die_name(die);
6965  if (name == "void")
6966  return true;
6967 
6968  return false;
6969 }
6970 
6971 /// Test if a DIE represents a pointer type.
6972 ///
6973 /// @param die the die to consider.
6974 ///
6975 /// @return true iff @p die represents a pointer type.
6976 static bool
6977 die_is_pointer_type(const Dwarf_Die* die)
6978 {
6979  if (!die)
6980  return false;
6981 
6982  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6983  if (tag == DW_TAG_pointer_type)
6984  return true;
6985 
6986  return false;
6987 }
6988 
6989 /// Test if a DIE is for a pointer, reference or qualified type to
6990 /// anonymous class or struct.
6991 ///
6992 /// @param die the DIE to consider.
6993 ///
6994 /// @return true iff @p is for a pointer, reference or qualified type
6995 /// to anonymous class or struct.
6996 static bool
6997 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
6998 {
6999  if (!die_is_pointer_array_or_reference_type(die)
7000  && !die_is_qualified_type(die))
7001  return false;
7002 
7003  Dwarf_Die underlying_type_die;
7004  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7005  return false;
7006 
7007  if (!die_is_class_type(&underlying_type_die))
7008  return false;
7009 
7010  string name = die_name(&underlying_type_die);
7011 
7012  return name.empty();
7013 }
7014 
7015 /// Test if a DIE represents a reference type.
7016 ///
7017 /// @param die the die to consider.
7018 ///
7019 /// @return true iff @p die represents a reference type.
7020 static bool
7021 die_is_reference_type(const Dwarf_Die* die)
7022 {
7023  if (!die)
7024  return false;
7025 
7026  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7027  if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7028  return true;
7029 
7030  return false;
7031 }
7032 
7033 /// Test if a DIE represents an array type.
7034 ///
7035 /// @param die the die to consider.
7036 ///
7037 /// @return true iff @p die represents an array type.
7038 static bool
7039 die_is_array_type(const Dwarf_Die* die)
7040 {
7041  if (!die)
7042  return false;
7043 
7044  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7045  if (tag == DW_TAG_array_type)
7046  return true;
7047 
7048  return false;
7049 }
7050 
7051 /// Test if a DIE represents a pointer, reference or array type.
7052 ///
7053 /// @param die the die to consider.
7054 ///
7055 /// @return true iff @p die represents a pointer or reference type.
7056 static bool
7057 die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7058 {return (die_is_pointer_type(die)
7059  || die_is_reference_type(die)
7060  || die_is_array_type(die));}
7061 
7062 /// Test if a DIE represents a pointer or a reference type.
7063 ///
7064 /// @param die the die to consider.
7065 ///
7066 /// @return true iff @p die represents a pointer or reference type.
7067 static bool
7068 die_is_pointer_or_reference_type(const Dwarf_Die* die)
7069 {return (die_is_pointer_type(die) || die_is_reference_type(die));}
7070 
7071 /// Test if a DIE represents a pointer, a reference or a typedef type.
7072 ///
7073 /// @param die the die to consider.
7074 ///
7075 /// @return true iff @p die represents a pointer, a reference or a
7076 /// typedef type.
7077 static bool
7078 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7079 {return (die_is_pointer_array_or_reference_type(die)
7080  || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7081 
7082 /// Test if a DIE represents a class type.
7083 ///
7084 /// @param die the die to consider.
7085 ///
7086 /// @return true iff @p die represents a class type.
7087 static bool
7088 die_is_class_type(const Dwarf_Die* die)
7089 {
7090  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7091 
7092  if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7093  return true;
7094 
7095  return false;
7096 }
7097 
7098 /// Test if a DIE is for a qualified type.
7099 ///
7100 /// @param die the DIE to consider.
7101 ///
7102 /// @return true iff @p die is for a qualified type.
7103 static bool
7104 die_is_qualified_type(const Dwarf_Die* die)
7105 {
7106  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7107  if (tag == DW_TAG_const_type
7108  || tag == DW_TAG_volatile_type
7109  || tag == DW_TAG_restrict_type)
7110  return true;
7111 
7112  return false;
7113 }
7114 
7115 /// Test if a DIE is for a function type.
7116 ///
7117 /// @param die the DIE to consider.
7118 ///
7119 /// @return true iff @p die is for a function type.
7120 static bool
7121 die_is_function_type(const Dwarf_Die *die)
7122 {
7123  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7124  if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7125  return true;
7126 
7127  return false;
7128 }
7129 
7130 /// Test if a DIE for a function pointer or member function has an
7131 /// DW_AT_object_pointer attribute.
7132 ///
7133 /// @param die the DIE to consider.
7134 ///
7135 /// @param object_pointer out parameter. It's set to the DIE for the
7136 /// object pointer iff the function returns true.
7137 ///
7138 /// @return true iff the DIE @p die has an object pointer. In that
7139 /// case, the parameter @p object_pointer is set to the DIE of that
7140 /// object pointer.
7141 static bool
7142 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7143 {
7144  if (!die)
7145  return false;
7146 
7147  if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7148  return true;
7149 
7150  return false;
7151 }
7152 
7153 /// Test if a DIE has children DIEs.
7154 ///
7155 /// @param die the DIE to consider.
7156 ///
7157 /// @return true iff @p DIE has at least one child node.
7158 static bool
7159 die_has_children(const Dwarf_Die* die)
7160 {
7161  if (!die)
7162  return false;
7163 
7164  Dwarf_Die child;
7165  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7166  return true;
7167 
7168  return false;
7169 }
7170 
7171 /// When given the object pointer DIE of a function type or member
7172 /// function DIE, this function returns the "this" pointer that points
7173 /// to the associated class.
7174 ///
7175 /// @param die the DIE of the object pointer of the function or member
7176 /// function to consider.
7177 ///
7178 /// @param this_pointer_die out parameter. This is set to the DIE of
7179 /// the "this" pointer iff the function returns true.
7180 ///
7181 /// @return true iff the function found the "this" pointer from the
7182 /// object pointer DIE @p die. In that case, the parameter @p
7183 /// this_pointer_die is set to the DIE of that "this" pointer.
7184 static bool
7185 die_this_pointer_from_object_pointer(Dwarf_Die* die,
7186  Dwarf_Die& this_pointer_die)
7187 {
7188  ABG_ASSERT(die);
7189  ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7190 
7191  if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7192  return true;
7193 
7194  return false;
7195 }
7196 
7197 /// Test if a given "this" pointer that points to a particular class
7198 /// type is for a const class or not. If it's for a const class, then
7199 /// it means the function type or the member function associated to
7200 /// that "this" pointer is const.
7201 ///
7202 /// @param die the DIE of the "this" pointer to consider.
7203 ///
7204 /// @return true iff @p die points to a const class type.
7205 static bool
7206 die_this_pointer_is_const(Dwarf_Die* die)
7207 {
7208  ABG_ASSERT(die);
7209 
7210  if (dwarf_tag(die) == DW_TAG_pointer_type)
7211  {
7212  Dwarf_Die pointed_to_type_die;
7213  if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7214  if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7215  return true;
7216  }
7217 
7218  return false;
7219 }
7220 
7221 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
7222 /// attribute) points to a const implicit class and so is for a const
7223 /// method or or a const member function type.
7224 ///
7225 /// @param die the DIE of the object pointer to consider.
7226 ///
7227 /// @return true iff the object pointer represented by @p die is for a
7228 /// a const method or const member function type.
7229 static bool
7230 die_object_pointer_is_for_const_method(Dwarf_Die* die)
7231 {
7232  ABG_ASSERT(die);
7233  ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7234 
7235  Dwarf_Die this_pointer_die;
7236  if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7237  if (die_this_pointer_is_const(&this_pointer_die))
7238  return true;
7239 
7240  return false;
7241 }
7242 
7243 /// Test if a DIE represents an entity that is at class scope.
7244 ///
7245 /// @param rdr the DWARF reader to use.
7246 ///
7247 /// @param die the DIE to consider.
7248 ///
7249 /// @param where_offset where we are logically at in the DIE stream.
7250 ///
7251 /// @param class_scope_die out parameter. Set to the DIE of the
7252 /// containing class iff @p die happens to be at class scope; that is,
7253 /// iff the function returns true.
7254 ///
7255 /// @return true iff @p die is at class scope. In that case, @p
7256 /// class_scope_die is set to the DIE of the class that contains @p
7257 /// die.
7258 static bool
7259 die_is_at_class_scope(const reader& rdr,
7260  const Dwarf_Die* die,
7261  size_t where_offset,
7262  Dwarf_Die& class_scope_die)
7263 {
7264  if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7265  return false;
7266 
7267  int tag = dwarf_tag(&class_scope_die);
7268 
7269  return (tag == DW_TAG_structure_type
7270  || tag == DW_TAG_class_type
7271  || tag == DW_TAG_union_type);
7272 }
7273 
7274 /// Return the leaf object under a pointer, reference or qualified
7275 /// type DIE.
7276 ///
7277 /// @param die the DIE of the type to consider.
7278 ///
7279 /// @param peeled_die out parameter. Set to the DIE of the leaf
7280 /// object iff the function actually peeled anything.
7281 ///
7282 /// @return true upon successful completion.
7283 static bool
7284 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7285 {
7286  if (!die)
7287  return false;
7288 
7289  int tag = dwarf_tag(die);
7290 
7291  if (tag == DW_TAG_const_type
7292  || tag == DW_TAG_volatile_type
7293  || tag == DW_TAG_restrict_type
7294  || tag == DW_TAG_pointer_type
7295  || tag == DW_TAG_reference_type
7296  || tag == DW_TAG_rvalue_reference_type)
7297  {
7298  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7299  return false;
7300  }
7301  else
7302  return false;
7303 
7304  memcpy(&peeled_die, die, sizeof(peeled_die));
7305 
7306  while (tag == DW_TAG_const_type
7307  || tag == DW_TAG_volatile_type
7308  || tag == DW_TAG_restrict_type
7309  || tag == DW_TAG_pointer_type
7310  || tag == DW_TAG_reference_type
7311  || tag == DW_TAG_rvalue_reference_type)
7312  {
7313  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7314  break;
7315  tag = dwarf_tag(&peeled_die);
7316  }
7317 
7318  return true;
7319 }
7320 
7321 /// Return the leaf object under a qualified type DIE.
7322 ///
7323 /// @param die the DIE of the type to consider.
7324 ///
7325 /// @param peeled_die out parameter. Set to the DIE of the leaf
7326 /// object iff the function actually peeled anything.
7327 ///
7328 /// @return true upon successful completion.
7329 static bool
7330 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7331 {
7332  if (!die)
7333  return false;
7334 
7335  memcpy(&peeled_die, die, sizeof(peeled_die));
7336 
7337  int tag = dwarf_tag(&peeled_die);
7338 
7339  bool result = false;
7340  while (tag == DW_TAG_const_type
7341  || tag == DW_TAG_volatile_type
7342  || tag == DW_TAG_restrict_type)
7343  {
7344  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7345  break;
7346  tag = dwarf_tag(&peeled_die);
7347  result = true;
7348  }
7349 
7350  return result;
7351 }
7352 
7353 /// Return the leaf object under a typedef type DIE.
7354 ///
7355 /// @param die the DIE of the type to consider.
7356 ///
7357 /// @param peeled_die out parameter. Set to the DIE of the leaf
7358 /// object iff the function actually peeled anything.
7359 ///
7360 /// @return true upon successful completion.
7361 static bool
7362 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7363 {
7364  if (!die)
7365  return false;
7366 
7367  int tag = dwarf_tag(die);
7368 
7369  memcpy(&peeled_die, die, sizeof(peeled_die));
7370 
7371  if (tag == DW_TAG_typedef)
7372  {
7373  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7374  return false;
7375  }
7376  else
7377  return false;
7378 
7379  while (tag == DW_TAG_typedef)
7380  {
7381  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7382  break;
7383  tag = dwarf_tag(&peeled_die);
7384  }
7385 
7386  return true;
7387 
7388 }
7389 
7390 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7391 ///
7392 /// @param die the DIE to consider.
7393 ///
7394 /// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7395 /// iff the function returned true.
7396 ///
7397 /// @return true iff the function could peel @p die.
7398 static bool
7399 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7400 {
7401  if (!die)
7402  return false;
7403 
7404  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7405 
7406  if (tag == DW_TAG_pointer_type
7407  || tag == DW_TAG_reference_type
7408  || tag == DW_TAG_rvalue_reference_type
7409  || tag == DW_TAG_typedef)
7410  {
7411  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7412  return false;
7413  }
7414  else
7415  return false;
7416 
7417  while (tag == DW_TAG_pointer_type
7418  || tag == DW_TAG_reference_type
7419  || tag == DW_TAG_rvalue_reference_type
7420  || tag == DW_TAG_typedef)
7421  {
7422  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7423  break;
7424  tag = dwarf_tag(&peeled_die);
7425  }
7426  return true;
7427 }
7428 
7429 /// Test if a DIE for a function type represents a method type.
7430 ///
7431 /// @param rdr the DWARF reader.
7432 ///
7433 /// @param die the DIE to consider.
7434 ///
7435 /// @param where_offset where we logically are in the stream of DIEs.
7436 ///
7437 /// @param object_pointer_die out parameter. This is set by the
7438 /// function to the DIE that refers to the formal function parameter
7439 /// which holds the implicit "this" pointer of the method. That die
7440 /// is called the object pointer DIE. This is set iff the function
7441 ///
7442 /// @param class_die out parameter. This is set by the function to
7443 /// the DIE that represents the class of the method type. This is set
7444 /// iff the function returns true.
7445 ///
7446 /// @param is_static out parameter. This is set to true by the
7447 /// function if @p die is a static method. This is set iff the
7448 /// function returns true.
7449 ///
7450 /// @return true iff @p die is a DIE for a method type.
7451 static bool
7452 die_function_type_is_method_type(const reader& rdr,
7453  const Dwarf_Die *die,
7454  size_t where_offset,
7455  Dwarf_Die& object_pointer_die,
7456  Dwarf_Die& class_die,
7457  bool& is_static)
7458 {
7459  if (!die)
7460  return false;
7461 
7462  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7463  ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7464 
7465  bool has_object_pointer = false;
7466  is_static = false;
7467  if (tag == DW_TAG_subprogram)
7468  {
7469  Dwarf_Die spec_or_origin_die;
7470  if (die_die_attribute(die, DW_AT_specification,
7471  spec_or_origin_die)
7472  || die_die_attribute(die, DW_AT_abstract_origin,
7473  spec_or_origin_die))
7474  {
7475  if (die_has_object_pointer(&spec_or_origin_die,
7476  object_pointer_die))
7477  has_object_pointer = true;
7478  else
7479  {
7480  if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7481  where_offset, class_die))
7482  is_static = true;
7483  else
7484  return false;
7485  }
7486  }
7487  else
7488  {
7489  if (die_has_object_pointer(die, object_pointer_die))
7490  has_object_pointer = true;
7491  else
7492  {
7493  if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7494  is_static = true;
7495  else
7496  return false;
7497  }
7498  }
7499  }
7500  else
7501  {
7502  if (die_has_object_pointer(die, object_pointer_die))
7503  has_object_pointer = true;
7504  else
7505  return false;
7506  }
7507 
7508  if (!is_static)
7509  {
7510  ABG_ASSERT(has_object_pointer);
7511  // The object pointer die points to a DW_TAG_formal_parameter which
7512  // is the "this" parameter. The type of the "this" parameter is a
7513  // pointer. Let's get that pointer type.
7514  Dwarf_Die this_type_die;
7515  if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7516  return false;
7517 
7518  // So the class type is the type pointed to by the type of the "this"
7519  // parameter.
7520  if (!die_peel_qual_ptr(&this_type_die, class_die))
7521  return false;
7522 
7523  // And make we return a class type, rather than a typedef to a
7524  // class.
7525  die_peel_typedef(&class_die, class_die);
7526  }
7527 
7528  return true;
7529 }
7530 
7531 enum virtuality
7532 {
7533  VIRTUALITY_NOT_VIRTUAL,
7534  VIRTUALITY_VIRTUAL,
7535  VIRTUALITY_PURE_VIRTUAL
7536 };
7537 
7538 /// Get the virtual-ness of a given DIE, that is, the value of the
7539 /// DW_AT_virtuality attribute.
7540 ///
7541 /// @param die the DIE to read from.
7542 ///
7543 /// @param virt the resulting virtuality attribute. This is set iff
7544 /// the function returns true.
7545 ///
7546 /// @return true if the virtual-ness could be determined.
7547 static bool
7548 die_virtuality(const Dwarf_Die* die, virtuality& virt)
7549 {
7550  if (!die)
7551  return false;
7552 
7553  uint64_t v = 0;
7554  die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7555 
7556  if (v == DW_VIRTUALITY_virtual)
7557  virt = VIRTUALITY_VIRTUAL;
7558  else if (v == DW_VIRTUALITY_pure_virtual)
7559  virt = VIRTUALITY_PURE_VIRTUAL;
7560  else
7561  virt = VIRTUALITY_NOT_VIRTUAL;
7562 
7563  return true;
7564 }
7565 
7566 /// Test whether the DIE represent either a virtual base or function.
7567 ///
7568 /// @param die the DIE to consider.
7569 ///
7570 /// @return bool if the DIE represents a virtual base or function,
7571 /// false othersise.
7572 static bool
7573 die_is_virtual(const Dwarf_Die* die)
7574 {
7575  virtuality v;
7576  if (!die_virtuality(die, v))
7577  return false;
7578 
7579  return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7580 }
7581 
7582 /// Test if the DIE represents an entity that was declared inlined.
7583 ///
7584 /// @param die the DIE to test for.
7585 ///
7586 /// @return true if the DIE represents an entity that was declared
7587 /// inlined.
7588 static bool
7589 die_is_declared_inline(Dwarf_Die* die)
7590 {
7591  uint64_t inline_value = 0;
7592  if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7593  return false;
7594  return inline_value == DW_INL_declared_inlined;
7595 }
7596 
7597 /// Compare two DWARF strings using the most accurate (and slowest)
7598 /// method possible.
7599 ///
7600 /// @param l the DIE that carries the first string to consider, as an
7601 /// attribute value.
7602 ///
7603 /// @param attr_name the name of the attribute which value is the
7604 /// string to compare.
7605 ///
7606 /// @return true iff the string carried by @p l equals the one carried
7607 /// by @p r.
7608 static bool
7609 slowly_compare_strings(const Dwarf_Die *l,
7610  const Dwarf_Die *r,
7611  unsigned attr_name)
7612 {
7613  const char *l_str = die_char_str_attribute(l, attr_name),
7614  *r_str = die_char_str_attribute(r, attr_name);
7615  if (!l_str && !r_str)
7616  return true;
7617  return l_str && r_str && !strcmp(l_str, r_str);
7618 }
7619 
7620 /// This function is a fast routine (optimization) to compare the
7621 /// values of two string attributes of two DIEs.
7622 ///
7623 /// @param l the first DIE to consider.
7624 ///
7625 /// @param r the second DIE to consider.
7626 ///
7627 /// @param attr_name the name of the attribute to compare, on the two
7628 /// DIEs above.
7629 ///
7630 /// @param result out parameter. This is set to the result of the
7631 /// comparison. If the value of attribute @p attr_name on DIE @p l
7632 /// equals the value of attribute @p attr_name on DIE @p r, then the
7633 /// the argument of this parameter is set to true. Otherwise, it's
7634 /// set to false. Note that the argument of this parameter is set iff
7635 /// the function returned true.
7636 ///
7637 /// @return true iff the comparison could be performed. There are
7638 /// cases in which the comparison cannot be performed. For instance,
7639 /// if one of the DIEs does not have the attribute @p attr_name. In
7640 /// any case, if this function returns true, then the parameter @p
7641 /// result is set to the result of the comparison.
7642 static bool
7643 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7644  unsigned attr_name,
7645  bool &result)
7646 {
7647  Dwarf_Attribute l_attr, r_attr;
7648  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7649  || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7650  return false;
7651 
7652  ABG_ASSERT(l_attr.form == DW_FORM_strp
7653  || l_attr.form == DW_FORM_string
7654  || l_attr.form == DW_FORM_GNU_strp_alt
7655  || form_is_DW_FORM_strx(l_attr.form)
7656  || form_is_DW_FORM_line_strp(l_attr.form));
7657 
7658  ABG_ASSERT(r_attr.form == DW_FORM_strp
7659  || r_attr.form == DW_FORM_string
7660  || r_attr.form == DW_FORM_GNU_strp_alt
7661  || form_is_DW_FORM_strx(r_attr.form)
7662  || form_is_DW_FORM_line_strp(r_attr.form));
7663 
7664  if ((l_attr.form == DW_FORM_strp
7665  && r_attr.form == DW_FORM_strp)
7666  || (l_attr.form == DW_FORM_GNU_strp_alt
7667  && r_attr.form == DW_FORM_GNU_strp_alt)
7668  || (form_is_DW_FORM_strx(l_attr.form)
7669  && form_is_DW_FORM_strx(r_attr.form))
7670  || (form_is_DW_FORM_line_strp(l_attr.form)
7671  && form_is_DW_FORM_line_strp(r_attr.form)))
7672  {
7673  // So these string attributes are actually pointers into a
7674  // string table. The string table is most likely de-duplicated
7675  // so comparing the *values* of the pointers should be enough.
7676  //
7677  // This is the fast path.
7678  if (l_attr.valp == r_attr.valp)
7679  {
7680 #if WITH_DEBUG_TYPE_CANONICALIZATION
7681  ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7682 #endif
7683  result = true;
7684  return true;
7685  }
7686  }
7687 
7688  // If we reached this point it means we couldn't use the fast path
7689  // because the string atttributes are strings that are "inline" in
7690  // the debug info section. Let's just compare them the slow and
7691  // obvious way.
7692  result = slowly_compare_strings(l, r, attr_name);
7693  return true;
7694 }
7695 
7696 /// Compare the file path of the compilation units (aka CUs)
7697 /// associated to two DIEs.
7698 ///
7699 /// If the DIEs are for pointers or typedefs, this function also
7700 /// compares the file paths of the CUs of the leaf DIEs (underlying
7701 /// DIEs of the pointer or the typedef).
7702 ///
7703 /// @param l the first type DIE to consider.
7704 ///
7705 /// @param r the second type DIE to consider.
7706 ///
7707 /// @return true iff the file paths of the DIEs of the two types are
7708 /// equal.
7709 static bool
7710 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7711 {
7712  Dwarf_Die l_cu, r_cu;
7713  if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7714  ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7715  return false;
7716 
7717  bool compared =
7718  compare_dies_string_attribute_value(&l_cu, &r_cu,
7719  DW_AT_name,
7720  result);
7721  if (compared && result)
7722  {
7723  Dwarf_Die peeled_l, peeled_r;
7724  if (die_is_pointer_reference_or_typedef_type(l)
7725  && die_is_pointer_reference_or_typedef_type(r)
7726  && die_peel_pointer_and_typedef(l, peeled_l)
7727  && die_peel_pointer_and_typedef(r, peeled_r))
7728  {
7729  if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7730  ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7731  return false;
7732  compared =
7733  compare_dies_string_attribute_value(&l_cu, &r_cu,
7734  DW_AT_name,
7735  result);
7736  }
7737  }
7738 
7739  return compared;
7740 }
7741 
7742 // -----------------------------------
7743 // <location expression evaluation>
7744 // -----------------------------------
7745 
7746 /// Get the value of a given DIE attribute, knowing that it must be a
7747 /// location expression.
7748 ///
7749 /// @param die the DIE to read the attribute from.
7750 ///
7751 /// @param attr_name the name of the attribute to read the value for.
7752 ///
7753 /// @param expr the pointer to allocate and fill with the resulting
7754 /// array of operators + operands forming a dwarf expression. This is
7755 /// set iff the function returns true.
7756 ///
7757 /// @param expr_len the length of the resulting dwarf expression.
7758 /// This is set iff the function returns true.
7759 ///
7760 /// @return true if the attribute exists and has a non-empty dwarf expression
7761 /// as value. In that case the expr and expr_len arguments are set to the
7762 /// resulting dwarf expression.
7763 static bool
7764 die_location_expr(const Dwarf_Die* die,
7765  unsigned attr_name,
7766  Dwarf_Op** expr,
7767  size_t* expr_len)
7768 {
7769  if (!die)
7770  return false;
7771 
7772  Dwarf_Attribute attr;
7773  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7774  return false;
7775 
7776  size_t len = 0;
7777  bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7778 
7779  // Ignore location expressions where reading them succeeded but
7780  // their length is 0.
7781  result &= len > 0;
7782 
7783  if (result)
7784  *expr_len = len;
7785 
7786  return result;
7787 }
7788 
7789 /// If the current operation in the dwarf expression represents a push
7790 /// of a constant value onto the dwarf expr virtual machine (aka
7791 /// DEVM), perform the operation and update the DEVM.
7792 ///
7793 /// If the result of the operation is a constant, update the DEVM
7794 /// accumulator with its value. Otherwise, the DEVM accumulator is
7795 /// left with its previous value.
7796 ///
7797 /// @param ops the array of the dwarf expression operations to consider.
7798 ///
7799 /// @param ops_len the lengths of @p ops array above.
7800 ///
7801 /// @param index the index of the operation to interpret, in @p ops.
7802 ///
7803 /// @param next_index the index of the operation to interpret at the
7804 /// next step, after this function completed and returned. This is
7805 /// set an output parameter that is set iff the function returns true.
7806 ///
7807 /// @param ctxt the DEVM evaluation context.
7808 ///
7809 /// @return true if the current operation actually pushes a constant
7810 /// value onto the DEVM stack, false otherwise.
7811 static bool
7812 op_pushes_constant_value(Dwarf_Op* ops,
7813  size_t ops_len,
7814  size_t index,
7815  size_t& next_index,
7816  dwarf_expr_eval_context& ctxt)
7817 {
7818  ABG_ASSERT(index < ops_len);
7819 
7820  Dwarf_Op& op = ops[index];
7821  int64_t value = 0;
7822 
7823  switch (op.atom)
7824  {
7825  case DW_OP_addr:
7826  value = ops[index].number;
7827  break;
7828 
7829  case DW_OP_const1u:
7830  case DW_OP_const1s:
7831  case DW_OP_const2u:
7832  case DW_OP_const2s:
7833  case DW_OP_const4u:
7834  case DW_OP_const4s:
7835  case DW_OP_const8u:
7836  case DW_OP_const8s:
7837  case DW_OP_constu:
7838  case DW_OP_consts:
7839  value = ops[index].number;
7840  break;
7841 
7842  case DW_OP_lit0:
7843  value = 0;
7844  break;
7845  case DW_OP_lit1:
7846  value = 1;
7847  break;
7848  case DW_OP_lit2:
7849  value = 2;
7850  break;
7851  case DW_OP_lit3:
7852  value = 3;
7853  break;
7854  case DW_OP_lit4:
7855  value = 4;
7856  break;
7857  case DW_OP_lit5:
7858  value = 5;
7859  break;
7860  case DW_OP_lit6:
7861  value = 6;
7862  break;
7863  case DW_OP_lit7:
7864  value = 7;
7865  break;
7866  case DW_OP_lit8:
7867  value = 8;
7868  break;
7869  case DW_OP_lit9:
7870  value = 9;
7871  break;
7872  case DW_OP_lit10:
7873  value = 10;
7874  break;
7875  case DW_OP_lit11:
7876  value = 11;
7877  break;
7878  case DW_OP_lit12:
7879  value = 12;
7880  break;
7881  case DW_OP_lit13:
7882  value = 13;
7883  break;
7884  case DW_OP_lit14:
7885  value = 14;
7886  break;
7887  case DW_OP_lit15:
7888  value = 15;
7889  break;
7890  case DW_OP_lit16:
7891  value = 16;
7892  break;
7893  case DW_OP_lit17:
7894  value = 17;
7895  break;
7896  case DW_OP_lit18:
7897  value = 18;
7898  break;
7899  case DW_OP_lit19:
7900  value = 19;
7901  break;
7902  case DW_OP_lit20:
7903  value = 20;
7904  break;
7905  case DW_OP_lit21:
7906  value = 21;
7907  break;
7908  case DW_OP_lit22:
7909  value = 22;
7910  break;
7911  case DW_OP_lit23:
7912  value = 23;
7913  break;
7914  case DW_OP_lit24:
7915  value = 24;
7916  break;
7917  case DW_OP_lit25:
7918  value = 25;
7919  break;
7920  case DW_OP_lit26:
7921  value = 26;
7922  break;
7923  case DW_OP_lit27:
7924  value = 27;
7925  break;
7926  case DW_OP_lit28:
7927  value = 28;
7928  break;
7929  case DW_OP_lit29:
7930  value = 29;
7931  break;
7932  case DW_OP_lit30:
7933  value = 30;
7934  break;
7935  case DW_OP_lit31:
7936  value = 31;
7937  break;
7938 
7939  default:
7940  return false;
7941  }
7942 
7943  expr_result r(value);
7944  ctxt.push(r);
7945  ctxt.accum = r;
7946  next_index = index + 1;
7947 
7948  return true;
7949 }
7950 
7951 /// If the current operation in the dwarf expression represents a push
7952 /// of a non-constant value onto the dwarf expr virtual machine (aka
7953 /// DEVM), perform the operation and update the DEVM. A non-constant
7954 /// is namely a quantity for which we need inferior (a running program
7955 /// image) state to know the exact value.
7956 ///
7957 /// Upon successful completion, as the result of the operation is a
7958 /// non-constant the DEVM accumulator value is left to its state as of
7959 /// before the invocation of this function.
7960 ///
7961 /// @param ops the array of the dwarf expression operations to consider.
7962 ///
7963 /// @param ops_len the lengths of @p ops array above.
7964 ///
7965 /// @param index the index of the operation to interpret, in @p ops.
7966 ///
7967 /// @param next_index the index of the operation to interpret at the
7968 /// next step, after this function completed and returned. This is
7969 /// set an output parameter that is set iff the function returns true.
7970 ///
7971 /// @param ctxt the DEVM evaluation context.
7972 ///
7973 /// @return true if the current operation actually pushes a
7974 /// non-constant value onto the DEVM stack, false otherwise.
7975 static bool
7976 op_pushes_non_constant_value(Dwarf_Op* ops,
7977  size_t ops_len,
7978  size_t index,
7979  size_t& next_index,
7980  dwarf_expr_eval_context& ctxt)
7981 {
7982  ABG_ASSERT(index < ops_len);
7983  Dwarf_Op& op = ops[index];
7984 
7985  switch (op.atom)
7986  {
7987  case DW_OP_reg0:
7988  case DW_OP_reg1:
7989  case DW_OP_reg2:
7990  case DW_OP_reg3:
7991  case DW_OP_reg4:
7992  case DW_OP_reg5:
7993  case DW_OP_reg6:
7994  case DW_OP_reg7:
7995  case DW_OP_reg8:
7996  case DW_OP_reg9:
7997  case DW_OP_reg10:
7998  case DW_OP_reg11:
7999  case DW_OP_reg12:
8000  case DW_OP_reg13:
8001  case DW_OP_reg14:
8002  case DW_OP_reg15:
8003  case DW_OP_reg16:
8004  case DW_OP_reg17:
8005  case DW_OP_reg18:
8006  case DW_OP_reg19:
8007  case DW_OP_reg20:
8008  case DW_OP_reg21:
8009  case DW_OP_reg22:
8010  case DW_OP_reg23:
8011  case DW_OP_reg24:
8012  case DW_OP_reg25:
8013  case DW_OP_reg26:
8014  case DW_OP_reg27:
8015  case DW_OP_reg28:
8016  case DW_OP_reg29:
8017  case DW_OP_reg30:
8018  case DW_OP_reg31:
8019  next_index = index + 1;
8020  break;
8021 
8022  case DW_OP_breg0:
8023  case DW_OP_breg1:
8024  case DW_OP_breg2:
8025  case DW_OP_breg3:
8026  case DW_OP_breg4:
8027  case DW_OP_breg5:
8028  case DW_OP_breg6:
8029  case DW_OP_breg7:
8030  case DW_OP_breg8:
8031  case DW_OP_breg9:
8032  case DW_OP_breg10:
8033  case DW_OP_breg11:
8034  case DW_OP_breg12:
8035  case DW_OP_breg13:
8036  case DW_OP_breg14:
8037  case DW_OP_breg15:
8038  case DW_OP_breg16:
8039  case DW_OP_breg17:
8040  case DW_OP_breg18:
8041  case DW_OP_breg19:
8042  case DW_OP_breg20:
8043  case DW_OP_breg21:
8044  case DW_OP_breg22:
8045  case DW_OP_breg23:
8046  case DW_OP_breg24:
8047  case DW_OP_breg25:
8048  case DW_OP_breg26:
8049  case DW_OP_breg27:
8050  case DW_OP_breg28:
8051  case DW_OP_breg29:
8052  case DW_OP_breg30:
8053  case DW_OP_breg31:
8054  next_index = index + 1;
8055  break;
8056 
8057  case DW_OP_regx:
8058  next_index = index + 2;
8059  break;
8060 
8061  case DW_OP_fbreg:
8062  next_index = index + 1;
8063  break;
8064 
8065  case DW_OP_bregx:
8066  next_index = index + 1;
8067  break;
8068 
8069  case DW_OP_GNU_variable_value:
8070  next_index = index + 1;
8071  break;
8072 
8073  default:
8074  return false;
8075  }
8076 
8077  expr_result r(false);
8078  ctxt.push(r);
8079 
8080  return true;
8081 }
8082 
8083 /// If the current operation in the dwarf expression represents a
8084 /// manipulation of the stack of the DWARF Expression Virtual Machine
8085 /// (aka DEVM), this function performs the operation and updates the
8086 /// state of the DEVM. If the result of the operation represents a
8087 /// constant value, then the accumulator of the DEVM is set to that
8088 /// result's value, Otherwise, the DEVM accumulator is left with its
8089 /// previous value.
8090 ///
8091 /// @param expr the array of the dwarf expression operations to consider.
8092 ///
8093 /// @param expr_len the lengths of @p ops array above.
8094 ///
8095 /// @param index the index of the operation to interpret, in @p ops.
8096 ///
8097 /// @param next_index the index of the operation to interpret at the
8098 /// next step, after this function completed and returned. This is
8099 /// set an output parameter that is set iff the function returns true.
8100 ///
8101 /// @param ctxt the DEVM evaluation context.
8102 ///
8103 /// @return true if the current operation actually manipulates the
8104 /// DEVM stack, false otherwise.
8105 static bool
8106 op_manipulates_stack(Dwarf_Op* expr,
8107  size_t expr_len,
8108  size_t index,
8109  size_t& next_index,
8110  dwarf_expr_eval_context& ctxt)
8111 {
8112  Dwarf_Op& op = expr[index];
8113  expr_result v;
8114 
8115  switch (op.atom)
8116  {
8117  case DW_OP_dup:
8118  v = ctxt.stack.front();
8119  ctxt.push(v);
8120  break;
8121 
8122  case DW_OP_drop:
8123  v = ctxt.stack.front();
8124  ctxt.pop();
8125  break;
8126 
8127  case DW_OP_over:
8128  ABG_ASSERT(ctxt.stack.size() > 1);
8129  v = ctxt.stack[1];
8130  ctxt.push(v);
8131  break;
8132 
8133  case DW_OP_pick:
8134  ABG_ASSERT(index + 1 < expr_len);
8135  v = op.number;
8136  ctxt.push(v);
8137  break;
8138 
8139  case DW_OP_swap:
8140  ABG_ASSERT(ctxt.stack.size() > 1);
8141  v = ctxt.stack[1];
8142  ctxt.stack.erase(ctxt.stack.begin() + 1);
8143  ctxt.push(v);
8144  break;
8145 
8146  case DW_OP_rot:
8147  ABG_ASSERT(ctxt.stack.size() > 2);
8148  v = ctxt.stack[2];
8149  ctxt.stack.erase(ctxt.stack.begin() + 2);
8150  ctxt.push(v);
8151  break;
8152 
8153  case DW_OP_deref:
8154  case DW_OP_deref_size:
8155  ABG_ASSERT(ctxt.stack.size() > 0);
8156  ctxt.pop();
8157  v.is_const(false);
8158  ctxt.push(v);
8159  break;
8160 
8161  case DW_OP_xderef:
8162  case DW_OP_xderef_size:
8163  ABG_ASSERT(ctxt.stack.size() > 1);
8164  ctxt.pop();
8165  ctxt.pop();
8166  v.is_const(false);
8167  ctxt.push(v);
8168  break;
8169 
8170  case DW_OP_push_object_address:
8171  v.is_const(false);
8172  ctxt.push(v);
8173  break;
8174 
8175  case DW_OP_form_tls_address:
8176  case DW_OP_GNU_push_tls_address:
8177  ABG_ASSERT(ctxt.stack.size() > 0);
8178  v = ctxt.pop();
8179  if (op.atom == DW_OP_form_tls_address)
8180  v.is_const(false);
8181  ctxt.push(v);
8182  break;
8183 
8184  case DW_OP_call_frame_cfa:
8185  v.is_const(false);
8186  ctxt.push(v);
8187  break;
8188 
8189  default:
8190  return false;
8191  }
8192 
8193  if (v.is_const())
8194  ctxt.accum = v;
8195 
8196  if (op.atom == DW_OP_form_tls_address
8197  || op.atom == DW_OP_GNU_push_tls_address)
8198  ctxt.set_tls_address(true);
8199  else
8200  ctxt.set_tls_address(false);
8201 
8202  next_index = index + 1;
8203 
8204  return true;
8205 }
8206 
8207 /// If the current operation in the dwarf expression represents a push
8208 /// of an arithmetic or logic operation onto the dwarf expr virtual
8209 /// machine (aka DEVM), perform the operation and update the DEVM.
8210 ///
8211 /// If the result of the operation is a constant, update the DEVM
8212 /// accumulator with its value. Otherwise, the DEVM accumulator is
8213 /// left with its previous value.
8214 ///
8215 /// @param expr the array of the dwarf expression operations to consider.
8216 ///
8217 /// @param expr_len the lengths of @p expr array above.
8218 ///
8219 /// @param index the index of the operation to interpret, in @p expr.
8220 ///
8221 /// @param next_index the index of the operation to interpret at the
8222 /// next step, after this function completed and returned. This is
8223 /// set an output parameter that is set iff the function returns true.
8224 ///
8225 /// @param ctxt the DEVM evaluation context.
8226 ///
8227 /// @return true if the current operation actually represent an
8228 /// arithmetic or logic operation.
8229 static bool
8230 op_is_arith_logic(Dwarf_Op* expr,
8231  size_t expr_len,
8232  size_t index,
8233  size_t& next_index,
8234  dwarf_expr_eval_context& ctxt)
8235 {
8236  ABG_ASSERT(index < expr_len);
8237 
8238  Dwarf_Op& op = expr[index];
8239  expr_result val1, val2;
8240  bool result = false;
8241 
8242  switch (op.atom)
8243  {
8244  case DW_OP_abs:
8245  ABG_ASSERT(ctxt.stack.size() > 0);
8246  val1 = ctxt.pop();
8247  val1 = val1.abs();
8248  ctxt.push(val1);
8249  result = true;
8250  break;
8251 
8252  case DW_OP_and:
8253  ABG_ASSERT(ctxt.stack.size() > 1);
8254  val1 = ctxt.pop();
8255  val2 = ctxt.pop();
8256  ctxt.push(val1 & val2);
8257  break;
8258 
8259  case DW_OP_div:
8260  ABG_ASSERT(ctxt.stack.size() > 1);
8261  val1 = ctxt.pop();
8262  val2 = ctxt.pop();
8263  if (!val1.is_const())
8264  val1 = 1;
8265  ctxt.push(val2 / val1);
8266  result = true;
8267  break;
8268 
8269  case DW_OP_minus:
8270  ABG_ASSERT(ctxt.stack.size() > 1);
8271  val1 = ctxt.pop();
8272  val2 = ctxt.pop();
8273  ctxt.push(val2 - val1);
8274  result = true;
8275  break;
8276 
8277  case DW_OP_mod:
8278  ABG_ASSERT(ctxt.stack.size() > 1);
8279  val1 = ctxt.pop();
8280  val2 = ctxt.pop();
8281  ctxt.push(val2 % val1);
8282  result = true;
8283  break;
8284 
8285  case DW_OP_mul:
8286  ABG_ASSERT(ctxt.stack.size() > 1);
8287  val1 = ctxt.pop();
8288  val2 = ctxt.pop();
8289  ctxt.push(val2 * val1);
8290  result = true;
8291  break;
8292 
8293  case DW_OP_neg:
8294  ABG_ASSERT(ctxt.stack.size() > 0);
8295  val1 = ctxt.pop();
8296  ctxt.push(-val1);
8297  result = true;
8298  break;
8299 
8300  case DW_OP_not:
8301  ABG_ASSERT(ctxt.stack.size() > 0);
8302  val1 = ctxt.pop();
8303  ctxt.push(~val1);
8304  result = true;
8305  break;
8306 
8307  case DW_OP_or:
8308  ABG_ASSERT(ctxt.stack.size() > 1);
8309  val1 = ctxt.pop();
8310  val2 = ctxt.pop();
8311  ctxt.push(val1 | val2);
8312  result = true;
8313  break;
8314 
8315  case DW_OP_plus:
8316  ABG_ASSERT(ctxt.stack.size() > 1);
8317  val1 = ctxt.pop();
8318  val2 = ctxt.pop();
8319  ctxt.push(val2 + val1);
8320  result = true;
8321  break;
8322 
8323  case DW_OP_plus_uconst:
8324  ABG_ASSERT(ctxt.stack.size() > 0);
8325  val1 = ctxt.pop();
8326  val1 += op.number;
8327  ctxt.push(val1);
8328  result = true;
8329  break;
8330 
8331  case DW_OP_shl:
8332  ABG_ASSERT(ctxt.stack.size() > 1);
8333  val1 = ctxt.pop();
8334  val2 = ctxt.pop();
8335  ctxt.push(val2 << val1);
8336  result = true;
8337  break;
8338 
8339  case DW_OP_shr:
8340  case DW_OP_shra:
8341  ABG_ASSERT(ctxt.stack.size() > 1);
8342  val1 = ctxt.pop();
8343  val2 = ctxt.pop();
8344  ctxt.push(val2 >> val1);
8345  result = true;
8346  break;
8347 
8348  case DW_OP_xor:
8349  ABG_ASSERT(ctxt.stack.size() > 1);
8350  val1 = ctxt.pop();
8351  val2 = ctxt.pop();
8352  ctxt.push(val2 ^ val1);
8353  result = true;
8354  break;
8355 
8356  default:
8357  break;
8358  }
8359 
8360  if (result == true)
8361  {
8362  if (ctxt.stack.front().is_const())
8363  ctxt.accum = ctxt.stack.front();
8364 
8365  next_index = index + 1;
8366  }
8367  return result;;
8368 }
8369 
8370 /// If the current operation in the dwarf expression represents a push
8371 /// of a control flow operation onto the dwarf expr virtual machine
8372 /// (aka DEVM), perform the operation and update the DEVM.
8373 ///
8374 /// If the result of the operation is a constant, update the DEVM
8375 /// accumulator with its value. Otherwise, the DEVM accumulator is
8376 /// left with its previous value.
8377 ///
8378 /// @param expr the array of the dwarf expression operations to consider.
8379 ///
8380 /// @param expr_len the lengths of @p expr array above.
8381 ///
8382 /// @param index the index of the operation to interpret, in @p expr.
8383 ///
8384 /// @param next_index the index of the operation to interpret at the
8385 /// next step, after this function completed and returned. This is
8386 /// set an output parameter that is set iff the function returns true.
8387 ///
8388 /// @param ctxt the DEVM evaluation context.
8389 ///
8390 /// @return true if the current operation actually represents a
8391 /// control flow operation, false otherwise.
8392 static bool
8393 op_is_control_flow(Dwarf_Op* expr,
8394  size_t expr_len,
8395  size_t index,
8396  size_t& next_index,
8397  dwarf_expr_eval_context& ctxt)
8398 {
8399  ABG_ASSERT(index < expr_len);
8400 
8401  Dwarf_Op& op = expr[index];
8402  expr_result val1, val2;
8403 
8404  switch (op.atom)
8405  {
8406  case DW_OP_eq:
8407  case DW_OP_ge:
8408  case DW_OP_gt:
8409  case DW_OP_le:
8410  case DW_OP_lt:
8411  case DW_OP_ne:
8412  {
8413  bool value = true;
8414  val1 = ctxt.pop();
8415  val2 = ctxt.pop();
8416  if (op.atom == DW_OP_eq)
8417  value = val2 == val1;
8418  else if (op.atom == DW_OP_ge)
8419  value = val2 >= val1;
8420  else if (op.atom == DW_OP_gt)
8421  value = val2 > val1;
8422  else if (op.atom == DW_OP_le)
8423  value = val2 <= val1;
8424  else if (op.atom == DW_OP_lt)
8425  value = val2 < val1;
8426  else if (op.atom == DW_OP_ne)
8427  value = val2 != val1;
8428 
8429  val1 = value ? 1 : 0;
8430  ctxt.push(val1);
8431  }
8432  break;
8433 
8434  case DW_OP_skip:
8435  if (op.number > 0)
8436  index += op.number - 1;
8437  break;
8438 
8439  case DW_OP_bra:
8440  val1 = ctxt.pop();
8441  if (val1.const_value() != 0)
8442  index += val1.const_value() - 1;
8443  break;
8444 
8445  case DW_OP_call2:
8446  case DW_OP_call4:
8447  case DW_OP_call_ref:
8448  case DW_OP_nop:
8449  break;
8450 
8451  default:
8452  return false;
8453  }
8454 
8455  if (ctxt.stack.front().is_const())
8456  ctxt.accum = ctxt.stack.front();
8457 
8458  next_index = index + 1;
8459  return true;
8460 }
8461 
8462 /// This function quickly evaluates a DWARF expression that is a
8463 /// constant.
8464 ///
8465 /// This is a "fast path" function that quickly evaluates a DWARF
8466 /// expression that is only made of a DW_OP_plus_uconst operator.
8467 ///
8468 /// This is a sub-routine of die_member_offset.
8469 ///
8470 /// @param expr the DWARF expression to evaluate.
8471 ///
8472 /// @param expr_len the length of the expression @p expr.
8473 ///
8474 /// @param value out parameter. This is set to the result of the
8475 /// evaluation of @p expr, iff this function returns true.
8476 ///
8477 /// @return true iff the evaluation of @p expr went OK.
8478 static bool
8479 eval_quickly(Dwarf_Op* expr,
8480  uint64_t expr_len,
8481  int64_t& value)
8482 {
8483  if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8484  {
8485  value = expr[0].number;
8486  return true;
8487  }
8488  return false;
8489 }
8490 
8491 /// Evaluate the value of the last sub-expression that is a constant,
8492 /// inside a given DWARF expression.
8493 ///
8494 /// @param expr the DWARF expression to consider.
8495 ///
8496 /// @param expr_len the length of the expression to consider.
8497 ///
8498 /// @param value the resulting value of the last constant
8499 /// sub-expression of the DWARF expression. This is set iff the
8500 /// function returns true.
8501 ///
8502 /// @param is_tls_address out parameter. This is set to true iff
8503 /// the resulting value of the evaluation is a TLS (thread local
8504 /// storage) address.
8505 ///
8506 /// @param eval_ctxt the evaluation context to (re)use. Note that
8507 /// this function initializes this context before using it.
8508 ///
8509 /// @return true if the function could find a constant sub-expression
8510 /// to evaluate, false otherwise.
8511 static bool
8512 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8513  size_t expr_len,
8514  int64_t& value,
8515  bool& is_tls_address,
8516  dwarf_expr_eval_context &eval_ctxt)
8517 {
8518  // Reset the evaluation context before evaluating the constant sub
8519  // expression contained in the DWARF expression 'expr'.
8520  eval_ctxt.reset();
8521 
8522  size_t index = 0, next_index = 0;
8523  do
8524  {
8525  if (op_is_arith_logic(expr, expr_len, index,
8526  next_index, eval_ctxt)
8527  || op_pushes_constant_value(expr, expr_len, index,
8528  next_index, eval_ctxt)
8529  || op_manipulates_stack(expr, expr_len, index,
8530  next_index, eval_ctxt)
8531  || op_pushes_non_constant_value(expr, expr_len, index,
8532  next_index, eval_ctxt)
8533  || op_is_control_flow(expr, expr_len, index,
8534  next_index, eval_ctxt))
8535  ;
8536  else
8537  next_index = index + 1;
8538 
8539  ABG_ASSERT(next_index > index);
8540  index = next_index;
8541  } while (index < expr_len);
8542 
8543  is_tls_address = eval_ctxt.set_tls_address();
8544  if (eval_ctxt.accum.is_const())
8545  {
8546  value = eval_ctxt.accum;
8547  return true;
8548  }
8549  return false;
8550 }
8551 
8552 /// Evaluate the value of the last sub-expression that is a constant,
8553 /// inside a given DWARF expression.
8554 ///
8555 /// @param expr the DWARF expression to consider.
8556 ///
8557 /// @param expr_len the length of the expression to consider.
8558 ///
8559 /// @param value the resulting value of the last constant
8560 /// sub-expression of the DWARF expression. This is set iff the
8561 /// function returns true.
8562 ///
8563 /// @return true if the function could find a constant sub-expression
8564 /// to evaluate, false otherwise.
8565 static bool
8566 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8567  size_t expr_len,
8568  int64_t& value,
8569  bool& is_tls_address)
8570 {
8571  dwarf_expr_eval_context eval_ctxt;
8572  return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8573  is_tls_address, eval_ctxt);
8574 }
8575 
8576 // -----------------------------------
8577 // </location expression evaluation>
8578 // -----------------------------------
8579 
8580 /// Convert a DW_AT_bit_offset attribute value into the same value as
8581 /// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8582 ///
8583 /// On big endian machines, the value of the DW_AT_bit_offset
8584 /// attribute + 8 * the value of the DW_AT_data_member_location
8585 /// attribute is the same as the value of the DW_AT_data_bit_offset
8586 /// attribute.
8587 ///
8588 /// On little endian machines however, the situation is different.
8589 /// The DW_AT_bit_offset value for a bit field is the number of bits
8590 /// to the left of the most significant bit of the bit field, within
8591 /// the integer value at DW_AT_data_member_location.
8592 ///
8593 /// The DW_AT_data_bit_offset offset value is the number of bits to
8594 /// the right of the least significant bit of the bit field, again
8595 /// relative to the containing integer value.
8596 ///
8597 /// In other words, DW_AT_data_bit_offset is what everybody would
8598 /// instinctively think of as being the "offset of the bit field". 8 *
8599 /// DW_AT_data_member_location + DW_AT_bit_offset however is very
8600 /// counter-intuitive on little endian machines.
8601 ///
8602 /// This function thus reads the value of a DW_AT_bit_offset property
8603 /// of a DIE and converts it into what the DW_AT_data_bit_offset would
8604 /// have been if it was present, ignoring the contribution of
8605 /// DW_AT_data_member_location.
8606 ///
8607 /// Note that DW_AT_bit_offset has been made obsolete starting from
8608 /// DWARF5 (for GCC; Clang still emits it).
8609 ///
8610 /// If you like coffee and it's not too late, now might be a good time
8611 /// to have a coffee break. Otherwise if it's late at night, you
8612 /// might want to consider an herbal tea break. Then come back to
8613 /// read this.
8614 ///
8615 ///
8616 /// In what follows, the bit fields are all contained within the first
8617 /// whole int of the struct, so DW_AT_data_member_location is 0.
8618 ///
8619 /// Okay, to have a better idea of what DW_AT_bit_offset and
8620 /// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8621 /// have bit fields data members defined as:
8622 ///
8623 /// struct S
8624 /// {
8625 /// int j:5;
8626 /// int k:6;
8627 /// int m:5;
8628 /// int n:8;
8629 /// };
8630 ///
8631 /// The below wonderful (at least!) ASCII art sketch describes the
8632 /// layout of the bitfields of 'struct S' on a little endian machine.
8633 /// You need to read the sketch from the bottom-up.
8634 ///
8635 /// So please scroll down to its bottom. Note how the 32 bits integer
8636 /// word containing the bit fields is laid out with its least
8637 /// significant bit starting on the right hand side, at index 0.
8638 ///
8639 /// Then slowly scroll up starting from there, and take the time to
8640 /// read each line and see how the bit fields are laid out and what
8641 /// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8642 /// the bit fields.
8643 ///
8644 /// DW_AT_bit_offset(n)
8645 /// < - - - - - - >
8646 /// | | n |
8647 /// ^ ^< - - - - >^
8648 /// DW_AT_data_bit_offset(n)
8649 /// < - - - - - - - - - - - - - - - >
8650 /// | |
8651 /// ^ ^
8652 /// DW_AT_bit_offset(m)
8653 /// <--------------------------------->
8654 /// | | m |
8655 /// ^ ^< - >^
8656 /// DW_AT_data_bit_offset(m)
8657 /// < - - - - - - - - - - >
8658 /// | |
8659 /// ^ ^
8660 /// DW_AT_bit_offset(k)
8661 /// <-------------------------------------------->
8662 /// | | k |
8663 /// ^ ^< - - >^
8664 /// DW_AT_data_bit_offset(k)
8665 /// < - - - - >
8666 /// | |
8667 /// ^ ^
8668 /// DW_AT_bit_offset(j)
8669 /// <-------------------------------------------------------->
8670 /// | |
8671 /// ^ ^
8672 /// n m k j
8673 /// < - - - - - - > < - - - > < - - - - > < - - - >
8674 ///
8675 /// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8676 /// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8677 /// 31 27 23 16 15 11 10 6 5 4 0
8678 ///
8679 /// So, the different bit fields all fit in one 32 bits word, assuming
8680 /// the bit fields are tightly packed.
8681 ///
8682 /// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8683 /// on this little endian machine and let's see how it relates to
8684 /// DW_AT_data_bit_offset of j.
8685 ///
8686 /// DW_AT_bit_offset(j) would be equal to the number of bits from the
8687 /// left of the 32 bits word (i.e from bit number 31) to the most
8688 /// significant bit of the j bit field (i.e, bit number 4). Thus:
8689 ///
8690 /// DW_AT_bit_offset(j) =
8691 /// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8692 ///
8693 /// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8694 /// 32 bits word (i.e, bit number 0) to the lest significant bit of
8695 /// the 'j' bit field (ie, bit number 0). Thus:
8696 ///
8697 /// DW_AT_data_bit_offset(j) = 0.
8698 ///
8699 /// More generally, we can notice that:
8700 ///
8701 /// sizeof_in_bits(int) =
8702 /// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8703 ///
8704 /// It follows that:
8705 ///
8706 /// DW_AT_data_bit_offset(j) =
8707 /// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8708 ///
8709 /// Thus:
8710 ///
8711 /// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8712 ///
8713 /// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8714 /// from the right hand side of the word. It is what we would
8715 /// intuitively think it is. DW_AT_bit_offset however is super
8716 /// counter-intuitive, pfff.
8717 ///
8718 /// Anyway, this general equation holds true for all bit fields.
8719 ///
8720 /// Similarly, it follows that:
8721 ///
8722 /// DW_AT_bit_offset(k) =
8723 /// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8724 ///
8725 /// Thus:
8726 /// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8727 ///
8728 ///
8729 /// Likewise:
8730 ///
8731 /// DW_AT_bit_offset(m) =
8732 /// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8733 ///
8734 ///
8735 /// Thus:
8736 /// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8737 ///
8738 /// And:
8739 ///
8740 ///
8741 /// Lastly:
8742 ///
8743 /// DW_AT_bit_offset(n) =
8744 /// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8745 ///
8746 /// Thus:
8747 /// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8748 ///
8749 /// Luckily, the body of the function is much smaller than this
8750 /// comment. Enjoy!
8751 ///
8752 /// @param die the DIE to consider.
8753 ///
8754 /// @param is_big_endian this is true iff the machine we are looking at
8755 /// is big endian.
8756 ///
8757 /// @param offset this is the output parameter into which the value of
8758 /// the DW_AT_bit_offset is put, converted as if it was the value of
8759 /// the DW_AT_data_bit_offset parameter, less the contribution of
8760 /// DW_AT_data_member_location. This parameter is set iff the
8761 /// function returns true.
8762 ///
8763 /// @return true if DW_AT_bit_offset was found on @p die.
8764 static bool
8765 read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8766  bool is_big_endian,
8767  uint64_t &offset)
8768 {
8769  uint64_t off = 0;
8770  if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8771  return false;
8772 
8773  if (is_big_endian)
8774  {
8775  offset = off;
8776  return true;
8777  }
8778 
8779  // Okay, we are looking at a little endian machine. We need to
8780  // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8781  // have been. To understand this, you really need to read the
8782  // preliminary comment of this function.
8783  uint64_t containing_anonymous_object_size = 0;
8784  ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8785  containing_anonymous_object_size));
8786  containing_anonymous_object_size *= 8;
8787 
8788  uint64_t bitfield_size = 0;
8789  ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8790  bitfield_size));
8791 
8792  // As noted in the the preliminary comment of this function if we
8793  // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8794  // its DW_AT_bit_offset value, the equation is:
8795  //
8796  // DW_AT_data_bit_offset(k) =
8797  // sizeof_in_bits(containing_anonymous_object_size)
8798  // - DW_AT_data_bit_offset(k)
8799  // - sizeof_in_bits(k)
8800  offset = containing_anonymous_object_size - off - bitfield_size;
8801 
8802  return true;
8803 }
8804 
8805 /// Get the value of the DW_AT_data_member_location of the given DIE
8806 /// attribute as an constant.
8807 ///
8808 /// @param die the DIE to read the attribute from.
8809 ///
8810 /// @param offset the attribute as a constant value. This is set iff
8811 /// the function returns true.
8812 ///
8813 /// @return true if the attribute exists and has a constant value. In
8814 /// that case the offset is set to the value.
8815 static bool
8816 die_constant_data_member_location(const Dwarf_Die *die,
8817  int64_t& offset)
8818 {
8819  if (!die)
8820  return false;
8821 
8822  Dwarf_Attribute attr;
8823  if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8824  DW_AT_data_member_location,
8825  &attr))
8826  return false;
8827 
8828  Dwarf_Word val;
8829  if (dwarf_formudata(&attr, &val) != 0)
8830  return false;
8831 
8832  offset = val;
8833  return true;
8834 }
8835 
8836 /// Get the offset of a struct/class member as represented by the
8837 /// value of the DW_AT_data_member_location attribute.
8838 ///
8839 /// There is a huge gotcha in here. The value of the
8840 /// DW_AT_data_member_location is not necessarily a constant that one
8841 /// would just read and be done with it. Rather, it can be a DWARF
8842 /// expression that one has to interpret. In general, the offset can
8843 /// be given by the DW_AT_data_bit_offset or by the
8844 /// DW_AT_data_member_location attribute and optionally the
8845 /// DW_AT_bit_offset attribute. The bit offset attributes are
8846 /// always simple constants, but the DW_AT_data_member_location
8847 /// attribute is a DWARF location expression.
8848 ///
8849 /// When it's the DW_AT_data_member_location that is present,
8850 /// there are three cases to possibly take into account:
8851 ///
8852 /// 1/ The offset in the vtable where the offset of a virtual base
8853 /// can be found, aka vptr offset. Given the address of a
8854 /// given object O, the vptr offset for B is given by the
8855 /// (DWARF) expression:
8856 ///
8857 /// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8858 ///
8859 /// where VIRTUAL_OFFSET is a constant value; In this case,
8860 /// this function returns the constant VIRTUAL_OFFSET, as this
8861 /// is enough to detect changes in a given virtual base
8862 /// relative to the other virtual bases.
8863 ///
8864 /// 2/ The offset of a regular data member. Given the address of
8865 /// a struct object named O, the memory location for a
8866 /// particular data member is given by the (DWARF) expression:
8867 ///
8868 /// address(O) + OFFSET
8869 ///
8870 /// where OFFSET is a constant. In this case, this function
8871 /// returns the OFFSET constant.
8872 ///
8873 /// 3/ The offset of a virtual member function in the virtual
8874 /// pointer. The DWARF expression is a constant that designates
8875 /// the offset of the function in the vtable. In this case this
8876 /// function returns that constant.
8877 ///
8878 /// @param rdr the DWARF reader to consider.
8879 ///
8880 /// @param die the DIE to read the information from.
8881 ///
8882 /// @param offset the resulting constant offset, in bits. This
8883 /// argument is set iff the function returns true.
8884 static bool
8885 die_member_offset(const reader& rdr,
8886  const Dwarf_Die* die,
8887  int64_t& offset)
8888 {
8889  Dwarf_Op* expr = NULL;
8890  size_t expr_len = 0;
8891  uint64_t bit_offset = 0;
8892 
8893  // First let's see if the DW_AT_data_bit_offset attribute is
8894  // present.
8895  if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8896  {
8897  offset = bit_offset;
8898  return true;
8899  }
8900 
8901  // First try to read DW_AT_data_member_location as a plain constant.
8902  // We do this because the generic method using die_location_expr
8903  // might hit a bug in elfutils libdw dwarf_location_expression only
8904  // fixed in elfutils 0.184+. The bug only triggers if the attribute
8905  // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8906  // handle all constants here because that is more consistent (and
8907  // slightly faster in the general case where the attribute isn't a
8908  // full DWARF expression).
8909  if (!die_constant_data_member_location(die, offset))
8910  {
8911  // Otherwise, let's see if the DW_AT_data_member_location
8912  // attribute and, optionally, the DW_AT_bit_offset attributes
8913  // are present.
8914  if (!die_location_expr(die, DW_AT_data_member_location,
8915  &expr, &expr_len))
8916  return false;
8917 
8918  // The DW_AT_data_member_location attribute is present. Let's
8919  // evaluate it and get its constant sub-expression and return
8920  // that one.
8921  if (!eval_quickly(expr, expr_len, offset))
8922  {
8923  bool is_tls_address = false;
8924  if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8925  offset, is_tls_address,
8926  rdr.dwarf_expr_eval_ctxt()))
8927  return false;
8928  }
8929  }
8930  offset *= 8;
8931 
8932  // On little endian machines, we need to convert the
8933  // DW_AT_bit_offset attribute into a relative offset to 8 *
8934  // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8935  // would be if it were used instead.
8936  //
8937  // In other words, before adding it to 8 *
8938  // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8939  // converted into a human-understandable form that represents the
8940  // offset of the bitfield data member it describes. For details
8941  // about the conversion, please read the extensive comments of
8942  // read_and_convert_DW_at_bit_offset.
8943  bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
8944  if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8945  offset += bit_offset;
8946 
8947  return true;
8948 }
8949 
8950 /// Read the value of the DW_AT_location attribute from a DIE,
8951 /// evaluate the resulting DWARF expression and, if it's a constant
8952 /// expression, return it.
8953 ///
8954 /// @param die the DIE to consider.
8955 ///
8956 /// @param address the resulting constant address. This is set iff
8957 /// the function returns true.
8958 ///
8959 /// @return true iff the whole sequence of action described above
8960 /// could be completed normally.
8961 static bool
8962 die_location_address(Dwarf_Die* die,
8963  Dwarf_Addr& address,
8964  bool& is_tls_address)
8965 {
8966  Dwarf_Op* expr = NULL;
8967  size_t expr_len = 0;
8968 
8969  is_tls_address = false;
8970 
8971  if (!die)
8972  return false;
8973 
8974  Dwarf_Attribute attr;
8975  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8976  return false;
8977 
8978  if (dwarf_getlocation(&attr, &expr, &expr_len))
8979  return false;
8980  // Ignore location expressions where reading them succeeded but
8981  // their length is 0.
8982  if (expr_len == 0)
8983  return false;
8984 
8985  Dwarf_Attribute result;
8986  if (!dwarf_getlocation_attr(&attr, expr, &result))
8987  // A location that has been interpreted as an address.
8988  return !dwarf_formaddr(&result, &address);
8989 
8990  // Just get the address out of the number field.
8991  address = expr->number;
8992  return true;
8993 }
8994 
8995 /// Return the index of a function in its virtual table. That is,
8996 /// return the value of the DW_AT_vtable_elem_location attribute.
8997 ///
8998 /// @param die the DIE of the function to consider.
8999 ///
9000 /// @param vindex the resulting index. This is set iff the function
9001 /// returns true.
9002 ///
9003 /// @return true if the DIE has a DW_AT_vtable_elem_location
9004 /// attribute.
9005 static bool
9006 die_virtual_function_index(Dwarf_Die* die,
9007  int64_t& vindex)
9008 {
9009  if (!die)
9010  return false;
9011 
9012  Dwarf_Op* expr = NULL;
9013  size_t expr_len = 0;
9014  if (!die_location_expr(die, DW_AT_vtable_elem_location,
9015  &expr, &expr_len))
9016  return false;
9017 
9018  int64_t i = 0;
9019  bool is_tls_addr = false;
9020  if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9021  return false;
9022 
9023  vindex = i;
9024  return true;
9025 }
9026 
9027 /// Test if a given DIE represents an anonymous type.
9028 ///
9029 /// Anonymous types we are interested in are classes, unions and
9030 /// enumerations.
9031 ///
9032 /// @param die the DIE to consider.
9033 ///
9034 /// @return true iff @p die represents an anonymous type.
9035 bool
9036 is_anonymous_type_die(Dwarf_Die *die)
9037 {
9038  int tag = dwarf_tag(die);
9039 
9040  if (tag == DW_TAG_class_type
9041  || tag == DW_TAG_structure_type
9042  || tag == DW_TAG_union_type
9043  || tag == DW_TAG_enumeration_type)
9044  return die_is_anonymous(die);
9045 
9046  return false;
9047 }
9048 
9049 /// Return the base of the internal name to represent an anonymous
9050 /// type.
9051 ///
9052 /// Typically, anonymous enums would be named
9053 /// __anonymous_enum__<number>, anonymous struct or classes would be
9054 /// named __anonymous_struct__<number> and anonymous unions would be
9055 /// named __anonymous_union__<number>. The first part of these
9056 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9057 /// the base name. This function returns that base name, depending on
9058 /// the kind of type DIE we are looking at.
9059 ///
9060 /// @param die the type DIE to look at. This function expects a type
9061 /// DIE with an empty DW_AT_name property value (anonymous).
9062 ///
9063 /// @return a string representing the base of the internal anonymous
9064 /// name.
9065 static string
9066 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9067 {
9068  ABG_ASSERT(die_is_type(die));
9069  ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9070 
9071  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9072  string type_name;
9073  if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9075  else if (tag == DW_TAG_union_type)
9077  else if (tag == DW_TAG_enumeration_type)
9079 
9080  return type_name;
9081 }
9082 
9083 /// Build a full internal anonymous type name.
9084 ///
9085 /// @param base_name this is the base name as returned by the function
9086 /// @ref get_internal_anonymous_die_prefix_name.
9087 ///
9088 /// @param anonymous_type_index this is the index of the anonymous
9089 /// type in its scope. That is, if there are more than one anonymous
9090 /// types of a given kind in a scope, this index is what tells them
9091 /// appart, starting from 0.
9092 ///
9093 /// @return the built string, which is a concatenation of @p base_name
9094 /// and @p anonymous_type_index.
9095 static string
9096 build_internal_anonymous_die_name(const string &base_name,
9097  size_t anonymous_type_index)
9098 {
9099  string name = base_name;
9100  if (anonymous_type_index && !base_name.empty())
9101  {
9102  std::ostringstream o;
9103  o << base_name << anonymous_type_index;
9104  name = o.str();
9105  }
9106  return name;
9107 }
9108 
9109 
9110 /// Build a full internal anonymous type name.
9111 ///
9112 /// @param die the DIE representing the anonymous type to consider.
9113 ///
9114 /// @param anonymous_type_index the index of the anonymous type
9115 /// represented by @p DIE, in its scope. That is, if there are
9116 /// several different anonymous types of the same kind as @p die, this
9117 /// index is what tells them appart.
9118 ///
9119 /// @return the internal name of the anonymous type represented by @p
9120 /// DIE.
9121 static string
9122 get_internal_anonymous_die_name(Dwarf_Die *die,
9123  size_t anonymous_type_index)
9124 {
9125  string name = get_internal_anonymous_die_prefix_name(die);
9126  name = build_internal_anonymous_die_name(name, anonymous_type_index);
9127  return name;
9128 }
9129 
9130 // ------------------------------------
9131 // <DIE pretty printer>
9132 // ------------------------------------
9133 
9134 /// Compute the qualified name of a DIE that represents a type.
9135 ///
9136 /// For instance, if the DIE tag is DW_TAG_subprogram then this
9137 /// function computes the name of the function *type*.
9138 ///
9139 /// @param rdr the DWARF reader.
9140 ///
9141 /// @param die the DIE to consider.
9142 ///
9143 /// @param where_offset where in the are logically are in the DIE
9144 /// stream.
9145 ///
9146 /// @return a copy of the qualified name of the type.
9147 static string
9148 die_qualified_type_name(const reader& rdr,
9149  const Dwarf_Die* die,
9150  size_t where_offset)
9151 {
9152  if (!die)
9153  return "";
9154 
9155  int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9156  if (tag == DW_TAG_compile_unit
9157  || tag == DW_TAG_partial_unit
9158  || tag == DW_TAG_type_unit)
9159  return "";
9160 
9161  string name = die_name(die);
9162 
9163  Dwarf_Die scope_die;
9164  if (!get_scope_die(rdr, die, where_offset, scope_die))
9165  return "";
9166 
9167  string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9168  bool colon_colon = die_is_type(die) || die_is_namespace(die);
9169  string separator = colon_colon ? "::" : ".";
9170 
9171  string repr;
9172 
9173  switch (tag)
9174  {
9175  case DW_TAG_unspecified_type:
9176  break;
9177 
9178  case DW_TAG_base_type:
9179  {
9180  abigail::ir::integral_type int_type;
9181  if (parse_integral_type(name, int_type))
9182  repr = int_type;
9183  else
9184  repr = name;
9185  }
9186  break;
9187 
9188  case DW_TAG_typedef:
9189  case DW_TAG_enumeration_type:
9190  case DW_TAG_structure_type:
9191  case DW_TAG_class_type:
9192  case DW_TAG_union_type:
9193  {
9194  if (name.empty())
9195  // TODO: handle cases where there are more than one
9196  // anonymous type of the same kind in the same scope. In
9197  // that case, their name must be built with the function
9198  // get_internal_anonymous_die_name or something of the same
9199  // kind.
9200  name = get_internal_anonymous_die_prefix_name(die);
9201 
9202  ABG_ASSERT(!name.empty());
9203  repr = parent_name.empty() ? name : parent_name + separator + name;
9204  }
9205  break;
9206 
9207  case DW_TAG_const_type:
9208  case DW_TAG_volatile_type:
9209  case DW_TAG_restrict_type:
9210  {
9211  Dwarf_Die underlying_type_die;
9212  bool has_underlying_type_die =
9213  die_die_attribute(die, DW_AT_type, underlying_type_die);
9214 
9215  if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9216  break;
9217 
9218  if (tag == DW_TAG_const_type)
9219  {
9220  if (has_underlying_type_die
9221  && die_is_reference_type(&underlying_type_die))
9222  // A reference is always const. So, to lower false
9223  // positive reports in diff computations, we consider a
9224  // const reference just as a reference. But we need to
9225  // keep the qualified-ness of the type. So we introduce
9226  // a 'no-op' qualifier here. Please remember that this
9227  // has to be kept in sync with what is done in
9228  // get_name_of_qualified_type. So if you change this
9229  // here, you have to change that code there too.
9230  repr = "";
9231  else if (!has_underlying_type_die
9232  || die_is_void_type(&underlying_type_die))
9233  {
9234  repr = "void";
9235  break;
9236  }
9237  else
9238  repr = "const";
9239  }
9240  else if (tag == DW_TAG_volatile_type)
9241  repr = "volatile";
9242  else if (tag == DW_TAG_restrict_type)
9243  repr = "restrict";
9244  else
9246 
9247  string underlying_type_repr;
9248  if (has_underlying_type_die)
9249  underlying_type_repr =
9250  die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9251  else
9252  underlying_type_repr = "void";
9253 
9254  if (underlying_type_repr.empty())
9255  repr.clear();
9256  else
9257  {
9258  if (has_underlying_type_die)
9259  {
9260  Dwarf_Die peeled;
9261  die_peel_qualified(&underlying_type_die, peeled);
9262  if (die_is_pointer_or_reference_type(&peeled))
9263  repr = underlying_type_repr + " " + repr;
9264  else
9265  repr += " " + underlying_type_repr;
9266  }
9267  else
9268  repr += " " + underlying_type_repr;
9269  }
9270  }
9271  break;
9272 
9273  case DW_TAG_pointer_type:
9274  case DW_TAG_reference_type:
9275  case DW_TAG_rvalue_reference_type:
9276  {
9277  Dwarf_Die pointed_to_type_die;
9278  if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9279  {
9280  if (tag == DW_TAG_pointer_type)
9281  repr = "void*";
9282  break;
9283  }
9284 
9285  if (die_is_unspecified(&pointed_to_type_die))
9286  break;
9287 
9288  string pointed_type_repr =
9289  die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9290 
9291  repr = pointed_type_repr;
9292  if (repr.empty())
9293  break;
9294 
9295  if (tag == DW_TAG_pointer_type)
9296  repr += "*";
9297  else if (tag == DW_TAG_reference_type)
9298  repr += "&";
9299  else if (tag == DW_TAG_rvalue_reference_type)
9300  repr += "&&";
9301  else
9303  }
9304  break;
9305 
9306  case DW_TAG_subrange_type:
9307  {
9308  // In Ada, this one can be generated on its own, that is, not
9309  // as a sub-type of an array. So we need to support it on its
9310  // own. Note that when it's emitted as the sub-type of an
9311  // array like in C and C++, this is handled differently, for
9312  // now. But we try to make this usable by other languages
9313  // that are not Ada, even if we modelled it after Ada.
9314 
9315  // So we build a subrange type for the sole purpose of using
9316  // the ::as_string() method of that type. So we don't add
9317  // that type to the current type tree being built.
9319  build_subrange_type(const_cast<reader&>(rdr),
9320  die, where_offset,
9321  /*associate_die_to_type=*/false);
9322  repr += s->as_string();
9323  break;
9324  }
9325 
9326  case DW_TAG_array_type:
9327  {
9328  Dwarf_Die element_type_die;
9329  if (!die_die_attribute(die, DW_AT_type, element_type_die))
9330  break;
9331  string element_type_name =
9332  die_qualified_type_name(rdr, &element_type_die, where_offset);
9333  if (element_type_name.empty())
9334  break;
9335 
9337  build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9338  die, subranges, where_offset,
9339  /*associate_type_to_die=*/false);
9340 
9341  repr = element_type_name;
9342  repr += array_type_def::subrange_type::vector_as_string(subranges);
9343  }
9344  break;
9345 
9346  case DW_TAG_subroutine_type:
9347  case DW_TAG_subprogram:
9348  {
9349  string return_type_name;
9350  string class_name;
9351  vector<string> parm_names;
9352  bool is_const = false;
9353  bool is_static = false;
9354 
9355  die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9356  /*pretty_print=*/true,
9357  return_type_name, class_name,
9358  parm_names, is_const,
9359  is_static);
9360  if (return_type_name.empty())
9361  return_type_name = "void";
9362 
9363  repr = return_type_name;
9364 
9365  if (!class_name.empty())
9366  {
9367  // This is a method, so print the class name.
9368  repr += " (" + class_name + "::*)";
9369  }
9370 
9371  // Now parameters.
9372  repr += " (";
9373  for (vector<string>::const_iterator i = parm_names.begin();
9374  i != parm_names.end();
9375  ++i)
9376  {
9377  if (i != parm_names.begin())
9378  repr += ", ";
9379  repr += *i;
9380  }
9381  repr += ")";
9382 
9383  }
9384  break;
9385 
9386  case DW_TAG_string_type:
9387  case DW_TAG_ptr_to_member_type:
9388  case DW_TAG_set_type:
9389  case DW_TAG_file_type:
9390  case DW_TAG_packed_type:
9391  case DW_TAG_thrown_type:
9392  case DW_TAG_interface_type:
9393  case DW_TAG_shared_type:
9394  break;
9395  }
9396 
9397  return repr;
9398 }
9399 
9400 /// Compute the qualified name of a decl represented by a given DIE.
9401 ///
9402 /// For instance, for a DIE of tag DW_TAG_subprogram this function
9403 /// computes the signature of the function *declaration*.
9404 ///
9405 /// @param rdr the DWARF reader.
9406 ///
9407 /// @param die the DIE to consider.
9408 ///
9409 /// @param where_offset where we are logically at in the DIE stream.
9410 ///
9411 /// @return a copy of the computed name.
9412 static string
9413 die_qualified_decl_name(const reader& rdr,
9414  const Dwarf_Die* die,
9415  size_t where_offset)
9416 {
9417  if (!die || !die_is_decl(die))
9418  return "";
9419 
9420  string name = die_name(die);
9421 
9422  Dwarf_Die scope_die;
9423  if (!get_scope_die(rdr, die, where_offset, scope_die))
9424  return "";
9425 
9426  string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9427  string separator = "::";
9428 
9429  string repr;
9430 
9431  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9432  switch (tag)
9433  {
9434  case DW_TAG_namespace:
9435  case DW_TAG_member:
9436  case DW_TAG_variable:
9437  repr = scope_name.empty() ? name : scope_name + separator + name;
9438  break;
9439  case DW_TAG_subprogram:
9440  repr = die_function_signature(rdr, die, where_offset);
9441  break;
9442 
9443  case DW_TAG_unspecified_parameters:
9444  repr = "...";
9445  break;
9446 
9447  case DW_TAG_formal_parameter:
9448  case DW_TAG_imported_declaration:
9449  case DW_TAG_GNU_template_template_param:
9450  case DW_TAG_GNU_template_parameter_pack:
9451  case DW_TAG_GNU_formal_parameter_pack:
9452  break;
9453  }
9454  return repr;
9455 }
9456 
9457 /// Compute the qualified name of the artifact represented by a given
9458 /// DIE.
9459 ///
9460 /// If the DIE represents a type, then the function computes the name
9461 /// of the type. Otherwise, if the DIE represents a decl then the
9462 /// function computes the name of the decl. Note that a DIE of tag
9463 /// DW_TAG_subprogram is going to be considered as a "type" -- just
9464 /// like if it was a DW_TAG_subroutine_type.
9465 ///
9466 /// @param rdr the DWARF reader.
9467 ///
9468 /// @param die the DIE to consider.
9469 ///
9470 /// @param where_offset where we are logically at in the DIE stream.
9471 ///
9472 /// @return a copy of the computed name.
9473 static string
9474 die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9475 {
9476  if (die_is_type(die))
9477  return die_qualified_type_name(rdr, die, where);
9478  else if (die_is_decl(die))
9479  return die_qualified_decl_name(rdr, die, where);
9480  return "";
9481 }
9482 
9483 /// Test if the qualified name of a given type should be empty.
9484 ///
9485 /// The reason why the name of a DIE with a given tag would be empty
9486 /// is that libabigail's internal representation doesn't yet support
9487 /// that tag; or if the DIE's qualified name is built from names of
9488 /// sub-types DIEs whose tags are not yet supported.
9489 ///
9490 /// @param rdr the DWARF reader.
9491 ///
9492 /// @param die the DIE to consider.
9493 ///
9494 /// @param where where we are logically at, in the DIE stream.
9495 ///
9496 /// @param qualified_name the qualified name of the DIE. This is set
9497 /// only iff the function returns false.
9498 ///
9499 /// @return true if the qualified name of the DIE is empty.
9500 static bool
9501 die_qualified_type_name_empty(const reader& rdr,
9502  const Dwarf_Die* die,
9503  size_t where, string &qualified_name)
9504 {
9505  if (!die)
9506  return true;
9507 
9508  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9509 
9510  string qname;
9511  if (tag == DW_TAG_typedef
9512  || tag == DW_TAG_pointer_type
9513  || tag == DW_TAG_reference_type
9514  || tag == DW_TAG_rvalue_reference_type
9515  || tag == DW_TAG_array_type
9516  || tag == DW_TAG_const_type
9517  || tag == DW_TAG_volatile_type
9518  || tag == DW_TAG_restrict_type)
9519  {
9520  Dwarf_Die underlying_type_die;
9521  if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9522  {
9523  string name =
9524  die_qualified_type_name(rdr, &underlying_type_die, where);
9525  if (name.empty())
9526  return true;
9527  }
9528  }
9529  else
9530  {
9531  string name = die_qualified_type_name(rdr, die, where);
9532  if (name.empty())
9533  return true;
9534  }
9535 
9536  qname = die_qualified_type_name(rdr, die, where);
9537  if (qname.empty())
9538  return true;
9539 
9540  qualified_name = qname;
9541  return false;
9542 }
9543 
9544 /// Given the DIE that represents a function type, compute the names
9545 /// of the following properties the function's type:
9546 ///
9547 /// - return type
9548 /// - enclosing class (if the function is a member function)
9549 /// - function parameter types
9550 ///
9551 /// When the function we are looking at is a member function, it also
9552 /// tells if it's const.
9553 ///
9554 /// @param rdr the DWARF reader.
9555 ///
9556 /// @param die the DIE of the function or function type we are looking
9557 /// at.
9558 ///
9559 /// @param where_offset where we are logically at in the DIE stream.
9560 ///
9561 /// @param pretty_print if set to yes, the type names are going to be
9562 /// pretty-printed names; otherwise, they are just qualified type
9563 /// names.
9564 ///
9565 /// @param return_type_name out parameter. This contains the name of
9566 /// the return type of the function.
9567 ///
9568 /// @param class_name out parameter. If the function is a member
9569 /// function, this contains the name of the enclosing class.
9570 ///
9571 /// @param parm_names out parameter. This vector is set to the names
9572 /// of the types of the parameters of the function.
9573 ///
9574 /// @param is_const out parameter. If the function is a member
9575 /// function, this is set to true iff the member function is const.
9576 ///
9577 /// @param is_static out parameter. If the function is a static
9578 /// member function, then this is set to true.
9579 static void
9580 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9581  const Dwarf_Die* die,
9582  size_t where_offset,
9583  bool pretty_print,
9584  string &return_type_name,
9585  string &class_name,
9586  vector<string>& parm_names,
9587  bool& is_const,
9588  bool& is_static)
9589 {
9590  Dwarf_Die child;
9591  Dwarf_Die ret_type_die;
9592  if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9593  return_type_name = "void";
9594  else
9595  return_type_name =
9596  pretty_print
9597  ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9598  : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9599 
9600  if (return_type_name.empty())
9601  return_type_name = "void";
9602 
9603  Dwarf_Die object_pointer_die, class_die;
9604  bool is_method_type =
9605  die_function_type_is_method_type(rdr, die, where_offset,
9606  object_pointer_die,
9607  class_die, is_static);
9608 
9609  is_const = false;
9610  if (is_method_type)
9611  {
9612  class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9613 
9614  Dwarf_Die this_pointer_die;
9615  Dwarf_Die pointed_to_die;
9616  if (!is_static
9617  && die_die_attribute(&object_pointer_die, DW_AT_type,
9618  this_pointer_die))
9619  if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9620  if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9621  is_const = true;
9622 
9623  string fn_name = die_name(die);
9624  string non_qualified_class_name = die_name(&class_die);
9625  bool is_ctor = fn_name == non_qualified_class_name;
9626  bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9627 
9628  if (is_ctor || is_dtor)
9629  return_type_name.clear();
9630  }
9631 
9632  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9633  do
9634  {
9635  int child_tag = dwarf_tag(&child);
9636  if (child_tag == DW_TAG_formal_parameter)
9637  {
9638  Dwarf_Die parm_type_die;
9639  if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9640  continue;
9641  string qualified_name =
9642  pretty_print
9643  ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9644  : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9645 
9646  if (qualified_name.empty())
9647  continue;
9648  parm_names.push_back(qualified_name);
9649  }
9650  else if (child_tag == DW_TAG_unspecified_parameters)
9651  {
9652  // This is a variadic function parameter.
9653  parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9654  // After a DW_TAG_unspecified_parameters tag, we shouldn't
9655  // keep reading for parameters. The
9656  // unspecified_parameters TAG should be the last parameter
9657  // that we record. For instance, if there are multiple
9658  // DW_TAG_unspecified_parameters DIEs then we should care
9659  // only for the first one.
9660  break;
9661  }
9662  }
9663  while (dwarf_siblingof(&child, &child) == 0);
9664 
9665  if (class_name.empty())
9666  {
9667  Dwarf_Die parent_die;
9668  if (get_parent_die(rdr, die, parent_die, where_offset))
9669  {
9670  if (die_is_class_type(&parent_die))
9671  class_name =
9672  rdr.get_die_qualified_type_name(&parent_die, where_offset);
9673  }
9674  }
9675 }
9676 
9677 /// This computes the signature of the a function declaration
9678 /// represented by a DIE.
9679 ///
9680 /// @param rdr the DWARF reader.
9681 ///
9682 /// @param fn_die the DIE of the function to consider.
9683 ///
9684 /// @param where_offset where we are logically at in the stream of
9685 /// DIEs.
9686 ///
9687 /// @return a copy of the computed function signature string.
9688 static string
9689 die_function_signature(const reader& rdr,
9690  const Dwarf_Die *fn_die,
9691  size_t where_offset)
9692 {
9693 
9695  bool has_lang = false;
9696  if ((has_lang = rdr.get_die_language(fn_die, lang)))
9697  {
9698  // In a binary originating from the C language, it's OK to use
9699  // the linkage name of the function as a key for the map which
9700  // is meant to reduce the number of DIE comparisons involved
9701  // during DIE canonicalization computation.
9702  if (is_c_language(lang))
9703  {
9704  string fn_name = die_linkage_name(fn_die);
9705  if (fn_name.empty())
9706  fn_name = die_name(fn_die);
9707  return fn_name;
9708  }
9709  }
9710 
9711  // TODO: When we can structurally compare DIEs originating from C++
9712  // as well, we can use the linkage name of functions in C++ too, to
9713  // reduce the number of comparisons involved during DIE
9714  // canonicalization.
9715 
9716  string return_type_name;
9717  Dwarf_Die ret_type_die;
9718  if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9719  return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9720  where_offset);
9721 
9722  if (return_type_name.empty())
9723  return_type_name = "void";
9724 
9725  Dwarf_Die scope_die;
9726  string scope_name;
9727  if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9728  scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9729  string fn_name = die_name(fn_die);
9730  if (!scope_name.empty())
9731  fn_name = scope_name + "::" + fn_name;
9732 
9733  string class_name;
9734  vector<string> parm_names;
9735  bool is_const = false;
9736  bool is_static = false;
9737 
9738  die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9739  /*pretty_print=*/false,
9740  return_type_name, class_name,
9741  parm_names, is_const, is_static);
9742 
9743  bool is_virtual = die_is_virtual(fn_die);
9744 
9745  string repr = class_name.empty() ? "function" : "method";
9746  if (is_virtual)
9747  repr += " virtual";
9748 
9749  if (!return_type_name.empty())
9750  repr += " " + return_type_name;
9751 
9752  repr += " " + fn_name;
9753 
9754  // Now parameters.
9755  repr += "(";
9756  bool some_parm_emitted = false;
9757  for (vector<string>::const_iterator i = parm_names.begin();
9758  i != parm_names.end();
9759  ++i)
9760  {
9761  if (i != parm_names.begin())
9762  {
9763  if (some_parm_emitted)
9764  repr += ", ";
9765  }
9766  else
9767  if (!is_static && !class_name.empty())
9768  // We are printing a non-static method name, skip the implicit "this"
9769  // parameter type.
9770  continue;
9771  repr += *i;
9772  some_parm_emitted = true;
9773  }
9774  repr += ")";
9775 
9776  if (is_const)
9777  {
9778  ABG_ASSERT(!class_name.empty());
9779  repr += " const";
9780  }
9781 
9782  return repr;
9783 }
9784 
9785 /// Return a pretty string representation of a type, for internal purposes.
9786 ///
9787 /// By internal purpose, we mean things like key-ing types for lookup
9788 /// purposes and so on.
9789 ///
9790 /// Note that this function is also used to pretty print functions.
9791 /// For functions, it prints the *type* of the function.
9792 ///
9793 /// @param rdr the context to use.
9794 ///
9795 /// @param the DIE of the type to pretty print.
9796 ///
9797 /// @param where_offset where we logically are placed when calling
9798 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9799 /// entries.
9800 ///
9801 /// @return the resulting pretty representation.
9802 static string
9803 die_pretty_print_type(reader& rdr,
9804  const Dwarf_Die* die,
9805  size_t where_offset)
9806 {
9807  if (!die
9808  || (!die_is_type(die)
9809  && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9810  return "";
9811 
9812  string repr;
9813 
9814  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9815  switch (tag)
9816  {
9817  case DW_TAG_string_type:
9818  // For now, we won't try to go get the actual representation of
9819  // the string because this would make things more complicated;
9820  // for that we'd need to interpret some location expressions to
9821  // get the length of the string. And for dynamically allocated
9822  // strings, the result of the location expression evaluation
9823  // might not even be a constant. So at the moment I consider
9824  // this to be a lot of hassle for no great return. Until proven
9825  // otherwise, of course.
9826  repr = "string type";
9827 
9828  case DW_TAG_unspecified_type:
9829  case DW_TAG_ptr_to_member_type:
9830  break;
9831 
9832  case DW_TAG_namespace:
9833  repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9834  break;
9835 
9836  case DW_TAG_base_type:
9837  repr = rdr.get_die_qualified_type_name(die, where_offset);
9838  break;
9839 
9840  case DW_TAG_typedef:
9841  {
9842  string qualified_name;
9843  if (!die_qualified_type_name_empty(rdr, die,
9844  where_offset,
9845  qualified_name))
9846  repr = "typedef " + qualified_name;
9847  }
9848  break;
9849 
9850  case DW_TAG_const_type:
9851  case DW_TAG_volatile_type:
9852  case DW_TAG_restrict_type:
9853  case DW_TAG_pointer_type:
9854  case DW_TAG_reference_type:
9855  case DW_TAG_rvalue_reference_type:
9856  repr = rdr.get_die_qualified_type_name(die, where_offset);
9857  break;
9858 
9859  case DW_TAG_enumeration_type:
9860  {
9861  string qualified_name =
9862  rdr.get_die_qualified_type_name(die, where_offset);
9863  repr = "enum " + qualified_name;
9864  }
9865  break;
9866 
9867  case DW_TAG_structure_type:
9868  case DW_TAG_class_type:
9869  {
9870  string qualified_name =
9871  rdr.get_die_qualified_type_name(die, where_offset);
9872  repr = "class " + qualified_name;
9873  }
9874  break;
9875 
9876  case DW_TAG_union_type:
9877  {
9878  string qualified_name =
9879  rdr.get_die_qualified_type_name(die, where_offset);
9880  repr = "union " + qualified_name;
9881  }
9882  break;
9883 
9884  case DW_TAG_array_type:
9885  {
9886  Dwarf_Die element_type_die;
9887  if (!die_die_attribute(die, DW_AT_type, element_type_die))
9888  break;
9889  string element_type_name =
9890  rdr.get_die_qualified_type_name(&element_type_die, where_offset);
9891  if (element_type_name.empty())
9892  break;
9893 
9895  build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
9896  /*associate_type_to_die=*/false);
9897 
9898  repr = element_type_name;
9899  repr += array_type_def::subrange_type::vector_as_string(subranges);
9900  }
9901  break;
9902 
9903  case DW_TAG_subrange_type:
9904  {
9905  // So this can be generated by Ada, on its own; that is, not
9906  // as a subtype of an array. In that case we need to handle
9907  // it properly.
9908 
9909  // For now, we consider that the pretty printed name of the
9910  // subrange type is its name. We might need something more
9911  // advance, should the needs of the users get more
9912  // complicated.
9913  repr += die_qualified_type_name(rdr, die, where_offset);
9914  }
9915  break;
9916 
9917  case DW_TAG_subroutine_type:
9918  case DW_TAG_subprogram:
9919  {
9920  string return_type_name;
9921  string class_name;
9922  vector<string> parm_names;
9923  bool is_const = false;
9924  bool is_static = false;
9925 
9926  die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9927  /*pretty_print=*/true,
9928  return_type_name, class_name,
9929  parm_names, is_const,
9930  is_static);
9931  if (class_name.empty())
9932  repr = "function type";
9933  else
9934  repr = "method type";
9935  repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
9936  }
9937  break;
9938 
9939  case DW_TAG_set_type:
9940  case DW_TAG_file_type:
9941  case DW_TAG_packed_type:
9942  case DW_TAG_thrown_type:
9943  case DW_TAG_interface_type:
9944  case DW_TAG_shared_type:
9946  }
9947 
9948  return repr;
9949 }
9950 
9951 /// Return a pretty string representation of a declaration, for
9952 /// internal purposes.
9953 ///
9954 /// By internal purpose, we mean things like key-ing declarations for
9955 /// lookup purposes and so on.
9956 ///
9957 /// Note that this function is also used to pretty print functions.
9958 /// For functions, it prints the signature of the function.
9959 ///
9960 /// @param rdr the context to use.
9961 ///
9962 /// @param the DIE of the declaration to pretty print.
9963 ///
9964 /// @param where_offset where we logically are placed when calling
9965 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9966 /// entries.
9967 ///
9968 /// @return the resulting pretty representation.
9969 static string
9970 die_pretty_print_decl(reader& rdr,
9971  const Dwarf_Die* die,
9972  size_t where_offset)
9973 {
9974  if (!die || !die_is_decl(die))
9975  return "";
9976 
9977  string repr;
9978 
9979  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9980  switch (tag)
9981  {
9982  case DW_TAG_namespace:
9983  repr = "namespace " + die_qualified_name(rdr, die, where_offset);
9984  break;
9985 
9986  case DW_TAG_member:
9987  case DW_TAG_variable:
9988  {
9989  string type_repr = "void";
9990  Dwarf_Die type_die;
9991  if (die_die_attribute(die, DW_AT_type, type_die))
9992  type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
9993  repr = die_qualified_name(rdr, die, where_offset);
9994  if (!repr.empty())
9995  repr = type_repr + " " + repr;
9996  }
9997  break;
9998 
9999  case DW_TAG_subprogram:
10000  repr = die_function_signature(rdr, die, where_offset);
10001  break;
10002 
10003  default:
10004  break;
10005  }
10006  return repr;
10007 }
10008 
10009 /// Compute the pretty printed representation of an artifact
10010 /// represented by a DIE.
10011 ///
10012 /// If the DIE is a type, compute the its pretty representation as a
10013 /// type; otherwise, if it's a declaration, compute its pretty
10014 /// representation as a declaration. Note for For instance, that a
10015 /// DW_TAG_subprogram DIE is going to be represented as a function
10016 /// *type*.
10017 ///
10018 /// @param rdr the DWARF reader.
10019 ///
10020 /// @param die the DIE to consider.
10021 ///
10022 /// @param where_offset we in the DIE stream we are logically at.
10023 ///
10024 /// @return a copy of the pretty printed artifact.
10025 static string
10026 die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10027 {
10028  if (die_is_type(die))
10029  return die_pretty_print_type(rdr, die, where_offset);
10030  else if (die_is_decl(die))
10031  return die_pretty_print_decl(rdr, die, where_offset);
10032  return "";
10033 }
10034 
10035 // -----------------------------------
10036 // </die pretty printer>
10037 // -----------------------------------
10038 
10039 
10040 // ----------------------------------
10041 // <die comparison engine>
10042 // ---------------------------------
10043 
10044 /// Compares two decls DIEs
10045 ///
10046 /// This works only for DIEs emitted by the C language.
10047 ///
10048 /// This implementation doesn't yet support namespaces.
10049 ///
10050 /// This is a subroutine of compare_dies.
10051 ///
10052 /// @return true iff @p l equals @p r.
10053 static bool
10054 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10055 {
10056  ABG_ASSERT(l && r);
10057 
10058  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10059  int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10060  if (l_tag != r_tag)
10061  return false;
10062 
10063  bool result = false;
10064 
10065  if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10066  {
10067  // Fast path for functions and global variables.
10068  if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10069  result)
10070  || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10071  result))
10072  {
10073  if (!result)
10074  return false;
10075  }
10076 
10077  if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10078  result))
10079  {
10080  if (!result)
10081  return false;
10082  }
10083  return true;
10084  }
10085 
10086  // Fast path for types.
10087  if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10088  result))
10089  return result;
10090  return true;
10091 }
10092 
10093 /// Test if at least one of two ODR-relevant DIEs is decl-only.
10094 ///
10095 /// @param rdr the DWARF reader to consider.
10096 ///
10097 /// @param l the first type DIE to consider.
10098 ///
10099 /// @param r the second type DIE to consider.
10100 ///
10101 /// @return true iff either @p l or @p r is decl-only and both are
10102 /// ODR-relevant.
10103 static bool
10104 at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10105  const Dwarf_Die *l,
10106  const Dwarf_Die *r)
10107 {
10108  if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10109  return false;
10110 
10111  if ((die_is_declaration_only(l) && die_has_no_child(l))
10112  || (die_is_declaration_only(r) && die_has_no_child(r)))
10113  return true;
10114  return false;
10115 }
10116 
10117 /// Compares two type DIEs
10118 ///
10119 /// This is a subroutine of compare_dies.
10120 ///
10121 /// Note that this function doesn't look at the name of the DIEs.
10122 /// Naming is taken into account by the function compare_as_decl_dies.
10123 ///
10124 /// If the two DIEs are from a translation unit that is subject to the
10125 /// ONE Definition Rule, then the function considers that if one DIE
10126 /// is a declaration, then it's equivalent to the second. In that
10127 /// case, the sizes of the two DIEs are not compared. This is so that
10128 /// a declaration of a type compares equal to the definition of the
10129 /// type.
10130 ///
10131 /// @param rdr the DWARF reader to consider.
10132 ///
10133 /// @param l the left operand of the comparison operator.
10134 ///
10135 /// @param r the right operand of the comparison operator.
10136 ///
10137 /// @return true iff @p l equals @p r.
10138 static bool
10139 compare_as_type_dies(const reader& rdr,
10140  const Dwarf_Die *l,
10141  const Dwarf_Die *r)
10142 {
10143  ABG_ASSERT(l && r);
10144  ABG_ASSERT(die_is_type(l));
10145  ABG_ASSERT(die_is_type(r));
10146 
10147  if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10148  && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10149  && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10150  != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10151  // For now, we cannot compare DW_TAG_string_type because of its
10152  // string_length attribute that is a location descriptor that is
10153  // not necessarily a constant. So it's super hard to evaluate it
10154  // in a libabigail context. So for now, we just say that all
10155  // DW_TAG_string_type DIEs are different, by default.
10156  return false;
10157 
10158  if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10159  // A declaration of a type compares equal to the definition of the
10160  // type.
10161  return true;
10162 
10163  uint64_t l_size = 0, r_size = 0;
10164  die_size_in_bits(l, l_size);
10165  die_size_in_bits(r, r_size);
10166 
10167  return l_size == r_size;
10168 }
10169 
10170 /// Compare two DIEs as decls (looking as their names etc) and as
10171 /// types (looking at their size etc).
10172 ///
10173 /// @param rdr the DWARF reader to consider.
10174 ///
10175 /// @param l the first DIE to consider.
10176 ///
10177 /// @param r the second DIE to consider.
10178 ///
10179 /// @return TRUE iff @p l equals @p r as far as naming and size is
10180 /// concerned.
10181 static bool
10182 compare_as_decl_and_type_dies(const reader &rdr,
10183  const Dwarf_Die *l,
10184  const Dwarf_Die *r)
10185 {
10186  if (!compare_as_decl_dies(l, r)
10187  || !compare_as_type_dies(rdr, l, r))
10188  return false;
10189 
10190  return true;
10191 }
10192 
10193 /// Test if two DIEs representing function declarations have the same
10194 /// linkage name, and thus are considered equal if they are C or C++,
10195 /// because the two DIEs represent functions in the same binary.
10196 ///
10197 /// If the DIEs don't have a linkage name, the function compares their
10198 /// name. But in that case, the caller of the function must know that
10199 /// in C++ for instance, that doesn't imply that the two functions are
10200 /// equal.
10201 ///
10202 /// @param rdr the @ref reader to consider.
10203 ///
10204 /// @param l the first function DIE to consider.
10205 ///
10206 /// @param r the second function DIE to consider.
10207 ///
10208 /// @return true iff the function represented by @p l have the same
10209 /// linkage name as the function represented by @p r.
10210 static bool
10211 fn_die_equal_by_linkage_name(const reader &rdr,
10212  const Dwarf_Die *l,
10213  const Dwarf_Die *r)
10214 {
10215  if (!!l != !!r)
10216  return false;
10217 
10218  if (!l)
10219  return false;
10220 
10221  int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10222  ABG_ASSERT(tag == DW_TAG_subprogram);
10223  tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10224  ABG_ASSERT(tag == DW_TAG_subprogram);
10225 
10226  string lname = die_name(l), rname = die_name(r);
10227  string llinkage_name = die_linkage_name(l),
10228  rlinkage_name = die_linkage_name(r);
10229 
10230  if (rdr.die_is_in_c_or_cplusplus(l)
10231  && rdr.die_is_in_c_or_cplusplus(r))
10232  {
10233  if (!llinkage_name.empty() && !rlinkage_name.empty())
10234  return llinkage_name == rlinkage_name;
10235  else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10236  return false;
10237  else
10238  return lname == rname;
10239  }
10240 
10241  return (!llinkage_name.empty()
10242  && !rlinkage_name.empty()
10243  && llinkage_name == rlinkage_name);
10244 }
10245 
10246 /// Compare two DIEs in the context of DIE canonicalization.
10247 ///
10248 /// If DIE canonicalization is on, the function compares the DIEs
10249 /// canonically and structurally. The two types of comparison should
10250 /// be equal, of course.
10251 ///
10252 /// @param rdr the DWARF reader.
10253 ///
10254 /// @param l_offset the offset of the first canonical DIE to compare.
10255 ///
10256 /// @param r_offset the offset of the second canonical DIE to compare.
10257 ///
10258 /// @param l_die_source the source of the DIE denoted by the offset @p
10259 /// l_offset.
10260 ///
10261 /// @param r_die_source the source of the DIE denoted by the offset @p
10262 /// r_offset.
10263 ///
10264 /// @param l_has_canonical_die_offset output parameter. Is set to
10265 /// true if @p l_offset has a canonical DIE.
10266 ///
10267 /// @param r_has_canonical_die_offset output parameter. Is set to
10268 /// true if @p r_offset has a canonical DIE.
10269 ///
10270 /// @param l_canonical_die_offset output parameter. If @p
10271 /// l_has_canonical_die_offset is set to true, then this parameter is
10272 /// set to the offset of the canonical DIE of the DIE designated by @p
10273 /// l_offset.
10274 static bool
10275 try_canonical_die_comparison(const reader& rdr,
10276  Dwarf_Off l_offset, Dwarf_Off r_offset,
10277  die_source l_die_source, die_source r_die_source,
10278  bool& l_has_canonical_die_offset,
10279  bool& r_has_canonical_die_offset,
10280  Dwarf_Off& l_canonical_die_offset,
10281  Dwarf_Off& r_canonical_die_offset,
10282  bool& result)
10283 {
10284 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10285  if (rdr.debug_die_canonicalization_is_on_
10286  && !rdr.use_canonical_die_comparison_)
10287  return false;
10288 #endif
10289 
10290 
10291  l_has_canonical_die_offset =
10292  (l_canonical_die_offset =
10293  rdr.get_canonical_die_offset(l_offset, l_die_source,
10294  /*die_as_type=*/true));
10295 
10296  r_has_canonical_die_offset =
10297  (r_canonical_die_offset =
10298  rdr.get_canonical_die_offset(r_offset, r_die_source,
10299  /*die_as_type=*/true));
10300 
10301  if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10302  {
10303  result = (l_canonical_die_offset == r_canonical_die_offset);
10304  return true;
10305  }
10306 
10307  return false;
10308 }
10309 
10310 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10311 /// This function is called whenever a DIE comparison fails.
10312 ///
10313 /// This function is intended for debugging purposes. The idea is for
10314 /// hackers to set a breakpoint on this function so that they can
10315 /// discover why exactly the comparison failed. They then can execute
10316 /// the program from compare_dies_during_canonicalization, for
10317 /// instance.
10318 ///
10319 /// @param @l the left-hand side of the DIE comparison.
10320 ///
10321 /// @param @r the right-hand side of the DIE comparison.
10322 static void
10323 notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10324 {
10325 }
10326 
10327 #define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10328  notify_die_comparison_failed(l, r)
10329 #else
10330 #define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10331 #endif
10332 
10333 /// A macro used to return from DIE comparison routines.
10334 ///
10335 /// If the return value is false, the macro invokes the
10336 /// notify_die_comparison_failed signalling function before returning.
10337 /// That way, hackers willing to learn more about why the comparison
10338 /// routine returned "false" can just set a breakpoint on
10339 /// notify_die_comparison_failed and execute the program from
10340 /// compare_dies_during_canonicalization, for instance.
10341 ///
10342 /// @param value the value to return from the DIE comparison routines.
10343 #define ABG_RETURN(value) \
10344  do \
10345  { \
10346  if ((value) == COMPARISON_RESULT_DIFFERENT) \
10347  { \
10348  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10349  } \
10350  return return_comparison_result(l, r, dies_being_compared, \
10351  value, aggregates_being_compared, \
10352  update_canonical_dies_on_the_fly); \
10353  } \
10354  while(false)
10355 
10356 /// A macro used to return the "false" boolean from DIE comparison
10357 /// routines.
10358 ///
10359 /// As the return value is false, the macro invokes the
10360 /// notify_die_comparison_failed signalling function before returning.
10361 ///
10362 /// @param value the value to return from the DIE comparison routines.
10363 #define ABG_RETURN_FALSE \
10364  do \
10365  { \
10366  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10367  return return_comparison_result(l, r, dies_being_compared, \
10368  COMPARISON_RESULT_DIFFERENT, \
10369  aggregates_being_compared, \
10370  update_canonical_dies_on_the_fly); \
10371  } while(false)
10372 
10373 /// A macro to set the 'result' variable to 'false'.
10374 ///
10375 /// The macro invokes the notify_die_comparison_failed function so
10376 /// that the hacker can set a debugging breakpoint on
10377 /// notify_die_comparison_failed to know where a DIE comparison failed
10378 /// during compare_dies_during_canonicalization for instance.
10379 ///
10380 /// @param result the 'result' variable to set.
10381 ///
10382 /// @param l the first DIE of the comparison operation.
10383 ///
10384 /// @param r the second DIE of the comparison operation.
10385 #define SET_RESULT_TO_FALSE(result, l , r) \
10386  do \
10387  { \
10388  result = COMPARISON_RESULT_DIFFERENT; \
10389  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10390  } while(false)
10391 
10392 /// A macro to set the 'result' variable to a given value.
10393 ///
10394 /// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10395 /// invokes the notify_die_comparison_failed function so that the
10396 /// hacker can set a debugging breakpoint on
10397 /// notify_die_comparison_failed to know where a DIE comparison failed
10398 /// during compare_dies_during_canonicalization for instance.
10399 ///
10400 /// @param result the 'result' variable to set.
10401 ///
10402 /// @param l the first DIE of the comparison operation.
10403 ///
10404 /// @param r the second DIE of the comparison operation.
10405 #define SET_RESULT_TO(result, value, l , r) \
10406  do \
10407  { \
10408  result = (value); \
10409  if (result == COMPARISON_RESULT_DIFFERENT) \
10410  { \
10411  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10412  } \
10413  } while(false)
10414 
10415 #define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10416  do \
10417  { \
10418  if (aggregates_being_compared.contains(dies_being_compared)) \
10419  { \
10420  result = COMPARISON_RESULT_CYCLE_DETECTED; \
10421  aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10422  ABG_RETURN(result); \
10423  } \
10424  } \
10425  while(false)
10426 
10427 /// Get the next member sibling of a given class or union member DIE.
10428 ///
10429 /// @param die the DIE to consider.
10430 ///
10431 /// @param member out parameter. This is set to the next member
10432 /// sibling, iff the function returns TRUE.
10433 ///
10434 /// @return TRUE iff the function set @p member to the next member
10435 /// sibling DIE.
10436 static bool
10437 get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10438 {
10439  if (!die)
10440  return false;
10441 
10442  bool found_member = false;
10443  for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10444  member) == 0);
10445  found_member;
10446  found_member = (dwarf_siblingof(member, member) == 0))
10447  {
10448  int tag = dwarf_tag(member);
10449  if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10450  break;
10451  }
10452 
10453  return found_member;
10454 }
10455 
10456 /// Get the first child DIE of a class/struct/union DIE that is a
10457 /// member DIE.
10458 ///
10459 /// @param die the DIE to consider.
10460 ///
10461 /// @param child out parameter. This is set to the first child DIE of
10462 /// @p iff this function returns TRUE.
10463 ///
10464 /// @return TRUE iff @p child is set to the first child DIE of @p die
10465 /// that is a member DIE.
10466 static bool
10467 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10468 {
10469  if (!die)
10470  return false;
10471 
10472  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10473  ABG_ASSERT(tag == DW_TAG_structure_type
10474  || tag == DW_TAG_union_type
10475  || tag == DW_TAG_class_type);
10476 
10477  bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10478  child) == 0);
10479 
10480  if (!found_child)
10481  return false;
10482 
10483  tag = dwarf_tag(child);
10484 
10485  if (!(tag == DW_TAG_member
10486  || tag == DW_TAG_inheritance
10487  || tag == DW_TAG_subprogram))
10488  found_child = get_next_member_sibling_die(child, child);
10489 
10490  return found_child;
10491 }
10492 
10493 /// This is a sub-routine of return_comparison_result.
10494 ///
10495 /// Propagate the canonical type of a the right-hand-side DIE to the
10496 /// lef-hand-side DIE. This is a optimization that is done when the
10497 /// two DIEs compare equal.
10498 ///
10499 /// If the right-hand-side DIE is not canonicalized, the function
10500 /// performs its canonicalization.
10501 ///
10502 /// This optimization is performed only if
10503 /// is_canon_type_to_be_propagated_tag returns true.
10504 ///
10505 /// @param rdr the current context to consider.
10506 ///
10507 /// @param l the left-hand-side DIE of the comparison. It's going to
10508 /// receive the canonical type of the other DIE.
10509 ///
10510 /// @param r the right-hand-side DIE of the comparison. Its canonical
10511 /// type is propagated to @p l.
10512 static void
10513 maybe_propagate_canonical_type(const reader& rdr,
10514  const Dwarf_Die* l,
10515  const Dwarf_Die* r)
10516 {
10517  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10518  r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10519 
10520  if (l_tag != r_tag)
10521  return;
10522 
10523  if (is_canon_type_to_be_propagated_tag(l_tag))
10524  propagate_canonical_type(rdr, l, r);
10525 }
10526 
10527 /// Propagate the canonical type of a the right-hand-side DIE to the
10528 /// left-hand-side DIE. This is a optimization that is done when the
10529 /// two DIEs compare equal.
10530 ///
10531 /// If the right-hand-side DIE is not canonicalized, the function
10532 /// performs its canonicalization.
10533 ///
10534 /// @param rdr the current context to consider.
10535 ///
10536 /// @param l the left-hand-side DIE of the comparison. It's going to
10537 /// receive the canonical type of the other DIE.
10538 ///
10539 /// @param r the right-hand-side DIE of the comparison. Its canonical
10540 /// type is propagated to @p l.
10541 static void
10542 propagate_canonical_type(const reader& rdr,
10543  const Dwarf_Die* l,
10544  const Dwarf_Die* r)
10545 {
10546  ABG_ASSERT(l && r);
10547 
10548  // If 'l' has no canonical DIE and if 'r' has one, then propagage
10549  // the canonical DIE of 'r' to 'l'.
10550  //
10551  // In case 'r' has no canonical DIE, then compute it, and then
10552  // propagate that canonical DIE to 'r'.
10553  const die_source l_source = rdr.get_die_source(l);
10554  const die_source r_source = rdr.get_die_source(r);
10555 
10556  Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10557  Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10558  bool l_has_canonical_die_offset = false;
10559  bool r_has_canonical_die_offset = false;
10560  Dwarf_Off l_canonical_die_offset = 0;
10561  Dwarf_Off r_canonical_die_offset = 0;
10562 
10563  l_has_canonical_die_offset =
10564  (l_canonical_die_offset =
10565  rdr.get_canonical_die_offset(l_offset, l_source,
10566  /*die_as_type=*/true));
10567 
10568  r_has_canonical_die_offset =
10569  (r_canonical_die_offset =
10570  rdr.get_canonical_die_offset(r_offset, r_source,
10571  /*die_as_type=*/true));
10572 
10573 
10574  if (!l_has_canonical_die_offset
10575  // A DIE can be equivalent only to another DIE of the same
10576  // source.
10577  && l_source == r_source)
10578  {
10579  if (!r_has_canonical_die_offset)
10580  rdr.compute_canonical_die_offset(r, r_canonical_die_offset,
10581  /*die_as_type=*/true);
10582  ABG_ASSERT(r_canonical_die_offset);
10583  rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10584  /*die_as_type=*/true);
10585  offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10586  rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10587  rdr.canonical_propagated_count_++;
10588  }
10589 }
10590 
10591 /// This function does the book keeping of comparison pairs necessary
10592 /// to handle
10593 ///
10594 /// * the detection of cycles during the comparison of aggregate
10595 /// types, in conjuction with the macro
10596 /// RETURN_IF_COMPARISON_CYCLE_DETECTED
10597 ///
10598 /// * the handling of the canonical type propagation optimisation
10599 /// to speed-up type canonicalization.
10600 ///
10601 ///
10602 /// Note that this function is essentially a sub-routine of
10603 /// compare_dies.
10604 ///
10605 /// @param l the left-hand-side DIE being compared.
10606 ///
10607 /// @param r the right-hand-side DIE being compared.
10608 ///
10609 /// @param cur_dies the pair of die offsets of l and r. This is
10610 /// redundant as it can been computed from @p l and @p r. However,
10611 /// getting it as an argument is an optimization to avoid computing it
10612 /// over and over again, given how often this function is invoked from
10613 /// compare_dies.
10614 ///
10615 /// @param return the result of comparing @p l against @p r.
10616 ///
10617 /// @param comparison_stack the stack of pair of type DIEs being
10618 /// compared.
10619 ///
10620 /// @param do_propagate_canonical_type if true then the function
10621 /// performs canonical DIEs propagation, meaning that if @p l equals
10622 /// @p r and if @p r has a canonical type, then the canonical type of
10623 /// @p l is set to the canonical type of @p r.
10624 static comparison_result
10625 return_comparison_result(const Dwarf_Die* l,
10626  const Dwarf_Die* r,
10627  const offset_pair_type& cur_dies,
10628  comparison_result result,
10629  offset_pairs_stack_type& comparison_stack,
10630  bool do_propagate_canonical_type = true)
10631 {
10632  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10633 
10634  if (result == COMPARISON_RESULT_EQUAL)
10635  {
10636  // The result comparing the two types is "true", basically. So
10637  // let's propagate the canonical type of r onto l, so that we
10638  // don't need to compute the canonical type of r.
10639  if (do_propagate_canonical_type)
10640  {
10641  // Propagate canonical type.
10642  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10643 
10644  // TODO: do we need to confirm any tentative canonical
10645  // propagation?
10646  }
10647  }
10648  else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10649  {
10650  // So upon detection of the comparison cycle, compare_dies
10651  // returned early with the comparison result
10652  // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10653  // carry on with the comparison of all the OTHER sub-types of
10654  // the redundant type. If they all compare equal, then it means
10655  // the redundant type pair compared equal. Otherwise, it
10656  // compared different.
10657  //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10658  // Let's fall through to let the end of this function set the
10659  // result to COMPARISON_RESULT_UNKNOWN;
10660  }
10661  else if (result == COMPARISON_RESULT_UNKNOWN)
10662  {
10663  // Here is an introductory comment describing what we are going
10664  // to do in this case where the result of the comparison of the
10665  // current pair of type is not "false", basically.
10666  //
10667  // This means that we don't yet know what the result of
10668  // comparing these two types is, because one of the sub-types of
10669  // the types being compared is "redundant", meaning it appears
10670  // more than once in the comparison stack, so if we were to
10671  // naively try to carry on with the comparison member-wise, we'd
10672  // end up with an endless loop, a.k.a "comparison cycle".
10673  //
10674  // If the current type pair is redundant then:
10675  //
10676  // * This is a redundant type that has just been fully
10677  // compared. In that case, all the types that depend on
10678  // this redundant type and that have been tentatively
10679  // canonical-type-propagated must see their canonical types
10680  // "confirmed". This means that this type is going to be
10681  // considered as not being redundant anymore, meaning all
10682  // the types that depend on it must be updated as not being
10683  // dependant on it anymore, and the type itsef must be
10684  // removed from the map of redundant types.
10685  //
10686  // After the type's canonical-type-propagation is confirmed,
10687  // the result of its comparison must also be changed into
10688  // COMPARISON_RESULT_EQUAL.
10689  //
10690  // After that, If the current type depends on a redundant type,
10691  // then propagate its canonical type AND track it as having its
10692  // type being canonical-type-propagated.
10693  //
10694  // If the current type is not redundant however, then it must be
10695  // dependant on a redundant type. If it's not dependant on a
10696  // redundant type, then it must be of those types which
10697  // comparisons are not tracked for cycle, probably because they
10698  // are not aggregates. Otherwise, ABORT to understand why. I
10699  // believe this should not happen. In any case, after that
10700  // safety check is passed, we just need to return at this point.
10701 
10702  if (comparison_stack.is_redundant(cur_dies)
10703  && comparison_stack.vect_.back() == cur_dies)
10704  {
10705  // We are in the case described above of a redundant type
10706  // that has been fully compared.
10707  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10708  comparison_stack.confirm_canonical_propagated_type(cur_dies);
10709 
10710  result = COMPARISON_RESULT_EQUAL;
10711  }
10712  else if (is_canon_type_to_be_propagated_tag(l_tag)
10713  && comparison_stack.vect_.back() == cur_dies)
10714  {
10715  // The current type is not redundant. So, as described in
10716  // the introductory comment above, it must be dependant on a
10717  // redundant type.
10718  ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10719  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10720  // Then pass through.
10721  }
10722  }
10723  else if (result == COMPARISON_RESULT_DIFFERENT)
10724  {
10725  // Here is an introductory comment describing what we are going
10726  // to do in this case where the result of the comparison of the
10727  // current pair of type is "false", basically.
10728  //
10729  // If the type pair {l,r} is redundant then cancel the
10730  // canonical-type-propagation of all the dependant pairs that
10731  // depends on this redundant {l, r}. This means walk the types
10732  // that depends on {l, r} and cancel their
10733  // canonical-propagate-type, that means remove their canonical
10734  // types and mark them as not being canonically-propagated.
10735  // Also, erase their cached comparison results that was likely
10736  // set to COMPARISON_RESULT_UNKNOWN.
10737  //
10738  // Also, update the cached result for this pair, that was likely
10739  // to be COMPARISON_RESULT_UNKNOWN.
10740  if (comparison_stack.is_redundant(cur_dies)
10741  && comparison_stack.vect_.back() == cur_dies)
10742  comparison_stack.cancel_canonical_propagated_type(cur_dies);
10743  }
10744  else
10745  {
10746  // We should never reach here.
10748  }
10749 
10750  if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10751  result = COMPARISON_RESULT_UNKNOWN;
10752  else if (is_canon_type_to_be_propagated_tag(l_tag)
10753  && !comparison_stack.vect_.empty()
10754  && comparison_stack.vect_.back() == cur_dies)
10755  //Finally pop the pair types being compared from comparison_stack
10756  //iff {l,r} is on the top of the stack. If it's not, then it means
10757  //we are looking at a type that was detected as a being redundant
10758  //and thus hasn't been pushed to the stack yet gain.
10759  comparison_stack.erase(cur_dies);
10760 
10761  maybe_cache_type_comparison_result(comparison_stack.rdr_,
10762  l_tag, cur_dies, result);
10763 
10764  return result;
10765 }
10766 
10767 /// Compare two DIEs emitted by a C compiler.
10768 ///
10769 /// @param rdr the DWARF reader used to load the DWARF information.
10770 ///
10771 /// @param l the left-hand-side argument of this comparison operator.
10772 ///
10773 /// @param r the righ-hand-side argument of this comparison operator.
10774 ///
10775 /// @param aggregates_being_compared this holds the names of the set
10776 /// of aggregates being compared. It's used by the comparison
10777 /// function to avoid recursing infinitely when faced with types
10778 /// referencing themselves through pointers or references. By
10779 /// default, just pass an empty instance of @ref istring_set_type to
10780 /// it.
10781 ///
10782 /// @param update_canonical_dies_on_the_fly if true, when two
10783 /// sub-types compare equal (during the comparison of @p l and @p r)
10784 /// update their canonical type. That way, two types of the same name
10785 /// are structurally compared to each other only once. So the
10786 /// non-linear structural comparison of two types of the same name
10787 /// only happen once.
10788 ///
10789 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10790 static comparison_result
10791 compare_dies(const reader& rdr,
10792  const Dwarf_Die *l, const Dwarf_Die *r,
10793  offset_pairs_stack_type& aggregates_being_compared,
10794  bool update_canonical_dies_on_the_fly)
10795 {
10796  ABG_ASSERT(l);
10797  ABG_ASSERT(r);
10798 
10799  const die_source l_die_source = rdr.get_die_source(l);
10800  const die_source r_die_source = rdr.get_die_source(r);
10801 
10802  offset_type l_offset =
10803  {
10804  l_die_source,
10805  dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10806  };
10807 
10808  offset_type r_offset =
10809  {
10810  r_die_source,
10811  dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10812  };
10813 
10814  offset_pair_type dies_being_compared(l_offset, r_offset);
10815 
10816  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10817  r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10818 
10819  if (l_tag != r_tag)
10821 
10822  if (l_offset == r_offset)
10823  return COMPARISON_RESULT_EQUAL;
10824 
10825  if (rdr.leverage_dwarf_factorization()
10826  && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10827  && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10828  if (l_offset != r_offset)
10829  return COMPARISON_RESULT_DIFFERENT;
10830 
10831  comparison_result result = COMPARISON_RESULT_EQUAL;
10832  if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10833  dies_being_compared,
10834  result))
10835  return result;
10836 
10837  Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10838  bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10839 
10840  // If 'l' and 'r' already have canonical DIEs, then just compare the
10841  // offsets of their canonical DIEs.
10842  if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10843  {
10844  bool canonical_compare_result = false;
10845  if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10846  l_die_source, r_die_source,
10847  l_has_canonical_die_offset,
10848  r_has_canonical_die_offset,
10849  l_canonical_die_offset,
10850  r_canonical_die_offset,
10851  canonical_compare_result))
10852  {
10853  comparison_result result;
10854  SET_RESULT_TO(result,
10855  (canonical_compare_result
10856  ? COMPARISON_RESULT_EQUAL
10857  : COMPARISON_RESULT_DIFFERENT),
10858  l, r);
10859  return result;
10860  }
10861  }
10862 
10863 
10864 
10865  switch (l_tag)
10866  {
10867  case DW_TAG_base_type:
10868  case DW_TAG_string_type:
10869  case DW_TAG_unspecified_type:
10870  if (!compare_as_decl_and_type_dies(rdr, l, r))
10871  SET_RESULT_TO_FALSE(result, l, r);
10872  break;
10873 
10874  case DW_TAG_typedef:
10875  case DW_TAG_pointer_type:
10876  case DW_TAG_reference_type:
10877  case DW_TAG_rvalue_reference_type:
10878  case DW_TAG_const_type:
10879  case DW_TAG_volatile_type:
10880  case DW_TAG_restrict_type:
10881  {
10882  if (!compare_as_type_dies(rdr, l, r))
10883  {
10884  SET_RESULT_TO_FALSE(result, l, r);
10885  break;
10886  }
10887 
10888  bool from_the_same_tu = false;
10889  if (!pointer_or_qual_die_of_anonymous_class_type(l)
10890  && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10891  && from_the_same_tu)
10892  {
10893  // These two typedefs, pointer, reference, or qualified
10894  // types have the same name and are defined in the same TU.
10895  // They thus ought to be the same.
10896  //
10897  // Note that pointers, reference or qualified types to
10898  // anonymous types are not taking into account here because
10899  // those always need to be structurally compared.
10900  SET_RESULT_TO_FALSE(result, l, r);
10901  break;
10902  }
10903  }
10904 
10905  {
10906  // No fancy optimization in this case. We need to
10907  // structurally compare the two DIEs.
10908  Dwarf_Die lu_type_die, ru_type_die;
10909  bool lu_is_void, ru_is_void;
10910 
10911  lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10912  ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10913 
10914  if (lu_is_void && ru_is_void)
10915  result = COMPARISON_RESULT_EQUAL;
10916  else if (lu_is_void != ru_is_void)
10917  SET_RESULT_TO_FALSE(result, l, r);
10918  else
10919  result = compare_dies(rdr, &lu_type_die, &ru_type_die,
10920  aggregates_being_compared,
10921  update_canonical_dies_on_the_fly);
10922  }
10923  break;
10924 
10925  case DW_TAG_enumeration_type:
10926  if (!compare_as_decl_and_type_dies(rdr, l, r))
10927  SET_RESULT_TO_FALSE(result, l, r);
10928  else
10929  {
10930  // Walk the enumerators.
10931  Dwarf_Die l_enumtor, r_enumtor;
10932  bool found_l_enumtor = true, found_r_enumtor = true;
10933 
10934  if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10935  for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10936  &l_enumtor) == 0,
10937  found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10938  &r_enumtor) == 0;
10939  found_l_enumtor && found_r_enumtor;
10940  found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
10941  found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
10942  {
10943  int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
10944  if ( l_tag != r_tag)
10945  {
10946  SET_RESULT_TO_FALSE(result, l, r);
10947  break;
10948  }
10949 
10950  if (l_tag != DW_TAG_enumerator)
10951  continue;
10952 
10953  uint64_t l_val = 0, r_val = 0;
10954  die_unsigned_constant_attribute(&l_enumtor,
10955  DW_AT_const_value,
10956  l_val);
10957  die_unsigned_constant_attribute(&r_enumtor,
10958  DW_AT_const_value,
10959  r_val);
10960  if (l_val != r_val)
10961  {
10962  SET_RESULT_TO_FALSE(result, l, r);
10963  break;
10964  }
10965  }
10966  if (found_l_enumtor != found_r_enumtor )
10967  SET_RESULT_TO_FALSE(result, l, r);
10968  }
10969  break;
10970 
10971  case DW_TAG_structure_type:
10972  case DW_TAG_union_type:
10973  case DW_TAG_class_type:
10974  {
10975  RETURN_IF_COMPARISON_CYCLE_DETECTED;
10976 
10977  rdr.compare_count_++;
10978 
10979  if (!compare_as_decl_and_type_dies(rdr, l, r))
10980  SET_RESULT_TO_FALSE(result, l, r);
10981  else if (rdr.options().assume_odr_for_cplusplus
10982  && rdr.odr_is_relevant(l)
10983  && rdr.odr_is_relevant(r)
10984  && !die_is_anonymous(l)
10985  && !die_is_anonymous(r))
10986  result = COMPARISON_RESULT_EQUAL;
10987  else
10988  {
10989  aggregates_being_compared.add(dies_being_compared);
10990 
10991  Dwarf_Die l_member, r_member;
10992  bool found_l_member = true, found_r_member = true;
10993 
10994  if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10995  for (found_l_member = get_member_child_die(l, &l_member),
10996  found_r_member = get_member_child_die(r, &r_member);
10997  found_l_member && found_r_member;
10998  found_l_member = get_next_member_sibling_die(&l_member,
10999  &l_member),
11000  found_r_member = get_next_member_sibling_die(&r_member,
11001  &r_member))
11002  {
11003  int l_tag = dwarf_tag(&l_member),
11004  r_tag = dwarf_tag(&r_member);
11005 
11006  if (l_tag != r_tag)
11007  {
11008  SET_RESULT_TO_FALSE(result, l, r);
11009  break;
11010  }
11011 
11012  ABG_ASSERT(l_tag == DW_TAG_member
11013  || l_tag == DW_TAG_variable
11014  || l_tag == DW_TAG_inheritance
11015  || l_tag == DW_TAG_subprogram);
11016 
11017  comparison_result local_result =
11018  compare_dies(rdr, &l_member, &r_member,
11019  aggregates_being_compared,
11020  update_canonical_dies_on_the_fly);
11021 
11022  if (local_result == COMPARISON_RESULT_UNKNOWN)
11023  // Note that if the result of comparing any
11024  // sub-type is COMPARISON_RESULT_EQUAL, just
11025  // because we have at least one sub-type's
11026  // comparison being COMPARISON_RESULT_UNKNOWN
11027  // means that the comparison of this type will
11028  // return COMPARISON_RESULT_UNKNOWN to show
11029  // callers that this type (and all the types that
11030  // depend on it) depends on a redundant type
11031  result = local_result;
11032 
11033  if (local_result == COMPARISON_RESULT_DIFFERENT)
11034  {
11035  SET_RESULT_TO_FALSE(result, l, r);
11036  break;
11037  }
11038  }
11039  if (found_l_member != found_r_member)
11040  {
11041  SET_RESULT_TO_FALSE(result, l, r);
11042  break;
11043  }
11044  }
11045  }
11046  break;
11047 
11048  case DW_TAG_array_type:
11049  {
11050  RETURN_IF_COMPARISON_CYCLE_DETECTED;
11051 
11052  aggregates_being_compared.add(dies_being_compared);
11053 
11054  rdr.compare_count_++;
11055 
11056  Dwarf_Die l_child, r_child;
11057  bool found_l_child, found_r_child;
11058  for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11059  &l_child) == 0,
11060  found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11061  &r_child) == 0;
11062  found_l_child && found_r_child;
11063  found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11064  found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11065  {
11066  int l_child_tag = dwarf_tag(&l_child),
11067  r_child_tag = dwarf_tag(&r_child);
11068  if (l_child_tag == DW_TAG_subrange_type
11069  || r_child_tag == DW_TAG_subrange_type)
11070  {
11071  result = compare_dies(rdr, &l_child, &r_child,
11072  aggregates_being_compared,
11073  update_canonical_dies_on_the_fly);
11074  if (!result)
11075  {
11076  SET_RESULT_TO_FALSE(result, l, r);
11077  break;
11078  }
11079  }
11080  }
11081  if (found_l_child != found_r_child)
11082  SET_RESULT_TO_FALSE(result, l, r);
11083  // Compare the types of the elements of the array.
11084  Dwarf_Die ltype_die, rtype_die;
11085  bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11086  bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11087  ABG_ASSERT(found_ltype && found_rtype);
11088 
11089  result = compare_dies(rdr, &ltype_die, &rtype_die,
11090  aggregates_being_compared,
11091  update_canonical_dies_on_the_fly);
11092  if (!result)
11094  }
11095  break;
11096 
11097  case DW_TAG_subrange_type:
11098  {
11099  uint64_t l_lower_bound = 0, r_lower_bound = 0,
11100  l_upper_bound = 0, r_upper_bound = 0;
11101  bool l_lower_bound_set = false, r_lower_bound_set = false,
11102  l_upper_bound_set = false, r_upper_bound_set = false;
11103 
11104  l_lower_bound_set =
11105  die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11106  r_lower_bound_set =
11107  die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11108 
11109  if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11110  l_upper_bound))
11111  {
11112  uint64_t l_count = 0;
11113  if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11114  {
11115  l_upper_bound = l_lower_bound + l_count;
11116  l_upper_bound_set = true;
11117  if (l_upper_bound)
11118  --l_upper_bound;
11119  }
11120  }
11121  else
11122  l_upper_bound_set = true;
11123 
11124  if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11125  r_upper_bound))
11126  {
11127  uint64_t r_count = 0;
11128  if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11129  {
11130  r_upper_bound = r_lower_bound + r_count;
11131  r_upper_bound_set = true;
11132  if (r_upper_bound)
11133  --r_upper_bound;
11134  }
11135  }
11136  else
11137  r_upper_bound_set = true;
11138 
11139  if ((l_lower_bound_set != r_lower_bound_set)
11140  || (l_upper_bound_set != r_upper_bound_set)
11141  || (l_lower_bound != r_lower_bound)
11142  || (l_upper_bound != r_upper_bound))
11143  SET_RESULT_TO_FALSE(result, l, r);
11144  }
11145  break;
11146 
11147  case DW_TAG_subroutine_type:
11148  case DW_TAG_subprogram:
11149  {
11150  RETURN_IF_COMPARISON_CYCLE_DETECTED;
11151 
11152  aggregates_being_compared.add(dies_being_compared);
11153 
11154  rdr.compare_count_++;
11155 
11156  if (l_tag == DW_TAG_subprogram
11157  && !fn_die_equal_by_linkage_name(rdr, l, r))
11158  {
11159  SET_RESULT_TO_FALSE(result, l, r);
11160  break;
11161  }
11162  else if (l_tag == DW_TAG_subprogram
11163  && rdr.die_is_in_c(l) && rdr.die_is_in_c(r)
11164  /*&& fn_die_equal_by_linkage_name(rdr, l, r)*/)
11165  {
11166  result = COMPARISON_RESULT_EQUAL;
11167  break;
11168  }
11169  else if (!rdr.die_is_in_c(l) && !rdr.die_is_in_c(r))
11170  {
11171  // In C, we cannot have two different functions with the
11172  // same linkage name in a given binary. But here we are
11173  // looking at DIEs that don't originate from C. So we
11174  // need to compare return types and parameter types.
11175  Dwarf_Die l_return_type, r_return_type;
11176  bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11177  l_return_type);
11178  bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11179  r_return_type);
11180  if (l_return_type_is_void != r_return_type_is_void
11181  || (!l_return_type_is_void
11182  && !compare_dies(rdr,
11183  &l_return_type, &r_return_type,
11184  aggregates_being_compared,
11185  update_canonical_dies_on_the_fly)))
11186  SET_RESULT_TO_FALSE(result, l, r);
11187  else
11188  {
11189  Dwarf_Die l_child, r_child;
11190  bool found_l_child, found_r_child;
11191  for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11192  &l_child) == 0,
11193  found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11194  &r_child) == 0;
11195  found_l_child && found_r_child;
11196  found_l_child = dwarf_siblingof(&l_child,
11197  &l_child) == 0,
11198  found_r_child = dwarf_siblingof(&r_child,
11199  &r_child)==0)
11200  {
11201  int l_child_tag = dwarf_tag(&l_child);
11202  int r_child_tag = dwarf_tag(&r_child);
11203  comparison_result local_result =
11204  COMPARISON_RESULT_EQUAL;
11205  if (l_child_tag != r_child_tag)
11206  local_result = COMPARISON_RESULT_DIFFERENT;
11207  if (l_child_tag == DW_TAG_formal_parameter)
11208  local_result =
11209  compare_dies(rdr, &l_child, &r_child,
11210  aggregates_being_compared,
11211  update_canonical_dies_on_the_fly);
11212  if (local_result == COMPARISON_RESULT_DIFFERENT)
11213  {
11214  result = local_result;
11215  SET_RESULT_TO_FALSE(result, l, r);
11216  break;
11217  }
11218  if (local_result == COMPARISON_RESULT_UNKNOWN)
11219  // Note that if the result of comparing any
11220  // sub-type is COMPARISON_RESULT_EQUAL, just
11221  // because we have at least one sub-type's
11222  // comparison being COMPARISON_RESULT_UNKNOWN
11223  // means that the comparison of this type will
11224  // return COMPARISON_RESULT_UNKNOWN to show
11225  // callers that this type (and all the types
11226  // that depend on it) depends on a redundant
11227  // type and so, can't be
11228  // canonical-type-propagated.
11229  result = local_result;
11230  }
11231  if (found_l_child != found_r_child)
11232  {
11233  SET_RESULT_TO_FALSE(result, l, r);
11234  break;
11235  }
11236  }
11237  }
11238  }
11239  break;
11240 
11241  case DW_TAG_formal_parameter:
11242  {
11243  Dwarf_Die l_type, r_type;
11244  bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11245  bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11246  if (l_type_is_void != r_type_is_void)
11247  SET_RESULT_TO_FALSE(result, l, r);
11248  else if (!l_type_is_void)
11249  {
11250  comparison_result local_result =
11251  compare_dies(rdr, &l_type, &r_type,
11252  aggregates_being_compared,
11253  update_canonical_dies_on_the_fly);
11254  SET_RESULT_TO(result, local_result, l, r);
11255  }
11256  }
11257  break;
11258 
11259  case DW_TAG_variable:
11260  case DW_TAG_member:
11261  if (compare_as_decl_dies(l, r))
11262  {
11263  // Compare the offsets of the data members
11264  if (l_tag == DW_TAG_member)
11265  {
11266  int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11267  die_member_offset(rdr, l, l_offset_in_bits);
11268  die_member_offset(rdr, r, r_offset_in_bits);
11269  if (l_offset_in_bits != r_offset_in_bits)
11270  SET_RESULT_TO_FALSE(result, l, r);
11271  }
11272  if (result)
11273  {
11274  // Compare the types of the data members or variables.
11275  Dwarf_Die l_type, r_type;
11276  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11277  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11278  comparison_result local_result =
11279  compare_dies(rdr, &l_type, &r_type,
11280  aggregates_being_compared,
11281  update_canonical_dies_on_the_fly);
11282  SET_RESULT_TO(result, local_result, l, r);
11283  }
11284  }
11285  else
11286  SET_RESULT_TO_FALSE(result, l, r);
11287  break;
11288 
11289  case DW_TAG_inheritance:
11290  {
11291  Dwarf_Die l_type, r_type;
11292  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11293  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11294  result = compare_dies(rdr, &l_type, &r_type,
11295  aggregates_being_compared,
11296  update_canonical_dies_on_the_fly);
11297  if (!result)
11298  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11299 
11300  uint64_t l_a = 0, r_a = 0;
11301  die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11302  die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11303  if (l_a != r_a)
11304  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11305 
11306  die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11307  die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11308  if (l_a != r_a)
11309  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11310 
11311  int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11312  die_member_offset(rdr, l, l_offset_in_bits);
11313  die_member_offset(rdr, r, r_offset_in_bits);
11314  if (l_offset_in_bits != r_offset_in_bits)
11315  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11316  }
11317  break;
11318 
11319  case DW_TAG_ptr_to_member_type:
11320  {
11321  bool comp_result = false;
11322  if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11323  if (!comp_result)
11324  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11325 
11326  Dwarf_Die l_type, r_type;
11327  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11328  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11329  result = compare_dies(rdr, &l_type, &r_type,
11330  aggregates_being_compared,
11331  update_canonical_dies_on_the_fly);
11332  if (!result)
11333  ABG_RETURN(result);
11334 
11335  ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11336  ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11337  result = compare_dies(rdr, &l_type, &r_type,
11338  aggregates_being_compared,
11339  update_canonical_dies_on_the_fly);
11340  if (!result)
11341  ABG_RETURN(result);
11342  }
11343  break;
11344 
11345  case DW_TAG_enumerator:
11346  case DW_TAG_packed_type:
11347  case DW_TAG_set_type:
11348  case DW_TAG_file_type:
11349  case DW_TAG_thrown_type:
11350  case DW_TAG_interface_type:
11351  case DW_TAG_shared_type:
11352  case DW_TAG_compile_unit:
11353  case DW_TAG_namespace:
11354  case DW_TAG_module:
11355  case DW_TAG_constant:
11356  case DW_TAG_partial_unit:
11357  case DW_TAG_imported_unit:
11358  case DW_TAG_dwarf_procedure:
11359  case DW_TAG_imported_declaration:
11360  case DW_TAG_entry_point:
11361  case DW_TAG_label:
11362  case DW_TAG_lexical_block:
11363  case DW_TAG_unspecified_parameters:
11364  case DW_TAG_variant:
11365  case DW_TAG_common_block:
11366  case DW_TAG_common_inclusion:
11367  case DW_TAG_inlined_subroutine:
11368  case DW_TAG_with_stmt:
11369  case DW_TAG_access_declaration:
11370  case DW_TAG_catch_block:
11371  case DW_TAG_friend:
11372  case DW_TAG_namelist:
11373  case DW_TAG_namelist_item:
11374  case DW_TAG_template_type_parameter:
11375  case DW_TAG_template_value_parameter:
11376  case DW_TAG_try_block:
11377  case DW_TAG_variant_part:
11378  case DW_TAG_imported_module:
11379  case DW_TAG_condition:
11380  case DW_TAG_type_unit:
11381  case DW_TAG_template_alias:
11382  case DW_TAG_lo_user:
11383  case DW_TAG_MIPS_loop:
11384  case DW_TAG_format_label:
11385  case DW_TAG_function_template:
11386  case DW_TAG_class_template:
11387  case DW_TAG_GNU_BINCL:
11388  case DW_TAG_GNU_EINCL:
11389  case DW_TAG_GNU_template_template_param:
11390  case DW_TAG_GNU_template_parameter_pack:
11391  case DW_TAG_GNU_formal_parameter_pack:
11392  case DW_TAG_GNU_call_site:
11393  case DW_TAG_GNU_call_site_parameter:
11394  case DW_TAG_hi_user:
11395 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11396  if (rdr.debug_die_canonicalization_is_on_)
11398 #endif
11400  break;
11401  }
11402 
11403  ABG_RETURN(result);
11404 }
11405 
11406 /// Compare two DIEs emitted by a C compiler.
11407 ///
11408 /// @param rdr the DWARF reader used to load the DWARF information.
11409 ///
11410 /// @param l the left-hand-side argument of this comparison operator.
11411 ///
11412 /// @param r the righ-hand-side argument of this comparison operator.
11413 ///
11414 /// @param update_canonical_dies_on_the_fly if yes, then this function
11415 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11416 /// comparing l and r. This helps in making so that sub-type DIEs of
11417 /// 'l' and 'r' are compared structurally only once. This is how we
11418 /// turn this exponential comparison problem into a problem that is a
11419 /// closer to a linear one.
11420 ///
11421 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11422 static comparison_result
11423 compare_dies(const reader& rdr,
11424  const Dwarf_Die *l,
11425  const Dwarf_Die *r,
11426  bool update_canonical_dies_on_the_fly)
11427 {
11428  offset_pairs_stack_type aggregates_being_compared(rdr);
11429  return compare_dies(rdr, l, r, aggregates_being_compared,
11430  update_canonical_dies_on_the_fly);
11431 }
11432 
11433 /// Compare two DIEs for the purpose of canonicalization.
11434 ///
11435 /// This is a sub-routine of reader::get_canonical_die.
11436 ///
11437 /// When DIE canonicalization debugging is on, this function performs
11438 /// both structural and canonical comparison. It expects that both
11439 /// comparison yield the same result.
11440 ///
11441 /// @param rdr the DWARF reader.
11442 ///
11443 /// @param l the left-hand-side comparison operand DIE.
11444 ///
11445 /// @param r the right-hand-side comparison operand DIE.
11446 ///
11447 /// @param update_canonical_dies_on_the_fly if true, then some
11448 /// aggregate DIEs will see their canonical types propagated.
11449 ///
11450 /// @return true iff @p l equals @p r.
11451 static bool
11452 compare_dies_during_canonicalization(reader& rdr,
11453  const Dwarf_Die *l,
11454  const Dwarf_Die *r,
11455  bool update_canonical_dies_on_the_fly)
11456 {
11457 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11458  if (rdr.debug_die_canonicalization_is_on_)
11459  {
11460  bool canonical_equality = false, structural_equality = false;
11461  rdr.use_canonical_die_comparison_ = false;
11462  structural_equality = compare_dies(rdr, l, r,
11463  /*update_canonical_dies_on_the_fly=*/false);
11464  rdr.use_canonical_die_comparison_ = true;
11465  canonical_equality = compare_dies(rdr, l, r,
11466  update_canonical_dies_on_the_fly);
11467  if (canonical_equality != structural_equality)
11468  {
11469  std::cerr << "structural & canonical equality different for DIEs: "
11470  << std::hex
11471  << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11472  << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11473  << std::dec
11474  << ", repr: '"
11475  << rdr.get_die_pretty_type_representation(l, 0)
11476  << "'"
11477  << std::endl;
11479  }
11480  return structural_equality;
11481  }
11482 #endif
11483  return compare_dies(rdr, l, r,
11484  update_canonical_dies_on_the_fly);
11485 }
11486 
11487 // ----------------------------------
11488 // </die comparison engine>
11489 // ---------------------------------
11490 
11491 /// Get the point where a DW_AT_import DIE is used to import a given
11492 /// (unit) DIE, between two DIEs.
11493 ///
11494 /// @param rdr the dwarf reader to consider.
11495 ///
11496 /// @param partial_unit_offset the imported unit for which we want to
11497 /// know the insertion point. This is usually a partial unit (with
11498 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11499 /// so.
11500 ///
11501 /// @param first_die_offset the offset of the DIE from which this
11502 /// function starts looking for the import point of
11503 /// @partial_unit_offset. Note that this offset is excluded from the
11504 /// set of potential solutions.
11505 ///
11506 /// @param first_die_cu_offset the offset of the (compilation) unit
11507 /// that @p first_die_cu_offset belongs to.
11508 ///
11509 /// @param source where the DIE of first_die_cu_offset unit comes
11510 /// from.
11511 ///
11512 /// @param last_die_offset the offset of the last DIE of the up to
11513 /// which this function looks for the import point of @p
11514 /// partial_unit_offset. Note that this offset is excluded from the
11515 /// set of potential solutions.
11516 ///
11517 /// @param imported_point_offset. The resulting
11518 /// imported_point_offset. Note that if the imported DIE @p
11519 /// partial_unit_offset is not found between @p first_die_offset and
11520 /// @p last_die_offset, this parameter is left untouched by this
11521 /// function.
11522 ///
11523 /// @return true iff an imported unit is found between @p
11524 /// first_die_offset and @p last_die_offset.
11525 static bool
11526 find_import_unit_point_between_dies(const reader& rdr,
11527  size_t partial_unit_offset,
11528  Dwarf_Off first_die_offset,
11529  Dwarf_Off first_die_cu_offset,
11530  die_source source,
11531  size_t last_die_offset,
11532  size_t& imported_point_offset)
11533 {
11534  const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11535  rdr.tu_die_imported_unit_points_map(source);
11536 
11537  tu_die_imported_unit_points_map_type::const_iterator iter =
11538  tu_die_imported_unit_points_map.find(first_die_cu_offset);
11539 
11540  ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11541 
11542  const imported_unit_points_type& imported_unit_points = iter->second;
11543  if (imported_unit_points.empty())
11544  return false;
11545 
11546  imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11547  imported_unit_points_type::const_iterator e = imported_unit_points.end();
11548 
11549  find_lower_bound_in_imported_unit_points(imported_unit_points,
11550  first_die_offset,
11551  b);
11552 
11553  if (last_die_offset != static_cast<size_t>(-1))
11554  find_lower_bound_in_imported_unit_points(imported_unit_points,
11555  last_die_offset,
11556  e);
11557 
11558  if (e != imported_unit_points.end())
11559  {
11560  for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11561  if (i->imported_unit_die_off == partial_unit_offset)
11562  {
11563  imported_point_offset = i->offset_of_import ;
11564  return true;
11565  }
11566 
11567  for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11568  {
11569  if (find_import_unit_point_between_dies(rdr,
11570  partial_unit_offset,
11571  i->imported_unit_child_off,
11572  i->imported_unit_cu_off,
11573  i->imported_unit_die_source,
11574  /*(Dwarf_Off)*/-1,
11575  imported_point_offset))
11576  return true;
11577  }
11578  }
11579  else
11580  {
11581  for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11582  if (i->imported_unit_die_off == partial_unit_offset)
11583  {
11584  imported_point_offset = i->offset_of_import ;
11585  return true;
11586  }
11587 
11588  for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11589  {
11590  if (find_import_unit_point_between_dies(rdr,
11591  partial_unit_offset,
11592  i->imported_unit_child_off,
11593  i->imported_unit_cu_off,
11594  i->imported_unit_die_source,
11595  /*(Dwarf_Off)*/-1,
11596  imported_point_offset))
11597  return true;
11598  }
11599  }
11600 
11601  return false;
11602 }
11603 
11604 /// In the current translation unit, get the last point where a
11605 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
11606 /// given DIE is found. That given DIE is called the limit DIE.
11607 ///
11608 /// Said otherwise, this function returns the last import point of a
11609 /// unit, before a limit.
11610 ///
11611 /// @param rdr the dwarf reader to consider.
11612 ///
11613 /// @param partial_unit_offset the imported unit for which we want to
11614 /// know the insertion point of. This is usually a partial unit (with
11615 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11616 /// so.
11617 ///
11618 /// @param where_offset the offset of the limit DIE.
11619 ///
11620 /// @param imported_point_offset. The resulting imported_point_offset.
11621 /// Note that if the imported DIE @p partial_unit_offset is not found
11622 /// before @p die_offset, this is set to the last @p
11623 /// partial_unit_offset found under @p parent_die.
11624 ///
11625 /// @return true iff an imported unit is found before @p die_offset.
11626 /// Note that if an imported unit is found after @p die_offset then @p
11627 /// imported_point_offset is set and the function return false.
11628 static bool
11629 find_import_unit_point_before_die(const reader& rdr,
11630  size_t partial_unit_offset,
11631  size_t where_offset,
11632  size_t& imported_point_offset)
11633 {
11634  size_t import_point_offset = 0;
11635  Dwarf_Die first_die_of_tu;
11636 
11637  if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11638  &first_die_of_tu) != 0)
11639  return false;
11640 
11641  Dwarf_Die cu_die_memory;
11642  Dwarf_Die *cu_die;
11643 
11644  cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11645  &cu_die_memory, 0, 0);
11646 
11647  if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11648  dwarf_dieoffset(&first_die_of_tu),
11649  dwarf_dieoffset(cu_die),
11650  /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11651  where_offset,
11652  import_point_offset))
11653  {
11654  imported_point_offset = import_point_offset;
11655  return true;
11656  }
11657 
11658  if (import_point_offset)
11659  {
11660  imported_point_offset = import_point_offset;
11661  return true;
11662  }
11663 
11664  return false;
11665 }
11666 
11667 /// Return the parent DIE for a given DIE.
11668 ///
11669 /// Note that the function build_die_parent_map() must have been
11670 /// called before this one can work. This function either succeeds or
11671 /// aborts the current process.
11672 ///
11673 /// @param rdr the DWARF reader to consider.
11674 ///
11675 /// @param die the DIE for which we want the parent.
11676 ///
11677 /// @param parent_die the output parameter set to the parent die of
11678 /// @p die. Its memory must be allocated and handled by the caller.
11679 ///
11680 /// @param where_offset the offset of the DIE where we are "logically"
11681 /// positionned at, in the DIE tree. This is useful when @p die is
11682 /// e.g, DW_TAG_partial_unit that can be included in several places in
11683 /// the DIE tree.
11684 ///
11685 /// @return true if the function could get a parent DIE, false
11686 /// otherwise.
11687 static bool
11688 get_parent_die(const reader& rdr,
11689  const Dwarf_Die* die,
11690  Dwarf_Die& parent_die,
11691  size_t where_offset)
11692 {
11693  ABG_ASSERT(rdr.dwarf_debug_info());
11694 
11695  const die_source source = rdr.get_die_source(die);
11696 
11697  const offset_offset_map_type& m = rdr.die_parent_map(source);
11698  offset_offset_map_type::const_iterator i =
11699  m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11700 
11701  if (i == m.end())
11702  return false;
11703 
11704  switch (source)
11705  {
11706  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11707  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11708  i->second, &parent_die));
11709  break;
11710  case ALT_DEBUG_INFO_DIE_SOURCE:
11711  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11712  i->second, &parent_die));
11713  break;
11714  case TYPE_UNIT_DIE_SOURCE:
11715  ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11716  i->second, &parent_die));
11717  break;
11718  case NO_DEBUG_INFO_DIE_SOURCE:
11719  case NUMBER_OF_DIE_SOURCES:
11721  }
11722 
11723  if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11724  {
11725  if (where_offset == 0)
11726  {
11727  parent_die = *rdr.cur_tu_die();
11728  return true;
11729  }
11730  size_t import_point_offset = 0;
11731  bool found =
11732  find_import_unit_point_before_die(rdr,
11733  dwarf_dieoffset(&parent_die),
11734  where_offset,
11735  import_point_offset);
11736  if (!found)
11737  // It looks like parent_die (which comes from the alternate
11738  // debug info file) hasn't been imported into this TU. So,
11739  // Let's assume its logical parent is the DIE of the current
11740  // TU.
11741  parent_die = *rdr.cur_tu_die();
11742  else
11743  {
11744  ABG_ASSERT(import_point_offset);
11745  Dwarf_Die import_point_die;
11746  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11747  import_point_offset,
11748  &import_point_die));
11749  return get_parent_die(rdr, &import_point_die,
11750  parent_die, where_offset);
11751  }
11752  }
11753 
11754  return true;
11755 }
11756 
11757 /// Get the DIE representing the scope of a given DIE.
11758 ///
11759 /// Please note that when the DIE we are looking at has a
11760 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11761 /// DIE is the parent DIE of the DIE referred to by that attribute.
11762 /// This is the only case where a scope DIE is different from the
11763 /// parent DIE of a given DIE.
11764 ///
11765 /// Also note that if the current translation unit is from C, then
11766 /// this returns the global scope.
11767 ///
11768 /// @param rdr the DWARF reader to use.
11769 ///
11770 /// @param die the DIE to consider.
11771 ///
11772 /// @param where_offset where we are logically at in the DIE stream.
11773 ///
11774 /// @param scope_die out parameter. This is set to the resulting
11775 /// scope DIE iff the function returns true.
11776 static bool
11777 get_scope_die(const reader& rdr,
11778  const Dwarf_Die* die,
11779  size_t where_offset,
11780  Dwarf_Die& scope_die)
11781 {
11782  if (is_c_language(rdr.cur_transl_unit()->get_language()))
11783  {
11784  ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11785  return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11786  }
11787 
11788  Dwarf_Die logical_parent_die;
11789  if (die_die_attribute(die, DW_AT_specification,
11790  logical_parent_die, false)
11791  || die_die_attribute(die, DW_AT_abstract_origin,
11792  logical_parent_die, false))
11793  return get_scope_die(rdr, &logical_parent_die, where_offset, scope_die);
11794 
11795  if (!get_parent_die(rdr, die, scope_die, where_offset))
11796  return false;
11797 
11798  if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11799  || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11800  || dwarf_tag(&scope_die) == DW_TAG_array_type)
11801  return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11802 
11803  return true;
11804 }
11805 
11806 /// Return the abigail IR node representing the scope of a given DIE.
11807 ///
11808 /// Note that it is the logical scope that is returned. That is, if
11809 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11810 /// attribute, it's the scope of the referred-to DIE (via these
11811 /// attributes) that is returned.
11812 ///
11813 /// Also note that if the current translation unit is from C, then
11814 /// this returns the global scope.
11815 ///
11816 /// @param rdr the dwarf reader to use.
11817 ///
11818 /// @param die the DIE to get the scope for.
11819 ///
11820 /// @param called_from_public_decl is true if this function has been
11821 /// initially called within the context of a public decl.
11822 ///
11823 /// @param where_offset the offset of the DIE where we are "logically"
11824 /// positionned at, in the DIE tree. This is useful when @p die is
11825 /// e.g, DW_TAG_partial_unit that can be included in several places in
11826 /// the DIE tree.
11827 static scope_decl_sptr
11828 get_scope_for_die(reader& rdr,
11829  Dwarf_Die* die,
11830  bool called_for_public_decl,
11831  size_t where_offset)
11832 {
11833  const die_source source_of_die = rdr.get_die_source(die);
11834 
11835  translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11836  rdr.get_die_language(die, die_lang);
11837  if (is_c_language(die_lang)
11838  || rdr.die_parent_map(source_of_die).empty())
11839  {
11840  // In units for the C languages all decls belong to the global
11841  // namespace. This is generally the case if Libabigail
11842  // determined that no DIE -> parent map was needed.
11843  ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11844  return rdr.global_scope();
11845  }
11846 
11847  Dwarf_Die cloned_die;
11848  if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
11849  || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
11850  return get_scope_for_die(rdr, &cloned_die,
11851  called_for_public_decl,
11852  where_offset);
11853 
11854  Dwarf_Die parent_die;
11855 
11856  if (!get_parent_die(rdr, die, parent_die, where_offset))
11857  return rdr.nil_scope();
11858 
11859  if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11860  || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11861  || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11862  {
11863  if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11864  || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11865  {
11866  ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11867  || source_of_die == TYPE_UNIT_DIE_SOURCE);
11868  return rdr.cur_transl_unit()->get_global_scope();
11869  }
11870 
11871  // For top level DIEs like DW_TAG_compile_unit, we just want to
11872  // return the global scope for the corresponding translation
11873  // unit. This must have been set by
11874  // build_translation_unit_and_add_to_ir if we already started to
11875  // build the translation unit of parent_die. Otherwise, just
11876  // return the global scope of the current translation unit.
11877  die_tu_map_type::const_iterator i =
11878  rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
11879  if (i != rdr.die_tu_map().end())
11880  return i->second->get_global_scope();
11881  return rdr.cur_transl_unit()->get_global_scope();
11882  }
11883 
11884  scope_decl_sptr s;
11886  if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11887  || dwarf_tag(&parent_die) == DW_TAG_array_type
11888  || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
11889  // this is an entity defined in a scope that is a function.
11890  // Normally, I would say that this should be dropped. But I have
11891  // seen a case where a typedef DIE needed by a function parameter
11892  // was defined right before the parameter, under the scope of the
11893  // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
11894  // the function parm too. So for that case, let's say that the
11895  // scope is the scope of the function itself. Note that this is
11896  // an error of the DWARF emitter. We should never see this DIE in
11897  // this context.
11898  {
11899  scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
11900  called_for_public_decl,
11901  where_offset);
11902  if (is_anonymous_type_die(die))
11903  // For anonymous type that have nothing to do in a function or
11904  // array type context, let's put it in the containing
11905  // namespace. That is, do not let it be in a containing class
11906  // or union where it has nothing to do.
11907  while (is_class_or_union_type(s))
11908  {
11909  if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
11910  return rdr.nil_scope();
11911  s = get_scope_for_die(rdr, &parent_die,
11912  called_for_public_decl,
11913  where_offset);
11914  }
11915  return s;
11916  }
11917  else
11918  d = build_ir_node_from_die(rdr, &parent_die,
11919  called_for_public_decl,
11920  where_offset);
11921  s = dynamic_pointer_cast<scope_decl>(d);
11922  if (!s)
11923  // this is an entity defined in someting that is not a scope.
11924  // Let's drop it.
11925  return rdr.nil_scope();
11926 
11927  class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11928  if (cl && cl->get_is_declaration_only())
11929  {
11930  scope_decl_sptr scop =
11931  dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
11932  if (scop)
11933  s = scop;
11934  else
11935  s = cl;
11936  }
11937  return s;
11938 }
11939 
11940 /// Convert a DWARF constant representing the value of the
11941 /// DW_AT_language property into the translation_unit::language
11942 /// enumerator.
11943 ///
11944 /// @param l the DWARF constant to convert.
11945 ///
11946 /// @return the resulting translation_unit::language enumerator.
11948 dwarf_language_to_tu_language(size_t l)
11949 {
11950  switch (l)
11951  {
11952  case DW_LANG_C89:
11953  return translation_unit::LANG_C89;
11954  case DW_LANG_C:
11955  return translation_unit::LANG_C;
11956  case DW_LANG_Ada83:
11957  return translation_unit::LANG_Ada83;
11958  case DW_LANG_C_plus_plus:
11959  return translation_unit::LANG_C_plus_plus;
11960  case DW_LANG_Cobol74:
11961  return translation_unit::LANG_Cobol74;
11962  case DW_LANG_Cobol85:
11963  return translation_unit::LANG_Cobol85;
11964  case DW_LANG_Fortran77:
11965  return translation_unit::LANG_Fortran77;
11966  case DW_LANG_Fortran90:
11967  return translation_unit::LANG_Fortran90;
11968  case DW_LANG_Pascal83:
11969  return translation_unit::LANG_Pascal83;
11970  case DW_LANG_Modula2:
11971  return translation_unit::LANG_Modula2;
11972  case DW_LANG_Java:
11973  return translation_unit::LANG_Java;
11974  case DW_LANG_C99:
11975  return translation_unit::LANG_C99;
11976  case DW_LANG_Ada95:
11977  return translation_unit::LANG_Ada95;
11978  case DW_LANG_Fortran95:
11979  return translation_unit::LANG_Fortran95;
11980  case DW_LANG_PLI:
11981  return translation_unit::LANG_PLI;
11982  case DW_LANG_ObjC:
11983  return translation_unit::LANG_ObjC;
11984  case DW_LANG_ObjC_plus_plus:
11985  return translation_unit::LANG_ObjC_plus_plus;
11986 
11987 #ifdef HAVE_DW_LANG_Rust_enumerator
11988  case DW_LANG_Rust:
11989  return translation_unit::LANG_Rust;
11990 #endif
11991 
11992 #ifdef HAVE_DW_LANG_UPC_enumerator
11993  case DW_LANG_UPC:
11994  return translation_unit::LANG_UPC;
11995 #endif
11996 
11997 #ifdef HAVE_DW_LANG_D_enumerator
11998  case DW_LANG_D:
11999  return translation_unit::LANG_D;
12000 #endif
12001 
12002 #ifdef HAVE_DW_LANG_Python_enumerator
12003  case DW_LANG_Python:
12004  return translation_unit::LANG_Python;
12005 #endif
12006 
12007 #ifdef HAVE_DW_LANG_Go_enumerator
12008  case DW_LANG_Go:
12009  return translation_unit::LANG_Go;
12010 #endif
12011 
12012 #ifdef HAVE_DW_LANG_C11_enumerator
12013  case DW_LANG_C11:
12014  return translation_unit::LANG_C11;
12015 #endif
12016 
12017 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12018  case DW_LANG_C_plus_plus_03:
12019  return translation_unit::LANG_C_plus_plus_03;
12020 #endif
12021 
12022 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12023  case DW_LANG_C_plus_plus_11:
12024  return translation_unit::LANG_C_plus_plus_11;
12025 #endif
12026 
12027 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12028  case DW_LANG_C_plus_plus_14:
12029  return translation_unit::LANG_C_plus_plus_14;
12030 #endif
12031 
12032 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12033  case DW_LANG_Mips_Assembler:
12034  return translation_unit::LANG_Mips_Assembler;
12035 #endif
12036 
12037  default:
12038  return translation_unit::LANG_UNKNOWN;
12039  }
12040 }
12041 
12042 /// Get the default array lower bound value as defined by the DWARF
12043 /// specification, version 4, depending on the language of the
12044 /// translation unit.
12045 ///
12046 /// @param l the language of the translation unit.
12047 ///
12048 /// @return the default array lower bound value.
12049 static uint64_t
12050 get_default_array_lower_bound(translation_unit::language l)
12051 {
12052  int value = 0;
12053  switch (l)
12054  {
12055  case translation_unit::LANG_UNKNOWN:
12056  value = 0;
12057  break;
12058  case translation_unit::LANG_Cobol74:
12059  case translation_unit::LANG_Cobol85:
12060  value = 1;
12061  break;
12062  case translation_unit::LANG_C89:
12063  case translation_unit::LANG_C99:
12064  case translation_unit::LANG_C11:
12065  case translation_unit::LANG_C:
12066  case translation_unit::LANG_C_plus_plus_03:
12067  case translation_unit::LANG_C_plus_plus_11:
12068  case translation_unit::LANG_C_plus_plus_14:
12069  case translation_unit::LANG_C_plus_plus:
12070  case translation_unit::LANG_ObjC:
12071  case translation_unit::LANG_ObjC_plus_plus:
12072  case translation_unit::LANG_Rust:
12073  value = 0;
12074  break;
12075  case translation_unit::LANG_Fortran77:
12076  case translation_unit::LANG_Fortran90:
12077  case translation_unit::LANG_Fortran95:
12078  case translation_unit::LANG_Ada83:
12079  case translation_unit::LANG_Ada95:
12080  case translation_unit::LANG_Pascal83:
12081  case translation_unit::LANG_Modula2:
12082  value = 1;
12083  break;
12084  case translation_unit::LANG_Java:
12085  value = 0;
12086  break;
12087  case translation_unit::LANG_PLI:
12088  value = 1;
12089  break;
12090  case translation_unit::LANG_UPC:
12091  case translation_unit::LANG_D:
12092  case translation_unit::LANG_Python:
12093  case translation_unit::LANG_Go:
12094  case translation_unit::LANG_Mips_Assembler:
12095  value = 0;
12096  break;
12097  }
12098 
12099  return value;
12100 }
12101 
12102 /// For a given offset, find the lower bound of a sorted vector of
12103 /// imported unit point offset.
12104 ///
12105 /// The lower bound is the smallest point (the point with the smallest
12106 /// offset) which is the greater than a given offset.
12107 ///
12108 /// @param imported_unit_points_type the sorted vector of imported
12109 /// unit points.
12110 ///
12111 /// @param val the offset to consider when looking for the lower
12112 /// bound.
12113 ///
12114 /// @param r an iterator to the lower bound found. This parameter is
12115 /// set iff the function returns true.
12116 ///
12117 /// @return true iff the lower bound has been found.
12118 static bool
12119 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12120  Dwarf_Off val,
12121  imported_unit_points_type::const_iterator& r)
12122 {
12123  imported_unit_point v(val);
12124  imported_unit_points_type::const_iterator result =
12125  std::lower_bound(p.begin(), p.end(), v);
12126 
12127  bool is_ok = result != p.end();
12128 
12129  if (is_ok)
12130  r = result;
12131 
12132  return is_ok;
12133 }
12134 
12135 /// Given a DW_TAG_compile_unit, build and return the corresponding
12136 /// abigail::translation_unit ir node. Note that this function
12137 /// recursively reads the children dies of the current DIE and
12138 /// populates the resulting translation unit.
12139 ///
12140 /// @param rdr the DWARF reader to use.
12141 ///
12142 /// @param die the DW_TAG_compile_unit DIE to consider.
12143 ///
12144 /// @param address_size the size of the addresses expressed in this
12145 /// translation unit in general.
12146 ///
12147 /// @return a pointer to the resulting translation_unit.
12148 static translation_unit_sptr
12149 build_translation_unit_and_add_to_ir(reader& rdr,
12150  Dwarf_Die* die,
12151  char address_size)
12152 {
12153  translation_unit_sptr result;
12154 
12155  if (!die)
12156  return result;
12157  ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12158 
12159  // Clear the part of the context that is dependent on the translation
12160  // unit we are reading.
12161  rdr.clear_per_translation_unit_data();
12162 
12163  rdr.cur_tu_die(die);
12164 
12165  string path = die_string_attribute(die, DW_AT_name);
12166  if (path == "<artificial>")
12167  {
12168  // This is a file artificially generated by the compiler, so its
12169  // name is '<artificial>'. As we want all different translation
12170  // units to have unique path names, let's suffix this path name
12171  // with its die offset.
12172  std::ostringstream o;
12173  o << path << "-" << std::hex << dwarf_dieoffset(die);
12174  path = o.str();
12175  }
12176  string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12177 
12178  // See if the same translation unit exits already in the current
12179  // corpus. Sometimes, the same translation unit can be present
12180  // several times in the same debug info. The content of the
12181  // different instances of the translation unit are different. So to
12182  // represent that, we are going to re-use the same translation
12183  // unit. That is, it's going to be the union of all the translation
12184  // units of the same path.
12185  {
12186  const string& abs_path =
12187  compilation_dir.empty() ? path : compilation_dir + "/" + path;
12188  result = rdr.corpus()->find_translation_unit(abs_path);
12189  }
12190 
12191  if (!result)
12192  {
12193  result.reset(new translation_unit(rdr.env(),
12194  path,
12195  address_size));
12196  result->set_compilation_dir_path(compilation_dir);
12197  rdr.corpus()->add(result);
12198  uint64_t l = 0;
12199  die_unsigned_constant_attribute(die, DW_AT_language, l);
12200  result->set_language(dwarf_language_to_tu_language(l));
12201  }
12202 
12203  rdr.cur_transl_unit(result);
12204  rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12205 
12206  Dwarf_Die child;
12207  if (dwarf_child(die, &child) != 0)
12208  return result;
12209 
12210  result->set_is_constructed(false);
12211 
12212  do
12213  // Analyze all the DIEs we encounter unless we are asked to only
12214  // analyze exported interfaces and the types reachables from them.
12215  if (!rdr.env().analyze_exported_interfaces_only()
12216  || rdr.is_decl_die_with_exported_symbol(&child))
12217  build_ir_node_from_die(rdr, &child,
12218  die_is_public_decl(&child),
12219  dwarf_dieoffset(&child));
12220  while (dwarf_siblingof(&child, &child) == 0);
12221 
12222  if (!rdr.var_decls_to_re_add_to_tree().empty())
12223  for (list<var_decl_sptr>::const_iterator v =
12224  rdr.var_decls_to_re_add_to_tree().begin();
12225  v != rdr.var_decls_to_re_add_to_tree().end();
12226  ++v)
12227  {
12228  if (is_member_decl(*v))
12229  continue;
12230 
12231  ABG_ASSERT((*v)->get_scope());
12232  string demangled_name =
12233  demangle_cplus_mangled_name((*v)->get_linkage_name());
12234  if (!demangled_name.empty())
12235  {
12236  std::list<string> fqn_comps;
12237  fqn_to_components(demangled_name, fqn_comps);
12238  string mem_name = fqn_comps.back();
12239  fqn_comps.pop_back();
12240  class_decl_sptr class_type;
12241  string ty_name;
12242  if (!fqn_comps.empty())
12243  {
12244  ty_name = components_to_type_name(fqn_comps);
12245  class_type =
12246  lookup_class_type(ty_name, *rdr.cur_transl_unit());
12247  }
12248  if (class_type)
12249  {
12250  // So we are seeing a member variable for which there
12251  // is a global variable definition DIE not having a
12252  // reference attribute pointing back to the member
12253  // variable declaration DIE. Thus remove the global
12254  // variable definition from its current non-class
12255  // scope ...
12256  decl_base_sptr d;
12257  if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12258  // This is the data member with the same name in cl.
12259  // We just need to flag it as static.
12260  ;
12261  else
12262  {
12263  // In this case there is no data member with the
12264  // same name in cl already. Let's add it there then
12265  // ...
12267  d = add_decl_to_scope(*v, class_type);
12268  }
12269 
12270  ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12271  // Let's flag the data member as static.
12272  set_member_is_static(d, true);
12273  }
12274  }
12275  }
12276  rdr.var_decls_to_re_add_to_tree().clear();
12277 
12278  result->set_is_constructed(true);
12279 
12280  return result;
12281 }
12282 
12283 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12284 /// DW_TAG_module (for fortran) DIE.
12285 ///
12286 /// Note that this function connects the DW_TAG_namespace to the IR
12287 /// being currently created, reads the children of the DIE and
12288 /// connects them to the IR as well.
12289 ///
12290 /// @param rdr the DWARF reader to use.
12291 ///
12292 /// @param die the DIE to read from. Must be either DW_TAG_namespace
12293 /// or DW_TAG_module.
12294 ///
12295 /// @param where_offset the offset of the DIE where we are "logically"
12296 /// positionned at, in the DIE tree. This is useful when @p die is
12297 /// e.g, DW_TAG_partial_unit that can be included in several places in
12298 /// the DIE tree.
12299 ///
12300 /// @return the resulting @ref abigail::namespace_decl or NULL if it
12301 /// couldn't be created.
12302 static namespace_decl_sptr
12303 build_namespace_decl_and_add_to_ir(reader& rdr,
12304  Dwarf_Die* die,
12305  size_t where_offset)
12306 {
12307  namespace_decl_sptr result;
12308 
12309  if (!die)
12310  return result;
12311 
12312  unsigned tag = dwarf_tag(die);
12313  if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12314  return result;
12315 
12316  scope_decl_sptr scope = get_scope_for_die(rdr, die,
12317  /*called_for_public_decl=*/false,
12318  where_offset);
12319 
12320  string name, linkage_name;
12321  location loc;
12322  die_loc_and_name(rdr, die, loc, name, linkage_name);
12323 
12324  result.reset(new namespace_decl(rdr.env(), name, loc));
12325  add_decl_to_scope(result, scope.get());
12326  rdr.associate_die_to_decl(die, result, where_offset);
12327 
12328  Dwarf_Die child;
12329  if (dwarf_child(die, &child) != 0)
12330  return result;
12331 
12332  rdr.scope_stack().push(result.get());
12333  do
12334  build_ir_node_from_die(rdr, &child,
12335  // If this namespace DIE is private
12336  // (anonymous) then all its content is
12337  // considered private. Otherwise, its
12338  // public decls are considered public.
12339  /*called_from_public_decl=*/
12340  die_is_public_decl(die) && die_is_public_decl(&child),
12341  where_offset);
12342  while (dwarf_siblingof(&child, &child) == 0);
12343  rdr.scope_stack().pop();
12344 
12345  return result;
12346 }
12347 
12348 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12349 ///
12350 /// @param rdr the DWARF reader to use.
12351 ///
12352 /// @param die the DW_TAG_base_type to consider.
12353 ///
12354 /// @param where_offset where we are logically at in the DIE stream.
12355 ///
12356 /// @return the resulting decl_base_sptr.
12357 static type_decl_sptr
12358 build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12359 {
12360  type_decl_sptr result;
12361 
12362  if (!die)
12363  return result;
12364  ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12365 
12366  uint64_t byte_size = 0, bit_size = 0;
12367  if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12368  if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12369  return result;
12370 
12371  if (bit_size == 0 && byte_size != 0)
12372  // Update the bit size.
12373  bit_size = byte_size * 8;
12374 
12375  string type_name, linkage_name;
12376  location loc;
12377  die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12378 
12379  if (byte_size == 0)
12380  {
12381  // The size of the type is zero, that must mean that we are
12382  // looking at the definition of the void type.
12383  if (type_name == "void")
12384  result = is_type_decl(build_ir_node_for_void_type(rdr));
12385  else
12386  // A type of size zero that is not void? Hmmh, I am not sure
12387  // what that means. Return nil for now.
12388  return result;
12389  }
12390 
12391  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12392  {
12393  string normalized_type_name = type_name;
12394  integral_type int_type;
12395  if (parse_integral_type(type_name, int_type))
12396  normalized_type_name = int_type.to_string();
12397  result = lookup_basic_type(normalized_type_name, *corp);
12398  }
12399 
12400  if (!result)
12401  if (corpus_sptr corp = rdr.corpus())
12402  result = lookup_basic_type(type_name, *corp);
12403  if (!result)
12404  result.reset(new type_decl(rdr.env(), type_name, bit_size,
12405  /*alignment=*/0, loc, linkage_name));
12406  rdr.associate_die_to_type(die, result, where_offset);
12407  return result;
12408 }
12409 
12410 /// Construct the type that is to be used as the underlying type of an
12411 /// enum.
12412 ///
12413 /// @param rdr the DWARF reader to use.
12414 ///
12415 /// @param enum_name the name of the enum that this type is going to
12416 /// be the underlying type of.
12417 ///
12418 /// @param enum_size the size of the enum.
12419 ///
12420 /// @param is_anonymous whether the underlying type is anonymous or
12421 /// not. By default, this should be set to true as before c++11 (and
12422 /// in C), it's almost the case.
12423 static type_decl_sptr
12424 build_enum_underlying_type(reader& rdr,
12425  string enum_name,
12426  uint64_t enum_size,
12427  bool is_anonymous = true)
12428 {
12429  string underlying_type_name =
12430  build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12431  enum_size);
12432 
12433  type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12434  enum_size, enum_size, location()));
12435  result->set_is_anonymous(is_anonymous);
12436  result->set_is_artificial(true);
12437  translation_unit_sptr tu = rdr.cur_transl_unit();
12438  decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12439  result = dynamic_pointer_cast<type_decl>(d);
12440  ABG_ASSERT(result);
12441  canonicalize(result);
12442  return result;
12443 }
12444 
12445 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12446 ///
12447 /// @param rdr the DWARF reader to use.
12448 ///
12449 /// @param die the DIE to read from.
12450 ///
12451 /// @param scope the scope of the final enum. Note that this function
12452 /// does *NOT* add the built type to this scope. The scope is just so
12453 /// that the function knows how to name anonymous enums.
12454 ///
12455 /// @param is_declaration_only is true if the DIE denoted by @p die is
12456 /// a declaration-only DIE.
12457 ///
12458 /// @return the built enum_type_decl or NULL if it could not be built.
12459 static enum_type_decl_sptr
12460 build_enum_type(reader& rdr,
12461  Dwarf_Die* die,
12462  scope_decl* scope,
12463  size_t where_offset,
12464  bool is_declaration_only)
12465 {
12466  enum_type_decl_sptr result;
12467  if (!die)
12468  return result;
12469 
12470  unsigned tag = dwarf_tag(die);
12471  if (tag != DW_TAG_enumeration_type)
12472  return result;
12473 
12474  string name, linkage_name;
12475  location loc;
12476  die_loc_and_name(rdr, die, loc, name, linkage_name);
12477 
12478  bool is_anonymous = false;
12479  // If the enum is anonymous, let's give it a name.
12480  if (name.empty())
12481  {
12482  name = get_internal_anonymous_die_prefix_name(die);
12483  ABG_ASSERT(!name.empty());
12484  // But we remember that the type is anonymous.
12485  is_anonymous = true;
12486 
12487  if (size_t s = scope->get_num_anonymous_member_enums())
12488  name = build_internal_anonymous_die_name(name, s);
12489  }
12490 
12491  bool use_odr = rdr.odr_is_relevant(die);
12492  // If the type has location, then associate it to its
12493  // representation. This way, all occurences of types with the same
12494  // representation (name) and location can be later detected as being
12495  // for the same type.
12496 
12497  if (!is_anonymous)
12498  {
12499  if (use_odr)
12500  {
12501  if (enum_type_decl_sptr pre_existing_enum =
12502  is_enum_type(rdr.lookup_artifact_from_die(die)))
12503  result = pre_existing_enum;
12504  }
12505  else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12506  {
12507  if (loc)
12508  result = lookup_enum_type_per_location(loc.expand(), *corp);
12509  }
12510  else if (loc)
12511  {
12512  if (enum_type_decl_sptr pre_existing_enum =
12513  is_enum_type(rdr.lookup_artifact_from_die(die)))
12514  if (pre_existing_enum->get_location() == loc)
12515  result = pre_existing_enum;
12516  }
12517 
12518  if (result)
12519  {
12520  rdr.associate_die_to_type(die, result, where_offset);
12521  return result;
12522  }
12523  }
12524  // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12525  // we can look them up?
12526 
12527  uint64_t size = 0;
12528  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12529  size *= 8;
12530  bool is_artificial = die_is_artificial(die);
12531 
12532  // for now we consider that underlying types of enums are all anonymous
12533  bool enum_underlying_type_is_anonymous= true;
12534 
12536  Dwarf_Die child;
12537  if (dwarf_child(die, &child) == 0)
12538  {
12539  do
12540  {
12541  if (dwarf_tag(&child) != DW_TAG_enumerator)
12542  continue;
12543 
12544  string n, m;
12545  location l;
12546  die_loc_and_name(rdr, &child, l, n, m);
12547  uint64_t val = 0;
12548  die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12549  enms.push_back(enum_type_decl::enumerator(n, val));
12550  }
12551  while (dwarf_siblingof(&child, &child) == 0);
12552  }
12553 
12554  // DWARF up to version 4 (at least) doesn't seem to carry the
12555  // underlying type, so let's create an artificial one here, which
12556  // sole purpose is to be passed to the constructor of the
12557  // enum_type_decl type.
12558  type_decl_sptr t =
12559  build_enum_underlying_type(rdr, name, size,
12560  enum_underlying_type_is_anonymous);
12561  t->set_is_declaration_only(is_declaration_only);
12562 
12563  result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12564  result->set_is_anonymous(is_anonymous);
12565  result->set_is_declaration_only(is_declaration_only);
12566  result->set_is_artificial(is_artificial);
12567  rdr.associate_die_to_type(die, result, where_offset);
12568 
12569  rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12570 
12571  return result;
12572 }
12573 
12574 /// Once a function_decl has been built and added to a class as a
12575 /// member function, this function updates the information of the
12576 /// function_decl concerning the properties of its relationship with
12577 /// the member class. That is, it updates properties like
12578 /// virtualness, access, constness, cdtorness, etc ...
12579 ///
12580 /// @param die the DIE of the function_decl that has been just built.
12581 ///
12582 /// @param f the function_decl that has just been built from @p die.
12583 ///
12584 /// @param klass the @ref class_or_union that @p f belongs to.
12585 ///
12586 /// @param rdr the context used to read the ELF/DWARF information.
12587 static void
12588 finish_member_function_reading(Dwarf_Die* die,
12589  const function_decl_sptr& f,
12590  const class_or_union_sptr klass,
12591  reader& rdr)
12592 {
12593  ABG_ASSERT(klass);
12594 
12595  method_decl_sptr m = is_method_decl(f);
12596  ABG_ASSERT(m);
12597 
12598  method_type_sptr method_t = is_method_type(m->get_type());
12599  ABG_ASSERT(method_t);
12600 
12601  bool is_ctor = (f->get_name() == klass->get_name());
12602  bool is_dtor = (!f->get_name().empty()
12603  && static_cast<string>(f->get_name())[0] == '~');
12604  bool is_virtual = die_is_virtual(die);
12605  int64_t vindex = -1;
12606  if (is_virtual)
12607  die_virtual_function_index(die, vindex);
12608  access_specifier access = public_access;
12609  if (class_decl_sptr c = is_class_type(klass))
12610  if (!c->is_struct())
12611  access = private_access;
12612  die_access_specifier(die, access);
12613 
12614  bool is_static = false;
12615  {
12616  // Let's see if the first parameter is a pointer to an instance of
12617  // the same class type as the current class and has a
12618  // DW_AT_artificial attribute flag set. We are not looking at
12619  // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12620  // emitted in GCC 4_4, which was already DWARF 3.
12621  function_decl::parameter_sptr first_parm;
12622  if (!f->get_parameters().empty())
12623  first_parm = f->get_parameters()[0];
12624 
12625  bool is_artificial = first_parm && first_parm->get_is_artificial();
12626  type_base_sptr this_ptr_type, other_klass;
12627 
12628  if (is_artificial)
12629  this_ptr_type = first_parm->get_type();
12630 
12631  // Sometimes, the type of the "this" pointer is "const class_type* const".
12632  //
12633  // Meaning that the "this pointer" itself is const qualified. So
12634  // let's get the underlying underlying non-qualified pointer.
12635  if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12636  this_ptr_type = q->get_underlying_type();
12637 
12638  // Now, get the pointed-to type.
12639  if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12640  other_klass = p->get_pointed_to_type();
12641 
12642  // Sometimes, other_klass can be qualified; e.g, volatile. In
12643  // that case, let's get the unqualified version of other_klass.
12644  if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12645  other_klass = q->get_underlying_type();
12646 
12647  if (other_klass
12648  && get_type_name(other_klass) == klass->get_qualified_name())
12649  ;
12650  else
12651  is_static = true;
12652 
12653  if (is_static)
12654  {
12655  // If we are looking at a DWARF version that is high enough
12656  // for the DW_AT_object_pointer attribute to be present, let's
12657  // see if it's present. If it is, then the current member
12658  // function is not static.
12659  Dwarf_Die object_pointer_die;
12660  if (die_has_object_pointer(die, object_pointer_die))
12661  is_static = false;
12662  }
12663  }
12664  set_member_access_specifier(m, access);
12665  if (vindex != -1)
12667  if (is_virtual)
12668  set_member_function_is_virtual(m, is_virtual);
12669  set_member_is_static(m, is_static);
12670  set_member_function_is_ctor(m, is_ctor);
12671  set_member_function_is_dtor(m, is_dtor);
12672  set_member_function_is_const(m, method_t->get_is_const());
12673 
12675 
12676  if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12677  {
12678  // This is a virtual member function which has a linkage name
12679  // but has no underlying symbol set.
12680  //
12681  // The underlying elf symbol to set to this function can show up
12682  // later in the DWARF input or it can be that, because of some
12683  // compiler optimization, the relation between this function and
12684  // its underlying elf symbol is simply not emitted in the DWARF.
12685  //
12686  // Let's thus schedule this function for a later fixup pass
12687  // (performed by
12688  // reader::fixup_functions_with_no_symbols()) that will
12689  // set its underlying symbol.
12690  //
12691  // Note that if the underying symbol is encountered later in the
12692  // DWARF input, then the part of build_function_decl() that
12693  // updates the function to set its underlying symbol will
12694  // de-schedule this function wrt fixup pass.
12695  Dwarf_Off die_offset = dwarf_dieoffset(die);
12696  die_function_decl_map_type &fns_with_no_symbol =
12697  rdr.die_function_decl_with_no_symbol_map();
12698  die_function_decl_map_type::const_iterator i =
12699  fns_with_no_symbol.find(die_offset);
12700  if (i == fns_with_no_symbol.end())
12701  fns_with_no_symbol[die_offset] = f;
12702  }
12703 
12704 }
12705 
12706 /// If a function DIE has attributes which have not yet been read and
12707 /// added to the internal representation that represents that function
12708 /// then read those extra attributes and update the internal
12709 /// representation.
12710 ///
12711 /// @param rdr the DWARF reader to use.
12712 ///
12713 /// @param die the function DIE to consider.
12714 ///
12715 /// @param where_offset where we logical are, currently, in the stream
12716 /// of DIEs. If you don't know what this is, you can just set it to zero.
12717 ///
12718 /// @param existing_fn the representation of the function to update.
12719 ///
12720 /// @return the updated function representation.
12721 static function_decl_sptr
12722 maybe_finish_function_decl_reading(reader& rdr,
12723  Dwarf_Die* die,
12724  size_t where_offset,
12725  const function_decl_sptr& existing_fn)
12726 {
12727  function_decl_sptr result = build_function_decl(rdr, die,
12728  where_offset,
12729  existing_fn);
12730 
12731  return result;
12732 }
12733 
12734 /// Lookup a class or a typedef with a given qualified name in the
12735 /// corpus that a given scope belongs to.
12736 ///
12737 /// @param scope the scope to consider.
12738 ///
12739 /// @param type_name the qualified name of the type to look for.
12740 ///
12741 /// @return the typedef or class type found.
12742 static type_base_sptr
12743 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12744 {
12745  string qname = build_qualified_name(scope, type_name);
12746  corpus* corp = scope->get_corpus();
12747  type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12748  return result;
12749 }
12750 
12751 /// Lookup a class of typedef type from the current corpus being
12752 /// constructed.
12753 ///
12754 /// The type being looked for has the same name as a given DIE.
12755 ///
12756 /// @param rdr the DWARF reader to use.
12757 ///
12758 /// @param die the DIE which has the same name as the type we are
12759 /// looking for.
12760 ///
12761 /// @param called_for_public_decl whether this function is being
12762 /// called from a a publicly defined declaration.
12763 ///
12764 /// @param where_offset where we are logically at in the DIE stream.
12765 ///
12766 /// @return the type found.
12767 static type_base_sptr
12768 lookup_class_or_typedef_from_corpus(reader& rdr,
12769  Dwarf_Die* die,
12770  bool called_for_public_decl,
12771  size_t where_offset)
12772 {
12773  if (!die)
12774  return class_decl_sptr();
12775 
12776  string class_name = die_string_attribute(die, DW_AT_name);
12777  if (class_name.empty())
12778  return class_decl_sptr();
12779 
12780  scope_decl_sptr scope = get_scope_for_die(rdr, die,
12781  called_for_public_decl,
12782  where_offset);
12783  if (scope)
12784  return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12785 
12786  return type_base_sptr();
12787 }
12788 
12789 /// Lookup a class, typedef or enum type with a given qualified name
12790 /// in the corpus that a given scope belongs to.
12791 ///
12792 /// @param scope the scope to consider.
12793 ///
12794 /// @param type_name the qualified name of the type to look for.
12795 ///
12796 /// @return the typedef, enum or class type found.
12797 static type_base_sptr
12798 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12799  const string& type_name)
12800 {
12801  string qname = build_qualified_name(scope, type_name);
12802  corpus* corp = scope->get_corpus();
12803  type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12804  return result;
12805 }
12806 
12807 /// Lookup a class, typedef or enum type in a given scope, in the
12808 /// corpus that scope belongs to.
12809 ///
12810 /// @param die the DIE of the class, typedef or enum to lookup.
12811 ///
12812 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
12813 /// type, this is the index of that anonymous type in its scope, in
12814 /// case there are several anonymous types of the same kind in that
12815 /// scope.
12816 ///
12817 /// @param scope the scope in which to look the type for.
12818 ///
12819 /// @return the typedef, enum or class type found.
12820 static type_base_sptr
12821 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12822  size_t anonymous_member_type_idx,
12823  scope_decl* scope)
12824 {
12825  if (!die)
12826  return class_decl_sptr();
12827 
12828  string type_name = die_string_attribute(die, DW_AT_name);
12829  if (is_anonymous_type_die(die))
12830  type_name =
12831  get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12832 
12833  if (type_name.empty())
12834  return class_decl_sptr();
12835 
12836  return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12837 }
12838 
12839 
12840 /// Test if a DIE represents a function that is a member of a given
12841 /// class type.
12842 ///
12843 /// @param rdr the DWARF reader.
12844 ///
12845 /// @param function_die the DIE of the function to consider.
12846 ///
12847 /// @param class_type the class type to consider.
12848 ///
12849 /// @param where_offset where we are logically at in the DIE stream.
12850 ///
12851 /// @return the method declaration corresponding to the member
12852 /// function of @p class_type, iff @p function_die is for a member
12853 /// function of @p class_type.
12854 static method_decl_sptr
12855 is_function_for_die_a_member_of_class(reader& rdr,
12856  Dwarf_Die* function_die,
12857  const class_or_union_sptr& class_type)
12858 {
12859  type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
12860 
12861  if (!artifact)
12862  return method_decl_sptr();
12863 
12864  method_decl_sptr method = is_method_decl(artifact);
12865  method_type_sptr method_type;
12866 
12867  if (method)
12868  method_type = method->get_type();
12869  else
12870  method_type = is_method_type(artifact);
12871  ABG_ASSERT(method_type);
12872 
12873  class_or_union_sptr method_class = method_type->get_class_type();
12874  ABG_ASSERT(method_class);
12875 
12876  string method_class_name = method_class->get_qualified_name(),
12877  class_type_name = class_type->get_qualified_name();
12878 
12879  if (method_class_name == class_type_name)
12880  {
12881  //ABG_ASSERT(class_type.get() == method_class.get());
12882  return method;
12883  }
12884 
12885  return method_decl_sptr();
12886 }
12887 
12888 /// If a given function DIE represents an existing member function of
12889 /// a given class, then update that member function with new
12890 /// properties present in the DIE. Otherwise, if the DIE represents a
12891 /// new member function that is not already present in the class then
12892 /// add that new member function to the class.
12893 ///
12894 /// @param rdr the DWARF reader.
12895 ///
12896 /// @param function_die the DIE of the potential member function to
12897 /// consider.
12898 ///
12899 /// @param class_type the class type to consider.
12900 ///
12901 /// @param called_from_public_decl is true iff this function was
12902 /// called from a publicly defined and exported declaration.
12903 ///
12904 /// @param where_offset where we are logically at in the DIE stream.
12905 ///
12906 /// @return the method decl representing the member function.
12907 static method_decl_sptr
12908 add_or_update_member_function(reader& rdr,
12909  Dwarf_Die* function_die,
12910  const class_or_union_sptr& class_type,
12911  bool called_from_public_decl,
12912  size_t where_offset)
12913 {
12914  method_decl_sptr method =
12915  is_function_for_die_a_member_of_class(rdr, function_die, class_type);
12916 
12917  if (!method)
12918  method = is_method_decl(build_ir_node_from_die(rdr, function_die,
12919  class_type.get(),
12920  called_from_public_decl,
12921  where_offset));
12922  if (!method)
12923  return method_decl_sptr();
12924 
12925  finish_member_function_reading(function_die,
12926  is_function_decl(method),
12927  class_type, rdr);
12928  return method;
12929 }
12930 
12931 /// Build a an IR node for class type from a DW_TAG_structure_type or
12932 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
12933 /// currently built.
12934 ///
12935 /// If the represents class type that already exists, then update the
12936 /// existing class type with the new properties found in the DIE.
12937 ///
12938 /// It meanst that this function can also update an existing
12939 /// class_decl node with data members, member functions and other
12940 /// properties coming from the DIE.
12941 ///
12942 /// @param rdr the DWARF reader to consider.
12943 ///
12944 /// @param die the DIE to read information from. Must be either a
12945 /// DW_TAG_structure_type or a DW_TAG_class_type.
12946 ///
12947 /// @param scope a pointer to the scope_decl* under which this class
12948 /// is to be added to.
12949 ///
12950 /// @param is_struct whether the class was declared as a struct.
12951 ///
12952 /// @param klass if non-null, this is a klass to append the members
12953 /// to. Otherwise, this function just builds the class from scratch.
12954 ///
12955 /// @param called_from_public_decl set to true if this class is being
12956 /// called from a "Public declaration like vars or public symbols".
12957 ///
12958 /// @param where_offset the offset of the DIE where we are "logically"
12959 /// positionned at, in the DIE tree. This is useful when @p die is
12960 /// e.g, DW_TAG_partial_unit that can be included in several places in
12961 /// the DIE tree.
12962 ///
12963 /// @param is_declaration_only is true if the DIE denoted by @p die is
12964 /// a declaration-only DIE.
12965 ///
12966 /// @return the resulting class_type.
12967 static class_decl_sptr
12968 add_or_update_class_type(reader& rdr,
12969  Dwarf_Die* die,
12970  scope_decl* scope,
12971  bool is_struct,
12972  class_decl_sptr klass,
12973  bool called_from_public_decl,
12974  size_t where_offset,
12975  bool is_declaration_only)
12976 {
12977  class_decl_sptr result;
12978  if (!die)
12979  return result;
12980 
12981  const die_source source = rdr.get_die_source(die);
12982 
12983  unsigned tag = dwarf_tag(die);
12984 
12985  if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
12986  return result;
12987 
12988  {
12989  die_class_or_union_map_type::const_iterator i =
12990  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12991  if (i != rdr.die_wip_classes_map(source).end())
12992  {
12993  class_decl_sptr class_type = is_class_type(i->second);
12994  ABG_ASSERT(class_type);
12995  return class_type;
12996  }
12997  }
12998 
12999  string name, linkage_name;
13000  location loc;
13001  die_loc_and_name(rdr, die, loc, name, linkage_name);
13002 
13003  bool is_anonymous = false;
13004  if (name.empty())
13005  {
13006  // So we are looking at an anonymous struct. Let's
13007  // give it a name.
13008  name = get_internal_anonymous_die_prefix_name(die);
13009  ABG_ASSERT(!name.empty());
13010  // But we remember that the type is anonymous.
13011  is_anonymous = true;
13012 
13013  if (size_t s = scope->get_num_anonymous_member_classes())
13014  name = build_internal_anonymous_die_name(name, s);
13015  }
13016 
13017  if (!is_anonymous)
13018  {
13019  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13020  {
13021  if (loc)
13022  // TODO: if there is only one class defined in the corpus
13023  // for this location, then re-use it. But if there are
13024  // more than one, then do not re-use it, for now.
13025  result = lookup_class_type_per_location(loc.expand(), *corp);
13026  else
13027  // TODO: if there is just one class for that name defined,
13028  // then re-use it. Otherwise, don't.
13029  result = lookup_class_type(name, *corp);
13030  if (result
13031  // If we are seeing a declaration of a definition we
13032  // already had, or if we are seing a type with the same
13033  // declaration-only-ness that we had before, then keep
13034  // the one we already had.
13035  && (result->get_is_declaration_only() == is_declaration_only
13036  || (!result->get_is_declaration_only()
13037  && is_declaration_only)))
13038  {
13039  rdr.associate_die_to_type(die, result, where_offset);
13040  return result;
13041  }
13042  else
13043  // We might be seeing the definition of a declaration we
13044  // already had. In that case, keep the definition and
13045  // drop the declaration.
13046  result.reset();
13047  }
13048  }
13049 
13050  // If we've already seen the same class as 'die', then let's re-use
13051  // that one, unless it's an anonymous class. We can't really safely
13052  // re-use anonymous classes as they have no name, by construction.
13053  // What we can do, rather, is to reuse the typedef that name them,
13054  // when they do have a naming typedef.
13055  if (!is_anonymous)
13056  if (class_decl_sptr pre_existing_class =
13057  is_class_type(rdr.lookup_type_artifact_from_die(die)))
13058  klass = pre_existing_class;
13059 
13060  uint64_t size = 0;
13061  die_size_in_bits(die, size);
13062  bool is_artificial = die_is_artificial(die);
13063 
13064  Dwarf_Die child;
13065  bool has_child = (dwarf_child(die, &child) == 0);
13066 
13067  decl_base_sptr res;
13068  if (klass)
13069  {
13070  res = result = klass;
13071  if (has_child && klass->get_is_declaration_only()
13072  && klass->get_definition_of_declaration())
13073  res = result = is_class_type(klass->get_definition_of_declaration());
13074  if (loc)
13075  result->set_location(loc);
13076  }
13077  else
13078  {
13079  result.reset(new class_decl(rdr.env(), name, size,
13080  /*alignment=*/0, is_struct, loc,
13081  decl_base::VISIBILITY_DEFAULT,
13082  is_anonymous));
13083 
13084  result->set_is_declaration_only(is_declaration_only);
13085 
13086  res = add_decl_to_scope(result, scope);
13087  result = dynamic_pointer_cast<class_decl>(res);
13088  ABG_ASSERT(result);
13089  }
13090 
13091  if (!klass || klass->get_is_declaration_only())
13092  if (size != result->get_size_in_bits())
13093  result->set_size_in_bits(size);
13094 
13095  if (klass)
13096  // We are amending a class that was built before. So let's check
13097  // if we need to amend its "declaration-only-ness" status.
13098  if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13099  // The size of the class doesn't match its
13100  // 'declaration-only-ness". We might have a non-zero sized
13101  // class which is declaration-only, or a zero sized class that
13102  // is not declaration-only. Let's set the declaration-only-ness
13103  // according to what we are instructed to.
13104  //
13105  // Note however that there are binaries out there emitted by
13106  // compilers (Clang, in C++) emit declarations-only classes that
13107  // have non-zero size. So we must honor these too. That is why
13108  // we are not forcing the declaration-only-ness to false when a
13109  // class has non-zero size. An example of such binary is
13110  // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13111  result->set_is_declaration_only(is_declaration_only);
13112 
13113  // If a non-decl-only class has children node and is advertized as
13114  // having a non-zero size let's trust that.
13115  if (!result->get_is_declaration_only() && has_child)
13116  if (result->get_size_in_bits() == 0 && size != 0)
13117  result->set_size_in_bits(size);
13118 
13119  result->set_is_artificial(is_artificial);
13120 
13121  rdr.associate_die_to_type(die, result, where_offset);
13122 
13123  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13124 
13125  if (!has_child)
13126  // TODO: set the access specifier for the declaration-only class
13127  // here.
13128  return result;
13129 
13130  rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13131 
13132  bool is_incomplete_type = false;
13133  if (is_declaration_only && size == 0 && has_child)
13134  // this is an incomplete DWARF type as defined by [5.7.1]
13135  //
13136  // An incomplete structure, union or class type is represented by
13137  // a structure, union or class entry that does not have a byte
13138  // size attribute and that has a DW_AT_declaration attribute.
13139  //
13140  // Let's consider that it's thus a decl-only class, likely
13141  // referred to by a pointer. If we later encounter a definition
13142  // for this decl-only class type, then this decl-only class will
13143  // be resolved to it by the code in
13144  // reader::resolve_declaration_only_classes.
13145  is_incomplete_type = true;
13146 
13147  scope_decl_sptr scop =
13148  dynamic_pointer_cast<scope_decl>(res);
13149  ABG_ASSERT(scop);
13150  rdr.scope_stack().push(scop.get());
13151 
13152  if (has_child && !is_incomplete_type)
13153  {
13154  int anonymous_member_class_index = -1;
13155  int anonymous_member_union_index = -1;
13156  int anonymous_member_enum_index = -1;
13157 
13158  do
13159  {
13160  tag = dwarf_tag(&child);
13161 
13162  // Handle base classes.
13163  if (tag == DW_TAG_inheritance)
13164  {
13165  result->set_is_declaration_only(false);
13166 
13167  Dwarf_Die type_die;
13168  if (!die_die_attribute(&child, DW_AT_type, type_die))
13169  continue;
13170 
13171  type_base_sptr base_type;
13172  if (!(base_type =
13173  lookup_class_or_typedef_from_corpus(rdr, &type_die,
13174  called_from_public_decl,
13175  where_offset)))
13176  {
13177  base_type =
13178  is_type(build_ir_node_from_die(rdr, &type_die,
13179  called_from_public_decl,
13180  where_offset));
13181  }
13182  // Sometimes base_type can be a typedef. Let's make
13183  // sure that typedef is compatible with a class type.
13185  if (!b)
13186  continue;
13187 
13188  access_specifier access =
13189  is_struct
13190  ? public_access
13191  : private_access;
13192 
13193  die_access_specifier(&child, access);
13194 
13195  bool is_virt= die_is_virtual(&child);
13196  int64_t offset = 0;
13197  bool is_offset_present =
13198  die_member_offset(rdr, &child, offset);
13199 
13200  class_decl::base_spec_sptr base(new class_decl::base_spec
13201  (b, access,
13202  is_offset_present ? offset : -1,
13203  is_virt));
13204  if (b->get_is_declaration_only())
13205  ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13206  if (result->find_base_class(b->get_qualified_name()))
13207  continue;
13208  result->add_base_specifier(base);
13209  }
13210  // Handle data members.
13211  else if (tag == DW_TAG_member
13212  || tag == DW_TAG_variable)
13213  {
13214  Dwarf_Die type_die;
13215  if (!die_die_attribute(&child, DW_AT_type, type_die))
13216  continue;
13217 
13218  string n, m;
13219  location loc;
13220  die_loc_and_name(rdr, &child, loc, n, m);
13221  /// For now, we skip the hidden vtable pointer.
13222  /// Currently, we're looking for a member starting with
13223  /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13224  /// use as a name for the hidden vtable pointer.
13225  if (n.substr(0, 5) == "_vptr"
13226  && n.size() > 5
13227  && !std::isalnum(n.at(5))
13228  && n.at(5) != '_')
13229  continue;
13230 
13231  // If the variable is already a member of this class,
13232  // move on. If it's an anonymous data member, we need
13233  // to handle it differently. We'll do that later below.
13234  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13235  continue;
13236 
13237  int64_t offset_in_bits = 0;
13238  bool is_laid_out = die_member_offset(rdr, &child,
13239  offset_in_bits);
13240  // For now, is_static == !is_laid_out. When we have
13241  // templates, we'll try to be more specific. For now,
13242  // this approximation should do OK.
13243  bool is_static = !is_laid_out;
13244 
13245  if (is_static && variable_is_suppressed(rdr,
13246  result.get(),
13247  &child))
13248  continue;
13249 
13250  decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13251  called_from_public_decl,
13252  where_offset));
13253  type_base_sptr t = is_type(ty);
13254  if (!t)
13255  continue;
13256 
13257  if (n.empty() && !die_is_anonymous_data_member(&child))
13258  {
13259  // We must be in a case where the data member has an
13260  // empty name because the DWARF emitter has a bug.
13261  // Let's generate an artificial name for that data
13262  // member.
13263  n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13264  ABG_ASSERT(!n.empty());
13265  }
13266 
13267  // The call to build_ir_node_from_die above could have
13268  // triggered the adding of a data member named 'n' into
13269  // result. So let's check again if the variable is
13270  // already a member of this class. Here again, if it's
13271  // an anonymous data member, we need to handle it
13272  // differently. We'll do that later below.
13273  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13274  continue;
13275 
13276  if (!is_static)
13277  // We have a non-static data member. So this class
13278  // cannot be a declaration-only class anymore, even if
13279  // some DWARF emitters might consider it otherwise.
13280  result->set_is_declaration_only(false);
13281  access_specifier access =
13282  is_struct
13283  ? public_access
13284  : private_access;
13285 
13286  die_access_specifier(&child, access);
13287 
13288  var_decl_sptr dm(new var_decl(n, t, loc, m));
13289  if (n.empty()
13290  && anonymous_data_member_exists_in_class(*dm, *result))
13291  // dm is an anonymous data member that was already
13292  // present in the current class so let's not add it.
13293  continue;
13294  result->add_data_member(dm, access, is_laid_out,
13295  is_static, offset_in_bits);
13296  ABG_ASSERT(has_scope(dm));
13297  rdr.associate_die_to_decl(&child, dm, where_offset,
13298  /*associate_by_repr=*/false);
13299  }
13300  // Handle member functions;
13301  else if (tag == DW_TAG_subprogram)
13302  {
13303  decl_base_sptr r =
13304  add_or_update_member_function(rdr, &child, result,
13305  called_from_public_decl,
13306  where_offset);
13308  rdr.associate_die_to_decl(&child, f, where_offset,
13309  /*associate_by_repr=*/true);
13310  }
13311  // Handle member types
13312  else if (die_is_type(&child))
13313  {
13314  // Track the anonymous type index in the current
13315  // scope. Look for what this means by reading the
13316  // comment of the function
13317  // build_internal_anonymous_die_name.
13318  int anonymous_member_type_index = 0;
13319  if (is_anonymous_type_die(&child))
13320  {
13321  // Update the anonymous type index.
13322  if (die_is_class_type(&child))
13323  anonymous_member_type_index =
13324  ++anonymous_member_class_index;
13325  else if (dwarf_tag(&child) == DW_TAG_union_type)
13326  anonymous_member_type_index =
13327  ++anonymous_member_union_index;
13328  else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13329  anonymous_member_type_index =
13330  ++anonymous_member_enum_index;
13331  }
13332  // if the type is not already a member of this class,
13333  // then add it to the class.
13334  if ((is_anonymous_type_die(&child)
13335  && !lookup_class_typedef_or_enum_type_from_corpus
13336  (&child, anonymous_member_type_index, result.get()))
13337  || !result->find_member_type(die_name(&child)))
13338  build_ir_node_from_die(rdr, &child, result.get(),
13339  called_from_public_decl,
13340  where_offset);
13341  }
13342  } while (dwarf_siblingof(&child, &child) == 0);
13343  }
13344 
13345  rdr.scope_stack().pop();
13346 
13347  {
13348  die_class_or_union_map_type::const_iterator i =
13349  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13350  if (i != rdr.die_wip_classes_map(source).end())
13351  {
13352  if (is_member_type(i->second))
13354  get_member_access_specifier(i->second));
13355  rdr.die_wip_classes_map(source).erase(i);
13356  }
13357  }
13358 
13359  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13360  return result;
13361 }
13362 
13363 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
13364 ///
13365 /// @param rdr the DWARF reader to use.
13366 ///
13367 /// @param die the DIE to read from.
13368 ///
13369 /// @param scope the scope the resulting @ref union_decl belongs to.
13370 ///
13371 /// @param union_type if this parameter is non-nil, then this function
13372 /// updates the @ref union_decl that it points to, rather than
13373 /// creating a new @ref union_decl.
13374 ///
13375 /// @param called_from_public_decl is true if this function has been
13376 /// initially called within the context of a public decl.
13377 ///
13378 /// @param where_offset the offset of the DIE where we are "logically"
13379 /// positionned at, in the DIE tree. This is useful when @p die is
13380 /// e.g, DW_TAG_partial_unit that can be included in several places in
13381 /// the DIE tree.
13382 ///
13383 /// @param is_declaration_only is true if the DIE denoted by @p die is
13384 /// a declaration-only DIE.
13385 ///
13386 /// @return the resulting @ref union_decl type.
13387 static union_decl_sptr
13388 add_or_update_union_type(reader& rdr,
13389  Dwarf_Die* die,
13390  scope_decl* scope,
13391  union_decl_sptr union_type,
13392  bool called_from_public_decl,
13393  size_t where_offset,
13394  bool is_declaration_only)
13395 {
13396  union_decl_sptr result;
13397  if (!die)
13398  return result;
13399 
13400  unsigned tag = dwarf_tag(die);
13401 
13402  if (tag != DW_TAG_union_type)
13403  return result;
13404 
13405  const die_source source = rdr.get_die_source(die);
13406  {
13407  die_class_or_union_map_type::const_iterator i =
13408  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13409  if (i != rdr.die_wip_classes_map(source).end())
13410  {
13411  union_decl_sptr u = is_union_type(i->second);
13412  ABG_ASSERT(u);
13413  return u;
13414  }
13415  }
13416 
13417  string name, linkage_name;
13418  location loc;
13419  die_loc_and_name(rdr, die, loc, name, linkage_name);
13420 
13421  bool is_anonymous = false;
13422  if (name.empty())
13423  {
13424  // So we are looking at an anonymous union. Let's give it a
13425  // name.
13426  name = get_internal_anonymous_die_prefix_name(die);
13427  ABG_ASSERT(!name.empty());
13428  // But we remember that the type is anonymous.
13429  is_anonymous = true;
13430 
13431  if (size_t s = scope->get_num_anonymous_member_unions())
13432  name = build_internal_anonymous_die_name(name, s);
13433  }
13434 
13435  // If the type has location, then associate it to its
13436  // representation. This way, all occurences of types with the same
13437  // representation (name) and location can be later detected as being
13438  // for the same type.
13439 
13440  if (!is_anonymous)
13441  {
13442  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13443  {
13444  if (loc)
13445  result = lookup_union_type_per_location(loc.expand(), *corp);
13446  else
13447  result = lookup_union_type(name, *corp);
13448 
13449  if (result)
13450  {
13451  rdr.associate_die_to_type(die, result, where_offset);
13452  return result;
13453  }
13454  }
13455  }
13456 
13457  // if we've already seen a union with the same union as 'die' then
13458  // let's re-use that one. We can't really safely re-use anonymous
13459  // unions as they have no name, by construction. What we can do,
13460  // rather, is to reuse the typedef that name them, when they do have
13461  // a naming typedef.
13462  if (!is_anonymous)
13463  if (union_decl_sptr pre_existing_union =
13464  is_union_type(rdr.lookup_artifact_from_die(die)))
13465  union_type = pre_existing_union;
13466 
13467  uint64_t size = 0;
13468  die_size_in_bits(die, size);
13469  bool is_artificial = die_is_artificial(die);
13470 
13471  if (union_type)
13472  {
13473  result = union_type;
13474  result->set_location(loc);
13475  }
13476  else
13477  {
13478  result.reset(new union_decl(rdr.env(), name, size, loc,
13479  decl_base::VISIBILITY_DEFAULT,
13480  is_anonymous));
13481  if (is_declaration_only)
13482  result->set_is_declaration_only(true);
13483  result = is_union_type(add_decl_to_scope(result, scope));
13484  ABG_ASSERT(result);
13485  }
13486 
13487  if (size)
13488  {
13489  result->set_size_in_bits(size);
13490  result->set_is_declaration_only(false);
13491  }
13492 
13493  result->set_is_artificial(is_artificial);
13494 
13495  rdr.associate_die_to_type(die, result, where_offset);
13496 
13497  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13498 
13499  Dwarf_Die child;
13500  bool has_child = (dwarf_child(die, &child) == 0);
13501  if (!has_child)
13502  return result;
13503 
13504  rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13505 
13506  scope_decl_sptr scop =
13507  dynamic_pointer_cast<scope_decl>(result);
13508  ABG_ASSERT(scop);
13509  rdr.scope_stack().push(scop.get());
13510 
13511  if (has_child)
13512  {
13513  do
13514  {
13515  tag = dwarf_tag(&child);
13516  // Handle data members.
13517  if (tag == DW_TAG_member || tag == DW_TAG_variable)
13518  {
13519  Dwarf_Die type_die;
13520  if (!die_die_attribute(&child, DW_AT_type, type_die))
13521  continue;
13522 
13523  string n, m;
13524  location loc;
13525  die_loc_and_name(rdr, &child, loc, n, m);
13526 
13527  // Because we can be updating an existing union, let's
13528  // make sure we don't already have a member of the same
13529  // name. Anonymous member are handled a bit later below
13530  // so let's not consider them here.
13531  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13532  continue;
13533 
13534  ssize_t offset_in_bits = 0;
13535  decl_base_sptr ty =
13536  is_decl(build_ir_node_from_die(rdr, &type_die,
13537  called_from_public_decl,
13538  where_offset));
13539  type_base_sptr t = is_type(ty);
13540  if (!t)
13541  continue;
13542 
13543  // We have a non-static data member. So this union
13544  // cannot be a declaration-only union anymore, even if
13545  // some DWARF emitters might consider it otherwise.
13546  result->set_is_declaration_only(false);
13547  access_specifier access = public_access;
13548 
13549  die_access_specifier(&child, access);
13550 
13551  var_decl_sptr dm(new var_decl(n, t, loc, m));
13552  // If dm is an anonymous data member, let's make sure
13553  // the current union doesn't already have it as a data
13554  // member.
13555  if (n.empty() && result->find_data_member(dm))
13556  continue;
13557 
13558  result->add_data_member(dm, access, /*is_laid_out=*/true,
13559  /*is_static=*/false,
13560  offset_in_bits);
13561  ABG_ASSERT(has_scope(dm));
13562  rdr.associate_die_to_decl(&child, dm, where_offset,
13563  /*associate_by_repr=*/false);
13564  }
13565  // Handle member functions;
13566  else if (tag == DW_TAG_subprogram)
13567  {
13568  decl_base_sptr r =
13569  is_decl(build_ir_node_from_die(rdr, &child,
13570  result.get(),
13571  called_from_public_decl,
13572  where_offset));
13573  if (!r)
13574  continue;
13575 
13576  function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13577  ABG_ASSERT(f);
13578 
13579  finish_member_function_reading(&child, f, result, rdr);
13580 
13581  rdr.associate_die_to_decl(&child, f, where_offset,
13582  /*associate_by_repr=*/false);
13583  }
13584  // Handle member types
13585  else if (die_is_type(&child))
13586  decl_base_sptr td =
13587  is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13588  called_from_public_decl,
13589  where_offset));
13590  } while (dwarf_siblingof(&child, &child) == 0);
13591  }
13592 
13593  rdr.scope_stack().pop();
13594 
13595  {
13596  die_class_or_union_map_type::const_iterator i =
13597  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13598  if (i != rdr.die_wip_classes_map(source).end())
13599  {
13600  if (is_member_type(i->second))
13602  get_member_access_specifier(i->second));
13603  rdr.die_wip_classes_map(source).erase(i);
13604  }
13605  }
13606 
13607  return result;
13608 }
13609 
13610 /// build a qualified type from a DW_TAG_const_type,
13611 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13612 ///
13613 /// @param rdr the DWARF reader to consider.
13614 ///
13615 /// @param die the input DIE to read from.
13616 ///
13617 /// @param called_from_public_decl true if this function was called
13618 /// from a context where either a public function or a public variable
13619 /// is being built.
13620 ///
13621 /// @param where_offset the offset of the DIE where we are "logically"
13622 /// positionned at, in the DIE tree. This is useful when @p die is
13623 /// e.g, DW_TAG_partial_unit that can be included in several places in
13624 /// the DIE tree.
13625 ///
13626 /// @return the resulting qualified_type_def.
13627 static type_base_sptr
13628 build_qualified_type(reader& rdr,
13629  Dwarf_Die* die,
13630  bool called_from_public_decl,
13631  size_t where_offset)
13632 {
13633  type_base_sptr result;
13634  if (!die)
13635  return result;
13636 
13637  unsigned tag = dwarf_tag(die);
13638 
13639  if (tag != DW_TAG_const_type
13640  && tag != DW_TAG_volatile_type
13641  && tag != DW_TAG_restrict_type)
13642  return result;
13643 
13644  Dwarf_Die underlying_type_die;
13645  decl_base_sptr utype_decl;
13646  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13647  // So, if no DW_AT_type is present, then this means (if we are
13648  // looking at a debug info emitted by GCC) that we are looking
13649  // at a qualified void type.
13650  utype_decl = build_ir_node_for_void_type(rdr);
13651 
13652  if (!utype_decl)
13653  utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13654  called_from_public_decl,
13655  where_offset));
13656  if (!utype_decl)
13657  return result;
13658 
13659  // The call to build_ir_node_from_die() could have triggered the
13660  // creation of the type for this DIE. In that case, just return it.
13661  if (type_base_sptr t = rdr.lookup_type_from_die(die))
13662  {
13663  result = t;
13664  rdr.associate_die_to_type(die, result, where_offset);
13665  return result;
13666  }
13667 
13668  type_base_sptr utype = is_type(utype_decl);
13669  ABG_ASSERT(utype);
13670 
13671  qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13672  if (tag == DW_TAG_const_type)
13673  qual |= qualified_type_def::CV_CONST;
13674  else if (tag == DW_TAG_volatile_type)
13675  qual |= qualified_type_def::CV_VOLATILE;
13676  else if (tag == DW_TAG_restrict_type)
13677  qual |= qualified_type_def::CV_RESTRICT;
13678  else
13680 
13681  if (!result)
13682  result.reset(new qualified_type_def(utype, qual, location()));
13683 
13684  rdr.associate_die_to_type(die, result, where_offset);
13685 
13686  return result;
13687 }
13688 
13689 /// Walk a tree of typedef of qualified arrays and schedule all type
13690 /// nodes for canonicalization.
13691 ///
13692 /// This is to be used after an array tree has been cloned. In that
13693 /// case, the newly cloned type nodes have to be scheduled for
13694 /// canonicalization.
13695 ///
13696 /// This is a subroutine of maybe_strip_qualification.
13697 ///
13698 /// @param t the type node to be scheduled for canonicalization.
13699 ///
13700 /// @param rdr the DWARF reader to use.
13701 static void
13702 schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13703  reader &rdr)
13704 {
13705  if (typedef_decl_sptr type = is_typedef(t))
13706  {
13707  schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13708  rdr);
13709  rdr.schedule_type_for_late_canonicalization(t);
13710  }
13711  else if (qualified_type_def_sptr type = is_qualified_type(t))
13712  {
13713  schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13714  rdr);
13715  rdr.schedule_type_for_late_canonicalization(t);
13716  }
13717  else if (array_type_def_sptr type = is_array_type(t))
13718  {
13719  for (vector<array_type_def::subrange_sptr>::const_iterator i =
13720  type->get_subranges().begin();
13721  i != type->get_subranges().end();
13722  ++i)
13723  {
13724  if (!(*i)->get_scope())
13725  add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13726  rdr.schedule_type_for_late_canonicalization(*i);
13727 
13728  }
13729  schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13730  rdr);
13731  rdr.schedule_type_for_late_canonicalization(type);
13732  }
13733 }
13734 
13735 /// Strip qualification from a qualified type, when it makes sense.
13736 ///
13737 /// DWARF constructs "const reference". This is redundant because a
13738 /// reference is always const. The issue is these redundant types then
13739 /// leak into the IR and make for bad diagnostics.
13740 ///
13741 /// This function thus strips the const qualifier from the type in
13742 /// that case. It might contain code to strip other cases like this
13743 /// in the future.
13744 ///
13745 /// @param t the type to strip const qualification from.
13746 ///
13747 /// @param rdr the @ref reader to use.
13748 ///
13749 /// @return the stripped type or just return @p t.
13750 static decl_base_sptr
13751 maybe_strip_qualification(const qualified_type_def_sptr t,
13752  reader &rdr)
13753 {
13754  if (!t)
13755  return t;
13756 
13757  decl_base_sptr result = t;
13758  type_base_sptr u = t->get_underlying_type();
13759 
13762  if (result.get() != t.get())
13763  return result;
13764 
13765  if (is_array_type(u) || is_typedef_of_array(u))
13766  {
13767  array_type_def_sptr array;
13768  scope_decl * scope = 0;
13769  if ((array = is_array_type(u)))
13770  {
13771  scope = array->get_scope();
13772  ABG_ASSERT(scope);
13773  array = is_array_type(clone_array_tree(array));
13774  schedule_array_tree_for_late_canonicalization(array, rdr);
13775  add_decl_to_scope(array, scope);
13776  t->set_underlying_type(array);
13777  u = t->get_underlying_type();
13778  }
13779  else if (is_typedef_of_array(u))
13780  {
13781  scope = is_decl(u)->get_scope();
13782  ABG_ASSERT(scope);
13783  typedef_decl_sptr typdef =
13785  schedule_array_tree_for_late_canonicalization(typdef, rdr);
13786  ABG_ASSERT(typdef);
13787  add_decl_to_scope(typdef, scope);
13788  t->set_underlying_type(typdef);
13789  u = t->get_underlying_type();
13790  array = is_typedef_of_array(u);
13791  }
13792  else
13794 
13795  ABG_ASSERT(array);
13796  // We should not be editing types that are already canonicalized.
13797  ABG_ASSERT(!array->get_canonical_type());
13798  type_base_sptr element_type = array->get_element_type();
13799 
13800  if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13801  {
13802  // We should not be editing types that are already canonicalized.
13803  ABG_ASSERT(!qualified->get_canonical_type());
13804  qualified_type_def::CV quals = qualified->get_cv_quals();
13805  quals |= t->get_cv_quals();
13806  qualified->set_cv_quals(quals);
13808  result = is_decl(u);
13809  }
13810  else
13811  {
13812  qualified_type_def_sptr qual_type
13813  (new qualified_type_def(element_type,
13814  t->get_cv_quals(),
13815  t->get_location()));
13817  add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13818  array->set_element_type(qual_type);
13819  rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13820  result = is_decl(u);
13821  }
13822  }
13823 
13824  return result;
13825 }
13826 
13827 /// Build a pointer type from a DW_TAG_pointer_type DIE.
13828 ///
13829 /// @param rdr the DWARF reader to consider.
13830 ///
13831 /// @param die the DIE to read information from.
13832 ///
13833 /// @param called_from_public_decl true if this function was called
13834 /// from a context where either a public function or a public variable
13835 /// is being built.
13836 ///
13837 /// @param where_offset the offset of the DIE where we are "logically"
13838 /// positionned at, in the DIE tree. This is useful when @p die is
13839 /// e.g, DW_TAG_partial_unit that can be included in several places in
13840 /// the DIE tree.
13841 ///
13842 /// @return the resulting pointer to pointer_type_def.
13843 static pointer_type_def_sptr
13844 build_pointer_type_def(reader& rdr,
13845  Dwarf_Die* die,
13846  bool called_from_public_decl,
13847  size_t where_offset)
13848 {
13849  pointer_type_def_sptr result;
13850 
13851  if (!die)
13852  return result;
13853 
13854  unsigned tag = dwarf_tag(die);
13855  if (tag != DW_TAG_pointer_type)
13856  return result;
13857 
13858  type_or_decl_base_sptr utype_decl;
13859  Dwarf_Die underlying_type_die;
13860  bool has_underlying_type_die = false;
13861  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13862  // If the DW_AT_type attribute is missing, that means we are
13863  // looking at a pointer to "void".
13864  utype_decl = build_ir_node_for_void_type(rdr);
13865  else
13866  has_underlying_type_die = true;
13867 
13868  if (!utype_decl && has_underlying_type_die)
13869  utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
13870  called_from_public_decl,
13871  where_offset);
13872  if (!utype_decl)
13873  return result;
13874 
13875  // The call to build_ir_node_from_die() could have triggered the
13876  // creation of the type for this DIE. In that case, just return it.
13877  if (type_base_sptr t = rdr.lookup_type_from_die(die))
13878  {
13879  result = is_pointer_type(t);
13880  ABG_ASSERT(result);
13881  return result;
13882  }
13883 
13884  type_base_sptr utype = is_type(utype_decl);
13885  ABG_ASSERT(utype);
13886 
13887  // if the DIE for the pointer type doesn't have a byte_size
13888  // attribute then we assume the size of the pointer is the address
13889  // size of the current translation unit.
13890  uint64_t size = rdr.cur_transl_unit()->get_address_size();
13891  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13892  // The size as expressed by DW_AT_byte_size is in byte, so let's
13893  // convert it to bits.
13894  size *= 8;
13895 
13896  // And the size of the pointer must be the same as the address size
13897  // of the current translation unit.
13898  ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13899 
13900  result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13901  ABG_ASSERT(result->get_pointed_to_type());
13902 
13903  rdr.associate_die_to_type(die, result, where_offset);
13904  return result;
13905 }
13906 
13907 /// Build a reference type from either a DW_TAG_reference_type or
13908 /// DW_TAG_rvalue_reference_type DIE.
13909 ///
13910 /// @param rdr the DWARF reader to consider.
13911 ///
13912 /// @param die the DIE to read from.
13913 ///
13914 /// @param called_from_public_decl true if this function was called
13915 /// from a context where either a public function or a public variable
13916 /// is being built.
13917 ///
13918 /// @param where_offset the offset of the DIE where we are "logically"
13919 /// positionned at, in the DIE tree. This is useful when @p die is
13920 /// e.g, DW_TAG_partial_unit that can be included in several places in
13921 /// the DIE tree.
13922 ///
13923 /// @return a pointer to the resulting reference_type_def.
13925 build_reference_type(reader& rdr,
13926  Dwarf_Die* die,
13927  bool called_from_public_decl,
13928  size_t where_offset)
13929 {
13930  reference_type_def_sptr result;
13931 
13932  if (!die)
13933  return result;
13934 
13935  unsigned tag = dwarf_tag(die);
13936  if (tag != DW_TAG_reference_type
13937  && tag != DW_TAG_rvalue_reference_type)
13938  return result;
13939 
13940  Dwarf_Die underlying_type_die;
13941  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13942  return result;
13943 
13944  type_or_decl_base_sptr utype_decl =
13945  build_ir_node_from_die(rdr, &underlying_type_die,
13946  called_from_public_decl,
13947  where_offset);
13948  if (!utype_decl)
13949  return result;
13950 
13951  // The call to build_ir_node_from_die() could have triggered the
13952  // creation of the type for this DIE. In that case, just return it.
13953  if (type_base_sptr t = rdr.lookup_type_from_die(die))
13954  {
13955  result = is_reference_type(t);
13956  ABG_ASSERT(result);
13957  return result;
13958  }
13959 
13960  type_base_sptr utype = is_type(utype_decl);
13961  ABG_ASSERT(utype);
13962 
13963  // if the DIE for the reference type doesn't have a byte_size
13964  // attribute then we assume the size of the reference is the address
13965  // size of the current translation unit.
13966  uint64_t size = rdr.cur_transl_unit()->get_address_size();
13967  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13968  size *= 8;
13969 
13970  // And the size of the pointer must be the same as the address size
13971  // of the current translation unit.
13972  ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13973 
13974  bool is_lvalue = tag == DW_TAG_reference_type;
13975 
13976  result.reset(new reference_type_def(utype, is_lvalue, size,
13977  /*alignment=*/0,
13978  location()));
13979  if (corpus_sptr corp = rdr.corpus())
13980  if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
13981  result = t;
13982  rdr.associate_die_to_type(die, result, where_offset);
13983  return result;
13984 }
13985 
13986 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13987 ///
13988 /// @param rdr the DWARF reader to consider.
13989 ///
13990 /// @param die the DIE to read from.
13991 ///
13992 /// @param is_method points to a class or union declaration iff we're
13993 /// building the type for a method. This is the enclosing class or
13994 /// union of the method.
13995 ///
13996 /// @param where_offset the offset of the DIE where we are "logically"
13997 /// positioned at, in the DIE tree. This is useful when @p die is
13998 /// e.g, DW_TAG_partial_unit that can be included in several places in
13999 /// the DIE tree.
14000 ///
14001 /// @return a pointer to the resulting function_type_sptr.
14002 static function_type_sptr
14003 build_function_type(reader& rdr,
14004  Dwarf_Die* die,
14005  class_or_union_sptr is_method,
14006  size_t where_offset)
14007 {
14008  function_type_sptr result;
14009 
14010  if (!die)
14011  return result;
14012 
14013  ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14014  || dwarf_tag(die) == DW_TAG_subprogram);
14015 
14016  const die_source source = rdr.get_die_source(die);
14017 
14018  {
14019  size_t off = dwarf_dieoffset(die);
14020  auto i = rdr.die_wip_function_types_map(source).find(off);
14021  if (i != rdr.die_wip_function_types_map(source).end())
14022  {
14023  function_type_sptr fn_type = is_function_type(i->second);
14024  ABG_ASSERT(fn_type);
14025  return fn_type;
14026  }
14027  }
14028 
14029  decl_base_sptr type_decl;
14030 
14031  translation_unit_sptr tu = rdr.cur_transl_unit();
14032  ABG_ASSERT(tu);
14033 
14034  /// If, inside the current translation unit, we've already seen a
14035  /// function type with the same text representation, then reuse that
14036  /// one instead.
14037  if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14038  {
14039  result = is_function_type(t);
14040  ABG_ASSERT(result);
14041  rdr.associate_die_to_type(die, result, where_offset);
14042  return result;
14043  }
14044 
14045  bool odr_is_relevant = rdr.odr_is_relevant(die);
14046  if (odr_is_relevant)
14047  {
14048  // So we can rely on the One Definition Rule to say that if
14049  // several different function types have the same name (or
14050  // rather, representation) across the entire binary, then they
14051  // ought to designate the same function type. So let's ensure
14052  // that if we've already seen a function type with the same
14053  // representation as the function type 'die', then it's the same
14054  // type as the one denoted by 'die'.
14055  if (function_type_sptr fn_type =
14056  is_function_type(rdr.lookup_type_artifact_from_die(die)))
14057  {
14058  rdr.associate_die_to_type(die, fn_type, where_offset);
14059  return fn_type;
14060  }
14061  }
14062 
14063  // Let's look at the DIE to detect if it's the DIE for a method
14064  // (type). If it is, we can deduce the name of its enclosing class
14065  // and if it's a static or const.
14066  bool is_const = false;
14067  bool is_static = false;
14068  Dwarf_Die object_pointer_die;
14069  Dwarf_Die class_type_die;
14070  bool has_this_parm_die =
14071  die_function_type_is_method_type(rdr, die, where_offset,
14072  object_pointer_die,
14073  class_type_die,
14074  is_static);
14075  if (has_this_parm_die)
14076  {
14077  // The function (type) has a "this" parameter DIE. It means it's
14078  // a member function DIE.
14079  if (!is_static)
14080  if (die_object_pointer_is_for_const_method(&object_pointer_die))
14081  is_const = true;
14082 
14083  if (!is_method)
14084  {
14085  // We were initially called as if the function represented
14086  // by DIE was *NOT* a member function. But now we know it's
14087  // a member function. Let's take that into account.
14088  class_or_union_sptr klass_type =
14089  is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14090  /*called_from_pub_decl=*/true,
14091  where_offset));
14092  ABG_ASSERT(klass_type);
14093  is_method = klass_type;
14094  }
14095  }
14096 
14097  // Let's create the type early and record it as being for the DIE
14098  // 'die'. This way, when building the sub-type triggers the
14099  // creation of a type matching the same 'die', then we'll reuse this
14100  // one.
14101 
14102  result.reset(is_method
14103  ? new method_type(is_method, is_const,
14104  tu->get_address_size(),
14105  /*alignment=*/0)
14106  : new function_type(rdr.env(), tu->get_address_size(),
14107  /*alignment=*/0));
14108  rdr.associate_die_to_type(die, result, where_offset);
14109  rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14110 
14111  type_base_sptr return_type;
14112  Dwarf_Die ret_type_die;
14113  if (die_die_attribute(die, DW_AT_type, ret_type_die))
14114  return_type =
14115  is_type(build_ir_node_from_die(rdr, &ret_type_die,
14116  /*called_from_public_decl=*/true,
14117  where_offset));
14118  if (!return_type)
14119  return_type = is_type(build_ir_node_for_void_type(rdr));
14120  result->set_return_type(return_type);
14121 
14122  Dwarf_Die child;
14123  function_decl::parameters function_parms;
14124 
14125  if (dwarf_child(die, &child) == 0)
14126  do
14127  {
14128  int child_tag = dwarf_tag(&child);
14129  if (child_tag == DW_TAG_formal_parameter)
14130  {
14131  // This is a "normal" function parameter.
14132  string name, linkage_name;
14133  location loc;
14134  die_loc_and_name(rdr, &child, loc, name, linkage_name);
14136  // Sometimes, bogus compiler emit names that are
14137  // non-ascii garbage. Let's just ditch that for now.
14138  name.clear();
14139  bool is_artificial = die_is_artificial(&child);
14140  type_base_sptr parm_type;
14141  Dwarf_Die parm_type_die;
14142  if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14143  parm_type =
14144  is_type(build_ir_node_from_die(rdr, &parm_type_die,
14145  /*called_from_public_decl=*/true,
14146  where_offset));
14147  if (!parm_type)
14148  continue;
14150  (new function_decl::parameter(parm_type, name, loc,
14151  /*variadic_marker=*/false,
14152  is_artificial));
14153  function_parms.push_back(p);
14154  }
14155  else if (child_tag == DW_TAG_unspecified_parameters)
14156  {
14157  // This is a variadic function parameter.
14158  bool is_artificial = die_is_artificial(&child);
14159 
14160  type_base_sptr parm_type =
14161  is_type(build_ir_node_for_variadic_parameter_type(rdr));
14163  (new function_decl::parameter(parm_type,
14164  /*name=*/"",
14165  location(),
14166  /*variadic_marker=*/true,
14167  is_artificial));
14168  function_parms.push_back(p);
14169  // After a DW_TAG_unspecified_parameters tag, we shouldn't
14170  // keep reading for parameters. The
14171  // unspecified_parameters TAG should be the last parameter
14172  // that we record. For instance, if there are multiple
14173  // DW_TAG_unspecified_parameters DIEs then we should care
14174  // only for the first one.
14175  break;
14176  }
14177  }
14178  while (dwarf_siblingof(&child, &child) == 0);
14179 
14180  result->set_parameters(function_parms);
14181 
14182  tu->bind_function_type_life_time(result);
14183 
14184  result->set_is_artificial(true);
14185 
14186  rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14187 
14188  {
14189  die_function_type_map_type::const_iterator i =
14190  rdr.die_wip_function_types_map(source).
14191  find(dwarf_dieoffset(die));
14192  if (i != rdr.die_wip_function_types_map(source).end())
14193  rdr.die_wip_function_types_map(source).erase(i);
14194  }
14195 
14196  maybe_canonicalize_type(result, rdr);
14197  return result;
14198 }
14199 
14200 /// Build a subrange type from a DW_TAG_subrange_type.
14201 ///
14202 /// @param rdr the DWARF reader to consider.
14203 ///
14204 /// @param die the DIE to read from.
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 /// @param associate_die_to_type if this is true then the resulting
14212 /// type is associated to the @p die, so that next time when the
14213 /// system looks up the type associated to it, the current resulting
14214 /// type is returned. If false, then no association is done and the
14215 /// resulting type can be destroyed right after. This can be useful
14216 /// when the sole purpose of building the @ref
14217 /// array_type_def::subrange_type is to use some of its method like,
14218 /// e.g, its name pretty printing methods.
14219 ///
14220 /// @return the newly built instance of @ref
14221 /// array_type_def::subrange_type, or nil if no type could be built.
14223 build_subrange_type(reader& rdr,
14224  const Dwarf_Die* die,
14225  size_t where_offset,
14226  bool associate_type_to_die)
14227 {
14229 
14230  if (!die)
14231  return result;
14232 
14233  unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14234  if (tag != DW_TAG_subrange_type)
14235  return result;
14236 
14237  string name = die_name(die);
14238 
14239  // load the underlying type.
14240  Dwarf_Die underlying_type_die;
14241  type_base_sptr underlying_type;
14242  /* Unless there is an underlying type which says differently. */
14243  bool is_signed = false;
14244  if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14245  underlying_type =
14246  is_type(build_ir_node_from_die(rdr,
14247  &underlying_type_die,
14248  /*called_from_public_decl=*/true,
14249  where_offset));
14250 
14251  if (underlying_type)
14252  {
14253  uint64_t ate;
14254  if (die_unsigned_constant_attribute (&underlying_type_die,
14255  DW_AT_encoding,
14256  ate))
14257  is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14258  }
14259 
14260  translation_unit::language language = rdr.cur_transl_unit()->get_language();
14261  array_type_def::subrange_type::bound_value lower_bound =
14262  get_default_array_lower_bound(language);
14263  array_type_def::subrange_type::bound_value upper_bound;
14264  uint64_t count = 0;
14265  bool is_infinite = false;
14266  bool count_present = false;
14267 
14268  // The DWARF 4 specifications says, in [5.11 Subrange
14269  // Type Entries]:
14270  //
14271  // The subrange entry may have the attributes
14272  // DW_AT_lower_bound and DW_AT_upper_bound to
14273  // specify, respectively, the lower and upper bound
14274  // values of the subrange.
14275  //
14276  // So let's look for DW_AT_lower_bound first.
14277  die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14278 
14279  bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14280  is_signed, upper_bound);
14281  if (!found_upper_bound)
14282  found_upper_bound = subrange_die_indirect_bound_value(die,
14283  DW_AT_upper_bound,
14284  upper_bound,
14285  is_signed);
14286  // Then, DW_AT_upper_bound.
14287  if (!found_upper_bound)
14288  {
14289  // The DWARF 4 spec says, in [5.11 Subrange Type
14290  // Entries]:
14291  //
14292  // The DW_AT_upper_bound attribute may be replaced
14293  // by a DW_AT_count attribute, whose value
14294  // describes the number of elements in the
14295  // subrange rather than the value of the last
14296  // element."
14297  //
14298  // So, as DW_AT_upper_bound is not present in this
14299  // case, let's see if there is a DW_AT_count.
14300  if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14301  {
14302  count_present = true;
14303  // We can deduce the upper_bound from the
14304  // lower_bound and the number of elements of the
14305  // array:
14306  int64_t u = lower_bound.get_signed_value() + count;
14307  upper_bound = u - 1;
14308  }
14309 
14310  if (!count_present)
14311  // No upper_bound nor count was present on the DIE, this means
14312  // the array is considered to have an infinite (or rather not
14313  // known) size.
14314  is_infinite = true;
14315  }
14316 
14317  if (UINT64_MAX == upper_bound.get_unsigned_value())
14318  // If the upper_bound size is the max of the integer value
14319  // then it most certainly means unknown size.
14320  is_infinite = true;
14321 
14322  result.reset
14323  (new array_type_def::subrange_type(rdr.env(),
14324  name,
14325  lower_bound,
14326  upper_bound,
14327  location()));
14328  result->is_infinite(is_infinite);
14329 
14330  if (underlying_type)
14331  result->set_underlying_type(underlying_type);
14332 
14333  // Let's ensure the resulting subrange looks metabolically healhty.
14334  ABG_ASSERT(result->is_infinite()
14335  || (result->get_length() ==
14336  (uint64_t) (result->get_upper_bound()
14337  - result->get_lower_bound() + 1)));
14338 
14339  if (associate_type_to_die)
14340  rdr.associate_die_to_type(die, result, where_offset);
14341 
14342  return result;
14343 }
14344 
14345 /// Build the sub-ranges of an array type.
14346 ///
14347 /// This is a sub-routine of build_array_type().
14348 ///
14349 /// @param rdr the context to read from.
14350 ///
14351 /// @param die the DIE of tag DW_TAG_array_type which contains
14352 /// children DIEs that represent the sub-ranges.
14353 ///
14354 /// @param subranges out parameter. This is set to the sub-ranges
14355 /// that are built from @p die.
14356 ///
14357 /// @param where_offset the offset of the DIE where we are "logically"
14358 /// positioned at, in the DIE tree. This is useful when @p die is
14359 /// e.g, DW_TAG_partial_unit that can be included in several places in
14360 /// the DIE tree.
14361 static void
14362 build_subranges_from_array_type_die(reader& rdr,
14363  const Dwarf_Die* die,
14364  array_type_def::subranges_type& subranges,
14365  size_t where_offset,
14366  bool associate_type_to_die)
14367 {
14368  Dwarf_Die child;
14369 
14370  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14371  {
14372  do
14373  {
14374  int child_tag = dwarf_tag(&child);
14375  if (child_tag == DW_TAG_subrange_type)
14376  {
14378  if (associate_type_to_die)
14379  {
14380  // We are being called to create the type, add it to
14381  // the current type graph and associate it to the
14382  // DIE it's been created from.
14384  build_ir_node_from_die(rdr, &child,
14385  /*called_from_public_decl=*/true,
14386  where_offset);
14387  s = is_subrange_type(t);
14388  }
14389  else
14390  // We are being called to create the type but *NOT*
14391  // add it to the current tyupe tree, *NOR* associate
14392  // it to the DIE it's been created from.
14393  s = build_subrange_type(rdr, &child,
14394  where_offset,
14395  /*associate_type_to_die=*/false);
14396  if (s)
14397  subranges.push_back(s);
14398  }
14399  }
14400  while (dwarf_siblingof(&child, &child) == 0);
14401  }
14402 }
14403 
14404 /// Build an array type from a DW_TAG_array_type DIE.
14405 ///
14406 /// @param rdr the DWARF reader to consider.
14407 ///
14408 /// @param die the DIE to read from.
14409 ///
14410 /// @param called_from_public_decl true if this function was called
14411 /// from a context where either a public function or a public variable
14412 /// is being built.
14413 ///
14414 /// @param where_offset the offset of the DIE where we are "logically"
14415 /// positioned at, in the DIE tree. This is useful when @p die is
14416 /// e.g, DW_TAG_partial_unit that can be included in several places in
14417 /// the DIE tree.
14418 ///
14419 /// @return a pointer to the resulting array_type_def.
14420 static array_type_def_sptr
14421 build_array_type(reader& rdr,
14422  Dwarf_Die* die,
14423  bool called_from_public_decl,
14424  size_t where_offset)
14425 {
14426  array_type_def_sptr result;
14427 
14428  if (!die)
14429  return result;
14430 
14431  unsigned tag = dwarf_tag(die);
14432  if (tag != DW_TAG_array_type)
14433  return result;
14434 
14435  decl_base_sptr type_decl;
14436  Dwarf_Die type_die;
14437 
14438  if (die_die_attribute(die, DW_AT_type, type_die))
14439  type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14440  called_from_public_decl,
14441  where_offset));
14442  if (!type_decl)
14443  return result;
14444 
14445  // The call to build_ir_node_from_die() could have triggered the
14446  // creation of the type for this DIE. In that case, just return it.
14447  if (type_base_sptr t = rdr.lookup_type_from_die(die))
14448  {
14449  result = is_array_type(t);
14450  ABG_ASSERT(result);
14451  return result;
14452  }
14453 
14454  type_base_sptr type = is_type(type_decl);
14455  ABG_ASSERT(type);
14456 
14458 
14459  build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14460 
14461  result.reset(new array_type_def(type, subranges, location()));
14462 
14463  return result;
14464 }
14465 
14466 /// Create a typedef_decl from a DW_TAG_typedef DIE.
14467 ///
14468 /// @param rdr the DWARF reader to consider.
14469 ///
14470 /// @param die the DIE to read from.
14471 ///
14472 /// @param called_from_public_decl true if this function was called
14473 /// from a context where either a public function or a public variable
14474 /// is being built.
14475 ///
14476 /// @param where_offset the offset of the DIE where we are "logically"
14477 /// positionned at, in the DIE tree. This is useful when @p die is
14478 /// e.g, DW_TAG_partial_unit that can be included in several places in
14479 /// the DIE tree.
14480 ///
14481 /// @return the newly created typedef_decl.
14482 static typedef_decl_sptr
14483 build_typedef_type(reader& rdr,
14484  Dwarf_Die* die,
14485  bool called_from_public_decl,
14486  size_t where_offset)
14487 {
14488  typedef_decl_sptr result;
14489 
14490  if (!die)
14491  return result;
14492 
14493  unsigned tag = dwarf_tag(die);
14494  if (tag != DW_TAG_typedef)
14495  return result;
14496 
14497  string name, linkage_name;
14498  location loc;
14499  die_loc_and_name(rdr, die, loc, name, linkage_name);
14500 
14501  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14502  if (loc)
14503  result = lookup_typedef_type_per_location(loc.expand(), *corp);
14504 
14505  if (!result)
14506  {
14507  type_base_sptr utype;
14508  Dwarf_Die underlying_type_die;
14509  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14510  // A typedef DIE with no underlying type means a typedef to
14511  // void type.
14512  utype = rdr.env().get_void_type();
14513 
14514  if (!utype)
14515  utype =
14516  is_type(build_ir_node_from_die(rdr,
14517  &underlying_type_die,
14518  called_from_public_decl,
14519  where_offset));
14520  if (!utype)
14521  return result;
14522 
14523  ABG_ASSERT(utype);
14524  result.reset(new typedef_decl(name, utype, loc, linkage_name));
14525 
14526  if ((is_class_or_union_type(utype) || is_enum_type(utype))
14527  && is_anonymous_type(utype))
14528  {
14529  // This is a naming typedef for an enum or a class. Let's
14530  // mark the underlying decl as such.
14531  decl_base_sptr decl = is_decl(utype);
14532  ABG_ASSERT(decl);
14533  decl->set_naming_typedef(result);
14534  }
14535  }
14536 
14537  rdr.associate_die_to_type(die, result, where_offset);
14538 
14539  return result;
14540 }
14541 
14542 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14543 /// denoted by the DIE is not suppressed by a suppression
14544 /// specification associated to the current DWARF reader.
14545 ///
14546 /// Note that if a member variable declaration with the same name as
14547 /// the name of the DIE we are looking at exists, this function returns
14548 /// that existing variable declaration.
14549 ///
14550 /// @param rdr the DWARF reader to use.
14551 ///
14552 /// @param die the DIE representing the variable we are looking at.
14553 ///
14554 /// @param where_offset the offset of the DIE where we are "logically"
14555 /// positionned at, in the DIE tree. This is useful when @p die is
14556 /// e.g, DW_TAG_partial_unit that can be included in several places in
14557 /// the DIE tree.
14558 ///
14559 /// @param result if this is set to an existing var_decl, this means
14560 /// that the function will append the new properties it sees on @p die
14561 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14562 /// new var_decl is going to be allocated and returned.
14563 ///
14564 /// @param is_required_decl_spec this is true iff the variable to
14565 /// build is referred to as being the specification of another
14566 /// variable.
14567 ///
14568 /// @return a pointer to the newly created var_decl. If the var_decl
14569 /// could not be built, this function returns NULL.
14570 static var_decl_sptr
14571 build_or_get_var_decl_if_not_suppressed(reader& rdr,
14572  scope_decl *scope,
14573  Dwarf_Die *die,
14574  size_t where_offset,
14575  var_decl_sptr result,
14576  bool is_required_decl_spec)
14577 {
14578  var_decl_sptr var;
14579  if (variable_is_suppressed(rdr, scope, die, is_required_decl_spec))
14580  return var;
14581 
14582  if (class_decl* class_type = is_class_type(scope))
14583  {
14584  string var_name = die_name(die);
14585  if (!var_name.empty())
14586  if ((var = class_type->find_data_member(var_name)))
14587  return var;
14588  }
14589  var = build_var_decl(rdr, die, where_offset, result);
14590  return var;
14591 }
14592 
14593 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
14594 ///
14595 /// @param rdr the DWARF reader to use.
14596 ///
14597 /// @param die the DIE representing the variable we are looking at.
14598 ///
14599 /// @param where_offset the offset of the DIE where we are "logically"
14600 /// positionned at, in the DIE tree. This is useful when @p die is
14601 /// e.g, DW_TAG_partial_unit that can be included in several places in
14602 /// the DIE tree.
14603 ///
14604 /// @param result if this is set to an existing var_decl, this means
14605 /// that the function will append the new properties it sees on @p die
14606 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14607 /// new var_decl is going to be allocated and returned.
14608 ///
14609 /// @return a pointer to the newly created var_decl. If the var_decl
14610 /// could not be built, this function returns NULL.
14611 static var_decl_sptr
14612 build_var_decl(reader& rdr,
14613  Dwarf_Die *die,
14614  size_t where_offset,
14615  var_decl_sptr result)
14616 {
14617  if (!die)
14618  return result;
14619 
14620  int tag = dwarf_tag(die);
14621  ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14622 
14623  if (!die_is_public_decl(die))
14624  return result;
14625 
14626  type_base_sptr type;
14627  Dwarf_Die type_die;
14628  if (die_die_attribute(die, DW_AT_type, type_die))
14629  {
14630  decl_base_sptr ty =
14631  is_decl(build_ir_node_from_die(rdr, &type_die,
14632  /*called_from_public_decl=*/true,
14633  where_offset));
14634  if (!ty)
14635  return result;
14636  type = is_type(ty);
14637  ABG_ASSERT(type);
14638  }
14639 
14640  if (!type && !result)
14641  return result;
14642 
14643  string name, linkage_name;
14644  location loc;
14645  die_loc_and_name(rdr, die, loc, name, linkage_name);
14646 
14647  if (!result)
14648  result.reset(new var_decl(name, type, loc, linkage_name));
14649  else
14650  {
14651  // We were called to append properties that might have been
14652  // missing from the first version of the variable. And usually
14653  // that missing property is the mangled name or the type.
14654  if (!linkage_name.empty())
14655  result->set_linkage_name(linkage_name);
14656 
14657  if (type)
14658  result->set_type(type);
14659  }
14660 
14661  // Check if a variable symbol with this name is exported by the elf
14662  // binary. If it is, then set the symbol of the variable, if it's
14663  // not set already.
14664  if (!result->get_symbol())
14665  {
14666  elf_symbol_sptr var_sym;
14667  Dwarf_Addr var_addr;
14668 
14669  if (rdr.get_variable_address(die, var_addr))
14670  {
14671  rdr.symtab()->
14672  update_main_symbol(var_addr,
14673  result->get_linkage_name().empty()
14674  ? result->get_name()
14675  : result->get_linkage_name());
14676  var_sym = rdr.variable_symbol_is_exported(var_addr);
14677  }
14678 
14679  if (var_sym)
14680  {
14681  result->set_symbol(var_sym);
14682  // If the linkage name is not set or is wrong, set it to
14683  // the name of the underlying symbol.
14684  string linkage_name = result->get_linkage_name();
14685  if (linkage_name.empty()
14686  || !var_sym->get_alias_from_name(linkage_name))
14687  result->set_linkage_name(var_sym->get_name());
14688  result->set_is_in_public_symbol_table(true);
14689  }
14690  }
14691 
14692  return result;
14693 }
14694 
14695 /// Test if a given function denoted by its DIE and its scope is
14696 /// suppressed by any of the suppression specifications associated to
14697 /// a given context of ELF/DWARF reading.
14698 ///
14699 /// Note that a non-member function which symbol is not exported is
14700 /// also suppressed.
14701 ///
14702 /// @param rdr the ELF/DWARF reading content of interest.
14703 ///
14704 /// @param scope of the scope of the function.
14705 ///
14706 /// @param function_die the DIE representing the function.
14707 ///
14708 /// @param is_declaration_only is true if the DIE denoted by @p die is
14709 /// a declaration-only DIE.
14710 ///
14711 /// @return true iff @p function_die is suppressed by at least one
14712 /// suppression specification attached to the @p rdr.
14713 static bool
14714 function_is_suppressed(const reader& rdr,
14715  const scope_decl* scope,
14716  Dwarf_Die *function_die,
14717  bool is_declaration_only)
14718 {
14719  if (function_die == 0
14720  || dwarf_tag(function_die) != DW_TAG_subprogram)
14721  return false;
14722 
14723  string fname = die_string_attribute(function_die, DW_AT_name);
14724  string flinkage_name = die_linkage_name(function_die);
14725  if (flinkage_name.empty() && rdr.die_is_in_c(function_die))
14726  flinkage_name = fname;
14727  string qualified_name = build_qualified_name(scope, fname);
14728 
14729  // A non-member non-static function which symbol is not exported is
14730  // suppressed.
14731  //
14732  // Note that if the non-member non-static function has an undefined
14733  // symbol, by default, it's not suppressed. Unless we are asked to
14734  // drop undefined symbols too.
14735  if (!is_class_type(scope)
14736  && (!is_declaration_only || rdr.drop_undefined_syms()))
14737  {
14738  Dwarf_Addr fn_addr;
14739  if (!rdr.get_function_address(function_die, fn_addr))
14740  return true;
14741 
14742  elf_symbol_sptr symbol =
14743  rdr.function_symbol_is_exported(fn_addr);
14744  if (!symbol)
14745  return true;
14746  if (!symbol->is_suppressed())
14747  return false;
14748 
14749  // Since there is only one symbol in DWARF associated with an elf_symbol,
14750  // we can assume this is the main symbol then. Otherwise the main hinting
14751  // did not work as expected.
14752  ABG_ASSERT(symbol->is_main_symbol());
14753  if (symbol->has_aliases())
14754  for (elf_symbol_sptr a = symbol->get_next_alias();
14755  !a->is_main_symbol(); a = a->get_next_alias())
14756  if (!a->is_suppressed())
14757  return false;
14758  }
14759 
14760  return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
14761  /*require_drop_property=*/true);
14762 }
14763 
14764 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
14765 /// function denoted by the DIE is not suppressed by a suppression
14766 /// specification associated to the current DWARF reader.
14767 ///
14768 /// Note that if a member function declaration with the same signature
14769 /// (pretty representation) as one of the DIE we are looking at
14770 /// exists, this function returns that existing function declaration.
14771 /// Similarly, if there is already a constructed member function with
14772 /// the same linkage name as the one on the DIE, this function returns
14773 /// that member function.
14774 ///
14775 /// Also note that the function_decl IR returned by this function must
14776 /// be passed to finish_member_function_reading because several
14777 /// properties from the DIE are actually read by that function, and
14778 /// the corresponding properties on the function_decl IR are updated
14779 /// accordingly. This is done to support "updating" a function_decl
14780 /// IR with properties scathered across several DIEs.
14781 ///
14782 /// @param rdr the DWARF reader to use.
14783 ///
14784 /// @param scope the scope of the function we are looking at.
14785 ///
14786 /// @param fn_die the DIE representing the function we are looking at.
14787 ///
14788 /// @param where_offset the offset of the DIE where we are "logically"
14789 /// positionned at, in the DIE tree. This is useful when @p die is
14790 /// e.g, DW_TAG_partial_unit that can be included in several places in
14791 /// the DIE tree.
14792 ///
14793 /// @param is_declaration_only is true if the DIE denoted by @p fn_die
14794 /// is a declaration-only DIE.
14795 ///
14796 /// @param result if this is set to an existing function_decl, this
14797 /// means that the function will append the new properties it sees on
14798 /// @p fn_die to that exising function_decl. Otherwise, if this
14799 /// parameter is NULL, a new function_decl is going to be allocated
14800 /// and returned.
14801 ///
14802 /// @return a pointer to the newly created var_decl. If the var_decl
14803 /// could not be built, this function returns NULL.
14804 static function_decl_sptr
14805 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
14806  scope_decl *scope,
14807  Dwarf_Die *fn_die,
14808  size_t where_offset,
14809  bool is_declaration_only,
14810  function_decl_sptr result)
14811 {
14812  function_decl_sptr fn;
14813  if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
14814  return fn;
14815 
14816  string name = die_name(fn_die);
14817  string linkage_name = die_linkage_name(fn_die);
14818  bool is_dtor = !name.empty() && name[0]== '~';
14819  bool is_virtual = false;
14820  if (is_dtor)
14821  {
14822  Dwarf_Attribute attr;
14823  if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
14824  DW_AT_vtable_elem_location,
14825  &attr))
14826  is_virtual = true;
14827  }
14828 
14829 
14830  // If we've already built an IR for a function with the same
14831  // signature (from another DIE), reuse it, unless that function is a
14832  // virtual C++ destructor. Several virtual C++ destructors with the
14833  // same signature can be implemented by several different ELF
14834  // symbols. So re-using C++ destructors like that can lead to us
14835  // missing some destructors.
14836  if (!result && (!(is_dtor && is_virtual)))
14837  if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
14838  {
14839  fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
14840  rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
14841  rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
14842  return fn;
14843  }
14844 
14845  // If a member function with the same linkage name as the one
14846  // carried by the DIE already exists, then return it.
14847  if (class_decl* klass = is_class_type(scope))
14848  {
14849  string linkage_name = die_linkage_name(fn_die);
14850  fn = klass->find_member_function_sptr(linkage_name);
14851  if (fn)
14852  // We found a member function that has the same signature.
14853  // Let's mark it for update.
14854  result = fn;
14855  }
14856 
14857  if (!fn || !fn->get_symbol())
14858  // We haven't yet been able to construct a function IR, or, we
14859  // have one 'partial' function IR that doesn't have any associated
14860  // symbol yet. Note that in the later case, a function IR without
14861  // any associated symbol will be dropped on the floor by
14862  // potential_member_fn_should_be_dropped. So let's build or a new
14863  // function IR or complete the existing partial IR.
14864  fn = build_function_decl(rdr, fn_die, where_offset, result);
14865 
14866  return fn;
14867 }
14868 
14869 /// Test if a given variable denoted by its DIE and its scope is
14870 /// suppressed by any of the suppression specifications associated to
14871 /// a given context of ELF/DWARF reading.
14872 ///
14873 /// @param rdr the ELF/DWARF reading content of interest.
14874 ///
14875 /// @param scope of the scope of the variable.
14876 ///
14877 /// @param variable_die the DIE representing the variable.
14878 ///
14879 /// @param is_required_decl_spec if true, means that the @p
14880 /// variable_die being considered is for a variable decl that is a
14881 /// specification for a concrete variable being built.
14882 ///
14883 /// @return true iff @p variable_die is suppressed by at least one
14884 /// suppression specification attached to the @p rdr.
14885 static bool
14886 variable_is_suppressed(const reader& rdr,
14887  const scope_decl* scope,
14888  Dwarf_Die *variable_die,
14889  bool is_required_decl_spec)
14890 {
14891  if (variable_die == 0
14892  || (dwarf_tag(variable_die) != DW_TAG_variable
14893  && dwarf_tag(variable_die) != DW_TAG_member))
14894  return false;
14895 
14896  string name = die_string_attribute(variable_die, DW_AT_name);
14897  string linkage_name = die_linkage_name(variable_die);
14898  if (linkage_name.empty() && rdr.die_is_in_c(variable_die))
14899  linkage_name = name;
14900  string qualified_name = build_qualified_name(scope, name);
14901 
14902  // If a non member variable that is a declaration (has no defined
14903  // and exported symbol) and is not the specification of another
14904  // concrete variable, then it's suppressed. This is a size
14905  // optimization; it removes useless declaration-only variables from
14906  // the IR.
14907  if (!is_class_type(scope) && !is_required_decl_spec)
14908  {
14909  Dwarf_Addr var_addr = 0;
14910  if (!rdr.get_variable_address(variable_die, var_addr))
14911  return true;
14912 
14913  elf_symbol_sptr symbol =
14914  rdr.variable_symbol_is_exported(var_addr);
14915  if (!symbol)
14916  return true;
14917  if (!symbol->is_suppressed())
14918  return false;
14919 
14920  // Since there is only one symbol in DWARF associated with an elf_symbol,
14921  // we can assume this is the main symbol then. Otherwise the main hinting
14922  // did not work as expected.
14923  ABG_ASSERT(symbol->is_main_symbol());
14924  if (symbol->has_aliases())
14925  for (elf_symbol_sptr a = symbol->get_next_alias();
14926  !a->is_main_symbol(); a = a->get_next_alias())
14927  if (!a->is_suppressed())
14928  return false;
14929  }
14930 
14931  return suppr::is_variable_suppressed(rdr,
14932  qualified_name,
14933  linkage_name,
14934  /*require_drop_property=*/true);
14935 }
14936 
14937 /// Test if a type (designated by a given DIE) in a given scope is
14938 /// suppressed by the suppression specifications that are associated
14939 /// to a given DWARF reader.
14940 ///
14941 /// @param rdr the DWARF reader to consider.
14942 ///
14943 /// @param scope of the scope of the type DIE to consider.
14944 ///
14945 /// @param type_die the DIE that designates the type to consider.
14946 ///
14947 /// @param type_is_private out parameter. If this function returns
14948 /// true (the type @p type_die is suppressed) and if the type was
14949 /// suppressed because it's private then this parameter is set to
14950 /// true.
14951 ///
14952 /// @return true iff the type designated by the DIE @p type_die, in
14953 /// the scope @p scope is suppressed by at the suppression
14954 /// specifications associated to the current DWARF reader.
14955 static bool
14956 type_is_suppressed(const reader& rdr,
14957  const scope_decl* scope,
14958  Dwarf_Die *type_die,
14959  bool &type_is_private)
14960 {
14961  if (type_die == 0
14962  || (dwarf_tag(type_die) != DW_TAG_enumeration_type
14963  && dwarf_tag(type_die) != DW_TAG_class_type
14964  && dwarf_tag(type_die) != DW_TAG_structure_type
14965  && dwarf_tag(type_die) != DW_TAG_union_type))
14966  return false;
14967 
14968  string type_name, linkage_name;
14969  location type_location;
14970  die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
14971  string qualified_name = build_qualified_name(scope, type_name);
14972 
14973  return suppr::is_type_suppressed(rdr,
14974  qualified_name,
14975  type_location,
14976  type_is_private,
14977  /*require_drop_property=*/true);
14978 }
14979 
14980 /// Test if a type (designated by a given DIE) in a given scope is
14981 /// suppressed by the suppression specifications that are associated
14982 /// to a given DWARF reader.
14983 ///
14984 /// @param rdr the DWARF reader to consider.
14985 ///
14986 /// @param scope of the scope of the type DIE to consider.
14987 ///
14988 /// @param type_die the DIE that designates the type to consider.
14989 ///
14990 /// @return true iff the type designated by the DIE @p type_die, in
14991 /// the scope @p scope is suppressed by at the suppression
14992 /// specifications associated to the current DWARF reader.
14993 static bool
14994 type_is_suppressed(const reader& rdr,
14995  const scope_decl* scope,
14996  Dwarf_Die *type_die)
14997 {
14998  bool type_is_private = false;
14999  return type_is_suppressed(rdr, scope, type_die, type_is_private);
15000 }
15001 
15002 /// Get the opaque version of a type that was suppressed because it's
15003 /// a private type.
15004 ///
15005 /// The opaque version version of the type is just a declared-only
15006 /// version of the type (class, union or enum type) denoted by @p
15007 /// type_die.
15008 ///
15009 /// @param rdr the DWARF reader in use.
15010 ///
15011 /// @param scope the scope of the type die we are looking at.
15012 ///
15013 /// @param type_die the type DIE we are looking at.
15014 ///
15015 /// @param where_offset the offset of the DIE where we are "logically"
15016 /// positionned at, in the DIE tree. This is useful when @p die is
15017 /// e.g, DW_TAG_partial_unit that can be included in several places in
15018 /// the DIE tree.
15019 ///
15020 /// @return the opaque version of the type denoted by @p type_die or
15021 /// nil if no opaque version was found.
15023 get_opaque_version_of_type(reader &rdr,
15024  scope_decl *scope,
15025  Dwarf_Die *type_die,
15026  size_t where_offset)
15027 {
15028  type_or_decl_base_sptr result;
15029 
15030  if (type_die == 0)
15031  return result;
15032 
15033  unsigned tag = dwarf_tag(type_die);
15034  if (tag != DW_TAG_class_type
15035  && tag != DW_TAG_structure_type
15036  && tag != DW_TAG_union_type
15037  && tag != DW_TAG_enumeration_type)
15038  return result;
15039 
15040  string type_name, linkage_name;
15041  location type_location;
15042  die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15043  if (!type_location)
15044  return result;
15045 
15046  string qualified_name = build_qualified_name(scope, type_name);
15047 
15048  //
15049  // TODO: also handle declaration-only unions. To do that, we mostly
15050  // need to adapt add_or_update_union_type to make it schedule
15051  // declaration-only unions for resolution too.
15052  //
15053  if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15054  {
15055  string_classes_or_unions_map::const_iterator i =
15056  rdr.declaration_only_classes().find(qualified_name);
15057  if (i != rdr.declaration_only_classes().end())
15058  result = i->second.back();
15059 
15060  if (!result)
15061  {
15062  // So we didn't find any pre-existing forward-declared-only
15063  // class for the class definition that we could return as an
15064  // opaque type. So let's build one.
15065  //
15066  // TODO: we need to be able to do this for unions too!
15067  class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15068  /*alignment=*/0, /*size=*/0,
15069  tag == DW_TAG_structure_type,
15070  type_location,
15071  decl_base::VISIBILITY_DEFAULT));
15072  klass->set_is_declaration_only(true);
15073  klass->set_is_artificial(die_is_artificial(type_die));
15074  add_decl_to_scope(klass, scope);
15075  rdr.associate_die_to_type(type_die, klass, where_offset);
15076  rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15077  result = klass;
15078  }
15079  }
15080 
15081  if (tag == DW_TAG_enumeration_type)
15082  {
15083  string_enums_map::const_iterator i =
15084  rdr.declaration_only_enums().find(qualified_name);
15085  if (i != rdr.declaration_only_enums().end())
15086  result = i->second.back();
15087 
15088  if (!result)
15089  {
15090  uint64_t size = 0;
15091  if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15092  size *= 8;
15093  type_decl_sptr underlying_type =
15094  build_enum_underlying_type(rdr, type_name, size,
15095  /*anonymous=*/true);
15096  enum_type_decl::enumerators enumeratorz;
15097  enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15098  type_location,
15099  underlying_type,
15100  enumeratorz,
15101  linkage_name));
15102  enum_type->set_is_artificial(die_is_artificial(type_die));
15103  add_decl_to_scope(enum_type, scope);
15104  result = enum_type;
15105  }
15106  }
15107 
15108  return result;
15109 }
15110 
15111 /// Create a function symbol with a given name.
15112 ///
15113 /// @param sym_name the name of the symbol to create.
15114 ///
15115 /// @param env the environment to create the symbol in.
15116 ///
15117 /// @return the newly created symbol.
15119 create_default_fn_sym(const string& sym_name, const environment& env)
15120 {
15121  elf_symbol::version ver;
15122  elf_symbol_sptr result =
15123  elf_symbol::create(env,
15124  /*symbol index=*/ 0,
15125  /*symbol size=*/ 0,
15126  sym_name,
15127  /*symbol type=*/ elf_symbol::FUNC_TYPE,
15128  /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15129  /*symbol is defined=*/ true,
15130  /*symbol is common=*/ false,
15131  /*symbol version=*/ ver,
15132  /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15133  return result;
15134 }
15135 
15136 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15137 ///
15138 /// @param rdr the DWARF reader to use
15139 ///
15140 /// @param die the DW_TAG_subprogram DIE to read from.
15141 ///
15142 /// @param where_offset the offset of the DIE where we are "logically"
15143 /// positionned at, in the DIE tree. This is useful when @p die is
15144 /// e.g, DW_TAG_partial_unit that can be included in several places in
15145 /// the DIE tree.
15146 ///
15147 /// @param called_for_public_decl this is set to true if the function
15148 /// was called for a public (function) decl.
15149 static function_decl_sptr
15150 build_function_decl(reader& rdr,
15151  Dwarf_Die* die,
15152  size_t where_offset,
15153  function_decl_sptr fn)
15154 {
15155  function_decl_sptr result = fn;
15156  if (!die)
15157  return result;
15158  ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
15159 
15160  if (!die_is_public_decl(die))
15161  return result;
15162 
15163  translation_unit_sptr tu = rdr.cur_transl_unit();
15164  ABG_ASSERT(tu);
15165 
15166  string fname, flinkage_name;
15167  location floc;
15168  die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15169 
15170  size_t is_inline = die_is_declared_inline(die);
15171  class_or_union_sptr is_method =
15172  is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15173 
15174  if (result)
15175  {
15176  // Add the properties that might have been missing from the
15177  // first declaration of the function. For now, it usually is
15178  // the mangled name that goes missing in the first declarations.
15179  //
15180  // Also note that if 'fn' has just been cloned, the current
15181  // linkage name (of the current DIE) might be different from the
15182  // linkage name of 'fn'. In that case, update the linkage name
15183  // of 'fn' too.
15184  if (!flinkage_name.empty()
15185  && result->get_linkage_name() != flinkage_name)
15186  result->set_linkage_name(flinkage_name);
15187  if (floc)
15188  if (!result->get_location())
15189  result->set_location(floc);
15190  }
15191  else
15192  {
15193  function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15194  where_offset));
15195  if (!fn_type)
15196  return result;
15197 
15198  maybe_canonicalize_type(fn_type, rdr);
15199 
15200  result.reset(is_method
15201  ? new method_decl(fname, fn_type,
15202  is_inline, floc,
15203  flinkage_name)
15204  : new function_decl(fname, fn_type,
15205  is_inline, floc,
15206  flinkage_name));
15207  }
15208 
15209  // Set the symbol of the function. If the linkage name is not set
15210  // or is wrong, set it to the name of the underlying symbol.
15211  if (!result->get_symbol())
15212  {
15213  elf_symbol_sptr fn_sym;
15214  Dwarf_Addr fn_addr;
15215  if (rdr.get_function_address(die, fn_addr))
15216  {
15217  rdr.symtab()->
15218  update_main_symbol(fn_addr,
15219  result->get_linkage_name().empty()
15220  ? result->get_name()
15221  : result->get_linkage_name());
15222  fn_sym = rdr.function_symbol_is_exported(fn_addr);
15223  }
15224 
15225  if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15226  {
15227  result->set_symbol(fn_sym);
15228  string linkage_name = result->get_linkage_name();
15229  if (linkage_name.empty())
15230  result->set_linkage_name(fn_sym->get_name());
15231  result->set_is_in_public_symbol_table(true);
15232  }
15233  }
15234 
15235  rdr.associate_die_to_type(die, result->get_type(), where_offset);
15236 
15237  size_t die_offset = dwarf_dieoffset(die);
15238 
15239  if (fn
15240  && is_member_function(fn)
15242  && !result->get_linkage_name().empty())
15243  // This function is a virtual member function which has its
15244  // linkage name *and* and has its underlying symbol correctly set.
15245  // It thus doesn't need any fixup related to elf symbol. So
15246  // remove it from the set of virtual member functions with linkage
15247  // names and no elf symbol that need to be fixed up.
15248  rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15249  return result;
15250 }
15251 
15252 /// Canonicalize a type if it's suitable for early canonicalizing, or,
15253 /// if it's not, schedule it for late canonicalization, after the
15254 /// debug info of the current translation unit has been fully read.
15255 ///
15256 /// A (composite) type is deemed suitable for early canonicalizing iff
15257 /// all of its sub-types are canonicalized themselve. Non composite
15258 /// types are always deemed suitable for early canonicalization.
15259 ///
15260 /// Note that this function knows how to deal with anonymous classes,
15261 /// structs and enums, unlike the overload below:
15262 ///
15263 /// @param t the type DIE to consider for canonicalization.
15264 ///
15265 /// @param rdr the @ref reader to use.
15266 static void
15267 maybe_canonicalize_type(const type_base_sptr& t,
15268  reader& rdr)
15269 {
15270  if (!t)
15271  return;
15272 
15273  type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15274  if (is_class_type(peeled_type)
15275  || is_union_type(peeled_type)
15276  || is_function_type(peeled_type)
15277  || is_array_type(peeled_type)
15278  || is_qualified_type(peeled_type)
15279  || is_enum_type(peeled_type)
15280  ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15281  // We delay canonicalization of classes/unions or typedef,
15282  // pointers, references and array to classes/unions. This is
15283  // because the (underlying) class might not be finished yet and we
15284  // might not be able to able detect it here (thinking about
15285  // classes that are work-in-progress, or classes that might be
15286  // later amended by some DWARF construct). So we err on the safe
15287  // side. We also delay canonicalization for array and qualified
15288  // types because they can be edited (in particular by
15289  // maybe_strip_qualification) after they are initially built.
15290  rdr.schedule_type_for_late_canonicalization(t);
15292  rdr.schedule_type_for_late_canonicalization(t);
15293  else
15294  canonicalize(t);
15295 }
15296 
15297 /// If a given decl is a member type declaration, set its access
15298 /// specifier from the DIE that represents it.
15299 ///
15300 /// @param member_type_declaration the member type declaration to
15301 /// consider.
15302 static void
15303 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15304  Dwarf_Die* die)
15305 {
15306  if (is_type(member_type_declaration)
15307  && is_member_decl(member_type_declaration))
15308  {
15309  class_or_union* scope =
15310  is_class_or_union_type(member_type_declaration->get_scope());
15311  ABG_ASSERT(scope);
15312 
15313  access_specifier access = public_access;
15314  if (class_decl* cl = is_class_type(scope))
15315  if (!cl->is_struct())
15316  access = private_access;
15317 
15318  die_access_specifier(die, access);
15319  set_member_access_specifier(member_type_declaration, access);
15320  }
15321 }
15322 
15323 /// This function tests if a given function which might be intented to
15324 /// be added to a class scope (to become a member function) should be
15325 /// dropped on the floor instead and not be added to the class.
15326 ///
15327 /// This is a subroutine of build_ir_node_from_die.
15328 ///
15329 /// @param fn the function to consider.
15330 ///
15331 /// @param scope the scope the function is intended to be added
15332 /// to. This might be of class type or not.
15333 ///
15334 /// @param fn_die the DWARF die of @p fn.
15335 ///
15336 /// @return true iff @p fn should be dropped on the floor.
15337 static bool
15338 potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15339  Dwarf_Die *fn_die)
15340 {
15341  if (!fn || fn->get_scope())
15342  return false;
15343 
15344  if (// A function that is not virtual ...
15345  !die_is_virtual(fn_die)
15346  // ... has a linkage name ...
15347  && !fn->get_linkage_name().empty()
15348  // .. and yet has no ELF symbol associated ...
15349  && !fn->get_symbol())
15350  // Should not be added to its class scope.
15351  //
15352  // Why would it? It's not part of the ABI anyway, as it doesn't
15353  // have any ELF symbol associated and is not a virtual member
15354  // function. It just constitutes bloat in the IR and might even
15355  // induce spurious change reports down the road.
15356  return true;
15357 
15358  return false;
15359 }
15360 
15361 /// Build an IR node from a given DIE and add the node to the current
15362 /// IR being build and held in the DWARF reader. Doing that is called
15363 /// "emitting an IR node for the DIE".
15364 ///
15365 /// @param rdr the DWARF reader.
15366 ///
15367 /// @param die the DIE to consider.
15368 ///
15369 /// @param scope the scope under which the resulting IR node has to be
15370 /// added.
15371 ///
15372 /// @param called_from_public_decl set to yes if this function is
15373 /// called from the functions used to build a public decl (functions
15374 /// and variables). In that case, this function accepts building IR
15375 /// nodes representing types. Otherwise, this function only creates
15376 /// IR nodes representing public decls (functions and variables).
15377 /// This is done to avoid emitting IR nodes for types that are not
15378 /// referenced by public functions or variables.
15379 ///
15380 /// @param where_offset the offset of the DIE where we are "logically"
15381 /// positionned at, in the DIE tree. This is useful when @p die is
15382 /// e.g, DW_TAG_partial_unit that can be included in several places in
15383 /// the DIE tree.
15384 ///
15385 /// @param is_required_decl_spec if true, it means the ir node to
15386 /// build is for a decl that is a specification for another decl that
15387 /// is concrete. If you don't know what this is, set it to false.
15388 ///
15389 /// @param is_declaration_only is true if the DIE denoted by @p die is
15390 /// a declaration-only DIE.
15391 ///
15392 /// @return the resulting IR node.
15394 build_ir_node_from_die(reader& rdr,
15395  Dwarf_Die* die,
15396  scope_decl* scope,
15397  bool called_from_public_decl,
15398  size_t where_offset,
15399  bool is_declaration_only,
15400  bool is_required_decl_spec)
15401 {
15402  type_or_decl_base_sptr result;
15403 
15404  if (!die || !scope)
15405  return result;
15406 
15407  int tag = dwarf_tag(die);
15408 
15409  if (!called_from_public_decl)
15410  {
15411  if (rdr.load_all_types() && die_is_type(die))
15412  /* We were instructed to load debug info for all types,
15413  included those that are not reachable from a public
15414  declaration. So load the debug info for this type. */;
15415  else if (tag != DW_TAG_subprogram
15416  && tag != DW_TAG_variable
15417  && tag != DW_TAG_member
15418  && tag != DW_TAG_namespace)
15419  return result;
15420  }
15421 
15422  const die_source source_of_die = rdr.get_die_source(die);
15423 
15424  if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15425  source_of_die)))
15426  {
15427  if (rdr.load_all_types())
15428  if (called_from_public_decl)
15429  if (type_base_sptr t = is_type(result))
15430  if (corpus *abi_corpus = scope->get_corpus())
15431  abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15432 
15433  return result;
15434  }
15435 
15436  // This is *the* bit of code that ensures we have the right notion
15437  // of "declared" at any point in a DIE chain formed from
15438  // DW_AT_abstract_origin and DW_AT_specification links. There should
15439  // be no other callers of die_is_declaration_only.
15440  is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15441 
15442  switch (tag)
15443  {
15444  // Type DIEs we support.
15445  case DW_TAG_base_type:
15446  if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15447  {
15448  result =
15449  add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15450  canonicalize(t);
15451  }
15452  break;
15453 
15454  case DW_TAG_typedef:
15455  {
15457  t = is_typedef(scope->find_member_type(die_name(die)));
15458 
15459  if (!t)
15460  t = build_typedef_type(rdr, die,
15461  called_from_public_decl,
15462  where_offset);
15463 
15464  result = add_decl_to_scope(t, scope);
15465  if (result)
15466  {
15467  maybe_set_member_type_access_specifier(is_decl(result), die);
15468  maybe_canonicalize_type(t, rdr);
15469  }
15470  }
15471  break;
15472 
15473  case DW_TAG_pointer_type:
15474  {
15476  build_pointer_type_def(rdr, die,
15477  called_from_public_decl,
15478  where_offset);
15479  if (p)
15480  {
15481  result =
15482  add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15483  ABG_ASSERT(result->get_translation_unit());
15484  maybe_canonicalize_type(p, rdr);
15485  }
15486  }
15487  break;
15488 
15489  case DW_TAG_reference_type:
15490  case DW_TAG_rvalue_reference_type:
15491  {
15493  build_reference_type(rdr, die,
15494  called_from_public_decl,
15495  where_offset);
15496  if (r)
15497  {
15498  result =
15499  add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15500 
15501  rdr.associate_die_to_type(die, r, where_offset);
15502  maybe_canonicalize_type(r, rdr);
15503  }
15504  }
15505  break;
15506 
15507  case DW_TAG_const_type:
15508  case DW_TAG_volatile_type:
15509  case DW_TAG_restrict_type:
15510  {
15511  type_base_sptr q =
15512  build_qualified_type(rdr, die,
15513  called_from_public_decl,
15514  where_offset);
15515  if (q)
15516  {
15517  // Strip some potentially redundant type qualifiers from
15518  // the qualified type we just built.
15519  decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15520  rdr);
15521  if (!d)
15522  d = get_type_declaration(q);
15523  ABG_ASSERT(d);
15524  type_base_sptr ty = is_type(d);
15525  // Associate the die to type ty again because 'ty'might be
15526  // different from 'q', because 'ty' is 'q' possibly
15527  // stripped from some redundant type qualifier.
15528  rdr.associate_die_to_type(die, ty, where_offset);
15529  result =
15530  add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15531  maybe_canonicalize_type(is_type(result), rdr);
15532  }
15533  }
15534  break;
15535 
15536  case DW_TAG_enumeration_type:
15537  {
15538  bool type_is_private = false;
15539  bool type_suppressed =
15540  type_is_suppressed(rdr, scope, die, type_is_private);
15541  if (type_suppressed && type_is_private)
15542  {
15543  // The type is suppressed because it's private. If other
15544  // non-suppressed and declaration-only instances of this
15545  // type exist in the current corpus, then it means those
15546  // non-suppressed instances are opaque versions of the
15547  // suppressed private type. Lets return one of these opaque
15548  // types then.
15549  result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15550  maybe_canonicalize_type(is_type(result), rdr);
15551  }
15552  else if (!type_suppressed)
15553  {
15554  enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15555  where_offset,
15556  is_declaration_only);
15557  result = add_decl_to_scope(e, scope);
15558  if (result)
15559  {
15560  maybe_set_member_type_access_specifier(is_decl(result), die);
15561  maybe_canonicalize_type(is_type(result), rdr);
15562  }
15563  }
15564  }
15565  break;
15566 
15567  case DW_TAG_class_type:
15568  case DW_TAG_structure_type:
15569  {
15570  bool type_is_private = false;
15571  bool type_suppressed=
15572  type_is_suppressed(rdr, scope, die, type_is_private);
15573 
15574  if (type_suppressed && type_is_private)
15575  {
15576  // The type is suppressed because it's private. If other
15577  // non-suppressed and declaration-only instances of this
15578  // type exist in the current corpus, then it means those
15579  // non-suppressed instances are opaque versions of the
15580  // suppressed private type. Lets return one of these opaque
15581  // types then.
15582  result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15583  maybe_canonicalize_type(is_type(result), rdr);
15584  }
15585  else if (!type_suppressed)
15586  {
15587  Dwarf_Die spec_die;
15588  scope_decl_sptr scop;
15589  class_decl_sptr klass;
15590  if (die_die_attribute(die, DW_AT_specification, spec_die))
15591  {
15592  scope_decl_sptr skope =
15593  get_scope_for_die(rdr, &spec_die,
15594  called_from_public_decl,
15595  where_offset);
15596  ABG_ASSERT(skope);
15597  decl_base_sptr cl =
15598  is_decl(build_ir_node_from_die(rdr, &spec_die,
15599  skope.get(),
15600  called_from_public_decl,
15601  where_offset,
15602  is_declaration_only,
15603  /*is_required_decl_spec=*/false));
15604  ABG_ASSERT(cl);
15605  klass = dynamic_pointer_cast<class_decl>(cl);
15606  ABG_ASSERT(klass);
15607 
15608  klass =
15609  add_or_update_class_type(rdr, die,
15610  skope.get(),
15611  tag == DW_TAG_structure_type,
15612  klass,
15613  called_from_public_decl,
15614  where_offset,
15615  is_declaration_only);
15616  }
15617  else
15618  klass =
15619  add_or_update_class_type(rdr, die, scope,
15620  tag == DW_TAG_structure_type,
15621  class_decl_sptr(),
15622  called_from_public_decl,
15623  where_offset,
15624  is_declaration_only);
15625  result = klass;
15626  if (klass)
15627  {
15628  maybe_set_member_type_access_specifier(klass, die);
15629  maybe_canonicalize_type(klass, rdr);
15630  }
15631  }
15632  }
15633  break;
15634  case DW_TAG_union_type:
15635  if (!type_is_suppressed(rdr, scope, die))
15636  {
15637  union_decl_sptr union_type =
15638  add_or_update_union_type(rdr, die, scope,
15639  union_decl_sptr(),
15640  called_from_public_decl,
15641  where_offset,
15642  is_declaration_only);
15643  if (union_type)
15644  {
15645  maybe_set_member_type_access_specifier(union_type, die);
15646  maybe_canonicalize_type(union_type, rdr);
15647  }
15648  result = union_type;
15649  }
15650  break;
15651  case DW_TAG_string_type:
15652  break;
15653  case DW_TAG_subroutine_type:
15654  {
15655  function_type_sptr f = build_function_type(rdr, die,
15656  class_decl_sptr(),
15657  where_offset);
15658  if (f)
15659  {
15660  result = f;
15661  result->set_is_artificial(false);
15662  maybe_canonicalize_type(f, rdr);
15663  }
15664  }
15665  break;
15666  case DW_TAG_array_type:
15667  {
15668  array_type_def_sptr a = build_array_type(rdr,
15669  die,
15670  called_from_public_decl,
15671  where_offset);
15672  if (a)
15673  {
15674  result =
15675  add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
15676  rdr.associate_die_to_type(die, a, where_offset);
15677  maybe_canonicalize_type(a, rdr);
15678  }
15679  break;
15680  }
15681  case DW_TAG_subrange_type:
15682  {
15683  // If we got here, this means the subrange type is a "free
15684  // form" defined in the global namespace of the current
15685  // translation unit, like what is found in Ada.
15687  build_subrange_type(rdr, die, where_offset);
15688  if (s)
15689  {
15690  result =
15691  add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
15692  rdr.associate_die_to_type(die, s, where_offset);
15693  maybe_canonicalize_type(s, rdr);
15694  }
15695  }
15696  break;
15697  case DW_TAG_packed_type:
15698  break;
15699  case DW_TAG_set_type:
15700  break;
15701  case DW_TAG_file_type:
15702  break;
15703  case DW_TAG_ptr_to_member_type:
15704  break;
15705  case DW_TAG_thrown_type:
15706  break;
15707  case DW_TAG_interface_type:
15708  break;
15709  case DW_TAG_unspecified_type:
15710  break;
15711  case DW_TAG_shared_type:
15712  break;
15713 
15714  case DW_TAG_compile_unit:
15715  // We shouldn't reach this point b/c this should be handled by
15716  // build_translation_unit.
15718 
15719  case DW_TAG_namespace:
15720  case DW_TAG_module:
15721  result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
15722  break;
15723 
15724  case DW_TAG_variable:
15725  case DW_TAG_member:
15726  {
15727  Dwarf_Die spec_die;
15728  bool var_is_cloned = false;
15729 
15730  if (tag == DW_TAG_member)
15731  ABG_ASSERT(!is_c_language(rdr.cur_transl_unit()->get_language()));
15732 
15733  if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15734  || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15735  spec_die, false)))
15736  {
15737  scope_decl_sptr spec_scope =
15738  get_scope_for_die(rdr, &spec_die,
15739  /*called_from_public_decl=*/
15740  die_is_effectively_public_decl(rdr, die),
15741  where_offset);
15742  if (spec_scope)
15743  {
15744  decl_base_sptr d =
15745  is_decl(build_ir_node_from_die(rdr, &spec_die,
15746  spec_scope.get(),
15747  called_from_public_decl,
15748  where_offset,
15749  is_declaration_only,
15750  /*is_required_decl_spec=*/true));
15751  if (d)
15752  {
15753  var_decl_sptr m =
15754  dynamic_pointer_cast<var_decl>(d);
15755  if (var_is_cloned)
15756  m = m->clone();
15757  m = build_var_decl(rdr, die, where_offset, m);
15758  if (is_data_member(m))
15759  {
15760  set_member_is_static(m, true);
15761  rdr.associate_die_to_decl(die, m, where_offset,
15762  /*associate_by_repr=*/false);
15763  }
15764  else
15765  {
15766  ABG_ASSERT(has_scope(m));
15767  rdr.var_decls_to_re_add_to_tree().push_back(m);
15768  }
15769  ABG_ASSERT(m->get_scope());
15770  rdr.maybe_add_var_to_exported_decls(m.get());
15771  result = m;
15772  }
15773  }
15774  }
15775  else if (var_decl_sptr v =
15776  build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
15777  where_offset,
15778  /*result=*/var_decl_sptr(),
15779  is_required_decl_spec))
15780  {
15781  result = add_decl_to_scope(v, scope);
15782  ABG_ASSERT(is_decl(result)->get_scope());
15783  v = dynamic_pointer_cast<var_decl>(result);
15784  ABG_ASSERT(v);
15785  ABG_ASSERT(v->get_scope());
15786  rdr.var_decls_to_re_add_to_tree().push_back(v);
15787  rdr.maybe_add_var_to_exported_decls(v.get());
15788  }
15789  }
15790  break;
15791 
15792  case DW_TAG_subprogram:
15793  {
15794  Dwarf_Die spec_die;
15795  Dwarf_Die abstract_origin_die;
15796  Dwarf_Die *interface_die = 0, *origin_die = 0;
15797  scope_decl_sptr interface_scope;
15798  if (die_is_artificial(die))
15799  break;
15800 
15801  function_decl_sptr fn;
15802  bool has_spec = die_die_attribute(die, DW_AT_specification,
15803  spec_die, true);
15804  bool has_abstract_origin =
15805  die_die_attribute(die, DW_AT_abstract_origin,
15806  abstract_origin_die, true);
15807  if (has_spec || has_abstract_origin)
15808  {
15809  interface_die =
15810  has_spec
15811  ? &spec_die
15812  : &abstract_origin_die;
15813  origin_die =
15814  has_abstract_origin
15815  ? &abstract_origin_die
15816  : &spec_die;
15817 
15818  string linkage_name = die_linkage_name(die);
15819  string spec_linkage_name = die_linkage_name(interface_die);
15820 
15821  interface_scope = get_scope_for_die(rdr, interface_die,
15822  called_from_public_decl,
15823  where_offset);
15824  if (interface_scope)
15825  {
15826  decl_base_sptr d;
15827  class_decl_sptr c = is_class_type(interface_scope);
15828  if (c && !linkage_name.empty())
15829  d = c->find_member_function_sptr(linkage_name);
15830 
15831  if (!d)
15832  d = is_decl(build_ir_node_from_die(rdr,
15833  origin_die,
15834  interface_scope.get(),
15835  called_from_public_decl,
15836  where_offset,
15837  is_declaration_only,
15838  /*is_required_decl_spec=*/true));
15839  if (d)
15840  {
15841  fn = dynamic_pointer_cast<function_decl>(d);
15842  if (has_abstract_origin
15843  && (linkage_name != spec_linkage_name))
15844  // The current DIE has 'd' as abstract orign,
15845  // and has a linkage name that is different
15846  // from from the linkage name of 'd'. That
15847  // means, the current DIE represents a clone
15848  // of 'd'.
15849  fn = fn->clone();
15850  }
15851  }
15852  }
15853  rdr.scope_stack().push(scope);
15854 
15855  scope_decl* logical_scope =
15856  interface_scope
15857  ? interface_scope.get()
15858  : scope;
15859 
15860  result = build_or_get_fn_decl_if_not_suppressed(rdr, logical_scope,
15861  die, where_offset,
15862  is_declaration_only,
15863  fn);
15864 
15865  if (result && !fn)
15866  {
15867  if (potential_member_fn_should_be_dropped(is_function_decl(result),
15868  die)
15869  && !is_required_decl_spec)
15870  {
15871  result.reset();
15872  break;
15873  }
15874  result = add_decl_to_scope(is_decl(result), logical_scope);
15875  }
15876 
15877  fn = is_function_decl(result);
15878  if (fn && is_member_function(fn))
15879  {
15880  class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
15881  sptr_utils::noop_deleter());
15882  ABG_ASSERT(klass);
15883  finish_member_function_reading(die, fn, klass, rdr);
15884  }
15885 
15886  if (fn)
15887  {
15888  rdr.maybe_add_fn_to_exported_decls(fn.get());
15889  rdr.associate_die_to_decl(die, fn, where_offset,
15890  /*associate_by_repr=*/false);
15891  maybe_canonicalize_type(fn->get_type(), rdr);
15892  }
15893 
15894  rdr.scope_stack().pop();
15895  }
15896  break;
15897 
15898  case DW_TAG_formal_parameter:
15899  // We should not read this case as it should have been dealt
15900  // with by build_function_decl above.
15902 
15903  case DW_TAG_constant:
15904  break;
15905  case DW_TAG_enumerator:
15906  break;
15907 
15908  case DW_TAG_partial_unit:
15909  case DW_TAG_imported_unit:
15910  // For now, the DIEs under these are read lazily when they are
15911  // referenced by a public decl DIE that is under a
15912  // DW_TAG_compile_unit, so we shouldn't get here.
15914 
15915  // Other declaration we don't really intend to support yet.
15916  case DW_TAG_dwarf_procedure:
15917  case DW_TAG_imported_declaration:
15918  case DW_TAG_entry_point:
15919  case DW_TAG_label:
15920  case DW_TAG_lexical_block:
15921  case DW_TAG_unspecified_parameters:
15922  case DW_TAG_variant:
15923  case DW_TAG_common_block:
15924  case DW_TAG_common_inclusion:
15925  case DW_TAG_inheritance:
15926  case DW_TAG_inlined_subroutine:
15927  case DW_TAG_with_stmt:
15928  case DW_TAG_access_declaration:
15929  case DW_TAG_catch_block:
15930  case DW_TAG_friend:
15931  case DW_TAG_namelist:
15932  case DW_TAG_namelist_item:
15933  case DW_TAG_template_type_parameter:
15934  case DW_TAG_template_value_parameter:
15935  case DW_TAG_try_block:
15936  case DW_TAG_variant_part:
15937  case DW_TAG_imported_module:
15938  case DW_TAG_condition:
15939  case DW_TAG_type_unit:
15940  case DW_TAG_template_alias:
15941  case DW_TAG_lo_user:
15942  case DW_TAG_MIPS_loop:
15943  case DW_TAG_format_label:
15944  case DW_TAG_function_template:
15945  case DW_TAG_class_template:
15946  case DW_TAG_GNU_BINCL:
15947  case DW_TAG_GNU_EINCL:
15948  case DW_TAG_GNU_template_template_param:
15949  case DW_TAG_GNU_template_parameter_pack:
15950  case DW_TAG_GNU_formal_parameter_pack:
15951  case DW_TAG_GNU_call_site:
15952  case DW_TAG_GNU_call_site_parameter:
15953  case DW_TAG_hi_user:
15954  default:
15955  break;
15956  }
15957 
15958  if (result && tag != DW_TAG_subroutine_type)
15959  rdr.associate_die_to_decl(die, is_decl(result), where_offset,
15960  /*associate_by_repr=*/false);
15961 
15962  if (result)
15963  if (rdr.load_all_types())
15964  if (called_from_public_decl)
15965  if (type_base_sptr t = is_type(result))
15966  if (corpus *abi_corpus = scope->get_corpus())
15967  abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15968 
15969  return result;
15970 }
15971 
15972 /// Build the IR node for a void type.
15973 ///
15974 /// @param rdr the DWARF reader to use.
15975 ///
15976 /// @return the void type node.
15977 static decl_base_sptr
15978 build_ir_node_for_void_type(reader& rdr)
15979 {
15980  const environment& env = rdr.env();
15981 
15982  type_base_sptr t = env.get_void_type();
15983  decl_base_sptr type_declaration = get_type_declaration(t);
15984  if (!has_scope(type_declaration))
15985  add_decl_to_scope(type_declaration,
15986  rdr.cur_transl_unit()->get_global_scope());
15987  canonicalize(t);
15988  return type_declaration;
15989 }
15990 
15991 /// Build the IR node for a variadic parameter type.
15992 ///
15993 /// @param rdr the DWARF reader to use.
15994 ///
15995 /// @return the variadic parameter type.
15996 static decl_base_sptr
15997 build_ir_node_for_variadic_parameter_type(reader &rdr)
15998 {
15999 
16000  const environment& env = rdr.env();
16001 
16002  type_base_sptr t = env.get_variadic_parameter_type();
16003  decl_base_sptr type_declaration = get_type_declaration(t);
16004  if (!has_scope(type_declaration))
16005  add_decl_to_scope(type_declaration,
16006  rdr.cur_transl_unit()->get_global_scope());
16007  canonicalize(t);
16008  return type_declaration;
16009 }
16010 
16011 /// Build an IR node from a given DIE and add the node to the current
16012 /// IR being build and held in the DWARF reader. Doing that is called
16013 /// "emitting an IR node for the DIE".
16014 ///
16015 /// @param rdr the DWARF reader.
16016 ///
16017 /// @param die the DIE to consider.
16018 ///
16019 /// @param called_from_public_decl set to yes if this function is
16020 /// called from the functions used to build a public decl (functions
16021 /// and variables). In that case, this function accepts building IR
16022 /// nodes representing types. Otherwise, this function only creates
16023 /// IR nodes representing public decls (functions and variables).
16024 /// This is done to avoid emitting IR nodes for types that are not
16025 /// referenced by public functions or variables.
16026 ///
16027 /// @param where_offset the offset of the DIE where we are "logically"
16028 /// positionned at, in the DIE tree. This is useful when @p die is
16029 /// e.g, DW_TAG_partial_unit that can be included in several places in
16030 /// the DIE tree.
16031 ///
16032 /// @return the resulting IR node.
16034 build_ir_node_from_die(reader& rdr,
16035  Dwarf_Die* die,
16036  bool called_from_public_decl,
16037  size_t where_offset)
16038 {
16039  if (!die)
16040  return decl_base_sptr();
16041 
16042  if (is_c_language(rdr.cur_transl_unit()->get_language()))
16043  {
16044  const scope_decl_sptr& scop = rdr.global_scope();
16045  return build_ir_node_from_die(rdr, die, scop.get(),
16046  called_from_public_decl,
16047  where_offset,
16048  true);
16049  }
16050 
16051  // Normaly, a decl that is meant to be external has a DW_AT_external
16052  // set. But then some compilers fail to always emit that flag. For
16053  // instance, for static data members, some compilers won't emit the
16054  // DW_AT_external. In that case, we assume that if the variable is
16055  // at global or named namespace scope, then we can assume it's
16056  // external. If the variable doesn't have any ELF symbol associated
16057  // to it, it'll be dropped on the floor anyway. Those variable
16058  // decls are considered as being "effectively public".
16059  bool consider_as_called_from_public_decl =
16060  called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16061  scope_decl_sptr scope = get_scope_for_die(rdr, die,
16062  consider_as_called_from_public_decl,
16063  where_offset);
16064  return build_ir_node_from_die(rdr, die, scope.get(),
16065  called_from_public_decl,
16066  where_offset,
16067  true);
16068 }
16069 
16070 /// Create a dwarf::reader.
16071 ///
16072 /// @param elf_path the path to the elf file the reader is to be used
16073 /// for.
16074 ///
16075 /// @param debug_info_root_paths a vector to the paths to the
16076 /// directories under which the debug info is to be found for @p
16077 /// elf_path. Pass an empty vector if the debug info is not in a
16078 /// split file.
16079 ///
16080 /// @param environment the environment used by the current context.
16081 /// This environment contains resources needed by the DWARF reader and by
16082 /// the types and declarations that are to be created later. Note
16083 /// that ABI artifacts that are to be compared all need to be created
16084 /// within the same environment.
16085 ///
16086 /// Please also note that the life time of this environment object
16087 /// must be greater than the life time of the resulting @ref
16088 /// reader the context uses resources that are allocated in the
16089 /// environment.
16090 ///
16091 /// @param load_all_types if set to false only the types that are
16092 /// reachable from publicly exported declarations (of functions and
16093 /// variables) are read. If set to true then all types found in the
16094 /// debug information are loaded.
16095 ///
16096 /// @param linux_kernel_mode if set to true, then consider the special
16097 /// linux kernel symbol tables when determining if a symbol is
16098 /// exported or not.
16099 ///
16100 /// @return a smart pointer to the resulting dwarf::reader.
16101 elf_based_reader_sptr
16102 create_reader(const std::string& elf_path,
16103  const vector<char**>& debug_info_root_paths,
16105  bool load_all_types,
16106  bool linux_kernel_mode)
16107 {
16108 
16109  reader_sptr r = reader::create(elf_path,
16110  debug_info_root_paths,
16111  environment,
16112  load_all_types,
16113  linux_kernel_mode);
16114  return static_pointer_cast<elf_based_reader>(r);
16115 }
16116 
16117 /// Re-initialize a reader so that it can re-used to read
16118 /// another binary.
16119 ///
16120 /// @param rdr the context to re-initialize.
16121 ///
16122 /// @param elf_path the path to the elf file the context is to be used
16123 /// for.
16124 ///
16125 /// @param debug_info_root_path a pointer to the path to the root
16126 /// directory under which the debug info is to be found for @p
16127 /// elf_path. Leave this to NULL if the debug info is not in a split
16128 /// file.
16129 ///
16130 /// @param environment the environment used by the current context.
16131 /// This environment contains resources needed by the DWARF reader and by
16132 /// the types and declarations that are to be created later. Note
16133 /// that ABI artifacts that are to be compared all need to be created
16134 /// within the same environment.
16135 ///
16136 /// Please also note that the life time of this environment object
16137 /// must be greater than the life time of the resulting @ref
16138 /// reader the context uses resources that are allocated in the
16139 /// environment.
16140 ///
16141 /// @param load_all_types if set to false only the types that are
16142 /// reachable from publicly exported declarations (of functions and
16143 /// variables) are read. If set to true then all types found in the
16144 /// debug information are loaded.
16145 ///
16146 /// @param linux_kernel_mode if set to true, then consider the special
16147 /// linux kernel symbol tables when determining if a symbol is
16148 /// exported or not.
16149 ///
16150 /// @return a smart pointer to the resulting dwarf::reader.
16151 void
16153  const std::string& elf_path,
16154  const vector<char**>&debug_info_root_path,
16155  bool read_all_types,
16156  bool linux_kernel_mode)
16157 {
16158  reader& r = dynamic_cast<reader&>(rdr);
16159  r.initialize(elf_path, debug_info_root_path,
16160  read_all_types, linux_kernel_mode);
16161 }
16162 
16163 /// Read all @ref abigail::translation_unit possible from the debug info
16164 /// accessible from an elf file, stuff them into a libabigail ABI
16165 /// Corpus and return it.
16166 ///
16167 /// @param elf_path the path to the elf file.
16168 ///
16169 /// @param debug_info_root_paths a vector of pointers to root paths
16170 /// under which to look for the debug info of the elf files that are
16171 /// later handled by the Dwfl. This for cases where the debug info is
16172 /// split into a different file from the binary we want to inspect.
16173 /// On Red Hat compatible systems, this root path is usually
16174 /// /usr/lib/debug by default. If this argument is set to NULL, then
16175 /// "./debug" and /usr/lib/debug will be searched for sub-directories
16176 /// containing the debug info file.
16177 ///
16178 /// @param environment the environment used by the current context.
16179 /// This environment contains resources needed by the DWARF reader and by
16180 /// the types and declarations that are to be created later. Note
16181 /// that ABI artifacts that are to be compared all need to be created
16182 /// within the same environment. Also, the lifetime of the
16183 /// environment must be greater than the lifetime of the resulting
16184 /// corpus because the corpus uses resources that are allocated in the
16185 /// environment.
16186 ///
16187 /// @param load_all_types if set to false only the types that are
16188 /// reachable from publicly exported declarations (of functions and
16189 /// variables) are read. If set to true then all types found in the
16190 /// debug information are loaded.
16191 ///
16192 /// @param resulting_corp a pointer to the resulting abigail::corpus.
16193 ///
16194 /// @return the resulting status.
16195 corpus_sptr
16196 read_corpus_from_elf(const std::string& elf_path,
16197  const vector<char**>& debug_info_root_paths,
16199  bool load_all_types,
16200  fe_iface::status& status)
16201 {
16202  elf_based_reader_sptr rdr =
16203  dwarf::reader::create(elf_path, debug_info_root_paths,
16204  environment, load_all_types,
16205  /*linux_kernel_mode=*/false);
16206 
16207  return rdr->read_corpus(status);
16208 }
16209 
16210 /// Look into the symbol tables of a given elf file and see if we find
16211 /// a given symbol.
16212 ///
16213 /// @param env the environment we are operating from.
16214 ///
16215 /// @param elf_path the path to the elf file to consider.
16216 ///
16217 /// @param symbol_name the name of the symbol to look for.
16218 ///
16219 /// @param demangle if true, try to demangle the symbol name found in
16220 /// the symbol table.
16221 ///
16222 /// @param syms the vector of symbols found with the name @p symbol_name.
16223 ///
16224 /// @return true iff the symbol was found among the publicly exported
16225 /// symbols of the ELF file.
16226 bool
16227 lookup_symbol_from_elf(const environment& env,
16228  const string& elf_path,
16229  const string& symbol_name,
16230  bool demangle,
16231  vector<elf_symbol_sptr>& syms)
16232 
16233 {
16234  if (elf_version(EV_CURRENT) == EV_NONE)
16235  return false;
16236 
16237  int fd = open(elf_path.c_str(), O_RDONLY);
16238  if (fd < 0)
16239  return false;
16240 
16241  struct stat s;
16242  if (fstat(fd, &s))
16243  return false;
16244 
16245  Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16246  if (elf == 0)
16247  return false;
16248 
16249  bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16250  demangle, syms);
16251  elf_end(elf);
16252  close(fd);
16253 
16254  return value;
16255 }
16256 
16257 /// Look into the symbol tables of an elf file to see if a public
16258 /// function of a given name is found.
16259 ///
16260 /// @param env the environment we are operating from.
16261 ///
16262 /// @param elf_path the path to the elf file to consider.
16263 ///
16264 /// @param symbol_name the name of the function to look for.
16265 ///
16266 /// @param syms the vector of public function symbols found with the
16267 /// name @p symname.
16268 ///
16269 /// @return true iff a function with symbol name @p symbol_name is
16270 /// found.
16271 bool
16273  const string& path,
16274  const string& symname,
16275  vector<elf_symbol_sptr>& syms)
16276 {
16277  if (elf_version(EV_CURRENT) == EV_NONE)
16278  return false;
16279 
16280  int fd = open(path.c_str(), O_RDONLY);
16281  if (fd < 0)
16282  return false;
16283 
16284  struct stat s;
16285  if (fstat(fd, &s))
16286  return false;
16287 
16288  Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16289  if (elf == 0)
16290  return false;
16291 
16292  bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16293  elf_end(elf);
16294  close(fd);
16295 
16296  return value;
16297 }
16298 
16299 }// end namespace dwarf
16300 
16301 }// end namespace abigail
The private data and functions of the abigail::ir::corpus type.
#define ABG_RETURN_FALSE
A macro used to return the "false" boolean from DIE comparison routines.
#define SET_RESULT_TO(result, value, l, r)
A macro to set the 'result' variable to a given value.
#define SET_RESULT_TO_FALSE(result, l, r)
A macro to set the 'result' variable to 'false'.
#define ABG_RETURN(value)
A macro used to return from DIE comparison routines.
This file contains the declarations of the entry points to de-serialize an instance of abigail::corpu...
This file contains the declarations for an elf-based. DWARF and CTF readers can inherit this one.
bool architecture_is_big_endian(Elf *elf_handle)
Test if the endianness of the current binary is Big Endian.
bool get_binary_load_address(Elf *elf_handle, GElf_Addr &load_address)
Get the address at which a given binary is loaded in memory.
bool get_version_for_symbol(Elf *elf_handle, size_t symbol_index, bool get_def_version, elf_symbol::version &version)
Return the version for a symbol that is at a given index in its SHT_SYMTAB section.
elf_symbol::binding stb_to_elf_symbol_binding(unsigned char stb)
Convert an elf symbol binding (given by the ELF{32,64}_ST_BIND macros) into an elf_symbol::binding va...
elf_symbol::visibility stv_to_elf_symbol_visibility(unsigned char stv)
Convert an ELF symbol visiblity given by the symbols ->st_other data member as returned by the GELF_S...
bool find_symbol_table_section_index(Elf *elf_handle, size_t &symtab_index)
Find the index (in the section headers table) of the symbol table section.
elf_symbol::type stt_to_elf_symbol_type(unsigned char stt)
Convert an elf symbol type (given by the ELF{32,64}_ST_TYPE macros) into an elf_symbol::type value.
hash_table_kind find_hash_table_section_index(Elf *elf_handle, size_t &ht_section_index, size_t &symtab_section_index)
Get the offset offset of the hash table section.
This contains a set of ELF utilities used by the dwarf reader.
#define ABG_ASSERT(cond)
This is a wrapper around the 'assert' glibc call. It allows for its argument to have side effects,...
Definition: abg-fwd.h:1589
This contains the private implementation of the suppression engine of libabigail.
Utilities to ease the wrapping of C types into std::shared_ptr.
This contains the private implementation of the suppression engine of libabigail.
This contains the declarations for the symtab reader.
#define ABG_ASSERT_NOT_REACHED
A macro that expands to aborting the program when executed.
Simplified implementation of std::optional just enough to be used as a replacement for our purposes a...
virtual ir::corpus_sptr read_corpus(status &status)
Read the ELF information associated to the current ELF file and construct an ABI representation from ...
The common interface of readers based on ELF.
status
The status of the fe_iface::read_corpus call.
Definition: abg-fe-iface.h:38
The abstraction of an interned string.
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition: abg-ir.h:2460
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2467
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4155
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:45
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:4865
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1171
binding
The binding of a symbol.
Definition: abg-ir.h:917
type
The type of a symbol.
Definition: abg-ir.h:904
static elf_symbol_sptr create(const environment &e, size_t i, size_t s, const string &n, type t, binding b, bool d, bool c, const version &ve, visibility vi, bool is_in_ksymtab=false, const abg_compat::optional< uint32_t > &crc={}, const abg_compat::optional< std::string > &ns={}, bool is_suppressed=false)
Factory of instances of elf_symbol.
Definition: abg-ir.cc:1902
visibility
The visibility of the symbol.
Definition: abg-ir.h:926
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2690
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:140
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3044
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3051
The internal representation of an integral type.
Definition: abg-ir-priv.h:46
string to_string(bool internal=false) const
Return the string representation of the current instance of integral_type.
Definition: abg-ir.cc:15414
The source location of a token.
Definition: abg-ir.h:290
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2223
language
The language of the translation unit.
Definition: abg-ir.h:676
visiting_kind operator~(visiting_kind l)
The overloaded 'bit inversion' operator for visiting_kind.
unordered_map< std::pair< offset_type, offset_type >, offset_pair_set_type, offset_pair_hash > offset_pair_set_map_type
A convenience typedef for an unordered_map that associates a pair of offset_type to a set of pairs of...
unordered_map< Dwarf_Off, translation_unit_sptr > die_tu_map_type
Convenience typedef for a map which key is the offset of a DW_TAG_compile_unit and the value is the c...
bool lookup_symbol_from_elf(const environment &env, const string &elf_path, const string &symbol_name, bool demangle, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of a given elf file and see if we find a given symbol.
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)
Re-initialize a reader so that it can re-used to read another binary.
unordered_map< string, classes_type > string_classes_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
std::pair< offset_type, offset_type > offset_pair_type
A convenience typedef for a pair of offset_type.
elf_symbol_sptr create_default_fn_sym(const string &sym_name, const environment &env)
Create a function symbol with a given name.
shared_ptr< addr_elf_symbol_sptr_map_type > addr_elf_symbol_sptr_map_sptr
Convenience typedef for a shared pointer to an addr_elf_symbol_sptr_map_type.
unordered_map< Dwarf_Off, class_or_union_sptr > die_class_or_union_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
bool is_anonymous_type_die(Dwarf_Die *die)
Test if a given DIE represents an anonymous type.
unordered_map< Dwarf_Off, function_type_sptr > die_function_type_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< Dwarf_Off, interned_string > die_istring_map_type
Convenience typedef for a map which key is the offset of a DIE and the value is the corresponding qua...
unordered_map< Dwarf_Off, function_decl_sptr > die_function_decl_map_type
Convenience typedef for a map which key the offset of a dwarf die and which value is the correspondin...
unordered_set< offset_type, offset_hash > offset_set_type
A convenience typedef for an unordered set of DIE offsets.
unordered_map< Dwarf_Off, type_or_decl_base_sptr > die_artefact_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< std::pair< offset_type, offset_type >, offset_pair_vector_type, offset_pair_hash > offset_pair_vect_map_type
A convenience typedef for an unordered map that associates a pair of offset_type to a vector of pairs...
unordered_map< interned_string, function_type_sptr, hash_interned_string > istring_fn_type_map_type
Convenience typedef for a map that associates an interned_string to a function_type_sptr.
unordered_map< Dwarf_Off, class_decl_sptr > die_class_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
unordered_map< string, classes_or_unions_type > string_classes_or_unions_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
unordered_map< Dwarf_Off, imported_unit_points_type > tu_die_imported_unit_points_map_type
Convenience typedef for a vector of imported_unit_point.
vector< Dwarf_Off > dwarf_offsets_type
A convenience typedef for a vector of Dwarf_Off.
unordered_map< Dwarf_Off, Dwarf_Off > offset_offset_map_type
Convenience typedef for a map which key is a dwarf offset. The value is also a dwarf offset.
unordered_set< std::pair< offset_type, offset_type >, offset_pair_hash > offset_pair_set_type
A convenience typedef for an unordered set of pairs of offset_type.
unordered_map< string, enums_type > string_enums_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
stack< scope_decl * > scope_stack_type
Convenience typedef for a stack containing the scopes up to the current point in the abigail Internal...
vector< std::pair< offset_type, offset_type > > offset_pair_vector_type
A convenience typedef for a vector of pairs of offset_type.
bool lookup_public_function_symbol_from_elf(environment &env, const string &path, const string &symname, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of an elf file to see if a public function of a given name is found.
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)
Create a dwarf::reader.
die_source
Where a DIE comes from. For instance, a DIE can come from the main debug info section,...
unordered_map< interned_string, dwarf_offsets_type, hash_interned_string > istring_dwarf_offsets_map_type
Convenience typedef for a map which is an interned_string and which value is a vector of offsets.
vector< imported_unit_point > imported_unit_points_type
Convenience typedef for a vector of imported_unit_point.
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)
Read all abigail::translation_unit possible from the debug info accessible from an elf file,...
reference_type_def_sptr lookup_reference_type(const interned_string &type_name, const translation_unit &tu)
Lookup a reference type from a translation unit.
Definition: abg-ir.cc:11593
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:229
type_decl_sptr lookup_basic_type(const interned_string &type_name, const translation_unit &tu)
Lookup a basic type from a translation unit.
Definition: abg-ir.cc:11262
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:215
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:263
access_specifier
Access specifier for class members.
Definition: abg-ir.h:856
const type_base_wptrs_type * lookup_enum_types(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find the enum type*s* that have a given qualified name.
Definition: abg-ir.cc:12843
type_base_sptr lookup_class_or_typedef_type(const string &qualified_name, const corpus &corp)
Look into a corpus to find a class, union or typedef type which has a given qualified name.
Definition: abg-ir.cc:13005
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6659
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition: abg-fwd.h:143
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10194
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5472
array_type_def::subrange_type * is_subrange_type(const type_or_decl_base *type)
Test if a type is an array_type_def::subrange_type.
Definition: abg-ir.cc:11032
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:863
class_decl_sptr is_compatible_with_class_type(const type_base_sptr &t)
Test if a type is a class. This function looks through typedefs.
Definition: abg-ir.cc:10417
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8317
bool odr_is_relevant(const type_or_decl_base &artifact)
By looking at the language of the TU a given ABI artifact belongs to, test if the ONE Definition Rule...
Definition: abg-ir.cc:9808
comparison_result
The result of structural comparison of type ABI artifacts.
Definition: abg-ir-priv.h:33
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:10449
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:234
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10245
type_base_sptr lookup_class_typedef_or_enum_type(const string &qualified_name, const corpus &corp)
Look into a corpus to find a class, typedef or enum type which has a given qualified name.
Definition: abg-ir.cc:13030
array_type_def * is_array_type(const type_or_decl_base *type)
Test if a type is an array_type_def.
Definition: abg-ir.cc:10954
reference_type_def * is_reference_type(type_or_decl_base *t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:10599
bool anonymous_data_member_exists_in_class(const var_decl &anon_dm, const class_or_union &clazz)
Test if a given anonymous data member exists in a class or union.
Definition: abg-ir.cc:6078
class_decl_sptr lookup_class_type_per_location(const interned_string &loc, const corpus &corp)
Look up a class_decl from a given corpus by its location.
Definition: abg-ir.cc:12706
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6462
class_or_union * is_class_or_union_type(const type_or_decl_base *t)
Test if a type is a class_or_union.
Definition: abg-ir.cc:10509
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:187
void canonicalize_types(const input_iterator &begin, const input_iterator &end, deref_lambda deref)
Compute the canonical type for all the IR types of the system.
Definition: abg-ir-priv.h:1314
type_base_sptr peel_typedef_pointer_or_reference_type(const type_base_sptr type)
Return the leaf underlying or pointed-to type node of a typedef_decl, pointer_type_def,...
Definition: abg-ir.cc:7291
void set_member_function_is_const(function_decl &f, bool is_const)
set the const-ness property of a member function.
Definition: abg-ir.cc:6518
decl_base_sptr strip_useless_const_qualification(const qualified_type_def_sptr t)
Strip qualification from a qualified type, when it makes sense.
Definition: abg-ir.cc:6846
void set_member_function_is_ctor(function_decl &f, bool c)
Setter for the is_ctor property of the member function.
Definition: abg-ir.cc:6405
const type_decl * is_type_decl(const type_or_decl_base *t)
Test whether a type is a type_decl (a builtin type).
Definition: abg-ir.cc:10278
function_type_sptr is_function_type(const type_or_decl_base_sptr &t)
Test whether a type is a function_type.
Definition: abg-ir.cc:10689
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5617
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10336
enum_type_decl_sptr lookup_enum_type_per_location(const interned_string &loc, const corpus &corp)
Look up an enum_type_decl from a given corpus, by its location.
Definition: abg-ir.cc:12873
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:205
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:161
class_decl_sptr lookup_class_type(const string &fqn, const translation_unit &tu)
Lookup a class type from a translation unit.
Definition: abg-ir.cc:11302
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1651
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:15332
const enum_type_decl * is_enum_type(const type_or_decl_base *d)
Test if a decl is an enum_type_decl.
Definition: abg-ir.cc:10399
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8386
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:246
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:258
shared_ptr< type_or_decl_base > type_or_decl_base_sptr
A convenience typedef for a shared_ptr to type_or_decl_base.
Definition: abg-fwd.h:119
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition: abg-fwd.h:134
const decl_base_sptr lookup_var_decl_in_scope(const string &fqn, const scope_decl_sptr &skope)
Lookup a var_decl in a scope.
Definition: abg-ir.cc:11792
type_base * type_has_non_canonicalized_subtype(type_base_sptr t)
Test if a type has sub-types that are non-canonicalized.
Definition: abg-ir.cc:26284
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1665
union_decl_sptr lookup_union_type(const interned_string &type_name, const translation_unit &tu)
Lookup a union type from a translation unit.
Definition: abg-ir.cc:11339
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:14839
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:220
pointer_type_def * is_pointer_type(type_or_decl_base *t)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:10558
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6348
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1637
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10134
method_decl * is_method_decl(const type_or_decl_base *d)
Test if a function_decl is actually a method_decl.
Definition: abg-ir.cc:23624
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5537
decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl *scope)
Appends a declaration to a given scope, if the declaration doesn't already belong to one.
Definition: abg-ir.cc:8293
string build_internal_underlying_enum_type_name(const string &base_name, bool is_anonymous, uint64_t size)
Build the internal name of the underlying type of an enum.
Definition: abg-ir.cc:26874
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5588
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:169
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6621
class_or_union * look_through_decl_only_class(class_or_union *the_class)
If a class (or union) is a decl-only class, get its definition. Otherwise, just return the initial cl...
Definition: abg-ir.cc:10749
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:10528
const type_base_wptrs_type * lookup_union_types(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find the union type*s* that have a given qualified name.
Definition: abg-ir.cc:12661
array_type_def_sptr is_typedef_of_array(const type_base_sptr &t)
Test if a type is a typedef of an array.
Definition: abg-ir.cc:11011
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5686
const type_base_wptrs_type * lookup_class_types(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find the class type*s* that have a given qualified name.
Definition: abg-ir.cc:12610
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:9827
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:24747
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:156
void strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr &t)
Merge redundant qualifiers from a tree of qualified types.
Definition: abg-ir.cc:6969
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:282
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1674
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:14206
string components_to_type_name(const list< string > &comps)
Turn a set of qualified name components (that name a type) into a qualified name string.
Definition: abg-ir.cc:11152
interned_string get_type_name(const type_base_sptr &t, bool qualified, bool internal)
Get the name of a given type and return a copy of it.
Definition: abg-ir.cc:8678
type_base_sptr clone_array_tree(const type_base_sptr t)
Clone a type tree made of an array or a typedef of array.
Definition: abg-ir.cc:7550
typedef_decl_sptr lookup_typedef_type_per_location(const interned_string &loc, const corpus &corp)
Lookup a typedef_decl from a corpus, by its location.
Definition: abg-ir.cc:12968
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10082
method_type_sptr is_method_type(const type_or_decl_base_sptr &t)
Test whether a type is a method_type.
Definition: abg-ir.cc:10719
qualified_type_def * is_qualified_type(const type_or_decl_base *t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:10669
union_decl_sptr lookup_union_type_per_location(const interned_string &loc, const corpus &corp)
Lookup a union type in a given corpus, from its location.
Definition: abg-ir.cc:11372
string build_qualified_name(const scope_decl *scope, const string &name)
Build and return a qualified name from a name and its scope.
Definition: abg-ir.cc:8575
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6591
enum_type_decl_sptr look_through_decl_only_enum(const enum_type_decl &the_enum)
If an enum is a decl-only enum, get its definition. Otherwise, just return the initial enum.
Definition: abg-ir.cc:10779
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5490
void fqn_to_components(const string &fqn, list< string > &comps)
Decompose a fully qualified name into the list of its components.
Definition: abg-ir.cc:11126
bool is_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_private, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
bool is_function_suppressed(const fe_iface &fe, const string &fn_name, const string &fn_linkage_name, bool require_drop_property)
Test if a function is matched by at least one suppression specification associated with a given front...
bool is_variable_suppressed(const fe_iface &fe, const string &var_name, const string &var_linkage_name, bool require_drop_property)
Test if a variable is matched by at least one suppression specification associated with a given front...
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
Toplevel namespace for libabigail.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition: abg-ir.cc:186
fe_iface::status operator&(fe_iface::status l, fe_iface::status r)
The bitwise AND operator for the fe_iface::status type.
fe_iface::status operator|(fe_iface::status l, fe_iface::status r)
The bitwise OR operator for the fe_iface::status type.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:152
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:169
A functor to hash instances of interned_string.